2 ** $Id: print.c,v 1.44 2003/04/07 20:34:20 lhf Exp $
4 ** See Copyright Notice in lua.h
22 #define Sizeof(x) ((int)sizeof(x))
23 #define VOID(p) ((const void*)(p))
25 static void PrintString(const Proto
* f
, int n
)
27 const char* s
=svalue(&f
->k
[n
]);
33 case '"': printf("\\\""); break;
34 case '\a': printf("\\a"); break;
35 case '\b': printf("\\b"); break;
36 case '\f': printf("\\f"); break;
37 case '\n': printf("\\n"); break;
38 case '\r': printf("\\r"); break;
39 case '\t': printf("\\t"); break;
40 case '\v': printf("\\v"); break;
41 default: putchar(*s
); break;
47 static void PrintConstant(const Proto
* f
, int i
)
49 const TObject
* o
=&f
->k
[i
];
53 printf(LUA_NUMBER_FMT
,nvalue(o
));
61 default: /* cannot happen */
62 printf("? type=%d",ttype(o
));
67 static void PrintCode(const Proto
* f
)
69 const Instruction
* code
=f
->code
;
71 for (pc
=0; pc
<n
; pc
++)
73 Instruction i
=code
[pc
];
74 OpCode o
=GET_OPCODE(i
);
79 int sbc
=GETARG_sBx(i
);
80 int line
=getline(f
,pc
);
82 printf("%0*lX",Sizeof(i
)*2,i
);
84 printf("\t%d\t",pc
+1);
85 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
86 printf("%-9s\t",luaP_opnames
[o
]);
89 case iABC
: printf("%d %d %d",a
,b
,c
); break;
90 case iABx
: printf("%d %d",a
,bc
); break;
91 case iAsBx
: printf("%d %d",a
,sbc
); break;
96 printf("\t; "); PrintConstant(f
,bc
);
100 printf("\t; %s", (f
->sizeupvalues
>0) ? getstr(f
->upvalues
[b
]) : "-");
104 printf("\t; %s",svalue(&f
->k
[bc
]));
108 if (c
>=MAXSTACK
) { printf("\t; "); PrintConstant(f
,c
-MAXSTACK
); }
119 if (b
>=MAXSTACK
|| c
>=MAXSTACK
)
122 if (b
>=MAXSTACK
) PrintConstant(f
,b
-MAXSTACK
); else printf("-");
124 if (c
>=MAXSTACK
) PrintConstant(f
,c
-MAXSTACK
);
130 printf("\t; to %d",sbc
+pc
+2);
133 printf("\t; %p",VOID(f
->p
[bc
]));
142 static const char* Source(const Proto
* f
)
144 const char* s
=getstr(f
->source
);
145 if (*s
=='@' || *s
=='=')
147 else if (*s
==LUA_SIGNATURE
[0])
153 #define IsMain(f) (f->lineDefined==0)
155 #define SS(x) (x==1)?"":"s"
158 static void PrintHeader(const Proto
* f
)
160 printf("\n%s <%s:%d> (%d instruction%s, %d bytes at %p)\n",
161 IsMain(f
)?"main":"function",Source(f
),f
->lineDefined
,
162 S(f
->sizecode
),f
->sizecode
*Sizeof(Instruction
),VOID(f
));
163 printf("%d%s param%s, %d stack%s, %d upvalue%s, ",
164 f
->numparams
,f
->is_vararg
?"+":"",SS(f
->numparams
),S(f
->maxstacksize
),
166 printf("%d local%s, %d constant%s, %d function%s\n",
167 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
171 static void PrintConstants(const Proto
* f
)
174 printf("constants (%d) for %p:\n",n
,VOID(f
));
183 static void PrintLocals(const Proto
* f
)
185 int i
,n
=f
->sizelocvars
;
186 printf("locals (%d) for %p:\n",n
,VOID(f
));
189 printf("\t%d\t%s\t%d\t%d\n",
190 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
,f
->locvars
[i
].endpc
);
194 static void PrintUpvalues(const Proto
* f
)
196 int i
,n
=f
->sizeupvalues
;
197 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
198 if (f
->upvalues
==NULL
) return;
201 printf("\t%d\t%s\n",i
,getstr(f
->upvalues
[i
]));
206 void luaU_print(const Proto
* f
)
216 for (i
=0; i
<n
; i
++) luaU_print(f
->p
[i
]);