print.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
  3. ** print bytecodes
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define LUAC_CROSS_FILE
  7. #include "luac_cross.h"
  8. #include C_HEADER_CTYPE
  9. #include C_HEADER_STDIO
  10. #define luac_c
  11. #define LUA_CORE
  12. #include "ldebug.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lundump.h"
  16. #define PrintFunction luaU_print
  17. #define Sizeof(x) ((int)sizeof(x))
  18. #define VOID(p) ((const void*)(p))
  19. static void PrintString(const TString* ts)
  20. {
  21. const char* s=getstr(ts);
  22. size_t i,n=ts->tsv.len;
  23. putchar('"');
  24. for (i=0; i<n; i++)
  25. {
  26. int c=s[i];
  27. switch (c)
  28. {
  29. case '"': printf("\\\""); break;
  30. case '\\': printf("\\\\"); break;
  31. case '\a': printf("\\a"); break;
  32. case '\b': printf("\\b"); break;
  33. case '\f': printf("\\f"); break;
  34. case '\n': printf("\\n"); break;
  35. case '\r': printf("\\r"); break;
  36. case '\t': printf("\\t"); break;
  37. case '\v': printf("\\v"); break;
  38. default: if (isprint((unsigned char)c))
  39. putchar(c);
  40. else
  41. printf("\\%03u",(unsigned char)c);
  42. }
  43. }
  44. putchar('"');
  45. }
  46. static void PrintConstant(const Proto* f, int i)
  47. {
  48. const TValue* o=&f->k[i];
  49. switch (ttype(o))
  50. {
  51. case LUA_TNIL:
  52. printf("nil");
  53. break;
  54. case LUA_TBOOLEAN:
  55. printf(bvalue(o) ? "true" : "false");
  56. break;
  57. case LUA_TNUMBER:
  58. printf(LUA_NUMBER_FMT,nvalue(o));
  59. break;
  60. case LUA_TSTRING:
  61. PrintString(rawtsvalue(o));
  62. break;
  63. default: /* cannot happen */
  64. printf("? type=%d",ttype(o));
  65. break;
  66. }
  67. }
  68. static void PrintCode(const Proto* f)
  69. {
  70. const Instruction* code=f->code;
  71. int pc,n=f->sizecode;
  72. for (pc=0; pc<n; pc++)
  73. {
  74. Instruction i=code[pc];
  75. OpCode o=GET_OPCODE(i);
  76. int a=GETARG_A(i);
  77. int b=GETARG_B(i);
  78. int c=GETARG_C(i);
  79. int bx=GETARG_Bx(i);
  80. int sbx=GETARG_sBx(i);
  81. int line=getline(f,pc);
  82. printf("\t%d\t",pc+1);
  83. if (line>0) printf("[%d]\t",line); else printf("[-]\t");
  84. printf("%-9s\t",luaP_opnames[o]);
  85. switch (getOpMode(o))
  86. {
  87. case iABC:
  88. printf("%d",a);
  89. if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
  90. if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
  91. break;
  92. case iABx:
  93. if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
  94. break;
  95. case iAsBx:
  96. if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
  97. break;
  98. }
  99. switch (o)
  100. {
  101. case OP_LOADK:
  102. printf("\t; "); PrintConstant(f,bx);
  103. break;
  104. case OP_GETUPVAL:
  105. case OP_SETUPVAL:
  106. printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
  107. break;
  108. case OP_GETGLOBAL:
  109. case OP_SETGLOBAL:
  110. printf("\t; %s",svalue(&f->k[bx]));
  111. break;
  112. case OP_GETTABLE:
  113. case OP_SELF:
  114. if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
  115. break;
  116. case OP_SETTABLE:
  117. case OP_ADD:
  118. case OP_SUB:
  119. case OP_MUL:
  120. case OP_DIV:
  121. case OP_POW:
  122. case OP_EQ:
  123. case OP_LT:
  124. case OP_LE:
  125. if (ISK(b) || ISK(c))
  126. {
  127. printf("\t; ");
  128. if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
  129. printf(" ");
  130. if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
  131. }
  132. break;
  133. case OP_JMP:
  134. case OP_FORLOOP:
  135. case OP_FORPREP:
  136. printf("\t; to %d",sbx+pc+2);
  137. break;
  138. case OP_CLOSURE:
  139. printf("\t; %p",VOID(f->p[bx]));
  140. break;
  141. case OP_SETLIST:
  142. if (c==0) printf("\t; %d",(int)code[++pc]);
  143. else printf("\t; %d",c);
  144. break;
  145. default:
  146. break;
  147. }
  148. printf("\n");
  149. }
  150. }
  151. #define SS(x) (x==1)?"":"s"
  152. #define S(x) x,SS(x)
  153. static void PrintHeader(const Proto* f)
  154. {
  155. const char* s=getstr(f->source);
  156. if (*s=='@' || *s=='=')
  157. s++;
  158. else if (*s==LUA_SIGNATURE[0])
  159. s="(bstring)";
  160. else
  161. s="(string)";
  162. printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
  163. (f->linedefined==0)?"main":"function",s,
  164. f->linedefined,f->lastlinedefined,
  165. S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
  166. printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
  167. f->numparams,f->is_vararg?"+":"",SS(f->numparams),
  168. S(f->maxstacksize),S(f->nups));
  169. printf("%d local%s, %d constant%s, %d function%s\n",
  170. S(f->sizelocvars),S(f->sizek),S(f->sizep));
  171. }
  172. static void PrintConstants(const Proto* f)
  173. {
  174. int i,n=f->sizek;
  175. printf("constants (%d) for %p:\n",n,VOID(f));
  176. for (i=0; i<n; i++)
  177. {
  178. printf("\t%d\t",i+1);
  179. PrintConstant(f,i);
  180. printf("\n");
  181. }
  182. }
  183. static void PrintLocals(const Proto* f)
  184. {
  185. int i,n=f->sizelocvars;
  186. printf("locals (%d) for %p:\n",n,VOID(f));
  187. for (i=0; i<n; i++)
  188. {
  189. printf("\t%d\t%s\t%d\t%d\n",
  190. i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
  191. }
  192. }
  193. static void PrintUpvalues(const Proto* f)
  194. {
  195. int i,n=f->sizeupvalues;
  196. printf("upvalues (%d) for %p:\n",n,VOID(f));
  197. if (f->upvalues==NULL) return;
  198. for (i=0; i<n; i++)
  199. {
  200. printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
  201. }
  202. }
  203. void PrintFunction(const Proto* f, int full)
  204. {
  205. int i,n=f->sizep;
  206. PrintHeader(f);
  207. PrintCode(f);
  208. if (full)
  209. {
  210. PrintConstants(f);
  211. PrintLocals(f);
  212. PrintUpvalues(f);
  213. }
  214. for (i=0; i<n; i++) PrintFunction(f->p[i],full);
  215. }