1
0

mkopcodeh.awk 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/awk -f
  2. #
  3. # Generate the file opcodes.h.
  4. #
  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
  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. #
  17. # We go to the trouble of making some OP_ values 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 translate from one to the other is avoided. This makes the
  23. # code generator run (infinitesimally) faster and more importantly it makes
  24. # the library footprint smaller.
  25. #
  26. # This script also scans for lines of the form:
  27. #
  28. # case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */
  29. #
  30. # 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.
  33. #
  34. # Remember the TK_ values from the parse.h file
  35. /^#define TK_/ {
  36. tk[$2] = 0+$3 # tk[x] holds the numeric value for TK symbol X
  37. }
  38. # Scan for "case OP_aaaa:" lines in the vdbe.c file
  39. /^case OP_/ {
  40. name = $2
  41. sub(/:/,"",name)
  42. sub("\r","",name)
  43. op[name] = -1 # op[x] holds the numeric value for OP symbol x
  44. jump[name] = 0
  45. out2_prerelease[name] = 0
  46. in1[name] = 0
  47. in2[name] = 0
  48. in3[name] = 0
  49. out2[name] = 0
  50. out3[name] = 0
  51. for(i=3; i<NF; i++){
  52. if($i=="same" && $(i+1)=="as"){
  53. sym = $(i+2)
  54. sub(/,/,"",sym)
  55. val = tk[sym]
  56. op[name] = val
  57. used[val] = 1
  58. sameas[val] = sym
  59. def[val] = name
  60. }
  61. x = $i
  62. sub(",","",x)
  63. if(x=="jump"){
  64. jump[name] = 1
  65. }else if(x=="out2-prerelease"){
  66. out2_prerelease[name] = 1
  67. }else if(x=="in1"){
  68. in1[name] = 1
  69. }else if(x=="in2"){
  70. in2[name] = 1
  71. }else if(x=="in3"){
  72. in3[name] = 1
  73. }else if(x=="out2"){
  74. out2[name] = 1
  75. }else if(x=="out3"){
  76. out3[name] = 1
  77. }
  78. }
  79. order[n_op++] = name;
  80. }
  81. # Assign numbers to all opcodes and output the result.
  82. END {
  83. cnt = 0
  84. max = 0
  85. print "/* Automatically generated. Do not edit */"
  86. print "/* See the mkopcodeh.awk script for details */"
  87. op["OP_Noop"] = -1;
  88. order[n_op++] = "OP_Noop";
  89. op["OP_Explain"] = -1;
  90. order[n_op++] = "OP_Explain";
  91. # Assign small values to opcodes that are processed by resolveP2Values()
  92. # to make code generation for the switch() statement smaller and faster.
  93. for(i=0; i<n_op; i++){
  94. name = order[i];
  95. if( op[name]>=0 ) continue;
  96. if( name=="OP_Function" \
  97. || name=="OP_AggStep" \
  98. || name=="OP_Transaction" \
  99. || name=="OP_AutoCommit" \
  100. || name=="OP_Savepoint" \
  101. || name=="OP_Checkpoint" \
  102. || name=="OP_Vacuum" \
  103. || name=="OP_JournalMode" \
  104. || name=="OP_VUpdate" \
  105. || name=="OP_VFilter" \
  106. || name=="OP_Next" \
  107. || name=="OP_SorterNext" \
  108. || name=="OP_Prev" \
  109. ){
  110. cnt++
  111. while( used[cnt] ) cnt++
  112. op[name] = cnt
  113. used[cnt] = 1
  114. def[cnt] = name
  115. }
  116. }
  117. # Generate the numeric values for opcodes
  118. for(i=0; i<n_op; i++){
  119. name = order[i];
  120. if( op[name]<0 ){
  121. cnt++
  122. while( used[cnt] ) cnt++
  123. op[name] = cnt
  124. used[cnt] = 1
  125. def[cnt] = name
  126. }
  127. }
  128. max = cnt
  129. for(i=1; i<=max; i++){
  130. if( !used[i] ){
  131. def[i] = "OP_NotUsed_" i
  132. }
  133. printf "#define %-25s %15d", def[i], i
  134. if( sameas[i] ){
  135. printf " /* same as %-12s*/", sameas[i]
  136. }
  137. printf "\n"
  138. }
  139. # Generate the bitvectors:
  140. #
  141. # bit 0: jump
  142. # bit 1: pushes a result onto stack
  143. # bit 2: output to p1. release p1 before opcode runs
  144. #
  145. for(i=0; i<=max; i++){
  146. name = def[i]
  147. a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
  148. if( jump[name] ) a0 = 1;
  149. if( out2_prerelease[name] ) a1 = 2;
  150. if( in1[name] ) a2 = 4;
  151. if( in2[name] ) a3 = 8;
  152. if( in3[name] ) a4 = 16;
  153. if( out2[name] ) a5 = 32;
  154. if( out3[name] ) a6 = 64;
  155. bv[i] = a0+a1+a2+a3+a4+a5+a6+a7;
  156. }
  157. print "\n"
  158. print "/* Properties such as \"out2\" or \"jump\" that are specified in"
  159. print "** comments following the \"case\" for each opcode in the vdbe.c"
  160. print "** are encoded into bitvectors as follows:"
  161. print "*/"
  162. print "#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */"
  163. print "#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */"
  164. print "#define OPFLG_IN1 0x0004 /* in1: P1 is an input */"
  165. print "#define OPFLG_IN2 0x0008 /* in2: P2 is an input */"
  166. print "#define OPFLG_IN3 0x0010 /* in3: P3 is an input */"
  167. print "#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */"
  168. print "#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */"
  169. print "#define OPFLG_INITIALIZER {\\"
  170. for(i=0; i<=max; i++){
  171. if( i%8==0 ) printf("/* %3d */",i)
  172. printf " 0x%02x,", bv[i]
  173. if( i%8==7 ) printf("\\\n");
  174. }
  175. print "}"
  176. }