reade.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* This file is ment to be included in the file read_emeV.c.
  7. It contains the part that takes care of the reading of human readable
  8. EM-code.
  9. */
  10. #include <ctype.h>
  11. /* #define XXX_YYY /* only for early debugging */
  12. #ifdef XXX_YYY
  13. #define out(str) ((void) sys_write(STDOUT, str, strlen(str)))
  14. #else
  15. #define out(s)
  16. #endif
  17. #define fit16i(x) ((x) >= -32768L && (x) <= 32767L)
  18. #define HSIZE 256 /* Size of hashtable for mnemonics */
  19. static int hashtab[HSIZE]; /* The hashtable for mnemonics */
  20. static int argnum; /* Number of arguments */
  21. #define COMMENTSTARTER ';'
  22. /* inithash, pre_hash, hash: Simple hashtable mechanism
  23. */
  24. PRIVATE int
  25. hash(s)
  26. register char *s;
  27. {
  28. register int h = 0;
  29. while (*s) {
  30. h <<= 1;
  31. h += *s++;
  32. }
  33. return h;
  34. }
  35. PRIVATE
  36. pre_hash(i, s)
  37. char *s;
  38. {
  39. register int h;
  40. assert(i != 0);
  41. h = hash(s);
  42. for (;;) {
  43. h++;
  44. if (h >= HSIZE) h %= HSIZE;
  45. if (hashtab[h] == 0) {
  46. hashtab[h] = i;
  47. return;
  48. }
  49. }
  50. /*NOTREACHED*/
  51. }
  52. extern char em_mnem[][4];
  53. extern char em_pseu[][4];
  54. PRIVATE
  55. inithash()
  56. {
  57. register int i;
  58. /* Enter instructions ... */
  59. for (i = sp_fmnem; i <= sp_lmnem; i++) {
  60. pre_hash(i, em_mnem[i - sp_fmnem]);
  61. }
  62. /* and pseudos ... */
  63. for (i = sp_fpseu; i <= sp_lpseu; i++) {
  64. pre_hash(i, em_pseu[i - sp_fpseu]);
  65. }
  66. }
  67. /* nospace: skip until we find a non-space character. Also skip
  68. comments.
  69. */
  70. PRIVATE int
  71. nospace()
  72. {
  73. register int c;
  74. do c = getbyte();
  75. while (isspace(c) && c != '\n');
  76. if (c == COMMENTSTARTER) {
  77. do c = getbyte();
  78. while (c != '\n' && c != EOF);
  79. }
  80. return c;
  81. }
  82. /* syntax: Put an error message in EM_error and skip to the end of the line
  83. */
  84. PRIVATE
  85. syntax(s)
  86. char *s;
  87. {
  88. register int c;
  89. xerror(s);
  90. state = 0;
  91. while ((c = getbyte()) != '\n' && c != EOF) /* nothing */ ;
  92. ungetbyte(c);
  93. }
  94. /* checkeol: check that we have a complete line (except maybe for spaces)
  95. */
  96. PRIVATE
  97. checkeol()
  98. {
  99. if (nospace() != '\n') {
  100. syntax("end of line expected");
  101. (void) nospace();
  102. }
  103. }
  104. /* getescape: read a '\' escape sequence
  105. */
  106. PRIVATE int
  107. getescape()
  108. {
  109. register int c, j, r;
  110. if ((c = getbyte()) >= '0' && c <= '7') { /* numeric escape */
  111. r = c - '0';
  112. for (j = 0; j < 2; j++) {
  113. if ((c = getbyte()) < '0' || c > '7') {
  114. ungetbyte(c);
  115. return r;
  116. }
  117. r <<= 3;
  118. r += c - '0';
  119. }
  120. return r;
  121. }
  122. switch(c) {
  123. case 'b': return '\b';
  124. case 'f': return '\f';
  125. case 'n': return '\n';
  126. case 'r': return '\r';
  127. case 't': return '\t';
  128. }
  129. return c;
  130. }
  131. /* getname: Read a string of characters representing an identifier
  132. */
  133. PRIVATE struct string *
  134. getname()
  135. {
  136. register char *p;
  137. register struct string *s;
  138. register int c;
  139. s = stringentry();
  140. p = s->str;
  141. c = getbyte();
  142. if (!(isalpha(c) || c == '_')) {
  143. ungetbyte(c);
  144. syntax("Letter expected");
  145. return s;
  146. }
  147. while (isalnum(c) || c == '_') {
  148. if (p < &(s->str[STRSIZ])) *p++ = c;
  149. c = getbyte();
  150. }
  151. ungetbyte(c);
  152. *p = '\0';
  153. s->length = p - s->str;
  154. return s;
  155. }
  156. /* getstring: read a string of characters between quotes
  157. */
  158. PRIVATE struct string *
  159. getstring()
  160. {
  161. register char *p;
  162. struct string *s;
  163. register int c;
  164. static int termc;
  165. s = stringentry();
  166. p = s->str;
  167. if (!(state & INSTRING)) { /* Not reading a string yet */
  168. termc = getbyte();
  169. /* assert(termc == '"' || termc == '\''); */
  170. /* This assertion does not work. Compiler error messages.
  171. The trouble lies in the ", it terminates the string
  172. created in the assertion macro
  173. */
  174. }
  175. for (;;) {
  176. if ((c = getbyte()) == '\n' || c == EOF) {
  177. ungetbyte(c);
  178. syntax("non-terminated string");
  179. break;
  180. }
  181. if (c == termc) {
  182. if (termc == '"') *p++ = '\0';
  183. state &= ~INSTRING;
  184. break;
  185. }
  186. if (c == '\\') c = getescape();
  187. if (p >= &(s->str[STRSIZ])) {
  188. state |= INSTRING;
  189. ungetbyte(c);
  190. break;
  191. }
  192. *p++ = c;
  193. }
  194. *p = '\0';
  195. s->length = p - s->str;
  196. return s;
  197. }
  198. PRIVATE struct e_args *gettyp();
  199. PRIVATE int
  200. offsetted(argtyp, ap)
  201. arith *ap;
  202. {
  203. register int c;
  204. register struct e_args *ap1;
  205. if ((c = nospace()) == '+' || c == '-') {
  206. ap1 = gettyp(cst_ptyp);
  207. if (c == '-') *ap = -(ap1->em_cst);
  208. else *ap = ap1->em_cst;
  209. return sp_doff;
  210. }
  211. else *ap = 0;
  212. ungetbyte(c);
  213. return argtyp;
  214. }
  215. PRIVATE int
  216. getnumber(c, ap)
  217. register int c;
  218. register struct e_args *ap;
  219. {
  220. register char *p;
  221. int n;
  222. register struct string *s = stringentry();
  223. int expsign;
  224. long str2long();
  225. p = s->str;
  226. ap->em_argtype = cst_ptyp;
  227. expsign = 0;
  228. if (c == '+' || c == '-') {
  229. if (c == '-') *p++ = c;
  230. c = getbyte();
  231. }
  232. if (! isdigit(c)) {
  233. ungetbyte(c);
  234. syntax("digit expected");
  235. i_strings--;
  236. return sp_cst4;
  237. }
  238. n = sp_cst4;
  239. for (;;) {
  240. if (p >= &(s->str[STRSIZ])) {
  241. syntax("number too long");
  242. i_strings--;
  243. return sp_cst4;
  244. }
  245. *p++ = c;
  246. if ((c = getbyte()) == '.' || c == 'e' || c == 'E') {
  247. expsign = c != '.';
  248. n = sp_fcon;
  249. continue;
  250. }
  251. if (expsign) {
  252. expsign = 0;
  253. if (c == '+' || c == '-') continue;
  254. }
  255. if (! isdigit(c)) break;
  256. }
  257. ungetbyte(c);
  258. *p = '\0';
  259. c = nospace();
  260. if (n == sp_fcon && c != 'F') {
  261. ungetbyte(c);
  262. syntax("'F' expected");
  263. return n;
  264. }
  265. if (c == 'I' || c == 'U' || c == 'F') {
  266. ap->em_str = s->str;
  267. ap->em_size = gettyp(cst_ptyp)->em_cst;
  268. switch(c) {
  269. case 'I':
  270. ap->em_argtype = ico_ptyp;
  271. return sp_icon;
  272. case 'U':
  273. ap->em_argtype = uco_ptyp;
  274. return sp_ucon;
  275. case 'F':
  276. ap->em_argtype = fco_ptyp;
  277. return sp_fcon;
  278. }
  279. assert(0);
  280. }
  281. ungetbyte(c);
  282. ap->em_cst = (arith) str2long(s->str, 10);
  283. i_strings--;
  284. return sp_cst4;
  285. }
  286. PRIVATE int getexpr();
  287. PRIVATE int
  288. getfactor(c, ap)
  289. register int c;
  290. register struct e_args *ap;
  291. {
  292. if (c == '(') {
  293. if (getexpr(nospace(), ap) != sp_cst4) {
  294. syntax("expression expected");
  295. }
  296. else if ((c = nospace()) != ')') {
  297. ungetbyte(c);
  298. syntax("')' expected");
  299. }
  300. return sp_cst4;
  301. }
  302. return getnumber(c, ap);
  303. }
  304. PRIVATE int
  305. getterm(c, ap)
  306. register int c;
  307. register struct e_args *ap;
  308. {
  309. arith left;
  310. if ((c = getfactor(c, ap)) != sp_cst4) return c;
  311. for (;;) {
  312. if ((c = nospace()) != '*' && c != '/' && c != '%') {
  313. ungetbyte(c);
  314. break;
  315. }
  316. left = ap->em_cst;
  317. if (getfactor(nospace(), ap) != sp_cst4) {
  318. syntax("factor expected");
  319. break;
  320. }
  321. if (c == '*') ap->em_cst *= left;
  322. else if (c == '/') ap->em_cst = left / ap->em_cst;
  323. else ap->em_cst = left % ap->em_cst;
  324. }
  325. return sp_cst4;
  326. }
  327. PRIVATE int
  328. getexpr(c, ap)
  329. register int c;
  330. register struct e_args *ap;
  331. {
  332. arith left;
  333. if ((c = getterm(c, ap)) != sp_cst4) return c;
  334. for (;;) {
  335. if ((c = nospace()) != '+' && c != '-') {
  336. ungetbyte(c);
  337. break;
  338. }
  339. left = ap->em_cst;
  340. if (getterm(nospace(), ap) != sp_cst4) {
  341. syntax("term expected");
  342. break;
  343. }
  344. if (c == '+') ap->em_cst += left;
  345. else ap->em_cst = left - ap->em_cst;
  346. }
  347. return sp_cst4;
  348. }
  349. PRIVATE int
  350. get15u()
  351. {
  352. register struct e_args *ap = argentry();
  353. ap->em_next = 0;
  354. if (getnumber(getbyte(), ap) != sp_cst4) {
  355. syntax("integer expected");
  356. }
  357. else check((ap->em_cst & ~077777) == 0);
  358. i_emargs--;
  359. return (int) (ap->em_cst);
  360. }
  361. PRIVATE struct e_args *
  362. gettyp(typset)
  363. {
  364. register int c, t;
  365. register struct e_args *ap = argentry();
  366. register int argtyp;
  367. ap->em_next = 0;
  368. if ((c = nospace()) == '\n') {
  369. ungetbyte(c);
  370. out("newline\n");
  371. argtyp = sp_cend;
  372. }
  373. else if (isdigit(c) || c == '+' || c == '-' || c == '(') {
  374. out("expr\n");
  375. argtyp = getexpr(c, ap);
  376. if (argtyp == sp_cst4 && fit16i(ap->em_cst)) argtyp = sp_cst2;
  377. }
  378. else if (isalpha(c) || c == '_') {
  379. out("name\n");
  380. ungetbyte(c);
  381. ap->em_dnam = getname()->str;
  382. ap->em_argtype = sof_ptyp;
  383. argtyp = offsetted(sp_dnam, &(ap->em_soff));
  384. }
  385. else if (c == '.') {
  386. out(".label\n");
  387. ap->em_dlb = get15u();
  388. ap->em_argtype = nof_ptyp;
  389. argtyp = offsetted(sp_dlb2, &(ap->em_noff));
  390. }
  391. else if (c == '*') {
  392. out("*label\n");
  393. ap->em_ilb = get15u();
  394. ap->em_argtype = ilb_ptyp;
  395. argtyp = sp_ilb2;
  396. }
  397. else if (c == '$') {
  398. out("$name\n");
  399. ap->em_pnam = getname()->str;
  400. ap->em_argtype = pro_ptyp;
  401. argtyp = sp_pnam;
  402. }
  403. else if (c == '"' || c == '\'') {
  404. register struct string *s;
  405. out("string\n");
  406. ungetbyte(c);
  407. s = getstring(0);
  408. ap->em_str = s->str;
  409. ap->em_size = s->length;
  410. ap->em_argtype = str_ptyp;
  411. argtyp = sp_scon;
  412. }
  413. else if (c == '?') {
  414. out("?\n");
  415. argtyp = sp_cend;
  416. }
  417. else {
  418. /* c != '\n', so "ungetbyte" not neccesary */
  419. syntax("operand expected");
  420. return ap;
  421. }
  422. t = argtyp - sp_fspec;
  423. assert(t >= 0 && t < 16);
  424. if ((typset & (1 << t)) == 0) {
  425. syntax("Bad argument type");
  426. return ap;
  427. }
  428. if (argtyp == sp_cend) return 0;
  429. return ap;
  430. }
  431. PRIVATE struct e_args *
  432. getarg(typset)
  433. {
  434. register int c;
  435. if (argnum != 1) {
  436. if ((c = nospace()) != ',') {
  437. if (c != '\n') {
  438. syntax("comma expected");
  439. return 0;
  440. }
  441. ungetbyte(c);
  442. }
  443. }
  444. argnum++;
  445. return gettyp(typset);
  446. }
  447. /* getmnem: We found the start of either an instruction or a pseudo.
  448. get the rest of it
  449. */
  450. PRIVATE
  451. getmnem(c, p)
  452. register struct e_instr *p;
  453. {
  454. register int h;
  455. int i;
  456. register struct string *s;
  457. ungetbyte(c);
  458. s = getname();
  459. h = hash(s->str);
  460. for (;;) {
  461. h++;
  462. if (h >= HSIZE) h %= HSIZE;
  463. if ((i = hashtab[h]) == 0) {
  464. syntax("bad mnemonic");
  465. return;
  466. }
  467. else if (i <= sp_lmnem) {
  468. assert(i >= sp_fmnem);
  469. if (strcmp(s->str, em_mnem[i - sp_fmnem]) != 0) {
  470. continue;
  471. }
  472. p->em_type = EM_MNEM;
  473. p->em_opcode = i;
  474. break;
  475. }
  476. assert(i <= sp_lpseu && i >= sp_fpseu);
  477. if (strcmp(s->str, em_pseu[i - sp_fpseu]) != 0) {
  478. continue;
  479. }
  480. if (i == ps_mes) {
  481. p->em_type = EM_STARTMES;
  482. break;
  483. }
  484. p->em_opcode = i;
  485. p->em_type = EM_PSEU;
  486. break;
  487. }
  488. i_strings--;
  489. }
  490. PRIVATE
  491. line_line()
  492. {
  493. register struct e_args *ap;
  494. static char filebuf[STRSIZ + 1];
  495. char *btscpy();
  496. ap = gettyp(ptyp(sp_cst2));
  497. EM_lineno = ap->em_cst;
  498. i_emargs--;
  499. ap = gettyp(str_ptyp);
  500. btscpy(filebuf, ap->em_str, (int) ap->em_size);
  501. i_emargs--;
  502. i_strings--;
  503. EM_filename = filebuf;
  504. }
  505. PRIVATE
  506. getlabel(c, p)
  507. register struct e_instr *p;
  508. {
  509. register struct e_args *ap;
  510. ungetbyte(c);
  511. ap = gettyp(lab_ptyp|ptyp(sp_cst2));
  512. switch(ap->em_argtype) {
  513. case cst_ptyp:
  514. p->em_type = EM_DEFILB;
  515. p->em_deflb = ap->em_cst;
  516. break;
  517. case sof_ptyp:
  518. p->em_type = EM_DEFDNAM;
  519. p->em_defdnam = ap->em_dnam;
  520. break;
  521. case nof_ptyp:
  522. p->em_type = EM_DEFDLB;
  523. p->em_deflb = ap->em_dlb;
  524. break;
  525. }
  526. checkeol();
  527. }
  528. PRIVATE struct e_instr *
  529. gethead()
  530. {
  531. register int c, i;
  532. register struct e_instr *p = &emhead;
  533. argnum = 1;
  534. for (;;) {
  535. EM_lineno++;
  536. c = getbyte();
  537. if (c == COMMENTSTARTER) {
  538. do c = getbyte();
  539. while (c != '\n' && c != EOF);
  540. }
  541. if (c == EOF) return 0;
  542. if (c == '\n') continue;
  543. if (isspace(c)) {
  544. c = nospace();
  545. if (isalpha(c) || c == '_') {
  546. getmnem(c, p);
  547. return p;
  548. }
  549. ungetbyte(c);
  550. }
  551. else if (c == '#') line_line();
  552. else {
  553. getlabel(c, p);
  554. return p;
  555. }
  556. checkeol();
  557. }
  558. /*NOTREACHED*/
  559. }