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