blob: a7ca1aa1d0d5c13898337581885d5f737e444154 [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"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800183#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
H. Peter Anvin65289e82009-07-25 17:25:11 -0700185enum match_result {
186 /*
187 * Matching errors. These should be sorted so that more specific
188 * errors come later in the sequence.
189 */
190 MERR_INVALOP,
191 MERR_OPSIZEMISSING,
192 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700193 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700194 MERR_BADCPU,
195 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800196 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700197 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700198 MERR_BADBND,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700199 /*
200 * Matching success; the conditional ones first
201 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400202 MOK_JUMP, /* Matching OK but needs jmp_match() */
203 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700204};
205
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700207 enum ea_type type; /* what kind of EA is this? */
208 int sib_present; /* is a SIB byte necessary? */
209 int bytes; /* # of bytes of offset needed */
210 int size; /* lazy - this is sib+bytes+1 */
211 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700212 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000213} ea;
214
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400215#define GEN_SIB(scale, index, base) \
216 (((scale) << 6) | ((index) << 3) | ((base)))
217
218#define GEN_MODRM(mod, reg, rm) \
219 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
220
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400221static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000222static efunc errfunc;
223static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000224static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800226static int64_t calcsize(int32_t, int64_t, int, insn *,
227 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700228static void gencode(int32_t segment, int64_t offset, int bits,
229 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400230 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700231static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400232 insn *instruction,
233 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700234static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700235static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000236static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700237static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000238static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700239static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700240static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000241
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700242static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700243
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400244static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000245{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700246 return ins->prefixes[pos] == prefix;
247}
248
249static void assert_no_prefix(insn * ins, enum prefix_pos pos)
250{
251 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400252 errfunc(ERR_NONFATAL, "invalid %s prefix",
253 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700254}
255
256static const char *size_name(int size)
257{
258 switch (size) {
259 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700271 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700273 case 64:
274 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000277 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700278}
279
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400280static void warn_overflow(int pass, int size)
281{
282 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
283 "%s data exceeds bounds", size_name(size));
284}
285
286static void warn_overflow_const(int64_t data, int size)
287{
288 if (overflow_general(data, size))
289 warn_overflow(ERR_PASS1, size);
290}
291
292static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700293{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100294 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295 if (overflow_general(o->offset, size))
296 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700297 }
298}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400299
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000300/*
301 * This routine wrappers the real output format's output routine,
302 * in order to pass a copy of the data off to the listing file
303 * generator at the same time.
304 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800305static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800306 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400307 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000308{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000309 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000310 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800311 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000312
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800313 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400314 /*
315 * This is a non-relocated address, and we're going to
316 * convert it into RAWDATA format.
317 */
318 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400320 if (size > 8) {
321 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
322 return;
323 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700324
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400325 WRITEADDR(q, *(int64_t *)data, size);
326 data = p;
327 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000328 }
329
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800330 list->output(offset, data, type, size);
331
Frank Kotlerabebb082003-09-06 04:45:37 +0000332 /*
333 * this call to src_get determines when we call the
334 * debug-format-specific "linenum" function
335 * it updates lineno and lnfname to the current values
336 * returning 0 if "same as last time", -2 if lnfname
337 * changed, and the amount by which lineno changed,
338 * if it did. thus, these variables must be static
339 */
340
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400341 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000342 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800344 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000345}
346
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400347static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
348{
349 if (opx->segment != NO_SEG) {
350 uint64_t data = opx->offset;
351 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
352 } else {
353 uint8_t byte = opx->offset;
354 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
355 }
356}
357
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700358static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800359 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800361 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800362 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000363 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800364 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000365
H. Peter Anvin755f5212012-02-25 11:41:34 -0800366 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700367 return false;
368 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400369 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700370 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400371 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700372
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800373 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100374
Victor van den Elzen154e5922009-02-25 17:32:00 +0100375 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100376 /* Be optimistic in pass 1 */
377 return true;
378
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700380 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000381
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700382 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800383 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
384
385 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
386 /* jmp short (opcode eb) cannot be used with bnd prefix. */
387 ins->prefixes[PPS_REP] = P_none;
388 }
389
390 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000392
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400393int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400394 insn * instruction, struct ofmt *output, efunc error,
395 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000396{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000397 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700399 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800400 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000401 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800402 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300403 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000404
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000406 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 outfmt = output; /* likewise */
408 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000409
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300410 wsize = idata_bytes(instruction->opcode);
411 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000413
H. Peter Anvineba20a72002-04-30 20:53:55 +0000414 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000416 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 if (t < 0)
418 errfunc(ERR_PANIC,
419 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000420
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400422 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400424 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700425 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400426 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000427 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700428 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800430 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400431 offset += wsize;
432 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700433 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400434 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000436
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800438 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000440
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 if (align) {
442 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100443 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800444 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 }
446 offset += e->stringlen + align;
447 }
448 }
449 if (t > 0 && t == instruction->times - 1) {
450 /*
451 * Dummy call to list->output to give the offset to the
452 * listing module.
453 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800454 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 list->uplevel(LIST_TIMES);
456 }
457 }
458 if (instruction->times > 1)
459 list->downlevel(LIST_TIMES);
460 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000461 }
462
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700464 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400467 fp = fopen(fname, "rb");
468 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
470 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400471 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
473 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200474 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400475 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700476 static char buf[4096];
477 size_t t = instruction->times;
478 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400479 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481 len = ftell(fp);
482 if (instruction->eops->next) {
483 base = instruction->eops->next->offset;
484 len -= base;
485 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700486 len > (size_t)instruction->eops->next->next->offset)
487 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 }
489 /*
490 * Dummy call to list->output to give the offset to the
491 * listing module.
492 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800493 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 list->uplevel(LIST_INCBIN);
495 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700496 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 fseek(fp, base, SEEK_SET);
499 l = len;
500 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700501 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400502 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 if (!m) {
504 /*
505 * This shouldn't happen unless the file
506 * actually changes while we are reading
507 * it.
508 */
509 error(ERR_NONFATAL,
510 "`incbin': unexpected EOF while"
511 " reading file `%s'", fname);
512 t = 0; /* Try to exit cleanly */
513 break;
514 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800515 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 NO_SEG, NO_SEG);
517 l -= m;
518 }
519 }
520 list->downlevel(LIST_INCBIN);
521 if (instruction->times > 1) {
522 /*
523 * Dummy call to list->output to give the offset to the
524 * listing module.
525 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800526 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 list->uplevel(LIST_TIMES);
528 list->downlevel(LIST_TIMES);
529 }
530 fclose(fp);
531 return instruction->times * len;
532 }
533 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000534 }
535
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700536 /* Check to see if we need an address-size prefix */
537 add_asp(instruction, bits);
538
H. Peter Anvin23595f52009-07-25 17:44:25 -0700539 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700540
H. Peter Anvin23595f52009-07-25 17:44:25 -0700541 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400542 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800543 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400544 itimes = instruction->times;
545 if (insn_size < 0) /* shouldn't be, on pass two */
546 error(ERR_PANIC, "errors made it through from pass one");
547 else
548 while (itimes--) {
549 for (j = 0; j < MAXPREFIX; j++) {
550 uint8_t c = 0;
551 switch (instruction->prefixes[j]) {
552 case P_WAIT:
553 c = 0x9B;
554 break;
555 case P_LOCK:
556 c = 0xF0;
557 break;
558 case P_REPNE:
559 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800560 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700561 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400562 c = 0xF2;
563 break;
564 case P_REPE:
565 case P_REPZ:
566 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800567 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400568 c = 0xF3;
569 break;
570 case R_CS:
571 if (bits == 64) {
572 error(ERR_WARNING | ERR_PASS2,
573 "cs segment base generated, but will be ignored in 64-bit mode");
574 }
575 c = 0x2E;
576 break;
577 case R_DS:
578 if (bits == 64) {
579 error(ERR_WARNING | ERR_PASS2,
580 "ds segment base generated, but will be ignored in 64-bit mode");
581 }
582 c = 0x3E;
583 break;
584 case R_ES:
585 if (bits == 64) {
586 error(ERR_WARNING | ERR_PASS2,
587 "es segment base generated, but will be ignored in 64-bit mode");
588 }
589 c = 0x26;
590 break;
591 case R_FS:
592 c = 0x64;
593 break;
594 case R_GS:
595 c = 0x65;
596 break;
597 case R_SS:
598 if (bits == 64) {
599 error(ERR_WARNING | ERR_PASS2,
600 "ss segment base generated, but will be ignored in 64-bit mode");
601 }
602 c = 0x36;
603 break;
604 case R_SEGR6:
605 case R_SEGR7:
606 error(ERR_NONFATAL,
607 "segr6 and segr7 cannot be used as prefixes");
608 break;
609 case P_A16:
610 if (bits == 64) {
611 error(ERR_NONFATAL,
612 "16-bit addressing is not supported "
613 "in 64-bit mode");
614 } else if (bits != 16)
615 c = 0x67;
616 break;
617 case P_A32:
618 if (bits != 32)
619 c = 0x67;
620 break;
621 case P_A64:
622 if (bits != 64) {
623 error(ERR_NONFATAL,
624 "64-bit addressing is only supported "
625 "in 64-bit mode");
626 }
627 break;
628 case P_ASP:
629 c = 0x67;
630 break;
631 case P_O16:
632 if (bits != 16)
633 c = 0x66;
634 break;
635 case P_O32:
636 if (bits == 16)
637 c = 0x66;
638 break;
639 case P_O64:
640 /* REX.W */
641 break;
642 case P_OSP:
643 c = 0x66;
644 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700645 case P_EVEX:
646 /* EVEX */
647 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400648 case P_none:
649 break;
650 default:
651 error(ERR_PANIC, "invalid instruction prefix");
652 }
653 if (c != 0) {
654 out(offset, segment, &c, OUT_RAWDATA, 1,
655 NO_SEG, NO_SEG);
656 offset++;
657 }
658 }
659 insn_end = offset + insn_size;
660 gencode(segment, offset, bits, instruction,
661 temp, insn_end);
662 offset += insn_size;
663 if (itimes > 0 && itimes == instruction->times - 1) {
664 /*
665 * Dummy call to list->output to give the offset to the
666 * listing module.
667 */
668 list->output(offset, NULL, OUT_RAWDATA, 0);
669 list->uplevel(LIST_TIMES);
670 }
671 }
672 if (instruction->times > 1)
673 list->downlevel(LIST_TIMES);
674 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700675 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400676 /* No match */
677 switch (m) {
678 case MERR_OPSIZEMISSING:
679 error(ERR_NONFATAL, "operation size not specified");
680 break;
681 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400683 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700684 case MERR_BRNUMMISMATCH:
685 error(ERR_NONFATAL,
686 "mismatch in the number of broadcasting elements");
687 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400688 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 break;
691 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800692 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400693 bits);
694 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800695 case MERR_BADBND:
696 error(ERR_NONFATAL, "bnd prefix is not allowed");
697 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400698 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 error(ERR_NONFATAL,
700 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400701 break;
702 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000703 }
704 return 0;
705}
706
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400707int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400708 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000709{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000710 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700711 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000712
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000714 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000715
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400716 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700719 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
720 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400721 instruction->opcode == I_DT || instruction->opcode == I_DO ||
722 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000723 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300724 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000725
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300727 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400729 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000730 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000731
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400733 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400735 warn_overflow_const(e->offset, wsize);
736 } else if (e->type == EOT_DB_STRING ||
737 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 align = (-osize) % wsize;
741 if (align < 0)
742 align += wsize;
743 isize += osize + align;
744 }
745 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746 }
747
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400749 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300751 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700752 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400754 fp = fopen(fname, "rb");
755 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
757 fname);
758 else if (fseek(fp, 0L, SEEK_END) < 0)
759 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
760 fname);
761 else {
762 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 if (instruction->eops->next) {
764 len -= instruction->eops->next->offset;
765 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700766 len > (size_t)instruction->eops->next->next->offset) {
767 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 }
769 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300770 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300772 if (fp)
773 fclose(fp);
774 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000775 }
776
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700777 /* Check to see if we need an address-size prefix */
778 add_asp(instruction, bits);
779
H. Peter Anvin23595f52009-07-25 17:44:25 -0700780 m = find_match(&temp, instruction, segment, offset, bits);
781 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400782 /* we've matched an instruction. */
783 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400784 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100785
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800786 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400787 if (isize < 0)
788 return -1;
789 for (j = 0; j < MAXPREFIX; j++) {
790 switch (instruction->prefixes[j]) {
791 case P_A16:
792 if (bits != 16)
793 isize++;
794 break;
795 case P_A32:
796 if (bits != 32)
797 isize++;
798 break;
799 case P_O16:
800 if (bits != 16)
801 isize++;
802 break;
803 case P_O32:
804 if (bits == 16)
805 isize++;
806 break;
807 case P_A64:
808 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700809 case P_EVEX:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400810 case P_none:
811 break;
812 default:
813 isize++;
814 break;
815 }
816 }
817 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700818 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400819 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000820 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821}
822
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800823static void bad_hle_warn(const insn * ins, uint8_t hleok)
824{
825 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800826 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800827 static const enum whatwarn warn[2][4] =
828 {
829 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
830 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
831 };
832 unsigned int n;
833
834 n = (unsigned int)rep_pfx - P_XACQUIRE;
835 if (n > 1)
836 return; /* Not XACQUIRE/XRELEASE */
837
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800838 ww = warn[n][hleok];
839 if (!is_class(MEMORY, ins->oprs[0].type))
840 ww = w_inval; /* HLE requires operand 0 to be memory */
841
842 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800843 case w_none:
844 break;
845
846 case w_lock:
847 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800848 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800849 "%s with this instruction requires lock",
850 prefix_name(rep_pfx));
851 }
852 break;
853
854 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800855 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800856 "%s invalid with this instruction",
857 prefix_name(rep_pfx));
858 break;
859 }
860}
861
H. Peter Anvin507ae032008-10-09 15:37:10 -0700862/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400863#define case3(x) case (x): case (x)+1: case (x)+2
864#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700865
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800866static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800867 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800869 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800870 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000871 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000872 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700873 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700874 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700875 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700876 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800878 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700879 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000880
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700881 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700882 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700883 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700884
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700885 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400886 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 (void)segment; /* Don't warn that this parameter is unused */
889 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000890
H. Peter Anvin839eca22007-10-29 23:12:47 -0700891 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400892 c = *codes++;
893 op1 = (c & 3) + ((opex & 1) << 2);
894 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
895 opx = &ins->oprs[op1];
896 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700897
H. Peter Anvin839eca22007-10-29 23:12:47 -0700898 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400899 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 codes += c, length += c;
901 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700902
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400903 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 opex = c;
905 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 case4(010):
908 ins->rex |=
909 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 codes++, length++;
911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Jin Kyu Song164d6072013-10-15 19:10:13 -0700913 case4(014):
914 /* this is an index reg of MIB operand */
915 mib_index = opx->basereg;
916 break;
917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400918 case4(020):
919 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 length++;
921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400923 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 length += 2;
925 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700928 if (opx->type & (BITS16 | BITS32 | BITS64))
929 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 else
931 length += (bits == 16) ? 2 : 4;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 length += 4;
936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700939 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 length++;
944 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400946 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000947 length += 8; /* MOV reg64/imm */
948 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 length += 2;
952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700955 if (opx->type & (BITS16 | BITS32 | BITS64))
956 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 else
958 length += (bits == 16) ? 2 : 4;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 length += 4;
963 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700966 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 case 0172:
970 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700972 length++;
973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700975 case4(0174):
976 length++;
977 break;
978
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700979 case4(0240):
980 ins->rex |= REX_EV;
981 ins->vexreg = regval(opx);
982 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
983 ins->vex_cm = *codes++;
984 ins->vex_wlp = *codes++;
985 ins->evex_tuple = (*codes++ - 0300);
986 break;
987
988 case 0250:
989 ins->rex |= REX_EV;
990 ins->vexreg = 0;
991 ins->vex_cm = *codes++;
992 ins->vex_wlp = *codes++;
993 ins->evex_tuple = (*codes++ - 0300);
994 break;
995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(0254):
997 length += 4;
998 break;
999
1000 case4(0260):
1001 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001002 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 ins->vex_cm = *codes++;
1004 ins->vex_wlp = *codes++;
1005 break;
1006
1007 case 0270:
1008 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001009 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 ins->vex_cm = *codes++;
1011 ins->vex_wlp = *codes++;
1012 break;
1013
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001014 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001015 hleok = c & 3;
1016 break;
1017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 case4(0274):
1019 length++;
1020 break;
1021
1022 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 if (bits == 64)
1027 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001028 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001032 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001036 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
Keith Kaniosb7a89542007-04-12 02:40:54 +00001038 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001039 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1040 has_prefix(ins, PPS_ASIZE, P_A32))
1041 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001043
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 case4(0314):
1045 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001048 {
1049 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1050 if (pfx == P_O16)
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_O16;
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 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001060 {
1061 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1062 if (pfx == P_O32)
1063 break;
1064 if (pfx != P_none)
1065 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1066 else
1067 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001069 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 case 0322:
1072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
Keith Kaniosb7a89542007-04-12 02:40:54 +00001074 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001075 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
Keith Kaniosb7a89542007-04-12 02:40:54 +00001078 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 case 0325:
1083 ins->rex |= REX_NH;
1084 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001085
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001086 case 0326:
1087 break;
1088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 case 0330:
1090 codes++, length++;
1091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001096 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0333:
1098 length++;
1099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001101 case 0334:
1102 ins->rex |= REX_L;
1103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001105 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001108 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001109 if (!ins->prefixes[PPS_REP])
1110 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001114 if (!ins->prefixes[PPS_REP])
1115 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 if (ins->oprs[0].segment != NO_SEG)
1120 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1121 " quantity of BSS space");
1122 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001123 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case 0341:
1127 if (!ins->prefixes[PPS_WAIT])
1128 ins->prefixes[PPS_WAIT] = P_WAIT;
1129 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 case 0360:
1132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001134 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 length++;
1136 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 case 0364:
1139 case 0365:
1140 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001141
Keith Kanios48af1772007-08-17 07:37:52 +00001142 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001143 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 length++;
1145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001147 case 0370:
1148 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 case 0373:
1152 length++;
1153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001155 case 0374:
1156 eat = EA_XMMVSIB;
1157 break;
1158
1159 case 0375:
1160 eat = EA_YMMVSIB;
1161 break;
1162
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001163 case 0376:
1164 eat = EA_ZMMVSIB;
1165 break;
1166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 case4(0100):
1168 case4(0110):
1169 case4(0120):
1170 case4(0130):
1171 case4(0200):
1172 case4(0204):
1173 case4(0210):
1174 case4(0214):
1175 case4(0220):
1176 case4(0224):
1177 case4(0230):
1178 case4(0234):
1179 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001181 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 opflags_t rflags;
1183 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001184 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001185
Keith Kaniosb7a89542007-04-12 02:40:54 +00001186 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001187
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 if (c <= 0177) {
1189 /* pick rfield from operand b (opx) */
1190 rflags = regflag(opx);
1191 rfield = nasm_regvals[opx->basereg];
1192 } else {
1193 rflags = 0;
1194 rfield = c & 7;
1195 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001196
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001197 /* EVEX.b1 : evex_brerop contains the operand position */
1198 op_er_sae = (ins->evex_brerop >= 0 ?
1199 &ins->oprs[ins->evex_brerop] : NULL);
1200
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001201 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1202 /* set EVEX.b */
1203 ins->evex_p[2] |= EVEX_P2B;
1204 if (op_er_sae->decoflags & ER) {
1205 /* set EVEX.RC (rounding control) */
1206 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1207 & EVEX_P2RC;
1208 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001209 } else {
1210 /* set EVEX.L'L (vector length) */
1211 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001212 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001213 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214 /* set EVEX.b */
1215 ins->evex_p[2] |= EVEX_P2B;
1216 }
1217 }
1218
Jin Kyu Song164d6072013-10-15 19:10:13 -07001219 /*
1220 * if a separate form of MIB (ICC style) is used,
1221 * the index reg info is merged into mem operand
1222 */
1223 if (mib_index != R_none) {
1224 opy->indexreg = mib_index;
1225 opy->scale = 1;
1226 opy->hintbase = mib_index;
1227 opy->hinttype = EAH_NOTBASE;
1228 }
1229
Jin Kyu Song3b653232013-11-08 11:41:12 -08001230 /*
1231 * only for mib operands, make a single reg index [reg*1].
1232 * gas uses this form to explicitly denote index register.
1233 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001234 if (itemp_has(temp, IF_MIB) &&
Jin Kyu Song3b653232013-11-08 11:41:12 -08001235 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1236 opy->hinttype == EAH_NOTBASE)) {
1237 opy->indexreg = opy->basereg;
1238 opy->basereg = -1;
1239 opy->scale = 1;
1240 }
1241
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001242 if (process_ea(opy, &ea_data, bits,
1243 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 errfunc(ERR_NONFATAL, "invalid effective address");
1245 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001246 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001247 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001249 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 }
1251 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 default:
1254 errfunc(ERR_PANIC, "internal instruction table corrupt"
1255 ": instruction code \\%o (0x%02X) given", c, c);
1256 break;
1257 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001258 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001259
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001260 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001261
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001262 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001263 if (ins->rex & REX_H) {
1264 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1265 return -1;
1266 }
1267 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001268 }
1269
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001270 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001271 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001272
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001273 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001274 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 return -1;
1276 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001277 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001279 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 ins->rex &= ~REX_W;
1281 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001282 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001283 ins->rex |= REX_W;
1284 bad32 &= ~REX_W;
1285 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001286 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 /* Follow REX_W */
1288 break;
1289 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001290
H. Peter Anvinfc561202011-07-07 16:58:22 -07001291 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1293 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001294 } else if (!(ins->rex & REX_EV) &&
1295 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1296 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1297 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001299 if (ins->rex & REX_EV)
1300 length += 4;
1301 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 length += 3;
1303 else
1304 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001305 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 if (ins->rex & REX_H) {
1307 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1308 return -1;
1309 } else if (bits == 64) {
1310 length++;
1311 } else if ((ins->rex & REX_L) &&
1312 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001313 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001315 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001316 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 length++;
1318 } else {
1319 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1320 return -1;
1321 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001322 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001323
1324 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001325 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001326 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001327 "instruction is not lockable");
1328 }
1329
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001330 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001331
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001332 return length;
1333}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001334
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001335static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1336{
1337 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001338 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001339 ins->rex = (ins->rex & REX_REAL) | REX_P;
1340 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1341 ins->rex = 0;
1342 return 1;
1343 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001344 }
1345
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001346 return 0;
1347}
1348
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001349static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001350 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001353 uint8_t c;
1354 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001355 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001356 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001357 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001358 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001359 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001360 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001361 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001362
H. Peter Anvin839eca22007-10-29 23:12:47 -07001363 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 c = *codes++;
1365 op1 = (c & 3) + ((opex & 1) << 2);
1366 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1367 opx = &ins->oprs[op1];
1368 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001369
H. Peter Anvin839eca22007-10-29 23:12:47 -07001370 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 case 01:
1372 case 02:
1373 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001375 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001376 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 codes += c;
1378 offset += c;
1379 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001380
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 case 05:
1382 case 06:
1383 case 07:
1384 opex = c;
1385 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001386
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001388 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001389 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001390 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 offset += 1;
1392 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001393
Jin Kyu Song164d6072013-10-15 19:10:13 -07001394 case4(014):
1395 break;
1396
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001397 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001398 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001399 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001400 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001402 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 offset += 1;
1404 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001405
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001406 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001407 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001408 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001409 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001410 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 offset += 1;
1412 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001414 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001415 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001416 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001417 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001418 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 offset += 2;
1420 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001421
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001422 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001423 if (opx->type & (BITS16 | BITS32))
1424 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 else
1426 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001427 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001428 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001429 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001430 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 offset += size;
1432 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001433
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001434 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001435 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001436 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001437 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001438 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 offset += 4;
1440 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001441
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001442 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001443 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001444 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001445 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001446 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001447 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 offset += size;
1449 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001451 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001452 if (opx->segment != segment) {
1453 data = opx->offset;
1454 out(offset, segment, &data,
1455 OUT_REL1ADR, insn_end - offset,
1456 opx->segment, opx->wrt);
1457 } else {
1458 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 if (data > 127 || data < -128)
1460 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001461 out(offset, segment, &data,
1462 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1463 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 offset += 1;
1465 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001467 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001468 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001469 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001470 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001471 offset += 8;
1472 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001473
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001474 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001475 if (opx->segment != segment) {
1476 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001478 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001479 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001481 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001483 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 }
1485 offset += 2;
1486 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001489 if (opx->type & (BITS16 | BITS32 | BITS64))
1490 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 else
1492 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001493 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001494 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001495 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1497 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001499 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001501 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 }
1503 offset += size;
1504 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001505
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001507 if (opx->segment != segment) {
1508 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001510 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001511 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
1517 offset += 4;
1518 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001521 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001522 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1523 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001524 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001525 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001526 outfmt->segbase(1 + opx->segment),
1527 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001528 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001530
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001531 case 0172:
1532 c = *codes++;
1533 opx = &ins->oprs[c >> 3];
1534 bytes[0] = nasm_regvals[opx->basereg] << 4;
1535 opx = &ins->oprs[c & 7];
1536 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1537 errfunc(ERR_NONFATAL,
1538 "non-absolute expression not permitted as argument %d",
1539 c & 7);
1540 } else {
1541 if (opx->offset & ~15) {
1542 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1543 "four-bit argument exceeds bounds");
1544 }
1545 bytes[0] |= opx->offset & 15;
1546 }
1547 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1548 offset++;
1549 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001550
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001551 case 0173:
1552 c = *codes++;
1553 opx = &ins->oprs[c >> 4];
1554 bytes[0] = nasm_regvals[opx->basereg] << 4;
1555 bytes[0] |= c & 15;
1556 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1557 offset++;
1558 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001559
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001560 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001561 bytes[0] = nasm_regvals[opx->basereg] << 4;
1562 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1563 offset++;
1564 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001565
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001566 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001567 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1569 (int32_t)data != (int64_t)data) {
1570 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1571 "signed dword immediate exceeds bounds");
1572 }
1573 out(offset, segment, &data, OUT_ADDRESS, 4,
1574 opx->segment, opx->wrt);
1575 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001576 break;
1577
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001578 case4(0240):
1579 case 0250:
1580 codes += 3;
1581 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1582 EVEX_P2Z | EVEX_P2AAA, 2);
1583 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1584 bytes[0] = 0x62;
1585 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001586 bytes[1] = ((((ins->rex & 7) << 5) |
1587 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1588 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001589 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1590 ((~ins->vexreg & 15) << 3) |
1591 (1 << 2) | (ins->vex_wlp & 3);
1592 bytes[3] = ins->evex_p[2];
1593 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1594 offset += 4;
1595 break;
1596
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001597 case4(0260):
1598 case 0270:
1599 codes += 2;
1600 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1601 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1602 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1603 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001604 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1606 offset += 3;
1607 } else {
1608 bytes[0] = 0xc5;
1609 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001610 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1612 offset += 2;
1613 }
1614 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001615
H. Peter Anvine014f352012-02-25 22:35:19 -08001616 case 0271:
1617 case 0272:
1618 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001619 break;
1620
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001621 case4(0274):
1622 {
1623 uint64_t uv, um;
1624 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001625
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001626 if (ins->rex & REX_W)
1627 s = 64;
1628 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1629 s = 16;
1630 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1631 s = 32;
1632 else
1633 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 um = (uint64_t)2 << (s-1);
1636 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001637
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 if (uv > 127 && uv < (uint64_t)-128 &&
1639 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001640 /* If this wasn't explicitly byte-sized, warn as though we
1641 * had fallen through to the imm16/32/64 case.
1642 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001643 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001644 "%s value exceeds bounds",
1645 (opx->type & BITS8) ? "signed byte" :
1646 s == 16 ? "word" :
1647 s == 32 ? "dword" :
1648 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001650 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001651 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001652 out(offset, segment, &data, OUT_ADDRESS, 1,
1653 opx->segment, opx->wrt);
1654 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001655 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001656 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1657 NO_SEG);
1658 }
1659 offset += 1;
1660 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001662
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001665
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001667 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001669 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 offset += 1;
1671 } else
1672 offset += 0;
1673 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001676 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001678 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 offset += 1;
1680 } else
1681 offset += 0;
1682 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 case 0312:
1685 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001686
Keith Kaniosb7a89542007-04-12 02:40:54 +00001687 case 0313:
1688 ins->rex = 0;
1689 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001690
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 case4(0314):
1692 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001693
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001697
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001699 case 0323:
1700 break;
1701
Keith Kaniosb7a89542007-04-12 02:40:54 +00001702 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001703 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001705
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 case 0325:
1707 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001708
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001709 case 0326:
1710 break;
1711
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001713 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001714 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 offset += 1;
1716 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001717
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001723 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001724 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 offset += 1;
1726 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001727
Keith Kanios48af1772007-08-17 07:37:52 +00001728 case 0334:
1729 if (ins->rex & REX_R) {
1730 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001731 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001732 offset += 1;
1733 }
1734 ins->rex &= ~(REX_L|REX_R);
1735 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001736
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001737 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001739
H. Peter Anvin962e3052008-08-28 17:47:16 -07001740 case 0336:
1741 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 if (ins->oprs[0].segment != NO_SEG)
1746 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1747 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001748 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 if (size > 0)
1750 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001751 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 offset += size;
1753 }
1754 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001756 case 0341:
1757 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001758
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 case 0360:
1760 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001761
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 case 0361:
1763 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001764 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1765 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001766 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001767
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001768 case 0364:
1769 case 0365:
1770 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001771
Keith Kanios48af1772007-08-17 07:37:52 +00001772 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001773 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001774 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001775 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001776 offset += 1;
1777 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001778
Jin Kyu Song03041092013-10-15 19:38:51 -07001779 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001781
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 case 0373:
1783 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001784 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 offset += 1;
1786 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001788 case 0374:
1789 eat = EA_XMMVSIB;
1790 break;
1791
1792 case 0375:
1793 eat = EA_YMMVSIB;
1794 break;
1795
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001796 case 0376:
1797 eat = EA_ZMMVSIB;
1798 break;
1799
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 case4(0100):
1801 case4(0110):
1802 case4(0120):
1803 case4(0130):
1804 case4(0200):
1805 case4(0204):
1806 case4(0210):
1807 case4(0214):
1808 case4(0220):
1809 case4(0224):
1810 case4(0230):
1811 case4(0234):
1812 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 ea ea_data;
1814 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001815 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001816 uint8_t *p;
1817 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001818 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001819
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001820 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 /* pick rfield from operand b (opx) */
1822 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001823 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001824 } else {
1825 /* rfield is constant */
1826 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001830 if (process_ea(opy, &ea_data, bits,
1831 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 p = bytes;
1835 *p++ = ea_data.modrm;
1836 if (ea_data.sib_present)
1837 *p++ = ea_data.sib;
1838
1839 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001840 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001842 /*
1843 * Make sure the address gets the right offset in case
1844 * the line breaks in the .lst file (BR 1197827)
1845 */
1846 offset += s;
1847 s = 0;
1848
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 switch (ea_data.bytes) {
1850 case 0:
1851 break;
1852 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 case 2:
1854 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001855 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001856 /* use compressed displacement, if available */
1857 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 if (ea_data.rip) {
1860 if (opy->segment == segment) {
1861 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001862 if (overflow_signed(data, ea_data.bytes))
1863 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 out(offset, segment, &data, OUT_ADDRESS,
1865 ea_data.bytes, NO_SEG, NO_SEG);
1866 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001867 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 out(offset, segment, &data, OUT_REL4ADR,
1869 insn_end - offset, opy->segment, opy->wrt);
1870 }
1871 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001872 if (overflow_general(data, ins->addr_size >> 3) ||
1873 signed_bits(data, ins->addr_size) !=
1874 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001875 warn_overflow(ERR_PASS2, ea_data.bytes);
1876
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001877 out(offset, segment, &data, OUT_ADDRESS,
1878 ea_data.bytes, opy->segment, opy->wrt);
1879 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001881 default:
1882 /* Impossible! */
1883 errfunc(ERR_PANIC,
1884 "Invalid amount of bytes (%d) for offset?!",
1885 ea_data.bytes);
1886 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 }
1888 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 }
1890 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001892 default:
1893 errfunc(ERR_PANIC, "internal instruction table corrupt"
1894 ": instruction code \\%o (0x%02X) given", c, c);
1895 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001897 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001898}
1899
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001900static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001901{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001902 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001903 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001904 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001905}
1906
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001907static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001908{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001909 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001911 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001912}
1913
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001914static int op_rexflags(const operand * o, int mask)
1915{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001916 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001917 int val;
1918
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001919 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001920 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001921
H. Peter Anvina4835d42008-05-20 14:21:29 -07001922 flags = nasm_reg_flags[o->basereg];
1923 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001924
1925 return rexflags(val, flags, mask);
1926}
1927
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001928static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001929{
1930 int rex = 0;
1931
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001932 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001933 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001934 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001935 rex |= REX_W;
1936 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1937 rex |= REX_H;
1938 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1939 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001940
1941 return rex & mask;
1942}
1943
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001944static int evexflags(int val, decoflags_t deco,
1945 int mask, uint8_t byte)
1946{
1947 int evex = 0;
1948
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001949 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001950 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001951 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001952 evex |= (EVEX_P0RP | EVEX_P0X);
1953 break;
1954 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001955 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001956 evex |= EVEX_P2VP;
1957 if (deco & Z)
1958 evex |= EVEX_P2Z;
1959 if (deco & OPMASK_MASK)
1960 evex |= deco & EVEX_P2AAA;
1961 break;
1962 }
1963 return evex & mask;
1964}
1965
1966static int op_evexflags(const operand * o, int mask, uint8_t byte)
1967{
1968 int val;
1969
1970 if (!is_register(o->basereg))
1971 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1972
1973 val = nasm_regvals[o->basereg];
1974
1975 return evexflags(val, o->decoflags, mask, byte);
1976}
1977
H. Peter Anvin23595f52009-07-25 17:44:25 -07001978static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001979 insn *instruction,
1980 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001981{
1982 const struct itemplate *temp;
1983 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001984 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001985 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001986 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001987 int i;
1988
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001989 /* broadcasting uses a different data element size */
1990 for (i = 0; i < instruction->operands; i++)
1991 if (i == broadcast)
1992 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1993 else
1994 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001995
1996 merr = MERR_INVALOP;
1997
1998 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001999 temp->opcode != I_none; temp++) {
2000 m = matches(temp, instruction, bits);
2001 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002002 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002003 m = MOK_GOOD;
2004 else
2005 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002006 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 /*
2008 * Missing operand size and a candidate for fuzzy matching...
2009 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002010 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002011 if (i == broadcast)
2012 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2013 else
2014 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 opsizemissing = true;
2016 }
2017 if (m > merr)
2018 merr = m;
2019 if (merr == MOK_GOOD)
2020 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002021 }
2022
2023 /* No match, but see if we can get a fuzzy operand size match... */
2024 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002025 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002026
2027 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002028 /*
2029 * We ignore extrinsic operand sizes on registers, so we should
2030 * never try to fuzzy-match on them. This also resolves the case
2031 * when we have e.g. "xmmrm128" in two different positions.
2032 */
2033 if (is_class(REGISTER, instruction->oprs[i].type))
2034 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002035
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002036 /* This tests if xsizeflags[i] has more than one bit set */
2037 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2038 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002039
Jin Kyu Song7903c072013-10-30 03:00:12 -07002040 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002041 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002042 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2043 BITS32 : BITS64);
2044 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002045 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002046 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002047 }
2048
2049 /* Try matching again... */
2050 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002051 temp->opcode != I_none; temp++) {
2052 m = matches(temp, instruction, bits);
2053 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002054 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002055 m = MOK_GOOD;
2056 else
2057 m = MERR_INVALOP;
2058 }
2059 if (m > merr)
2060 merr = m;
2061 if (merr == MOK_GOOD)
2062 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002063 }
2064
H. Peter Anvina81655b2009-07-25 18:15:28 -07002065done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002066 *tempp = temp;
2067 return merr;
2068}
2069
H. Peter Anvin65289e82009-07-25 17:25:11 -07002070static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002071 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002072{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002073 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002074 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002075 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002076
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002077 /*
2078 * Check the opcode
2079 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002081 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002082
2083 /*
2084 * Count the operands
2085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002087 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002088
2089 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002090 * Is it legal?
2091 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002092 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002093 return MERR_INVALOP;
2094
2095 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002096 * Check that no spurious colons or TOs are present
2097 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 for (i = 0; i < itemp->operands; i++)
2099 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002100 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002101
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002102 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002103 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002104 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002105 switch (itemp_smask(itemp)) {
2106 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002107 asize = BITS8;
2108 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002109 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002110 asize = BITS16;
2111 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002112 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002113 asize = BITS32;
2114 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002115 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002116 asize = BITS64;
2117 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002118 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002119 asize = BITS128;
2120 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002121 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002122 asize = BITS256;
2123 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002124 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002125 asize = BITS512;
2126 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002127 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002128 switch (bits) {
2129 case 16:
2130 asize = BITS16;
2131 break;
2132 case 32:
2133 asize = BITS32;
2134 break;
2135 case 64:
2136 asize = BITS64;
2137 break;
2138 default:
2139 asize = 0;
2140 break;
2141 }
2142 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002143 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002144 asize = 0;
2145 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002146 }
2147
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002148 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002149 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002150 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002151 memset(size, 0, sizeof size);
2152 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002153 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002154 /* S- flags apply to all operands */
2155 for (i = 0; i < MAX_OPERANDS; i++)
2156 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002157 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002158
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002159 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002160 * Check that the operand flags all match up,
2161 * it's a bit tricky so lets be verbose:
2162 *
2163 * 1) Find out the size of operand. If instruction
2164 * doesn't have one specified -- we're trying to
2165 * guess it either from template (IF_S* flag) or
2166 * from code bits.
2167 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002168 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002169 * template has an operand size specified AND this size differ
2170 * from which instruction has (perhaps we got it from code bits)
2171 * we are:
2172 * a) Check that only size of instruction and operand is differ
2173 * other characteristics do match
2174 * b) Perhaps it's a register specified in instruction so
2175 * for such a case we just mark that operand as "size
2176 * missing" and this will turn on fuzzy operand size
2177 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002178 */
2179 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002180 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002181 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002182 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002183 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002184 opflags_t template_opsize, insn_opsize;
2185
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002186 if (!(type & SIZE_MASK))
2187 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002188
Jin Kyu Song7903c072013-10-30 03:00:12 -07002189 insn_opsize = type & SIZE_MASK;
2190 if (!is_broadcast) {
2191 template_opsize = itemp->opd[i] & SIZE_MASK;
2192 } else {
2193 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2194 /*
2195 * when broadcasting, the element size depends on
2196 * the instruction type. decorator flag should match.
2197 */
2198
2199 if (deco_brsize) {
2200 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002201 /* calculate the proper number : {1to<brcast_num>} */
2202 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2203 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002204 } else {
2205 template_opsize = 0;
2206 }
2207 }
2208
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002209 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002210 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002211 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002212 } else if (template_opsize) {
2213 if (template_opsize != insn_opsize) {
2214 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002215 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002216 } else if (!is_class(REGISTER, type)) {
2217 /*
2218 * Note: we don't honor extrinsic operand sizes for registers,
2219 * so "missing operand size" for a register should be
2220 * considered a wildcard match rather than an error.
2221 */
2222 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002223 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002224 } else if (is_broadcast &&
2225 (brcast_num !=
2226 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2227 /*
2228 * broadcasting opsize matches but the number of repeated memory
2229 * element does not match.
2230 * if 64b double precision float is broadcasted to zmm (512b),
2231 * broadcasting decorator must be {1to8}.
2232 */
2233 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 }
Jin Kyu Song945b1b82013-10-25 19:29:53 -07002235 } else if (instruction->prefixes[PPS_EVEX] &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 !itemp_has(itemp, IF_AVX512)) {
Jin Kyu Song945b1b82013-10-25 19:29:53 -07002237 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002238 }
2239 }
2240
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002241 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002242 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002243
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002244 /*
2245 * Check operand sizes
2246 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002247 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2248 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002250 asize = itemp->opd[i] & SIZE_MASK;
2251 if (asize) {
2252 for (i = 0; i < oprs; i++)
2253 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 break;
2255 }
2256 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002257 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002259 }
2260
Keith Kaniosb7a89542007-04-12 02:40:54 +00002261 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002262 if (!(itemp->opd[i] & SIZE_MASK) &&
2263 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002264 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002265 }
2266
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002267 /*
2268 * Check template is okay at the set cpu level
2269 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002270 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002271 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002272
Keith Kaniosb7a89542007-04-12 02:40:54 +00002273 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002274 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002275 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002276 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002277 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002279 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002280 * If we have a HLE prefix, look for the NOHLE flag
2281 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002282 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002283 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2284 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2285 return MERR_BADHLE;
2286
2287 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002288 * Check if special handling needed for Jumps
2289 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002290 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002291 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292
Jin Kyu Song03041092013-10-15 19:38:51 -07002293 /*
2294 * Check if BND prefix is allowed
2295 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002296 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002297 has_prefix(instruction, PPS_REP, P_BND))
2298 return MERR_BADBND;
2299
H. Peter Anvin60926242009-07-26 16:25:38 -07002300 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002301}
2302
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002303/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002304 * Check if ModR/M.mod should/can be 01.
2305 * - EAF_BYTEOFFS is set
2306 * - offset can fit in a byte when EVEX is not used
2307 * - offset can be compressed when EVEX is used
2308 */
2309#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2310 (o >= -128 && o <= 127 && \
2311 seg == NO_SEG && !forw_ref && \
2312 !(input->eaflags & EAF_WORDOFFS) && \
2313 !(ins->rex & REX_EV)) || \
2314 (ins->rex & REX_EV && \
2315 is_disp8n(input, ins, &output->disp8)))
2316
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002317static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002318 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002319{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002320 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002321 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002322
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002323 output->type = EA_SCALAR;
2324 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002325 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002326
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002327 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002328 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002329 /* EVEX.R' flag for the REG operand */
2330 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002331
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002332 if (is_class(REGISTER, input->type)) {
2333 /*
2334 * It's a direct register.
2335 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002336 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002337 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002338
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002339 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002340 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002341
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002342 /* broadcasting is not available with a direct register operand. */
2343 if (input->decoflags & BRDCAST_MASK) {
2344 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2345 goto err;
2346 }
2347
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002348 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002349 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002350 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002351 output->bytes = 0; /* no offset necessary either */
2352 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2353 } else {
2354 /*
2355 * It's a memory reference.
2356 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002357
2358 /* Embedded rounding or SAE is not available with a mem ref operand. */
2359 if (input->decoflags & (ER | SAE)) {
2360 nasm_error(ERR_NONFATAL,
2361 "Embedded rounding is available only with reg-reg op.");
2362 return -1;
2363 }
2364
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002365 if (input->basereg == -1 &&
2366 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002367 /*
2368 * It's a pure offset.
2369 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002370 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2371 input->segment == NO_SEG) {
2372 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2373 input->type &= ~IP_REL;
2374 input->type |= MEMORY;
2375 }
2376
2377 if (input->eaflags & EAF_BYTEOFFS ||
2378 (input->eaflags & EAF_WORDOFFS &&
2379 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2380 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2381 }
2382
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002383 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002384 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 output->sib = GEN_SIB(0, 4, 5);
2386 output->bytes = 4;
2387 output->modrm = GEN_MODRM(0, rfield, 4);
2388 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002389 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002390 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002391 output->bytes = (addrbits != 16 ? 4 : 2);
2392 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2393 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002394 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002395 } else {
2396 /*
2397 * It's an indirection.
2398 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002400 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002402 int t, it, bt; /* register numbers */
2403 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002404
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 if (s == 0)
2406 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002407
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002408 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002409 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002410 ix = nasm_reg_flags[i];
2411 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002412 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002413 ix = 0;
2414 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002415
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002416 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002417 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002418 bx = nasm_reg_flags[b];
2419 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002420 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002421 bx = 0;
2422 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002423
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002424 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002425 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002426 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002427 int32_t o = input->offset;
2428 int mod, scale, index, base;
2429
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002430 /*
2431 * For a vector SIB, one has to be a vector and the other,
2432 * if present, a GPR. The vector must be the index operand.
2433 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002434 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002435 if (s == 0)
2436 s = 1;
2437 else if (s != 1)
2438 goto err;
2439
2440 t = bt, bt = it, it = t;
2441 x = bx, bx = ix, ix = x;
2442 }
2443
2444 if (bt != -1) {
2445 if (REG_GPR & ~bx)
2446 goto err;
2447 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2448 sok &= bx;
2449 else
2450 goto err;
2451 }
2452
2453 /*
2454 * While we're here, ensure the user didn't specify
2455 * WORD or QWORD
2456 */
2457 if (input->disp_size == 16 || input->disp_size == 64)
2458 goto err;
2459
2460 if (addrbits == 16 ||
2461 (addrbits == 32 && !(sok & BITS32)) ||
2462 (addrbits == 64 && !(sok & BITS64)))
2463 goto err;
2464
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2466 : ((ix & YMMREG & ~REG_EA)
2467 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002468
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 output->rex |= rexflags(it, ix, REX_X);
2470 output->rex |= rexflags(bt, bx, REX_B);
2471 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002472
2473 index = it & 7; /* it is known to be != -1 */
2474
2475 switch (s) {
2476 case 1:
2477 scale = 0;
2478 break;
2479 case 2:
2480 scale = 1;
2481 break;
2482 case 4:
2483 scale = 2;
2484 break;
2485 case 8:
2486 scale = 3;
2487 break;
2488 default: /* then what the smeg is it? */
2489 goto err; /* panic */
2490 }
2491
2492 if (bt == -1) {
2493 base = 5;
2494 mod = 0;
2495 } else {
2496 base = (bt & 7);
2497 if (base != REG_NUM_EBP && o == 0 &&
2498 seg == NO_SEG && !forw_ref &&
2499 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2500 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002501 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002502 mod = 1;
2503 else
2504 mod = 2;
2505 }
2506
2507 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002508 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2509 output->modrm = GEN_MODRM(mod, rfield, 4);
2510 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002511 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002512 /*
2513 * it must be a 32/64-bit memory reference. Firstly we have
2514 * to check that all registers involved are type E/Rxx.
2515 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002516 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002517 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002518
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002519 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002520 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2521 sok &= ix;
2522 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002523 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002525
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 if (bt != -1) {
2527 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002528 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002529 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002530 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002531 sok &= bx;
2532 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002533
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 /*
2535 * While we're here, ensure the user didn't specify
2536 * WORD or QWORD
2537 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002538 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002540
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 if (addrbits == 16 ||
2542 (addrbits == 32 && !(sok & BITS32)) ||
2543 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002545
Keith Kaniosb7a89542007-04-12 02:40:54 +00002546 /* now reorganize base/index */
2547 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002548 ((hb == b && ht == EAH_NOTBASE) ||
2549 (hb == i && ht == EAH_MAKEBASE))) {
2550 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002551 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 x = bx, bx = ix, ix = x;
2553 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002554 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002555 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002556 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002557 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002558 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002559 }
2560 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2561 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002562 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002563 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2564 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002565 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002567 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002568 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002569 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002570 x = ix, ix = bx, bx = x;
2571 }
2572 if (it == REG_NUM_ESP ||
2573 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002575
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002576 output->rex |= rexflags(it, ix, REX_X);
2577 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002578
Keith Kanios48af1772007-08-17 07:37:52 +00002579 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002580 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002582
Keith Kaniosb7a89542007-04-12 02:40:54 +00002583 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002586 } else {
2587 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002588 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 seg == NO_SEG && !forw_ref &&
2590 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002591 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002592 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002593 mod = 1;
2594 else
2595 mod = 2;
2596 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002597
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002598 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002599 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2600 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002601 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002602 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002604
Keith Kaniosb7a89542007-04-12 02:40:54 +00002605 if (it == -1)
2606 index = 4, s = 1;
2607 else
2608 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002609
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 switch (s) {
2611 case 1:
2612 scale = 0;
2613 break;
2614 case 2:
2615 scale = 1;
2616 break;
2617 case 4:
2618 scale = 2;
2619 break;
2620 case 8:
2621 scale = 3;
2622 break;
2623 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002626
Keith Kaniosb7a89542007-04-12 02:40:54 +00002627 if (bt == -1) {
2628 base = 5;
2629 mod = 0;
2630 } else {
2631 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002632 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002633 seg == NO_SEG && !forw_ref &&
2634 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002635 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002636 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002637 mod = 1;
2638 else
2639 mod = 2;
2640 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002641
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002642 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002643 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2644 output->modrm = GEN_MODRM(mod, rfield, 4);
2645 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 }
2647 } else { /* it's 16-bit */
2648 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002649 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002650
Keith Kaniosb7a89542007-04-12 02:40:54 +00002651 /* check for 64-bit long mode */
2652 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002654
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002656 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2657 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002658 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002659
Keith Kaniosb7a89542007-04-12 02:40:54 +00002660 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002661 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002663
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002665 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 if (b == -1 && i != -1) {
2667 int tmp = b;
2668 b = i;
2669 i = tmp;
2670 } /* swap */
2671 if ((b == R_SI || b == R_DI) && i != -1) {
2672 int tmp = b;
2673 b = i;
2674 i = tmp;
2675 }
2676 /* have BX/BP as base, SI/DI index */
2677 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002678 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 if (i != -1 && b != -1 &&
2680 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002681 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002683 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002684
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 rm = -1;
2686 if (i != -1)
2687 switch (i * 256 + b) {
2688 case R_SI * 256 + R_BX:
2689 rm = 0;
2690 break;
2691 case R_DI * 256 + R_BX:
2692 rm = 1;
2693 break;
2694 case R_SI * 256 + R_BP:
2695 rm = 2;
2696 break;
2697 case R_DI * 256 + R_BP:
2698 rm = 3;
2699 break;
2700 } else
2701 switch (b) {
2702 case R_SI:
2703 rm = 4;
2704 break;
2705 case R_DI:
2706 rm = 5;
2707 break;
2708 case R_BP:
2709 rm = 6;
2710 break;
2711 case R_BX:
2712 rm = 7;
2713 break;
2714 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002716 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002717
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002718 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2719 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002721 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 mod = 1;
2723 else
2724 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002725
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002726 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002727 output->bytes = mod; /* bytes of offset needed */
2728 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 }
2730 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002731 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002732
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002733 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002734 return output->type;
2735
2736err:
2737 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002738}
2739
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002740static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002741{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002742 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002743 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002745 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002746
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002747 switch (ins->prefixes[PPS_ASIZE]) {
2748 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002749 valid &= 16;
2750 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002751 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 valid &= 32;
2753 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002754 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 valid &= 64;
2756 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002757 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 valid &= (addrbits == 32) ? 16 : 32;
2759 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002760 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002761 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002762 }
2763
2764 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002765 if (is_class(MEMORY, ins->oprs[j].type)) {
2766 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002767
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002768 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002769 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002770 i = 0;
2771 else
2772 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002773
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002774 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002775 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002776 b = 0;
2777 else
2778 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002779
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002780 if (ins->oprs[j].scale == 0)
2781 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002782
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002783 if (!i && !b) {
2784 int ds = ins->oprs[j].disp_size;
2785 if ((addrbits != 64 && ds > 8) ||
2786 (addrbits == 64 && ds == 16))
2787 valid &= ds;
2788 } else {
2789 if (!(REG16 & ~b))
2790 valid &= 16;
2791 if (!(REG32 & ~b))
2792 valid &= 32;
2793 if (!(REG64 & ~b))
2794 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002795
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002796 if (!(REG16 & ~i))
2797 valid &= 16;
2798 if (!(REG32 & ~i))
2799 valid &= 32;
2800 if (!(REG64 & ~i))
2801 valid &= 64;
2802 }
2803 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002804 }
2805
2806 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002807 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002808 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002810 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002812 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 /* Impossible... */
2814 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2815 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002816 }
2817
2818 defdisp = ins->addr_size == 16 ? 16 : 32;
2819
2820 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002821 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2822 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2823 /*
2824 * mem_offs sizes must match the address size; if not,
2825 * strip the MEM_OFFS bit and match only EA instructions
2826 */
2827 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2828 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002829 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002830}