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