blob: bf1490d0063d117c1bc032642afc2de76b88517f [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:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800646 case P_VEX3:
647 case P_VEX2:
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 Song6cfa9682013-11-26 17:27:48 -0800695 case MERR_ENCMISMATCH:
696 error(ERR_NONFATAL, "specific encoding scheme not available");
697 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800698 case MERR_BADBND:
699 error(ERR_NONFATAL, "bnd prefix is not allowed");
700 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400701 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 error(ERR_NONFATAL,
703 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400704 break;
705 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000706 }
707 return 0;
708}
709
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400710int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400711 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000712{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000713 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700714 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000715
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000717 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400719 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000721
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700722 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
723 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400724 instruction->opcode == I_DT || instruction->opcode == I_DO ||
725 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300727 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300730 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000731
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400732 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000733 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000734
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400736 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400738 warn_overflow_const(e->offset, wsize);
739 } else if (e->type == EOT_DB_STRING ||
740 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000742
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 align = (-osize) % wsize;
744 if (align < 0)
745 align += wsize;
746 isize += osize + align;
747 }
748 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000749 }
750
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400752 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300754 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700755 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000756
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400757 fp = fopen(fname, "rb");
758 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
760 fname);
761 else if (fseek(fp, 0L, SEEK_END) < 0)
762 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
763 fname);
764 else {
765 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 if (instruction->eops->next) {
767 len -= instruction->eops->next->offset;
768 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700769 len > (size_t)instruction->eops->next->next->offset) {
770 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 }
772 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300773 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300775 if (fp)
776 fclose(fp);
777 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000778 }
779
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700780 /* Check to see if we need an address-size prefix */
781 add_asp(instruction, bits);
782
H. Peter Anvin23595f52009-07-25 17:44:25 -0700783 m = find_match(&temp, instruction, segment, offset, bits);
784 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400785 /* we've matched an instruction. */
786 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400787 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100788
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800789 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400790 if (isize < 0)
791 return -1;
792 for (j = 0; j < MAXPREFIX; j++) {
793 switch (instruction->prefixes[j]) {
794 case P_A16:
795 if (bits != 16)
796 isize++;
797 break;
798 case P_A32:
799 if (bits != 32)
800 isize++;
801 break;
802 case P_O16:
803 if (bits != 16)
804 isize++;
805 break;
806 case P_O32:
807 if (bits == 16)
808 isize++;
809 break;
810 case P_A64:
811 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700812 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800813 case P_VEX3:
814 case P_VEX2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400815 case P_none:
816 break;
817 default:
818 isize++;
819 break;
820 }
821 }
822 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700823 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400824 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826}
827
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800828static void bad_hle_warn(const insn * ins, uint8_t hleok)
829{
830 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800831 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800832 static const enum whatwarn warn[2][4] =
833 {
834 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
835 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
836 };
837 unsigned int n;
838
839 n = (unsigned int)rep_pfx - P_XACQUIRE;
840 if (n > 1)
841 return; /* Not XACQUIRE/XRELEASE */
842
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800843 ww = warn[n][hleok];
844 if (!is_class(MEMORY, ins->oprs[0].type))
845 ww = w_inval; /* HLE requires operand 0 to be memory */
846
847 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800848 case w_none:
849 break;
850
851 case w_lock:
852 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800853 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800854 "%s with this instruction requires lock",
855 prefix_name(rep_pfx));
856 }
857 break;
858
859 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800860 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800861 "%s invalid with this instruction",
862 prefix_name(rep_pfx));
863 break;
864 }
865}
866
H. Peter Anvin507ae032008-10-09 15:37:10 -0700867/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400868#define case3(x) case (x): case (x)+1: case (x)+2
869#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700870
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800871static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800872 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000873{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800874 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800875 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000876 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000877 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700878 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700879 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700880 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700881 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800882 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800883 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700884 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000885
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700886 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700887 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700888 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700889
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700890 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400891 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 (void)segment; /* Don't warn that this parameter is unused */
894 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000895
H. Peter Anvin839eca22007-10-29 23:12:47 -0700896 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400897 c = *codes++;
898 op1 = (c & 3) + ((opex & 1) << 2);
899 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
900 opx = &ins->oprs[op1];
901 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700902
H. Peter Anvin839eca22007-10-29 23:12:47 -0700903 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400904 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 codes += c, length += c;
906 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700907
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400908 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400909 opex = c;
910 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400912 case4(010):
913 ins->rex |=
914 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 codes++, length++;
916 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700917
Jin Kyu Song164d6072013-10-15 19:10:13 -0700918 case4(014):
919 /* this is an index reg of MIB operand */
920 mib_index = opx->basereg;
921 break;
922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400923 case4(020):
924 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 length++;
926 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400928 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 length += 2;
930 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700931
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400932 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700933 if (opx->type & (BITS16 | BITS32 | BITS64))
934 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 else
936 length += (bits == 16) ? 2 : 4;
937 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 length += 4;
941 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700942
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400943 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700944 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700946
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 length++;
949 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700950
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400951 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000952 length += 8; /* MOV reg64/imm */
953 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400955 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 length += 2;
957 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700958
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400959 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700960 if (opx->type & (BITS16 | BITS32 | BITS64))
961 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 else
963 length += (bits == 16) ? 2 : 4;
964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 length += 4;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700971 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400974 case 0172:
975 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700977 length++;
978 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700979
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700980 case4(0174):
981 length++;
982 break;
983
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700984 case4(0240):
985 ins->rex |= REX_EV;
986 ins->vexreg = regval(opx);
987 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
988 ins->vex_cm = *codes++;
989 ins->vex_wlp = *codes++;
990 ins->evex_tuple = (*codes++ - 0300);
991 break;
992
993 case 0250:
994 ins->rex |= REX_EV;
995 ins->vexreg = 0;
996 ins->vex_cm = *codes++;
997 ins->vex_wlp = *codes++;
998 ins->evex_tuple = (*codes++ - 0300);
999 break;
1000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(0254):
1002 length += 4;
1003 break;
1004
1005 case4(0260):
1006 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001007 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 ins->vex_cm = *codes++;
1009 ins->vex_wlp = *codes++;
1010 break;
1011
1012 case 0270:
1013 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001014 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 ins->vex_cm = *codes++;
1016 ins->vex_wlp = *codes++;
1017 break;
1018
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001019 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001020 hleok = c & 3;
1021 break;
1022
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001023 case4(0274):
1024 length++;
1025 break;
1026
1027 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 if (bits == 64)
1032 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001033 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001037 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
Keith Kaniosb7a89542007-04-12 02:40:54 +00001043 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1045 has_prefix(ins, PPS_ASIZE, P_A32))
1046 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 case4(0314):
1050 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001053 {
1054 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1055 if (pfx == P_O16)
1056 break;
1057 if (pfx != P_none)
1058 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1059 else
1060 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001062 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001065 {
1066 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1067 if (pfx == P_O32)
1068 break;
1069 if (pfx != P_none)
1070 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1071 else
1072 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001074 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001075
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 case 0322:
1077 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001078
Keith Kaniosb7a89542007-04-12 02:40:54 +00001079 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001080 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001081 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
Keith Kaniosb7a89542007-04-12 02:40:54 +00001083 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001084 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 case 0325:
1088 ins->rex |= REX_NH;
1089 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001090
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001091 case 0326:
1092 break;
1093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 case 0330:
1095 codes++, length++;
1096 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001101 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 case 0333:
1103 length++;
1104 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 case 0334:
1107 ins->rex |= REX_L;
1108 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001109
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001110 case 0335:
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 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001114 if (!ins->prefixes[PPS_REP])
1115 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001119 if (!ins->prefixes[PPS_REP])
1120 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 if (ins->oprs[0].segment != NO_SEG)
1125 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1126 " quantity of BSS space");
1127 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001128 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 case 0341:
1132 if (!ins->prefixes[PPS_WAIT])
1133 ins->prefixes[PPS_WAIT] = P_WAIT;
1134 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001135
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001136 case 0360:
1137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001139 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 length++;
1141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case 0364:
1144 case 0365:
1145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Keith Kanios48af1772007-08-17 07:37:52 +00001147 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001148 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 length++;
1150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001152 case 0370:
1153 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 case 0373:
1157 length++;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001160 case 0374:
1161 eat = EA_XMMVSIB;
1162 break;
1163
1164 case 0375:
1165 eat = EA_YMMVSIB;
1166 break;
1167
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001168 case 0376:
1169 eat = EA_ZMMVSIB;
1170 break;
1171
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 case4(0100):
1173 case4(0110):
1174 case4(0120):
1175 case4(0130):
1176 case4(0200):
1177 case4(0204):
1178 case4(0210):
1179 case4(0214):
1180 case4(0220):
1181 case4(0224):
1182 case4(0230):
1183 case4(0234):
1184 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001186 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001187 opflags_t rflags;
1188 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001189 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001190
Keith Kaniosb7a89542007-04-12 02:40:54 +00001191 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 if (c <= 0177) {
1194 /* pick rfield from operand b (opx) */
1195 rflags = regflag(opx);
1196 rfield = nasm_regvals[opx->basereg];
1197 } else {
1198 rflags = 0;
1199 rfield = c & 7;
1200 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001201
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001202 /* EVEX.b1 : evex_brerop contains the operand position */
1203 op_er_sae = (ins->evex_brerop >= 0 ?
1204 &ins->oprs[ins->evex_brerop] : NULL);
1205
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001206 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1207 /* set EVEX.b */
1208 ins->evex_p[2] |= EVEX_P2B;
1209 if (op_er_sae->decoflags & ER) {
1210 /* set EVEX.RC (rounding control) */
1211 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1212 & EVEX_P2RC;
1213 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214 } else {
1215 /* set EVEX.L'L (vector length) */
1216 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001217 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001218 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001219 /* set EVEX.b */
1220 ins->evex_p[2] |= EVEX_P2B;
1221 }
1222 }
1223
Jin Kyu Song164d6072013-10-15 19:10:13 -07001224 /*
1225 * if a separate form of MIB (ICC style) is used,
1226 * the index reg info is merged into mem operand
1227 */
1228 if (mib_index != R_none) {
1229 opy->indexreg = mib_index;
1230 opy->scale = 1;
1231 opy->hintbase = mib_index;
1232 opy->hinttype = EAH_NOTBASE;
1233 }
1234
Jin Kyu Song3b653232013-11-08 11:41:12 -08001235 /*
1236 * only for mib operands, make a single reg index [reg*1].
1237 * gas uses this form to explicitly denote index register.
1238 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001239 if (itemp_has(temp, IF_MIB) &&
Jin Kyu Song3b653232013-11-08 11:41:12 -08001240 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1241 opy->hinttype == EAH_NOTBASE)) {
1242 opy->indexreg = opy->basereg;
1243 opy->basereg = -1;
1244 opy->scale = 1;
1245 }
1246
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001247 if (process_ea(opy, &ea_data, bits,
1248 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 errfunc(ERR_NONFATAL, "invalid effective address");
1250 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001251 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001254 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 }
1256 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001257
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001258 default:
1259 errfunc(ERR_PANIC, "internal instruction table corrupt"
1260 ": instruction code \\%o (0x%02X) given", c, c);
1261 break;
1262 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001263 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001264
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001265 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001266
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001267 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001268 if (ins->rex & REX_H) {
1269 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1270 return -1;
1271 }
1272 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001273 }
1274
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001275 switch (ins->prefixes[PPS_VEX]) {
1276 case P_EVEX:
1277 if (!(ins->rex & REX_EV))
1278 return -1;
1279 break;
1280 case P_VEX3:
1281 case P_VEX2:
1282 if (!(ins->rex & REX_V))
1283 return -1;
1284 break;
1285 default:
1286 break;
1287 }
1288
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001289 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001290 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001291
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001293 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 return -1;
1295 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001296 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001298 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 ins->rex &= ~REX_W;
1300 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001301 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 ins->rex |= REX_W;
1303 bad32 &= ~REX_W;
1304 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001305 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 /* Follow REX_W */
1307 break;
1308 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001309
H. Peter Anvinfc561202011-07-07 16:58:22 -07001310 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001311 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1312 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001313 } else if (!(ins->rex & REX_EV) &&
1314 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1315 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1316 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001318 if (ins->rex & REX_EV)
1319 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001320 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1321 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 length += 3;
1323 else
1324 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001325 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 if (ins->rex & REX_H) {
1327 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1328 return -1;
1329 } else if (bits == 64) {
1330 length++;
1331 } else if ((ins->rex & REX_L) &&
1332 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001333 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001335 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001336 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 length++;
1338 } else {
1339 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1340 return -1;
1341 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001342 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001343
1344 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001345 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001346 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001347 "instruction is not lockable");
1348 }
1349
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001350 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001351
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001352 return length;
1353}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001354
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001355static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1356{
1357 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001358 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001359 ins->rex = (ins->rex & REX_REAL) | REX_P;
1360 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1361 ins->rex = 0;
1362 return 1;
1363 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001364 }
1365
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001366 return 0;
1367}
1368
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001369static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001370 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001371 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001372{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001373 uint8_t c;
1374 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001375 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001376 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001377 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001378 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001379 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001380 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001381 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001382
H. Peter Anvin839eca22007-10-29 23:12:47 -07001383 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 c = *codes++;
1385 op1 = (c & 3) + ((opex & 1) << 2);
1386 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1387 opx = &ins->oprs[op1];
1388 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001389
H. Peter Anvin839eca22007-10-29 23:12:47 -07001390 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 case 01:
1392 case 02:
1393 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001394 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001395 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001396 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 codes += c;
1398 offset += c;
1399 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001401 case 05:
1402 case 06:
1403 case 07:
1404 opex = c;
1405 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001407 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001408 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001409 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001410 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 offset += 1;
1412 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413
Jin Kyu Song164d6072013-10-15 19:10:13 -07001414 case4(014):
1415 break;
1416
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001417 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001418 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001419 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001420 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001422 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 offset += 1;
1424 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001426 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001427 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001428 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001430 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 offset += 1;
1432 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001433
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001434 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001435 warn_overflow_opd(opx, 2);
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, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001438 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 offset += 2;
1440 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001441
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001442 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001443 if (opx->type & (BITS16 | BITS32))
1444 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 else
1446 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001447 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001448 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001449 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001450 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 offset += size;
1452 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001455 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001456 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001457 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001458 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 offset += 4;
1460 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001461
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001463 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001464 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001465 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001466 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001467 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 offset += size;
1469 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001472 if (opx->segment != segment) {
1473 data = opx->offset;
1474 out(offset, segment, &data,
1475 OUT_REL1ADR, insn_end - offset,
1476 opx->segment, opx->wrt);
1477 } else {
1478 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001479 if (data > 127 || data < -128)
1480 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001481 out(offset, segment, &data,
1482 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 offset += 1;
1485 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001486
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001487 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001488 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001489 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001490 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001491 offset += 8;
1492 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001493
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001495 if (opx->segment != segment) {
1496 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001498 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001499 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001501 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001503 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 }
1505 offset += 2;
1506 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001507
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001508 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001509 if (opx->type & (BITS16 | BITS32 | BITS64))
1510 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 else
1512 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001514 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1517 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001519 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001521 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 }
1523 offset += size;
1524 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001525
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001526 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001527 if (opx->segment != segment) {
1528 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001530 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001531 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001533 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001535 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 }
1537 offset += 4;
1538 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001539
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001540 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001541 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001542 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1543 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001545 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001546 outfmt->segbase(1 + opx->segment),
1547 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001548 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001551 case 0172:
1552 c = *codes++;
1553 opx = &ins->oprs[c >> 3];
1554 bytes[0] = nasm_regvals[opx->basereg] << 4;
1555 opx = &ins->oprs[c & 7];
1556 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1557 errfunc(ERR_NONFATAL,
1558 "non-absolute expression not permitted as argument %d",
1559 c & 7);
1560 } else {
1561 if (opx->offset & ~15) {
1562 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1563 "four-bit argument exceeds bounds");
1564 }
1565 bytes[0] |= opx->offset & 15;
1566 }
1567 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1568 offset++;
1569 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001570
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001571 case 0173:
1572 c = *codes++;
1573 opx = &ins->oprs[c >> 4];
1574 bytes[0] = nasm_regvals[opx->basereg] << 4;
1575 bytes[0] |= c & 15;
1576 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1577 offset++;
1578 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001579
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001580 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 bytes[0] = nasm_regvals[opx->basereg] << 4;
1582 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1583 offset++;
1584 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001587 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1589 (int32_t)data != (int64_t)data) {
1590 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1591 "signed dword immediate exceeds bounds");
1592 }
1593 out(offset, segment, &data, OUT_ADDRESS, 4,
1594 opx->segment, opx->wrt);
1595 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001596 break;
1597
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001598 case4(0240):
1599 case 0250:
1600 codes += 3;
1601 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1602 EVEX_P2Z | EVEX_P2AAA, 2);
1603 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1604 bytes[0] = 0x62;
1605 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001606 bytes[1] = ((((ins->rex & 7) << 5) |
1607 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1608 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001609 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1610 ((~ins->vexreg & 15) << 3) |
1611 (1 << 2) | (ins->vex_wlp & 3);
1612 bytes[3] = ins->evex_p[2];
1613 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1614 offset += 4;
1615 break;
1616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(0260):
1618 case 0270:
1619 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001620 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1621 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001622 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1623 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1624 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001625 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001626 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1627 offset += 3;
1628 } else {
1629 bytes[0] = 0xc5;
1630 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001631 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1633 offset += 2;
1634 }
1635 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001636
H. Peter Anvine014f352012-02-25 22:35:19 -08001637 case 0271:
1638 case 0272:
1639 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001640 break;
1641
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001642 case4(0274):
1643 {
1644 uint64_t uv, um;
1645 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001646
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 if (ins->rex & REX_W)
1648 s = 64;
1649 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1650 s = 16;
1651 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1652 s = 32;
1653 else
1654 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 um = (uint64_t)2 << (s-1);
1657 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 if (uv > 127 && uv < (uint64_t)-128 &&
1660 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001661 /* If this wasn't explicitly byte-sized, warn as though we
1662 * had fallen through to the imm16/32/64 case.
1663 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001664 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001665 "%s value exceeds bounds",
1666 (opx->type & BITS8) ? "signed byte" :
1667 s == 16 ? "word" :
1668 s == 32 ? "dword" :
1669 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001670 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001671 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001672 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001673 out(offset, segment, &data, OUT_ADDRESS, 1,
1674 opx->segment, opx->wrt);
1675 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001676 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001677 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1678 NO_SEG);
1679 }
1680 offset += 1;
1681 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001683
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001688 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001690 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 offset += 1;
1692 } else
1693 offset += 0;
1694 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001697 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001699 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 offset += 1;
1701 } else
1702 offset += 0;
1703 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001704
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 case 0312:
1706 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001707
Keith Kaniosb7a89542007-04-12 02:40:54 +00001708 case 0313:
1709 ins->rex = 0;
1710 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001711
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 case4(0314):
1713 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001714
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001718
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001720 case 0323:
1721 break;
1722
Keith Kaniosb7a89542007-04-12 02:40:54 +00001723 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001724 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001726
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 case 0325:
1728 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001729
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001730 case 0326:
1731 break;
1732
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001734 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001735 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 offset += 1;
1737 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001738
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001741
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001744 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001745 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 offset += 1;
1747 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001748
Keith Kanios48af1772007-08-17 07:37:52 +00001749 case 0334:
1750 if (ins->rex & REX_R) {
1751 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001752 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001753 offset += 1;
1754 }
1755 ins->rex &= ~(REX_L|REX_R);
1756 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001757
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001758 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001760
H. Peter Anvin962e3052008-08-28 17:47:16 -07001761 case 0336:
1762 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001763 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001764
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 if (ins->oprs[0].segment != NO_SEG)
1767 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1768 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001769 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 if (size > 0)
1771 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001772 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 offset += size;
1774 }
1775 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 case 0341:
1778 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001779
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001780 case 0360:
1781 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001782
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001783 case 0361:
1784 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001785 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1786 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001787 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case 0364:
1790 case 0365:
1791 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001792
Keith Kanios48af1772007-08-17 07:37:52 +00001793 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001795 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001796 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001797 offset += 1;
1798 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001799
Jin Kyu Song03041092013-10-15 19:38:51 -07001800 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0373:
1804 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001805 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 offset += 1;
1807 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001808
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001809 case 0374:
1810 eat = EA_XMMVSIB;
1811 break;
1812
1813 case 0375:
1814 eat = EA_YMMVSIB;
1815 break;
1816
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001817 case 0376:
1818 eat = EA_ZMMVSIB;
1819 break;
1820
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 case4(0100):
1822 case4(0110):
1823 case4(0120):
1824 case4(0130):
1825 case4(0200):
1826 case4(0204):
1827 case4(0210):
1828 case4(0214):
1829 case4(0220):
1830 case4(0224):
1831 case4(0230):
1832 case4(0234):
1833 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 ea ea_data;
1835 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001837 uint8_t *p;
1838 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001840
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001841 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 /* pick rfield from operand b (opx) */
1843 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001844 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 } else {
1846 /* rfield is constant */
1847 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001849 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001851 if (process_ea(opy, &ea_data, bits,
1852 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001854
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 p = bytes;
1856 *p++ = ea_data.modrm;
1857 if (ea_data.sib_present)
1858 *p++ = ea_data.sib;
1859
1860 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001861 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001863 /*
1864 * Make sure the address gets the right offset in case
1865 * the line breaks in the .lst file (BR 1197827)
1866 */
1867 offset += s;
1868 s = 0;
1869
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 switch (ea_data.bytes) {
1871 case 0:
1872 break;
1873 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 case 2:
1875 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001876 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001877 /* use compressed displacement, if available */
1878 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001880 if (ea_data.rip) {
1881 if (opy->segment == segment) {
1882 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001883 if (overflow_signed(data, ea_data.bytes))
1884 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 out(offset, segment, &data, OUT_ADDRESS,
1886 ea_data.bytes, NO_SEG, NO_SEG);
1887 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001888 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 out(offset, segment, &data, OUT_REL4ADR,
1890 insn_end - offset, opy->segment, opy->wrt);
1891 }
1892 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001893 if (overflow_general(data, ins->addr_size >> 3) ||
1894 signed_bits(data, ins->addr_size) !=
1895 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001896 warn_overflow(ERR_PASS2, ea_data.bytes);
1897
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001898 out(offset, segment, &data, OUT_ADDRESS,
1899 ea_data.bytes, opy->segment, opy->wrt);
1900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001902 default:
1903 /* Impossible! */
1904 errfunc(ERR_PANIC,
1905 "Invalid amount of bytes (%d) for offset?!",
1906 ea_data.bytes);
1907 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 }
1909 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001910 }
1911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001912
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 default:
1914 errfunc(ERR_PANIC, "internal instruction table corrupt"
1915 ": instruction code \\%o (0x%02X) given", c, c);
1916 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001918 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001919}
1920
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001921static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001922{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001923 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001924 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001925 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001926}
1927
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001928static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001929{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001930 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001932 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001933}
1934
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001935static int op_rexflags(const operand * o, int mask)
1936{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001937 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001938 int val;
1939
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001940 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001941 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001942
H. Peter Anvina4835d42008-05-20 14:21:29 -07001943 flags = nasm_reg_flags[o->basereg];
1944 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001945
1946 return rexflags(val, flags, mask);
1947}
1948
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001949static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001950{
1951 int rex = 0;
1952
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001953 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001954 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001955 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001956 rex |= REX_W;
1957 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1958 rex |= REX_H;
1959 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1960 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001961
1962 return rex & mask;
1963}
1964
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001965static int evexflags(int val, decoflags_t deco,
1966 int mask, uint8_t byte)
1967{
1968 int evex = 0;
1969
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001970 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001971 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001972 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001973 evex |= (EVEX_P0RP | EVEX_P0X);
1974 break;
1975 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001976 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001977 evex |= EVEX_P2VP;
1978 if (deco & Z)
1979 evex |= EVEX_P2Z;
1980 if (deco & OPMASK_MASK)
1981 evex |= deco & EVEX_P2AAA;
1982 break;
1983 }
1984 return evex & mask;
1985}
1986
1987static int op_evexflags(const operand * o, int mask, uint8_t byte)
1988{
1989 int val;
1990
1991 if (!is_register(o->basereg))
1992 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1993
1994 val = nasm_regvals[o->basereg];
1995
1996 return evexflags(val, o->decoflags, mask, byte);
1997}
1998
H. Peter Anvin23595f52009-07-25 17:44:25 -07001999static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002000 insn *instruction,
2001 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002002{
2003 const struct itemplate *temp;
2004 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002005 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002006 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002007 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002008 int i;
2009
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002010 /* broadcasting uses a different data element size */
2011 for (i = 0; i < instruction->operands; i++)
2012 if (i == broadcast)
2013 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2014 else
2015 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002016
2017 merr = MERR_INVALOP;
2018
2019 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002020 temp->opcode != I_none; temp++) {
2021 m = matches(temp, instruction, bits);
2022 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002023 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002024 m = MOK_GOOD;
2025 else
2026 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002027 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002028 /*
2029 * Missing operand size and a candidate for fuzzy matching...
2030 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002031 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002032 if (i == broadcast)
2033 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2034 else
2035 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002036 opsizemissing = true;
2037 }
2038 if (m > merr)
2039 merr = m;
2040 if (merr == MOK_GOOD)
2041 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002042 }
2043
2044 /* No match, but see if we can get a fuzzy operand size match... */
2045 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002047
2048 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002049 /*
2050 * We ignore extrinsic operand sizes on registers, so we should
2051 * never try to fuzzy-match on them. This also resolves the case
2052 * when we have e.g. "xmmrm128" in two different positions.
2053 */
2054 if (is_class(REGISTER, instruction->oprs[i].type))
2055 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002056
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002057 /* This tests if xsizeflags[i] has more than one bit set */
2058 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2059 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002060
Jin Kyu Song7903c072013-10-30 03:00:12 -07002061 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002062 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002063 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2064 BITS32 : BITS64);
2065 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002066 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002067 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002068 }
2069
2070 /* Try matching again... */
2071 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 temp->opcode != I_none; temp++) {
2073 m = matches(temp, instruction, bits);
2074 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002075 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002076 m = MOK_GOOD;
2077 else
2078 m = MERR_INVALOP;
2079 }
2080 if (m > merr)
2081 merr = m;
2082 if (merr == MOK_GOOD)
2083 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002084 }
2085
H. Peter Anvina81655b2009-07-25 18:15:28 -07002086done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002087 *tempp = temp;
2088 return merr;
2089}
2090
H. Peter Anvin65289e82009-07-25 17:25:11 -07002091static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002092 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002093{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002094 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002095 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002096 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002097
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002098 /*
2099 * Check the opcode
2100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002102 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002103
2104 /*
2105 * Count the operands
2106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002108 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002109
2110 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002111 * Is it legal?
2112 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002113 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002114 return MERR_INVALOP;
2115
2116 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002117 * {evex} available?
2118 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002119 switch (instruction->prefixes[PPS_VEX]) {
2120 case P_EVEX:
2121 if (!itemp_has(itemp, IF_EVEX))
2122 return MERR_ENCMISMATCH;
2123 break;
2124 case P_VEX3:
2125 case P_VEX2:
2126 if (!itemp_has(itemp, IF_VEX))
2127 return MERR_ENCMISMATCH;
2128 break;
2129 default:
2130 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002131 }
2132
2133 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002134 * Check that no spurious colons or TOs are present
2135 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 for (i = 0; i < itemp->operands; i++)
2137 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002138 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002139
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002140 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002141 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002142 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002143 switch (itemp_smask(itemp)) {
2144 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002145 asize = BITS8;
2146 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002147 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002148 asize = BITS16;
2149 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002150 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002151 asize = BITS32;
2152 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002153 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002154 asize = BITS64;
2155 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002156 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002157 asize = BITS128;
2158 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002159 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002160 asize = BITS256;
2161 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002162 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002163 asize = BITS512;
2164 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002165 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002166 switch (bits) {
2167 case 16:
2168 asize = BITS16;
2169 break;
2170 case 32:
2171 asize = BITS32;
2172 break;
2173 case 64:
2174 asize = BITS64;
2175 break;
2176 default:
2177 asize = 0;
2178 break;
2179 }
2180 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002181 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002182 asize = 0;
2183 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002184 }
2185
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002186 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002187 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002188 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002189 memset(size, 0, sizeof size);
2190 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002191 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002192 /* S- flags apply to all operands */
2193 for (i = 0; i < MAX_OPERANDS; i++)
2194 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002195 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002196
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002197 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002198 * Check that the operand flags all match up,
2199 * it's a bit tricky so lets be verbose:
2200 *
2201 * 1) Find out the size of operand. If instruction
2202 * doesn't have one specified -- we're trying to
2203 * guess it either from template (IF_S* flag) or
2204 * from code bits.
2205 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002206 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002207 * template has an operand size specified AND this size differ
2208 * from which instruction has (perhaps we got it from code bits)
2209 * we are:
2210 * a) Check that only size of instruction and operand is differ
2211 * other characteristics do match
2212 * b) Perhaps it's a register specified in instruction so
2213 * for such a case we just mark that operand as "size
2214 * missing" and this will turn on fuzzy operand size
2215 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002216 */
2217 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002219 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002220 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002221 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002222 opflags_t template_opsize, insn_opsize;
2223
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 if (!(type & SIZE_MASK))
2225 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002226
Jin Kyu Song7903c072013-10-30 03:00:12 -07002227 insn_opsize = type & SIZE_MASK;
2228 if (!is_broadcast) {
2229 template_opsize = itemp->opd[i] & SIZE_MASK;
2230 } else {
2231 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2232 /*
2233 * when broadcasting, the element size depends on
2234 * the instruction type. decorator flag should match.
2235 */
2236
2237 if (deco_brsize) {
2238 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002239 /* calculate the proper number : {1to<brcast_num>} */
2240 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2241 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002242 } else {
2243 template_opsize = 0;
2244 }
2245 }
2246
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002247 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002248 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002249 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002250 } else if (template_opsize) {
2251 if (template_opsize != insn_opsize) {
2252 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002253 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002254 } else if (!is_class(REGISTER, type)) {
2255 /*
2256 * Note: we don't honor extrinsic operand sizes for registers,
2257 * so "missing operand size" for a register should be
2258 * considered a wildcard match rather than an error.
2259 */
2260 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002261 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002262 } else if (is_broadcast &&
2263 (brcast_num !=
2264 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2265 /*
2266 * broadcasting opsize matches but the number of repeated memory
2267 * element does not match.
2268 * if 64b double precision float is broadcasted to zmm (512b),
2269 * broadcasting decorator must be {1to8}.
2270 */
2271 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002272 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002273 }
2274 }
2275
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002276 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002277 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002278
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002279 /*
2280 * Check operand sizes
2281 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002282 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2283 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002285 asize = itemp->opd[i] & SIZE_MASK;
2286 if (asize) {
2287 for (i = 0; i < oprs; i++)
2288 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002289 break;
2290 }
2291 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002292 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002294 }
2295
Keith Kaniosb7a89542007-04-12 02:40:54 +00002296 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 if (!(itemp->opd[i] & SIZE_MASK) &&
2298 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002299 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002300 }
2301
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002302 /*
2303 * Check template is okay at the set cpu level
2304 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002305 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002306 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002307
Keith Kaniosb7a89542007-04-12 02:40:54 +00002308 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002309 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002310 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002311 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002312 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002314 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002315 * If we have a HLE prefix, look for the NOHLE flag
2316 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002317 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002318 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2319 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2320 return MERR_BADHLE;
2321
2322 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002323 * Check if special handling needed for Jumps
2324 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002325 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002326 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327
Jin Kyu Song03041092013-10-15 19:38:51 -07002328 /*
2329 * Check if BND prefix is allowed
2330 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002331 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002332 has_prefix(instruction, PPS_REP, P_BND))
2333 return MERR_BADBND;
2334
H. Peter Anvin60926242009-07-26 16:25:38 -07002335 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002336}
2337
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002338/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002339 * Check if ModR/M.mod should/can be 01.
2340 * - EAF_BYTEOFFS is set
2341 * - offset can fit in a byte when EVEX is not used
2342 * - offset can be compressed when EVEX is used
2343 */
2344#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2345 (o >= -128 && o <= 127 && \
2346 seg == NO_SEG && !forw_ref && \
2347 !(input->eaflags & EAF_WORDOFFS) && \
2348 !(ins->rex & REX_EV)) || \
2349 (ins->rex & REX_EV && \
2350 is_disp8n(input, ins, &output->disp8)))
2351
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002352static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002353 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002354{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002355 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002356 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002357
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002358 output->type = EA_SCALAR;
2359 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002360 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002361
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002362 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002363 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002364 /* EVEX.R' flag for the REG operand */
2365 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002366
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002367 if (is_class(REGISTER, input->type)) {
2368 /*
2369 * It's a direct register.
2370 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002371 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002372 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002373
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002374 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002375 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002376
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002377 /* broadcasting is not available with a direct register operand. */
2378 if (input->decoflags & BRDCAST_MASK) {
2379 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2380 goto err;
2381 }
2382
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002383 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002384 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002385 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002386 output->bytes = 0; /* no offset necessary either */
2387 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2388 } else {
2389 /*
2390 * It's a memory reference.
2391 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002392
2393 /* Embedded rounding or SAE is not available with a mem ref operand. */
2394 if (input->decoflags & (ER | SAE)) {
2395 nasm_error(ERR_NONFATAL,
2396 "Embedded rounding is available only with reg-reg op.");
2397 return -1;
2398 }
2399
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002400 if (input->basereg == -1 &&
2401 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002402 /*
2403 * It's a pure offset.
2404 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002405 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2406 input->segment == NO_SEG) {
2407 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2408 input->type &= ~IP_REL;
2409 input->type |= MEMORY;
2410 }
2411
2412 if (input->eaflags & EAF_BYTEOFFS ||
2413 (input->eaflags & EAF_WORDOFFS &&
2414 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2415 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2416 }
2417
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002418 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002419 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002420 output->sib = GEN_SIB(0, 4, 5);
2421 output->bytes = 4;
2422 output->modrm = GEN_MODRM(0, rfield, 4);
2423 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002424 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002425 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 output->bytes = (addrbits != 16 ? 4 : 2);
2427 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2428 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002429 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002430 } else {
2431 /*
2432 * It's an indirection.
2433 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002435 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002437 int t, it, bt; /* register numbers */
2438 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 if (s == 0)
2441 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002442
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002443 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002444 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002445 ix = nasm_reg_flags[i];
2446 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002447 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002448 ix = 0;
2449 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002450
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002451 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002452 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002453 bx = nasm_reg_flags[b];
2454 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002455 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002456 bx = 0;
2457 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002458
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002459 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002460 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002461 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002462 int32_t o = input->offset;
2463 int mod, scale, index, base;
2464
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002465 /*
2466 * For a vector SIB, one has to be a vector and the other,
2467 * if present, a GPR. The vector must be the index operand.
2468 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002470 if (s == 0)
2471 s = 1;
2472 else if (s != 1)
2473 goto err;
2474
2475 t = bt, bt = it, it = t;
2476 x = bx, bx = ix, ix = x;
2477 }
2478
2479 if (bt != -1) {
2480 if (REG_GPR & ~bx)
2481 goto err;
2482 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2483 sok &= bx;
2484 else
2485 goto err;
2486 }
2487
2488 /*
2489 * While we're here, ensure the user didn't specify
2490 * WORD or QWORD
2491 */
2492 if (input->disp_size == 16 || input->disp_size == 64)
2493 goto err;
2494
2495 if (addrbits == 16 ||
2496 (addrbits == 32 && !(sok & BITS32)) ||
2497 (addrbits == 64 && !(sok & BITS64)))
2498 goto err;
2499
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002500 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2501 : ((ix & YMMREG & ~REG_EA)
2502 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002503
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002504 output->rex |= rexflags(it, ix, REX_X);
2505 output->rex |= rexflags(bt, bx, REX_B);
2506 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002507
2508 index = it & 7; /* it is known to be != -1 */
2509
2510 switch (s) {
2511 case 1:
2512 scale = 0;
2513 break;
2514 case 2:
2515 scale = 1;
2516 break;
2517 case 4:
2518 scale = 2;
2519 break;
2520 case 8:
2521 scale = 3;
2522 break;
2523 default: /* then what the smeg is it? */
2524 goto err; /* panic */
2525 }
2526
2527 if (bt == -1) {
2528 base = 5;
2529 mod = 0;
2530 } else {
2531 base = (bt & 7);
2532 if (base != REG_NUM_EBP && o == 0 &&
2533 seg == NO_SEG && !forw_ref &&
2534 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2535 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002536 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002537 mod = 1;
2538 else
2539 mod = 2;
2540 }
2541
2542 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002543 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2544 output->modrm = GEN_MODRM(mod, rfield, 4);
2545 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002546 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 /*
2548 * it must be a 32/64-bit memory reference. Firstly we have
2549 * to check that all registers involved are type E/Rxx.
2550 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002551 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002552 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002553
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002554 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2556 sok &= ix;
2557 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002559 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002560
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002561 if (bt != -1) {
2562 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002563 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002564 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002565 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002566 sok &= bx;
2567 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002568
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002569 /*
2570 * While we're here, ensure the user didn't specify
2571 * WORD or QWORD
2572 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002573 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002575
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002576 if (addrbits == 16 ||
2577 (addrbits == 32 && !(sok & BITS32)) ||
2578 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002579 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002580
Keith Kaniosb7a89542007-04-12 02:40:54 +00002581 /* now reorganize base/index */
2582 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002583 ((hb == b && ht == EAH_NOTBASE) ||
2584 (hb == i && ht == EAH_MAKEBASE))) {
2585 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002586 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002587 x = bx, bx = ix, ix = x;
2588 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002589 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002590 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002591 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002593 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002594 }
2595 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2596 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002597 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002598 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2599 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002600 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002602 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002603 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002604 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002605 x = ix, ix = bx, bx = x;
2606 }
2607 if (it == REG_NUM_ESP ||
2608 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002610
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002611 output->rex |= rexflags(it, ix, REX_X);
2612 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002613
Keith Kanios48af1772007-08-17 07:37:52 +00002614 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002615 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002617
Keith Kaniosb7a89542007-04-12 02:40:54 +00002618 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002621 } else {
2622 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002623 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002624 seg == NO_SEG && !forw_ref &&
2625 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002626 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002627 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002628 mod = 1;
2629 else
2630 mod = 2;
2631 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002632
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002633 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002634 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2635 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002636 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002637 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002639
Keith Kaniosb7a89542007-04-12 02:40:54 +00002640 if (it == -1)
2641 index = 4, s = 1;
2642 else
2643 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 switch (s) {
2646 case 1:
2647 scale = 0;
2648 break;
2649 case 2:
2650 scale = 1;
2651 break;
2652 case 4:
2653 scale = 2;
2654 break;
2655 case 8:
2656 scale = 3;
2657 break;
2658 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002661
Keith Kaniosb7a89542007-04-12 02:40:54 +00002662 if (bt == -1) {
2663 base = 5;
2664 mod = 0;
2665 } else {
2666 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002667 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 seg == NO_SEG && !forw_ref &&
2669 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002670 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002671 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002672 mod = 1;
2673 else
2674 mod = 2;
2675 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002676
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002677 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002678 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2679 output->modrm = GEN_MODRM(mod, rfield, 4);
2680 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 }
2682 } else { /* it's 16-bit */
2683 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002684 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002685
Keith Kaniosb7a89542007-04-12 02:40:54 +00002686 /* check for 64-bit long mode */
2687 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002688 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002689
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2692 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002693 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002694
Keith Kaniosb7a89542007-04-12 02:40:54 +00002695 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002696 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002697 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002698
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002700 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 if (b == -1 && i != -1) {
2702 int tmp = b;
2703 b = i;
2704 i = tmp;
2705 } /* swap */
2706 if ((b == R_SI || b == R_DI) && i != -1) {
2707 int tmp = b;
2708 b = i;
2709 i = tmp;
2710 }
2711 /* have BX/BP as base, SI/DI index */
2712 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002713 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 if (i != -1 && b != -1 &&
2715 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002716 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002717 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002718 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 rm = -1;
2721 if (i != -1)
2722 switch (i * 256 + b) {
2723 case R_SI * 256 + R_BX:
2724 rm = 0;
2725 break;
2726 case R_DI * 256 + R_BX:
2727 rm = 1;
2728 break;
2729 case R_SI * 256 + R_BP:
2730 rm = 2;
2731 break;
2732 case R_DI * 256 + R_BP:
2733 rm = 3;
2734 break;
2735 } else
2736 switch (b) {
2737 case R_SI:
2738 rm = 4;
2739 break;
2740 case R_DI:
2741 rm = 5;
2742 break;
2743 case R_BP:
2744 rm = 6;
2745 break;
2746 case R_BX:
2747 rm = 7;
2748 break;
2749 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002750 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002751 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002752
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002753 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2754 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002756 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 mod = 1;
2758 else
2759 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002760
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002761 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002762 output->bytes = mod; /* bytes of offset needed */
2763 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 }
2765 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002766 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002767
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002768 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002769 return output->type;
2770
2771err:
2772 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002773}
2774
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002775static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002776{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002777 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002778 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002779
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002780 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002781
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002782 switch (ins->prefixes[PPS_ASIZE]) {
2783 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002784 valid &= 16;
2785 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002786 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 valid &= 32;
2788 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002789 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002790 valid &= 64;
2791 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002792 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002793 valid &= (addrbits == 32) ? 16 : 32;
2794 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002795 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002796 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002797 }
2798
2799 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002800 if (is_class(MEMORY, ins->oprs[j].type)) {
2801 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002802
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002803 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002804 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002805 i = 0;
2806 else
2807 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002810 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 b = 0;
2812 else
2813 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002814
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002815 if (ins->oprs[j].scale == 0)
2816 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002817
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002818 if (!i && !b) {
2819 int ds = ins->oprs[j].disp_size;
2820 if ((addrbits != 64 && ds > 8) ||
2821 (addrbits == 64 && ds == 16))
2822 valid &= ds;
2823 } else {
2824 if (!(REG16 & ~b))
2825 valid &= 16;
2826 if (!(REG32 & ~b))
2827 valid &= 32;
2828 if (!(REG64 & ~b))
2829 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002830
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002831 if (!(REG16 & ~i))
2832 valid &= 16;
2833 if (!(REG32 & ~i))
2834 valid &= 32;
2835 if (!(REG64 & ~i))
2836 valid &= 64;
2837 }
2838 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002839 }
2840
2841 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002842 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002843 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002845 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002847 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002848 /* Impossible... */
2849 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2850 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002851 }
2852
2853 defdisp = ins->addr_size == 16 ? 16 : 32;
2854
2855 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002856 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2857 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2858 /*
2859 * mem_offs sizes must match the address size; if not,
2860 * strip the MEM_OFFS bit and match only EA instructions
2861 */
2862 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2863 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002864 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002865}