blob: 31c3612ae42d5381a2ff1f6b383402168aa46d3d [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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000016#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000017
18#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000019#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000021#include "stdscan.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "parser.h"
23#include "float.h"
24
H. Peter Anvine2c80182005-01-15 22:15:51 +000025extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000026extern int32_t abs_seg; /* ABSOLUTE segment */
27extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000028
H. Peter Anvine2c80182005-01-15 22:15:51 +000029#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000030
H. Peter Anvine2c80182005-01-15 22:15:51 +000031static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033static int i;
34static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000036static struct ofmt *outfmt; /* Structure of addresses of output routines */
37static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000038
H. Peter Anvine2c80182005-01-15 22:15:51 +000039void parser_global_info(struct ofmt *output, loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000040{
41 outfmt = output;
42 location = locp;
43}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044
Keith Kaniosa6dfa782007-04-13 16:47:53 +000045insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +000046 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +000047{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048 int operand;
49 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000050 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051
H. Peter Anvin76690a12002-04-30 20:52:49 +000052 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055 stdscan_reset();
56 stdscan_bufptr = buffer;
57 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
H. Peter Anvine2c80182005-01-15 22:15:51 +000059 result->label = NULL; /* Assume no label */
60 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +000061 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 if (i == 0) { /* blank line - ignore */
64 result->opcode = -1; /* and no instruction either */
65 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066 }
67 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +000068 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
69 error(ERR_NONFATAL, "label or instruction expected"
70 " at start of line");
71 result->opcode = -1;
72 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073 }
74
H. Peter Anvine2c80182005-01-15 22:15:51 +000075 if (i == TOKEN_ID) { /* there's a label here */
76 result->label = tokval.t_charptr;
77 i = stdscan(NULL, &tokval);
78 if (i == ':') { /* skip over the optional colon */
79 i = stdscan(NULL, &tokval);
80 } else if (i == 0) {
81 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
82 "label alone on a line without a colon might be in error");
83 }
84 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
85 /*
86 * FIXME: location->segment could be NO_SEG, in which case
87 * it is possible we should be passing 'abs_seg'. Look into this.
88 * Work out whether that is *really* what we should be doing.
89 * Generally fix things. I think this is right as it is, but
90 * am still not certain.
91 */
92 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
93 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
94 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000095 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 if (i == 0) {
98 result->opcode = -1; /* this line contains just a label */
99 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 }
101
102 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104
105 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000107 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 /*
109 * Handle special case: the TIMES prefix.
110 */
111 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
112 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114 i = stdscan(NULL, &tokval);
115 value =
116 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
117 i = tokval.t_type;
118 if (!value) { /* but, error in evaluator */
119 result->opcode = -1; /* unrecoverable parse error: */
120 return result; /* ignore this instruction */
121 }
122 if (!is_simple(value)) {
123 error(ERR_NONFATAL,
124 "non-constant argument supplied to TIMES");
125 result->times = 1L;
126 } else {
127 result->times = value->value;
128 if (value->value < 0) {
129 error(ERR_NONFATAL, "TIMES value %d is negative",
130 value->value);
131 result->times = 0;
132 }
133 }
134 } else {
135 if (result->nprefix == MAXPREFIX)
136 error(ERR_NONFATAL,
137 "instruction has more than %d prefixes", MAXPREFIX);
138 else
139 result->prefixes[result->nprefix++] = tokval.t_integer;
140 i = stdscan(NULL, &tokval);
141 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142 }
143
144 if (i != TOKEN_INSN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145 if (result->nprefix > 0 && i == 0) {
146 /*
147 * Instruction prefixes are present, but no actual
148 * instruction. This is allowed: at this point we
149 * invent a notional instruction of RESB 0.
150 */
151 result->opcode = I_RESB;
152 result->operands = 1;
153 result->oprs[0].type = IMMEDIATE;
154 result->oprs[0].offset = 0L;
155 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
156 return result;
157 } else {
158 error(ERR_NONFATAL, "parser: instruction expected");
159 result->opcode = -1;
160 return result;
161 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000162 }
163
164 result->opcode = tokval.t_integer;
165 result->condition = tokval.t_inttwo;
166
167 /*
168 * RESB, RESW and RESD cannot be satisfied with incorrectly
169 * evaluated operands, since the correct values _must_ be known
170 * on the first pass. Hence, even in pass one, we set the
171 * `critical' flag on calling evaluate(), so that it will bomb
172 * out on undefined symbols. Nasty, but there's nothing we can
173 * do about it.
174 *
175 * For the moment, EQU has the same difficulty, so we'll
176 * include that.
177 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700178 if (result->opcode == I_RESB || result->opcode == I_RESW ||
179 result->opcode == I_RESD || result->opcode == I_RESQ ||
180 result->opcode == I_REST || result->opcode == I_RESO ||
181 result->opcode == I_EQU || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 critical = pass0;
183 } else
184 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700186 if (result->opcode == I_DB || result->opcode == I_DW ||
187 result->opcode == I_DD || result->opcode == I_DQ ||
188 result->opcode == I_DT || result->opcode == I_DO ||
189 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 extop *eop, **tail = &result->eops, **fixptr;
191 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700196 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 */
198 while (1) {
199 i = stdscan(NULL, &tokval);
200 if (i == 0)
201 break;
202 fixptr = tail;
203 eop = *tail = nasm_malloc(sizeof(extop));
204 tail = &eop->next;
205 eop->next = NULL;
206 eop->type = EOT_NOTHING;
207 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
210 eop->type = EOT_DB_STRING;
211 eop->stringval = tokval.t_charptr;
212 eop->stringlen = tokval.t_inttwo;
213 i = stdscan(NULL, &tokval); /* eat the comma */
214 continue;
215 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700217 if ((i == TOKEN_FLOAT && is_comma_next())
218 || i == '-' || i == '+') {
219 int32_t sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700221 if (i == '+' || i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000222 char *save = stdscan_bufptr;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700223 int token = i;
224 sign = (i == '-') ? -1 : 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226 if (i != TOKEN_FLOAT || !is_comma_next()) {
227 stdscan_bufptr = save;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700228 i = tokval.t_type = token;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000229 }
230 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000231
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 if (i == TOKEN_FLOAT) {
233 eop->type = EOT_DB_STRING;
234 result->eops_float = TRUE;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700235 switch (result->opcode) {
236 case I_DW:
237 eop->stringlen = 2;
238 break;
239 case I_DD:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000240 eop->stringlen = 4;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700241 break;
242 case I_DQ:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243 eop->stringlen = 8;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700244 break;
245 case I_DT:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000246 eop->stringlen = 10;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700247 break;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700248 case I_DO:
249 eop->stringlen = 16;
250 break;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700251 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252 error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700253 " encountered in `db' instruction");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254 /*
255 * fix suggested by Pedro Gimeno... original line
256 * was:
257 * eop->type = EOT_NOTHING;
258 */
259 eop->stringlen = 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700260 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000261 }
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700262 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000263 tail = &eop->next;
264 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000265 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700266 if (!eop->stringlen ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000268 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269 eop->stringlen, error))
270 eop->type = EOT_NOTHING;
271 i = stdscan(NULL, &tokval); /* eat the comma */
272 continue;
273 }
274 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000275
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276 /* anything else */
277 {
278 expr *value;
279 value = evaluate(stdscan, NULL, &tokval, NULL,
280 critical, error, NULL);
281 i = tokval.t_type;
282 if (!value) { /* error in evaluator */
283 result->opcode = -1; /* unrecoverable parse error: */
284 return result; /* ignore this instruction */
285 }
286 if (is_unknown(value)) {
287 eop->type = EOT_DB_NUMBER;
288 eop->offset = 0; /* doesn't matter what we put */
289 eop->segment = eop->wrt = NO_SEG; /* likewise */
290 } else if (is_reloc(value)) {
291 eop->type = EOT_DB_NUMBER;
292 eop->offset = reloc_value(value);
293 eop->segment = reloc_seg(value);
294 eop->wrt = reloc_wrt(value);
295 } else {
296 error(ERR_NONFATAL,
297 "operand %d: expression is not simple"
298 " or relocatable", oper_num);
299 }
300 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000301
H. Peter Anvine2c80182005-01-15 22:15:51 +0000302 /*
303 * We're about to call stdscan(), which will eat the
304 * comma that we're currently sitting on between
305 * arguments. However, we'd better check first that it
306 * _is_ a comma.
307 */
308 if (i == 0) /* also could be EOL */
309 break;
310 if (i != ',') {
311 error(ERR_NONFATAL, "comma expected after operand %d",
312 oper_num);
313 result->opcode = -1; /* unrecoverable parse error: */
314 return result; /* ignore this instruction */
315 }
316 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000317
H. Peter Anvine2c80182005-01-15 22:15:51 +0000318 if (result->opcode == I_INCBIN) {
319 /*
320 * Correct syntax for INCBIN is that there should be
321 * one string operand, followed by one or two numeric
322 * operands.
323 */
324 if (!result->eops || result->eops->type != EOT_DB_STRING)
325 error(ERR_NONFATAL, "`incbin' expects a file name");
326 else if (result->eops->next &&
327 result->eops->next->type != EOT_DB_NUMBER)
328 error(ERR_NONFATAL, "`incbin': second parameter is",
329 " non-numeric");
330 else if (result->eops->next && result->eops->next->next &&
331 result->eops->next->next->type != EOT_DB_NUMBER)
332 error(ERR_NONFATAL, "`incbin': third parameter is",
333 " non-numeric");
334 else if (result->eops->next && result->eops->next->next &&
335 result->eops->next->next->next)
336 error(ERR_NONFATAL,
337 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000339 return result;
340 /*
341 * If we reach here, one of the above errors happened.
342 * Throw the instruction away.
343 */
344 result->opcode = -1;
345 return result;
346 } else /* DB ... */ if (oper_num == 0)
347 error(ERR_WARNING | ERR_PASS1,
348 "no operand for data declaration");
349 else
350 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 }
354
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700355 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356 * of these, separated by commas, and terminated by a zero token. */
357
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700358 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 expr *value; /* used most of the time */
360 int mref; /* is this going to be a memory ref? */
361 int bracket; /* is it a [] mref, or a & mref? */
362 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
365 result->oprs[operand].eaflags = 0; /* and this */
366 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000367
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 i = stdscan(NULL, &tokval);
369 if (i == 0)
370 break; /* end of operands: get out of here */
371 result->oprs[operand].type = 0; /* so far, no override */
372 while (i == TOKEN_SPECIAL) { /* size specifiers */
373 switch ((int)tokval.t_integer) {
374 case S_BYTE:
375 if (!setsize) /* we want to use only the first */
376 result->oprs[operand].type |= BITS8;
377 setsize = 1;
378 break;
379 case S_WORD:
380 if (!setsize)
381 result->oprs[operand].type |= BITS16;
382 setsize = 1;
383 break;
384 case S_DWORD:
385 case S_LONG:
386 if (!setsize)
387 result->oprs[operand].type |= BITS32;
388 setsize = 1;
389 break;
390 case S_QWORD:
391 if (!setsize)
392 result->oprs[operand].type |= BITS64;
393 setsize = 1;
394 break;
395 case S_TWORD:
396 if (!setsize)
397 result->oprs[operand].type |= BITS80;
398 setsize = 1;
399 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700400 case S_OWORD:
401 if (!setsize)
402 result->oprs[operand].type |= BITS128;
403 setsize = 1;
404 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 case S_TO:
406 result->oprs[operand].type |= TO;
407 break;
408 case S_STRICT:
409 result->oprs[operand].type |= STRICT;
410 break;
411 case S_FAR:
412 result->oprs[operand].type |= FAR;
413 break;
414 case S_NEAR:
415 result->oprs[operand].type |= NEAR;
416 break;
417 case S_SHORT:
418 result->oprs[operand].type |= SHORT;
419 break;
420 default:
421 error(ERR_NONFATAL, "invalid operand size specification");
422 }
423 i = stdscan(NULL, &tokval);
424 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 if (i == '[' || i == '&') { /* memory reference */
427 mref = TRUE;
428 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000429 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
430 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 if (tasm_compatible_mode) {
432 switch ((int)tokval.t_integer) {
433 /* For TASM compatibility a size override inside the
434 * brackets changes the size of the operand, not the
435 * address type of the operand as it does in standard
436 * NASM syntax. Hence:
437 *
438 * mov eax,[DWORD val]
439 *
440 * is valid syntax in TASM compatibility mode. Note that
441 * you lose the ability to override the default address
442 * type for the instruction, but we never use anything
443 * but 32-bit flat model addressing in our code.
444 */
445 case S_BYTE:
446 result->oprs[operand].type |= BITS8;
447 break;
448 case S_WORD:
449 result->oprs[operand].type |= BITS16;
450 break;
451 case S_DWORD:
452 case S_LONG:
453 result->oprs[operand].type |= BITS32;
454 break;
455 case S_QWORD:
456 result->oprs[operand].type |= BITS64;
457 break;
458 case S_TWORD:
459 result->oprs[operand].type |= BITS80;
460 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700461 case S_OWORD:
462 result->oprs[operand].type |= BITS128;
463 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464 default:
465 error(ERR_NONFATAL,
466 "invalid operand size specification");
467 }
468 } else {
469 /* Standard NASM compatible syntax */
470 switch ((int)tokval.t_integer) {
471 case S_NOSPLIT:
472 result->oprs[operand].eaflags |= EAF_TIMESTWO;
473 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000474 case S_REL:
475 result->oprs[operand].eaflags |= EAF_REL;
476 break;
477 case S_ABS:
478 result->oprs[operand].eaflags |= EAF_ABS;
479 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 case S_BYTE:
481 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
482 break;
483 case S_WORD:
484 result->oprs[operand].addr_size = 16;
485 result->oprs[operand].eaflags |= EAF_WORDOFFS;
486 break;
487 case S_DWORD:
488 case S_LONG:
489 result->oprs[operand].addr_size = 32;
490 result->oprs[operand].eaflags |= EAF_WORDOFFS;
491 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000492 case S_QWORD:
493 result->oprs[operand].addr_size = 64;
494 result->oprs[operand].eaflags |= EAF_WORDOFFS;
495 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 default:
497 error(ERR_NONFATAL, "invalid size specification in"
498 " effective address");
499 }
500 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 }
502 } else { /* immediate operand, or register */
503 mref = FALSE;
504 bracket = FALSE; /* placate optimisers */
505 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 if ((result->oprs[operand].type & FAR) && !mref &&
508 result->opcode != I_JMP && result->opcode != I_CALL) {
509 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
510 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000511
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 value = evaluate(stdscan, NULL, &tokval,
513 &result->oprs[operand].opflags,
514 critical, error, &hints);
515 i = tokval.t_type;
516 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
517 result->forw_ref = TRUE;
518 }
519 if (!value) { /* error in evaluator */
520 result->opcode = -1; /* unrecoverable parse error: */
521 return result; /* ignore this instruction */
522 }
523 if (i == ':' && mref) { /* it was seg:offset */
524 /*
525 * Process the segment override.
526 */
527 if (value[1].type != 0 || value->value != 1 ||
528 REG_SREG & ~reg_flags[value->type])
529 error(ERR_NONFATAL, "invalid segment override");
530 else if (result->nprefix == MAXPREFIX)
531 error(ERR_NONFATAL,
532 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000533 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000535 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000536 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000537 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 i = stdscan(NULL, &tokval); /* then skip the colon */
540 if (i == TOKEN_SPECIAL) { /* another check for size override */
541 switch ((int)tokval.t_integer) {
542 case S_WORD:
543 result->oprs[operand].addr_size = 16;
544 break;
545 case S_DWORD:
546 case S_LONG:
547 result->oprs[operand].addr_size = 32;
548 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000549 case S_QWORD:
550 result->oprs[operand].addr_size = 64;
551 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 default:
553 error(ERR_NONFATAL, "invalid size specification in"
554 " effective address");
555 }
556 i = stdscan(NULL, &tokval);
557 }
558 value = evaluate(stdscan, NULL, &tokval,
559 &result->oprs[operand].opflags,
560 critical, error, &hints);
561 i = tokval.t_type;
562 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
563 result->forw_ref = TRUE;
564 }
565 /* and get the offset */
566 if (!value) { /* but, error in evaluator */
567 result->opcode = -1; /* unrecoverable parse error: */
568 return result; /* ignore this instruction */
569 }
570 }
571 if (mref && bracket) { /* find ] at the end */
572 if (i != ']') {
573 error(ERR_NONFATAL, "parser: expecting ]");
574 do { /* error recovery again */
575 i = stdscan(NULL, &tokval);
576 } while (i != 0 && i != ',');
577 } else /* we got the required ] */
578 i = stdscan(NULL, &tokval);
579 } else { /* immediate operand */
580 if (i != 0 && i != ',' && i != ':') {
581 error(ERR_NONFATAL, "comma or end of line expected");
582 do { /* error recovery */
583 i = stdscan(NULL, &tokval);
584 } while (i != 0 && i != ',');
585 } else if (i == ':') {
586 result->oprs[operand].type |= COLON;
587 }
588 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000589
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 /* now convert the exprs returned from evaluate() into operand
591 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000592
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 if (mref) { /* it's a memory reference */
594 expr *e = value;
595 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000596 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 b = i = -1, o = s = 0;
599 result->oprs[operand].hintbase = hints.base;
600 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000601
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
603 if (e->value == 1) /* in fact it can be basereg */
604 b = e->type;
605 else /* no, it has to be indexreg */
606 i = e->type, s = e->value;
607 e++;
608 }
609 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
610 if (b != -1) /* If the first was the base, ... */
611 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 else if (e->value != 1) { /* If both want to be index */
614 error(ERR_NONFATAL,
615 "beroset-p-592-invalid effective address");
616 result->opcode = -1;
617 return result;
618 } else
619 b = e->type;
620 e++;
621 }
622 if (e->type != 0) { /* is there an offset? */
623 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
624 error(ERR_NONFATAL,
625 "beroset-p-603-invalid effective address");
626 result->opcode = -1;
627 return result;
628 } else {
629 if (e->type == EXPR_UNKNOWN) {
630 o = 0; /* doesn't matter what */
631 result->oprs[operand].wrt = NO_SEG; /* nor this */
632 result->oprs[operand].segment = NO_SEG; /* or this */
633 while (e->type)
634 e++; /* go to the end of the line */
635 } else {
636 if (e->type == EXPR_SIMPLE) {
637 o = e->value;
638 e++;
639 }
640 if (e->type == EXPR_WRT) {
641 result->oprs[operand].wrt = e->value;
642 e++;
643 } else
644 result->oprs[operand].wrt = NO_SEG;
645 /*
646 * Look for a segment base type.
647 */
648 if (e->type && e->type < EXPR_SEGBASE) {
649 error(ERR_NONFATAL,
650 "beroset-p-630-invalid effective address");
651 result->opcode = -1;
652 return result;
653 }
654 while (e->type && e->value == 0)
655 e++;
656 if (e->type && e->value != 1) {
657 error(ERR_NONFATAL,
658 "beroset-p-637-invalid effective address");
659 result->opcode = -1;
660 return result;
661 }
662 if (e->type) {
663 result->oprs[operand].segment =
664 e->type - EXPR_SEGBASE;
665 e++;
666 } else
667 result->oprs[operand].segment = NO_SEG;
668 while (e->type && e->value == 0)
669 e++;
670 if (e->type) {
671 error(ERR_NONFATAL,
672 "beroset-p-650-invalid effective address");
673 result->opcode = -1;
674 return result;
675 }
676 }
677 }
678 } else {
679 o = 0;
680 result->oprs[operand].wrt = NO_SEG;
681 result->oprs[operand].segment = NO_SEG;
682 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000683
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 if (e->type != 0) { /* there'd better be nothing left! */
685 error(ERR_NONFATAL,
686 "beroset-p-663-invalid effective address");
687 result->opcode = -1;
688 return result;
689 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000690
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700691 /* It is memory, but it can match any r/m operand */
692 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000693
694 if (b == -1 && (i == -1 || s == 0)) {
695 int is_rel = globalbits == 64 &&
696 !(result->oprs[operand].eaflags & EAF_ABS) &&
697 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000698 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000699 (result->oprs[operand].eaflags & EAF_REL));
700
701 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
702 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 result->oprs[operand].basereg = b;
704 result->oprs[operand].indexreg = i;
705 result->oprs[operand].scale = s;
706 result->oprs[operand].offset = o;
707 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 if (is_just_unknown(value)) { /* it's immediate but unknown */
710 result->oprs[operand].type |= IMMEDIATE;
711 result->oprs[operand].offset = 0; /* don't care */
712 result->oprs[operand].segment = NO_SEG; /* don't care again */
713 result->oprs[operand].wrt = NO_SEG; /* still don't care */
714 } else if (is_reloc(value)) { /* it's immediate */
715 result->oprs[operand].type |= IMMEDIATE;
716 result->oprs[operand].offset = reloc_value(value);
717 result->oprs[operand].segment = reloc_seg(value);
718 result->oprs[operand].wrt = reloc_wrt(value);
719 if (is_simple(value)) {
720 if (reloc_value(value) == 1)
721 result->oprs[operand].type |= UNITY;
722 if (optimizing >= 0 &&
723 !(result->oprs[operand].type & STRICT)) {
724 if (reloc_value(value) >= -128 &&
725 reloc_value(value) <= 127)
726 result->oprs[operand].type |= SBYTE;
727 }
728 }
729 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000730
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731 if (value->type >= EXPR_SIMPLE || value->value != 1) {
732 error(ERR_NONFATAL, "invalid operand type");
733 result->opcode = -1;
734 return result;
735 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 /*
738 * check that its only 1 register, not an expression...
739 */
740 for (i = 1; value[i].type; i++)
741 if (value[i].value) {
742 error(ERR_NONFATAL, "invalid operand type");
743 result->opcode = -1;
744 return result;
745 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 /* clear overrides, except TO which applies to FPU regs */
748 if (result->oprs[operand].type & ~TO) {
749 /*
750 * we want to produce a warning iff the specified size
751 * is different from the register size
752 */
753 i = result->oprs[operand].type & SIZE_MASK;
754 } else
755 i = 0;
756
757 result->oprs[operand].type &= TO;
758 result->oprs[operand].type |= REGISTER;
759 result->oprs[operand].type |= reg_flags[value->type];
760 result->oprs[operand].basereg = value->type;
761
762 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
763 error(ERR_WARNING | ERR_PASS1,
764 "register size specification ignored");
765 }
766 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000767 }
768
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000770
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 while (operand < 3) /* clear remaining operands */
772 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773
774 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700775 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776 */
777 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 case I_RESW:
779 result->opcode = I_RESB;
780 result->oprs[0].offset *= 2;
781 break;
782 case I_RESD:
783 result->opcode = I_RESB;
784 result->oprs[0].offset *= 4;
785 break;
786 case I_RESQ:
787 result->opcode = I_RESB;
788 result->oprs[0].offset *= 8;
789 break;
790 case I_REST:
791 result->opcode = I_RESB;
792 result->oprs[0].offset *= 10;
793 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700794 case I_RESO:
795 result->opcode = I_RESB;
796 result->oprs[0].offset *= 16;
797 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700798 default:
799 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800 }
801
802 return result;
803}
804
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000806{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000807 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000808 int i;
809 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810
H. Peter Anvin76690a12002-04-30 20:52:49 +0000811 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000813 stdscan_bufptr = p;
814 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815}
816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000818{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819 extop *e;
820
821 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 e = i->eops;
823 i->eops = i->eops->next;
824 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825 }
826}