2 ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
4 ** See Copyright Notice in lua.h
18 #define PrintFunction luaU_print
20 #define Sizeof(x) ((int)sizeof(x))
21 #define VOID(p) ((const void*)(p))
23 static void PrintString(const TString
* ts
)
25 const char* s
=getstr(ts
);
26 size_t i
,n
=ts
->tsv
.len
;
33 case '"': printf("\\\""); break;
34 case '\\': printf("\\\\"); break;
35 case '\a': printf("\\a"); break;
36 case '\b': printf("\\b"); break;
37 case '\f': printf("\\f"); break;
38 case '\n': printf("\\n"); break;
39 case '\r': printf("\\r"); break;
40 case '\t': printf("\\t"); break;
41 case '\v': printf("\\v"); break;
42 default: if (isprint((unsigned char)c
))
45 printf("\\%03u",(unsigned char)c
);
51 static void PrintConstant(const Proto
* f
, int i
)
53 const TValue
* o
=&f
->k
[i
];
60 printf(bvalue(o
) ? "true" : "false");
63 printf(LUA_NUMBER_FMT
,nvalue(o
));
66 PrintString(rawtsvalue(o
));
68 default: /* cannot happen */
69 printf("? type=%d",ttype(o
));
74 static void PrintCode(const Proto
* f
)
76 const Instruction
* code
=f
->code
;
78 for (pc
=0; pc
<n
; pc
++)
80 Instruction i
=code
[pc
];
81 OpCode o
=GET_OPCODE(i
);
86 int sbx
=GETARG_sBx(i
);
87 int line
=getline(f
,pc
);
88 printf("\t%d\t",pc
+1);
89 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
90 printf("%-9s\t",luaP_opnames
[o
]);
95 if (getBMode(o
)!=OpArgN
) printf(" %d",ISK(b
) ? (-1-INDEXK(b
)) : b
);
96 if (getCMode(o
)!=OpArgN
) printf(" %d",ISK(c
) ? (-1-INDEXK(c
)) : c
);
99 if (getBMode(o
)==OpArgK
) printf("%d %d",a
,-1-bx
); else printf("%d %d",a
,bx
);
102 if (o
==OP_JMP
) printf("%d",sbx
); else printf("%d %d",a
,sbx
);
108 printf("\t; "); PrintConstant(f
,bx
);
112 printf("\t; %s", (f
->sizeupvalues
>0) ? getstr(f
->upvalues
[b
]) : "-");
116 printf("\t; %s",svalue(&f
->k
[bx
]));
120 if (ISK(c
)) { printf("\t; "); PrintConstant(f
,INDEXK(c
)); }
131 if (ISK(b
) || ISK(c
))
134 if (ISK(b
)) PrintConstant(f
,INDEXK(b
)); else printf("-");
136 if (ISK(c
)) PrintConstant(f
,INDEXK(c
)); else printf("-");
142 printf("\t; to %d",sbx
+pc
+2);
145 printf("\t; %p",VOID(f
->p
[bx
]));
148 if (c
==0) printf("\t; %d",(int)code
[++pc
]);
149 else printf("\t; %d",c
);
158 #define SS(x) (x==1)?"":"s"
161 static void PrintHeader(const Proto
* f
)
163 const char* s
=getstr(f
->source
);
164 if (*s
=='@' || *s
=='=')
166 else if (*s
==LUA_SIGNATURE
[0])
170 printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
171 (f
->linedefined
==0)?"main":"function",s
,
172 f
->linedefined
,f
->lastlinedefined
,
173 S(f
->sizecode
),f
->sizecode
*Sizeof(Instruction
),VOID(f
));
174 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
175 f
->numparams
,f
->is_vararg
?"+":"",SS(f
->numparams
),
176 S(f
->maxstacksize
),S(f
->nups
));
177 printf("%d local%s, %d constant%s, %d function%s\n",
178 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
181 static void PrintConstants(const Proto
* f
)
184 printf("constants (%d) for %p:\n",n
,VOID(f
));
187 printf("\t%d\t",i
+1);
193 static void PrintLocals(const Proto
* f
)
195 int i
,n
=f
->sizelocvars
;
196 printf("locals (%d) for %p:\n",n
,VOID(f
));
199 printf("\t%d\t%s\t%d\t%d\n",
200 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
+1,f
->locvars
[i
].endpc
+1);
204 static void PrintUpvalues(const Proto
* f
)
206 int i
,n
=f
->sizeupvalues
;
207 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
208 if (f
->upvalues
==NULL
) return;
211 printf("\t%d\t%s\n",i
,getstr(f
->upvalues
[i
]));
215 void PrintFunction(const Proto
* f
, int full
)
226 for (i
=0; i
<n
; i
++) PrintFunction(f
->p
[i
],full
);