blob: 704e2eb4dd030fb53e5741f047d2100164dc5ae1 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* parser.c source line parser for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 *
8 * initial version 27/iii/95 by Simon Tatham
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <stddef.h>
14#include <string.h>
15#include <ctype.h>
16
17#include "nasm.h"
18#include "nasmlib.h"
19#include "parser.h"
20#include "float.h"
21
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022static long reg_flags[] = { /* sizes and special flags */
23 0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL,
24 REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8,
25 REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG,
26 REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX,
27 REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS,
28 MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG,
29 REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG,
30 FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG,
31 REG_TREG
32};
33
34enum { /* special tokens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000035 S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
36 S_SHORT, S_TO, S_TWORD, S_WORD
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037};
38
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000039static int is_comma_next (void);
40
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041static int i;
42static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043static efunc error;
H. Peter Anvineba20a72002-04-30 20:53:55 +000044static struct ofmt *outfmt; /* Structure of addresses of output routines */
45static loc_t *location; /* Pointer to current line's segment,offset */
46
47void parser_global_info (struct ofmt *output, loc_t *locp)
48{
49 outfmt = output;
50 location = locp;
51}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052
H. Peter Anvin76690a12002-04-30 20:52:49 +000053insn *parse_line (int pass, char *buffer, insn *result,
H. Peter Anvineba20a72002-04-30 20:53:55 +000054 efunc errfunc, evalfunc evaluate, ldfunc ldef)
55{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056 int operand;
57 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000058 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin76690a12002-04-30 20:52:49 +000060 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062
H. Peter Anvin76690a12002-04-30 20:52:49 +000063 stdscan_reset();
64 stdscan_bufptr = buffer;
65 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066
H. Peter Anvineba20a72002-04-30 20:53:55 +000067 result->label = NULL; /* Assume no label */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068 result->eops = NULL; /* must do this, whatever happens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000069 result->operands = 0; /* must initialise this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
71 if (i==0) { /* blank line - ignore */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072 result->opcode = -1; /* and no instruction either */
73 return result;
74 }
75 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
76 (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
77 error (ERR_NONFATAL, "label or instruction expected"
78 " at start of line");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079 result->opcode = -1;
80 return result;
81 }
82
83 if (i == TOKEN_ID) { /* there's a label here */
H. Peter Anvin76690a12002-04-30 20:52:49 +000084 result->label = tokval.t_charptr;
H. Peter Anvin76690a12002-04-30 20:52:49 +000085 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086 if (i == ':') { /* skip over the optional colon */
H. Peter Anvin76690a12002-04-30 20:52:49 +000087 i = stdscan(NULL, &tokval);
H. Peter Anvineba20a72002-04-30 20:53:55 +000088 } else if (i == 0) {
89 error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
H. Peter Anvin6768eb72002-04-30 20:52:26 +000090 "label alone on a line without a colon might be in error");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000091 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000092 if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
93 {
94 /*
95 * FIXME: location->segment could be NO_SEG, in which case
96 * it is possible we should be passing 'abs_seg'. Look into this.
97 * Work out whether that is *really* what we should be doing.
98 * Generally fix things. I think this is right as it is, but
99 * am still not certain.
100 */
101 ldef (result->label, location->segment,
102 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
103 }
104 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
106 if (i==0) {
107 result->opcode = -1; /* this line contains just a label */
108 return result;
109 }
110
111 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000112 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113
114 while (i == TOKEN_PREFIX ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000115 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
116 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117 /*
118 * Handle special case: the TIMES prefix.
119 */
120 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
121 expr *value;
122
H. Peter Anvin76690a12002-04-30 20:52:49 +0000123 i = stdscan(NULL, &tokval);
124 value = evaluate (stdscan, NULL, &tokval, NULL, pass, error, NULL);
125 i = tokval.t_type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126 if (!value) { /* but, error in evaluator */
127 result->opcode = -1; /* unrecoverable parse error: */
128 return result; /* ignore this instruction */
129 }
130 if (!is_simple (value)) {
131 error (ERR_NONFATAL,
132 "non-constant argument supplied to TIMES");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000133 result->times = 1L;
134 } else {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135 result->times = value->value;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000136 if (value->value < 0) {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000137 error(ERR_NONFATAL, "TIMES value %d is negative",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138 value->value);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000139 result->times = 0;
140 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142 } else {
143 if (result->nprefix == MAXPREFIX)
144 error (ERR_NONFATAL,
145 "instruction has more than %d prefixes", MAXPREFIX);
146 else
147 result->prefixes[result->nprefix++] = tokval.t_integer;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000148 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000149 }
150 }
151
152 if (i != TOKEN_INSN) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000153 if (result->nprefix > 0 && i == 0) {
154 /*
155 * Instruction prefixes are present, but no actual
156 * instruction. This is allowed: at this point we
157 * invent a notional instruction of RESB 0.
158 */
159 result->opcode = I_RESB;
160 result->operands = 1;
161 result->oprs[0].type = IMMEDIATE;
162 result->oprs[0].offset = 0L;
163 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
164 return result;
165 } else {
166 error (ERR_NONFATAL, "parser: instruction expected");
167 result->opcode = -1;
168 return result;
169 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 }
171
172 result->opcode = tokval.t_integer;
173 result->condition = tokval.t_inttwo;
174
175 /*
176 * RESB, RESW and RESD cannot be satisfied with incorrectly
177 * evaluated operands, since the correct values _must_ be known
178 * on the first pass. Hence, even in pass one, we set the
179 * `critical' flag on calling evaluate(), so that it will bomb
180 * out on undefined symbols. Nasty, but there's nothing we can
181 * do about it.
182 *
183 * For the moment, EQU has the same difficulty, so we'll
184 * include that.
185 */
186 if (result->opcode == I_RESB ||
187 result->opcode == I_RESW ||
188 result->opcode == I_RESD ||
189 result->opcode == I_RESQ ||
190 result->opcode == I_REST ||
191 result->opcode == I_EQU)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000192 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000193 critical = pass;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000195 else
196 critical = (pass==2 ? 2 : 0);
197
198 if (result->opcode == I_DB ||
199 result->opcode == I_DW ||
200 result->opcode == I_DD ||
201 result->opcode == I_DQ ||
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 result->opcode == I_DT ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203 result->opcode == I_INCBIN)
204 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000205 extop *eop, **tail = &result->eops, **fixptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206 int oper_num = 0;
207
H. Peter Anvineba20a72002-04-30 20:53:55 +0000208 result->eops_float = FALSE;
209
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000210 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000211 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000212 */
213 while (1) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000214 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000215 if (i == 0)
216 break;
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000217 fixptr = tail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218 eop = *tail = nasm_malloc(sizeof(extop));
219 tail = &eop->next;
220 eop->next = NULL;
221 eop->type = EOT_NOTHING;
222 oper_num++;
223
224 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
225 eop->type = EOT_DB_STRING;
226 eop->stringval = tokval.t_charptr;
227 eop->stringlen = tokval.t_inttwo;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000228 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229 continue;
230 }
231
H. Peter Anvineba20a72002-04-30 20:53:55 +0000232 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233 long sign = +1L;
234
235 if (i == '-') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000236 char *save = stdscan_bufptr;
237 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000238 sign = -1L;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000239 if (i != TOKEN_FLOAT || !is_comma_next()) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000240 stdscan_bufptr = save;
241 i = tokval.t_type = '-';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000242 }
243 }
244
245 if (i == TOKEN_FLOAT) {
246 eop->type = EOT_DB_STRING;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000247 result->eops_float = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000248 if (result->opcode == I_DD)
249 eop->stringlen = 4;
250 else if (result->opcode == I_DQ)
251 eop->stringlen = 8;
252 else if (result->opcode == I_DT)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253 eop->stringlen = 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000254 else {
255 error(ERR_NONFATAL, "floating-point constant"
256 " encountered in `D%c' instruction",
257 result->opcode == I_DW ? 'W' : 'B');
H. Peter Anvineba20a72002-04-30 20:53:55 +0000258 /*
259 * fix suggested by Pedro Gimeno... original line
260 * was:
261 * eop->type = EOT_NOTHING;
262 */
263 eop->stringlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000264 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000265 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000266 tail = &eop->next;
267 *fixptr = eop;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000268 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000269 if (eop->stringlen < 4 ||
270 !float_const (tokval.t_charptr, sign,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000271 (unsigned char *)eop->stringval,
272 eop->stringlen, error))
273 eop->type = EOT_NOTHING;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000274 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000275 continue;
276 }
277 }
278
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279 /* anything else */
280 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281 expr *value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000282 value = evaluate (stdscan, NULL, &tokval, NULL,
283 critical, error, NULL);
284 i = tokval.t_type;
285 if (!value) { /* error in evaluator */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000286 result->opcode = -1;/* unrecoverable parse error: */
287 return result; /* ignore this instruction */
288 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000289 if (is_unknown(value)) {
290 eop->type = EOT_DB_NUMBER;
291 eop->offset = 0; /* doesn't matter what we put */
292 eop->segment = eop->wrt = NO_SEG; /* likewise */
293 } else if (is_reloc(value)) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000294 eop->type = EOT_DB_NUMBER;
295 eop->offset = reloc_value(value);
296 eop->segment = reloc_seg(value);
297 eop->wrt = reloc_wrt(value);
298 } else {
299 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000300 "operand %d: expression is not simple"
301 " or relocatable", oper_num);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000302 }
303 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000304
305 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000306 * We're about to call stdscan(), which will eat the
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000307 * comma that we're currently sitting on between
308 * arguments. However, we'd better check first that it
309 * _is_ a comma.
310 */
311 if (i == 0) /* also could be EOL */
312 break;
313 if (i != ',') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000314 error (ERR_NONFATAL, "comma expected after operand %d",
315 oper_num);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000316 result->opcode = -1;/* unrecoverable parse error: */
317 return result; /* ignore this instruction */
318 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000319 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320
321 if (result->opcode == I_INCBIN) {
322 /*
323 * Correct syntax for INCBIN is that there should be
324 * one string operand, followed by one or two numeric
325 * operands.
326 */
327 if (!result->eops || result->eops->type != EOT_DB_STRING)
328 error (ERR_NONFATAL, "`incbin' expects a file name");
329 else if (result->eops->next &&
330 result->eops->next->type != EOT_DB_NUMBER)
331 error (ERR_NONFATAL, "`incbin': second parameter is",
332 " non-numeric");
333 else if (result->eops->next && result->eops->next->next &&
334 result->eops->next->next->type != EOT_DB_NUMBER)
335 error (ERR_NONFATAL, "`incbin': third parameter is",
336 " non-numeric");
337 else if (result->eops->next && result->eops->next->next &&
338 result->eops->next->next->next)
339 error (ERR_NONFATAL, "`incbin': more than three parameters");
340 else
341 return result;
342 /*
343 * If we reach here, one of the above errors happened.
344 * Throw the instruction away.
345 */
346 result->opcode = -1;
347 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348 } else /* DB ... */
349 if (oper_num == 0)
350 error (ERR_WARNING|ERR_PASS1,
351 "no operand for data declaration");
352 else
353 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000355 return result;
356 }
357
358 /* right. Now we begin to parse the operands. There may be up to three
359 * of these, separated by commas, and terminated by a zero token. */
360
361 for (operand = 0; operand < 3; operand++) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 expr *value; /* used most of the time */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000363 int mref; /* is this going to be a memory ref? */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000364 int bracket; /* is it a [] mref, or a & mref? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000365 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000366
367 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 result->oprs[operand].eaflags = 0; /* and this */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000369 result->oprs[operand].opflags = 0;
370
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000372 if (i == 0) break; /* end of operands: get out of here */
373 result->oprs[operand].type = 0; /* so far, no override */
374 while (i == TOKEN_SPECIAL) {/* size specifiers */
375 switch ((int)tokval.t_integer) {
376 case S_BYTE:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000377 if (!setsize) /* we want to use only the first */
378 result->oprs[operand].type |= BITS8;
379 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000380 break;
381 case S_WORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000382 if (!setsize)
383 result->oprs[operand].type |= BITS16;
384 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000385 break;
386 case S_DWORD:
387 case S_LONG:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388 if (!setsize)
389 result->oprs[operand].type |= BITS32;
390 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391 break;
392 case S_QWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393 if (!setsize)
394 result->oprs[operand].type |= BITS64;
395 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000396 break;
397 case S_TWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000398 if (!setsize)
399 result->oprs[operand].type |= BITS80;
400 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401 break;
402 case S_TO:
403 result->oprs[operand].type |= TO;
404 break;
405 case S_FAR:
406 result->oprs[operand].type |= FAR;
407 break;
408 case S_NEAR:
409 result->oprs[operand].type |= NEAR;
410 break;
411 case S_SHORT:
412 result->oprs[operand].type |= SHORT;
413 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000414 default:
415 error (ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000416 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000417 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000418 }
419
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000420 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421 mref = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000422 bracket = (i == '[');
H. Peter Anvin76690a12002-04-30 20:52:49 +0000423 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000424 if (i == TOKEN_SPECIAL) { /* check for address size override */
425 switch ((int)tokval.t_integer) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426 case S_NOSPLIT:
427 result->oprs[operand].eaflags |= EAF_TIMESTWO;
428 break;
429 case S_BYTE:
430 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
431 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000432 case S_WORD:
433 result->oprs[operand].addr_size = 16;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000434 result->oprs[operand].eaflags |= EAF_WORDOFFS;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000435 break;
436 case S_DWORD:
437 case S_LONG:
438 result->oprs[operand].addr_size = 32;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 result->oprs[operand].eaflags |= EAF_WORDOFFS;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000440 break;
441 default:
442 error (ERR_NONFATAL, "invalid size specification in"
443 " effective address");
444 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000446 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000447 } else { /* immediate operand, or register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000448 mref = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000449 bracket = FALSE; /* placate optimisers */
450 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000451
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000453 &result->oprs[operand].opflags,
454 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000455 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000456 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
457 result->forw_ref = TRUE;
458 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000459 if (!value) { /* error in evaluator */
460 result->opcode = -1; /* unrecoverable parse error: */
461 return result; /* ignore this instruction */
462 }
463 if (i == ':' && mref) { /* it was seg:offset */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000464 /*
465 * Process the segment override.
466 */
467 if (value[1].type!=0 || value->value!=1 ||
468 REG_SREG & ~reg_flags[value->type])
469 error (ERR_NONFATAL, "invalid segment override");
470 else if (result->nprefix == MAXPREFIX)
471 error (ERR_NONFATAL,
472 "instruction has more than %d prefixes",
473 MAXPREFIX);
474 else
475 result->prefixes[result->nprefix++] = value->type;
476
477 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000478 if (i == TOKEN_SPECIAL) { /* another check for size override */
479 switch ((int)tokval.t_integer) {
480 case S_WORD:
481 result->oprs[operand].addr_size = 16;
482 break;
483 case S_DWORD:
484 case S_LONG:
485 result->oprs[operand].addr_size = 32;
486 break;
487 default:
488 error (ERR_NONFATAL, "invalid size specification in"
489 " effective address");
490 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000491 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000493 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000494 &result->oprs[operand].opflags,
495 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000496 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
498 result->forw_ref = TRUE;
499 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 /* and get the offset */
501 if (!value) { /* but, error in evaluator */
502 result->opcode = -1; /* unrecoverable parse error: */
503 return result; /* ignore this instruction */
504 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000505 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000506 if (mref && bracket) { /* find ] at the end */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000507 if (i != ']') {
508 error (ERR_NONFATAL, "parser: expecting ]");
509 do { /* error recovery again */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000510 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000511 } while (i != 0 && i != ',');
512 } else /* we got the required ] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000513 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000514 } else { /* immediate operand */
515 if (i != 0 && i != ',' && i != ':') {
516 error (ERR_NONFATAL, "comma or end of line expected");
517 do { /* error recovery */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000518 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000519 } while (i != 0 && i != ',');
520 } else if (i == ':') {
521 result->oprs[operand].type |= COLON;
522 }
523 }
524
525 /* now convert the exprs returned from evaluate() into operand
526 * descriptions... */
527
528 if (mref) { /* it's a memory reference */
529 expr *e = value;
530 int b, i, s; /* basereg, indexreg, scale */
531 long o; /* offset */
532
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533 b = i = -1, o = s = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534 result->oprs[operand].hintbase = hints.base;
535 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000536
H. Peter Anvin76690a12002-04-30 20:52:49 +0000537 if (e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000538 if (e->value == 1) /* in fact it can be basereg */
539 b = e->type;
540 else /* no, it has to be indexreg */
541 i = e->type, s = e->value;
542 e++;
543 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544 if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
545 {
546 if (b != -1) /* If the first was the base, ... */
547 i = e->type, s = e->value; /* second has to be indexreg */
548
549 else if (e->value != 1) /* If both want to be index */
550 {
551 error(ERR_NONFATAL, "invalid effective address");
552 result->opcode = -1;
553 return result;
554 }
555 else
556 b = e->type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557 e++;
558 }
559 if (e->type != 0) { /* is there an offset? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560 if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
561 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562 error (ERR_NONFATAL, "invalid effective address");
563 result->opcode = -1;
564 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000565 }
566 else
567 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000568 if (e->type == EXPR_UNKNOWN) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000569 o = 0; /* doesn't matter what */
570 result->oprs[operand].wrt = NO_SEG; /* nor this */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000571 result->oprs[operand].segment = NO_SEG; /* or this */
572 while (e->type) e++; /* go to the end of the line */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573 }
574 else
575 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576 if (e->type == EXPR_SIMPLE) {
577 o = e->value;
578 e++;
579 }
580 if (e->type == EXPR_WRT) {
581 result->oprs[operand].wrt = e->value;
582 e++;
583 } else
584 result->oprs[operand].wrt = NO_SEG;
585 /*
586 * Look for a segment base type.
587 */
588 if (e->type && e->type < EXPR_SEGBASE) {
589 error (ERR_NONFATAL, "invalid effective address");
590 result->opcode = -1;
591 return result;
592 }
593 while (e->type && e->value == 0)
594 e++;
595 if (e->type && e->value != 1) {
596 error (ERR_NONFATAL, "invalid effective address");
597 result->opcode = -1;
598 return result;
599 }
600 if (e->type) {
601 result->oprs[operand].segment =
602 e->type - EXPR_SEGBASE;
603 e++;
604 } else
605 result->oprs[operand].segment = NO_SEG;
606 while (e->type && e->value == 0)
607 e++;
608 if (e->type) {
609 error (ERR_NONFATAL, "invalid effective address");
610 result->opcode = -1;
611 return result;
612 }
H. Peter Anvinea838272002-04-30 20:51:53 +0000613 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000614 }
615 } else {
616 o = 0;
617 result->oprs[operand].wrt = NO_SEG;
618 result->oprs[operand].segment = NO_SEG;
619 }
620
621 if (e->type != 0) { /* there'd better be nothing left! */
622 error (ERR_NONFATAL, "invalid effective address");
623 result->opcode = -1;
624 return result;
625 }
626
627 result->oprs[operand].type |= MEMORY;
628 if (b==-1 && (i==-1 || s==0))
629 result->oprs[operand].type |= MEM_OFFS;
630 result->oprs[operand].basereg = b;
631 result->oprs[operand].indexreg = i;
632 result->oprs[operand].scale = s;
633 result->oprs[operand].offset = o;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634 }
635 else /* it's not a memory reference */
636 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000637 if (is_just_unknown(value)) { /* it's immediate but unknown */
638 result->oprs[operand].type |= IMMEDIATE;
639 result->oprs[operand].offset = 0; /* don't care */
640 result->oprs[operand].segment = NO_SEG; /* don't care again */
641 result->oprs[operand].wrt = NO_SEG;/* still don't care */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000642 }
643 else if (is_reloc(value)) /* it's immediate */
644 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000645 result->oprs[operand].type |= IMMEDIATE;
646 result->oprs[operand].offset = reloc_value(value);
647 result->oprs[operand].segment = reloc_seg(value);
648 result->oprs[operand].wrt = reloc_wrt(value);
649 if (is_simple(value) && reloc_value(value)==1)
650 result->oprs[operand].type |= UNITY;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651 }
652 else /* it's a register */
653 {
654 int i;
655
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000656 if (value->type>=EXPR_SIMPLE || value->value!=1) {
657 error (ERR_NONFATAL, "invalid operand type");
658 result->opcode = -1;
659 return result;
660 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661
662 /*
663 * check that its only 1 register, not an expression...
664 */
665 for (i = 1; value[i].type; i++)
666 if (value[i].value) {
667 error (ERR_NONFATAL, "invalid operand type");
668 result->opcode = -1;
669 return result;
670 }
671
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000672 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000673 if (result->oprs[operand].type & ~TO) {
674 /*
675 * we want to produce a warning iff the specified size
676 * is different from the register size
677 */
678 i = result->oprs[operand].type & SIZE_MASK;
679 }
680 else
681 i = 0;
682
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000683 result->oprs[operand].type &= TO;
684 result->oprs[operand].type |= REGISTER;
685 result->oprs[operand].type |= reg_flags[value->type];
686 result->oprs[operand].basereg = value->type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687
688 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
689 error (ERR_WARNING|ERR_PASS1,
690 "register size specification ignored");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000691 }
692 }
693 }
694
695 result->operands = operand; /* set operand count */
696
697 while (operand<3) /* clear remaining operands */
698 result->oprs[operand++].type = 0;
699
700 /*
701 * Transform RESW, RESD, RESQ, REST into RESB.
702 */
703 switch (result->opcode) {
704 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
705 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
706 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
707 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
708 }
709
710 return result;
711}
712
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713static int is_comma_next (void)
714{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000715 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000716 int i;
717 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718
H. Peter Anvin76690a12002-04-30 20:52:49 +0000719 p = stdscan_bufptr;
720 i = stdscan (NULL, &tv);
721 stdscan_bufptr = p;
722 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723}
724
H. Peter Anvineba20a72002-04-30 20:53:55 +0000725void cleanup_insn (insn *i)
726{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727 extop *e;
728
729 while (i->eops) {
730 e = i->eops;
731 i->eops = i->eops->next;
732 nasm_free (e);
733 }
734}