1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * assemble.c code generation for the Netwide Assembler
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
39 * \1..\4 - that many literal bytes follow in the code stream
40 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
43 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
45 * \14..\17 - a signed byte immediate operand, from operand 0..3
46 * \20..\23 - a byte immediate operand, from operand 0..3
47 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
48 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
50 * assembly mode or the operand-size override on the operand
51 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
53 * depending on the address size of the instruction.
54 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
58 * assembly mode or the operand-size override on the operand
59 * \70..\73 - a long relative operand, from operand 0..3
60 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
61 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
63 * \140..\143 - an immediate word or signed byte for operand 0..3
64 * \144..\147 - or 2 (s-field) into opcode byte if operand 0..3
65 * is a signed byte rather than a word. Opcode byte follows.
66 * \150..\153 - an immediate dword or signed byte for operand 0..3
67 * \154..\157 - or 2 (s-field) into opcode byte if operand 0..3
68 * is a signed byte rather than a dword. Opcode byte follows.
69 * \160..\163 - this instruction uses DREX rather than REX, with the
70 * OC0 field set to 0, and the dest field taken from
72 * \164..\167 - this instruction uses DREX rather than REX, with the
73 * OC0 field set to 1, and the dest field taken from
75 * \171 - placement of DREX suffix in the absence of an EA
76 * \172\ab - the register number from operand a in bits 7..4, with
77 * the 4-bit immediate from operand b in bits 3..0.
78 * \173\xab - the register number from operand a in bits 7..4, with
79 * the value b in bits 3..0.
80 * \174\a - the register number from operand a in bits 7..4, and
81 * an arbitrary value in bits 3..0 (assembled as zero.)
82 * \2ab - a ModRM, calculated on EA in operand a, with the spare
83 * field equal to digit b.
84 * \250..\253 - same as \150..\153, except warn if the 64-bit operand
85 * is not equal to the truncated and sign-extended 32-bit
86 * operand; used for 32-bit immediates in 64-bit mode.
87 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
88 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
89 * V field taken from operand 0..3.
90 * \270 - this instruction uses VEX/XOP rather than REX, with the
91 * V field set to 1111b.
93 * VEX/XOP prefixes are followed by the sequence:
94 * \tmm\wlp where mm is the M field; and wlp is:
96 * [w0] ww = 0 for W = 0
97 * [w1] ww = 1 for W = 1
98 * [wx] ww = 2 for W don't care (always assembled as 0)
99 * [ww] ww = 3 for W used as REX.W
101 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
103 * \274..\277 - a signed byte immediate operand, from operand 0..3,
104 * which is to be extended to the operand size.
105 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
106 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
107 * \312 - (disassembler only) invalid with non-default address size.
108 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
109 * \314 - (disassembler only) invalid with REX.B
110 * \315 - (disassembler only) invalid with REX.X
111 * \316 - (disassembler only) invalid with REX.R
112 * \317 - (disassembler only) invalid with REX.W
113 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
114 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
115 * \322 - indicates that this instruction is only valid when the
116 * operand size is the default (instruction to disassembler,
117 * generates no code in the assembler)
118 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
119 * \324 - indicates 64-bit operand size requiring REX prefix.
120 * \325 - instruction which always uses spl/bpl/sil/dil
121 * \330 - a literal byte follows in the code stream, to be added
122 * to the condition code value of the instruction.
123 * \331 - instruction not valid with REP prefix. Hint for
124 * disassembler only; for SSE instructions.
125 * \332 - REP prefix (0xF2 byte) used as opcode extension.
126 * \333 - REP prefix (0xF3 byte) used as opcode extension.
127 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
128 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
129 * \336 - force a REP(E) prefix (0xF2) even if not specified.
130 * \337 - force a REPNE prefix (0xF3) even if not specified.
131 * \336-\337 are still listed as prefixes in the disassembler.
132 * \340 - reserve <operand 0> bytes of uninitialized storage.
133 * Operand 0 had better be a segmentless constant.
134 * \341 - this instruction needs a WAIT "prefix"
135 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
136 * (POP is never used for CS) depending on operand 0
137 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
139 * \360 - no SSE prefix (== \364\331)
140 * \361 - 66 SSE prefix (== \366\331)
141 * \362 - F2 SSE prefix (== \364\332)
142 * \363 - F3 SSE prefix (== \364\333)
143 * \364 - operand-size prefix (0x66) not permitted
144 * \365 - address-size prefix (0x67) not permitted
145 * \366 - operand-size prefix (0x66) used as opcode extension
146 * \367 - address-size prefix (0x67) used as opcode extension
147 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
148 * 370 is used for Jcc, 371 is used for JMP.
149 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
150 * used for conditional jump over longer jump
153 #include "compiler.h"
157 #include <inttypes.h>
161 #include "assemble.h"
166 int sib_present
; /* is a SIB byte necessary? */
167 int bytes
; /* # of bytes of offset needed */
168 int size
; /* lazy - this is sib+bytes+1 */
169 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
172 static uint32_t cpu
; /* cpu level received from nasm.c */
173 static efunc errfunc
;
174 static struct ofmt
*outfmt
;
175 static ListGen
*list
;
177 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
178 static void gencode(int32_t segment
, int64_t offset
, int bits
,
179 insn
* ins
, const struct itemplate
*temp
,
181 static int matches(const struct itemplate
*, insn
*, int bits
);
182 static int32_t regflag(const operand
*);
183 static int32_t regval(const operand
*);
184 static int rexflags(int, int32_t, int);
185 static int op_rexflags(const operand
*, int);
186 static ea
*process_ea(operand
*, ea
*, int, int, int, int32_t);
187 static void add_asp(insn
*, int);
189 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
191 return ins
->prefixes
[pos
] == prefix
;
194 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
196 if (ins
->prefixes
[pos
])
197 errfunc(ERR_NONFATAL
, "invalid %s prefix",
198 prefix_name(ins
->prefixes
[pos
]));
201 static const char *size_name(int size
)
223 static void warn_overflow(int size
, const struct operand
*o
)
225 if (size
< 8 && o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
226 int64_t lim
= ((int64_t)1 << (size
*8))-1;
227 int64_t data
= o
->offset
;
229 if (data
< ~lim
|| data
> lim
)
230 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
231 "%s data exceeds bounds", size_name(size
));
235 * This routine wrappers the real output format's output routine,
236 * in order to pass a copy of the data off to the listing file
237 * generator at the same time.
239 static void out(int64_t offset
, int32_t segto
, const void *data
,
240 enum out_type type
, uint64_t size
,
241 int32_t segment
, int32_t wrt
)
243 static int32_t lineno
= 0; /* static!!! */
244 static char *lnfname
= NULL
;
247 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
249 * This is a non-relocated address, and we're going to
250 * convert it into RAWDATA format.
255 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
259 WRITEADDR(q
, *(int64_t *)data
, size
);
264 list
->output(offset
, data
, type
, size
);
267 * this call to src_get determines when we call the
268 * debug-format-specific "linenum" function
269 * it updates lineno and lnfname to the current values
270 * returning 0 if "same as last time", -2 if lnfname
271 * changed, and the amount by which lineno changed,
272 * if it did. thus, these variables must be static
275 if (src_get(&lineno
, &lnfname
)) {
276 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
279 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
282 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
283 insn
* ins
, const uint8_t *code
)
288 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
292 if (optimizing
< 0 && c
== 0371)
295 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
297 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
298 /* Be optimistic in pass 1 */
301 if (ins
->oprs
[0].segment
!= segment
)
304 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
305 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
308 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
309 insn
* instruction
, struct ofmt
*output
, efunc error
,
312 const struct itemplate
*temp
;
317 int64_t start
= offset
;
318 int64_t wsize
= 0; /* size for DB etc. */
320 errfunc
= error
; /* to pass to other functions */
322 outfmt
= output
; /* likewise */
323 list
= listgen
; /* and again */
325 switch (instruction
->opcode
) {
355 int32_t t
= instruction
->times
;
358 "instruction->times < 0 (%ld) in assemble()", t
);
360 while (t
--) { /* repeat TIMES times */
361 for (e
= instruction
->eops
; e
; e
= e
->next
) {
362 if (e
->type
== EOT_DB_NUMBER
) {
364 if (e
->segment
!= NO_SEG
)
365 errfunc(ERR_NONFATAL
,
366 "one-byte relocation attempted");
368 uint8_t out_byte
= e
->offset
;
369 out(offset
, segment
, &out_byte
,
370 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
372 } else if (wsize
> 8) {
373 errfunc(ERR_NONFATAL
,
374 "integer supplied to a DT, DO or DY"
377 out(offset
, segment
, &e
->offset
,
378 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
380 } else if (e
->type
== EOT_DB_STRING
||
381 e
->type
== EOT_DB_STRING_FREE
) {
384 out(offset
, segment
, e
->stringval
,
385 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
386 align
= e
->stringlen
% wsize
;
389 align
= wsize
- align
;
390 out(offset
, segment
, zero_buffer
,
391 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
393 offset
+= e
->stringlen
+ align
;
396 if (t
> 0 && t
== instruction
->times
- 1) {
398 * Dummy call to list->output to give the offset to the
401 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
402 list
->uplevel(LIST_TIMES
);
405 if (instruction
->times
> 1)
406 list
->downlevel(LIST_TIMES
);
407 return offset
- start
;
410 if (instruction
->opcode
== I_INCBIN
) {
411 const char *fname
= instruction
->eops
->stringval
;
414 fp
= fopen(fname
, "rb");
416 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
418 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
419 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
422 static char buf
[4096];
423 size_t t
= instruction
->times
;
428 if (instruction
->eops
->next
) {
429 base
= instruction
->eops
->next
->offset
;
431 if (instruction
->eops
->next
->next
&&
432 len
> (size_t)instruction
->eops
->next
->next
->offset
)
433 len
= (size_t)instruction
->eops
->next
->next
->offset
;
436 * Dummy call to list->output to give the offset to the
439 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
440 list
->uplevel(LIST_INCBIN
);
444 fseek(fp
, base
, SEEK_SET
);
448 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
451 * This shouldn't happen unless the file
452 * actually changes while we are reading
456 "`incbin': unexpected EOF while"
457 " reading file `%s'", fname
);
458 t
= 0; /* Try to exit cleanly */
461 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
466 list
->downlevel(LIST_INCBIN
);
467 if (instruction
->times
> 1) {
469 * Dummy call to list->output to give the offset to the
472 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
473 list
->uplevel(LIST_TIMES
);
474 list
->downlevel(LIST_TIMES
);
477 return instruction
->times
* len
;
479 return 0; /* if we're here, there's an error */
482 /* Check to see if we need an address-size prefix */
483 add_asp(instruction
, bits
);
487 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++){
488 int m
= matches(temp
, instruction
, bits
);
490 (m
== 99 && jmp_match(segment
, offset
, bits
,
491 instruction
, temp
->code
))) {
493 int64_t insn_size
= calcsize(segment
, offset
, bits
,
494 instruction
, temp
->code
);
495 itimes
= instruction
->times
;
496 if (insn_size
< 0) /* shouldn't be, on pass two */
497 error(ERR_PANIC
, "errors made it through from pass one");
500 for (j
= 0; j
< MAXPREFIX
; j
++) {
502 switch (instruction
->prefixes
[j
]) {
520 error(ERR_WARNING
| ERR_PASS2
,
521 "cs segment base generated, but will be ignored in 64-bit mode");
527 error(ERR_WARNING
| ERR_PASS2
,
528 "ds segment base generated, but will be ignored in 64-bit mode");
534 error(ERR_WARNING
| ERR_PASS2
,
535 "es segment base generated, but will be ignored in 64-bit mode");
547 error(ERR_WARNING
| ERR_PASS2
,
548 "ss segment base generated, but will be ignored in 64-bit mode");
555 "segr6 and segr7 cannot be used as prefixes");
560 "16-bit addressing is not supported "
562 } else if (bits
!= 16)
572 "64-bit addressing is only supported "
596 error(ERR_PANIC
, "invalid instruction prefix");
599 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
604 insn_end
= offset
+ insn_size
;
605 gencode(segment
, offset
, bits
, instruction
,
608 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
610 * Dummy call to list->output to give the offset to the
613 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
614 list
->uplevel(LIST_TIMES
);
617 if (instruction
->times
> 1)
618 list
->downlevel(LIST_TIMES
);
619 return offset
- start
;
620 } else if (m
> 0 && m
> size_prob
) {
625 if (temp
->opcode
== -1) { /* didn't match any instruction */
628 error(ERR_NONFATAL
, "operation size not specified");
631 error(ERR_NONFATAL
, "mismatch in operand sizes");
634 error(ERR_NONFATAL
, "no instruction for this cpu level");
637 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
642 "invalid combination of opcode and operands");
649 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
650 insn
* instruction
, efunc error
)
652 const struct itemplate
*temp
;
654 errfunc
= error
; /* to pass to other functions */
657 if (instruction
->opcode
== -1)
660 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
661 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
662 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
663 instruction
->opcode
== I_DY
) {
665 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
668 switch (instruction
->opcode
) {
694 for (e
= instruction
->eops
; e
; e
= e
->next
) {
698 if (e
->type
== EOT_DB_NUMBER
)
700 else if (e
->type
== EOT_DB_STRING
||
701 e
->type
== EOT_DB_STRING_FREE
)
702 osize
= e
->stringlen
;
704 align
= (-osize
) % wsize
;
707 isize
+= osize
+ align
;
709 return isize
* instruction
->times
;
712 if (instruction
->opcode
== I_INCBIN
) {
713 const char *fname
= instruction
->eops
->stringval
;
717 fp
= fopen(fname
, "rb");
719 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
721 else if (fseek(fp
, 0L, SEEK_END
) < 0)
722 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
727 if (instruction
->eops
->next
) {
728 len
-= instruction
->eops
->next
->offset
;
729 if (instruction
->eops
->next
->next
&&
730 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
731 len
= (size_t)instruction
->eops
->next
->next
->offset
;
734 return instruction
->times
* len
;
736 return 0; /* if we're here, there's an error */
739 /* Check to see if we need an address-size prefix */
740 add_asp(instruction
, bits
);
742 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++) {
743 int m
= matches(temp
, instruction
, bits
);
745 (m
== 99 && jmp_match(segment
, offset
, bits
,
746 instruction
, temp
->code
))) {
747 /* we've matched an instruction. */
749 const uint8_t *codes
= temp
->code
;
752 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
755 for (j
= 0; j
< MAXPREFIX
; j
++) {
756 switch (instruction
->prefixes
[j
]) {
782 return isize
* instruction
->times
;
785 return -1; /* didn't match any instruction */
788 static bool possible_sbyte(operand
*o
)
790 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
791 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
792 optimizing
>= 0 && !(o
->type
& STRICT
);
795 /* check that opn[op] is a signed byte of size 16 or 32 */
796 static bool is_sbyte16(operand
*o
)
800 if (!possible_sbyte(o
))
804 return v
>= -128 && v
<= 127;
807 static bool is_sbyte32(operand
*o
)
811 if (!possible_sbyte(o
))
815 return v
>= -128 && v
<= 127;
818 /* Common construct */
819 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
821 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
822 insn
* ins
, const uint8_t *codes
)
831 ins
->rex
= 0; /* Ensure REX is reset */
833 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
836 (void)segment
; /* Don't warn that this parameter is unused */
837 (void)offset
; /* Don't warn that this parameter is unused */
841 op1
= (c
& 3) + ((opex
& 1) << 2);
842 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
843 opx
= &ins
->oprs
[op1
];
844 opex
= 0; /* For the next iteration */
851 codes
+= c
, length
+= c
;
862 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
877 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
878 length
+= (opx
->type
& BITS16
) ? 2 : 4;
880 length
+= (bits
== 16) ? 2 : 4;
888 length
+= ins
->addr_size
>> 3;
896 length
+= 8; /* MOV reg64/imm */
904 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
905 length
+= (opx
->type
& BITS16
) ? 2 : 4;
907 length
+= (bits
== 16) ? 2 : 4;
919 length
+= is_sbyte16(opx
) ? 1 : 2;
928 length
+= is_sbyte32(opx
) ? 1 : 4;
939 ins
->drexdst
= regval(opx
);
944 ins
->rex
|= REX_D
|REX_OC
;
945 ins
->drexdst
= regval(opx
);
959 length
+= is_sbyte32(opx
) ? 1 : 4;
968 ins
->drexdst
= regval(opx
);
969 ins
->vex_cm
= *codes
++;
970 ins
->vex_wlp
= *codes
++;
976 ins
->vex_cm
= *codes
++;
977 ins
->vex_wlp
= *codes
++;
990 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
994 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1001 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1002 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1010 length
+= (bits
!= 16);
1014 length
+= (bits
== 16);
1052 if (!ins
->prefixes
[PPS_LREP
])
1053 ins
->prefixes
[PPS_LREP
] = P_REP
;
1057 if (!ins
->prefixes
[PPS_LREP
])
1058 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1062 if (ins
->oprs
[0].segment
!= NO_SEG
)
1063 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1064 " quantity of BSS space");
1066 length
+= ins
->oprs
[0].offset
;
1070 if (!ins
->prefixes
[PPS_WAIT
])
1071 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1121 struct operand
*opy
= &ins
->oprs
[op2
];
1123 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1126 /* pick rfield from operand b (opx) */
1127 rflags
= regflag(opx
);
1128 rfield
= nasm_regvals
[opx
->basereg
];
1133 if (!process_ea(opy
, &ea_data
, bits
,
1134 ins
->addr_size
, rfield
, rflags
)) {
1135 errfunc(ERR_NONFATAL
, "invalid effective address");
1138 ins
->rex
|= ea_data
.rex
;
1139 length
+= ea_data
.size
;
1145 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1146 ": instruction code \\%o (0x%02X) given", c
, c
);
1151 ins
->rex
&= rex_mask
;
1153 if (ins
->rex
& REX_NH
) {
1154 if (ins
->rex
& REX_H
) {
1155 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1158 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1161 if (ins
->rex
& REX_V
) {
1162 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1164 if (ins
->rex
& REX_H
) {
1165 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1168 switch (ins
->vex_wlp
& 030) {
1182 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1183 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1186 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1190 } else if (ins
->rex
& REX_D
) {
1191 if (ins
->rex
& REX_H
) {
1192 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1195 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1196 ins
->drexdst
> 7)) {
1197 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1201 } else if (ins
->rex
& REX_REAL
) {
1202 if (ins
->rex
& REX_H
) {
1203 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1205 } else if (bits
== 64) {
1207 } else if ((ins
->rex
& REX_L
) &&
1208 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1211 assert_no_prefix(ins
, PPS_LREP
);
1214 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1222 #define EMIT_REX() \
1223 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1224 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1225 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1230 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1231 insn
* ins
, const struct itemplate
*temp
,
1234 static char condval
[] = { /* conditional opcodes */
1235 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1236 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1237 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1244 struct operand
*opx
;
1245 const uint8_t *codes
= temp
->code
;
1250 op1
= (c
& 3) + ((opex
& 1) << 2);
1251 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1252 opx
= &ins
->oprs
[op1
];
1253 opex
= 0; /* For the next iteration */
1261 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1274 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1275 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1280 /* The test for BITS8 and SBYTE here is intended to avoid
1281 warning on optimizer actions due to SBYTE, while still
1282 warn on explicit BYTE directives. Also warn, obviously,
1283 if the optimizer isn't enabled. */
1284 if (((opx
->type
& BITS8
) ||
1285 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1286 (opx
->offset
< -128 || opx
->offset
> 127)) {
1287 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1288 "signed byte value exceeds bounds");
1290 if (opx
->segment
!= NO_SEG
) {
1292 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1293 opx
->segment
, opx
->wrt
);
1295 bytes
[0] = opx
->offset
;
1296 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1303 if (opx
->offset
< -256 || opx
->offset
> 255) {
1304 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1305 "byte value exceeds bounds");
1307 if (opx
->segment
!= NO_SEG
) {
1309 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1310 opx
->segment
, opx
->wrt
);
1312 bytes
[0] = opx
->offset
;
1313 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1320 if (opx
->offset
< 0 || opx
->offset
> 255)
1321 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1322 "unsigned byte value exceeds bounds");
1323 if (opx
->segment
!= NO_SEG
) {
1325 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1326 opx
->segment
, opx
->wrt
);
1328 bytes
[0] = opx
->offset
;
1329 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1336 warn_overflow(2, opx
);
1338 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1339 opx
->segment
, opx
->wrt
);
1344 if (opx
->type
& (BITS16
| BITS32
))
1345 size
= (opx
->type
& BITS16
) ? 2 : 4;
1347 size
= (bits
== 16) ? 2 : 4;
1348 warn_overflow(size
, opx
);
1350 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1351 opx
->segment
, opx
->wrt
);
1356 warn_overflow(4, opx
);
1358 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1359 opx
->segment
, opx
->wrt
);
1365 size
= ins
->addr_size
>> 3;
1366 warn_overflow(size
, opx
);
1367 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1368 opx
->segment
, opx
->wrt
);
1373 if (opx
->segment
!= segment
)
1374 errfunc(ERR_NONFATAL
,
1375 "short relative jump outside segment");
1376 data
= opx
->offset
- insn_end
;
1377 if (data
> 127 || data
< -128)
1378 errfunc(ERR_NONFATAL
, "short jump is out of range");
1380 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1385 data
= (int64_t)opx
->offset
;
1386 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1387 opx
->segment
, opx
->wrt
);
1392 if (opx
->segment
!= segment
) {
1394 out(offset
, segment
, &data
,
1395 OUT_REL2ADR
, insn_end
- offset
,
1396 opx
->segment
, opx
->wrt
);
1398 data
= opx
->offset
- insn_end
;
1399 out(offset
, segment
, &data
,
1400 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1406 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1407 size
= (opx
->type
& BITS16
) ? 2 : 4;
1409 size
= (bits
== 16) ? 2 : 4;
1410 if (opx
->segment
!= segment
) {
1412 out(offset
, segment
, &data
,
1413 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1414 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1416 data
= opx
->offset
- insn_end
;
1417 out(offset
, segment
, &data
,
1418 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1424 if (opx
->segment
!= segment
) {
1426 out(offset
, segment
, &data
,
1427 OUT_REL4ADR
, insn_end
- offset
,
1428 opx
->segment
, opx
->wrt
);
1430 data
= opx
->offset
- insn_end
;
1431 out(offset
, segment
, &data
,
1432 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1438 if (opx
->segment
== NO_SEG
)
1439 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1442 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1443 outfmt
->segbase(1 + opx
->segment
),
1450 warn_overflow(2, opx
);
1451 if (is_sbyte16(opx
)) {
1453 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1457 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1458 opx
->segment
, opx
->wrt
);
1465 bytes
[0] = *codes
++;
1466 if (is_sbyte16(opx
))
1467 bytes
[0] |= 2; /* s-bit */
1468 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1474 warn_overflow(4, opx
);
1475 if (is_sbyte32(opx
)) {
1477 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1481 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1482 opx
->segment
, opx
->wrt
);
1489 bytes
[0] = *codes
++;
1490 if (is_sbyte32(opx
))
1491 bytes
[0] |= 2; /* s-bit */
1492 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1502 (ins
->drexdst
<< 4) |
1503 (ins
->rex
& REX_OC
? 0x08 : 0) |
1504 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1506 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1512 opx
= &ins
->oprs
[c
>> 3];
1513 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1514 opx
= &ins
->oprs
[c
& 7];
1515 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1516 errfunc(ERR_NONFATAL
,
1517 "non-absolute expression not permitted as argument %d",
1520 if (opx
->offset
& ~15) {
1521 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1522 "four-bit argument exceeds bounds");
1524 bytes
[0] |= opx
->offset
& 15;
1526 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1532 opx
= &ins
->oprs
[c
>> 4];
1533 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1535 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1541 opx
= &ins
->oprs
[c
];
1542 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1543 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1549 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1550 (int32_t)data
!= (int64_t)data
) {
1551 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1552 "signed dword immediate exceeds bounds");
1554 if (is_sbyte32(opx
)) {
1556 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1560 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1561 opx
->segment
, opx
->wrt
);
1568 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1569 (int32_t)data
!= (int64_t)data
) {
1570 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1571 "signed dword immediate exceeds bounds");
1573 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1574 opx
->segment
, opx
->wrt
);
1581 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1582 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1583 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1584 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1585 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1586 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1590 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1591 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1592 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1602 if (ins
->rex
& REX_W
)
1604 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1606 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1611 um
= (uint64_t)2 << (s
-1);
1614 if (uv
> 127 && uv
< (uint64_t)-128 &&
1615 (uv
< um
-128 || uv
> um
-1)) {
1616 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1617 "signed byte value exceeds bounds");
1619 if (opx
->segment
!= NO_SEG
) {
1621 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1622 opx
->segment
, opx
->wrt
);
1625 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1636 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1638 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1645 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1647 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1666 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1675 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1693 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1694 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1703 *bytes
= c
- 0332 + 0xF2;
1704 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1709 if (ins
->rex
& REX_R
) {
1711 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1714 ins
->rex
&= ~(REX_L
|REX_R
);
1725 if (ins
->oprs
[0].segment
!= NO_SEG
)
1726 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1728 int64_t size
= ins
->oprs
[0].offset
;
1730 out(offset
, segment
, NULL
,
1731 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1742 switch (ins
->oprs
[0].basereg
) {
1757 "bizarre 8086 segment register received");
1759 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1766 switch (ins
->oprs
[0].basereg
) {
1775 "bizarre 386 segment register received");
1777 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1786 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1792 bytes
[0] = c
- 0362 + 0xf2;
1793 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1803 *bytes
= c
- 0366 + 0x66;
1804 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1814 *bytes
= bits
== 16 ? 3 : 5;
1815 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1838 struct operand
*opy
= &ins
->oprs
[op2
];
1841 /* pick rfield from operand b (opx) */
1842 rflags
= regflag(opx
);
1843 rfield
= nasm_regvals
[opx
->basereg
];
1845 /* rfield is constant */
1850 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1852 errfunc(ERR_NONFATAL
, "invalid effective address");
1857 *p
++ = ea_data
.modrm
;
1858 if (ea_data
.sib_present
)
1861 /* DREX suffixes come between the SIB and the displacement */
1862 if (ins
->rex
& REX_D
) {
1863 *p
++ = (ins
->drexdst
<< 4) |
1864 (ins
->rex
& REX_OC
? 0x08 : 0) |
1865 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1870 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1873 * Make sure the address gets the right offset in case
1874 * the line breaks in the .lst file (BR 1197827)
1879 switch (ea_data
.bytes
) {
1887 warn_overflow(ea_data
.bytes
, opy
);
1890 if (opy
->segment
== segment
) {
1892 out(offset
, segment
, &data
, OUT_ADDRESS
,
1893 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1895 out(offset
, segment
, &data
, OUT_REL4ADR
,
1896 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1900 out(offset
, segment
, &data
, OUT_ADDRESS
,
1901 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1907 "Invalid amount of bytes (%d) for offset?!",
1916 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1917 ": instruction code \\%o (0x%02X) given", c
, c
);
1923 static int32_t regflag(const operand
* o
)
1925 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1926 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1928 return nasm_reg_flags
[o
->basereg
];
1931 static int32_t regval(const operand
* o
)
1933 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1934 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1936 return nasm_regvals
[o
->basereg
];
1939 static int op_rexflags(const operand
* o
, int mask
)
1944 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1945 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1948 flags
= nasm_reg_flags
[o
->basereg
];
1949 val
= nasm_regvals
[o
->basereg
];
1951 return rexflags(val
, flags
, mask
);
1954 static int rexflags(int val
, int32_t flags
, int mask
)
1959 rex
|= REX_B
|REX_X
|REX_R
;
1962 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1964 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1970 static int matches(const struct itemplate
*itemp
, insn
* instruction
, int bits
)
1972 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1979 if (itemp
->opcode
!= instruction
->opcode
)
1983 * Count the operands
1985 if (itemp
->operands
!= instruction
->operands
)
1989 * Check that no spurious colons or TOs are present
1991 for (i
= 0; i
< itemp
->operands
; i
++)
1992 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
1996 * Process size flags
1998 if (itemp
->flags
& IF_ARMASK
) {
1999 memset(size
, 0, sizeof size
);
2001 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2003 switch (itemp
->flags
& IF_SMASK
) {
2040 switch (itemp
->flags
& IF_SMASK
) {
2075 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2080 * Check that the operand flags all match up
2082 for (i
= 0; i
< itemp
->operands
; i
++) {
2083 int32_t type
= instruction
->oprs
[i
].type
;
2084 if (!(type
& SIZE_MASK
))
2087 if (itemp
->opd
[i
] & SAME_AS
) {
2088 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2089 if (type
!= instruction
->oprs
[j
].type
||
2090 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2092 } else if (itemp
->opd
[i
] & ~type
||
2093 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2094 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2095 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2104 * Check operand sizes
2106 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2107 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2109 for (i
= 0; i
< oprs
; i
++) {
2110 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2112 for (j
= 0; j
< oprs
; j
++)
2118 oprs
= itemp
->operands
;
2121 for (i
= 0; i
< itemp
->operands
; i
++) {
2122 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2123 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2128 * Check template is okay at the set cpu level
2130 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2134 * Verify the appropriate long mode flag.
2136 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2140 * Check if special handling needed for Jumps
2142 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2148 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2149 int addrbits
, int rfield
, int32_t rflags
)
2151 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2153 output
->rip
= false;
2155 /* REX flags for the rfield operand */
2156 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2158 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2162 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2163 || input
->basereg
>= REG_ENUM_LIMIT
)
2166 i
= nasm_regvals
[input
->basereg
];
2169 return NULL
; /* Invalid EA register */
2171 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2173 output
->sib_present
= false; /* no SIB necessary */
2174 output
->bytes
= 0; /* no offset necessary either */
2175 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2176 } else { /* it's a memory reference */
2177 if (input
->basereg
== -1
2178 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2179 /* it's a pure offset */
2180 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2181 int scale
, index
, base
;
2182 output
->sib_present
= true;
2186 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2188 output
->modrm
= 4 | ((rfield
& 7) << 3);
2189 output
->rip
= false;
2191 output
->sib_present
= false;
2192 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2193 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2194 output
->rip
= bits
== 64;
2196 } else { /* it's an indirection */
2197 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2198 int32_t o
= input
->offset
, seg
= input
->segment
;
2199 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2202 int32_t ix
, bx
; /* register flags */
2205 i
= -1; /* make this easy, at least */
2207 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2208 it
= nasm_regvals
[i
];
2209 ix
= nasm_reg_flags
[i
];
2215 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2216 bt
= nasm_regvals
[b
];
2217 bx
= nasm_reg_flags
[b
];
2223 /* check for a 32/64-bit memory reference... */
2224 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2225 /* it must be a 32/64-bit memory reference. Firstly we have
2226 * to check that all registers involved are type E/Rxx. */
2227 int32_t sok
= BITS32
|BITS64
;
2230 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2238 return NULL
; /* Invalid register */
2239 if (~sok
& bx
& SIZE_MASK
)
2240 return NULL
; /* Invalid size */
2244 /* While we're here, ensure the user didn't specify
2246 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2249 if (addrbits
== 16 ||
2250 (addrbits
== 32 && !(sok
& BITS32
)) ||
2251 (addrbits
== 64 && !(sok
& BITS64
)))
2254 /* now reorganize base/index */
2255 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2256 ((hb
== b
&& ht
== EAH_NOTBASE
)
2257 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2258 /* swap if hints say so */
2259 t
= bt
, bt
= it
, it
= t
;
2260 t
= bx
, bx
= ix
, ix
= t
;
2262 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2263 bt
= -1, bx
= 0, s
++;
2264 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2265 /* make single reg base, unless hint */
2266 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2268 if (((s
== 2 && it
!= REG_NUM_ESP
2269 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2270 || s
== 5 || s
== 9) && bt
== -1)
2271 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2272 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2273 && (input
->eaflags
& EAF_TIMESTWO
))
2274 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2275 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2276 if (s
== 1 && it
== REG_NUM_ESP
) {
2277 /* swap ESP into base if scale is 1 */
2278 t
= it
, it
= bt
, bt
= t
;
2279 t
= ix
, ix
= bx
, bx
= t
;
2281 if (it
== REG_NUM_ESP
2282 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2283 return NULL
; /* wrong, for various reasons */
2285 output
->rex
|= rexflags(it
, ix
, REX_X
);
2286 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2288 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2297 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2298 seg
== NO_SEG
&& !forw_ref
&&
2300 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2302 else if (input
->eaflags
& EAF_BYTEOFFS
||
2303 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2305 && !(input
->eaflags
& EAF_WORDOFFS
)))
2311 output
->sib_present
= false;
2312 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2313 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2316 int mod
, scale
, index
, base
;
2336 default: /* then what the smeg is it? */
2337 return NULL
; /* panic */
2345 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2346 seg
== NO_SEG
&& !forw_ref
&&
2348 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2350 else if (input
->eaflags
& EAF_BYTEOFFS
||
2351 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2353 && !(input
->eaflags
& EAF_WORDOFFS
)))
2359 output
->sib_present
= true;
2360 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2361 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2362 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2364 } else { /* it's 16-bit */
2367 /* check for 64-bit long mode */
2371 /* check all registers are BX, BP, SI or DI */
2372 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2373 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2374 && i
!= R_SI
&& i
!= R_DI
))
2377 /* ensure the user didn't specify DWORD/QWORD */
2378 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2381 if (s
!= 1 && i
!= -1)
2382 return NULL
; /* no can do, in 16-bit EA */
2383 if (b
== -1 && i
!= -1) {
2388 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2393 /* have BX/BP as base, SI/DI index */
2395 return NULL
; /* shouldn't ever happen, in theory */
2396 if (i
!= -1 && b
!= -1 &&
2397 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2398 return NULL
; /* invalid combinations */
2399 if (b
== -1) /* pure offset: handled above */
2400 return NULL
; /* so if it gets to here, panic! */
2404 switch (i
* 256 + b
) {
2405 case R_SI
* 256 + R_BX
:
2408 case R_DI
* 256 + R_BX
:
2411 case R_SI
* 256 + R_BP
:
2414 case R_DI
* 256 + R_BP
:
2432 if (rm
== -1) /* can't happen, in theory */
2433 return NULL
; /* so panic if it does */
2435 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2436 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2438 else if (input
->eaflags
& EAF_BYTEOFFS
||
2439 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2441 && !(input
->eaflags
& EAF_WORDOFFS
)))
2446 output
->sib_present
= false; /* no SIB - it's 16-bit */
2447 output
->bytes
= mod
; /* bytes of offset needed */
2448 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2453 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2457 static void add_asp(insn
*ins
, int addrbits
)
2462 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2464 switch (ins
->prefixes
[PPS_ASIZE
]) {
2475 valid
&= (addrbits
== 32) ? 16 : 32;
2481 for (j
= 0; j
< ins
->operands
; j
++) {
2482 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2485 /* Verify as Register */
2486 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2487 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2490 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2492 /* Verify as Register */
2493 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2494 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2497 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2499 if (ins
->oprs
[j
].scale
== 0)
2503 int ds
= ins
->oprs
[j
].disp_size
;
2504 if ((addrbits
!= 64 && ds
> 8) ||
2505 (addrbits
== 64 && ds
== 16))
2525 if (valid
& addrbits
) {
2526 ins
->addr_size
= addrbits
;
2527 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2528 /* Add an address size prefix */
2529 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2530 ins
->prefixes
[PPS_ASIZE
] = pref
;
2531 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2534 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2535 ins
->addr_size
= addrbits
; /* Error recovery */
2538 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2540 for (j
= 0; j
< ins
->operands
; j
++) {
2541 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2542 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2543 != ins
->addr_size
) {
2544 /* mem_offs sizes must match the address size; if not,
2545 strip the MEM_OFFS bit and match only EA instructions */
2546 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);