2 ** $Id: print.c,v 1.54 2006/01/11 22:49:27 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 Proto
* f
, int n
)
25 const char* s
=svalue(&f
->k
[n
]);
31 case '"': printf("\\\""); break;
32 case '\a': printf("\\a"); break;
33 case '\b': printf("\\b"); break;
34 case '\f': printf("\\f"); break;
35 case '\n': printf("\\n"); break;
36 case '\r': printf("\\r"); break;
37 case '\t': printf("\\t"); break;
38 case '\v': printf("\\v"); break;
39 default: if (isprint((unsigned char)*s
))
42 printf("\\%03u",(unsigned char)*s
);
48 static void PrintConstant(const Proto
* f
, int i
)
50 const TValue
* o
=&f
->k
[i
];
57 printf(bvalue(o
) ? "true" : "false");
60 printf(LUA_NUMBER_FMT
,nvalue(o
));
65 default: /* cannot happen */
66 printf("? type=%d",ttype(o
));
71 static void PrintCode(const Proto
* f
)
73 const Instruction
* code
=f
->code
;
75 for (pc
=0; pc
<n
; pc
++)
77 Instruction i
=code
[pc
];
78 OpCode o
=GET_OPCODE(i
);
83 int sbx
=GETARG_sBx(i
);
84 int line
=getline(f
,pc
);
85 printf("\t%d\t",pc
+1);
86 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
87 printf("%-9s\t",luaP_opnames
[o
]);
92 if (getBMode(o
)!=OpArgN
) printf(" %d",ISK(b
) ? (-1-INDEXK(b
)) : b
);
93 if (getCMode(o
)!=OpArgN
) printf(" %d",ISK(c
) ? (-1-INDEXK(c
)) : c
);
96 if (getBMode(o
)==OpArgK
) printf("%d %d",a
,-1-bx
); else printf("%d %d",a
,bx
);
99 if (o
==OP_JMP
) printf("%d",sbx
); else printf("%d %d",a
,sbx
);
105 printf("\t; "); PrintConstant(f
,bx
);
109 printf("\t; %s", (f
->sizeupvalues
>0) ? getstr(f
->upvalues
[b
]) : "-");
113 printf("\t; %s",svalue(&f
->k
[bx
]));
117 if (ISK(c
)) { printf("\t; "); PrintConstant(f
,INDEXK(c
)); }
128 if (ISK(b
) || ISK(c
))
131 if (ISK(b
)) PrintConstant(f
,INDEXK(b
)); else printf("-");
133 if (ISK(c
)) PrintConstant(f
,INDEXK(c
)); else printf("-");
139 printf("\t; to %d",sbx
+pc
+2);
142 printf("\t; %p",VOID(f
->p
[bx
]));
145 if (c
==0) printf("\t; %d",(int)code
[++pc
]);
146 else printf("\t; %d",c
);
155 #define SS(x) (x==1)?"":"s"
158 static void PrintHeader(const Proto
* f
)
160 const char* s
=getstr(f
->source
);
161 if (*s
=='@' || *s
=='=')
163 else if (*s
==LUA_SIGNATURE
[0])
167 printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
168 (f
->linedefined
==0)?"main":"function",s
,
169 f
->linedefined
,f
->lastlinedefined
,
170 S(f
->sizecode
),f
->sizecode
*Sizeof(Instruction
),VOID(f
));
171 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
172 f
->numparams
,f
->is_vararg
?"+":"",SS(f
->numparams
),
173 S(f
->maxstacksize
),S(f
->nups
));
174 printf("%d local%s, %d constant%s, %d function%s\n",
175 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
178 static void PrintConstants(const Proto
* f
)
181 printf("constants (%d) for %p:\n",n
,VOID(f
));
184 printf("\t%d\t",i
+1);
190 static void PrintLocals(const Proto
* f
)
192 int i
,n
=f
->sizelocvars
;
193 printf("locals (%d) for %p:\n",n
,VOID(f
));
196 printf("\t%d\t%s\t%d\t%d\n",
197 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
+1,f
->locvars
[i
].endpc
+1);
201 static void PrintUpvalues(const Proto
* f
)
203 int i
,n
=f
->sizeupvalues
;
204 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
205 if (f
->upvalues
==NULL
) return;
208 printf("\t%d\t%s\n",i
,getstr(f
->upvalues
[i
]));
212 void PrintFunction(const Proto
* f
, int full
)
223 for (i
=0; i
<n
; i
++) PrintFunction(f
->p
[i
],full
);