blob: 00823d5e4b7d1fba383307240da77b92b9aa875e [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#
28# case OP_aaaa: /* no-push */
29#
30# When the no-push comment is found on an opcode, it means that that
drh0ed4fcd2006-01-26 14:29:58 +000031# opcode does not leave a result on the stack. By identifying which
drhfa3b19e2005-11-24 22:22:29 +000032# opcodes leave results on the stack it is possible to determine a
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#
drhf2bc0132004-10-04 13:19:23 +000040
danielk1977bc04f852005-03-29 08:26:13 +000041
drhf2bc0132004-10-04 13:19:23 +000042# Remember the TK_ values from the parse.h file
43/^#define TK_/ {
drh0a99ded2007-10-12 18:36:26 +000044 tk[$2] = 0+$3
drhf2bc0132004-10-04 13:19:23 +000045}
46
47# Scan for "case OP_aaaa:" lines in the vdbe.c file
48/^case OP_/ {
49 name = $2
drhb726ee62005-09-05 20:35:25 +000050 sub(/:/,"",name)
51 sub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000052 op[name] = -1
drh3a40f692008-01-04 16:50:09 +000053 jump[name] = 0
danielk1977287fb612008-01-04 19:10:28 +000054 nopush[name] = 0
drh4c583122008-01-04 22:01:03 +000055 out2_prerelease[name] = 0
danielk1977bc04f852005-03-29 08:26:13 +000056 for(i=3; i<NF; i++){
drhf2bc0132004-10-04 13:19:23 +000057 if($i=="same" && $(i+1)=="as"){
danielk1977bc04f852005-03-29 08:26:13 +000058 sym = $(i+2)
59 sub(/,/,"",sym)
60 op[name] = tk[sym]
drhf2bc0132004-10-04 13:19:23 +000061 used[op[name]] = 1
danielk1977bc04f852005-03-29 08:26:13 +000062 sameas[op[name]] = sym
63 }
drh4aeb7bf2008-01-04 19:12:35 +000064 x = $i
65 sub(",","",x)
66 if(x=="no-push"){
danielk19777a5147c2005-03-29 13:07:00 +000067 nopush[name] = 1
drh4aeb7bf2008-01-04 19:12:35 +000068 }else if(x=="jump"){
drh3a40f692008-01-04 16:50:09 +000069 jump[name] = 1
drh4c583122008-01-04 22:01:03 +000070 }else if(x=="out2-prerelease"){
71 out2_prerelease[name] = 1
drhf2bc0132004-10-04 13:19:23 +000072 }
73 }
74}
75
76# Assign numbers to all opcodes and output the result.
77END {
78 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000079 max = 0
drhb327f772004-10-06 15:03:57 +000080 print "/* Automatically generated. Do not edit */"
81 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000082 for(name in op){
83 if( op[name]<0 ){
84 cnt++
85 while( used[cnt] ) cnt++
86 op[name] = cnt
87 }
drhdaa28ff2004-12-10 17:17:18 +000088 used[op[name]] = 1;
89 if( op[name]>max ) max = op[name]
drh0602c2e2005-01-21 17:07:22 +000090 printf "#define %-25s %15d", name, op[name]
91 if( sameas[op[name]] ) {
danielk197724c8ab82005-02-09 01:40:23 +000092 printf " /* same as %-12s*/", sameas[op[name]]
drh0602c2e2005-01-21 17:07:22 +000093 }
94 printf "\n"
95
drhf2bc0132004-10-04 13:19:23 +000096 }
drhdaa28ff2004-12-10 17:17:18 +000097 seenUnused = 0;
98 for(i=1; i<max; i++){
99 if( !used[i] ){
100 if( !seenUnused ){
101 printf "\n/* The following opcode values are never used */\n"
102 seenUnused = 1
103 }
drh0602c2e2005-01-21 17:07:22 +0000104 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
drhdaa28ff2004-12-10 17:17:18 +0000105 }
106 }
danielk1977bc04f852005-03-29 08:26:13 +0000107
drh3a40f692008-01-04 16:50:09 +0000108 # Generate the bitvectors:
109 #
110 # bit 0: jump
drh4c583122008-01-04 22:01:03 +0000111 # bit 1: pushes a result onto stack
112 # bit 2: output to p1. release p1 before opcode runs
drh3a40f692008-01-04 16:50:09 +0000113 #
114 for(i=0; i<=max; i++) bv[i] = 0;
danielk1977bc04f852005-03-29 08:26:13 +0000115 for(name in op){
drh4aeb7bf2008-01-04 19:12:35 +0000116 x = op[name]
117 a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
118 if( jump[name] ) a0 = 1;
drh4c583122008-01-04 22:01:03 +0000119 if( nopush[name]==0 ) a1 = 2;
120 if( out2_prerelease[name] ) a2 = 4;
drh4aeb7bf2008-01-04 19:12:35 +0000121 bv[x] = a0+a1+a2+a3+a4+a5+a6+a7;
danielk1977bc04f852005-03-29 08:26:13 +0000122 }
drh3a40f692008-01-04 16:50:09 +0000123 print "\n"
124 print "/* Properties such as \"out2\" or \"jump\" that are specified in"
125 print "** comments following the "case" for each opcode in the vdbe.c"
126 print "** are encoded into bitvectors as follows:"
127 print "*/"
drh4c583122008-01-04 22:01:03 +0000128 print "#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */"
129 print "#define OPFLG_PUSH 0x02 /* ~no-push: Does not push */"
130 print "#define OPFLG_OUT2_PRERELEASE 0x04 /* out2-prerelease: */"
drh3a40f692008-01-04 16:50:09 +0000131 print "#define OPFLG_INITIALIZER {\\"
132 for(i=0; i<=max; i++){
133 printf " 0x%02x,", bv[i]
drh4c583122008-01-04 22:01:03 +0000134 if( i%8==7 ) printf("\\\n");
danielk1977bc04f852005-03-29 08:26:13 +0000135 }
drh3a40f692008-01-04 16:50:09 +0000136 print "}"
drhf2bc0132004-10-04 13:19:23 +0000137}