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