blob: fad755e88f39e861ea615010b8ec0364aaa65a80 [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
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <stddef.h>
16#include <string.h>
17#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000018#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000019
20#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000021#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000023#include "stdscan.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000024#include "parser.h"
25#include "float.h"
26
H. Peter Anvine2c80182005-01-15 22:15:51 +000027extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000028extern int32_t abs_seg; /* ABSOLUTE segment */
29extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000030
H. Peter Anvine2c80182005-01-15 22:15:51 +000031#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032
H. Peter Anvine2c80182005-01-15 22:15:51 +000033static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035static int i;
36static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000038static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvin12e46512007-10-03 21:30:57 -070039static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000040
H. Peter Anvin12e46512007-10-03 21:30:57 -070041void parser_global_info(struct ofmt *output, struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000042{
43 outfmt = output;
44 location = locp;
45}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070047static int prefix_slot(enum prefixes prefix)
48{
49 switch (prefix) {
50 case R_CS:
51 case R_DS:
52 case R_SS:
53 case R_ES:
54 case R_FS:
55 case R_GS:
56 return PPS_SEG;
57 case P_LOCK:
58 case P_REP:
59 case P_REPE:
60 case P_REPZ:
61 case P_REPNE:
62 case P_REPNZ:
63 return PPS_LREP;
64 case P_O16:
65 case P_O32:
66 case P_O64:
67 case P_OSP:
68 return PPS_OSIZE;
69 case P_A16:
70 case P_A32:
71 case P_A64:
72 case P_ASP:
73 return PPS_ASIZE;
74 default:
75 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
76 return -1;
77 }
78}
79
80static void process_size_override(insn * result, int operand)
81{
82 if (tasm_compatible_mode) {
83 switch ((int)tokval.t_integer) {
84 /* For TASM compatibility a size override inside the
85 * brackets changes the size of the operand, not the
86 * address type of the operand as it does in standard
87 * NASM syntax. Hence:
88 *
89 * mov eax,[DWORD val]
90 *
91 * is valid syntax in TASM compatibility mode. Note that
92 * you lose the ability to override the default address
93 * type for the instruction, but we never use anything
94 * but 32-bit flat model addressing in our code.
95 */
96 case S_BYTE:
97 result->oprs[operand].type |= BITS8;
98 break;
99 case S_WORD:
100 result->oprs[operand].type |= BITS16;
101 break;
102 case S_DWORD:
103 case S_LONG:
104 result->oprs[operand].type |= BITS32;
105 break;
106 case S_QWORD:
107 result->oprs[operand].type |= BITS64;
108 break;
109 case S_TWORD:
110 result->oprs[operand].type |= BITS80;
111 break;
112 case S_OWORD:
113 result->oprs[operand].type |= BITS128;
114 break;
115 default:
116 error(ERR_NONFATAL,
117 "invalid operand size specification");
118 break;
119 }
120 } else {
121 /* Standard NASM compatible syntax */
122 switch ((int)tokval.t_integer) {
123 case S_NOSPLIT:
124 result->oprs[operand].eaflags |= EAF_TIMESTWO;
125 break;
126 case S_REL:
127 result->oprs[operand].eaflags |= EAF_REL;
128 break;
129 case S_ABS:
130 result->oprs[operand].eaflags |= EAF_ABS;
131 break;
132 case S_BYTE:
133 result->oprs[operand].disp_size = 8;
134 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
135 break;
136 case P_A16:
137 case P_A32:
138 case P_A64:
139 if (result->prefixes[PPS_ASIZE] &&
140 result->prefixes[PPS_ASIZE] != tokval.t_integer)
141 error(ERR_NONFATAL,
142 "conflicting address size specifications");
143 else
144 result->prefixes[PPS_ASIZE] = tokval.t_integer;
145 break;
146 case S_WORD:
147 result->oprs[operand].disp_size = 16;
148 result->oprs[operand].eaflags |= EAF_WORDOFFS;
149 break;
150 case S_DWORD:
151 case S_LONG:
152 result->oprs[operand].disp_size = 32;
153 result->oprs[operand].eaflags |= EAF_WORDOFFS;
154 break;
155 case S_QWORD:
156 result->oprs[operand].disp_size = 64;
157 result->oprs[operand].eaflags |= EAF_WORDOFFS;
158 break;
159 default:
160 error(ERR_NONFATAL, "invalid size specification in"
161 " effective address");
162 break;
163 }
164 }
165}
166
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000167insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000168 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000169{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 int operand;
171 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000172 struct eval_hints hints;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700173 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700175 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin76690a12002-04-30 20:52:49 +0000178 stdscan_reset();
179 stdscan_bufptr = buffer;
180 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 result->label = NULL; /* Assume no label */
183 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000184 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvine2c80182005-01-15 22:15:51 +0000186 if (i == 0) { /* blank line - ignore */
187 result->opcode = -1; /* and no instruction either */
188 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189 }
190 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
192 error(ERR_NONFATAL, "label or instruction expected"
193 " at start of line");
194 result->opcode = -1;
195 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000196 }
197
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 if (i == TOKEN_ID) { /* there's a label here */
199 result->label = tokval.t_charptr;
200 i = stdscan(NULL, &tokval);
201 if (i == ':') { /* skip over the optional colon */
202 i = stdscan(NULL, &tokval);
203 } else if (i == 0) {
204 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
205 "label alone on a line without a colon might be in error");
206 }
207 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
208 /*
209 * FIXME: location->segment could be NO_SEG, in which case
210 * it is possible we should be passing 'abs_seg'. Look into this.
211 * Work out whether that is *really* what we should be doing.
212 * Generally fix things. I think this is right as it is, but
213 * am still not certain.
214 */
215 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700216 location->offset, NULL, true, false, outfmt, errfunc);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000218 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 if (i == 0) {
221 result->opcode = -1; /* this line contains just a label */
222 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000223 }
224
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700225 for (j = 0; j < MAXPREFIX; j++)
226 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000227 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000228
229 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000231 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 /*
233 * Handle special case: the TIMES prefix.
234 */
235 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
236 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 i = stdscan(NULL, &tokval);
239 value =
240 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
241 i = tokval.t_type;
242 if (!value) { /* but, error in evaluator */
243 result->opcode = -1; /* unrecoverable parse error: */
244 return result; /* ignore this instruction */
245 }
246 if (!is_simple(value)) {
247 error(ERR_NONFATAL,
248 "non-constant argument supplied to TIMES");
249 result->times = 1L;
250 } else {
251 result->times = value->value;
252 if (value->value < 0) {
253 error(ERR_NONFATAL, "TIMES value %d is negative",
254 value->value);
255 result->times = 0;
256 }
257 }
258 } else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 int slot = prefix_slot(tokval.t_integer);
260 if (result->prefixes[slot]) {
261 error(ERR_NONFATAL,
262 "instruction has conflicting prefixes");
263 }
264 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 i = stdscan(NULL, &tokval);
266 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267 }
268
269 if (i != TOKEN_INSN) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700270 int j;
271 enum prefixes pfx;
272
273 for (j = 0; j < MAXPREFIX; j++)
274 if ((pfx = result->prefixes[j]) != P_none)
275 break;
276
277 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000278 /*
279 * Instruction prefixes are present, but no actual
280 * instruction. This is allowed: at this point we
281 * invent a notional instruction of RESB 0.
282 */
283 result->opcode = I_RESB;
284 result->operands = 1;
285 result->oprs[0].type = IMMEDIATE;
286 result->oprs[0].offset = 0L;
287 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
288 return result;
289 } else {
290 error(ERR_NONFATAL, "parser: instruction expected");
291 result->opcode = -1;
292 return result;
293 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000294 }
295
296 result->opcode = tokval.t_integer;
297 result->condition = tokval.t_inttwo;
298
299 /*
300 * RESB, RESW and RESD cannot be satisfied with incorrectly
301 * evaluated operands, since the correct values _must_ be known
302 * on the first pass. Hence, even in pass one, we set the
303 * `critical' flag on calling evaluate(), so that it will bomb
304 * out on undefined symbols. Nasty, but there's nothing we can
305 * do about it.
306 *
307 * For the moment, EQU has the same difficulty, so we'll
308 * include that.
309 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700310 if (result->opcode == I_RESB || result->opcode == I_RESW ||
311 result->opcode == I_RESD || result->opcode == I_RESQ ||
312 result->opcode == I_REST || result->opcode == I_RESO ||
313 result->opcode == I_EQU || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314 critical = pass0;
315 } else
316 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000317
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700318 if (result->opcode == I_DB || result->opcode == I_DW ||
319 result->opcode == I_DD || result->opcode == I_DQ ||
320 result->opcode == I_DT || result->opcode == I_DO ||
321 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 extop *eop, **tail = &result->eops, **fixptr;
323 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000324
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700325 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700328 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 */
330 while (1) {
331 i = stdscan(NULL, &tokval);
332 if (i == 0)
333 break;
334 fixptr = tail;
335 eop = *tail = nasm_malloc(sizeof(extop));
336 tail = &eop->next;
337 eop->next = NULL;
338 eop->type = EOT_NOTHING;
339 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
342 eop->type = EOT_DB_STRING;
343 eop->stringval = tokval.t_charptr;
344 eop->stringlen = tokval.t_inttwo;
345 i = stdscan(NULL, &tokval); /* eat the comma */
346 continue;
347 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000348
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700349 if ((i == TOKEN_FLOAT && is_comma_next())
350 || i == '-' || i == '+') {
351 int32_t sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700353 if (i == '+' || i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000354 char *save = stdscan_bufptr;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700355 int token = i;
356 sign = (i == '-') ? -1 : 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000357 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 if (i != TOKEN_FLOAT || !is_comma_next()) {
359 stdscan_bufptr = save;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700360 i = tokval.t_type = token;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 }
362 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 if (i == TOKEN_FLOAT) {
365 eop->type = EOT_DB_STRING;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700366 result->eops_float = true;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700367 switch (result->opcode) {
368 case I_DW:
369 eop->stringlen = 2;
370 break;
371 case I_DD:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 eop->stringlen = 4;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700373 break;
374 case I_DQ:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 eop->stringlen = 8;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700376 break;
377 case I_DT:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 eop->stringlen = 10;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700379 break;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700380 case I_DO:
381 eop->stringlen = 16;
382 break;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700383 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700385 " encountered in `db' instruction");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 /*
387 * fix suggested by Pedro Gimeno... original line
388 * was:
389 * eop->type = EOT_NOTHING;
390 */
391 eop->stringlen = 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700392 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 }
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700394 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 tail = &eop->next;
396 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000397 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700398 if (!eop->stringlen ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000400 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 eop->stringlen, error))
402 eop->type = EOT_NOTHING;
403 i = stdscan(NULL, &tokval); /* eat the comma */
404 continue;
405 }
406 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 /* anything else */
409 {
410 expr *value;
411 value = evaluate(stdscan, NULL, &tokval, NULL,
412 critical, error, NULL);
413 i = tokval.t_type;
414 if (!value) { /* error in evaluator */
415 result->opcode = -1; /* unrecoverable parse error: */
416 return result; /* ignore this instruction */
417 }
418 if (is_unknown(value)) {
419 eop->type = EOT_DB_NUMBER;
420 eop->offset = 0; /* doesn't matter what we put */
421 eop->segment = eop->wrt = NO_SEG; /* likewise */
422 } else if (is_reloc(value)) {
423 eop->type = EOT_DB_NUMBER;
424 eop->offset = reloc_value(value);
425 eop->segment = reloc_seg(value);
426 eop->wrt = reloc_wrt(value);
427 } else {
428 error(ERR_NONFATAL,
429 "operand %d: expression is not simple"
430 " or relocatable", oper_num);
431 }
432 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000433
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 /*
435 * We're about to call stdscan(), which will eat the
436 * comma that we're currently sitting on between
437 * arguments. However, we'd better check first that it
438 * _is_ a comma.
439 */
440 if (i == 0) /* also could be EOL */
441 break;
442 if (i != ',') {
443 error(ERR_NONFATAL, "comma expected after operand %d",
444 oper_num);
445 result->opcode = -1; /* unrecoverable parse error: */
446 return result; /* ignore this instruction */
447 }
448 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 if (result->opcode == I_INCBIN) {
451 /*
452 * Correct syntax for INCBIN is that there should be
453 * one string operand, followed by one or two numeric
454 * operands.
455 */
456 if (!result->eops || result->eops->type != EOT_DB_STRING)
457 error(ERR_NONFATAL, "`incbin' expects a file name");
458 else if (result->eops->next &&
459 result->eops->next->type != EOT_DB_NUMBER)
460 error(ERR_NONFATAL, "`incbin': second parameter is",
461 " non-numeric");
462 else if (result->eops->next && result->eops->next->next &&
463 result->eops->next->next->type != EOT_DB_NUMBER)
464 error(ERR_NONFATAL, "`incbin': third parameter is",
465 " non-numeric");
466 else if (result->eops->next && result->eops->next->next &&
467 result->eops->next->next->next)
468 error(ERR_NONFATAL,
469 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000470 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 return result;
472 /*
473 * If we reach here, one of the above errors happened.
474 * Throw the instruction away.
475 */
476 result->opcode = -1;
477 return result;
478 } else /* DB ... */ if (oper_num == 0)
479 error(ERR_WARNING | ERR_PASS1,
480 "no operand for data declaration");
481 else
482 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000483
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485 }
486
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700487 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488 * of these, separated by commas, and terminated by a zero token. */
489
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700490 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700491 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 int mref; /* is this going to be a memory ref? */
493 int bracket; /* is it a [] mref, or a & mref? */
494 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700496 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 result->oprs[operand].eaflags = 0; /* and this */
498 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 i = stdscan(NULL, &tokval);
501 if (i == 0)
502 break; /* end of operands: get out of here */
503 result->oprs[operand].type = 0; /* so far, no override */
504 while (i == TOKEN_SPECIAL) { /* size specifiers */
505 switch ((int)tokval.t_integer) {
506 case S_BYTE:
507 if (!setsize) /* we want to use only the first */
508 result->oprs[operand].type |= BITS8;
509 setsize = 1;
510 break;
511 case S_WORD:
512 if (!setsize)
513 result->oprs[operand].type |= BITS16;
514 setsize = 1;
515 break;
516 case S_DWORD:
517 case S_LONG:
518 if (!setsize)
519 result->oprs[operand].type |= BITS32;
520 setsize = 1;
521 break;
522 case S_QWORD:
523 if (!setsize)
524 result->oprs[operand].type |= BITS64;
525 setsize = 1;
526 break;
527 case S_TWORD:
528 if (!setsize)
529 result->oprs[operand].type |= BITS80;
530 setsize = 1;
531 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700532 case S_OWORD:
533 if (!setsize)
534 result->oprs[operand].type |= BITS128;
535 setsize = 1;
536 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 case S_TO:
538 result->oprs[operand].type |= TO;
539 break;
540 case S_STRICT:
541 result->oprs[operand].type |= STRICT;
542 break;
543 case S_FAR:
544 result->oprs[operand].type |= FAR;
545 break;
546 case S_NEAR:
547 result->oprs[operand].type |= NEAR;
548 break;
549 case S_SHORT:
550 result->oprs[operand].type |= SHORT;
551 break;
552 default:
553 error(ERR_NONFATAL, "invalid operand size specification");
554 }
555 i = stdscan(NULL, &tokval);
556 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700559 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700561 i = stdscan(NULL, &tokval); /* then skip the colon */
562 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
563 process_size_override(result, operand);
564 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 }
566 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700567 mref = false;
568 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if ((result->oprs[operand].type & FAR) && !mref &&
572 result->opcode != I_JMP && result->opcode != I_CALL) {
573 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
574 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000575
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 value = evaluate(stdscan, NULL, &tokval,
577 &result->oprs[operand].opflags,
578 critical, error, &hints);
579 i = tokval.t_type;
580 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700581 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 }
583 if (!value) { /* error in evaluator */
584 result->opcode = -1; /* unrecoverable parse error: */
585 return result; /* ignore this instruction */
586 }
587 if (i == ':' && mref) { /* it was seg:offset */
588 /*
589 * Process the segment override.
590 */
591 if (value[1].type != 0 || value->value != 1 ||
592 REG_SREG & ~reg_flags[value->type])
593 error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700594 else if (result->prefixes[PPS_SEG])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700596 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000597 else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700598 result->prefixes[PPS_SEG] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000599 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000600 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000601 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000602
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700604 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
605 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 i = stdscan(NULL, &tokval);
607 }
608 value = evaluate(stdscan, NULL, &tokval,
609 &result->oprs[operand].opflags,
610 critical, error, &hints);
611 i = tokval.t_type;
612 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700613 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 }
615 /* and get the offset */
616 if (!value) { /* but, error in evaluator */
617 result->opcode = -1; /* unrecoverable parse error: */
618 return result; /* ignore this instruction */
619 }
620 }
621 if (mref && bracket) { /* find ] at the end */
622 if (i != ']') {
623 error(ERR_NONFATAL, "parser: expecting ]");
624 do { /* error recovery again */
625 i = stdscan(NULL, &tokval);
626 } while (i != 0 && i != ',');
627 } else /* we got the required ] */
628 i = stdscan(NULL, &tokval);
629 } else { /* immediate operand */
630 if (i != 0 && i != ',' && i != ':') {
631 error(ERR_NONFATAL, "comma or end of line expected");
632 do { /* error recovery */
633 i = stdscan(NULL, &tokval);
634 } while (i != 0 && i != ',');
635 } else if (i == ':') {
636 result->oprs[operand].type |= COLON;
637 }
638 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000639
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 /* now convert the exprs returned from evaluate() into operand
641 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 if (mref) { /* it's a memory reference */
644 expr *e = value;
645 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000646 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000647
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 b = i = -1, o = s = 0;
649 result->oprs[operand].hintbase = hints.base;
650 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
653 if (e->value == 1) /* in fact it can be basereg */
654 b = e->type;
655 else /* no, it has to be indexreg */
656 i = e->type, s = e->value;
657 e++;
658 }
659 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
660 if (b != -1) /* If the first was the base, ... */
661 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 else if (e->value != 1) { /* If both want to be index */
664 error(ERR_NONFATAL,
665 "beroset-p-592-invalid effective address");
666 result->opcode = -1;
667 return result;
668 } else
669 b = e->type;
670 e++;
671 }
672 if (e->type != 0) { /* is there an offset? */
673 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
674 error(ERR_NONFATAL,
675 "beroset-p-603-invalid effective address");
676 result->opcode = -1;
677 return result;
678 } else {
679 if (e->type == EXPR_UNKNOWN) {
680 o = 0; /* doesn't matter what */
681 result->oprs[operand].wrt = NO_SEG; /* nor this */
682 result->oprs[operand].segment = NO_SEG; /* or this */
683 while (e->type)
684 e++; /* go to the end of the line */
685 } else {
686 if (e->type == EXPR_SIMPLE) {
687 o = e->value;
688 e++;
689 }
690 if (e->type == EXPR_WRT) {
691 result->oprs[operand].wrt = e->value;
692 e++;
693 } else
694 result->oprs[operand].wrt = NO_SEG;
695 /*
696 * Look for a segment base type.
697 */
698 if (e->type && e->type < EXPR_SEGBASE) {
699 error(ERR_NONFATAL,
700 "beroset-p-630-invalid effective address");
701 result->opcode = -1;
702 return result;
703 }
704 while (e->type && e->value == 0)
705 e++;
706 if (e->type && e->value != 1) {
707 error(ERR_NONFATAL,
708 "beroset-p-637-invalid effective address");
709 result->opcode = -1;
710 return result;
711 }
712 if (e->type) {
713 result->oprs[operand].segment =
714 e->type - EXPR_SEGBASE;
715 e++;
716 } else
717 result->oprs[operand].segment = NO_SEG;
718 while (e->type && e->value == 0)
719 e++;
720 if (e->type) {
721 error(ERR_NONFATAL,
722 "beroset-p-650-invalid effective address");
723 result->opcode = -1;
724 return result;
725 }
726 }
727 }
728 } else {
729 o = 0;
730 result->oprs[operand].wrt = NO_SEG;
731 result->oprs[operand].segment = NO_SEG;
732 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 if (e->type != 0) { /* there'd better be nothing left! */
735 error(ERR_NONFATAL,
736 "beroset-p-663-invalid effective address");
737 result->opcode = -1;
738 return result;
739 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700741 /* It is memory, but it can match any r/m operand */
742 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000743
744 if (b == -1 && (i == -1 || s == 0)) {
745 int is_rel = globalbits == 64 &&
746 !(result->oprs[operand].eaflags & EAF_ABS) &&
747 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000748 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000749 (result->oprs[operand].eaflags & EAF_REL));
750
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700751 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000752 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 result->oprs[operand].basereg = b;
754 result->oprs[operand].indexreg = i;
755 result->oprs[operand].scale = s;
756 result->oprs[operand].offset = o;
757 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 if (is_just_unknown(value)) { /* it's immediate but unknown */
760 result->oprs[operand].type |= IMMEDIATE;
761 result->oprs[operand].offset = 0; /* don't care */
762 result->oprs[operand].segment = NO_SEG; /* don't care again */
763 result->oprs[operand].wrt = NO_SEG; /* still don't care */
764 } else if (is_reloc(value)) { /* it's immediate */
765 result->oprs[operand].type |= IMMEDIATE;
766 result->oprs[operand].offset = reloc_value(value);
767 result->oprs[operand].segment = reloc_seg(value);
768 result->oprs[operand].wrt = reloc_wrt(value);
769 if (is_simple(value)) {
770 if (reloc_value(value) == 1)
771 result->oprs[operand].type |= UNITY;
772 if (optimizing >= 0 &&
773 !(result->oprs[operand].type & STRICT)) {
774 if (reloc_value(value) >= -128 &&
775 reloc_value(value) <= 127)
776 result->oprs[operand].type |= SBYTE;
777 }
778 }
779 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000780
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 if (value->type >= EXPR_SIMPLE || value->value != 1) {
782 error(ERR_NONFATAL, "invalid operand type");
783 result->opcode = -1;
784 return result;
785 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000786
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 /*
788 * check that its only 1 register, not an expression...
789 */
790 for (i = 1; value[i].type; i++)
791 if (value[i].value) {
792 error(ERR_NONFATAL, "invalid operand type");
793 result->opcode = -1;
794 return result;
795 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000796
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 /* clear overrides, except TO which applies to FPU regs */
798 if (result->oprs[operand].type & ~TO) {
799 /*
800 * we want to produce a warning iff the specified size
801 * is different from the register size
802 */
803 i = result->oprs[operand].type & SIZE_MASK;
804 } else
805 i = 0;
806
807 result->oprs[operand].type &= TO;
808 result->oprs[operand].type |= REGISTER;
809 result->oprs[operand].type |= reg_flags[value->type];
810 result->oprs[operand].basereg = value->type;
811
812 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
813 error(ERR_WARNING | ERR_PASS1,
814 "register size specification ignored");
815 }
816 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000817 }
818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000820
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700821/* clear remaining operands */
822while (operand < MAX_OPERANDS)
823 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824
825 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700826 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000827 */
828 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 case I_RESW:
830 result->opcode = I_RESB;
831 result->oprs[0].offset *= 2;
832 break;
833 case I_RESD:
834 result->opcode = I_RESB;
835 result->oprs[0].offset *= 4;
836 break;
837 case I_RESQ:
838 result->opcode = I_RESB;
839 result->oprs[0].offset *= 8;
840 break;
841 case I_REST:
842 result->opcode = I_RESB;
843 result->oprs[0].offset *= 10;
844 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700845 case I_RESO:
846 result->opcode = I_RESB;
847 result->oprs[0].offset *= 16;
848 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700849 default:
850 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000851 }
852
853 return result;
854}
855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000857{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000858 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000859 int i;
860 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000861
H. Peter Anvin76690a12002-04-30 20:52:49 +0000862 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000864 stdscan_bufptr = p;
865 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000866}
867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000869{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000870 extop *e;
871
872 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 e = i->eops;
874 i->eops = i->eops->next;
875 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000876 }
877}