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
find_match(const struct itemplate
**tempp
,
200 int32_t segment
, int64_t offset
, int bits
);
201 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
202 static opflags_t
regflag(const operand
*);
203 static int32_t regval(const operand
*);
204 static int rexflags(int, opflags_t
, int);
205 static int op_rexflags(const operand
*, int);
206 static ea
*process_ea(operand
*, ea
*, int, int, int, opflags_t
);
207 static void add_asp(insn
*, int);
209 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
211 return ins
->prefixes
[pos
] == prefix
;
214 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
216 if (ins
->prefixes
[pos
])
217 errfunc(ERR_NONFATAL
, "invalid %s prefix",
218 prefix_name(ins
->prefixes
[pos
]));
221 static const char *size_name(int size
)
243 static void warn_overflow(int pass
, int size
)
245 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
246 "%s data exceeds bounds", size_name(size
));
249 static void warn_overflow_const(int64_t data
, int size
)
251 if (overflow_general(data
, size
))
252 warn_overflow(ERR_PASS1
, size
);
255 static void warn_overflow_opd(const struct operand
*o
, int size
)
257 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
258 if (overflow_general(o
->offset
, size
))
259 warn_overflow(ERR_PASS2
, size
);
264 * This routine wrappers the real output format's output routine,
265 * in order to pass a copy of the data off to the listing file
266 * generator at the same time.
268 static void out(int64_t offset
, int32_t segto
, const void *data
,
269 enum out_type type
, uint64_t size
,
270 int32_t segment
, int32_t wrt
)
272 static int32_t lineno
= 0; /* static!!! */
273 static char *lnfname
= NULL
;
276 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
278 * This is a non-relocated address, and we're going to
279 * convert it into RAWDATA format.
284 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
288 WRITEADDR(q
, *(int64_t *)data
, size
);
293 list
->output(offset
, data
, type
, size
);
296 * this call to src_get determines when we call the
297 * debug-format-specific "linenum" function
298 * it updates lineno and lnfname to the current values
299 * returning 0 if "same as last time", -2 if lnfname
300 * changed, and the amount by which lineno changed,
301 * if it did. thus, these variables must be static
304 if (src_get(&lineno
, &lnfname
)) {
305 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
308 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
311 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
312 insn
* ins
, const uint8_t *code
)
317 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
321 if (optimizing
< 0 && c
== 0371)
324 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
326 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
327 /* Be optimistic in pass 1 */
330 if (ins
->oprs
[0].segment
!= segment
)
333 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
334 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
337 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
338 insn
* instruction
, struct ofmt
*output
, efunc error
,
341 const struct itemplate
*temp
;
346 int64_t start
= offset
;
347 int64_t wsize
; /* size for DB etc. */
349 errfunc
= error
; /* to pass to other functions */
351 outfmt
= output
; /* likewise */
352 list
= listgen
; /* and again */
354 wsize
= idata_bytes(instruction
->opcode
);
360 int32_t t
= instruction
->times
;
363 "instruction->times < 0 (%ld) in assemble()", t
);
365 while (t
--) { /* repeat TIMES times */
366 list_for_each(e
, instruction
->eops
) {
367 if (e
->type
== EOT_DB_NUMBER
) {
369 if (e
->segment
!= NO_SEG
)
370 errfunc(ERR_NONFATAL
,
371 "one-byte relocation attempted");
373 uint8_t out_byte
= e
->offset
;
374 out(offset
, segment
, &out_byte
,
375 OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
377 } else if (wsize
> 8) {
378 errfunc(ERR_NONFATAL
,
379 "integer supplied to a DT, DO or DY"
382 out(offset
, segment
, &e
->offset
,
383 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
385 } else if (e
->type
== EOT_DB_STRING
||
386 e
->type
== EOT_DB_STRING_FREE
) {
389 out(offset
, segment
, e
->stringval
,
390 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
391 align
= e
->stringlen
% wsize
;
394 align
= wsize
- align
;
395 out(offset
, segment
, zero_buffer
,
396 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
398 offset
+= e
->stringlen
+ align
;
401 if (t
> 0 && t
== instruction
->times
- 1) {
403 * Dummy call to list->output to give the offset to the
406 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
407 list
->uplevel(LIST_TIMES
);
410 if (instruction
->times
> 1)
411 list
->downlevel(LIST_TIMES
);
412 return offset
- start
;
415 if (instruction
->opcode
== I_INCBIN
) {
416 const char *fname
= instruction
->eops
->stringval
;
419 fp
= fopen(fname
, "rb");
421 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
423 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
424 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
427 static char buf
[4096];
428 size_t t
= instruction
->times
;
433 if (instruction
->eops
->next
) {
434 base
= instruction
->eops
->next
->offset
;
436 if (instruction
->eops
->next
->next
&&
437 len
> (size_t)instruction
->eops
->next
->next
->offset
)
438 len
= (size_t)instruction
->eops
->next
->next
->offset
;
441 * Dummy call to list->output to give the offset to the
444 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
445 list
->uplevel(LIST_INCBIN
);
449 fseek(fp
, base
, SEEK_SET
);
453 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
456 * This shouldn't happen unless the file
457 * actually changes while we are reading
461 "`incbin': unexpected EOF while"
462 " reading file `%s'", fname
);
463 t
= 0; /* Try to exit cleanly */
466 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
471 list
->downlevel(LIST_INCBIN
);
472 if (instruction
->times
> 1) {
474 * Dummy call to list->output to give the offset to the
477 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
478 list
->uplevel(LIST_TIMES
);
479 list
->downlevel(LIST_TIMES
);
482 return instruction
->times
* len
;
484 return 0; /* if we're here, there's an error */
487 /* Check to see if we need an address-size prefix */
488 add_asp(instruction
, bits
);
490 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
494 int64_t insn_size
= calcsize(segment
, offset
, bits
,
495 instruction
, temp
->code
);
496 itimes
= instruction
->times
;
497 if (insn_size
< 0) /* shouldn't be, on pass two */
498 error(ERR_PANIC
, "errors made it through from pass one");
501 for (j
= 0; j
< MAXPREFIX
; j
++) {
503 switch (instruction
->prefixes
[j
]) {
521 error(ERR_WARNING
| ERR_PASS2
,
522 "cs segment base generated, but will be ignored in 64-bit mode");
528 error(ERR_WARNING
| ERR_PASS2
,
529 "ds segment base generated, but will be ignored in 64-bit mode");
535 error(ERR_WARNING
| ERR_PASS2
,
536 "es segment base generated, but will be ignored in 64-bit mode");
548 error(ERR_WARNING
| ERR_PASS2
,
549 "ss segment base generated, but will be ignored in 64-bit mode");
556 "segr6 and segr7 cannot be used as prefixes");
561 "16-bit addressing is not supported "
563 } else if (bits
!= 16)
573 "64-bit addressing is only supported "
597 error(ERR_PANIC
, "invalid instruction prefix");
600 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
605 insn_end
= offset
+ insn_size
;
606 gencode(segment
, offset
, bits
, instruction
,
609 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
611 * Dummy call to list->output to give the offset to the
614 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
615 list
->uplevel(LIST_TIMES
);
618 if (instruction
->times
> 1)
619 list
->downlevel(LIST_TIMES
);
620 return offset
- start
;
624 case MERR_OPSIZEMISSING
:
625 error(ERR_NONFATAL
, "operation size not specified");
627 case MERR_OPSIZEMISMATCH
:
628 error(ERR_NONFATAL
, "mismatch in operand sizes");
631 error(ERR_NONFATAL
, "no instruction for this cpu level");
634 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
639 "invalid combination of opcode and operands");
646 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
647 insn
* instruction
, efunc error
)
649 const struct itemplate
*temp
;
652 errfunc
= error
; /* to pass to other functions */
655 if (instruction
->opcode
== I_none
)
658 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
659 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
660 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
661 instruction
->opcode
== I_DY
) {
663 int32_t isize
, osize
, wsize
;
666 wsize
= idata_bytes(instruction
->opcode
);
668 list_for_each(e
, instruction
->eops
) {
672 if (e
->type
== EOT_DB_NUMBER
) {
674 warn_overflow_const(e
->offset
, wsize
);
675 } else if (e
->type
== EOT_DB_STRING
||
676 e
->type
== EOT_DB_STRING_FREE
)
677 osize
= e
->stringlen
;
679 align
= (-osize
) % wsize
;
682 isize
+= osize
+ align
;
684 return isize
* instruction
->times
;
687 if (instruction
->opcode
== I_INCBIN
) {
688 const char *fname
= instruction
->eops
->stringval
;
693 fp
= fopen(fname
, "rb");
695 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
697 else if (fseek(fp
, 0L, SEEK_END
) < 0)
698 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
702 if (instruction
->eops
->next
) {
703 len
-= instruction
->eops
->next
->offset
;
704 if (instruction
->eops
->next
->next
&&
705 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
706 len
= (size_t)instruction
->eops
->next
->next
->offset
;
709 val
= instruction
->times
* len
;
716 /* Check to see if we need an address-size prefix */
717 add_asp(instruction
, bits
);
719 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
721 /* we've matched an instruction. */
723 const uint8_t *codes
= temp
->code
;
726 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
729 for (j
= 0; j
< MAXPREFIX
; j
++) {
730 switch (instruction
->prefixes
[j
]) {
756 return isize
* instruction
->times
;
758 return -1; /* didn't match any instruction */
762 static bool possible_sbyte(operand
*o
)
764 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
765 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
766 optimizing
>= 0 && !(o
->type
& STRICT
);
769 /* check that opn[op] is a signed byte of size 16 or 32 */
770 static bool is_sbyte16(operand
*o
)
774 if (!possible_sbyte(o
))
778 return v
>= -128 && v
<= 127;
781 static bool is_sbyte32(operand
*o
)
785 if (!possible_sbyte(o
))
789 return v
>= -128 && v
<= 127;
792 /* Common construct */
793 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
795 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
796 insn
* ins
, const uint8_t *codes
)
805 ins
->rex
= 0; /* Ensure REX is reset */
807 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
810 (void)segment
; /* Don't warn that this parameter is unused */
811 (void)offset
; /* Don't warn that this parameter is unused */
815 op1
= (c
& 3) + ((opex
& 1) << 2);
816 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
817 opx
= &ins
->oprs
[op1
];
818 opex
= 0; /* For the next iteration */
825 codes
+= c
, length
+= c
;
836 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
851 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
852 length
+= (opx
->type
& BITS16
) ? 2 : 4;
854 length
+= (bits
== 16) ? 2 : 4;
862 length
+= ins
->addr_size
>> 3;
870 length
+= 8; /* MOV reg64/imm */
878 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
879 length
+= (opx
->type
& BITS16
) ? 2 : 4;
881 length
+= (bits
== 16) ? 2 : 4;
893 length
+= is_sbyte16(opx
) ? 1 : 2;
902 length
+= is_sbyte32(opx
) ? 1 : 4;
913 ins
->drexdst
= regval(opx
);
918 ins
->rex
|= REX_D
|REX_OC
;
919 ins
->drexdst
= regval(opx
);
933 length
+= is_sbyte32(opx
) ? 1 : 4;
942 ins
->drexdst
= regval(opx
);
943 ins
->vex_cm
= *codes
++;
944 ins
->vex_wlp
= *codes
++;
950 ins
->vex_cm
= *codes
++;
951 ins
->vex_wlp
= *codes
++;
964 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
968 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
975 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
976 has_prefix(ins
, PPS_ASIZE
, P_A32
))
984 length
+= (bits
!= 16);
988 length
+= (bits
== 16);
1026 if (!ins
->prefixes
[PPS_LREP
])
1027 ins
->prefixes
[PPS_LREP
] = P_REP
;
1031 if (!ins
->prefixes
[PPS_LREP
])
1032 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1036 if (ins
->oprs
[0].segment
!= NO_SEG
)
1037 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1038 " quantity of BSS space");
1040 length
+= ins
->oprs
[0].offset
;
1044 if (!ins
->prefixes
[PPS_WAIT
])
1045 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1095 struct operand
*opy
= &ins
->oprs
[op2
];
1097 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1100 /* pick rfield from operand b (opx) */
1101 rflags
= regflag(opx
);
1102 rfield
= nasm_regvals
[opx
->basereg
];
1107 if (!process_ea(opy
, &ea_data
, bits
,
1108 ins
->addr_size
, rfield
, rflags
)) {
1109 errfunc(ERR_NONFATAL
, "invalid effective address");
1112 ins
->rex
|= ea_data
.rex
;
1113 length
+= ea_data
.size
;
1119 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1120 ": instruction code \\%o (0x%02X) given", c
, c
);
1125 ins
->rex
&= rex_mask
;
1127 if (ins
->rex
& REX_NH
) {
1128 if (ins
->rex
& REX_H
) {
1129 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1132 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1135 if (ins
->rex
& REX_V
) {
1136 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1138 if (ins
->rex
& REX_H
) {
1139 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1142 switch (ins
->vex_wlp
& 030) {
1156 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1157 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1160 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1164 } else if (ins
->rex
& REX_D
) {
1165 if (ins
->rex
& REX_H
) {
1166 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1169 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1170 ins
->drexdst
> 7)) {
1171 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1175 } else if (ins
->rex
& REX_REAL
) {
1176 if (ins
->rex
& REX_H
) {
1177 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1179 } else if (bits
== 64) {
1181 } else if ((ins
->rex
& REX_L
) &&
1182 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1185 assert_no_prefix(ins
, PPS_LREP
);
1188 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1196 #define EMIT_REX() \
1197 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1198 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1199 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1204 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1205 insn
* ins
, const struct itemplate
*temp
,
1208 static char condval
[] = { /* conditional opcodes */
1209 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1210 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1211 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1218 struct operand
*opx
;
1219 const uint8_t *codes
= temp
->code
;
1224 op1
= (c
& 3) + ((opex
& 1) << 2);
1225 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1226 opx
= &ins
->oprs
[op1
];
1227 opex
= 0; /* For the next iteration */
1235 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1248 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1249 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1254 /* The test for BITS8 and SBYTE here is intended to avoid
1255 warning on optimizer actions due to SBYTE, while still
1256 warn on explicit BYTE directives. Also warn, obviously,
1257 if the optimizer isn't enabled. */
1258 if (((opx
->type
& BITS8
) ||
1259 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1260 (opx
->offset
< -128 || opx
->offset
> 127)) {
1261 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1262 "signed byte value exceeds bounds");
1264 if (opx
->segment
!= NO_SEG
) {
1266 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1267 opx
->segment
, opx
->wrt
);
1269 bytes
[0] = opx
->offset
;
1270 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1277 if (opx
->offset
< -256 || opx
->offset
> 255) {
1278 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1279 "byte value exceeds bounds");
1281 if (opx
->segment
!= NO_SEG
) {
1283 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1284 opx
->segment
, opx
->wrt
);
1286 bytes
[0] = opx
->offset
;
1287 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1294 if (opx
->offset
< 0 || opx
->offset
> 255)
1295 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1296 "unsigned byte value exceeds bounds");
1297 if (opx
->segment
!= NO_SEG
) {
1299 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1300 opx
->segment
, opx
->wrt
);
1302 bytes
[0] = opx
->offset
;
1303 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1310 warn_overflow_opd(opx
, 2);
1312 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1313 opx
->segment
, opx
->wrt
);
1318 if (opx
->type
& (BITS16
| BITS32
))
1319 size
= (opx
->type
& BITS16
) ? 2 : 4;
1321 size
= (bits
== 16) ? 2 : 4;
1322 warn_overflow_opd(opx
, size
);
1324 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1325 opx
->segment
, opx
->wrt
);
1330 warn_overflow_opd(opx
, 4);
1332 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1333 opx
->segment
, opx
->wrt
);
1339 size
= ins
->addr_size
>> 3;
1340 warn_overflow_opd(opx
, size
);
1341 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1342 opx
->segment
, opx
->wrt
);
1347 if (opx
->segment
!= segment
)
1348 errfunc(ERR_NONFATAL
,
1349 "short relative jump outside segment");
1350 data
= opx
->offset
- insn_end
;
1351 if (data
> 127 || data
< -128)
1352 errfunc(ERR_NONFATAL
, "short jump is out of range");
1354 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1359 data
= (int64_t)opx
->offset
;
1360 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1361 opx
->segment
, opx
->wrt
);
1366 if (opx
->segment
!= segment
) {
1368 out(offset
, segment
, &data
,
1369 OUT_REL2ADR
, insn_end
- offset
,
1370 opx
->segment
, opx
->wrt
);
1372 data
= opx
->offset
- insn_end
;
1373 out(offset
, segment
, &data
,
1374 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1380 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1381 size
= (opx
->type
& BITS16
) ? 2 : 4;
1383 size
= (bits
== 16) ? 2 : 4;
1384 if (opx
->segment
!= segment
) {
1386 out(offset
, segment
, &data
,
1387 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1388 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1390 data
= opx
->offset
- insn_end
;
1391 out(offset
, segment
, &data
,
1392 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1398 if (opx
->segment
!= segment
) {
1400 out(offset
, segment
, &data
,
1401 OUT_REL4ADR
, insn_end
- offset
,
1402 opx
->segment
, opx
->wrt
);
1404 data
= opx
->offset
- insn_end
;
1405 out(offset
, segment
, &data
,
1406 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1412 if (opx
->segment
== NO_SEG
)
1413 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1416 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1417 outfmt
->segbase(1 + opx
->segment
),
1424 warn_overflow_opd(opx
, 2);
1425 if (is_sbyte16(opx
)) {
1427 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1431 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1432 opx
->segment
, opx
->wrt
);
1439 bytes
[0] = *codes
++;
1440 if (is_sbyte16(opx
))
1441 bytes
[0] |= 2; /* s-bit */
1442 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1448 warn_overflow_opd(opx
, 4);
1449 if (is_sbyte32(opx
)) {
1451 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1455 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1456 opx
->segment
, opx
->wrt
);
1463 bytes
[0] = *codes
++;
1464 if (is_sbyte32(opx
))
1465 bytes
[0] |= 2; /* s-bit */
1466 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1476 (ins
->drexdst
<< 4) |
1477 (ins
->rex
& REX_OC
? 0x08 : 0) |
1478 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1480 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1486 opx
= &ins
->oprs
[c
>> 3];
1487 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1488 opx
= &ins
->oprs
[c
& 7];
1489 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1490 errfunc(ERR_NONFATAL
,
1491 "non-absolute expression not permitted as argument %d",
1494 if (opx
->offset
& ~15) {
1495 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1496 "four-bit argument exceeds bounds");
1498 bytes
[0] |= opx
->offset
& 15;
1500 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1506 opx
= &ins
->oprs
[c
>> 4];
1507 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1509 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1515 opx
= &ins
->oprs
[c
];
1516 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1517 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1523 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1524 (int32_t)data
!= (int64_t)data
) {
1525 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1526 "signed dword immediate exceeds bounds");
1528 if (is_sbyte32(opx
)) {
1530 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1534 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1535 opx
->segment
, opx
->wrt
);
1542 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1543 (int32_t)data
!= (int64_t)data
) {
1544 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1545 "signed dword immediate exceeds bounds");
1547 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1548 opx
->segment
, opx
->wrt
);
1555 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1556 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1557 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1558 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1559 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1560 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1564 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1565 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1566 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1576 if (ins
->rex
& REX_W
)
1578 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1580 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1585 um
= (uint64_t)2 << (s
-1);
1588 if (uv
> 127 && uv
< (uint64_t)-128 &&
1589 (uv
< um
-128 || uv
> um
-1)) {
1590 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1591 "signed byte value exceeds bounds");
1593 if (opx
->segment
!= NO_SEG
) {
1595 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1596 opx
->segment
, opx
->wrt
);
1599 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1610 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1612 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1619 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1621 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1640 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1649 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1667 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1668 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1677 *bytes
= c
- 0332 + 0xF2;
1678 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1683 if (ins
->rex
& REX_R
) {
1685 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1688 ins
->rex
&= ~(REX_L
|REX_R
);
1699 if (ins
->oprs
[0].segment
!= NO_SEG
)
1700 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1702 int64_t size
= ins
->oprs
[0].offset
;
1704 out(offset
, segment
, NULL
,
1705 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1716 switch (ins
->oprs
[0].basereg
) {
1731 "bizarre 8086 segment register received");
1733 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1740 switch (ins
->oprs
[0].basereg
) {
1749 "bizarre 386 segment register received");
1751 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1760 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1766 bytes
[0] = c
- 0362 + 0xf2;
1767 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1777 *bytes
= c
- 0366 + 0x66;
1778 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1788 *bytes
= bits
== 16 ? 3 : 5;
1789 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1812 struct operand
*opy
= &ins
->oprs
[op2
];
1815 /* pick rfield from operand b (opx) */
1816 rflags
= regflag(opx
);
1817 rfield
= nasm_regvals
[opx
->basereg
];
1819 /* rfield is constant */
1824 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1826 errfunc(ERR_NONFATAL
, "invalid effective address");
1831 *p
++ = ea_data
.modrm
;
1832 if (ea_data
.sib_present
)
1835 /* DREX suffixes come between the SIB and the displacement */
1836 if (ins
->rex
& REX_D
) {
1837 *p
++ = (ins
->drexdst
<< 4) |
1838 (ins
->rex
& REX_OC
? 0x08 : 0) |
1839 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1844 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1847 * Make sure the address gets the right offset in case
1848 * the line breaks in the .lst file (BR 1197827)
1853 switch (ea_data
.bytes
) {
1863 if (opy
->segment
== segment
) {
1865 if (overflow_signed(data
, ea_data
.bytes
))
1866 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1867 out(offset
, segment
, &data
, OUT_ADDRESS
,
1868 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1870 /* overflow check in output/linker? */
1871 out(offset
, segment
, &data
, OUT_REL4ADR
,
1872 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1875 if (overflow_general(opy
->offset
, ins
->addr_size
>> 3) ||
1876 signed_bits(opy
->offset
, ins
->addr_size
) !=
1877 signed_bits(opy
->offset
, ea_data
.bytes
* 8))
1878 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1881 out(offset
, segment
, &data
, OUT_ADDRESS
,
1882 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1888 "Invalid amount of bytes (%d) for offset?!",
1897 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1898 ": instruction code \\%o (0x%02X) given", c
, c
);
1904 static opflags_t
regflag(const operand
* o
)
1906 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1907 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1909 return nasm_reg_flags
[o
->basereg
];
1912 static int32_t regval(const operand
* o
)
1914 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1915 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1917 return nasm_regvals
[o
->basereg
];
1920 static int op_rexflags(const operand
* o
, int mask
)
1925 if (o
->basereg
< EXPR_REG_START
|| o
->basereg
>= REG_ENUM_LIMIT
) {
1926 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1929 flags
= nasm_reg_flags
[o
->basereg
];
1930 val
= nasm_regvals
[o
->basereg
];
1932 return rexflags(val
, flags
, mask
);
1935 static int rexflags(int val
, opflags_t flags
, int mask
)
1940 rex
|= REX_B
|REX_X
|REX_R
;
1943 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1945 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1951 static enum match_result
find_match(const struct itemplate
**tempp
,
1953 int32_t segment
, int64_t offset
, int bits
)
1955 const struct itemplate
*temp
;
1956 enum match_result m
, merr
;
1957 opflags_t xsizeflags
[MAX_OPERANDS
];
1958 bool opsizemissing
= false;
1961 for (i
= 0; i
< instruction
->operands
; i
++)
1962 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
1964 merr
= MERR_INVALOP
;
1966 for (temp
= nasm_instructions
[instruction
->opcode
];
1967 temp
->opcode
!= I_none
; temp
++) {
1968 m
= matches(temp
, instruction
, bits
);
1969 if (m
== MOK_JUMP
) {
1970 if (jmp_match(segment
, offset
, bits
, instruction
, temp
->code
))
1974 } else if (m
== MERR_OPSIZEMISSING
&&
1975 (temp
->flags
& IF_SMASK
) != IF_SX
) {
1977 * Missing operand size and a candidate for fuzzy matching...
1979 for (i
= 0; i
< temp
->operands
; i
++) {
1980 if ((temp
->opd
[i
] & SAME_AS
) == 0)
1981 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
1983 opsizemissing
= true;
1987 if (merr
== MOK_GOOD
)
1991 /* No match, but see if we can get a fuzzy operand size match... */
1995 for (i
= 0; i
< instruction
->operands
; i
++) {
1997 * We ignore extrinsic operand sizes on registers, so we should
1998 * never try to fuzzy-match on them. This also resolves the case
1999 * when we have e.g. "xmmrm128" in two different positions.
2001 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2004 /* This tests if xsizeflags[i] has more than one bit set */
2005 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2006 goto done
; /* No luck */
2008 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2011 /* Try matching again... */
2012 for (temp
= nasm_instructions
[instruction
->opcode
];
2013 temp
->opcode
!= I_none
; temp
++) {
2014 m
= matches(temp
, instruction
, bits
);
2015 if (m
== MOK_JUMP
) {
2016 if (jmp_match(segment
, offset
, bits
, instruction
, temp
->code
))
2023 if (merr
== MOK_GOOD
)
2032 static enum match_result
matches(const struct itemplate
*itemp
,
2033 insn
*instruction
, int bits
)
2035 int i
, size
[MAX_OPERANDS
], asize
, oprs
;
2036 bool opsizemissing
= false;
2041 if (itemp
->opcode
!= instruction
->opcode
)
2042 return MERR_INVALOP
;
2045 * Count the operands
2047 if (itemp
->operands
!= instruction
->operands
)
2048 return MERR_INVALOP
;
2051 * Check that no spurious colons or TOs are present
2053 for (i
= 0; i
< itemp
->operands
; i
++)
2054 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2055 return MERR_INVALOP
;
2058 * Process size flags
2060 switch (itemp
->flags
& IF_SMASK
) {
2100 if (itemp
->flags
& IF_ARMASK
) {
2101 /* S- flags only apply to a specific operand */
2102 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2103 memset(size
, 0, sizeof size
);
2106 /* S- flags apply to all operands */
2107 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2112 * Check that the operand flags all match up,
2113 * it's a bit tricky so lets be verbose:
2115 * 1) Find out the size of operand. If instruction
2116 * doesn't have one specified -- we're trying to
2117 * guess it either from template (IF_S* flag) or
2120 * 2) If template operand (i) has SAME_AS flag [used for registers only]
2121 * (ie the same operand as was specified somewhere in template, and
2122 * this referred operand index is being achieved via ~SAME_AS)
2123 * we are to be sure that both registers (in template and instruction)
2126 * 3) If template operand do not match the instruction OR
2127 * template has an operand size specified AND this size differ
2128 * from which instruction has (perhaps we got it from code bits)
2130 * a) Check that only size of instruction and operand is differ
2131 * other characteristics do match
2132 * b) Perhaps it's a register specified in instruction so
2133 * for such a case we just mark that operand as "size
2134 * missing" and this will turn on fuzzy operand size
2135 * logic facility (handled by a caller)
2137 for (i
= 0; i
< itemp
->operands
; i
++) {
2138 opflags_t type
= instruction
->oprs
[i
].type
;
2139 if (!(type
& SIZE_MASK
))
2142 if (itemp
->opd
[i
] & SAME_AS
) {
2143 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2144 if (type
!= instruction
->oprs
[j
].type
||
2145 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2146 return MERR_INVALOP
;
2147 } else if (itemp
->opd
[i
] & ~type
||
2148 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2149 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2150 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) || (type
& SIZE_MASK
)) {
2151 return MERR_INVALOP
;
2152 } else if (!is_class(REGISTER
, type
)) {
2154 * Note: we don't honor extrinsic operand sizes for registers,
2155 * so "missing operand size" for a register should be
2156 * considered a wildcard match rather than an error.
2158 opsizemissing
= true;
2164 return MERR_OPSIZEMISSING
;
2167 * Check operand sizes
2169 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2170 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2171 for (i
= 0; i
< oprs
; i
++) {
2172 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2174 for (i
= 0; i
< oprs
; i
++)
2180 oprs
= itemp
->operands
;
2183 for (i
= 0; i
< itemp
->operands
; i
++) {
2184 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2185 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2186 return MERR_OPSIZEMISMATCH
;
2190 * Check template is okay at the set cpu level
2192 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2196 * Verify the appropriate long mode flag.
2198 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2199 return MERR_BADMODE
;
2202 * Check if special handling needed for Jumps
2204 if ((itemp
->code
[0] & 0374) == 0370)
2210 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2211 int addrbits
, int rfield
, opflags_t rflags
)
2213 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2215 output
->rip
= false;
2217 /* REX flags for the rfield operand */
2218 output
->rex
|= rexflags(rfield
, rflags
, REX_R
|REX_P
|REX_W
|REX_H
);
2220 if (is_class(REGISTER
, input
->type
)) { /* register direct */
2224 if (input
->basereg
< EXPR_REG_START
/* Verify as Register */
2225 || input
->basereg
>= REG_ENUM_LIMIT
)
2228 i
= nasm_regvals
[input
->basereg
];
2231 return NULL
; /* Invalid EA register */
2233 output
->rex
|= op_rexflags(input
, REX_B
|REX_P
|REX_W
|REX_H
);
2235 output
->sib_present
= false; /* no SIB necessary */
2236 output
->bytes
= 0; /* no offset necessary either */
2237 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2238 } else { /* it's a memory reference */
2239 if (input
->basereg
== -1
2240 && (input
->indexreg
== -1 || input
->scale
== 0)) {
2241 /* it's a pure offset */
2243 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2244 input
->segment
== NO_SEG
) {
2245 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2246 input
->type
&= ~IP_REL
;
2247 input
->type
|= MEMORY
;
2250 if (input
->eaflags
& EAF_BYTEOFFS
||
2251 (input
->eaflags
& EAF_WORDOFFS
&&
2252 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2253 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2256 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2257 int scale
, index
, base
;
2258 output
->sib_present
= true;
2262 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2264 output
->modrm
= 4 | ((rfield
& 7) << 3);
2265 output
->rip
= false;
2267 output
->sib_present
= false;
2268 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2269 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2270 output
->rip
= bits
== 64;
2272 } else { /* it's an indirection */
2273 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2274 int32_t seg
= input
->segment
;
2275 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2276 int t
, it
, bt
; /* register numbers */
2277 opflags_t x
, ix
, bx
; /* register flags */
2280 i
= -1; /* make this easy, at least */
2282 if (i
>= EXPR_REG_START
&& i
< REG_ENUM_LIMIT
) {
2283 it
= nasm_regvals
[i
];
2284 ix
= nasm_reg_flags
[i
];
2290 if (b
>= EXPR_REG_START
&& b
< REG_ENUM_LIMIT
) {
2291 bt
= nasm_regvals
[b
];
2292 bx
= nasm_reg_flags
[b
];
2298 /* check for a 32/64-bit memory reference... */
2299 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2300 /* it must be a 32/64-bit memory reference. Firstly we have
2301 * to check that all registers involved are type E/Rxx. */
2302 int32_t sok
= BITS32
|BITS64
, o
= input
->offset
;
2305 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2313 return NULL
; /* Invalid register */
2314 if (~sok
& bx
& SIZE_MASK
)
2315 return NULL
; /* Invalid size */
2319 /* While we're here, ensure the user didn't specify
2321 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2324 if (addrbits
== 16 ||
2325 (addrbits
== 32 && !(sok
& BITS32
)) ||
2326 (addrbits
== 64 && !(sok
& BITS64
)))
2329 /* now reorganize base/index */
2330 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2331 ((hb
== b
&& ht
== EAH_NOTBASE
)
2332 || (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2333 /* swap if hints say so */
2334 t
= bt
, bt
= it
, it
= t
;
2335 x
= bx
, bx
= ix
, ix
= x
;
2337 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2338 bt
= -1, bx
= 0, s
++;
2339 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2340 /* make single reg base, unless hint */
2341 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2343 if (((s
== 2 && it
!= REG_NUM_ESP
2344 && !(input
->eaflags
& EAF_TIMESTWO
)) || s
== 3
2345 || s
== 5 || s
== 9) && bt
== -1)
2346 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2347 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
2348 && (input
->eaflags
& EAF_TIMESTWO
))
2349 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2350 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2351 if (s
== 1 && it
== REG_NUM_ESP
) {
2352 /* swap ESP into base if scale is 1 */
2353 t
= it
, it
= bt
, bt
= t
;
2354 x
= ix
, ix
= bx
, bx
= x
;
2356 if (it
== REG_NUM_ESP
2357 || (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2358 return NULL
; /* wrong, for various reasons */
2360 output
->rex
|= rexflags(it
, ix
, REX_X
);
2361 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2363 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2372 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2373 seg
== NO_SEG
&& !forw_ref
&&
2375 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2377 else if (input
->eaflags
& EAF_BYTEOFFS
||
2378 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2380 && !(input
->eaflags
& EAF_WORDOFFS
)))
2386 output
->sib_present
= false;
2387 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2388 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2391 int mod
, scale
, index
, base
;
2411 default: /* then what the smeg is it? */
2412 return NULL
; /* panic */
2420 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2421 seg
== NO_SEG
&& !forw_ref
&&
2423 (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2425 else if (input
->eaflags
& EAF_BYTEOFFS
||
2426 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2428 && !(input
->eaflags
& EAF_WORDOFFS
)))
2434 output
->sib_present
= true;
2435 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2436 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2437 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2439 } else { /* it's 16-bit */
2441 int16_t o
= input
->offset
;
2443 /* check for 64-bit long mode */
2447 /* check all registers are BX, BP, SI or DI */
2448 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
2449 && b
!= R_DI
) || (i
!= -1 && i
!= R_BP
&& i
!= R_BX
2450 && i
!= R_SI
&& i
!= R_DI
))
2453 /* ensure the user didn't specify DWORD/QWORD */
2454 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2457 if (s
!= 1 && i
!= -1)
2458 return NULL
; /* no can do, in 16-bit EA */
2459 if (b
== -1 && i
!= -1) {
2464 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2469 /* have BX/BP as base, SI/DI index */
2471 return NULL
; /* shouldn't ever happen, in theory */
2472 if (i
!= -1 && b
!= -1 &&
2473 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2474 return NULL
; /* invalid combinations */
2475 if (b
== -1) /* pure offset: handled above */
2476 return NULL
; /* so if it gets to here, panic! */
2480 switch (i
* 256 + b
) {
2481 case R_SI
* 256 + R_BX
:
2484 case R_DI
* 256 + R_BX
:
2487 case R_SI
* 256 + R_BP
:
2490 case R_DI
* 256 + R_BP
:
2508 if (rm
== -1) /* can't happen, in theory */
2509 return NULL
; /* so panic if it does */
2511 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2512 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2514 else if (input
->eaflags
& EAF_BYTEOFFS
||
2515 (o
>= -128 && o
<= 127 && seg
== NO_SEG
2517 && !(input
->eaflags
& EAF_WORDOFFS
)))
2522 output
->sib_present
= false; /* no SIB - it's 16-bit */
2523 output
->bytes
= mod
; /* bytes of offset needed */
2524 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2529 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2533 static void add_asp(insn
*ins
, int addrbits
)
2538 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2540 switch (ins
->prefixes
[PPS_ASIZE
]) {
2551 valid
&= (addrbits
== 32) ? 16 : 32;
2557 for (j
= 0; j
< ins
->operands
; j
++) {
2558 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2561 /* Verify as Register */
2562 if (ins
->oprs
[j
].indexreg
< EXPR_REG_START
2563 || ins
->oprs
[j
].indexreg
>= REG_ENUM_LIMIT
)
2566 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2568 /* Verify as Register */
2569 if (ins
->oprs
[j
].basereg
< EXPR_REG_START
2570 || ins
->oprs
[j
].basereg
>= REG_ENUM_LIMIT
)
2573 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2575 if (ins
->oprs
[j
].scale
== 0)
2579 int ds
= ins
->oprs
[j
].disp_size
;
2580 if ((addrbits
!= 64 && ds
> 8) ||
2581 (addrbits
== 64 && ds
== 16))
2601 if (valid
& addrbits
) {
2602 ins
->addr_size
= addrbits
;
2603 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2604 /* Add an address size prefix */
2605 enum prefixes pref
= (addrbits
== 32) ? P_A16
: P_A32
;
2606 ins
->prefixes
[PPS_ASIZE
] = pref
;
2607 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2610 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2611 ins
->addr_size
= addrbits
; /* Error recovery */
2614 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2616 for (j
= 0; j
< ins
->operands
; j
++) {
2617 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2618 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
)
2619 != ins
->addr_size
) {
2620 /* mem_offs sizes must match the address size; if not,
2621 strip the MEM_OFFS bit and match only EA instructions */
2622 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);