blob: 6fb3b75940dc4e7af37272a67db682fd1fe5fcce [file] [log] [blame]
drhb15393b2015-10-07 02:52:09 +00001#!/usr/bin/tclsh
2#
3# Generate the file opcodes.h.
4#
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
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
drh7cc84c22016-04-11 13:36:42 +000023# code generator smaller and faster.
drhb15393b2015-10-07 02:52:09 +000024#
25# This script also scans for lines of the form:
26#
mistachkin758784d2018-07-25 15:12:29 +000027# case OP_aaaa: /* jump, in1, in2, in3, out2, out3 */
drhb15393b2015-10-07 02:52:09 +000028#
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.
32#
33
34set in stdin
35set currentOp {}
mistachkin758784d2018-07-25 15:12:29 +000036set prevName {}
drhb15393b2015-10-07 02:52:09 +000037set nOp 0
mistachkin758784d2018-07-25 15:12:29 +000038set nGroup 0
drhb15393b2015-10-07 02:52:09 +000039while {![eof $in]} {
40 set line [gets $in]
41
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.
45 #
46 if {[regexp {^#define TK_} $line]} {
47 set tk([lindex $line 1]) [lindex $line 2]
48 continue
49 }
50
51 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces
52 # a new opcode. Remember which parameters are used.
53 #
54 if {[regexp {^.. Opcode: } $line]} {
55 set currentOp OP_[lindex $line 2]
56 set m 0
57 foreach term $line {
58 switch $term {
59 P1 {incr m 1}
60 P2 {incr m 2}
61 P3 {incr m 4}
62 P4 {incr m 8}
63 P5 {incr m 16}
64 }
65 }
66 set paramused($currentOp) $m
67 }
68
69 # Find "** Synopsis: " lines that follow Opcode:
70 #
71 if {[regexp {^.. Synopsis: (.*)} $line all x] && $currentOp!=""} {
72 set synopsis($currentOp) [string trim $x]
73 }
74
75 # Scan for "case OP_aaaa:" lines in the vdbe.c file
76 #
77 if {[regexp {^case OP_} $line]} {
78 set line [split $line]
79 set name [string trim [lindex $line 1] :]
drh4031baf2018-05-28 17:31:20 +000080 if {$name=="OP_Abortable"} continue; # put OP_Abortable last
drhb15393b2015-10-07 02:52:09 +000081 set op($name) -1
mistachkin758784d2018-07-25 15:12:29 +000082 set group($name) 0
drhb15393b2015-10-07 02:52:09 +000083 set jump($name) 0
84 set in1($name) 0
85 set in2($name) 0
86 set in3($name) 0
drhb15393b2015-10-07 02:52:09 +000087 set out2($name) 0
drhed94af52016-02-01 17:20:08 +000088 set out3($name) 0
dan2adb3092022-12-06 18:48:06 +000089 set ncycle($name) 0
drhb15393b2015-10-07 02:52:09 +000090 for {set i 3} {$i<[llength $line]-1} {incr i} {
91 switch [string trim [lindex $line $i] ,] {
92 same {
93 incr i
94 if {[lindex $line $i]=="as"} {
95 incr i
96 set sym [string trim [lindex $line $i] ,]
97 set val $tk($sym)
98 set op($name) $val
99 set used($val) 1
100 set sameas($val) $sym
101 set def($val) $name
102 }
103 }
mistachkin758784d2018-07-25 15:12:29 +0000104 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}
dan2adb3092022-12-06 18:48:06 +0000111 ncycle {set ncycle($name) 1}
drhb15393b2015-10-07 02:52:09 +0000112 }
113 }
mistachkin758784d2018-07-25 15:12:29 +0000114 if {$group($name)} {
115 set newGroup 0
116 if {[info exists groups($nGroup)]} {
117 if {$prevName=="" || !$group($prevName)} {
118 set newGroup 1
119 }
120 }
121 lappend groups($nGroup) $name
122 if {$newGroup} {incr nGroup}
123 } else {
124 if {$prevName!="" && $group($prevName)} {
125 incr nGroup
126 }
127 }
drhb15393b2015-10-07 02:52:09 +0000128 set order($nOp) $name
mistachkin758784d2018-07-25 15:12:29 +0000129 set prevName $name
drhb15393b2015-10-07 02:52:09 +0000130 incr nOp
131 }
132}
133
134# Assign numbers to all opcodes and output the result.
135#
drhb15393b2015-10-07 02:52:09 +0000136puts "/* Automatically generated. Do not edit */"
137puts "/* See the tool/mkopcodeh.tcl script for details */"
drh4031baf2018-05-28 17:31:20 +0000138foreach name {OP_Noop OP_Explain OP_Abortable} {
drhed94af52016-02-01 17:20:08 +0000139 set jump($name) 0
140 set in1($name) 0
141 set in2($name) 0
142 set in3($name) 0
143 set out2($name) 0
144 set out3($name) 0
dan2adb3092022-12-06 18:48:06 +0000145 set ncycle($name) 0
drhed94af52016-02-01 17:20:08 +0000146 set op($name) -1
147 set order($nOp) $name
148 incr nOp
149}
drhb15393b2015-10-07 02:52:09 +0000150
drhe74ca512021-05-18 12:36:35 +0000151# 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().
drhb15393b2015-10-07 02:52:09 +0000154#
155set rp2v_ops {
156 OP_Transaction
157 OP_AutoCommit
158 OP_Savepoint
159 OP_Checkpoint
160 OP_Vacuum
161 OP_JournalMode
162 OP_VUpdate
163 OP_VFilter
drh064390b2022-07-01 19:42:12 +0000164 OP_Init
drhb15393b2015-10-07 02:52:09 +0000165}
166
drhe74ca512021-05-18 12:36:35 +0000167# Assign the smallest values to opcodes that are processed by resolveP2Values()
drhb15393b2015-10-07 02:52:09 +0000168# to make code generation for the switch() statement smaller and faster.
169#
drhed94af52016-02-01 17:20:08 +0000170set cnt -1
drhb15393b2015-10-07 02:52:09 +0000171for {set i 0} {$i<$nOp} {incr i} {
172 set name $order($i)
173 if {[lsearch $rp2v_ops $name]>=0} {
174 incr cnt
175 while {[info exists used($cnt)]} {incr cnt}
176 set op($name) $cnt
177 set used($cnt) 1
178 set def($cnt) $name
179 }
180}
drhe74ca512021-05-18 12:36:35 +0000181set mxCase1 $cnt
drhb15393b2015-10-07 02:52:09 +0000182
drh7cc84c22016-04-11 13:36:42 +0000183# Assign the next group of values to JUMP opcodes
184#
185for {set i 0} {$i<$nOp} {incr i} {
186 set name $order($i)
187 if {$op($name)>=0} continue
188 if {!$jump($name)} continue
189 incr cnt
190 while {[info exists used($cnt)]} {incr cnt}
191 set op($name) $cnt
192 set used($cnt) 1
193 set def($cnt) $name
194}
195
196# Find the numeric value for the largest JUMP opcode
197#
198set mxJump -1
199for {set i 0} {$i<$nOp} {incr i} {
200 set name $order($i)
201 if {$jump($name) && $op($name)>$mxJump} {set mxJump $op($name)}
202}
203
204
mistachkin758784d2018-07-25 15:12:29 +0000205# Generate the numeric values for all remaining opcodes, while
206# preserving any groupings of opcodes (i.e. those that must be
207# together).
drhb15393b2015-10-07 02:52:09 +0000208#
mistachkin758784d2018-07-25 15:12:29 +0000209for {set g 0} {$g<$nGroup} {incr g} {
210 set gLen [llength $groups($g)]
211 set ok 0; set start -1
mistachkinf9ac1ab2021-06-01 21:07:49 +0000212 set seek $cnt
mistachkin758784d2018-07-25 15:12:29 +0000213 while {!$ok} {
mistachkinf9ac1ab2021-06-01 21:07:49 +0000214 incr seek
mistachkin758784d2018-07-25 15:12:29 +0000215 while {[info exists used($seek)]} {incr seek}
216 set ok 1; set start $seek
217 for {set j 0} {$j<$gLen} {incr j} {
218 incr seek
219 if {[info exists used($seek)]} {
220 set ok 0; break
221 }
222 }
223 }
224 if {$ok} {
225 set next $start
226 for {set j 0} {$j<$gLen} {incr j} {
227 set name [lindex $groups($g) $j]
228 if {$op($name)>=0} continue
229 set op($name) $next
230 set used($next) 1
231 set def($next) $name
232 incr next
233 }
234 } else {
235 error "cannot find opcodes for group: $groups($g)"
236 }
237}
238
drhb15393b2015-10-07 02:52:09 +0000239for {set i 0} {$i<$nOp} {incr i} {
240 set name $order($i)
241 if {$op($name)<0} {
242 incr cnt
243 while {[info exists used($cnt)]} {incr cnt}
244 set op($name) $cnt
245 set used($cnt) 1
246 set def($cnt) $name
247 }
248}
dan0e6b8302017-07-14 17:50:11 +0000249
250set max [lindex [lsort -decr -integer [array names used]] 0]
251for {set i 0} {$i<=$max} {incr i} {
drhb15393b2015-10-07 02:52:09 +0000252 if {![info exists used($i)]} {
253 set def($i) "OP_NotUsed_$i"
254 }
dan0e6b8302017-07-14 17:50:11 +0000255 if {$i>$max} {set max $i}
drhb15393b2015-10-07 02:52:09 +0000256 set name $def($i)
257 puts -nonewline [format {#define %-16s %3d} $name $i]
258 set com {}
mistachkin758784d2018-07-25 15:12:29 +0000259 if {[info exists jump($name)] && $jump($name)} {
drha0286052017-08-02 03:21:52 +0000260 lappend com "jump"
261 }
drhb15393b2015-10-07 02:52:09 +0000262 if {[info exists sameas($i)]} {
drha0286052017-08-02 03:21:52 +0000263 lappend com "same as $sameas($i)"
drhb15393b2015-10-07 02:52:09 +0000264 }
265 if {[info exists synopsis($name)]} {
drha0286052017-08-02 03:21:52 +0000266 lappend com "synopsis: $synopsis($name)"
drhb15393b2015-10-07 02:52:09 +0000267 }
drha0286052017-08-02 03:21:52 +0000268 if {[llength $com]} {
269 puts -nonewline [format " /* %-42s */" [join $com {, }]]
drhb15393b2015-10-07 02:52:09 +0000270 }
271 puts ""
272}
273
dan0e6b8302017-07-14 17:50:11 +0000274if {$max>255} {
275 error "More than 255 opcodes - VdbeOp.opcode is of type u8!"
276}
277
drhb15393b2015-10-07 02:52:09 +0000278# Generate the bitvectors:
279#
280set bv(0) 0
drhed94af52016-02-01 17:20:08 +0000281for {set i 0} {$i<=$max} {incr i} {
drhed94af52016-02-01 17:20:08 +0000282 set x 0
dan0e6b8302017-07-14 17:50:11 +0000283 set name $def($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}
dan2adb3092022-12-06 18:48:06 +0000291 if {$ncycle($name)} {incr x 64}
dan0e6b8302017-07-14 17:50:11 +0000292 }
drhed94af52016-02-01 17:20:08 +0000293 set bv($i) $x
drhb15393b2015-10-07 02:52:09 +0000294}
295puts ""
296puts "/* Properties such as \"out2\" or \"jump\" that are specified in"
297puts "** comments following the \"case\" for each opcode in the vdbe.c"
298puts "** are encoded into bitvectors as follows:"
299puts "*/"
drhed94af52016-02-01 17:20:08 +0000300puts "#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */"
301puts "#define OPFLG_IN1 0x02 /* in1: P1 is an input */"
302puts "#define OPFLG_IN2 0x04 /* in2: P2 is an input */"
303puts "#define OPFLG_IN3 0x08 /* in3: P3 is an input */"
304puts "#define OPFLG_OUT2 0x10 /* out2: P2 is an output */"
305puts "#define OPFLG_OUT3 0x20 /* out3: P3 is an output */"
dan2adb3092022-12-06 18:48:06 +0000306puts "#define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */"
drhb15393b2015-10-07 02:52:09 +0000307puts "#define OPFLG_INITIALIZER \173\\"
308for {set i 0} {$i<=$max} {incr i} {
309 if {$i%8==0} {
310 puts -nonewline [format "/* %3d */" $i]
311 }
312 puts -nonewline [format " 0x%02x," $bv($i)]
313 if {$i%8==7} {
314 puts "\\"
315 }
316}
317puts "\175"
drh7cc84c22016-04-11 13:36:42 +0000318puts ""
drhe74ca512021-05-18 12:36:35 +0000319puts "/* The resolve3P2Values() routine is able to run faster if it knows"
drh7cc84c22016-04-11 13:36:42 +0000320puts "** the value of the largest JUMP opcode. The smaller the maximum"
321puts "** JUMP opcode the better, so the mkopcodeh.tcl script that"
322puts "** generated this include file strives to group all JUMP opcodes"
323puts "** together near the beginning of the list."
324puts "*/"
drhc310db32016-04-11 16:35:05 +0000325puts "#define SQLITE_MX_JUMP_OPCODE $mxJump /* Maximum JUMP opcode */"