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"
167 * Matching errors. These should be sorted so that more specific
168 * errors come later in the sequence.
176 * Matching success; the conditional ones first
178 MOK_JUMP
, /* Matching OK but needs jmp_match() */
179 MOK_GOOD
/* Matching unconditionally OK */
183 int sib_present
; /* is a SIB byte necessary? */
184 int bytes
; /* # of bytes of offset needed */
185 int size
; /* lazy - this is sib+bytes+1 */
186 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
189 static uint32_t cpu
; /* cpu level received from nasm.c */
190 static efunc errfunc
;
191 static struct ofmt
*outfmt
;
192 static ListGen
*list
;
194 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
195 static void gencode(int32_t segment
, int64_t offset
, int bits
,
196 insn
* ins
, const struct itemplate
*temp
,
198 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
199 static int32_t regflag(const operand
*);
200 static int32_t regval(const operand
*);
201 static int rexflags(int, int32_t, int);
202 static int op_rexflags(const operand
*, int);
203 static ea
*process_ea(operand
*, ea
*, int, int, int, int32_t);
204 static void add_asp(insn
*, int);
206 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
208 return ins
->prefixes
[pos
] == prefix
;
211 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
213 if (ins
->prefixes
[pos
])
214 errfunc(ERR_NONFATAL
, "invalid %s prefix",
215 prefix_name(ins
->prefixes
[pos
]));
218 static const char *size_name(int size
)
240 static void warn_overflow(int size
, const struct operand
*o
)
242 if (size
< 8 && o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
243 int64_t lim
= ((int64_t)1 << (size
*8))-1;
244 int64_t data
= o
->offset
;
246 if (data
< ~lim
|| data
> lim
)
247 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
248 "%s data exceeds bounds", size_name(size
));
252 * This routine wrappers the real output format's output routine,
253 * in order to pass a copy of the data off to the listing file
254 * generator at the same time.
256 static void out(int64_t offset
, int32_t segto
, const void *data
,
257 enum out_type type
, uint64_t size
,
258 int32_t segment
, int32_t wrt
)
260 static int32_t lineno
= 0; /* static!!! */
261 static char *lnfname
= NULL
;
264 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
266 * This is a non-relocated address, and we're going to
267 * convert it into RAWDATA format.
272 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
276 WRITEADDR(q
, *(int64_t *)data
, size
);
281 list
->output(offset
, data
, type
, size
);
284 * this call to src_get determines when we call the
285 * debug-format-specific "linenum" function
286 * it updates lineno and lnfname to the current values
287 * returning 0 if "same as last time", -2 if lnfname
288 * changed, and the amount by which lineno changed,
289 * if it did. thus, these variables must be static
292 if (src_get(&lineno
, &lnfname
)) {
293 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
296 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
299 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
300 insn
* ins
, const uint8_t *code
)
305 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
309 if (optimizing
< 0 && c
== 0371)
312 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
314 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
315 /* Be optimistic in pass 1 */
318 if (ins
->oprs
[0].segment
!= segment
)
321 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
322 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
325 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
326 insn
* instruction
, struct ofmt
*output
, efunc error
,
329 const struct itemplate
*temp
;
331 enum match_result size_prob
;
334 int64_t start
= offset
;
335 int64_t wsize
= 0; /* size for DB etc. */
337 errfunc
= error
; /* to pass to other functions */
339 outfmt
= output
; /* likewise */
340 list
= listgen
; /* and again */
342 switch (instruction
->opcode
) {
372 int32_t t
= instruction
->times
;
375 "instruction->times < 0 (%ld) in assemble()", t
);
377 while (t
--) { /* repeat TIMES times */
378 for (e
= instruction
->eops
; e
; e
= e
->next
) {
379 if (e
->type
== EOT_DB_NUMBER
) {
381 if (e
->segment
!= NO_SEG
)
382 errfunc(ERR_NONFATAL
,
383 "one-byte relocation attempted");
385 uint8_t out_byte
= e
->offset
;
386 out(offset
, segment
, &out_byte
,
387 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
389 } else if (wsize
> 8) {
390 errfunc(ERR_NONFATAL
,
391 "integer supplied to a DT, DO or DY"
394 out(offset
, segment
, &e
->offset
,
395 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
397 } else if (e
->type
== EOT_DB_STRING
||
398 e
->type
== EOT_DB_STRING_FREE
) {
401 out(offset
, segment
, e
->stringval
,
402 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
403 align
= e
->stringlen
% wsize
;
406 align
= wsize
- align
;
407 out(offset
, segment
, zero_buffer
,
408 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
410 offset
+= e
->stringlen
+ align
;
413 if (t
> 0 && t
== instruction
->times
- 1) {
415 * Dummy call to list->output to give the offset to the
418 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
419 list
->uplevel(LIST_TIMES
);
422 if (instruction
->times
> 1)
423 list
->downlevel(LIST_TIMES
);
424 return offset
- start
;
427 if (instruction
->opcode
== I_INCBIN
) {
428 const char *fname
= instruction
->eops
->stringval
;
431 fp
= fopen(fname
, "rb");
433 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
435 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
436 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
439 static char buf
[4096];
440 size_t t
= instruction
->times
;
445 if (instruction
->eops
->next
) {
446 base
= instruction
->eops
->next
->offset
;
448 if (instruction
->eops
->next
->next
&&
449 len
> (size_t)instruction
->eops
->next
->next
->offset
)
450 len
= (size_t)instruction
->eops
->next
->next
->offset
;
453 * Dummy call to list->output to give the offset to the
456 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
457 list
->uplevel(LIST_INCBIN
);
461 fseek(fp
, base
, SEEK_SET
);
465 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
468 * This shouldn't happen unless the file
469 * actually changes while we are reading
473 "`incbin': unexpected EOF while"
474 " reading file `%s'", fname
);
475 t
= 0; /* Try to exit cleanly */
478 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
483 list
->downlevel(LIST_INCBIN
);
484 if (instruction
->times
> 1) {
486 * Dummy call to list->output to give the offset to the
489 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
490 list
->uplevel(LIST_TIMES
);
491 list
->downlevel(LIST_TIMES
);
494 return instruction
->times
* len
;
496 return 0; /* if we're here, there's an error */
499 /* Check to see if we need an address-size prefix */
500 add_asp(instruction
, bits
);
502 size_prob
= MERR_INVALOP
;
504 for (temp
= nasm_instructions
[instruction
->opcode
];
505 temp
->opcode
!= -1; temp
++){
506 enum match_result m
= matches(temp
, instruction
, bits
);
508 (m
== MOK_JUMP
&& jmp_match(segment
, offset
, bits
,
509 instruction
, temp
->code
))) {
511 int64_t insn_size
= calcsize(segment
, offset
, bits
,
512 instruction
, temp
->code
);
513 itimes
= instruction
->times
;
514 if (insn_size
< 0) /* shouldn't be, on pass two */
515 error(ERR_PANIC
, "errors made it through from pass one");
518 for (j
= 0; j
< MAXPREFIX
; j
++) {
520 switch (instruction
->prefixes
[j
]) {
538 error(ERR_WARNING
| ERR_PASS2
,
539 "cs segment base generated, but will be ignored in 64-bit mode");
545 error(ERR_WARNING
| ERR_PASS2
,
546 "ds segment base generated, but will be ignored in 64-bit mode");
552 error(ERR_WARNING
| ERR_PASS2
,
553 "es segment base generated, but will be ignored in 64-bit mode");
565 error(ERR_WARNING
| ERR_PASS2
,
566 "ss segment base generated, but will be ignored in 64-bit mode");
573 "segr6 and segr7 cannot be used as prefixes");
578 "16-bit addressing is not supported "
580 } else if (bits
!= 16)
590 "64-bit addressing is only supported "
614 error(ERR_PANIC
, "invalid instruction prefix");
617 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
622 insn_end
= offset
+ insn_size
;
623 gencode(segment
, offset
, bits
, instruction
,
626 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
628 * Dummy call to list->output to give the offset to the
631 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
632 list
->uplevel(LIST_TIMES
);
635 if (instruction
->times
> 1)
636 list
->downlevel(LIST_TIMES
);
637 return offset
- start
;
638 } else if (m
> 0 && m
> size_prob
) {
643 if (temp
->opcode
== -1) { /* didn't match any instruction */
645 case MERR_OPSIZEMISSING
:
646 error(ERR_NONFATAL
, "operation size not specified");
648 case MERR_OPSIZEMISMATCH
:
649 error(ERR_NONFATAL
, "mismatch in operand sizes");
652 error(ERR_NONFATAL
, "no instruction for this cpu level");
655 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
660 "invalid combination of opcode and operands");
667 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
668 insn
* instruction
, efunc error
)
670 const struct itemplate
*temp
;
672 errfunc
= error
; /* to pass to other functions */
675 if (instruction
->opcode
== -1)
678 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
679 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
680 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
681 instruction
->opcode
== I_DY
) {
683 int32_t isize
, osize
, wsize
= 0; /* placate gcc */
686 switch (instruction
->opcode
) {
712 for (e
= instruction
->eops
; e
; e
= e
->next
) {
716 if (e
->type
== EOT_DB_NUMBER
)
718 else if (e
->type
== EOT_DB_STRING
||
719 e
->type
== EOT_DB_STRING_FREE
)
720 osize
= e
->stringlen
;
722 align
= (-osize
) % wsize
;
725 isize
+= osize
+ align
;
727 return isize
* instruction
->times
;
730 if (instruction
->opcode
== I_INCBIN
) {
731 const char *fname
= instruction
->eops
->stringval
;
735 fp
= fopen(fname
, "rb");
737 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
739 else if (fseek(fp
, 0L, SEEK_END
) < 0)
740 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
745 if (instruction
->eops
->next
) {
746 len
-= instruction
->eops
->next
->offset
;
747 if (instruction
->eops
->next
->next
&&
748 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
749 len
= (size_t)instruction
->eops
->next
->next
->offset
;
752 return instruction
->times
* len
;
754 return 0; /* if we're here, there's an error */
757 /* Check to see if we need an address-size prefix */
758 add_asp(instruction
, bits
);
760 for (temp
= nasm_instructions
[instruction
->opcode
];
761 temp
->opcode
!= -1; temp
++) {
762 enum match_result m
= matches(temp
, instruction
, bits
);
764 (m
== MOK_JUMP
&& jmp_match(segment
, offset
, bits
,
765 instruction
, temp
->code
))) {
766 /* we've matched an instruction. */
768 const uint8_t *codes
= temp
->code
;
771 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
774 for (j
= 0; j
< MAXPREFIX
; j
++) {
775 switch (instruction
->prefixes
[j
]) {
801 return isize
* instruction
->times
;
804 return -1; /* didn't match any instruction */
807 static bool possible_sbyte(operand
*o
)
809 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
810 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
811 optimizing
>= 0 && !(o
->type
& STRICT
);
814 /* check that opn[op] is a signed byte of size 16 or 32 */
815 static bool is_sbyte16(operand
*o
)
819 if (!possible_sbyte(o
))
823 return v
>= -128 && v
<= 127;
826 static bool is_sbyte32(operand
*o
)
830 if (!possible_sbyte(o
))
834 return v
>= -128 && v
<= 127;
837 /* Common construct */
838 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
840 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
841 insn
* ins
, const uint8_t *codes
)
850 ins
->rex
= 0; /* Ensure REX is reset */
852 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
855 (void)segment
; /* Don't warn that this parameter is unused */
856 (void)offset
; /* Don't warn that this parameter is unused */
860 op1
= (c
& 3) + ((opex
& 1) << 2);
861 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
862 opx
= &ins
->oprs
[op1
];
863 opex
= 0; /* For the next iteration */
870 codes
+= c
, length
+= c
;
881 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
896 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
897 length
+= (opx
->type
& BITS16
) ? 2 : 4;
899 length
+= (bits
== 16) ? 2 : 4;
907 length
+= ins
->addr_size
>> 3;
915 length
+= 8; /* MOV reg64/imm */
923 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
924 length
+= (opx
->type
& BITS16
) ? 2 : 4;
926 length
+= (bits
== 16) ? 2 : 4;
938 length
+= is_sbyte16(opx
) ? 1 : 2;
947 length
+= is_sbyte32(opx
) ? 1 : 4;
958 ins
->drexdst
= regval(opx
);
963 ins
->rex
|= REX_D
|REX_OC
;
964 ins
->drexdst
= regval(opx
);
978 length
+= is_sbyte32(opx
) ? 1 : 4;
987 ins
->drexdst
= regval(opx
);
988 ins
->vex_cm
= *codes
++;
989 ins
->vex_wlp
= *codes
++;
995 ins
->vex_cm
= *codes
++;
996 ins
->vex_wlp
= *codes
++;
1009 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1013 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1020 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1021 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1029 length
+= (bits
!= 16);
1033 length
+= (bits
== 16);
1071 if (!ins
->prefixes
[PPS_LREP
])
1072 ins
->prefixes
[PPS_LREP
] = P_REP
;
1076 if (!ins
->prefixes
[PPS_LREP
])
1077 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1081 if (ins
->oprs
[0].segment
!= NO_SEG
)
1082 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1083 " quantity of BSS space");
1085 length
+= ins
->oprs
[0].offset
;
1089 if (!ins
->prefixes
[PPS_WAIT
])
1090 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1140 struct operand
*opy
= &ins
->oprs
[op2
];
1142 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1145 /* pick rfield from operand b (opx) */
1146 rflags
= regflag(opx
);
1147 rfield
= nasm_regvals
[opx
->basereg
];
1152 if (!process_ea(opy
, &ea_data
, bits
,
1153 ins
->addr_size
, rfield
, rflags
)) {
1154 errfunc(ERR_NONFATAL
, "invalid effective address");
1157 ins
->rex
|= ea_data
.rex
;
1158 length
+= ea_data
.size
;
1164 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1165 ": instruction code \\%o (0x%02X) given", c
, c
);
1170 ins
->rex
&= rex_mask
;
1172 if (ins
->rex
& REX_NH
) {
1173 if (ins
->rex
& REX_H
) {
1174 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1177 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1180 if (ins
->rex
& REX_V
) {
1181 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1183 if (ins
->rex
& REX_H
) {
1184 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1187 switch (ins
->vex_wlp
& 030) {
1201 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1202 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1205 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1209 } else if (ins
->rex
& REX_D
) {
1210 if (ins
->rex
& REX_H
) {
1211 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1214 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1215 ins
->drexdst
> 7)) {
1216 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1220 } else if (ins
->rex
& REX_REAL
) {
1221 if (ins
->rex
& REX_H
) {
1222 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1224 } else if (bits
== 64) {
1226 } else if ((ins
->rex
& REX_L
) &&
1227 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1230 assert_no_prefix(ins
, PPS_LREP
);
1233 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1241 #define EMIT_REX() \
1242 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1243 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1244 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1249 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1250 insn
* ins
, const struct itemplate
*temp
,
1253 static char condval
[] = { /* conditional opcodes */
1254 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1255 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1256 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1263 struct operand
*opx
;
1264 const uint8_t *codes
= temp
->code
;
1269 op1
= (c
& 3) + ((opex
& 1) << 2);
1270 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1271 opx
= &ins
->oprs
[op1
];
1272 opex
= 0; /* For the next iteration */
1280 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1293 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1294 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1299 /* The test for BITS8 and SBYTE here is intended to avoid
1300 warning on optimizer actions due to SBYTE, while still
1301 warn on explicit BYTE directives. Also warn, obviously,
1302 if the optimizer isn't enabled. */
1303 if (((opx
->type
& BITS8
) ||
1304 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1305 (opx
->offset
< -128 || opx
->offset
> 127)) {
1306 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1307 "signed byte value exceeds bounds");
1309 if (opx
->segment
!= NO_SEG
) {
1311 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1312 opx
->segment
, opx
->wrt
);
1314 bytes
[0] = opx
->offset
;
1315 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1322 if (opx
->offset
< -256 || opx
->offset
> 255) {
1323 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1324 "byte value exceeds bounds");
1326 if (opx
->segment
!= NO_SEG
) {
1328 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1329 opx
->segment
, opx
->wrt
);
1331 bytes
[0] = opx
->offset
;
1332 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1339 if (opx
->offset
< 0 || opx
->offset
> 255)
1340 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1341 "unsigned byte value exceeds bounds");
1342 if (opx
->segment
!= NO_SEG
) {
1344 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1345 opx
->segment
, opx
->wrt
);
1347 bytes
[0] = opx
->offset
;
1348 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1355 warn_overflow(2, opx
);
1357 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1358 opx
->segment
, opx
->wrt
);
1363 if (opx
->type
& (BITS16
| BITS32
))
1364 size
= (opx
->type
& BITS16
) ? 2 : 4;
1366 size
= (bits
== 16) ? 2 : 4;
1367 warn_overflow(size
, opx
);
1369 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1370 opx
->segment
, opx
->wrt
);
1375 warn_overflow(4, opx
);
1377 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1378 opx
->segment
, opx
->wrt
);
1384 size
= ins
->addr_size
>> 3;
1385 warn_overflow(size
, opx
);
1386 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1387 opx
->segment
, opx
->wrt
);
1392 if (opx
->segment
!= segment
)
1393 errfunc(ERR_NONFATAL
,
1394 "short relative jump outside segment");
1395 data
= opx
->offset
- insn_end
;
1396 if (data
> 127 || data
< -128)
1397 errfunc(ERR_NONFATAL
, "short jump is out of range");
1399 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1404 data
= (int64_t)opx
->offset
;
1405 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1406 opx
->segment
, opx
->wrt
);
1411 if (opx
->segment
!= segment
) {
1413 out(offset
, segment
, &data
,
1414 OUT_REL2ADR
, insn_end
- offset
,
1415 opx
->segment
, opx
->wrt
);
1417 data
= opx
->offset
- insn_end
;
1418 out(offset
, segment
, &data
,
1419 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1425 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1426 size
= (opx
->type
& BITS16
) ? 2 : 4;
1428 size
= (bits
== 16) ? 2 : 4;
1429 if (opx
->segment
!= segment
) {
1431 out(offset
, segment
, &data
,
1432 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1433 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1435 data
= opx
->offset
- insn_end
;
1436 out(offset
, segment
, &data
,
1437 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1443 if (opx
->segment
!= segment
) {
1445 out(offset
, segment
, &data
,
1446 OUT_REL4ADR
, insn_end
- offset
,
1447 opx
->segment
, opx
->wrt
);
1449 data
= opx
->offset
- insn_end
;
1450 out(offset
, segment
, &data
,
1451 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1457 if (opx
->segment
== NO_SEG
)
1458 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1461 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1462 outfmt
->segbase(1 + opx
->segment
),
1469 warn_overflow(2, opx
);
1470 if (is_sbyte16(opx
)) {
1472 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1476 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1477 opx
->segment
, opx
->wrt
);
1484 bytes
[0] = *codes
++;
1485 if (is_sbyte16(opx
))
1486 bytes
[0] |= 2; /* s-bit */
1487 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1493 warn_overflow(4, opx
);
1494 if (is_sbyte32(opx
)) {
1496 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1500 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1501 opx
->segment
, opx
->wrt
);
1508 bytes
[0] = *codes
++;
1509 if (is_sbyte32(opx
))
1510 bytes
[0] |= 2; /* s-bit */
1511 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1521 (ins
->drexdst
<< 4) |
1522 (ins
->rex
& REX_OC
? 0x08 : 0) |
1523 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1525 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1531 opx
= &ins
->oprs
[c
>> 3];
1532 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1533 opx
= &ins
->oprs
[c
& 7];
1534 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1535 errfunc(ERR_NONFATAL
,
1536 "non-absolute expression not permitted as argument %d",
1539 if (opx
->offset
& ~15) {
1540 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1541 "four-bit argument exceeds bounds");
1543 bytes
[0] |= opx
->offset
& 15;
1545 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1551 opx
= &ins
->oprs
[c
>> 4];
1552 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1554 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1560 opx
= &ins
->oprs
[c
];
1561 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1562 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
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 if (is_sbyte32(opx
)) {
1575 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1579 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1580 opx
->segment
, opx
->wrt
);
1587 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1588 (int32_t)data
!= (int64_t)data
) {
1589 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1590 "signed dword immediate exceeds bounds");
1592 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1593 opx
->segment
, opx
->wrt
);
1600 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1601 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1602 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1603 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1604 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1605 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1609 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1610 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1611 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1621 if (ins
->rex
& REX_W
)
1623 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1625 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1630 um
= (uint64_t)2 << (s
-1);
1633 if (uv
> 127 && uv
< (uint64_t)-128 &&
1634 (uv
< um
-128 || uv
> um
-1)) {
1635 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1636 "signed byte value exceeds bounds");
1638 if (opx
->segment
!= NO_SEG
) {
1640 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1641 opx
->segment
, opx
->wrt
);
1644 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1655 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1657 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1664 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1666 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1685 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1694 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1712 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1713 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1722 *bytes
= c
- 0332 + 0xF2;
1723 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1728 if (ins
->rex
& REX_R
) {
1730 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1733 ins
->rex
&= ~(REX_L
|REX_R
);
1744 if (ins
->oprs
[0].segment
!= NO_SEG
)
1745 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1747 int64_t size
= ins
->oprs
[0].offset
;
1749 out(offset
, segment
, NULL
,
1750 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1761 switch (ins
->oprs
[0].basereg
) {
1776 "bizarre 8086 segment register received");
1778 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1785 switch (ins
->oprs
[0].basereg
) {
1794 "bizarre 386 segment register received");
1796 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1805 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1811 bytes
[0] = c
- 0362 + 0xf2;
1812 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1822 *bytes
= c
- 0366 + 0x66;
1823 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1833 *bytes
= bits
== 16 ? 3 : 5;
1834 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1857 struct operand
*opy
= &ins
->oprs
[op2
];
1860 /* pick rfield from operand b (opx) */
1861 rflags
= regflag(opx
);
1862 rfield
= nasm_regvals
[opx
->basereg
];
1864 /* rfield is constant */
1869 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1871 errfunc(ERR_NONFATAL
, "invalid effective address");
1876 *p
++ = ea_data
.modrm
;
1877 if (ea_data
.sib_present
)
1880 /* DREX suffixes come between the SIB and the displacement */
1881 if (ins
->rex
& REX_D
) {
1882 *p
++ = (ins
->drexdst
<< 4) |
1883 (ins
->rex
& REX_OC
? 0x08 : 0) |
1884 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1889 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1892 * Make sure the address gets the right offset in case
1893 * the line breaks in the .lst file (BR 1197827)
1898 switch (ea_data
.bytes
) {
1906 warn_overflow(ea_data
.bytes
, opy
);
1909 if (opy
->segment
== segment
) {
1911 out(offset
, segment
, &data
, OUT_ADDRESS
,
1912 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1914 out(offset
, segment
, &data
, OUT_REL4ADR
,
1915 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1919 out(offset
, segment
, &data
, OUT_ADDRESS
,
1920 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1926 "Invalid amount of bytes (%d) for offset?!",
1935 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1936 ": instruction code \\%o (0x%02X) given", c
, c
);
1942 static int32_t regflag(const operand
* o
)
1944 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1945 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1947 return nasm_reg_flags
[o
->basereg
];
1950 static int32_t regval(const operand
* o
)
1952 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1953 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1955 return nasm_regvals
[o
->basereg
];
1958 static int op_rexflags(const operand
* o
, int mask
)
1963 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1964 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1967 flags
= nasm_reg_flags
[o
->basereg
];
1968 val
= nasm_regvals
[o
->basereg
];
1970 return rexflags(val
, flags
, mask
);
1973 static int rexflags(int val
, int32_t flags
, int mask
)
1978 rex
|= REX_B
|REX_X
|REX_R
;
1981 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1983 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1989 static enum match_result
matches(const struct itemplate
*itemp
,
1990 insn
*instruction
, int bits
)
1992 int i
, size
[MAX_OPERANDS
], asize
, oprs
, ret
;
1999 if (itemp
->opcode
!= instruction
->opcode
)
2000 return MERR_INVALOP
;
2003 * Count the operands
2005 if (itemp
->operands
!= instruction
->operands
)
2006 return MERR_INVALOP
;
2009 * Check that no spurious colons or TOs are present
2011 for (i
= 0; i
< itemp
->operands
; i
++)
2012 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2013 return MERR_INVALOP
;
2016 * Process size flags
2018 if (itemp
->flags
& IF_ARMASK
) {
2019 memset(size
, 0, sizeof size
);
2021 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2023 switch (itemp
->flags
& IF_SMASK
) {
2060 switch (itemp
->flags
& IF_SMASK
) {
2095 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2100 * Check that the operand flags all match up
2102 for (i
= 0; i
< itemp
->operands
; i
++) {
2103 int32_t type
= instruction
->oprs
[i
].type
;
2104 if (!(type
& SIZE_MASK
))
2107 if (itemp
->opd
[i
] & SAME_AS
) {
2108 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2109 if (type
!= instruction
->oprs
[j
].type
||
2110 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2111 return MERR_INVALOP
;
2112 } else if (itemp
->opd
[i
] & ~type
||
2113 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2114 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2115 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2117 return MERR_INVALOP
;
2119 return MERR_OPSIZEMISSING
;
2124 * Check operand sizes
2126 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2127 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2129 for (i
= 0; i
< oprs
; i
++) {
2130 if ((asize
= itemp
->opd
[i
] & SIZE_MASK
) != 0) {
2132 for (j
= 0; j
< oprs
; j
++)
2138 oprs
= itemp
->operands
;
2141 for (i
= 0; i
< itemp
->operands
; i
++) {
2142 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2143 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2144 return MERR_OPSIZEMISMATCH
;
2148 * Check template is okay at the set cpu level
2150 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2154 * Verify the appropriate long mode flag.
2156 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2157 return MERR_BADMODE
;
2160 * Check if special handling needed for Jumps
2162 if ((uint8_t)(itemp
->code
[0]) >= 0370)
2168 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2169 int addrbits
, int rfield
, int32_t rflags
)
2171 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2173 output
->rip
= false;
2175 /* REX flags for the rfield operand */
2176 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2178 if (!(REGISTER
& ~input
->type
)) { /* register direct */
2182 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2183 || input
->basereg
>= REG_ENUM_LIMIT
)
2186 i
= nasm_regvals
[input
->basereg
];
2189 return NULL
; /* Invalid EA register */
2191 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2193 output
->sib_present
= false; /* no SIB necessary */
2194 output
->bytes
= 0; /* no offset necessary either */
2195 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2196 } else { /* it's a memory reference */
2197 if (input
->basereg
== -1
2198 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2199 /* it's a pure offset */
2200 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2201 int scale
, index
, base
;
2202 output
->sib_present
= true;
2206 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2208 output
->modrm
= 4 | ((rfield
& 7) << 3);
2209 output
->rip
= false;
2211 output
->sib_present
= false;
2212 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2213 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2214 output
->rip
= bits
== 64;
2216 } else { /* it's an indirection */
2217 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2218 int32_t o
= input
->offset
, seg
= input
->segment
;
2219 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2222 int32_t ix
, bx
; /* register flags */
2225 i
= -1; /* make this easy, at least */
2227 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2228 it
= nasm_regvals
[i
];
2229 ix
= nasm_reg_flags
[i
];
2235 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2236 bt
= nasm_regvals
[b
];
2237 bx
= nasm_reg_flags
[b
];
2243 /* check for a 32/64-bit memory reference... */
2244 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2245 /* it must be a 32/64-bit memory reference. Firstly we have
2246 * to check that all registers involved are type E/Rxx. */
2247 int32_t sok
= BITS32
|BITS64
;
2250 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2258 return NULL
; /* Invalid register */
2259 if (~sok
& bx
& SIZE_MASK
)
2260 return NULL
; /* Invalid size */
2264 /* While we're here, ensure the user didn't specify
2266 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2269 if (addrbits
== 16 ||
2270 (addrbits
== 32 && !(sok
& BITS32
)) ||
2271 (addrbits
== 64 && !(sok
& BITS64
)))
2274 /* now reorganize base/index */
2275 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2276 ((hb
== b
&& ht
== EAH_NOTBASE
)
2277 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2278 /* swap if hints say so */
2279 t
= bt
, bt
= it
, it
= t
;
2280 t
= bx
, bx
= ix
, ix
= t
;
2282 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2283 bt
= -1, bx
= 0, s
++;
2284 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2285 /* make single reg base, unless hint */
2286 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2288 if (((s
== 2 && it
!= REG_NUM_ESP
2289 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2290 || s
== 5 || s
== 9) && bt
== -1)
2291 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2292 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2293 && (input
->eaflags
& EAF_TIMESTWO
))
2294 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2295 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2296 if (s
== 1 && it
== REG_NUM_ESP
) {
2297 /* swap ESP into base if scale is 1 */
2298 t
= it
, it
= bt
, bt
= t
;
2299 t
= ix
, ix
= bx
, bx
= t
;
2301 if (it
== REG_NUM_ESP
2302 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2303 return NULL
; /* wrong, for various reasons */
2305 output
->rex
|= rexflags(it
, ix
, REX_X
);
2306 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2308 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2317 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2318 seg
== NO_SEG
&& !forw_ref
&&
2320 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2322 else if (input
->eaflags
& EAF_BYTEOFFS
||
2323 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2325 && !(input
->eaflags
& EAF_WORDOFFS
)))
2331 output
->sib_present
= false;
2332 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2333 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2336 int mod
, scale
, index
, base
;
2356 default: /* then what the smeg is it? */
2357 return NULL
; /* panic */
2365 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2366 seg
== NO_SEG
&& !forw_ref
&&
2368 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2370 else if (input
->eaflags
& EAF_BYTEOFFS
||
2371 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2373 && !(input
->eaflags
& EAF_WORDOFFS
)))
2379 output
->sib_present
= true;
2380 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2381 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2382 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2384 } else { /* it's 16-bit */
2387 /* check for 64-bit long mode */
2391 /* check all registers are BX, BP, SI or DI */
2392 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2393 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2394 && i
!= R_SI
&& i
!= R_DI
))
2397 /* ensure the user didn't specify DWORD/QWORD */
2398 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2401 if (s
!= 1 && i
!= -1)
2402 return NULL
; /* no can do, in 16-bit EA */
2403 if (b
== -1 && i
!= -1) {
2408 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2413 /* have BX/BP as base, SI/DI index */
2415 return NULL
; /* shouldn't ever happen, in theory */
2416 if (i
!= -1 && b
!= -1 &&
2417 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2418 return NULL
; /* invalid combinations */
2419 if (b
== -1) /* pure offset: handled above */
2420 return NULL
; /* so if it gets to here, panic! */
2424 switch (i
* 256 + b
) {
2425 case R_SI
* 256 + R_BX
:
2428 case R_DI
* 256 + R_BX
:
2431 case R_SI
* 256 + R_BP
:
2434 case R_DI
* 256 + R_BP
:
2452 if (rm
== -1) /* can't happen, in theory */
2453 return NULL
; /* so panic if it does */
2455 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2456 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2458 else if (input
->eaflags
& EAF_BYTEOFFS
||
2459 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2461 && !(input
->eaflags
& EAF_WORDOFFS
)))
2466 output
->sib_present
= false; /* no SIB - it's 16-bit */
2467 output
->bytes
= mod
; /* bytes of offset needed */
2468 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2473 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2477 static void add_asp(insn
*ins
, int addrbits
)
2482 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2484 switch (ins
->prefixes
[PPS_ASIZE
]) {
2495 valid
&= (addrbits
== 32) ? 16 : 32;
2501 for (j
= 0; j
< ins
->operands
; j
++) {
2502 if (!(MEMORY
& ~ins
->oprs
[j
].type
)) {
2505 /* Verify as Register */
2506 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2507 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2510 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2512 /* Verify as Register */
2513 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2514 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2517 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2519 if (ins
->oprs
[j
].scale
== 0)
2523 int ds
= ins
->oprs
[j
].disp_size
;
2524 if ((addrbits
!= 64 && ds
> 8) ||
2525 (addrbits
== 64 && ds
== 16))
2545 if (valid
& addrbits
) {
2546 ins
->addr_size
= addrbits
;
2547 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2548 /* Add an address size prefix */
2549 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2550 ins
->prefixes
[PPS_ASIZE
] = pref
;
2551 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2554 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2555 ins
->addr_size
= addrbits
; /* Error recovery */
2558 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2560 for (j
= 0; j
< ins
->operands
; j
++) {
2561 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2562 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2563 != ins
->addr_size
) {
2564 /* mem_offs sizes must match the address size; if not,
2565 strip the MEM_OFFS bit and match only EA instructions */
2566 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);