blob: b2a03403ebbb42881b64b5b9f3ed81dbe80cd47e [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"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000018#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000019#include "nasmlib.h"
20#include "parser.h"
21#include "float.h"
22
H. Peter Anvind0e365d2002-05-26 18:19:19 +000023extern int in_abs_seg; /* ABSOLUTE segment flag */
24extern long abs_seg; /* ABSOLUTE segment */
25extern long abs_offset; /* ABSOLUTE segment offset */
26
H. Peter Anvin232badb2002-06-06 02:41:20 +000027#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000028
29enum { /* special tokens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000030 S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
H. Peter Anvin01377d82002-05-21 03:16:33 +000031 S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032};
33
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034static int is_comma_next (void);
35
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036static int i;
37static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000038static efunc error;
H. Peter Anvineba20a72002-04-30 20:53:55 +000039static struct ofmt *outfmt; /* Structure of addresses of output routines */
40static loc_t *location; /* Pointer to current line's segment,offset */
41
42void parser_global_info (struct ofmt *output, loc_t *locp)
43{
44 outfmt = output;
45 location = locp;
46}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
H. Peter Anvin76690a12002-04-30 20:52:49 +000048insn *parse_line (int pass, char *buffer, insn *result,
H. Peter Anvineba20a72002-04-30 20:53:55 +000049 efunc errfunc, evalfunc evaluate, ldfunc ldef)
50{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051 int operand;
52 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000053 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057
H. Peter Anvin76690a12002-04-30 20:52:49 +000058 stdscan_reset();
59 stdscan_bufptr = buffer;
60 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvineba20a72002-04-30 20:53:55 +000062 result->label = NULL; /* Assume no label */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000063 result->eops = NULL; /* must do this, whatever happens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000064 result->operands = 0; /* must initialise this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
66 if (i==0) { /* blank line - ignore */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000067 result->opcode = -1; /* and no instruction either */
68 return result;
69 }
70 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
71 (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
72 error (ERR_NONFATAL, "label or instruction expected"
73 " at start of line");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000074 result->opcode = -1;
75 return result;
76 }
77
78 if (i == TOKEN_ID) { /* there's a label here */
H. Peter Anvin76690a12002-04-30 20:52:49 +000079 result->label = tokval.t_charptr;
H. Peter Anvin76690a12002-04-30 20:52:49 +000080 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000081 if (i == ':') { /* skip over the optional colon */
H. Peter Anvin76690a12002-04-30 20:52:49 +000082 i = stdscan(NULL, &tokval);
H. Peter Anvineba20a72002-04-30 20:53:55 +000083 } else if (i == 0) {
84 error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
H. Peter Anvin6768eb72002-04-30 20:52:26 +000085 "label alone on a line without a colon might be in error");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000087 if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
88 {
89 /*
90 * FIXME: location->segment could be NO_SEG, in which case
91 * it is possible we should be passing 'abs_seg'. Look into this.
92 * Work out whether that is *really* what we should be doing.
93 * Generally fix things. I think this is right as it is, but
94 * am still not certain.
95 */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000096 ldef (result->label, in_abs_seg?abs_seg:location->segment,
H. Peter Anvineba20a72002-04-30 20:53:55 +000097 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
98 }
99 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100
101 if (i==0) {
102 result->opcode = -1; /* this line contains just a label */
103 return result;
104 }
105
106 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000107 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
109 while (i == TOKEN_PREFIX ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000110 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
111 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000112 /*
113 * Handle special case: the TIMES prefix.
114 */
115 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
116 expr *value;
117
H. Peter Anvin76690a12002-04-30 20:52:49 +0000118 i = stdscan(NULL, &tokval);
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000119 value = evaluate (stdscan, NULL, &tokval, NULL, pass0, error, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000120 i = tokval.t_type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121 if (!value) { /* but, error in evaluator */
122 result->opcode = -1; /* unrecoverable parse error: */
123 return result; /* ignore this instruction */
124 }
125 if (!is_simple (value)) {
126 error (ERR_NONFATAL,
127 "non-constant argument supplied to TIMES");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000128 result->times = 1L;
129 } else {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130 result->times = value->value;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000131 if (value->value < 0) {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000132 error(ERR_NONFATAL, "TIMES value %d is negative",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000133 value->value);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000134 result->times = 0;
135 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000137 } else {
138 if (result->nprefix == MAXPREFIX)
139 error (ERR_NONFATAL,
140 "instruction has more than %d prefixes", MAXPREFIX);
141 else
142 result->prefixes[result->nprefix++] = tokval.t_integer;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000143 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144 }
145 }
146
147 if (i != TOKEN_INSN) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000148 if (result->nprefix > 0 && i == 0) {
149 /*
150 * Instruction prefixes are present, but no actual
151 * instruction. This is allowed: at this point we
152 * invent a notional instruction of RESB 0.
153 */
154 result->opcode = I_RESB;
155 result->operands = 1;
156 result->oprs[0].type = IMMEDIATE;
157 result->oprs[0].offset = 0L;
158 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
159 return result;
160 } else {
161 error (ERR_NONFATAL, "parser: instruction expected");
162 result->opcode = -1;
163 return result;
164 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165 }
166
167 result->opcode = tokval.t_integer;
168 result->condition = tokval.t_inttwo;
169
170 /*
171 * RESB, RESW and RESD cannot be satisfied with incorrectly
172 * evaluated operands, since the correct values _must_ be known
173 * on the first pass. Hence, even in pass one, we set the
174 * `critical' flag on calling evaluate(), so that it will bomb
175 * out on undefined symbols. Nasty, but there's nothing we can
176 * do about it.
177 *
178 * For the moment, EQU has the same difficulty, so we'll
179 * include that.
180 */
181 if (result->opcode == I_RESB ||
182 result->opcode == I_RESW ||
183 result->opcode == I_RESD ||
184 result->opcode == I_RESQ ||
185 result->opcode == I_REST ||
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000186 result->opcode == I_EQU ||
187 result->opcode == I_INCBIN) /* fbk */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000188 {
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000189 critical = pass0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000190 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191 else
192 critical = (pass==2 ? 2 : 0);
193
194 if (result->opcode == I_DB ||
195 result->opcode == I_DW ||
196 result->opcode == I_DD ||
197 result->opcode == I_DQ ||
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 result->opcode == I_DT ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199 result->opcode == I_INCBIN)
200 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000201 extop *eop, **tail = &result->eops, **fixptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000202 int oper_num = 0;
203
H. Peter Anvineba20a72002-04-30 20:53:55 +0000204 result->eops_float = FALSE;
205
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000207 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208 */
209 while (1) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000210 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000211 if (i == 0)
212 break;
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000213 fixptr = tail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214 eop = *tail = nasm_malloc(sizeof(extop));
215 tail = &eop->next;
216 eop->next = NULL;
217 eop->type = EOT_NOTHING;
218 oper_num++;
219
220 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
221 eop->type = EOT_DB_STRING;
222 eop->stringval = tokval.t_charptr;
223 eop->stringlen = tokval.t_inttwo;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000224 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225 continue;
226 }
227
H. Peter Anvineba20a72002-04-30 20:53:55 +0000228 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229 long sign = +1L;
230
231 if (i == '-') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000232 char *save = stdscan_bufptr;
233 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000234 sign = -1L;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000235 if (i != TOKEN_FLOAT || !is_comma_next()) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000236 stdscan_bufptr = save;
237 i = tokval.t_type = '-';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000238 }
239 }
240
241 if (i == TOKEN_FLOAT) {
242 eop->type = EOT_DB_STRING;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000243 result->eops_float = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000244 if (result->opcode == I_DD)
245 eop->stringlen = 4;
246 else if (result->opcode == I_DQ)
247 eop->stringlen = 8;
248 else if (result->opcode == I_DT)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000249 eop->stringlen = 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000250 else {
251 error(ERR_NONFATAL, "floating-point constant"
252 " encountered in `D%c' instruction",
253 result->opcode == I_DW ? 'W' : 'B');
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254 /*
255 * fix suggested by Pedro Gimeno... original line
256 * was:
257 * eop->type = EOT_NOTHING;
258 */
259 eop->stringlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000260 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000261 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000262 tail = &eop->next;
263 *fixptr = eop;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000264 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000265 if (eop->stringlen < 4 ||
266 !float_const (tokval.t_charptr, sign,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267 (unsigned char *)eop->stringval,
268 eop->stringlen, error))
269 eop->type = EOT_NOTHING;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000270 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000271 continue;
272 }
273 }
274
H. Peter Anvineba20a72002-04-30 20:53:55 +0000275 /* anything else */
276 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000277 expr *value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000278 value = evaluate (stdscan, NULL, &tokval, NULL,
279 critical, error, NULL);
280 i = tokval.t_type;
281 if (!value) { /* error in evaluator */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000282 result->opcode = -1;/* unrecoverable parse error: */
283 return result; /* ignore this instruction */
284 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000285 if (is_unknown(value)) {
286 eop->type = EOT_DB_NUMBER;
287 eop->offset = 0; /* doesn't matter what we put */
288 eop->segment = eop->wrt = NO_SEG; /* likewise */
289 } else if (is_reloc(value)) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000290 eop->type = EOT_DB_NUMBER;
291 eop->offset = reloc_value(value);
292 eop->segment = reloc_seg(value);
293 eop->wrt = reloc_wrt(value);
294 } else {
295 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296 "operand %d: expression is not simple"
297 " or relocatable", oper_num);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000298 }
299 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000300
301 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000302 * We're about to call stdscan(), which will eat the
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000303 * comma that we're currently sitting on between
304 * arguments. However, we'd better check first that it
305 * _is_ a comma.
306 */
307 if (i == 0) /* also could be EOL */
308 break;
309 if (i != ',') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000310 error (ERR_NONFATAL, "comma expected after operand %d",
311 oper_num);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000312 result->opcode = -1;/* unrecoverable parse error: */
313 return result; /* ignore this instruction */
314 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316
317 if (result->opcode == I_INCBIN) {
318 /*
319 * Correct syntax for INCBIN is that there should be
320 * one string operand, followed by one or two numeric
321 * operands.
322 */
323 if (!result->eops || result->eops->type != EOT_DB_STRING)
324 error (ERR_NONFATAL, "`incbin' expects a file name");
325 else if (result->eops->next &&
326 result->eops->next->type != EOT_DB_NUMBER)
327 error (ERR_NONFATAL, "`incbin': second parameter is",
328 " non-numeric");
329 else if (result->eops->next && result->eops->next->next &&
330 result->eops->next->next->type != EOT_DB_NUMBER)
331 error (ERR_NONFATAL, "`incbin': third parameter is",
332 " non-numeric");
333 else if (result->eops->next && result->eops->next->next &&
334 result->eops->next->next->next)
335 error (ERR_NONFATAL, "`incbin': more than three parameters");
336 else
337 return result;
338 /*
339 * If we reach here, one of the above errors happened.
340 * Throw the instruction away.
341 */
342 result->opcode = -1;
343 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344 } else /* DB ... */
345 if (oper_num == 0)
346 error (ERR_WARNING|ERR_PASS1,
347 "no operand for data declaration");
348 else
349 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 return result;
352 }
353
354 /* right. Now we begin to parse the operands. There may be up to three
355 * of these, separated by commas, and terminated by a zero token. */
356
357 for (operand = 0; operand < 3; operand++) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000358 expr *value; /* used most of the time */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 int mref; /* is this going to be a memory ref? */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000360 int bracket; /* is it a [] mref, or a & mref? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000361 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362
363 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 result->oprs[operand].eaflags = 0; /* and this */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000365 result->oprs[operand].opflags = 0;
366
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000368 if (i == 0) break; /* end of operands: get out of here */
369 result->oprs[operand].type = 0; /* so far, no override */
370 while (i == TOKEN_SPECIAL) {/* size specifiers */
371 switch ((int)tokval.t_integer) {
372 case S_BYTE:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000373 if (!setsize) /* we want to use only the first */
374 result->oprs[operand].type |= BITS8;
375 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000376 break;
377 case S_WORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000378 if (!setsize)
379 result->oprs[operand].type |= BITS16;
380 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000381 break;
382 case S_DWORD:
383 case S_LONG:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000384 if (!setsize)
385 result->oprs[operand].type |= BITS32;
386 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387 break;
388 case S_QWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000389 if (!setsize)
390 result->oprs[operand].type |= BITS64;
391 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000392 break;
393 case S_TWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000394 if (!setsize)
395 result->oprs[operand].type |= BITS80;
396 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397 break;
398 case S_TO:
399 result->oprs[operand].type |= TO;
400 break;
H. Peter Anvin01377d82002-05-21 03:16:33 +0000401 case S_STRICT:
402 result->oprs[operand].type |= STRICT;
403 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000404 case S_FAR:
405 result->oprs[operand].type |= FAR;
406 break;
407 case S_NEAR:
408 result->oprs[operand].type |= NEAR;
409 break;
410 case S_SHORT:
411 result->oprs[operand].type |= SHORT;
412 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000413 default:
414 error (ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000415 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000416 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000417 }
418
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000419 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000420 mref = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000421 bracket = (i == '[');
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000422 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000423 if (i == TOKEN_SPECIAL) { /* check for address size override */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000424 if (tasm_compatible_mode) {
425 switch ((int)tokval.t_integer) {
426 /* For TASM compatibility a size override inside the
427 * brackets changes the size of the operand, not the
428 * address type of the operand as it does in standard
429 * NASM syntax. Hence:
430 *
431 * mov eax,[DWORD val]
432 *
433 * is valid syntax in TASM compatibility mode. Note that
434 * you lose the ability to override the default address
435 * type for the instruction, but we never use anything
436 * but 32-bit flat model addressing in our code.
437 */
438 case S_BYTE:
439 result->oprs[operand].type |= BITS8;
440 break;
441 case S_WORD:
442 result->oprs[operand].type |= BITS16;
443 break;
444 case S_DWORD:
445 case S_LONG:
446 result->oprs[operand].type |= BITS32;
447 break;
448 case S_QWORD:
449 result->oprs[operand].type |= BITS64;
450 break;
451 case S_TWORD:
452 result->oprs[operand].type |= BITS80;
453 break;
454 default:
455 error (ERR_NONFATAL, "invalid operand size specification");
456 }
457 } else {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000458 /* Standard NASM compatible syntax */
459 switch ((int)tokval.t_integer) {
460 case S_NOSPLIT:
461 result->oprs[operand].eaflags |= EAF_TIMESTWO;
462 break;
463 case S_BYTE:
464 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
465 break;
466 case S_WORD:
467 result->oprs[operand].addr_size = 16;
468 result->oprs[operand].eaflags |= EAF_WORDOFFS;
469 break;
470 case S_DWORD:
471 case S_LONG:
472 result->oprs[operand].addr_size = 32;
473 result->oprs[operand].eaflags |= EAF_WORDOFFS;
474 break;
475 default:
476 error (ERR_NONFATAL, "invalid size specification in"
477 " effective address");
478 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000479 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000480 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000481 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000482 } else { /* immediate operand, or register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483 mref = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000484 bracket = FALSE; /* placate optimisers */
485 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000486
Debbie Wiles63b53f72002-06-04 19:31:24 +0000487 if((result->oprs[operand].type & FAR) && !mref)
488 {
489 error (ERR_NONFATAL, "invalid use of FAR operand specifier");
490 }
491
H. Peter Anvin76690a12002-04-30 20:52:49 +0000492 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493 &result->oprs[operand].opflags,
494 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000495 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000496 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
497 result->forw_ref = TRUE;
498 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499 if (!value) { /* error in evaluator */
500 result->opcode = -1; /* unrecoverable parse error: */
501 return result; /* ignore this instruction */
502 }
503 if (i == ':' && mref) { /* it was seg:offset */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000504 /*
505 * Process the segment override.
506 */
507 if (value[1].type!=0 || value->value!=1 ||
508 REG_SREG & ~reg_flags[value->type])
509 error (ERR_NONFATAL, "invalid segment override");
510 else if (result->nprefix == MAXPREFIX)
511 error (ERR_NONFATAL,
512 "instruction has more than %d prefixes",
513 MAXPREFIX);
514 else
515 result->prefixes[result->nprefix++] = value->type;
516
517 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000518 if (i == TOKEN_SPECIAL) { /* another check for size override */
519 switch ((int)tokval.t_integer) {
520 case S_WORD:
521 result->oprs[operand].addr_size = 16;
522 break;
523 case S_DWORD:
524 case S_LONG:
525 result->oprs[operand].addr_size = 32;
526 break;
527 default:
528 error (ERR_NONFATAL, "invalid size specification in"
529 " effective address");
530 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000531 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000532 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000533 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534 &result->oprs[operand].opflags,
535 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000536 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
538 result->forw_ref = TRUE;
539 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000540 /* and get the offset */
541 if (!value) { /* but, error in evaluator */
542 result->opcode = -1; /* unrecoverable parse error: */
543 return result; /* ignore this instruction */
544 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000545 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000546 if (mref && bracket) { /* find ] at the end */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000547 if (i != ']') {
548 error (ERR_NONFATAL, "parser: expecting ]");
549 do { /* error recovery again */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000550 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551 } while (i != 0 && i != ',');
552 } else /* we got the required ] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000553 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000554 } else { /* immediate operand */
555 if (i != 0 && i != ',' && i != ':') {
556 error (ERR_NONFATAL, "comma or end of line expected");
557 do { /* error recovery */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000558 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559 } while (i != 0 && i != ',');
560 } else if (i == ':') {
561 result->oprs[operand].type |= COLON;
562 }
563 }
564
565 /* now convert the exprs returned from evaluate() into operand
566 * descriptions... */
567
568 if (mref) { /* it's a memory reference */
569 expr *e = value;
570 int b, i, s; /* basereg, indexreg, scale */
571 long o; /* offset */
572
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000573 b = i = -1, o = s = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000574 result->oprs[operand].hintbase = hints.base;
575 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000576
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577 if (e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000578 if (e->value == 1) /* in fact it can be basereg */
579 b = e->type;
580 else /* no, it has to be indexreg */
581 i = e->type, s = e->value;
582 e++;
583 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584 if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
585 {
586 if (b != -1) /* If the first was the base, ... */
587 i = e->type, s = e->value; /* second has to be indexreg */
588
589 else if (e->value != 1) /* If both want to be index */
590 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000591 error(ERR_NONFATAL, "beroset-p-592-invalid effective address");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592 result->opcode = -1;
593 return result;
594 }
595 else
596 b = e->type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597 e++;
598 }
599 if (e->type != 0) { /* is there an offset? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600 if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
601 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000602 error (ERR_NONFATAL, "beroset-p-603-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000603 result->opcode = -1;
604 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605 }
606 else
607 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000608 if (e->type == EXPR_UNKNOWN) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609 o = 0; /* doesn't matter what */
610 result->oprs[operand].wrt = NO_SEG; /* nor this */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000611 result->oprs[operand].segment = NO_SEG; /* or this */
612 while (e->type) e++; /* go to the end of the line */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613 }
614 else
615 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000616 if (e->type == EXPR_SIMPLE) {
617 o = e->value;
618 e++;
619 }
620 if (e->type == EXPR_WRT) {
621 result->oprs[operand].wrt = e->value;
622 e++;
623 } else
624 result->oprs[operand].wrt = NO_SEG;
625 /*
626 * Look for a segment base type.
627 */
628 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000629 error (ERR_NONFATAL, "beroset-p-630-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000630 result->opcode = -1;
631 return result;
632 }
633 while (e->type && e->value == 0)
634 e++;
635 if (e->type && e->value != 1) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000636 error (ERR_NONFATAL, "beroset-p-637-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000637 result->opcode = -1;
638 return result;
639 }
640 if (e->type) {
641 result->oprs[operand].segment =
642 e->type - EXPR_SEGBASE;
643 e++;
644 } else
645 result->oprs[operand].segment = NO_SEG;
646 while (e->type && e->value == 0)
647 e++;
648 if (e->type) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000649 error (ERR_NONFATAL, "beroset-p-650-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000650 result->opcode = -1;
651 return result;
652 }
H. Peter Anvinea838272002-04-30 20:51:53 +0000653 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000654 }
655 } else {
656 o = 0;
657 result->oprs[operand].wrt = NO_SEG;
658 result->oprs[operand].segment = NO_SEG;
659 }
660
661 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000662 error (ERR_NONFATAL, "beroset-p-663-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000663 result->opcode = -1;
664 return result;
665 }
666
667 result->oprs[operand].type |= MEMORY;
668 if (b==-1 && (i==-1 || s==0))
669 result->oprs[operand].type |= MEM_OFFS;
670 result->oprs[operand].basereg = b;
671 result->oprs[operand].indexreg = i;
672 result->oprs[operand].scale = s;
673 result->oprs[operand].offset = o;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000674 }
675 else /* it's not a memory reference */
676 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000677 if (is_just_unknown(value)) { /* it's immediate but unknown */
678 result->oprs[operand].type |= IMMEDIATE;
679 result->oprs[operand].offset = 0; /* don't care */
680 result->oprs[operand].segment = NO_SEG; /* don't care again */
681 result->oprs[operand].wrt = NO_SEG;/* still don't care */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000682 }
683 else if (is_reloc(value)) /* it's immediate */
684 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000685 result->oprs[operand].type |= IMMEDIATE;
686 result->oprs[operand].offset = reloc_value(value);
687 result->oprs[operand].segment = reloc_seg(value);
688 result->oprs[operand].wrt = reloc_wrt(value);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000689 if (is_simple(value)) {
690 if (reloc_value(value)==1)
691 result->oprs[operand].type |= UNITY;
H. Peter Anvin8c1da7b2002-05-22 20:45:09 +0000692 if (optimizing>=0 &&
693 !(result->oprs[operand].type & STRICT)) {
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000694 if (reloc_value(value) >= -128 &&
695 reloc_value(value) <= 127)
696 result->oprs[operand].type |= SBYTE;
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000697 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000698 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000699 }
700 else /* it's a register */
701 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000702 if (value->type>=EXPR_SIMPLE || value->value!=1) {
703 error (ERR_NONFATAL, "invalid operand type");
704 result->opcode = -1;
705 return result;
706 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000707
708 /*
709 * check that its only 1 register, not an expression...
710 */
711 for (i = 1; value[i].type; i++)
712 if (value[i].value) {
713 error (ERR_NONFATAL, "invalid operand type");
714 result->opcode = -1;
715 return result;
716 }
717
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000719 if (result->oprs[operand].type & ~TO) {
720 /*
721 * we want to produce a warning iff the specified size
722 * is different from the register size
723 */
724 i = result->oprs[operand].type & SIZE_MASK;
725 }
726 else
727 i = 0;
728
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000729 result->oprs[operand].type &= TO;
730 result->oprs[operand].type |= REGISTER;
731 result->oprs[operand].type |= reg_flags[value->type];
732 result->oprs[operand].basereg = value->type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733
734 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
735 error (ERR_WARNING|ERR_PASS1,
736 "register size specification ignored");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737 }
738 }
739 }
740
741 result->operands = operand; /* set operand count */
742
743 while (operand<3) /* clear remaining operands */
744 result->oprs[operand++].type = 0;
745
746 /*
747 * Transform RESW, RESD, RESQ, REST into RESB.
748 */
749 switch (result->opcode) {
750 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
751 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
752 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
753 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
754 }
755
756 return result;
757}
758
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759static int is_comma_next (void)
760{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000761 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000762 int i;
763 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000764
H. Peter Anvin76690a12002-04-30 20:52:49 +0000765 p = stdscan_bufptr;
766 i = stdscan (NULL, &tv);
767 stdscan_bufptr = p;
768 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000769}
770
H. Peter Anvineba20a72002-04-30 20:53:55 +0000771void cleanup_insn (insn *i)
772{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773 extop *e;
774
775 while (i->eops) {
776 e = i->eops;
777 i->eops = i->eops->next;
778 nasm_free (e);
779 }
780}