lua.stx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. %{
  2. char *rcs_luastx = "$Id: lua.stx,v 3.12 1994/11/25 19:24:57 roberto Exp $";
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "mem.h"
  7. #include "opcode.h"
  8. #include "hash.h"
  9. #include "inout.h"
  10. #include "tree.h"
  11. #include "table.h"
  12. #include "lua.h"
  13. /* to avoid warnings generated by yacc */
  14. int yyparse (void);
  15. #define malloc luaI_malloc
  16. #define realloc luaI_realloc
  17. #define free luaI_free
  18. #ifndef LISTING
  19. #define LISTING 0
  20. #endif
  21. #ifndef CODE_BLOCK
  22. #define CODE_BLOCK 256
  23. #endif
  24. static Long maxcode;
  25. static Long maxmain;
  26. static Long maxcurr ;
  27. static Byte *funcCode = NULL;
  28. static Byte **initcode;
  29. static Byte *basepc;
  30. static Long maincode;
  31. static Long pc;
  32. #define MAXVAR 32
  33. static long varbuffer[MAXVAR]; /* variables in an assignment list;
  34. it's long to store negative Word values */
  35. static int nvarbuffer=0; /* number of variables at a list */
  36. static Word localvar[STACKGAP]; /* store local variable names */
  37. static int nlocalvar=0; /* number of local variables */
  38. #define MAXFIELDS FIELDS_PER_FLUSH*2
  39. static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
  40. static int nfields=0;
  41. /* Internal functions */
  42. static void code_byte (Byte c)
  43. {
  44. if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
  45. {
  46. maxcurr *= 2;
  47. basepc = growvector(basepc, maxcurr, Byte);
  48. }
  49. basepc[pc++] = c;
  50. }
  51. static void code_word (Word n)
  52. {
  53. CodeWord code;
  54. code.w = n;
  55. code_byte(code.m.c1);
  56. code_byte(code.m.c2);
  57. }
  58. static void code_float (float n)
  59. {
  60. CodeFloat code;
  61. code.f = n;
  62. code_byte(code.m.c1);
  63. code_byte(code.m.c2);
  64. code_byte(code.m.c3);
  65. code_byte(code.m.c4);
  66. }
  67. static void code_code (Byte *b)
  68. {
  69. CodeCode code;
  70. code.b = b;
  71. code_byte(code.m.c1);
  72. code_byte(code.m.c2);
  73. code_byte(code.m.c3);
  74. code_byte(code.m.c4);
  75. }
  76. static void code_word_at (Byte *p, Word n)
  77. {
  78. CodeWord code;
  79. code.w = n;
  80. *p++ = code.m.c1;
  81. *p++ = code.m.c2;
  82. }
  83. static void push_field (Word name)
  84. {
  85. if (nfields < STACKGAP-1)
  86. fields[nfields++] = name;
  87. else
  88. lua_error ("too many fields in a constructor");
  89. }
  90. static void flush_record (int n)
  91. {
  92. int i;
  93. if (n == 0) return;
  94. code_byte(STORERECORD);
  95. code_byte(n);
  96. for (i=0; i<n; i++)
  97. code_word(fields[--nfields]);
  98. }
  99. static void flush_list (int m, int n)
  100. {
  101. if (n == 0) return;
  102. if (m == 0)
  103. code_byte(STORELIST0);
  104. else
  105. if (m < 255)
  106. {
  107. code_byte(STORELIST);
  108. code_byte(m);
  109. }
  110. else
  111. lua_error ("list constructor too long");
  112. code_byte(n);
  113. }
  114. static void add_nlocalvar (int n)
  115. {
  116. if (MAX_TEMPS+nlocalvar+MAXVAR+n < STACKGAP)
  117. nlocalvar += n;
  118. else
  119. lua_error ("too many local variables");
  120. }
  121. static void incr_nvarbuffer (void)
  122. {
  123. if (nvarbuffer < MAXVAR-1)
  124. nvarbuffer++;
  125. else
  126. lua_error ("variable buffer overflow");
  127. }
  128. static void code_number (float f)
  129. {
  130. Word i = (Word)f;
  131. if (f == (float)i) /* f has an (short) integer value */
  132. {
  133. if (i <= 2) code_byte(PUSH0 + i);
  134. else if (i <= 255)
  135. {
  136. code_byte(PUSHBYTE);
  137. code_byte(i);
  138. }
  139. else
  140. {
  141. code_byte(PUSHWORD);
  142. code_word(i);
  143. }
  144. }
  145. else
  146. {
  147. code_byte(PUSHFLOAT);
  148. code_float(f);
  149. }
  150. }
  151. %}
  152. %union
  153. {
  154. int vInt;
  155. float vFloat;
  156. char *pChar;
  157. Word vWord;
  158. Long vLong;
  159. Byte *pByte;
  160. TreeNode *pNode;
  161. }
  162. %start functionlist
  163. %token WRONGTOKEN
  164. %token NIL
  165. %token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
  166. %token RETURN
  167. %token LOCAL
  168. %token FUNCTION
  169. %token <vFloat> NUMBER
  170. %token <vWord> STRING
  171. %token <pNode> NAME
  172. %token <vInt> DEBUG
  173. %type <vLong> PrepJump
  174. %type <vInt> expr, exprlist, exprlist1, varlist1, funcParams, funcvalue
  175. %type <vInt> fieldlist, localdeclist, decinit
  176. %type <vInt> ffieldlist1
  177. %type <vInt> lfieldlist1
  178. %type <vLong> var, singlevar
  179. %type <pByte> body
  180. %left AND OR
  181. %left EQ NE '>' '<' LE GE
  182. %left CONC
  183. %left '+' '-'
  184. %left '*' '/'
  185. %left UNARY NOT
  186. %right '^'
  187. %% /* beginning of rules section */
  188. functionlist : /* empty */
  189. | functionlist
  190. {
  191. pc=maincode; basepc=*initcode; maxcurr=maxmain;
  192. nlocalvar=0;
  193. }
  194. stat sc
  195. {
  196. maincode=pc; *initcode=basepc; maxmain=maxcurr;
  197. }
  198. | functionlist function
  199. | functionlist method
  200. | functionlist setdebug
  201. ;
  202. function : FUNCTION NAME
  203. {
  204. init_function($2);
  205. }
  206. body
  207. {
  208. Word func = luaI_findsymbol($2);
  209. s_tag(func) = LUA_T_FUNCTION;
  210. s_bvalue(func) = $4;
  211. }
  212. ;
  213. method : FUNCTION NAME ':' NAME
  214. {
  215. init_function($4);
  216. localvar[nlocalvar]=luaI_findsymbolbyname("self");
  217. add_nlocalvar(1);
  218. }
  219. body
  220. {
  221. /* assign function to table field */
  222. pc=maincode; basepc=*initcode; maxcurr=maxmain;
  223. nlocalvar=0;
  224. lua_pushvar(luaI_findsymbol($2)+1);
  225. code_byte(PUSHSTRING);
  226. code_word(luaI_findconstant($4));
  227. code_byte(PUSHFUNCTION);
  228. code_code($6);
  229. code_byte(STOREINDEXED0);
  230. maincode=pc; *initcode=basepc; maxmain=maxcurr;
  231. }
  232. ;
  233. body : '(' parlist ')' block END
  234. {
  235. codereturn();
  236. $$ = newvector(pc, Byte);
  237. memcpy($$, basepc, pc*sizeof(Byte));
  238. funcCode = basepc; maxcode=maxcurr;
  239. #if LISTING
  240. PrintCode(funcCode,funcCode+pc);
  241. #endif
  242. }
  243. ;
  244. statlist : /* empty */
  245. | statlist stat sc
  246. ;
  247. sc : /* empty */ | ';' ;
  248. stat : { codedebugline(); } stat1 ;
  249. cond : { codedebugline(); } expr1 ;
  250. stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
  251. { codeIf($4, $6); }
  252. | WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
  253. {
  254. basepc[$5] = IFFJMP;
  255. code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
  256. basepc[$7] = UPJMP;
  257. code_word_at(basepc+$7+1, pc - ($<vLong>2));
  258. }
  259. | REPEAT {$<vLong>$=pc;} block UNTIL cond PrepJump
  260. {
  261. basepc[$6] = IFFUPJMP;
  262. code_word_at(basepc+$6+1, pc - ($<vLong>2));
  263. }
  264. | varlist1 '=' exprlist1
  265. {
  266. {
  267. int i;
  268. adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
  269. for (i=nvarbuffer-1; i>=0; i--)
  270. lua_codestore (i);
  271. if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
  272. lua_codeadjust (0);
  273. }
  274. }
  275. | functioncall { code_byte(0); }
  276. | LOCAL localdeclist decinit
  277. { add_nlocalvar($2);
  278. adjust_mult_assign($2, $3, 0);
  279. }
  280. ;
  281. elsepart : /* empty */
  282. | ELSE block
  283. | ELSEIF cond THEN PrepJump block PrepJump elsepart
  284. { codeIf($4, $6); }
  285. ;
  286. block : {$<vInt>$ = nlocalvar;} statlist ret
  287. {
  288. if (nlocalvar != $<vInt>1)
  289. {
  290. nlocalvar = $<vInt>1;
  291. lua_codeadjust (0);
  292. }
  293. }
  294. ;
  295. ret : /* empty */
  296. | RETURN { codedebugline(); } exprlist sc
  297. {
  298. if ($3 < 0) code_byte(MULT_RET);
  299. codereturn();
  300. }
  301. ;
  302. PrepJump : /* empty */
  303. {
  304. $$ = pc;
  305. code_byte(0); /* open space */
  306. code_word (0);
  307. }
  308. expr1 : expr { if ($1 == 0) code_byte(1); }
  309. ;
  310. expr : '(' expr ')' { $$ = $2; }
  311. | expr1 EQ expr1 { code_byte(EQOP); $$ = 1; }
  312. | expr1 '<' expr1 { code_byte(LTOP); $$ = 1; }
  313. | expr1 '>' expr1 { code_byte(GTOP); $$ = 1; }
  314. | expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; }
  315. | expr1 LE expr1 { code_byte(LEOP); $$ = 1; }
  316. | expr1 GE expr1 { code_byte(GEOP); $$ = 1; }
  317. | expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; }
  318. | expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; }
  319. | expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; }
  320. | expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; }
  321. | expr1 '^' expr1 { code_byte(POWOP); $$ = 1; }
  322. | expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; }
  323. | '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
  324. | table { $$ = 1; }
  325. | varexp { $$ = 1;}
  326. | NUMBER { code_number($1); $$ = 1; }
  327. | STRING
  328. {
  329. code_byte(PUSHSTRING);
  330. code_word($1);
  331. $$ = 1;
  332. }
  333. | NIL {code_byte(PUSHNIL); $$ = 1; }
  334. | functioncall { $$ = 0; }
  335. | NOT expr1 { code_byte(NOTOP); $$ = 1;}
  336. | expr1 AND PrepJump {code_byte(POP); } expr1
  337. {
  338. basepc[$3] = ONFJMP;
  339. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  340. $$ = 1;
  341. }
  342. | expr1 OR PrepJump {code_byte(POP); } expr1
  343. {
  344. basepc[$3] = ONTJMP;
  345. code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
  346. $$ = 1;
  347. }
  348. ;
  349. table :
  350. {
  351. code_byte(CREATEARRAY);
  352. $<vLong>$ = pc; code_word(0);
  353. }
  354. '{' fieldlist '}'
  355. {
  356. code_word_at(basepc+$<vLong>1, $3);
  357. }
  358. ;
  359. functioncall : funcvalue funcParams
  360. { code_byte(CALLFUNC); code_byte($1+$2); }
  361. ;
  362. funcvalue : varexp { $$ = 0; }
  363. | varexp ':' NAME
  364. {
  365. code_byte(PUSHSELF);
  366. code_word(luaI_findconstant($3));
  367. $$ = 1;
  368. }
  369. ;
  370. funcParams : '(' exprlist ')'
  371. { if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; }
  372. | table { $$ = 1; }
  373. ;
  374. exprlist : /* empty */ { $$ = 0; }
  375. | exprlist1 { $$ = $1; }
  376. ;
  377. exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
  378. | exprlist1 ',' { if ($1 < 0) code_byte(1); } expr
  379. {
  380. int r = $1 < 0 ? -$1 : $1;
  381. $$ = ($4 == 0) ? -(r+1) : r+1;
  382. }
  383. ;
  384. parlist : /* empty */ { lua_codeadjust(0); }
  385. | parlist1 { lua_codeadjust(0); }
  386. ;
  387. parlist1 : NAME
  388. {
  389. localvar[nlocalvar]=luaI_findsymbol($1);
  390. add_nlocalvar(1);
  391. }
  392. | parlist1 ',' NAME
  393. {
  394. localvar[nlocalvar]=luaI_findsymbol($3);
  395. add_nlocalvar(1);
  396. }
  397. ;
  398. fieldlist : /* empty */ { $$ = 0; }
  399. | lfieldlist1 lastcomma
  400. { $$ = $1; flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  401. | ffieldlist1 lastcomma
  402. { $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
  403. | lfieldlist1 ';'
  404. { flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
  405. ffieldlist1 lastcomma
  406. { $$ = $1+$4; flush_record($4%FIELDS_PER_FLUSH); }
  407. ;
  408. lastcomma : /* empty */
  409. | ','
  410. ;
  411. ffieldlist1 : ffield {$$=1;}
  412. | ffieldlist1 ',' ffield
  413. {
  414. $$=$1+1;
  415. if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
  416. }
  417. ;
  418. ffield : NAME '=' expr1
  419. {
  420. push_field(luaI_findconstant($1));
  421. }
  422. ;
  423. lfieldlist1 : expr1 {$$=1;}
  424. | lfieldlist1 ',' expr1
  425. {
  426. $$=$1+1;
  427. if ($$%FIELDS_PER_FLUSH == 0)
  428. flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
  429. }
  430. ;
  431. varlist1 : var
  432. {
  433. nvarbuffer = 0;
  434. varbuffer[nvarbuffer] = $1; incr_nvarbuffer();
  435. $$ = ($1 == 0) ? 1 : 0;
  436. }
  437. | varlist1 ',' var
  438. {
  439. varbuffer[nvarbuffer] = $3; incr_nvarbuffer();
  440. $$ = ($3 == 0) ? $1 + 1 : $1;
  441. }
  442. ;
  443. var : singlevar { $$ = $1; }
  444. | varexp '[' expr1 ']'
  445. {
  446. $$ = 0; /* indexed variable */
  447. }
  448. | varexp '.' NAME
  449. {
  450. code_byte(PUSHSTRING);
  451. code_word(luaI_findconstant($3));
  452. $$ = 0; /* indexed variable */
  453. }
  454. ;
  455. singlevar : NAME
  456. {
  457. Word s = luaI_findsymbol($1);
  458. int local = lua_localname (s);
  459. if (local == -1) /* global var */
  460. $$ = s + 1; /* return positive value */
  461. else
  462. $$ = -(local+1); /* return negative value */
  463. }
  464. ;
  465. varexp : var { lua_pushvar($1); }
  466. ;
  467. localdeclist : NAME {localvar[nlocalvar]=luaI_findsymbol($1); $$ = 1;}
  468. | localdeclist ',' NAME
  469. {
  470. localvar[nlocalvar+$1]=luaI_findsymbol($3);
  471. $$ = $1+1;
  472. }
  473. ;
  474. decinit : /* empty */ { $$ = 0; }
  475. | '=' exprlist1 { $$ = $2; }
  476. ;
  477. setdebug : DEBUG {lua_debug = $1;}
  478. %%
  479. /*
  480. ** Search a local name and if find return its index. If do not find return -1
  481. */
  482. static int lua_localname (Word n)
  483. {
  484. int i;
  485. for (i=nlocalvar-1; i >= 0; i--)
  486. if (n == localvar[i]) return i; /* local var */
  487. return -1; /* global var */
  488. }
  489. /*
  490. ** Push a variable given a number. If number is positive, push global variable
  491. ** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
  492. ** Otherwise, if zero, push indexed variable (record).
  493. */
  494. static void lua_pushvar (long number)
  495. {
  496. if (number > 0) /* global var */
  497. {
  498. code_byte(PUSHGLOBAL);
  499. code_word(number-1);
  500. }
  501. else if (number < 0) /* local var */
  502. {
  503. number = (-number) - 1;
  504. if (number < 10) code_byte(PUSHLOCAL0 + number);
  505. else
  506. {
  507. code_byte(PUSHLOCAL);
  508. code_byte(number);
  509. }
  510. }
  511. else
  512. {
  513. code_byte(PUSHINDEXED);
  514. }
  515. }
  516. static void lua_codeadjust (int n)
  517. {
  518. if (n+nlocalvar == 0)
  519. code_byte(ADJUST0);
  520. else
  521. {
  522. code_byte(ADJUST);
  523. code_byte(n+nlocalvar);
  524. }
  525. }
  526. static void init_function (TreeNode *func)
  527. {
  528. if (funcCode == NULL) /* first function */
  529. {
  530. funcCode = newvector(CODE_BLOCK, Byte);
  531. maxcode = CODE_BLOCK;
  532. }
  533. pc=0; basepc=funcCode; maxcurr=maxcode;
  534. nlocalvar=0;
  535. if (lua_debug)
  536. {
  537. code_byte(SETFUNCTION);
  538. code_code((Byte *)strdup(lua_file[lua_nfile-1]));
  539. code_word(luaI_findconstant(func));
  540. }
  541. }
  542. static void codereturn (void)
  543. {
  544. if (lua_debug) code_byte(RESET);
  545. if (nlocalvar == 0)
  546. code_byte(RETCODE0);
  547. else
  548. {
  549. code_byte(RETCODE);
  550. code_byte(nlocalvar);
  551. }
  552. }
  553. static void codedebugline (void)
  554. {
  555. if (lua_debug)
  556. {
  557. code_byte(SETLINE);
  558. code_word(lua_linenumber);
  559. }
  560. }
  561. static void adjust_mult_assign (int vars, int exps, int temps)
  562. {
  563. if (exps < 0)
  564. {
  565. int r = vars - (-exps-1);
  566. if (r >= 0)
  567. code_byte(r);
  568. else
  569. {
  570. code_byte(0);
  571. lua_codeadjust(temps);
  572. }
  573. }
  574. else if (vars != exps)
  575. lua_codeadjust(temps);
  576. }
  577. static void lua_codestore (int i)
  578. {
  579. if (varbuffer[i] > 0) /* global var */
  580. {
  581. code_byte(STOREGLOBAL);
  582. code_word(varbuffer[i]-1);
  583. }
  584. else if (varbuffer[i] < 0) /* local var */
  585. {
  586. int number = (-varbuffer[i]) - 1;
  587. if (number < 10) code_byte(STORELOCAL0 + number);
  588. else
  589. {
  590. code_byte(STORELOCAL);
  591. code_byte(number);
  592. }
  593. }
  594. else /* indexed var */
  595. {
  596. int j;
  597. int upper=0; /* number of indexed variables upper */
  598. int param; /* number of itens until indexed expression */
  599. for (j=i+1; j <nvarbuffer; j++)
  600. if (varbuffer[j] == 0) upper++;
  601. param = upper*2 + i;
  602. if (param == 0)
  603. code_byte(STOREINDEXED0);
  604. else
  605. {
  606. code_byte(STOREINDEXED);
  607. code_byte(param);
  608. }
  609. }
  610. }
  611. static void codeIf (Long thenAdd, Long elseAdd)
  612. {
  613. Long elseinit = elseAdd+sizeof(Word)+1;
  614. if (pc == elseinit) /* no else */
  615. {
  616. pc -= sizeof(Word)+1;
  617. elseinit = pc;
  618. }
  619. else
  620. {
  621. basepc[elseAdd] = JMP;
  622. code_word_at(basepc+elseAdd+1, pc-elseinit);
  623. }
  624. basepc[thenAdd] = IFFJMP;
  625. code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
  626. }
  627. static void yyerror (char *s)
  628. {
  629. static char msg[256];
  630. sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
  631. s, lua_lasttext (), lua_linenumber, lua_filename());
  632. lua_error (msg);
  633. }
  634. /*
  635. ** Parse LUA code.
  636. */
  637. void lua_parse (Byte **code)
  638. {
  639. initcode = code;
  640. *initcode = newvector(CODE_BLOCK, Byte);
  641. maincode = 0;
  642. maxmain = CODE_BLOCK;
  643. if (yyparse ()) lua_error("parse error");
  644. (*initcode)[maincode++] = RETCODE0;
  645. #if LISTING
  646. { static void PrintCode (Byte *c, Byte *end);
  647. PrintCode(*initcode,*initcode+maincode); }
  648. #endif
  649. }
  650. #if LISTING
  651. static void PrintCode (Byte *code, Byte *end)
  652. {
  653. Byte *p = code;
  654. printf ("\n\nCODE\n");
  655. while (p != end)
  656. {
  657. switch ((OpCode)*p)
  658. {
  659. case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
  660. case PUSH0: case PUSH1: case PUSH2:
  661. printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
  662. p++;
  663. break;
  664. case PUSHBYTE:
  665. printf ("%d PUSHBYTE %d\n", p-code, *(++p));
  666. p++;
  667. break;
  668. case PUSHWORD:
  669. {
  670. CodeWord c;
  671. int n = p-code;
  672. p++;
  673. get_word(c,p);
  674. printf ("%d PUSHWORD %d\n", n, c.w);
  675. }
  676. break;
  677. case PUSHFLOAT:
  678. {
  679. CodeFloat c;
  680. int n = p-code;
  681. p++;
  682. get_float(c,p);
  683. printf ("%d PUSHFLOAT %f\n", n, c.f);
  684. }
  685. break;
  686. case PUSHSTRING:
  687. {
  688. CodeWord c;
  689. int n = p-code;
  690. p++;
  691. get_word(c,p);
  692. printf ("%d PUSHSTRING %d\n", n, c.w);
  693. }
  694. break;
  695. case PUSHFUNCTION:
  696. {
  697. CodeCode c;
  698. int n = p-code;
  699. p++;
  700. get_code(c,p);
  701. printf ("%d PUSHFUNCTION %p\n", n, c.b);
  702. }
  703. break;
  704. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  705. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  706. case PUSHLOCAL8: case PUSHLOCAL9:
  707. printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
  708. p++;
  709. break;
  710. case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
  711. p++;
  712. break;
  713. case PUSHGLOBAL:
  714. {
  715. CodeWord c;
  716. int n = p-code;
  717. p++;
  718. get_word(c,p);
  719. printf ("%d PUSHGLOBAL %d\n", n, c.w);
  720. }
  721. break;
  722. case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
  723. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
  724. case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
  725. case STORELOCAL8: case STORELOCAL9:
  726. printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
  727. p++;
  728. break;
  729. case STORELOCAL:
  730. printf ("%d STORELOCAL %d\n", p-code, *(++p));
  731. p++;
  732. break;
  733. case STOREGLOBAL:
  734. {
  735. CodeWord c;
  736. int n = p-code;
  737. p++;
  738. get_word(c,p);
  739. printf ("%d STOREGLOBAL %d\n", n, c.w);
  740. }
  741. break;
  742. case PUSHSELF:
  743. {
  744. CodeWord c;
  745. int n = p-code;
  746. p++;
  747. get_word(c,p);
  748. printf ("%d PUSHSELF %d\n", n, c.w);
  749. }
  750. break;
  751. case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
  752. case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
  753. p++;
  754. break;
  755. case STORELIST0:
  756. printf("%d STORELIST0 %d\n", p-code, *(++p));
  757. p++;
  758. break;
  759. case STORELIST:
  760. printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
  761. p+=3;
  762. break;
  763. case STORERECORD:
  764. printf("%d STORERECORD %d\n", p-code, *(++p));
  765. p += *p*sizeof(Word) + 1;
  766. break;
  767. case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
  768. case ADJUST:
  769. printf ("%d ADJUST %d\n", p-code, *(++p));
  770. p++;
  771. break;
  772. case CREATEARRAY:
  773. {
  774. CodeWord c;
  775. int n = p-code;
  776. p++;
  777. get_word(c,p);
  778. printf ("%d CREATEARRAY %d\n", n, c.w);
  779. break;
  780. }
  781. case EQOP: printf ("%d EQOP\n", (p++)-code); break;
  782. case LTOP: printf ("%d LTOP\n", (p++)-code); break;
  783. case LEOP: printf ("%d LEOP\n", (p++)-code); break;
  784. case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
  785. case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
  786. case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
  787. case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
  788. case POWOP: printf ("%d POWOP\n", (p++)-code); break;
  789. case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
  790. case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
  791. case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
  792. case ONTJMP:
  793. {
  794. CodeWord c;
  795. int n = p-code;
  796. p++;
  797. get_word(c,p);
  798. printf ("%d ONTJMP %d\n", n, c.w);
  799. }
  800. break;
  801. case ONFJMP:
  802. {
  803. CodeWord c;
  804. int n = p-code;
  805. p++;
  806. get_word(c,p);
  807. printf ("%d ONFJMP %d\n", n, c.w);
  808. }
  809. break;
  810. case JMP:
  811. {
  812. CodeWord c;
  813. int n = p-code;
  814. p++;
  815. get_word(c,p);
  816. printf ("%d JMP %d\n", n, c.w);
  817. }
  818. break;
  819. case UPJMP:
  820. {
  821. CodeWord c;
  822. int n = p-code;
  823. p++;
  824. get_word(c,p);
  825. printf ("%d UPJMP %d\n", n, c.w);
  826. }
  827. break;
  828. case IFFJMP:
  829. {
  830. CodeWord c;
  831. int n = p-code;
  832. p++;
  833. get_word(c,p);
  834. printf ("%d IFFJMP %d\n", n, c.w);
  835. }
  836. break;
  837. case IFFUPJMP:
  838. {
  839. CodeWord c;
  840. int n = p-code;
  841. p++;
  842. get_word(c,p);
  843. printf ("%d IFFUPJMP %d\n", n, c.w);
  844. }
  845. break;
  846. case POP: printf ("%d POP\n", (p++)-code); break;
  847. case CALLFUNC:
  848. printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
  849. p+=3;
  850. break;
  851. case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
  852. case RETCODE:
  853. printf ("%d RETCODE %d\n", p-code, *(++p));
  854. p++;
  855. break;
  856. case SETFUNCTION:
  857. {
  858. CodeCode c1;
  859. CodeWord c2;
  860. int n = p-code;
  861. p++;
  862. get_code(c1,p);
  863. get_word(c2,p);
  864. printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.b, c2.w);
  865. }
  866. break;
  867. case SETLINE:
  868. {
  869. CodeWord c;
  870. int n = p-code;
  871. p++;
  872. get_word(c,p);
  873. printf ("%d SETLINE %d\n", n, c.w);
  874. }
  875. break;
  876. case RESET: printf ("%d RESET\n", (p++)-code); break;
  877. default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
  878. }
  879. }
  880. }
  881. #endif