blob: 1d8c3c104c3bfc55affc9a428aff01c7a0f16df5 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000183#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
185#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000186#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230static efunc errfunc;
231static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000232static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800234static int64_t calcsize(int32_t, int64_t, int, insn *,
235 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700236static void gencode(int32_t segment, int64_t offset, int bits,
237 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700239static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400240 insn *instruction,
241 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700242static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700245static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000246static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700248static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700250static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700251
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400252static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000253{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700254 return ins->prefixes[pos] == prefix;
255}
256
257static void assert_no_prefix(insn * ins, enum prefix_pos pos)
258{
259 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 errfunc(ERR_NONFATAL, "invalid %s prefix",
261 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262}
263
264static const char *size_name(int size)
265{
266 switch (size) {
267 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700279 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700281 case 64:
282 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700283 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400284 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000285 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700286}
287
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400288static void warn_overflow(int pass, int size)
289{
290 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
291 "%s data exceeds bounds", size_name(size));
292}
293
294static void warn_overflow_const(int64_t data, int size)
295{
296 if (overflow_general(data, size))
297 warn_overflow(ERR_PASS1, size);
298}
299
300static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700301{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100302 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303 if (overflow_general(o->offset, size))
304 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700305 }
306}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400307
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000308/*
309 * This routine wrappers the real output format's output routine,
310 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800311 * generator at the same time, flatten unnecessary relocations,
312 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000313 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800314static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800315 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400316 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000317{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000318 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000319 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800320 uint8_t p[8];
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800321 const int asize = abs((int)size); /* True address size */
322 const int abits = asize << 3; /* Address size in bits */
323 const int amax = maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800325 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400326 /*
327 * This is a non-relocated address, and we're going to
328 * convert it into RAWDATA format.
329 */
330 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800331
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800332 if (asize > 8) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400333 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
334 return;
335 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700336
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800337 WRITEADDR(q, *(int64_t *)data, asize);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400338 data = p;
339 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340 }
341
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800342 list->output(offset, data, type, size);
343
Frank Kotlerabebb082003-09-06 04:45:37 +0000344 /*
345 * this call to src_get determines when we call the
346 * debug-format-specific "linenum" function
347 * it updates lineno and lnfname to the current values
348 * returning 0 if "same as last time", -2 if lnfname
349 * changed, and the amount by which lineno changed,
350 * if it did. thus, these variables must be static
351 */
352
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400353 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000355
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800356 if (type == OUT_ADDRESS && abits > maxbits) {
357 if (asize < 0) {
358 errfunc(ERR_NONFATAL,
359 "%d-bit signed relocation unsupported by output format %s\n",
360 abits, outfmt->shortname);
361 } else {
362 errfunc(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800363 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800364 abits, maxbits);
365 outfmt->output(segto, data, type, amax, segment, wrt);
366 size -= amax;
367 }
368 data = zero_buffer;
369 type = OUT_RAWDATA;
370 }
371
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800372 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000373}
374
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800375static void out_imm8(int64_t offset, int32_t segment,
376 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400377{
378 if (opx->segment != NO_SEG) {
379 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800380 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400381 } else {
382 uint8_t byte = opx->offset;
383 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
384 }
385}
386
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700387static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800388 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800390 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800391 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000392 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800393 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000394
H. Peter Anvin755f5212012-02-25 11:41:34 -0800395 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700396 return false;
397 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400398 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700399 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400400 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700401
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800402 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100403
Victor van den Elzen154e5922009-02-25 17:32:00 +0100404 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100405 /* Be optimistic in pass 1 */
406 return true;
407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700409 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000410
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700411 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800412 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
413
414 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
415 /* jmp short (opcode eb) cannot be used with bnd prefix. */
416 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800417 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
418 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800419 }
420
421 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000423
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400424int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400425 insn * instruction, struct ofmt *output, efunc error,
426 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000427{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000428 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700430 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800431 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000432 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800433 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300434 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000435
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000437 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 outfmt = output; /* likewise */
439 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000440
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300441 wsize = idata_bytes(instruction->opcode);
442 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000444
H. Peter Anvineba20a72002-04-30 20:53:55 +0000445 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000447 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 if (t < 0)
449 errfunc(ERR_PANIC,
450 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000451
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400453 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400455 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700456 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400457 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000458 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700459 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800461 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400462 offset += wsize;
463 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700464 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400465 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800469 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 if (align) {
473 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100474 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800475 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 }
477 offset += e->stringlen + align;
478 }
479 }
480 if (t > 0 && t == instruction->times - 1) {
481 /*
482 * Dummy call to list->output to give the offset to the
483 * listing module.
484 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800485 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 list->uplevel(LIST_TIMES);
487 }
488 }
489 if (instruction->times > 1)
490 list->downlevel(LIST_TIMES);
491 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 }
493
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700495 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400498 fp = fopen(fname, "rb");
499 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
501 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400502 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
504 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200505 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400506 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700507 static char buf[4096];
508 size_t t = instruction->times;
509 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400510 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 len = ftell(fp);
513 if (instruction->eops->next) {
514 base = instruction->eops->next->offset;
515 len -= base;
516 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700517 len > (size_t)instruction->eops->next->next->offset)
518 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 }
520 /*
521 * Dummy call to list->output to give the offset to the
522 * listing module.
523 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800524 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 list->uplevel(LIST_INCBIN);
526 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700527 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 fseek(fp, base, SEEK_SET);
530 l = len;
531 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700532 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400533 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 if (!m) {
535 /*
536 * This shouldn't happen unless the file
537 * actually changes while we are reading
538 * it.
539 */
540 error(ERR_NONFATAL,
541 "`incbin': unexpected EOF while"
542 " reading file `%s'", fname);
543 t = 0; /* Try to exit cleanly */
544 break;
545 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800546 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 NO_SEG, NO_SEG);
548 l -= m;
549 }
550 }
551 list->downlevel(LIST_INCBIN);
552 if (instruction->times > 1) {
553 /*
554 * Dummy call to list->output to give the offset to the
555 * listing module.
556 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800557 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 list->uplevel(LIST_TIMES);
559 list->downlevel(LIST_TIMES);
560 }
561 fclose(fp);
562 return instruction->times * len;
563 }
564 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565 }
566
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700567 /* Check to see if we need an address-size prefix */
568 add_asp(instruction, bits);
569
H. Peter Anvin23595f52009-07-25 17:44:25 -0700570 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700571
H. Peter Anvin23595f52009-07-25 17:44:25 -0700572 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400573 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800574 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400575 itimes = instruction->times;
576 if (insn_size < 0) /* shouldn't be, on pass two */
577 error(ERR_PANIC, "errors made it through from pass one");
578 else
579 while (itimes--) {
580 for (j = 0; j < MAXPREFIX; j++) {
581 uint8_t c = 0;
582 switch (instruction->prefixes[j]) {
583 case P_WAIT:
584 c = 0x9B;
585 break;
586 case P_LOCK:
587 c = 0xF0;
588 break;
589 case P_REPNE:
590 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800591 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700592 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400593 c = 0xF2;
594 break;
595 case P_REPE:
596 case P_REPZ:
597 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800598 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400599 c = 0xF3;
600 break;
601 case R_CS:
602 if (bits == 64) {
603 error(ERR_WARNING | ERR_PASS2,
604 "cs segment base generated, but will be ignored in 64-bit mode");
605 }
606 c = 0x2E;
607 break;
608 case R_DS:
609 if (bits == 64) {
610 error(ERR_WARNING | ERR_PASS2,
611 "ds segment base generated, but will be ignored in 64-bit mode");
612 }
613 c = 0x3E;
614 break;
615 case R_ES:
616 if (bits == 64) {
617 error(ERR_WARNING | ERR_PASS2,
618 "es segment base generated, but will be ignored in 64-bit mode");
619 }
620 c = 0x26;
621 break;
622 case R_FS:
623 c = 0x64;
624 break;
625 case R_GS:
626 c = 0x65;
627 break;
628 case R_SS:
629 if (bits == 64) {
630 error(ERR_WARNING | ERR_PASS2,
631 "ss segment base generated, but will be ignored in 64-bit mode");
632 }
633 c = 0x36;
634 break;
635 case R_SEGR6:
636 case R_SEGR7:
637 error(ERR_NONFATAL,
638 "segr6 and segr7 cannot be used as prefixes");
639 break;
640 case P_A16:
641 if (bits == 64) {
642 error(ERR_NONFATAL,
643 "16-bit addressing is not supported "
644 "in 64-bit mode");
645 } else if (bits != 16)
646 c = 0x67;
647 break;
648 case P_A32:
649 if (bits != 32)
650 c = 0x67;
651 break;
652 case P_A64:
653 if (bits != 64) {
654 error(ERR_NONFATAL,
655 "64-bit addressing is only supported "
656 "in 64-bit mode");
657 }
658 break;
659 case P_ASP:
660 c = 0x67;
661 break;
662 case P_O16:
663 if (bits != 16)
664 c = 0x66;
665 break;
666 case P_O32:
667 if (bits == 16)
668 c = 0x66;
669 break;
670 case P_O64:
671 /* REX.W */
672 break;
673 case P_OSP:
674 c = 0x66;
675 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700676 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800677 case P_VEX3:
678 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800679 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400680 case P_none:
681 break;
682 default:
683 error(ERR_PANIC, "invalid instruction prefix");
684 }
685 if (c != 0) {
686 out(offset, segment, &c, OUT_RAWDATA, 1,
687 NO_SEG, NO_SEG);
688 offset++;
689 }
690 }
691 insn_end = offset + insn_size;
692 gencode(segment, offset, bits, instruction,
693 temp, insn_end);
694 offset += insn_size;
695 if (itimes > 0 && itimes == instruction->times - 1) {
696 /*
697 * Dummy call to list->output to give the offset to the
698 * listing module.
699 */
700 list->output(offset, NULL, OUT_RAWDATA, 0);
701 list->uplevel(LIST_TIMES);
702 }
703 }
704 if (instruction->times > 1)
705 list->downlevel(LIST_TIMES);
706 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700707 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400708 /* No match */
709 switch (m) {
710 case MERR_OPSIZEMISSING:
711 error(ERR_NONFATAL, "operation size not specified");
712 break;
713 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400715 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700716 case MERR_BRNUMMISMATCH:
717 error(ERR_NONFATAL,
718 "mismatch in the number of broadcasting elements");
719 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400720 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400722 break;
723 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800724 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400725 bits);
726 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800727 case MERR_ENCMISMATCH:
728 error(ERR_NONFATAL, "specific encoding scheme not available");
729 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800730 case MERR_BADBND:
731 error(ERR_NONFATAL, "bnd prefix is not allowed");
732 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800733 case MERR_BADREPNE:
734 error(ERR_NONFATAL, "%s prefix is not allowed",
735 (has_prefix(instruction, PPS_REP, P_REPNE) ?
736 "repne" : "repnz"));
737 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400738 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 error(ERR_NONFATAL,
740 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400741 break;
742 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743 }
744 return 0;
745}
746
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400747int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400748 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000749{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000750 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700751 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000752
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000754 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000755
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400756 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000758
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700759 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
760 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 instruction->opcode == I_DT || instruction->opcode == I_DO ||
762 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300764 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000765
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300767 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000768
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400769 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000770 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400773 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400775 warn_overflow_const(e->offset, wsize);
776 } else if (e->type == EOT_DB_STRING ||
777 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000779
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 align = (-osize) % wsize;
781 if (align < 0)
782 align += wsize;
783 isize += osize + align;
784 }
785 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786 }
787
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400789 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300791 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700792 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000793
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400794 fp = fopen(fname, "rb");
795 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
797 fname);
798 else if (fseek(fp, 0L, SEEK_END) < 0)
799 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
800 fname);
801 else {
802 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (instruction->eops->next) {
804 len -= instruction->eops->next->offset;
805 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700806 len > (size_t)instruction->eops->next->next->offset) {
807 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 }
809 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300810 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300812 if (fp)
813 fclose(fp);
814 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000815 }
816
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700817 /* Check to see if we need an address-size prefix */
818 add_asp(instruction, bits);
819
H. Peter Anvin23595f52009-07-25 17:44:25 -0700820 m = find_match(&temp, instruction, segment, offset, bits);
821 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400822 /* we've matched an instruction. */
823 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400824 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100825
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800826 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400827 if (isize < 0)
828 return -1;
829 for (j = 0; j < MAXPREFIX; j++) {
830 switch (instruction->prefixes[j]) {
831 case P_A16:
832 if (bits != 16)
833 isize++;
834 break;
835 case P_A32:
836 if (bits != 32)
837 isize++;
838 break;
839 case P_O16:
840 if (bits != 16)
841 isize++;
842 break;
843 case P_O32:
844 if (bits == 16)
845 isize++;
846 break;
847 case P_A64:
848 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700849 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800850 case P_VEX3:
851 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800852 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400853 case P_none:
854 break;
855 default:
856 isize++;
857 break;
858 }
859 }
860 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700861 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400862 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000863 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000864}
865
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800866static void bad_hle_warn(const insn * ins, uint8_t hleok)
867{
868 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800869 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800870 static const enum whatwarn warn[2][4] =
871 {
872 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
873 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
874 };
875 unsigned int n;
876
877 n = (unsigned int)rep_pfx - P_XACQUIRE;
878 if (n > 1)
879 return; /* Not XACQUIRE/XRELEASE */
880
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800881 ww = warn[n][hleok];
882 if (!is_class(MEMORY, ins->oprs[0].type))
883 ww = w_inval; /* HLE requires operand 0 to be memory */
884
885 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800886 case w_none:
887 break;
888
889 case w_lock:
890 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800891 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800892 "%s with this instruction requires lock",
893 prefix_name(rep_pfx));
894 }
895 break;
896
897 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800898 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800899 "%s invalid with this instruction",
900 prefix_name(rep_pfx));
901 break;
902 }
903}
904
H. Peter Anvin507ae032008-10-09 15:37:10 -0700905/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400906#define case3(x) case (x): case (x)+1: case (x)+2
907#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700908
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800909static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800910 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000911{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800912 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800913 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000914 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000915 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700916 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700917 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700918 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700919 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800920 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800921 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700922 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700924 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700925 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700926 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700927
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700928 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400929 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700930
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 (void)segment; /* Don't warn that this parameter is unused */
932 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000933
H. Peter Anvin839eca22007-10-29 23:12:47 -0700934 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 c = *codes++;
936 op1 = (c & 3) + ((opex & 1) << 2);
937 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
938 opx = &ins->oprs[op1];
939 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940
H. Peter Anvin839eca22007-10-29 23:12:47 -0700941 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400942 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 codes += c, length += c;
944 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700945
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400946 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 opex = c;
948 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(010):
951 ins->rex |=
952 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 codes++, length++;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Jin Kyu Song164d6072013-10-15 19:10:13 -0700956 case4(014):
957 /* this is an index reg of MIB operand */
958 mib_index = opx->basereg;
959 break;
960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(020):
962 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 length++;
964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 length += 2;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700971 if (opx->type & (BITS16 | BITS32 | BITS64))
972 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 else
974 length += (bits == 16) ? 2 : 4;
975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 length += 4;
979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700982 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length++;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000990 length += 8; /* MOV reg64/imm */
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 length += 2;
995 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700998 if (opx->type & (BITS16 | BITS32 | BITS64))
999 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 else
1001 length += (bits == 16) ? 2 : 4;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 length += 4;
1006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001009 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case 0172:
1013 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001015 length++;
1016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001018 case4(0174):
1019 length++;
1020 break;
1021
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001022 case4(0240):
1023 ins->rex |= REX_EV;
1024 ins->vexreg = regval(opx);
1025 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1026 ins->vex_cm = *codes++;
1027 ins->vex_wlp = *codes++;
1028 ins->evex_tuple = (*codes++ - 0300);
1029 break;
1030
1031 case 0250:
1032 ins->rex |= REX_EV;
1033 ins->vexreg = 0;
1034 ins->vex_cm = *codes++;
1035 ins->vex_wlp = *codes++;
1036 ins->evex_tuple = (*codes++ - 0300);
1037 break;
1038
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001039 case4(0254):
1040 length += 4;
1041 break;
1042
1043 case4(0260):
1044 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001045 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001046 ins->vex_cm = *codes++;
1047 ins->vex_wlp = *codes++;
1048 break;
1049
1050 case 0270:
1051 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001052 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001053 ins->vex_cm = *codes++;
1054 ins->vex_wlp = *codes++;
1055 break;
1056
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001057 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001058 hleok = c & 3;
1059 break;
1060
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 case4(0274):
1062 length++;
1063 break;
1064
1065 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001069 if (bits == 64)
1070 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001071 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001075 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
Keith Kaniosb7a89542007-04-12 02:40:54 +00001081 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1083 has_prefix(ins, PPS_ASIZE, P_A32))
1084 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 case4(0314):
1088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001091 {
1092 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1093 if (pfx == P_O16)
1094 break;
1095 if (pfx != P_none)
1096 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1097 else
1098 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001100 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001103 {
1104 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1105 if (pfx == P_O32)
1106 break;
1107 if (pfx != P_none)
1108 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1109 else
1110 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001112 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 case 0322:
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Keith Kaniosb7a89542007-04-12 02:40:54 +00001117 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001118 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Keith Kaniosb7a89542007-04-12 02:40:54 +00001121 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001122 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001125 case 0325:
1126 ins->rex |= REX_NH;
1127 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001128
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001129 case 0326:
1130 break;
1131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0330:
1133 codes++, length++;
1134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001139 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 0333:
1141 length++;
1142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 case 0334:
1145 ins->rex |= REX_L;
1146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001148 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001152 if (!ins->prefixes[PPS_REP])
1153 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001157 if (!ins->prefixes[PPS_REP])
1158 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 if (ins->oprs[0].segment != NO_SEG)
1163 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1164 " quantity of BSS space");
1165 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001166 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case 0341:
1170 if (!ins->prefixes[PPS_WAIT])
1171 ins->prefixes[PPS_WAIT] = P_WAIT;
1172 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0360:
1175 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001176
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001177 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001178 length++;
1179 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 case 0364:
1182 case 0365:
1183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
Keith Kanios48af1772007-08-17 07:37:52 +00001185 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001186 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001187 length++;
1188 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001189
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001190 case 0370:
1191 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001193
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 case 0373:
1195 length++;
1196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001198 case 0374:
1199 eat = EA_XMMVSIB;
1200 break;
1201
1202 case 0375:
1203 eat = EA_YMMVSIB;
1204 break;
1205
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001206 case 0376:
1207 eat = EA_ZMMVSIB;
1208 break;
1209
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001210 case4(0100):
1211 case4(0110):
1212 case4(0120):
1213 case4(0130):
1214 case4(0200):
1215 case4(0204):
1216 case4(0210):
1217 case4(0214):
1218 case4(0220):
1219 case4(0224):
1220 case4(0230):
1221 case4(0234):
1222 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001224 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 opflags_t rflags;
1226 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001227 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001228
Keith Kaniosb7a89542007-04-12 02:40:54 +00001229 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001230
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001231 if (c <= 0177) {
1232 /* pick rfield from operand b (opx) */
1233 rflags = regflag(opx);
1234 rfield = nasm_regvals[opx->basereg];
1235 } else {
1236 rflags = 0;
1237 rfield = c & 7;
1238 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001239
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001240 /* EVEX.b1 : evex_brerop contains the operand position */
1241 op_er_sae = (ins->evex_brerop >= 0 ?
1242 &ins->oprs[ins->evex_brerop] : NULL);
1243
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001244 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1245 /* set EVEX.b */
1246 ins->evex_p[2] |= EVEX_P2B;
1247 if (op_er_sae->decoflags & ER) {
1248 /* set EVEX.RC (rounding control) */
1249 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1250 & EVEX_P2RC;
1251 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001252 } else {
1253 /* set EVEX.L'L (vector length) */
1254 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001255 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001256 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001257 /* set EVEX.b */
1258 ins->evex_p[2] |= EVEX_P2B;
1259 }
1260 }
1261
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001262 if (itemp_has(temp, IF_MIB)) {
1263 opy->eaflags |= EAF_MIB;
1264 /*
1265 * if a separate form of MIB (ICC style) is used,
1266 * the index reg info is merged into mem operand
1267 */
1268 if (mib_index != R_none) {
1269 opy->indexreg = mib_index;
1270 opy->scale = 1;
1271 opy->hintbase = mib_index;
1272 opy->hinttype = EAH_NOTBASE;
1273 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001274 }
1275
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001276 if (process_ea(opy, &ea_data, bits,
1277 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 errfunc(ERR_NONFATAL, "invalid effective address");
1279 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001280 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001281 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001283 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 }
1285 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001286
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 default:
1288 errfunc(ERR_PANIC, "internal instruction table corrupt"
1289 ": instruction code \\%o (0x%02X) given", c, c);
1290 break;
1291 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001292 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001293
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001294 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001295
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001296 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 if (ins->rex & REX_H) {
1298 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1299 return -1;
1300 }
1301 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001302 }
1303
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001304 switch (ins->prefixes[PPS_VEX]) {
1305 case P_EVEX:
1306 if (!(ins->rex & REX_EV))
1307 return -1;
1308 break;
1309 case P_VEX3:
1310 case P_VEX2:
1311 if (!(ins->rex & REX_V))
1312 return -1;
1313 break;
1314 default:
1315 break;
1316 }
1317
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001318 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001320
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001322 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 return -1;
1324 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001325 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001327 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 ins->rex &= ~REX_W;
1329 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001330 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 ins->rex |= REX_W;
1332 bad32 &= ~REX_W;
1333 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001334 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001335 /* Follow REX_W */
1336 break;
1337 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001338
H. Peter Anvinfc561202011-07-07 16:58:22 -07001339 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1341 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001342 } else if (!(ins->rex & REX_EV) &&
1343 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1344 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1345 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001347 if (ins->rex & REX_EV)
1348 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001349 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1350 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 length += 3;
1352 else
1353 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001354 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 if (ins->rex & REX_H) {
1356 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1357 return -1;
1358 } else if (bits == 64) {
1359 length++;
1360 } else if ((ins->rex & REX_L) &&
1361 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001362 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001364 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001365 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 length++;
1367 } else {
1368 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1369 return -1;
1370 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001371 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001372
1373 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001374 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001375 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001376 "instruction is not lockable");
1377 }
1378
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001379 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001380
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001381 /*
1382 * when BND prefix is set by DEFAULT directive,
1383 * BND prefix is added to every appropriate instruction line
1384 * unless it is overridden by NOBND prefix.
1385 */
1386 if (globalbnd &&
1387 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1388 ins->prefixes[PPS_REP] = P_BND;
1389
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001390 return length;
1391}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001392
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001393static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1394{
1395 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001396 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001397 !(ins->rex & (REX_V | REX_EV)) &&
1398 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001399 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001400 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001401 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001402 return 1;
1403 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001404 }
1405
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001406 return 0;
1407}
1408
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001409static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001410 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001411 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001412{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001413 uint8_t c;
1414 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001415 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001416 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001417 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001418 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001419 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001420 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001421 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001422
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001423 ins->rex_done = false;
1424
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001426 c = *codes++;
1427 op1 = (c & 3) + ((opex & 1) << 2);
1428 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1429 opx = &ins->oprs[op1];
1430 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001431
H. Peter Anvin839eca22007-10-29 23:12:47 -07001432 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 case 01:
1434 case 02:
1435 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001436 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001437 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001438 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 codes += c;
1440 offset += c;
1441 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001442
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001443 case 05:
1444 case 06:
1445 case 07:
1446 opex = c;
1447 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001448
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001449 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001450 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001451 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001452 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 offset += 1;
1454 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001455
Jin Kyu Song164d6072013-10-15 19:10:13 -07001456 case4(014):
1457 break;
1458
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001460 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001461 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001464 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 offset += 1;
1466 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001467
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001468 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001469 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001470 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001472 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 offset += 1;
1474 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001475
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001476 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001477 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001478 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001479 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001480 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 offset += 2;
1482 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001483
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001484 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001485 if (opx->type & (BITS16 | BITS32))
1486 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 else
1488 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001489 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001490 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001491 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001492 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 offset += size;
1494 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001495
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001497 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001498 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001499 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 offset += 4;
1502 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001503
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001505 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001506 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001507 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001508 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001509 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 offset += size;
1511 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001512
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001513 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001514 if (opx->segment != segment) {
1515 data = opx->offset;
1516 out(offset, segment, &data,
1517 OUT_REL1ADR, insn_end - offset,
1518 opx->segment, opx->wrt);
1519 } else {
1520 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001521 if (data > 127 || data < -128)
1522 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001523 out(offset, segment, &data,
1524 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1525 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 offset += 1;
1527 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001528
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001529 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001530 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001531 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001532 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001533 offset += 8;
1534 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001535
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001537 if (opx->segment != segment) {
1538 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001540 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001541 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001543 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001545 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 }
1547 offset += 2;
1548 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001549
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001550 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001551 if (opx->type & (BITS16 | BITS32 | BITS64))
1552 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 else
1554 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001557 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1559 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001561 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001563 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 }
1565 offset += size;
1566 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001569 if (opx->segment != segment) {
1570 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001572 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001573 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001575 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001577 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 }
1579 offset += 4;
1580 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001583 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001584 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1585 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001587 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001588 outfmt->segbase(1 + opx->segment),
1589 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001590 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case 0172:
1594 c = *codes++;
1595 opx = &ins->oprs[c >> 3];
1596 bytes[0] = nasm_regvals[opx->basereg] << 4;
1597 opx = &ins->oprs[c & 7];
1598 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1599 errfunc(ERR_NONFATAL,
1600 "non-absolute expression not permitted as argument %d",
1601 c & 7);
1602 } else {
1603 if (opx->offset & ~15) {
1604 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1605 "four-bit argument exceeds bounds");
1606 }
1607 bytes[0] |= opx->offset & 15;
1608 }
1609 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1610 offset++;
1611 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case 0173:
1614 c = *codes++;
1615 opx = &ins->oprs[c >> 4];
1616 bytes[0] = nasm_regvals[opx->basereg] << 4;
1617 bytes[0] |= c & 15;
1618 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1619 offset++;
1620 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001621
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001622 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 bytes[0] = nasm_regvals[opx->basereg] << 4;
1624 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1625 offset++;
1626 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001629 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001630 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1631 (int32_t)data != (int64_t)data) {
1632 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1633 "signed dword immediate exceeds bounds");
1634 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001635 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 opx->segment, opx->wrt);
1637 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001638 break;
1639
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001640 case4(0240):
1641 case 0250:
1642 codes += 3;
1643 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1644 EVEX_P2Z | EVEX_P2AAA, 2);
1645 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1646 bytes[0] = 0x62;
1647 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001648 bytes[1] = ((((ins->rex & 7) << 5) |
1649 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1650 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001651 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1652 ((~ins->vexreg & 15) << 3) |
1653 (1 << 2) | (ins->vex_wlp & 3);
1654 bytes[3] = ins->evex_p[2];
1655 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1656 offset += 4;
1657 break;
1658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(0260):
1660 case 0270:
1661 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001662 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1663 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001664 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1665 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1666 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001667 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1669 offset += 3;
1670 } else {
1671 bytes[0] = 0xc5;
1672 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001673 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1675 offset += 2;
1676 }
1677 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001678
H. Peter Anvine014f352012-02-25 22:35:19 -08001679 case 0271:
1680 case 0272:
1681 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001682 break;
1683
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 case4(0274):
1685 {
1686 uint64_t uv, um;
1687 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001688
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 if (ins->rex & REX_W)
1690 s = 64;
1691 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1692 s = 16;
1693 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1694 s = 32;
1695 else
1696 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001697
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 um = (uint64_t)2 << (s-1);
1699 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 if (uv > 127 && uv < (uint64_t)-128 &&
1702 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001703 /* If this wasn't explicitly byte-sized, warn as though we
1704 * had fallen through to the imm16/32/64 case.
1705 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001706 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001707 "%s value exceeds bounds",
1708 (opx->type & BITS8) ? "signed byte" :
1709 s == 16 ? "word" :
1710 s == 32 ? "dword" :
1711 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001713 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001714 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001715 out(offset, segment, &data, OUT_ADDRESS, 1,
1716 opx->segment, opx->wrt);
1717 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001718 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001719 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1720 NO_SEG);
1721 }
1722 offset += 1;
1723 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001725
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001728
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001730 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001732 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 offset += 1;
1734 } else
1735 offset += 0;
1736 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001737
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001739 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001741 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 offset += 1;
1743 } else
1744 offset += 0;
1745 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001746
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 case 0312:
1748 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001749
Keith Kaniosb7a89542007-04-12 02:40:54 +00001750 case 0313:
1751 ins->rex = 0;
1752 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001754 case4(0314):
1755 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001756
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001762 case 0323:
1763 break;
1764
Keith Kaniosb7a89542007-04-12 02:40:54 +00001765 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001766 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001768
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001769 case 0325:
1770 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001771
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001772 case 0326:
1773 break;
1774
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001776 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001777 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 offset += 1;
1779 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001784 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001786 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001787 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 offset += 1;
1789 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790
Keith Kanios48af1772007-08-17 07:37:52 +00001791 case 0334:
1792 if (ins->rex & REX_R) {
1793 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001794 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001795 offset += 1;
1796 }
1797 ins->rex &= ~(REX_L|REX_R);
1798 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001799
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001800 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001801 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001802
H. Peter Anvin962e3052008-08-28 17:47:16 -07001803 case 0336:
1804 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 if (ins->oprs[0].segment != NO_SEG)
1809 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1810 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001811 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 if (size > 0)
1813 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001814 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 offset += size;
1816 }
1817 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001818
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001819 case 0341:
1820 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001821
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001822 case 0360:
1823 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001824
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 case 0361:
1826 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001827 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1828 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001830
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001831 case 0364:
1832 case 0365:
1833 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001834
Keith Kanios48af1772007-08-17 07:37:52 +00001835 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001837 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001838 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001839 offset += 1;
1840 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001841
Jin Kyu Song03041092013-10-15 19:38:51 -07001842 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 case 0373:
1846 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001847 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 offset += 1;
1849 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001851 case 0374:
1852 eat = EA_XMMVSIB;
1853 break;
1854
1855 case 0375:
1856 eat = EA_YMMVSIB;
1857 break;
1858
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001859 case 0376:
1860 eat = EA_ZMMVSIB;
1861 break;
1862
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001863 case4(0100):
1864 case4(0110):
1865 case4(0120):
1866 case4(0130):
1867 case4(0200):
1868 case4(0204):
1869 case4(0210):
1870 case4(0214):
1871 case4(0220):
1872 case4(0224):
1873 case4(0230):
1874 case4(0234):
1875 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 ea ea_data;
1877 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001879 uint8_t *p;
1880 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001882
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001883 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001884 /* pick rfield from operand b (opx) */
1885 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001886 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001887 } else {
1888 /* rfield is constant */
1889 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001893 if (process_ea(opy, &ea_data, bits,
1894 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001896
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 p = bytes;
1898 *p++ = ea_data.modrm;
1899 if (ea_data.sib_present)
1900 *p++ = ea_data.sib;
1901
1902 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001903 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001905 /*
1906 * Make sure the address gets the right offset in case
1907 * the line breaks in the .lst file (BR 1197827)
1908 */
1909 offset += s;
1910 s = 0;
1911
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001912 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001913 /* use compressed displacement, if available */
1914 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 if (ea_data.rip) {
1917 if (opy->segment == segment) {
1918 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001919 if (overflow_signed(data, ea_data.bytes))
1920 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001921 out(offset, segment, &data, OUT_ADDRESS,
1922 ea_data.bytes, NO_SEG, NO_SEG);
1923 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001924 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001925 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 insn_end - offset, opy->segment, opy->wrt);
1927 }
1928 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001929 int asize = ins->addr_size >> 3;
1930 int atype = ea_data.bytes;
1931
1932 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001933 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001934 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001935 warn_overflow(ERR_PASS2, ea_data.bytes);
1936
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001937 if (asize > ea_data.bytes) {
1938 /*
1939 * If the address isn't the full width of
1940 * the address size, treat is as signed...
1941 */
1942 atype = -atype;
1943 }
1944
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001945 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001946 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001947 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 }
1949 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 }
1951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001953 default:
1954 errfunc(ERR_PANIC, "internal instruction table corrupt"
1955 ": instruction code \\%o (0x%02X) given", c, c);
1956 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001958 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001959}
1960
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001961static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001962{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001963 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001965 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001966}
1967
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001968static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001969{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001970 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001972 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001973}
1974
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001975static int op_rexflags(const operand * o, int mask)
1976{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001977 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001978 int val;
1979
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001980 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001981 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001982
H. Peter Anvina4835d42008-05-20 14:21:29 -07001983 flags = nasm_reg_flags[o->basereg];
1984 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001985
1986 return rexflags(val, flags, mask);
1987}
1988
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001989static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990{
1991 int rex = 0;
1992
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001993 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001994 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 rex |= REX_W;
1997 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1998 rex |= REX_H;
1999 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2000 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002001
2002 return rex & mask;
2003}
2004
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002005static int evexflags(int val, decoflags_t deco,
2006 int mask, uint8_t byte)
2007{
2008 int evex = 0;
2009
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002010 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002011 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002012 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002013 evex |= (EVEX_P0RP | EVEX_P0X);
2014 break;
2015 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002016 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002017 evex |= EVEX_P2VP;
2018 if (deco & Z)
2019 evex |= EVEX_P2Z;
2020 if (deco & OPMASK_MASK)
2021 evex |= deco & EVEX_P2AAA;
2022 break;
2023 }
2024 return evex & mask;
2025}
2026
2027static int op_evexflags(const operand * o, int mask, uint8_t byte)
2028{
2029 int val;
2030
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002031 val = nasm_regvals[o->basereg];
2032
2033 return evexflags(val, o->decoflags, mask, byte);
2034}
2035
H. Peter Anvin23595f52009-07-25 17:44:25 -07002036static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002037 insn *instruction,
2038 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002039{
2040 const struct itemplate *temp;
2041 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002042 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002043 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002044 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002045 int i;
2046
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002047 /* broadcasting uses a different data element size */
2048 for (i = 0; i < instruction->operands; i++)
2049 if (i == broadcast)
2050 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2051 else
2052 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002053
2054 merr = MERR_INVALOP;
2055
2056 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002057 temp->opcode != I_none; temp++) {
2058 m = matches(temp, instruction, bits);
2059 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002060 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 m = MOK_GOOD;
2062 else
2063 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002064 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002065 /*
2066 * Missing operand size and a candidate for fuzzy matching...
2067 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002068 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002069 if (i == broadcast)
2070 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2071 else
2072 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002073 opsizemissing = true;
2074 }
2075 if (m > merr)
2076 merr = m;
2077 if (merr == MOK_GOOD)
2078 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002079 }
2080
2081 /* No match, but see if we can get a fuzzy operand size match... */
2082 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002084
2085 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002086 /*
2087 * We ignore extrinsic operand sizes on registers, so we should
2088 * never try to fuzzy-match on them. This also resolves the case
2089 * when we have e.g. "xmmrm128" in two different positions.
2090 */
2091 if (is_class(REGISTER, instruction->oprs[i].type))
2092 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002093
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 /* This tests if xsizeflags[i] has more than one bit set */
2095 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2096 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002097
Jin Kyu Song7903c072013-10-30 03:00:12 -07002098 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002099 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002100 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2101 BITS32 : BITS64);
2102 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002103 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002104 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002105 }
2106
2107 /* Try matching again... */
2108 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002109 temp->opcode != I_none; temp++) {
2110 m = matches(temp, instruction, bits);
2111 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002112 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002113 m = MOK_GOOD;
2114 else
2115 m = MERR_INVALOP;
2116 }
2117 if (m > merr)
2118 merr = m;
2119 if (merr == MOK_GOOD)
2120 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002121 }
2122
H. Peter Anvina81655b2009-07-25 18:15:28 -07002123done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002124 *tempp = temp;
2125 return merr;
2126}
2127
Mark Charneydcaef4b2014-10-09 13:45:17 -04002128static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2129{
2130 opflags_t opsize = opflags & SIZE_MASK;
2131 uint8_t brcast_num;
2132
2133 /*
2134 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2135 * this cannot be a simple arithmetic calculation.
2136 */
2137 if (brsize > BITS64)
2138 errfunc(ERR_FATAL,
2139 "size of broadcasting element is greater than 64 bits");
2140
2141 switch (opsize) {
2142 case BITS64:
2143 brcast_num = BITS64 / brsize;
2144 break;
2145 default:
2146 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2147 break;
2148 }
2149
2150 return brcast_num;
2151}
2152
H. Peter Anvin65289e82009-07-25 17:25:11 -07002153static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002154 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002155{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002156 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002157 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002158 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002159
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002160 /*
2161 * Check the opcode
2162 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002164 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002165
2166 /*
2167 * Count the operands
2168 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002170 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002171
2172 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002173 * Is it legal?
2174 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002175 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002176 return MERR_INVALOP;
2177
2178 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002179 * {evex} available?
2180 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002181 switch (instruction->prefixes[PPS_VEX]) {
2182 case P_EVEX:
2183 if (!itemp_has(itemp, IF_EVEX))
2184 return MERR_ENCMISMATCH;
2185 break;
2186 case P_VEX3:
2187 case P_VEX2:
2188 if (!itemp_has(itemp, IF_VEX))
2189 return MERR_ENCMISMATCH;
2190 break;
2191 default:
2192 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002193 }
2194
2195 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002196 * Check that no spurious colons or TOs are present
2197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 for (i = 0; i < itemp->operands; i++)
2199 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002200 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002201
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002202 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002203 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 switch (itemp_smask(itemp)) {
2206 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002207 asize = BITS8;
2208 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002209 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002210 asize = BITS16;
2211 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002212 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002213 asize = BITS32;
2214 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002215 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002216 asize = BITS64;
2217 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002218 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 asize = BITS128;
2220 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002221 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 asize = BITS256;
2223 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002224 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002225 asize = BITS512;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 switch (bits) {
2229 case 16:
2230 asize = BITS16;
2231 break;
2232 case 32:
2233 asize = BITS32;
2234 break;
2235 case 64:
2236 asize = BITS64;
2237 break;
2238 default:
2239 asize = 0;
2240 break;
2241 }
2242 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002243 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002244 asize = 0;
2245 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002246 }
2247
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002248 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002250 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002251 memset(size, 0, sizeof size);
2252 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002253 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002254 /* S- flags apply to all operands */
2255 for (i = 0; i < MAX_OPERANDS; i++)
2256 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002257 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002258
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002259 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002260 * Check that the operand flags all match up,
2261 * it's a bit tricky so lets be verbose:
2262 *
2263 * 1) Find out the size of operand. If instruction
2264 * doesn't have one specified -- we're trying to
2265 * guess it either from template (IF_S* flag) or
2266 * from code bits.
2267 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002268 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 * template has an operand size specified AND this size differ
2270 * from which instruction has (perhaps we got it from code bits)
2271 * we are:
2272 * a) Check that only size of instruction and operand is differ
2273 * other characteristics do match
2274 * b) Perhaps it's a register specified in instruction so
2275 * for such a case we just mark that operand as "size
2276 * missing" and this will turn on fuzzy operand size
2277 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002278 */
2279 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002280 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002281 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002282 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002283 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002284 opflags_t template_opsize, insn_opsize;
2285
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002286 if (!(type & SIZE_MASK))
2287 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002288
Jin Kyu Song7903c072013-10-30 03:00:12 -07002289 insn_opsize = type & SIZE_MASK;
2290 if (!is_broadcast) {
2291 template_opsize = itemp->opd[i] & SIZE_MASK;
2292 } else {
2293 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2294 /*
2295 * when broadcasting, the element size depends on
2296 * the instruction type. decorator flag should match.
2297 */
2298
2299 if (deco_brsize) {
2300 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002301 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002302 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002303 } else {
2304 template_opsize = 0;
2305 }
2306 }
2307
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002308 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002309 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002310 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 } else if (template_opsize) {
2312 if (template_opsize != insn_opsize) {
2313 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002314 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002315 } else if (!is_class(REGISTER, type)) {
2316 /*
2317 * Note: we don't honor extrinsic operand sizes for registers,
2318 * so "missing operand size" for a register should be
2319 * considered a wildcard match rather than an error.
2320 */
2321 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002322 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002323 } else if (is_broadcast &&
2324 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002325 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002326 /*
2327 * broadcasting opsize matches but the number of repeated memory
2328 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002329 * if 64b double precision float is broadcasted to ymm (256b),
2330 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002331 */
2332 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002333 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002334 }
2335 }
2336
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002337 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002338 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002339
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002340 /*
2341 * Check operand sizes
2342 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002343 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2344 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002346 asize = itemp->opd[i] & SIZE_MASK;
2347 if (asize) {
2348 for (i = 0; i < oprs; i++)
2349 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 break;
2351 }
2352 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002353 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002355 }
2356
Keith Kaniosb7a89542007-04-12 02:40:54 +00002357 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 if (!(itemp->opd[i] & SIZE_MASK) &&
2359 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002360 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 }
2362
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002363 /*
2364 * Check template is okay at the set cpu level
2365 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002366 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002367 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002368
Keith Kaniosb7a89542007-04-12 02:40:54 +00002369 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002370 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002371 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002372 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002373 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002375 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002376 * If we have a HLE prefix, look for the NOHLE flag
2377 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002378 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002379 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2380 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2381 return MERR_BADHLE;
2382
2383 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002384 * Check if special handling needed for Jumps
2385 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002386 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002387 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388
Jin Kyu Song03041092013-10-15 19:38:51 -07002389 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002390 * Check if BND prefix is allowed.
2391 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002392 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002393 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002394 (has_prefix(instruction, PPS_REP, P_BND) ||
2395 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002396 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002397 else if (itemp_has(itemp, IF_BND) &&
2398 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2399 has_prefix(instruction, PPS_REP, P_REPNZ)))
2400 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002401
H. Peter Anvin60926242009-07-26 16:25:38 -07002402 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002403}
2404
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002405/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002406 * Check if ModR/M.mod should/can be 01.
2407 * - EAF_BYTEOFFS is set
2408 * - offset can fit in a byte when EVEX is not used
2409 * - offset can be compressed when EVEX is used
2410 */
2411#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2412 (o >= -128 && o <= 127 && \
2413 seg == NO_SEG && !forw_ref && \
2414 !(input->eaflags & EAF_WORDOFFS) && \
2415 !(ins->rex & REX_EV)) || \
2416 (ins->rex & REX_EV && \
2417 is_disp8n(input, ins, &output->disp8)))
2418
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002419static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002420 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002421{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002422 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002424 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002425
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 output->type = EA_SCALAR;
2427 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002428 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002429
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002430 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002431 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002432 /* EVEX.R' flag for the REG operand */
2433 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002434
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002435 if (is_class(REGISTER, input->type)) {
2436 /*
2437 * It's a direct register.
2438 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002439 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002440 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002441
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002442 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002443 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002444
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002445 /* broadcasting is not available with a direct register operand. */
2446 if (input->decoflags & BRDCAST_MASK) {
2447 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2448 goto err;
2449 }
2450
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002453 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002454 output->bytes = 0; /* no offset necessary either */
2455 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2456 } else {
2457 /*
2458 * It's a memory reference.
2459 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002460
2461 /* Embedded rounding or SAE is not available with a mem ref operand. */
2462 if (input->decoflags & (ER | SAE)) {
2463 nasm_error(ERR_NONFATAL,
2464 "Embedded rounding is available only with reg-reg op.");
2465 return -1;
2466 }
2467
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002468 if (input->basereg == -1 &&
2469 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002470 /*
2471 * It's a pure offset.
2472 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002473 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2474 input->segment == NO_SEG) {
2475 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2476 input->type &= ~IP_REL;
2477 input->type |= MEMORY;
2478 }
2479
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002480 if (bits == 64 &&
2481 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2482 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2483 return -1;
2484 }
2485
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002486 if (eaflags & EAF_BYTEOFFS ||
2487 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002488 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2489 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2490 }
2491
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002492 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002494 output->sib = GEN_SIB(0, 4, 5);
2495 output->bytes = 4;
2496 output->modrm = GEN_MODRM(0, rfield, 4);
2497 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002498 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002499 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002500 output->bytes = (addrbits != 16 ? 4 : 2);
2501 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2502 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002503 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002504 } else {
2505 /*
2506 * It's an indirection.
2507 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002509 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 int t, it, bt; /* register numbers */
2512 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 if (s == 0)
2515 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002516
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002517 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002518 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 ix = nasm_reg_flags[i];
2520 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002521 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 ix = 0;
2523 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002524
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002526 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 bx = nasm_reg_flags[b];
2528 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002529 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002530 bx = 0;
2531 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002532
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002534 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002535 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536 int32_t o = input->offset;
2537 int mod, scale, index, base;
2538
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 /*
2540 * For a vector SIB, one has to be a vector and the other,
2541 * if present, a GPR. The vector must be the index operand.
2542 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002543 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544 if (s == 0)
2545 s = 1;
2546 else if (s != 1)
2547 goto err;
2548
2549 t = bt, bt = it, it = t;
2550 x = bx, bx = ix, ix = x;
2551 }
2552
2553 if (bt != -1) {
2554 if (REG_GPR & ~bx)
2555 goto err;
2556 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2557 sok &= bx;
2558 else
2559 goto err;
2560 }
2561
2562 /*
2563 * While we're here, ensure the user didn't specify
2564 * WORD or QWORD
2565 */
2566 if (input->disp_size == 16 || input->disp_size == 64)
2567 goto err;
2568
2569 if (addrbits == 16 ||
2570 (addrbits == 32 && !(sok & BITS32)) ||
2571 (addrbits == 64 && !(sok & BITS64)))
2572 goto err;
2573
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002574 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2575 : ((ix & YMMREG & ~REG_EA)
2576 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002578 output->rex |= rexflags(it, ix, REX_X);
2579 output->rex |= rexflags(bt, bx, REX_B);
2580 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002581
2582 index = it & 7; /* it is known to be != -1 */
2583
2584 switch (s) {
2585 case 1:
2586 scale = 0;
2587 break;
2588 case 2:
2589 scale = 1;
2590 break;
2591 case 4:
2592 scale = 2;
2593 break;
2594 case 8:
2595 scale = 3;
2596 break;
2597 default: /* then what the smeg is it? */
2598 goto err; /* panic */
2599 }
2600
2601 if (bt == -1) {
2602 base = 5;
2603 mod = 0;
2604 } else {
2605 base = (bt & 7);
2606 if (base != REG_NUM_EBP && o == 0 &&
2607 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002608 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002610 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611 mod = 1;
2612 else
2613 mod = 2;
2614 }
2615
2616 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002617 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2618 output->modrm = GEN_MODRM(mod, rfield, 4);
2619 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 /*
2622 * it must be a 32/64-bit memory reference. Firstly we have
2623 * to check that all registers involved are type E/Rxx.
2624 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002625 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002627
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002628 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002629 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2630 sok &= ix;
2631 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002633 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002635 if (bt != -1) {
2636 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 sok &= bx;
2641 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /*
2644 * While we're here, ensure the user didn't specify
2645 * WORD or QWORD
2646 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002647 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 if (addrbits == 16 ||
2651 (addrbits == 32 && !(sok & BITS32)) ||
2652 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002654
Keith Kaniosb7a89542007-04-12 02:40:54 +00002655 /* now reorganize base/index */
2656 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 ((hb == b && ht == EAH_NOTBASE) ||
2658 (hb == i && ht == EAH_MAKEBASE))) {
2659 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002660 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 x = bx, bx = ix, ix = x;
2662 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002663
Jin Kyu Song164d6072013-10-15 19:10:13 -07002664 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002666 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002668 if (eaflags & EAF_MIB) {
2669 /* only for mib operands */
2670 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2671 /*
2672 * make a single reg index [reg*1].
2673 * gas uses this form for an explicit index register.
2674 */
2675 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2676 }
2677 if ((ht == EAH_SUMMED) && bt == -1) {
2678 /* separate once summed index into [base, index] */
2679 bt = it, bx = ix, s--;
2680 }
2681 } else {
2682 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002683 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002684 s == 3 || s == 5 || s == 9) && bt == -1) {
2685 /* convert 3*EAX to EAX+2*EAX */
2686 bt = it, bx = ix, s--;
2687 }
2688 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002689 (eaflags & EAF_TIMESTWO) &&
2690 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002691 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002692 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002693 * to sib format with 0x0 displacement - [EAX*1+0].
2694 */
2695 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2696 }
2697 }
Keith Kanios48af1772007-08-17 07:37:52 +00002698 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002699 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002700 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002701 x = ix, ix = bx, bx = x;
2702 }
2703 if (it == REG_NUM_ESP ||
2704 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002705 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 output->rex |= rexflags(it, ix, REX_X);
2708 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002709
Keith Kanios48af1772007-08-17 07:37:52 +00002710 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002711 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002713
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 } else {
2718 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002719 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002720 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002721 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002722 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002723 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002724 mod = 1;
2725 else
2726 mod = 2;
2727 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002728
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002729 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002730 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2731 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002732 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 if (it == -1)
2737 index = 4, s = 1;
2738 else
2739 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 switch (s) {
2742 case 1:
2743 scale = 0;
2744 break;
2745 case 2:
2746 scale = 1;
2747 break;
2748 case 4:
2749 scale = 2;
2750 break;
2751 case 8:
2752 scale = 3;
2753 break;
2754 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 if (bt == -1) {
2759 base = 5;
2760 mod = 0;
2761 } else {
2762 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002763 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002765 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002766 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002767 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002768 mod = 1;
2769 else
2770 mod = 2;
2771 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002773 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002774 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2775 output->modrm = GEN_MODRM(mod, rfield, 4);
2776 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 }
2778 } else { /* it's 16-bit */
2779 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002780 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002781
Keith Kaniosb7a89542007-04-12 02:40:54 +00002782 /* check for 64-bit long mode */
2783 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002784 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2788 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002789 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002790
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002792 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002796 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 if (b == -1 && i != -1) {
2798 int tmp = b;
2799 b = i;
2800 i = tmp;
2801 } /* swap */
2802 if ((b == R_SI || b == R_DI) && i != -1) {
2803 int tmp = b;
2804 b = i;
2805 i = tmp;
2806 }
2807 /* have BX/BP as base, SI/DI index */
2808 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 if (i != -1 && b != -1 &&
2811 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 rm = -1;
2817 if (i != -1)
2818 switch (i * 256 + b) {
2819 case R_SI * 256 + R_BX:
2820 rm = 0;
2821 break;
2822 case R_DI * 256 + R_BX:
2823 rm = 1;
2824 break;
2825 case R_SI * 256 + R_BP:
2826 rm = 2;
2827 break;
2828 case R_DI * 256 + R_BP:
2829 rm = 3;
2830 break;
2831 } else
2832 switch (b) {
2833 case R_SI:
2834 rm = 4;
2835 break;
2836 case R_DI:
2837 rm = 5;
2838 break;
2839 case R_BP:
2840 rm = 6;
2841 break;
2842 case R_BX:
2843 rm = 7;
2844 break;
2845 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002847 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002848
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002849 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002850 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002852 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 mod = 1;
2854 else
2855 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002856
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002857 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002858 output->bytes = mod; /* bytes of offset needed */
2859 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 }
2861 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002863
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002865 return output->type;
2866
2867err:
2868 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002869}
2870
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002871static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002872{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002873 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002874 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002875
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002876 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002877
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002878 switch (ins->prefixes[PPS_ASIZE]) {
2879 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002880 valid &= 16;
2881 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002883 valid &= 32;
2884 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002885 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 valid &= 64;
2887 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002888 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 valid &= (addrbits == 32) ? 16 : 32;
2890 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 }
2894
2895 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 if (is_class(MEMORY, ins->oprs[j].type)) {
2897 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002900 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 i = 0;
2902 else
2903 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002906 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 b = 0;
2908 else
2909 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (ins->oprs[j].scale == 0)
2912 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 if (!i && !b) {
2915 int ds = ins->oprs[j].disp_size;
2916 if ((addrbits != 64 && ds > 8) ||
2917 (addrbits == 64 && ds == 16))
2918 valid &= ds;
2919 } else {
2920 if (!(REG16 & ~b))
2921 valid &= 16;
2922 if (!(REG32 & ~b))
2923 valid &= 32;
2924 if (!(REG64 & ~b))
2925 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 if (!(REG16 & ~i))
2928 valid &= 16;
2929 if (!(REG32 & ~i))
2930 valid &= 32;
2931 if (!(REG64 & ~i))
2932 valid &= 64;
2933 }
2934 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002935 }
2936
2937 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002939 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002941 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002943 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 /* Impossible... */
2945 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2946 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002947 }
2948
2949 defdisp = ins->addr_size == 16 ? 16 : 32;
2950
2951 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2953 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2954 /*
2955 * mem_offs sizes must match the address size; if not,
2956 * strip the MEM_OFFS bit and match only EA instructions
2957 */
2958 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2959 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002960 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002961}