blob: d0be7a46040217009bd0c9451ef8fd66877386fc [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 Anvind0e365d2002-05-26 18:19:19 +000022extern int in_abs_seg; /* ABSOLUTE segment flag */
23extern long abs_seg; /* ABSOLUTE segment */
24extern long abs_offset; /* ABSOLUTE segment offset */
25
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000026static long reg_flags[] = { /* sizes and special flags */
27 0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL,
28 REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8,
29 REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG,
30 REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX,
31 REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS,
32 MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG,
33 REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG,
34 FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG,
H. Peter Anvin4836e332002-04-30 20:56:43 +000035 REG_TREG,
36 XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037};
38
39enum { /* special tokens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000040 S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
H. Peter Anvin01377d82002-05-21 03:16:33 +000041 S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000042};
43
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044static int is_comma_next (void);
45
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046static int i;
47static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048static efunc error;
H. Peter Anvineba20a72002-04-30 20:53:55 +000049static struct ofmt *outfmt; /* Structure of addresses of output routines */
50static loc_t *location; /* Pointer to current line's segment,offset */
51
52void parser_global_info (struct ofmt *output, loc_t *locp)
53{
54 outfmt = output;
55 location = locp;
56}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057
H. Peter Anvin76690a12002-04-30 20:52:49 +000058insn *parse_line (int pass, char *buffer, insn *result,
H. Peter Anvineba20a72002-04-30 20:53:55 +000059 efunc errfunc, evalfunc evaluate, ldfunc ldef)
60{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 int operand;
62 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000063 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin76690a12002-04-30 20:52:49 +000065 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000067
H. Peter Anvin76690a12002-04-30 20:52:49 +000068 stdscan_reset();
69 stdscan_bufptr = buffer;
70 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000071
H. Peter Anvineba20a72002-04-30 20:53:55 +000072 result->label = NULL; /* Assume no label */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073 result->eops = NULL; /* must do this, whatever happens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000074 result->operands = 0; /* must initialise this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000075
76 if (i==0) { /* blank line - ignore */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077 result->opcode = -1; /* and no instruction either */
78 return result;
79 }
80 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
81 (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
82 error (ERR_NONFATAL, "label or instruction expected"
83 " at start of line");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084 result->opcode = -1;
85 return result;
86 }
87
88 if (i == TOKEN_ID) { /* there's a label here */
H. Peter Anvin76690a12002-04-30 20:52:49 +000089 result->label = tokval.t_charptr;
H. Peter Anvin76690a12002-04-30 20:52:49 +000090 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000091 if (i == ':') { /* skip over the optional colon */
H. Peter Anvin76690a12002-04-30 20:52:49 +000092 i = stdscan(NULL, &tokval);
H. Peter Anvineba20a72002-04-30 20:53:55 +000093 } else if (i == 0) {
94 error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
H. Peter Anvin6768eb72002-04-30 20:52:26 +000095 "label alone on a line without a colon might be in error");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000097 if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
98 {
99 /*
100 * FIXME: location->segment could be NO_SEG, in which case
101 * it is possible we should be passing 'abs_seg'. Look into this.
102 * Work out whether that is *really* what we should be doing.
103 * Generally fix things. I think this is right as it is, but
104 * am still not certain.
105 */
H. Peter Anvind0e365d2002-05-26 18:19:19 +0000106 ldef (result->label, in_abs_seg?abs_seg:location->segment,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000107 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
108 }
109 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000110
111 if (i==0) {
112 result->opcode = -1; /* this line contains just a label */
113 return result;
114 }
115
116 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000118
119 while (i == TOKEN_PREFIX ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000120 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
121 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122 /*
123 * Handle special case: the TIMES prefix.
124 */
125 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
126 expr *value;
127
H. Peter Anvin76690a12002-04-30 20:52:49 +0000128 i = stdscan(NULL, &tokval);
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000129 value = evaluate (stdscan, NULL, &tokval, NULL, pass0, error, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000130 i = tokval.t_type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131 if (!value) { /* but, error in evaluator */
132 result->opcode = -1; /* unrecoverable parse error: */
133 return result; /* ignore this instruction */
134 }
135 if (!is_simple (value)) {
136 error (ERR_NONFATAL,
137 "non-constant argument supplied to TIMES");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138 result->times = 1L;
139 } else {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140 result->times = value->value;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000141 if (value->value < 0) {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000142 error(ERR_NONFATAL, "TIMES value %d is negative",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143 value->value);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000144 result->times = 0;
145 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000147 } else {
148 if (result->nprefix == MAXPREFIX)
149 error (ERR_NONFATAL,
150 "instruction has more than %d prefixes", MAXPREFIX);
151 else
152 result->prefixes[result->nprefix++] = tokval.t_integer;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000153 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154 }
155 }
156
157 if (i != TOKEN_INSN) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000158 if (result->nprefix > 0 && i == 0) {
159 /*
160 * Instruction prefixes are present, but no actual
161 * instruction. This is allowed: at this point we
162 * invent a notional instruction of RESB 0.
163 */
164 result->opcode = I_RESB;
165 result->operands = 1;
166 result->oprs[0].type = IMMEDIATE;
167 result->oprs[0].offset = 0L;
168 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
169 return result;
170 } else {
171 error (ERR_NONFATAL, "parser: instruction expected");
172 result->opcode = -1;
173 return result;
174 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000175 }
176
177 result->opcode = tokval.t_integer;
178 result->condition = tokval.t_inttwo;
179
180 /*
181 * RESB, RESW and RESD cannot be satisfied with incorrectly
182 * evaluated operands, since the correct values _must_ be known
183 * on the first pass. Hence, even in pass one, we set the
184 * `critical' flag on calling evaluate(), so that it will bomb
185 * out on undefined symbols. Nasty, but there's nothing we can
186 * do about it.
187 *
188 * For the moment, EQU has the same difficulty, so we'll
189 * include that.
190 */
191 if (result->opcode == I_RESB ||
192 result->opcode == I_RESW ||
193 result->opcode == I_RESD ||
194 result->opcode == I_RESQ ||
195 result->opcode == I_REST ||
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000196 result->opcode == I_EQU ||
197 result->opcode == I_INCBIN) /* fbk */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 {
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000199 critical = pass0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000201 else
202 critical = (pass==2 ? 2 : 0);
203
204 if (result->opcode == I_DB ||
205 result->opcode == I_DW ||
206 result->opcode == I_DD ||
207 result->opcode == I_DQ ||
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208 result->opcode == I_DT ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000209 result->opcode == I_INCBIN)
210 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000211 extop *eop, **tail = &result->eops, **fixptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000212 int oper_num = 0;
213
H. Peter Anvineba20a72002-04-30 20:53:55 +0000214 result->eops_float = FALSE;
215
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000217 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218 */
219 while (1) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000220 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221 if (i == 0)
222 break;
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000223 fixptr = tail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224 eop = *tail = nasm_malloc(sizeof(extop));
225 tail = &eop->next;
226 eop->next = NULL;
227 eop->type = EOT_NOTHING;
228 oper_num++;
229
230 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
231 eop->type = EOT_DB_STRING;
232 eop->stringval = tokval.t_charptr;
233 eop->stringlen = tokval.t_inttwo;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000234 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000235 continue;
236 }
237
H. Peter Anvineba20a72002-04-30 20:53:55 +0000238 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000239 long sign = +1L;
240
241 if (i == '-') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242 char *save = stdscan_bufptr;
243 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000244 sign = -1L;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000245 if (i != TOKEN_FLOAT || !is_comma_next()) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000246 stdscan_bufptr = save;
247 i = tokval.t_type = '-';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000248 }
249 }
250
251 if (i == TOKEN_FLOAT) {
252 eop->type = EOT_DB_STRING;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253 result->eops_float = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000254 if (result->opcode == I_DD)
255 eop->stringlen = 4;
256 else if (result->opcode == I_DQ)
257 eop->stringlen = 8;
258 else if (result->opcode == I_DT)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259 eop->stringlen = 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000260 else {
261 error(ERR_NONFATAL, "floating-point constant"
262 " encountered in `D%c' instruction",
263 result->opcode == I_DW ? 'W' : 'B');
H. Peter Anvineba20a72002-04-30 20:53:55 +0000264 /*
265 * fix suggested by Pedro Gimeno... original line
266 * was:
267 * eop->type = EOT_NOTHING;
268 */
269 eop->stringlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000270 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000271 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000272 tail = &eop->next;
273 *fixptr = eop;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000274 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000275 if (eop->stringlen < 4 ||
276 !float_const (tokval.t_charptr, sign,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000277 (unsigned char *)eop->stringval,
278 eop->stringlen, error))
279 eop->type = EOT_NOTHING;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281 continue;
282 }
283 }
284
H. Peter Anvineba20a72002-04-30 20:53:55 +0000285 /* anything else */
286 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000287 expr *value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000288 value = evaluate (stdscan, NULL, &tokval, NULL,
289 critical, error, NULL);
290 i = tokval.t_type;
291 if (!value) { /* error in evaluator */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000292 result->opcode = -1;/* unrecoverable parse error: */
293 return result; /* ignore this instruction */
294 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000295 if (is_unknown(value)) {
296 eop->type = EOT_DB_NUMBER;
297 eop->offset = 0; /* doesn't matter what we put */
298 eop->segment = eop->wrt = NO_SEG; /* likewise */
299 } else if (is_reloc(value)) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000300 eop->type = EOT_DB_NUMBER;
301 eop->offset = reloc_value(value);
302 eop->segment = reloc_seg(value);
303 eop->wrt = reloc_wrt(value);
304 } else {
305 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000306 "operand %d: expression is not simple"
307 " or relocatable", oper_num);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000308 }
309 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000310
311 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000312 * We're about to call stdscan(), which will eat the
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000313 * comma that we're currently sitting on between
314 * arguments. However, we'd better check first that it
315 * _is_ a comma.
316 */
317 if (i == 0) /* also could be EOL */
318 break;
319 if (i != ',') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000320 error (ERR_NONFATAL, "comma expected after operand %d",
321 oper_num);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000322 result->opcode = -1;/* unrecoverable parse error: */
323 return result; /* ignore this instruction */
324 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000325 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326
327 if (result->opcode == I_INCBIN) {
328 /*
329 * Correct syntax for INCBIN is that there should be
330 * one string operand, followed by one or two numeric
331 * operands.
332 */
333 if (!result->eops || result->eops->type != EOT_DB_STRING)
334 error (ERR_NONFATAL, "`incbin' expects a file name");
335 else if (result->eops->next &&
336 result->eops->next->type != EOT_DB_NUMBER)
337 error (ERR_NONFATAL, "`incbin': second parameter is",
338 " non-numeric");
339 else if (result->eops->next && result->eops->next->next &&
340 result->eops->next->next->type != EOT_DB_NUMBER)
341 error (ERR_NONFATAL, "`incbin': third parameter is",
342 " non-numeric");
343 else if (result->eops->next && result->eops->next->next &&
344 result->eops->next->next->next)
345 error (ERR_NONFATAL, "`incbin': more than three parameters");
346 else
347 return result;
348 /*
349 * If we reach here, one of the above errors happened.
350 * Throw the instruction away.
351 */
352 result->opcode = -1;
353 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354 } else /* DB ... */
355 if (oper_num == 0)
356 error (ERR_WARNING|ERR_PASS1,
357 "no operand for data declaration");
358 else
359 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000361 return result;
362 }
363
364 /* right. Now we begin to parse the operands. There may be up to three
365 * of these, separated by commas, and terminated by a zero token. */
366
367 for (operand = 0; operand < 3; operand++) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 expr *value; /* used most of the time */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000369 int mref; /* is this going to be a memory ref? */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000370 int bracket; /* is it a [] mref, or a & mref? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000371 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000372
373 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374 result->oprs[operand].eaflags = 0; /* and this */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000375 result->oprs[operand].opflags = 0;
376
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000378 if (i == 0) break; /* end of operands: get out of here */
379 result->oprs[operand].type = 0; /* so far, no override */
380 while (i == TOKEN_SPECIAL) {/* size specifiers */
381 switch ((int)tokval.t_integer) {
382 case S_BYTE:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383 if (!setsize) /* we want to use only the first */
384 result->oprs[operand].type |= BITS8;
385 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000386 break;
387 case S_WORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388 if (!setsize)
389 result->oprs[operand].type |= BITS16;
390 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391 break;
392 case S_DWORD:
393 case S_LONG:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000394 if (!setsize)
395 result->oprs[operand].type |= BITS32;
396 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397 break;
398 case S_QWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399 if (!setsize)
400 result->oprs[operand].type |= BITS64;
401 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000402 break;
403 case S_TWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000404 if (!setsize)
405 result->oprs[operand].type |= BITS80;
406 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407 break;
408 case S_TO:
409 result->oprs[operand].type |= TO;
410 break;
H. Peter Anvin01377d82002-05-21 03:16:33 +0000411 case S_STRICT:
412 result->oprs[operand].type |= STRICT;
413 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414 case S_FAR:
415 result->oprs[operand].type |= FAR;
416 break;
417 case S_NEAR:
418 result->oprs[operand].type |= NEAR;
419 break;
420 case S_SHORT:
421 result->oprs[operand].type |= SHORT;
422 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000423 default:
424 error (ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000427 }
428
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000429 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000430 mref = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000431 bracket = (i == '[');
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000432 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433 if (i == TOKEN_SPECIAL) { /* check for address size override */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000434 if (tasm_compatible_mode) {
435 switch ((int)tokval.t_integer) {
436 /* For TASM compatibility a size override inside the
437 * brackets changes the size of the operand, not the
438 * address type of the operand as it does in standard
439 * NASM syntax. Hence:
440 *
441 * mov eax,[DWORD val]
442 *
443 * is valid syntax in TASM compatibility mode. Note that
444 * you lose the ability to override the default address
445 * type for the instruction, but we never use anything
446 * but 32-bit flat model addressing in our code.
447 */
448 case S_BYTE:
449 result->oprs[operand].type |= BITS8;
450 break;
451 case S_WORD:
452 result->oprs[operand].type |= BITS16;
453 break;
454 case S_DWORD:
455 case S_LONG:
456 result->oprs[operand].type |= BITS32;
457 break;
458 case S_QWORD:
459 result->oprs[operand].type |= BITS64;
460 break;
461 case S_TWORD:
462 result->oprs[operand].type |= BITS80;
463 break;
464 default:
465 error (ERR_NONFATAL, "invalid operand size specification");
466 }
467 } else {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000468 /* Standard NASM compatible syntax */
469 switch ((int)tokval.t_integer) {
470 case S_NOSPLIT:
471 result->oprs[operand].eaflags |= EAF_TIMESTWO;
472 break;
473 case S_BYTE:
474 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
475 break;
476 case S_WORD:
477 result->oprs[operand].addr_size = 16;
478 result->oprs[operand].eaflags |= EAF_WORDOFFS;
479 break;
480 case S_DWORD:
481 case S_LONG:
482 result->oprs[operand].addr_size = 32;
483 result->oprs[operand].eaflags |= EAF_WORDOFFS;
484 break;
485 default:
486 error (ERR_NONFATAL, "invalid size specification in"
487 " effective address");
488 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000489 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000490 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000492 } else { /* immediate operand, or register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493 mref = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494 bracket = FALSE; /* placate optimisers */
495 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000496
H. Peter Anvin76690a12002-04-30 20:52:49 +0000497 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000498 &result->oprs[operand].opflags,
499 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000500 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
502 result->forw_ref = TRUE;
503 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000504 if (!value) { /* error in evaluator */
505 result->opcode = -1; /* unrecoverable parse error: */
506 return result; /* ignore this instruction */
507 }
508 if (i == ':' && mref) { /* it was seg:offset */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509 /*
510 * Process the segment override.
511 */
512 if (value[1].type!=0 || value->value!=1 ||
513 REG_SREG & ~reg_flags[value->type])
514 error (ERR_NONFATAL, "invalid segment override");
515 else if (result->nprefix == MAXPREFIX)
516 error (ERR_NONFATAL,
517 "instruction has more than %d prefixes",
518 MAXPREFIX);
519 else
520 result->prefixes[result->nprefix++] = value->type;
521
522 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523 if (i == TOKEN_SPECIAL) { /* another check for size override */
524 switch ((int)tokval.t_integer) {
525 case S_WORD:
526 result->oprs[operand].addr_size = 16;
527 break;
528 case S_DWORD:
529 case S_LONG:
530 result->oprs[operand].addr_size = 32;
531 break;
532 default:
533 error (ERR_NONFATAL, "invalid size specification in"
534 " effective address");
535 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000536 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000537 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000538 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000539 &result->oprs[operand].opflags,
540 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000541 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000542 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
543 result->forw_ref = TRUE;
544 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000545 /* and get the offset */
546 if (!value) { /* but, error in evaluator */
547 result->opcode = -1; /* unrecoverable parse error: */
548 return result; /* ignore this instruction */
549 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000550 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000551 if (mref && bracket) { /* find ] at the end */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000552 if (i != ']') {
553 error (ERR_NONFATAL, "parser: expecting ]");
554 do { /* error recovery again */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000555 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000556 } while (i != 0 && i != ',');
557 } else /* we got the required ] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000558 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559 } else { /* immediate operand */
560 if (i != 0 && i != ',' && i != ':') {
561 error (ERR_NONFATAL, "comma or end of line expected");
562 do { /* error recovery */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000563 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000564 } while (i != 0 && i != ',');
565 } else if (i == ':') {
566 result->oprs[operand].type |= COLON;
567 }
568 }
569
570 /* now convert the exprs returned from evaluate() into operand
571 * descriptions... */
572
573 if (mref) { /* it's a memory reference */
574 expr *e = value;
575 int b, i, s; /* basereg, indexreg, scale */
576 long o; /* offset */
577
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000578 b = i = -1, o = s = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000579 result->oprs[operand].hintbase = hints.base;
580 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000581
H. Peter Anvin76690a12002-04-30 20:52:49 +0000582 if (e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000583 if (e->value == 1) /* in fact it can be basereg */
584 b = e->type;
585 else /* no, it has to be indexreg */
586 i = e->type, s = e->value;
587 e++;
588 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589 if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
590 {
591 if (b != -1) /* If the first was the base, ... */
592 i = e->type, s = e->value; /* second has to be indexreg */
593
594 else if (e->value != 1) /* If both want to be index */
595 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000596 error(ERR_NONFATAL, "beroset-p-592-invalid effective address");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597 result->opcode = -1;
598 return result;
599 }
600 else
601 b = e->type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000602 e++;
603 }
604 if (e->type != 0) { /* is there an offset? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605 if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
606 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000607 error (ERR_NONFATAL, "beroset-p-603-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000608 result->opcode = -1;
609 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610 }
611 else
612 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000613 if (e->type == EXPR_UNKNOWN) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614 o = 0; /* doesn't matter what */
615 result->oprs[operand].wrt = NO_SEG; /* nor this */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000616 result->oprs[operand].segment = NO_SEG; /* or this */
617 while (e->type) e++; /* go to the end of the line */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618 }
619 else
620 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000621 if (e->type == EXPR_SIMPLE) {
622 o = e->value;
623 e++;
624 }
625 if (e->type == EXPR_WRT) {
626 result->oprs[operand].wrt = e->value;
627 e++;
628 } else
629 result->oprs[operand].wrt = NO_SEG;
630 /*
631 * Look for a segment base type.
632 */
633 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000634 error (ERR_NONFATAL, "beroset-p-630-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000635 result->opcode = -1;
636 return result;
637 }
638 while (e->type && e->value == 0)
639 e++;
640 if (e->type && e->value != 1) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000641 error (ERR_NONFATAL, "beroset-p-637-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000642 result->opcode = -1;
643 return result;
644 }
645 if (e->type) {
646 result->oprs[operand].segment =
647 e->type - EXPR_SEGBASE;
648 e++;
649 } else
650 result->oprs[operand].segment = NO_SEG;
651 while (e->type && e->value == 0)
652 e++;
653 if (e->type) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654 error (ERR_NONFATAL, "beroset-p-650-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000655 result->opcode = -1;
656 return result;
657 }
H. Peter Anvinea838272002-04-30 20:51:53 +0000658 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000659 }
660 } else {
661 o = 0;
662 result->oprs[operand].wrt = NO_SEG;
663 result->oprs[operand].segment = NO_SEG;
664 }
665
666 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000667 error (ERR_NONFATAL, "beroset-p-663-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000668 result->opcode = -1;
669 return result;
670 }
671
672 result->oprs[operand].type |= MEMORY;
673 if (b==-1 && (i==-1 || s==0))
674 result->oprs[operand].type |= MEM_OFFS;
675 result->oprs[operand].basereg = b;
676 result->oprs[operand].indexreg = i;
677 result->oprs[operand].scale = s;
678 result->oprs[operand].offset = o;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000679 }
680 else /* it's not a memory reference */
681 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000682 if (is_just_unknown(value)) { /* it's immediate but unknown */
683 result->oprs[operand].type |= IMMEDIATE;
684 result->oprs[operand].offset = 0; /* don't care */
685 result->oprs[operand].segment = NO_SEG; /* don't care again */
686 result->oprs[operand].wrt = NO_SEG;/* still don't care */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687 }
688 else if (is_reloc(value)) /* it's immediate */
689 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000690 result->oprs[operand].type |= IMMEDIATE;
691 result->oprs[operand].offset = reloc_value(value);
692 result->oprs[operand].segment = reloc_seg(value);
693 result->oprs[operand].wrt = reloc_wrt(value);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000694 if (is_simple(value)) {
695 if (reloc_value(value)==1)
696 result->oprs[operand].type |= UNITY;
H. Peter Anvin8c1da7b2002-05-22 20:45:09 +0000697 if (optimizing>=0 &&
698 !(result->oprs[operand].type & STRICT)) {
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000699 if (reloc_value(value) >= -128 &&
700 reloc_value(value) <= 127)
701 result->oprs[operand].type |= SBYTE;
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000702 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000703 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704 }
705 else /* it's a register */
706 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000707 if (value->type>=EXPR_SIMPLE || value->value!=1) {
708 error (ERR_NONFATAL, "invalid operand type");
709 result->opcode = -1;
710 return result;
711 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000712
713 /*
714 * check that its only 1 register, not an expression...
715 */
716 for (i = 1; value[i].type; i++)
717 if (value[i].value) {
718 error (ERR_NONFATAL, "invalid operand type");
719 result->opcode = -1;
720 return result;
721 }
722
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724 if (result->oprs[operand].type & ~TO) {
725 /*
726 * we want to produce a warning iff the specified size
727 * is different from the register size
728 */
729 i = result->oprs[operand].type & SIZE_MASK;
730 }
731 else
732 i = 0;
733
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000734 result->oprs[operand].type &= TO;
735 result->oprs[operand].type |= REGISTER;
736 result->oprs[operand].type |= reg_flags[value->type];
737 result->oprs[operand].basereg = value->type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000738
739 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
740 error (ERR_WARNING|ERR_PASS1,
741 "register size specification ignored");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000742 }
743 }
744 }
745
746 result->operands = operand; /* set operand count */
747
748 while (operand<3) /* clear remaining operands */
749 result->oprs[operand++].type = 0;
750
751 /*
752 * Transform RESW, RESD, RESQ, REST into RESB.
753 */
754 switch (result->opcode) {
755 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
756 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
757 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
758 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
759 }
760
761 return result;
762}
763
H. Peter Anvineba20a72002-04-30 20:53:55 +0000764static int is_comma_next (void)
765{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000767 int i;
768 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000769
H. Peter Anvin76690a12002-04-30 20:52:49 +0000770 p = stdscan_bufptr;
771 i = stdscan (NULL, &tv);
772 stdscan_bufptr = p;
773 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000774}
775
H. Peter Anvineba20a72002-04-30 20:53:55 +0000776void cleanup_insn (insn *i)
777{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000778 extop *e;
779
780 while (i->eops) {
781 e = i->eops;
782 i->eops = i->eops->next;
783 nasm_free (e);
784 }
785}