blob: 4976c4664cd8a8e443e23e2373f6b806d27eb2e1 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07003 * Copyright 1996-2014 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
311 * generator at the same time.
312 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800313static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800314 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400315 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000316{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000317 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000318 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800319 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000320
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800321 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400322 /*
323 * This is a non-relocated address, and we're going to
324 * convert it into RAWDATA format.
325 */
326 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800327
328 size = abs((int)size);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400329 if (size > 8) {
330 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
331 return;
332 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700333
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400334 WRITEADDR(q, *(int64_t *)data, size);
335 data = p;
336 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000337 }
338
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800339 list->output(offset, data, type, size);
340
Frank Kotlerabebb082003-09-06 04:45:37 +0000341 /*
342 * this call to src_get determines when we call the
343 * debug-format-specific "linenum" function
344 * it updates lineno and lnfname to the current values
345 * returning 0 if "same as last time", -2 if lnfname
346 * changed, and the amount by which lineno changed,
347 * if it did. thus, these variables must be static
348 */
349
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400350 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000352
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800353 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000354}
355
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800356static void out_imm8(int64_t offset, int32_t segment,
357 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400358{
359 if (opx->segment != NO_SEG) {
360 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800361 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400362 } else {
363 uint8_t byte = opx->offset;
364 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
365 }
366}
367
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700368static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800369 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800371 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800372 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000373 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800374 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000375
H. Peter Anvin755f5212012-02-25 11:41:34 -0800376 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700377 return false;
378 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400379 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700380 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400381 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700382
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800383 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100384
Victor van den Elzen154e5922009-02-25 17:32:00 +0100385 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100386 /* Be optimistic in pass 1 */
387 return true;
388
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700390 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000391
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700392 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800393 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
394
395 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
396 /* jmp short (opcode eb) cannot be used with bnd prefix. */
397 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800398 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
399 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800400 }
401
402 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000404
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400405int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400406 insn * instruction, struct ofmt *output, efunc error,
407 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000409 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700411 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800412 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800414 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300415 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000416
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000418 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 outfmt = output; /* likewise */
420 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300422 wsize = idata_bytes(instruction->opcode);
423 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000425
H. Peter Anvineba20a72002-04-30 20:53:55 +0000426 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000428 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 if (t < 0)
430 errfunc(ERR_PANIC,
431 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400434 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400436 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700437 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400438 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000439 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700440 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800442 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400443 offset += wsize;
444 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700445 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400446 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000448
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800450 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 if (align) {
454 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100455 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800456 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 }
458 offset += e->stringlen + align;
459 }
460 }
461 if (t > 0 && t == instruction->times - 1) {
462 /*
463 * Dummy call to list->output to give the offset to the
464 * listing module.
465 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800466 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 list->uplevel(LIST_TIMES);
468 }
469 }
470 if (instruction->times > 1)
471 list->downlevel(LIST_TIMES);
472 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000473 }
474
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700476 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400479 fp = fopen(fname, "rb");
480 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
482 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400483 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
485 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200486 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400487 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700488 static char buf[4096];
489 size_t t = instruction->times;
490 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400491 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000492
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 len = ftell(fp);
494 if (instruction->eops->next) {
495 base = instruction->eops->next->offset;
496 len -= base;
497 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700498 len > (size_t)instruction->eops->next->next->offset)
499 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 }
501 /*
502 * Dummy call to list->output to give the offset to the
503 * listing module.
504 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800505 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 list->uplevel(LIST_INCBIN);
507 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700508 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 fseek(fp, base, SEEK_SET);
511 l = len;
512 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700513 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400514 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 if (!m) {
516 /*
517 * This shouldn't happen unless the file
518 * actually changes while we are reading
519 * it.
520 */
521 error(ERR_NONFATAL,
522 "`incbin': unexpected EOF while"
523 " reading file `%s'", fname);
524 t = 0; /* Try to exit cleanly */
525 break;
526 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800527 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 NO_SEG, NO_SEG);
529 l -= m;
530 }
531 }
532 list->downlevel(LIST_INCBIN);
533 if (instruction->times > 1) {
534 /*
535 * Dummy call to list->output to give the offset to the
536 * listing module.
537 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800538 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 list->uplevel(LIST_TIMES);
540 list->downlevel(LIST_TIMES);
541 }
542 fclose(fp);
543 return instruction->times * len;
544 }
545 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000546 }
547
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700548 /* Check to see if we need an address-size prefix */
549 add_asp(instruction, bits);
550
H. Peter Anvin23595f52009-07-25 17:44:25 -0700551 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700552
H. Peter Anvin23595f52009-07-25 17:44:25 -0700553 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400554 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800555 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400556 itimes = instruction->times;
557 if (insn_size < 0) /* shouldn't be, on pass two */
558 error(ERR_PANIC, "errors made it through from pass one");
559 else
560 while (itimes--) {
561 for (j = 0; j < MAXPREFIX; j++) {
562 uint8_t c = 0;
563 switch (instruction->prefixes[j]) {
564 case P_WAIT:
565 c = 0x9B;
566 break;
567 case P_LOCK:
568 c = 0xF0;
569 break;
570 case P_REPNE:
571 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800572 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700573 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400574 c = 0xF2;
575 break;
576 case P_REPE:
577 case P_REPZ:
578 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800579 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400580 c = 0xF3;
581 break;
582 case R_CS:
583 if (bits == 64) {
584 error(ERR_WARNING | ERR_PASS2,
585 "cs segment base generated, but will be ignored in 64-bit mode");
586 }
587 c = 0x2E;
588 break;
589 case R_DS:
590 if (bits == 64) {
591 error(ERR_WARNING | ERR_PASS2,
592 "ds segment base generated, but will be ignored in 64-bit mode");
593 }
594 c = 0x3E;
595 break;
596 case R_ES:
597 if (bits == 64) {
598 error(ERR_WARNING | ERR_PASS2,
599 "es segment base generated, but will be ignored in 64-bit mode");
600 }
601 c = 0x26;
602 break;
603 case R_FS:
604 c = 0x64;
605 break;
606 case R_GS:
607 c = 0x65;
608 break;
609 case R_SS:
610 if (bits == 64) {
611 error(ERR_WARNING | ERR_PASS2,
612 "ss segment base generated, but will be ignored in 64-bit mode");
613 }
614 c = 0x36;
615 break;
616 case R_SEGR6:
617 case R_SEGR7:
618 error(ERR_NONFATAL,
619 "segr6 and segr7 cannot be used as prefixes");
620 break;
621 case P_A16:
622 if (bits == 64) {
623 error(ERR_NONFATAL,
624 "16-bit addressing is not supported "
625 "in 64-bit mode");
626 } else if (bits != 16)
627 c = 0x67;
628 break;
629 case P_A32:
630 if (bits != 32)
631 c = 0x67;
632 break;
633 case P_A64:
634 if (bits != 64) {
635 error(ERR_NONFATAL,
636 "64-bit addressing is only supported "
637 "in 64-bit mode");
638 }
639 break;
640 case P_ASP:
641 c = 0x67;
642 break;
643 case P_O16:
644 if (bits != 16)
645 c = 0x66;
646 break;
647 case P_O32:
648 if (bits == 16)
649 c = 0x66;
650 break;
651 case P_O64:
652 /* REX.W */
653 break;
654 case P_OSP:
655 c = 0x66;
656 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700657 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800658 case P_VEX3:
659 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800660 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400661 case P_none:
662 break;
663 default:
664 error(ERR_PANIC, "invalid instruction prefix");
665 }
666 if (c != 0) {
667 out(offset, segment, &c, OUT_RAWDATA, 1,
668 NO_SEG, NO_SEG);
669 offset++;
670 }
671 }
672 insn_end = offset + insn_size;
673 gencode(segment, offset, bits, instruction,
674 temp, insn_end);
675 offset += insn_size;
676 if (itimes > 0 && itimes == instruction->times - 1) {
677 /*
678 * Dummy call to list->output to give the offset to the
679 * listing module.
680 */
681 list->output(offset, NULL, OUT_RAWDATA, 0);
682 list->uplevel(LIST_TIMES);
683 }
684 }
685 if (instruction->times > 1)
686 list->downlevel(LIST_TIMES);
687 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700688 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400689 /* No match */
690 switch (m) {
691 case MERR_OPSIZEMISSING:
692 error(ERR_NONFATAL, "operation size not specified");
693 break;
694 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400696 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700697 case MERR_BRNUMMISMATCH:
698 error(ERR_NONFATAL,
699 "mismatch in the number of broadcasting elements");
700 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400701 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400703 break;
704 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800705 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400706 bits);
707 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800708 case MERR_ENCMISMATCH:
709 error(ERR_NONFATAL, "specific encoding scheme not available");
710 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800711 case MERR_BADBND:
712 error(ERR_NONFATAL, "bnd prefix is not allowed");
713 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800714 case MERR_BADREPNE:
715 error(ERR_NONFATAL, "%s prefix is not allowed",
716 (has_prefix(instruction, PPS_REP, P_REPNE) ?
717 "repne" : "repnz"));
718 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400719 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 error(ERR_NONFATAL,
721 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400722 break;
723 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000724 }
725 return 0;
726}
727
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400728int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400729 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000730{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000731 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700732 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000735 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400737 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700740 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
741 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400742 instruction->opcode == I_DT || instruction->opcode == I_DO ||
743 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300745 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300748 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000749
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400750 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000751 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000752
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400754 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400756 warn_overflow_const(e->offset, wsize);
757 } else if (e->type == EOT_DB_STRING ||
758 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 align = (-osize) % wsize;
762 if (align < 0)
763 align += wsize;
764 isize += osize + align;
765 }
766 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000767 }
768
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400770 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300772 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700773 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000774
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400775 fp = fopen(fname, "rb");
776 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
778 fname);
779 else if (fseek(fp, 0L, SEEK_END) < 0)
780 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
781 fname);
782 else {
783 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 if (instruction->eops->next) {
785 len -= instruction->eops->next->offset;
786 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700787 len > (size_t)instruction->eops->next->next->offset) {
788 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 }
790 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300791 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300793 if (fp)
794 fclose(fp);
795 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000796 }
797
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700798 /* Check to see if we need an address-size prefix */
799 add_asp(instruction, bits);
800
H. Peter Anvin23595f52009-07-25 17:44:25 -0700801 m = find_match(&temp, instruction, segment, offset, bits);
802 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400803 /* we've matched an instruction. */
804 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400805 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100806
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800807 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400808 if (isize < 0)
809 return -1;
810 for (j = 0; j < MAXPREFIX; j++) {
811 switch (instruction->prefixes[j]) {
812 case P_A16:
813 if (bits != 16)
814 isize++;
815 break;
816 case P_A32:
817 if (bits != 32)
818 isize++;
819 break;
820 case P_O16:
821 if (bits != 16)
822 isize++;
823 break;
824 case P_O32:
825 if (bits == 16)
826 isize++;
827 break;
828 case P_A64:
829 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700830 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800831 case P_VEX3:
832 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800833 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400834 case P_none:
835 break;
836 default:
837 isize++;
838 break;
839 }
840 }
841 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700842 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400843 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000844 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000845}
846
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800847static void bad_hle_warn(const insn * ins, uint8_t hleok)
848{
849 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800850 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800851 static const enum whatwarn warn[2][4] =
852 {
853 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
854 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
855 };
856 unsigned int n;
857
858 n = (unsigned int)rep_pfx - P_XACQUIRE;
859 if (n > 1)
860 return; /* Not XACQUIRE/XRELEASE */
861
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800862 ww = warn[n][hleok];
863 if (!is_class(MEMORY, ins->oprs[0].type))
864 ww = w_inval; /* HLE requires operand 0 to be memory */
865
866 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800867 case w_none:
868 break;
869
870 case w_lock:
871 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800872 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800873 "%s with this instruction requires lock",
874 prefix_name(rep_pfx));
875 }
876 break;
877
878 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800879 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800880 "%s invalid with this instruction",
881 prefix_name(rep_pfx));
882 break;
883 }
884}
885
H. Peter Anvin507ae032008-10-09 15:37:10 -0700886/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400887#define case3(x) case (x): case (x)+1: case (x)+2
888#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700889
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800890static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800891 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000892{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800893 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800894 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000895 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000896 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700897 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700898 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700899 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700900 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800901 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800902 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700903 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000904
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700905 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700906 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700907 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700908
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700909 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400910 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700911
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 (void)segment; /* Don't warn that this parameter is unused */
913 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000914
H. Peter Anvin839eca22007-10-29 23:12:47 -0700915 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400916 c = *codes++;
917 op1 = (c & 3) + ((opex & 1) << 2);
918 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
919 opx = &ins->oprs[op1];
920 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700921
H. Peter Anvin839eca22007-10-29 23:12:47 -0700922 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400923 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 codes += c, length += c;
925 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700926
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400927 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400928 opex = c;
929 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400931 case4(010):
932 ins->rex |=
933 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 codes++, length++;
935 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700936
Jin Kyu Song164d6072013-10-15 19:10:13 -0700937 case4(014):
938 /* this is an index reg of MIB operand */
939 mib_index = opx->basereg;
940 break;
941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(020):
943 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 length++;
945 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700946
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 length += 2;
949 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700950
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400951 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700952 if (opx->type & (BITS16 | BITS32 | BITS64))
953 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 else
955 length += (bits == 16) ? 2 : 4;
956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 length += 4;
960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700963 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 length++;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000971 length += 8; /* MOV reg64/imm */
972 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400974 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 length += 2;
976 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700977
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400978 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700979 if (opx->type & (BITS16 | BITS32 | BITS64))
980 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 else
982 length += (bits == 16) ? 2 : 4;
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length += 4;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700990 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case 0172:
994 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700996 length++;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700999 case4(0174):
1000 length++;
1001 break;
1002
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001003 case4(0240):
1004 ins->rex |= REX_EV;
1005 ins->vexreg = regval(opx);
1006 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1007 ins->vex_cm = *codes++;
1008 ins->vex_wlp = *codes++;
1009 ins->evex_tuple = (*codes++ - 0300);
1010 break;
1011
1012 case 0250:
1013 ins->rex |= REX_EV;
1014 ins->vexreg = 0;
1015 ins->vex_cm = *codes++;
1016 ins->vex_wlp = *codes++;
1017 ins->evex_tuple = (*codes++ - 0300);
1018 break;
1019
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 case4(0254):
1021 length += 4;
1022 break;
1023
1024 case4(0260):
1025 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001026 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 ins->vex_cm = *codes++;
1028 ins->vex_wlp = *codes++;
1029 break;
1030
1031 case 0270:
1032 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001033 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 ins->vex_cm = *codes++;
1035 ins->vex_wlp = *codes++;
1036 break;
1037
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001038 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001039 hleok = c & 3;
1040 break;
1041
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001042 case4(0274):
1043 length++;
1044 break;
1045
1046 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001050 if (bits == 64)
1051 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001052 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001056 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
Keith Kaniosb7a89542007-04-12 02:40:54 +00001062 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001063 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1064 has_prefix(ins, PPS_ASIZE, P_A32))
1065 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001067
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 case4(0314):
1069 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001072 {
1073 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1074 if (pfx == P_O16)
1075 break;
1076 if (pfx != P_none)
1077 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1078 else
1079 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001081 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001084 {
1085 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1086 if (pfx == P_O32)
1087 break;
1088 if (pfx != P_none)
1089 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1090 else
1091 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001093 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 case 0322:
1096 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001097
Keith Kaniosb7a89542007-04-12 02:40:54 +00001098 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001099 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
Keith Kaniosb7a89542007-04-12 02:40:54 +00001102 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001103 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001104 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 case 0325:
1107 ins->rex |= REX_NH;
1108 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001109
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001110 case 0326:
1111 break;
1112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 case 0330:
1114 codes++, length++;
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001120 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 case 0333:
1122 length++;
1123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001125 case 0334:
1126 ins->rex |= REX_L;
1127 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001129 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001130 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001133 if (!ins->prefixes[PPS_REP])
1134 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001138 if (!ins->prefixes[PPS_REP])
1139 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 if (ins->oprs[0].segment != NO_SEG)
1144 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1145 " quantity of BSS space");
1146 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001147 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001149
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 case 0341:
1151 if (!ins->prefixes[PPS_WAIT])
1152 ins->prefixes[PPS_WAIT] = P_WAIT;
1153 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001154
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 case 0360:
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001158 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 case 0364:
1163 case 0365:
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
Keith Kanios48af1772007-08-17 07:37:52 +00001166 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001167 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 length++;
1169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001171 case 0370:
1172 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001174
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 case 0373:
1176 length++;
1177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001179 case 0374:
1180 eat = EA_XMMVSIB;
1181 break;
1182
1183 case 0375:
1184 eat = EA_YMMVSIB;
1185 break;
1186
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001187 case 0376:
1188 eat = EA_ZMMVSIB;
1189 break;
1190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 case4(0100):
1192 case4(0110):
1193 case4(0120):
1194 case4(0130):
1195 case4(0200):
1196 case4(0204):
1197 case4(0210):
1198 case4(0214):
1199 case4(0220):
1200 case4(0224):
1201 case4(0230):
1202 case4(0234):
1203 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001205 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001206 opflags_t rflags;
1207 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001208 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001209
Keith Kaniosb7a89542007-04-12 02:40:54 +00001210 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001211
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001212 if (c <= 0177) {
1213 /* pick rfield from operand b (opx) */
1214 rflags = regflag(opx);
1215 rfield = nasm_regvals[opx->basereg];
1216 } else {
1217 rflags = 0;
1218 rfield = c & 7;
1219 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001220
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001221 /* EVEX.b1 : evex_brerop contains the operand position */
1222 op_er_sae = (ins->evex_brerop >= 0 ?
1223 &ins->oprs[ins->evex_brerop] : NULL);
1224
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001225 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1226 /* set EVEX.b */
1227 ins->evex_p[2] |= EVEX_P2B;
1228 if (op_er_sae->decoflags & ER) {
1229 /* set EVEX.RC (rounding control) */
1230 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1231 & EVEX_P2RC;
1232 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001233 } else {
1234 /* set EVEX.L'L (vector length) */
1235 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001236 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001237 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001238 /* set EVEX.b */
1239 ins->evex_p[2] |= EVEX_P2B;
1240 }
1241 }
1242
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001243 if (itemp_has(temp, IF_MIB)) {
1244 opy->eaflags |= EAF_MIB;
1245 /*
1246 * if a separate form of MIB (ICC style) is used,
1247 * the index reg info is merged into mem operand
1248 */
1249 if (mib_index != R_none) {
1250 opy->indexreg = mib_index;
1251 opy->scale = 1;
1252 opy->hintbase = mib_index;
1253 opy->hinttype = EAH_NOTBASE;
1254 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001255 }
1256
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001257 if (process_ea(opy, &ea_data, bits,
1258 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 errfunc(ERR_NONFATAL, "invalid effective address");
1260 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001261 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001264 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 }
1266 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001267
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001268 default:
1269 errfunc(ERR_PANIC, "internal instruction table corrupt"
1270 ": instruction code \\%o (0x%02X) given", c, c);
1271 break;
1272 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001273 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001274
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001275 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001276
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001277 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 if (ins->rex & REX_H) {
1279 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1280 return -1;
1281 }
1282 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001283 }
1284
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001285 switch (ins->prefixes[PPS_VEX]) {
1286 case P_EVEX:
1287 if (!(ins->rex & REX_EV))
1288 return -1;
1289 break;
1290 case P_VEX3:
1291 case P_VEX2:
1292 if (!(ins->rex & REX_V))
1293 return -1;
1294 break;
1295 default:
1296 break;
1297 }
1298
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001299 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001303 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 return -1;
1305 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001306 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001308 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 ins->rex &= ~REX_W;
1310 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001311 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 ins->rex |= REX_W;
1313 bad32 &= ~REX_W;
1314 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001315 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 /* Follow REX_W */
1317 break;
1318 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001319
H. Peter Anvinfc561202011-07-07 16:58:22 -07001320 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1322 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001323 } else if (!(ins->rex & REX_EV) &&
1324 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1325 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1326 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001328 if (ins->rex & REX_EV)
1329 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001330 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1331 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001332 length += 3;
1333 else
1334 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001335 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 if (ins->rex & REX_H) {
1337 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1338 return -1;
1339 } else if (bits == 64) {
1340 length++;
1341 } else if ((ins->rex & REX_L) &&
1342 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001343 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001345 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001346 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 length++;
1348 } else {
1349 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1350 return -1;
1351 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001352 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001353
1354 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001355 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001356 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001357 "instruction is not lockable");
1358 }
1359
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001360 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001361
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001362 /*
1363 * when BND prefix is set by DEFAULT directive,
1364 * BND prefix is added to every appropriate instruction line
1365 * unless it is overridden by NOBND prefix.
1366 */
1367 if (globalbnd &&
1368 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1369 ins->prefixes[PPS_REP] = P_BND;
1370
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001371 return length;
1372}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001373
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001374static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1375{
1376 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001377 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001378 !(ins->rex & (REX_V | REX_EV)) &&
1379 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001380 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001381 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001382 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001383 return 1;
1384 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001385 }
1386
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001387 return 0;
1388}
1389
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001390static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001391 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001393{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001394 uint8_t c;
1395 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001396 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001397 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001398 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001399 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001400 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001401 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001402 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001403
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001404 ins->rex_done = false;
1405
H. Peter Anvin839eca22007-10-29 23:12:47 -07001406 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001407 c = *codes++;
1408 op1 = (c & 3) + ((opex & 1) << 2);
1409 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1410 opx = &ins->oprs[op1];
1411 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001412
H. Peter Anvin839eca22007-10-29 23:12:47 -07001413 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 case 01:
1415 case 02:
1416 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001417 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001418 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001419 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 codes += c;
1421 offset += c;
1422 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001423
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001424 case 05:
1425 case 06:
1426 case 07:
1427 opex = c;
1428 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001429
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001430 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001431 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001432 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001433 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 offset += 1;
1435 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001436
Jin Kyu Song164d6072013-10-15 19:10:13 -07001437 case4(014):
1438 break;
1439
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001440 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001441 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001442 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001443 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001445 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 offset += 1;
1447 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001448
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001449 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001450 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001451 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001452 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001453 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 offset += 1;
1455 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001456
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001457 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001458 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001459 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001460 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001461 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 offset += 2;
1463 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001464
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001465 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001466 if (opx->type & (BITS16 | BITS32))
1467 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 else
1469 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001470 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001471 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001472 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001473 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 offset += size;
1475 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001476
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001477 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001478 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001479 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001480 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001481 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 offset += 4;
1483 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001484
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001486 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001487 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001488 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001489 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001490 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 offset += size;
1492 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001493
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001495 if (opx->segment != segment) {
1496 data = opx->offset;
1497 out(offset, segment, &data,
1498 OUT_REL1ADR, insn_end - offset,
1499 opx->segment, opx->wrt);
1500 } else {
1501 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001502 if (data > 127 || data < -128)
1503 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001504 out(offset, segment, &data,
1505 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1506 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 offset += 1;
1508 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001509
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001511 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001512 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001514 offset += 8;
1515 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001516
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001517 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001518 if (opx->segment != segment) {
1519 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001521 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001522 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001524 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001526 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 }
1528 offset += 2;
1529 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001530
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001531 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001532 if (opx->type & (BITS16 | BITS32 | BITS64))
1533 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 else
1535 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001536 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001537 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001538 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1540 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001542 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001544 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 }
1546 offset += size;
1547 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001548
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001549 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 if (opx->segment != segment) {
1551 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001553 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001554 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001558 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 }
1560 offset += 4;
1561 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001562
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001564 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001565 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1566 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001567 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001568 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001569 outfmt->segbase(1 + opx->segment),
1570 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001571 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001573
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 case 0172:
1575 c = *codes++;
1576 opx = &ins->oprs[c >> 3];
1577 bytes[0] = nasm_regvals[opx->basereg] << 4;
1578 opx = &ins->oprs[c & 7];
1579 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1580 errfunc(ERR_NONFATAL,
1581 "non-absolute expression not permitted as argument %d",
1582 c & 7);
1583 } else {
1584 if (opx->offset & ~15) {
1585 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1586 "four-bit argument exceeds bounds");
1587 }
1588 bytes[0] |= opx->offset & 15;
1589 }
1590 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1591 offset++;
1592 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 case 0173:
1595 c = *codes++;
1596 opx = &ins->oprs[c >> 4];
1597 bytes[0] = nasm_regvals[opx->basereg] << 4;
1598 bytes[0] |= c & 15;
1599 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1600 offset++;
1601 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001602
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001603 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 bytes[0] = nasm_regvals[opx->basereg] << 4;
1605 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1606 offset++;
1607 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001610 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1612 (int32_t)data != (int64_t)data) {
1613 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1614 "signed dword immediate exceeds bounds");
1615 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001616 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 opx->segment, opx->wrt);
1618 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001619 break;
1620
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001621 case4(0240):
1622 case 0250:
1623 codes += 3;
1624 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1625 EVEX_P2Z | EVEX_P2AAA, 2);
1626 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1627 bytes[0] = 0x62;
1628 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001629 bytes[1] = ((((ins->rex & 7) << 5) |
1630 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1631 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001632 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1633 ((~ins->vexreg & 15) << 3) |
1634 (1 << 2) | (ins->vex_wlp & 3);
1635 bytes[3] = ins->evex_p[2];
1636 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1637 offset += 4;
1638 break;
1639
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001640 case4(0260):
1641 case 0270:
1642 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001643 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1644 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1646 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1647 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001648 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1650 offset += 3;
1651 } else {
1652 bytes[0] = 0xc5;
1653 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001654 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001655 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1656 offset += 2;
1657 }
1658 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001659
H. Peter Anvine014f352012-02-25 22:35:19 -08001660 case 0271:
1661 case 0272:
1662 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001663 break;
1664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001665 case4(0274):
1666 {
1667 uint64_t uv, um;
1668 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001670 if (ins->rex & REX_W)
1671 s = 64;
1672 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1673 s = 16;
1674 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1675 s = 32;
1676 else
1677 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001678
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 um = (uint64_t)2 << (s-1);
1680 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001681
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 if (uv > 127 && uv < (uint64_t)-128 &&
1683 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001684 /* If this wasn't explicitly byte-sized, warn as though we
1685 * had fallen through to the imm16/32/64 case.
1686 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001687 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001688 "%s value exceeds bounds",
1689 (opx->type & BITS8) ? "signed byte" :
1690 s == 16 ? "word" :
1691 s == 32 ? "dword" :
1692 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001694 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001695 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001696 out(offset, segment, &data, OUT_ADDRESS, 1,
1697 opx->segment, opx->wrt);
1698 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001699 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001700 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1701 NO_SEG);
1702 }
1703 offset += 1;
1704 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001705 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001709
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001711 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001713 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 offset += 1;
1715 } else
1716 offset += 0;
1717 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001718
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001720 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001722 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 offset += 1;
1724 } else
1725 offset += 0;
1726 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001727
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 case 0312:
1729 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001730
Keith Kaniosb7a89542007-04-12 02:40:54 +00001731 case 0313:
1732 ins->rex = 0;
1733 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001734
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001735 case4(0314):
1736 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001737
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001743 case 0323:
1744 break;
1745
Keith Kaniosb7a89542007-04-12 02:40:54 +00001746 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001747 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001749
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 case 0325:
1751 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001752
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001753 case 0326:
1754 break;
1755
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001757 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001758 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 offset += 1;
1760 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001764
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001765 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001767 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001768 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 offset += 1;
1770 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001771
Keith Kanios48af1772007-08-17 07:37:52 +00001772 case 0334:
1773 if (ins->rex & REX_R) {
1774 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001775 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001776 offset += 1;
1777 }
1778 ins->rex &= ~(REX_L|REX_R);
1779 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001780
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001781 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001782 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001783
H. Peter Anvin962e3052008-08-28 17:47:16 -07001784 case 0336:
1785 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001786 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001787
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 if (ins->oprs[0].segment != NO_SEG)
1790 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1791 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001792 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 if (size > 0)
1794 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001795 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 offset += size;
1797 }
1798 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001799
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 case 0341:
1801 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001802
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001803 case 0360:
1804 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case 0361:
1807 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001808 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1809 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001810 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001811
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001812 case 0364:
1813 case 0365:
1814 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001815
Keith Kanios48af1772007-08-17 07:37:52 +00001816 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001818 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001819 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001820 offset += 1;
1821 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001822
Jin Kyu Song03041092013-10-15 19:38:51 -07001823 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 case 0373:
1827 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001828 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 offset += 1;
1830 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001831
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001832 case 0374:
1833 eat = EA_XMMVSIB;
1834 break;
1835
1836 case 0375:
1837 eat = EA_YMMVSIB;
1838 break;
1839
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001840 case 0376:
1841 eat = EA_ZMMVSIB;
1842 break;
1843
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 case4(0100):
1845 case4(0110):
1846 case4(0120):
1847 case4(0130):
1848 case4(0200):
1849 case4(0204):
1850 case4(0210):
1851 case4(0214):
1852 case4(0220):
1853 case4(0224):
1854 case4(0230):
1855 case4(0234):
1856 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 ea ea_data;
1858 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001860 uint8_t *p;
1861 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001863
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001864 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 /* pick rfield from operand b (opx) */
1866 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001867 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 } else {
1869 /* rfield is constant */
1870 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001872 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001874 if (process_ea(opy, &ea_data, bits,
1875 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001877
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 p = bytes;
1879 *p++ = ea_data.modrm;
1880 if (ea_data.sib_present)
1881 *p++ = ea_data.sib;
1882
1883 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001884 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001886 /*
1887 * Make sure the address gets the right offset in case
1888 * the line breaks in the .lst file (BR 1197827)
1889 */
1890 offset += s;
1891 s = 0;
1892
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001893 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001894 /* use compressed displacement, if available */
1895 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 if (ea_data.rip) {
1898 if (opy->segment == segment) {
1899 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001900 if (overflow_signed(data, ea_data.bytes))
1901 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 out(offset, segment, &data, OUT_ADDRESS,
1903 ea_data.bytes, NO_SEG, NO_SEG);
1904 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001905 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001906 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 insn_end - offset, opy->segment, opy->wrt);
1908 }
1909 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001910 int asize = ins->addr_size >> 3;
1911 int atype = ea_data.bytes;
1912
1913 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001914 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001915 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001916 warn_overflow(ERR_PASS2, ea_data.bytes);
1917
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001918 if (asize > ea_data.bytes) {
1919 /*
1920 * If the address isn't the full width of
1921 * the address size, treat is as signed...
1922 */
1923 atype = -atype;
1924 }
1925
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001927 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 }
1930 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 }
1932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001934 default:
1935 errfunc(ERR_PANIC, "internal instruction table corrupt"
1936 ": instruction code \\%o (0x%02X) given", c, c);
1937 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001939 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001940}
1941
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001942static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001943{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001944 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001945 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001946 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947}
1948
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001949static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001950{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001951 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001953 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001954}
1955
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001956static int op_rexflags(const operand * o, int mask)
1957{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001958 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001959 int val;
1960
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001961 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001962 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001963
H. Peter Anvina4835d42008-05-20 14:21:29 -07001964 flags = nasm_reg_flags[o->basereg];
1965 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001966
1967 return rexflags(val, flags, mask);
1968}
1969
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001970static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001971{
1972 int rex = 0;
1973
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001974 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001975 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001976 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001977 rex |= REX_W;
1978 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1979 rex |= REX_H;
1980 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1981 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001982
1983 return rex & mask;
1984}
1985
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001986static int evexflags(int val, decoflags_t deco,
1987 int mask, uint8_t byte)
1988{
1989 int evex = 0;
1990
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001991 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001992 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001993 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001994 evex |= (EVEX_P0RP | EVEX_P0X);
1995 break;
1996 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001997 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001998 evex |= EVEX_P2VP;
1999 if (deco & Z)
2000 evex |= EVEX_P2Z;
2001 if (deco & OPMASK_MASK)
2002 evex |= deco & EVEX_P2AAA;
2003 break;
2004 }
2005 return evex & mask;
2006}
2007
2008static int op_evexflags(const operand * o, int mask, uint8_t byte)
2009{
2010 int val;
2011
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002012 val = nasm_regvals[o->basereg];
2013
2014 return evexflags(val, o->decoflags, mask, byte);
2015}
2016
H. Peter Anvin23595f52009-07-25 17:44:25 -07002017static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 insn *instruction,
2019 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002020{
2021 const struct itemplate *temp;
2022 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002023 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002024 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002025 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002026 int i;
2027
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002028 /* broadcasting uses a different data element size */
2029 for (i = 0; i < instruction->operands; i++)
2030 if (i == broadcast)
2031 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2032 else
2033 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002034
2035 merr = MERR_INVALOP;
2036
2037 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002038 temp->opcode != I_none; temp++) {
2039 m = matches(temp, instruction, bits);
2040 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002041 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002042 m = MOK_GOOD;
2043 else
2044 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002045 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 /*
2047 * Missing operand size and a candidate for fuzzy matching...
2048 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002049 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002050 if (i == broadcast)
2051 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2052 else
2053 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002054 opsizemissing = true;
2055 }
2056 if (m > merr)
2057 merr = m;
2058 if (merr == MOK_GOOD)
2059 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002060 }
2061
2062 /* No match, but see if we can get a fuzzy operand size match... */
2063 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002064 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002065
2066 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 /*
2068 * We ignore extrinsic operand sizes on registers, so we should
2069 * never try to fuzzy-match on them. This also resolves the case
2070 * when we have e.g. "xmmrm128" in two different positions.
2071 */
2072 if (is_class(REGISTER, instruction->oprs[i].type))
2073 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002074
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 /* This tests if xsizeflags[i] has more than one bit set */
2076 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2077 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002078
Jin Kyu Song7903c072013-10-30 03:00:12 -07002079 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002080 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002081 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2082 BITS32 : BITS64);
2083 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002084 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002085 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002086 }
2087
2088 /* Try matching again... */
2089 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002090 temp->opcode != I_none; temp++) {
2091 m = matches(temp, instruction, bits);
2092 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002093 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 m = MOK_GOOD;
2095 else
2096 m = MERR_INVALOP;
2097 }
2098 if (m > merr)
2099 merr = m;
2100 if (merr == MOK_GOOD)
2101 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002102 }
2103
H. Peter Anvina81655b2009-07-25 18:15:28 -07002104done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002105 *tempp = temp;
2106 return merr;
2107}
2108
Mark Charneydcaef4b2014-10-09 13:45:17 -04002109static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2110{
2111 opflags_t opsize = opflags & SIZE_MASK;
2112 uint8_t brcast_num;
2113
2114 /*
2115 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2116 * this cannot be a simple arithmetic calculation.
2117 */
2118 if (brsize > BITS64)
2119 errfunc(ERR_FATAL,
2120 "size of broadcasting element is greater than 64 bits");
2121
2122 switch (opsize) {
2123 case BITS64:
2124 brcast_num = BITS64 / brsize;
2125 break;
2126 default:
2127 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2128 break;
2129 }
2130
2131 return brcast_num;
2132}
2133
H. Peter Anvin65289e82009-07-25 17:25:11 -07002134static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002135 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002136{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002137 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002138 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002139 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002140
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002141 /*
2142 * Check the opcode
2143 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002145 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002146
2147 /*
2148 * Count the operands
2149 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002151 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002152
2153 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002154 * Is it legal?
2155 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002156 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002157 return MERR_INVALOP;
2158
2159 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002160 * {evex} available?
2161 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002162 switch (instruction->prefixes[PPS_VEX]) {
2163 case P_EVEX:
2164 if (!itemp_has(itemp, IF_EVEX))
2165 return MERR_ENCMISMATCH;
2166 break;
2167 case P_VEX3:
2168 case P_VEX2:
2169 if (!itemp_has(itemp, IF_VEX))
2170 return MERR_ENCMISMATCH;
2171 break;
2172 default:
2173 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002174 }
2175
2176 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002177 * Check that no spurious colons or TOs are present
2178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 for (i = 0; i < itemp->operands; i++)
2180 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002181 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002182
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002184 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002185 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002186 switch (itemp_smask(itemp)) {
2187 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002188 asize = BITS8;
2189 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002190 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002191 asize = BITS16;
2192 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002193 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002194 asize = BITS32;
2195 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002196 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002197 asize = BITS64;
2198 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002199 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002200 asize = BITS128;
2201 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002202 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 asize = BITS256;
2204 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002206 asize = BITS512;
2207 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002208 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 switch (bits) {
2210 case 16:
2211 asize = BITS16;
2212 break;
2213 case 32:
2214 asize = BITS32;
2215 break;
2216 case 64:
2217 asize = BITS64;
2218 break;
2219 default:
2220 asize = 0;
2221 break;
2222 }
2223 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002224 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = 0;
2226 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002227 }
2228
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 memset(size, 0, sizeof size);
2233 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002234 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 /* S- flags apply to all operands */
2236 for (i = 0; i < MAX_OPERANDS; i++)
2237 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002238 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002239
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002240 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002241 * Check that the operand flags all match up,
2242 * it's a bit tricky so lets be verbose:
2243 *
2244 * 1) Find out the size of operand. If instruction
2245 * doesn't have one specified -- we're trying to
2246 * guess it either from template (IF_S* flag) or
2247 * from code bits.
2248 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002249 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 * template has an operand size specified AND this size differ
2251 * from which instruction has (perhaps we got it from code bits)
2252 * we are:
2253 * a) Check that only size of instruction and operand is differ
2254 * other characteristics do match
2255 * b) Perhaps it's a register specified in instruction so
2256 * for such a case we just mark that operand as "size
2257 * missing" and this will turn on fuzzy operand size
2258 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002259 */
2260 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002261 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002262 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002263 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002264 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002265 opflags_t template_opsize, insn_opsize;
2266
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 if (!(type & SIZE_MASK))
2268 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002269
Jin Kyu Song7903c072013-10-30 03:00:12 -07002270 insn_opsize = type & SIZE_MASK;
2271 if (!is_broadcast) {
2272 template_opsize = itemp->opd[i] & SIZE_MASK;
2273 } else {
2274 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2275 /*
2276 * when broadcasting, the element size depends on
2277 * the instruction type. decorator flag should match.
2278 */
2279
2280 if (deco_brsize) {
2281 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002282 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002283 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002284 } else {
2285 template_opsize = 0;
2286 }
2287 }
2288
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002289 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002290 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002291 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002292 } else if (template_opsize) {
2293 if (template_opsize != insn_opsize) {
2294 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002295 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002296 } else if (!is_class(REGISTER, type)) {
2297 /*
2298 * Note: we don't honor extrinsic operand sizes for registers,
2299 * so "missing operand size" for a register should be
2300 * considered a wildcard match rather than an error.
2301 */
2302 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002303 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002304 } else if (is_broadcast &&
2305 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002306 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002307 /*
2308 * broadcasting opsize matches but the number of repeated memory
2309 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002310 * if 64b double precision float is broadcasted to ymm (256b),
2311 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002312 */
2313 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002314 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002315 }
2316 }
2317
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002318 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002319 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002320
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002321 /*
2322 * Check operand sizes
2323 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002324 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2325 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002327 asize = itemp->opd[i] & SIZE_MASK;
2328 if (asize) {
2329 for (i = 0; i < oprs; i++)
2330 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 break;
2332 }
2333 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002334 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002336 }
2337
Keith Kaniosb7a89542007-04-12 02:40:54 +00002338 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 if (!(itemp->opd[i] & SIZE_MASK) &&
2340 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002341 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002342 }
2343
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002344 /*
2345 * Check template is okay at the set cpu level
2346 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002347 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002348 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002349
Keith Kaniosb7a89542007-04-12 02:40:54 +00002350 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002351 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002352 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002353 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002354 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002356 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002357 * If we have a HLE prefix, look for the NOHLE flag
2358 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002359 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002360 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2361 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2362 return MERR_BADHLE;
2363
2364 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002365 * Check if special handling needed for Jumps
2366 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002367 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002368 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369
Jin Kyu Song03041092013-10-15 19:38:51 -07002370 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002371 * Check if BND prefix is allowed.
2372 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002373 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002374 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002375 (has_prefix(instruction, PPS_REP, P_BND) ||
2376 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002377 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002378 else if (itemp_has(itemp, IF_BND) &&
2379 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2380 has_prefix(instruction, PPS_REP, P_REPNZ)))
2381 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002382
H. Peter Anvin60926242009-07-26 16:25:38 -07002383 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002384}
2385
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002386/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002387 * Check if ModR/M.mod should/can be 01.
2388 * - EAF_BYTEOFFS is set
2389 * - offset can fit in a byte when EVEX is not used
2390 * - offset can be compressed when EVEX is used
2391 */
2392#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2393 (o >= -128 && o <= 127 && \
2394 seg == NO_SEG && !forw_ref && \
2395 !(input->eaflags & EAF_WORDOFFS) && \
2396 !(ins->rex & REX_EV)) || \
2397 (ins->rex & REX_EV && \
2398 is_disp8n(input, ins, &output->disp8)))
2399
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002400static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002401 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002402{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002403 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002405 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002406
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002407 output->type = EA_SCALAR;
2408 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002409 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002410
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002411 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002412 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002413 /* EVEX.R' flag for the REG operand */
2414 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002415
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002416 if (is_class(REGISTER, input->type)) {
2417 /*
2418 * It's a direct register.
2419 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002420 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002421 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002422
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002424 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002425
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002426 /* broadcasting is not available with a direct register operand. */
2427 if (input->decoflags & BRDCAST_MASK) {
2428 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2429 goto err;
2430 }
2431
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002432 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002433 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002434 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002435 output->bytes = 0; /* no offset necessary either */
2436 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2437 } else {
2438 /*
2439 * It's a memory reference.
2440 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441
2442 /* Embedded rounding or SAE is not available with a mem ref operand. */
2443 if (input->decoflags & (ER | SAE)) {
2444 nasm_error(ERR_NONFATAL,
2445 "Embedded rounding is available only with reg-reg op.");
2446 return -1;
2447 }
2448
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002449 if (input->basereg == -1 &&
2450 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 /*
2452 * It's a pure offset.
2453 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002454 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2455 input->segment == NO_SEG) {
2456 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2457 input->type &= ~IP_REL;
2458 input->type |= MEMORY;
2459 }
2460
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002461 if (bits == 64 &&
2462 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2463 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2464 return -1;
2465 }
2466
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002467 if (eaflags & EAF_BYTEOFFS ||
2468 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002469 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2470 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2471 }
2472
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002473 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002474 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002475 output->sib = GEN_SIB(0, 4, 5);
2476 output->bytes = 4;
2477 output->modrm = GEN_MODRM(0, rfield, 4);
2478 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002479 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002480 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002481 output->bytes = (addrbits != 16 ? 4 : 2);
2482 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2483 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002484 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002485 } else {
2486 /*
2487 * It's an indirection.
2488 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002490 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002492 int t, it, bt; /* register numbers */
2493 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 if (s == 0)
2496 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002497
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002498 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002499 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002500 ix = nasm_reg_flags[i];
2501 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002502 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002503 ix = 0;
2504 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002505
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002506 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002507 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002508 bx = nasm_reg_flags[b];
2509 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002510 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 bx = 0;
2512 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002513
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002514 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002515 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002516 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002517 int32_t o = input->offset;
2518 int mod, scale, index, base;
2519
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002520 /*
2521 * For a vector SIB, one has to be a vector and the other,
2522 * if present, a GPR. The vector must be the index operand.
2523 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002524 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002525 if (s == 0)
2526 s = 1;
2527 else if (s != 1)
2528 goto err;
2529
2530 t = bt, bt = it, it = t;
2531 x = bx, bx = ix, ix = x;
2532 }
2533
2534 if (bt != -1) {
2535 if (REG_GPR & ~bx)
2536 goto err;
2537 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2538 sok &= bx;
2539 else
2540 goto err;
2541 }
2542
2543 /*
2544 * While we're here, ensure the user didn't specify
2545 * WORD or QWORD
2546 */
2547 if (input->disp_size == 16 || input->disp_size == 64)
2548 goto err;
2549
2550 if (addrbits == 16 ||
2551 (addrbits == 32 && !(sok & BITS32)) ||
2552 (addrbits == 64 && !(sok & BITS64)))
2553 goto err;
2554
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002555 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2556 : ((ix & YMMREG & ~REG_EA)
2557 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002559 output->rex |= rexflags(it, ix, REX_X);
2560 output->rex |= rexflags(bt, bx, REX_B);
2561 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562
2563 index = it & 7; /* it is known to be != -1 */
2564
2565 switch (s) {
2566 case 1:
2567 scale = 0;
2568 break;
2569 case 2:
2570 scale = 1;
2571 break;
2572 case 4:
2573 scale = 2;
2574 break;
2575 case 8:
2576 scale = 3;
2577 break;
2578 default: /* then what the smeg is it? */
2579 goto err; /* panic */
2580 }
2581
2582 if (bt == -1) {
2583 base = 5;
2584 mod = 0;
2585 } else {
2586 base = (bt & 7);
2587 if (base != REG_NUM_EBP && o == 0 &&
2588 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002589 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002590 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002591 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002592 mod = 1;
2593 else
2594 mod = 2;
2595 }
2596
2597 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002598 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2599 output->modrm = GEN_MODRM(mod, rfield, 4);
2600 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002602 /*
2603 * it must be a 32/64-bit memory reference. Firstly we have
2604 * to check that all registers involved are type E/Rxx.
2605 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002606 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002608
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002609 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002610 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2611 sok &= ix;
2612 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002613 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002614 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002615
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002616 if (bt != -1) {
2617 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002618 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002619 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 sok &= bx;
2622 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002624 /*
2625 * While we're here, ensure the user didn't specify
2626 * WORD or QWORD
2627 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002628 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002629 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002630
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002631 if (addrbits == 16 ||
2632 (addrbits == 32 && !(sok & BITS32)) ||
2633 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002635
Keith Kaniosb7a89542007-04-12 02:40:54 +00002636 /* now reorganize base/index */
2637 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 ((hb == b && ht == EAH_NOTBASE) ||
2639 (hb == i && ht == EAH_MAKEBASE))) {
2640 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002641 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002642 x = bx, bx = ix, ix = x;
2643 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002644
Jin Kyu Song164d6072013-10-15 19:10:13 -07002645 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002647 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002648 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002649 if (eaflags & EAF_MIB) {
2650 /* only for mib operands */
2651 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2652 /*
2653 * make a single reg index [reg*1].
2654 * gas uses this form for an explicit index register.
2655 */
2656 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2657 }
2658 if ((ht == EAH_SUMMED) && bt == -1) {
2659 /* separate once summed index into [base, index] */
2660 bt = it, bx = ix, s--;
2661 }
2662 } else {
2663 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002664 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002665 s == 3 || s == 5 || s == 9) && bt == -1) {
2666 /* convert 3*EAX to EAX+2*EAX */
2667 bt = it, bx = ix, s--;
2668 }
2669 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002670 (eaflags & EAF_TIMESTWO) &&
2671 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002672 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002673 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002674 * to sib format with 0x0 displacement - [EAX*1+0].
2675 */
2676 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2677 }
2678 }
Keith Kanios48af1772007-08-17 07:37:52 +00002679 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002681 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 x = ix, ix = bx, bx = x;
2683 }
2684 if (it == REG_NUM_ESP ||
2685 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002686 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002687
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002688 output->rex |= rexflags(it, ix, REX_X);
2689 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002690
Keith Kanios48af1772007-08-17 07:37:52 +00002691 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002692 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002694
Keith Kaniosb7a89542007-04-12 02:40:54 +00002695 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002698 } else {
2699 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002700 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002701 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002702 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002703 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002704 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002705 mod = 1;
2706 else
2707 mod = 2;
2708 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002709
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002710 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002711 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2712 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002713 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002714 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002716
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 if (it == -1)
2718 index = 4, s = 1;
2719 else
2720 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 switch (s) {
2723 case 1:
2724 scale = 0;
2725 break;
2726 case 2:
2727 scale = 1;
2728 break;
2729 case 4:
2730 scale = 2;
2731 break;
2732 case 8:
2733 scale = 3;
2734 break;
2735 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002736 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002738
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 if (bt == -1) {
2740 base = 5;
2741 mod = 0;
2742 } else {
2743 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002744 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002745 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002746 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002748 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002749 mod = 1;
2750 else
2751 mod = 2;
2752 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002753
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002754 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002755 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2756 output->modrm = GEN_MODRM(mod, rfield, 4);
2757 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 }
2759 } else { /* it's 16-bit */
2760 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002761 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002762
Keith Kaniosb7a89542007-04-12 02:40:54 +00002763 /* check for 64-bit long mode */
2764 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002765 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002766
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002768 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2769 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002770 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002771
Keith Kaniosb7a89542007-04-12 02:40:54 +00002772 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002773 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002774 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002775
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 if (b == -1 && i != -1) {
2779 int tmp = b;
2780 b = i;
2781 i = tmp;
2782 } /* swap */
2783 if ((b == R_SI || b == R_DI) && i != -1) {
2784 int tmp = b;
2785 b = i;
2786 i = tmp;
2787 }
2788 /* have BX/BP as base, SI/DI index */
2789 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002790 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 if (i != -1 && b != -1 &&
2792 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002794 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 rm = -1;
2798 if (i != -1)
2799 switch (i * 256 + b) {
2800 case R_SI * 256 + R_BX:
2801 rm = 0;
2802 break;
2803 case R_DI * 256 + R_BX:
2804 rm = 1;
2805 break;
2806 case R_SI * 256 + R_BP:
2807 rm = 2;
2808 break;
2809 case R_DI * 256 + R_BP:
2810 rm = 3;
2811 break;
2812 } else
2813 switch (b) {
2814 case R_SI:
2815 rm = 4;
2816 break;
2817 case R_DI:
2818 rm = 5;
2819 break;
2820 case R_BP:
2821 rm = 6;
2822 break;
2823 case R_BX:
2824 rm = 7;
2825 break;
2826 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002827 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002828 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002829
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002830 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002831 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002833 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 mod = 1;
2835 else
2836 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002837
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002838 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002839 output->bytes = mod; /* bytes of offset needed */
2840 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 }
2842 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002843 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002844
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002845 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002846 return output->type;
2847
2848err:
2849 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002850}
2851
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002852static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002853{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002854 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002855 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002856
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002857 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002858
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002859 switch (ins->prefixes[PPS_ASIZE]) {
2860 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002861 valid &= 16;
2862 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002863 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002864 valid &= 32;
2865 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002866 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002867 valid &= 64;
2868 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002869 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002870 valid &= (addrbits == 32) ? 16 : 32;
2871 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002872 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002873 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002874 }
2875
2876 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002877 if (is_class(MEMORY, ins->oprs[j].type)) {
2878 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002879
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002880 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002881 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002882 i = 0;
2883 else
2884 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002885
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002887 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002888 b = 0;
2889 else
2890 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 if (ins->oprs[j].scale == 0)
2893 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 if (!i && !b) {
2896 int ds = ins->oprs[j].disp_size;
2897 if ((addrbits != 64 && ds > 8) ||
2898 (addrbits == 64 && ds == 16))
2899 valid &= ds;
2900 } else {
2901 if (!(REG16 & ~b))
2902 valid &= 16;
2903 if (!(REG32 & ~b))
2904 valid &= 32;
2905 if (!(REG64 & ~b))
2906 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002907
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 if (!(REG16 & ~i))
2909 valid &= 16;
2910 if (!(REG32 & ~i))
2911 valid &= 32;
2912 if (!(REG64 & ~i))
2913 valid &= 64;
2914 }
2915 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002916 }
2917
2918 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002920 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002922 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002924 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 /* Impossible... */
2926 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2927 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002928 }
2929
2930 defdisp = ins->addr_size == 16 ? 16 : 32;
2931
2932 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2934 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2935 /*
2936 * mem_offs sizes must match the address size; if not,
2937 * strip the MEM_OFFS bit and match only EA instructions
2938 */
2939 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2940 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002941 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002942}