readk.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 must be included in the file "read_emk.c".
  7. It takes care of the reading of compact EM code.
  8. */
  9. #include <ctype.h>
  10. /* get16, get32: read a signed constant
  11. */
  12. PRIVATE int
  13. get16()
  14. {
  15. register int l_byte, h_byte;
  16. l_byte = getbyte();
  17. h_byte = getbyte();
  18. if (h_byte >= 128) h_byte -= 256;
  19. return l_byte | (h_byte << 8);
  20. }
  21. PRIVATE arith
  22. get32()
  23. {
  24. register arith l;
  25. register int h_byte;
  26. l = getbyte();
  27. l |= ((unsigned) getbyte() << 8);
  28. l |= ((arith) getbyte() << 16);
  29. h_byte = getbyte();
  30. if (h_byte >= 128) h_byte -= 256;
  31. return l | ((arith) h_byte << 24);
  32. }
  33. PRIVATE struct string *getstring();
  34. /* getarg : read an argument of any type, and check it against "typset"
  35. if neccesary. Put result in "ap".
  36. */
  37. PRIVATE
  38. getarg(typset, ap)
  39. register struct e_arg *ap;
  40. {
  41. register int i = getbyte();
  42. #ifdef CHECKING
  43. int argtyp;
  44. #endif /* CHECKING */
  45. ap->ema_argtype = 0;
  46. switch(i) {
  47. default:
  48. if (i < sp_fcst0+sp_ncst0 && i >= sp_fcst0) { /* A cst */
  49. ap->ema_cst = i - sp_zcst0;
  50. ap->ema_argtype = cst_ptyp;
  51. i = sp_cst2;
  52. }
  53. break;
  54. case sp_dlb1: /* Numeric data label encoded in one byte */
  55. ap->ema_dlb = getbyte();
  56. ap->ema_szoroff = 0;
  57. ap->ema_argtype = nof_ptyp;
  58. break;
  59. case sp_dlb2: /* Numeric data label encoded in two bytes */
  60. ap->ema_dlb = get16();
  61. ap->ema_szoroff = 0;
  62. ap->ema_argtype = nof_ptyp;
  63. #ifdef CHECKING
  64. if (ap->ema_dlb > 32767 && !EM_error) {
  65. EM_error = "Illegal data label";
  66. break;
  67. }
  68. #endif /* CHECKING */
  69. break;
  70. case sp_ilb1: /* Instruction label encoded in one byte */
  71. ap->ema_ilb = getbyte();
  72. ap->ema_argtype = ilb_ptyp;
  73. break;
  74. case sp_ilb2: /* Instruction label encoded in two bytes */
  75. ap->ema_ilb = get16();
  76. ap->ema_argtype = ilb_ptyp;
  77. #ifdef CHECKING
  78. if (ap->ema_ilb > 32767 && !EM_error) {
  79. EM_error = "Illegal instruction label";
  80. break;
  81. }
  82. #endif /* CHECKING */
  83. break;
  84. case sp_cst2: /* A cst encoded in two bytes */
  85. ap->ema_cst = get16();
  86. ap->ema_argtype = cst_ptyp;
  87. break;
  88. case sp_cst4: /* A cst encoded in four bytes */
  89. ap->ema_cst = get32();
  90. ap->ema_argtype = cst_ptyp;
  91. break;
  92. case sp_pnam: /* A procedure name */
  93. {
  94. register struct string *p;
  95. p = getstring(1);
  96. ap->ema_pnam = p->str;
  97. ap->ema_argtype = pro_ptyp;
  98. break;
  99. }
  100. case sp_dnam: /* A Non-numeric data label */
  101. {
  102. register struct string *p;
  103. p = getstring(1);
  104. ap->ema_dnam = p->str;
  105. ap->ema_szoroff = 0;
  106. ap->ema_argtype = sof_ptyp;
  107. break;
  108. }
  109. case sp_doff: /* A data label + offset */
  110. {
  111. struct e_arg dummy;
  112. getarg(lab_ptyp, ap);
  113. getarg(cst_ptyp, &dummy);
  114. ap->ema_szoroff = dummy.ema_cst;
  115. break;
  116. }
  117. case sp_icon: /* An integer constant */
  118. case sp_ucon: /* An unsigned constant */
  119. case sp_fcon: /* A floating constant */
  120. {
  121. register struct string *p;
  122. getarg(cst_ptyp, ap);
  123. ap->ema_szoroff = ap->ema_cst;
  124. p = getstring(0);
  125. ap->ema_argtype = ptyp(i);
  126. ap->ema_string = p->str;
  127. break;
  128. }
  129. case sp_scon: /* A string constant */
  130. {
  131. register struct string *p;
  132. p = getstring(0);
  133. ap->ema_argtype = str_ptyp;
  134. ap->ema_string = p->str;
  135. ap->ema_szoroff = p->length;
  136. break;
  137. }
  138. }
  139. #ifdef CHECKING
  140. argtyp = i;
  141. if (EM_error) return;
  142. if (i == EOF) {
  143. xfatal("Unexpected EOF");
  144. return;
  145. }
  146. if ((i -= sp_fspec) < 0 || i >= 16) {
  147. xerror("Illegal byte");
  148. return;
  149. }
  150. if ((typset & (1 << i)) == 0 && !EM_error) {
  151. EM_error = "Bad argument type";
  152. }
  153. if (argtyp == sp_cend) {
  154. ap->ema_argtype = 0;
  155. }
  156. #else /* not CHECKING */
  157. if (i == sp_cend) {
  158. ap->ema_argtype = 0;
  159. }
  160. #endif /* CHECKING */
  161. }
  162. #ifdef CHECKING
  163. /* checkident: check that a string indeed represents an identifier
  164. */
  165. PRIVATE int
  166. checkident(s)
  167. register struct string *s;
  168. {
  169. register char *p;
  170. register int n;
  171. p = s->str;
  172. if (!isascii(*p) || (!isalpha(*p) && *p != '_')) {
  173. return 0;
  174. }
  175. p++;
  176. for (n = s->length; --n > 0; p++) {
  177. if (!isascii(*p) || (!isalnum(*p) && *p != '_')) {
  178. return 0;
  179. }
  180. }
  181. return 1;
  182. }
  183. #endif /* CHECKING */
  184. /* getstring: read a string from the input
  185. */
  186. /*ARGSUSED*/
  187. PRIVATE struct string *
  188. getstring(isident)
  189. {
  190. register char *p;
  191. register int n;
  192. register struct string *s = &string;
  193. struct e_arg dummy;
  194. getarg(cst_ptyp, &dummy);
  195. /* Read length of string */
  196. n = dummy.ema_cst;
  197. #ifdef CHECKING
  198. if (n < 0) {
  199. xerror("Negative length in string");
  200. s->length = 0;
  201. return s;
  202. }
  203. #endif /* CHECKING */
  204. if (n > s->maxlen) {
  205. if (! s->maxlen) {
  206. s->str = Malloc(s->maxlen = 256);
  207. }
  208. else s->str = Realloc(s->str, (s->maxlen = (n+255)&~255));
  209. }
  210. s->length = n;
  211. p = s->str;
  212. while (--n >= 0) {
  213. *p++ = getbyte();
  214. }
  215. *p++ = '\0';
  216. #ifdef CHECKING
  217. if (isident) {
  218. if (!checkident(s) && !EM_error) {
  219. EM_error = "Illegal identifier";
  220. }
  221. }
  222. #endif /* CHECKING */
  223. return s;
  224. }
  225. /* gethead: read the start of an EM-line
  226. */
  227. PRIVATE
  228. gethead(p)
  229. register struct e_instr *p;
  230. {
  231. register int i;
  232. EM_lineno++;
  233. if ((i = getbyte()) < sp_fmnem+sp_nmnem && i >= sp_fmnem) {
  234. /* A mnemonic */
  235. p->em_type = EM_MNEM;
  236. p->em_opcode = i;
  237. return;
  238. }
  239. if (i < sp_fpseu+sp_npseu && i >= sp_fpseu) { /* A pseudo */
  240. if (i == ps_mes) {
  241. p->em_type = EM_STARTMES;
  242. }
  243. else p->em_type = EM_PSEU;
  244. p->em_opcode = i;
  245. return;
  246. }
  247. if (i < sp_filb0+sp_nilb0 && i >= sp_filb0) { /* Instruction label */
  248. p->em_type = EM_DEFILB;
  249. p->em_argtype = ilb_ptyp;
  250. p->em_ilb = i - sp_filb0;
  251. return;
  252. }
  253. switch(i) {
  254. case sp_ilb1: /* Instruction label */
  255. p->em_type = EM_DEFILB;
  256. p->em_argtype = ilb_ptyp;
  257. p->em_ilb = getbyte();
  258. break;
  259. case sp_ilb2: /* Instruction label */
  260. p->em_type = EM_DEFILB;
  261. p->em_argtype = ilb_ptyp;
  262. p->em_ilb = get16();
  263. #ifdef CHECKING
  264. if (p->em_ilb > 32767 && !EM_error) {
  265. EM_error = "Illegal instruction label definition";
  266. }
  267. #endif /* CHECKING */
  268. break;
  269. case sp_dlb1: /* Numeric data label */
  270. p->em_type = EM_DEFDLB;
  271. p->em_argtype = nof_ptyp;
  272. p->em_dlb = getbyte();
  273. break;
  274. case sp_dlb2: /* Numeric data label */
  275. p->em_type = EM_DEFDLB;
  276. p->em_argtype = nof_ptyp;
  277. p->em_dlb = get16();
  278. #ifdef CHECKING
  279. if (p->em_dlb > 32767 && !EM_error) {
  280. EM_error = "Illegal data label definition";
  281. }
  282. #endif /* CHECKING */
  283. break;
  284. case sp_dnam: /* Non-numeric data label */
  285. {
  286. struct string *s;
  287. p->em_type = EM_DEFDNAM;
  288. p->em_argtype = sof_ptyp;
  289. if (!(s = getstring(1))) {
  290. p->em_dnam = "";
  291. }
  292. else p->em_dnam = s->str;
  293. break;
  294. }
  295. case EOF: /* End of file */
  296. p->em_type = EM_EOF;
  297. return;
  298. default:
  299. xerror("Unknown opcode");
  300. break;
  301. }
  302. return;
  303. }