blob: f6b90c114dde30b6f5f18c5c7007100396d3bfe2 [file] [log] [blame]
drhf2bc0132004-10-04 13:19:23 +00001#!/usr/bin/awk -f
2#
drh722e95a2004-10-25 20:33:44 +00003# Generate the file opcodes.h.
4#
drhf2bc0132004-10-04 13:19:23 +00005# 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
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#
drh0ed4fcd2006-01-26 14:29:58 +000017# We go to the trouble of making some OP_ values the same as TK_ values
drh722e95a2004-10-25 20:33:44 +000018# 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,
drh0ed4fcd2006-01-26 14:29:58 +000022# code to translate from one to the other is avoided. This makes the
drh722e95a2004-10-25 20:33:44 +000023# code generator run (infinitesimally) faster and more importantly it makes
drh0ed4fcd2006-01-26 14:29:58 +000024# the library footprint smaller.
drh722e95a2004-10-25 20:33:44 +000025#
drhfa3b19e2005-11-24 22:22:29 +000026# This script also scans for lines of the form:
27#
drhb24a2002009-11-02 18:14:50 +000028# case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */
drhfa3b19e2005-11-24 22:22:29 +000029#
drhb24a2002009-11-02 18:14:50 +000030# 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.
drhfa3b19e2005-11-24 22:22:29 +000033#
drhf2bc0132004-10-04 13:19:23 +000034
danielk1977bc04f852005-03-29 08:26:13 +000035
drhf2bc0132004-10-04 13:19:23 +000036# Remember the TK_ values from the parse.h file
37/^#define TK_/ {
drh0a99ded2007-10-12 18:36:26 +000038 tk[$2] = 0+$3
drhf2bc0132004-10-04 13:19:23 +000039}
40
41# Scan for "case OP_aaaa:" lines in the vdbe.c file
42/^case OP_/ {
43 name = $2
drhb726ee62005-09-05 20:35:25 +000044 sub(/:/,"",name)
45 sub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000046 op[name] = -1
drh3a40f692008-01-04 16:50:09 +000047 jump[name] = 0
drh4c583122008-01-04 22:01:03 +000048 out2_prerelease[name] = 0
drhb1fdb2a2008-01-05 04:06:03 +000049 in1[name] = 0
50 in2[name] = 0
51 in3[name] = 0
drh93952eb2009-11-13 19:43:43 +000052 out2[name] = 0
drhb1fdb2a2008-01-05 04:06:03 +000053 out3[name] = 0
danielk1977bc04f852005-03-29 08:26:13 +000054 for(i=3; i<NF; i++){
drhf2bc0132004-10-04 13:19:23 +000055 if($i=="same" && $(i+1)=="as"){
danielk1977bc04f852005-03-29 08:26:13 +000056 sym = $(i+2)
57 sub(/,/,"",sym)
58 op[name] = tk[sym]
drhf2bc0132004-10-04 13:19:23 +000059 used[op[name]] = 1
danielk1977bc04f852005-03-29 08:26:13 +000060 sameas[op[name]] = sym
61 }
drh4aeb7bf2008-01-04 19:12:35 +000062 x = $i
63 sub(",","",x)
drh9cbf3422008-01-17 16:22:13 +000064 if(x=="jump"){
drh3a40f692008-01-04 16:50:09 +000065 jump[name] = 1
drh4c583122008-01-04 22:01:03 +000066 }else if(x=="out2-prerelease"){
67 out2_prerelease[name] = 1
drhb1fdb2a2008-01-05 04:06:03 +000068 }else if(x=="in1"){
69 in1[name] = 1
70 }else if(x=="in2"){
71 in2[name] = 1
72 }else if(x=="in3"){
73 in3[name] = 1
drh93952eb2009-11-13 19:43:43 +000074 }else if(x=="out2"){
75 out2[name] = 1
drhb1fdb2a2008-01-05 04:06:03 +000076 }else if(x=="out3"){
77 out3[name] = 1
drhf2bc0132004-10-04 13:19:23 +000078 }
79 }
drhb24a2002009-11-02 18:14:50 +000080 order[n_op++] = name;
drhf2bc0132004-10-04 13:19:23 +000081}
82
83# Assign numbers to all opcodes and output the result.
84END {
85 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000086 max = 0
drhb327f772004-10-06 15:03:57 +000087 print "/* Automatically generated. Do not edit */"
88 print "/* See the mkopcodeh.awk script for details */"
drh91fd4d42008-01-19 20:11:25 +000089 op["OP_Noop"] = -1;
drhccaf7732009-11-02 18:44:58 +000090 order[n_op++] = "OP_Noop";
drh91fd4d42008-01-19 20:11:25 +000091 op["OP_Explain"] = -1;
drhccaf7732009-11-02 18:44:58 +000092 order[n_op++] = "OP_Explain";
drhb24a2002009-11-02 18:14:50 +000093 for(i=0; i<n_op; i++){
94 name = order[i];
drhf2bc0132004-10-04 13:19:23 +000095 if( op[name]<0 ){
96 cnt++
97 while( used[cnt] ) cnt++
98 op[name] = cnt
99 }
drhdaa28ff2004-12-10 17:17:18 +0000100 used[op[name]] = 1;
101 if( op[name]>max ) max = op[name]
drh0602c2e2005-01-21 17:07:22 +0000102 printf "#define %-25s %15d", name, op[name]
103 if( sameas[op[name]] ) {
danielk197724c8ab82005-02-09 01:40:23 +0000104 printf " /* same as %-12s*/", sameas[op[name]]
drh0602c2e2005-01-21 17:07:22 +0000105 }
106 printf "\n"
107
drhf2bc0132004-10-04 13:19:23 +0000108 }
drhdaa28ff2004-12-10 17:17:18 +0000109 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 }
drh0602c2e2005-01-21 17:07:22 +0000116 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
drhdaa28ff2004-12-10 17:17:18 +0000117 }
118 }
danielk1977bc04f852005-03-29 08:26:13 +0000119
drh3a40f692008-01-04 16:50:09 +0000120 # Generate the bitvectors:
121 #
122 # bit 0: jump
drh4c583122008-01-04 22:01:03 +0000123 # bit 1: pushes a result onto stack
124 # bit 2: output to p1. release p1 before opcode runs
drh3a40f692008-01-04 16:50:09 +0000125 #
126 for(i=0; i<=max; i++) bv[i] = 0;
drhb24a2002009-11-02 18:14:50 +0000127 for(i=0; i<n_op; i++){
128 name = order[i];
drh4aeb7bf2008-01-04 19:12:35 +0000129 x = op[name]
130 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
drh93952eb2009-11-13 19:43:43 +0000131 # a7 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0
drh4aeb7bf2008-01-04 19:12:35 +0000132 if( jump[name] ) a0 = 1;
drh9cbf3422008-01-17 16:22:13 +0000133 if( out2_prerelease[name] ) a1 = 2;
134 if( in1[name] ) a2 = 4;
135 if( in2[name] ) a3 = 8;
136 if( in3[name] ) a4 = 16;
drh93952eb2009-11-13 19:43:43 +0000137 if( out2[name] ) a5 = 32;
138 if( out3[name] ) a6 = 64;
drh9cbf3422008-01-17 16:22:13 +0000139 # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15;
drhca48c902008-01-18 14:08:24 +0000140 bv[x] = a0+a1+a2+a3+a4+a5+a6+a7;
danielk1977bc04f852005-03-29 08:26:13 +0000141 }
drh3a40f692008-01-04 16:50:09 +0000142 print "\n"
143 print "/* Properties such as \"out2\" or \"jump\" that are specified in"
drh3de120b2008-01-10 00:08:43 +0000144 print "** comments following the \"case\" for each opcode in the vdbe.c"
drh3a40f692008-01-04 16:50:09 +0000145 print "** are encoded into bitvectors as follows:"
146 print "*/"
drh5b6afba2008-01-05 16:29:28 +0000147 print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */"
drh9cbf3422008-01-17 16:22:13 +0000148 print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */"
149 print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */"
150 print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */"
151 print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */"
drh93952eb2009-11-13 19:43:43 +0000152 print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */"
153 print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */"
drh3a40f692008-01-04 16:50:09 +0000154 print "#define OPFLG_INITIALIZER {\\"
155 for(i=0; i<=max; i++){
drh5b6afba2008-01-05 16:29:28 +0000156 if( i%8==0 ) printf("/* %3d */",i)
drh9cbf3422008-01-17 16:22:13 +0000157 printf " 0x%02x,", bv[i]
drh4c583122008-01-04 22:01:03 +0000158 if( i%8==7 ) printf("\\\n");
danielk1977bc04f852005-03-29 08:26:13 +0000159 }
drh3a40f692008-01-04 16:50:09 +0000160 print "}"
drhf2bc0132004-10-04 13:19:23 +0000161}