luac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. ** $Id: luac.c $
  3. ** Lua compiler (saves bytecodes to files; also lists bytecodes)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define luac_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <ctype.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "ldebug.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lopnames.h"
  20. #include "lstate.h"
  21. #include "lundump.h"
  22. static void PrintFunction(const Proto* f, int full);
  23. #define luaU_print PrintFunction
  24. #define PROGNAME "luac" /* default program name */
  25. #define OUTPUT PROGNAME ".out" /* default output file */
  26. static int listing=0; /* list bytecodes? */
  27. static int dumping=1; /* dump bytecodes? */
  28. static int stripping=0; /* strip debug information? */
  29. static char Output[]={ OUTPUT }; /* default output file name */
  30. static const char* output=Output; /* actual output file name */
  31. static const char* progname=PROGNAME; /* actual program name */
  32. static TString **tmname;
  33. static void fatal(const char* message)
  34. {
  35. fprintf(stderr,"%s: %s\n",progname,message);
  36. exit(EXIT_FAILURE);
  37. }
  38. static void cannot(const char* what)
  39. {
  40. fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
  41. exit(EXIT_FAILURE);
  42. }
  43. static void usage(const char* message)
  44. {
  45. if (*message=='-')
  46. fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message);
  47. else
  48. fprintf(stderr,"%s: %s\n",progname,message);
  49. fprintf(stderr,
  50. "usage: %s [options] [filenames]\n"
  51. "Available options are:\n"
  52. " -l list (use -l -l for full listing)\n"
  53. " -o name output to file 'name' (default is \"%s\")\n"
  54. " -p parse only\n"
  55. " -s strip debug information\n"
  56. " -v show version information\n"
  57. " -- stop handling options\n"
  58. " - stop handling options and process stdin\n"
  59. ,progname,Output);
  60. exit(EXIT_FAILURE);
  61. }
  62. #define IS(s) (strcmp(argv[i],s)==0)
  63. static int doargs(int argc, char* argv[])
  64. {
  65. int i;
  66. int version=0;
  67. if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
  68. for (i=1; i<argc; i++)
  69. {
  70. if (*argv[i]!='-') /* end of options; keep it */
  71. break;
  72. else if (IS("--")) /* end of options; skip it */
  73. {
  74. ++i;
  75. if (version) ++version;
  76. break;
  77. }
  78. else if (IS("-")) /* end of options; use stdin */
  79. break;
  80. else if (IS("-l")) /* list */
  81. ++listing;
  82. else if (IS("-o")) /* output file */
  83. {
  84. output=argv[++i];
  85. if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
  86. usage("'-o' needs argument");
  87. if (IS("-")) output=NULL;
  88. }
  89. else if (IS("-p")) /* parse only */
  90. dumping=0;
  91. else if (IS("-s")) /* strip debug information */
  92. stripping=1;
  93. else if (IS("-v")) /* show version */
  94. ++version;
  95. else /* unknown option */
  96. usage(argv[i]);
  97. }
  98. if (i==argc && (listing || !dumping))
  99. {
  100. dumping=0;
  101. argv[--i]=Output;
  102. }
  103. if (version)
  104. {
  105. printf("%s\n",LUA_COPYRIGHT);
  106. if (version==argc-1) exit(EXIT_SUCCESS);
  107. }
  108. return i;
  109. }
  110. #define FUNCTION "(function()end)();\n"
  111. static const char* reader(lua_State* L, void* ud, size_t* size)
  112. {
  113. UNUSED(L);
  114. if ((*(int*)ud)--)
  115. {
  116. *size=sizeof(FUNCTION)-1;
  117. return FUNCTION;
  118. }
  119. else
  120. {
  121. *size=0;
  122. return NULL;
  123. }
  124. }
  125. #define toproto(L,i) getproto(s2v(L->top.p+(i)))
  126. static const Proto* combine(lua_State* L, int n)
  127. {
  128. if (n==1)
  129. return toproto(L,-1);
  130. else
  131. {
  132. Proto* f;
  133. int i=n;
  134. if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
  135. f=toproto(L,-1);
  136. for (i=0; i<n; i++)
  137. {
  138. f->p[i]=toproto(L,i-n-1);
  139. if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
  140. }
  141. return f;
  142. }
  143. }
  144. static int writer(lua_State* L, const void* p, size_t size, void* u)
  145. {
  146. UNUSED(L);
  147. return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
  148. }
  149. static int pmain(lua_State* L)
  150. {
  151. int argc=(int)lua_tointeger(L,1);
  152. char** argv=(char**)lua_touserdata(L,2);
  153. const Proto* f;
  154. int i;
  155. tmname=G(L)->tmname;
  156. if (!lua_checkstack(L,argc)) fatal("too many input files");
  157. for (i=0; i<argc; i++)
  158. {
  159. const char* filename=IS("-") ? NULL : argv[i];
  160. if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
  161. }
  162. f=combine(L,argc);
  163. if (listing) luaU_print(f,listing>1);
  164. if (dumping)
  165. {
  166. FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  167. if (D==NULL) cannot("open");
  168. lua_lock(L);
  169. luaU_dump(L,f,writer,D,stripping);
  170. lua_unlock(L);
  171. if (ferror(D)) cannot("write");
  172. if (fclose(D)) cannot("close");
  173. }
  174. return 0;
  175. }
  176. int main(int argc, char* argv[])
  177. {
  178. lua_State* L;
  179. int i=doargs(argc,argv);
  180. argc-=i; argv+=i;
  181. if (argc<=0) usage("no input files given");
  182. L=luaL_newstate();
  183. if (L==NULL) fatal("cannot create state: not enough memory");
  184. lua_pushcfunction(L,&pmain);
  185. lua_pushinteger(L,argc);
  186. lua_pushlightuserdata(L,argv);
  187. if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
  188. lua_close(L);
  189. return EXIT_SUCCESS;
  190. }
  191. /*
  192. ** print bytecodes
  193. */
  194. #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
  195. #define VOID(p) ((const void*)(p))
  196. #define eventname(i) (getstr(tmname[i]))
  197. static void PrintString(const TString* ts)
  198. {
  199. const char* s=getstr(ts);
  200. size_t i,n=tsslen(ts);
  201. printf("\"");
  202. for (i=0; i<n; i++)
  203. {
  204. int c=(int)(unsigned char)s[i];
  205. switch (c)
  206. {
  207. case '"':
  208. printf("\\\"");
  209. break;
  210. case '\\':
  211. printf("\\\\");
  212. break;
  213. case '\a':
  214. printf("\\a");
  215. break;
  216. case '\b':
  217. printf("\\b");
  218. break;
  219. case '\f':
  220. printf("\\f");
  221. break;
  222. case '\n':
  223. printf("\\n");
  224. break;
  225. case '\r':
  226. printf("\\r");
  227. break;
  228. case '\t':
  229. printf("\\t");
  230. break;
  231. case '\v':
  232. printf("\\v");
  233. break;
  234. default:
  235. if (isprint(c)) printf("%c",c); else printf("\\%03d",c);
  236. break;
  237. }
  238. }
  239. printf("\"");
  240. }
  241. static void PrintType(const Proto* f, int i)
  242. {
  243. const TValue* o=&f->k[i];
  244. switch (ttypetag(o))
  245. {
  246. case LUA_VNIL:
  247. printf("N");
  248. break;
  249. case LUA_VFALSE:
  250. case LUA_VTRUE:
  251. printf("B");
  252. break;
  253. case LUA_VNUMFLT:
  254. printf("F");
  255. break;
  256. case LUA_VNUMINT:
  257. printf("I");
  258. break;
  259. case LUA_VSHRSTR:
  260. case LUA_VLNGSTR:
  261. printf("S");
  262. break;
  263. default: /* cannot happen */
  264. printf("?%d",ttypetag(o));
  265. break;
  266. }
  267. printf("\t");
  268. }
  269. static void PrintConstant(const Proto* f, int i)
  270. {
  271. const TValue* o=&f->k[i];
  272. switch (ttypetag(o))
  273. {
  274. case LUA_VNIL:
  275. printf("nil");
  276. break;
  277. case LUA_VFALSE:
  278. printf("false");
  279. break;
  280. case LUA_VTRUE:
  281. printf("true");
  282. break;
  283. case LUA_VNUMFLT:
  284. {
  285. char buff[100];
  286. sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
  287. printf("%s",buff);
  288. if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
  289. break;
  290. }
  291. case LUA_VNUMINT:
  292. printf(LUA_INTEGER_FMT,ivalue(o));
  293. break;
  294. case LUA_VSHRSTR:
  295. case LUA_VLNGSTR:
  296. PrintString(tsvalue(o));
  297. break;
  298. default: /* cannot happen */
  299. printf("?%d",ttypetag(o));
  300. break;
  301. }
  302. }
  303. #define COMMENT "\t; "
  304. #define EXTRAARG GETARG_Ax(code[pc+1])
  305. #define EXTRAARGC (EXTRAARG*(MAXARG_C+1))
  306. #define ISK (isk ? "k" : "")
  307. static void PrintCode(const Proto* f)
  308. {
  309. const Instruction* code=f->code;
  310. int pc,n=f->sizecode;
  311. for (pc=0; pc<n; pc++)
  312. {
  313. Instruction i=code[pc];
  314. OpCode o=GET_OPCODE(i);
  315. int a=GETARG_A(i);
  316. int b=GETARG_B(i);
  317. int c=GETARG_C(i);
  318. int ax=GETARG_Ax(i);
  319. int bx=GETARG_Bx(i);
  320. int sb=GETARG_sB(i);
  321. int sc=GETARG_sC(i);
  322. int sbx=GETARG_sBx(i);
  323. int isk=GETARG_k(i);
  324. int line=luaG_getfuncline(f,pc);
  325. printf("\t%d\t",pc+1);
  326. if (line>0) printf("[%d]\t",line); else printf("[-]\t");
  327. printf("%-9s\t",opnames[o]);
  328. switch (o)
  329. {
  330. case OP_MOVE:
  331. printf("%d %d",a,b);
  332. break;
  333. case OP_LOADI:
  334. printf("%d %d",a,sbx);
  335. break;
  336. case OP_LOADF:
  337. printf("%d %d",a,sbx);
  338. break;
  339. case OP_LOADK:
  340. printf("%d %d",a,bx);
  341. printf(COMMENT); PrintConstant(f,bx);
  342. break;
  343. case OP_LOADKX:
  344. printf("%d",a);
  345. printf(COMMENT); PrintConstant(f,EXTRAARG);
  346. break;
  347. case OP_LOADFALSE:
  348. printf("%d",a);
  349. break;
  350. case OP_LFALSESKIP:
  351. printf("%d",a);
  352. break;
  353. case OP_LOADTRUE:
  354. printf("%d",a);
  355. break;
  356. case OP_LOADNIL:
  357. printf("%d %d",a,b);
  358. printf(COMMENT "%d out",b+1);
  359. break;
  360. case OP_GETUPVAL:
  361. printf("%d %d",a,b);
  362. printf(COMMENT "%s",UPVALNAME(b));
  363. break;
  364. case OP_SETUPVAL:
  365. printf("%d %d",a,b);
  366. printf(COMMENT "%s",UPVALNAME(b));
  367. break;
  368. case OP_GETTABUP:
  369. printf("%d %d %d",a,b,c);
  370. printf(COMMENT "%s",UPVALNAME(b));
  371. printf(" "); PrintConstant(f,c);
  372. break;
  373. case OP_GETTABLE:
  374. printf("%d %d %d",a,b,c);
  375. break;
  376. case OP_GETI:
  377. printf("%d %d %d",a,b,c);
  378. break;
  379. case OP_GETFIELD:
  380. printf("%d %d %d",a,b,c);
  381. printf(COMMENT); PrintConstant(f,c);
  382. break;
  383. case OP_SETTABUP:
  384. printf("%d %d %d%s",a,b,c,ISK);
  385. printf(COMMENT "%s",UPVALNAME(a));
  386. printf(" "); PrintConstant(f,b);
  387. if (isk) { printf(" "); PrintConstant(f,c); }
  388. break;
  389. case OP_SETTABLE:
  390. printf("%d %d %d%s",a,b,c,ISK);
  391. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  392. break;
  393. case OP_SETI:
  394. printf("%d %d %d%s",a,b,c,ISK);
  395. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  396. break;
  397. case OP_SETFIELD:
  398. printf("%d %d %d%s",a,b,c,ISK);
  399. printf(COMMENT); PrintConstant(f,b);
  400. if (isk) { printf(" "); PrintConstant(f,c); }
  401. break;
  402. case OP_NEWTABLE:
  403. printf("%d %d %d",a,b,c);
  404. printf(COMMENT "%d",c+EXTRAARGC);
  405. break;
  406. case OP_SELF:
  407. printf("%d %d %d%s",a,b,c,ISK);
  408. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  409. break;
  410. case OP_ADDI:
  411. printf("%d %d %d",a,b,sc);
  412. break;
  413. case OP_ADDK:
  414. printf("%d %d %d",a,b,c);
  415. printf(COMMENT); PrintConstant(f,c);
  416. break;
  417. case OP_SUBK:
  418. printf("%d %d %d",a,b,c);
  419. printf(COMMENT); PrintConstant(f,c);
  420. break;
  421. case OP_MULK:
  422. printf("%d %d %d",a,b,c);
  423. printf(COMMENT); PrintConstant(f,c);
  424. break;
  425. case OP_MODK:
  426. printf("%d %d %d",a,b,c);
  427. printf(COMMENT); PrintConstant(f,c);
  428. break;
  429. case OP_POWK:
  430. printf("%d %d %d",a,b,c);
  431. printf(COMMENT); PrintConstant(f,c);
  432. break;
  433. case OP_DIVK:
  434. printf("%d %d %d",a,b,c);
  435. printf(COMMENT); PrintConstant(f,c);
  436. break;
  437. case OP_IDIVK:
  438. printf("%d %d %d",a,b,c);
  439. printf(COMMENT); PrintConstant(f,c);
  440. break;
  441. case OP_BANDK:
  442. printf("%d %d %d",a,b,c);
  443. printf(COMMENT); PrintConstant(f,c);
  444. break;
  445. case OP_BORK:
  446. printf("%d %d %d",a,b,c);
  447. printf(COMMENT); PrintConstant(f,c);
  448. break;
  449. case OP_BXORK:
  450. printf("%d %d %d",a,b,c);
  451. printf(COMMENT); PrintConstant(f,c);
  452. break;
  453. case OP_SHRI:
  454. printf("%d %d %d",a,b,sc);
  455. break;
  456. case OP_SHLI:
  457. printf("%d %d %d",a,b,sc);
  458. break;
  459. case OP_ADD:
  460. printf("%d %d %d",a,b,c);
  461. break;
  462. case OP_SUB:
  463. printf("%d %d %d",a,b,c);
  464. break;
  465. case OP_MUL:
  466. printf("%d %d %d",a,b,c);
  467. break;
  468. case OP_MOD:
  469. printf("%d %d %d",a,b,c);
  470. break;
  471. case OP_POW:
  472. printf("%d %d %d",a,b,c);
  473. break;
  474. case OP_DIV:
  475. printf("%d %d %d",a,b,c);
  476. break;
  477. case OP_IDIV:
  478. printf("%d %d %d",a,b,c);
  479. break;
  480. case OP_BAND:
  481. printf("%d %d %d",a,b,c);
  482. break;
  483. case OP_BOR:
  484. printf("%d %d %d",a,b,c);
  485. break;
  486. case OP_BXOR:
  487. printf("%d %d %d",a,b,c);
  488. break;
  489. case OP_SHL:
  490. printf("%d %d %d",a,b,c);
  491. break;
  492. case OP_SHR:
  493. printf("%d %d %d",a,b,c);
  494. break;
  495. case OP_MMBIN:
  496. printf("%d %d %d",a,b,c);
  497. printf(COMMENT "%s",eventname(c));
  498. break;
  499. case OP_MMBINI:
  500. printf("%d %d %d %d",a,sb,c,isk);
  501. printf(COMMENT "%s",eventname(c));
  502. if (isk) printf(" flip");
  503. break;
  504. case OP_MMBINK:
  505. printf("%d %d %d %d",a,b,c,isk);
  506. printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b);
  507. if (isk) printf(" flip");
  508. break;
  509. case OP_UNM:
  510. printf("%d %d",a,b);
  511. break;
  512. case OP_BNOT:
  513. printf("%d %d",a,b);
  514. break;
  515. case OP_NOT:
  516. printf("%d %d",a,b);
  517. break;
  518. case OP_LEN:
  519. printf("%d %d",a,b);
  520. break;
  521. case OP_CONCAT:
  522. printf("%d %d",a,b);
  523. break;
  524. case OP_CLOSE:
  525. printf("%d",a);
  526. break;
  527. case OP_TBC:
  528. printf("%d",a);
  529. break;
  530. case OP_JMP:
  531. printf("%d",GETARG_sJ(i));
  532. printf(COMMENT "to %d",GETARG_sJ(i)+pc+2);
  533. break;
  534. case OP_EQ:
  535. printf("%d %d %d",a,b,isk);
  536. break;
  537. case OP_LT:
  538. printf("%d %d %d",a,b,isk);
  539. break;
  540. case OP_LE:
  541. printf("%d %d %d",a,b,isk);
  542. break;
  543. case OP_EQK:
  544. printf("%d %d %d",a,b,isk);
  545. printf(COMMENT); PrintConstant(f,b);
  546. break;
  547. case OP_EQI:
  548. printf("%d %d %d",a,sb,isk);
  549. break;
  550. case OP_LTI:
  551. printf("%d %d %d",a,sb,isk);
  552. break;
  553. case OP_LEI:
  554. printf("%d %d %d",a,sb,isk);
  555. break;
  556. case OP_GTI:
  557. printf("%d %d %d",a,sb,isk);
  558. break;
  559. case OP_GEI:
  560. printf("%d %d %d",a,sb,isk);
  561. break;
  562. case OP_TEST:
  563. printf("%d %d",a,isk);
  564. break;
  565. case OP_TESTSET:
  566. printf("%d %d %d",a,b,isk);
  567. break;
  568. case OP_CALL:
  569. printf("%d %d %d",a,b,c);
  570. printf(COMMENT);
  571. if (b==0) printf("all in "); else printf("%d in ",b-1);
  572. if (c==0) printf("all out"); else printf("%d out",c-1);
  573. break;
  574. case OP_TAILCALL:
  575. printf("%d %d %d%s",a,b,c,ISK);
  576. printf(COMMENT "%d in",b-1);
  577. break;
  578. case OP_RETURN:
  579. printf("%d %d %d%s",a,b,c,ISK);
  580. printf(COMMENT);
  581. if (b==0) printf("all out"); else printf("%d out",b-1);
  582. break;
  583. case OP_RETURN0:
  584. break;
  585. case OP_RETURN1:
  586. printf("%d",a);
  587. break;
  588. case OP_FORLOOP:
  589. printf("%d %d",a,bx);
  590. printf(COMMENT "to %d",pc-bx+2);
  591. break;
  592. case OP_FORPREP:
  593. printf("%d %d",a,bx);
  594. printf(COMMENT "exit to %d",pc+bx+3);
  595. break;
  596. case OP_TFORPREP:
  597. printf("%d %d",a,bx);
  598. printf(COMMENT "to %d",pc+bx+2);
  599. break;
  600. case OP_TFORCALL:
  601. printf("%d %d",a,c);
  602. break;
  603. case OP_TFORLOOP:
  604. printf("%d %d",a,bx);
  605. printf(COMMENT "to %d",pc-bx+2);
  606. break;
  607. case OP_SETLIST:
  608. printf("%d %d %d",a,b,c);
  609. if (isk) printf(COMMENT "%d",c+EXTRAARGC);
  610. break;
  611. case OP_CLOSURE:
  612. printf("%d %d",a,bx);
  613. printf(COMMENT "%p",VOID(f->p[bx]));
  614. break;
  615. case OP_VARARG:
  616. printf("%d %d",a,c);
  617. printf(COMMENT);
  618. if (c==0) printf("all out"); else printf("%d out",c-1);
  619. break;
  620. case OP_VARARGPREP:
  621. printf("%d",a);
  622. break;
  623. case OP_EXTRAARG:
  624. printf("%d",ax);
  625. break;
  626. #if 0
  627. default:
  628. printf("%d %d %d",a,b,c);
  629. printf(COMMENT "not handled");
  630. break;
  631. #endif
  632. }
  633. printf("\n");
  634. }
  635. }
  636. #define SS(x) ((x==1)?"":"s")
  637. #define S(x) (int)(x),SS(x)
  638. static void PrintHeader(const Proto* f)
  639. {
  640. const char* s=f->source ? getstr(f->source) : "=?";
  641. if (*s=='@' || *s=='=')
  642. s++;
  643. else if (*s==LUA_SIGNATURE[0])
  644. s="(bstring)";
  645. else
  646. s="(string)";
  647. printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
  648. (f->linedefined==0)?"main":"function",s,
  649. f->linedefined,f->lastlinedefined,
  650. S(f->sizecode),VOID(f));
  651. printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
  652. (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
  653. S(f->maxstacksize),S(f->sizeupvalues));
  654. printf("%d local%s, %d constant%s, %d function%s\n",
  655. S(f->sizelocvars),S(f->sizek),S(f->sizep));
  656. }
  657. static void PrintDebug(const Proto* f)
  658. {
  659. int i,n;
  660. n=f->sizek;
  661. printf("constants (%d) for %p:\n",n,VOID(f));
  662. for (i=0; i<n; i++)
  663. {
  664. printf("\t%d\t",i);
  665. PrintType(f,i);
  666. PrintConstant(f,i);
  667. printf("\n");
  668. }
  669. n=f->sizelocvars;
  670. printf("locals (%d) for %p:\n",n,VOID(f));
  671. for (i=0; i<n; i++)
  672. {
  673. printf("\t%d\t%s\t%d\t%d\n",
  674. i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
  675. }
  676. n=f->sizeupvalues;
  677. printf("upvalues (%d) for %p:\n",n,VOID(f));
  678. for (i=0; i<n; i++)
  679. {
  680. printf("\t%d\t%s\t%d\t%d\n",
  681. i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
  682. }
  683. }
  684. static void PrintFunction(const Proto* f, int full)
  685. {
  686. int i,n=f->sizep;
  687. PrintHeader(f);
  688. PrintCode(f);
  689. if (full) PrintDebug(f);
  690. for (i=0; i<n; i++) PrintFunction(f->p[i],full);
  691. }