reade.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. if (! isdigit(c)) {
  243. ungetbyte(c);
  244. syntax("digit expected");
  245. return sp_cst4;
  246. }
  247. n = sp_cst4;
  248. for (;;) {
  249. if (p >= &(str[256])) {
  250. syntax("number too long");
  251. return sp_cst4;
  252. }
  253. *p++ = c;
  254. if ((c = getbyte()) == '.' || c == 'e' || c == 'E') {
  255. expsign = c != '.';
  256. n = sp_fcon;
  257. continue;
  258. }
  259. if (expsign) {
  260. expsign = 0;
  261. if (c == '+' || c == '-') continue;
  262. }
  263. if (! isdigit(c)) break;
  264. }
  265. ungetbyte(c);
  266. *p = '\0';
  267. c = nospace();
  268. if (n == sp_fcon && c != 'F') {
  269. ungetbyte(c);
  270. syntax("'F' expected");
  271. return n;
  272. }
  273. if (c == 'I' || c == 'U' || c == 'F') {
  274. struct e_arg dummy;
  275. strcpy(string.str, str);
  276. ap->ema_string = string.str;
  277. gettyp(cst_ptyp, &dummy);
  278. ap->ema_szoroff = dummy.ema_cst;
  279. switch(c) {
  280. case 'I':
  281. ap->ema_argtype = ico_ptyp;
  282. return sp_icon;
  283. case 'U':
  284. ap->ema_argtype = uco_ptyp;
  285. return sp_ucon;
  286. case 'F':
  287. ap->ema_argtype = fco_ptyp;
  288. return sp_fcon;
  289. }
  290. assert(0);
  291. }
  292. ungetbyte(c);
  293. ap->ema_cst = (arith) str2long(str, 10);
  294. return sp_cst4;
  295. }
  296. PRIVATE int getexpr();
  297. PRIVATE int
  298. getfactor(c, ap)
  299. register int c;
  300. register struct e_arg *ap;
  301. {
  302. if (c == '(') {
  303. if (getexpr(nospace(), ap) != sp_cst4) {
  304. syntax("expression expected");
  305. }
  306. else if ((c = nospace()) != ')') {
  307. ungetbyte(c);
  308. syntax("')' expected");
  309. }
  310. return sp_cst4;
  311. }
  312. return getnumber(c, ap);
  313. }
  314. PRIVATE int
  315. getterm(c, ap)
  316. register int c;
  317. register struct e_arg *ap;
  318. {
  319. arith left;
  320. if ((c = getfactor(c, ap)) != sp_cst4) return c;
  321. for (;;) {
  322. if ((c = nospace()) != '*' && c != '/' && c != '%') {
  323. ungetbyte(c);
  324. break;
  325. }
  326. left = ap->ema_cst;
  327. if (getfactor(nospace(), ap) != sp_cst4) {
  328. syntax("factor expected");
  329. break;
  330. }
  331. if (c == '*') ap->ema_cst *= left;
  332. else if (c == '/') ap->ema_cst = left / ap->ema_cst;
  333. else ap->ema_cst = left % ap->ema_cst;
  334. }
  335. return sp_cst4;
  336. }
  337. PRIVATE int
  338. getexpr(c, ap)
  339. register int c;
  340. register struct e_arg *ap;
  341. {
  342. arith left;
  343. if ((c = getterm(c, ap)) != sp_cst4) return c;
  344. for (;;) {
  345. if ((c = nospace()) != '+' && c != '-') {
  346. ungetbyte(c);
  347. break;
  348. }
  349. left = ap->ema_cst;
  350. if (getterm(nospace(), ap) != sp_cst4) {
  351. syntax("term expected");
  352. break;
  353. }
  354. if (c == '+') ap->ema_cst += left;
  355. else ap->ema_cst = left - ap->ema_cst;
  356. }
  357. return sp_cst4;
  358. }
  359. PRIVATE int
  360. get15u()
  361. {
  362. struct e_arg dummy;
  363. if (getnumber(getbyte(), &dummy) != sp_cst4) {
  364. syntax("integer expected");
  365. }
  366. else check((dummy.ema_cst & ~077777) == 0);
  367. return (int) (dummy.ema_cst);
  368. }
  369. PRIVATE void
  370. gettyp(typset, ap)
  371. register struct e_arg *ap;
  372. {
  373. register int c, t;
  374. register int argtyp;
  375. if ((c = nospace()) == '\n') {
  376. ungetbyte(c);
  377. out("newline\n");
  378. argtyp = sp_cend;
  379. }
  380. else if (isdigit(c) || c == '+' || c == '-' || c == '(') {
  381. out("expr\n");
  382. argtyp = getexpr(c, ap);
  383. if (argtyp == sp_cst4 && fit16i(ap->ema_cst)) argtyp = sp_cst2;
  384. }
  385. else if (isalpha(c) || c == '_') {
  386. out("name\n");
  387. ungetbyte(c);
  388. ap->ema_dnam = getname()->str;
  389. ap->ema_argtype = sof_ptyp;
  390. argtyp = offsetted(sp_dnam, &(ap->ema_szoroff));
  391. }
  392. else if (c == '.') {
  393. out(".label\n");
  394. ap->ema_dlb = get15u();
  395. ap->ema_argtype = nof_ptyp;
  396. argtyp = offsetted(sp_dlb2, &(ap->ema_szoroff));
  397. }
  398. else if (c == '*') {
  399. out("*label\n");
  400. ap->ema_ilb = get15u();
  401. ap->ema_argtype = ilb_ptyp;
  402. argtyp = sp_ilb2;
  403. }
  404. else if (c == '$') {
  405. out("$name\n");
  406. ap->ema_pnam = getname()->str;
  407. ap->ema_argtype = pro_ptyp;
  408. argtyp = sp_pnam;
  409. }
  410. else if (c == '"' || c == '\'') {
  411. register struct string *s;
  412. out("string\n");
  413. ungetbyte(c);
  414. s = getstring(0);
  415. ap->ema_string = s->str;
  416. ap->ema_szoroff = s->length;
  417. ap->ema_argtype = str_ptyp;
  418. argtyp = sp_scon;
  419. }
  420. else if (c == '?') {
  421. out("?\n");
  422. argtyp = sp_cend;
  423. ap->ema_argtype = 0;
  424. }
  425. else {
  426. /* c != '\n', so "ungetbyte" not neccesary */
  427. syntax("operand expected");
  428. return;
  429. }
  430. t = argtyp - sp_fspec;
  431. assert(t >= 0 && t < 16);
  432. if ((typset & (1 << t)) == 0) {
  433. syntax("Bad argument type");
  434. return;
  435. }
  436. if (argtyp == sp_cend) {
  437. ap->ema_argtype = 0;
  438. }
  439. }
  440. PRIVATE void
  441. getarg(typset, ap)
  442. struct e_arg *ap;
  443. {
  444. register int c;
  445. if (argnum != 1) {
  446. if ((c = nospace()) != ',') {
  447. if (c != '\n') {
  448. syntax("comma expected");
  449. return;
  450. }
  451. ungetbyte(c);
  452. }
  453. }
  454. argnum++;
  455. gettyp(typset, ap);
  456. }
  457. /* getmnem: We found the start of either an instruction or a pseudo.
  458. get the rest of it
  459. */
  460. PRIVATE void
  461. getmnem(c, p)
  462. register struct e_instr *p;
  463. {
  464. register int h;
  465. int i;
  466. register struct string *s;
  467. ungetbyte(c);
  468. s = getname();
  469. h = hash(s->str);
  470. for (;;) {
  471. h++;
  472. if (h >= HSIZE) h %= HSIZE;
  473. if ((i = hashtab[h]) == 0) {
  474. syntax("bad mnemonic");
  475. return;
  476. }
  477. else if (i <= sp_lmnem) {
  478. assert(i >= sp_fmnem);
  479. if (strcmp(s->str, em_mnem[i - sp_fmnem]) != 0) {
  480. continue;
  481. }
  482. p->em_type = EM_MNEM;
  483. p->em_opcode = i;
  484. break;
  485. }
  486. assert(i <= sp_lpseu && i >= sp_fpseu);
  487. if (strcmp(s->str, em_pseu[i - sp_fpseu]) != 0) {
  488. continue;
  489. }
  490. if (i == ps_mes) {
  491. p->em_type = EM_STARTMES;
  492. break;
  493. }
  494. p->em_opcode = i;
  495. p->em_type = EM_PSEU;
  496. break;
  497. }
  498. }
  499. PRIVATE void
  500. line_line()
  501. {
  502. static char filebuf[256 + 1];
  503. char *btscpy();
  504. struct e_arg dummy;
  505. gettyp(ptyp(sp_cst2), &dummy);
  506. EM_lineno = dummy.ema_cst;
  507. gettyp(str_ptyp, &dummy);
  508. btscpy(filebuf, dummy.ema_string, (int) dummy.ema_szoroff);
  509. EM_filename = filebuf;
  510. }
  511. PRIVATE void
  512. getlabel(c, p)
  513. register struct e_instr *p;
  514. {
  515. ungetbyte(c);
  516. gettyp(lab_ptyp|ptyp(sp_cst2), &(p->em_arg));
  517. switch(p->em_argtype) {
  518. case cst_ptyp:
  519. p->em_type = EM_DEFILB;
  520. p->em_argtype = ilb_ptyp;
  521. p->em_ilb = p->em_cst;
  522. break;
  523. case sof_ptyp:
  524. p->em_type = EM_DEFDNAM;
  525. break;
  526. case nof_ptyp:
  527. p->em_type = EM_DEFDLB;
  528. break;
  529. }
  530. checkeol();
  531. }
  532. PRIVATE void
  533. gethead(p)
  534. register struct e_instr *p;
  535. {
  536. register int c;
  537. argnum = 1;
  538. for (;;) {
  539. EM_lineno++;
  540. c = getbyte();
  541. if (c == COMMENTSTARTER) {
  542. do c = getbyte();
  543. while (c != '\n' && c != EOF);
  544. }
  545. if (c == EOF) {
  546. p->em_type = EM_EOF;
  547. return;
  548. }
  549. if (c == '\n') continue;
  550. if (isspace(c)) {
  551. c = nospace();
  552. if (isalpha(c) || c == '_') {
  553. getmnem(c, p);
  554. return;
  555. }
  556. ungetbyte(c);
  557. }
  558. else if (c == '#') line_line();
  559. else {
  560. getlabel(c, p);
  561. return;
  562. }
  563. checkeol();
  564. if (p->em_type == EM_ERROR || p->em_type == EM_FATAL) return;
  565. }
  566. /*NOTREACHED*/
  567. }