2 ** $Id: print.c,v 1.21 1999/05/25 19:58:55 lhf Exp $
4 ** See Copyright Notice in lua.h
12 static void PrintConstants(TProtoFunc
* tf
)
15 printf("constants (%d) for %p:\n",n
,tf
);
18 TObject
* o
=tf
->consts
+i
;
23 printf("N " NUMBER_FMT
"\n",(double)nvalue(o
));
26 printf("S %p\t\"%s\"\n",tsvalue(o
),svalue(o
));
29 printf("F %p\n",tfvalue(o
));
34 default: /* cannot happen */
35 printf("? type=%d\n",ttype(o
));
42 static void PrintConstant(TProtoFunc
* tf
, int i
, int at
)
44 TObject
* o
=luaU_getconstant(tf
,i
,at
);
48 printf(NUMBER_FMT
,(double)nvalue(o
));
51 printf("\"%s\"",svalue(o
));
54 printf("function at %p",(void*)tfvalue(o
));
59 default: /* cannot happen */
60 luaU_badconstant("print",i
,o
,tf
);
65 static void PrintCode(TProtoFunc
* tf
)
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
);
102 PrintConstant(tf
,i
,at
);
108 char* s
=luaF_getlocalname(tf
,i
+1,line
);
109 if (s
) printf("\t; %s",s
);
114 printf("\t; " SOURCE
,tf
->source
->str
,line
=i
);
121 /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
126 printf("\t; to %d",at
+i
+n
);
130 printf("\t; to %d",at
-i
+n
);
139 static void PrintLocals(TProtoFunc
* tf
)
141 LocVar
* v
=tf
->locvars
;
143 if (v
==NULL
|| v
->line
<0) return;
144 n
=tf
->code
[1]; if (n
>=ZEROVARARG
) n
-=ZEROVARARG
;
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(")");
156 ++i
; printf(" (%s",v
->varname
->str
);
160 while (i
--) printf(")");
164 #define IsMain(tf) (tf->lineDefined==0)
166 static void PrintHeader(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
)
168 int size
=luaU_codesize(tf
);
170 printf("\nmain " SOURCE
" (%d bytes at %p)\n",
171 tf
->source
->str
,tf
->lineDefined
,size
,tf
);
174 printf("\nfunction " SOURCE
" (%d bytes at %p); used at ",
175 tf
->source
->str
,tf
->lineDefined
,size
,tf
);
176 if (Main
&& IsMain(Main
))
184 static void PrintFunction(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
);
186 static void PrintFunctions(TProtoFunc
* Main
)
188 Byte
* code
=Main
->code
;
194 int n
=INFO(Main
,p
,&OP
);
196 int i
=OP
.arg
+longarg
;
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;
209 static void PrintFunction(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
)
211 PrintHeader(tf
,Main
,at
);
220 void luaU_printchunk(TProtoFunc
* Main
)
222 PrintFunction(Main
,0,0);