OLD | NEW |
(Empty) | |
| 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 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 set in stdin |
| 36 set currentOp {} |
| 37 set nOp 0 |
| 38 while {![eof $in]} { |
| 39 set line [gets $in] |
| 40 |
| 41 # Remember the TK_ values from the parse.h file. |
| 42 # NB: The "TK_" prefix stands for "ToKen", not the graphical Tk toolkit |
| 43 # commonly associated with TCL. |
| 44 # |
| 45 if {[regexp {^#define TK_} $line]} { |
| 46 set tk([lindex $line 1]) [lindex $line 2] |
| 47 continue |
| 48 } |
| 49 |
| 50 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces |
| 51 # a new opcode. Remember which parameters are used. |
| 52 # |
| 53 if {[regexp {^.. Opcode: } $line]} { |
| 54 set currentOp OP_[lindex $line 2] |
| 55 set m 0 |
| 56 foreach term $line { |
| 57 switch $term { |
| 58 P1 {incr m 1} |
| 59 P2 {incr m 2} |
| 60 P3 {incr m 4} |
| 61 P4 {incr m 8} |
| 62 P5 {incr m 16} |
| 63 } |
| 64 } |
| 65 set paramused($currentOp) $m |
| 66 } |
| 67 |
| 68 # Find "** Synopsis: " lines that follow Opcode: |
| 69 # |
| 70 if {[regexp {^.. Synopsis: (.*)} $line all x] && $currentOp!=""} { |
| 71 set synopsis($currentOp) [string trim $x] |
| 72 } |
| 73 |
| 74 # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 75 # |
| 76 if {[regexp {^case OP_} $line]} { |
| 77 set line [split $line] |
| 78 set name [string trim [lindex $line 1] :] |
| 79 set op($name) -1 |
| 80 set jump($name) 0 |
| 81 set in1($name) 0 |
| 82 set in2($name) 0 |
| 83 set in3($name) 0 |
| 84 set out1($name) 0 |
| 85 set out2($name) 0 |
| 86 for {set i 3} {$i<[llength $line]-1} {incr i} { |
| 87 switch [string trim [lindex $line $i] ,] { |
| 88 same { |
| 89 incr i |
| 90 if {[lindex $line $i]=="as"} { |
| 91 incr i |
| 92 set sym [string trim [lindex $line $i] ,] |
| 93 set val $tk($sym) |
| 94 set op($name) $val |
| 95 set used($val) 1 |
| 96 set sameas($val) $sym |
| 97 set def($val) $name |
| 98 } |
| 99 } |
| 100 jump {set jump($name) 1} |
| 101 in1 {set in1($name) 1} |
| 102 in2 {set in2($name) 1} |
| 103 in3 {set in3($name) 1} |
| 104 out2 {set out2($name) 1} |
| 105 out3 {set out3($name) 1} |
| 106 } |
| 107 } |
| 108 set order($nOp) $name |
| 109 incr nOp |
| 110 } |
| 111 } |
| 112 |
| 113 # Assign numbers to all opcodes and output the result. |
| 114 # |
| 115 set cnt 0 |
| 116 set max 0 |
| 117 puts "/* Automatically generated. Do not edit */" |
| 118 puts "/* See the tool/mkopcodeh.tcl script for details */" |
| 119 set op(OP_Noop) -1 |
| 120 set order($nOp) OP_Noop |
| 121 incr nOp |
| 122 set op(OP_Explain) -1 |
| 123 set order($nOp) OP_Explain |
| 124 incr nOp |
| 125 |
| 126 # The following are the opcodes that are processed by resolveP2Values() |
| 127 # |
| 128 set rp2v_ops { |
| 129 OP_Transaction |
| 130 OP_AutoCommit |
| 131 OP_Savepoint |
| 132 OP_Checkpoint |
| 133 OP_Vacuum |
| 134 OP_JournalMode |
| 135 OP_VUpdate |
| 136 OP_VFilter |
| 137 OP_Next |
| 138 OP_NextIfOpen |
| 139 OP_SorterNext |
| 140 OP_Prev |
| 141 OP_PrevIfOpen |
| 142 } |
| 143 |
| 144 # Assign small values to opcodes that are processed by resolveP2Values() |
| 145 # to make code generation for the switch() statement smaller and faster. |
| 146 # |
| 147 set cnt 0 |
| 148 for {set i 0} {$i<$nOp} {incr i} { |
| 149 set name $order($i) |
| 150 if {[lsearch $rp2v_ops $name]>=0} { |
| 151 incr cnt |
| 152 while {[info exists used($cnt)]} {incr cnt} |
| 153 set op($name) $cnt |
| 154 set used($cnt) 1 |
| 155 set def($cnt) $name |
| 156 } |
| 157 } |
| 158 |
| 159 # Generate the numeric values for remaining opcodes |
| 160 # |
| 161 for {set i 0} {$i<$nOp} {incr i} { |
| 162 set name $order($i) |
| 163 if {$op($name)<0} { |
| 164 incr cnt |
| 165 while {[info exists used($cnt)]} {incr cnt} |
| 166 set op($name) $cnt |
| 167 set used($cnt) 1 |
| 168 set def($cnt) $name |
| 169 } |
| 170 } |
| 171 set max $cnt |
| 172 for {set i 1} {$i<=$nOp} {incr i} { |
| 173 if {![info exists used($i)]} { |
| 174 set def($i) "OP_NotUsed_$i" |
| 175 } |
| 176 set name $def($i) |
| 177 puts -nonewline [format {#define %-16s %3d} $name $i] |
| 178 set com {} |
| 179 if {[info exists sameas($i)]} { |
| 180 set com "same as $sameas($i)" |
| 181 } |
| 182 if {[info exists synopsis($name)]} { |
| 183 set x $synopsis($name) |
| 184 if {$com==""} { |
| 185 set com "synopsis: $x" |
| 186 } else { |
| 187 append com ", synopsis: $x" |
| 188 } |
| 189 } |
| 190 if {$com!=""} { |
| 191 puts -nonewline [format " /* %-42s */" $com] |
| 192 } |
| 193 puts "" |
| 194 } |
| 195 |
| 196 # Generate the bitvectors: |
| 197 # |
| 198 set bv(0) 0 |
| 199 for {set i 1} {$i<=$max} {incr i} { |
| 200 set name $def($i) |
| 201 if {[info exists jump($name)] && $jump($name)} {set a0 1} {set a0 0} |
| 202 if {[info exists in1($name)] && $in1($name)} {set a1 2} {set a1 0} |
| 203 if {[info exists in2($name)] && $in2($name)} {set a2 4} {set a2 0} |
| 204 if {[info exists in3($name)] && $in3($name)} {set a3 8} {set a3 0} |
| 205 if {[info exists out2($name)] && $out2($name)} {set a4 16} {set a4 0} |
| 206 if {[info exists out3($name)] && $out3($name)} {set a5 32} {set a5 0} |
| 207 set bv($i) [expr {$a0+$a1+$a2+$a3+$a4+$a5}] |
| 208 } |
| 209 puts "" |
| 210 puts "/* Properties such as \"out2\" or \"jump\" that are specified in" |
| 211 puts "** comments following the \"case\" for each opcode in the vdbe.c" |
| 212 puts "** are encoded into bitvectors as follows:" |
| 213 puts "*/" |
| 214 puts "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" |
| 215 puts "#define OPFLG_IN1 0x0002 /* in1: P1 is an input */" |
| 216 puts "#define OPFLG_IN2 0x0004 /* in2: P2 is an input */" |
| 217 puts "#define OPFLG_IN3 0x0008 /* in3: P3 is an input */" |
| 218 puts "#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */" |
| 219 puts "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */" |
| 220 puts "#define OPFLG_INITIALIZER \173\\" |
| 221 for {set i 0} {$i<=$max} {incr i} { |
| 222 if {$i%8==0} { |
| 223 puts -nonewline [format "/* %3d */" $i] |
| 224 } |
| 225 puts -nonewline [format " 0x%02x," $bv($i)] |
| 226 if {$i%8==7} { |
| 227 puts "\\" |
| 228 } |
| 229 } |
| 230 puts "\175" |
OLD | NEW |