print.c
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:5k
源码类别:

其他游戏

开发平台:

Visual C++

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