Document the as86 ..start label
[nasm.git] / parser.c
blob6893c981a0d2723d2e10af982e453e0ece6e0c14
1 /* parser.c source line parser for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
9 */
11 #include "compiler.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <inttypes.h>
20 #include "nasm.h"
21 #include "insns.h"
22 #include "nasmlib.h"
23 #include "stdscan.h"
24 #include "parser.h"
25 #include "float.h"
26 #include "tables.h"
28 extern int in_abs_seg; /* ABSOLUTE segment flag */
29 extern int32_t abs_seg; /* ABSOLUTE segment */
30 extern int32_t abs_offset; /* ABSOLUTE segment offset */
32 static int is_comma_next(void);
34 static int i;
35 static struct tokenval tokval;
36 static efunc error;
37 static struct ofmt *outfmt; /* Structure of addresses of output routines */
38 static struct location *location; /* Pointer to current line's segment,offset */
40 void parser_global_info(struct ofmt *output, struct location * locp)
42 outfmt = output;
43 location = locp;
46 static int prefix_slot(enum prefixes prefix)
48 switch (prefix) {
49 case R_CS:
50 case R_DS:
51 case R_SS:
52 case R_ES:
53 case R_FS:
54 case R_GS:
55 return PPS_SEG;
56 case P_LOCK:
57 case P_REP:
58 case P_REPE:
59 case P_REPZ:
60 case P_REPNE:
61 case P_REPNZ:
62 return PPS_LREP;
63 case P_O16:
64 case P_O32:
65 case P_O64:
66 case P_OSP:
67 return PPS_OSIZE;
68 case P_A16:
69 case P_A32:
70 case P_A64:
71 case P_ASP:
72 return PPS_ASIZE;
73 default:
74 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
75 return -1;
79 static void process_size_override(insn * result, int operand)
81 if (tasm_compatible_mode) {
82 switch ((int)tokval.t_integer) {
83 /* For TASM compatibility a size override inside the
84 * brackets changes the size of the operand, not the
85 * address type of the operand as it does in standard
86 * NASM syntax. Hence:
88 * mov eax,[DWORD val]
90 * is valid syntax in TASM compatibility mode. Note that
91 * you lose the ability to override the default address
92 * type for the instruction, but we never use anything
93 * but 32-bit flat model addressing in our code.
95 case S_BYTE:
96 result->oprs[operand].type |= BITS8;
97 break;
98 case S_WORD:
99 result->oprs[operand].type |= BITS16;
100 break;
101 case S_DWORD:
102 case S_LONG:
103 result->oprs[operand].type |= BITS32;
104 break;
105 case S_QWORD:
106 result->oprs[operand].type |= BITS64;
107 break;
108 case S_TWORD:
109 result->oprs[operand].type |= BITS80;
110 break;
111 case S_OWORD:
112 result->oprs[operand].type |= BITS128;
113 break;
114 default:
115 error(ERR_NONFATAL,
116 "invalid operand size specification");
117 break;
119 } else {
120 /* Standard NASM compatible syntax */
121 switch ((int)tokval.t_integer) {
122 case S_NOSPLIT:
123 result->oprs[operand].eaflags |= EAF_TIMESTWO;
124 break;
125 case S_REL:
126 result->oprs[operand].eaflags |= EAF_REL;
127 break;
128 case S_ABS:
129 result->oprs[operand].eaflags |= EAF_ABS;
130 break;
131 case S_BYTE:
132 result->oprs[operand].disp_size = 8;
133 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
134 break;
135 case P_A16:
136 case P_A32:
137 case P_A64:
138 if (result->prefixes[PPS_ASIZE] &&
139 result->prefixes[PPS_ASIZE] != tokval.t_integer)
140 error(ERR_NONFATAL,
141 "conflicting address size specifications");
142 else
143 result->prefixes[PPS_ASIZE] = tokval.t_integer;
144 break;
145 case S_WORD:
146 result->oprs[operand].disp_size = 16;
147 result->oprs[operand].eaflags |= EAF_WORDOFFS;
148 break;
149 case S_DWORD:
150 case S_LONG:
151 result->oprs[operand].disp_size = 32;
152 result->oprs[operand].eaflags |= EAF_WORDOFFS;
153 break;
154 case S_QWORD:
155 result->oprs[operand].disp_size = 64;
156 result->oprs[operand].eaflags |= EAF_WORDOFFS;
157 break;
158 default:
159 error(ERR_NONFATAL, "invalid size specification in"
160 " effective address");
161 break;
166 insn *parse_line(int pass, char *buffer, insn * result,
167 efunc errfunc, evalfunc evaluate, ldfunc ldef)
169 int operand;
170 int critical;
171 struct eval_hints hints;
172 int j;
173 bool first;
174 bool insn_is_label = false;
176 restart_parse:
177 first = true;
178 result->forw_ref = false;
179 error = errfunc;
181 stdscan_reset();
182 stdscan_bufptr = buffer;
183 i = stdscan(NULL, &tokval);
185 result->label = NULL; /* Assume no label */
186 result->eops = NULL; /* must do this, whatever happens */
187 result->operands = 0; /* must initialize this */
189 if (i == 0) { /* blank line - ignore */
190 result->opcode = -1; /* and no instruction either */
191 return result;
193 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
194 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
195 error(ERR_NONFATAL, "label or instruction expected"
196 " at start of line");
197 result->opcode = -1;
198 return result;
201 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
202 /* there's a label here */
203 first = false;
204 result->label = tokval.t_charptr;
205 i = stdscan(NULL, &tokval);
206 if (i == ':') { /* skip over the optional colon */
207 i = stdscan(NULL, &tokval);
208 } else if (i == 0) {
209 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
210 "label alone on a line without a colon might be in error");
212 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
214 * FIXME: location->segment could be NO_SEG, in which case
215 * it is possible we should be passing 'abs_seg'. Look into this.
216 * Work out whether that is *really* what we should be doing.
217 * Generally fix things. I think this is right as it is, but
218 * am still not certain.
220 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
221 location->offset, NULL, true, false, outfmt, errfunc);
225 if (i == 0) {
226 result->opcode = -1; /* this line contains just a label */
227 return result;
230 for (j = 0; j < MAXPREFIX; j++)
231 result->prefixes[j] = P_none;
232 result->times = 1L;
234 while (i == TOKEN_PREFIX ||
235 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
237 first = false;
240 * Handle special case: the TIMES prefix.
242 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
243 expr *value;
245 i = stdscan(NULL, &tokval);
246 value =
247 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
248 i = tokval.t_type;
249 if (!value) { /* but, error in evaluator */
250 result->opcode = -1; /* unrecoverable parse error: */
251 return result; /* ignore this instruction */
253 if (!is_simple(value)) {
254 error(ERR_NONFATAL,
255 "non-constant argument supplied to TIMES");
256 result->times = 1L;
257 } else {
258 result->times = value->value;
259 if (value->value < 0 && pass0 == 2) {
260 error(ERR_NONFATAL, "TIMES value %d is negative",
261 value->value);
262 result->times = 0;
265 } else {
266 int slot = prefix_slot(tokval.t_integer);
267 if (result->prefixes[slot]) {
268 if (result->prefixes[slot] == tokval.t_integer)
269 error(ERR_WARNING,
270 "instruction has redundant prefixes");
271 else
272 error(ERR_NONFATAL,
273 "instruction has conflicting prefixes");
275 result->prefixes[slot] = tokval.t_integer;
276 i = stdscan(NULL, &tokval);
280 if (i != TOKEN_INSN) {
281 int j;
282 enum prefixes pfx;
284 for (j = 0; j < MAXPREFIX; j++)
285 if ((pfx = result->prefixes[j]) != P_none)
286 break;
288 if (i == 0 && pfx != P_none) {
290 * Instruction prefixes are present, but no actual
291 * instruction. This is allowed: at this point we
292 * invent a notional instruction of RESB 0.
294 result->opcode = I_RESB;
295 result->operands = 1;
296 result->oprs[0].type = IMMEDIATE;
297 result->oprs[0].offset = 0L;
298 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
299 return result;
300 } else {
301 error(ERR_NONFATAL, "parser: instruction expected");
302 result->opcode = -1;
303 return result;
307 result->opcode = tokval.t_integer;
308 result->condition = tokval.t_inttwo;
311 * INCBIN cannot be satisfied with incorrectly
312 * evaluated operands, since the correct values _must_ be known
313 * on the first pass. Hence, even in pass one, we set the
314 * `critical' flag on calling evaluate(), so that it will bomb
315 * out on undefined symbols.
317 if (result->opcode == I_INCBIN) {
318 critical = (pass0 < 2 ? 1 : 2);
320 } else
321 critical = (pass == 2 ? 2 : 0);
323 if (result->opcode == I_DB || result->opcode == I_DW ||
324 result->opcode == I_DD || result->opcode == I_DQ ||
325 result->opcode == I_DT || result->opcode == I_DO ||
326 result->opcode == I_DY || result->opcode == I_INCBIN) {
327 extop *eop, **tail = &result->eops, **fixptr;
328 int oper_num = 0;
329 int32_t sign;
331 result->eops_float = false;
334 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
336 while (1) {
337 i = stdscan(NULL, &tokval);
338 if (i == 0)
339 break;
340 else if (first && i == ':') {
341 insn_is_label = true;
342 goto restart_parse;
344 first = false;
345 fixptr = tail;
346 eop = *tail = nasm_malloc(sizeof(extop));
347 tail = &eop->next;
348 eop->next = NULL;
349 eop->type = EOT_NOTHING;
350 oper_num++;
351 sign = +1;
353 /* is_comma_next() here is to distinguish this from
354 a string used as part of an expression... */
355 if (i == TOKEN_STR && is_comma_next()) {
356 eop->type = EOT_DB_STRING;
357 eop->stringval = tokval.t_charptr;
358 eop->stringlen = tokval.t_inttwo;
359 i = stdscan(NULL, &tokval); /* eat the comma */
360 } else if (i == TOKEN_STRFUNC) {
361 bool parens = false;
362 const char *funcname = tokval.t_charptr;
363 enum strfunc func = tokval.t_integer;
364 i = stdscan(NULL, &tokval);
365 if (i == '(') {
366 parens = true;
367 i = stdscan(NULL, &tokval);
369 if (i != TOKEN_STR) {
370 error(ERR_NONFATAL,
371 "%s must be followed by a string constant",
372 funcname);
373 eop->type = EOT_NOTHING;
374 } else {
375 eop->type = EOT_DB_STRING_FREE;
376 eop->stringlen =
377 string_transform(tokval.t_charptr, tokval.t_inttwo,
378 &eop->stringval, func);
379 if (eop->stringlen == (size_t)-1) {
380 error(ERR_NONFATAL, "invalid string for transform");
381 eop->type = EOT_NOTHING;
384 if (parens && i && i != ')') {
385 i = stdscan(NULL, &tokval);
386 if (i != ')') {
387 error(ERR_NONFATAL, "unterminated %s function",
388 funcname);
391 if (i && i != ',')
392 i = stdscan(NULL, &tokval);
393 } else if (i == '-' || i == '+') {
394 char *save = stdscan_bufptr;
395 int token = i;
396 sign = (i == '-') ? -1 : 1;
397 i = stdscan(NULL, &tokval);
398 if (i != TOKEN_FLOAT) {
399 stdscan_bufptr = save;
400 i = tokval.t_type = token;
401 goto is_expression;
402 } else {
403 goto is_float;
405 } else if (i == TOKEN_FLOAT) {
406 is_float:
407 eop->type = EOT_DB_STRING;
408 result->eops_float = true;
409 switch (result->opcode) {
410 case I_DB:
411 eop->stringlen = 1;
412 break;
413 case I_DW:
414 eop->stringlen = 2;
415 break;
416 case I_DD:
417 eop->stringlen = 4;
418 break;
419 case I_DQ:
420 eop->stringlen = 8;
421 break;
422 case I_DT:
423 eop->stringlen = 10;
424 break;
425 case I_DO:
426 eop->stringlen = 16;
427 break;
428 case I_DY:
429 error(ERR_NONFATAL, "floating-point constant"
430 " encountered in DY instruction");
431 eop->stringlen = 0;
432 break;
433 default:
434 error(ERR_NONFATAL, "floating-point constant"
435 " encountered in unknown instruction");
437 * fix suggested by Pedro Gimeno... original line
438 * was:
439 * eop->type = EOT_NOTHING;
441 eop->stringlen = 0;
442 break;
444 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
445 tail = &eop->next;
446 *fixptr = eop;
447 eop->stringval = (char *)eop + sizeof(extop);
448 if (!eop->stringlen ||
449 !float_const(tokval.t_charptr, sign,
450 (uint8_t *)eop->stringval,
451 eop->stringlen, error))
452 eop->type = EOT_NOTHING;
453 i = stdscan(NULL, &tokval); /* eat the comma */
454 } else {
455 /* anything else, assume it is an expression */
456 expr *value;
458 is_expression:
459 value = evaluate(stdscan, NULL, &tokval, NULL,
460 critical, error, NULL);
461 i = tokval.t_type;
462 if (!value) { /* error in evaluator */
463 result->opcode = -1; /* unrecoverable parse error: */
464 return result; /* ignore this instruction */
466 if (is_unknown(value)) {
467 eop->type = EOT_DB_NUMBER;
468 eop->offset = 0; /* doesn't matter what we put */
469 eop->segment = eop->wrt = NO_SEG; /* likewise */
470 } else if (is_reloc(value)) {
471 eop->type = EOT_DB_NUMBER;
472 eop->offset = reloc_value(value);
473 eop->segment = reloc_seg(value);
474 eop->wrt = reloc_wrt(value);
475 } else {
476 error(ERR_NONFATAL,
477 "operand %d: expression is not simple"
478 " or relocatable", oper_num);
483 * We're about to call stdscan(), which will eat the
484 * comma that we're currently sitting on between
485 * arguments. However, we'd better check first that it
486 * _is_ a comma.
488 if (i == 0) /* also could be EOL */
489 break;
490 if (i != ',') {
491 error(ERR_NONFATAL, "comma expected after operand %d",
492 oper_num);
493 result->opcode = -1; /* unrecoverable parse error: */
494 return result; /* ignore this instruction */
498 if (result->opcode == I_INCBIN) {
500 * Correct syntax for INCBIN is that there should be
501 * one string operand, followed by one or two numeric
502 * operands.
504 if (!result->eops || result->eops->type != EOT_DB_STRING)
505 error(ERR_NONFATAL, "`incbin' expects a file name");
506 else if (result->eops->next &&
507 result->eops->next->type != EOT_DB_NUMBER)
508 error(ERR_NONFATAL, "`incbin': second parameter is",
509 " non-numeric");
510 else if (result->eops->next && result->eops->next->next &&
511 result->eops->next->next->type != EOT_DB_NUMBER)
512 error(ERR_NONFATAL, "`incbin': third parameter is",
513 " non-numeric");
514 else if (result->eops->next && result->eops->next->next &&
515 result->eops->next->next->next)
516 error(ERR_NONFATAL,
517 "`incbin': more than three parameters");
518 else
519 return result;
521 * If we reach here, one of the above errors happened.
522 * Throw the instruction away.
524 result->opcode = -1;
525 return result;
526 } else /* DB ... */ if (oper_num == 0)
527 error(ERR_WARNING | ERR_PASS1,
528 "no operand for data declaration");
529 else
530 result->operands = oper_num;
532 return result;
535 /* right. Now we begin to parse the operands. There may be up to four
536 * of these, separated by commas, and terminated by a zero token. */
538 for (operand = 0; operand < MAX_OPERANDS; operand++) {
539 expr *value; /* used most of the time */
540 int mref; /* is this going to be a memory ref? */
541 int bracket; /* is it a [] mref, or a & mref? */
542 int setsize = 0;
544 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
545 result->oprs[operand].eaflags = 0; /* and this */
546 result->oprs[operand].opflags = 0;
548 i = stdscan(NULL, &tokval);
549 if (i == 0)
550 break; /* end of operands: get out of here */
551 else if (first && i == ':') {
552 insn_is_label = true;
553 goto restart_parse;
555 first = false;
556 result->oprs[operand].type = 0; /* so far, no override */
557 while (i == TOKEN_SPECIAL) { /* size specifiers */
558 switch ((int)tokval.t_integer) {
559 case S_BYTE:
560 if (!setsize) /* we want to use only the first */
561 result->oprs[operand].type |= BITS8;
562 setsize = 1;
563 break;
564 case S_WORD:
565 if (!setsize)
566 result->oprs[operand].type |= BITS16;
567 setsize = 1;
568 break;
569 case S_DWORD:
570 case S_LONG:
571 if (!setsize)
572 result->oprs[operand].type |= BITS32;
573 setsize = 1;
574 break;
575 case S_QWORD:
576 if (!setsize)
577 result->oprs[operand].type |= BITS64;
578 setsize = 1;
579 break;
580 case S_TWORD:
581 if (!setsize)
582 result->oprs[operand].type |= BITS80;
583 setsize = 1;
584 break;
585 case S_OWORD:
586 if (!setsize)
587 result->oprs[operand].type |= BITS128;
588 setsize = 1;
589 break;
590 case S_YWORD:
591 if (!setsize)
592 result->oprs[operand].type |= BITS256;
593 setsize = 1;
594 break;
595 case S_TO:
596 result->oprs[operand].type |= TO;
597 break;
598 case S_STRICT:
599 result->oprs[operand].type |= STRICT;
600 break;
601 case S_FAR:
602 result->oprs[operand].type |= FAR;
603 break;
604 case S_NEAR:
605 result->oprs[operand].type |= NEAR;
606 break;
607 case S_SHORT:
608 result->oprs[operand].type |= SHORT;
609 break;
610 default:
611 error(ERR_NONFATAL, "invalid operand size specification");
613 i = stdscan(NULL, &tokval);
616 if (i == '[' || i == '&') { /* memory reference */
617 mref = true;
618 bracket = (i == '[');
619 i = stdscan(NULL, &tokval); /* then skip the colon */
620 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
621 process_size_override(result, operand);
622 i = stdscan(NULL, &tokval);
624 } else { /* immediate operand, or register */
625 mref = false;
626 bracket = false; /* placate optimisers */
629 if ((result->oprs[operand].type & FAR) && !mref &&
630 result->opcode != I_JMP && result->opcode != I_CALL) {
631 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
634 value = evaluate(stdscan, NULL, &tokval,
635 &result->oprs[operand].opflags,
636 critical, error, &hints);
637 i = tokval.t_type;
638 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
639 result->forw_ref = true;
641 if (!value) { /* error in evaluator */
642 result->opcode = -1; /* unrecoverable parse error: */
643 return result; /* ignore this instruction */
645 if (i == ':' && mref) { /* it was seg:offset */
647 * Process the segment override.
649 if (value[1].type != 0 || value->value != 1 ||
650 REG_SREG & ~nasm_reg_flags[value->type])
651 error(ERR_NONFATAL, "invalid segment override");
652 else if (result->prefixes[PPS_SEG])
653 error(ERR_NONFATAL,
654 "instruction has conflicting segment overrides");
655 else {
656 result->prefixes[PPS_SEG] = value->type;
657 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
658 result->oprs[operand].eaflags |= EAF_FSGS;
661 i = stdscan(NULL, &tokval); /* then skip the colon */
662 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
663 process_size_override(result, operand);
664 i = stdscan(NULL, &tokval);
666 value = evaluate(stdscan, NULL, &tokval,
667 &result->oprs[operand].opflags,
668 critical, error, &hints);
669 i = tokval.t_type;
670 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
671 result->forw_ref = true;
673 /* and get the offset */
674 if (!value) { /* but, error in evaluator */
675 result->opcode = -1; /* unrecoverable parse error: */
676 return result; /* ignore this instruction */
679 if (mref && bracket) { /* find ] at the end */
680 if (i != ']') {
681 error(ERR_NONFATAL, "parser: expecting ]");
682 do { /* error recovery again */
683 i = stdscan(NULL, &tokval);
684 } while (i != 0 && i != ',');
685 } else /* we got the required ] */
686 i = stdscan(NULL, &tokval);
687 } else { /* immediate operand */
688 if (i != 0 && i != ',' && i != ':') {
689 error(ERR_NONFATAL, "comma or end of line expected");
690 do { /* error recovery */
691 i = stdscan(NULL, &tokval);
692 } while (i != 0 && i != ',');
693 } else if (i == ':') {
694 result->oprs[operand].type |= COLON;
698 /* now convert the exprs returned from evaluate() into operand
699 * descriptions... */
701 if (mref) { /* it's a memory reference */
702 expr *e = value;
703 int b, i, s; /* basereg, indexreg, scale */
704 int64_t o; /* offset */
706 b = i = -1, o = s = 0;
707 result->oprs[operand].hintbase = hints.base;
708 result->oprs[operand].hinttype = hints.type;
710 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
711 if (e->value == 1) /* in fact it can be basereg */
712 b = e->type;
713 else /* no, it has to be indexreg */
714 i = e->type, s = e->value;
715 e++;
717 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
718 if (b != -1) /* If the first was the base, ... */
719 i = e->type, s = e->value; /* second has to be indexreg */
721 else if (e->value != 1) { /* If both want to be index */
722 error(ERR_NONFATAL,
723 "beroset-p-592-invalid effective address");
724 result->opcode = -1;
725 return result;
726 } else
727 b = e->type;
728 e++;
730 if (e->type != 0) { /* is there an offset? */
731 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
732 error(ERR_NONFATAL,
733 "beroset-p-603-invalid effective address");
734 result->opcode = -1;
735 return result;
736 } else {
737 if (e->type == EXPR_UNKNOWN) {
738 o = 0; /* doesn't matter what */
739 result->oprs[operand].wrt = NO_SEG; /* nor this */
740 result->oprs[operand].segment = NO_SEG; /* or this */
741 while (e->type)
742 e++; /* go to the end of the line */
743 } else {
744 if (e->type == EXPR_SIMPLE) {
745 o = e->value;
746 e++;
748 if (e->type == EXPR_WRT) {
749 result->oprs[operand].wrt = e->value;
750 e++;
751 } else
752 result->oprs[operand].wrt = NO_SEG;
754 * Look for a segment base type.
756 if (e->type && e->type < EXPR_SEGBASE) {
757 error(ERR_NONFATAL,
758 "beroset-p-630-invalid effective address");
759 result->opcode = -1;
760 return result;
762 while (e->type && e->value == 0)
763 e++;
764 if (e->type && e->value != 1) {
765 error(ERR_NONFATAL,
766 "beroset-p-637-invalid effective address");
767 result->opcode = -1;
768 return result;
770 if (e->type) {
771 result->oprs[operand].segment =
772 e->type - EXPR_SEGBASE;
773 e++;
774 } else
775 result->oprs[operand].segment = NO_SEG;
776 while (e->type && e->value == 0)
777 e++;
778 if (e->type) {
779 error(ERR_NONFATAL,
780 "beroset-p-650-invalid effective address");
781 result->opcode = -1;
782 return result;
786 } else {
787 o = 0;
788 result->oprs[operand].wrt = NO_SEG;
789 result->oprs[operand].segment = NO_SEG;
792 if (e->type != 0) { /* there'd better be nothing left! */
793 error(ERR_NONFATAL,
794 "beroset-p-663-invalid effective address");
795 result->opcode = -1;
796 return result;
799 /* It is memory, but it can match any r/m operand */
800 result->oprs[operand].type |= MEMORY_ANY;
802 if (b == -1 && (i == -1 || s == 0)) {
803 int is_rel = globalbits == 64 &&
804 !(result->oprs[operand].eaflags & EAF_ABS) &&
805 ((globalrel &&
806 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
807 (result->oprs[operand].eaflags & EAF_REL));
809 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
811 result->oprs[operand].basereg = b;
812 result->oprs[operand].indexreg = i;
813 result->oprs[operand].scale = s;
814 result->oprs[operand].offset = o;
815 } else { /* it's not a memory reference */
817 if (is_just_unknown(value)) { /* it's immediate but unknown */
818 result->oprs[operand].type |= IMMEDIATE;
819 result->oprs[operand].offset = 0; /* don't care */
820 result->oprs[operand].segment = NO_SEG; /* don't care again */
821 result->oprs[operand].wrt = NO_SEG; /* still don't care */
822 } else if (is_reloc(value)) { /* it's immediate */
823 result->oprs[operand].type |= IMMEDIATE;
824 result->oprs[operand].offset = reloc_value(value);
825 result->oprs[operand].segment = reloc_seg(value);
826 result->oprs[operand].wrt = reloc_wrt(value);
827 if (is_simple(value)) {
828 if (reloc_value(value) == 1)
829 result->oprs[operand].type |= UNITY;
830 if (optimizing >= 0 &&
831 !(result->oprs[operand].type & STRICT)) {
832 int64_t v64 = reloc_value(value);
833 int32_t v32 = (int32_t)v64;
834 int16_t v16 = (int16_t)v32;
836 if (v64 >= -128 && v64 <= 127)
837 result->oprs[operand].type |= SBYTE64;
838 if (v32 >= -128 && v32 <= 127)
839 result->oprs[operand].type |= SBYTE32;
840 if (v16 >= -128 && v16 <= 127)
841 result->oprs[operand].type |= SBYTE16;
844 } else { /* it's a register */
845 unsigned int rs;
847 if (value->type >= EXPR_SIMPLE || value->value != 1) {
848 error(ERR_NONFATAL, "invalid operand type");
849 result->opcode = -1;
850 return result;
854 * check that its only 1 register, not an expression...
856 for (i = 1; value[i].type; i++)
857 if (value[i].value) {
858 error(ERR_NONFATAL, "invalid operand type");
859 result->opcode = -1;
860 return result;
863 /* clear overrides, except TO which applies to FPU regs */
864 if (result->oprs[operand].type & ~TO) {
866 * we want to produce a warning iff the specified size
867 * is different from the register size
869 rs = result->oprs[operand].type & SIZE_MASK;
870 } else
871 rs = 0;
873 result->oprs[operand].type &= TO;
874 result->oprs[operand].type |= REGISTER;
875 result->oprs[operand].type |= nasm_reg_flags[value->type];
876 result->oprs[operand].basereg = value->type;
878 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
879 error(ERR_WARNING | ERR_PASS1,
880 "register size specification ignored");
885 result->operands = operand; /* set operand count */
887 /* clear remaining operands */
888 while (operand < MAX_OPERANDS)
889 result->oprs[operand++].type = 0;
892 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
894 switch (result->opcode) {
895 case I_RESW:
896 result->opcode = I_RESB;
897 result->oprs[0].offset *= 2;
898 break;
899 case I_RESD:
900 result->opcode = I_RESB;
901 result->oprs[0].offset *= 4;
902 break;
903 case I_RESQ:
904 result->opcode = I_RESB;
905 result->oprs[0].offset *= 8;
906 break;
907 case I_REST:
908 result->opcode = I_RESB;
909 result->oprs[0].offset *= 10;
910 break;
911 case I_RESO:
912 result->opcode = I_RESB;
913 result->oprs[0].offset *= 16;
914 break;
915 case I_RESY:
916 result->opcode = I_RESB;
917 result->oprs[0].offset *= 32;
918 break;
919 default:
920 break;
923 return result;
926 static int is_comma_next(void)
928 char *p;
929 int i;
930 struct tokenval tv;
932 p = stdscan_bufptr;
933 i = stdscan(NULL, &tv);
934 stdscan_bufptr = p;
935 return (i == ',' || i == ';' || !i);
938 void cleanup_insn(insn * i)
940 extop *e;
942 while ((e = i->eops)) {
943 i->eops = e->next;
944 if (e->type == EOT_DB_STRING_FREE)
945 nasm_free(e->stringval);
946 nasm_free(e);