1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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 * assemble.c code generation for the Netwide Assembler
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
39 * \1..\4 - that many literal bytes follow in the code stream
40 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
43 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
45 * \20..\23 - a byte immediate operand, from operand 0..3
46 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
47 * \30..\33 - a word immediate operand, from operand 0..3
48 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
49 * assembly mode or the operand-size override on the operand
50 * \40..\43 - a long immediate operand, from operand 0..3
51 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
52 * depending on the address size of the instruction.
53 * \50..\53 - a byte relative operand, from operand 0..3
54 * \54..\57 - a qword immediate operand, from operand 0..3
55 * \60..\63 - a word relative operand, from operand 0..3
56 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
57 * assembly mode or the operand-size override on the operand
58 * \70..\73 - a long relative operand, from operand 0..3
59 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
60 * \1ab - a ModRM, calculated on EA in operand a, with the spare
61 * field the register value of operand b.
62 * \172\ab - the register number from operand a in bits 7..4, with
63 * the 4-bit immediate from operand b in bits 3..0.
64 * \173\xab - the register number from operand a in bits 7..4, with
65 * the value b in bits 3..0.
66 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
67 * an arbitrary value in bits 3..0 (assembled as zero.)
68 * \2ab - a ModRM, calculated on EA in operand a, with the spare
69 * field equal to digit b.
70 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
71 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
72 * V field taken from operand 0..3.
73 * \270 - this instruction uses VEX/XOP rather than REX, with the
74 * V field set to 1111b.
76 * VEX/XOP prefixes are followed by the sequence:
77 * \tmm\wlp where mm is the M field; and wlp is:
79 * [l0] ll = 0 for L = 0 (.128, .lz)
80 * [l1] ll = 1 for L = 1 (.256)
81 * [lig] ll = 2 for L don't care (always assembled as 0)
83 * [w0] ww = 0 for W = 0
84 * [w1 ] ww = 1 for W = 1
85 * [wig] ww = 2 for W don't care (always assembled as 0)
86 * [ww] ww = 3 for W used as REX.W
88 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
90 * \271 - instruction takes XRELEASE (F3) with or without lock
91 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
92 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
93 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
94 * to the operand size (if o16/o32/o64 present) or the bit size
95 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
96 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
97 * \312 - (disassembler only) invalid with non-default address size.
98 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
99 * \314 - (disassembler only) invalid with REX.B
100 * \315 - (disassembler only) invalid with REX.X
101 * \316 - (disassembler only) invalid with REX.R
102 * \317 - (disassembler only) invalid with REX.W
103 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
104 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
105 * \322 - indicates that this instruction is only valid when the
106 * operand size is the default (instruction to disassembler,
107 * generates no code in the assembler)
108 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
109 * \324 - indicates 64-bit operand size requiring REX prefix.
110 * \325 - instruction which always uses spl/bpl/sil/dil
111 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
112 disassembler only; for SSE instructions.
113 * \330 - a literal byte follows in the code stream, to be added
114 * to the condition code value of the instruction.
115 * \331 - instruction not valid with REP prefix. Hint for
116 * disassembler only; for SSE instructions.
117 * \332 - REP prefix (0xF2 byte) used as opcode extension.
118 * \333 - REP prefix (0xF3 byte) used as opcode extension.
119 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
120 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
121 * \336 - force a REP(E) prefix (0xF3) even if not specified.
122 * \337 - force a REPNE prefix (0xF2) even if not specified.
123 * \336-\337 are still listed as prefixes in the disassembler.
124 * \340 - reserve <operand 0> bytes of uninitialized storage.
125 * Operand 0 had better be a segmentless constant.
126 * \341 - this instruction needs a WAIT "prefix"
127 * \360 - no SSE prefix (== \364\331)
128 * \361 - 66 SSE prefix (== \366\331)
129 * \364 - operand-size prefix (0x66) not permitted
130 * \365 - address-size prefix (0x67) not permitted
131 * \366 - operand-size prefix (0x66) used as opcode extension
132 * \367 - address-size prefix (0x67) used as opcode extension
133 * \370,\371 - match only if operand 0 meets byte jump criteria.
134 * 370 is used for Jcc, 371 is used for JMP.
135 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
136 * used for conditional jump over longer jump
137 * \374 - this instruction takes an XMM VSIB memory EA
138 * \375 - this instruction takes an YMM VSIB memory EA
141 #include "compiler.h"
145 #include <inttypes.h>
149 #include "assemble.h"
155 * Matching errors. These should be sorted so that more specific
156 * errors come later in the sequence.
165 * Matching success; the conditional ones first
167 MOK_JUMP
, /* Matching OK but needs jmp_match() */
168 MOK_GOOD
/* Matching unconditionally OK */
172 enum ea_type type
; /* what kind of EA is this? */
173 int sib_present
; /* is a SIB byte necessary? */
174 int bytes
; /* # of bytes of offset needed */
175 int size
; /* lazy - this is sib+bytes+1 */
176 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
179 #define GEN_SIB(scale, index, base) \
180 (((scale) << 6) | ((index) << 3) | ((base)))
182 #define GEN_MODRM(mod, reg, rm) \
183 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
185 static uint32_t cpu
; /* cpu level received from nasm.c */
186 static efunc errfunc
;
187 static struct ofmt
*outfmt
;
188 static ListGen
*list
;
190 static int64_t calcsize(int32_t, int64_t, int, insn
*,
191 const struct itemplate
*);
192 static void gencode(int32_t segment
, int64_t offset
, int bits
,
193 insn
* ins
, const struct itemplate
*temp
,
195 static enum match_result
find_match(const struct itemplate
**tempp
,
197 int32_t segment
, int64_t offset
, int bits
);
198 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
199 static opflags_t
regflag(const operand
*);
200 static int32_t regval(const operand
*);
201 static int rexflags(int, opflags_t
, int);
202 static int op_rexflags(const operand
*, int);
203 static void add_asp(insn
*, int);
205 static enum ea_type
process_ea(operand
*, ea
*, int, int, int, opflags_t
);
207 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
209 return ins
->prefixes
[pos
] == prefix
;
212 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
214 if (ins
->prefixes
[pos
])
215 errfunc(ERR_NONFATAL
, "invalid %s prefix",
216 prefix_name(ins
->prefixes
[pos
]));
219 static const char *size_name(int size
)
241 static void warn_overflow(int pass
, int size
)
243 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
244 "%s data exceeds bounds", size_name(size
));
247 static void warn_overflow_const(int64_t data
, int size
)
249 if (overflow_general(data
, size
))
250 warn_overflow(ERR_PASS1
, size
);
253 static void warn_overflow_opd(const struct operand
*o
, int size
)
255 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
256 if (overflow_general(o
->offset
, size
))
257 warn_overflow(ERR_PASS2
, size
);
262 * This routine wrappers the real output format's output routine,
263 * in order to pass a copy of the data off to the listing file
264 * generator at the same time.
266 static void out(int64_t offset
, int32_t segto
, const void *data
,
267 enum out_type type
, uint64_t size
,
268 int32_t segment
, int32_t wrt
)
270 static int32_t lineno
= 0; /* static!!! */
271 static char *lnfname
= NULL
;
274 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
276 * This is a non-relocated address, and we're going to
277 * convert it into RAWDATA format.
282 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
286 WRITEADDR(q
, *(int64_t *)data
, size
);
291 list
->output(offset
, data
, type
, size
);
294 * this call to src_get determines when we call the
295 * debug-format-specific "linenum" function
296 * it updates lineno and lnfname to the current values
297 * returning 0 if "same as last time", -2 if lnfname
298 * changed, and the amount by which lineno changed,
299 * if it did. thus, these variables must be static
302 if (src_get(&lineno
, &lnfname
))
303 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
305 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
308 static void out_imm8(int64_t offset
, int32_t segment
, struct operand
*opx
)
310 if (opx
->segment
!= NO_SEG
) {
311 uint64_t data
= opx
->offset
;
312 out(offset
, segment
, &data
, OUT_ADDRESS
, 1, opx
->segment
, opx
->wrt
);
314 uint8_t byte
= opx
->offset
;
315 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
319 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
320 insn
* ins
, const struct itemplate
*temp
)
323 const uint8_t *code
= temp
->code
;
326 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
330 if (optimizing
< 0 && c
== 0371)
333 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
335 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
336 /* Be optimistic in pass 1 */
339 if (ins
->oprs
[0].segment
!= segment
)
342 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
343 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
346 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
347 insn
* instruction
, struct ofmt
*output
, efunc error
,
350 const struct itemplate
*temp
;
355 int64_t start
= offset
;
356 int64_t wsize
; /* size for DB etc. */
358 errfunc
= error
; /* to pass to other functions */
360 outfmt
= output
; /* likewise */
361 list
= listgen
; /* and again */
363 wsize
= idata_bytes(instruction
->opcode
);
369 int32_t t
= instruction
->times
;
372 "instruction->times < 0 (%ld) in assemble()", t
);
374 while (t
--) { /* repeat TIMES times */
375 list_for_each(e
, instruction
->eops
) {
376 if (e
->type
== EOT_DB_NUMBER
) {
378 errfunc(ERR_NONFATAL
,
379 "integer supplied to a DT, DO or DY"
382 out(offset
, segment
, &e
->offset
,
383 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
386 } else if (e
->type
== EOT_DB_STRING
||
387 e
->type
== EOT_DB_STRING_FREE
) {
390 out(offset
, segment
, e
->stringval
,
391 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
392 align
= e
->stringlen
% wsize
;
395 align
= wsize
- align
;
396 out(offset
, segment
, zero_buffer
,
397 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
399 offset
+= e
->stringlen
+ align
;
402 if (t
> 0 && t
== instruction
->times
- 1) {
404 * Dummy call to list->output to give the offset to the
407 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
408 list
->uplevel(LIST_TIMES
);
411 if (instruction
->times
> 1)
412 list
->downlevel(LIST_TIMES
);
413 return offset
- start
;
416 if (instruction
->opcode
== I_INCBIN
) {
417 const char *fname
= instruction
->eops
->stringval
;
420 fp
= fopen(fname
, "rb");
422 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
424 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
425 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
429 static char buf
[4096];
430 size_t t
= instruction
->times
;
435 if (instruction
->eops
->next
) {
436 base
= instruction
->eops
->next
->offset
;
438 if (instruction
->eops
->next
->next
&&
439 len
> (size_t)instruction
->eops
->next
->next
->offset
)
440 len
= (size_t)instruction
->eops
->next
->next
->offset
;
443 * Dummy call to list->output to give the offset to the
446 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
447 list
->uplevel(LIST_INCBIN
);
451 fseek(fp
, base
, SEEK_SET
);
455 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
458 * This shouldn't happen unless the file
459 * actually changes while we are reading
463 "`incbin': unexpected EOF while"
464 " reading file `%s'", fname
);
465 t
= 0; /* Try to exit cleanly */
468 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
473 list
->downlevel(LIST_INCBIN
);
474 if (instruction
->times
> 1) {
476 * Dummy call to list->output to give the offset to the
479 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
480 list
->uplevel(LIST_TIMES
);
481 list
->downlevel(LIST_TIMES
);
484 return instruction
->times
* len
;
486 return 0; /* if we're here, there's an error */
489 /* Check to see if we need an address-size prefix */
490 add_asp(instruction
, bits
);
492 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
496 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
497 itimes
= instruction
->times
;
498 if (insn_size
< 0) /* shouldn't be, on pass two */
499 error(ERR_PANIC
, "errors made it through from pass one");
502 for (j
= 0; j
< MAXPREFIX
; j
++) {
504 switch (instruction
->prefixes
[j
]) {
524 error(ERR_WARNING
| ERR_PASS2
,
525 "cs segment base generated, but will be ignored in 64-bit mode");
531 error(ERR_WARNING
| ERR_PASS2
,
532 "ds segment base generated, but will be ignored in 64-bit mode");
538 error(ERR_WARNING
| ERR_PASS2
,
539 "es segment base generated, but will be ignored in 64-bit mode");
551 error(ERR_WARNING
| ERR_PASS2
,
552 "ss segment base generated, but will be ignored in 64-bit mode");
559 "segr6 and segr7 cannot be used as prefixes");
564 "16-bit addressing is not supported "
566 } else if (bits
!= 16)
576 "64-bit addressing is only supported "
600 error(ERR_PANIC
, "invalid instruction prefix");
603 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
608 insn_end
= offset
+ insn_size
;
609 gencode(segment
, offset
, bits
, instruction
,
612 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
614 * Dummy call to list->output to give the offset to the
617 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
618 list
->uplevel(LIST_TIMES
);
621 if (instruction
->times
> 1)
622 list
->downlevel(LIST_TIMES
);
623 return offset
- start
;
627 case MERR_OPSIZEMISSING
:
628 error(ERR_NONFATAL
, "operation size not specified");
630 case MERR_OPSIZEMISMATCH
:
631 error(ERR_NONFATAL
, "mismatch in operand sizes");
634 error(ERR_NONFATAL
, "no instruction for this cpu level");
637 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
642 "invalid combination of opcode and operands");
649 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
650 insn
* instruction
, efunc error
)
652 const struct itemplate
*temp
;
655 errfunc
= error
; /* to pass to other functions */
658 if (instruction
->opcode
== I_none
)
661 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
662 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
663 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
664 instruction
->opcode
== I_DY
) {
666 int32_t isize
, osize
, wsize
;
669 wsize
= idata_bytes(instruction
->opcode
);
671 list_for_each(e
, instruction
->eops
) {
675 if (e
->type
== EOT_DB_NUMBER
) {
677 warn_overflow_const(e
->offset
, wsize
);
678 } else if (e
->type
== EOT_DB_STRING
||
679 e
->type
== EOT_DB_STRING_FREE
)
680 osize
= e
->stringlen
;
682 align
= (-osize
) % wsize
;
685 isize
+= osize
+ align
;
687 return isize
* instruction
->times
;
690 if (instruction
->opcode
== I_INCBIN
) {
691 const char *fname
= instruction
->eops
->stringval
;
696 fp
= fopen(fname
, "rb");
698 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
700 else if (fseek(fp
, 0L, SEEK_END
) < 0)
701 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
705 if (instruction
->eops
->next
) {
706 len
-= instruction
->eops
->next
->offset
;
707 if (instruction
->eops
->next
->next
&&
708 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
709 len
= (size_t)instruction
->eops
->next
->next
->offset
;
712 val
= instruction
->times
* len
;
719 /* Check to see if we need an address-size prefix */
720 add_asp(instruction
, bits
);
722 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
724 /* we've matched an instruction. */
728 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
731 for (j
= 0; j
< MAXPREFIX
; j
++) {
732 switch (instruction
->prefixes
[j
]) {
758 return isize
* instruction
->times
;
760 return -1; /* didn't match any instruction */
764 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
766 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
767 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
768 static const enum whatwarn warn
[2][4] =
770 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
771 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
775 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
777 return; /* Not XACQUIRE/XRELEASE */
780 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
781 ww
= w_inval
; /* HLE requires operand 0 to be memory */
788 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
789 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
790 "%s with this instruction requires lock",
791 prefix_name(rep_pfx
));
796 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
797 "%s invalid with this instruction",
798 prefix_name(rep_pfx
));
803 /* Common construct */
804 #define case3(x) case (x): case (x)+1: case (x)+2
805 #define case4(x) case3(x): case (x)+3
807 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
808 insn
* ins
, const struct itemplate
*temp
)
810 const uint8_t *codes
= temp
->code
;
819 bool lockcheck
= true;
821 ins
->rex
= 0; /* Ensure REX is reset */
822 eat
= EA_SCALAR
; /* Expect a scalar EA */
824 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
827 (void)segment
; /* Don't warn that this parameter is unused */
828 (void)offset
; /* Don't warn that this parameter is unused */
832 op1
= (c
& 3) + ((opex
& 1) << 2);
833 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
834 opx
= &ins
->oprs
[op1
];
835 opex
= 0; /* For the next iteration */
839 codes
+= c
, length
+= c
;
848 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
862 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
863 length
+= (opx
->type
& BITS16
) ? 2 : 4;
865 length
+= (bits
== 16) ? 2 : 4;
873 length
+= ins
->addr_size
>> 3;
881 length
+= 8; /* MOV reg64/imm */
889 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
890 length
+= (opx
->type
& BITS16
) ? 2 : 4;
892 length
+= (bits
== 16) ? 2 : 4;
919 ins
->vexreg
= regval(opx
);
920 ins
->vex_cm
= *codes
++;
921 ins
->vex_wlp
= *codes
++;
927 ins
->vex_cm
= *codes
++;
928 ins
->vex_wlp
= *codes
++;
945 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
949 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
956 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
957 has_prefix(ins
, PPS_ASIZE
, P_A32
))
966 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
970 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
972 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
978 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
982 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
984 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1026 if (!ins
->prefixes
[PPS_REP
])
1027 ins
->prefixes
[PPS_REP
] = P_REP
;
1031 if (!ins
->prefixes
[PPS_REP
])
1032 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1036 if (ins
->oprs
[0].segment
!= NO_SEG
)
1037 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1038 " quantity of BSS space");
1040 length
+= ins
->oprs
[0].offset
;
1044 if (!ins
->prefixes
[PPS_WAIT
])
1045 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1095 struct operand
*opy
= &ins
->oprs
[op2
];
1097 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1100 /* pick rfield from operand b (opx) */
1101 rflags
= regflag(opx
);
1102 rfield
= nasm_regvals
[opx
->basereg
];
1107 if (process_ea(opy
, &ea_data
, bits
,ins
->addr_size
,
1108 rfield
, rflags
) != eat
) {
1109 errfunc(ERR_NONFATAL
, "invalid effective address");
1112 ins
->rex
|= ea_data
.rex
;
1113 length
+= ea_data
.size
;
1119 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1120 ": instruction code \\%o (0x%02X) given", c
, c
);
1125 ins
->rex
&= rex_mask
;
1127 if (ins
->rex
& REX_NH
) {
1128 if (ins
->rex
& REX_H
) {
1129 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1132 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1135 if (ins
->rex
& REX_V
) {
1136 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1138 if (ins
->rex
& REX_H
) {
1139 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1142 switch (ins
->vex_wlp
& 060) {
1156 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1157 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1160 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)))
1164 } else if (ins
->rex
& REX_REAL
) {
1165 if (ins
->rex
& REX_H
) {
1166 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1168 } else if (bits
== 64) {
1170 } else if ((ins
->rex
& REX_L
) &&
1171 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1174 assert_no_prefix(ins
, PPS_LOCK
);
1175 lockcheck
= false; /* Already errored, no need for warning */
1178 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1183 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1184 (!(temp
->flags
& IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1185 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1186 "instruction is not lockable");
1189 bad_hle_warn(ins
, hleok
);
1194 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1197 if ((ins
->rex
& REX_REAL
) && !(ins
->rex
& REX_V
)) {
1198 ins
->rex
= (ins
->rex
& REX_REAL
) | REX_P
;
1199 out(offset
, segment
, &ins
->rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1208 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1209 insn
* ins
, const struct itemplate
*temp
,
1217 struct operand
*opx
;
1218 const uint8_t *codes
= temp
->code
;
1220 enum ea_type eat
= EA_SCALAR
;
1224 op1
= (c
& 3) + ((opex
& 1) << 2);
1225 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1226 opx
= &ins
->oprs
[op1
];
1227 opex
= 0; /* For the next iteration */
1234 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1235 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1247 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1248 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1249 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1254 if (opx
->offset
< -256 || opx
->offset
> 255) {
1255 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1256 "byte value exceeds bounds");
1258 out_imm8(offset
, segment
, opx
);
1263 if (opx
->offset
< 0 || opx
->offset
> 255)
1264 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1265 "unsigned byte value exceeds bounds");
1266 out_imm8(offset
, segment
, opx
);
1271 warn_overflow_opd(opx
, 2);
1273 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1274 opx
->segment
, opx
->wrt
);
1279 if (opx
->type
& (BITS16
| BITS32
))
1280 size
= (opx
->type
& BITS16
) ? 2 : 4;
1282 size
= (bits
== 16) ? 2 : 4;
1283 warn_overflow_opd(opx
, size
);
1285 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1286 opx
->segment
, opx
->wrt
);
1291 warn_overflow_opd(opx
, 4);
1293 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1294 opx
->segment
, opx
->wrt
);
1300 size
= ins
->addr_size
>> 3;
1301 warn_overflow_opd(opx
, size
);
1302 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1303 opx
->segment
, opx
->wrt
);
1308 if (opx
->segment
!= segment
) {
1310 out(offset
, segment
, &data
,
1311 OUT_REL1ADR
, insn_end
- offset
,
1312 opx
->segment
, opx
->wrt
);
1314 data
= opx
->offset
- insn_end
;
1315 if (data
> 127 || data
< -128)
1316 errfunc(ERR_NONFATAL
, "short jump is out of range");
1317 out(offset
, segment
, &data
,
1318 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1324 data
= (int64_t)opx
->offset
;
1325 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1326 opx
->segment
, opx
->wrt
);
1331 if (opx
->segment
!= segment
) {
1333 out(offset
, segment
, &data
,
1334 OUT_REL2ADR
, insn_end
- offset
,
1335 opx
->segment
, opx
->wrt
);
1337 data
= opx
->offset
- insn_end
;
1338 out(offset
, segment
, &data
,
1339 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1345 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1346 size
= (opx
->type
& BITS16
) ? 2 : 4;
1348 size
= (bits
== 16) ? 2 : 4;
1349 if (opx
->segment
!= segment
) {
1351 out(offset
, segment
, &data
,
1352 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1353 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1355 data
= opx
->offset
- insn_end
;
1356 out(offset
, segment
, &data
,
1357 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1363 if (opx
->segment
!= segment
) {
1365 out(offset
, segment
, &data
,
1366 OUT_REL4ADR
, insn_end
- offset
,
1367 opx
->segment
, opx
->wrt
);
1369 data
= opx
->offset
- insn_end
;
1370 out(offset
, segment
, &data
,
1371 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1377 if (opx
->segment
== NO_SEG
)
1378 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1381 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1382 outfmt
->segbase(1 + opx
->segment
),
1389 opx
= &ins
->oprs
[c
>> 3];
1390 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1391 opx
= &ins
->oprs
[c
& 7];
1392 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1393 errfunc(ERR_NONFATAL
,
1394 "non-absolute expression not permitted as argument %d",
1397 if (opx
->offset
& ~15) {
1398 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1399 "four-bit argument exceeds bounds");
1401 bytes
[0] |= opx
->offset
& 15;
1403 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1409 opx
= &ins
->oprs
[c
>> 4];
1410 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1412 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1417 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1418 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1424 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1425 (int32_t)data
!= (int64_t)data
) {
1426 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1427 "signed dword immediate exceeds bounds");
1429 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1430 opx
->segment
, opx
->wrt
);
1437 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1438 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1439 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1440 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1441 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1442 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1446 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1447 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1448 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1463 if (ins
->rex
& REX_W
)
1465 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1467 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1472 um
= (uint64_t)2 << (s
-1);
1475 if (uv
> 127 && uv
< (uint64_t)-128 &&
1476 (uv
< um
-128 || uv
> um
-1)) {
1477 /* If this wasn't explicitly byte-sized, warn as though we
1478 * had fallen through to the imm16/32/64 case.
1480 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1481 "%s value exceeds bounds",
1482 (opx
->type
& BITS8
) ? "signed byte" :
1487 if (opx
->segment
!= NO_SEG
) {
1489 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1490 opx
->segment
, opx
->wrt
);
1493 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1504 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1506 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1513 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1515 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1550 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1551 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1560 *bytes
= c
- 0332 + 0xF2;
1561 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1566 if (ins
->rex
& REX_R
) {
1568 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1571 ins
->rex
&= ~(REX_L
|REX_R
);
1582 if (ins
->oprs
[0].segment
!= NO_SEG
)
1583 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1585 int64_t size
= ins
->oprs
[0].offset
;
1587 out(offset
, segment
, NULL
,
1588 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1601 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1611 *bytes
= c
- 0366 + 0x66;
1612 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1621 *bytes
= bits
== 16 ? 3 : 5;
1622 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1652 struct operand
*opy
= &ins
->oprs
[op2
];
1655 /* pick rfield from operand b (opx) */
1656 rflags
= regflag(opx
);
1657 rfield
= nasm_regvals
[opx
->basereg
];
1659 /* rfield is constant */
1664 if (process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1665 rfield
, rflags
) != eat
)
1666 errfunc(ERR_NONFATAL
, "invalid effective address");
1669 *p
++ = ea_data
.modrm
;
1670 if (ea_data
.sib_present
)
1674 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1677 * Make sure the address gets the right offset in case
1678 * the line breaks in the .lst file (BR 1197827)
1683 switch (ea_data
.bytes
) {
1693 if (opy
->segment
== segment
) {
1695 if (overflow_signed(data
, ea_data
.bytes
))
1696 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1697 out(offset
, segment
, &data
, OUT_ADDRESS
,
1698 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1700 /* overflow check in output/linker? */
1701 out(offset
, segment
, &data
, OUT_REL4ADR
,
1702 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1705 if (overflow_general(opy
->offset
, ins
->addr_size
>> 3) ||
1706 signed_bits(opy
->offset
, ins
->addr_size
) !=
1707 signed_bits(opy
->offset
, ea_data
.bytes
* 8))
1708 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1710 out(offset
, segment
, &data
, OUT_ADDRESS
,
1711 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1717 "Invalid amount of bytes (%d) for offset?!",
1726 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1727 ": instruction code \\%o (0x%02X) given", c
, c
);
1733 static opflags_t
regflag(const operand
* o
)
1735 if (!is_register(o
->basereg
))
1736 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1737 return nasm_reg_flags
[o
->basereg
];
1740 static int32_t regval(const operand
* o
)
1742 if (!is_register(o
->basereg
))
1743 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1744 return nasm_regvals
[o
->basereg
];
1747 static int op_rexflags(const operand
* o
, int mask
)
1752 if (!is_register(o
->basereg
))
1753 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1755 flags
= nasm_reg_flags
[o
->basereg
];
1756 val
= nasm_regvals
[o
->basereg
];
1758 return rexflags(val
, flags
, mask
);
1761 static int rexflags(int val
, opflags_t flags
, int mask
)
1766 rex
|= REX_B
|REX_X
|REX_R
;
1769 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1771 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1777 static enum match_result
find_match(const struct itemplate
**tempp
,
1779 int32_t segment
, int64_t offset
, int bits
)
1781 const struct itemplate
*temp
;
1782 enum match_result m
, merr
;
1783 opflags_t xsizeflags
[MAX_OPERANDS
];
1784 bool opsizemissing
= false;
1787 for (i
= 0; i
< instruction
->operands
; i
++)
1788 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
1790 merr
= MERR_INVALOP
;
1792 for (temp
= nasm_instructions
[instruction
->opcode
];
1793 temp
->opcode
!= I_none
; temp
++) {
1794 m
= matches(temp
, instruction
, bits
);
1795 if (m
== MOK_JUMP
) {
1796 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
1800 } else if (m
== MERR_OPSIZEMISSING
&&
1801 (temp
->flags
& IF_SMASK
) != IF_SX
) {
1803 * Missing operand size and a candidate for fuzzy matching...
1805 for (i
= 0; i
< temp
->operands
; i
++)
1806 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
1807 opsizemissing
= true;
1811 if (merr
== MOK_GOOD
)
1815 /* No match, but see if we can get a fuzzy operand size match... */
1819 for (i
= 0; i
< instruction
->operands
; i
++) {
1821 * We ignore extrinsic operand sizes on registers, so we should
1822 * never try to fuzzy-match on them. This also resolves the case
1823 * when we have e.g. "xmmrm128" in two different positions.
1825 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
1828 /* This tests if xsizeflags[i] has more than one bit set */
1829 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
1830 goto done
; /* No luck */
1832 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
1835 /* Try matching again... */
1836 for (temp
= nasm_instructions
[instruction
->opcode
];
1837 temp
->opcode
!= I_none
; temp
++) {
1838 m
= matches(temp
, instruction
, bits
);
1839 if (m
== MOK_JUMP
) {
1840 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
1847 if (merr
== MOK_GOOD
)
1856 static enum match_result
matches(const struct itemplate
*itemp
,
1857 insn
*instruction
, int bits
)
1859 opflags_t size
[MAX_OPERANDS
], asize
;
1860 bool opsizemissing
= false;
1866 if (itemp
->opcode
!= instruction
->opcode
)
1867 return MERR_INVALOP
;
1870 * Count the operands
1872 if (itemp
->operands
!= instruction
->operands
)
1873 return MERR_INVALOP
;
1878 if (!(optimizing
> 0) && (itemp
->flags
& IF_OPT
))
1879 return MERR_INVALOP
;
1882 * Check that no spurious colons or TOs are present
1884 for (i
= 0; i
< itemp
->operands
; i
++)
1885 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
1886 return MERR_INVALOP
;
1889 * Process size flags
1891 switch (itemp
->flags
& IF_SMASK
) {
1931 if (itemp
->flags
& IF_ARMASK
) {
1932 /* S- flags only apply to a specific operand */
1933 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
1934 memset(size
, 0, sizeof size
);
1937 /* S- flags apply to all operands */
1938 for (i
= 0; i
< MAX_OPERANDS
; i
++)
1943 * Check that the operand flags all match up,
1944 * it's a bit tricky so lets be verbose:
1946 * 1) Find out the size of operand. If instruction
1947 * doesn't have one specified -- we're trying to
1948 * guess it either from template (IF_S* flag) or
1951 * 2) If template operand do not match the instruction OR
1952 * template has an operand size specified AND this size differ
1953 * from which instruction has (perhaps we got it from code bits)
1955 * a) Check that only size of instruction and operand is differ
1956 * other characteristics do match
1957 * b) Perhaps it's a register specified in instruction so
1958 * for such a case we just mark that operand as "size
1959 * missing" and this will turn on fuzzy operand size
1960 * logic facility (handled by a caller)
1962 for (i
= 0; i
< itemp
->operands
; i
++) {
1963 opflags_t type
= instruction
->oprs
[i
].type
;
1964 if (!(type
& SIZE_MASK
))
1967 if (itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) {
1968 return MERR_INVALOP
;
1969 } else if ((itemp
->opd
[i
] & SIZE_MASK
) &&
1970 (itemp
->opd
[i
] & SIZE_MASK
) != (type
& SIZE_MASK
)) {
1971 if (type
& SIZE_MASK
) {
1972 return MERR_INVALOP
;
1973 } else if (!is_class(REGISTER
, type
)) {
1975 * Note: we don't honor extrinsic operand sizes for registers,
1976 * so "missing operand size" for a register should be
1977 * considered a wildcard match rather than an error.
1979 opsizemissing
= true;
1985 return MERR_OPSIZEMISSING
;
1988 * Check operand sizes
1990 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
1991 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
1992 for (i
= 0; i
< oprs
; i
++) {
1993 asize
= itemp
->opd
[i
] & SIZE_MASK
;
1995 for (i
= 0; i
< oprs
; i
++)
2001 oprs
= itemp
->operands
;
2004 for (i
= 0; i
< itemp
->operands
; i
++) {
2005 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2006 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2007 return MERR_OPSIZEMISMATCH
;
2011 * Check template is okay at the set cpu level
2013 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2017 * Verify the appropriate long mode flag.
2019 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2020 return MERR_BADMODE
;
2023 * If we have a HLE prefix, look for the NOHLE flag
2025 if ((itemp
->flags
& IF_NOHLE
) &&
2026 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2027 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2031 * Check if special handling needed for Jumps
2033 if ((itemp
->code
[0] & ~1) == 0370)
2039 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2040 int addrbits
, int rfield
, opflags_t rflags
)
2042 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2044 output
->type
= EA_SCALAR
;
2045 output
->rip
= false;
2047 /* REX flags for the rfield operand */
2048 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2050 if (is_class(REGISTER
, input
->type
)) {
2052 * It's a direct register.
2054 if (!is_register(input
->basereg
))
2057 if (!is_class(REG_EA
, regflag(input
)))
2060 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2061 output
->sib_present
= false; /* no SIB necessary */
2062 output
->bytes
= 0; /* no offset necessary either */
2063 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2066 * It's a memory reference.
2068 if (input
->basereg
== -1 &&
2069 (input
->indexreg
== -1 || input
->scale
== 0)) {
2071 * It's a pure offset.
2073 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2074 input
->segment
== NO_SEG
) {
2075 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2076 input
->type
&= ~IP_REL
;
2077 input
->type
|= MEMORY
;
2080 if (input
->eaflags
& EAF_BYTEOFFS
||
2081 (input
->eaflags
& EAF_WORDOFFS
&&
2082 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2083 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2086 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2087 output
->sib_present
= true;
2088 output
->sib
= GEN_SIB(0, 4, 5);
2090 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2091 output
->rip
= false;
2093 output
->sib_present
= false;
2094 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2095 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2096 output
->rip
= bits
== 64;
2100 * It's an indirection.
2102 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2103 int32_t seg
= input
->segment
;
2104 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2105 int t
, it
, bt
; /* register numbers */
2106 opflags_t x
, ix
, bx
; /* register flags */
2109 i
= -1; /* make this easy, at least */
2111 if (is_register(i
)) {
2112 it
= nasm_regvals
[i
];
2113 ix
= nasm_reg_flags
[i
];
2119 if (is_register(b
)) {
2120 bt
= nasm_regvals
[b
];
2121 bx
= nasm_reg_flags
[b
];
2127 /* if either one are a vector register... */
2128 if ((ix
|bx
) & (XMMREG
|YMMREG
) & ~REG_EA
) {
2129 opflags_t sok
= BITS32
| BITS64
;
2130 int32_t o
= input
->offset
;
2131 int mod
, scale
, index
, base
;
2134 * For a vector SIB, one has to be a vector and the other,
2135 * if present, a GPR. The vector must be the index operand.
2137 if (it
== -1 || (bx
& (XMMREG
|YMMREG
) & ~REG_EA
)) {
2143 t
= bt
, bt
= it
, it
= t
;
2144 x
= bx
, bx
= ix
, ix
= x
;
2150 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2157 * While we're here, ensure the user didn't specify
2160 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2163 if (addrbits
== 16 ||
2164 (addrbits
== 32 && !(sok
& BITS32
)) ||
2165 (addrbits
== 64 && !(sok
& BITS64
)))
2168 output
->type
= (ix
& YMMREG
& ~REG_EA
)
2169 ? EA_YMMVSIB
: EA_XMMVSIB
;
2171 output
->rex
|= rexflags(it
, ix
, REX_X
);
2172 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2174 index
= it
& 7; /* it is known to be != -1 */
2189 default: /* then what the smeg is it? */
2190 goto err
; /* panic */
2198 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2199 seg
== NO_SEG
&& !forw_ref
&&
2200 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2202 else if (input
->eaflags
& EAF_BYTEOFFS
||
2203 (o
>= -128 && o
<= 127 &&
2204 seg
== NO_SEG
&& !forw_ref
&&
2205 !(input
->eaflags
& EAF_WORDOFFS
)))
2211 output
->sib_present
= true;
2212 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2213 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2214 output
->sib
= GEN_SIB(scale
, index
, base
);
2215 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2217 * it must be a 32/64-bit memory reference. Firstly we have
2218 * to check that all registers involved are type E/Rxx.
2220 opflags_t sok
= BITS32
| BITS64
;
2221 int32_t o
= input
->offset
;
2224 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2232 goto err
; /* Invalid register */
2233 if (~sok
& bx
& SIZE_MASK
)
2234 goto err
; /* Invalid size */
2239 * While we're here, ensure the user didn't specify
2242 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2245 if (addrbits
== 16 ||
2246 (addrbits
== 32 && !(sok
& BITS32
)) ||
2247 (addrbits
== 64 && !(sok
& BITS64
)))
2250 /* now reorganize base/index */
2251 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2252 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2253 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2254 /* swap if hints say so */
2255 t
= bt
, bt
= it
, it
= t
;
2256 x
= bx
, bx
= ix
, ix
= x
;
2258 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2259 bt
= -1, bx
= 0, s
++;
2260 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2261 /* make single reg base, unless hint */
2262 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2264 if (((s
== 2 && it
!= REG_NUM_ESP
&& !(input
->eaflags
& EAF_TIMESTWO
)) ||
2265 s
== 3 || s
== 5 || s
== 9) && bt
== -1)
2266 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2267 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2268 (input
->eaflags
& EAF_TIMESTWO
))
2269 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2270 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2271 if (s
== 1 && it
== REG_NUM_ESP
) {
2272 /* swap ESP into base if scale is 1 */
2273 t
= it
, it
= bt
, bt
= t
;
2274 x
= ix
, ix
= bx
, bx
= x
;
2276 if (it
== REG_NUM_ESP
||
2277 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2278 goto err
; /* wrong, for various reasons */
2280 output
->rex
|= rexflags(it
, ix
, REX_X
);
2281 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2283 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2292 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2293 seg
== NO_SEG
&& !forw_ref
&&
2294 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2296 else if (input
->eaflags
& EAF_BYTEOFFS
||
2297 (o
>= -128 && o
<= 127 &&
2298 seg
== NO_SEG
&& !forw_ref
&&
2299 !(input
->eaflags
& EAF_WORDOFFS
)))
2305 output
->sib_present
= false;
2306 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2307 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2310 int mod
, scale
, index
, base
;
2330 default: /* then what the smeg is it? */
2331 goto err
; /* panic */
2339 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2340 seg
== NO_SEG
&& !forw_ref
&&
2341 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2343 else if (input
->eaflags
& EAF_BYTEOFFS
||
2344 (o
>= -128 && o
<= 127 &&
2345 seg
== NO_SEG
&& !forw_ref
&&
2346 !(input
->eaflags
& EAF_WORDOFFS
)))
2352 output
->sib_present
= true;
2353 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2354 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2355 output
->sib
= GEN_SIB(scale
, index
, base
);
2357 } else { /* it's 16-bit */
2359 int16_t o
= input
->offset
;
2361 /* check for 64-bit long mode */
2365 /* check all registers are BX, BP, SI or DI */
2366 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2367 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2370 /* ensure the user didn't specify DWORD/QWORD */
2371 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2374 if (s
!= 1 && i
!= -1)
2375 goto err
; /* no can do, in 16-bit EA */
2376 if (b
== -1 && i
!= -1) {
2381 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2386 /* have BX/BP as base, SI/DI index */
2388 goto err
; /* shouldn't ever happen, in theory */
2389 if (i
!= -1 && b
!= -1 &&
2390 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2391 goto err
; /* invalid combinations */
2392 if (b
== -1) /* pure offset: handled above */
2393 goto err
; /* so if it gets to here, panic! */
2397 switch (i
* 256 + b
) {
2398 case R_SI
* 256 + R_BX
:
2401 case R_DI
* 256 + R_BX
:
2404 case R_SI
* 256 + R_BP
:
2407 case R_DI
* 256 + R_BP
:
2425 if (rm
== -1) /* can't happen, in theory */
2426 goto err
; /* so panic if it does */
2428 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2429 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2431 else if (input
->eaflags
& EAF_BYTEOFFS
||
2432 (o
>= -128 && o
<= 127 && seg
== NO_SEG
&&
2433 !forw_ref
&& !(input
->eaflags
& EAF_WORDOFFS
)))
2438 output
->sib_present
= false; /* no SIB - it's 16-bit */
2439 output
->bytes
= mod
; /* bytes of offset needed */
2440 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2445 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2446 return output
->type
;
2449 return output
->type
= EA_INVALID
;
2452 static void add_asp(insn
*ins
, int addrbits
)
2457 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2459 switch (ins
->prefixes
[PPS_ASIZE
]) {
2470 valid
&= (addrbits
== 32) ? 16 : 32;
2476 for (j
= 0; j
< ins
->operands
; j
++) {
2477 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2480 /* Verify as Register */
2481 if (!is_register(ins
->oprs
[j
].indexreg
))
2484 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2486 /* Verify as Register */
2487 if (!is_register(ins
->oprs
[j
].basereg
))
2490 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2492 if (ins
->oprs
[j
].scale
== 0)
2496 int ds
= ins
->oprs
[j
].disp_size
;
2497 if ((addrbits
!= 64 && ds
> 8) ||
2498 (addrbits
== 64 && ds
== 16))
2518 if (valid
& addrbits
) {
2519 ins
->addr_size
= addrbits
;
2520 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2521 /* Add an address size prefix */
2522 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2523 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2526 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2527 ins
->addr_size
= addrbits
; /* Error recovery */
2530 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2532 for (j
= 0; j
< ins
->operands
; j
++) {
2533 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2534 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2536 * mem_offs sizes must match the address size; if not,
2537 * strip the MEM_OFFS bit and match only EA instructions
2539 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);