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