luac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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)();"
  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+(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. luaM_freearray(L,f->lineinfo,f->sizelineinfo);
  142. f->sizelineinfo=0;
  143. return f;
  144. }
  145. }
  146. static int writer(lua_State* L, const void* p, size_t size, void* u)
  147. {
  148. UNUSED(L);
  149. return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
  150. }
  151. static int pmain(lua_State* L)
  152. {
  153. int argc=(int)lua_tointeger(L,1);
  154. char** argv=(char**)lua_touserdata(L,2);
  155. const Proto* f;
  156. int i;
  157. tmname=G(L)->tmname;
  158. if (!lua_checkstack(L,argc)) fatal("too many input files");
  159. for (i=0; i<argc; i++)
  160. {
  161. const char* filename=IS("-") ? NULL : argv[i];
  162. if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
  163. }
  164. f=combine(L,argc);
  165. if (listing) luaU_print(f,listing>1);
  166. if (dumping)
  167. {
  168. FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  169. if (D==NULL) cannot("open");
  170. lua_lock(L);
  171. luaU_dump(L,f,writer,D,stripping);
  172. lua_unlock(L);
  173. if (ferror(D)) cannot("write");
  174. if (fclose(D)) cannot("close");
  175. }
  176. return 0;
  177. }
  178. int main(int argc, char* argv[])
  179. {
  180. lua_State* L;
  181. int i=doargs(argc,argv);
  182. argc-=i; argv+=i;
  183. if (argc<=0) usage("no input files given");
  184. L=luaL_newstate();
  185. if (L==NULL) fatal("cannot create state: not enough memory");
  186. lua_pushcfunction(L,&pmain);
  187. lua_pushinteger(L,argc);
  188. lua_pushlightuserdata(L,argv);
  189. if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
  190. lua_close(L);
  191. return EXIT_SUCCESS;
  192. }
  193. /*
  194. ** print bytecodes
  195. */
  196. #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
  197. #define VOID(p) ((const void*)(p))
  198. #define eventname(i) (getstr(tmname[i]))
  199. static void PrintString(const TString* ts)
  200. {
  201. const char* s=getstr(ts);
  202. size_t i,n=tsslen(ts);
  203. printf("\"");
  204. for (i=0; i<n; i++)
  205. {
  206. int c=(int)(unsigned char)s[i];
  207. switch (c)
  208. {
  209. case '"':
  210. printf("\\\"");
  211. break;
  212. case '\\':
  213. printf("\\\\");
  214. break;
  215. case '\a':
  216. printf("\\a");
  217. break;
  218. case '\b':
  219. printf("\\b");
  220. break;
  221. case '\f':
  222. printf("\\f");
  223. break;
  224. case '\n':
  225. printf("\\n");
  226. break;
  227. case '\r':
  228. printf("\\r");
  229. break;
  230. case '\t':
  231. printf("\\t");
  232. break;
  233. case '\v':
  234. printf("\\v");
  235. break;
  236. default:
  237. if (isprint(c)) printf("%c",c); else printf("\\%03d",c);
  238. break;
  239. }
  240. }
  241. printf("\"");
  242. }
  243. static void PrintType(const Proto* f, int i)
  244. {
  245. const TValue* o=&f->k[i];
  246. switch (ttypetag(o))
  247. {
  248. case LUA_VNIL:
  249. printf("N");
  250. break;
  251. case LUA_VFALSE:
  252. case LUA_VTRUE:
  253. printf("B");
  254. break;
  255. case LUA_VNUMFLT:
  256. printf("F");
  257. break;
  258. case LUA_VNUMINT:
  259. printf("I");
  260. break;
  261. case LUA_VSHRSTR:
  262. case LUA_VLNGSTR:
  263. printf("S");
  264. break;
  265. default: /* cannot happen */
  266. printf("?%d",ttypetag(o));
  267. break;
  268. }
  269. printf("\t");
  270. }
  271. static void PrintConstant(const Proto* f, int i)
  272. {
  273. const TValue* o=&f->k[i];
  274. switch (ttypetag(o))
  275. {
  276. case LUA_VNIL:
  277. printf("nil");
  278. break;
  279. case LUA_VFALSE:
  280. printf("false");
  281. break;
  282. case LUA_VTRUE:
  283. printf("true");
  284. break;
  285. case LUA_VNUMFLT:
  286. {
  287. char buff[100];
  288. sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
  289. printf("%s",buff);
  290. if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
  291. break;
  292. }
  293. case LUA_VNUMINT:
  294. printf(LUA_INTEGER_FMT,ivalue(o));
  295. break;
  296. case LUA_VSHRSTR:
  297. case LUA_VLNGSTR:
  298. PrintString(tsvalue(o));
  299. break;
  300. default: /* cannot happen */
  301. printf("?%d",ttypetag(o));
  302. break;
  303. }
  304. }
  305. #define COMMENT "\t; "
  306. #define EXTRAARG GETARG_Ax(code[pc+1])
  307. #define EXTRAARGC (EXTRAARG*(MAXARG_C+1))
  308. #define ISK (isk ? "k" : "")
  309. static void PrintCode(const Proto* f)
  310. {
  311. const Instruction* code=f->code;
  312. int pc,n=f->sizecode;
  313. for (pc=0; pc<n; pc++)
  314. {
  315. Instruction i=code[pc];
  316. OpCode o=GET_OPCODE(i);
  317. int a=GETARG_A(i);
  318. int b=GETARG_B(i);
  319. int c=GETARG_C(i);
  320. int ax=GETARG_Ax(i);
  321. int bx=GETARG_Bx(i);
  322. int sb=GETARG_sB(i);
  323. int sc=GETARG_sC(i);
  324. int sbx=GETARG_sBx(i);
  325. int isk=GETARG_k(i);
  326. int line=luaG_getfuncline(f,pc);
  327. printf("\t%d\t",pc+1);
  328. if (line>0) printf("[%d]\t",line); else printf("[-]\t");
  329. printf("%-9s\t",opnames[o]);
  330. switch (o)
  331. {
  332. case OP_MOVE:
  333. printf("%d %d",a,b);
  334. break;
  335. case OP_LOADI:
  336. printf("%d %d",a,sbx);
  337. break;
  338. case OP_LOADF:
  339. printf("%d %d",a,sbx);
  340. break;
  341. case OP_LOADK:
  342. printf("%d %d",a,bx);
  343. printf(COMMENT); PrintConstant(f,bx);
  344. break;
  345. case OP_LOADKX:
  346. printf("%d",a);
  347. printf(COMMENT); PrintConstant(f,EXTRAARG);
  348. break;
  349. case OP_LOADFALSE:
  350. printf("%d",a);
  351. break;
  352. case OP_LFALSESKIP:
  353. printf("%d",a);
  354. break;
  355. case OP_LOADTRUE:
  356. printf("%d",a);
  357. break;
  358. case OP_LOADNIL:
  359. printf("%d %d",a,b);
  360. printf(COMMENT "%d out",b+1);
  361. break;
  362. case OP_GETUPVAL:
  363. printf("%d %d",a,b);
  364. printf(COMMENT "%s",UPVALNAME(b));
  365. break;
  366. case OP_SETUPVAL:
  367. printf("%d %d",a,b);
  368. printf(COMMENT "%s",UPVALNAME(b));
  369. break;
  370. case OP_GETTABUP:
  371. printf("%d %d %d",a,b,c);
  372. printf(COMMENT "%s",UPVALNAME(b));
  373. printf(" "); PrintConstant(f,c);
  374. break;
  375. case OP_GETTABLE:
  376. printf("%d %d %d",a,b,c);
  377. break;
  378. case OP_GETI:
  379. printf("%d %d %d",a,b,c);
  380. break;
  381. case OP_GETFIELD:
  382. printf("%d %d %d",a,b,c);
  383. printf(COMMENT); PrintConstant(f,c);
  384. break;
  385. case OP_SETTABUP:
  386. printf("%d %d %d%s",a,b,c,ISK);
  387. printf(COMMENT "%s",UPVALNAME(a));
  388. printf(" "); PrintConstant(f,b);
  389. if (isk) { printf(" "); PrintConstant(f,c); }
  390. break;
  391. case OP_SETTABLE:
  392. printf("%d %d %d%s",a,b,c,ISK);
  393. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  394. break;
  395. case OP_SETI:
  396. printf("%d %d %d%s",a,b,c,ISK);
  397. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  398. break;
  399. case OP_SETFIELD:
  400. printf("%d %d %d%s",a,b,c,ISK);
  401. printf(COMMENT); PrintConstant(f,b);
  402. if (isk) { printf(" "); PrintConstant(f,c); }
  403. break;
  404. case OP_NEWTABLE:
  405. printf("%d %d %d",a,b,c);
  406. printf(COMMENT "%d",c+EXTRAARGC);
  407. break;
  408. case OP_SELF:
  409. printf("%d %d %d%s",a,b,c,ISK);
  410. if (isk) { printf(COMMENT); PrintConstant(f,c); }
  411. break;
  412. case OP_ADDI:
  413. printf("%d %d %d",a,b,sc);
  414. break;
  415. case OP_ADDK:
  416. printf("%d %d %d",a,b,c);
  417. printf(COMMENT); PrintConstant(f,c);
  418. break;
  419. case OP_SUBK:
  420. printf("%d %d %d",a,b,c);
  421. printf(COMMENT); PrintConstant(f,c);
  422. break;
  423. case OP_MULK:
  424. printf("%d %d %d",a,b,c);
  425. printf(COMMENT); PrintConstant(f,c);
  426. break;
  427. case OP_MODK:
  428. printf("%d %d %d",a,b,c);
  429. printf(COMMENT); PrintConstant(f,c);
  430. break;
  431. case OP_POWK:
  432. printf("%d %d %d",a,b,c);
  433. printf(COMMENT); PrintConstant(f,c);
  434. break;
  435. case OP_DIVK:
  436. printf("%d %d %d",a,b,c);
  437. printf(COMMENT); PrintConstant(f,c);
  438. break;
  439. case OP_IDIVK:
  440. printf("%d %d %d",a,b,c);
  441. printf(COMMENT); PrintConstant(f,c);
  442. break;
  443. case OP_BANDK:
  444. printf("%d %d %d",a,b,c);
  445. printf(COMMENT); PrintConstant(f,c);
  446. break;
  447. case OP_BORK:
  448. printf("%d %d %d",a,b,c);
  449. printf(COMMENT); PrintConstant(f,c);
  450. break;
  451. case OP_BXORK:
  452. printf("%d %d %d",a,b,c);
  453. printf(COMMENT); PrintConstant(f,c);
  454. break;
  455. case OP_SHRI:
  456. printf("%d %d %d",a,b,sc);
  457. break;
  458. case OP_SHLI:
  459. printf("%d %d %d",a,b,sc);
  460. break;
  461. case OP_ADD:
  462. printf("%d %d %d",a,b,c);
  463. break;
  464. case OP_SUB:
  465. printf("%d %d %d",a,b,c);
  466. break;
  467. case OP_MUL:
  468. printf("%d %d %d",a,b,c);
  469. break;
  470. case OP_MOD:
  471. printf("%d %d %d",a,b,c);
  472. break;
  473. case OP_POW:
  474. printf("%d %d %d",a,b,c);
  475. break;
  476. case OP_DIV:
  477. printf("%d %d %d",a,b,c);
  478. break;
  479. case OP_IDIV:
  480. printf("%d %d %d",a,b,c);
  481. break;
  482. case OP_BAND:
  483. printf("%d %d %d",a,b,c);
  484. break;
  485. case OP_BOR:
  486. printf("%d %d %d",a,b,c);
  487. break;
  488. case OP_BXOR:
  489. printf("%d %d %d",a,b,c);
  490. break;
  491. case OP_SHL:
  492. printf("%d %d %d",a,b,c);
  493. break;
  494. case OP_SHR:
  495. printf("%d %d %d",a,b,c);
  496. break;
  497. case OP_MMBIN:
  498. printf("%d %d %d",a,b,c);
  499. printf(COMMENT "%s",eventname(c));
  500. break;
  501. case OP_MMBINI:
  502. printf("%d %d %d %d",a,sb,c,isk);
  503. printf(COMMENT "%s",eventname(c));
  504. if (isk) printf(" flip");
  505. break;
  506. case OP_MMBINK:
  507. printf("%d %d %d %d",a,b,c,isk);
  508. printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b);
  509. if (isk) printf(" flip");
  510. break;
  511. case OP_UNM:
  512. printf("%d %d",a,b);
  513. break;
  514. case OP_BNOT:
  515. printf("%d %d",a,b);
  516. break;
  517. case OP_NOT:
  518. printf("%d %d",a,b);
  519. break;
  520. case OP_LEN:
  521. printf("%d %d",a,b);
  522. break;
  523. case OP_CONCAT:
  524. printf("%d %d",a,b);
  525. break;
  526. case OP_CLOSE:
  527. printf("%d",a);
  528. break;
  529. case OP_TBC:
  530. printf("%d",a);
  531. break;
  532. case OP_JMP:
  533. printf("%d",GETARG_sJ(i));
  534. printf(COMMENT "to %d",GETARG_sJ(i)+pc+2);
  535. break;
  536. case OP_EQ:
  537. printf("%d %d %d",a,b,isk);
  538. break;
  539. case OP_LT:
  540. printf("%d %d %d",a,b,isk);
  541. break;
  542. case OP_LE:
  543. printf("%d %d %d",a,b,isk);
  544. break;
  545. case OP_EQK:
  546. printf("%d %d %d",a,b,isk);
  547. printf(COMMENT); PrintConstant(f,b);
  548. break;
  549. case OP_EQI:
  550. printf("%d %d %d",a,sb,isk);
  551. break;
  552. case OP_LTI:
  553. printf("%d %d %d",a,sb,isk);
  554. break;
  555. case OP_LEI:
  556. printf("%d %d %d",a,sb,isk);
  557. break;
  558. case OP_GTI:
  559. printf("%d %d %d",a,sb,isk);
  560. break;
  561. case OP_GEI:
  562. printf("%d %d %d",a,sb,isk);
  563. break;
  564. case OP_TEST:
  565. printf("%d %d",a,isk);
  566. break;
  567. case OP_TESTSET:
  568. printf("%d %d %d",a,b,isk);
  569. break;
  570. case OP_CALL:
  571. printf("%d %d %d",a,b,c);
  572. printf(COMMENT);
  573. if (b==0) printf("all in "); else printf("%d in ",b-1);
  574. if (c==0) printf("all out"); else printf("%d out",c-1);
  575. break;
  576. case OP_TAILCALL:
  577. printf("%d %d %d%s",a,b,c,ISK);
  578. printf(COMMENT "%d in",b-1);
  579. break;
  580. case OP_RETURN:
  581. printf("%d %d %d%s",a,b,c,ISK);
  582. printf(COMMENT);
  583. if (b==0) printf("all out"); else printf("%d out",b-1);
  584. break;
  585. case OP_RETURN0:
  586. break;
  587. case OP_RETURN1:
  588. printf("%d",a);
  589. break;
  590. case OP_FORLOOP:
  591. printf("%d %d",a,bx);
  592. printf(COMMENT "to %d",pc-bx+2);
  593. break;
  594. case OP_FORPREP:
  595. printf("%d %d",a,bx);
  596. printf(COMMENT "exit to %d",pc+bx+3);
  597. break;
  598. case OP_TFORPREP:
  599. printf("%d %d",a,bx);
  600. printf(COMMENT "to %d",pc+bx+2);
  601. break;
  602. case OP_TFORCALL:
  603. printf("%d %d",a,c);
  604. break;
  605. case OP_TFORLOOP:
  606. printf("%d %d",a,bx);
  607. printf(COMMENT "to %d",pc-bx+2);
  608. break;
  609. case OP_SETLIST:
  610. printf("%d %d %d",a,b,c);
  611. if (isk) printf(COMMENT "%d",c+EXTRAARGC);
  612. break;
  613. case OP_CLOSURE:
  614. printf("%d %d",a,bx);
  615. printf(COMMENT "%p",VOID(f->p[bx]));
  616. break;
  617. case OP_VARARG:
  618. printf("%d %d",a,c);
  619. printf(COMMENT);
  620. if (c==0) printf("all out"); else printf("%d out",c-1);
  621. break;
  622. case OP_VARARGPREP:
  623. printf("%d",a);
  624. break;
  625. case OP_EXTRAARG:
  626. printf("%d",ax);
  627. break;
  628. #if 0
  629. default:
  630. printf("%d %d %d",a,b,c);
  631. printf(COMMENT "not handled");
  632. break;
  633. #endif
  634. }
  635. printf("\n");
  636. }
  637. }
  638. #define SS(x) ((x==1)?"":"s")
  639. #define S(x) (int)(x),SS(x)
  640. static void PrintHeader(const Proto* f)
  641. {
  642. const char* s=f->source ? getstr(f->source) : "=?";
  643. if (*s=='@' || *s=='=')
  644. s++;
  645. else if (*s==LUA_SIGNATURE[0])
  646. s="(bstring)";
  647. else
  648. s="(string)";
  649. printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
  650. (f->linedefined==0)?"main":"function",s,
  651. f->linedefined,f->lastlinedefined,
  652. S(f->sizecode),VOID(f));
  653. printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
  654. (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
  655. S(f->maxstacksize),S(f->sizeupvalues));
  656. printf("%d local%s, %d constant%s, %d function%s\n",
  657. S(f->sizelocvars),S(f->sizek),S(f->sizep));
  658. }
  659. static void PrintDebug(const Proto* f)
  660. {
  661. int i,n;
  662. n=f->sizek;
  663. printf("constants (%d) for %p:\n",n,VOID(f));
  664. for (i=0; i<n; i++)
  665. {
  666. printf("\t%d\t",i);
  667. PrintType(f,i);
  668. PrintConstant(f,i);
  669. printf("\n");
  670. }
  671. n=f->sizelocvars;
  672. printf("locals (%d) for %p:\n",n,VOID(f));
  673. for (i=0; i<n; i++)
  674. {
  675. printf("\t%d\t%s\t%d\t%d\n",
  676. i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
  677. }
  678. n=f->sizeupvalues;
  679. printf("upvalues (%d) for %p:\n",n,VOID(f));
  680. for (i=0; i<n; i++)
  681. {
  682. printf("\t%d\t%s\t%d\t%d\n",
  683. i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
  684. }
  685. }
  686. static void PrintFunction(const Proto* f, int full)
  687. {
  688. int i,n=f->sizep;
  689. PrintHeader(f);
  690. PrintCode(f);
  691. if (full) PrintDebug(f);
  692. for (i=0; i<n; i++) PrintFunction(f->p[i],full);
  693. }