1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * parser.c source line parser for the Netwide Assembler
56 extern int in_abs_seg
; /* ABSOLUTE segment flag */
57 extern int32_t abs_seg
; /* ABSOLUTE segment */
58 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
60 static int is_comma_next(void);
63 static struct tokenval tokval
;
64 static struct location
*location
; /* Pointer to current line's segment,offset */
66 void parser_global_info(struct location
* locp
)
71 static int prefix_slot(int prefix
)
104 nasm_error(ERR_PANIC
, "Invalid value %d passed to prefix_slot()", prefix
);
109 static void process_size_override(insn
*result
, int operand
)
111 if (tasm_compatible_mode
) {
112 switch ((int)tokval
.t_integer
) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
118 * mov eax,[DWORD val]
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
126 result
->oprs
[operand
].type
|= BITS8
;
129 result
->oprs
[operand
].type
|= BITS16
;
133 result
->oprs
[operand
].type
|= BITS32
;
136 result
->oprs
[operand
].type
|= BITS64
;
139 result
->oprs
[operand
].type
|= BITS80
;
142 result
->oprs
[operand
].type
|= BITS128
;
145 nasm_error(ERR_NONFATAL
,
146 "invalid operand size specification");
150 /* Standard NASM compatible syntax */
151 switch ((int)tokval
.t_integer
) {
153 result
->oprs
[operand
].eaflags
|= EAF_TIMESTWO
;
156 result
->oprs
[operand
].eaflags
|= EAF_REL
;
159 result
->oprs
[operand
].eaflags
|= EAF_ABS
;
162 result
->oprs
[operand
].disp_size
= 8;
163 result
->oprs
[operand
].eaflags
|= EAF_BYTEOFFS
;
168 if (result
->prefixes
[PPS_ASIZE
] &&
169 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
170 nasm_error(ERR_NONFATAL
,
171 "conflicting address size specifications");
173 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
176 result
->oprs
[operand
].disp_size
= 16;
177 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
181 result
->oprs
[operand
].disp_size
= 32;
182 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
185 result
->oprs
[operand
].disp_size
= 64;
186 result
->oprs
[operand
].eaflags
|= EAF_WORDOFFS
;
189 nasm_error(ERR_NONFATAL
, "invalid size specification in"
190 " effective address");
197 * when two or more decorators follow a register operand,
198 * consecutive decorators are parsed here.
199 * opmask and zeroing decorators can be placed in any order.
200 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
201 * decorator(s) are placed at the end of an operand.
203 static bool parse_braces(decoflags_t
*decoflags
)
206 bool recover
= false;
210 if (i
== TOKEN_OPMASK
) {
211 if (*decoflags
& OPMASK_MASK
) {
212 nasm_error(ERR_NONFATAL
, "opmask k%lu is already set",
213 *decoflags
& OPMASK_MASK
);
214 *decoflags
&= ~OPMASK_MASK
;
216 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
217 } else if (i
== TOKEN_DECORATOR
) {
218 switch (tokval
.t_integer
) {
221 * according to AVX512 spec, only zeroing/merging decorator
222 * is supported with opmask
224 *decoflags
|= GEN_Z(0);
227 nasm_error(ERR_NONFATAL
, "{%s} is not an expected decorator",
231 } else if (i
== ',' || i
== TOKEN_EOS
){
234 nasm_error(ERR_NONFATAL
, "only a series of valid decorators"
239 i
= stdscan(NULL
, &tokval
);
245 insn
*parse_line(int pass
, char *buffer
, insn
*result
, ldfunc ldef
)
247 bool insn_is_label
= false;
248 struct eval_hints hints
;
256 result
->forw_ref
= false;
260 i
= stdscan(NULL
, &tokval
);
262 result
->label
= NULL
; /* Assume no label */
263 result
->eops
= NULL
; /* must do this, whatever happens */
264 result
->operands
= 0; /* must initialize this */
265 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
266 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
268 /* Ignore blank lines */
269 if (i
== TOKEN_EOS
) {
270 result
->opcode
= I_none
;
277 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
278 nasm_error(ERR_NONFATAL
,
279 "label or instruction expected at start of line");
280 result
->opcode
= I_none
;
284 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
285 /* there's a label here */
287 result
->label
= tokval
.t_charptr
;
288 i
= stdscan(NULL
, &tokval
);
289 if (i
== ':') { /* skip over the optional colon */
290 i
= stdscan(NULL
, &tokval
);
292 nasm_error(ERR_WARNING
| ERR_WARN_OL
| ERR_PASS1
,
293 "label alone on a line without a colon might be in error");
295 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
297 * FIXME: location->segment could be NO_SEG, in which case
298 * it is possible we should be passing 'abs_seg'. Look into this.
299 * Work out whether that is *really* what we should be doing.
300 * Generally fix things. I think this is right as it is, but
301 * am still not certain.
303 ldef(result
->label
, in_abs_seg
? abs_seg
: location
->segment
,
304 location
->offset
, NULL
, true, false);
308 /* Just a label here */
309 if (i
== TOKEN_EOS
) {
310 result
->opcode
= I_none
;
314 nasm_build_assert(P_none
!= 0);
315 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
318 while (i
== TOKEN_PREFIX
||
319 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
323 * Handle special case: the TIMES prefix.
325 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
328 i
= stdscan(NULL
, &tokval
);
329 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass0
, nasm_error
, NULL
);
331 if (!value
) { /* but, error in evaluator */
332 result
->opcode
= I_none
; /* unrecoverable parse error: */
333 return result
; /* ignore this instruction */
335 if (!is_simple(value
)) {
336 nasm_error(ERR_NONFATAL
,
337 "non-constant argument supplied to TIMES");
340 result
->times
= value
->value
;
341 if (value
->value
< 0 && pass0
== 2) {
342 nasm_error(ERR_NONFATAL
, "TIMES value %"PRId64
" is negative",
348 int slot
= prefix_slot(tokval
.t_integer
);
349 if (result
->prefixes
[slot
]) {
350 if (result
->prefixes
[slot
] == tokval
.t_integer
)
351 nasm_error(ERR_WARNING
| ERR_PASS1
,
352 "instruction has redundant prefixes");
354 nasm_error(ERR_NONFATAL
,
355 "instruction has conflicting prefixes");
357 result
->prefixes
[slot
] = tokval
.t_integer
;
358 i
= stdscan(NULL
, &tokval
);
362 if (i
!= TOKEN_INSN
) {
366 for (j
= 0; j
< MAXPREFIX
; j
++) {
367 if ((pfx
= result
->prefixes
[j
]) != P_none
)
371 if (i
== 0 && pfx
!= P_none
) {
373 * Instruction prefixes are present, but no actual
374 * instruction. This is allowed: at this point we
375 * invent a notional instruction of RESB 0.
377 result
->opcode
= I_RESB
;
378 result
->operands
= 1;
379 result
->oprs
[0].type
= IMMEDIATE
;
380 result
->oprs
[0].offset
= 0L;
381 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
384 nasm_error(ERR_NONFATAL
, "parser: instruction expected");
385 result
->opcode
= I_none
;
390 result
->opcode
= tokval
.t_integer
;
391 result
->condition
= tokval
.t_inttwo
;
394 * INCBIN cannot be satisfied with incorrectly
395 * evaluated operands, since the correct values _must_ be known
396 * on the first pass. Hence, even in pass one, we set the
397 * `critical' flag on calling evaluate(), so that it will bomb
398 * out on undefined symbols.
400 if (result
->opcode
== I_INCBIN
) {
401 critical
= (pass0
< 2 ? 1 : 2);
404 critical
= (pass
== 2 ? 2 : 0);
406 if (result
->opcode
== I_DB
|| result
->opcode
== I_DW
||
407 result
->opcode
== I_DD
|| result
->opcode
== I_DQ
||
408 result
->opcode
== I_DT
|| result
->opcode
== I_DO
||
409 result
->opcode
== I_DY
|| result
->opcode
== I_DZ
||
410 result
->opcode
== I_INCBIN
) {
411 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
415 result
->eops_float
= false;
418 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
421 i
= stdscan(NULL
, &tokval
);
424 else if (first
&& i
== ':') {
425 insn_is_label
= true;
430 eop
= *tail
= nasm_malloc(sizeof(extop
));
433 eop
->type
= EOT_NOTHING
;
438 * is_comma_next() here is to distinguish this from
439 * a string used as part of an expression...
441 if (i
== TOKEN_STR
&& is_comma_next()) {
442 eop
->type
= EOT_DB_STRING
;
443 eop
->stringval
= tokval
.t_charptr
;
444 eop
->stringlen
= tokval
.t_inttwo
;
445 i
= stdscan(NULL
, &tokval
); /* eat the comma */
446 } else if (i
== TOKEN_STRFUNC
) {
448 const char *funcname
= tokval
.t_charptr
;
449 enum strfunc func
= tokval
.t_integer
;
450 i
= stdscan(NULL
, &tokval
);
453 i
= stdscan(NULL
, &tokval
);
455 if (i
!= TOKEN_STR
) {
456 nasm_error(ERR_NONFATAL
,
457 "%s must be followed by a string constant",
459 eop
->type
= EOT_NOTHING
;
461 eop
->type
= EOT_DB_STRING_FREE
;
463 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
464 &eop
->stringval
, func
);
465 if (eop
->stringlen
== (size_t)-1) {
466 nasm_error(ERR_NONFATAL
, "invalid string for transform");
467 eop
->type
= EOT_NOTHING
;
470 if (parens
&& i
&& i
!= ')') {
471 i
= stdscan(NULL
, &tokval
);
473 nasm_error(ERR_NONFATAL
, "unterminated %s function",
478 i
= stdscan(NULL
, &tokval
);
479 } else if (i
== '-' || i
== '+') {
480 char *save
= stdscan_get();
482 sign
= (i
== '-') ? -1 : 1;
483 i
= stdscan(NULL
, &tokval
);
484 if (i
!= TOKEN_FLOAT
) {
486 i
= tokval
.t_type
= token
;
491 } else if (i
== TOKEN_FLOAT
) {
493 eop
->type
= EOT_DB_STRING
;
494 result
->eops_float
= true;
496 eop
->stringlen
= idata_bytes(result
->opcode
);
497 if (eop
->stringlen
> 16) {
498 nasm_error(ERR_NONFATAL
, "floating-point constant"
499 " encountered in DY or DZ instruction");
501 } else if (eop
->stringlen
< 1) {
502 nasm_error(ERR_NONFATAL
, "floating-point constant"
503 " encountered in unknown instruction");
505 * fix suggested by Pedro Gimeno... original line was:
506 * eop->type = EOT_NOTHING;
511 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
514 eop
->stringval
= (char *)eop
+ sizeof(extop
);
515 if (!eop
->stringlen
||
516 !float_const(tokval
.t_charptr
, sign
,
517 (uint8_t *)eop
->stringval
,
518 eop
->stringlen
, nasm_error
))
519 eop
->type
= EOT_NOTHING
;
520 i
= stdscan(NULL
, &tokval
); /* eat the comma */
522 /* anything else, assume it is an expression */
526 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
527 critical
, nasm_error
, NULL
);
529 if (!value
) { /* error in evaluator */
530 result
->opcode
= I_none
; /* unrecoverable parse error: */
531 return result
; /* ignore this instruction */
533 if (is_unknown(value
)) {
534 eop
->type
= EOT_DB_NUMBER
;
535 eop
->offset
= 0; /* doesn't matter what we put */
536 eop
->segment
= eop
->wrt
= NO_SEG
; /* likewise */
537 } else if (is_reloc(value
)) {
538 eop
->type
= EOT_DB_NUMBER
;
539 eop
->offset
= reloc_value(value
);
540 eop
->segment
= reloc_seg(value
);
541 eop
->wrt
= reloc_wrt(value
);
543 nasm_error(ERR_NONFATAL
,
544 "operand %d: expression is not simple"
545 " or relocatable", oper_num
);
550 * We're about to call stdscan(), which will eat the
551 * comma that we're currently sitting on between
552 * arguments. However, we'd better check first that it
555 if (i
== TOKEN_EOS
) /* also could be EOL */
558 nasm_error(ERR_NONFATAL
, "comma expected after operand %d",
560 result
->opcode
= I_none
;/* unrecoverable parse error: */
561 return result
; /* ignore this instruction */
565 if (result
->opcode
== I_INCBIN
) {
567 * Correct syntax for INCBIN is that there should be
568 * one string operand, followed by one or two numeric
571 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
572 nasm_error(ERR_NONFATAL
, "`incbin' expects a file name");
573 else if (result
->eops
->next
&&
574 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
575 nasm_error(ERR_NONFATAL
, "`incbin': second parameter is"
577 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
578 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
579 nasm_error(ERR_NONFATAL
, "`incbin': third parameter is"
581 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
582 result
->eops
->next
->next
->next
)
583 nasm_error(ERR_NONFATAL
,
584 "`incbin': more than three parameters");
588 * If we reach here, one of the above errors happened.
589 * Throw the instruction away.
591 result
->opcode
= I_none
;
593 } else /* DB ... */ if (oper_num
== 0)
594 nasm_error(ERR_WARNING
| ERR_PASS1
,
595 "no operand for data declaration");
597 result
->operands
= oper_num
;
603 * Now we begin to parse the operands. There may be up to four
604 * of these, separated by commas, and terminated by a zero token.
607 for (operand
= 0; operand
< MAX_OPERANDS
; operand
++) {
608 expr
*value
; /* used most of the time */
609 int mref
; /* is this going to be a memory ref? */
610 int bracket
; /* is it a [] mref, or a & mref? */
612 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
614 result
->oprs
[operand
].disp_size
= 0; /* have to zero this whatever */
615 result
->oprs
[operand
].eaflags
= 0; /* and this */
616 result
->oprs
[operand
].opflags
= 0;
617 result
->oprs
[operand
].decoflags
= 0;
619 i
= stdscan(NULL
, &tokval
);
621 break; /* end of operands: get out of here */
622 else if (first
&& i
== ':') {
623 insn_is_label
= true;
627 result
->oprs
[operand
].type
= 0; /* so far, no override */
628 while (i
== TOKEN_SPECIAL
) { /* size specifiers */
629 switch ((int)tokval
.t_integer
) {
631 if (!setsize
) /* we want to use only the first */
632 result
->oprs
[operand
].type
|= BITS8
;
637 result
->oprs
[operand
].type
|= BITS16
;
643 result
->oprs
[operand
].type
|= BITS32
;
648 result
->oprs
[operand
].type
|= BITS64
;
653 result
->oprs
[operand
].type
|= BITS80
;
658 result
->oprs
[operand
].type
|= BITS128
;
663 result
->oprs
[operand
].type
|= BITS256
;
668 result
->oprs
[operand
].type
|= BITS512
;
672 result
->oprs
[operand
].type
|= TO
;
675 result
->oprs
[operand
].type
|= STRICT
;
678 result
->oprs
[operand
].type
|= FAR
;
681 result
->oprs
[operand
].type
|= NEAR
;
684 result
->oprs
[operand
].type
|= SHORT
;
687 nasm_error(ERR_NONFATAL
, "invalid operand size specification");
689 i
= stdscan(NULL
, &tokval
);
692 if (i
== '[' || i
== '&') { /* memory reference */
694 bracket
= (i
== '[');
695 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
696 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
697 process_size_override(result
, operand
);
698 i
= stdscan(NULL
, &tokval
);
700 } else { /* immediate operand, or register */
702 bracket
= false; /* placate optimisers */
705 if ((result
->oprs
[operand
].type
& FAR
) && !mref
&&
706 result
->opcode
!= I_JMP
&& result
->opcode
!= I_CALL
) {
707 nasm_error(ERR_NONFATAL
, "invalid use of FAR operand specifier");
710 value
= evaluate(stdscan
, NULL
, &tokval
,
711 &result
->oprs
[operand
].opflags
,
712 critical
, nasm_error
, &hints
);
714 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
715 result
->forw_ref
= true;
717 if (!value
) { /* nasm_error in evaluator */
718 result
->opcode
= I_none
; /* unrecoverable parse error: */
719 return result
; /* ignore this instruction */
721 if (i
== ':' && mref
) { /* it was seg:offset */
723 * Process the segment override.
725 if (value
[1].type
!= 0 ||
727 !IS_SREG(value
->type
))
728 nasm_error(ERR_NONFATAL
, "invalid segment override");
729 else if (result
->prefixes
[PPS_SEG
])
730 nasm_error(ERR_NONFATAL
,
731 "instruction has conflicting segment overrides");
733 result
->prefixes
[PPS_SEG
] = value
->type
;
734 if (IS_FSGS(value
->type
))
735 result
->oprs
[operand
].eaflags
|= EAF_FSGS
;
738 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
739 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_PREFIX
) {
740 process_size_override(result
, operand
);
741 i
= stdscan(NULL
, &tokval
);
743 value
= evaluate(stdscan
, NULL
, &tokval
,
744 &result
->oprs
[operand
].opflags
,
745 critical
, nasm_error
, &hints
);
747 if (result
->oprs
[operand
].opflags
& OPFLAG_FORWARD
) {
748 result
->forw_ref
= true;
750 /* and get the offset */
751 if (!value
) { /* but, error in evaluator */
752 result
->opcode
= I_none
; /* unrecoverable parse error: */
753 return result
; /* ignore this instruction */
758 if (mref
&& bracket
) { /* find ] at the end */
760 nasm_error(ERR_NONFATAL
, "parser: expecting ]");
762 } else { /* we got the required ] */
763 i
= stdscan(NULL
, &tokval
);
764 if ((i
== TOKEN_DECORATOR
) || (i
== TOKEN_OPMASK
)) {
766 * according to AVX512 spec, broacast or opmask decorator
767 * is expected for memory reference operands
769 if (tokval
.t_flag
& TFLAG_BRDCAST
) {
770 brace_flags
|= GEN_BRDCAST(0);
771 i
= stdscan(NULL
, &tokval
);
772 } else if (i
== TOKEN_OPMASK
) {
773 brace_flags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
774 i
= stdscan(NULL
, &tokval
);
776 nasm_error(ERR_NONFATAL
, "broadcast or opmask "
777 "decorator expected inside braces");
782 if (i
!= 0 && i
!= ',') {
783 nasm_error(ERR_NONFATAL
, "comma or end of line expected");
787 } else { /* immediate operand */
788 if (i
!= 0 && i
!= ',' && i
!= ':' &&
789 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
790 nasm_error(ERR_NONFATAL
, "comma, colon, decorator or end of "
791 "line expected after operand");
793 } else if (i
== ':') {
794 result
->oprs
[operand
].type
|= COLON
;
795 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
796 /* parse opmask (and zeroing) after an operand */
797 recover
= parse_braces(&brace_flags
);
801 do { /* error recovery */
802 i
= stdscan(NULL
, &tokval
);
803 } while (i
!= 0 && i
!= ',');
807 * now convert the exprs returned from evaluate()
808 * into operand descriptions...
811 if (mref
) { /* it's a memory reference */
813 int b
, i
, s
; /* basereg, indexreg, scale */
814 int64_t o
; /* offset */
816 b
= i
= -1, o
= s
= 0;
817 result
->oprs
[operand
].hintbase
= hints
.base
;
818 result
->oprs
[operand
].hinttype
= hints
.type
;
820 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* this bit's a register */
821 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
823 if (is_gpr
&& e
->value
== 1)
824 b
= e
->type
; /* It can be basereg */
825 else /* No, it has to be indexreg */
826 i
= e
->type
, s
= e
->value
;
829 if (e
->type
&& e
->type
<= EXPR_REG_END
) { /* it's a 2nd register */
830 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
832 if (b
!= -1) /* If the first was the base, ... */
833 i
= e
->type
, s
= e
->value
; /* second has to be indexreg */
835 else if (!is_gpr
|| e
->value
!= 1) {
836 /* If both want to be index */
837 nasm_error(ERR_NONFATAL
,
838 "invalid effective address: two index registers");
839 result
->opcode
= I_none
;
845 if (e
->type
!= 0) { /* is there an offset? */
846 if (e
->type
<= EXPR_REG_END
) { /* in fact, is there an error? */
847 nasm_error(ERR_NONFATAL
,
848 "beroset-p-603-invalid effective address");
849 result
->opcode
= I_none
;
852 if (e
->type
== EXPR_UNKNOWN
) {
853 result
->oprs
[operand
].opflags
|= OPFLAG_UNKNOWN
;
854 o
= 0; /* doesn't matter what */
855 result
->oprs
[operand
].wrt
= NO_SEG
; /* nor this */
856 result
->oprs
[operand
].segment
= NO_SEG
; /* or this */
858 e
++; /* go to the end of the line */
860 if (e
->type
== EXPR_SIMPLE
) {
864 if (e
->type
== EXPR_WRT
) {
865 result
->oprs
[operand
].wrt
= e
->value
;
868 result
->oprs
[operand
].wrt
= NO_SEG
;
870 * Look for a segment base type.
872 if (e
->type
&& e
->type
< EXPR_SEGBASE
) {
873 nasm_error(ERR_NONFATAL
,
874 "beroset-p-630-invalid effective address");
875 result
->opcode
= I_none
;
878 while (e
->type
&& e
->value
== 0)
880 if (e
->type
&& e
->value
!= 1) {
881 nasm_error(ERR_NONFATAL
,
882 "beroset-p-637-invalid effective address");
883 result
->opcode
= I_none
;
887 result
->oprs
[operand
].segment
=
888 e
->type
- EXPR_SEGBASE
;
891 result
->oprs
[operand
].segment
= NO_SEG
;
892 while (e
->type
&& e
->value
== 0)
895 nasm_error(ERR_NONFATAL
,
896 "beroset-p-650-invalid effective address");
897 result
->opcode
= I_none
;
904 result
->oprs
[operand
].wrt
= NO_SEG
;
905 result
->oprs
[operand
].segment
= NO_SEG
;
908 if (e
->type
!= 0) { /* there'd better be nothing left! */
909 nasm_error(ERR_NONFATAL
,
910 "beroset-p-663-invalid effective address");
911 result
->opcode
= I_none
;
915 /* It is memory, but it can match any r/m operand */
916 result
->oprs
[operand
].type
|= MEMORY_ANY
;
918 if (b
== -1 && (i
== -1 || s
== 0)) {
919 int is_rel
= globalbits
== 64 &&
920 !(result
->oprs
[operand
].eaflags
& EAF_ABS
) &&
922 !(result
->oprs
[operand
].eaflags
& EAF_FSGS
)) ||
923 (result
->oprs
[operand
].eaflags
& EAF_REL
));
925 result
->oprs
[operand
].type
|= is_rel
? IP_REL
: MEM_OFFS
;
929 opflags_t iclass
= nasm_reg_flags
[i
];
931 if (is_class(XMMREG
,iclass
))
932 result
->oprs
[operand
].type
|= XMEM
;
933 else if (is_class(YMMREG
,iclass
))
934 result
->oprs
[operand
].type
|= YMEM
;
935 else if (is_class(ZMMREG
,iclass
))
936 result
->oprs
[operand
].type
|= ZMEM
;
939 result
->oprs
[operand
].basereg
= b
;
940 result
->oprs
[operand
].indexreg
= i
;
941 result
->oprs
[operand
].scale
= s
;
942 result
->oprs
[operand
].offset
= o
;
943 result
->oprs
[operand
].decoflags
|= brace_flags
;
944 } else { /* it's not a memory reference */
945 if (is_just_unknown(value
)) { /* it's immediate but unknown */
946 result
->oprs
[operand
].type
|= IMMEDIATE
;
947 result
->oprs
[operand
].opflags
|= OPFLAG_UNKNOWN
;
948 result
->oprs
[operand
].offset
= 0; /* don't care */
949 result
->oprs
[operand
].segment
= NO_SEG
; /* don't care again */
950 result
->oprs
[operand
].wrt
= NO_SEG
; /* still don't care */
952 if(optimizing
>= 0 && !(result
->oprs
[operand
].type
& STRICT
)) {
954 result
->oprs
[operand
].type
|=
955 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
957 } else if (is_reloc(value
)) { /* it's immediate */
958 result
->oprs
[operand
].type
|= IMMEDIATE
;
959 result
->oprs
[operand
].offset
= reloc_value(value
);
960 result
->oprs
[operand
].segment
= reloc_seg(value
);
961 result
->oprs
[operand
].wrt
= reloc_wrt(value
);
963 if (is_simple(value
)) {
964 uint64_t n
= reloc_value(value
);
966 result
->oprs
[operand
].type
|= UNITY
;
967 if (optimizing
>= 0 &&
968 !(result
->oprs
[operand
].type
& STRICT
)) {
969 if ((uint32_t) (n
+ 128) <= 255)
970 result
->oprs
[operand
].type
|= SBYTEDWORD
;
971 if ((uint16_t) (n
+ 128) <= 255)
972 result
->oprs
[operand
].type
|= SBYTEWORD
;
974 result
->oprs
[operand
].type
|= UDWORD
;
975 if (n
+ 0x80000000 <= 0xFFFFFFFF)
976 result
->oprs
[operand
].type
|= SDWORD
;
979 } else if(value
->type
== EXPR_RDSAE
) {
981 * it's not an operand but a rounding or SAE decorator.
982 * put the decorator information in the (opflag_t) type field
983 * of previous operand.
986 switch (value
->value
) {
992 result
->oprs
[operand
].decoflags
|=
993 (value
->value
== BRC_SAE
? SAE
: ER
);
994 result
->evex_rm
= value
->value
;
997 nasm_error(ERR_NONFATAL
, "invalid decorator");
1000 } else { /* it's a register */
1003 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1004 nasm_error(ERR_NONFATAL
, "invalid operand type");
1005 result
->opcode
= I_none
;
1010 * check that its only 1 register, not an expression...
1012 for (i
= 1; value
[i
].type
; i
++)
1013 if (value
[i
].value
) {
1014 nasm_error(ERR_NONFATAL
, "invalid operand type");
1015 result
->opcode
= I_none
;
1019 /* clear overrides, except TO which applies to FPU regs */
1020 if (result
->oprs
[operand
].type
& ~TO
) {
1022 * we want to produce a warning iff the specified size
1023 * is different from the register size
1025 rs
= result
->oprs
[operand
].type
& SIZE_MASK
;
1029 result
->oprs
[operand
].type
&= TO
;
1030 result
->oprs
[operand
].type
|= REGISTER
;
1031 result
->oprs
[operand
].type
|= nasm_reg_flags
[value
->type
];
1032 result
->oprs
[operand
].decoflags
|= brace_flags
;
1033 result
->oprs
[operand
].basereg
= value
->type
;
1035 if (rs
&& (result
->oprs
[operand
].type
& SIZE_MASK
) != rs
)
1036 nasm_error(ERR_WARNING
| ERR_PASS1
,
1037 "register size specification ignored");
1041 /* remember the position of operand having broadcasting/ER mode */
1042 if (result
->oprs
[operand
].decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1043 result
->evex_brerop
= operand
;
1046 result
->operands
= operand
; /* set operand count */
1048 /* clear remaining operands */
1049 while (operand
< MAX_OPERANDS
)
1050 result
->oprs
[operand
++].type
= 0;
1053 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
1055 switch (result
->opcode
) {
1057 result
->opcode
= I_RESB
;
1058 result
->oprs
[0].offset
*= 2;
1061 result
->opcode
= I_RESB
;
1062 result
->oprs
[0].offset
*= 4;
1065 result
->opcode
= I_RESB
;
1066 result
->oprs
[0].offset
*= 8;
1069 result
->opcode
= I_RESB
;
1070 result
->oprs
[0].offset
*= 10;
1073 result
->opcode
= I_RESB
;
1074 result
->oprs
[0].offset
*= 16;
1077 result
->opcode
= I_RESB
;
1078 result
->oprs
[0].offset
*= 32;
1081 result
->opcode
= I_RESB
;
1082 result
->oprs
[0].offset
*= 64;
1091 static int is_comma_next(void)
1098 i
= stdscan(NULL
, &tv
);
1101 return (i
== ',' || i
== ';' || !i
);
1104 void cleanup_insn(insn
* i
)
1108 while ((e
= i
->eops
)) {
1110 if (e
->type
== EOT_DB_STRING_FREE
)
1111 nasm_free(e
->stringval
);