1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2019 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
54 static int is_comma_next(void);
56 static struct tokenval tokval
;
58 static int prefix_slot(int prefix
)
97 nasm_panic("Invalid value %d passed to prefix_slot()", prefix
);
102 static void process_size_override(insn
*result
, operand
*op
)
104 if (tasm_compatible_mode
) {
105 switch (tokval
.t_integer
) {
106 /* For TASM compatibility a size override inside the
107 * brackets changes the size of the operand, not the
108 * address type of the operand as it does in standard
109 * NASM syntax. Hence:
111 * mov eax,[DWORD val]
113 * is valid syntax in TASM compatibility mode. Note that
114 * you lose the ability to override the default address
115 * type for the instruction, but we never use anything
116 * but 32-bit flat model addressing in our code.
138 nasm_nonfatal("invalid operand size specification");
142 /* Standard NASM compatible syntax */
143 switch (tokval
.t_integer
) {
145 op
->eaflags
|= EAF_TIMESTWO
;
148 op
->eaflags
|= EAF_REL
;
151 op
->eaflags
|= EAF_ABS
;
155 op
->eaflags
|= EAF_BYTEOFFS
;
160 if (result
->prefixes
[PPS_ASIZE
] &&
161 result
->prefixes
[PPS_ASIZE
] != tokval
.t_integer
)
162 nasm_nonfatal("conflicting address size specifications");
164 result
->prefixes
[PPS_ASIZE
] = tokval
.t_integer
;
168 op
->eaflags
|= EAF_WORDOFFS
;
173 op
->eaflags
|= EAF_WORDOFFS
;
177 op
->eaflags
|= EAF_WORDOFFS
;
180 nasm_nonfatal("invalid size specification in"
181 " effective address");
188 * Brace decorators are are parsed here. opmask and zeroing
189 * decorators can be placed in any order. e.g. zmm1 {k2}{z} or zmm2
190 * {z}{k3} decorator(s) are placed at the end of an operand.
192 static bool parse_braces(decoflags_t
*decoflags
)
201 if (*decoflags
& OPMASK_MASK
) {
202 nasm_nonfatal("opmask k%"PRIu64
" is already set",
203 *decoflags
& OPMASK_MASK
);
204 *decoflags
&= ~OPMASK_MASK
;
206 *decoflags
|= VAL_OPMASK(nasm_regvals
[tokval
.t_integer
]);
208 case TOKEN_DECORATOR
:
209 j
= tokval
.t_integer
;
212 *decoflags
|= Z_MASK
;
218 *decoflags
|= BRDCAST_MASK
| VAL_BRNUM(j
- BRC_1TO2
);
221 nasm_nonfatal("{%s} is not an expected decorator",
230 nasm_nonfatal("only a series of valid decorators expected");
233 i
= stdscan(NULL
, &tokval
);
237 static inline const expr
*next_expr(const expr
*e
, const expr
**next_list
)
251 static inline void init_operand(operand
*op
)
253 memset(op
, 0, sizeof *op
);
257 op
->segment
= NO_SEG
;
261 static int parse_mref(operand
*op
, const expr
*e
)
263 int b
, i
, s
; /* basereg, indexreg, scale */
264 int64_t o
; /* offset */
271 for (; e
->type
; e
++) {
272 if (e
->type
<= EXPR_REG_END
) {
273 bool is_gpr
= is_class(REG_GPR
,nasm_reg_flags
[e
->type
]);
275 if (is_gpr
&& e
->value
== 1 && b
== -1) {
276 /* It can be basereg */
278 } else if (i
== -1) {
279 /* Must be index register */
284 nasm_nonfatal("invalid effective address: two index registers");
286 nasm_nonfatal("invalid effective address: impossible register");
288 nasm_nonfatal("invalid effective address: too many registers");
291 } else if (e
->type
== EXPR_UNKNOWN
) {
292 op
->opflags
|= OPFLAG_UNKNOWN
;
293 } else if (e
->type
== EXPR_SIMPLE
) {
295 } else if (e
->type
== EXPR_WRT
) {
297 } else if (e
->type
>= EXPR_SEGBASE
) {
299 if (op
->segment
!= NO_SEG
) {
300 nasm_nonfatal("invalid effective address: multiple base segments");
303 op
->segment
= e
->type
- EXPR_SEGBASE
;
304 } else if (e
->value
== -1 &&
305 e
->type
== location
.segment
+ EXPR_SEGBASE
&&
306 !(op
->opflags
& OPFLAG_RELATIVE
)) {
307 op
->opflags
|= OPFLAG_RELATIVE
;
309 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
313 nasm_nonfatal("invalid effective address: bad subexpression type");
325 static void mref_set_optype(operand
*op
)
328 int i
= op
->indexreg
;
331 /* It is memory, but it can match any r/m operand */
332 op
->type
|= MEMORY_ANY
;
334 if (b
== -1 && (i
== -1 || s
== 0)) {
335 int is_rel
= globalbits
== 64 &&
336 !(op
->eaflags
& EAF_ABS
) &&
338 !(op
->eaflags
& EAF_FSGS
)) ||
339 (op
->eaflags
& EAF_REL
));
341 op
->type
|= is_rel
? IP_REL
: MEM_OFFS
;
345 opflags_t iclass
= nasm_reg_flags
[i
];
347 if (is_class(XMMREG
,iclass
))
349 else if (is_class(YMMREG
,iclass
))
351 else if (is_class(ZMMREG
,iclass
))
357 * Convert an expression vector returned from evaluate() into an
358 * extop structure. Return zero on success.
360 static int value_to_extop(expr
* vect
, extop
*eop
, int32_t myseg
)
362 eop
->type
= EOT_DB_NUMBER
;
364 eop
->segment
= eop
->wrt
= NO_SEG
;
365 eop
->relative
= false;
367 for (; vect
->type
; vect
++) {
368 if (!vect
->value
) /* zero term, safe to ignore */
371 if (vect
->type
<= EXPR_REG_END
) /* false if a register is present */
374 if (vect
->type
== EXPR_UNKNOWN
) /* something we can't resolve yet */
377 if (vect
->type
== EXPR_SIMPLE
) {
378 /* Simple number expression */
379 eop
->offset
+= vect
->value
;
382 if (eop
->wrt
== NO_SEG
&& !eop
->relative
&& vect
->type
== EXPR_WRT
) {
384 eop
->wrt
= vect
->value
;
388 if (!eop
->relative
&&
389 vect
->type
== EXPR_SEGBASE
+ myseg
&& vect
->value
== -1) {
390 /* Expression of the form: foo - $ */
391 eop
->relative
= true;
395 if (eop
->segment
== NO_SEG
&& vect
->type
>= EXPR_SEGBASE
&&
397 eop
->segment
= vect
->type
- EXPR_SEGBASE
;
401 /* Otherwise, badness */
405 /* We got to the end and it was all okay */
409 insn
*parse_line(char *buffer
, insn
*result
)
411 bool insn_is_label
= false;
412 struct eval_hints hints
;
420 nasm_static_assert(P_none
== 0);
424 result
->forw_ref
= false;
428 i
= stdscan(NULL
, &tokval
);
430 memset(result
->prefixes
, P_none
, sizeof(result
->prefixes
));
431 result
->times
= 1; /* No TIMES either yet */
432 result
->label
= NULL
; /* Assume no label */
433 result
->eops
= NULL
; /* must do this, whatever happens */
434 result
->operands
= 0; /* must initialize this */
435 result
->evex_rm
= 0; /* Ensure EVEX rounding mode is reset */
436 result
->evex_brerop
= -1; /* Reset EVEX broadcasting/ER op position */
438 /* Ignore blank lines */
445 (i
!= TOKEN_REG
|| !IS_SREG(tokval
.t_integer
))) {
446 nasm_nonfatal("label or instruction expected at start of line");
450 if (i
== TOKEN_ID
|| (insn_is_label
&& i
== TOKEN_INSN
)) {
451 /* there's a label here */
453 result
->label
= tokval
.t_charptr
;
454 i
= stdscan(NULL
, &tokval
);
455 if (i
== ':') { /* skip over the optional colon */
456 i
= stdscan(NULL
, &tokval
);
459 *!label-orphan [on] labels alone on lines without trailing `:'
461 *! warns about source lines which contain no instruction but define
462 *! a label without a trailing colon. This is most likely indicative
463 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
465 nasm_warn(WARN_LABEL_ORPHAN
,
466 "label alone on a line without a colon might be in error");
468 if (i
!= TOKEN_INSN
|| tokval
.t_integer
!= I_EQU
) {
470 * FIXME: location.segment could be NO_SEG, in which case
471 * it is possible we should be passing 'absolute.segment'. Look into this.
472 * Work out whether that is *really* what we should be doing.
473 * Generally fix things. I think this is right as it is, but
474 * am still not certain.
476 define_label(result
->label
,
477 in_absolute
? absolute
.segment
: location
.segment
,
478 location
.offset
, true);
482 /* Just a label here */
486 while (i
== TOKEN_PREFIX
||
487 (i
== TOKEN_REG
&& IS_SREG(tokval
.t_integer
))) {
491 * Handle special case: the TIMES prefix.
493 if (i
== TOKEN_PREFIX
&& tokval
.t_integer
== P_TIMES
) {
496 i
= stdscan(NULL
, &tokval
);
497 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass_stable(), NULL
);
499 if (!value
) /* Error in evaluator */
501 if (!is_simple(value
)) {
502 nasm_nonfatal("non-constant argument supplied to TIMES");
505 result
->times
= value
->value
;
506 if (value
->value
< 0) {
507 nasm_nonfatalf(ERR_PASS2
, "TIMES value %"PRId64
" is negative", value
->value
);
512 int slot
= prefix_slot(tokval
.t_integer
);
513 if (result
->prefixes
[slot
]) {
514 if (result
->prefixes
[slot
] == tokval
.t_integer
)
515 nasm_warn(WARN_OTHER
, "instruction has redundant prefixes");
517 nasm_nonfatal("instruction has conflicting prefixes");
519 result
->prefixes
[slot
] = tokval
.t_integer
;
520 i
= stdscan(NULL
, &tokval
);
524 if (i
!= TOKEN_INSN
) {
528 for (j
= 0; j
< MAXPREFIX
; j
++) {
529 if ((pfx
= result
->prefixes
[j
]) != P_none
)
533 if (i
== 0 && pfx
!= P_none
) {
535 * Instruction prefixes are present, but no actual
536 * instruction. This is allowed: at this point we
537 * invent a notional instruction of RESB 0.
539 result
->opcode
= I_RESB
;
540 result
->operands
= 1;
541 nasm_zero(result
->oprs
);
542 result
->oprs
[0].type
= IMMEDIATE
;
543 result
->oprs
[0].offset
= 0L;
544 result
->oprs
[0].segment
= result
->oprs
[0].wrt
= NO_SEG
;
547 nasm_nonfatal("parser: instruction expected");
552 result
->opcode
= tokval
.t_integer
;
553 result
->condition
= tokval
.t_inttwo
;
556 * INCBIN cannot be satisfied with incorrectly
557 * evaluated operands, since the correct values _must_ be known
558 * on the first pass. Hence, even in pass one, we set the
559 * `critical' flag on calling evaluate(), so that it will bomb
560 * out on undefined symbols.
562 critical
= pass_final() || (result
->opcode
== I_INCBIN
);
564 if (opcode_is_db(result
->opcode
) || result
->opcode
== I_INCBIN
) {
565 extop
*eop
, **tail
= &result
->eops
, **fixptr
;
569 result
->eops_float
= false;
572 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
575 i
= stdscan(NULL
, &tokval
);
578 else if (first
&& i
== ':') {
579 insn_is_label
= true;
584 eop
= *tail
= nasm_malloc(sizeof(extop
));
587 eop
->type
= EOT_NOTHING
;
592 * is_comma_next() here is to distinguish this from
593 * a string used as part of an expression...
595 if (i
== TOKEN_STR
&& is_comma_next()) {
596 eop
->type
= EOT_DB_STRING
;
597 eop
->stringval
= tokval
.t_charptr
;
598 eop
->stringlen
= tokval
.t_inttwo
;
599 i
= stdscan(NULL
, &tokval
); /* eat the comma */
600 } else if (i
== TOKEN_STRFUNC
) {
602 const char *funcname
= tokval
.t_charptr
;
603 enum strfunc func
= tokval
.t_integer
;
604 i
= stdscan(NULL
, &tokval
);
607 i
= stdscan(NULL
, &tokval
);
609 if (i
!= TOKEN_STR
) {
610 nasm_nonfatal("%s must be followed by a string constant",
612 eop
->type
= EOT_NOTHING
;
614 eop
->type
= EOT_DB_STRING_FREE
;
616 string_transform(tokval
.t_charptr
, tokval
.t_inttwo
,
617 &eop
->stringval
, func
);
618 if (eop
->stringlen
== (size_t)-1) {
619 nasm_nonfatal("invalid input string to %s", funcname
);
620 eop
->type
= EOT_NOTHING
;
623 if (parens
&& i
&& i
!= ')') {
624 i
= stdscan(NULL
, &tokval
);
626 nasm_nonfatal("unterminated %s function", funcname
);
629 i
= stdscan(NULL
, &tokval
);
630 } else if (i
== '-' || i
== '+') {
631 char *save
= stdscan_get();
633 sign
= (i
== '-') ? -1 : 1;
634 i
= stdscan(NULL
, &tokval
);
635 if (i
!= TOKEN_FLOAT
) {
637 i
= tokval
.t_type
= token
;
642 } else if (i
== TOKEN_FLOAT
) {
644 eop
->type
= EOT_DB_STRING
;
645 result
->eops_float
= true;
647 eop
->stringlen
= db_bytes(result
->opcode
);
648 if (eop
->stringlen
> 16) {
649 nasm_nonfatal("floating-point constant"
650 " encountered in DY or DZ instruction");
652 } else if (eop
->stringlen
< 1) {
653 nasm_nonfatal("floating-point constant"
654 " encountered in unknown instruction");
656 * fix suggested by Pedro Gimeno... original line was:
657 * eop->type = EOT_NOTHING;
662 eop
= nasm_realloc(eop
, sizeof(extop
) + eop
->stringlen
);
665 eop
->stringval
= (char *)eop
+ sizeof(extop
);
666 if (!eop
->stringlen
||
667 !float_const(tokval
.t_charptr
, sign
,
668 (uint8_t *)eop
->stringval
, eop
->stringlen
))
669 eop
->type
= EOT_NOTHING
;
670 i
= stdscan(NULL
, &tokval
); /* eat the comma */
672 /* anything else, assume it is an expression */
676 value
= evaluate(stdscan
, NULL
, &tokval
, NULL
,
679 if (!value
) /* Error in evaluator */
681 if (value_to_extop(value
, eop
, location
.segment
)) {
682 nasm_nonfatal("operand %d: expression is not simple or relocatable",
688 * We're about to call stdscan(), which will eat the
689 * comma that we're currently sitting on between
690 * arguments. However, we'd better check first that it
693 if (i
== TOKEN_EOS
) /* also could be EOL */
696 nasm_nonfatal("comma expected after operand %d", oper_num
);
701 if (result
->opcode
== I_INCBIN
) {
703 * Correct syntax for INCBIN is that there should be
704 * one string operand, followed by one or two numeric
707 if (!result
->eops
|| result
->eops
->type
!= EOT_DB_STRING
)
708 nasm_nonfatal("`incbin' expects a file name");
709 else if (result
->eops
->next
&&
710 result
->eops
->next
->type
!= EOT_DB_NUMBER
)
711 nasm_nonfatal("`incbin': second parameter is"
713 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
714 result
->eops
->next
->next
->type
!= EOT_DB_NUMBER
)
715 nasm_nonfatal("`incbin': third parameter is"
717 else if (result
->eops
->next
&& result
->eops
->next
->next
&&
718 result
->eops
->next
->next
->next
)
719 nasm_nonfatal("`incbin': more than three parameters");
723 * If we reach here, one of the above errors happened.
724 * Throw the instruction away.
727 } else /* DB ... */ if (oper_num
== 0)
728 nasm_warn(WARN_OTHER
, "no operand for data declaration");
730 result
->operands
= oper_num
;
736 * Now we begin to parse the operands. There may be up to four
737 * of these, separated by commas, and terminated by a zero token.
739 far_jmp_ok
= result
->opcode
== I_JMP
|| result
->opcode
== I_CALL
;
741 for (opnum
= 0; opnum
< MAX_OPERANDS
; opnum
++) {
742 operand
*op
= &result
->oprs
[opnum
];
743 expr
*value
; /* used most of the time */
744 bool mref
= false; /* is this going to be a memory ref? */
745 int bracket
= 0; /* is it a [] mref, or a "naked" mref? */
746 bool mib
; /* compound (mib) mref? */
748 decoflags_t brace_flags
= 0; /* flags for decorators in braces */
752 i
= stdscan(NULL
, &tokval
);
754 break; /* end of operands: get out of here */
755 else if (first
&& i
== ':') {
756 insn_is_label
= true;
760 op
->type
= 0; /* so far, no override */
761 /* size specifiers */
762 while (i
== TOKEN_SPECIAL
|| i
== TOKEN_SIZE
) {
763 switch (tokval
.t_integer
) {
765 if (!setsize
) /* we want to use only the first */
821 nasm_nonfatal("invalid operand size specification");
823 i
= stdscan(NULL
, &tokval
);
826 if (i
== '[' || i
== TOKEN_MASM_PTR
|| i
== '&') {
827 /* memory reference */
829 bracket
+= (i
== '[');
830 i
= stdscan(NULL
, &tokval
);
843 process_size_override(result
, op
);
851 tokval
.t_type
= TOKEN_NUM
;
852 tokval
.t_integer
= 0;
853 stdscan_set(stdscan_get() - 1); /* rewind the comma */
857 case TOKEN_MASM_FLAT
:
858 i
= stdscan(NULL
, &tokval
);
860 nasm_nonfatal("unknown use of FLAT in MASM emulation");
872 i
= stdscan(NULL
, &tokval
);
876 value
= evaluate(stdscan
, NULL
, &tokval
,
877 &op
->opflags
, critical
, &hints
);
879 if (op
->opflags
& OPFLAG_FORWARD
) {
880 result
->forw_ref
= true;
882 if (!value
) /* Error in evaluator */
885 if (i
== '[' && !bracket
) {
886 /* displacement[regs] syntax */
888 parse_mref(op
, value
); /* Process what we have so far */
892 if (i
== ':' && (mref
|| !far_jmp_ok
)) {
893 /* segment override? */
897 * Process the segment override.
899 if (value
[1].type
!= 0 ||
901 !IS_SREG(value
->type
))
902 nasm_nonfatal("invalid segment override");
903 else if (result
->prefixes
[PPS_SEG
])
904 nasm_nonfatal("instruction has conflicting segment overrides");
906 result
->prefixes
[PPS_SEG
] = value
->type
;
907 if (IS_FSGS(value
->type
))
908 op
->eaflags
|= EAF_FSGS
;
911 i
= stdscan(NULL
, &tokval
); /* then skip the colon */
916 if (mref
&& bracket
&& i
== ',') {
917 /* [seg:base+offset,index*scale] syntax (mib) */
918 operand o2
; /* Index operand */
920 if (parse_mref(op
, value
))
923 i
= stdscan(NULL
, &tokval
); /* Eat comma */
924 value
= evaluate(stdscan
, NULL
, &tokval
, &op
->opflags
,
931 if (parse_mref(&o2
, value
))
934 if (o2
.basereg
!= -1 && o2
.indexreg
== -1) {
935 o2
.indexreg
= o2
.basereg
;
940 if (op
->indexreg
!= -1 || o2
.basereg
!= -1 || o2
.offset
!= 0 ||
941 o2
.segment
!= NO_SEG
|| o2
.wrt
!= NO_SEG
) {
942 nasm_nonfatal("invalid mib expression");
946 op
->indexreg
= o2
.indexreg
;
947 op
->scale
= o2
.scale
;
949 if (op
->basereg
!= -1) {
950 op
->hintbase
= op
->basereg
;
951 op
->hinttype
= EAH_MAKEBASE
;
952 } else if (op
->indexreg
!= -1) {
953 op
->hintbase
= op
->indexreg
;
954 op
->hinttype
= EAH_NOTBASE
;
957 op
->hinttype
= EAH_NOHINT
;
968 i
= stdscan(NULL
, &tokval
);
970 nasm_nonfatal("expecting ] at end of memory operand");
973 } else if (bracket
== 0) {
975 } else if (bracket
> 0) {
976 nasm_nonfatal("excess brackets in memory operand");
978 } else if (bracket
< 0) {
979 nasm_nonfatal("unmatched ] in memory operand");
983 if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
984 /* parse opmask (and zeroing) after an operand */
985 recover
= parse_braces(&brace_flags
);
988 if (!recover
&& i
!= 0 && i
!= ',') {
989 nasm_nonfatal("comma, decorator or end of line expected, got %d", i
);
992 } else { /* immediate operand */
993 if (i
!= 0 && i
!= ',' && i
!= ':' &&
994 i
!= TOKEN_DECORATOR
&& i
!= TOKEN_OPMASK
) {
995 nasm_nonfatal("comma, colon, decorator or end of "
996 "line expected after operand");
998 } else if (i
== ':') {
1000 } else if (i
== TOKEN_DECORATOR
|| i
== TOKEN_OPMASK
) {
1001 /* parse opmask (and zeroing) after an operand */
1002 recover
= parse_braces(&brace_flags
);
1006 do { /* error recovery */
1007 i
= stdscan(NULL
, &tokval
);
1008 } while (i
!= 0 && i
!= ',');
1012 * now convert the exprs returned from evaluate()
1013 * into operand descriptions...
1015 op
->decoflags
|= brace_flags
;
1017 if (mref
) { /* it's a memory reference */
1018 /* A mib reference was fully parsed already */
1020 if (parse_mref(op
, value
))
1022 op
->hintbase
= hints
.base
;
1023 op
->hinttype
= hints
.type
;
1025 mref_set_optype(op
);
1026 } else if ((op
->type
& FAR
) && !far_jmp_ok
) {
1027 nasm_nonfatal("invalid use of FAR operand specifier");
1029 } else { /* it's not a memory reference */
1030 if (is_just_unknown(value
)) { /* it's immediate but unknown */
1031 op
->type
|= IMMEDIATE
;
1032 op
->opflags
|= OPFLAG_UNKNOWN
;
1033 op
->offset
= 0; /* don't care */
1034 op
->segment
= NO_SEG
; /* don't care again */
1035 op
->wrt
= NO_SEG
; /* still don't care */
1037 if(optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1040 UNITY
| SBYTEWORD
| SBYTEDWORD
| UDWORD
| SDWORD
;
1042 } else if (is_reloc(value
)) { /* it's immediate */
1043 uint64_t n
= reloc_value(value
);
1045 op
->type
|= IMMEDIATE
;
1047 op
->segment
= reloc_seg(value
);
1048 op
->wrt
= reloc_wrt(value
);
1049 op
->opflags
|= is_self_relative(value
) ? OPFLAG_RELATIVE
: 0;
1051 if (is_simple(value
)) {
1054 if (optimizing
.level
>= 0 && !(op
->type
& STRICT
)) {
1055 if ((uint32_t) (n
+ 128) <= 255)
1056 op
->type
|= SBYTEDWORD
;
1057 if ((uint16_t) (n
+ 128) <= 255)
1058 op
->type
|= SBYTEWORD
;
1059 if (n
<= UINT64_C(0xFFFFFFFF))
1061 if (n
+ UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
1065 } else if (value
->type
== EXPR_RDSAE
) {
1067 * it's not an operand but a rounding or SAE decorator.
1068 * put the decorator information in the (opflag_t) type field
1069 * of previous operand.
1072 switch (value
->value
) {
1078 op
->decoflags
|= (value
->value
== BRC_SAE
? SAE
: ER
);
1079 result
->evex_rm
= value
->value
;
1082 nasm_nonfatal("invalid decorator");
1085 } else { /* it's a register */
1087 uint64_t regset_size
= 0;
1089 if (value
->type
>= EXPR_SIMPLE
|| value
->value
!= 1) {
1090 nasm_nonfatal("invalid operand type");
1095 * We do not allow any kind of expression, except for
1096 * reg+value in which case it is a register set.
1098 for (i
= 1; value
[i
].type
; i
++) {
1099 if (!value
[i
].value
)
1102 switch (value
[i
].type
) {
1105 regset_size
= value
[i
].value
+ 1;
1110 nasm_nonfatal("invalid operand type");
1115 if ((regset_size
& (regset_size
- 1)) ||
1116 regset_size
>= (UINT64_C(1) << REGSET_BITS
)) {
1117 nasm_nonfatalf(ERR_PASS2
, "invalid register set size");
1121 /* clear overrides, except TO which applies to FPU regs */
1122 if (op
->type
& ~TO
) {
1124 * we want to produce a warning iff the specified size
1125 * is different from the register size
1127 rs
= op
->type
& SIZE_MASK
;
1133 * Make sure we're not out of nasm_reg_flags, still
1134 * probably this should be fixed when we're defining
1137 * An easy trigger is
1139 * e equ 0x80000000:0
1143 if (value
->type
< EXPR_REG_START
||
1144 value
->type
> EXPR_REG_END
) {
1145 nasm_nonfatal("invalid operand type");
1150 op
->type
|= REGISTER
;
1151 op
->type
|= nasm_reg_flags
[value
->type
];
1152 op
->type
|= (regset_size
>> 1) << REGSET_SHIFT
;
1153 op
->decoflags
|= brace_flags
;
1154 op
->basereg
= value
->type
;
1157 opflags_t opsize
= nasm_reg_flags
[value
->type
] & SIZE_MASK
;
1159 op
->type
|= rs
; /* For non-size-specific registers, permit size override */
1160 } else if (opsize
!= rs
) {
1162 *!regsize [on] register size specification ignored
1164 *! warns about a register with implicit size (such as \c{EAX}, which is always 32 bits)
1165 *! been given an explicit size specification which is inconsistent with the size
1166 *! of the named register, e.g. \c{WORD EAX}. \c{DWORD EAX} or \c{WORD AX} are
1167 *! permitted, and do not trigger this warning. Some registers which \e{do not} imply
1168 *! a specific size, such as \c{K0}, may need this specification unless the instruction
1169 *! itself implies the instruction size:
1171 *! \c KMOVW K0,[foo] ; Permitted, KMOVW implies 16 bits
1172 *! \c KMOV WORD K0,[foo] ; Permitted, WORD K0 specifies instruction size
1173 *! \c KMOV K0,WORD [foo] ; Permitted, WORD [foo] specifies instruction size
1174 *! \c KMOV K0,[foo] ; Not permitted, instruction size ambiguous
1176 nasm_warn(WARN_REGSIZE
, "invalid register size specification ignored");
1182 /* remember the position of operand having broadcasting/ER mode */
1183 if (op
->decoflags
& (BRDCAST_MASK
| ER
| SAE
))
1184 result
->evex_brerop
= opnum
;
1187 result
->operands
= opnum
; /* set operand count */
1189 /* clear remaining operands */
1190 while (opnum
< MAX_OPERANDS
)
1191 result
->oprs
[opnum
++].type
= 0;
1196 result
->opcode
= I_none
;
1200 static int is_comma_next(void)
1207 i
= stdscan(NULL
, &tv
);
1210 return (i
== ',' || i
== ';' || !i
);
1213 void cleanup_insn(insn
* i
)
1217 while ((e
= i
->eops
)) {
1219 if (e
->type
== EOT_DB_STRING_FREE
)
1220 nasm_free(e
->stringval
);