Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/tool/mkopcodeh.tcl # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / tool / mkopcodeh.tcl` 332 lines ยท 9378 bytes Raw Blame History 1 #!/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 23 # code generator smaller and faster. 24 # 25 # This script also scans for lines of the form: 26 # 27 # case OP_aaaa: /* jump, in1, in2, in3, out2, out3 */ 28 # 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 34 set in stdin 35 set currentOp {} 36 set prevName {} 37 set nOp 0 38 set nGroup 0 39 while {![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] :] 80 if {$name=="OP_Abortable"} continue; # put OP_Abortable last 81 set op($name) -1 82 set group($name) 0 83 set jump($name) 0 84 set jump0($name) 0 85 set in1($name) 0 86 set in2($name) 0 87 set in3($name) 0 88 set out2($name) 0 89 set out3($name) 0 90 set ncycle($name) 0 91 for {set i 3} {$i<[llength $line]-1} {incr i} { 92 switch [string trim [lindex $line $i] ,] { 93 same { 94 incr i 95 if {[lindex $line $i]=="as"} { 96 incr i 97 set sym [string trim [lindex $line $i] ,] 98 set val $tk($sym) 99 set op($name) $val 100 set used($val) 1 101 set sameas($val) $sym 102 set def($val) $name 103 } 104 } 105 group {set group($name) 1} 106 jump {set jump($name) 1} 107 in1 {set in1($name) 1} 108 in2 {set in2($name) 1} 109 in3 {set in3($name) 1} 110 out2 {set out2($name) 1} 111 out3 {set out3($name) 1} 112 ncycle {set ncycle($name) 1} 113 jump0 {set jump($name) 1; set jump0($name) 1;} 114 } 115 } 116 if {$group($name)} { 117 set newGroup 0 118 if {[info exists groups($nGroup)]} { 119 if {$prevName=="" || !$group($prevName)} { 120 set newGroup 1 121 } 122 } 123 lappend groups($nGroup) $name 124 if {$newGroup} {incr nGroup} 125 } else { 126 if {$prevName!="" && $group($prevName)} { 127 incr nGroup 128 } 129 } 130 set order($nOp) $name 131 set prevName $name 132 incr nOp 133 } 134 } 135 136 # Assign numbers to all opcodes and output the result. 137 # 138 puts "/* Automatically generated. Do not edit */" 139 puts "/* See the tool/mkopcodeh.tcl script for details */" 140 foreach name {OP_Noop OP_Explain OP_Abortable} { 141 set jump($name) 0 142 set jump0($name) 0 143 set in1($name) 0 144 set in2($name) 0 145 set in3($name) 0 146 set out2($name) 0 147 set out3($name) 0 148 set ncycle($name) 0 149 set op($name) -1 150 set order($nOp) $name 151 incr nOp 152 } 153 154 # The following are the opcodes that receive special processing in the 155 # resolveP2Values() routine. Update this list whenever new cases are 156 # added to the pOp->opcode switch within resolveP2Values(). 157 # 158 set rp2v_ops { 159 OP_Transaction 160 OP_AutoCommit 161 OP_Savepoint 162 OP_Checkpoint 163 OP_Vacuum 164 OP_JournalMode 165 OP_VUpdate 166 OP_VFilter 167 OP_Init 168 } 169 170 # Assign the smallest values to opcodes that are processed by resolveP2Values() 171 # to make code generation for the switch() statement smaller and faster. 172 # 173 set cnt -1 174 for {set i 0} {$i<$nOp} {incr i} { 175 set name $order($i) 176 if {[lsearch $rp2v_ops $name]>=0} { 177 incr cnt 178 while {[info exists used($cnt)]} {incr cnt} 179 set op($name) $cnt 180 set used($cnt) 1 181 set def($cnt) $name 182 } 183 } 184 set mxCase1 $cnt 185 186 # Assign the next group of values to JUMP opcodes 187 # 188 for {set i 0} {$i<$nOp} {incr i} { 189 set name $order($i) 190 if {$op($name)>=0} continue 191 if {!$jump($name)} continue 192 incr cnt 193 while {[info exists used($cnt)]} {incr cnt} 194 set op($name) $cnt 195 set used($cnt) 1 196 set def($cnt) $name 197 } 198 199 # Find the numeric value for the largest JUMP opcode 200 # 201 set mxJump -1 202 for {set i 0} {$i<$nOp} {incr i} { 203 set name $order($i) 204 if {$jump($name) && $op($name)>$mxJump} {set mxJump $op($name)} 205 } 206 207 208 # Generate the numeric values for all remaining opcodes, while 209 # preserving any groupings of opcodes (i.e. those that must be 210 # together). 211 # 212 for {set g 0} {$g<$nGroup} {incr g} { 213 set gLen [llength $groups($g)] 214 set ok 0; set start -1 215 set seek $cnt 216 while {!$ok} { 217 incr seek 218 while {[info exists used($seek)]} {incr seek} 219 set ok 1; set start $seek 220 for {set j 0} {$j<$gLen} {incr j} { 221 incr seek 222 if {[info exists used($seek)]} { 223 set ok 0; break 224 } 225 } 226 } 227 if {$ok} { 228 set next $start 229 for {set j 0} {$j<$gLen} {incr j} { 230 set name [lindex $groups($g) $j] 231 if {$op($name)>=0} continue 232 set op($name) $next 233 set used($next) 1 234 set def($next) $name 235 incr next 236 } 237 } else { 238 error "cannot find opcodes for group: $groups($g)" 239 } 240 } 241 242 for {set i 0} {$i<$nOp} {incr i} { 243 set name $order($i) 244 if {$op($name)<0} { 245 incr cnt 246 while {[info exists used($cnt)]} {incr cnt} 247 set op($name) $cnt 248 set used($cnt) 1 249 set def($cnt) $name 250 } 251 } 252 253 set max [lindex [lsort -decr -integer [array names used]] 0] 254 for {set i 0} {$i<=$max} {incr i} { 255 if {![info exists used($i)]} { 256 set def($i) "OP_NotUsed_$i" 257 } 258 if {$i>$max} {set max $i} 259 set name $def($i) 260 puts -nonewline [format {#define %-16s %3d} $name $i] 261 set com {} 262 if {[info exists jump0($name)] && $jump0($name)} { 263 lappend com "jump0" 264 } elseif {[info exists jump($name)] && $jump($name)} { 265 lappend com "jump" 266 } 267 if {[info exists sameas($i)]} { 268 lappend com "same as $sameas($i)" 269 } 270 if {[info exists synopsis($name)]} { 271 lappend com "synopsis: $synopsis($name)" 272 } 273 if {[llength $com]} { 274 puts -nonewline [format " /* %-42s */" [join $com {, }]] 275 } 276 puts "" 277 } 278 279 if {$max>255} { 280 error "More than 255 opcodes - VdbeOp.opcode is of type u8!" 281 } 282 283 # Generate the bitvectors: 284 # 285 set bv(0) 0 286 for {set i 0} {$i<=$max} {incr i} { 287 set x 0 288 set name $def($i) 289 if {[string match OP_NotUsed* $name]==0} { 290 if {$jump($name)} {incr x 1} 291 if {$in1($name)} {incr x 2} 292 if {$in2($name)} {incr x 4} 293 if {$in3($name)} {incr x 8} 294 if {$out2($name)} {incr x 16} 295 if {$out3($name)} {incr x 32} 296 if {$ncycle($name)} {incr x 64} 297 if {$jump0($name)} {incr x 128} 298 } 299 set bv($i) $x 300 } 301 puts "" 302 puts "/* Properties such as \"out2\" or \"jump\" that are specified in" 303 puts "** comments following the \"case\" for each opcode in the vdbe.c" 304 puts "** are encoded into bitvectors as follows:" 305 puts "*/" 306 puts "#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */" 307 puts "#define OPFLG_IN1 0x02 /* in1: P1 is an input */" 308 puts "#define OPFLG_IN2 0x04 /* in2: P2 is an input */" 309 puts "#define OPFLG_IN3 0x08 /* in3: P3 is an input */" 310 puts "#define OPFLG_OUT2 0x10 /* out2: P2 is an output */" 311 puts "#define OPFLG_OUT3 0x20 /* out3: P3 is an output */" 312 puts "#define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */" 313 puts "#define OPFLG_JUMP0 0x80 /* jump0: P2 might be zero */" 314 puts "#define OPFLG_INITIALIZER \173\\" 315 for {set i 0} {$i<=$max} {incr i} { 316 if {$i%8==0} { 317 puts -nonewline [format "/* %3d */" $i] 318 } 319 puts -nonewline [format " 0x%02x," $bv($i)] 320 if {$i%8==7} { 321 puts "\\" 322 } 323 } 324 puts "\175" 325 puts "" 326 puts "/* The resolve3P2Values() routine is able to run faster if it knows" 327 puts "** the value of the largest JUMP opcode. The smaller the maximum" 328 puts "** JUMP opcode the better, so the mkopcodeh.tcl script that" 329 puts "** generated this include file strives to group all JUMP opcodes" 330 puts "** together near the beginning of the list." 331 puts "*/" 332 puts "#define SQLITE_MX_JUMP_OPCODE $mxJump /* Maximum JUMP opcode */"