blob: 00acd208b4a8bbff83704f2ce07f12390a5ca591 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003 * Copyright 1996-2013 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 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000176#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
178#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000179#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include "assemble.h"
181#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700182#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
H. Peter Anvin65289e82009-07-25 17:25:11 -0700184enum match_result {
185 /*
186 * Matching errors. These should be sorted so that more specific
187 * errors come later in the sequence.
188 */
189 MERR_INVALOP,
190 MERR_OPSIZEMISSING,
191 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700192 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700193 MERR_BADCPU,
194 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800195 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700196 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700197 MERR_BADBND,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700198 /*
199 * Matching success; the conditional ones first
200 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400201 MOK_JUMP, /* Matching OK but needs jmp_match() */
202 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700203};
204
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000205typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700206 enum ea_type type; /* what kind of EA is this? */
207 int sib_present; /* is a SIB byte necessary? */
208 int bytes; /* # of bytes of offset needed */
209 int size; /* lazy - this is sib+bytes+1 */
210 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700211 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000212} ea;
213
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400214#define GEN_SIB(scale, index, base) \
215 (((scale) << 6) | ((index) << 3) | ((base)))
216
217#define GEN_MODRM(mod, reg, rm) \
218 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
219
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700220static iflags_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221static efunc errfunc;
222static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000223static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800225static int64_t calcsize(int32_t, int64_t, int, insn *,
226 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700227static void gencode(int32_t segment, int64_t offset, int bits,
228 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400229 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700230static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400231 insn *instruction,
232 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700233static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700234static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000235static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700236static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000237static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700238static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700239static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000240
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700241static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700242
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400243static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000244{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700245 return ins->prefixes[pos] == prefix;
246}
247
248static void assert_no_prefix(insn * ins, enum prefix_pos pos)
249{
250 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400251 errfunc(ERR_NONFATAL, "invalid %s prefix",
252 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700253}
254
255static const char *size_name(int size)
256{
257 switch (size) {
258 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400259 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700260 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400261 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400263 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400265 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400267 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700268 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400269 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700270 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700272 case 64:
273 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000276 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700277}
278
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400279static void warn_overflow(int pass, int size)
280{
281 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
282 "%s data exceeds bounds", size_name(size));
283}
284
285static void warn_overflow_const(int64_t data, int size)
286{
287 if (overflow_general(data, size))
288 warn_overflow(ERR_PASS1, size);
289}
290
291static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700292{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100293 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400294 if (overflow_general(o->offset, size))
295 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700296 }
297}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400298
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000299/*
300 * This routine wrappers the real output format's output routine,
301 * in order to pass a copy of the data off to the listing file
302 * generator at the same time.
303 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800304static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800305 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400306 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000307{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000308 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000309 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800310 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000311
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800312 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400313 /*
314 * This is a non-relocated address, and we're going to
315 * convert it into RAWDATA format.
316 */
317 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800318
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400319 if (size > 8) {
320 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
321 return;
322 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700323
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400324 WRITEADDR(q, *(int64_t *)data, size);
325 data = p;
326 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000327 }
328
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800329 list->output(offset, data, type, size);
330
Frank Kotlerabebb082003-09-06 04:45:37 +0000331 /*
332 * this call to src_get determines when we call the
333 * debug-format-specific "linenum" function
334 * it updates lineno and lnfname to the current values
335 * returning 0 if "same as last time", -2 if lnfname
336 * changed, and the amount by which lineno changed,
337 * if it did. thus, these variables must be static
338 */
339
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400340 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000342
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800343 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000344}
345
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400346static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
347{
348 if (opx->segment != NO_SEG) {
349 uint64_t data = opx->offset;
350 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
351 } else {
352 uint8_t byte = opx->offset;
353 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
354 }
355}
356
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700357static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800358 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800360 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800361 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000362 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000363
H. Peter Anvin755f5212012-02-25 11:41:34 -0800364 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700365 return false;
366 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400367 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700368 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400369 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700370
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800371 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100372
Victor van den Elzen154e5922009-02-25 17:32:00 +0100373 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100374 /* Be optimistic in pass 1 */
375 return true;
376
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700378 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000379
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700380 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
381 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000383
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700384int64_t assemble(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400385 insn * instruction, struct ofmt *output, efunc error,
386 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000387{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000388 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700390 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800391 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000392 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800393 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300394 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000395
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000397 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 outfmt = output; /* likewise */
399 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000400
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300401 wsize = idata_bytes(instruction->opcode);
402 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000404
H. Peter Anvineba20a72002-04-30 20:53:55 +0000405 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000407 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (t < 0)
409 errfunc(ERR_PANIC,
410 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000411
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400413 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400415 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700416 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400417 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000418 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700419 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800421 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400422 offset += wsize;
423 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700424 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400425 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800429 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000431
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 if (align) {
433 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100434 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800435 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 }
437 offset += e->stringlen + align;
438 }
439 }
440 if (t > 0 && t == instruction->times - 1) {
441 /*
442 * Dummy call to list->output to give the offset to the
443 * listing module.
444 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800445 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 list->uplevel(LIST_TIMES);
447 }
448 }
449 if (instruction->times > 1)
450 list->downlevel(LIST_TIMES);
451 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000452 }
453
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700455 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000457
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400458 fp = fopen(fname, "rb");
459 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
461 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400462 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
464 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200465 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400466 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700467 static char buf[4096];
468 size_t t = instruction->times;
469 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400470 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 len = ftell(fp);
473 if (instruction->eops->next) {
474 base = instruction->eops->next->offset;
475 len -= base;
476 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700477 len > (size_t)instruction->eops->next->next->offset)
478 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 }
480 /*
481 * Dummy call to list->output to give the offset to the
482 * listing module.
483 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800484 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 list->uplevel(LIST_INCBIN);
486 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700487 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000488
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 fseek(fp, base, SEEK_SET);
490 l = len;
491 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700492 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400493 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 if (!m) {
495 /*
496 * This shouldn't happen unless the file
497 * actually changes while we are reading
498 * it.
499 */
500 error(ERR_NONFATAL,
501 "`incbin': unexpected EOF while"
502 " reading file `%s'", fname);
503 t = 0; /* Try to exit cleanly */
504 break;
505 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800506 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 NO_SEG, NO_SEG);
508 l -= m;
509 }
510 }
511 list->downlevel(LIST_INCBIN);
512 if (instruction->times > 1) {
513 /*
514 * Dummy call to list->output to give the offset to the
515 * listing module.
516 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800517 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 list->uplevel(LIST_TIMES);
519 list->downlevel(LIST_TIMES);
520 }
521 fclose(fp);
522 return instruction->times * len;
523 }
524 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 }
526
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700527 /* Check to see if we need an address-size prefix */
528 add_asp(instruction, bits);
529
H. Peter Anvin23595f52009-07-25 17:44:25 -0700530 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700531
H. Peter Anvin23595f52009-07-25 17:44:25 -0700532 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400533 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800534 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400535 itimes = instruction->times;
536 if (insn_size < 0) /* shouldn't be, on pass two */
537 error(ERR_PANIC, "errors made it through from pass one");
538 else
539 while (itimes--) {
540 for (j = 0; j < MAXPREFIX; j++) {
541 uint8_t c = 0;
542 switch (instruction->prefixes[j]) {
543 case P_WAIT:
544 c = 0x9B;
545 break;
546 case P_LOCK:
547 c = 0xF0;
548 break;
549 case P_REPNE:
550 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800551 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700552 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400553 c = 0xF2;
554 break;
555 case P_REPE:
556 case P_REPZ:
557 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800558 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400559 c = 0xF3;
560 break;
561 case R_CS:
562 if (bits == 64) {
563 error(ERR_WARNING | ERR_PASS2,
564 "cs segment base generated, but will be ignored in 64-bit mode");
565 }
566 c = 0x2E;
567 break;
568 case R_DS:
569 if (bits == 64) {
570 error(ERR_WARNING | ERR_PASS2,
571 "ds segment base generated, but will be ignored in 64-bit mode");
572 }
573 c = 0x3E;
574 break;
575 case R_ES:
576 if (bits == 64) {
577 error(ERR_WARNING | ERR_PASS2,
578 "es segment base generated, but will be ignored in 64-bit mode");
579 }
580 c = 0x26;
581 break;
582 case R_FS:
583 c = 0x64;
584 break;
585 case R_GS:
586 c = 0x65;
587 break;
588 case R_SS:
589 if (bits == 64) {
590 error(ERR_WARNING | ERR_PASS2,
591 "ss segment base generated, but will be ignored in 64-bit mode");
592 }
593 c = 0x36;
594 break;
595 case R_SEGR6:
596 case R_SEGR7:
597 error(ERR_NONFATAL,
598 "segr6 and segr7 cannot be used as prefixes");
599 break;
600 case P_A16:
601 if (bits == 64) {
602 error(ERR_NONFATAL,
603 "16-bit addressing is not supported "
604 "in 64-bit mode");
605 } else if (bits != 16)
606 c = 0x67;
607 break;
608 case P_A32:
609 if (bits != 32)
610 c = 0x67;
611 break;
612 case P_A64:
613 if (bits != 64) {
614 error(ERR_NONFATAL,
615 "64-bit addressing is only supported "
616 "in 64-bit mode");
617 }
618 break;
619 case P_ASP:
620 c = 0x67;
621 break;
622 case P_O16:
623 if (bits != 16)
624 c = 0x66;
625 break;
626 case P_O32:
627 if (bits == 16)
628 c = 0x66;
629 break;
630 case P_O64:
631 /* REX.W */
632 break;
633 case P_OSP:
634 c = 0x66;
635 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700636 case P_EVEX:
637 /* EVEX */
638 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400639 case P_none:
640 break;
641 default:
642 error(ERR_PANIC, "invalid instruction prefix");
643 }
644 if (c != 0) {
645 out(offset, segment, &c, OUT_RAWDATA, 1,
646 NO_SEG, NO_SEG);
647 offset++;
648 }
649 }
650 insn_end = offset + insn_size;
651 gencode(segment, offset, bits, instruction,
652 temp, insn_end);
653 offset += insn_size;
654 if (itimes > 0 && itimes == instruction->times - 1) {
655 /*
656 * Dummy call to list->output to give the offset to the
657 * listing module.
658 */
659 list->output(offset, NULL, OUT_RAWDATA, 0);
660 list->uplevel(LIST_TIMES);
661 }
662 }
663 if (instruction->times > 1)
664 list->downlevel(LIST_TIMES);
665 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700666 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400667 /* No match */
668 switch (m) {
669 case MERR_OPSIZEMISSING:
670 error(ERR_NONFATAL, "operation size not specified");
671 break;
672 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400674 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700675 case MERR_BRNUMMISMATCH:
676 error(ERR_NONFATAL,
677 "mismatch in the number of broadcasting elements");
678 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400679 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400681 break;
682 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800683 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400684 bits);
685 break;
686 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 error(ERR_NONFATAL,
688 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400689 break;
690 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000691 }
692 return 0;
693}
694
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700695int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400696 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000697{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000698 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700699 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000700
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000702 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000703
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400704 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000706
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700707 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
708 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400709 instruction->opcode == I_DT || instruction->opcode == I_DO ||
710 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300712 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300715 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000716
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400717 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000718 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000719
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400721 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400723 warn_overflow_const(e->offset, wsize);
724 } else if (e->type == EOT_DB_STRING ||
725 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 align = (-osize) % wsize;
729 if (align < 0)
730 align += wsize;
731 isize += osize + align;
732 }
733 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000734 }
735
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400737 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300739 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700740 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000741
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400742 fp = fopen(fname, "rb");
743 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
745 fname);
746 else if (fseek(fp, 0L, SEEK_END) < 0)
747 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
748 fname);
749 else {
750 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 if (instruction->eops->next) {
752 len -= instruction->eops->next->offset;
753 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700754 len > (size_t)instruction->eops->next->next->offset) {
755 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 }
757 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300758 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300760 if (fp)
761 fclose(fp);
762 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000763 }
764
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700765 /* Check to see if we need an address-size prefix */
766 add_asp(instruction, bits);
767
H. Peter Anvin23595f52009-07-25 17:44:25 -0700768 m = find_match(&temp, instruction, segment, offset, bits);
769 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400770 /* we've matched an instruction. */
771 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400772 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100773
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800774 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400775 if (isize < 0)
776 return -1;
777 for (j = 0; j < MAXPREFIX; j++) {
778 switch (instruction->prefixes[j]) {
779 case P_A16:
780 if (bits != 16)
781 isize++;
782 break;
783 case P_A32:
784 if (bits != 32)
785 isize++;
786 break;
787 case P_O16:
788 if (bits != 16)
789 isize++;
790 break;
791 case P_O32:
792 if (bits == 16)
793 isize++;
794 break;
795 case P_A64:
796 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700797 case P_EVEX:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400798 case P_none:
799 break;
800 default:
801 isize++;
802 break;
803 }
804 }
805 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700806 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400807 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809}
810
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800811static void bad_hle_warn(const insn * ins, uint8_t hleok)
812{
813 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800814 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800815 static const enum whatwarn warn[2][4] =
816 {
817 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
818 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
819 };
820 unsigned int n;
821
822 n = (unsigned int)rep_pfx - P_XACQUIRE;
823 if (n > 1)
824 return; /* Not XACQUIRE/XRELEASE */
825
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800826 ww = warn[n][hleok];
827 if (!is_class(MEMORY, ins->oprs[0].type))
828 ww = w_inval; /* HLE requires operand 0 to be memory */
829
830 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800831 case w_none:
832 break;
833
834 case w_lock:
835 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800836 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800837 "%s with this instruction requires lock",
838 prefix_name(rep_pfx));
839 }
840 break;
841
842 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800843 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800844 "%s invalid with this instruction",
845 prefix_name(rep_pfx));
846 break;
847 }
848}
849
H. Peter Anvin507ae032008-10-09 15:37:10 -0700850/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400851#define case3(x) case (x): case (x)+1: case (x)+2
852#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700853
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800854static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800855 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000856{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800857 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800858 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000859 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000860 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700861 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700862 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700863 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700864 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800865 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800866 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700867 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700869 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700870 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700871 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700872
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700873 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400874 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700875
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 (void)segment; /* Don't warn that this parameter is unused */
877 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000878
H. Peter Anvin839eca22007-10-29 23:12:47 -0700879 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400880 c = *codes++;
881 op1 = (c & 3) + ((opex & 1) << 2);
882 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
883 opx = &ins->oprs[op1];
884 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700885
H. Peter Anvin839eca22007-10-29 23:12:47 -0700886 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400887 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 codes += c, length += c;
889 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700890
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400891 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400892 opex = c;
893 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400895 case4(010):
896 ins->rex |=
897 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 codes++, length++;
899 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700900
Jin Kyu Song164d6072013-10-15 19:10:13 -0700901 case4(014):
902 /* this is an index reg of MIB operand */
903 mib_index = opx->basereg;
904 break;
905
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400906 case4(020):
907 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 length++;
909 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400911 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 length += 2;
913 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400915 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700916 if (opx->type & (BITS16 | BITS32 | BITS64))
917 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 else
919 length += (bits == 16) ? 2 : 4;
920 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400922 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 length += 4;
924 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700925
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400926 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700927 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400930 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 length++;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000935 length += 8; /* MOV reg64/imm */
936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 length += 2;
940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700943 if (opx->type & (BITS16 | BITS32 | BITS64))
944 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 else
946 length += (bits == 16) ? 2 : 4;
947 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400949 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 length += 4;
951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700954 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case 0172:
958 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400959 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700960 length++;
961 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700962
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700963 case4(0174):
964 length++;
965 break;
966
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700967 case4(0240):
968 ins->rex |= REX_EV;
969 ins->vexreg = regval(opx);
970 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
971 ins->vex_cm = *codes++;
972 ins->vex_wlp = *codes++;
973 ins->evex_tuple = (*codes++ - 0300);
974 break;
975
976 case 0250:
977 ins->rex |= REX_EV;
978 ins->vexreg = 0;
979 ins->vex_cm = *codes++;
980 ins->vex_wlp = *codes++;
981 ins->evex_tuple = (*codes++ - 0300);
982 break;
983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(0254):
985 length += 4;
986 break;
987
988 case4(0260):
989 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700990 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400991 ins->vex_cm = *codes++;
992 ins->vex_wlp = *codes++;
993 break;
994
995 case 0270:
996 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700997 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 ins->vex_cm = *codes++;
999 ins->vex_wlp = *codes++;
1000 break;
1001
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001002 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001003 hleok = c & 3;
1004 break;
1005
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 case4(0274):
1007 length++;
1008 break;
1009
1010 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 if (bits == 64)
1015 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001016 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001018
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001020 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
Keith Kaniosb7a89542007-04-12 02:40:54 +00001026 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1028 has_prefix(ins, PPS_ASIZE, P_A32))
1029 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001031
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001032 case4(0314):
1033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001036 {
1037 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1038 if (pfx == P_O16)
1039 break;
1040 if (pfx != P_none)
1041 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1042 else
1043 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001045 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001048 {
1049 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1050 if (pfx == P_O32)
1051 break;
1052 if (pfx != P_none)
1053 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1054 else
1055 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001057 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 0322:
1060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
Keith Kaniosb7a89542007-04-12 02:40:54 +00001062 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001063 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001064 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
Keith Kaniosb7a89542007-04-12 02:40:54 +00001066 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001067 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001068 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001069
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001070 case 0325:
1071 ins->rex |= REX_NH;
1072 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001073
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001074 case 0326:
1075 break;
1076
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 case 0330:
1078 codes++, length++;
1079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001083
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001084 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 case 0333:
1086 length++;
1087 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001089 case 0334:
1090 ins->rex |= REX_L;
1091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001093 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001096 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001097 if (!ins->prefixes[PPS_REP])
1098 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001101 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001102 if (!ins->prefixes[PPS_REP])
1103 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001104 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 if (ins->oprs[0].segment != NO_SEG)
1108 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1109 " quantity of BSS space");
1110 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001111 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 case 0341:
1115 if (!ins->prefixes[PPS_WAIT])
1116 ins->prefixes[PPS_WAIT] = P_WAIT;
1117 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001118
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 case 0360:
1120 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001122 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 length++;
1124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case 0364:
1127 case 0365:
1128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
Keith Kanios48af1772007-08-17 07:37:52 +00001130 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001131 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 length++;
1133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001135 case 0370:
1136 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 case 0373:
1140 length++;
1141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001143 case 0374:
1144 eat = EA_XMMVSIB;
1145 break;
1146
1147 case 0375:
1148 eat = EA_YMMVSIB;
1149 break;
1150
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001151 case 0376:
1152 eat = EA_ZMMVSIB;
1153 break;
1154
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 case4(0100):
1156 case4(0110):
1157 case4(0120):
1158 case4(0130):
1159 case4(0200):
1160 case4(0204):
1161 case4(0210):
1162 case4(0214):
1163 case4(0220):
1164 case4(0224):
1165 case4(0230):
1166 case4(0234):
1167 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001169 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 opflags_t rflags;
1171 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001172 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001173
Keith Kaniosb7a89542007-04-12 02:40:54 +00001174 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001175
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 if (c <= 0177) {
1177 /* pick rfield from operand b (opx) */
1178 rflags = regflag(opx);
1179 rfield = nasm_regvals[opx->basereg];
1180 } else {
1181 rflags = 0;
1182 rfield = c & 7;
1183 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001184
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001185 /* EVEX.b1 : evex_brerop contains the operand position */
1186 op_er_sae = (ins->evex_brerop >= 0 ?
1187 &ins->oprs[ins->evex_brerop] : NULL);
1188
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001189 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1190 /* set EVEX.b */
1191 ins->evex_p[2] |= EVEX_P2B;
1192 if (op_er_sae->decoflags & ER) {
1193 /* set EVEX.RC (rounding control) */
1194 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1195 & EVEX_P2RC;
1196 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001197 } else {
1198 /* set EVEX.L'L (vector length) */
1199 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001200 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001201 /* set EVEX.b */
1202 ins->evex_p[2] |= EVEX_P2B;
1203 }
1204 }
1205
Jin Kyu Song164d6072013-10-15 19:10:13 -07001206 /*
1207 * if a separate form of MIB (ICC style) is used,
1208 * the index reg info is merged into mem operand
1209 */
1210 if (mib_index != R_none) {
1211 opy->indexreg = mib_index;
1212 opy->scale = 1;
1213 opy->hintbase = mib_index;
1214 opy->hinttype = EAH_NOTBASE;
1215 }
1216
Jin Kyu Song3b653232013-11-08 11:41:12 -08001217 /*
1218 * only for mib operands, make a single reg index [reg*1].
1219 * gas uses this form to explicitly denote index register.
1220 */
1221 if ((temp->flags & IF_MIB) &&
1222 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1223 opy->hinttype == EAH_NOTBASE)) {
1224 opy->indexreg = opy->basereg;
1225 opy->basereg = -1;
1226 opy->scale = 1;
1227 }
1228
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001229 if (process_ea(opy, &ea_data, bits,
1230 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 errfunc(ERR_NONFATAL, "invalid effective address");
1232 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001233 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001236 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001237 }
1238 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001239
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 default:
1241 errfunc(ERR_PANIC, "internal instruction table corrupt"
1242 ": instruction code \\%o (0x%02X) given", c, c);
1243 break;
1244 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001245 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001246
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001247 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001248
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001249 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 if (ins->rex & REX_H) {
1251 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1252 return -1;
1253 }
1254 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001255 }
1256
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001257 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001258 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001259
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001261 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 return -1;
1263 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001264 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001266 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001267 ins->rex &= ~REX_W;
1268 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001269 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 ins->rex |= REX_W;
1271 bad32 &= ~REX_W;
1272 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001273 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001274 /* Follow REX_W */
1275 break;
1276 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001277
H. Peter Anvinfc561202011-07-07 16:58:22 -07001278 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001279 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1280 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001281 } else if (!(ins->rex & REX_EV) &&
1282 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1283 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1284 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001285 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001286 if (ins->rex & REX_EV)
1287 length += 4;
1288 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 length += 3;
1290 else
1291 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001292 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001293 if (ins->rex & REX_H) {
1294 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1295 return -1;
1296 } else if (bits == 64) {
1297 length++;
1298 } else if ((ins->rex & REX_L) &&
1299 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1300 cpu >= IF_X86_64) {
1301 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001302 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001303 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 length++;
1305 } else {
1306 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1307 return -1;
1308 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001309 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001310
1311 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1312 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001313 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001314 "instruction is not lockable");
1315 }
1316
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001317 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001318
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001319 return length;
1320}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001321
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001322static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1323{
1324 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001325 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001326 ins->rex = (ins->rex & REX_REAL) | REX_P;
1327 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1328 ins->rex = 0;
1329 return 1;
1330 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001331 }
1332
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001333 return 0;
1334}
1335
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001336static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001337 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001339{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001340 uint8_t c;
1341 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001342 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001343 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001344 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001345 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001346 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001347 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001348 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001349
H. Peter Anvin839eca22007-10-29 23:12:47 -07001350 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 c = *codes++;
1352 op1 = (c & 3) + ((opex & 1) << 2);
1353 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1354 opx = &ins->oprs[op1];
1355 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001356
H. Peter Anvin839eca22007-10-29 23:12:47 -07001357 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 case 01:
1359 case 02:
1360 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001362 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001363 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 codes += c;
1365 offset += c;
1366 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001367
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 case 05:
1369 case 06:
1370 case 07:
1371 opex = c;
1372 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001373
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001375 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001376 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001377 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 offset += 1;
1379 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001380
Jin Kyu Song164d6072013-10-15 19:10:13 -07001381 case4(014):
1382 break;
1383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001385 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001386 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001389 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 offset += 1;
1391 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001393 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001394 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001395 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001396 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001397 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 offset += 1;
1399 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001401 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001402 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001403 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001404 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001405 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 offset += 2;
1407 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001409 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001410 if (opx->type & (BITS16 | BITS32))
1411 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 else
1413 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001414 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001415 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001416 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001417 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 offset += size;
1419 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001421 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001422 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001423 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001424 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 offset += 4;
1427 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001428
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001430 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001431 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001432 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001433 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001434 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 offset += size;
1436 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001437
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001438 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001439 if (opx->segment != segment) {
1440 data = opx->offset;
1441 out(offset, segment, &data,
1442 OUT_REL1ADR, insn_end - offset,
1443 opx->segment, opx->wrt);
1444 } else {
1445 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 if (data > 127 || data < -128)
1447 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001448 out(offset, segment, &data,
1449 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1450 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 offset += 1;
1452 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001455 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001456 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001458 offset += 8;
1459 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001460
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001461 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001462 if (opx->segment != segment) {
1463 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001465 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001466 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001468 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001470 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 }
1472 offset += 2;
1473 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001474
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001475 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001476 if (opx->type & (BITS16 | BITS32 | BITS64))
1477 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 else
1479 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001480 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001481 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001482 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001483 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1484 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001486 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001488 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 }
1490 offset += size;
1491 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001492
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001493 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001494 if (opx->segment != segment) {
1495 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001497 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001498 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001502 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 }
1504 offset += 4;
1505 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001506
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001507 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001508 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001509 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1510 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001511 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001512 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 outfmt->segbase(1 + opx->segment),
1514 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001515 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001517
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001518 case 0172:
1519 c = *codes++;
1520 opx = &ins->oprs[c >> 3];
1521 bytes[0] = nasm_regvals[opx->basereg] << 4;
1522 opx = &ins->oprs[c & 7];
1523 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1524 errfunc(ERR_NONFATAL,
1525 "non-absolute expression not permitted as argument %d",
1526 c & 7);
1527 } else {
1528 if (opx->offset & ~15) {
1529 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1530 "four-bit argument exceeds bounds");
1531 }
1532 bytes[0] |= opx->offset & 15;
1533 }
1534 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1535 offset++;
1536 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001537
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 case 0173:
1539 c = *codes++;
1540 opx = &ins->oprs[c >> 4];
1541 bytes[0] = nasm_regvals[opx->basereg] << 4;
1542 bytes[0] |= c & 15;
1543 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1544 offset++;
1545 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001546
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001547 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 bytes[0] = nasm_regvals[opx->basereg] << 4;
1549 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1550 offset++;
1551 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001552
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001554 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1556 (int32_t)data != (int64_t)data) {
1557 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1558 "signed dword immediate exceeds bounds");
1559 }
1560 out(offset, segment, &data, OUT_ADDRESS, 4,
1561 opx->segment, opx->wrt);
1562 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001563 break;
1564
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001565 case4(0240):
1566 case 0250:
1567 codes += 3;
1568 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1569 EVEX_P2Z | EVEX_P2AAA, 2);
1570 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1571 bytes[0] = 0x62;
1572 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001573 bytes[1] = ((((ins->rex & 7) << 5) |
1574 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1575 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001576 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1577 ((~ins->vexreg & 15) << 3) |
1578 (1 << 2) | (ins->vex_wlp & 3);
1579 bytes[3] = ins->evex_p[2];
1580 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1581 offset += 4;
1582 break;
1583
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001584 case4(0260):
1585 case 0270:
1586 codes += 2;
1587 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1588 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1589 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1590 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001591 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1593 offset += 3;
1594 } else {
1595 bytes[0] = 0xc5;
1596 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001597 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1599 offset += 2;
1600 }
1601 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001602
H. Peter Anvine014f352012-02-25 22:35:19 -08001603 case 0271:
1604 case 0272:
1605 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001606 break;
1607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case4(0274):
1609 {
1610 uint64_t uv, um;
1611 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 if (ins->rex & REX_W)
1614 s = 64;
1615 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1616 s = 16;
1617 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1618 s = 32;
1619 else
1620 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001621
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001622 um = (uint64_t)2 << (s-1);
1623 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001624
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001625 if (uv > 127 && uv < (uint64_t)-128 &&
1626 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001627 /* If this wasn't explicitly byte-sized, warn as though we
1628 * had fallen through to the imm16/32/64 case.
1629 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001630 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001631 "%s value exceeds bounds",
1632 (opx->type & BITS8) ? "signed byte" :
1633 s == 16 ? "word" :
1634 s == 32 ? "dword" :
1635 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001637 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001638 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001639 out(offset, segment, &data, OUT_ADDRESS, 1,
1640 opx->segment, opx->wrt);
1641 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001642 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001643 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1644 NO_SEG);
1645 }
1646 offset += 1;
1647 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001652
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001654 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001656 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 offset += 1;
1658 } else
1659 offset += 0;
1660 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001663 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001665 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 offset += 1;
1667 } else
1668 offset += 0;
1669 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 case 0312:
1672 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001673
Keith Kaniosb7a89542007-04-12 02:40:54 +00001674 case 0313:
1675 ins->rex = 0;
1676 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001677
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 case4(0314):
1679 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001686 case 0323:
1687 break;
1688
Keith Kaniosb7a89542007-04-12 02:40:54 +00001689 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001690 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001692
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 case 0325:
1694 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001695
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001696 case 0326:
1697 break;
1698
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001700 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001701 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 offset += 1;
1703 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001704
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001707
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001708 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001710 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001711 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 offset += 1;
1713 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001714
Keith Kanios48af1772007-08-17 07:37:52 +00001715 case 0334:
1716 if (ins->rex & REX_R) {
1717 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001718 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001719 offset += 1;
1720 }
1721 ins->rex &= ~(REX_L|REX_R);
1722 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001723
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001724 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001725 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001726
H. Peter Anvin962e3052008-08-28 17:47:16 -07001727 case 0336:
1728 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001730
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 if (ins->oprs[0].segment != NO_SEG)
1733 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1734 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001735 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 if (size > 0)
1737 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001738 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 offset += size;
1740 }
1741 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001742
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001743 case 0341:
1744 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case 0360:
1747 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001748
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 case 0361:
1750 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001751 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1752 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001754
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 case 0364:
1756 case 0365:
1757 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001758
Keith Kanios48af1772007-08-17 07:37:52 +00001759 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001761 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001762 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001763 offset += 1;
1764 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001765
Jin Kyu Song03041092013-10-15 19:38:51 -07001766 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001768
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 case 0373:
1770 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001771 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 offset += 1;
1773 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001774
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001775 case 0374:
1776 eat = EA_XMMVSIB;
1777 break;
1778
1779 case 0375:
1780 eat = EA_YMMVSIB;
1781 break;
1782
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001783 case 0376:
1784 eat = EA_ZMMVSIB;
1785 break;
1786
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001787 case4(0100):
1788 case4(0110):
1789 case4(0120):
1790 case4(0130):
1791 case4(0200):
1792 case4(0204):
1793 case4(0210):
1794 case4(0214):
1795 case4(0220):
1796 case4(0224):
1797 case4(0230):
1798 case4(0234):
1799 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 ea ea_data;
1801 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001803 uint8_t *p;
1804 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001806
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001807 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001808 /* pick rfield from operand b (opx) */
1809 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001810 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 } else {
1812 /* rfield is constant */
1813 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001815 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001817 if (process_ea(opy, &ea_data, bits,
1818 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 p = bytes;
1822 *p++ = ea_data.modrm;
1823 if (ea_data.sib_present)
1824 *p++ = ea_data.sib;
1825
1826 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001827 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001829 /*
1830 * Make sure the address gets the right offset in case
1831 * the line breaks in the .lst file (BR 1197827)
1832 */
1833 offset += s;
1834 s = 0;
1835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 switch (ea_data.bytes) {
1837 case 0:
1838 break;
1839 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case 2:
1841 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001842 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001843 /* use compressed displacement, if available */
1844 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 if (ea_data.rip) {
1847 if (opy->segment == segment) {
1848 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001849 if (overflow_signed(data, ea_data.bytes))
1850 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 out(offset, segment, &data, OUT_ADDRESS,
1852 ea_data.bytes, NO_SEG, NO_SEG);
1853 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001854 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 out(offset, segment, &data, OUT_REL4ADR,
1856 insn_end - offset, opy->segment, opy->wrt);
1857 }
1858 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001859 if (overflow_general(data, ins->addr_size >> 3) ||
1860 signed_bits(data, ins->addr_size) !=
1861 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001862 warn_overflow(ERR_PASS2, ea_data.bytes);
1863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 out(offset, segment, &data, OUT_ADDRESS,
1865 ea_data.bytes, opy->segment, opy->wrt);
1866 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001868 default:
1869 /* Impossible! */
1870 errfunc(ERR_PANIC,
1871 "Invalid amount of bytes (%d) for offset?!",
1872 ea_data.bytes);
1873 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 }
1875 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 }
1877 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 default:
1880 errfunc(ERR_PANIC, "internal instruction table corrupt"
1881 ": instruction code \\%o (0x%02X) given", c, c);
1882 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001884 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001885}
1886
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001887static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001888{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001889 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001890 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001891 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001892}
1893
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001894static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001895{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001896 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001898 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001899}
1900
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001901static int op_rexflags(const operand * o, int mask)
1902{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001903 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001904 int val;
1905
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001906 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001907 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001908
H. Peter Anvina4835d42008-05-20 14:21:29 -07001909 flags = nasm_reg_flags[o->basereg];
1910 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001911
1912 return rexflags(val, flags, mask);
1913}
1914
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001915static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001916{
1917 int rex = 0;
1918
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001919 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001921 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 rex |= REX_W;
1923 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1924 rex |= REX_H;
1925 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1926 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001927
1928 return rex & mask;
1929}
1930
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001931static int evexflags(int val, decoflags_t deco,
1932 int mask, uint8_t byte)
1933{
1934 int evex = 0;
1935
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001936 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001937 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001938 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001939 evex |= (EVEX_P0RP | EVEX_P0X);
1940 break;
1941 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001942 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001943 evex |= EVEX_P2VP;
1944 if (deco & Z)
1945 evex |= EVEX_P2Z;
1946 if (deco & OPMASK_MASK)
1947 evex |= deco & EVEX_P2AAA;
1948 break;
1949 }
1950 return evex & mask;
1951}
1952
1953static int op_evexflags(const operand * o, int mask, uint8_t byte)
1954{
1955 int val;
1956
1957 if (!is_register(o->basereg))
1958 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1959
1960 val = nasm_regvals[o->basereg];
1961
1962 return evexflags(val, o->decoflags, mask, byte);
1963}
1964
H. Peter Anvin23595f52009-07-25 17:44:25 -07001965static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 insn *instruction,
1967 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001968{
1969 const struct itemplate *temp;
1970 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001971 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001972 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001973 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001974 int i;
1975
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001976 /* broadcasting uses a different data element size */
1977 for (i = 0; i < instruction->operands; i++)
1978 if (i == broadcast)
1979 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1980 else
1981 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001982
1983 merr = MERR_INVALOP;
1984
1985 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001986 temp->opcode != I_none; temp++) {
1987 m = matches(temp, instruction, bits);
1988 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001989 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001990 m = MOK_GOOD;
1991 else
1992 m = MERR_INVALOP;
1993 } else if (m == MERR_OPSIZEMISSING &&
1994 (temp->flags & IF_SMASK) != IF_SX) {
1995 /*
1996 * Missing operand size and a candidate for fuzzy matching...
1997 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001998 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001999 if (i == broadcast)
2000 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2001 else
2002 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002003 opsizemissing = true;
2004 }
2005 if (m > merr)
2006 merr = m;
2007 if (merr == MOK_GOOD)
2008 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002009 }
2010
2011 /* No match, but see if we can get a fuzzy operand size match... */
2012 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002014
2015 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002016 /*
2017 * We ignore extrinsic operand sizes on registers, so we should
2018 * never try to fuzzy-match on them. This also resolves the case
2019 * when we have e.g. "xmmrm128" in two different positions.
2020 */
2021 if (is_class(REGISTER, instruction->oprs[i].type))
2022 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002023
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002024 /* This tests if xsizeflags[i] has more than one bit set */
2025 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2026 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002027
Jin Kyu Song7903c072013-10-30 03:00:12 -07002028 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002029 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002030 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2031 BITS32 : BITS64);
2032 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002033 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002034 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002035 }
2036
2037 /* Try matching again... */
2038 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002039 temp->opcode != I_none; temp++) {
2040 m = matches(temp, instruction, bits);
2041 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002042 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002043 m = MOK_GOOD;
2044 else
2045 m = MERR_INVALOP;
2046 }
2047 if (m > merr)
2048 merr = m;
2049 if (merr == MOK_GOOD)
2050 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002051 }
2052
H. Peter Anvina81655b2009-07-25 18:15:28 -07002053done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002054 *tempp = temp;
2055 return merr;
2056}
2057
H. Peter Anvin65289e82009-07-25 17:25:11 -07002058static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002059 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002060{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002061 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002062 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002063 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002064
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002065 /*
2066 * Check the opcode
2067 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002069 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002070
2071 /*
2072 * Count the operands
2073 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002075 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002076
2077 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002078 * Is it legal?
2079 */
2080 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2081 return MERR_INVALOP;
2082
2083 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002084 * Check that no spurious colons or TOs are present
2085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 for (i = 0; i < itemp->operands; i++)
2087 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002088 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002089
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002090 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002091 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002092 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002093 switch (itemp->flags & IF_SMASK) {
2094 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002095 asize = BITS8;
2096 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002097 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002098 asize = BITS16;
2099 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002100 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002101 asize = BITS32;
2102 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002103 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002104 asize = BITS64;
2105 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002106 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002107 asize = BITS128;
2108 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002109 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002110 asize = BITS256;
2111 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002112 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002113 asize = BITS512;
2114 break;
2115 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002116 switch (bits) {
2117 case 16:
2118 asize = BITS16;
2119 break;
2120 case 32:
2121 asize = BITS32;
2122 break;
2123 case 64:
2124 asize = BITS64;
2125 break;
2126 default:
2127 asize = 0;
2128 break;
2129 }
2130 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002131 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002132 asize = 0;
2133 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002134 }
2135
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002136 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002137 /* S- flags only apply to a specific operand */
2138 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2139 memset(size, 0, sizeof size);
2140 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002141 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002142 /* S- flags apply to all operands */
2143 for (i = 0; i < MAX_OPERANDS; i++)
2144 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002145 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002146
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002147 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002148 * Check that the operand flags all match up,
2149 * it's a bit tricky so lets be verbose:
2150 *
2151 * 1) Find out the size of operand. If instruction
2152 * doesn't have one specified -- we're trying to
2153 * guess it either from template (IF_S* flag) or
2154 * from code bits.
2155 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002156 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002157 * template has an operand size specified AND this size differ
2158 * from which instruction has (perhaps we got it from code bits)
2159 * we are:
2160 * a) Check that only size of instruction and operand is differ
2161 * other characteristics do match
2162 * b) Perhaps it's a register specified in instruction so
2163 * for such a case we just mark that operand as "size
2164 * missing" and this will turn on fuzzy operand size
2165 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002166 */
2167 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002168 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002169 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002170 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002171 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002172 opflags_t template_opsize, insn_opsize;
2173
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002174 if (!(type & SIZE_MASK))
2175 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002176
Jin Kyu Song7903c072013-10-30 03:00:12 -07002177 insn_opsize = type & SIZE_MASK;
2178 if (!is_broadcast) {
2179 template_opsize = itemp->opd[i] & SIZE_MASK;
2180 } else {
2181 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2182 /*
2183 * when broadcasting, the element size depends on
2184 * the instruction type. decorator flag should match.
2185 */
2186
2187 if (deco_brsize) {
2188 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002189 /* calculate the proper number : {1to<brcast_num>} */
2190 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2191 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002192 } else {
2193 template_opsize = 0;
2194 }
2195 }
2196
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002197 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002198 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002199 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002200 } else if (template_opsize) {
2201 if (template_opsize != insn_opsize) {
2202 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002203 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002204 } else if (!is_class(REGISTER, type)) {
2205 /*
2206 * Note: we don't honor extrinsic operand sizes for registers,
2207 * so "missing operand size" for a register should be
2208 * considered a wildcard match rather than an error.
2209 */
2210 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002211 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002212 } else if (is_broadcast &&
2213 (brcast_num !=
2214 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2215 /*
2216 * broadcasting opsize matches but the number of repeated memory
2217 * element does not match.
2218 * if 64b double precision float is broadcasted to zmm (512b),
2219 * broadcasting decorator must be {1to8}.
2220 */
2221 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 }
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002223 } else if (is_register(instruction->oprs[i].basereg) &&
2224 nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
Jin Kyu Songdd1c0c12013-09-13 14:12:56 -07002225 !(itemp->flags & IF_AVX512)) {
Jin Kyu Song66c61922013-08-26 20:28:43 -07002226 return MERR_ENCMISMATCH;
Jin Kyu Song945b1b82013-10-25 19:29:53 -07002227 } else if (instruction->prefixes[PPS_EVEX] &&
2228 !(itemp->flags & IF_AVX512)) {
2229 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002230 }
2231 }
2232
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002233 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002235
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002236 /*
2237 * Check operand sizes
2238 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002239 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002242 asize = itemp->opd[i] & SIZE_MASK;
2243 if (asize) {
2244 for (i = 0; i < oprs; i++)
2245 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 break;
2247 }
2248 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002249 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002251 }
2252
Keith Kaniosb7a89542007-04-12 02:40:54 +00002253 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 if (!(itemp->opd[i] & SIZE_MASK) &&
2255 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002256 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002257 }
2258
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002259 /*
2260 * Check template is okay at the set cpu level
2261 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002262 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002263 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002264
Keith Kaniosb7a89542007-04-12 02:40:54 +00002265 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002266 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002267 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002268 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002269 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002271 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002272 * If we have a HLE prefix, look for the NOHLE flag
2273 */
2274 if ((itemp->flags & IF_NOHLE) &&
2275 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2276 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2277 return MERR_BADHLE;
2278
2279 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002280 * Check if special handling needed for Jumps
2281 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002282 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002283 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284
Jin Kyu Song03041092013-10-15 19:38:51 -07002285 /*
2286 * Check if BND prefix is allowed
2287 */
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08002288 if ((IF_BND & ~itemp->flags) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002289 has_prefix(instruction, PPS_REP, P_BND))
2290 return MERR_BADBND;
2291
H. Peter Anvin60926242009-07-26 16:25:38 -07002292 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002293}
2294
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002295/*
2296 * Check if offset is a multiple of N with corresponding tuple type
2297 * if Disp8*N is available, compressed displacement is stored in compdisp
2298 */
2299static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2300{
2301 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2302 {{16, 32, 64}, {8, 8, 8}}};
2303 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2304 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2305
2306 bool evex_b = input->decoflags & BRDCAST_MASK;
2307 enum ttypes tuple = ins->evex_tuple;
2308 /* vex_wlp composed as [wwllpp] */
2309 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2310 /* wig(=2) is treated as w0(=0) */
2311 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2312 int32_t off = input->offset;
2313 uint8_t n = 0;
2314 int32_t disp8;
2315
2316 switch(tuple) {
2317 case FV:
2318 n = fv_n[evex_w][evex_b][vectlen];
2319 break;
2320 case HV:
2321 n = hv_n[evex_b][vectlen];
2322 break;
2323
2324 case FVM:
2325 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2326 n = 1 << (vectlen + 4);
2327 break;
2328 case T1S8: /* N = 1 */
2329 case T1S16: /* N = 2 */
2330 n = tuple - T1S8 + 1;
2331 break;
2332 case T1S:
2333 /* N = 4 for 32bit, 8 for 64bit */
2334 n = evex_w ? 8 : 4;
2335 break;
2336 case T1F32:
2337 case T1F64:
2338 /* N = 4 for 32bit, 8 for 64bit */
2339 n = (tuple == T1F32 ? 4 : 8);
2340 break;
2341 case T2:
2342 case T4:
2343 case T8:
2344 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2345 n = 0;
2346 else
Jin Kyu Songd2d9c3e2013-08-26 20:28:41 -07002347 n = 1 << (tuple - T2 + evex_w + 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002348 break;
2349 case HVM:
2350 case QVM:
2351 case OVM:
2352 n = 1 << (OVM - tuple + vectlen + 1);
2353 break;
2354 case M128:
2355 n = 16;
2356 break;
2357 case DUP:
2358 n = dup_n[vectlen];
2359 break;
2360
2361 default:
2362 break;
2363 }
2364
2365 if (n && !(off & (n - 1))) {
2366 disp8 = off / n;
2367 /* if it fits in Disp8 */
2368 if (disp8 >= -128 && disp8 <= 127) {
2369 *compdisp = disp8;
2370 return true;
2371 }
2372 }
2373
2374 *compdisp = 0;
2375 return false;
2376}
2377
2378/*
2379 * Check if ModR/M.mod should/can be 01.
2380 * - EAF_BYTEOFFS is set
2381 * - offset can fit in a byte when EVEX is not used
2382 * - offset can be compressed when EVEX is used
2383 */
2384#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2385 (o >= -128 && o <= 127 && \
2386 seg == NO_SEG && !forw_ref && \
2387 !(input->eaflags & EAF_WORDOFFS) && \
2388 !(ins->rex & REX_EV)) || \
2389 (ins->rex & REX_EV && \
2390 is_disp8n(input, ins, &output->disp8)))
2391
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002392static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002393 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002394{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002395 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002396 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002397
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002398 output->type = EA_SCALAR;
2399 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002400 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002401
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002402 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002403 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404 /* EVEX.R' flag for the REG operand */
2405 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002406
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002407 if (is_class(REGISTER, input->type)) {
2408 /*
2409 * It's a direct register.
2410 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002411 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002412 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002413
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002414 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002415 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002416
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002417 /* broadcasting is not available with a direct register operand. */
2418 if (input->decoflags & BRDCAST_MASK) {
2419 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2420 goto err;
2421 }
2422
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002423 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002424 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002425 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 output->bytes = 0; /* no offset necessary either */
2427 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2428 } else {
2429 /*
2430 * It's a memory reference.
2431 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002432
2433 /* Embedded rounding or SAE is not available with a mem ref operand. */
2434 if (input->decoflags & (ER | SAE)) {
2435 nasm_error(ERR_NONFATAL,
2436 "Embedded rounding is available only with reg-reg op.");
2437 return -1;
2438 }
2439
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002440 if (input->basereg == -1 &&
2441 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002442 /*
2443 * It's a pure offset.
2444 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002445 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2446 input->segment == NO_SEG) {
2447 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2448 input->type &= ~IP_REL;
2449 input->type |= MEMORY;
2450 }
2451
2452 if (input->eaflags & EAF_BYTEOFFS ||
2453 (input->eaflags & EAF_WORDOFFS &&
2454 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2455 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2456 }
2457
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002458 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002459 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002460 output->sib = GEN_SIB(0, 4, 5);
2461 output->bytes = 4;
2462 output->modrm = GEN_MODRM(0, rfield, 4);
2463 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002464 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002465 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002466 output->bytes = (addrbits != 16 ? 4 : 2);
2467 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2468 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002469 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002470 } else {
2471 /*
2472 * It's an indirection.
2473 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002475 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002477 int t, it, bt; /* register numbers */
2478 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 if (s == 0)
2481 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002482
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002483 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002484 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 ix = nasm_reg_flags[i];
2486 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002487 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 ix = 0;
2489 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002491 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002492 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 bx = nasm_reg_flags[b];
2494 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002495 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002496 bx = 0;
2497 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002498
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002499 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002500 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002501 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002502 int32_t o = input->offset;
2503 int mod, scale, index, base;
2504
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002505 /*
2506 * For a vector SIB, one has to be a vector and the other,
2507 * if present, a GPR. The vector must be the index operand.
2508 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002509 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002510 if (s == 0)
2511 s = 1;
2512 else if (s != 1)
2513 goto err;
2514
2515 t = bt, bt = it, it = t;
2516 x = bx, bx = ix, ix = x;
2517 }
2518
2519 if (bt != -1) {
2520 if (REG_GPR & ~bx)
2521 goto err;
2522 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2523 sok &= bx;
2524 else
2525 goto err;
2526 }
2527
2528 /*
2529 * While we're here, ensure the user didn't specify
2530 * WORD or QWORD
2531 */
2532 if (input->disp_size == 16 || input->disp_size == 64)
2533 goto err;
2534
2535 if (addrbits == 16 ||
2536 (addrbits == 32 && !(sok & BITS32)) ||
2537 (addrbits == 64 && !(sok & BITS64)))
2538 goto err;
2539
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002540 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2541 : ((ix & YMMREG & ~REG_EA)
2542 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002543
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002544 output->rex |= rexflags(it, ix, REX_X);
2545 output->rex |= rexflags(bt, bx, REX_B);
2546 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002547
2548 index = it & 7; /* it is known to be != -1 */
2549
2550 switch (s) {
2551 case 1:
2552 scale = 0;
2553 break;
2554 case 2:
2555 scale = 1;
2556 break;
2557 case 4:
2558 scale = 2;
2559 break;
2560 case 8:
2561 scale = 3;
2562 break;
2563 default: /* then what the smeg is it? */
2564 goto err; /* panic */
2565 }
2566
2567 if (bt == -1) {
2568 base = 5;
2569 mod = 0;
2570 } else {
2571 base = (bt & 7);
2572 if (base != REG_NUM_EBP && o == 0 &&
2573 seg == NO_SEG && !forw_ref &&
2574 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2575 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002576 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577 mod = 1;
2578 else
2579 mod = 2;
2580 }
2581
2582 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002583 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2584 output->modrm = GEN_MODRM(mod, rfield, 4);
2585 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002586 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002587 /*
2588 * it must be a 32/64-bit memory reference. Firstly we have
2589 * to check that all registers involved are type E/Rxx.
2590 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002591 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002592 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002593
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002594 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002595 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2596 sok &= ix;
2597 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002598 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002599 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002601 if (bt != -1) {
2602 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002603 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002604 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002605 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002606 sok &= bx;
2607 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 /*
2610 * While we're here, ensure the user didn't specify
2611 * WORD or QWORD
2612 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002613 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002614 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002615
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002616 if (addrbits == 16 ||
2617 (addrbits == 32 && !(sok & BITS32)) ||
2618 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002619 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002620
Keith Kaniosb7a89542007-04-12 02:40:54 +00002621 /* now reorganize base/index */
2622 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002623 ((hb == b && ht == EAH_NOTBASE) ||
2624 (hb == i && ht == EAH_MAKEBASE))) {
2625 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002626 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002627 x = bx, bx = ix, ix = x;
2628 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002629 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002630 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002631 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002632 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002633 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002634 }
2635 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2636 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002637 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2639 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002640 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002642 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002644 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002645 x = ix, ix = bx, bx = x;
2646 }
2647 if (it == REG_NUM_ESP ||
2648 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002649 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002650
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 output->rex |= rexflags(it, ix, REX_X);
2652 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002653
Keith Kanios48af1772007-08-17 07:37:52 +00002654 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002657
Keith Kaniosb7a89542007-04-12 02:40:54 +00002658 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661 } else {
2662 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002663 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 seg == NO_SEG && !forw_ref &&
2665 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002666 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002667 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668 mod = 1;
2669 else
2670 mod = 2;
2671 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002672
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002673 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002674 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2675 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002676 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002677 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002679
Keith Kaniosb7a89542007-04-12 02:40:54 +00002680 if (it == -1)
2681 index = 4, s = 1;
2682 else
2683 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002684
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 switch (s) {
2686 case 1:
2687 scale = 0;
2688 break;
2689 case 2:
2690 scale = 1;
2691 break;
2692 case 4:
2693 scale = 2;
2694 break;
2695 case 8:
2696 scale = 3;
2697 break;
2698 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002699 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002701
Keith Kaniosb7a89542007-04-12 02:40:54 +00002702 if (bt == -1) {
2703 base = 5;
2704 mod = 0;
2705 } else {
2706 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002707 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002708 seg == NO_SEG && !forw_ref &&
2709 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002710 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002711 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002712 mod = 1;
2713 else
2714 mod = 2;
2715 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002716
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002717 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002718 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2719 output->modrm = GEN_MODRM(mod, rfield, 4);
2720 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 }
2722 } else { /* it's 16-bit */
2723 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002724 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002725
Keith Kaniosb7a89542007-04-12 02:40:54 +00002726 /* check for 64-bit long mode */
2727 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002728 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002729
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2732 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002733 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002734
Keith Kaniosb7a89542007-04-12 02:40:54 +00002735 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002736 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002737 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002740 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 if (b == -1 && i != -1) {
2742 int tmp = b;
2743 b = i;
2744 i = tmp;
2745 } /* swap */
2746 if ((b == R_SI || b == R_DI) && i != -1) {
2747 int tmp = b;
2748 b = i;
2749 i = tmp;
2750 }
2751 /* have BX/BP as base, SI/DI index */
2752 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002753 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 if (i != -1 && b != -1 &&
2755 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002756 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002757 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002758 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 rm = -1;
2761 if (i != -1)
2762 switch (i * 256 + b) {
2763 case R_SI * 256 + R_BX:
2764 rm = 0;
2765 break;
2766 case R_DI * 256 + R_BX:
2767 rm = 1;
2768 break;
2769 case R_SI * 256 + R_BP:
2770 rm = 2;
2771 break;
2772 case R_DI * 256 + R_BP:
2773 rm = 3;
2774 break;
2775 } else
2776 switch (b) {
2777 case R_SI:
2778 rm = 4;
2779 break;
2780 case R_DI:
2781 rm = 5;
2782 break;
2783 case R_BP:
2784 rm = 6;
2785 break;
2786 case R_BX:
2787 rm = 7;
2788 break;
2789 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002790 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002791 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002792
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002793 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2794 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002796 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 mod = 1;
2798 else
2799 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002800
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002801 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002802 output->bytes = mod; /* bytes of offset needed */
2803 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 }
2805 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002806 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002807
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 return output->type;
2810
2811err:
2812 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002813}
2814
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002815static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002816{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002817 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002818 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002819
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002820 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002821
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002822 switch (ins->prefixes[PPS_ASIZE]) {
2823 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002824 valid &= 16;
2825 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002826 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002827 valid &= 32;
2828 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002829 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002830 valid &= 64;
2831 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002832 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002833 valid &= (addrbits == 32) ? 16 : 32;
2834 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002835 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002836 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837 }
2838
2839 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002840 if (is_class(MEMORY, ins->oprs[j].type)) {
2841 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002842
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002844 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002845 i = 0;
2846 else
2847 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002848
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002850 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002851 b = 0;
2852 else
2853 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002854
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 if (ins->oprs[j].scale == 0)
2856 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002857
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002858 if (!i && !b) {
2859 int ds = ins->oprs[j].disp_size;
2860 if ((addrbits != 64 && ds > 8) ||
2861 (addrbits == 64 && ds == 16))
2862 valid &= ds;
2863 } else {
2864 if (!(REG16 & ~b))
2865 valid &= 16;
2866 if (!(REG32 & ~b))
2867 valid &= 32;
2868 if (!(REG64 & ~b))
2869 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002871 if (!(REG16 & ~i))
2872 valid &= 16;
2873 if (!(REG32 & ~i))
2874 valid &= 32;
2875 if (!(REG64 & ~i))
2876 valid &= 64;
2877 }
2878 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002879 }
2880
2881 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002882 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002883 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002884 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002885 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002887 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002888 /* Impossible... */
2889 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2890 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891 }
2892
2893 defdisp = ins->addr_size == 16 ? 16 : 32;
2894
2895 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2897 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2898 /*
2899 * mem_offs sizes must match the address size; if not,
2900 * strip the MEM_OFFS bit and match only EA instructions
2901 */
2902 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2903 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002904 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002905}