fillem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. #ifndef NORCSID
  2. static char rcsid2[] = "$Id$";
  3. #endif
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "assert.h"
  8. #include <em_spec.h>
  9. #include <em_pseu.h>
  10. #include <em_flag.h>
  11. #include <em_ptyp.h>
  12. #include <em_mes.h>
  13. #include "mach.h"
  14. #include "param.h"
  15. #include "tables.h"
  16. #include "types.h"
  17. #include <cgg_cg.h>
  18. #include "data.h"
  19. #include "result.h"
  20. #ifdef REGVARS
  21. #include "regvar.h"
  22. #include <em_reg.h>
  23. #endif
  24. #include "extern.h"
  25. /*
  26. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  27. * See the copyright notice in the ACK home directory, in the file "Copyright".
  28. *
  29. * Author: Hans van Staveren
  30. */
  31. #ifndef newplb /* retrofit for older mach.h */
  32. #define newplb newilb
  33. #endif
  34. #ifdef fmt_id
  35. #ifdef id_first
  36. It is an error to define both fmt_id and id_first.
  37. Read the documentation.
  38. #endif
  39. #endif
  40. #ifdef fmt_ilb
  41. #ifdef ilb_fmt
  42. It is an error to define both fmt_ilb and ilb_fmt.
  43. Read the documentation.
  44. #endif
  45. #endif
  46. /* segment types for switchseg() */
  47. #define SEGTXT 0
  48. #define SEGCON 1
  49. #define SEGROM 2
  50. #define SEGBSS 3
  51. long con();
  52. #define get8() getc(emfile)
  53. FILE *emfile;
  54. extern FILE *codefile;
  55. extern FILE *freopen();
  56. int nextispseu,savetab1;
  57. int opcode;
  58. int offtyp;
  59. long argval;
  60. int dlbval;
  61. char *str,argstr[128],labstr[128];
  62. unsigned int maxstrsiz;
  63. int strsiz;
  64. int holno=0;
  65. int procno=0;
  66. int curseg= -1;
  67. int part_size=0;
  68. word part_word=0;
  69. #ifdef REGVARS
  70. int regallowed=0;
  71. #endif
  72. extern char em_flag[];
  73. extern short em_ptyp[];
  74. extern double atof();
  75. /* Own version of atol that continues computing on overflow.
  76. We don't know that about the ANSI C one.
  77. */
  78. long our_atol(s)
  79. register char *s;
  80. {
  81. register long total = 0;
  82. register unsigned digit;
  83. int minus = 0;
  84. while (*s == ' ' || *s == '\t') s++;
  85. if (*s == '+') s++;
  86. else if (*s == '-') {
  87. s++;
  88. minus = 1;
  89. }
  90. while ((digit = *s++ - '0') < 10) {
  91. total *= 10;
  92. total += digit;
  93. }
  94. return(minus ? -total : total);
  95. }
  96. #define sp_cstx sp_cst2
  97. string tostring();
  98. string holstr();
  99. string strarg();
  100. string mystrcpy();
  101. string myalloc();
  102. long get32();
  103. in_init(filename) char *filename; {
  104. emfile = stdin;
  105. if (filename && (emfile=freopen(filename,"r",stdin))==NULL)
  106. error("Can't open %s",filename);
  107. if (get16()!=sp_magic)
  108. error("Bad format %s",filename ? filename : "standard-input");
  109. str = myalloc(maxstrsiz=256);
  110. }
  111. in_start() {
  112. #ifdef modhead
  113. fprintf(codefile,"%s",modhead) ;
  114. #endif
  115. }
  116. in_finish() {
  117. }
  118. fillemlines() {
  119. register int t,i;
  120. register struct emline *lp;
  121. while ((emlines+nemlines)-emp<MAXEMLINES-5) {
  122. assert(nemlines<MAXEMLINES);
  123. if (nextispseu) {
  124. emlines[nemlines].em_instr=0;
  125. return;
  126. }
  127. lp = &emlines[nemlines++];
  128. switch(t=table1()) {
  129. default:
  130. error("unknown instruction byte");
  131. case sp_ilb1:
  132. case sp_ilb2:
  133. #ifdef USE_TES
  134. lp->em_instr = op_lab;
  135. lp->em_optyp = OPSYMBOL;
  136. lp->em_soper = strarg(t);
  137. lp->em_u.em_loper = argval;
  138. return;
  139. #endif
  140. case sp_fpseu:
  141. case sp_dlb1:
  142. case sp_dlb2:
  143. case sp_dnam:
  144. nextispseu=1; savetab1=t;
  145. nemlines--;
  146. lp->em_instr = 0;
  147. return;
  148. case EOF:
  149. nextispseu=1; savetab1=t;
  150. nemlines--;
  151. lp->em_instr = 0;
  152. return;
  153. case sp_fmnem:
  154. lp->em_instr = opcode;
  155. break;
  156. }
  157. i=em_flag[lp->em_instr-sp_fmnem] & EM_PAR;
  158. if ( i == PAR_NO ) {
  159. lp->em_optyp = OPNO;
  160. lp->em_soper = 0;
  161. continue;
  162. }
  163. t= em_ptyp[i];
  164. t= getarg(t);
  165. switch(i) {
  166. case PAR_L:
  167. assert(t == sp_cstx);
  168. if (argval >= 0)
  169. argval += TEM_BSIZE;
  170. lp->em_optyp = OPINT;
  171. lp->em_u.em_ioper = argval;
  172. lp->em_soper = tostring((word) argval);
  173. continue;
  174. case PAR_G:
  175. if (t != sp_cstx)
  176. break;
  177. lp->em_optyp = OPSYMBOL;
  178. lp->em_soper = holstr((word) argval);
  179. continue;
  180. case PAR_B:
  181. t = sp_ilb2;
  182. #ifdef USE_TES
  183. lp->em_optyp = OPSYMBOL;
  184. lp->em_u.em_loper = argval;
  185. lp->em_soper = strarg(t);
  186. #endif
  187. break;
  188. case PAR_D:
  189. assert(t == sp_cstx);
  190. lp->em_optyp = OPSYMBOL;
  191. lp->em_soper = strarg(t);
  192. lp->em_u.em_loper = argval;
  193. continue;
  194. }
  195. lp->em_soper = strarg(t);
  196. if (t==sp_cend)
  197. lp->em_optyp = OPNO;
  198. else if (t==sp_cstx) {
  199. lp->em_optyp = OPINT;
  200. lp->em_u.em_ioper = argval;
  201. } else
  202. lp->em_optyp = OPSYMBOL;
  203. }
  204. }
  205. dopseudo() {
  206. register b,t;
  207. register full n;
  208. register long save;
  209. word romcont[MAXROM+1];
  210. int nromwords;
  211. int rombit,rommask;
  212. unsigned stackupto();
  213. if (nextispseu==0 || nemlines>0)
  214. error("No table entry for %d",emlines[0].em_instr);
  215. nextispseu=0;
  216. switch(savetab1) {
  217. #ifndef USE_TES
  218. case sp_ilb1:
  219. case sp_ilb2:
  220. swtxt();
  221. /* dummy = */stackupto(&fakestack[stackheight-1],maxply,TRUE);
  222. cleanregs();
  223. strarg(savetab1);
  224. newilb(argstr);
  225. #ifndef NDEBUG
  226. { extern int Debug; extern char * strtdebug;
  227. if (strcmp(strtdebug,argstr)==0)
  228. Debug = strtdebug[-2]-'0';
  229. }
  230. #endif
  231. return;
  232. #endif
  233. case sp_dlb1:
  234. case sp_dlb2:
  235. case sp_dnam:
  236. strarg(savetab1);
  237. savelab();
  238. return;
  239. case sp_fpseu:
  240. break;
  241. case EOF:
  242. swtxt();
  243. in_finish();
  244. out_finish();
  245. popstr(0);
  246. tstoutput();
  247. exit(0);
  248. default:
  249. error("Unknown opcode %d",savetab1);
  250. }
  251. switch (opcode) {
  252. case ps_hol:
  253. sprintf(labstr,hol_fmt,++holno);
  254. case ps_bss:
  255. getarg(cst_ptyp);
  256. n = (full) argval;
  257. t = getarg(val_ptyp);
  258. save = argval;
  259. getarg(cst_ptyp);
  260. b = (int) argval;
  261. argval = save;
  262. bss(n,t,b);
  263. break;
  264. case ps_con:
  265. switchseg(SEGCON);
  266. dumplab();
  267. con(getarg(val_ptyp));
  268. while ((t = getarg(any_ptyp)) != sp_cend)
  269. con(t);
  270. break;
  271. case ps_rom:
  272. switchseg(SEGROM);
  273. xdumplab();
  274. nromwords=0;
  275. rommask=0;
  276. rombit=1;
  277. for (;;) {
  278. t=getarg(val_ptyp);
  279. while (t!=sp_cend) {
  280. if (t==sp_cstx && nromwords<MAXROM) {
  281. romcont[nromwords] = (word) argval;
  282. rommask |= rombit;
  283. }
  284. nromwords++;
  285. rombit <<= 1;
  286. con(t);
  287. t=getarg(any_ptyp);
  288. }
  289. {
  290. int c = get8();
  291. if (c == ps_rom) continue;
  292. if (c != EOF) ungetc(c, emfile);
  293. }
  294. break;
  295. }
  296. if (nromwords != 0) {
  297. romcont[MAXROM]=rommask;
  298. enterglo(labstr,romcont);
  299. }
  300. labstr[0]=0;
  301. break;
  302. case ps_mes:
  303. getarg(ptyp(sp_cst2));
  304. if (argval == ms_emx) {
  305. getarg(ptyp(sp_cst2));
  306. if (argval != TEM_WSIZE)
  307. fatal("bad word size");
  308. getarg(ptyp(sp_cst2));
  309. if (argval != TEM_PSIZE)
  310. fatal("bad pointer size");
  311. if ( getarg(any_ptyp)!=sp_cend )
  312. fatal("too many parameters");
  313. #ifdef USE_TES
  314. } else if (argval == ms_tes) {
  315. int lbl, size, flthr;
  316. getarg(ptyp(sp_cst2)); lbl = argval;
  317. getarg(ptyp(sp_cst2)); size = argval;
  318. getarg(ptyp(sp_cst2)); flthr = argval;
  319. if ( getarg(any_ptyp)!=sp_cend )
  320. fatal("too many parameters");
  321. add_label(lbl,size, flthr);
  322. #endif
  323. #ifdef REGVARS
  324. } else if (argval == ms_gto) {
  325. getarg(ptyp(sp_cend));
  326. if (!regallowed)
  327. error("mes 3 not allowed here");
  328. fixregvars(TRUE);
  329. regallowed=0;
  330. } else if (argval == ms_reg) {
  331. long r_off;
  332. int r_size,r_type,r_score;
  333. struct regvar *linkreg();
  334. if (!regallowed)
  335. error("mes 3 not allowed here");
  336. if(getarg(ptyp(sp_cst2)|ptyp(sp_cend)) == sp_cend) {
  337. fixregvars(FALSE);
  338. regallowed=0;
  339. } else {
  340. r_off = argval;
  341. if (r_off >= 0)
  342. r_off += TEM_BSIZE;
  343. getarg(ptyp(sp_cst2));
  344. r_size = argval;
  345. getarg(ptyp(sp_cst2));
  346. r_type = argval;
  347. if (r_type<reg_any || r_type>reg_float)
  348. fatal("Bad type in register message");
  349. if(getarg(ptyp(sp_cst2)|ptyp(sp_cend)) == sp_cend)
  350. r_score = 0;
  351. else {
  352. r_score = argval;
  353. if ( getarg(any_ptyp)!=sp_cend )
  354. fatal("too many parameters");
  355. }
  356. tryreg(linkreg(r_off,r_size,r_type,r_score),r_type);
  357. }
  358. #endif
  359. } else
  360. mes((word)argval);
  361. break;
  362. case ps_exa:
  363. strarg(getarg(sym_ptyp));
  364. ex_ap(argstr);
  365. break;
  366. case ps_ina:
  367. strarg(getarg(sym_ptyp));
  368. in_ap(argstr);
  369. break;
  370. case ps_exp:
  371. strarg(getarg(ptyp(sp_pnam)));
  372. ex_ap(argstr);
  373. break;
  374. case ps_inp:
  375. strarg(getarg(ptyp(sp_pnam)));
  376. in_ap(argstr);
  377. break;
  378. case ps_pro:
  379. switchseg(SEGTXT);
  380. procno++;
  381. strarg(getarg(ptyp(sp_pnam)));
  382. newplb(argstr);
  383. getarg(cst_ptyp);
  384. prolog((full)argval);
  385. #ifdef REGVARS
  386. regallowed++;
  387. #endif
  388. break;
  389. case ps_end:
  390. getarg(cst_ptyp | ptyp(sp_cend));
  391. #ifdef USE_TES
  392. kill_labels();
  393. #endif
  394. cleanregs();
  395. #ifdef REGVARS
  396. unlinkregs();
  397. #endif
  398. tstoutput();
  399. break;
  400. default:
  401. error("No table entry for %d",savetab1);
  402. }
  403. }
  404. /* ----- input ----- */
  405. int getarg(typset) {
  406. register t,argtyp;
  407. argtyp = t = table2();
  408. if (t == EOF)
  409. fatal("unexpected EOF");
  410. t -= sp_fspec;
  411. t = 1 << t;
  412. if ((typset & t) == 0)
  413. error("bad argument type %d",argtyp);
  414. return(argtyp);
  415. }
  416. int table1() {
  417. register i;
  418. i = get8();
  419. if (i < sp_fmnem+sp_nmnem && i >= sp_fmnem) {
  420. opcode = i;
  421. return(sp_fmnem);
  422. }
  423. if (i < sp_fpseu+sp_npseu && i >= sp_fpseu) {
  424. opcode = i;
  425. return(sp_fpseu);
  426. }
  427. if (i < sp_filb0+sp_nilb0 && i >= sp_filb0) {
  428. argval = i - sp_filb0;
  429. return(sp_ilb2);
  430. }
  431. return(table3(i));
  432. }
  433. int table2() {
  434. register i;
  435. i = get8();
  436. if (i < sp_fcst0+sp_ncst0 && i >= sp_fcst0) {
  437. argval = i - sp_zcst0;
  438. return(sp_cstx);
  439. }
  440. return(table3(i));
  441. }
  442. int table3(i) {
  443. word consiz;
  444. switch(i) {
  445. case sp_ilb1:
  446. argval = get8();
  447. break;
  448. case sp_dlb1:
  449. dlbval = get8();
  450. break;
  451. case sp_dlb2:
  452. dlbval = get16();
  453. break;
  454. case sp_cst2:
  455. i = sp_cstx;
  456. case sp_ilb2:
  457. argval = get16();
  458. break;
  459. case sp_cst4:
  460. i = sp_cstx;
  461. argval = get32();
  462. break;
  463. case sp_dnam:
  464. case sp_pnam:
  465. case sp_scon:
  466. getstring();
  467. break;
  468. case sp_doff:
  469. offtyp = getarg(sym_ptyp);
  470. getarg(cst_ptyp);
  471. break;
  472. case sp_icon:
  473. case sp_ucon:
  474. case sp_fcon:
  475. getarg(cst_ptyp);
  476. consiz = (word) argval;
  477. getstring();
  478. argval = consiz;
  479. break;
  480. }
  481. return(i);
  482. }
  483. int get16() {
  484. register int l_byte, h_byte;
  485. l_byte = get8();
  486. h_byte = get8();
  487. if ( h_byte>=128 ) h_byte -= 256 ;
  488. return l_byte | (h_byte*256) ;
  489. }
  490. long get32() {
  491. register long l;
  492. register int h_byte;
  493. l = get8();
  494. l |= ((unsigned) get8())*256 ;
  495. l |= get8()*256L*256L ;
  496. h_byte = get8() ;
  497. if ( h_byte>=128 ) h_byte -= 256 ;
  498. return l | (h_byte*256L*256*256L) ;
  499. }
  500. getstring() {
  501. register char *p;
  502. register n;
  503. getarg(cst_ptyp);
  504. if (argval < 0)
  505. fatal("string/identifier too long");
  506. if (argval >= maxstrsiz) {
  507. myfree(str);
  508. str = myalloc((unsigned) argval + 1);
  509. maxstrsiz = argval + 1;
  510. }
  511. strsiz = n = (int) argval;
  512. p = str;
  513. while (--n >= 0)
  514. *p++ = get8();
  515. *p++ = '\0';
  516. }
  517. char *strarg(t) {
  518. register char *p;
  519. switch (t) {
  520. case sp_ilb1:
  521. case sp_ilb2:
  522. #ifdef fmt_ilb
  523. fmt_ilb(procno,((int) argval),argstr);
  524. #else
  525. sprintf(argstr,ilb_fmt,procno,(int)argval);
  526. #endif
  527. break;
  528. case sp_dlb1:
  529. case sp_dlb2:
  530. sprintf(argstr,dlb_fmt,dlbval);
  531. break;
  532. case sp_cstx:
  533. sprintf(argstr,cst_fmt,(full)argval);
  534. break;
  535. case sp_dnam:
  536. case sp_pnam:
  537. #ifdef fmt_id
  538. fmt_id(str,argstr);
  539. #else
  540. p = argstr;
  541. if (strsiz < 8 || str[0] == id_first)
  542. *p++ = id_first;
  543. sprintf(p,"%.*s",strsiz,str);
  544. #endif
  545. break;
  546. case sp_doff:
  547. strarg(offtyp);
  548. for (p = argstr; *p; p++)
  549. ;
  550. if ((full) argval >= 0)
  551. *p++ = '+';
  552. else {
  553. *p++ = '-';
  554. argval = - (full) argval;
  555. }
  556. sprintf(p,off_fmt,(full)argval);
  557. break;
  558. case sp_cend:
  559. return("");
  560. }
  561. return(mystrcpy(argstr));
  562. }
  563. bss(n,t,b) full n; {
  564. register long s = 0;
  565. if (n % TEM_WSIZE)
  566. fatal("bad BSS size");
  567. if (b==0
  568. #ifdef BSS_INIT
  569. || (t==sp_cstx && argval==BSS_INIT)
  570. #endif /* BSS_INIT */
  571. ) {
  572. switchseg(SEGBSS);
  573. newlbss(labstr,n);
  574. labstr[0]=0;
  575. return;
  576. }
  577. switchseg(SEGCON);
  578. dumplab();
  579. while (n > 0)
  580. n -= (s = con(t));
  581. if (s % TEM_WSIZE)
  582. fatal("bad BSS initializer");
  583. }
  584. long con(t) {
  585. register i;
  586. strarg(t);
  587. switch (t) {
  588. case sp_ilb1:
  589. case sp_ilb2:
  590. case sp_pnam:
  591. part_flush();
  592. con_ilb(argstr);
  593. return((long)TEM_PSIZE);
  594. case sp_dlb1:
  595. case sp_dlb2:
  596. case sp_dnam:
  597. case sp_doff:
  598. part_flush();
  599. con_dlb(argstr);
  600. return((long)TEM_PSIZE);
  601. case sp_cstx:
  602. con_part(TEM_WSIZE,(word)argval);
  603. return((long)TEM_WSIZE);
  604. case sp_scon:
  605. for (i = 0; i < strsiz; i++)
  606. con_part(1,(word) str[i]);
  607. return((long)strsiz);
  608. case sp_icon:
  609. case sp_ucon:
  610. if (argval > TEM_WSIZE) {
  611. part_flush();
  612. con_mult((word)argval);
  613. } else {
  614. con_part((int)argval,(word)our_atol(str));
  615. }
  616. return(argval);
  617. case sp_fcon:
  618. part_flush();
  619. con_float();
  620. return(argval);
  621. }
  622. assert(FALSE);
  623. /* NOTREACHED */
  624. }
  625. extern char *segname[];
  626. swtxt() {
  627. switchseg(SEGTXT);
  628. }
  629. switchseg(s) {
  630. if (s == curseg)
  631. return;
  632. part_flush();
  633. if ((curseg = s) >= 0)
  634. fprintf(codefile,"%s\n",segname[s]);
  635. }
  636. savelab() {
  637. register char *p,*q;
  638. part_flush();
  639. if (labstr[0]) {
  640. dlbdlb(argstr,labstr);
  641. return;
  642. }
  643. p = argstr;
  644. q = labstr;
  645. while (*q++ = *p++)
  646. ;
  647. }
  648. dumplab() {
  649. if (labstr[0] == 0)
  650. return;
  651. assert(part_size == 0);
  652. newdlb(labstr);
  653. labstr[0] = 0;
  654. }
  655. xdumplab() {
  656. if (labstr[0] == 0)
  657. return;
  658. assert(part_size == 0);
  659. newdlb(labstr);
  660. }
  661. part_flush() {
  662. /*
  663. * Each new data fragment and each data label starts at
  664. * a new target machine word
  665. */
  666. if (part_size == 0)
  667. return;
  668. con_cst(part_word);
  669. part_size = 0;
  670. part_word = 0;
  671. }
  672. string holstr(n) word n; {
  673. sprintf(str,hol_off,n,holno);
  674. return(mystrcpy(str));
  675. }
  676. /* ----- machine dependent routines ----- */
  677. #include "mach.c"