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 rather than REX, with the
60 * V field taken from operand 0..3.
61 * \270 - this instruction uses VEX rather than REX, with the
62 * V field set to 1111b.
64 * VEX prefixes are followed by the sequence:
65 * \mm\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
73 * \274..\277 - a signed byte immediate operand, from operand 0..3,
74 * which is to be extended to the operand size.
75 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
76 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
77 * \312 - (disassembler only) marker on LOOP, LOOPxx instructions.
78 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
79 * \314 - (disassembler only) invalid with REX.B
80 * \315 - (disassembler only) invalid with REX.X
81 * \316 - (disassembler only) invalid with REX.R
82 * \317 - (disassembler only) invalid with REX.W
83 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
84 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
85 * \322 - indicates that this instruction is only valid when the
86 * operand size is the default (instruction to disassembler,
87 * generates no code in the assembler)
88 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
89 * \324 - indicates 64-bit operand size requiring REX prefix.
90 * \330 - a literal byte follows in the code stream, to be added
91 * to the condition code value of the instruction.
92 * \331 - instruction not valid with REP prefix. Hint for
93 * disassembler only; for SSE instructions.
94 * \332 - REP prefix (0xF2 byte) used as opcode extension.
95 * \333 - REP prefix (0xF3 byte) used as opcode extension.
96 * \334 - LOCK prefix used instead of REX.R
97 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
98 * \336 - force a REP(E) prefix (0xF2) even if not specified.
99 * \337 - force a REPNE prefix (0xF3) even if not specified.
100 * \336-\337 are still listed as prefixes in the disassembler.
101 * \340 - reserve <operand 0> bytes of uninitialized storage.
102 * Operand 0 had better be a segmentless constant.
103 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
104 * (POP is never used for CS) depending on operand 0
105 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
107 * \360 - no SSE prefix (== \364\331)
108 * \361 - 66 SSE prefix (== \366\331)
109 * \362 - F2 SSE prefix (== \364\332)
110 * \363 - F3 SSE prefix (== \364\333)
111 * \364 - operand-size prefix (0x66) not permitted
112 * \365 - address-size prefix (0x67) not permitted
113 * \366 - operand-size prefix (0x66) used as opcode extension
114 * \367 - address-size prefix (0x67) used as opcode extension
115 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
116 * 370 is used for Jcc, 371 is used for JMP.
117 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
118 * used for conditional jump over longer jump
121 #include "compiler.h"
125 #include <inttypes.h>
129 #include "assemble.h"
133 /* Initialized to zero by the C standard */
134 static const uint8_t const_zero_buf
[256];
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
);
267 if (ins
->oprs
[0].segment
!= segment
)
270 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
271 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
274 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
275 insn
* instruction
, struct ofmt
*output
, efunc error
,
278 const struct itemplate
*temp
;
283 int64_t start
= offset
;
284 int64_t wsize
= 0; /* size for DB etc. */
286 errfunc
= error
; /* to pass to other functions */
288 outfmt
= output
; /* likewise */
289 list
= listgen
; /* and again */
291 switch (instruction
->opcode
) {
321 int32_t t
= instruction
->times
;
324 "instruction->times < 0 (%ld) in assemble()", t
);
326 while (t
--) { /* repeat TIMES times */
327 for (e
= instruction
->eops
; e
; e
= e
->next
) {
328 if (e
->type
== EOT_DB_NUMBER
) {
330 if (e
->segment
!= NO_SEG
)
331 errfunc(ERR_NONFATAL
,
332 "one-byte relocation attempted");
334 uint8_t out_byte
= e
->offset
;
335 out(offset
, segment
, &out_byte
,
336 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
338 } else if (wsize
> 8) {
339 errfunc(ERR_NONFATAL
,
340 "integer supplied to a DT, DO or DY"
343 out(offset
, segment
, &e
->offset
,
344 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
346 } else if (e
->type
== EOT_DB_STRING
||
347 e
->type
== EOT_DB_STRING_FREE
) {
350 out(offset
, segment
, e
->stringval
,
351 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
352 align
= e
->stringlen
% wsize
;
355 align
= wsize
- align
;
356 out(offset
, segment
, const_zero_buf
,
357 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
359 offset
+= e
->stringlen
+ align
;
362 if (t
> 0 && t
== instruction
->times
- 1) {
364 * Dummy call to list->output to give the offset to the
367 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
368 list
->uplevel(LIST_TIMES
);
371 if (instruction
->times
> 1)
372 list
->downlevel(LIST_TIMES
);
373 return offset
- start
;
376 if (instruction
->opcode
== I_INCBIN
) {
377 const char *fname
= instruction
->eops
->stringval
;
380 fp
= fopen(fname
, "rb");
382 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
384 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
385 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
388 static char buf
[4096];
389 size_t t
= instruction
->times
;
394 if (instruction
->eops
->next
) {
395 base
= instruction
->eops
->next
->offset
;
397 if (instruction
->eops
->next
->next
&&
398 len
> (size_t)instruction
->eops
->next
->next
->offset
)
399 len
= (size_t)instruction
->eops
->next
->next
->offset
;
402 * Dummy call to list->output to give the offset to the
405 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
406 list
->uplevel(LIST_INCBIN
);
410 fseek(fp
, base
, SEEK_SET
);
414 fread(buf
, 1, (l
> (int32_t) sizeof(buf
) ? (int32_t) sizeof(buf
) : l
),
418 * This shouldn't happen unless the file
419 * actually changes while we are reading
423 "`incbin': unexpected EOF while"
424 " reading file `%s'", fname
);
425 t
= 0; /* Try to exit cleanly */
428 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
433 list
->downlevel(LIST_INCBIN
);
434 if (instruction
->times
> 1) {
436 * Dummy call to list->output to give the offset to the
439 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
440 list
->uplevel(LIST_TIMES
);
441 list
->downlevel(LIST_TIMES
);
444 return instruction
->times
* len
;
446 return 0; /* if we're here, there's an error */
449 /* Check to see if we need an address-size prefix */
450 add_asp(instruction
, bits
);
454 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++){
455 int m
= matches(temp
, instruction
, bits
);
457 (m
== 99 && jmp_match(segment
, offset
, bits
,
458 instruction
, temp
->code
))) {
460 int64_t insn_size
= calcsize(segment
, offset
, bits
,
461 instruction
, temp
->code
);
462 itimes
= instruction
->times
;
463 if (insn_size
< 0) /* shouldn't be, on pass two */
464 error(ERR_PANIC
, "errors made it through from pass one");
467 for (j
= 0; j
< MAXPREFIX
; j
++) {
469 switch (instruction
->prefixes
[j
]) {
484 error(ERR_WARNING
| ERR_PASS2
,
485 "cs segment base generated, but will be ignored in 64-bit mode");
491 error(ERR_WARNING
| ERR_PASS2
,
492 "ds segment base generated, but will be ignored in 64-bit mode");
498 error(ERR_WARNING
| ERR_PASS2
,
499 "es segment base generated, but will be ignored in 64-bit mode");
511 error(ERR_WARNING
| ERR_PASS2
,
512 "ss segment base generated, but will be ignored in 64-bit mode");
519 "segr6 and segr7 cannot be used as prefixes");
524 "16-bit addressing is not supported "
526 } else if (bits
!= 16)
536 "64-bit addressing is only supported "
560 error(ERR_PANIC
, "invalid instruction prefix");
563 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
568 insn_end
= offset
+ insn_size
;
569 gencode(segment
, offset
, bits
, instruction
,
572 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
574 * Dummy call to list->output to give the offset to the
577 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
578 list
->uplevel(LIST_TIMES
);
581 if (instruction
->times
> 1)
582 list
->downlevel(LIST_TIMES
);
583 return offset
- start
;
584 } else if (m
> 0 && m
> size_prob
) {
589 if (temp
->opcode
== -1) { /* didn't match any instruction */
592 error(ERR_NONFATAL
, "operation size not specified");
595 error(ERR_NONFATAL
, "mismatch in operand sizes");
598 error(ERR_NONFATAL
, "no instruction for this cpu level");
601 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
606 "invalid combination of opcode and operands");
613 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
614 insn
* instruction
, efunc error
)
616 const struct itemplate
*temp
;
618 errfunc
= error
; /* to pass to other functions */
621 if (instruction
->opcode
== -1)
624 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
625 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
626 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
627 instruction
->opcode
== I_DY
) {
629 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
632 switch (instruction
->opcode
) {
658 for (e
= instruction
->eops
; e
; e
= e
->next
) {
662 if (e
->type
== EOT_DB_NUMBER
)
664 else if (e
->type
== EOT_DB_STRING
||
665 e
->type
== EOT_DB_STRING_FREE
)
666 osize
= e
->stringlen
;
668 align
= (-osize
) % wsize
;
671 isize
+= osize
+ align
;
673 return isize
* instruction
->times
;
676 if (instruction
->opcode
== I_INCBIN
) {
677 const char *fname
= instruction
->eops
->stringval
;
681 fp
= fopen(fname
, "rb");
683 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
685 else if (fseek(fp
, 0L, SEEK_END
) < 0)
686 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
691 if (instruction
->eops
->next
) {
692 len
-= instruction
->eops
->next
->offset
;
693 if (instruction
->eops
->next
->next
&&
694 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
695 len
= (size_t)instruction
->eops
->next
->next
->offset
;
698 return instruction
->times
* len
;
700 return 0; /* if we're here, there's an error */
703 /* Check to see if we need an address-size prefix */
704 add_asp(instruction
, bits
);
706 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++) {
707 int m
= matches(temp
, instruction
, bits
);
709 (m
== 99 && jmp_match(segment
, offset
, bits
,
710 instruction
, temp
->code
))) {
711 /* we've matched an instruction. */
713 const uint8_t *codes
= temp
->code
;
716 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
719 for (j
= 0; j
< MAXPREFIX
; j
++) {
720 switch (instruction
->prefixes
[j
]) {
746 return isize
* instruction
->times
;
749 return -1; /* didn't match any instruction */
752 static bool possible_sbyte(operand
*o
)
754 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
755 !(o
->opflags
& OPFLAG_FORWARD
) &&
756 optimizing
>= 0 && !(o
->type
& STRICT
);
759 /* check that opn[op] is a signed byte of size 16 or 32 */
760 static bool is_sbyte16(operand
*o
)
764 if (!possible_sbyte(o
))
768 return v
>= -128 && v
<= 127;
771 static bool is_sbyte32(operand
*o
)
775 if (!possible_sbyte(o
))
779 return v
>= -128 && v
<= 127;
782 /* Common construct */
783 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
785 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
786 insn
* ins
, const uint8_t *codes
)
795 ins
->rex
= 0; /* Ensure REX is reset */
797 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
800 (void)segment
; /* Don't warn that this parameter is unused */
801 (void)offset
; /* Don't warn that this parameter is unused */
805 op1
= (c
& 3) + ((opex
& 1) << 2);
806 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
807 opx
= &ins
->oprs
[op1
];
808 opex
= 0; /* For the next iteration */
815 codes
+= c
, length
+= c
;
826 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
841 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
842 length
+= (opx
->type
& BITS16
) ? 2 : 4;
844 length
+= (bits
== 16) ? 2 : 4;
852 length
+= ins
->addr_size
>> 3;
860 length
+= 8; /* MOV reg64/imm */
868 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
869 length
+= (opx
->type
& BITS16
) ? 2 : 4;
871 length
+= (bits
== 16) ? 2 : 4;
883 length
+= is_sbyte16(opx
) ? 1 : 2;
892 length
+= is_sbyte32(opx
) ? 1 : 4;
903 ins
->drexdst
= regval(opx
);
908 ins
->rex
|= REX_D
|REX_OC
;
909 ins
->drexdst
= regval(opx
);
923 length
+= is_sbyte32(opx
) ? 1 : 4;
932 ins
->drexdst
= regval(opx
);
933 ins
->vex_m
= *codes
++;
934 ins
->vex_wlp
= *codes
++;
940 ins
->vex_m
= *codes
++;
941 ins
->vex_wlp
= *codes
++;
954 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
958 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
965 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
966 has_prefix(ins
, PPS_ASIZE
, P_A32
))
974 length
+= (bits
!= 16);
978 length
+= (bits
== 16);
1012 if (!ins
->prefixes
[PPS_LREP
])
1013 ins
->prefixes
[PPS_LREP
] = P_REP
;
1017 if (!ins
->prefixes
[PPS_LREP
])
1018 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1022 if (ins
->oprs
[0].segment
!= NO_SEG
)
1023 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1024 " quantity of BSS space");
1026 length
+= ins
->oprs
[0].offset
;
1076 struct operand
*opy
= &ins
->oprs
[op2
];
1078 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1081 /* pick rfield from operand b (opx) */
1082 rflags
= regflag(opx
);
1083 rfield
= nasm_regvals
[opx
->basereg
];
1088 if (!process_ea(opy
, &ea_data
, bits
,
1089 ins
->addr_size
, rfield
, rflags
)) {
1090 errfunc(ERR_NONFATAL
, "invalid effective address");
1093 ins
->rex
|= ea_data
.rex
;
1094 length
+= ea_data
.size
;
1100 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1101 ": instruction code 0x%02X given", c
);
1106 ins
->rex
&= rex_mask
;
1108 if (ins
->rex
& REX_V
) {
1109 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1111 if (ins
->rex
& REX_H
) {
1112 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1115 switch (ins
->vex_wlp
& 030) {
1129 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1130 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1133 if (ins
->vex_m
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1137 } else if (ins
->rex
& REX_D
) {
1138 if (ins
->rex
& REX_H
) {
1139 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1142 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1143 ins
->drexdst
> 7)) {
1144 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1148 } else if (ins
->rex
& REX_REAL
) {
1149 if (ins
->rex
& REX_H
) {
1150 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1152 } else if (bits
== 64) {
1154 } else if ((ins
->rex
& REX_L
) &&
1155 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1158 assert_no_prefix(ins
, PPS_LREP
);
1161 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1169 #define EMIT_REX() \
1170 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1171 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1172 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1177 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1178 insn
* ins
, const struct itemplate
*temp
,
1181 static char condval
[] = { /* conditional opcodes */
1182 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1183 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1184 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1191 struct operand
*opx
;
1192 const uint8_t *codes
= temp
->code
;
1197 op1
= (c
& 3) + ((opex
& 1) << 2);
1198 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1199 opx
= &ins
->oprs
[op1
];
1200 opex
= 0; /* For the next iteration */
1208 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1221 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1222 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1227 /* The test for BITS8 and SBYTE here is intended to avoid
1228 warning on optimizer actions due to SBYTE, while still
1229 warn on explicit BYTE directives. Also warn, obviously,
1230 if the optimizer isn't enabled. */
1231 if (((opx
->type
& BITS8
) ||
1232 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1233 (opx
->offset
< -128 || opx
->offset
> 127)) {
1234 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1235 "signed byte value exceeds bounds");
1237 if (opx
->segment
!= NO_SEG
) {
1239 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1240 opx
->segment
, opx
->wrt
);
1242 bytes
[0] = opx
->offset
;
1243 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1250 if (opx
->offset
< -256 || opx
->offset
> 255) {
1251 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1252 "byte value exceeds bounds");
1254 if (opx
->segment
!= NO_SEG
) {
1256 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1257 opx
->segment
, opx
->wrt
);
1259 bytes
[0] = opx
->offset
;
1260 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1267 if (opx
->offset
< 0 || opx
->offset
> 255)
1268 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1269 "unsigned byte value exceeds bounds");
1270 if (opx
->segment
!= NO_SEG
) {
1272 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1273 opx
->segment
, opx
->wrt
);
1275 bytes
[0] = opx
->offset
;
1276 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1283 warn_overflow(2, opx
);
1285 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1286 opx
->segment
, opx
->wrt
);
1291 if (opx
->type
& (BITS16
| BITS32
))
1292 size
= (opx
->type
& BITS16
) ? 2 : 4;
1294 size
= (bits
== 16) ? 2 : 4;
1295 warn_overflow(size
, opx
);
1297 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1298 opx
->segment
, opx
->wrt
);
1303 warn_overflow(4, opx
);
1305 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1306 opx
->segment
, opx
->wrt
);
1312 size
= ins
->addr_size
>> 3;
1313 warn_overflow(size
, opx
);
1314 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1315 opx
->segment
, opx
->wrt
);
1320 if (opx
->segment
!= segment
)
1321 errfunc(ERR_NONFATAL
,
1322 "short relative jump outside segment");
1323 data
= opx
->offset
- insn_end
;
1324 if (data
> 127 || data
< -128)
1325 errfunc(ERR_NONFATAL
, "short jump is out of range");
1327 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1332 data
= (int64_t)opx
->offset
;
1333 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1334 opx
->segment
, opx
->wrt
);
1339 if (opx
->segment
!= segment
) {
1341 out(offset
, segment
, &data
,
1342 OUT_REL2ADR
, insn_end
- offset
,
1343 opx
->segment
, opx
->wrt
);
1345 data
= opx
->offset
- insn_end
;
1346 out(offset
, segment
, &data
,
1347 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1353 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1354 size
= (opx
->type
& BITS16
) ? 2 : 4;
1356 size
= (bits
== 16) ? 2 : 4;
1357 if (opx
->segment
!= segment
) {
1359 out(offset
, segment
, &data
,
1360 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1361 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1363 data
= opx
->offset
- insn_end
;
1364 out(offset
, segment
, &data
,
1365 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1371 if (opx
->segment
!= segment
) {
1373 out(offset
, segment
, &data
,
1374 OUT_REL4ADR
, insn_end
- offset
,
1375 opx
->segment
, opx
->wrt
);
1377 data
= opx
->offset
- insn_end
;
1378 out(offset
, segment
, &data
,
1379 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1385 if (opx
->segment
== NO_SEG
)
1386 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1389 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1390 outfmt
->segbase(1 + opx
->segment
),
1397 warn_overflow(2, opx
);
1398 if (is_sbyte16(opx
)) {
1400 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1404 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1405 opx
->segment
, opx
->wrt
);
1412 bytes
[0] = *codes
++;
1413 if (is_sbyte16(opx
))
1414 bytes
[0] |= 2; /* s-bit */
1415 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1421 warn_overflow(4, opx
);
1422 if (is_sbyte32(opx
)) {
1424 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1428 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1429 opx
->segment
, opx
->wrt
);
1436 bytes
[0] = *codes
++;
1437 if (is_sbyte32(opx
))
1438 bytes
[0] |= 2; /* s-bit */
1439 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1449 (ins
->drexdst
<< 4) |
1450 (ins
->rex
& REX_OC
? 0x08 : 0) |
1451 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1453 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1459 opx
= &ins
->oprs
[c
>> 3];
1460 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1461 opx
= &ins
->oprs
[c
& 7];
1462 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1463 errfunc(ERR_NONFATAL
,
1464 "non-absolute expression not permitted as argument %d",
1467 if (opx
->offset
& ~15) {
1468 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1469 "four-bit argument exceeds bounds");
1471 bytes
[0] |= opx
->offset
& 15;
1473 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1479 opx
= &ins
->oprs
[c
>> 4];
1480 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1482 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1488 opx
= &ins
->oprs
[c
];
1489 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1490 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1496 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1497 (int32_t)data
!= (int64_t)data
) {
1498 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1499 "signed dword immediate exceeds bounds");
1501 if (is_sbyte32(opx
)) {
1503 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1507 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1508 opx
->segment
, opx
->wrt
);
1515 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1516 (int32_t)data
!= (int64_t)data
) {
1517 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1518 "signed dword immediate exceeds bounds");
1520 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1521 opx
->segment
, opx
->wrt
);
1528 if (ins
->vex_m
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1530 bytes
[1] = ins
->vex_m
| ((~ins
->rex
& 7) << 5);
1531 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1532 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1533 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1537 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1538 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1539 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1549 if (ins
->rex
& REX_W
)
1551 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1553 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1558 um
= (uint64_t)2 << (s
-1);
1561 if (uv
> 127 && uv
< (uint64_t)-128 &&
1562 (uv
< um
-128 || uv
> um
-1)) {
1563 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1564 "signed byte value exceeds bounds");
1566 if (opx
->segment
!= NO_SEG
) {
1568 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1569 opx
->segment
, opx
->wrt
);
1572 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1583 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1585 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1592 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1594 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1613 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1622 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1637 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1638 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1647 *bytes
= c
- 0332 + 0xF2;
1648 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1653 if (ins
->rex
& REX_R
) {
1655 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1658 ins
->rex
&= ~(REX_L
|REX_R
);
1669 if (ins
->oprs
[0].segment
!= NO_SEG
)
1670 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1672 int64_t size
= ins
->oprs
[0].offset
;
1674 out(offset
, segment
, NULL
,
1675 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1683 switch (ins
->oprs
[0].basereg
) {
1698 "bizarre 8086 segment register received");
1700 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1707 switch (ins
->oprs
[0].basereg
) {
1716 "bizarre 386 segment register received");
1718 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1727 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1733 bytes
[0] = c
- 0362 + 0xf2;
1734 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1744 *bytes
= c
- 0366 + 0x66;
1745 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1755 *bytes
= bits
== 16 ? 3 : 5;
1756 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1779 struct operand
*opy
= &ins
->oprs
[op2
];
1782 /* pick rfield from operand b (opx) */
1783 rflags
= regflag(opx
);
1784 rfield
= nasm_regvals
[opx
->basereg
];
1786 /* rfield is constant */
1791 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1793 errfunc(ERR_NONFATAL
, "invalid effective address");
1798 *p
++ = ea_data
.modrm
;
1799 if (ea_data
.sib_present
)
1802 /* DREX suffixes come between the SIB and the displacement */
1803 if (ins
->rex
& REX_D
) {
1804 *p
++ = (ins
->drexdst
<< 4) |
1805 (ins
->rex
& REX_OC
? 0x08 : 0) |
1806 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1811 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1814 * Make sure the address gets the right offset in case
1815 * the line breaks in the .lst file (BR 1197827)
1820 switch (ea_data
.bytes
) {
1828 warn_overflow(ea_data
.bytes
, opy
);
1831 if (opy
->segment
== segment
) {
1833 out(offset
, segment
, &data
, OUT_ADDRESS
,
1834 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1836 out(offset
, segment
, &data
, OUT_REL4ADR
,
1837 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1841 out(offset
, segment
, &data
, OUT_ADDRESS
,
1842 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1848 "Invalid amount of bytes (%d) for offset?!",
1857 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1858 ": instruction code 0x%02X given", c
);
1864 static int32_t regflag(const operand
* o
)
1866 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1867 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1869 return nasm_reg_flags
[o
->basereg
];
1872 static int32_t regval(const operand
* o
)
1874 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1875 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1877 return nasm_regvals
[o
->basereg
];
1880 static int op_rexflags(const operand
* o
, int mask
)
1885 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1886 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1889 flags
= nasm_reg_flags
[o
->basereg
];
1890 val
= nasm_regvals
[o
->basereg
];
1892 return rexflags(val
, flags
, mask
);
1895 static int rexflags(int val
, int32_t flags
, int mask
)
1900 rex
|= REX_B
|REX_X
|REX_R
;
1903 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1905 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1911 static int matches(const struct itemplate
*itemp
, insn
* instruction
, int bits
)
1913 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1920 if (itemp
->opcode
!= instruction
->opcode
)
1924 * Count the operands
1926 if (itemp
->operands
!= instruction
->operands
)
1930 * Check that no spurious colons or TOs are present
1932 for (i
= 0; i
< itemp
->operands
; i
++)
1933 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
1937 * Process size flags
1939 if (itemp
->flags
& IF_ARMASK
) {
1940 memset(size
, 0, sizeof size
);
1942 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
1944 switch (itemp
->flags
& IF_SMASK
) {
1981 switch (itemp
->flags
& IF_SMASK
) {
2016 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2021 * Check that the operand flags all match up
2023 for (i
= 0; i
< itemp
->operands
; i
++) {
2024 int32_t type
= instruction
->oprs
[i
].type
;
2025 if (!(type
& SIZE_MASK
))
2028 if (itemp
->opd
[i
] & SAME_AS
) {
2029 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2030 if (type
!= instruction
->oprs
[j
].type
||
2031 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2033 } else if (itemp
->opd
[i
] & ~type
||
2034 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2035 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2036 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2045 * Check operand sizes
2047 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2048 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2050 for (i
= 0; i
< oprs
; i
++) {
2051 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2053 for (j
= 0; j
< oprs
; j
++)
2059 oprs
= itemp
->operands
;
2062 for (i
= 0; i
< itemp
->operands
; i
++) {
2063 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2064 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2069 * Check template is okay at the set cpu level
2071 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2075 * Verify the appropriate long mode flag.
2077 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2081 * Check if special handling needed for Jumps
2083 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2089 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2090 int addrbits
, int rfield
, int32_t rflags
)
2092 bool forw_ref
= !!(input
->opflags
& OPFLAG_FORWARD
);
2094 output
->rip
= false;
2096 /* REX flags for the rfield operand */
2097 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2099 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2103 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2104 || input
->basereg
>= REG_ENUM_LIMIT
)
2107 i
= nasm_regvals
[input
->basereg
];
2110 return NULL
; /* Invalid EA register */
2112 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2114 output
->sib_present
= false; /* no SIB necessary */
2115 output
->bytes
= 0; /* no offset necessary either */
2116 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2117 } else { /* it's a memory reference */
2118 if (input
->basereg
== -1
2119 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2120 /* it's a pure offset */
2121 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2122 int scale
, index
, base
;
2123 output
->sib_present
= true;
2127 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2129 output
->modrm
= 4 | ((rfield
& 7) << 3);
2130 output
->rip
= false;
2132 output
->sib_present
= false;
2133 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2134 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2135 output
->rip
= bits
== 64;
2137 } else { /* it's an indirection */
2138 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2139 int32_t o
= input
->offset
, seg
= input
->segment
;
2140 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2143 int32_t ix
, bx
; /* register flags */
2146 i
= -1; /* make this easy, at least */
2148 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2149 it
= nasm_regvals
[i
];
2150 ix
= nasm_reg_flags
[i
];
2156 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2157 bt
= nasm_regvals
[b
];
2158 bx
= nasm_reg_flags
[b
];
2164 /* check for a 32/64-bit memory reference... */
2165 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2166 /* it must be a 32/64-bit memory reference. Firstly we have
2167 * to check that all registers involved are type E/Rxx. */
2168 int32_t sok
= BITS32
|BITS64
;
2171 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2179 return NULL
; /* Invalid register */
2180 if (~sok
& bx
& SIZE_MASK
)
2181 return NULL
; /* Invalid size */
2185 /* While we're here, ensure the user didn't specify
2187 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2190 if (addrbits
== 16 ||
2191 (addrbits
== 32 && !(sok
& BITS32
)) ||
2192 (addrbits
== 64 && !(sok
& BITS64
)))
2195 /* now reorganize base/index */
2196 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2197 ((hb
== b
&& ht
== EAH_NOTBASE
)
2198 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2199 /* swap if hints say so */
2200 t
= bt
, bt
= it
, it
= t
;
2201 t
= bx
, bx
= ix
, ix
= t
;
2203 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2204 bt
= -1, bx
= 0, s
++;
2205 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2206 /* make single reg base, unless hint */
2207 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2209 if (((s
== 2 && it
!= REG_NUM_ESP
2210 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2211 || s
== 5 || s
== 9) && bt
== -1)
2212 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2213 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2214 && (input
->eaflags
& EAF_TIMESTWO
))
2215 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2216 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2217 if (s
== 1 && it
== REG_NUM_ESP
) {
2218 /* swap ESP into base if scale is 1 */
2219 t
= it
, it
= bt
, bt
= t
;
2220 t
= ix
, ix
= bx
, bx
= t
;
2222 if (it
== REG_NUM_ESP
2223 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2224 return NULL
; /* wrong, for various reasons */
2226 output
->rex
|= rexflags(it
, ix
, REX_X
);
2227 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2229 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2238 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2239 seg
== NO_SEG
&& !forw_ref
&&
2241 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2243 else if (input
->eaflags
& EAF_BYTEOFFS
||
2244 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2246 && !(input
->eaflags
& EAF_WORDOFFS
)))
2252 output
->sib_present
= false;
2253 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2254 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2257 int mod
, scale
, index
, base
;
2277 default: /* then what the smeg is it? */
2278 return NULL
; /* panic */
2286 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2287 seg
== NO_SEG
&& !forw_ref
&&
2289 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2291 else if (input
->eaflags
& EAF_BYTEOFFS
||
2292 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2294 && !(input
->eaflags
& EAF_WORDOFFS
)))
2300 output
->sib_present
= true;
2301 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2302 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2303 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2305 } else { /* it's 16-bit */
2308 /* check for 64-bit long mode */
2312 /* check all registers are BX, BP, SI or DI */
2313 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2314 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2315 && i
!= R_SI
&& i
!= R_DI
))
2318 /* ensure the user didn't specify DWORD/QWORD */
2319 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2322 if (s
!= 1 && i
!= -1)
2323 return NULL
; /* no can do, in 16-bit EA */
2324 if (b
== -1 && i
!= -1) {
2329 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2334 /* have BX/BP as base, SI/DI index */
2336 return NULL
; /* shouldn't ever happen, in theory */
2337 if (i
!= -1 && b
!= -1 &&
2338 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2339 return NULL
; /* invalid combinations */
2340 if (b
== -1) /* pure offset: handled above */
2341 return NULL
; /* so if it gets to here, panic! */
2345 switch (i
* 256 + b
) {
2346 case R_SI
* 256 + R_BX
:
2349 case R_DI
* 256 + R_BX
:
2352 case R_SI
* 256 + R_BP
:
2355 case R_DI
* 256 + R_BP
:
2373 if (rm
== -1) /* can't happen, in theory */
2374 return NULL
; /* so panic if it does */
2376 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2377 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2379 else if (input
->eaflags
& EAF_BYTEOFFS
||
2380 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2382 && !(input
->eaflags
& EAF_WORDOFFS
)))
2387 output
->sib_present
= false; /* no SIB - it's 16-bit */
2388 output
->bytes
= mod
; /* bytes of offset needed */
2389 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2394 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2398 static void add_asp(insn
*ins
, int addrbits
)
2403 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2405 switch (ins
->prefixes
[PPS_ASIZE
]) {
2416 valid
&= (addrbits
== 32) ? 16 : 32;
2422 for (j
= 0; j
< ins
->operands
; j
++) {
2423 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2426 /* Verify as Register */
2427 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2428 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2431 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2433 /* Verify as Register */
2434 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2435 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2438 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2440 if (ins
->oprs
[j
].scale
== 0)
2444 int ds
= ins
->oprs
[j
].disp_size
;
2445 if ((addrbits
!= 64 && ds
> 8) ||
2446 (addrbits
== 64 && ds
== 16))
2466 if (valid
& addrbits
) {
2467 ins
->addr_size
= addrbits
;
2468 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2469 /* Add an address size prefix */
2470 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2471 ins
->prefixes
[PPS_ASIZE
] = pref
;
2472 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2475 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2476 ins
->addr_size
= addrbits
; /* Error recovery */
2479 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2481 for (j
= 0; j
< ins
->operands
; j
++) {
2482 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2483 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2484 != ins
->addr_size
) {
2485 /* mem_offs sizes must match the address size; if not,
2486 strip the MEM_OFFS bit and match only EA instructions */
2487 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);