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, \2, \3 - that many literal bytes follow in the code stream
11 * \10..\13 - a literal byte follows in the code stream, to be added
12 * to the register value of operand 0..3
13 * \14..\17 - a signed byte immediate operand, from operand 0..3
14 * \20..\23 - a byte immediate operand, from operand 0..3
15 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
16 * \30..\33 - a word immediate operand, from operand 0..3
17 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
18 * assembly mode or the operand-size override on the operand
19 * \40..\43 - a long immediate operand, from operand 0..3
20 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
21 * depending on the address size of the instruction.
22 * \50..\53 - a byte relative operand, from operand 0..3
23 * \54..\57 - a qword immediate operand, from operand 0..3
24 * \60..\63 - a word relative operand, from operand 0..3
25 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
26 * assembly mode or the operand-size override on the operand
27 * \70..\73 - a long relative operand, from operand 0..3
28 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
29 * \1ab - a ModRM, calculated on EA in operand a, with the spare
30 * field the register value of operand b.
31 * \140..\143 - an immediate word or signed byte for operand 0..3
32 * \144..\147 - or 2 (s-field) into opcode byte if operand 0..3
33 * is a signed byte rather than a word. Opcode byte follows.
34 * \150..\153 - an immediate dword or signed byte for operand 0..3
35 * \154..\157 - or 2 (s-field) into opcode byte if operand 0..3
36 * is a signed byte rather than a dword. Opcode byte follows.
37 * \160..\163 - this instruction uses DREX rather than REX, with the
38 * OC0 field set to 0, and the dest field taken from
40 * \164..\167 - this instruction uses DREX rather than REX, with the
41 * OC0 field set to 1, and the dest field taken from
43 * \171 - placement of DREX suffix in the absence of an EA
44 * \172\ab - the register number from operand a in bits 7..4, with
45 * the 4-bit immediate from operand b in bits 3..0.
46 * \173\xab - the register number from operand a in bits 7..4, with
47 * the value b in bits 3..0.
48 * \174\a - the register number from operand a in bits 7..4, and
49 * an arbitrary value in bits 3..0 (assembled as zero.)
50 * \2ab - a ModRM, calculated on EA in operand a, with the spare
51 * field equal to digit b.
52 * \250..\253 - same as \150..\153, except warn if the 64-bit operand
53 * is not equal to the truncated and sign-extended 32-bit
54 * operand; used for 32-bit immediates in 64-bit mode.
55 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
56 * \260..\263 - this instruction uses VEX rather than REX, with the
57 * V field taken from operand 0..3.
58 * \270 - this instruction uses VEX rather than REX, with the
59 * V field set to 1111b.
61 * VEX prefixes are followed by the sequence:
62 * \mm\wlp where mm is the M field; and wlp is:
64 * [w0] ww = 0 for W = 0
65 * [w1] ww = 1 for W = 1
66 * [wx] ww = 2 for W don't care (always assembled as 0)
67 * [ww] ww = 3 for W used as REX.W
70 * \274..\277 - a signed byte immediate operand, from operand 0..3,
71 * which is to be extended to the operand size.
72 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
73 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
74 * \312 - (disassembler only) marker on LOOP, LOOPxx instructions.
75 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
76 * \314 - (disassembler only) invalid with REX.B
77 * \315 - (disassembler only) invalid with REX.X
78 * \316 - (disassembler only) invalid with REX.R
79 * \317 - (disassembler only) invalid with REX.W
80 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
81 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
82 * \322 - indicates that this instruction is only valid when the
83 * operand size is the default (instruction to disassembler,
84 * generates no code in the assembler)
85 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
86 * \324 - indicates 64-bit operand size requiring REX prefix.
87 * \330 - a literal byte follows in the code stream, to be added
88 * to the condition code value of the instruction.
89 * \331 - instruction not valid with REP prefix. Hint for
90 * disassembler only; for SSE instructions.
91 * \332 - REP prefix (0xF2 byte) used as opcode extension.
92 * \333 - REP prefix (0xF3 byte) used as opcode extension.
93 * \334 - LOCK prefix used instead of REX.R
94 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
95 * \336 - force a REP(E) prefix (0xF2) even if not specified.
96 * \337 - force a REPNE prefix (0xF3) even if not specified.
97 * \336-\337 are still listed as prefixes in the disassembler.
98 * \340 - reserve <operand 0> bytes of uninitialized storage.
99 * Operand 0 had better be a segmentless constant.
100 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
101 * (POP is never used for CS) depending on operand 0
102 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
104 * \360 - no SSE prefix (== \364\331)
105 * \361 - 66 SSE prefix (== \366\331)
106 * \362 - F2 SSE prefix (== \364\332)
107 * \363 - F3 SSE prefix (== \364\333)
108 * \364 - operand-size prefix (0x66) not permitted
109 * \365 - address-size prefix (0x67) not permitted
110 * \366 - operand-size prefix (0x66) used as opcode extension
111 * \367 - address-size prefix (0x67) used as opcode extension
112 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
113 * 370 is used for Jcc, 371 is used for JMP.
114 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
115 * used for conditional jump over longer jump
118 #include "compiler.h"
122 #include <inttypes.h>
126 #include "assemble.h"
130 /* Initialized to zero by the C standard */
131 static const uint8_t const_zero_buf
[256];
134 int sib_present
; /* is a SIB byte necessary? */
135 int bytes
; /* # of bytes of offset needed */
136 int size
; /* lazy - this is sib+bytes+1 */
137 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
140 static uint32_t cpu
; /* cpu level received from nasm.c */
141 static efunc errfunc
;
142 static struct ofmt
*outfmt
;
143 static ListGen
*list
;
145 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
146 static void gencode(int32_t segment
, int64_t offset
, int bits
,
147 insn
* ins
, const struct itemplate
*temp
,
149 static int matches(const struct itemplate
*, insn
*, int bits
);
150 static int32_t regflag(const operand
*);
151 static int32_t regval(const operand
*);
152 static int rexflags(int, int32_t, int);
153 static int op_rexflags(const operand
*, int);
154 static ea
*process_ea(operand
*, ea
*, int, int, int, int32_t);
155 static void add_asp(insn
*, int);
157 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
159 return ins
->prefixes
[pos
] == prefix
;
162 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
164 if (ins
->prefixes
[pos
])
165 errfunc(ERR_NONFATAL
, "invalid %s prefix",
166 prefix_name(ins
->prefixes
[pos
]));
169 static const char *size_name(int size
)
191 static void warn_overflow(int size
, const struct operand
*o
)
193 if (size
< 8 && o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
194 int64_t lim
= ((int64_t)1 << (size
*8))-1;
195 int64_t data
= o
->offset
;
197 if (data
< ~lim
|| data
> lim
)
198 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
199 "%s data exceeds bounds", size_name(size
));
203 * This routine wrappers the real output format's output routine,
204 * in order to pass a copy of the data off to the listing file
205 * generator at the same time.
207 static void out(int64_t offset
, int32_t segto
, const void *data
,
208 enum out_type type
, uint64_t size
,
209 int32_t segment
, int32_t wrt
)
211 static int32_t lineno
= 0; /* static!!! */
212 static char *lnfname
= NULL
;
215 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
217 * This is a non-relocated address, and we're going to
218 * convert it into RAWDATA format.
223 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
227 WRITEADDR(q
, *(int64_t *)data
, size
);
232 list
->output(offset
, data
, type
, size
);
235 * this call to src_get determines when we call the
236 * debug-format-specific "linenum" function
237 * it updates lineno and lnfname to the current values
238 * returning 0 if "same as last time", -2 if lnfname
239 * changed, and the amount by which lineno changed,
240 * if it did. thus, these variables must be static
243 if (src_get(&lineno
, &lnfname
)) {
244 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
247 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
250 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
251 insn
* ins
, const uint8_t *code
)
256 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
260 if (optimizing
< 0 && c
== 0371)
263 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
264 if (ins
->oprs
[0].segment
!= segment
)
267 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
268 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
271 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
272 insn
* instruction
, struct ofmt
*output
, efunc error
,
275 const struct itemplate
*temp
;
280 int64_t start
= offset
;
281 int64_t wsize
= 0; /* size for DB etc. */
283 errfunc
= error
; /* to pass to other functions */
285 outfmt
= output
; /* likewise */
286 list
= listgen
; /* and again */
288 switch (instruction
->opcode
) {
318 int32_t t
= instruction
->times
;
321 "instruction->times < 0 (%ld) in assemble()", t
);
323 while (t
--) { /* repeat TIMES times */
324 for (e
= instruction
->eops
; e
; e
= e
->next
) {
325 if (e
->type
== EOT_DB_NUMBER
) {
327 if (e
->segment
!= NO_SEG
)
328 errfunc(ERR_NONFATAL
,
329 "one-byte relocation attempted");
331 uint8_t out_byte
= e
->offset
;
332 out(offset
, segment
, &out_byte
,
333 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
335 } else if (wsize
> 8) {
336 errfunc(ERR_NONFATAL
,
337 "integer supplied to a DT, DO or DY"
340 out(offset
, segment
, &e
->offset
,
341 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
343 } else if (e
->type
== EOT_DB_STRING
||
344 e
->type
== EOT_DB_STRING_FREE
) {
347 out(offset
, segment
, e
->stringval
,
348 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
349 align
= e
->stringlen
% wsize
;
352 align
= wsize
- align
;
353 out(offset
, segment
, const_zero_buf
,
354 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
356 offset
+= e
->stringlen
+ align
;
359 if (t
> 0 && t
== instruction
->times
- 1) {
361 * Dummy call to list->output to give the offset to the
364 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
365 list
->uplevel(LIST_TIMES
);
368 if (instruction
->times
> 1)
369 list
->downlevel(LIST_TIMES
);
370 return offset
- start
;
373 if (instruction
->opcode
== I_INCBIN
) {
374 const char *fname
= instruction
->eops
->stringval
;
377 fp
= fopen(fname
, "rb");
379 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
381 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
382 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
385 static char buf
[4096];
386 size_t t
= instruction
->times
;
391 if (instruction
->eops
->next
) {
392 base
= instruction
->eops
->next
->offset
;
394 if (instruction
->eops
->next
->next
&&
395 len
> (size_t)instruction
->eops
->next
->next
->offset
)
396 len
= (size_t)instruction
->eops
->next
->next
->offset
;
399 * Dummy call to list->output to give the offset to the
402 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
403 list
->uplevel(LIST_INCBIN
);
407 fseek(fp
, base
, SEEK_SET
);
411 fread(buf
, 1, (l
> (int32_t) sizeof(buf
) ? (int32_t) sizeof(buf
) : l
),
415 * This shouldn't happen unless the file
416 * actually changes while we are reading
420 "`incbin': unexpected EOF while"
421 " reading file `%s'", fname
);
422 t
= 0; /* Try to exit cleanly */
425 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
430 list
->downlevel(LIST_INCBIN
);
431 if (instruction
->times
> 1) {
433 * Dummy call to list->output to give the offset to the
436 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
437 list
->uplevel(LIST_TIMES
);
438 list
->downlevel(LIST_TIMES
);
441 return instruction
->times
* len
;
443 return 0; /* if we're here, there's an error */
446 /* Check to see if we need an address-size prefix */
447 add_asp(instruction
, bits
);
451 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++){
452 int m
= matches(temp
, instruction
, bits
);
454 (m
== 99 && jmp_match(segment
, offset
, bits
,
455 instruction
, temp
->code
))) {
457 int64_t insn_size
= calcsize(segment
, offset
, bits
,
458 instruction
, temp
->code
);
459 itimes
= instruction
->times
;
460 if (insn_size
< 0) /* shouldn't be, on pass two */
461 error(ERR_PANIC
, "errors made it through from pass one");
464 for (j
= 0; j
< MAXPREFIX
; j
++) {
466 switch (instruction
->prefixes
[j
]) {
481 error(ERR_WARNING
| ERR_PASS2
,
482 "cs segment base generated, but will be ignored in 64-bit mode");
488 error(ERR_WARNING
| ERR_PASS2
,
489 "ds segment base generated, but will be ignored in 64-bit mode");
495 error(ERR_WARNING
| ERR_PASS2
,
496 "es segment base generated, but will be ignored in 64-bit mode");
508 error(ERR_WARNING
| ERR_PASS2
,
509 "ss segment base generated, but will be ignored in 64-bit mode");
516 "segr6 and segr7 cannot be used as prefixes");
521 "16-bit addressing is not supported "
523 } else if (bits
!= 16)
533 "64-bit addressing is only supported "
557 error(ERR_PANIC
, "invalid instruction prefix");
560 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
565 insn_end
= offset
+ insn_size
;
566 gencode(segment
, offset
, bits
, instruction
,
569 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
571 * Dummy call to list->output to give the offset to the
574 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
575 list
->uplevel(LIST_TIMES
);
578 if (instruction
->times
> 1)
579 list
->downlevel(LIST_TIMES
);
580 return offset
- start
;
581 } else if (m
> 0 && m
> size_prob
) {
586 if (temp
->opcode
== -1) { /* didn't match any instruction */
589 error(ERR_NONFATAL
, "operation size not specified");
592 error(ERR_NONFATAL
, "mismatch in operand sizes");
595 error(ERR_NONFATAL
, "no instruction for this cpu level");
598 error(ERR_NONFATAL
, "instruction not supported in 64-bit mode");
602 "invalid combination of opcode and operands");
609 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
610 insn
* instruction
, efunc error
)
612 const struct itemplate
*temp
;
614 errfunc
= error
; /* to pass to other functions */
617 if (instruction
->opcode
== -1)
620 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
621 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
622 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
623 instruction
->opcode
== I_DY
) {
625 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
628 switch (instruction
->opcode
) {
654 for (e
= instruction
->eops
; e
; e
= e
->next
) {
658 if (e
->type
== EOT_DB_NUMBER
)
660 else if (e
->type
== EOT_DB_STRING
||
661 e
->type
== EOT_DB_STRING_FREE
)
662 osize
= e
->stringlen
;
664 align
= (-osize
) % wsize
;
667 isize
+= osize
+ align
;
669 return isize
* instruction
->times
;
672 if (instruction
->opcode
== I_INCBIN
) {
673 const char *fname
= instruction
->eops
->stringval
;
677 fp
= fopen(fname
, "rb");
679 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
681 else if (fseek(fp
, 0L, SEEK_END
) < 0)
682 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
687 if (instruction
->eops
->next
) {
688 len
-= instruction
->eops
->next
->offset
;
689 if (instruction
->eops
->next
->next
&&
690 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
691 len
= (size_t)instruction
->eops
->next
->next
->offset
;
694 return instruction
->times
* len
;
696 return 0; /* if we're here, there's an error */
699 /* Check to see if we need an address-size prefix */
700 add_asp(instruction
, bits
);
702 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++) {
703 int m
= matches(temp
, instruction
, bits
);
705 (m
== 99 && jmp_match(segment
, offset
, bits
,
706 instruction
, temp
->code
))) {
707 /* we've matched an instruction. */
709 const uint8_t *codes
= temp
->code
;
712 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
715 for (j
= 0; j
< MAXPREFIX
; j
++) {
716 switch (instruction
->prefixes
[j
]) {
742 return isize
* instruction
->times
;
745 return -1; /* didn't match any instruction */
748 static bool possible_sbyte(operand
*o
)
750 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
751 !(o
->opflags
& OPFLAG_FORWARD
) &&
752 optimizing
>= 0 && !(o
->type
& STRICT
);
755 /* check that opn[op] is a signed byte of size 16 or 32 */
756 static bool is_sbyte16(operand
*o
)
760 if (!possible_sbyte(o
))
764 return v
>= -128 && v
<= 127;
767 static bool is_sbyte32(operand
*o
)
771 if (!possible_sbyte(o
))
775 return v
>= -128 && v
<= 127;
778 /* Common construct */
779 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
781 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
782 insn
* ins
, const uint8_t *codes
)
789 ins
->rex
= 0; /* Ensure REX is reset */
791 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
794 (void)segment
; /* Don't warn that this parameter is unused */
795 (void)offset
; /* Don't warn that this parameter is unused */
799 opx
= &ins
->oprs
[c
& 3];
804 codes
+= c
, length
+= c
;
809 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
824 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
825 length
+= (opx
->type
& BITS16
) ? 2 : 4;
827 length
+= (bits
== 16) ? 2 : 4;
835 length
+= ins
->addr_size
>> 3;
843 length
+= 8; /* MOV reg64/imm */
851 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
852 length
+= (opx
->type
& BITS16
) ? 2 : 4;
854 length
+= (bits
== 16) ? 2 : 4;
866 length
+= is_sbyte16(opx
) ? 1 : 2;
875 length
+= is_sbyte32(opx
) ? 1 : 4;
886 ins
->drexdst
= regval(opx
);
891 ins
->rex
|= REX_D
|REX_OC
;
892 ins
->drexdst
= regval(opx
);
906 length
+= is_sbyte32(opx
) ? 1 : 4;
915 ins
->drexdst
= regval(opx
);
916 ins
->vex_m
= *codes
++;
917 ins
->vex_wlp
= *codes
++;
923 ins
->vex_m
= *codes
++;
924 ins
->vex_wlp
= *codes
++;
937 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
941 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
948 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
949 has_prefix(ins
, PPS_ASIZE
, P_A32
))
957 length
+= (bits
!= 16);
961 length
+= (bits
== 16);
995 if (!ins
->prefixes
[PPS_LREP
])
996 ins
->prefixes
[PPS_LREP
] = P_REP
;
1000 if (!ins
->prefixes
[PPS_LREP
])
1001 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1005 if (ins
->oprs
[0].segment
!= NO_SEG
)
1006 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1007 " quantity of BSS space");
1009 length
+= ins
->oprs
[0].offset
;
1059 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1062 /* pick rfield from operand b */
1063 rflags
= regflag(&ins
->oprs
[c
& 7]);
1064 rfield
= nasm_regvals
[ins
->oprs
[c
& 7].basereg
];
1071 (&ins
->oprs
[(c
>> 3) & 7], &ea_data
, bits
,
1072 ins
->addr_size
, rfield
, rflags
)) {
1073 errfunc(ERR_NONFATAL
, "invalid effective address");
1076 ins
->rex
|= ea_data
.rex
;
1077 length
+= ea_data
.size
;
1083 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1084 ": instruction code 0x%02X given", c
);
1089 ins
->rex
&= rex_mask
;
1091 if (ins
->rex
& REX_V
) {
1092 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1094 if (ins
->rex
& REX_H
) {
1095 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1098 switch (ins
->vex_wlp
& 030) {
1112 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1113 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1116 if (ins
->vex_m
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1120 } else if (ins
->rex
& REX_D
) {
1121 if (ins
->rex
& REX_H
) {
1122 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1125 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1126 ins
->drexdst
> 7)) {
1127 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1131 } else if (ins
->rex
& REX_REAL
) {
1132 if (ins
->rex
& REX_H
) {
1133 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1135 } else if (bits
== 64) {
1137 } else if ((ins
->rex
& REX_L
) &&
1138 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1141 assert_no_prefix(ins
, PPS_LREP
);
1144 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1152 #define EMIT_REX() \
1153 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1154 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1155 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1160 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1161 insn
* ins
, const struct itemplate
*temp
,
1164 static char condval
[] = { /* conditional opcodes */
1165 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1166 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1167 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1173 struct operand
*opx
;
1174 const uint8_t *codes
= temp
->code
;
1178 opx
= &ins
->oprs
[c
& 3];
1184 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1191 bytes
[0] = *codes
++ + ((regval(opx
)) & 7);
1192 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1197 /* The test for BITS8 and SBYTE here is intended to avoid
1198 warning on optimizer actions due to SBYTE, while still
1199 warn on explicit BYTE directives. Also warn, obviously,
1200 if the optimizer isn't enabled. */
1201 if (((opx
->type
& BITS8
) ||
1202 !(opx
->type
& temp
->opd
[c
& 3] & BYTENESS
)) &&
1203 (opx
->offset
< -128 || opx
->offset
> 127)) {
1204 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1205 "signed byte value exceeds bounds");
1207 if (opx
->segment
!= NO_SEG
) {
1209 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1210 opx
->segment
, opx
->wrt
);
1212 bytes
[0] = opx
->offset
;
1213 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1220 if (opx
->offset
< -256 || opx
->offset
> 255) {
1221 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1222 "byte value exceeds bounds");
1224 if (opx
->segment
!= NO_SEG
) {
1226 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1227 opx
->segment
, opx
->wrt
);
1229 bytes
[0] = opx
->offset
;
1230 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1237 if (opx
->offset
< 0 || opx
->offset
> 255)
1238 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1239 "unsigned byte value exceeds bounds");
1240 if (opx
->segment
!= NO_SEG
) {
1242 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1243 opx
->segment
, opx
->wrt
);
1245 bytes
[0] = opx
->offset
;
1246 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1253 warn_overflow(2, opx
);
1255 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1256 opx
->segment
, opx
->wrt
);
1261 if (opx
->type
& (BITS16
| BITS32
))
1262 size
= (opx
->type
& BITS16
) ? 2 : 4;
1264 size
= (bits
== 16) ? 2 : 4;
1265 warn_overflow(size
, opx
);
1267 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1268 opx
->segment
, opx
->wrt
);
1273 warn_overflow(4, opx
);
1275 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1276 opx
->segment
, opx
->wrt
);
1282 size
= ins
->addr_size
>> 3;
1283 warn_overflow(size
, opx
);
1284 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1285 opx
->segment
, opx
->wrt
);
1290 if (opx
->segment
!= segment
)
1291 errfunc(ERR_NONFATAL
,
1292 "short relative jump outside segment");
1293 data
= opx
->offset
- insn_end
;
1294 if (data
> 127 || data
< -128)
1295 errfunc(ERR_NONFATAL
, "short jump is out of range");
1297 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1302 data
= (int64_t)opx
->offset
;
1303 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1304 opx
->segment
, opx
->wrt
);
1309 if (opx
->segment
!= segment
) {
1311 out(offset
, segment
, &data
,
1312 OUT_REL2ADR
, insn_end
- offset
,
1313 opx
->segment
, opx
->wrt
);
1315 data
= opx
->offset
- insn_end
;
1316 out(offset
, segment
, &data
,
1317 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1323 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1324 size
= (opx
->type
& BITS16
) ? 2 : 4;
1326 size
= (bits
== 16) ? 2 : 4;
1327 if (opx
->segment
!= segment
) {
1329 out(offset
, segment
, &data
,
1330 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1331 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1333 data
= opx
->offset
- insn_end
;
1334 out(offset
, segment
, &data
,
1335 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1341 if (opx
->segment
!= segment
) {
1343 out(offset
, segment
, &data
,
1344 OUT_REL4ADR
, insn_end
- offset
,
1345 opx
->segment
, opx
->wrt
);
1347 data
= opx
->offset
- insn_end
;
1348 out(offset
, segment
, &data
,
1349 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1355 if (opx
->segment
== NO_SEG
)
1356 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1359 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1360 outfmt
->segbase(1 + opx
->segment
),
1367 warn_overflow(2, opx
);
1368 if (is_sbyte16(opx
)) {
1370 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1374 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1375 opx
->segment
, opx
->wrt
);
1382 bytes
[0] = *codes
++;
1383 if (is_sbyte16(opx
))
1384 bytes
[0] |= 2; /* s-bit */
1385 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1391 warn_overflow(4, opx
);
1392 if (is_sbyte32(opx
)) {
1394 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1398 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1399 opx
->segment
, opx
->wrt
);
1406 bytes
[0] = *codes
++;
1407 if (is_sbyte32(opx
))
1408 bytes
[0] |= 2; /* s-bit */
1409 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1419 (ins
->drexdst
<< 4) |
1420 (ins
->rex
& REX_OC
? 0x08 : 0) |
1421 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1423 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1429 opx
= &ins
->oprs
[c
>> 3];
1430 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1431 opx
= &ins
->oprs
[c
& 7];
1432 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1433 errfunc(ERR_NONFATAL
,
1434 "non-absolute expression not permitted as argument %d",
1437 if (opx
->offset
& ~15) {
1438 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1439 "four-bit argument exceeds bounds");
1441 bytes
[0] |= opx
->offset
& 15;
1443 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1449 opx
= &ins
->oprs
[c
>> 4];
1450 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1452 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1458 opx
= &ins
->oprs
[c
];
1459 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1460 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1466 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1467 (int32_t)data
!= (int64_t)data
) {
1468 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1469 "signed dword immediate exceeds bounds");
1471 if (is_sbyte32(opx
)) {
1473 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1477 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1478 opx
->segment
, opx
->wrt
);
1485 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1486 (int32_t)data
!= (int64_t)data
) {
1487 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1488 "signed dword immediate exceeds bounds");
1490 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1491 opx
->segment
, opx
->wrt
);
1498 if (ins
->vex_m
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1500 bytes
[1] = ins
->vex_m
| ((~ins
->rex
& 7) << 5);
1501 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1502 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1503 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1507 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1508 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1509 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1519 if (ins
->rex
& REX_W
)
1521 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1523 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1528 um
= (uint64_t)2 << (s
-1);
1531 if (uv
> 127 && uv
< (uint64_t)-128 &&
1532 (uv
< um
-128 || uv
> um
-1)) {
1533 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1534 "signed byte value exceeds bounds");
1536 if (opx
->segment
!= NO_SEG
) {
1538 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1539 opx
->segment
, opx
->wrt
);
1542 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1553 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1555 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1562 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1564 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1583 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1592 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1607 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1608 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1617 *bytes
= c
- 0332 + 0xF2;
1618 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1623 if (ins
->rex
& REX_R
) {
1625 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1628 ins
->rex
&= ~(REX_L
|REX_R
);
1639 if (ins
->oprs
[0].segment
!= NO_SEG
)
1640 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1642 int64_t size
= ins
->oprs
[0].offset
;
1644 out(offset
, segment
, NULL
,
1645 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1653 switch (ins
->oprs
[0].basereg
) {
1668 "bizarre 8086 segment register received");
1670 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1677 switch (ins
->oprs
[0].basereg
) {
1686 "bizarre 386 segment register received");
1688 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1697 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1703 bytes
[0] = c
- 0362 + 0xf2;
1704 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1714 *bytes
= c
- 0366 + 0x66;
1715 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1725 *bytes
= bits
== 16 ? 3 : 5;
1726 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1751 /* pick rfield from operand b */
1752 rflags
= regflag(&ins
->oprs
[c
& 7]);
1753 rfield
= nasm_regvals
[ins
->oprs
[c
& 7].basereg
];
1755 /* rfield is constant */
1761 (&ins
->oprs
[(c
>> 3) & 7], &ea_data
, bits
,
1762 ins
->addr_size
, rfield
, rflags
)) {
1763 errfunc(ERR_NONFATAL
, "invalid effective address");
1768 *p
++ = ea_data
.modrm
;
1769 if (ea_data
.sib_present
)
1772 /* DREX suffixes come between the SIB and the displacement */
1773 if (ins
->rex
& REX_D
) {
1775 (ins
->drexdst
<< 4) |
1776 (ins
->rex
& REX_OC
? 0x08 : 0) |
1777 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1782 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1785 * Make sure the address gets the right offset in case
1786 * the line breaks in the .lst file (BR 1197827)
1791 switch (ea_data
.bytes
) {
1795 if (ins
->oprs
[(c
>> 3) & 7].segment
!= NO_SEG
) {
1796 data
= ins
->oprs
[(c
>> 3) & 7].offset
;
1797 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1798 ins
->oprs
[(c
>> 3) & 7].segment
,
1799 ins
->oprs
[(c
>> 3) & 7].wrt
);
1801 *bytes
= ins
->oprs
[(c
>> 3) & 7].offset
;
1802 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1,
1810 data
= ins
->oprs
[(c
>> 3) & 7].offset
;
1811 warn_overflow(ea_data
.bytes
, opx
);
1814 if (ins
->oprs
[(c
>> 3) & 7].segment
== segment
) {
1816 out(offset
, segment
, &data
,
1817 OUT_ADDRESS
, ea_data
.bytes
,
1820 out(offset
, segment
, &data
,
1821 OUT_REL4ADR
, insn_end
- offset
,
1822 ins
->oprs
[(c
>> 3) & 7].segment
,
1823 ins
->oprs
[(c
>> 3) & 7].wrt
);
1827 out(offset
, segment
, &data
,
1828 OUT_ADDRESS
, ea_data
.bytes
,
1829 ins
->oprs
[(c
>> 3) & 7].segment
,
1830 ins
->oprs
[(c
>> 3) & 7].wrt
);
1839 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1840 ": instruction code 0x%02X given", c
);
1846 static int32_t regflag(const operand
* o
)
1848 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1849 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1851 return nasm_reg_flags
[o
->basereg
];
1854 static int32_t regval(const operand
* o
)
1856 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1857 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1859 return nasm_regvals
[o
->basereg
];
1862 static int op_rexflags(const operand
* o
, int mask
)
1867 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1868 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1871 flags
= nasm_reg_flags
[o
->basereg
];
1872 val
= nasm_regvals
[o
->basereg
];
1874 return rexflags(val
, flags
, mask
);
1877 static int rexflags(int val
, int32_t flags
, int mask
)
1882 rex
|= REX_B
|REX_X
|REX_R
;
1885 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1887 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1893 static int matches(const struct itemplate
*itemp
, insn
* instruction
, int bits
)
1895 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1902 if (itemp
->opcode
!= instruction
->opcode
)
1906 * Count the operands
1908 if (itemp
->operands
!= instruction
->operands
)
1912 * Check that no spurious colons or TOs are present
1914 for (i
= 0; i
< itemp
->operands
; i
++)
1915 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
1919 * Process size flags
1921 if (itemp
->flags
& IF_ARMASK
) {
1922 memset(size
, 0, sizeof size
);
1924 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
1926 switch (itemp
->flags
& IF_SMASK
) {
1963 switch (itemp
->flags
& IF_SMASK
) {
1998 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2003 * Check that the operand flags all match up
2005 for (i
= 0; i
< itemp
->operands
; i
++) {
2006 int32_t type
= instruction
->oprs
[i
].type
;
2007 if (!(type
& SIZE_MASK
))
2010 if (itemp
->opd
[i
] & SAME_AS
) {
2011 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2012 if (type
!= instruction
->oprs
[j
].type
||
2013 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2015 } else if (itemp
->opd
[i
] & ~type
||
2016 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2017 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2018 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2027 * Check operand sizes
2029 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2030 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2032 for (i
= 0; i
< oprs
; i
++) {
2033 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2035 for (j
= 0; j
< oprs
; j
++)
2041 oprs
= itemp
->operands
;
2044 for (i
= 0; i
< itemp
->operands
; i
++) {
2045 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2046 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2051 * Check template is okay at the set cpu level
2053 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2057 * Check if instruction is available in long mode
2059 if ((itemp
->flags
& IF_NOLONG
) && (bits
== 64))
2063 * Check if special handling needed for Jumps
2065 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2071 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2072 int addrbits
, int rfield
, int32_t rflags
)
2074 bool forw_ref
= !!(input
->opflags
& OPFLAG_FORWARD
);
2076 output
->rip
= false;
2078 /* REX flags for the rfield operand */
2079 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2081 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2085 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2086 || input
->basereg
>= REG_ENUM_LIMIT
)
2089 i
= nasm_regvals
[input
->basereg
];
2092 return NULL
; /* Invalid EA register */
2094 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2096 output
->sib_present
= false; /* no SIB necessary */
2097 output
->bytes
= 0; /* no offset necessary either */
2098 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2099 } else { /* it's a memory reference */
2100 if (input
->basereg
== -1
2101 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2102 /* it's a pure offset */
2103 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2104 int scale
, index
, base
;
2105 output
->sib_present
= true;
2109 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2111 output
->modrm
= 4 | ((rfield
& 7) << 3);
2112 output
->rip
= false;
2114 output
->sib_present
= false;
2115 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2116 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2117 output
->rip
= bits
== 64;
2119 } else { /* it's an indirection */
2120 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2121 int32_t o
= input
->offset
, seg
= input
->segment
;
2122 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2125 int32_t ix
, bx
; /* register flags */
2128 i
= -1; /* make this easy, at least */
2130 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2131 it
= nasm_regvals
[i
];
2132 ix
= nasm_reg_flags
[i
];
2138 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2139 bt
= nasm_regvals
[b
];
2140 bx
= nasm_reg_flags
[b
];
2146 /* check for a 32/64-bit memory reference... */
2147 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2148 /* it must be a 32/64-bit memory reference. Firstly we have
2149 * to check that all registers involved are type E/Rxx. */
2150 int32_t sok
= BITS32
|BITS64
;
2153 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2161 return NULL
; /* Invalid register */
2162 if (~sok
& bx
& SIZE_MASK
)
2163 return NULL
; /* Invalid size */
2167 /* While we're here, ensure the user didn't specify
2169 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2172 if (addrbits
== 16 ||
2173 (addrbits
== 32 && !(sok
& BITS32
)) ||
2174 (addrbits
== 64 && !(sok
& BITS64
)))
2177 /* now reorganize base/index */
2178 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2179 ((hb
== b
&& ht
== EAH_NOTBASE
)
2180 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2181 /* swap if hints say so */
2182 t
= bt
, bt
= it
, it
= t
;
2183 t
= bx
, bx
= ix
, ix
= t
;
2185 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2186 bt
= -1, bx
= 0, s
++;
2187 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2188 /* make single reg base, unless hint */
2189 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2191 if (((s
== 2 && it
!= REG_NUM_ESP
2192 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2193 || s
== 5 || s
== 9) && bt
== -1)
2194 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2195 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2196 && (input
->eaflags
& EAF_TIMESTWO
))
2197 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2198 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2199 if (s
== 1 && it
== REG_NUM_ESP
) {
2200 /* swap ESP into base if scale is 1 */
2201 t
= it
, it
= bt
, bt
= t
;
2202 t
= ix
, ix
= bx
, bx
= t
;
2204 if (it
== REG_NUM_ESP
2205 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2206 return NULL
; /* wrong, for various reasons */
2208 output
->rex
|= rexflags(it
, ix
, REX_X
);
2209 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2211 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2220 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2221 seg
== NO_SEG
&& !forw_ref
&&
2223 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2225 else if (input
->eaflags
& EAF_BYTEOFFS
||
2226 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2228 && !(input
->eaflags
& EAF_WORDOFFS
)))
2234 output
->sib_present
= false;
2235 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2236 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2239 int mod
, scale
, index
, base
;
2259 default: /* then what the smeg is it? */
2260 return NULL
; /* panic */
2268 if (base
!= 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
= true;
2283 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2284 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2285 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2287 } else { /* it's 16-bit */
2290 /* check for 64-bit long mode */
2294 /* check all registers are BX, BP, SI or DI */
2295 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2296 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2297 && i
!= R_SI
&& i
!= R_DI
))
2300 /* ensure the user didn't specify DWORD/QWORD */
2301 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2304 if (s
!= 1 && i
!= -1)
2305 return NULL
; /* no can do, in 16-bit EA */
2306 if (b
== -1 && i
!= -1) {
2311 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2316 /* have BX/BP as base, SI/DI index */
2318 return NULL
; /* shouldn't ever happen, in theory */
2319 if (i
!= -1 && b
!= -1 &&
2320 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2321 return NULL
; /* invalid combinations */
2322 if (b
== -1) /* pure offset: handled above */
2323 return NULL
; /* so if it gets to here, panic! */
2327 switch (i
* 256 + b
) {
2328 case R_SI
* 256 + R_BX
:
2331 case R_DI
* 256 + R_BX
:
2334 case R_SI
* 256 + R_BP
:
2337 case R_DI
* 256 + R_BP
:
2355 if (rm
== -1) /* can't happen, in theory */
2356 return NULL
; /* so panic if it does */
2358 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2359 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2361 else if (input
->eaflags
& EAF_BYTEOFFS
||
2362 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2364 && !(input
->eaflags
& EAF_WORDOFFS
)))
2369 output
->sib_present
= false; /* no SIB - it's 16-bit */
2370 output
->bytes
= mod
; /* bytes of offset needed */
2371 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2376 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2380 static void add_asp(insn
*ins
, int addrbits
)
2385 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2387 switch (ins
->prefixes
[PPS_ASIZE
]) {
2398 valid
&= (addrbits
== 32) ? 16 : 32;
2404 for (j
= 0; j
< ins
->operands
; j
++) {
2405 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2408 /* Verify as Register */
2409 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2410 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2413 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2415 /* Verify as Register */
2416 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2417 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2420 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2422 if (ins
->oprs
[j
].scale
== 0)
2426 int ds
= ins
->oprs
[j
].disp_size
;
2427 if ((addrbits
!= 64 && ds
> 8) ||
2428 (addrbits
== 64 && ds
== 16))
2448 if (valid
& addrbits
) {
2449 ins
->addr_size
= addrbits
;
2450 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2451 /* Add an address size prefix */
2452 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2453 ins
->prefixes
[PPS_ASIZE
] = pref
;
2454 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2457 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2458 ins
->addr_size
= addrbits
; /* Error recovery */
2461 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2463 for (j
= 0; j
< ins
->operands
; j
++) {
2464 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2465 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2466 != ins
->addr_size
) {
2467 /* mem_offs sizes must match the address size; if not,
2468 strip the MEM_OFFS bit and match only EA instructions */
2469 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);