blob: 2fe7bfb2201dcb7ce46abec8ea99ed27d7cb8872 [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 Anvin232badb2002-06-06 02:41:20 +000026#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000027
28enum { /* special tokens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000029 S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
H. Peter Anvin01377d82002-05-21 03:16:33 +000030 S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000031};
32
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033static int is_comma_next (void);
34
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 Anvineba20a72002-04-30 20:53:55 +000038static struct ofmt *outfmt; /* Structure of addresses of output routines */
39static loc_t *location; /* Pointer to current line's segment,offset */
40
41void parser_global_info (struct ofmt *output, loc_t *locp)
42{
43 outfmt = output;
44 location = locp;
45}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
H. Peter Anvin76690a12002-04-30 20:52:49 +000047insn *parse_line (int pass, char *buffer, insn *result,
H. Peter Anvineba20a72002-04-30 20:53:55 +000048 efunc errfunc, evalfunc evaluate, ldfunc ldef)
49{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000050 int operand;
51 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000052 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053
H. Peter Anvin76690a12002-04-30 20:52:49 +000054 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056
H. Peter Anvin76690a12002-04-30 20:52:49 +000057 stdscan_reset();
58 stdscan_bufptr = buffer;
59 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvineba20a72002-04-30 20:53:55 +000061 result->label = NULL; /* Assume no label */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062 result->eops = NULL; /* must do this, whatever happens */
H. Peter Anvin76690a12002-04-30 20:52:49 +000063 result->operands = 0; /* must initialise this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
65 if (i==0) { /* blank line - ignore */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066 result->opcode = -1; /* and no instruction either */
67 return result;
68 }
69 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
70 (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
71 error (ERR_NONFATAL, "label or instruction expected"
72 " at start of line");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073 result->opcode = -1;
74 return result;
75 }
76
77 if (i == TOKEN_ID) { /* there's a label here */
H. Peter Anvin76690a12002-04-30 20:52:49 +000078 result->label = tokval.t_charptr;
H. Peter Anvin76690a12002-04-30 20:52:49 +000079 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000080 if (i == ':') { /* skip over the optional colon */
H. Peter Anvin76690a12002-04-30 20:52:49 +000081 i = stdscan(NULL, &tokval);
H. Peter Anvineba20a72002-04-30 20:53:55 +000082 } else if (i == 0) {
83 error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
H. Peter Anvin6768eb72002-04-30 20:52:26 +000084 "label alone on a line without a colon might be in error");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000086 if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
87 {
88 /*
89 * FIXME: location->segment could be NO_SEG, in which case
90 * it is possible we should be passing 'abs_seg'. Look into this.
91 * Work out whether that is *really* what we should be doing.
92 * Generally fix things. I think this is right as it is, but
93 * am still not certain.
94 */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000095 ldef (result->label, in_abs_seg?abs_seg:location->segment,
H. Peter Anvineba20a72002-04-30 20:53:55 +000096 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
97 }
98 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000099
100 if (i==0) {
101 result->opcode = -1; /* this line contains just a label */
102 return result;
103 }
104
105 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000106 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
108 while (i == TOKEN_PREFIX ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000109 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
110 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000111 /*
112 * Handle special case: the TIMES prefix.
113 */
114 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
115 expr *value;
116
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 i = stdscan(NULL, &tokval);
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000118 value = evaluate (stdscan, NULL, &tokval, NULL, pass0, error, NULL);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000119 i = tokval.t_type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120 if (!value) { /* but, error in evaluator */
121 result->opcode = -1; /* unrecoverable parse error: */
122 return result; /* ignore this instruction */
123 }
124 if (!is_simple (value)) {
125 error (ERR_NONFATAL,
126 "non-constant argument supplied to TIMES");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000127 result->times = 1L;
128 } else {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129 result->times = value->value;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000130 if (value->value < 0) {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000131 error(ERR_NONFATAL, "TIMES value %d is negative",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132 value->value);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000133 result->times = 0;
134 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000136 } else {
137 if (result->nprefix == MAXPREFIX)
138 error (ERR_NONFATAL,
139 "instruction has more than %d prefixes", MAXPREFIX);
140 else
141 result->prefixes[result->nprefix++] = tokval.t_integer;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000142 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 }
144 }
145
146 if (i != TOKEN_INSN) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000147 if (result->nprefix > 0 && i == 0) {
148 /*
149 * Instruction prefixes are present, but no actual
150 * instruction. This is allowed: at this point we
151 * invent a notional instruction of RESB 0.
152 */
153 result->opcode = I_RESB;
154 result->operands = 1;
155 result->oprs[0].type = IMMEDIATE;
156 result->oprs[0].offset = 0L;
157 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
158 return result;
159 } else {
160 error (ERR_NONFATAL, "parser: instruction expected");
161 result->opcode = -1;
162 return result;
163 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000164 }
165
166 result->opcode = tokval.t_integer;
167 result->condition = tokval.t_inttwo;
168
169 /*
170 * RESB, RESW and RESD cannot be satisfied with incorrectly
171 * evaluated operands, since the correct values _must_ be known
172 * on the first pass. Hence, even in pass one, we set the
173 * `critical' flag on calling evaluate(), so that it will bomb
174 * out on undefined symbols. Nasty, but there's nothing we can
175 * do about it.
176 *
177 * For the moment, EQU has the same difficulty, so we'll
178 * include that.
179 */
180 if (result->opcode == I_RESB ||
181 result->opcode == I_RESW ||
182 result->opcode == I_RESD ||
183 result->opcode == I_RESQ ||
184 result->opcode == I_REST ||
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000185 result->opcode == I_EQU ||
186 result->opcode == I_INCBIN) /* fbk */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000187 {
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000188 critical = pass0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000189 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000190 else
191 critical = (pass==2 ? 2 : 0);
192
193 if (result->opcode == I_DB ||
194 result->opcode == I_DW ||
195 result->opcode == I_DD ||
196 result->opcode == I_DQ ||
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 result->opcode == I_DT ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 result->opcode == I_INCBIN)
199 {
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000200 extop *eop, **tail = &result->eops, **fixptr;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000201 int oper_num = 0;
202
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203 result->eops_float = FALSE;
204
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000205 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000206 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000207 */
208 while (1) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000209 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000210 if (i == 0)
211 break;
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000212 fixptr = tail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000213 eop = *tail = nasm_malloc(sizeof(extop));
214 tail = &eop->next;
215 eop->next = NULL;
216 eop->type = EOT_NOTHING;
217 oper_num++;
218
219 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
220 eop->type = EOT_DB_STRING;
221 eop->stringval = tokval.t_charptr;
222 eop->stringlen = tokval.t_inttwo;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000223 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224 continue;
225 }
226
H. Peter Anvineba20a72002-04-30 20:53:55 +0000227 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000228 long sign = +1L;
229
230 if (i == '-') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000231 char *save = stdscan_bufptr;
232 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233 sign = -1L;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000234 if (i != TOKEN_FLOAT || !is_comma_next()) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000235 stdscan_bufptr = save;
236 i = tokval.t_type = '-';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237 }
238 }
239
240 if (i == TOKEN_FLOAT) {
241 eop->type = EOT_DB_STRING;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000242 result->eops_float = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000243 if (result->opcode == I_DD)
244 eop->stringlen = 4;
245 else if (result->opcode == I_DQ)
246 eop->stringlen = 8;
247 else if (result->opcode == I_DT)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000248 eop->stringlen = 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249 else {
250 error(ERR_NONFATAL, "floating-point constant"
251 " encountered in `D%c' instruction",
252 result->opcode == I_DW ? 'W' : 'B');
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253 /*
254 * fix suggested by Pedro Gimeno... original line
255 * was:
256 * eop->type = EOT_NOTHING;
257 */
258 eop->stringlen = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000259 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000260 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
H. Peter Anvin87bc6192002-04-30 20:53:16 +0000261 tail = &eop->next;
262 *fixptr = eop;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000263 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000264 if (eop->stringlen < 4 ||
265 !float_const (tokval.t_charptr, sign,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000266 (unsigned char *)eop->stringval,
267 eop->stringlen, error))
268 eop->type = EOT_NOTHING;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000269 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000270 continue;
271 }
272 }
273
H. Peter Anvineba20a72002-04-30 20:53:55 +0000274 /* anything else */
275 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000276 expr *value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000277 value = evaluate (stdscan, NULL, &tokval, NULL,
278 critical, error, NULL);
279 i = tokval.t_type;
280 if (!value) { /* error in evaluator */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281 result->opcode = -1;/* unrecoverable parse error: */
282 return result; /* ignore this instruction */
283 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000284 if (is_unknown(value)) {
285 eop->type = EOT_DB_NUMBER;
286 eop->offset = 0; /* doesn't matter what we put */
287 eop->segment = eop->wrt = NO_SEG; /* likewise */
288 } else if (is_reloc(value)) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000289 eop->type = EOT_DB_NUMBER;
290 eop->offset = reloc_value(value);
291 eop->segment = reloc_seg(value);
292 eop->wrt = reloc_wrt(value);
293 } else {
294 error (ERR_NONFATAL,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000295 "operand %d: expression is not simple"
296 " or relocatable", oper_num);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000297 }
298 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000299
300 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000301 * We're about to call stdscan(), which will eat the
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000302 * comma that we're currently sitting on between
303 * arguments. However, we'd better check first that it
304 * _is_ a comma.
305 */
306 if (i == 0) /* also could be EOL */
307 break;
308 if (i != ',') {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000309 error (ERR_NONFATAL, "comma expected after operand %d",
310 oper_num);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000311 result->opcode = -1;/* unrecoverable parse error: */
312 return result; /* ignore this instruction */
313 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000314 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315
316 if (result->opcode == I_INCBIN) {
317 /*
318 * Correct syntax for INCBIN is that there should be
319 * one string operand, followed by one or two numeric
320 * operands.
321 */
322 if (!result->eops || result->eops->type != EOT_DB_STRING)
323 error (ERR_NONFATAL, "`incbin' expects a file name");
324 else if (result->eops->next &&
325 result->eops->next->type != EOT_DB_NUMBER)
326 error (ERR_NONFATAL, "`incbin': second parameter is",
327 " non-numeric");
328 else if (result->eops->next && result->eops->next->next &&
329 result->eops->next->next->type != EOT_DB_NUMBER)
330 error (ERR_NONFATAL, "`incbin': third parameter is",
331 " non-numeric");
332 else if (result->eops->next && result->eops->next->next &&
333 result->eops->next->next->next)
334 error (ERR_NONFATAL, "`incbin': more than three parameters");
335 else
336 return result;
337 /*
338 * If we reach here, one of the above errors happened.
339 * Throw the instruction away.
340 */
341 result->opcode = -1;
342 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343 } else /* DB ... */
344 if (oper_num == 0)
345 error (ERR_WARNING|ERR_PASS1,
346 "no operand for data declaration");
347 else
348 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000350 return result;
351 }
352
353 /* right. Now we begin to parse the operands. There may be up to three
354 * of these, separated by commas, and terminated by a zero token. */
355
356 for (operand = 0; operand < 3; operand++) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357 expr *value; /* used most of the time */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000358 int mref; /* is this going to be a memory ref? */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000359 int bracket; /* is it a [] mref, or a & mref? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000360 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000361
362 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000363 result->oprs[operand].eaflags = 0; /* and this */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000364 result->oprs[operand].opflags = 0;
365
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000367 if (i == 0) break; /* end of operands: get out of here */
368 result->oprs[operand].type = 0; /* so far, no override */
369 while (i == TOKEN_SPECIAL) {/* size specifiers */
370 switch ((int)tokval.t_integer) {
371 case S_BYTE:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000372 if (!setsize) /* we want to use only the first */
373 result->oprs[operand].type |= BITS8;
374 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000375 break;
376 case S_WORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000377 if (!setsize)
378 result->oprs[operand].type |= BITS16;
379 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000380 break;
381 case S_DWORD:
382 case S_LONG:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383 if (!setsize)
384 result->oprs[operand].type |= BITS32;
385 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000386 break;
387 case S_QWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388 if (!setsize)
389 result->oprs[operand].type |= BITS64;
390 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391 break;
392 case S_TWORD:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393 if (!setsize)
394 result->oprs[operand].type |= BITS80;
395 setsize = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000396 break;
397 case S_TO:
398 result->oprs[operand].type |= TO;
399 break;
H. Peter Anvin01377d82002-05-21 03:16:33 +0000400 case S_STRICT:
401 result->oprs[operand].type |= STRICT;
402 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000403 case S_FAR:
404 result->oprs[operand].type |= FAR;
405 break;
406 case S_NEAR:
407 result->oprs[operand].type |= NEAR;
408 break;
409 case S_SHORT:
410 result->oprs[operand].type |= SHORT;
411 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000412 default:
413 error (ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000415 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000416 }
417
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000418 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419 mref = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000420 bracket = (i == '[');
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000422 if (i == TOKEN_SPECIAL) { /* check for address size override */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423 if (tasm_compatible_mode) {
424 switch ((int)tokval.t_integer) {
425 /* For TASM compatibility a size override inside the
426 * brackets changes the size of the operand, not the
427 * address type of the operand as it does in standard
428 * NASM syntax. Hence:
429 *
430 * mov eax,[DWORD val]
431 *
432 * is valid syntax in TASM compatibility mode. Note that
433 * you lose the ability to override the default address
434 * type for the instruction, but we never use anything
435 * but 32-bit flat model addressing in our code.
436 */
437 case S_BYTE:
438 result->oprs[operand].type |= BITS8;
439 break;
440 case S_WORD:
441 result->oprs[operand].type |= BITS16;
442 break;
443 case S_DWORD:
444 case S_LONG:
445 result->oprs[operand].type |= BITS32;
446 break;
447 case S_QWORD:
448 result->oprs[operand].type |= BITS64;
449 break;
450 case S_TWORD:
451 result->oprs[operand].type |= BITS80;
452 break;
453 default:
454 error (ERR_NONFATAL, "invalid operand size specification");
455 }
456 } else {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000457 /* Standard NASM compatible syntax */
458 switch ((int)tokval.t_integer) {
459 case S_NOSPLIT:
460 result->oprs[operand].eaflags |= EAF_TIMESTWO;
461 break;
462 case S_BYTE:
463 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
464 break;
465 case S_WORD:
466 result->oprs[operand].addr_size = 16;
467 result->oprs[operand].eaflags |= EAF_WORDOFFS;
468 break;
469 case S_DWORD:
470 case S_LONG:
471 result->oprs[operand].addr_size = 32;
472 result->oprs[operand].eaflags |= EAF_WORDOFFS;
473 break;
474 default:
475 error (ERR_NONFATAL, "invalid size specification in"
476 " effective address");
477 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000478 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000479 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000480 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000481 } else { /* immediate operand, or register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000482 mref = FALSE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000483 bracket = FALSE; /* placate optimisers */
484 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485
Debbie Wiles63b53f72002-06-04 19:31:24 +0000486 if((result->oprs[operand].type & FAR) && !mref)
487 {
488 error (ERR_NONFATAL, "invalid use of FAR operand specifier");
489 }
490
H. Peter Anvin76690a12002-04-30 20:52:49 +0000491 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000492 &result->oprs[operand].opflags,
493 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000494 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000495 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
496 result->forw_ref = TRUE;
497 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000498 if (!value) { /* error in evaluator */
499 result->opcode = -1; /* unrecoverable parse error: */
500 return result; /* ignore this instruction */
501 }
502 if (i == ':' && mref) { /* it was seg:offset */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000503 /*
504 * Process the segment override.
505 */
506 if (value[1].type!=0 || value->value!=1 ||
507 REG_SREG & ~reg_flags[value->type])
508 error (ERR_NONFATAL, "invalid segment override");
509 else if (result->nprefix == MAXPREFIX)
510 error (ERR_NONFATAL,
511 "instruction has more than %d prefixes",
512 MAXPREFIX);
513 else
514 result->prefixes[result->nprefix++] = value->type;
515
516 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000517 if (i == TOKEN_SPECIAL) { /* another check for size override */
518 switch ((int)tokval.t_integer) {
519 case S_WORD:
520 result->oprs[operand].addr_size = 16;
521 break;
522 case S_DWORD:
523 case S_LONG:
524 result->oprs[operand].addr_size = 32;
525 break;
526 default:
527 error (ERR_NONFATAL, "invalid size specification in"
528 " effective address");
529 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000530 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000531 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000532 value = evaluate (stdscan, NULL, &tokval,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533 &result->oprs[operand].opflags,
534 critical, error, &hints);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000535 i = tokval.t_type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000536 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
537 result->forw_ref = TRUE;
538 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000539 /* and get the offset */
540 if (!value) { /* but, error in evaluator */
541 result->opcode = -1; /* unrecoverable parse error: */
542 return result; /* ignore this instruction */
543 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000544 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000545 if (mref && bracket) { /* find ] at the end */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000546 if (i != ']') {
547 error (ERR_NONFATAL, "parser: expecting ]");
548 do { /* error recovery again */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000549 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000550 } while (i != 0 && i != ',');
551 } else /* we got the required ] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000552 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000553 } else { /* immediate operand */
554 if (i != 0 && i != ',' && i != ':') {
555 error (ERR_NONFATAL, "comma or end of line expected");
556 do { /* error recovery */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000557 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000558 } while (i != 0 && i != ',');
559 } else if (i == ':') {
560 result->oprs[operand].type |= COLON;
561 }
562 }
563
564 /* now convert the exprs returned from evaluate() into operand
565 * descriptions... */
566
567 if (mref) { /* it's a memory reference */
568 expr *e = value;
569 int b, i, s; /* basereg, indexreg, scale */
570 long o; /* offset */
571
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000572 b = i = -1, o = s = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000573 result->oprs[operand].hintbase = hints.base;
574 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000575
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576 if (e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000577 if (e->value == 1) /* in fact it can be basereg */
578 b = e->type;
579 else /* no, it has to be indexreg */
580 i = e->type, s = e->value;
581 e++;
582 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000583 if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
584 {
585 if (b != -1) /* If the first was the base, ... */
586 i = e->type, s = e->value; /* second has to be indexreg */
587
588 else if (e->value != 1) /* If both want to be index */
589 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000590 error(ERR_NONFATAL, "beroset-p-592-invalid effective address");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591 result->opcode = -1;
592 return result;
593 }
594 else
595 b = e->type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596 e++;
597 }
598 if (e->type != 0) { /* is there an offset? */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599 if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
600 {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000601 error (ERR_NONFATAL, "beroset-p-603-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000602 result->opcode = -1;
603 return result;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604 }
605 else
606 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000607 if (e->type == EXPR_UNKNOWN) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608 o = 0; /* doesn't matter what */
609 result->oprs[operand].wrt = NO_SEG; /* nor this */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000610 result->oprs[operand].segment = NO_SEG; /* or this */
611 while (e->type) e++; /* go to the end of the line */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612 }
613 else
614 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000615 if (e->type == EXPR_SIMPLE) {
616 o = e->value;
617 e++;
618 }
619 if (e->type == EXPR_WRT) {
620 result->oprs[operand].wrt = e->value;
621 e++;
622 } else
623 result->oprs[operand].wrt = NO_SEG;
624 /*
625 * Look for a segment base type.
626 */
627 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000628 error (ERR_NONFATAL, "beroset-p-630-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000629 result->opcode = -1;
630 return result;
631 }
632 while (e->type && e->value == 0)
633 e++;
634 if (e->type && e->value != 1) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000635 error (ERR_NONFATAL, "beroset-p-637-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000636 result->opcode = -1;
637 return result;
638 }
639 if (e->type) {
640 result->oprs[operand].segment =
641 e->type - EXPR_SEGBASE;
642 e++;
643 } else
644 result->oprs[operand].segment = NO_SEG;
645 while (e->type && e->value == 0)
646 e++;
647 if (e->type) {
H. Peter Anvin734b1882002-04-30 21:01:08 +0000648 error (ERR_NONFATAL, "beroset-p-650-invalid effective address");
H. Peter Anvin76690a12002-04-30 20:52:49 +0000649 result->opcode = -1;
650 return result;
651 }
H. Peter Anvinea838272002-04-30 20:51:53 +0000652 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000653 }
654 } else {
655 o = 0;
656 result->oprs[operand].wrt = NO_SEG;
657 result->oprs[operand].segment = NO_SEG;
658 }
659
660 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000661 error (ERR_NONFATAL, "beroset-p-663-invalid effective address");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000662 result->opcode = -1;
663 return result;
664 }
665
666 result->oprs[operand].type |= MEMORY;
667 if (b==-1 && (i==-1 || s==0))
668 result->oprs[operand].type |= MEM_OFFS;
669 result->oprs[operand].basereg = b;
670 result->oprs[operand].indexreg = i;
671 result->oprs[operand].scale = s;
672 result->oprs[operand].offset = o;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000673 }
674 else /* it's not a memory reference */
675 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000676 if (is_just_unknown(value)) { /* it's immediate but unknown */
677 result->oprs[operand].type |= IMMEDIATE;
678 result->oprs[operand].offset = 0; /* don't care */
679 result->oprs[operand].segment = NO_SEG; /* don't care again */
680 result->oprs[operand].wrt = NO_SEG;/* still don't care */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681 }
682 else if (is_reloc(value)) /* it's immediate */
683 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000684 result->oprs[operand].type |= IMMEDIATE;
685 result->oprs[operand].offset = reloc_value(value);
686 result->oprs[operand].segment = reloc_seg(value);
687 result->oprs[operand].wrt = reloc_wrt(value);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000688 if (is_simple(value)) {
689 if (reloc_value(value)==1)
690 result->oprs[operand].type |= UNITY;
H. Peter Anvin8c1da7b2002-05-22 20:45:09 +0000691 if (optimizing>=0 &&
692 !(result->oprs[operand].type & STRICT)) {
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000693 if (reloc_value(value) >= -128 &&
694 reloc_value(value) <= 127)
695 result->oprs[operand].type |= SBYTE;
H. Peter Anvin4cf17482002-04-30 21:01:38 +0000696 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000697 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000698 }
699 else /* it's a register */
700 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701 if (value->type>=EXPR_SIMPLE || value->value!=1) {
702 error (ERR_NONFATAL, "invalid operand type");
703 result->opcode = -1;
704 return result;
705 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000706
707 /*
708 * check that its only 1 register, not an expression...
709 */
710 for (i = 1; value[i].type; i++)
711 if (value[i].value) {
712 error (ERR_NONFATAL, "invalid operand type");
713 result->opcode = -1;
714 return result;
715 }
716
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000717 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718 if (result->oprs[operand].type & ~TO) {
719 /*
720 * we want to produce a warning iff the specified size
721 * is different from the register size
722 */
723 i = result->oprs[operand].type & SIZE_MASK;
724 }
725 else
726 i = 0;
727
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728 result->oprs[operand].type &= TO;
729 result->oprs[operand].type |= REGISTER;
730 result->oprs[operand].type |= reg_flags[value->type];
731 result->oprs[operand].basereg = value->type;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000732
733 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
734 error (ERR_WARNING|ERR_PASS1,
735 "register size specification ignored");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736 }
737 }
738 }
739
740 result->operands = operand; /* set operand count */
741
742 while (operand<3) /* clear remaining operands */
743 result->oprs[operand++].type = 0;
744
745 /*
746 * Transform RESW, RESD, RESQ, REST into RESB.
747 */
748 switch (result->opcode) {
749 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
750 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
751 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
752 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
753 }
754
755 return result;
756}
757
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758static int is_comma_next (void)
759{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000761 int i;
762 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000763
H. Peter Anvin76690a12002-04-30 20:52:49 +0000764 p = stdscan_bufptr;
765 i = stdscan (NULL, &tv);
766 stdscan_bufptr = p;
767 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000768}
769
H. Peter Anvineba20a72002-04-30 20:53:55 +0000770void cleanup_insn (insn *i)
771{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000772 extop *e;
773
774 while (i->eops) {
775 e = i->eops;
776 i->eops = i->eops->next;
777 nasm_free (e);
778 }
779}