1 /* $Id: readk.c,v 1.17 1994/06/24 11:21:35 ceriel Exp $ */
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".
6 /* This file must be included in the file "read_emk.c".
7 It takes care of the reading of compact EM code.
12 /* get16, get32: read a signed constant
17 register int l_byte, h_byte;
21 if (h_byte >= 128) h_byte -= 256;
22 return l_byte | (h_byte << 8);
32 l |= ((unsigned) getbyte() << 8);
33 l |= ((arith) getbyte() << 16);
35 if (h_byte >= 128) h_byte -= 256;
36 return l | ((arith) h_byte << 24);
39 PRIVATE struct string *getstring();
41 /* getarg : read an argument of any type, and check it against "typset"
42 if neccesary. Put result in "ap".
46 register struct e_arg *ap;
48 register int i = getbyte();
56 if (i < sp_fcst0+sp_ncst0 && i >= sp_fcst0) { /* A cst */
57 ap->ema_cst = i - sp_zcst0;
58 ap->ema_argtype = cst_ptyp;
63 case sp_dlb1: /* Numeric data label encoded in one byte */
64 ap->ema_dlb = getbyte();
66 ap->ema_argtype = nof_ptyp;
69 case sp_dlb2: /* Numeric data label encoded in two bytes */
70 ap->ema_dlb = get16();
72 ap->ema_argtype = nof_ptyp;
74 if (ap->ema_dlb > 32767 && !EM_error) {
75 EM_error = "Illegal data label";
81 case sp_ilb1: /* Instruction label encoded in one byte */
82 ap->ema_ilb = getbyte();
83 ap->ema_argtype = ilb_ptyp;
86 case sp_ilb2: /* Instruction label encoded in two bytes */
87 ap->ema_ilb = get16();
88 ap->ema_argtype = ilb_ptyp;
90 if (ap->ema_ilb > 32767 && !EM_error) {
91 EM_error = "Illegal instruction label";
97 case sp_cst2: /* A cst encoded in two bytes */
98 ap->ema_cst = get16();
99 ap->ema_argtype = cst_ptyp;
102 case sp_cst4: /* A cst encoded in four bytes */
103 ap->ema_cst = get32();
104 ap->ema_argtype = cst_ptyp;
107 case sp_pnam: /* A procedure name */
109 register struct string *p;
112 ap->ema_pnam = p->str;
113 ap->ema_argtype = pro_ptyp;
117 case sp_dnam: /* A Non-numeric data label */
119 register struct string *p;
122 ap->ema_dnam = p->str;
124 ap->ema_argtype = sof_ptyp;
128 case sp_doff: /* A data label + offset */
132 getarg(lab_ptyp, ap);
133 getarg(cst_ptyp, &dummy);
134 ap->ema_szoroff = dummy.ema_cst;
138 case sp_icon: /* An integer constant */
139 case sp_ucon: /* An unsigned constant */
140 case sp_fcon: /* A floating constant */
142 register struct string *p;
144 getarg(cst_ptyp, ap);
145 ap->ema_szoroff = ap->ema_cst;
147 ap->ema_argtype = ptyp(i);
148 ap->ema_string = p->str;
152 case sp_scon: /* A string constant */
154 register struct string *p;
157 ap->ema_argtype = str_ptyp;
158 ap->ema_string = p->str;
159 ap->ema_szoroff = p->length;
165 if (EM_error) return;
168 xfatal("Unexpected EOF");
172 if ((i -= sp_fspec) < 0 || i >= 16) {
173 xerror("Illegal byte");
177 if ((typset & (1 << i)) == 0 && !EM_error) {
178 EM_error = "Bad argument type";
180 if (argtyp == sp_cend) {
183 #else /* not CHECKING */
187 #endif /* CHECKING */
191 /* checkident: check that a string indeed represents an identifier
195 register struct string *s;
201 if (!isascii(*p) || (!isalpha(*p) && *p != '_')) {
205 for (n = s->length; --n > 0; p++) {
206 if (!isascii(*p) || (!isalnum(*p) && *p != '_')) {
212 #endif /* CHECKING */
214 /* getstring: read a string from the input
217 PRIVATE struct string *
222 register struct string *s = &string;
225 getarg(cst_ptyp, &dummy);
226 /* Read length of string */
230 xerror("Negative length in string");
234 #endif /* CHECKING */
238 s->str = Malloc(s->maxlen = 256);
240 else s->str = Realloc(s->str, (s->maxlen = (n+255)&~255));
252 if (!checkident(s) && !EM_error) {
253 EM_error = "Illegal identifier";
256 #endif /* CHECKING */
260 /* gethead: read the start of an EM-line
264 register struct e_instr *p;
270 if ((i = getbyte()) < sp_fmnem+sp_nmnem && i >= sp_fmnem) {
272 p->em_type = EM_MNEM;
277 if (i < sp_fpseu+sp_npseu && i >= sp_fpseu) { /* A pseudo */
279 p->em_type = EM_STARTMES;
281 else p->em_type = EM_PSEU;
286 if (i < sp_filb0+sp_nilb0 && i >= sp_filb0) { /* Instruction label */
287 p->em_type = EM_DEFILB;
288 p->em_argtype = ilb_ptyp;
289 p->em_ilb = i - sp_filb0;
294 case sp_ilb1: /* Instruction label */
295 p->em_type = EM_DEFILB;
296 p->em_argtype = ilb_ptyp;
297 p->em_ilb = getbyte();
300 case sp_ilb2: /* Instruction label */
301 p->em_type = EM_DEFILB;
302 p->em_argtype = ilb_ptyp;
305 if (p->em_ilb > 32767 && !EM_error) {
306 EM_error = "Illegal instruction label definition";
308 #endif /* CHECKING */
311 case sp_dlb1: /* Numeric data label */
312 p->em_type = EM_DEFDLB;
313 p->em_argtype = nof_ptyp;
314 p->em_dlb = getbyte();
317 case sp_dlb2: /* Numeric data label */
318 p->em_type = EM_DEFDLB;
319 p->em_argtype = nof_ptyp;
322 if (p->em_dlb > 32767 && !EM_error) {
323 EM_error = "Illegal data label definition";
325 #endif /* CHECKING */
328 case sp_dnam: /* Non-numeric data label */
332 p->em_type = EM_DEFDNAM;
333 p->em_argtype = sof_ptyp;
334 if (!(s = getstring(1))) {
337 else p->em_dnam = s->str;
341 case EOF: /* End of file */
346 xerror("Unknown opcode");