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