Imported from ../lua-3.2.tar.gz.
[lua.git] / src / luac / print.c
blobb1ee8934b9dbcb8ed0af59ff00c11626c44dfe81
1 /*
2 ** $Id: print.c,v 1.21 1999/05/25 19:58:55 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 static void PrintConstants(TProtoFunc* tf)
14 int i,n=tf->nconsts;
15 printf("constants (%d) for %p:\n",n,tf);
16 for (i=0; i<n; i++)
18 TObject* o=tf->consts+i;
19 printf("%6d ",i);
20 switch (ttype(o))
22 case LUA_T_NUMBER:
23 printf("N " NUMBER_FMT "\n",(double)nvalue(o));
24 break;
25 case LUA_T_STRING:
26 printf("S %p\t\"%s\"\n",tsvalue(o),svalue(o));
27 break;
28 case LUA_T_PROTO:
29 printf("F %p\n",tfvalue(o));
30 break;
31 case LUA_T_NIL:
32 printf("nil\n");
33 break;
34 default: /* cannot happen */
35 printf("? type=%d\n",ttype(o));
36 break;
40 #endif
42 static void PrintConstant(TProtoFunc* tf, int i, int at)
44 TObject* o=luaU_getconstant(tf,i,at);
45 switch (ttype(o))
47 case LUA_T_NUMBER:
48 printf(NUMBER_FMT,(double)nvalue(o));
49 break;
50 case LUA_T_STRING:
51 printf("\"%s\"",svalue(o));
52 break;
53 case LUA_T_PROTO:
54 printf("function at %p",(void*)tfvalue(o));
55 break;
56 case LUA_T_NIL:
57 printf("(nil)");
58 break;
59 default: /* cannot happen */
60 luaU_badconstant("print",i,o,tf);
61 break;
65 static void PrintCode(TProtoFunc* tf)
67 Byte* code=tf->code;
68 Byte* p=code;
69 int line=0;
70 int longarg=0;
71 for (;;)
73 Opcode OP;
74 int n=INFO(tf,p,&OP);
75 int i=OP.arg+longarg;
76 int at=p-code;
77 longarg=0;
78 printf("%6d ",at);
80 Byte* q=p;
81 int j=n;
82 while (j--) printf("%02X",*q++);
84 printf("%*s%-14s ",2*(5-n),"",OP.name);
85 if (OP.arg >=0) printf("%d",i);
86 if (OP.arg2>=0) printf(" %d",OP.arg2);
88 switch (OP.class)
91 case ENDCODE:
92 printf("\n");
93 return;
95 case PUSHCONSTANT:
96 case GETGLOBAL:
97 case SETGLOBAL:
98 case GETDOTTED:
99 case PUSHSELF:
100 case CLOSURE:
101 printf("\t; ");
102 PrintConstant(tf,i,at);
103 break;
105 case PUSHLOCAL:
106 case SETLOCAL:
108 char* s=luaF_getlocalname(tf,i+1,line);
109 if (s) printf("\t; %s",s);
110 break;
113 case SETLINE:
114 printf("\t; " SOURCE,tf->source->str,line=i);
115 break;
117 case LONGARG:
118 longarg=i<<16;
119 break;
121 /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
122 case ONTJMP:
123 case ONFJMP:
124 case JMP:
125 case IFFJMP:
126 printf("\t; to %d",at+i+n);
127 break;
128 case IFTUPJMP:
129 case IFFUPJMP:
130 printf("\t; to %d",at-i+n);
131 break;
134 printf("\n");
135 p+=n;
139 static void PrintLocals(TProtoFunc* tf)
141 LocVar* v=tf->locvars;
142 int n,i;
143 if (v==NULL || v->line<0) return;
144 n=tf->code[1]; if (n>=ZEROVARARG) n-=ZEROVARARG;
145 printf("locals:");
146 for (i=0; i<n; v++,i++) /* arguments */
147 printf(" %s",v->varname->str);
148 for (; v->line>=0; v++)
150 if (v->varname==NULL)
152 --i; if (i<0) luaL_verror("bad locvars[%d]",v-tf->locvars); else printf(")");
154 else
156 ++i; printf(" (%s",v->varname->str);
159 i-=n;
160 while (i--) printf(")");
161 printf("\n");
164 #define IsMain(tf) (tf->lineDefined==0)
166 static void PrintHeader(TProtoFunc* tf, TProtoFunc* Main, int at)
168 int size=luaU_codesize(tf);
169 if (IsMain(tf))
170 printf("\nmain " SOURCE " (%d bytes at %p)\n",
171 tf->source->str,tf->lineDefined,size,tf);
172 else
174 printf("\nfunction " SOURCE " (%d bytes at %p); used at ",
175 tf->source->str,tf->lineDefined,size,tf);
176 if (Main && IsMain(Main))
177 printf("main");
178 else
179 printf("%p",Main);
180 printf("+%d\n",at);
184 static void PrintFunction(TProtoFunc* tf, TProtoFunc* Main, int at);
186 static void PrintFunctions(TProtoFunc* Main)
188 Byte* code=Main->code;
189 Byte* p=code;
190 int longarg=0;
191 for (;;)
193 Opcode OP;
194 int n=INFO(Main,p,&OP);
195 int op=OP.class;
196 int i=OP.arg+longarg;
197 longarg=0;
198 if (op==PUSHCONSTANT || op==CLOSURE)
200 TObject* o=Main->consts+i;
201 if (ttype(o)==LUA_T_PROTO) PrintFunction(tfvalue(o),Main,(int)(p-code));
203 else if (op==LONGARG) longarg=i<<16;
204 else if (op==ENDCODE) break;
205 p+=n;
209 static void PrintFunction(TProtoFunc* tf, TProtoFunc* Main, int at)
211 PrintHeader(tf,Main,at);
212 PrintLocals(tf);
213 PrintCode(tf);
214 #ifdef DEBUG
215 PrintConstants(tf);
216 #endif
217 PrintFunctions(tf);
220 void luaU_printchunk(TProtoFunc* Main)
222 PrintFunction(Main,0,0);