| OLD | NEW |
| 1 #!/usr/bin/awk -f | 1 #!/usr/bin/awk -f |
| 2 # | 2 # |
| 3 # Generate the file opcodes.h. | 3 # Generate the file opcodes.h. |
| 4 # | 4 # |
| 5 # This AWK script scans a concatenation of the parse.h output file from the | 5 # This AWK 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 run (infinitesimally) faster and more importantly it makes |
| 24 # the library footprint smaller. | 24 # the library footprint smaller. |
| 25 # | 25 # |
| 26 # This script also scans for lines of the form: | 26 # This script also scans for lines of the form: |
| 27 # | 27 # |
| 28 # case OP_aaaa: /* no-push */ | 28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ |
| 29 # | 29 # |
| 30 # When the no-push comment is found on an opcode, it means that that | 30 # When such comments are found on an opcode, it means that certain |
| 31 # opcode does not leave a result on the stack. By identifying which | 31 # properties apply to that opcode. Set corresponding flags using the |
| 32 # opcodes leave results on the stack it is possible to determine a | 32 # OPFLG_INITIALIZER macro. |
| 33 # much smaller upper bound on the size of the stack. This allows | |
| 34 # a smaller stack to be allocated, which is important to embedded | |
| 35 # systems with limited memory space. This script generates a series | |
| 36 # of "NOPUSH_MASK" defines that contain bitmaps of opcodes that leave | |
| 37 # results on the stack. The NOPUSH_MASK defines are used in vdbeaux.c | |
| 38 # to help determine the maximum stack size. | |
| 39 # | 33 # |
| 40 | 34 |
| 41 | 35 |
| 42 # Remember the TK_ values from the parse.h file | 36 # Remember the TK_ values from the parse.h file |
| 43 /^#define TK_/ { | 37 /^#define TK_/ { |
| 44 tk[$2] = 0+$3 | 38 tk[$2] = 0+$3 |
| 45 } | 39 } |
| 46 | 40 |
| 47 # Scan for "case OP_aaaa:" lines in the vdbe.c file | 41 # Scan for "case OP_aaaa:" lines in the vdbe.c file |
| 48 /^case OP_/ { | 42 /^case OP_/ { |
| 49 name = $2 | 43 name = $2 |
| 50 sub(/:/,"",name) | 44 sub(/:/,"",name) |
| 51 sub("\r","",name) | 45 sub("\r","",name) |
| 52 op[name] = -1 | 46 op[name] = -1 |
| 53 jump[name] = 0 | 47 jump[name] = 0 |
| 54 out2_prerelease[name] = 0 | 48 out2_prerelease[name] = 0 |
| 55 in1[name] = 0 | 49 in1[name] = 0 |
| 56 in2[name] = 0 | 50 in2[name] = 0 |
| 57 in3[name] = 0 | 51 in3[name] = 0 |
| 52 out2[name] = 0 |
| 58 out3[name] = 0 | 53 out3[name] = 0 |
| 59 for(i=3; i<NF; i++){ | 54 for(i=3; i<NF; i++){ |
| 60 if($i=="same" && $(i+1)=="as"){ | 55 if($i=="same" && $(i+1)=="as"){ |
| 61 sym = $(i+2) | 56 sym = $(i+2) |
| 62 sub(/,/,"",sym) | 57 sub(/,/,"",sym) |
| 63 op[name] = tk[sym] | 58 op[name] = tk[sym] |
| 64 used[op[name]] = 1 | 59 used[op[name]] = 1 |
| 65 sameas[op[name]] = sym | 60 sameas[op[name]] = sym |
| 66 } | 61 } |
| 67 x = $i | 62 x = $i |
| 68 sub(",","",x) | 63 sub(",","",x) |
| 69 if(x=="jump"){ | 64 if(x=="jump"){ |
| 70 jump[name] = 1 | 65 jump[name] = 1 |
| 71 }else if(x=="out2-prerelease"){ | 66 }else if(x=="out2-prerelease"){ |
| 72 out2_prerelease[name] = 1 | 67 out2_prerelease[name] = 1 |
| 73 }else if(x=="in1"){ | 68 }else if(x=="in1"){ |
| 74 in1[name] = 1 | 69 in1[name] = 1 |
| 75 }else if(x=="in2"){ | 70 }else if(x=="in2"){ |
| 76 in2[name] = 1 | 71 in2[name] = 1 |
| 77 }else if(x=="in3"){ | 72 }else if(x=="in3"){ |
| 78 in3[name] = 1 | 73 in3[name] = 1 |
| 74 }else if(x=="out2"){ |
| 75 out2[name] = 1 |
| 79 }else if(x=="out3"){ | 76 }else if(x=="out3"){ |
| 80 out3[name] = 1 | 77 out3[name] = 1 |
| 81 } | 78 } |
| 82 } | 79 } |
| 80 order[n_op++] = name; |
| 83 } | 81 } |
| 84 | 82 |
| 85 # Assign numbers to all opcodes and output the result. | 83 # Assign numbers to all opcodes and output the result. |
| 86 END { | 84 END { |
| 87 cnt = 0 | 85 cnt = 0 |
| 88 max = 0 | 86 max = 0 |
| 89 print "/* Automatically generated. Do not edit */" | 87 print "/* Automatically generated. Do not edit */" |
| 90 print "/* See the mkopcodeh.awk script for details */" | 88 print "/* See the mkopcodeh.awk script for details */" |
| 91 op["OP_Noop"] = -1; | 89 op["OP_Noop"] = -1; |
| 90 order[n_op++] = "OP_Noop"; |
| 92 op["OP_Explain"] = -1; | 91 op["OP_Explain"] = -1; |
| 93 for(name in op){ | 92 order[n_op++] = "OP_Explain"; |
| 93 for(i=0; i<n_op; i++){ |
| 94 name = order[i]; |
| 94 if( op[name]<0 ){ | 95 if( op[name]<0 ){ |
| 95 cnt++ | 96 cnt++ |
| 96 while( used[cnt] ) cnt++ | 97 while( used[cnt] ) cnt++ |
| 97 op[name] = cnt | 98 op[name] = cnt |
| 98 } | 99 } |
| 99 used[op[name]] = 1; | 100 used[op[name]] = 1; |
| 100 if( op[name]>max ) max = op[name] | 101 if( op[name]>max ) max = op[name] |
| 101 printf "#define %-25s %15d", name, op[name] | 102 printf "#define %-25s %15d", name, op[name] |
| 102 if( sameas[op[name]] ) { | 103 if( sameas[op[name]] ) { |
| 103 printf " /* same as %-12s*/", sameas[op[name]] | 104 printf " /* same as %-12s*/", sameas[op[name]] |
| (...skipping 12 matching lines...) Expand all Loading... |
| 116 } | 117 } |
| 117 } | 118 } |
| 118 | 119 |
| 119 # Generate the bitvectors: | 120 # Generate the bitvectors: |
| 120 # | 121 # |
| 121 # bit 0: jump | 122 # bit 0: jump |
| 122 # bit 1: pushes a result onto stack | 123 # bit 1: pushes a result onto stack |
| 123 # bit 2: output to p1. release p1 before opcode runs | 124 # bit 2: output to p1. release p1 before opcode runs |
| 124 # | 125 # |
| 125 for(i=0; i<=max; i++) bv[i] = 0; | 126 for(i=0; i<=max; i++) bv[i] = 0; |
| 126 for(name in op){ | 127 for(i=0; i<n_op; i++){ |
| 128 name = order[i]; |
| 127 x = op[name] | 129 x = op[name] |
| 128 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 | 130 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 |
| 129 # a8 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0 | 131 # a7 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0 |
| 130 if( jump[name] ) a0 = 1; | 132 if( jump[name] ) a0 = 1; |
| 131 if( out2_prerelease[name] ) a1 = 2; | 133 if( out2_prerelease[name] ) a1 = 2; |
| 132 if( in1[name] ) a2 = 4; | 134 if( in1[name] ) a2 = 4; |
| 133 if( in2[name] ) a3 = 8; | 135 if( in2[name] ) a3 = 8; |
| 134 if( in3[name] ) a4 = 16; | 136 if( in3[name] ) a4 = 16; |
| 135 if( out3[name] ) a5 = 32; | 137 if( out2[name] ) a5 = 32; |
| 138 if( out3[name] ) a6 = 64; |
| 136 # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15; | 139 # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15; |
| 137 bv[x] = a0+a1+a2+a3+a4+a5+a6+a7; | 140 bv[x] = a0+a1+a2+a3+a4+a5+a6+a7; |
| 138 } | 141 } |
| 139 print "\n" | 142 print "\n" |
| 140 print "/* Properties such as \"out2\" or \"jump\" that are specified in" | 143 print "/* Properties such as \"out2\" or \"jump\" that are specified in" |
| 141 print "** comments following the \"case\" for each opcode in the vdbe.c" | 144 print "** comments following the \"case\" for each opcode in the vdbe.c" |
| 142 print "** are encoded into bitvectors as follows:" | 145 print "** are encoded into bitvectors as follows:" |
| 143 print "*/" | 146 print "*/" |
| 144 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" | 147 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" |
| 145 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" | 148 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" |
| 146 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" | 149 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" |
| 147 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" | 150 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" |
| 148 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" | 151 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" |
| 149 print "#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */" | 152 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */" |
| 153 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */" |
| 150 print "#define OPFLG_INITIALIZER {\\" | 154 print "#define OPFLG_INITIALIZER {\\" |
| 151 for(i=0; i<=max; i++){ | 155 for(i=0; i<=max; i++){ |
| 152 if( i%8==0 ) printf("/* %3d */",i) | 156 if( i%8==0 ) printf("/* %3d */",i) |
| 153 printf " 0x%02x,", bv[i] | 157 printf " 0x%02x,", bv[i] |
| 154 if( i%8==7 ) printf("\\\n"); | 158 if( i%8==7 ) printf("\\\n"); |
| 155 } | 159 } |
| 156 print "}" | 160 print "}" |
| 157 } | 161 } |
| OLD | NEW |