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 # |
(...skipping 17 matching lines...) Expand all Loading... |
28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ | 28 # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ |
29 # | 29 # |
30 # When such comments are found on an opcode, it means that certain | 30 # When such comments are found on an opcode, it means that certain |
31 # properties apply to that opcode. Set corresponding flags using the | 31 # properties apply to that opcode. Set corresponding flags using the |
32 # OPFLG_INITIALIZER macro. | 32 # OPFLG_INITIALIZER macro. |
33 # | 33 # |
34 | 34 |
35 | 35 |
36 # Remember the TK_ values from the parse.h file | 36 # Remember the TK_ values from the parse.h file |
37 /^#define TK_/ { | 37 /^#define TK_/ { |
38 tk[$2] = 0+$3 | 38 tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X |
| 39 } |
| 40 |
| 41 # Find "/* Opcode: " lines in the vdbe.c file. Each one introduces |
| 42 # a new opcode. Remember which parameters are used. |
| 43 /^.. Opcode: / { |
| 44 currentOp = "OP_" $3 |
| 45 m = 0 |
| 46 for(i=4; i<=NF; i++){ |
| 47 x = $i |
| 48 if( x=="P1" ) m += 1 |
| 49 if( x=="P2" ) m += 2 |
| 50 if( x=="P3" ) m += 4 |
| 51 if( x=="P4" ) m += 8 |
| 52 if( x=="P5" ) m += 16 |
| 53 } |
| 54 paramused[currentOp] = m |
| 55 } |
| 56 |
| 57 # Find "** Synopsis: " lines that follow Opcode: |
| 58 /^.. Synopsis: / { |
| 59 if( currentOp ){ |
| 60 x = $3 |
| 61 for(i=4; i<=NF; i++){ |
| 62 x = x " " $i |
| 63 } |
| 64 synopsis[currentOp] = x |
| 65 } |
39 } | 66 } |
40 | 67 |
41 # Scan for "case OP_aaaa:" lines in the vdbe.c file | 68 # Scan for "case OP_aaaa:" lines in the vdbe.c file |
42 /^case OP_/ { | 69 /^case OP_/ { |
43 name = $2 | 70 name = $2 |
44 sub(/:/,"",name) | 71 sub(/:/,"",name) |
45 sub("\r","",name) | 72 sub("\r","",name) |
46 op[name] = -1 | 73 op[name] = -1 # op[x] holds the numeric value for OP symbol x |
47 jump[name] = 0 | 74 jump[name] = 0 |
48 out2_prerelease[name] = 0 | 75 out2_prerelease[name] = 0 |
49 in1[name] = 0 | 76 in1[name] = 0 |
50 in2[name] = 0 | 77 in2[name] = 0 |
51 in3[name] = 0 | 78 in3[name] = 0 |
52 out2[name] = 0 | 79 out2[name] = 0 |
53 out3[name] = 0 | 80 out3[name] = 0 |
54 for(i=3; i<NF; i++){ | 81 for(i=3; i<NF; i++){ |
55 if($i=="same" && $(i+1)=="as"){ | 82 if($i=="same" && $(i+1)=="as"){ |
56 sym = $(i+2) | 83 sym = $(i+2) |
57 sub(/,/,"",sym) | 84 sub(/,/,"",sym) |
58 op[name] = tk[sym] | 85 val = tk[sym] |
59 used[op[name]] = 1 | 86 op[name] = val |
60 sameas[op[name]] = sym | 87 used[val] = 1 |
| 88 sameas[val] = sym |
| 89 def[val] = name |
61 } | 90 } |
62 x = $i | 91 x = $i |
63 sub(",","",x) | 92 sub(",","",x) |
64 if(x=="jump"){ | 93 if(x=="jump"){ |
65 jump[name] = 1 | 94 jump[name] = 1 |
66 }else if(x=="out2-prerelease"){ | 95 }else if(x=="out2-prerelease"){ |
67 out2_prerelease[name] = 1 | 96 out2_prerelease[name] = 1 |
68 }else if(x=="in1"){ | 97 }else if(x=="in1"){ |
69 in1[name] = 1 | 98 in1[name] = 1 |
70 }else if(x=="in2"){ | 99 }else if(x=="in2"){ |
(...skipping 12 matching lines...) Expand all Loading... |
83 # Assign numbers to all opcodes and output the result. | 112 # Assign numbers to all opcodes and output the result. |
84 END { | 113 END { |
85 cnt = 0 | 114 cnt = 0 |
86 max = 0 | 115 max = 0 |
87 print "/* Automatically generated. Do not edit */" | 116 print "/* Automatically generated. Do not edit */" |
88 print "/* See the mkopcodeh.awk script for details */" | 117 print "/* See the mkopcodeh.awk script for details */" |
89 op["OP_Noop"] = -1; | 118 op["OP_Noop"] = -1; |
90 order[n_op++] = "OP_Noop"; | 119 order[n_op++] = "OP_Noop"; |
91 op["OP_Explain"] = -1; | 120 op["OP_Explain"] = -1; |
92 order[n_op++] = "OP_Explain"; | 121 order[n_op++] = "OP_Explain"; |
| 122 |
| 123 # Assign small values to opcodes that are processed by resolveP2Values() |
| 124 # to make code generation for the switch() statement smaller and faster. |
| 125 for(i=0; i<n_op; i++){ |
| 126 name = order[i]; |
| 127 if( op[name]>=0 ) continue; |
| 128 if( name=="OP_Function" \ |
| 129 || name=="OP_AggStep" \ |
| 130 || name=="OP_Transaction" \ |
| 131 || name=="OP_AutoCommit" \ |
| 132 || name=="OP_Savepoint" \ |
| 133 || name=="OP_Checkpoint" \ |
| 134 || name=="OP_Vacuum" \ |
| 135 || name=="OP_JournalMode" \ |
| 136 || name=="OP_VUpdate" \ |
| 137 || name=="OP_VFilter" \ |
| 138 || name=="OP_Next" \ |
| 139 || name=="OP_NextIfOpen" \ |
| 140 || name=="OP_SorterNext" \ |
| 141 || name=="OP_Prev" \ |
| 142 || name=="OP_PrevIfOpen" \ |
| 143 ){ |
| 144 cnt++ |
| 145 while( used[cnt] ) cnt++ |
| 146 op[name] = cnt |
| 147 used[cnt] = 1 |
| 148 def[cnt] = name |
| 149 } |
| 150 } |
| 151 |
| 152 # Generate the numeric values for opcodes |
93 for(i=0; i<n_op; i++){ | 153 for(i=0; i<n_op; i++){ |
94 name = order[i]; | 154 name = order[i]; |
95 if( op[name]<0 ){ | 155 if( op[name]<0 ){ |
96 cnt++ | 156 cnt++ |
97 while( used[cnt] ) cnt++ | 157 while( used[cnt] ) cnt++ |
98 op[name] = cnt | 158 op[name] = cnt |
| 159 used[cnt] = 1 |
| 160 def[cnt] = name |
99 } | 161 } |
100 used[op[name]] = 1; | 162 } |
101 if( op[name]>max ) max = op[name] | 163 max = cnt |
102 printf "#define %-25s %15d", name, op[name] | 164 for(i=1; i<=max; i++){ |
103 if( sameas[op[name]] ) { | 165 if( !used[i] ){ |
104 printf " /* same as %-12s*/", sameas[op[name]] | 166 def[i] = "OP_NotUsed_" i |
105 } | 167 } |
| 168 printf "#define %-16s %3d", def[i], i |
| 169 com = "" |
| 170 if( sameas[i] ){ |
| 171 com = "same as " sameas[i] |
| 172 } |
| 173 x = synopsis[def[i]] |
| 174 if( x ){ |
| 175 if( com=="" ){ |
| 176 com = "synopsis: " x |
| 177 } else { |
| 178 com = com ", synopsis: " x |
| 179 } |
| 180 } |
| 181 if( com!="" ){ |
| 182 printf " /* %-42s */", com |
| 183 } |
106 printf "\n" | 184 printf "\n" |
107 | |
108 } | |
109 seenUnused = 0; | |
110 for(i=1; i<max; i++){ | |
111 if( !used[i] ){ | |
112 if( !seenUnused ){ | |
113 printf "\n/* The following opcode values are never used */\n" | |
114 seenUnused = 1 | |
115 } | |
116 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i | |
117 } | |
118 } | 185 } |
119 | 186 |
120 # Generate the bitvectors: | 187 # Generate the bitvectors: |
121 # | 188 # |
122 # bit 0: jump | 189 # bit 0: jump |
123 # bit 1: pushes a result onto stack | 190 # bit 1: pushes a result onto stack |
124 # bit 2: output to p1. release p1 before opcode runs | 191 # bit 2: output to p1. release p1 before opcode runs |
125 # | 192 # |
126 for(i=0; i<=max; i++) bv[i] = 0; | 193 for(i=0; i<=max; i++){ |
127 for(i=0; i<n_op; i++){ | 194 name = def[i] |
128 name = order[i]; | |
129 x = op[name] | |
130 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 | 195 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0 |
131 # a7 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0 | |
132 if( jump[name] ) a0 = 1; | 196 if( jump[name] ) a0 = 1; |
133 if( out2_prerelease[name] ) a1 = 2; | 197 if( out2_prerelease[name] ) a1 = 2; |
134 if( in1[name] ) a2 = 4; | 198 if( in1[name] ) a2 = 4; |
135 if( in2[name] ) a3 = 8; | 199 if( in2[name] ) a3 = 8; |
136 if( in3[name] ) a4 = 16; | 200 if( in3[name] ) a4 = 16; |
137 if( out2[name] ) a5 = 32; | 201 if( out2[name] ) a5 = 32; |
138 if( out3[name] ) a6 = 64; | 202 if( out3[name] ) a6 = 64; |
139 # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15; | 203 bv[i] = a0+a1+a2+a3+a4+a5+a6+a7; |
140 bv[x] = a0+a1+a2+a3+a4+a5+a6+a7; | |
141 } | 204 } |
142 print "\n" | 205 print "\n" |
143 print "/* Properties such as \"out2\" or \"jump\" that are specified in" | 206 print "/* Properties such as \"out2\" or \"jump\" that are specified in" |
144 print "** comments following the \"case\" for each opcode in the vdbe.c" | 207 print "** comments following the \"case\" for each opcode in the vdbe.c" |
145 print "** are encoded into bitvectors as follows:" | 208 print "** are encoded into bitvectors as follows:" |
146 print "*/" | 209 print "*/" |
147 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" | 210 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */" |
148 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" | 211 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */" |
149 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" | 212 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */" |
150 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" | 213 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */" |
151 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" | 214 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */" |
152 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */" | 215 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */" |
153 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */" | 216 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */" |
154 print "#define OPFLG_INITIALIZER {\\" | 217 print "#define OPFLG_INITIALIZER {\\" |
155 for(i=0; i<=max; i++){ | 218 for(i=0; i<=max; i++){ |
156 if( i%8==0 ) printf("/* %3d */",i) | 219 if( i%8==0 ) printf("/* %3d */",i) |
157 printf " 0x%02x,", bv[i] | 220 printf " 0x%02x,", bv[i] |
158 if( i%8==7 ) printf("\\\n"); | 221 if( i%8==7 ) printf("\\\n"); |
159 } | 222 } |
160 print "}" | 223 print "}" |
| 224 if( 0 ){ |
| 225 print "\n/* Bitmask to indicate which fields (P1..P5) of each opcode are" |
| 226 print "** actually used.\n*/" |
| 227 print "#define OP_PARAM_USED_INITIALIZER {\\" |
| 228 for(i=0; i<=max; i++){ |
| 229 if( i%8==0 ) printf("/* %3d */",i) |
| 230 printf " 0x%02x,", paramused[def[i]] |
| 231 if( i%8==7 ) printf("\\\n"); |
| 232 } |
| 233 print "}" |
| 234 } |
161 } | 235 } |
OLD | NEW |