fillem.c 13 KB

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