reade.c 11 KB

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