1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2010 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 * [l0] ll = 0 for L = 0 (.128, .lz)
97 * [l1] ll = 1 for L = 1 (.256)
98 * [lig] ll = 2 for L don't care (always assembled as 0)
100 * [w0] ww = 0 for W = 0
101 * [w1 ] ww = 1 for W = 1
102 * [wig] ww = 2 for W don't care (always assembled as 0)
103 * [ww] ww = 3 for W used as REX.W
105 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
107 * \274..\277 - a signed byte immediate operand, from operand 0..3,
108 * which is to be extended to the operand size.
109 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
110 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
111 * \312 - (disassembler only) invalid with non-default address size.
112 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
113 * \314 - (disassembler only) invalid with REX.B
114 * \315 - (disassembler only) invalid with REX.X
115 * \316 - (disassembler only) invalid with REX.R
116 * \317 - (disassembler only) invalid with REX.W
117 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
118 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
119 * \322 - indicates that this instruction is only valid when the
120 * operand size is the default (instruction to disassembler,
121 * generates no code in the assembler)
122 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
123 * \324 - indicates 64-bit operand size requiring REX prefix.
124 * \325 - instruction which always uses spl/bpl/sil/dil
125 * \330 - a literal byte follows in the code stream, to be added
126 * to the condition code value of the instruction.
127 * \331 - instruction not valid with REP prefix. Hint for
128 * disassembler only; for SSE instructions.
129 * \332 - REP prefix (0xF2 byte) used as opcode extension.
130 * \333 - REP prefix (0xF3 byte) used as opcode extension.
131 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
132 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
133 * \336 - force a REP(E) prefix (0xF2) even if not specified.
134 * \337 - force a REPNE prefix (0xF3) even if not specified.
135 * \336-\337 are still listed as prefixes in the disassembler.
136 * \340 - reserve <operand 0> bytes of uninitialized storage.
137 * Operand 0 had better be a segmentless constant.
138 * \341 - this instruction needs a WAIT "prefix"
139 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
140 * (POP is never used for CS) depending on operand 0
141 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
143 * \360 - no SSE prefix (== \364\331)
144 * \361 - 66 SSE prefix (== \366\331)
145 * \362 - F2 SSE prefix (== \364\332)
146 * \363 - F3 SSE prefix (== \364\333)
147 * \364 - operand-size prefix (0x66) not permitted
148 * \365 - address-size prefix (0x67) not permitted
149 * \366 - operand-size prefix (0x66) used as opcode extension
150 * \367 - address-size prefix (0x67) used as opcode extension
151 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
152 * 370 is used for Jcc, 371 is used for JMP.
153 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
154 * used for conditional jump over longer jump
157 #include "compiler.h"
161 #include <inttypes.h>
165 #include "assemble.h"
171 * Matching errors. These should be sorted so that more specific
172 * errors come later in the sequence.
180 * Matching success; the conditional ones first
182 MOK_JUMP
, /* Matching OK but needs jmp_match() */
183 MOK_GOOD
/* Matching unconditionally OK */
187 int sib_present
; /* is a SIB byte necessary? */
188 int bytes
; /* # of bytes of offset needed */
189 int size
; /* lazy - this is sib+bytes+1 */
190 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
193 static uint32_t cpu
; /* cpu level received from nasm.c */
194 static efunc errfunc
;
195 static struct ofmt
*outfmt
;
196 static ListGen
*list
;
198 static int64_t calcsize(int32_t, int64_t, int, insn
*, const uint8_t *);
199 static void gencode(int32_t segment
, int64_t offset
, int bits
,
200 insn
* ins
, const struct itemplate
*temp
,
202 static enum match_result
find_match(const struct itemplate
**tempp
,
204 int32_t segment
, int64_t offset
, int bits
);
205 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
206 static opflags_t
regflag(const operand
*);
207 static int32_t regval(const operand
*);
208 static int rexflags(int, opflags_t
, int);
209 static int op_rexflags(const operand
*, int);
210 static ea
*process_ea(operand
*, ea
*, int, int, int, opflags_t
);
211 static void add_asp(insn
*, int);
213 static int has_prefix(insn
* ins
, enum prefix_pos pos
, enum prefixes prefix
)
215 return ins
->prefixes
[pos
] == prefix
;
218 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
220 if (ins
->prefixes
[pos
])
221 errfunc(ERR_NONFATAL
, "invalid %s prefix",
222 prefix_name(ins
->prefixes
[pos
]));
225 static const char *size_name(int size
)
247 static void warn_overflow(int pass
, int size
)
249 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
250 "%s data exceeds bounds", size_name(size
));
253 static void warn_overflow_const(int64_t data
, int size
)
255 if (overflow_general(data
, size
))
256 warn_overflow(ERR_PASS1
, size
);
259 static void warn_overflow_opd(const struct operand
*o
, int size
)
261 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
262 if (overflow_general(o
->offset
, size
))
263 warn_overflow(ERR_PASS2
, size
);
268 * This routine wrappers the real output format's output routine,
269 * in order to pass a copy of the data off to the listing file
270 * generator at the same time.
272 static void out(int64_t offset
, int32_t segto
, const void *data
,
273 enum out_type type
, uint64_t size
,
274 int32_t segment
, int32_t wrt
)
276 static int32_t lineno
= 0; /* static!!! */
277 static char *lnfname
= NULL
;
280 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
282 * This is a non-relocated address, and we're going to
283 * convert it into RAWDATA format.
288 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
292 WRITEADDR(q
, *(int64_t *)data
, size
);
297 list
->output(offset
, data
, type
, size
);
300 * this call to src_get determines when we call the
301 * debug-format-specific "linenum" function
302 * it updates lineno and lnfname to the current values
303 * returning 0 if "same as last time", -2 if lnfname
304 * changed, and the amount by which lineno changed,
305 * if it did. thus, these variables must be static
308 if (src_get(&lineno
, &lnfname
))
309 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
311 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
314 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
315 insn
* ins
, const uint8_t *code
)
320 if ((c
!= 0370 && c
!= 0371) || (ins
->oprs
[0].type
& STRICT
))
324 if (optimizing
< 0 && c
== 0371)
327 isize
= calcsize(segment
, offset
, bits
, ins
, code
);
329 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
330 /* Be optimistic in pass 1 */
333 if (ins
->oprs
[0].segment
!= segment
)
336 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
337 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
340 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
341 insn
* instruction
, struct ofmt
*output
, efunc error
,
344 const struct itemplate
*temp
;
349 int64_t start
= offset
;
350 int64_t wsize
; /* size for DB etc. */
352 errfunc
= error
; /* to pass to other functions */
354 outfmt
= output
; /* likewise */
355 list
= listgen
; /* and again */
357 wsize
= idata_bytes(instruction
->opcode
);
363 int32_t t
= instruction
->times
;
366 "instruction->times < 0 (%ld) in assemble()", t
);
368 while (t
--) { /* repeat TIMES times */
369 list_for_each(e
, instruction
->eops
) {
370 if (e
->type
== EOT_DB_NUMBER
) {
372 errfunc(ERR_NONFATAL
,
373 "integer supplied to a DT, DO or DY"
376 out(offset
, segment
, &e
->offset
,
377 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
380 } else if (e
->type
== EOT_DB_STRING
||
381 e
->type
== EOT_DB_STRING_FREE
) {
384 out(offset
, segment
, e
->stringval
,
385 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
386 align
= e
->stringlen
% wsize
;
389 align
= wsize
- align
;
390 out(offset
, segment
, zero_buffer
,
391 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
393 offset
+= e
->stringlen
+ align
;
396 if (t
> 0 && t
== instruction
->times
- 1) {
398 * Dummy call to list->output to give the offset to the
401 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
402 list
->uplevel(LIST_TIMES
);
405 if (instruction
->times
> 1)
406 list
->downlevel(LIST_TIMES
);
407 return offset
- start
;
410 if (instruction
->opcode
== I_INCBIN
) {
411 const char *fname
= instruction
->eops
->stringval
;
414 fp
= fopen(fname
, "rb");
416 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
418 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
419 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
422 static char buf
[4096];
423 size_t t
= instruction
->times
;
428 if (instruction
->eops
->next
) {
429 base
= instruction
->eops
->next
->offset
;
431 if (instruction
->eops
->next
->next
&&
432 len
> (size_t)instruction
->eops
->next
->next
->offset
)
433 len
= (size_t)instruction
->eops
->next
->next
->offset
;
436 * Dummy call to list->output to give the offset to the
439 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
440 list
->uplevel(LIST_INCBIN
);
444 fseek(fp
, base
, SEEK_SET
);
448 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
451 * This shouldn't happen unless the file
452 * actually changes while we are reading
456 "`incbin': unexpected EOF while"
457 " reading file `%s'", fname
);
458 t
= 0; /* Try to exit cleanly */
461 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
466 list
->downlevel(LIST_INCBIN
);
467 if (instruction
->times
> 1) {
469 * Dummy call to list->output to give the offset to the
472 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
473 list
->uplevel(LIST_TIMES
);
474 list
->downlevel(LIST_TIMES
);
477 return instruction
->times
* len
;
479 return 0; /* if we're here, there's an error */
482 /* Check to see if we need an address-size prefix */
483 add_asp(instruction
, bits
);
485 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
489 int64_t insn_size
= calcsize(segment
, offset
, bits
,
490 instruction
, temp
->code
);
491 itimes
= instruction
->times
;
492 if (insn_size
< 0) /* shouldn't be, on pass two */
493 error(ERR_PANIC
, "errors made it through from pass one");
496 for (j
= 0; j
< MAXPREFIX
; j
++) {
498 switch (instruction
->prefixes
[j
]) {
516 error(ERR_WARNING
| ERR_PASS2
,
517 "cs segment base generated, but will be ignored in 64-bit mode");
523 error(ERR_WARNING
| ERR_PASS2
,
524 "ds segment base generated, but will be ignored in 64-bit mode");
530 error(ERR_WARNING
| ERR_PASS2
,
531 "es segment base generated, but will be ignored in 64-bit mode");
543 error(ERR_WARNING
| ERR_PASS2
,
544 "ss segment base generated, but will be ignored in 64-bit mode");
551 "segr6 and segr7 cannot be used as prefixes");
556 "16-bit addressing is not supported "
558 } else if (bits
!= 16)
568 "64-bit addressing is only supported "
592 error(ERR_PANIC
, "invalid instruction prefix");
595 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
600 insn_end
= offset
+ insn_size
;
601 gencode(segment
, offset
, bits
, instruction
,
604 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
606 * Dummy call to list->output to give the offset to the
609 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
610 list
->uplevel(LIST_TIMES
);
613 if (instruction
->times
> 1)
614 list
->downlevel(LIST_TIMES
);
615 return offset
- start
;
619 case MERR_OPSIZEMISSING
:
620 error(ERR_NONFATAL
, "operation size not specified");
622 case MERR_OPSIZEMISMATCH
:
623 error(ERR_NONFATAL
, "mismatch in operand sizes");
626 error(ERR_NONFATAL
, "no instruction for this cpu level");
629 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
634 "invalid combination of opcode and operands");
641 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, uint32_t cp
,
642 insn
* instruction
, efunc error
)
644 const struct itemplate
*temp
;
647 errfunc
= error
; /* to pass to other functions */
650 if (instruction
->opcode
== I_none
)
653 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
654 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
655 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
656 instruction
->opcode
== I_DY
) {
658 int32_t isize
, osize
, wsize
;
661 wsize
= idata_bytes(instruction
->opcode
);
663 list_for_each(e
, instruction
->eops
) {
667 if (e
->type
== EOT_DB_NUMBER
) {
669 warn_overflow_const(e
->offset
, wsize
);
670 } else if (e
->type
== EOT_DB_STRING
||
671 e
->type
== EOT_DB_STRING_FREE
)
672 osize
= e
->stringlen
;
674 align
= (-osize
) % wsize
;
677 isize
+= osize
+ align
;
679 return isize
* instruction
->times
;
682 if (instruction
->opcode
== I_INCBIN
) {
683 const char *fname
= instruction
->eops
->stringval
;
688 fp
= fopen(fname
, "rb");
690 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
692 else if (fseek(fp
, 0L, SEEK_END
) < 0)
693 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
697 if (instruction
->eops
->next
) {
698 len
-= instruction
->eops
->next
->offset
;
699 if (instruction
->eops
->next
->next
&&
700 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
701 len
= (size_t)instruction
->eops
->next
->next
->offset
;
704 val
= instruction
->times
* len
;
711 /* Check to see if we need an address-size prefix */
712 add_asp(instruction
, bits
);
714 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
716 /* we've matched an instruction. */
718 const uint8_t *codes
= temp
->code
;
721 isize
= calcsize(segment
, offset
, bits
, instruction
, codes
);
724 for (j
= 0; j
< MAXPREFIX
; j
++) {
725 switch (instruction
->prefixes
[j
]) {
751 return isize
* instruction
->times
;
753 return -1; /* didn't match any instruction */
757 static bool possible_sbyte(operand
*o
)
759 return o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
&&
760 !(o
->opflags
& OPFLAG_UNKNOWN
) &&
761 optimizing
>= 0 && !(o
->type
& STRICT
);
764 /* check that opn[op] is a signed byte of size 16 or 32 */
765 static bool is_sbyte16(operand
*o
)
769 if (!possible_sbyte(o
))
773 return v
>= -128 && v
<= 127;
776 static bool is_sbyte32(operand
*o
)
780 if (!possible_sbyte(o
))
784 return v
>= -128 && v
<= 127;
787 /* Common construct */
788 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
790 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
791 insn
* ins
, const uint8_t *codes
)
800 ins
->rex
= 0; /* Ensure REX is reset */
802 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
805 (void)segment
; /* Don't warn that this parameter is unused */
806 (void)offset
; /* Don't warn that this parameter is unused */
810 op1
= (c
& 3) + ((opex
& 1) << 2);
811 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
812 opx
= &ins
->oprs
[op1
];
813 opex
= 0; /* For the next iteration */
820 codes
+= c
, length
+= c
;
831 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
846 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
847 length
+= (opx
->type
& BITS16
) ? 2 : 4;
849 length
+= (bits
== 16) ? 2 : 4;
857 length
+= ins
->addr_size
>> 3;
865 length
+= 8; /* MOV reg64/imm */
873 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
874 length
+= (opx
->type
& BITS16
) ? 2 : 4;
876 length
+= (bits
== 16) ? 2 : 4;
888 length
+= is_sbyte16(opx
) ? 1 : 2;
897 length
+= is_sbyte32(opx
) ? 1 : 4;
908 ins
->drexdst
= regval(opx
);
913 ins
->rex
|= REX_D
|REX_OC
;
914 ins
->drexdst
= regval(opx
);
928 length
+= is_sbyte32(opx
) ? 1 : 4;
937 ins
->drexdst
= regval(opx
);
938 ins
->vex_cm
= *codes
++;
939 ins
->vex_wlp
= *codes
++;
945 ins
->vex_cm
= *codes
++;
946 ins
->vex_wlp
= *codes
++;
959 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
963 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
970 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
971 has_prefix(ins
, PPS_ASIZE
, P_A32
))
979 length
+= (bits
!= 16);
983 length
+= (bits
== 16);
1021 if (!ins
->prefixes
[PPS_LREP
])
1022 ins
->prefixes
[PPS_LREP
] = P_REP
;
1026 if (!ins
->prefixes
[PPS_LREP
])
1027 ins
->prefixes
[PPS_LREP
] = P_REPNE
;
1031 if (ins
->oprs
[0].segment
!= NO_SEG
)
1032 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1033 " quantity of BSS space");
1035 length
+= ins
->oprs
[0].offset
;
1039 if (!ins
->prefixes
[PPS_WAIT
])
1040 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1090 struct operand
*opy
= &ins
->oprs
[op2
];
1092 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1095 /* pick rfield from operand b (opx) */
1096 rflags
= regflag(opx
);
1097 rfield
= nasm_regvals
[opx
->basereg
];
1102 if (!process_ea(opy
, &ea_data
, bits
,
1103 ins
->addr_size
, rfield
, rflags
)) {
1104 errfunc(ERR_NONFATAL
, "invalid effective address");
1107 ins
->rex
|= ea_data
.rex
;
1108 length
+= ea_data
.size
;
1114 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1115 ": instruction code \\%o (0x%02X) given", c
, c
);
1120 ins
->rex
&= rex_mask
;
1122 if (ins
->rex
& REX_NH
) {
1123 if (ins
->rex
& REX_H
) {
1124 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1127 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1130 if (ins
->rex
& REX_V
) {
1131 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1133 if (ins
->rex
& REX_H
) {
1134 errfunc(ERR_NONFATAL
, "cannot use high register in vex instruction");
1137 switch (ins
->vex_wlp
& 060) {
1151 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->drexdst
> 7)) {
1152 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1155 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_R
|REX_B
)))
1159 } else if (ins
->rex
& REX_D
) {
1160 if (ins
->rex
& REX_H
) {
1161 errfunc(ERR_NONFATAL
, "cannot use high register in drex instruction");
1164 if (bits
!= 64 && ((ins
->rex
& (REX_R
|REX_W
|REX_X
|REX_B
)) ||
1165 ins
->drexdst
> 7)) {
1166 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1170 } else if (ins
->rex
& REX_REAL
) {
1171 if (ins
->rex
& REX_H
) {
1172 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1174 } else if (bits
== 64) {
1176 } else if ((ins
->rex
& REX_L
) &&
1177 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1180 assert_no_prefix(ins
, PPS_LREP
);
1183 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1191 #define EMIT_REX() \
1192 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
1193 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1194 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1199 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1200 insn
* ins
, const struct itemplate
*temp
,
1203 static char condval
[] = { /* conditional opcodes */
1204 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1205 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1206 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1213 struct operand
*opx
;
1214 const uint8_t *codes
= temp
->code
;
1219 op1
= (c
& 3) + ((opex
& 1) << 2);
1220 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1221 opx
= &ins
->oprs
[op1
];
1222 opex
= 0; /* For the next iteration */
1230 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1243 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1244 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1250 * The test for BITS8 and SBYTE here is intended to avoid
1251 * warning on optimizer actions due to SBYTE, while still
1252 * warn on explicit BYTE directives. Also warn, obviously,
1253 * if the optimizer isn't enabled.
1255 if (((opx
->type
& BITS8
) ||
1256 !(opx
->type
& temp
->opd
[op1
] & BYTENESS
)) &&
1257 (opx
->offset
< -128 || opx
->offset
> 127)) {
1258 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1259 "signed byte value exceeds bounds");
1261 if (opx
->segment
!= NO_SEG
) {
1263 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1264 opx
->segment
, opx
->wrt
);
1266 bytes
[0] = opx
->offset
;
1267 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1274 if (opx
->offset
< -256 || opx
->offset
> 255) {
1275 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1276 "byte value exceeds bounds");
1278 if (opx
->segment
!= NO_SEG
) {
1280 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1281 opx
->segment
, opx
->wrt
);
1283 bytes
[0] = opx
->offset
;
1284 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1291 if (opx
->offset
< 0 || opx
->offset
> 255)
1292 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1293 "unsigned byte value exceeds bounds");
1294 if (opx
->segment
!= NO_SEG
) {
1296 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1297 opx
->segment
, opx
->wrt
);
1299 bytes
[0] = opx
->offset
;
1300 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1307 warn_overflow_opd(opx
, 2);
1309 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1310 opx
->segment
, opx
->wrt
);
1315 if (opx
->type
& (BITS16
| BITS32
))
1316 size
= (opx
->type
& BITS16
) ? 2 : 4;
1318 size
= (bits
== 16) ? 2 : 4;
1319 warn_overflow_opd(opx
, size
);
1321 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1322 opx
->segment
, opx
->wrt
);
1327 warn_overflow_opd(opx
, 4);
1329 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1330 opx
->segment
, opx
->wrt
);
1336 size
= ins
->addr_size
>> 3;
1337 warn_overflow_opd(opx
, size
);
1338 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1339 opx
->segment
, opx
->wrt
);
1344 if (opx
->segment
!= segment
) {
1346 out(offset
, segment
, &data
,
1347 OUT_REL1ADR
, insn_end
- offset
,
1348 opx
->segment
, opx
->wrt
);
1350 data
= opx
->offset
- insn_end
;
1351 if (data
> 127 || data
< -128)
1352 errfunc(ERR_NONFATAL
, "short jump is out of range");
1353 out(offset
, segment
, &data
,
1354 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1360 data
= (int64_t)opx
->offset
;
1361 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1362 opx
->segment
, opx
->wrt
);
1367 if (opx
->segment
!= segment
) {
1369 out(offset
, segment
, &data
,
1370 OUT_REL2ADR
, insn_end
- offset
,
1371 opx
->segment
, opx
->wrt
);
1373 data
= opx
->offset
- insn_end
;
1374 out(offset
, segment
, &data
,
1375 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1381 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1382 size
= (opx
->type
& BITS16
) ? 2 : 4;
1384 size
= (bits
== 16) ? 2 : 4;
1385 if (opx
->segment
!= segment
) {
1387 out(offset
, segment
, &data
,
1388 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1389 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1391 data
= opx
->offset
- insn_end
;
1392 out(offset
, segment
, &data
,
1393 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1399 if (opx
->segment
!= segment
) {
1401 out(offset
, segment
, &data
,
1402 OUT_REL4ADR
, insn_end
- offset
,
1403 opx
->segment
, opx
->wrt
);
1405 data
= opx
->offset
- insn_end
;
1406 out(offset
, segment
, &data
,
1407 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1413 if (opx
->segment
== NO_SEG
)
1414 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1417 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1418 outfmt
->segbase(1 + opx
->segment
),
1425 warn_overflow_opd(opx
, 2);
1426 if (is_sbyte16(opx
)) {
1428 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1432 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1433 opx
->segment
, opx
->wrt
);
1440 bytes
[0] = *codes
++;
1441 if (is_sbyte16(opx
))
1442 bytes
[0] |= 2; /* s-bit */
1443 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1449 warn_overflow_opd(opx
, 4);
1450 if (is_sbyte32(opx
)) {
1452 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1456 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1457 opx
->segment
, opx
->wrt
);
1464 bytes
[0] = *codes
++;
1465 if (is_sbyte32(opx
))
1466 bytes
[0] |= 2; /* s-bit */
1467 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1477 (ins
->drexdst
<< 4) |
1478 (ins
->rex
& REX_OC
? 0x08 : 0) |
1479 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1481 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1487 opx
= &ins
->oprs
[c
>> 3];
1488 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1489 opx
= &ins
->oprs
[c
& 7];
1490 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1491 errfunc(ERR_NONFATAL
,
1492 "non-absolute expression not permitted as argument %d",
1495 if (opx
->offset
& ~15) {
1496 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1497 "four-bit argument exceeds bounds");
1499 bytes
[0] |= opx
->offset
& 15;
1501 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1507 opx
= &ins
->oprs
[c
>> 4];
1508 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1510 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1516 opx
= &ins
->oprs
[c
];
1517 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1518 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1524 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1525 (int32_t)data
!= (int64_t)data
) {
1526 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1527 "signed dword immediate exceeds bounds");
1529 if (is_sbyte32(opx
)) {
1531 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1535 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1536 opx
->segment
, opx
->wrt
);
1543 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1544 (int32_t)data
!= (int64_t)data
) {
1545 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1546 "signed dword immediate exceeds bounds");
1548 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1549 opx
->segment
, opx
->wrt
);
1556 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1557 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1558 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1559 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1560 ((~ins
->drexdst
& 15)<< 3) | (ins
->vex_wlp
& 07);
1561 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1565 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1566 ((~ins
->drexdst
& 15) << 3) | (ins
->vex_wlp
& 07);
1567 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1577 if (ins
->rex
& REX_W
)
1579 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1581 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1586 um
= (uint64_t)2 << (s
-1);
1589 if (uv
> 127 && uv
< (uint64_t)-128 &&
1590 (uv
< um
-128 || uv
> um
-1)) {
1591 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1592 "signed byte value exceeds bounds");
1594 if (opx
->segment
!= NO_SEG
) {
1596 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1597 opx
->segment
, opx
->wrt
);
1600 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1611 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1613 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1620 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1622 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1641 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1650 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1668 *bytes
= *codes
++ ^ condval
[ins
->condition
];
1669 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1678 *bytes
= c
- 0332 + 0xF2;
1679 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1684 if (ins
->rex
& REX_R
) {
1686 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1689 ins
->rex
&= ~(REX_L
|REX_R
);
1700 if (ins
->oprs
[0].segment
!= NO_SEG
)
1701 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1703 int64_t size
= ins
->oprs
[0].offset
;
1705 out(offset
, segment
, NULL
,
1706 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1717 switch (ins
->oprs
[0].basereg
) {
1732 "bizarre 8086 segment register received");
1734 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1741 switch (ins
->oprs
[0].basereg
) {
1750 "bizarre 386 segment register received");
1752 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1761 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1767 bytes
[0] = c
- 0362 + 0xf2;
1768 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1778 *bytes
= c
- 0366 + 0x66;
1779 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1789 *bytes
= bits
== 16 ? 3 : 5;
1790 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1813 struct operand
*opy
= &ins
->oprs
[op2
];
1816 /* pick rfield from operand b (opx) */
1817 rflags
= regflag(opx
);
1818 rfield
= nasm_regvals
[opx
->basereg
];
1820 /* rfield is constant */
1825 if (!process_ea(opy
, &ea_data
, bits
, ins
->addr_size
,
1827 errfunc(ERR_NONFATAL
, "invalid effective address");
1832 *p
++ = ea_data
.modrm
;
1833 if (ea_data
.sib_present
)
1836 /* DREX suffixes come between the SIB and the displacement */
1837 if (ins
->rex
& REX_D
) {
1838 *p
++ = (ins
->drexdst
<< 4) |
1839 (ins
->rex
& REX_OC
? 0x08 : 0) |
1840 (ins
->rex
& (REX_R
|REX_X
|REX_B
));
1845 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1848 * Make sure the address gets the right offset in case
1849 * the line breaks in the .lst file (BR 1197827)
1854 switch (ea_data
.bytes
) {
1864 if (opy
->segment
== segment
) {
1866 if (overflow_signed(data
, ea_data
.bytes
))
1867 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1868 out(offset
, segment
, &data
, OUT_ADDRESS
,
1869 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1871 /* overflow check in output/linker? */
1872 out(offset
, segment
, &data
, OUT_REL4ADR
,
1873 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1876 if (overflow_general(opy
->offset
, ins
->addr_size
>> 3) ||
1877 signed_bits(opy
->offset
, ins
->addr_size
) !=
1878 signed_bits(opy
->offset
, ea_data
.bytes
* 8))
1879 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1882 out(offset
, segment
, &data
, OUT_ADDRESS
,
1883 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1889 "Invalid amount of bytes (%d) for offset?!",
1898 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1899 ": instruction code \\%o (0x%02X) given", c
, c
);
1905 static opflags_t
regflag(const operand
* o
)
1907 if (!is_register(o
->basereg
))
1908 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 (!is_register(o
->basereg
))
1915 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1916 return nasm_regvals
[o
->basereg
];
1919 static int op_rexflags(const operand
* o
, int mask
)
1924 if (!is_register(o
->basereg
))
1925 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1927 flags
= nasm_reg_flags
[o
->basereg
];
1928 val
= nasm_regvals
[o
->basereg
];
1930 return rexflags(val
, flags
, mask
);
1933 static int rexflags(int val
, opflags_t flags
, int mask
)
1938 rex
|= REX_B
|REX_X
|REX_R
;
1941 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1943 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1949 static enum match_result
find_match(const struct itemplate
**tempp
,
1951 int32_t segment
, int64_t offset
, int bits
)
1953 const struct itemplate
*temp
;
1954 enum match_result m
, merr
;
1955 opflags_t xsizeflags
[MAX_OPERANDS
];
1956 bool opsizemissing
= false;
1959 for (i
= 0; i
< instruction
->operands
; i
++)
1960 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
1962 merr
= MERR_INVALOP
;
1964 for (temp
= nasm_instructions
[instruction
->opcode
];
1965 temp
->opcode
!= I_none
; temp
++) {
1966 m
= matches(temp
, instruction
, bits
);
1967 if (m
== MOK_JUMP
) {
1968 if (jmp_match(segment
, offset
, bits
, instruction
, temp
->code
))
1972 } else if (m
== MERR_OPSIZEMISSING
&&
1973 (temp
->flags
& IF_SMASK
) != IF_SX
) {
1975 * Missing operand size and a candidate for fuzzy matching...
1977 for (i
= 0; i
< temp
->operands
; i
++) {
1978 if ((temp
->opd
[i
] & SAME_AS
) == 0)
1979 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
1981 opsizemissing
= true;
1985 if (merr
== MOK_GOOD
)
1989 /* No match, but see if we can get a fuzzy operand size match... */
1993 for (i
= 0; i
< instruction
->operands
; i
++) {
1995 * We ignore extrinsic operand sizes on registers, so we should
1996 * never try to fuzzy-match on them. This also resolves the case
1997 * when we have e.g. "xmmrm128" in two different positions.
1999 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2002 /* This tests if xsizeflags[i] has more than one bit set */
2003 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2004 goto done
; /* No luck */
2006 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2009 /* Try matching again... */
2010 for (temp
= nasm_instructions
[instruction
->opcode
];
2011 temp
->opcode
!= I_none
; temp
++) {
2012 m
= matches(temp
, instruction
, bits
);
2013 if (m
== MOK_JUMP
) {
2014 if (jmp_match(segment
, offset
, bits
, instruction
, temp
->code
))
2021 if (merr
== MOK_GOOD
)
2030 static enum match_result
matches(const struct itemplate
*itemp
,
2031 insn
*instruction
, int bits
)
2033 int i
, size
[MAX_OPERANDS
], asize
, oprs
;
2034 bool opsizemissing
= false;
2039 if (itemp
->opcode
!= instruction
->opcode
)
2040 return MERR_INVALOP
;
2043 * Count the operands
2045 if (itemp
->operands
!= instruction
->operands
)
2046 return MERR_INVALOP
;
2051 if (!(optimizing
> 0) && (itemp
->flags
& IF_OPT
))
2052 return MERR_INVALOP
;
2055 * Check that no spurious colons or TOs are present
2057 for (i
= 0; i
< itemp
->operands
; i
++)
2058 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2059 return MERR_INVALOP
;
2062 * Process size flags
2064 switch (itemp
->flags
& IF_SMASK
) {
2104 if (itemp
->flags
& IF_ARMASK
) {
2105 /* S- flags only apply to a specific operand */
2106 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2107 memset(size
, 0, sizeof size
);
2110 /* S- flags apply to all operands */
2111 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2116 * Check that the operand flags all match up,
2117 * it's a bit tricky so lets be verbose:
2119 * 1) Find out the size of operand. If instruction
2120 * doesn't have one specified -- we're trying to
2121 * guess it either from template (IF_S* flag) or
2124 * 2) If template operand (i) has SAME_AS flag [used for registers only]
2125 * (ie the same operand as was specified somewhere in template, and
2126 * this referred operand index is being achieved via ~SAME_AS)
2127 * we are to be sure that both registers (in template and instruction)
2130 * 3) If template operand do not match the instruction OR
2131 * template has an operand size specified AND this size differ
2132 * from which instruction has (perhaps we got it from code bits)
2134 * a) Check that only size of instruction and operand is differ
2135 * other characteristics do match
2136 * b) Perhaps it's a register specified in instruction so
2137 * for such a case we just mark that operand as "size
2138 * missing" and this will turn on fuzzy operand size
2139 * logic facility (handled by a caller)
2141 for (i
= 0; i
< itemp
->operands
; i
++) {
2142 opflags_t type
= instruction
->oprs
[i
].type
;
2143 if (!(type
& SIZE_MASK
))
2146 if (itemp
->opd
[i
] & SAME_AS
) {
2147 int j
= itemp
->opd
[i
] & ~SAME_AS
;
2148 if (type
!= instruction
->oprs
[j
].type
||
2149 instruction
->oprs
[i
].basereg
!= instruction
->oprs
[j
].basereg
)
2150 return MERR_INVALOP
;
2151 } else if (itemp
->opd
[i
] & ~type
||
2152 ((itemp
->opd
[i
] & SIZE_MASK
) &&
2153 ((itemp
->opd
[i
] ^ type
) & SIZE_MASK
))) {
2154 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) || (type
& SIZE_MASK
)) {
2155 return MERR_INVALOP
;
2156 } else if (!is_class(REGISTER
, type
)) {
2158 * Note: we don't honor extrinsic operand sizes for registers,
2159 * so "missing operand size" for a register should be
2160 * considered a wildcard match rather than an error.
2162 opsizemissing
= true;
2168 return MERR_OPSIZEMISSING
;
2171 * Check operand sizes
2173 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2174 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2175 for (i
= 0; i
< oprs
; i
++) {
2176 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2178 for (i
= 0; i
< oprs
; i
++)
2184 oprs
= itemp
->operands
;
2187 for (i
= 0; i
< itemp
->operands
; i
++) {
2188 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2189 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2190 return MERR_OPSIZEMISMATCH
;
2194 * Check template is okay at the set cpu level
2196 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2200 * Verify the appropriate long mode flag.
2202 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2203 return MERR_BADMODE
;
2206 * Check if special handling needed for Jumps
2208 if ((itemp
->code
[0] & 0374) == 0370)
2214 static ea
*process_ea(operand
* input
, ea
* output
, int bits
,
2215 int addrbits
, int rfield
, opflags_t rflags
)
2217 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2219 output
->rip
= false;
2221 /* REX flags for the rfield operand */
2222 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2224 if (is_class(REGISTER
, input
->type
)) { /* register direct */
2228 if (!is_register(input
->basereg
))
2231 i
= nasm_regvals
[input
->basereg
];
2234 return NULL
; /* Invalid EA register */
2236 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2238 output
->sib_present
= false; /* no SIB necessary */
2239 output
->bytes
= 0; /* no offset necessary either */
2240 output
->modrm
= 0xC0 | ((rfield
& 7) << 3) | (i
& 7);
2241 } else { /* it's a memory reference */
2242 if (input
->basereg
== -1 &&
2243 (input
->indexreg
== -1 || input
->scale
== 0)) {
2244 /* it's a pure offset */
2246 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2247 input
->segment
== NO_SEG
) {
2248 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2249 input
->type
&= ~IP_REL
;
2250 input
->type
|= MEMORY
;
2253 if (input
->eaflags
& EAF_BYTEOFFS
||
2254 (input
->eaflags
& EAF_WORDOFFS
&&
2255 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2256 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2259 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2260 int scale
, index
, base
;
2261 output
->sib_present
= true;
2265 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2267 output
->modrm
= 4 | ((rfield
& 7) << 3);
2268 output
->rip
= false;
2270 output
->sib_present
= false;
2271 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2272 output
->modrm
= (addrbits
!= 16 ? 5 : 6) | ((rfield
& 7) << 3);
2273 output
->rip
= bits
== 64;
2275 } else { /* it's an indirection */
2276 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2277 int32_t seg
= input
->segment
;
2278 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2279 int t
, it
, bt
; /* register numbers */
2280 opflags_t x
, ix
, bx
; /* register flags */
2283 i
= -1; /* make this easy, at least */
2285 if (is_register(i
)) {
2286 it
= nasm_regvals
[i
];
2287 ix
= nasm_reg_flags
[i
];
2293 if (is_register(b
)) {
2294 bt
= nasm_regvals
[b
];
2295 bx
= nasm_reg_flags
[b
];
2301 /* check for a 32/64-bit memory reference... */
2302 if ((ix
|bx
) & (BITS32
|BITS64
)) {
2304 * it must be a 32/64-bit memory reference. Firstly we have
2305 * to check that all registers involved are type E/Rxx.
2307 int32_t sok
= BITS32
| BITS64
, o
= input
->offset
;
2310 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2318 return NULL
; /* Invalid register */
2319 if (~sok
& bx
& SIZE_MASK
)
2320 return NULL
; /* Invalid size */
2325 * While we're here, ensure the user didn't specify
2328 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2331 if (addrbits
== 16 ||
2332 (addrbits
== 32 && !(sok
& BITS32
)) ||
2333 (addrbits
== 64 && !(sok
& BITS64
)))
2336 /* now reorganize base/index */
2337 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2338 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2339 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2340 /* swap if hints say so */
2341 t
= bt
, bt
= it
, it
= t
;
2342 x
= bx
, bx
= ix
, ix
= x
;
2344 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2345 bt
= -1, bx
= 0, s
++;
2346 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2347 /* make single reg base, unless hint */
2348 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2350 if (((s
== 2 && it
!= REG_NUM_ESP
&& !(input
->eaflags
& EAF_TIMESTWO
)) ||
2351 s
== 3 || s
== 5 || s
== 9) && bt
== -1)
2352 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2353 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2354 (input
->eaflags
& EAF_TIMESTWO
))
2355 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2356 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2357 if (s
== 1 && it
== REG_NUM_ESP
) {
2358 /* swap ESP into base if scale is 1 */
2359 t
= it
, it
= bt
, bt
= t
;
2360 x
= ix
, ix
= bx
, bx
= x
;
2362 if (it
== REG_NUM_ESP
||
2363 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2364 return NULL
; /* wrong, for various reasons */
2366 output
->rex
|= rexflags(it
, ix
, REX_X
);
2367 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2369 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2378 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2379 seg
== NO_SEG
&& !forw_ref
&&
2380 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2382 else if (input
->eaflags
& EAF_BYTEOFFS
||
2383 (o
>= -128 && o
<= 127 &&
2384 seg
== NO_SEG
&& !forw_ref
&&
2385 !(input
->eaflags
& EAF_WORDOFFS
)))
2391 output
->sib_present
= false;
2392 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2393 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2396 int mod
, scale
, index
, base
;
2416 default: /* then what the smeg is it? */
2417 return NULL
; /* panic */
2425 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2426 seg
== NO_SEG
&& !forw_ref
&&
2427 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2429 else if (input
->eaflags
& EAF_BYTEOFFS
||
2430 (o
>= -128 && o
<= 127 &&
2431 seg
== NO_SEG
&& !forw_ref
&&
2432 !(input
->eaflags
& EAF_WORDOFFS
)))
2438 output
->sib_present
= true;
2439 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2440 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | 4;
2441 output
->sib
= (scale
<< 6) | (index
<< 3) | base
;
2443 } else { /* it's 16-bit */
2445 int16_t o
= input
->offset
;
2447 /* check for 64-bit long mode */
2451 /* check all registers are BX, BP, SI or DI */
2452 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2453 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2456 /* ensure the user didn't specify DWORD/QWORD */
2457 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2460 if (s
!= 1 && i
!= -1)
2461 return NULL
; /* no can do, in 16-bit EA */
2462 if (b
== -1 && i
!= -1) {
2467 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2472 /* have BX/BP as base, SI/DI index */
2474 return NULL
; /* shouldn't ever happen, in theory */
2475 if (i
!= -1 && b
!= -1 &&
2476 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2477 return NULL
; /* invalid combinations */
2478 if (b
== -1) /* pure offset: handled above */
2479 return NULL
; /* so if it gets to here, panic! */
2483 switch (i
* 256 + b
) {
2484 case R_SI
* 256 + R_BX
:
2487 case R_DI
* 256 + R_BX
:
2490 case R_SI
* 256 + R_BP
:
2493 case R_DI
* 256 + R_BP
:
2511 if (rm
== -1) /* can't happen, in theory */
2512 return NULL
; /* so panic if it does */
2514 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2515 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2517 else if (input
->eaflags
& EAF_BYTEOFFS
||
2518 (o
>= -128 && o
<= 127 && seg
== NO_SEG
&&
2519 !forw_ref
&& !(input
->eaflags
& EAF_WORDOFFS
)))
2524 output
->sib_present
= false; /* no SIB - it's 16-bit */
2525 output
->bytes
= mod
; /* bytes of offset needed */
2526 output
->modrm
= (mod
<< 6) | ((rfield
& 7) << 3) | rm
;
2531 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2535 static void add_asp(insn
*ins
, int addrbits
)
2540 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2542 switch (ins
->prefixes
[PPS_ASIZE
]) {
2553 valid
&= (addrbits
== 32) ? 16 : 32;
2559 for (j
= 0; j
< ins
->operands
; j
++) {
2560 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2563 /* Verify as Register */
2564 if (!is_register(ins
->oprs
[j
].indexreg
))
2567 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2569 /* Verify as Register */
2570 if (!is_register(ins
->oprs
[j
].basereg
))
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
) != 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
2623 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);