1 /* assemble.c code generation for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * the actual codes (C syntax, i.e. octal):
9 * \0 - terminates the code. (Unless it's a literal of course.)
10 * \1..\4 - that many literal bytes follow in the code stream
11 * \5 - add 4 to the primary operand number (b, low octdigit)
12 * \6 - add 4 to the secondary operand number (a, middle octdigit)
13 * \7 - add 4 to both the primary and the secondary operand number
14 * \10..\13 - a literal byte follows in the code stream, to be added
15 * to the register value of operand 0..3
16 * \14..\17 - a signed byte immediate operand, from operand 0..3
17 * \20..\23 - a byte immediate operand, from operand 0..3
18 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
19 * \30..\33 - a word immediate operand, from operand 0..3
20 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
21 * assembly mode or the operand-size override on the operand
22 * \40..\43 - a long immediate operand, from operand 0..3
23 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
24 * depending on the address size of the instruction.
25 * \50..\53 - a byte relative operand, from operand 0..3
26 * \54..\57 - a qword immediate operand, from operand 0..3
27 * \60..\63 - a word relative operand, from operand 0..3
28 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
29 * assembly mode or the operand-size override on the operand
30 * \70..\73 - a long relative operand, from operand 0..3
31 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
32 * \1ab - a ModRM, calculated on EA in operand a, with the spare
33 * field the register value of operand b.
34 * \140..\143 - an immediate word or signed byte for operand 0..3
35 * \144..\147 - or 2 (s-field) into opcode byte if operand 0..3
36 * is a signed byte rather than a word. Opcode byte follows.
37 * \150..\153 - an immediate dword or signed byte for operand 0..3
38 * \154..\157 - or 2 (s-field) into opcode byte if operand 0..3
39 * is a signed byte rather than a dword. Opcode byte follows.
40 * \160..\163 - this instruction uses DREX rather than REX, with the
41 * OC0 field set to 0, and the dest field taken from
43 * \164..\167 - this instruction uses DREX rather than REX, with the
44 * OC0 field set to 1, and the dest field taken from
46 * \171 - placement of DREX suffix in the absence of an EA
47 * \172\ab - the register number from operand a in bits 7..4, with
48 * the 4-bit immediate from operand b in bits 3..0.
49 * \173\xab - the register number from operand a in bits 7..4, with
50 * the value b in bits 3..0.
51 * \174\a - the register number from operand a in bits 7..4, and
52 * an arbitrary value in bits 3..0 (assembled as zero.)
53 * \2ab - a ModRM, calculated on EA in operand a, with the spare
54 * field equal to digit b.
55 * \250..\253 - same as \150..\153, except warn if the 64-bit operand
56 * is not equal to the truncated and sign-extended 32-bit
57 * operand; used for 32-bit immediates in 64-bit mode.
58 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
59 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
60 * V field taken from operand 0..3.
61 * \270 - this instruction uses VEX/XOP rather than REX, with the
62 * V field set to 1111b.
64 * VEX/XOP prefixes are followed by the sequence:
65 * \tmm\wlp where mm is the M field; and wlp is:
67 * [w0] ww = 0 for W = 0
68 * [w1] ww = 1 for W = 1
69 * [wx] ww = 2 for W don't care (always assembled as 0)
70 * [ww] ww = 3 for W used as REX.W
72 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
74 * \274..\277 - a signed byte immediate operand, from operand 0..3,
75 * which is to be extended to the operand size.
76 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
77 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
78 * \312 - (disassembler only) invalid with non-default address size.
79 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
80 * \314 - (disassembler only) invalid with REX.B
81 * \315 - (disassembler only) invalid with REX.X
82 * \316 - (disassembler only) invalid with REX.R
83 * \317 - (disassembler only) invalid with REX.W
84 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
85 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
86 * \322 - indicates that this instruction is only valid when the
87 * operand size is the default (instruction to disassembler,
88 * generates no code in the assembler)
89 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
90 * \324 - indicates 64-bit operand size requiring REX prefix.
91 * \325 - instruction which always uses spl/bpl/sil/dil
92 * \330 - a literal byte follows in the code stream, to be added
93 * to the condition code value of the instruction.
94 * \331 - instruction not valid with REP prefix. Hint for
95 * disassembler only; for SSE instructions.
96 * \332 - REP prefix (0xF2 byte) used as opcode extension.
97 * \333 - REP prefix (0xF3 byte) used as opcode extension.
98 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
99 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
100 * \336 - force a REP(E) prefix (0xF2) even if not specified.
101 * \337 - force a REPNE prefix (0xF3) even if not specified.
102 * \336-\337 are still listed as prefixes in the disassembler.
103 * \340 - reserve <operand 0> bytes of uninitialized storage.
104 * Operand 0 had better be a segmentless constant.
105 * \341 - this instruction needs a WAIT "prefix"
106 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
107 * (POP is never used for CS) depending on operand 0
108 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
110 * \360 - no SSE prefix (== \364\331)
111 * \361 - 66 SSE prefix (== \366\331)
112 * \362 - F2 SSE prefix (== \364\332)
113 * \363 - F3 SSE prefix (== \364\333)
114 * \364 - operand-size prefix (0x66) not permitted
115 * \365 - address-size prefix (0x67) not permitted
116 * \366 - operand-size prefix (0x66) used as opcode extension
117 * \367 - address-size prefix (0x67) used as opcode extension
118 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
119 * 370 is used for Jcc, 371 is used for JMP.
120 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
121 * used for conditional jump over longer jump
124 #include "compiler.h"
128 #include <inttypes.h>
132 #include "assemble.h"
137 int sib_present
; /* is a SIB byte necessary? */
138 int bytes
; /* # of bytes of offset needed */
139 int size
; /* lazy - this is sib+bytes+1 */
140 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
143 static uint32_t cpu
; /* cpu level received from nasm.c */
144 static efunc errfunc
;
145 static struct ofmt
*outfmt
;
146 static ListGen
*list
;
148 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
149 static void gencode(int32_t segment
, int64_t offset
, int bits
,
150 insn
* ins
, const struct itemplate
*temp
,
152 static int matches(const struct itemplate
*, insn
*, int bits
);
153 static int32_t regflag(const operand
*);
154 static int32_t regval(const operand
*);
155 static int rexflags(int, int32_t, int);
156 static int op_rexflags(const operand
*, int);
157 static ea
*process_ea(operand
*, ea
*, int, int, int, int32_t);
158 static void add_asp(insn
*, int);
160 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
162 return ins
->prefixes
[pos
] == prefix
;
165 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
167 if (ins
->prefixes
[pos
])
168 errfunc(ERR_NONFATAL
, "invalid %s prefix",
169 prefix_name(ins
->prefixes
[pos
]));
172 static const char *size_name(int size
)
194 static void warn_overflow(int size
, const struct operand
*o
)
196 if (size
< 8 && o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
197 int64_t lim
= ((int64_t)1 << (size
*8))-1;
198 int64_t data
= o
->offset
;
200 if (data
< ~lim
|| data
> lim
)
201 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
202 "%s data exceeds bounds", size_name(size
));
206 * This routine wrappers the real output format's output routine,
207 * in order to pass a copy of the data off to the listing file
208 * generator at the same time.
210 static void out(int64_t offset
, int32_t segto
, const void *data
,
211 enum out_type type
, uint64_t size
,
212 int32_t segment
, int32_t wrt
)
214 static int32_t lineno
= 0; /* static!!! */
215 static char *lnfname
= NULL
;
218 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
220 * This is a non-relocated address, and we're going to
221 * convert it into RAWDATA format.
226 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
230 WRITEADDR(q
, *(int64_t *)data
, size
);
235 list
->output(offset
, data
, type
, size
);
238 * this call to src_get determines when we call the
239 * debug-format-specific "linenum" function
240 * it updates lineno and lnfname to the current values
241 * returning 0 if "same as last time", -2 if lnfname
242 * changed, and the amount by which lineno changed,
243 * if it did. thus, these variables must be static
246 if (src_get(&lineno
, &lnfname
)) {
247 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
250 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
253 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
254 insn
* ins
, const uint8_t *code
)
259 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
263 if (optimizing
< 0 && c
== 0371)
266 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
268 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
269 /* Be optimistic in pass 1 */
272 if (ins
->oprs
[0].segment
!= segment
)
275 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
276 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
279 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
280 insn
* instruction
, struct ofmt
*output
, efunc error
,
283 const struct itemplate
*temp
;
288 int64_t start
= offset
;
289 int64_t wsize
= 0; /* size for DB etc. */
291 errfunc
= error
; /* to pass to other functions */
293 outfmt
= output
; /* likewise */
294 list
= listgen
; /* and again */
296 switch (instruction
->opcode
) {
326 int32_t t
= instruction
->times
;
329 "instruction->times < 0 (%ld) in assemble()", t
);
331 while (t
--) { /* repeat TIMES times */
332 for (e
= instruction
->eops
; e
; e
= e
->next
) {
333 if (e
->type
== EOT_DB_NUMBER
) {
335 if (e
->segment
!= NO_SEG
)
336 errfunc(ERR_NONFATAL
,
337 "one-byte relocation attempted");
339 uint8_t out_byte
= e
->offset
;
340 out(offset
, segment
, &out_byte
,
341 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
343 } else if (wsize
> 8) {
344 errfunc(ERR_NONFATAL
,
345 "integer supplied to a DT, DO or DY"
348 out(offset
, segment
, &e
->offset
,
349 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
351 } else if (e
->type
== EOT_DB_STRING
||
352 e
->type
== EOT_DB_STRING_FREE
) {
355 out(offset
, segment
, e
->stringval
,
356 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
357 align
= e
->stringlen
% wsize
;
360 align
= wsize
- align
;
361 out(offset
, segment
, zero_buffer
,
362 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
364 offset
+= e
->stringlen
+ align
;
367 if (t
> 0 && t
== instruction
->times
- 1) {
369 * Dummy call to list->output to give the offset to the
372 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
373 list
->uplevel(LIST_TIMES
);
376 if (instruction
->times
> 1)
377 list
->downlevel(LIST_TIMES
);
378 return offset
- start
;
381 if (instruction
->opcode
== I_INCBIN
) {
382 const char *fname
= instruction
->eops
->stringval
;
385 fp
= fopen(fname
, "rb");
387 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
389 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
390 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
393 static char buf
[4096];
394 size_t t
= instruction
->times
;
399 if (instruction
->eops
->next
) {
400 base
= instruction
->eops
->next
->offset
;
402 if (instruction
->eops
->next
->next
&&
403 len
> (size_t)instruction
->eops
->next
->next
->offset
)
404 len
= (size_t)instruction
->eops
->next
->next
->offset
;
407 * Dummy call to list->output to give the offset to the
410 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
411 list
->uplevel(LIST_INCBIN
);
415 fseek(fp
, base
, SEEK_SET
);
419 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
422 * This shouldn't happen unless the file
423 * actually changes while we are reading
427 "`incbin': unexpected EOF while"
428 " reading file `%s'", fname
);
429 t
= 0; /* Try to exit cleanly */
432 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
437 list
->downlevel(LIST_INCBIN
);
438 if (instruction
->times
> 1) {
440 * Dummy call to list->output to give the offset to the
443 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
444 list
->uplevel(LIST_TIMES
);
445 list
->downlevel(LIST_TIMES
);
448 return instruction
->times
* len
;
450 return 0; /* if we're here, there's an error */
453 /* Check to see if we need an address-size prefix */
454 add_asp(instruction
, bits
);
458 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++){
459 int m
= matches(temp
, instruction
, bits
);
461 (m
== 99 && jmp_match(segment
, offset
, bits
,
462 instruction
, temp
->code
))) {
464 int64_t insn_size
= calcsize(segment
, offset
, bits
,
465 instruction
, temp
->code
);
466 itimes
= instruction
->times
;
467 if (insn_size
< 0) /* shouldn't be, on pass two */
468 error(ERR_PANIC
, "errors made it through from pass one");
471 for (j
= 0; j
< MAXPREFIX
; j
++) {
473 switch (instruction
->prefixes
[j
]) {
491 error(ERR_WARNING
| ERR_PASS2
,
492 "cs segment base generated, but will be ignored in 64-bit mode");
498 error(ERR_WARNING
| ERR_PASS2
,
499 "ds segment base generated, but will be ignored in 64-bit mode");
505 error(ERR_WARNING
| ERR_PASS2
,
506 "es segment base generated, but will be ignored in 64-bit mode");
518 error(ERR_WARNING
| ERR_PASS2
,
519 "ss segment base generated, but will be ignored in 64-bit mode");
526 "segr6 and segr7 cannot be used as prefixes");
531 "16-bit addressing is not supported "
533 } else if (bits
!= 16)
543 "64-bit addressing is only supported "
567 error(ERR_PANIC
, "invalid instruction prefix");
570 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
575 insn_end
= offset
+ insn_size
;
576 gencode(segment
, offset
, bits
, instruction
,
579 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
581 * Dummy call to list->output to give the offset to the
584 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
585 list
->uplevel(LIST_TIMES
);
588 if (instruction
->times
> 1)
589 list
->downlevel(LIST_TIMES
);
590 return offset
- start
;
591 } else if (m
> 0 && m
> size_prob
) {
596 if (temp
->opcode
== -1) { /* didn't match any instruction */
599 error(ERR_NONFATAL
, "operation size not specified");
602 error(ERR_NONFATAL
, "mismatch in operand sizes");
605 error(ERR_NONFATAL
, "no instruction for this cpu level");
608 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
613 "invalid combination of opcode and operands");
620 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
621 insn
* instruction
, efunc error
)
623 const struct itemplate
*temp
;
625 errfunc
= error
; /* to pass to other functions */
628 if (instruction
->opcode
== -1)
631 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
632 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
633 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
634 instruction
->opcode
== I_DY
) {
636 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
639 switch (instruction
->opcode
) {
665 for (e
= instruction
->eops
; e
; e
= e
->next
) {
669 if (e
->type
== EOT_DB_NUMBER
)
671 else if (e
->type
== EOT_DB_STRING
||
672 e
->type
== EOT_DB_STRING_FREE
)
673 osize
= e
->stringlen
;
675 align
= (-osize
) % wsize
;
678 isize
+= osize
+ align
;
680 return isize
* instruction
->times
;
683 if (instruction
->opcode
== I_INCBIN
) {
684 const char *fname
= instruction
->eops
->stringval
;
688 fp
= fopen(fname
, "rb");
690 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
692 else if (fseek(fp
, 0L, SEEK_END
) < 0)
693 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
698 if (instruction
->eops
->next
) {
699 len
-= instruction
->eops
->next
->offset
;
700 if (instruction
->eops
->next
->next
&&
701 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
702 len
= (size_t)instruction
->eops
->next
->next
->offset
;
705 return instruction
->times
* len
;
707 return 0; /* if we're here, there's an error */
710 /* Check to see if we need an address-size prefix */
711 add_asp(instruction
, bits
);
713 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++) {
714 int m
= matches(temp
, instruction
, bits
);
716 (m
== 99 && jmp_match(segment
, offset
, bits
,
717 instruction
, temp
->code
))) {
718 /* we've matched an instruction. */
720 const uint8_t *codes
= temp
->code
;
723 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
726 for (j
= 0; j
< MAXPREFIX
; j
++) {
727 switch (instruction
->prefixes
[j
]) {
753 return isize
* instruction
->times
;
756 return -1; /* didn't match any instruction */
759 static bool possible_sbyte(operand
*o
)
761 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
762 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
763 optimizing
>= 0 && !(o
->type
& STRICT
);
766 /* check that opn[op] is a signed byte of size 16 or 32 */
767 static bool is_sbyte16(operand
*o
)
771 if (!possible_sbyte(o
))
775 return v
>= -128 && v
<= 127;
778 static bool is_sbyte32(operand
*o
)
782 if (!possible_sbyte(o
))
786 return v
>= -128 && v
<= 127;
789 /* Common construct */
790 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
792 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
793 insn
* ins
, const uint8_t *codes
)
802 ins
->rex
= 0; /* Ensure REX is reset */
804 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
807 (void)segment
; /* Don't warn that this parameter is unused */
808 (void)offset
; /* Don't warn that this parameter is unused */
812 op1
= (c
& 3) + ((opex
& 1) << 2);
813 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
814 opx
= &ins
->oprs
[op1
];
815 opex
= 0; /* For the next iteration */
822 codes
+= c
, length
+= c
;
833 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
848 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
849 length
+= (opx
->type
& BITS16
) ? 2 : 4;
851 length
+= (bits
== 16) ? 2 : 4;
859 length
+= ins
->addr_size
>> 3;
867 length
+= 8; /* MOV reg64/imm */
875 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
876 length
+= (opx
->type
& BITS16
) ? 2 : 4;
878 length
+= (bits
== 16) ? 2 : 4;
890 length
+= is_sbyte16(opx
) ? 1 : 2;
899 length
+= is_sbyte32(opx
) ? 1 : 4;
910 ins
->drexdst
= regval(opx
);
915 ins
->rex
|= REX_D
|REX_OC
;
916 ins
->drexdst
= regval(opx
);
930 length
+= is_sbyte32(opx
) ? 1 : 4;
939 ins
->drexdst
= regval(opx
);
940 ins
->vex_cm
= *codes
++;
941 ins
->vex_wlp
= *codes
++;
947 ins
->vex_cm
= *codes
++;
948 ins
->vex_wlp
= *codes
++;
961 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
965 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
972 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
973 has_prefix(ins
, PPS_ASIZE
, P_A32
))
981 length
+= (bits
!= 16);
985 length
+= (bits
== 16);
1023 if (!ins
->prefixes
[PPS_LREP
])
1024 ins
->prefixes
[PPS_LREP
] = P_REP
;
1028 if (!ins
->prefixes
[PPS_LREP
])
1029 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1033 if (ins
->oprs
[0].segment
!= NO_SEG
)
1034 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1035 " quantity of BSS space");
1037 length
+= ins
->oprs
[0].offset
;
1041 if (!ins
->prefixes
[PPS_WAIT
])
1042 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1092 struct operand
*opy
= &ins
->oprs
[op2
];
1094 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1097 /* pick rfield from operand b (opx) */
1098 rflags
= regflag(opx
);
1099 rfield
= nasm_regvals
[opx
->basereg
];
1104 if (!process_ea(opy
, &ea_data
, bits
,
1105 ins
->addr_size
, rfield
, rflags
)) {
1106 errfunc(ERR_NONFATAL
, "invalid effective address");
1109 ins
->rex
|= ea_data
.rex
;
1110 length
+= ea_data
.size
;
1116 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1117 ": instruction code \\%o (0x%02X) given", c
, c
);
1122 ins
->rex
&= rex_mask
;
1124 if (ins
->rex
& REX_NH
) {
1125 if (ins
->rex
& REX_H
) {
1126 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1129 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1132 if (ins
->rex
& REX_V
) {
1133 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1135 if (ins
->rex
& REX_H
) {
1136 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1139 switch (ins
->vex_wlp
& 030) {
1153 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1154 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1157 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1161 } else if (ins
->rex
& REX_D
) {
1162 if (ins
->rex
& REX_H
) {
1163 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1166 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1167 ins
->drexdst
> 7)) {
1168 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1172 } else if (ins
->rex
& REX_REAL
) {
1173 if (ins
->rex
& REX_H
) {
1174 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1176 } else if (bits
== 64) {
1178 } else if ((ins
->rex
& REX_L
) &&
1179 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1182 assert_no_prefix(ins
, PPS_LREP
);
1185 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1193 #define EMIT_REX() \
1194 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1195 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1196 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1201 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1202 insn
* ins
, const struct itemplate
*temp
,
1205 static char condval
[] = { /* conditional opcodes */
1206 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1207 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1208 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1215 struct operand
*opx
;
1216 const uint8_t *codes
= temp
->code
;
1221 op1
= (c
& 3) + ((opex
& 1) << 2);
1222 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1223 opx
= &ins
->oprs
[op1
];
1224 opex
= 0; /* For the next iteration */
1232 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1245 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1246 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1251 /* The test for BITS8 and SBYTE here is intended to avoid
1252 warning on optimizer actions due to SBYTE, while still
1253 warn on explicit BYTE directives. Also warn, obviously,
1254 if the optimizer isn't enabled. */
1255 if (((opx
->type
& BITS8
) ||
1256 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1257 (opx
->offset
< -128 || opx
->offset
> 127)) {
1258 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1259 "signed byte value exceeds bounds");
1261 if (opx
->segment
!= NO_SEG
) {
1263 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1264 opx
->segment
, opx
->wrt
);
1266 bytes
[0] = opx
->offset
;
1267 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1274 if (opx
->offset
< -256 || opx
->offset
> 255) {
1275 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1276 "byte value exceeds bounds");
1278 if (opx
->segment
!= NO_SEG
) {
1280 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1281 opx
->segment
, opx
->wrt
);
1283 bytes
[0] = opx
->offset
;
1284 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1291 if (opx
->offset
< 0 || opx
->offset
> 255)
1292 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1293 "unsigned byte value exceeds bounds");
1294 if (opx
->segment
!= NO_SEG
) {
1296 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1297 opx
->segment
, opx
->wrt
);
1299 bytes
[0] = opx
->offset
;
1300 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1307 warn_overflow(2, opx
);
1309 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1310 opx
->segment
, opx
->wrt
);
1315 if (opx
->type
& (BITS16
| BITS32
))
1316 size
= (opx
->type
& BITS16
) ? 2 : 4;
1318 size
= (bits
== 16) ? 2 : 4;
1319 warn_overflow(size
, opx
);
1321 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1322 opx
->segment
, opx
->wrt
);
1327 warn_overflow(4, opx
);
1329 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1330 opx
->segment
, opx
->wrt
);
1336 size
= ins
->addr_size
>> 3;
1337 warn_overflow(size
, opx
);
1338 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1339 opx
->segment
, opx
->wrt
);
1344 if (opx
->segment
!= segment
)
1345 errfunc(ERR_NONFATAL
,
1346 "short relative jump outside segment");
1347 data
= opx
->offset
- insn_end
;
1348 if (data
> 127 || data
< -128)
1349 errfunc(ERR_NONFATAL
, "short jump is out of range");
1351 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1356 data
= (int64_t)opx
->offset
;
1357 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1358 opx
->segment
, opx
->wrt
);
1363 if (opx
->segment
!= segment
) {
1365 out(offset
, segment
, &data
,
1366 OUT_REL2ADR
, insn_end
- offset
,
1367 opx
->segment
, opx
->wrt
);
1369 data
= opx
->offset
- insn_end
;
1370 out(offset
, segment
, &data
,
1371 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1377 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1378 size
= (opx
->type
& BITS16
) ? 2 : 4;
1380 size
= (bits
== 16) ? 2 : 4;
1381 if (opx
->segment
!= segment
) {
1383 out(offset
, segment
, &data
,
1384 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1385 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1387 data
= opx
->offset
- insn_end
;
1388 out(offset
, segment
, &data
,
1389 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1395 if (opx
->segment
!= segment
) {
1397 out(offset
, segment
, &data
,
1398 OUT_REL4ADR
, insn_end
- offset
,
1399 opx
->segment
, opx
->wrt
);
1401 data
= opx
->offset
- insn_end
;
1402 out(offset
, segment
, &data
,
1403 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1409 if (opx
->segment
== NO_SEG
)
1410 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1413 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1414 outfmt
->segbase(1 + opx
->segment
),
1421 warn_overflow(2, opx
);
1422 if (is_sbyte16(opx
)) {
1424 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1428 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1429 opx
->segment
, opx
->wrt
);
1436 bytes
[0] = *codes
++;
1437 if (is_sbyte16(opx
))
1438 bytes
[0] |= 2; /* s-bit */
1439 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1445 warn_overflow(4, opx
);
1446 if (is_sbyte32(opx
)) {
1448 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1452 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1453 opx
->segment
, opx
->wrt
);
1460 bytes
[0] = *codes
++;
1461 if (is_sbyte32(opx
))
1462 bytes
[0] |= 2; /* s-bit */
1463 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1473 (ins
->drexdst
<< 4) |
1474 (ins
->rex
& REX_OC
? 0x08 : 0) |
1475 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1477 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1483 opx
= &ins
->oprs
[c
>> 3];
1484 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1485 opx
= &ins
->oprs
[c
& 7];
1486 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1487 errfunc(ERR_NONFATAL
,
1488 "non-absolute expression not permitted as argument %d",
1491 if (opx
->offset
& ~15) {
1492 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1493 "four-bit argument exceeds bounds");
1495 bytes
[0] |= opx
->offset
& 15;
1497 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1503 opx
= &ins
->oprs
[c
>> 4];
1504 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1506 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1512 opx
= &ins
->oprs
[c
];
1513 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1514 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1520 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1521 (int32_t)data
!= (int64_t)data
) {
1522 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1523 "signed dword immediate exceeds bounds");
1525 if (is_sbyte32(opx
)) {
1527 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1531 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1532 opx
->segment
, opx
->wrt
);
1539 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1540 (int32_t)data
!= (int64_t)data
) {
1541 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1542 "signed dword immediate exceeds bounds");
1544 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1545 opx
->segment
, opx
->wrt
);
1552 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1553 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1554 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1555 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1556 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1557 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1561 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1562 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1563 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1573 if (ins
->rex
& REX_W
)
1575 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1577 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1582 um
= (uint64_t)2 << (s
-1);
1585 if (uv
> 127 && uv
< (uint64_t)-128 &&
1586 (uv
< um
-128 || uv
> um
-1)) {
1587 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1588 "signed byte value exceeds bounds");
1590 if (opx
->segment
!= NO_SEG
) {
1592 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1593 opx
->segment
, opx
->wrt
);
1596 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1607 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1609 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1616 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1618 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1637 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1646 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1664 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1665 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1674 *bytes
= c
- 0332 + 0xF2;
1675 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1680 if (ins
->rex
& REX_R
) {
1682 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1685 ins
->rex
&= ~(REX_L
|REX_R
);
1696 if (ins
->oprs
[0].segment
!= NO_SEG
)
1697 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1699 int64_t size
= ins
->oprs
[0].offset
;
1701 out(offset
, segment
, NULL
,
1702 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1713 switch (ins
->oprs
[0].basereg
) {
1728 "bizarre 8086 segment register received");
1730 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1737 switch (ins
->oprs
[0].basereg
) {
1746 "bizarre 386 segment register received");
1748 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1757 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1763 bytes
[0] = c
- 0362 + 0xf2;
1764 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1774 *bytes
= c
- 0366 + 0x66;
1775 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1785 *bytes
= bits
== 16 ? 3 : 5;
1786 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1809 struct operand
*opy
= &ins
->oprs
[op2
];
1812 /* pick rfield from operand b (opx) */
1813 rflags
= regflag(opx
);
1814 rfield
= nasm_regvals
[opx
->basereg
];
1816 /* rfield is constant */
1821 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1823 errfunc(ERR_NONFATAL
, "invalid effective address");
1828 *p
++ = ea_data
.modrm
;
1829 if (ea_data
.sib_present
)
1832 /* DREX suffixes come between the SIB and the displacement */
1833 if (ins
->rex
& REX_D
) {
1834 *p
++ = (ins
->drexdst
<< 4) |
1835 (ins
->rex
& REX_OC
? 0x08 : 0) |
1836 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1841 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1844 * Make sure the address gets the right offset in case
1845 * the line breaks in the .lst file (BR 1197827)
1850 switch (ea_data
.bytes
) {
1858 warn_overflow(ea_data
.bytes
, opy
);
1861 if (opy
->segment
== segment
) {
1863 out(offset
, segment
, &data
, OUT_ADDRESS
,
1864 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1866 out(offset
, segment
, &data
, OUT_REL4ADR
,
1867 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1871 out(offset
, segment
, &data
, OUT_ADDRESS
,
1872 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1878 "Invalid amount of bytes (%d) for offset?!",
1887 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1888 ": instruction code \\%o (0x%02X) given", c
, c
);
1894 static int32_t regflag(const operand
* o
)
1896 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1897 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1899 return nasm_reg_flags
[o
->basereg
];
1902 static int32_t regval(const operand
* o
)
1904 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1905 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1907 return nasm_regvals
[o
->basereg
];
1910 static int op_rexflags(const operand
* o
, int mask
)
1915 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1916 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1919 flags
= nasm_reg_flags
[o
->basereg
];
1920 val
= nasm_regvals
[o
->basereg
];
1922 return rexflags(val
, flags
, mask
);
1925 static int rexflags(int val
, int32_t flags
, int mask
)
1930 rex
|= REX_B
|REX_X
|REX_R
;
1933 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1935 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1941 static int matches(const struct itemplate
*itemp
, insn
* instruction
, int bits
)
1943 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1950 if (itemp
->opcode
!= instruction
->opcode
)
1954 * Count the operands
1956 if (itemp
->operands
!= instruction
->operands
)
1960 * Check that no spurious colons or TOs are present
1962 for (i
= 0; i
< itemp
->operands
; i
++)
1963 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
1967 * Process size flags
1969 if (itemp
->flags
& IF_ARMASK
) {
1970 memset(size
, 0, sizeof size
);
1972 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
1974 switch (itemp
->flags
& IF_SMASK
) {
2011 switch (itemp
->flags
& IF_SMASK
) {
2046 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2051 * Check that the operand flags all match up
2053 for (i
= 0; i
< itemp
->operands
; i
++) {
2054 int32_t type
= instruction
->oprs
[i
].type
;
2055 if (!(type
& SIZE_MASK
))
2058 if (itemp
->opd
[i
] & SAME_AS
) {
2059 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2060 if (type
!= instruction
->oprs
[j
].type
||
2061 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2063 } else if (itemp
->opd
[i
] & ~type
||
2064 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2065 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2066 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2075 * Check operand sizes
2077 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2078 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2080 for (i
= 0; i
< oprs
; i
++) {
2081 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2083 for (j
= 0; j
< oprs
; j
++)
2089 oprs
= itemp
->operands
;
2092 for (i
= 0; i
< itemp
->operands
; i
++) {
2093 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2094 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2099 * Check template is okay at the set cpu level
2101 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2105 * Verify the appropriate long mode flag.
2107 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2111 * Check if special handling needed for Jumps
2113 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2119 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2120 int addrbits
, int rfield
, int32_t rflags
)
2122 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2124 output
->rip
= false;
2126 /* REX flags for the rfield operand */
2127 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2129 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2133 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2134 || input
->basereg
>= REG_ENUM_LIMIT
)
2137 i
= nasm_regvals
[input
->basereg
];
2140 return NULL
; /* Invalid EA register */
2142 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2144 output
->sib_present
= false; /* no SIB necessary */
2145 output
->bytes
= 0; /* no offset necessary either */
2146 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2147 } else { /* it's a memory reference */
2148 if (input
->basereg
== -1
2149 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2150 /* it's a pure offset */
2151 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2152 int scale
, index
, base
;
2153 output
->sib_present
= true;
2157 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2159 output
->modrm
= 4 | ((rfield
& 7) << 3);
2160 output
->rip
= false;
2162 output
->sib_present
= false;
2163 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2164 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2165 output
->rip
= bits
== 64;
2167 } else { /* it's an indirection */
2168 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2169 int32_t o
= input
->offset
, seg
= input
->segment
;
2170 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2173 int32_t ix
, bx
; /* register flags */
2176 i
= -1; /* make this easy, at least */
2178 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2179 it
= nasm_regvals
[i
];
2180 ix
= nasm_reg_flags
[i
];
2186 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2187 bt
= nasm_regvals
[b
];
2188 bx
= nasm_reg_flags
[b
];
2194 /* check for a 32/64-bit memory reference... */
2195 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2196 /* it must be a 32/64-bit memory reference. Firstly we have
2197 * to check that all registers involved are type E/Rxx. */
2198 int32_t sok
= BITS32
|BITS64
;
2201 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2209 return NULL
; /* Invalid register */
2210 if (~sok
& bx
& SIZE_MASK
)
2211 return NULL
; /* Invalid size */
2215 /* While we're here, ensure the user didn't specify
2217 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2220 if (addrbits
== 16 ||
2221 (addrbits
== 32 && !(sok
& BITS32
)) ||
2222 (addrbits
== 64 && !(sok
& BITS64
)))
2225 /* now reorganize base/index */
2226 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2227 ((hb
== b
&& ht
== EAH_NOTBASE
)
2228 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2229 /* swap if hints say so */
2230 t
= bt
, bt
= it
, it
= t
;
2231 t
= bx
, bx
= ix
, ix
= t
;
2233 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2234 bt
= -1, bx
= 0, s
++;
2235 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2236 /* make single reg base, unless hint */
2237 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2239 if (((s
== 2 && it
!= REG_NUM_ESP
2240 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2241 || s
== 5 || s
== 9) && bt
== -1)
2242 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2243 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2244 && (input
->eaflags
& EAF_TIMESTWO
))
2245 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2246 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2247 if (s
== 1 && it
== REG_NUM_ESP
) {
2248 /* swap ESP into base if scale is 1 */
2249 t
= it
, it
= bt
, bt
= t
;
2250 t
= ix
, ix
= bx
, bx
= t
;
2252 if (it
== REG_NUM_ESP
2253 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2254 return NULL
; /* wrong, for various reasons */
2256 output
->rex
|= rexflags(it
, ix
, REX_X
);
2257 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2259 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2268 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2269 seg
== NO_SEG
&& !forw_ref
&&
2271 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2273 else if (input
->eaflags
& EAF_BYTEOFFS
||
2274 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2276 && !(input
->eaflags
& EAF_WORDOFFS
)))
2282 output
->sib_present
= false;
2283 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2284 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2287 int mod
, scale
, index
, base
;
2307 default: /* then what the smeg is it? */
2308 return NULL
; /* panic */
2316 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2317 seg
== NO_SEG
&& !forw_ref
&&
2319 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2321 else if (input
->eaflags
& EAF_BYTEOFFS
||
2322 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2324 && !(input
->eaflags
& EAF_WORDOFFS
)))
2330 output
->sib_present
= true;
2331 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2332 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2333 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2335 } else { /* it's 16-bit */
2338 /* check for 64-bit long mode */
2342 /* check all registers are BX, BP, SI or DI */
2343 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2344 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2345 && i
!= R_SI
&& i
!= R_DI
))
2348 /* ensure the user didn't specify DWORD/QWORD */
2349 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2352 if (s
!= 1 && i
!= -1)
2353 return NULL
; /* no can do, in 16-bit EA */
2354 if (b
== -1 && i
!= -1) {
2359 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2364 /* have BX/BP as base, SI/DI index */
2366 return NULL
; /* shouldn't ever happen, in theory */
2367 if (i
!= -1 && b
!= -1 &&
2368 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2369 return NULL
; /* invalid combinations */
2370 if (b
== -1) /* pure offset: handled above */
2371 return NULL
; /* so if it gets to here, panic! */
2375 switch (i
* 256 + b
) {
2376 case R_SI
* 256 + R_BX
:
2379 case R_DI
* 256 + R_BX
:
2382 case R_SI
* 256 + R_BP
:
2385 case R_DI
* 256 + R_BP
:
2403 if (rm
== -1) /* can't happen, in theory */
2404 return NULL
; /* so panic if it does */
2406 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2407 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2409 else if (input
->eaflags
& EAF_BYTEOFFS
||
2410 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2412 && !(input
->eaflags
& EAF_WORDOFFS
)))
2417 output
->sib_present
= false; /* no SIB - it's 16-bit */
2418 output
->bytes
= mod
; /* bytes of offset needed */
2419 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2424 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2428 static void add_asp(insn
*ins
, int addrbits
)
2433 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2435 switch (ins
->prefixes
[PPS_ASIZE
]) {
2446 valid
&= (addrbits
== 32) ? 16 : 32;
2452 for (j
= 0; j
< ins
->operands
; j
++) {
2453 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2456 /* Verify as Register */
2457 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2458 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2461 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2463 /* Verify as Register */
2464 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2465 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2468 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2470 if (ins
->oprs
[j
].scale
== 0)
2474 int ds
= ins
->oprs
[j
].disp_size
;
2475 if ((addrbits
!= 64 && ds
> 8) ||
2476 (addrbits
== 64 && ds
== 16))
2496 if (valid
& addrbits
) {
2497 ins
->addr_size
= addrbits
;
2498 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2499 /* Add an address size prefix */
2500 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2501 ins
->prefixes
[PPS_ASIZE
] = pref
;
2502 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2505 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2506 ins
->addr_size
= addrbits
; /* Error recovery */
2509 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2511 for (j
= 0; j
< ins
->operands
; j
++) {
2512 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2513 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2514 != ins
->addr_size
) {
2515 /* mem_offs sizes must match the address size; if not,
2516 strip the MEM_OFFS bit and match only EA instructions */
2517 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);