3 # Generate the file opcodes.h.
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
9 # The lines of the vdbe.c that we are interested in are of the form:
11 # case OP_aaaa: /* same as TK_bbbbb */
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.
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.
26 # This script also scans for lines of the form:
28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */
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.
36 # Remember the TK_ values from the parse.h file
38 tk
[$
2] =
0+$
3 # tk[x] holds the numeric value for TK symbol X
41 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces
42 # a new opcode. Remember which parameters are used.
54 paramused
[currentOp
] = m
57 # Find "** Synopsis: " lines that follow Opcode:
64 synopsis
[currentOp
] = x
68 # Scan for "case OP_aaaa:" lines in the vdbe.c file
73 op
[name
] =
-1 # op[x] holds the numeric value for OP symbol x
75 out2_prerelease
[name
] =
0
82 if($i==
"same" && $
(i
+1)==
"as"){
95 }else if(x==
"out2-prerelease"){
96 out2_prerelease
[name
] =
1
109 order
[n_op
++] = name
;
112 # Assign numbers to all opcodes and output the result.
116 print "/* Automatically generated. Do not edit */"
117 print "/* See the mkopcodeh.awk script for details */"
119 order
[n_op
++] =
"OP_Noop";
120 op
["OP_Explain"] =
-1;
121 order
[n_op
++] =
"OP_Explain";
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
++){
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" \
139 || name==
"OP_NextIfOpen" \
140 || name==
"OP_SorterNext" \
142 || name==
"OP_PrevIfOpen" \
145 while( used
[cnt
] ) cnt
++
152 # Generate the numeric values for opcodes
153 for(i=
0; i
<n_op
; i
++){
157 while( used
[cnt
] ) cnt
++
164 for(i=
1; i
<=max
; i
++){
166 def
[i
] =
"OP_NotUsed_" i
168 printf "#define %-16s %3d", def
[i
], i
171 com =
"same as " sameas
[i
]
178 com = com
", synopsis: " x
182 printf " /* %-42s */", com
187 # Generate the bitvectors:
190 # bit 1: pushes a result onto stack
191 # bit 2: output to p1. release p1 before opcode runs
193 for(i=
0; i
<=max
; 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
;
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:"
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");
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");