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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * assemble.c code generation for the Netwide Assembler
49 * the actual codes (C syntax, i.e. octal):
50 * \0 - terminates the code. (Unless it's a literal of course.)
51 * \1..\4 - that many literal bytes follow in the code stream
52 * \5 - add 4 to the primary operand number (b, low octdigit)
53 * \6 - add 4 to the secondary operand number (a, middle octdigit)
54 * \7 - add 4 to both the primary and the secondary operand number
55 * \10..\13 - a literal byte follows in the code stream, to be added
56 * to the register value of operand 0..3
57 * \14..\17 - a signed byte immediate operand, from operand 0..3
58 * \20..\23 - a byte immediate operand, from operand 0..3
59 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
60 * \30..\33 - a word immediate operand, from operand 0..3
61 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
62 * assembly mode or the operand-size override on the operand
63 * \40..\43 - a long immediate operand, from operand 0..3
64 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
65 * depending on the address size of the instruction.
66 * \50..\53 - a byte relative operand, from operand 0..3
67 * \54..\57 - a qword immediate operand, from operand 0..3
68 * \60..\63 - a word relative operand, from operand 0..3
69 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
70 * assembly mode or the operand-size override on the operand
71 * \70..\73 - a long relative operand, from operand 0..3
72 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
73 * \1ab - a ModRM, calculated on EA in operand a, with the spare
74 * field the register value of operand b.
75 * \140..\143 - an immediate word or signed byte for operand 0..3
76 * \144..\147 - or 2 (s-field) into opcode byte if operand 0..3
77 * is a signed byte rather than a word. Opcode byte follows.
78 * \150..\153 - an immediate dword or signed byte for operand 0..3
79 * \154..\157 - or 2 (s-field) into opcode byte if operand 0..3
80 * is a signed byte rather than a dword. Opcode byte follows.
81 * \160..\163 - this instruction uses DREX rather than REX, with the
82 * OC0 field set to 0, and the dest field taken from
84 * \164..\167 - this instruction uses DREX rather than REX, with the
85 * OC0 field set to 1, and the dest field taken from
87 * \171 - placement of DREX suffix in the absence of an EA
88 * \172\ab - the register number from operand a in bits 7..4, with
89 * the 4-bit immediate from operand b in bits 3..0.
90 * \173\xab - the register number from operand a in bits 7..4, with
91 * the value b in bits 3..0.
92 * \174\a - the register number from operand a in bits 7..4, and
93 * an arbitrary value in bits 3..0 (assembled as zero.)
94 * \2ab - a ModRM, calculated on EA in operand a, with the spare
95 * field equal to digit b.
96 * \250..\253 - same as \150..\153, except warn if the 64-bit operand
97 * is not equal to the truncated and sign-extended 32-bit
98 * operand; used for 32-bit immediates in 64-bit mode.
99 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
100 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
101 * V field taken from operand 0..3.
102 * \270 - this instruction uses VEX/XOP rather than REX, with the
103 * V field set to 1111b.
105 * VEX/XOP prefixes are followed by the sequence:
106 * \tmm\wlp where mm is the M field; and wlp is:
108 * [w0] ww = 0 for W = 0
109 * [w1] ww = 1 for W = 1
110 * [wx] ww = 2 for W don't care (always assembled as 0)
111 * [ww] ww = 3 for W used as REX.W
113 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
115 * \274..\277 - a signed byte immediate operand, from operand 0..3,
116 * which is to be extended to the operand size.
117 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
118 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
119 * \312 - (disassembler only) invalid with non-default address size.
120 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
121 * \314 - (disassembler only) invalid with REX.B
122 * \315 - (disassembler only) invalid with REX.X
123 * \316 - (disassembler only) invalid with REX.R
124 * \317 - (disassembler only) invalid with REX.W
125 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
126 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
127 * \322 - indicates that this instruction is only valid when the
128 * operand size is the default (instruction to disassembler,
129 * generates no code in the assembler)
130 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
131 * \324 - indicates 64-bit operand size requiring REX prefix.
132 * \325 - instruction which always uses spl/bpl/sil/dil
133 * \330 - a literal byte follows in the code stream, to be added
134 * to the condition code value of the instruction.
135 * \331 - instruction not valid with REP prefix. Hint for
136 * disassembler only; for SSE instructions.
137 * \332 - REP prefix (0xF2 byte) used as opcode extension.
138 * \333 - REP prefix (0xF3 byte) used as opcode extension.
139 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
140 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
141 * \336 - force a REP(E) prefix (0xF2) even if not specified.
142 * \337 - force a REPNE prefix (0xF3) even if not specified.
143 * \336-\337 are still listed as prefixes in the disassembler.
144 * \340 - reserve <operand 0> bytes of uninitialized storage.
145 * Operand 0 had better be a segmentless constant.
146 * \341 - this instruction needs a WAIT "prefix"
147 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
148 * (POP is never used for CS) depending on operand 0
149 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
151 * \360 - no SSE prefix (== \364\331)
152 * \361 - 66 SSE prefix (== \366\331)
153 * \362 - F2 SSE prefix (== \364\332)
154 * \363 - F3 SSE prefix (== \364\333)
155 * \364 - operand-size prefix (0x66) not permitted
156 * \365 - address-size prefix (0x67) not permitted
157 * \366 - operand-size prefix (0x66) used as opcode extension
158 * \367 - address-size prefix (0x67) used as opcode extension
159 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
160 * 370 is used for Jcc, 371 is used for JMP.
161 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
162 * used for conditional jump over longer jump
165 #include "compiler.h"
169 #include <inttypes.h>
173 #include "assemble.h"
178 int sib_present
; /* is a SIB byte necessary? */
179 int bytes
; /* # of bytes of offset needed */
180 int size
; /* lazy - this is sib+bytes+1 */
181 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
184 static uint32_t cpu
; /* cpu level received from nasm.c */
185 static efunc errfunc
;
186 static struct ofmt
*outfmt
;
187 static ListGen
*list
;
189 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
190 static void gencode(int32_t segment
, int64_t offset
, int bits
,
191 insn
* ins
, const struct itemplate
*temp
,
193 static int matches(const struct itemplate
*, insn
*, int bits
);
194 static int32_t regflag(const operand
*);
195 static int32_t regval(const operand
*);
196 static int rexflags(int, int32_t, int);
197 static int op_rexflags(const operand
*, int);
198 static ea
*process_ea(operand
*, ea
*, int, int, int, int32_t);
199 static void add_asp(insn
*, int);
201 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
203 return ins
->prefixes
[pos
] == prefix
;
206 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
208 if (ins
->prefixes
[pos
])
209 errfunc(ERR_NONFATAL
, "invalid %s prefix",
210 prefix_name(ins
->prefixes
[pos
]));
213 static const char *size_name(int size
)
235 static void warn_overflow(int size
, const struct operand
*o
)
237 if (size
< 8 && o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
238 int64_t lim
= ((int64_t)1 << (size
*8))-1;
239 int64_t data
= o
->offset
;
241 if (data
< ~lim
|| data
> lim
)
242 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
243 "%s data exceeds bounds", size_name(size
));
247 * This routine wrappers the real output format's output routine,
248 * in order to pass a copy of the data off to the listing file
249 * generator at the same time.
251 static void out(int64_t offset
, int32_t segto
, const void *data
,
252 enum out_type type
, uint64_t size
,
253 int32_t segment
, int32_t wrt
)
255 static int32_t lineno
= 0; /* static!!! */
256 static char *lnfname
= NULL
;
259 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
261 * This is a non-relocated address, and we're going to
262 * convert it into RAWDATA format.
267 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
271 WRITEADDR(q
, *(int64_t *)data
, size
);
276 list
->output(offset
, data
, type
, size
);
279 * this call to src_get determines when we call the
280 * debug-format-specific "linenum" function
281 * it updates lineno and lnfname to the current values
282 * returning 0 if "same as last time", -2 if lnfname
283 * changed, and the amount by which lineno changed,
284 * if it did. thus, these variables must be static
287 if (src_get(&lineno
, &lnfname
)) {
288 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
291 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
294 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
295 insn
* ins
, const uint8_t *code
)
300 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
304 if (optimizing
< 0 && c
== 0371)
307 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
309 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
310 /* Be optimistic in pass 1 */
313 if (ins
->oprs
[0].segment
!= segment
)
316 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
317 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
320 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
321 insn
* instruction
, struct ofmt
*output
, efunc error
,
324 const struct itemplate
*temp
;
329 int64_t start
= offset
;
330 int64_t wsize
= 0; /* size for DB etc. */
332 errfunc
= error
; /* to pass to other functions */
334 outfmt
= output
; /* likewise */
335 list
= listgen
; /* and again */
337 switch (instruction
->opcode
) {
367 int32_t t
= instruction
->times
;
370 "instruction->times < 0 (%ld) in assemble()", t
);
372 while (t
--) { /* repeat TIMES times */
373 for (e
= instruction
->eops
; e
; e
= e
->next
) {
374 if (e
->type
== EOT_DB_NUMBER
) {
376 if (e
->segment
!= NO_SEG
)
377 errfunc(ERR_NONFATAL
,
378 "one-byte relocation attempted");
380 uint8_t out_byte
= e
->offset
;
381 out(offset
, segment
, &out_byte
,
382 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
384 } else if (wsize
> 8) {
385 errfunc(ERR_NONFATAL
,
386 "integer supplied to a DT, DO or DY"
389 out(offset
, segment
, &e
->offset
,
390 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
392 } else if (e
->type
== EOT_DB_STRING
||
393 e
->type
== EOT_DB_STRING_FREE
) {
396 out(offset
, segment
, e
->stringval
,
397 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
398 align
= e
->stringlen
% wsize
;
401 align
= wsize
- align
;
402 out(offset
, segment
, zero_buffer
,
403 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
405 offset
+= e
->stringlen
+ align
;
408 if (t
> 0 && t
== instruction
->times
- 1) {
410 * Dummy call to list->output to give the offset to the
413 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
414 list
->uplevel(LIST_TIMES
);
417 if (instruction
->times
> 1)
418 list
->downlevel(LIST_TIMES
);
419 return offset
- start
;
422 if (instruction
->opcode
== I_INCBIN
) {
423 const char *fname
= instruction
->eops
->stringval
;
426 fp
= fopen(fname
, "rb");
428 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
430 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
431 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
434 static char buf
[4096];
435 size_t t
= instruction
->times
;
440 if (instruction
->eops
->next
) {
441 base
= instruction
->eops
->next
->offset
;
443 if (instruction
->eops
->next
->next
&&
444 len
> (size_t)instruction
->eops
->next
->next
->offset
)
445 len
= (size_t)instruction
->eops
->next
->next
->offset
;
448 * Dummy call to list->output to give the offset to the
451 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
452 list
->uplevel(LIST_INCBIN
);
456 fseek(fp
, base
, SEEK_SET
);
460 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
463 * This shouldn't happen unless the file
464 * actually changes while we are reading
468 "`incbin': unexpected EOF while"
469 " reading file `%s'", fname
);
470 t
= 0; /* Try to exit cleanly */
473 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
478 list
->downlevel(LIST_INCBIN
);
479 if (instruction
->times
> 1) {
481 * Dummy call to list->output to give the offset to the
484 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
485 list
->uplevel(LIST_TIMES
);
486 list
->downlevel(LIST_TIMES
);
489 return instruction
->times
* len
;
491 return 0; /* if we're here, there's an error */
494 /* Check to see if we need an address-size prefix */
495 add_asp(instruction
, bits
);
499 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++){
500 int m
= matches(temp
, instruction
, bits
);
502 (m
== 99 && jmp_match(segment
, offset
, bits
,
503 instruction
, temp
->code
))) {
505 int64_t insn_size
= calcsize(segment
, offset
, bits
,
506 instruction
, temp
->code
);
507 itimes
= instruction
->times
;
508 if (insn_size
< 0) /* shouldn't be, on pass two */
509 error(ERR_PANIC
, "errors made it through from pass one");
512 for (j
= 0; j
< MAXPREFIX
; j
++) {
514 switch (instruction
->prefixes
[j
]) {
532 error(ERR_WARNING
| ERR_PASS2
,
533 "cs segment base generated, but will be ignored in 64-bit mode");
539 error(ERR_WARNING
| ERR_PASS2
,
540 "ds segment base generated, but will be ignored in 64-bit mode");
546 error(ERR_WARNING
| ERR_PASS2
,
547 "es segment base generated, but will be ignored in 64-bit mode");
559 error(ERR_WARNING
| ERR_PASS2
,
560 "ss segment base generated, but will be ignored in 64-bit mode");
567 "segr6 and segr7 cannot be used as prefixes");
572 "16-bit addressing is not supported "
574 } else if (bits
!= 16)
584 "64-bit addressing is only supported "
608 error(ERR_PANIC
, "invalid instruction prefix");
611 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
616 insn_end
= offset
+ insn_size
;
617 gencode(segment
, offset
, bits
, instruction
,
620 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
622 * Dummy call to list->output to give the offset to the
625 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
626 list
->uplevel(LIST_TIMES
);
629 if (instruction
->times
> 1)
630 list
->downlevel(LIST_TIMES
);
631 return offset
- start
;
632 } else if (m
> 0 && m
> size_prob
) {
637 if (temp
->opcode
== -1) { /* didn't match any instruction */
640 error(ERR_NONFATAL
, "operation size not specified");
643 error(ERR_NONFATAL
, "mismatch in operand sizes");
646 error(ERR_NONFATAL
, "no instruction for this cpu level");
649 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
654 "invalid combination of opcode and operands");
661 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
662 insn
* instruction
, efunc error
)
664 const struct itemplate
*temp
;
666 errfunc
= error
; /* to pass to other functions */
669 if (instruction
->opcode
== -1)
672 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
673 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
674 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
675 instruction
->opcode
== I_DY
) {
677 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
680 switch (instruction
->opcode
) {
706 for (e
= instruction
->eops
; e
; e
= e
->next
) {
710 if (e
->type
== EOT_DB_NUMBER
)
712 else if (e
->type
== EOT_DB_STRING
||
713 e
->type
== EOT_DB_STRING_FREE
)
714 osize
= e
->stringlen
;
716 align
= (-osize
) % wsize
;
719 isize
+= osize
+ align
;
721 return isize
* instruction
->times
;
724 if (instruction
->opcode
== I_INCBIN
) {
725 const char *fname
= instruction
->eops
->stringval
;
729 fp
= fopen(fname
, "rb");
731 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
733 else if (fseek(fp
, 0L, SEEK_END
) < 0)
734 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
739 if (instruction
->eops
->next
) {
740 len
-= instruction
->eops
->next
->offset
;
741 if (instruction
->eops
->next
->next
&&
742 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
743 len
= (size_t)instruction
->eops
->next
->next
->offset
;
746 return instruction
->times
* len
;
748 return 0; /* if we're here, there's an error */
751 /* Check to see if we need an address-size prefix */
752 add_asp(instruction
, bits
);
754 for (temp
= nasm_instructions
[instruction
->opcode
]; temp
->opcode
!= -1; temp
++) {
755 int m
= matches(temp
, instruction
, bits
);
757 (m
== 99 && jmp_match(segment
, offset
, bits
,
758 instruction
, temp
->code
))) {
759 /* we've matched an instruction. */
761 const uint8_t *codes
= temp
->code
;
764 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
767 for (j
= 0; j
< MAXPREFIX
; j
++) {
768 switch (instruction
->prefixes
[j
]) {
794 return isize
* instruction
->times
;
797 return -1; /* didn't match any instruction */
800 static bool possible_sbyte(operand
*o
)
802 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
803 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
804 optimizing
>= 0 && !(o
->type
& STRICT
);
807 /* check that opn[op] is a signed byte of size 16 or 32 */
808 static bool is_sbyte16(operand
*o
)
812 if (!possible_sbyte(o
))
816 return v
>= -128 && v
<= 127;
819 static bool is_sbyte32(operand
*o
)
823 if (!possible_sbyte(o
))
827 return v
>= -128 && v
<= 127;
830 /* Common construct */
831 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
833 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
834 insn
* ins
, const uint8_t *codes
)
843 ins
->rex
= 0; /* Ensure REX is reset */
845 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
848 (void)segment
; /* Don't warn that this parameter is unused */
849 (void)offset
; /* Don't warn that this parameter is unused */
853 op1
= (c
& 3) + ((opex
& 1) << 2);
854 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
855 opx
= &ins
->oprs
[op1
];
856 opex
= 0; /* For the next iteration */
863 codes
+= c
, length
+= c
;
874 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
889 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
890 length
+= (opx
->type
& BITS16
) ? 2 : 4;
892 length
+= (bits
== 16) ? 2 : 4;
900 length
+= ins
->addr_size
>> 3;
908 length
+= 8; /* MOV reg64/imm */
916 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
917 length
+= (opx
->type
& BITS16
) ? 2 : 4;
919 length
+= (bits
== 16) ? 2 : 4;
931 length
+= is_sbyte16(opx
) ? 1 : 2;
940 length
+= is_sbyte32(opx
) ? 1 : 4;
951 ins
->drexdst
= regval(opx
);
956 ins
->rex
|= REX_D
|REX_OC
;
957 ins
->drexdst
= regval(opx
);
971 length
+= is_sbyte32(opx
) ? 1 : 4;
980 ins
->drexdst
= regval(opx
);
981 ins
->vex_cm
= *codes
++;
982 ins
->vex_wlp
= *codes
++;
988 ins
->vex_cm
= *codes
++;
989 ins
->vex_wlp
= *codes
++;
1002 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1006 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1013 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1014 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1022 length
+= (bits
!= 16);
1026 length
+= (bits
== 16);
1064 if (!ins
->prefixes
[PPS_LREP
])
1065 ins
->prefixes
[PPS_LREP
] = P_REP
;
1069 if (!ins
->prefixes
[PPS_LREP
])
1070 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1074 if (ins
->oprs
[0].segment
!= NO_SEG
)
1075 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1076 " quantity of BSS space");
1078 length
+= ins
->oprs
[0].offset
;
1082 if (!ins
->prefixes
[PPS_WAIT
])
1083 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1133 struct operand
*opy
= &ins
->oprs
[op2
];
1135 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1138 /* pick rfield from operand b (opx) */
1139 rflags
= regflag(opx
);
1140 rfield
= nasm_regvals
[opx
->basereg
];
1145 if (!process_ea(opy
, &ea_data
, bits
,
1146 ins
->addr_size
, rfield
, rflags
)) {
1147 errfunc(ERR_NONFATAL
, "invalid effective address");
1150 ins
->rex
|= ea_data
.rex
;
1151 length
+= ea_data
.size
;
1157 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1158 ": instruction code \\%o (0x%02X) given", c
, c
);
1163 ins
->rex
&= rex_mask
;
1165 if (ins
->rex
& REX_NH
) {
1166 if (ins
->rex
& REX_H
) {
1167 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1170 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1173 if (ins
->rex
& REX_V
) {
1174 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1176 if (ins
->rex
& REX_H
) {
1177 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1180 switch (ins
->vex_wlp
& 030) {
1194 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1195 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1198 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1202 } else if (ins
->rex
& REX_D
) {
1203 if (ins
->rex
& REX_H
) {
1204 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1207 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1208 ins
->drexdst
> 7)) {
1209 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1213 } else if (ins
->rex
& REX_REAL
) {
1214 if (ins
->rex
& REX_H
) {
1215 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1217 } else if (bits
== 64) {
1219 } else if ((ins
->rex
& REX_L
) &&
1220 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1223 assert_no_prefix(ins
, PPS_LREP
);
1226 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1234 #define EMIT_REX() \
1235 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1236 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1237 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1242 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1243 insn
* ins
, const struct itemplate
*temp
,
1246 static char condval
[] = { /* conditional opcodes */
1247 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1248 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1249 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1256 struct operand
*opx
;
1257 const uint8_t *codes
= temp
->code
;
1262 op1
= (c
& 3) + ((opex
& 1) << 2);
1263 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1264 opx
= &ins
->oprs
[op1
];
1265 opex
= 0; /* For the next iteration */
1273 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1286 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1287 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1292 /* The test for BITS8 and SBYTE here is intended to avoid
1293 warning on optimizer actions due to SBYTE, while still
1294 warn on explicit BYTE directives. Also warn, obviously,
1295 if the optimizer isn't enabled. */
1296 if (((opx
->type
& BITS8
) ||
1297 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1298 (opx
->offset
< -128 || opx
->offset
> 127)) {
1299 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1300 "signed byte value exceeds bounds");
1302 if (opx
->segment
!= NO_SEG
) {
1304 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1305 opx
->segment
, opx
->wrt
);
1307 bytes
[0] = opx
->offset
;
1308 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1315 if (opx
->offset
< -256 || opx
->offset
> 255) {
1316 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1317 "byte value exceeds bounds");
1319 if (opx
->segment
!= NO_SEG
) {
1321 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1322 opx
->segment
, opx
->wrt
);
1324 bytes
[0] = opx
->offset
;
1325 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1332 if (opx
->offset
< 0 || opx
->offset
> 255)
1333 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1334 "unsigned byte value exceeds bounds");
1335 if (opx
->segment
!= NO_SEG
) {
1337 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1338 opx
->segment
, opx
->wrt
);
1340 bytes
[0] = opx
->offset
;
1341 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1348 warn_overflow(2, opx
);
1350 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1351 opx
->segment
, opx
->wrt
);
1356 if (opx
->type
& (BITS16
| BITS32
))
1357 size
= (opx
->type
& BITS16
) ? 2 : 4;
1359 size
= (bits
== 16) ? 2 : 4;
1360 warn_overflow(size
, opx
);
1362 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1363 opx
->segment
, opx
->wrt
);
1368 warn_overflow(4, opx
);
1370 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1371 opx
->segment
, opx
->wrt
);
1377 size
= ins
->addr_size
>> 3;
1378 warn_overflow(size
, opx
);
1379 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1380 opx
->segment
, opx
->wrt
);
1385 if (opx
->segment
!= segment
)
1386 errfunc(ERR_NONFATAL
,
1387 "short relative jump outside segment");
1388 data
= opx
->offset
- insn_end
;
1389 if (data
> 127 || data
< -128)
1390 errfunc(ERR_NONFATAL
, "short jump is out of range");
1392 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1397 data
= (int64_t)opx
->offset
;
1398 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1399 opx
->segment
, opx
->wrt
);
1404 if (opx
->segment
!= segment
) {
1406 out(offset
, segment
, &data
,
1407 OUT_REL2ADR
, insn_end
- offset
,
1408 opx
->segment
, opx
->wrt
);
1410 data
= opx
->offset
- insn_end
;
1411 out(offset
, segment
, &data
,
1412 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1418 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1419 size
= (opx
->type
& BITS16
) ? 2 : 4;
1421 size
= (bits
== 16) ? 2 : 4;
1422 if (opx
->segment
!= segment
) {
1424 out(offset
, segment
, &data
,
1425 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1426 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1428 data
= opx
->offset
- insn_end
;
1429 out(offset
, segment
, &data
,
1430 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1436 if (opx
->segment
!= segment
) {
1438 out(offset
, segment
, &data
,
1439 OUT_REL4ADR
, insn_end
- offset
,
1440 opx
->segment
, opx
->wrt
);
1442 data
= opx
->offset
- insn_end
;
1443 out(offset
, segment
, &data
,
1444 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1450 if (opx
->segment
== NO_SEG
)
1451 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1454 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1455 outfmt
->segbase(1 + opx
->segment
),
1462 warn_overflow(2, opx
);
1463 if (is_sbyte16(opx
)) {
1465 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1469 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1470 opx
->segment
, opx
->wrt
);
1477 bytes
[0] = *codes
++;
1478 if (is_sbyte16(opx
))
1479 bytes
[0] |= 2; /* s-bit */
1480 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1486 warn_overflow(4, opx
);
1487 if (is_sbyte32(opx
)) {
1489 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1493 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1494 opx
->segment
, opx
->wrt
);
1501 bytes
[0] = *codes
++;
1502 if (is_sbyte32(opx
))
1503 bytes
[0] |= 2; /* s-bit */
1504 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1514 (ins
->drexdst
<< 4) |
1515 (ins
->rex
& REX_OC
? 0x08 : 0) |
1516 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1518 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1524 opx
= &ins
->oprs
[c
>> 3];
1525 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1526 opx
= &ins
->oprs
[c
& 7];
1527 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1528 errfunc(ERR_NONFATAL
,
1529 "non-absolute expression not permitted as argument %d",
1532 if (opx
->offset
& ~15) {
1533 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1534 "four-bit argument exceeds bounds");
1536 bytes
[0] |= opx
->offset
& 15;
1538 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1544 opx
= &ins
->oprs
[c
>> 4];
1545 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1547 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1553 opx
= &ins
->oprs
[c
];
1554 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1555 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1561 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1562 (int32_t)data
!= (int64_t)data
) {
1563 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1564 "signed dword immediate exceeds bounds");
1566 if (is_sbyte32(opx
)) {
1568 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1572 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1573 opx
->segment
, opx
->wrt
);
1580 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1581 (int32_t)data
!= (int64_t)data
) {
1582 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1583 "signed dword immediate exceeds bounds");
1585 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1586 opx
->segment
, opx
->wrt
);
1593 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1594 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1595 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1596 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1597 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1598 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1602 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1603 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1604 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1614 if (ins
->rex
& REX_W
)
1616 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1618 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1623 um
= (uint64_t)2 << (s
-1);
1626 if (uv
> 127 && uv
< (uint64_t)-128 &&
1627 (uv
< um
-128 || uv
> um
-1)) {
1628 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1629 "signed byte value exceeds bounds");
1631 if (opx
->segment
!= NO_SEG
) {
1633 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1634 opx
->segment
, opx
->wrt
);
1637 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1648 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1650 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1657 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1659 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1678 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1687 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1705 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1706 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1715 *bytes
= c
- 0332 + 0xF2;
1716 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1721 if (ins
->rex
& REX_R
) {
1723 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1726 ins
->rex
&= ~(REX_L
|REX_R
);
1737 if (ins
->oprs
[0].segment
!= NO_SEG
)
1738 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1740 int64_t size
= ins
->oprs
[0].offset
;
1742 out(offset
, segment
, NULL
,
1743 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1754 switch (ins
->oprs
[0].basereg
) {
1769 "bizarre 8086 segment register received");
1771 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1778 switch (ins
->oprs
[0].basereg
) {
1787 "bizarre 386 segment register received");
1789 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1798 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1804 bytes
[0] = c
- 0362 + 0xf2;
1805 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1815 *bytes
= c
- 0366 + 0x66;
1816 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1826 *bytes
= bits
== 16 ? 3 : 5;
1827 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1850 struct operand
*opy
= &ins
->oprs
[op2
];
1853 /* pick rfield from operand b (opx) */
1854 rflags
= regflag(opx
);
1855 rfield
= nasm_regvals
[opx
->basereg
];
1857 /* rfield is constant */
1862 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1864 errfunc(ERR_NONFATAL
, "invalid effective address");
1869 *p
++ = ea_data
.modrm
;
1870 if (ea_data
.sib_present
)
1873 /* DREX suffixes come between the SIB and the displacement */
1874 if (ins
->rex
& REX_D
) {
1875 *p
++ = (ins
->drexdst
<< 4) |
1876 (ins
->rex
& REX_OC
? 0x08 : 0) |
1877 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1882 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1885 * Make sure the address gets the right offset in case
1886 * the line breaks in the .lst file (BR 1197827)
1891 switch (ea_data
.bytes
) {
1899 warn_overflow(ea_data
.bytes
, opy
);
1902 if (opy
->segment
== segment
) {
1904 out(offset
, segment
, &data
, OUT_ADDRESS
,
1905 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1907 out(offset
, segment
, &data
, OUT_REL4ADR
,
1908 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1912 out(offset
, segment
, &data
, OUT_ADDRESS
,
1913 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1919 "Invalid amount of bytes (%d) for offset?!",
1928 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1929 ": instruction code \\%o (0x%02X) given", c
, c
);
1935 static int32_t regflag(const operand
* o
)
1937 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1938 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1940 return nasm_reg_flags
[o
->basereg
];
1943 static int32_t regval(const operand
* o
)
1945 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1946 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1948 return nasm_regvals
[o
->basereg
];
1951 static int op_rexflags(const operand
* o
, int mask
)
1956 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1957 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1960 flags
= nasm_reg_flags
[o
->basereg
];
1961 val
= nasm_regvals
[o
->basereg
];
1963 return rexflags(val
, flags
, mask
);
1966 static int rexflags(int val
, int32_t flags
, int mask
)
1971 rex
|= REX_B
|REX_X
|REX_R
;
1974 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1976 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1982 static int matches(const struct itemplate
*itemp
, insn
* instruction
, int bits
)
1984 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1991 if (itemp
->opcode
!= instruction
->opcode
)
1995 * Count the operands
1997 if (itemp
->operands
!= instruction
->operands
)
2001 * Check that no spurious colons or TOs are present
2003 for (i
= 0; i
< itemp
->operands
; i
++)
2004 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2008 * Process size flags
2010 if (itemp
->flags
& IF_ARMASK
) {
2011 memset(size
, 0, sizeof size
);
2013 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2015 switch (itemp
->flags
& IF_SMASK
) {
2052 switch (itemp
->flags
& IF_SMASK
) {
2087 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2092 * Check that the operand flags all match up
2094 for (i
= 0; i
< itemp
->operands
; i
++) {
2095 int32_t type
= instruction
->oprs
[i
].type
;
2096 if (!(type
& SIZE_MASK
))
2099 if (itemp
->opd
[i
] & SAME_AS
) {
2100 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2101 if (type
!= instruction
->oprs
[j
].type
||
2102 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2104 } else if (itemp
->opd
[i
] & ~type
||
2105 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2106 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2107 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2116 * Check operand sizes
2118 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2119 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2121 for (i
= 0; i
< oprs
; i
++) {
2122 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2124 for (j
= 0; j
< oprs
; j
++)
2130 oprs
= itemp
->operands
;
2133 for (i
= 0; i
< itemp
->operands
; i
++) {
2134 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2135 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2140 * Check template is okay at the set cpu level
2142 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2146 * Verify the appropriate long mode flag.
2148 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2152 * Check if special handling needed for Jumps
2154 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2160 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2161 int addrbits
, int rfield
, int32_t rflags
)
2163 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2165 output
->rip
= false;
2167 /* REX flags for the rfield operand */
2168 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2170 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2174 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2175 || input
->basereg
>= REG_ENUM_LIMIT
)
2178 i
= nasm_regvals
[input
->basereg
];
2181 return NULL
; /* Invalid EA register */
2183 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2185 output
->sib_present
= false; /* no SIB necessary */
2186 output
->bytes
= 0; /* no offset necessary either */
2187 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2188 } else { /* it's a memory reference */
2189 if (input
->basereg
== -1
2190 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2191 /* it's a pure offset */
2192 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2193 int scale
, index
, base
;
2194 output
->sib_present
= true;
2198 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2200 output
->modrm
= 4 | ((rfield
& 7) << 3);
2201 output
->rip
= false;
2203 output
->sib_present
= false;
2204 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2205 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2206 output
->rip
= bits
== 64;
2208 } else { /* it's an indirection */
2209 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2210 int32_t o
= input
->offset
, seg
= input
->segment
;
2211 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2214 int32_t ix
, bx
; /* register flags */
2217 i
= -1; /* make this easy, at least */
2219 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2220 it
= nasm_regvals
[i
];
2221 ix
= nasm_reg_flags
[i
];
2227 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2228 bt
= nasm_regvals
[b
];
2229 bx
= nasm_reg_flags
[b
];
2235 /* check for a 32/64-bit memory reference... */
2236 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2237 /* it must be a 32/64-bit memory reference. Firstly we have
2238 * to check that all registers involved are type E/Rxx. */
2239 int32_t sok
= BITS32
|BITS64
;
2242 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2250 return NULL
; /* Invalid register */
2251 if (~sok
& bx
& SIZE_MASK
)
2252 return NULL
; /* Invalid size */
2256 /* While we're here, ensure the user didn't specify
2258 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2261 if (addrbits
== 16 ||
2262 (addrbits
== 32 && !(sok
& BITS32
)) ||
2263 (addrbits
== 64 && !(sok
& BITS64
)))
2266 /* now reorganize base/index */
2267 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2268 ((hb
== b
&& ht
== EAH_NOTBASE
)
2269 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2270 /* swap if hints say so */
2271 t
= bt
, bt
= it
, it
= t
;
2272 t
= bx
, bx
= ix
, ix
= t
;
2274 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2275 bt
= -1, bx
= 0, s
++;
2276 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2277 /* make single reg base, unless hint */
2278 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2280 if (((s
== 2 && it
!= REG_NUM_ESP
2281 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2282 || s
== 5 || s
== 9) && bt
== -1)
2283 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2284 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2285 && (input
->eaflags
& EAF_TIMESTWO
))
2286 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2287 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2288 if (s
== 1 && it
== REG_NUM_ESP
) {
2289 /* swap ESP into base if scale is 1 */
2290 t
= it
, it
= bt
, bt
= t
;
2291 t
= ix
, ix
= bx
, bx
= t
;
2293 if (it
== REG_NUM_ESP
2294 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2295 return NULL
; /* wrong, for various reasons */
2297 output
->rex
|= rexflags(it
, ix
, REX_X
);
2298 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2300 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2309 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2310 seg
== NO_SEG
&& !forw_ref
&&
2312 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2314 else if (input
->eaflags
& EAF_BYTEOFFS
||
2315 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2317 && !(input
->eaflags
& EAF_WORDOFFS
)))
2323 output
->sib_present
= false;
2324 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2325 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2328 int mod
, scale
, index
, base
;
2348 default: /* then what the smeg is it? */
2349 return NULL
; /* panic */
2357 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2358 seg
== NO_SEG
&& !forw_ref
&&
2360 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2362 else if (input
->eaflags
& EAF_BYTEOFFS
||
2363 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2365 && !(input
->eaflags
& EAF_WORDOFFS
)))
2371 output
->sib_present
= true;
2372 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2373 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2374 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2376 } else { /* it's 16-bit */
2379 /* check for 64-bit long mode */
2383 /* check all registers are BX, BP, SI or DI */
2384 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2385 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2386 && i
!= R_SI
&& i
!= R_DI
))
2389 /* ensure the user didn't specify DWORD/QWORD */
2390 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2393 if (s
!= 1 && i
!= -1)
2394 return NULL
; /* no can do, in 16-bit EA */
2395 if (b
== -1 && i
!= -1) {
2400 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2405 /* have BX/BP as base, SI/DI index */
2407 return NULL
; /* shouldn't ever happen, in theory */
2408 if (i
!= -1 && b
!= -1 &&
2409 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2410 return NULL
; /* invalid combinations */
2411 if (b
== -1) /* pure offset: handled above */
2412 return NULL
; /* so if it gets to here, panic! */
2416 switch (i
* 256 + b
) {
2417 case R_SI
* 256 + R_BX
:
2420 case R_DI
* 256 + R_BX
:
2423 case R_SI
* 256 + R_BP
:
2426 case R_DI
* 256 + R_BP
:
2444 if (rm
== -1) /* can't happen, in theory */
2445 return NULL
; /* so panic if it does */
2447 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2448 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2450 else if (input
->eaflags
& EAF_BYTEOFFS
||
2451 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2453 && !(input
->eaflags
& EAF_WORDOFFS
)))
2458 output
->sib_present
= false; /* no SIB - it's 16-bit */
2459 output
->bytes
= mod
; /* bytes of offset needed */
2460 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2465 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2469 static void add_asp(insn
*ins
, int addrbits
)
2474 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2476 switch (ins
->prefixes
[PPS_ASIZE
]) {
2487 valid
&= (addrbits
== 32) ? 16 : 32;
2493 for (j
= 0; j
< ins
->operands
; j
++) {
2494 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2497 /* Verify as Register */
2498 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2499 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2502 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2504 /* Verify as Register */
2505 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2506 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2509 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2511 if (ins
->oprs
[j
].scale
== 0)
2515 int ds
= ins
->oprs
[j
].disp_size
;
2516 if ((addrbits
!= 64 && ds
> 8) ||
2517 (addrbits
== 64 && ds
== 16))
2537 if (valid
& addrbits
) {
2538 ins
->addr_size
= addrbits
;
2539 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2540 /* Add an address size prefix */
2541 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2542 ins
->prefixes
[PPS_ASIZE
] = pref
;
2543 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2546 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2547 ins
->addr_size
= addrbits
; /* Error recovery */
2550 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2552 for (j
= 0; j
< ins
->operands
; j
++) {
2553 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2554 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2555 != ins
->addr_size
) {
2556 /* mem_offs sizes must match the address size; if not,
2557 strip the MEM_OFFS bit and match only EA instructions */
2558 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);