OLD | NEW |
| (Empty) |
1 #!/usr/bin/awk -f | |
2 # | |
3 # Generate the file opcodes.h. | |
4 # | |
5 # This AWK script scans a concatenation of the parse.h output file from the | |
6 # parser and the vdbe.c source file in order to generate the opcodes numbers | |
7 # for all opcodes. | |
8 # | |
9 # The lines of the vdbe.c that we are interested in are of the form: | |
10 # | |
11 # case OP_aaaa: /* same as TK_bbbbb */ | |
12 # | |
13 # The TK_ comment is optional. If it is present, then the value assigned to | |
14 # the OP_ is the same as the TK_ value. If missing, the OP_ value is assigned | |
15 # a small integer that is different from every other OP_ value. | |
16 # | |
17 # We go to the trouble of making some OP_ values the same as TK_ values | |
18 # as an optimization. During parsing, things like expression operators | |
19 # are coded with TK_ values such as TK_ADD, TK_DIVIDE, and so forth. Later | |
20 # during code generation, we need to generate corresponding opcodes like | |
21 # OP_Add and OP_Divide. By making TK_ADD==OP_Add and TK_DIVIDE==OP_Divide, | |
22 # code to translate from one to the other is avoided. This makes the | |
23 # code generator run (infinitesimally) faster and more importantly it makes | |
24 # the library footprint smaller. | |
25 # | |
26 # This script also scans for lines of the form: | |
27 # | |
28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ | |
29 # | |
30 # When such comments are found on an opcode, it means that certain | |
31 # properties apply to that opcode. Set corresponding flags using the | |
32 # OPFLG_INITIALIZER macro. | |
33 # | |
34 | |
35 | |
36 # Remember the TK_ values from the parse.h file | |
37 /^#define TK_/ { | |
38 tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X | |
39 } | |
40 | |
41 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces | |
42 # a new opcode. Remember which parameters are used. | |
43 /^.. Opcode: / { | |
44 currentOp = "OP_" $3 | |
45 m = 0 | |
46 for(i=4; i<=NF; i++){ | |
47 x = $i | |
48 if( x=="P1" ) m += 1 | |
49 if( x=="P2" ) m += 2 | |
50 if( x=="P3" ) m += 4 | |
51 if( x=="P4" ) m += 8 | |
52 if( x=="P5" ) m += 16 | |
53 } | |
54 paramused[currentOp] = m | |
55 } | |
56 | |
57 # Find "** Synopsis: " lines that follow Opcode: | |
58 /^.. Synopsis: / { | |
59 if( currentOp ){ | |
60 x = $3 | |
61 for(i=4; i<=NF; i++){ | |
62 x = x " " $i | |
63 } | |
64 synopsis[currentOp] = x | |
65 } | |
66 } | |
67 | |
68 # Scan for "case OP_aaaa:" lines in the vdbe.c file | |
69 /^case OP_/ { | |
70 name = $2 | |
71 sub(/:/,"",name) | |
72 sub("\r","",name) | |
73 op[name] = -1 # op[x] holds the numeric value for OP symbol x | |
74 jump[name] = 0 | |
75 out2_prerelease[name] = 0 | |
76 in1[name] = 0 | |
77 in2[name] = 0 | |
78 in3[name] = 0 | |
79 out2[name] = 0 | |
80 out3[name] = 0 | |
81 for(i=3; i<NF; i++){ | |
82 if($i=="same" && $(i+1)=="as"){ | |
83 sym = $(i+2) | |
84 sub(/,/,"",sym) | |
85 val = tk[sym] | |
86 op[name] = val | |
87 used[val] = 1 | |
88 sameas[val] = sym | |
89 def[val] = name | |
90 } | |
91 x = $i | |
92 sub(",","",x) | |
93 if(x=="jump"){ | |
94 jump[name] = 1 | |
95 }else if(x=="out2-prerelease"){ | |
96 out2_prerelease[name] = 1 | |
97 }else if(x=="in1"){ | |
98 in1[name] = 1 | |
99 }else if(x=="in2"){ | |
100 in2[name] = 1 | |
101 }else if(x=="in3"){ | |
102 in3[name] = 1 | |
103 }else if(x=="out2"){ | |
104 out2[name] = 1 | |
105 }else if(x=="out3"){ | |
106 out3[name] = 1 | |
107 } | |
108 } | |
109 order[n_op++] = name; | |
110 } | |
111 | |
112 # Assign numbers to all opcodes and output the result. | |
113 END { | |
114 cnt = 0 | |
115 max = 0 | |
116 print "/* Automatically generated. Do not edit */" | |
117 print "/* See the mkopcodeh.awk script for details */" | |
118 op["OP_Noop"] = -1; | |
119 order[n_op++] = "OP_Noop"; | |
120 op["OP_Explain"] = -1; | |
121 order[n_op++] = "OP_Explain"; | |
122 | |
123 # Assign small values to opcodes that are processed by resolveP2Values() | |
124 # to make code generation for the switch() statement smaller and faster. | |
125 for(i=0; i<n_op; i++){ | |
126 name = order[i]; | |
127 if( op[name]>=0 ) continue; | |
128 if( name=="OP_Function" \ | |
129 || name=="OP_AggStep" \ | |
130 || name=="OP_Transaction" \ | |
131 || name=="OP_AutoCommit" \ | |
132 || name=="OP_Savepoint" \ | |
133 || name=="OP_Checkpoint" \ | |
134 || name=="OP_Vacuum" \ | |
135 || name=="OP_JournalMode" \ | |
136 || name=="OP_VUpdate" \ | |
137 || name=="OP_VFilter" \ | |
138 || name=="OP_Next" \ | |
139 || name=="OP_NextIfOpen" \ | |
140 || name=="OP_SorterNext" \ | |
141 || name=="OP_Prev" \ | |
142 || name=="OP_PrevIfOpen" \ | |
143 ){ | |
144 cnt++ | |
145 while( used[cnt] ) cnt++ | |
146 op[name] = cnt | |
147 used[cnt] = 1 | |
148 def[cnt] = name | |
149 } | |
150 } | |
151 | |
152 # Generate the numeric values for opcodes | |
153 for(i=0; i<n_op; i++){ | |
154 name = order[i]; | |
155 if( op[name]<0 ){ | |
156 cnt++ | |
157 while( used[cnt] ) cnt++ | |
158 op[name] = cnt | |
159 used[cnt] = 1 | |
160 def[cnt] = name | |
161 } | |
162 } | |
163 max = cnt | |
164 for(i=1; i<=max; i++){ | |
165 if( !used[i] ){ | |
166 def[i] = "OP_NotUsed_" i | |
167 } | |
168 printf "#define %-16s %3d", def[i], i | |
169 com = "" | |
170 if( sameas[i] ){ | |
171 com = "same as " sameas[i] | |
172 } | |
173 x = synopsis[def[i]] | |
174 if( x ){ | |
175 if( com=="" ){ | |
176 com = "synopsis: " x | |
177 } else { | |
178 com = com ", synopsis: " x | |
179 } | |
180 } | |
181 if( com!="" ){ | |
182 printf " /* %-42s */", com | |
183 } | |
184 printf "\n" | |
185 } | |
186 | |
187 # Generate the bitvectors: | |
188 # | |
189 # bit 0: jump | |
190 # bit 1: pushes a result onto stack | |
191 # bit 2: output to p1. release p1 before opcode runs | |
192 # | |
193 for(i=0; i<=max; i++){ | |
194 name = def[i] | |
195 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 | |
196 if( jump[name] ) a0 = 1; | |
197 if( out2_prerelease[name] ) a1 = 2; | |
198 if( in1[name] ) a2 = 4; | |
199 if( in2[name] ) a3 = 8; | |
200 if( in3[name] ) a4 = 16; | |
201 if( out2[name] ) a5 = 32; | |
202 if( out3[name] ) a6 = 64; | |
203 bv[i] = a0+a1+a2+a3+a4+a5+a6+a7; | |
204 } | |
205 print "\n" | |
206 print "/* Properties such as \"out2\" or \"jump\" that are specified in" | |
207 print "** comments following the \"case\" for each opcode in the vdbe.c" | |
208 print "** are encoded into bitvectors as follows:" | |
209 print "*/" | |
210 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" | |
211 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" | |
212 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" | |
213 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" | |
214 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" | |
215 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */" | |
216 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */" | |
217 print "#define OPFLG_INITIALIZER {\\" | |
218 for(i=0; i<=max; i++){ | |
219 if( i%8==0 ) printf("/* %3d */",i) | |
220 printf " 0x%02x,", bv[i] | |
221 if( i%8==7 ) printf("\\\n"); | |
222 } | |
223 print "}" | |
224 if( 0 ){ | |
225 print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are" | |
226 print "** actually used.\n*/" | |
227 print "#define OP_PARAM_USED_INITIALIZER {\\" | |
228 for(i=0; i<=max; i++){ | |
229 if( i%8==0 ) printf("/* %3d */",i) | |
230 printf " 0x%02x,", paramused[def[i]] | |
231 if( i%8==7 ) printf("\\\n"); | |
232 } | |
233 print "}" | |
234 } | |
235 } | |
OLD | NEW |