reade.c 11 KB

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