lua.stx 21 KB

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