2 ** $Id: print.c,v 1.13 1998/07/12 00:17:37 lhf Exp $
4 ** See Copyright Notice in lua.h
12 void PrintConstant1(TProtoFunc
* tf
, int i
)
14 TObject
* o
=tf
->consts
+i
;
16 if (i
<0 || i
>=tf
->nconsts
)
17 printf("(bad constant #%d: max=%d)",i
,tf
->nconsts
);
22 printf("N " NUMBER_FMT
"\n",nvalue(o
)); /* LUA_NUMBER */
25 printf("S %p\t\"%s\"\n",(void*)tsvalue(o
),svalue(o
));
28 printf("F %p\n",(void*)tfvalue(o
));
30 default: /* cannot happen */
31 printf("? %d\n",ttype(o
));
36 static void PrintConstants(TProtoFunc
* tf
)
39 printf("constants (%d):\n",n
);
40 for (i
=0; i
<n
; i
++) PrintConstant1(tf
,i
);
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
);
50 TObject
* o
=tf
->consts
+i
;
54 printf(NUMBER_FMT
,nvalue(o
)); /* LUA_NUMBER */
57 printf("\"%s\"",svalue(o
));
60 printf("function at %p",(void*)tfvalue(o
));
65 default: /* cannot happen */
66 printf("(bad constant #%d: type=%d [%s])\n",i
,ttype(o
),luaO_typename(o
));
72 #define VarStr(i) svalue(tf->consts+i)
74 static void PrintCode(TProtoFunc
* tf
)
85 printf("%6d ",(int)(p
-code
));
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");
103 printf(" %d",OP
.arg2
);
114 char* s
=luaF_getlocalname(tf
,i
+1,line
);
115 if (s
) printf("\t; %s",s
);
121 printf("\t; %s",VarStr(i
));
126 if (n
>=3) printf(" %d",OP
.arg2
);
130 printf("\t; \"%s\":%d",fileName(tf
),line
=i
);
133 /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
141 printf("\t; to %d",(int)(p
-code
)+i
+n
);
150 static void PrintLocals(TProtoFunc
* tf
)
152 LocVar
* v
=tf
->locvars
;
154 if (v
==NULL
|| v
->varname
==NULL
) return;
155 n
=tf
->code
[1]; if (n
>=ZEROVARARG
) n
-=ZEROVARARG
;
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
)
172 printf(" (%s",v
->varname
->str
); i
++;
176 while (i
--) printf(")");
181 static void PrintHeader(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
)
183 int size
=CodeSize(tf
);
185 printf("\nmain of \"%s\" (%d bytes at %p)\n",fileName(tf
),size
,(void*)tf
);
188 printf("\nfunction defined at \"%s\":%d (%d bytes at %p); used at ",
189 fileName(tf
),tf
->lineDefined
,size
,(void*)tf
);
193 printf("%p",(void*)Main
);
198 static void PrintFunction(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
);
200 static void PrintFunctions(TProtoFunc
* Main
)
202 Byte
* code
=Main
->code
;
207 int n
=INFO(Main
,p
,&OP
);
208 if (OP
.class==ENDCODE
) break;
209 if (OP
.class==PUSHCONSTANT
|| OP
.class==CLOSURE
)
212 TObject
* o
=Main
->consts
+i
;
213 if (ttype(o
)==LUA_T_PROTO
) PrintFunction(tfvalue(o
),Main
,(int)(p
-code
));
219 static void PrintFunction(TProtoFunc
* tf
, TProtoFunc
* Main
, int at
)
221 PrintHeader(tf
,Main
,at
);
230 void PrintChunk(TProtoFunc
* Main
)
232 PrintFunction(Main
,0,0);