fillem.c 12 KB

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