| OLD | NEW |
| 1 #!/usr/bin/tclsh | 1 #!/usr/bin/tclsh |
| 2 # | 2 # |
| 3 # Generate the file opcodes.h. | 3 # Generate the file opcodes.h. |
| 4 # | 4 # |
| 5 # This TCL script scans a concatenation of the parse.h output file from the | 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 | 6 # parser and the vdbe.c source file in order to generate the opcodes numbers |
| 7 # for all opcodes. | 7 # for all opcodes. |
| 8 # | 8 # |
| 9 # The lines of the vdbe.c that we are interested in are of the form: | 9 # The lines of the vdbe.c that we are interested in are of the form: |
| 10 # | 10 # |
| 11 # case OP_aaaa: /* same as TK_bbbbb */ | 11 # case OP_aaaa: /* same as TK_bbbbb */ |
| 12 # | 12 # |
| 13 # The TK_ comment is optional. If it is present, then the value assigned to | 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 | 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. | 15 # a small integer that is different from every other OP_ value. |
| 16 # | 16 # |
| 17 # We go to the trouble of making some OP_ values the same as TK_ values | 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 | 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 | 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 | 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, | 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 | 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 | 23 # code generator smaller and faster. |
| 24 # the library footprint smaller. | |
| 25 # | 24 # |
| 26 # This script also scans for lines of the form: | 25 # This script also scans for lines of the form: |
| 27 # | 26 # |
| 28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ | 27 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ |
| 29 # | 28 # |
| 30 # When such comments are found on an opcode, it means that certain | 29 # When such comments are found on an opcode, it means that certain |
| 31 # properties apply to that opcode. Set corresponding flags using the | 30 # properties apply to that opcode. Set corresponding flags using the |
| 32 # OPFLG_INITIALIZER macro. | 31 # OPFLG_INITIALIZER macro. |
| 33 # | 32 # |
| 34 | 33 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 # Scan for "case OP_aaaa:" lines in the vdbe.c file | 73 # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 75 # | 74 # |
| 76 if {[regexp {^case OP_} $line]} { | 75 if {[regexp {^case OP_} $line]} { |
| 77 set line [split $line] | 76 set line [split $line] |
| 78 set name [string trim [lindex $line 1] :] | 77 set name [string trim [lindex $line 1] :] |
| 79 set op($name) -1 | 78 set op($name) -1 |
| 80 set jump($name) 0 | 79 set jump($name) 0 |
| 81 set in1($name) 0 | 80 set in1($name) 0 |
| 82 set in2($name) 0 | 81 set in2($name) 0 |
| 83 set in3($name) 0 | 82 set in3($name) 0 |
| 84 set out1($name) 0 | |
| 85 set out2($name) 0 | 83 set out2($name) 0 |
| 84 set out3($name) 0 |
| 86 for {set i 3} {$i<[llength $line]-1} {incr i} { | 85 for {set i 3} {$i<[llength $line]-1} {incr i} { |
| 87 switch [string trim [lindex $line $i] ,] { | 86 switch [string trim [lindex $line $i] ,] { |
| 88 same { | 87 same { |
| 89 incr i | 88 incr i |
| 90 if {[lindex $line $i]=="as"} { | 89 if {[lindex $line $i]=="as"} { |
| 91 incr i | 90 incr i |
| 92 set sym [string trim [lindex $line $i] ,] | 91 set sym [string trim [lindex $line $i] ,] |
| 93 set val $tk($sym) | 92 set val $tk($sym) |
| 94 set op($name) $val | 93 set op($name) $val |
| 95 set used($val) 1 | 94 set used($val) 1 |
| 96 set sameas($val) $sym | 95 set sameas($val) $sym |
| 97 set def($val) $name | 96 set def($val) $name |
| 98 } | 97 } |
| 99 } | 98 } |
| 100 jump {set jump($name) 1} | 99 jump {set jump($name) 1} |
| 101 in1 {set in1($name) 1} | 100 in1 {set in1($name) 1} |
| 102 in2 {set in2($name) 1} | 101 in2 {set in2($name) 1} |
| 103 in3 {set in3($name) 1} | 102 in3 {set in3($name) 1} |
| 104 out2 {set out2($name) 1} | 103 out2 {set out2($name) 1} |
| 105 out3 {set out3($name) 1} | 104 out3 {set out3($name) 1} |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 set order($nOp) $name | 107 set order($nOp) $name |
| 109 incr nOp | 108 incr nOp |
| 110 } | 109 } |
| 111 } | 110 } |
| 112 | 111 |
| 113 # Assign numbers to all opcodes and output the result. | 112 # Assign numbers to all opcodes and output the result. |
| 114 # | 113 # |
| 115 set cnt 0 | |
| 116 set max 0 | |
| 117 puts "/* Automatically generated. Do not edit */" | 114 puts "/* Automatically generated. Do not edit */" |
| 118 puts "/* See the tool/mkopcodeh.tcl script for details */" | 115 puts "/* See the tool/mkopcodeh.tcl script for details */" |
| 119 set op(OP_Noop) -1 | 116 foreach name {OP_Noop OP_Explain} { |
| 120 set order($nOp) OP_Noop | 117 set jump($name) 0 |
| 121 incr nOp | 118 set in1($name) 0 |
| 122 set op(OP_Explain) -1 | 119 set in2($name) 0 |
| 123 set order($nOp) OP_Explain | 120 set in3($name) 0 |
| 124 incr nOp | 121 set out2($name) 0 |
| 122 set out3($name) 0 |
| 123 set op($name) -1 |
| 124 set order($nOp) $name |
| 125 incr nOp |
| 126 } |
| 125 | 127 |
| 126 # The following are the opcodes that are processed by resolveP2Values() | 128 # The following are the opcodes that are processed by resolveP2Values() |
| 127 # | 129 # |
| 128 set rp2v_ops { | 130 set rp2v_ops { |
| 129 OP_Transaction | 131 OP_Transaction |
| 130 OP_AutoCommit | 132 OP_AutoCommit |
| 131 OP_Savepoint | 133 OP_Savepoint |
| 132 OP_Checkpoint | 134 OP_Checkpoint |
| 133 OP_Vacuum | 135 OP_Vacuum |
| 134 OP_JournalMode | 136 OP_JournalMode |
| 135 OP_VUpdate | 137 OP_VUpdate |
| 136 OP_VFilter | 138 OP_VFilter |
| 137 OP_Next | 139 OP_Next |
| 138 OP_NextIfOpen | 140 OP_NextIfOpen |
| 139 OP_SorterNext | 141 OP_SorterNext |
| 140 OP_Prev | 142 OP_Prev |
| 141 OP_PrevIfOpen | 143 OP_PrevIfOpen |
| 142 } | 144 } |
| 143 | 145 |
| 144 # Assign small values to opcodes that are processed by resolveP2Values() | 146 # Assign small values to opcodes that are processed by resolveP2Values() |
| 145 # to make code generation for the switch() statement smaller and faster. | 147 # to make code generation for the switch() statement smaller and faster. |
| 146 # | 148 # |
| 147 set cnt 0 | 149 set cnt -1 |
| 148 for {set i 0} {$i<$nOp} {incr i} { | 150 for {set i 0} {$i<$nOp} {incr i} { |
| 149 set name $order($i) | 151 set name $order($i) |
| 150 if {[lsearch $rp2v_ops $name]>=0} { | 152 if {[lsearch $rp2v_ops $name]>=0} { |
| 151 incr cnt | 153 incr cnt |
| 152 while {[info exists used($cnt)]} {incr cnt} | 154 while {[info exists used($cnt)]} {incr cnt} |
| 153 set op($name) $cnt | 155 set op($name) $cnt |
| 154 set used($cnt) 1 | 156 set used($cnt) 1 |
| 155 set def($cnt) $name | 157 set def($cnt) $name |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 | 160 |
| 159 # Generate the numeric values for remaining opcodes | 161 # Assign the next group of values to JUMP opcodes |
| 160 # | 162 # |
| 161 for {set i 0} {$i<$nOp} {incr i} { | 163 for {set i 0} {$i<$nOp} {incr i} { |
| 162 set name $order($i) | 164 set name $order($i) |
| 165 if {$op($name)>=0} continue |
| 166 if {!$jump($name)} continue |
| 167 incr cnt |
| 168 while {[info exists used($cnt)]} {incr cnt} |
| 169 set op($name) $cnt |
| 170 set used($cnt) 1 |
| 171 set def($cnt) $name |
| 172 } |
| 173 |
| 174 # Find the numeric value for the largest JUMP opcode |
| 175 # |
| 176 set mxJump -1 |
| 177 for {set i 0} {$i<$nOp} {incr i} { |
| 178 set name $order($i) |
| 179 if {$jump($name) && $op($name)>$mxJump} {set mxJump $op($name)} |
| 180 } |
| 181 |
| 182 |
| 183 # Generate the numeric values for all remaining opcodes |
| 184 # |
| 185 for {set i 0} {$i<$nOp} {incr i} { |
| 186 set name $order($i) |
| 163 if {$op($name)<0} { | 187 if {$op($name)<0} { |
| 164 incr cnt | 188 incr cnt |
| 165 while {[info exists used($cnt)]} {incr cnt} | 189 while {[info exists used($cnt)]} {incr cnt} |
| 166 set op($name) $cnt | 190 set op($name) $cnt |
| 167 set used($cnt) 1 | 191 set used($cnt) 1 |
| 168 set def($cnt) $name | 192 set def($cnt) $name |
| 169 } | 193 } |
| 170 } | 194 } |
| 171 set max $cnt | 195 set max $cnt |
| 172 for {set i 1} {$i<=$nOp} {incr i} { | 196 for {set i 0} {$i<$nOp} {incr i} { |
| 173 if {![info exists used($i)]} { | 197 if {![info exists used($i)]} { |
| 174 set def($i) "OP_NotUsed_$i" | 198 set def($i) "OP_NotUsed_$i" |
| 175 } | 199 } |
| 176 set name $def($i) | 200 set name $def($i) |
| 177 puts -nonewline [format {#define %-16s %3d} $name $i] | 201 puts -nonewline [format {#define %-16s %3d} $name $i] |
| 178 set com {} | 202 set com {} |
| 179 if {[info exists sameas($i)]} { | 203 if {[info exists sameas($i)]} { |
| 180 set com "same as $sameas($i)" | 204 set com "same as $sameas($i)" |
| 181 } | 205 } |
| 182 if {[info exists synopsis($name)]} { | 206 if {[info exists synopsis($name)]} { |
| 183 set x $synopsis($name) | 207 set x $synopsis($name) |
| 184 if {$com==""} { | 208 if {$com==""} { |
| 185 set com "synopsis: $x" | 209 set com "synopsis: $x" |
| 186 } else { | 210 } else { |
| 187 append com ", synopsis: $x" | 211 append com ", synopsis: $x" |
| 188 } | 212 } |
| 189 } | 213 } |
| 190 if {$com!=""} { | 214 if {$com!=""} { |
| 191 puts -nonewline [format " /* %-42s */" $com] | 215 puts -nonewline [format " /* %-42s */" $com] |
| 192 } | 216 } |
| 193 puts "" | 217 puts "" |
| 194 } | 218 } |
| 195 | 219 |
| 196 # Generate the bitvectors: | 220 # Generate the bitvectors: |
| 197 # | 221 # |
| 198 set bv(0) 0 | 222 set bv(0) 0 |
| 199 for {set i 1} {$i<=$max} {incr i} { | 223 for {set i 0} {$i<=$max} {incr i} { |
| 200 set name $def($i) | 224 set name $def($i) |
| 201 if {[info exists jump($name)] && $jump($name)} {set a0 1} {set a0 0} | 225 set x 0 |
| 202 if {[info exists in1($name)] && $in1($name)} {set a1 2} {set a1 0} | 226 if {$jump($name)} {incr x 1} |
| 203 if {[info exists in2($name)] && $in2($name)} {set a2 4} {set a2 0} | 227 if {$in1($name)} {incr x 2} |
| 204 if {[info exists in3($name)] && $in3($name)} {set a3 8} {set a3 0} | 228 if {$in2($name)} {incr x 4} |
| 205 if {[info exists out2($name)] && $out2($name)} {set a4 16} {set a4 0} | 229 if {$in3($name)} {incr x 8} |
| 206 if {[info exists out3($name)] && $out3($name)} {set a5 32} {set a5 0} | 230 if {$out2($name)} {incr x 16} |
| 207 set bv($i) [expr {$a0+$a1+$a2+$a3+$a4+$a5}] | 231 if {$out3($name)} {incr x 32} |
| 232 set bv($i) $x |
| 208 } | 233 } |
| 209 puts "" | 234 puts "" |
| 210 puts "/* Properties such as \"out2\" or \"jump\" that are specified in" | 235 puts "/* Properties such as \"out2\" or \"jump\" that are specified in" |
| 211 puts "** comments following the \"case\" for each opcode in the vdbe.c" | 236 puts "** comments following the \"case\" for each opcode in the vdbe.c" |
| 212 puts "** are encoded into bitvectors as follows:" | 237 puts "** are encoded into bitvectors as follows:" |
| 213 puts "*/" | 238 puts "*/" |
| 214 puts "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" | 239 puts "#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */" |
| 215 puts "#define OPFLG_IN1 0x0002 /* in1: P1 is an input */" | 240 puts "#define OPFLG_IN1 0x02 /* in1: P1 is an input */" |
| 216 puts "#define OPFLG_IN2 0x0004 /* in2: P2 is an input */" | 241 puts "#define OPFLG_IN2 0x04 /* in2: P2 is an input */" |
| 217 puts "#define OPFLG_IN3 0x0008 /* in3: P3 is an input */" | 242 puts "#define OPFLG_IN3 0x08 /* in3: P3 is an input */" |
| 218 puts "#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */" | 243 puts "#define OPFLG_OUT2 0x10 /* out2: P2 is an output */" |
| 219 puts "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */" | 244 puts "#define OPFLG_OUT3 0x20 /* out3: P3 is an output */" |
| 220 puts "#define OPFLG_INITIALIZER \173\\" | 245 puts "#define OPFLG_INITIALIZER \173\\" |
| 221 for {set i 0} {$i<=$max} {incr i} { | 246 for {set i 0} {$i<=$max} {incr i} { |
| 222 if {$i%8==0} { | 247 if {$i%8==0} { |
| 223 puts -nonewline [format "/* %3d */" $i] | 248 puts -nonewline [format "/* %3d */" $i] |
| 224 } | 249 } |
| 225 puts -nonewline [format " 0x%02x," $bv($i)] | 250 puts -nonewline [format " 0x%02x," $bv($i)] |
| 226 if {$i%8==7} { | 251 if {$i%8==7} { |
| 227 puts "\\" | 252 puts "\\" |
| 228 } | 253 } |
| 229 } | 254 } |
| 230 puts "\175" | 255 puts "\175" |
| 256 puts "" |
| 257 puts "/* The sqlite3P2Values() routine is able to run faster if it knows" |
| 258 puts "** the value of the largest JUMP opcode. The smaller the maximum" |
| 259 puts "** JUMP opcode the better, so the mkopcodeh.tcl script that" |
| 260 puts "** generated this include file strives to group all JUMP opcodes" |
| 261 puts "** together near the beginning of the list." |
| 262 puts "*/" |
| 263 puts "#define SQLITE_MX_JUMP_OPCODE $mxJump /* Maximum JUMP opcode */" |
| OLD | NEW |