3 # Generate the file opcodes.h.
5 # This TCL 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 smaller and faster.
25 # This script also scans for lines of the form:
27 # case OP_aaaa: /* jump, in1, in2, in3, out2, out3 */
29 # When such comments are found on an opcode, it means that certain
30 # properties apply to that opcode. Set corresponding flags using the
31 # OPFLG_INITIALIZER macro.
42 # Remember the TK_ values from the parse.h file.
43 # NB: The "TK_" prefix stands for "ToKen", not the graphical Tk toolkit
44 # commonly associated with TCL.
46 if {[regexp {^
#define TK_} $line]} {
47 set tk([lindex $line 1]) [lindex $line 2]
51 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces
52 # a new opcode. Remember which parameters are used.
54 if {[regexp {^.. Opcode
: } $line]} {
55 set currentOp OP_
[lindex $line 2]
66 set paramused
($currentOp) $m
69 # Find "** Synopsis: " lines that follow Opcode:
71 if {[regexp {^.. Synopsis
: (.
*)} $line all x
] && $currentOp!=""} {
72 set synopsis
($currentOp) [string trim
$x]
75 # Scan for "case OP_aaaa:" lines in the vdbe.c file
77 if {[regexp {^case OP_
} $line]} {
78 set line
[split $line]
79 set name
[string trim
[lindex $line 1] :]
80 if {$name=="OP_Abortable"} continue; # put OP_Abortable last
90 for {set i
3} {$i<[llength $line]-1} {incr i
} {
91 switch [string trim
[lindex $line $i] ,] {
94 if {[lindex $line $i]=="as"} {
96 set sym
[string trim
[lindex $line $i] ,]
100 set sameas
($val) $sym
104 group
{set group
($name) 1}
105 jump
{set jump
($name) 1}
106 in1
{set in1
($name) 1}
107 in2
{set in2
($name) 1}
108 in3
{set in3
($name) 1}
109 out2
{set out2
($name) 1}
110 out3
{set out3
($name) 1}
111 ncycle
{set ncycle
($name) 1}
116 if {[info exists groups
($nGroup)]} {
117 if {$prevName=="" ||
!$group($prevName)} {
121 lappend groups
($nGroup) $name
122 if {$newGroup} {incr nGroup
}
124 if {$prevName!="" && $group($prevName)} {
128 set order
($nOp) $name
134 # Assign numbers to all opcodes and output the result.
136 puts "/* Automatically generated. Do not edit */"
137 puts "/* See the tool/mkopcodeh.tcl script for details */"
138 foreach name
{OP_Noop OP_Explain OP_Abortable
} {
147 set order
($nOp) $name
151 # The following are the opcodes that receive special processing in the
152 # resolveP2Values() routine. Update this list whenever new cases are
153 # added to the pOp->opcode switch within resolveP2Values().
167 # Assign the smallest values to opcodes that are processed by resolveP2Values()
168 # to make code generation for the switch() statement smaller and faster.
171 for {set i
0} {$i<$nOp} {incr i
} {
173 if {[lsearch $rp2v_ops $name]>=0} {
175 while {[info exists used
($cnt)]} {incr cnt
}
183 # Assign the next group of values to JUMP opcodes
185 for {set i
0} {$i<$nOp} {incr i
} {
187 if {$op($name)>=0} continue
188 if {!$jump($name)} continue
190 while {[info exists used
($cnt)]} {incr cnt
}
196 # Find the numeric value for the largest JUMP opcode
199 for {set i
0} {$i<$nOp} {incr i
} {
201 if {$jump($name) && $op($name)>$mxJump} {set mxJump
$op($name)}
205 # Generate the numeric values for all remaining opcodes, while
206 # preserving any groupings of opcodes (i.e. those that must be
209 for {set g
0} {$g<$nGroup} {incr g
} {
210 set gLen
[llength $groups($g)]
211 set ok
0; set start
-1
215 while {[info exists used
($seek)]} {incr seek}
216 set ok
1; set start
$seek
217 for {set j
0} {$j<$gLen} {incr j
} {
219 if {[info exists used
($seek)]} {
226 for {set j
0} {$j<$gLen} {incr j
} {
227 set name
[lindex $groups($g) $j]
228 if {$op($name)>=0} continue
235 error "cannot find opcodes for group: $groups($g)"
239 for {set i
0} {$i<$nOp} {incr i
} {
243 while {[info exists used
($cnt)]} {incr cnt
}
250 set max
[lindex [lsort -decr -integer [array names used
]] 0]
251 for {set i
0} {$i<=$max} {incr i
} {
252 if {![info exists used
($i)]} {
253 set def
($i) "OP_NotUsed_$i"
255 if {$i>$max} {set max
$i}
257 puts -nonewline [format {#define %-16s %3d} $name $i]
259 if {[info exists jump
($name)] && $jump($name)} {
262 if {[info exists sameas
($i)]} {
263 lappend com
"same as $sameas($i)"
265 if {[info exists synopsis
($name)]} {
266 lappend com
"synopsis: $synopsis($name)"
268 if {[llength $com]} {
269 puts -nonewline [format " /* %-42s */" [join $com {, }]]
275 error "More than 255 opcodes - VdbeOp.opcode is of type u8!"
278 # Generate the bitvectors:
281 for {set i
0} {$i<=$max} {incr i
} {
284 if {[string match OP_NotUsed
* $name]==0} {
285 if {$jump($name)} {incr x
1}
286 if {$in1($name)} {incr x
2}
287 if {$in2($name)} {incr x
4}
288 if {$in3($name)} {incr x
8}
289 if {$out2($name)} {incr x
16}
290 if {$out3($name)} {incr x
32}
291 if {$ncycle($name)} {incr x
64}
296 puts "/* Properties such as \"out2\" or \"jump\" that are specified in"
297 puts "** comments following the \"case\" for each opcode in the vdbe.c"
298 puts "** are encoded into bitvectors as follows:"
300 puts "#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */"
301 puts "#define OPFLG_IN1 0x02 /* in1: P1 is an input */"
302 puts "#define OPFLG_IN2 0x04 /* in2: P2 is an input */"
303 puts "#define OPFLG_IN3 0x08 /* in3: P3 is an input */"
304 puts "#define OPFLG_OUT2 0x10 /* out2: P2 is an output */"
305 puts "#define OPFLG_OUT3 0x20 /* out3: P3 is an output */"
306 puts "#define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */"
307 puts "#define OPFLG_INITIALIZER \173\\"
308 for {set i
0} {$i<=$max} {incr i
} {
310 puts -nonewline [format "/* %3d */" $i]
312 puts -nonewline [format " 0x%02x," $bv($i)]
319 puts "/* The resolve3P2Values() routine is able to run faster if it knows"
320 puts "** the value of the largest JUMP opcode. The smaller the maximum"
321 puts "** JUMP opcode the better, so the mkopcodeh.tcl script that"
322 puts "** generated this include file strives to group all JUMP opcodes"
323 puts "** together near the beginning of the list."
325 puts "#define SQLITE_MX_JUMP_OPCODE $mxJump /* Maximum JUMP opcode */"