1 /* $NetBSD: print.c,v 1.1.1.2 2012/03/15 00:08:12 alnsn Exp $ */
4 ** $Id: print.c,v 1.1.1.2 2012/03/15 00:08:12 alnsn Exp $
6 ** See Copyright Notice in lua.h
20 #define PrintFunction luaU_print
22 #define Sizeof(x) ((int)sizeof(x))
23 #define VOID(p) ((const void*)(p))
25 static void PrintString(const TString
* ts
)
27 const char* s
=getstr(ts
);
28 size_t i
,n
=ts
->tsv
.len
;
35 case '"': printf("\\\""); break;
36 case '\\': printf("\\\\"); break;
37 case '\a': printf("\\a"); break;
38 case '\b': printf("\\b"); break;
39 case '\f': printf("\\f"); break;
40 case '\n': printf("\\n"); break;
41 case '\r': printf("\\r"); break;
42 case '\t': printf("\\t"); break;
43 case '\v': printf("\\v"); break;
44 default: if (isprint((unsigned char)c
))
47 printf("\\%03u",(unsigned char)c
);
53 static void PrintConstant(const Proto
* f
, int i
)
55 const TValue
* o
=&f
->k
[i
];
62 printf(bvalue(o
) ? "true" : "false");
65 printf(LUA_NUMBER_FMT
,nvalue(o
));
68 PrintString(rawtsvalue(o
));
70 default: /* cannot happen */
71 printf("? type=%d",ttype(o
));
76 static void PrintCode(const Proto
* f
)
78 const Instruction
* code
=f
->code
;
80 for (pc
=0; pc
<n
; pc
++)
82 Instruction i
=code
[pc
];
83 OpCode o
=GET_OPCODE(i
);
88 int sbx
=GETARG_sBx(i
);
89 int line
=getline(f
,pc
);
90 printf("\t%d\t",pc
+1);
91 if (line
>0) printf("[%d]\t",line
); else printf("[-]\t");
92 printf("%-9s\t",luaP_opnames
[o
]);
97 if (getBMode(o
)!=OpArgN
) printf(" %d",ISK(b
) ? (-1-INDEXK(b
)) : b
);
98 if (getCMode(o
)!=OpArgN
) printf(" %d",ISK(c
) ? (-1-INDEXK(c
)) : c
);
101 if (getBMode(o
)==OpArgK
) printf("%d %d",a
,-1-bx
); else printf("%d %d",a
,bx
);
104 if (o
==OP_JMP
) printf("%d",sbx
); else printf("%d %d",a
,sbx
);
110 printf("\t; "); PrintConstant(f
,bx
);
114 printf("\t; %s", (f
->sizeupvalues
>0) ? getstr(f
->upvalues
[b
]) : "-");
118 printf("\t; %s",svalue(&f
->k
[bx
]));
122 if (ISK(c
)) { printf("\t; "); PrintConstant(f
,INDEXK(c
)); }
133 if (ISK(b
) || ISK(c
))
136 if (ISK(b
)) PrintConstant(f
,INDEXK(b
)); else printf("-");
138 if (ISK(c
)) PrintConstant(f
,INDEXK(c
)); else printf("-");
144 printf("\t; to %d",sbx
+pc
+2);
147 printf("\t; %p",VOID(f
->p
[bx
]));
150 if (c
==0) printf("\t; %d",(int)code
[++pc
]);
151 else printf("\t; %d",c
);
160 #define SS(x) (x==1)?"":"s"
163 static void PrintHeader(const Proto
* f
)
165 const char* s
=getstr(f
->source
);
166 if (*s
=='@' || *s
=='=')
168 else if (*s
==LUA_SIGNATURE
[0])
172 printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
173 (f
->linedefined
==0)?"main":"function",s
,
174 f
->linedefined
,f
->lastlinedefined
,
175 S(f
->sizecode
),f
->sizecode
*Sizeof(Instruction
),VOID(f
));
176 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
177 f
->numparams
,f
->is_vararg
?"+":"",SS(f
->numparams
),
178 S(f
->maxstacksize
),S(f
->nups
));
179 printf("%d local%s, %d constant%s, %d function%s\n",
180 S(f
->sizelocvars
),S(f
->sizek
),S(f
->sizep
));
183 static void PrintConstants(const Proto
* f
)
186 printf("constants (%d) for %p:\n",n
,VOID(f
));
189 printf("\t%d\t",i
+1);
195 static void PrintLocals(const Proto
* f
)
197 int i
,n
=f
->sizelocvars
;
198 printf("locals (%d) for %p:\n",n
,VOID(f
));
201 printf("\t%d\t%s\t%d\t%d\n",
202 i
,getstr(f
->locvars
[i
].varname
),f
->locvars
[i
].startpc
+1,f
->locvars
[i
].endpc
+1);
206 static void PrintUpvalues(const Proto
* f
)
208 int i
,n
=f
->sizeupvalues
;
209 printf("upvalues (%d) for %p:\n",n
,VOID(f
));
210 if (f
->upvalues
==NULL
) return;
213 printf("\t%d\t%s\n",i
,getstr(f
->upvalues
[i
]));
217 void PrintFunction(const Proto
* f
, int full
)
228 for (i
=0; i
<n
; i
++) PrintFunction(f
->p
[i
],full
);