blob: 44a12cc2420aad3f0b2f8f8cd14e172d7c5bdf76 [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
42 }
43 }
44}
45
46# Assign numbers to all opcodes and output the result.
47END {
48 cnt = 0
drhb327f772004-10-06 15:03:57 +000049 print "/* Automatically generated. Do not edit */"
50 print "/* See the mkopcodeh.awk script for details */"
drhf2bc0132004-10-04 13:19:23 +000051 for(name in op){
52 if( op[name]<0 ){
53 cnt++
54 while( used[cnt] ) cnt++
55 op[name] = cnt
56 }
57 printf "#define %-30s %d\n", name, op[name]
58 }
59}