luac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. ** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $
  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 "lobject.h"
  17. #include "lstate.h"
  18. #include "lundump.h"
  19. static void PrintFunction(const Proto *f, int full);
  20. #define luaU_print PrintFunction
  21. #define PROGNAME "luac" /* default program name */
  22. #define OUTPUT PROGNAME ".out" /* default output file */
  23. static int listing = 0; /* list bytecodes? */
  24. static int dumping = 1; /* dump bytecodes? */
  25. static int stripping = 0; /* strip debug information? */
  26. static char Output[] = { OUTPUT }; /* default output file name */
  27. static const char *output = Output; /* actual output file name */
  28. static const char *progname = PROGNAME; /* actual program name */
  29. static void fatal(const char *message)
  30. {
  31. fprintf(stderr, "%s: %s\n", progname, message);
  32. exit(EXIT_FAILURE);
  33. }
  34. static void cannot(const char *what)
  35. {
  36. fprintf(stderr, "%s: cannot %s %s: %s\n", progname, what, output, strerror(errno));
  37. exit(EXIT_FAILURE);
  38. }
  39. static void usage(const char *message)
  40. {
  41. if (*message == '-')
  42. fprintf(stderr, "%s: unrecognized option '%s'\n", progname, message);
  43. else
  44. fprintf(stderr, "%s: %s\n", progname, message);
  45. fprintf(stderr,
  46. "usage: %s [options] [filenames]\n"
  47. "Available options are:\n"
  48. " -l list (use -l -l for full listing)\n"
  49. " -o name output to file 'name' (default is \"%s\")\n"
  50. " -p parse only\n"
  51. " -s strip debug information\n"
  52. " -v show version information\n"
  53. " -- stop handling options\n"
  54. " - stop handling options and process stdin\n"
  55. , progname, Output);
  56. exit(EXIT_FAILURE);
  57. }
  58. #define IS(s) (strcmp(argv[i],s)==0)
  59. static int doargs(int argc, char *argv[])
  60. {
  61. int i;
  62. int version = 0;
  63. if (argv[0] != NULL && *argv[0] != 0) progname = argv[0];
  64. for (i = 1; i < argc; i++)
  65. {
  66. if (*argv[i] != '-') /* end of options; keep it */
  67. break;
  68. else if (IS("--")) /* end of options; skip it */
  69. {
  70. ++i;
  71. if (version) ++version;
  72. break;
  73. }
  74. else if (IS("-")) /* end of options; use stdin */
  75. break;
  76. else if (IS("-l")) /* list */
  77. ++listing;
  78. else if (IS("-o")) /* output file */
  79. {
  80. output = argv[++i];
  81. if (output == NULL || *output == 0 || (*output == '-' && output[1] != 0))
  82. usage("'-o' needs argument");
  83. if (IS("-")) output = NULL;
  84. }
  85. else if (IS("-p")) /* parse only */
  86. dumping = 0;
  87. else if (IS("-s")) /* strip debug information */
  88. stripping = 1;
  89. else if (IS("-v")) /* show version */
  90. ++version;
  91. else /* unknown option */
  92. usage(argv[i]);
  93. }
  94. if (i == argc && (listing || !dumping))
  95. {
  96. dumping = 0;
  97. argv[--i] = Output;
  98. }
  99. if (version)
  100. {
  101. printf("%s\n", LUA_COPYRIGHT);
  102. if (version == argc - 1) exit(EXIT_SUCCESS);
  103. }
  104. return i;
  105. }
  106. #define FUNCTION "(function()end)();"
  107. static const char *reader(lua_State *L, void *ud, size_t *size)
  108. {
  109. UNUSED(L);
  110. if ((*(int *)ud)--)
  111. {
  112. *size = sizeof(FUNCTION) - 1;
  113. return FUNCTION;
  114. }
  115. else
  116. {
  117. *size = 0;
  118. return NULL;
  119. }
  120. }
  121. #define toproto(L,i) getproto(L->top+(i))
  122. static const Proto *combine(lua_State *L, int n)
  123. {
  124. if (n == 1)
  125. return toproto(L, -1);
  126. else
  127. {
  128. Proto *f;
  129. int i = n;
  130. if (lua_load(L, reader, &i, "=(" PROGNAME ")", NULL) != LUA_OK) fatal(lua_tostring(L, -1));
  131. f = toproto(L, -1);
  132. for (i = 0; i < n; i++)
  133. {
  134. f->p[i] = toproto(L, i - n - 1);
  135. if (f->p[i]->sizeupvalues > 0) f->p[i]->upvalues[0].instack = 0;
  136. }
  137. f->sizelineinfo = 0;
  138. return f;
  139. }
  140. }
  141. static int writer(lua_State *L, const void *p, size_t size, void *u)
  142. {
  143. UNUSED(L);
  144. return (fwrite(p, size, 1, (FILE *)u) != 1) && (size != 0);
  145. }
  146. static int pmain(lua_State *L)
  147. {
  148. int argc = (int)lua_tointeger(L, 1);
  149. char **argv = (char **)lua_touserdata(L, 2);
  150. const Proto *f;
  151. int i;
  152. if (!lua_checkstack(L, argc)) fatal("too many input files");
  153. for (i = 0; i < argc; i++)
  154. {
  155. const char *filename = IS("-") ? NULL : argv[i];
  156. if (luaL_loadfile(L, filename) != LUA_OK) fatal(lua_tostring(L, -1));
  157. }
  158. f = combine(L, argc);
  159. if (listing) luaU_print(f, listing > 1);
  160. if (dumping)
  161. {
  162. FILE *D = (output == NULL) ? stdout : fopen(output, "wb");
  163. if (D == NULL) cannot("open");
  164. lua_lock(L);
  165. luaU_dump(L, f, writer, D, stripping);
  166. lua_unlock(L);
  167. if (ferror(D)) cannot("write");
  168. if (fclose(D)) cannot("close");
  169. }
  170. return 0;
  171. }
  172. int main(int argc, char *argv[])
  173. {
  174. lua_State *L;
  175. int i = doargs(argc, argv);
  176. argc -= i;
  177. argv += i;
  178. if (argc <= 0) usage("no input files given");
  179. L = luaL_newstate();
  180. if (L == NULL) fatal("cannot create state: not enough memory");
  181. lua_pushcfunction(L, &pmain);
  182. lua_pushinteger(L, argc);
  183. lua_pushlightuserdata(L, argv);
  184. if (lua_pcall(L, 2, 0, 0) != LUA_OK) fatal(lua_tostring(L, -1));
  185. lua_close(L);
  186. return EXIT_SUCCESS;
  187. }
  188. /*
  189. ** $Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp $
  190. ** print bytecodes
  191. ** See Copyright Notice in lua.h
  192. */
  193. #include <ctype.h>
  194. #include <stdio.h>
  195. #define luac_c
  196. #define LUA_CORE
  197. #include "ldebug.h"
  198. #include "lobject.h"
  199. #include "lopcodes.h"
  200. #define VOID(p) ((const void*)(p))
  201. static void PrintString(const TString *ts)
  202. {
  203. const char *s = getstr(ts);
  204. size_t i, n = tsslen(ts);
  205. printf("%c", '"');
  206. for (i = 0; i < n; i++)
  207. {
  208. int c = (int)(unsigned char)s[i];
  209. switch (c)
  210. {
  211. case '"':
  212. printf("\\\"");
  213. break;
  214. case '\\':
  215. printf("\\\\");
  216. break;
  217. case '\a':
  218. printf("\\a");
  219. break;
  220. case '\b':
  221. printf("\\b");
  222. break;
  223. case '\f':
  224. printf("\\f");
  225. break;
  226. case '\n':
  227. printf("\\n");
  228. break;
  229. case '\r':
  230. printf("\\r");
  231. break;
  232. case '\t':
  233. printf("\\t");
  234. break;
  235. case '\v':
  236. printf("\\v");
  237. break;
  238. default:
  239. if (isprint(c))
  240. printf("%c", c);
  241. else
  242. printf("\\%03d", c);
  243. }
  244. }
  245. printf("%c", '"');
  246. }
  247. static void PrintConstant(const Proto *f, int i)
  248. {
  249. const TValue *o = &f->k[i];
  250. switch (ttype(o))
  251. {
  252. case LUA_TNIL:
  253. printf("nil");
  254. break;
  255. case LUA_TBOOLEAN:
  256. printf(bvalue(o) ? "true" : "false");
  257. break;
  258. case LUA_TNUMFLT:
  259. {
  260. char buff[100];
  261. sprintf(buff, LUA_NUMBER_FMT, fltvalue(o));
  262. printf("%s", buff);
  263. if (buff[strspn(buff, "-0123456789")] == '\0') printf(".0");
  264. break;
  265. }
  266. case LUA_TNUMINT:
  267. printf(LUA_INTEGER_FMT, ivalue(o));
  268. break;
  269. case LUA_TSHRSTR:
  270. case LUA_TLNGSTR:
  271. PrintString(tsvalue(o));
  272. break;
  273. default: /* cannot happen */
  274. printf("? type=%d", ttype(o));
  275. break;
  276. }
  277. }
  278. #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
  279. #define MYK(x) (-1-(x))
  280. static void PrintCode(const Proto *f)
  281. {
  282. const Instruction *code = f->code;
  283. int pc, n = f->sizecode;
  284. for (pc = 0; pc < n; pc++)
  285. {
  286. Instruction i = code[pc];
  287. OpCode o = GET_OPCODE(i);
  288. int a = GETARG_A(i);
  289. int b = GETARG_B(i);
  290. int c = GETARG_C(i);
  291. int ax = GETARG_Ax(i);
  292. int bx = GETARG_Bx(i);
  293. int sbx = GETARG_sBx(i);
  294. int line = getfuncline(f, pc);
  295. printf("\t%d\t", pc + 1);
  296. if (line > 0) printf("[%d]\t", line);
  297. else printf("[-]\t");
  298. printf("%-9s\t", luaP_opnames[o]);
  299. switch (getOpMode(o))
  300. {
  301. case iABC:
  302. printf("%d", a);
  303. if (getBMode(o) != OpArgN) printf(" %d", ISK(b) ? (MYK(INDEXK(b))) : b);
  304. if (getCMode(o) != OpArgN) printf(" %d", ISK(c) ? (MYK(INDEXK(c))) : c);
  305. break;
  306. case iABx:
  307. printf("%d", a);
  308. if (getBMode(o) == OpArgK) printf(" %d", MYK(bx));
  309. if (getBMode(o) == OpArgU) printf(" %d", bx);
  310. break;
  311. case iAsBx:
  312. printf("%d %d", a, sbx);
  313. break;
  314. case iAx:
  315. printf("%d", MYK(ax));
  316. break;
  317. }
  318. switch (o)
  319. {
  320. case OP_LOADK:
  321. printf("\t; ");
  322. PrintConstant(f, bx);
  323. break;
  324. case OP_GETUPVAL:
  325. case OP_SETUPVAL:
  326. printf("\t; %s", UPVALNAME(b));
  327. break;
  328. case OP_GETTABUP:
  329. printf("\t; %s", UPVALNAME(b));
  330. if (ISK(c))
  331. {
  332. printf(" ");
  333. PrintConstant(f, INDEXK(c));
  334. }
  335. break;
  336. case OP_SETTABUP:
  337. printf("\t; %s", UPVALNAME(a));
  338. if (ISK(b))
  339. {
  340. printf(" ");
  341. PrintConstant(f, INDEXK(b));
  342. }
  343. if (ISK(c))
  344. {
  345. printf(" ");
  346. PrintConstant(f, INDEXK(c));
  347. }
  348. break;
  349. case OP_GETTABLE:
  350. case OP_SELF:
  351. if (ISK(c))
  352. {
  353. printf("\t; ");
  354. PrintConstant(f, INDEXK(c));
  355. }
  356. break;
  357. case OP_SETTABLE:
  358. case OP_ADD:
  359. case OP_SUB:
  360. case OP_MUL:
  361. case OP_POW:
  362. case OP_DIV:
  363. case OP_IDIV:
  364. case OP_BAND:
  365. case OP_BOR:
  366. case OP_BXOR:
  367. case OP_SHL:
  368. case OP_SHR:
  369. case OP_EQ:
  370. case OP_LT:
  371. case OP_LE:
  372. if (ISK(b) || ISK(c))
  373. {
  374. printf("\t; ");
  375. if (ISK(b)) PrintConstant(f, INDEXK(b));
  376. else printf("-");
  377. printf(" ");
  378. if (ISK(c)) PrintConstant(f, INDEXK(c));
  379. else printf("-");
  380. }
  381. break;
  382. case OP_JMP:
  383. case OP_FORLOOP:
  384. case OP_FORPREP:
  385. case OP_TFORLOOP:
  386. printf("\t; to %d", sbx + pc + 2);
  387. break;
  388. case OP_CLOSURE:
  389. printf("\t; %p", VOID(f->p[bx]));
  390. break;
  391. case OP_SETLIST:
  392. if (c == 0) printf("\t; %d", (int)code[++pc]);
  393. else printf("\t; %d", c);
  394. break;
  395. case OP_EXTRAARG:
  396. printf("\t; ");
  397. PrintConstant(f, ax);
  398. break;
  399. default:
  400. break;
  401. }
  402. printf("\n");
  403. }
  404. }
  405. #define SS(x) ((x==1)?"":"s")
  406. #define S(x) (int)(x),SS(x)
  407. static void PrintHeader(const Proto *f)
  408. {
  409. const char *s = f->source ? getstr(f->source) : "=?";
  410. if (*s == '@' || *s == '=')
  411. s++;
  412. else if (*s == LUA_SIGNATURE[0])
  413. s = "(bstring)";
  414. else
  415. s = "(string)";
  416. printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
  417. (f->linedefined == 0) ? "main" : "function", s,
  418. f->linedefined, f->lastlinedefined,
  419. S(f->sizecode), VOID(f));
  420. printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
  421. (int)(f->numparams), f->is_vararg ? "+" : "", SS(f->numparams),
  422. S(f->maxstacksize), S(f->sizeupvalues));
  423. printf("%d local%s, %d constant%s, %d function%s\n",
  424. S(f->sizelocvars), S(f->sizek), S(f->sizep));
  425. }
  426. static void PrintDebug(const Proto *f)
  427. {
  428. int i, n;
  429. n = f->sizek;
  430. printf("constants (%d) for %p:\n", n, VOID(f));
  431. for (i = 0; i < n; i++)
  432. {
  433. printf("\t%d\t", i + 1);
  434. PrintConstant(f, i);
  435. printf("\n");
  436. }
  437. n = f->sizelocvars;
  438. printf("locals (%d) for %p:\n", n, VOID(f));
  439. for (i = 0; i < n; i++)
  440. {
  441. printf("\t%d\t%s\t%d\t%d\n",
  442. i, getstr(f->locvars[i].varname), f->locvars[i].startpc + 1, f->locvars[i].endpc + 1);
  443. }
  444. n = f->sizeupvalues;
  445. printf("upvalues (%d) for %p:\n", n, VOID(f));
  446. for (i = 0; i < n; i++)
  447. {
  448. printf("\t%d\t%s\t%d\t%d\n",
  449. i, UPVALNAME(i), f->upvalues[i].instack, f->upvalues[i].idx);
  450. }
  451. }
  452. static void PrintFunction(const Proto *f, int full)
  453. {
  454. int i, n = f->sizep;
  455. PrintHeader(f);
  456. PrintCode(f);
  457. if (full) PrintDebug(f);
  458. for (i = 0; i < n; i++) PrintFunction(f->p[i], full);
  459. }