Imported from ../lua-3.1.tar.gz.
[lua.git] / src / luac / print.c
blobce98539015239d7b74033bfb4cf49a8cb5dad90f
1 /*
2 ** $Id: print.c,v 1.13 1998/07/12 00:17:37 lhf Exp $
3 ** print bytecodes
4 ** See Copyright Notice in lua.h
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "luac.h"
11 #ifdef DEBUG
12 void PrintConstant1(TProtoFunc* tf, int i)
14 TObject* o=tf->consts+i;
15 printf("%6d ",i);
16 if (i<0 || i>=tf->nconsts)
17 printf("(bad constant #%d: max=%d)",i,tf->nconsts);
18 else
19 switch (ttype(o))
21 case LUA_T_NUMBER:
22 printf("N " NUMBER_FMT "\n",nvalue(o)); /* LUA_NUMBER */
23 break;
24 case LUA_T_STRING:
25 printf("S %p\t\"%s\"\n",(void*)tsvalue(o),svalue(o));
26 break;
27 case LUA_T_PROTO:
28 printf("F %p\n",(void*)tfvalue(o));
29 break;
30 default: /* cannot happen */
31 printf("? %d\n",ttype(o));
32 break;
36 static void PrintConstants(TProtoFunc* tf)
38 int i,n=tf->nconsts;
39 printf("constants (%d):\n",n);
40 for (i=0; i<n; i++) PrintConstant1(tf,i);
42 #endif
44 static void PrintConstant(TProtoFunc* tf, int i)
46 if (i<0 || i>=tf->nconsts)
47 printf("(bad constant #%d: max=%d)",i,tf->nconsts);
48 else
50 TObject* o=tf->consts+i;
51 switch (ttype(o))
53 case LUA_T_NUMBER:
54 printf(NUMBER_FMT,nvalue(o)); /* LUA_NUMBER */
55 break;
56 case LUA_T_STRING:
57 printf("\"%s\"",svalue(o));
58 break;
59 case LUA_T_PROTO:
60 printf("function at %p",(void*)tfvalue(o));
61 break;
62 case LUA_T_NIL:
63 printf("(nil)");
64 break;
65 default: /* cannot happen */
66 printf("(bad constant #%d: type=%d [%s])\n",i,ttype(o),luaO_typename(o));
67 break;
72 #define VarStr(i) svalue(tf->consts+i)
74 static void PrintCode(TProtoFunc* tf)
76 Byte* code=tf->code;
77 Byte* p=code;
78 int line=0;
79 while (1)
81 Opcode OP;
82 int n=INFO(tf,p,&OP);
83 int op=OP.op;
84 int i=OP.arg;
85 printf("%6d ",(int)(p-code));
87 Byte* q=p;
88 int j=n;
89 while (j--) printf("%02X",*q++);
91 printf("%*s%-13s",2*(5-n),"",OP.name);
93 if (n!=1 || op<0) printf("\t%d",i); else if (i>=0) printf("\t");
95 switch (OP.class)
98 case ENDCODE:
99 printf("\n");
100 return;
102 case CLOSURE:
103 printf(" %d",OP.arg2);
104 case PUSHCONSTANT:
105 case GETDOTTED:
106 case PUSHSELF:
107 printf("\t; ");
108 PrintConstant(tf,i);
109 break;
111 case PUSHLOCAL:
112 case SETLOCAL:
114 char* s=luaF_getlocalname(tf,i+1,line);
115 if (s) printf("\t; %s",s);
116 break;
119 case GETGLOBAL:
120 case SETGLOBAL:
121 printf("\t; %s",VarStr(i));
122 break;
124 case SETLIST:
125 case CALLFUNC:
126 if (n>=3) printf(" %d",OP.arg2);
127 break;
129 case SETLINE:
130 printf("\t; \"%s\":%d",fileName(tf),line=i);
131 break;
133 /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
134 case IFTUPJMP:
135 case IFFUPJMP:
136 i=-i;
137 case ONTJMP:
138 case ONFJMP:
139 case JMP:
140 case IFFJMP:
141 printf("\t; to %d",(int)(p-code)+i+n);
142 break;
145 printf("\n");
146 p+=n;
150 static void PrintLocals(TProtoFunc* tf)
152 LocVar* v=tf->locvars;
153 int n,i=0;
154 if (v==NULL || v->varname==NULL) return;
155 n=tf->code[1]; if (n>=ZEROVARARG) n-=ZEROVARARG;
157 printf("locals:");
158 if (n>0)
160 for (i=0; i<n; v++,i++) printf(" %s",v->varname->str);
162 if (v->varname!=NULL)
164 for (; v->line>=0; v++)
166 if (v->varname==NULL)
168 printf(")"); --i;
170 else
172 printf(" (%s",v->varname->str); i++;
175 i-=n;
176 while (i--) printf(")");
178 printf("\n");
181 static void PrintHeader(TProtoFunc* tf, TProtoFunc* Main, int at)
183 int size=CodeSize(tf);
184 if (IsMain(tf))
185 printf("\nmain of \"%s\" (%d bytes at %p)\n",fileName(tf),size,(void*)tf);
186 else if (Main)
188 printf("\nfunction defined at \"%s\":%d (%d bytes at %p); used at ",
189 fileName(tf),tf->lineDefined,size,(void*)tf);
190 if (IsMain(Main))
191 printf("main");
192 else
193 printf("%p",(void*)Main);
194 printf("+%d\n",at);
198 static void PrintFunction(TProtoFunc* tf, TProtoFunc* Main, int at);
200 static void PrintFunctions(TProtoFunc* Main)
202 Byte* code=Main->code;
203 Byte* p=code;
204 while (1)
206 Opcode OP;
207 int n=INFO(Main,p,&OP);
208 if (OP.class==ENDCODE) break;
209 if (OP.class==PUSHCONSTANT || OP.class==CLOSURE)
211 int i=OP.arg;
212 TObject* o=Main->consts+i;
213 if (ttype(o)==LUA_T_PROTO) PrintFunction(tfvalue(o),Main,(int)(p-code));
215 p+=n;
219 static void PrintFunction(TProtoFunc* tf, TProtoFunc* Main, int at)
221 PrintHeader(tf,Main,at);
222 PrintLocals(tf);
223 PrintCode(tf);
224 #ifdef DEBUG
225 PrintConstants(tf);
226 #endif
227 PrintFunctions(tf);
230 void PrintChunk(TProtoFunc* Main)
232 PrintFunction(Main,0,0);