blob: 641b987a81e63764e8c9c84839d0f7049e68698a [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#
drh722e95a2004-10-25 20:33:44 +000017# We go to the trouble of making some OP_ value the same as TK_ values
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
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,
22# code to translation from one to the other is avoided. This makes the
23# code generator run (infinitesimally) faster and more importantly it makes
24# the total library smaller.
25#
drhf2bc0132004-10-04 13:19:23 +000026
27# Remember the TK_ values from the parse.h file
28/^#define TK_/ {
29 tk[$2] = $3
30}
31
32# Scan for "case OP_aaaa:" lines in the vdbe.c file
33/^case OP_/ {
34 name = $2
35 gsub(/:/,"",name)
drha1d65d02004-10-10 19:11:35 +000036 gsub("\r","",name)
drhf2bc0132004-10-04 13:19:23 +000037 op[name] = -1
38 for(i=3; i<NF-2; i++){
39 if($i=="same" && $(i+1)=="as"){
40 op[name] = tk[$(i+2)]
41 used[op[name]] = 1
drh0602c2e2005-01-21 17:07:22 +000042 sameas[op[name]] = $(i+2)
drhf2bc0132004-10-04 13:19:23 +000043 }
44 }
45}
46
47# Assign numbers to all opcodes and output the result.
48END {
49 cnt = 0
drhdaa28ff2004-12-10 17:17:18 +000050 max = 0
drhb327f772004-10-06 15:03:57 +000051 print "/* Automatically generated. Do not edit */"
52 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000053 for(name in op){
54 if( op[name]<0 ){
55 cnt++
56 while( used[cnt] ) cnt++
57 op[name] = cnt
58 }
drhdaa28ff2004-12-10 17:17:18 +000059 used[op[name]] = 1;
60 if( op[name]>max ) max = op[name]
drh0602c2e2005-01-21 17:07:22 +000061 printf "#define %-25s %15d", name, op[name]
62 if( sameas[op[name]] ) {
danielk197724c8ab82005-02-09 01:40:23 +000063 printf " /* same as %-12s*/", sameas[op[name]]
drh0602c2e2005-01-21 17:07:22 +000064 }
65 printf "\n"
66
drhf2bc0132004-10-04 13:19:23 +000067 }
drhdaa28ff2004-12-10 17:17:18 +000068 seenUnused = 0;
69 for(i=1; i<max; i++){
70 if( !used[i] ){
71 if( !seenUnused ){
72 printf "\n/* The following opcode values are never used */\n"
73 seenUnused = 1
74 }
drh0602c2e2005-01-21 17:07:22 +000075 printf "#define %-25s %15d\n", sprintf( "OP_NotUsed_%-3d", i ), i
drhdaa28ff2004-12-10 17:17:18 +000076 }
77 }
drhf2bc0132004-10-04 13:19:23 +000078}