blob: f06cee828e4670f6a301184030632dd05ed29539 [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;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800388 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
389 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800390 }
391
392 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000394
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400395int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400396 insn * instruction, struct ofmt *output, efunc error,
397 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000398{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000399 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700401 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800402 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000403 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800404 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300405 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000406
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000408 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 outfmt = output; /* likewise */
410 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000411
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300412 wsize = idata_bytes(instruction->opcode);
413 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000415
H. Peter Anvineba20a72002-04-30 20:53:55 +0000416 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000418 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 if (t < 0)
420 errfunc(ERR_PANIC,
421 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000422
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400424 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400426 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700427 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400428 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000429 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700430 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800432 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400433 offset += wsize;
434 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700435 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400436 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000438
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800440 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000442
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 if (align) {
444 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100445 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800446 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 }
448 offset += e->stringlen + align;
449 }
450 }
451 if (t > 0 && t == instruction->times - 1) {
452 /*
453 * Dummy call to list->output to give the offset to the
454 * listing module.
455 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800456 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 list->uplevel(LIST_TIMES);
458 }
459 }
460 if (instruction->times > 1)
461 list->downlevel(LIST_TIMES);
462 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463 }
464
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700466 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400469 fp = fopen(fname, "rb");
470 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
472 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400473 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
475 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200476 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400477 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700478 static char buf[4096];
479 size_t t = instruction->times;
480 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400481 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000482
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 len = ftell(fp);
484 if (instruction->eops->next) {
485 base = instruction->eops->next->offset;
486 len -= base;
487 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700488 len > (size_t)instruction->eops->next->next->offset)
489 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 }
491 /*
492 * Dummy call to list->output to give the offset to the
493 * listing module.
494 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800495 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 list->uplevel(LIST_INCBIN);
497 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700498 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 fseek(fp, base, SEEK_SET);
501 l = len;
502 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700503 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400504 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 if (!m) {
506 /*
507 * This shouldn't happen unless the file
508 * actually changes while we are reading
509 * it.
510 */
511 error(ERR_NONFATAL,
512 "`incbin': unexpected EOF while"
513 " reading file `%s'", fname);
514 t = 0; /* Try to exit cleanly */
515 break;
516 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800517 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 NO_SEG, NO_SEG);
519 l -= m;
520 }
521 }
522 list->downlevel(LIST_INCBIN);
523 if (instruction->times > 1) {
524 /*
525 * Dummy call to list->output to give the offset to the
526 * listing module.
527 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800528 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 list->uplevel(LIST_TIMES);
530 list->downlevel(LIST_TIMES);
531 }
532 fclose(fp);
533 return instruction->times * len;
534 }
535 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000536 }
537
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700538 /* Check to see if we need an address-size prefix */
539 add_asp(instruction, bits);
540
H. Peter Anvin23595f52009-07-25 17:44:25 -0700541 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700542
H. Peter Anvin23595f52009-07-25 17:44:25 -0700543 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400544 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800545 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400546 itimes = instruction->times;
547 if (insn_size < 0) /* shouldn't be, on pass two */
548 error(ERR_PANIC, "errors made it through from pass one");
549 else
550 while (itimes--) {
551 for (j = 0; j < MAXPREFIX; j++) {
552 uint8_t c = 0;
553 switch (instruction->prefixes[j]) {
554 case P_WAIT:
555 c = 0x9B;
556 break;
557 case P_LOCK:
558 c = 0xF0;
559 break;
560 case P_REPNE:
561 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800562 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700563 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400564 c = 0xF2;
565 break;
566 case P_REPE:
567 case P_REPZ:
568 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800569 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400570 c = 0xF3;
571 break;
572 case R_CS:
573 if (bits == 64) {
574 error(ERR_WARNING | ERR_PASS2,
575 "cs segment base generated, but will be ignored in 64-bit mode");
576 }
577 c = 0x2E;
578 break;
579 case R_DS:
580 if (bits == 64) {
581 error(ERR_WARNING | ERR_PASS2,
582 "ds segment base generated, but will be ignored in 64-bit mode");
583 }
584 c = 0x3E;
585 break;
586 case R_ES:
587 if (bits == 64) {
588 error(ERR_WARNING | ERR_PASS2,
589 "es segment base generated, but will be ignored in 64-bit mode");
590 }
591 c = 0x26;
592 break;
593 case R_FS:
594 c = 0x64;
595 break;
596 case R_GS:
597 c = 0x65;
598 break;
599 case R_SS:
600 if (bits == 64) {
601 error(ERR_WARNING | ERR_PASS2,
602 "ss segment base generated, but will be ignored in 64-bit mode");
603 }
604 c = 0x36;
605 break;
606 case R_SEGR6:
607 case R_SEGR7:
608 error(ERR_NONFATAL,
609 "segr6 and segr7 cannot be used as prefixes");
610 break;
611 case P_A16:
612 if (bits == 64) {
613 error(ERR_NONFATAL,
614 "16-bit addressing is not supported "
615 "in 64-bit mode");
616 } else if (bits != 16)
617 c = 0x67;
618 break;
619 case P_A32:
620 if (bits != 32)
621 c = 0x67;
622 break;
623 case P_A64:
624 if (bits != 64) {
625 error(ERR_NONFATAL,
626 "64-bit addressing is only supported "
627 "in 64-bit mode");
628 }
629 break;
630 case P_ASP:
631 c = 0x67;
632 break;
633 case P_O16:
634 if (bits != 16)
635 c = 0x66;
636 break;
637 case P_O32:
638 if (bits == 16)
639 c = 0x66;
640 break;
641 case P_O64:
642 /* REX.W */
643 break;
644 case P_OSP:
645 c = 0x66;
646 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700647 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800648 case P_VEX3:
649 case P_VEX2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400650 case P_none:
651 break;
652 default:
653 error(ERR_PANIC, "invalid instruction prefix");
654 }
655 if (c != 0) {
656 out(offset, segment, &c, OUT_RAWDATA, 1,
657 NO_SEG, NO_SEG);
658 offset++;
659 }
660 }
661 insn_end = offset + insn_size;
662 gencode(segment, offset, bits, instruction,
663 temp, insn_end);
664 offset += insn_size;
665 if (itimes > 0 && itimes == instruction->times - 1) {
666 /*
667 * Dummy call to list->output to give the offset to the
668 * listing module.
669 */
670 list->output(offset, NULL, OUT_RAWDATA, 0);
671 list->uplevel(LIST_TIMES);
672 }
673 }
674 if (instruction->times > 1)
675 list->downlevel(LIST_TIMES);
676 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700677 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400678 /* No match */
679 switch (m) {
680 case MERR_OPSIZEMISSING:
681 error(ERR_NONFATAL, "operation size not specified");
682 break;
683 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400685 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700686 case MERR_BRNUMMISMATCH:
687 error(ERR_NONFATAL,
688 "mismatch in the number of broadcasting elements");
689 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400692 break;
693 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800694 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400695 bits);
696 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800697 case MERR_ENCMISMATCH:
698 error(ERR_NONFATAL, "specific encoding scheme not available");
699 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800700 case MERR_BADBND:
701 error(ERR_NONFATAL, "bnd prefix is not allowed");
702 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400703 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 error(ERR_NONFATAL,
705 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400706 break;
707 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000708 }
709 return 0;
710}
711
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400712int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400713 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000715 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700716 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000717
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000719 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000720
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400721 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700724 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
725 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400726 instruction->opcode == I_DT || instruction->opcode == I_DO ||
727 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300729 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300732 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400734 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000735 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400738 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400740 warn_overflow_const(e->offset, wsize);
741 } else if (e->type == EOT_DB_STRING ||
742 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000744
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 align = (-osize) % wsize;
746 if (align < 0)
747 align += wsize;
748 isize += osize + align;
749 }
750 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751 }
752
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400754 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300756 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700757 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000758
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400759 fp = fopen(fname, "rb");
760 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
762 fname);
763 else if (fseek(fp, 0L, SEEK_END) < 0)
764 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
765 fname);
766 else {
767 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 if (instruction->eops->next) {
769 len -= instruction->eops->next->offset;
770 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700771 len > (size_t)instruction->eops->next->next->offset) {
772 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 }
774 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300775 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300777 if (fp)
778 fclose(fp);
779 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000780 }
781
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700782 /* Check to see if we need an address-size prefix */
783 add_asp(instruction, bits);
784
H. Peter Anvin23595f52009-07-25 17:44:25 -0700785 m = find_match(&temp, instruction, segment, offset, bits);
786 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400787 /* we've matched an instruction. */
788 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400789 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100790
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800791 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400792 if (isize < 0)
793 return -1;
794 for (j = 0; j < MAXPREFIX; j++) {
795 switch (instruction->prefixes[j]) {
796 case P_A16:
797 if (bits != 16)
798 isize++;
799 break;
800 case P_A32:
801 if (bits != 32)
802 isize++;
803 break;
804 case P_O16:
805 if (bits != 16)
806 isize++;
807 break;
808 case P_O32:
809 if (bits == 16)
810 isize++;
811 break;
812 case P_A64:
813 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700814 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800815 case P_VEX3:
816 case P_VEX2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400817 case P_none:
818 break;
819 default:
820 isize++;
821 break;
822 }
823 }
824 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700825 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400826 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000827 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000828}
829
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800830static void bad_hle_warn(const insn * ins, uint8_t hleok)
831{
832 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800833 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800834 static const enum whatwarn warn[2][4] =
835 {
836 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
837 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
838 };
839 unsigned int n;
840
841 n = (unsigned int)rep_pfx - P_XACQUIRE;
842 if (n > 1)
843 return; /* Not XACQUIRE/XRELEASE */
844
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800845 ww = warn[n][hleok];
846 if (!is_class(MEMORY, ins->oprs[0].type))
847 ww = w_inval; /* HLE requires operand 0 to be memory */
848
849 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800850 case w_none:
851 break;
852
853 case w_lock:
854 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800855 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800856 "%s with this instruction requires lock",
857 prefix_name(rep_pfx));
858 }
859 break;
860
861 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800862 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800863 "%s invalid with this instruction",
864 prefix_name(rep_pfx));
865 break;
866 }
867}
868
H. Peter Anvin507ae032008-10-09 15:37:10 -0700869/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400870#define case3(x) case (x): case (x)+1: case (x)+2
871#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700872
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800873static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800874 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000875{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800876 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800877 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000878 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000879 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700880 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700881 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700882 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700883 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800884 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700886 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000887
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700888 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700889 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700890 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700891
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700892 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400893 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700894
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 (void)segment; /* Don't warn that this parameter is unused */
896 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000897
H. Peter Anvin839eca22007-10-29 23:12:47 -0700898 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400899 c = *codes++;
900 op1 = (c & 3) + ((opex & 1) << 2);
901 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
902 opx = &ins->oprs[op1];
903 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700904
H. Peter Anvin839eca22007-10-29 23:12:47 -0700905 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400906 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 codes += c, length += c;
908 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700909
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400910 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400911 opex = c;
912 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400914 case4(010):
915 ins->rex |=
916 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 codes++, length++;
918 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700919
Jin Kyu Song164d6072013-10-15 19:10:13 -0700920 case4(014):
921 /* this is an index reg of MIB operand */
922 mib_index = opx->basereg;
923 break;
924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 case4(020):
926 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 length++;
928 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400930 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 length += 2;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700935 if (opx->type & (BITS16 | BITS32 | BITS64))
936 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 else
938 length += (bits == 16) ? 2 : 4;
939 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 length += 4;
943 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700944
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400945 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700946 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400949 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 length++;
951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000954 length += 8; /* MOV reg64/imm */
955 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 length += 2;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700962 if (opx->type & (BITS16 | BITS32 | BITS64))
963 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 else
965 length += (bits == 16) ? 2 : 4;
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 length += 4;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700973 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case 0172:
977 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400978 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700979 length++;
980 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700981
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700982 case4(0174):
983 length++;
984 break;
985
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700986 case4(0240):
987 ins->rex |= REX_EV;
988 ins->vexreg = regval(opx);
989 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
990 ins->vex_cm = *codes++;
991 ins->vex_wlp = *codes++;
992 ins->evex_tuple = (*codes++ - 0300);
993 break;
994
995 case 0250:
996 ins->rex |= REX_EV;
997 ins->vexreg = 0;
998 ins->vex_cm = *codes++;
999 ins->vex_wlp = *codes++;
1000 ins->evex_tuple = (*codes++ - 0300);
1001 break;
1002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(0254):
1004 length += 4;
1005 break;
1006
1007 case4(0260):
1008 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001009 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 ins->vex_cm = *codes++;
1011 ins->vex_wlp = *codes++;
1012 break;
1013
1014 case 0270:
1015 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001016 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 ins->vex_cm = *codes++;
1018 ins->vex_wlp = *codes++;
1019 break;
1020
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001021 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001022 hleok = c & 3;
1023 break;
1024
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001025 case4(0274):
1026 length++;
1027 break;
1028
1029 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001031
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001033 if (bits == 64)
1034 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001035 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001039 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001041
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001043 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001044
Keith Kaniosb7a89542007-04-12 02:40:54 +00001045 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001046 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1047 has_prefix(ins, PPS_ASIZE, P_A32))
1048 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001050
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 case4(0314):
1052 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001055 {
1056 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1057 if (pfx == P_O16)
1058 break;
1059 if (pfx != P_none)
1060 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1061 else
1062 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001064 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001067 {
1068 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1069 if (pfx == P_O32)
1070 break;
1071 if (pfx != P_none)
1072 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1073 else
1074 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001076 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0322:
1079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
Keith Kaniosb7a89542007-04-12 02:40:54 +00001081 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001082 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
Keith Kaniosb7a89542007-04-12 02:40:54 +00001085 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001086 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001087 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001089 case 0325:
1090 ins->rex |= REX_NH;
1091 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001092
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001093 case 0326:
1094 break;
1095
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 case 0330:
1097 codes++, length++;
1098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001103 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 case 0333:
1105 length++;
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001108 case 0334:
1109 ins->rex |= REX_L;
1110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001112 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001114
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001115 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001116 if (!ins->prefixes[PPS_REP])
1117 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001120 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001121 if (!ins->prefixes[PPS_REP])
1122 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 if (ins->oprs[0].segment != NO_SEG)
1127 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1128 " quantity of BSS space");
1129 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001130 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001132
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001133 case 0341:
1134 if (!ins->prefixes[PPS_WAIT])
1135 ins->prefixes[PPS_WAIT] = P_WAIT;
1136 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 case 0360:
1139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001141 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001142 length++;
1143 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001144
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 case 0364:
1146 case 0365:
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Keith Kanios48af1772007-08-17 07:37:52 +00001149 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001150 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 length++;
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001154 case 0370:
1155 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 case 0373:
1159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001162 case 0374:
1163 eat = EA_XMMVSIB;
1164 break;
1165
1166 case 0375:
1167 eat = EA_YMMVSIB;
1168 break;
1169
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001170 case 0376:
1171 eat = EA_ZMMVSIB;
1172 break;
1173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case4(0100):
1175 case4(0110):
1176 case4(0120):
1177 case4(0130):
1178 case4(0200):
1179 case4(0204):
1180 case4(0210):
1181 case4(0214):
1182 case4(0220):
1183 case4(0224):
1184 case4(0230):
1185 case4(0234):
1186 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001188 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001189 opflags_t rflags;
1190 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001191 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001192
Keith Kaniosb7a89542007-04-12 02:40:54 +00001193 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 if (c <= 0177) {
1196 /* pick rfield from operand b (opx) */
1197 rflags = regflag(opx);
1198 rfield = nasm_regvals[opx->basereg];
1199 } else {
1200 rflags = 0;
1201 rfield = c & 7;
1202 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001203
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001204 /* EVEX.b1 : evex_brerop contains the operand position */
1205 op_er_sae = (ins->evex_brerop >= 0 ?
1206 &ins->oprs[ins->evex_brerop] : NULL);
1207
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001208 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1209 /* set EVEX.b */
1210 ins->evex_p[2] |= EVEX_P2B;
1211 if (op_er_sae->decoflags & ER) {
1212 /* set EVEX.RC (rounding control) */
1213 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1214 & EVEX_P2RC;
1215 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001216 } else {
1217 /* set EVEX.L'L (vector length) */
1218 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001219 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001220 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001221 /* set EVEX.b */
1222 ins->evex_p[2] |= EVEX_P2B;
1223 }
1224 }
1225
Jin Kyu Song164d6072013-10-15 19:10:13 -07001226 /*
1227 * if a separate form of MIB (ICC style) is used,
1228 * the index reg info is merged into mem operand
1229 */
1230 if (mib_index != R_none) {
1231 opy->indexreg = mib_index;
1232 opy->scale = 1;
1233 opy->hintbase = mib_index;
1234 opy->hinttype = EAH_NOTBASE;
1235 }
1236
Jin Kyu Song3b653232013-11-08 11:41:12 -08001237 /*
1238 * only for mib operands, make a single reg index [reg*1].
1239 * gas uses this form to explicitly denote index register.
1240 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001241 if (itemp_has(temp, IF_MIB) &&
Jin Kyu Song3b653232013-11-08 11:41:12 -08001242 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1243 opy->hinttype == EAH_NOTBASE)) {
1244 opy->indexreg = opy->basereg;
1245 opy->basereg = -1;
1246 opy->scale = 1;
1247 }
1248
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001249 if (process_ea(opy, &ea_data, bits,
1250 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 errfunc(ERR_NONFATAL, "invalid effective address");
1252 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001253 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001254 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001256 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 }
1258 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001259
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 default:
1261 errfunc(ERR_PANIC, "internal instruction table corrupt"
1262 ": instruction code \\%o (0x%02X) given", c, c);
1263 break;
1264 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001265 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001266
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001267 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001268
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001269 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 if (ins->rex & REX_H) {
1271 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1272 return -1;
1273 }
1274 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001275 }
1276
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001277 switch (ins->prefixes[PPS_VEX]) {
1278 case P_EVEX:
1279 if (!(ins->rex & REX_EV))
1280 return -1;
1281 break;
1282 case P_VEX3:
1283 case P_VEX2:
1284 if (!(ins->rex & REX_V))
1285 return -1;
1286 break;
1287 default:
1288 break;
1289 }
1290
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001291 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001293
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001295 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 return -1;
1297 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001298 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001300 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 ins->rex &= ~REX_W;
1302 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001303 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 ins->rex |= REX_W;
1305 bad32 &= ~REX_W;
1306 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001307 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 /* Follow REX_W */
1309 break;
1310 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001311
H. Peter Anvinfc561202011-07-07 16:58:22 -07001312 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1314 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001315 } else if (!(ins->rex & REX_EV) &&
1316 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1317 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1318 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001320 if (ins->rex & REX_EV)
1321 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001322 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1323 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001324 length += 3;
1325 else
1326 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001327 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 if (ins->rex & REX_H) {
1329 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1330 return -1;
1331 } else if (bits == 64) {
1332 length++;
1333 } else if ((ins->rex & REX_L) &&
1334 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001335 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001337 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001338 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 length++;
1340 } else {
1341 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1342 return -1;
1343 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001344 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001345
1346 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001347 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001348 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001349 "instruction is not lockable");
1350 }
1351
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001352 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001353
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001354 return length;
1355}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001356
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001357static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1358{
1359 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001360 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001361 ins->rex = (ins->rex & REX_REAL) | REX_P;
1362 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1363 ins->rex = 0;
1364 return 1;
1365 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001366 }
1367
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001368 return 0;
1369}
1370
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001371static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001372 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001375 uint8_t c;
1376 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001377 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001378 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001379 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001380 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001381 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001382 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001383 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001384
H. Peter Anvin839eca22007-10-29 23:12:47 -07001385 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 c = *codes++;
1387 op1 = (c & 3) + ((opex & 1) << 2);
1388 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1389 opx = &ins->oprs[op1];
1390 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001391
H. Peter Anvin839eca22007-10-29 23:12:47 -07001392 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 case 01:
1394 case 02:
1395 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001396 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001397 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001398 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 codes += c;
1400 offset += c;
1401 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001402
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001403 case 05:
1404 case 06:
1405 case 07:
1406 opex = c;
1407 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001408
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001409 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001410 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001411 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001412 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 offset += 1;
1414 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001415
Jin Kyu Song164d6072013-10-15 19:10:13 -07001416 case4(014):
1417 break;
1418
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001419 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001420 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001421 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001422 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001424 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 offset += 1;
1426 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001427
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001428 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001429 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001430 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001431 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001432 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 offset += 1;
1434 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001435
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001436 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001437 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001438 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001439 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001440 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 offset += 2;
1442 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001444 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001445 if (opx->type & (BITS16 | BITS32))
1446 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 else
1448 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001449 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001450 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001451 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001452 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 offset += size;
1454 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001455
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001457 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001458 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001459 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001460 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 offset += 4;
1462 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001463
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001464 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001465 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001466 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001467 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001468 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001469 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 offset += size;
1471 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001473 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001474 if (opx->segment != segment) {
1475 data = opx->offset;
1476 out(offset, segment, &data,
1477 OUT_REL1ADR, insn_end - offset,
1478 opx->segment, opx->wrt);
1479 } else {
1480 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001481 if (data > 127 || data < -128)
1482 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001483 out(offset, segment, &data,
1484 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1485 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 offset += 1;
1487 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001488
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001489 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001490 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001491 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001492 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001493 offset += 8;
1494 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001495
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001497 if (opx->segment != segment) {
1498 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001500 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001501 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001503 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001505 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 }
1507 offset += 2;
1508 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001509
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001511 if (opx->type & (BITS16 | BITS32 | BITS64))
1512 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 else
1514 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001515 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001516 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001517 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001518 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1519 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001521 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001523 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 }
1525 offset += size;
1526 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001529 if (opx->segment != segment) {
1530 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001532 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001533 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001535 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001537 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 }
1539 offset += 4;
1540 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001541
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001543 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001544 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1545 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001546 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001547 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001548 outfmt->segbase(1 + opx->segment),
1549 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001550 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001552
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 case 0172:
1554 c = *codes++;
1555 opx = &ins->oprs[c >> 3];
1556 bytes[0] = nasm_regvals[opx->basereg] << 4;
1557 opx = &ins->oprs[c & 7];
1558 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1559 errfunc(ERR_NONFATAL,
1560 "non-absolute expression not permitted as argument %d",
1561 c & 7);
1562 } else {
1563 if (opx->offset & ~15) {
1564 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1565 "four-bit argument exceeds bounds");
1566 }
1567 bytes[0] |= opx->offset & 15;
1568 }
1569 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1570 offset++;
1571 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001572
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001573 case 0173:
1574 c = *codes++;
1575 opx = &ins->oprs[c >> 4];
1576 bytes[0] = nasm_regvals[opx->basereg] << 4;
1577 bytes[0] |= c & 15;
1578 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1579 offset++;
1580 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001581
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001582 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001583 bytes[0] = nasm_regvals[opx->basereg] << 4;
1584 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1585 offset++;
1586 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001589 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001590 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1591 (int32_t)data != (int64_t)data) {
1592 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1593 "signed dword immediate exceeds bounds");
1594 }
1595 out(offset, segment, &data, OUT_ADDRESS, 4,
1596 opx->segment, opx->wrt);
1597 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001598 break;
1599
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001600 case4(0240):
1601 case 0250:
1602 codes += 3;
1603 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1604 EVEX_P2Z | EVEX_P2AAA, 2);
1605 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1606 bytes[0] = 0x62;
1607 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001608 bytes[1] = ((((ins->rex & 7) << 5) |
1609 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1610 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001611 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1612 ((~ins->vexreg & 15) << 3) |
1613 (1 << 2) | (ins->vex_wlp & 3);
1614 bytes[3] = ins->evex_p[2];
1615 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1616 offset += 4;
1617 break;
1618
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001619 case4(0260):
1620 case 0270:
1621 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001622 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1623 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1625 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1626 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001627 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1629 offset += 3;
1630 } else {
1631 bytes[0] = 0xc5;
1632 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001633 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1635 offset += 2;
1636 }
1637 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001638
H. Peter Anvine014f352012-02-25 22:35:19 -08001639 case 0271:
1640 case 0272:
1641 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001642 break;
1643
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 case4(0274):
1645 {
1646 uint64_t uv, um;
1647 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 if (ins->rex & REX_W)
1650 s = 64;
1651 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1652 s = 16;
1653 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1654 s = 32;
1655 else
1656 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 um = (uint64_t)2 << (s-1);
1659 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 if (uv > 127 && uv < (uint64_t)-128 &&
1662 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001663 /* If this wasn't explicitly byte-sized, warn as though we
1664 * had fallen through to the imm16/32/64 case.
1665 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001666 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001667 "%s value exceeds bounds",
1668 (opx->type & BITS8) ? "signed byte" :
1669 s == 16 ? "word" :
1670 s == 32 ? "dword" :
1671 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001673 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001674 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001675 out(offset, segment, &data, OUT_ADDRESS, 1,
1676 opx->segment, opx->wrt);
1677 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001678 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001679 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1680 NO_SEG);
1681 }
1682 offset += 1;
1683 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001685
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001686 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001688
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001690 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001692 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 offset += 1;
1694 } else
1695 offset += 0;
1696 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001697
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001699 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001701 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 offset += 1;
1703 } else
1704 offset += 0;
1705 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 case 0312:
1708 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001709
Keith Kaniosb7a89542007-04-12 02:40:54 +00001710 case 0313:
1711 ins->rex = 0;
1712 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001713
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001714 case4(0314):
1715 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001720
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001722 case 0323:
1723 break;
1724
Keith Kaniosb7a89542007-04-12 02:40:54 +00001725 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001726 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 case 0325:
1730 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001731
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001732 case 0326:
1733 break;
1734
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001736 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001737 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 offset += 1;
1739 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001740
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001743
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001746 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001747 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 offset += 1;
1749 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750
Keith Kanios48af1772007-08-17 07:37:52 +00001751 case 0334:
1752 if (ins->rex & REX_R) {
1753 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001754 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001755 offset += 1;
1756 }
1757 ins->rex &= ~(REX_L|REX_R);
1758 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001759
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001760 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001762
H. Peter Anvin962e3052008-08-28 17:47:16 -07001763 case 0336:
1764 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001765 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 if (ins->oprs[0].segment != NO_SEG)
1769 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1770 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001771 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 if (size > 0)
1773 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001774 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 offset += size;
1776 }
1777 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001778
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001779 case 0341:
1780 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001781
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001782 case 0360:
1783 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001784
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001785 case 0361:
1786 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001787 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1788 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001790
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001791 case 0364:
1792 case 0365:
1793 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001794
Keith Kanios48af1772007-08-17 07:37:52 +00001795 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001797 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001798 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001799 offset += 1;
1800 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001801
Jin Kyu Song03041092013-10-15 19:38:51 -07001802 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001804
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case 0373:
1806 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001807 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 offset += 1;
1809 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001811 case 0374:
1812 eat = EA_XMMVSIB;
1813 break;
1814
1815 case 0375:
1816 eat = EA_YMMVSIB;
1817 break;
1818
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001819 case 0376:
1820 eat = EA_ZMMVSIB;
1821 break;
1822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 case4(0100):
1824 case4(0110):
1825 case4(0120):
1826 case4(0130):
1827 case4(0200):
1828 case4(0204):
1829 case4(0210):
1830 case4(0214):
1831 case4(0220):
1832 case4(0224):
1833 case4(0230):
1834 case4(0234):
1835 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 ea ea_data;
1837 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001839 uint8_t *p;
1840 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001842
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001843 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 /* pick rfield from operand b (opx) */
1845 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001846 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001847 } else {
1848 /* rfield is constant */
1849 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001853 if (process_ea(opy, &ea_data, bits,
1854 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001856
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 p = bytes;
1858 *p++ = ea_data.modrm;
1859 if (ea_data.sib_present)
1860 *p++ = ea_data.sib;
1861
1862 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001863 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001865 /*
1866 * Make sure the address gets the right offset in case
1867 * the line breaks in the .lst file (BR 1197827)
1868 */
1869 offset += s;
1870 s = 0;
1871
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 switch (ea_data.bytes) {
1873 case 0:
1874 break;
1875 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 case 2:
1877 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001878 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001879 /* use compressed displacement, if available */
1880 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001882 if (ea_data.rip) {
1883 if (opy->segment == segment) {
1884 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001885 if (overflow_signed(data, ea_data.bytes))
1886 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001887 out(offset, segment, &data, OUT_ADDRESS,
1888 ea_data.bytes, NO_SEG, NO_SEG);
1889 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001890 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 out(offset, segment, &data, OUT_REL4ADR,
1892 insn_end - offset, opy->segment, opy->wrt);
1893 }
1894 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001895 if (overflow_general(data, ins->addr_size >> 3) ||
1896 signed_bits(data, ins->addr_size) !=
1897 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001898 warn_overflow(ERR_PASS2, ea_data.bytes);
1899
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 out(offset, segment, &data, OUT_ADDRESS,
1901 ea_data.bytes, opy->segment, opy->wrt);
1902 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001904 default:
1905 /* Impossible! */
1906 errfunc(ERR_PANIC,
1907 "Invalid amount of bytes (%d) for offset?!",
1908 ea_data.bytes);
1909 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 }
1911 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 }
1913 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 default:
1916 errfunc(ERR_PANIC, "internal instruction table corrupt"
1917 ": instruction code \\%o (0x%02X) given", c, c);
1918 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001920 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001921}
1922
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001923static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001924{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001925 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001926 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001927 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001928}
1929
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001930static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001931{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001932 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001934 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001935}
1936
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001937static int op_rexflags(const operand * o, int mask)
1938{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001939 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001940 int val;
1941
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001942 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001943 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001944
H. Peter Anvina4835d42008-05-20 14:21:29 -07001945 flags = nasm_reg_flags[o->basereg];
1946 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947
1948 return rexflags(val, flags, mask);
1949}
1950
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001951static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001952{
1953 int rex = 0;
1954
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001955 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001956 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001957 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001958 rex |= REX_W;
1959 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1960 rex |= REX_H;
1961 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1962 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001963
1964 return rex & mask;
1965}
1966
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001967static int evexflags(int val, decoflags_t deco,
1968 int mask, uint8_t byte)
1969{
1970 int evex = 0;
1971
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001972 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001973 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001974 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001975 evex |= (EVEX_P0RP | EVEX_P0X);
1976 break;
1977 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001978 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001979 evex |= EVEX_P2VP;
1980 if (deco & Z)
1981 evex |= EVEX_P2Z;
1982 if (deco & OPMASK_MASK)
1983 evex |= deco & EVEX_P2AAA;
1984 break;
1985 }
1986 return evex & mask;
1987}
1988
1989static int op_evexflags(const operand * o, int mask, uint8_t byte)
1990{
1991 int val;
1992
1993 if (!is_register(o->basereg))
1994 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1995
1996 val = nasm_regvals[o->basereg];
1997
1998 return evexflags(val, o->decoflags, mask, byte);
1999}
2000
H. Peter Anvin23595f52009-07-25 17:44:25 -07002001static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002002 insn *instruction,
2003 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002004{
2005 const struct itemplate *temp;
2006 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002007 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002008 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002009 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002010 int i;
2011
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002012 /* broadcasting uses a different data element size */
2013 for (i = 0; i < instruction->operands; i++)
2014 if (i == broadcast)
2015 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2016 else
2017 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002018
2019 merr = MERR_INVALOP;
2020
2021 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002022 temp->opcode != I_none; temp++) {
2023 m = matches(temp, instruction, bits);
2024 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002025 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002026 m = MOK_GOOD;
2027 else
2028 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002029 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002030 /*
2031 * Missing operand size and a candidate for fuzzy matching...
2032 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002033 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002034 if (i == broadcast)
2035 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2036 else
2037 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002038 opsizemissing = true;
2039 }
2040 if (m > merr)
2041 merr = m;
2042 if (merr == MOK_GOOD)
2043 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002044 }
2045
2046 /* No match, but see if we can get a fuzzy operand size match... */
2047 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002049
2050 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002051 /*
2052 * We ignore extrinsic operand sizes on registers, so we should
2053 * never try to fuzzy-match on them. This also resolves the case
2054 * when we have e.g. "xmmrm128" in two different positions.
2055 */
2056 if (is_class(REGISTER, instruction->oprs[i].type))
2057 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002058
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 /* This tests if xsizeflags[i] has more than one bit set */
2060 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2061 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002062
Jin Kyu Song7903c072013-10-30 03:00:12 -07002063 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002064 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002065 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2066 BITS32 : BITS64);
2067 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002068 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002069 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002070 }
2071
2072 /* Try matching again... */
2073 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002074 temp->opcode != I_none; temp++) {
2075 m = matches(temp, instruction, bits);
2076 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002077 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002078 m = MOK_GOOD;
2079 else
2080 m = MERR_INVALOP;
2081 }
2082 if (m > merr)
2083 merr = m;
2084 if (merr == MOK_GOOD)
2085 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002086 }
2087
H. Peter Anvina81655b2009-07-25 18:15:28 -07002088done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002089 *tempp = temp;
2090 return merr;
2091}
2092
H. Peter Anvin65289e82009-07-25 17:25:11 -07002093static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002094 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002095{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002096 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002097 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002098 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002099
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002100 /*
2101 * Check the opcode
2102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002104 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002105
2106 /*
2107 * Count the operands
2108 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002110 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002111
2112 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002113 * Is it legal?
2114 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002115 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002116 return MERR_INVALOP;
2117
2118 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002119 * {evex} available?
2120 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002121 switch (instruction->prefixes[PPS_VEX]) {
2122 case P_EVEX:
2123 if (!itemp_has(itemp, IF_EVEX))
2124 return MERR_ENCMISMATCH;
2125 break;
2126 case P_VEX3:
2127 case P_VEX2:
2128 if (!itemp_has(itemp, IF_VEX))
2129 return MERR_ENCMISMATCH;
2130 break;
2131 default:
2132 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002133 }
2134
2135 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002136 * Check that no spurious colons or TOs are present
2137 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138 for (i = 0; i < itemp->operands; i++)
2139 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002140 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002141
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002142 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002143 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002144 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002145 switch (itemp_smask(itemp)) {
2146 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002147 asize = BITS8;
2148 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002149 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002150 asize = BITS16;
2151 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002152 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002153 asize = BITS32;
2154 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002155 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002156 asize = BITS64;
2157 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002158 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002159 asize = BITS128;
2160 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002161 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002162 asize = BITS256;
2163 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002164 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002165 asize = BITS512;
2166 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002167 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002168 switch (bits) {
2169 case 16:
2170 asize = BITS16;
2171 break;
2172 case 32:
2173 asize = BITS32;
2174 break;
2175 case 64:
2176 asize = BITS64;
2177 break;
2178 default:
2179 asize = 0;
2180 break;
2181 }
2182 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002183 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002184 asize = 0;
2185 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002186 }
2187
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002188 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002189 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002190 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002191 memset(size, 0, sizeof size);
2192 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002193 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002194 /* S- flags apply to all operands */
2195 for (i = 0; i < MAX_OPERANDS; i++)
2196 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002197 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002198
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002199 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002200 * Check that the operand flags all match up,
2201 * it's a bit tricky so lets be verbose:
2202 *
2203 * 1) Find out the size of operand. If instruction
2204 * doesn't have one specified -- we're trying to
2205 * guess it either from template (IF_S* flag) or
2206 * from code bits.
2207 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002208 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 * template has an operand size specified AND this size differ
2210 * from which instruction has (perhaps we got it from code bits)
2211 * we are:
2212 * a) Check that only size of instruction and operand is differ
2213 * other characteristics do match
2214 * b) Perhaps it's a register specified in instruction so
2215 * for such a case we just mark that operand as "size
2216 * missing" and this will turn on fuzzy operand size
2217 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002218 */
2219 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002220 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002221 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002222 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002223 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002224 opflags_t template_opsize, insn_opsize;
2225
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002226 if (!(type & SIZE_MASK))
2227 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002228
Jin Kyu Song7903c072013-10-30 03:00:12 -07002229 insn_opsize = type & SIZE_MASK;
2230 if (!is_broadcast) {
2231 template_opsize = itemp->opd[i] & SIZE_MASK;
2232 } else {
2233 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2234 /*
2235 * when broadcasting, the element size depends on
2236 * the instruction type. decorator flag should match.
2237 */
2238
2239 if (deco_brsize) {
2240 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002241 /* calculate the proper number : {1to<brcast_num>} */
2242 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2243 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002244 } else {
2245 template_opsize = 0;
2246 }
2247 }
2248
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002249 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002250 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002251 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002252 } else if (template_opsize) {
2253 if (template_opsize != insn_opsize) {
2254 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002255 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002256 } else if (!is_class(REGISTER, type)) {
2257 /*
2258 * Note: we don't honor extrinsic operand sizes for registers,
2259 * so "missing operand size" for a register should be
2260 * considered a wildcard match rather than an error.
2261 */
2262 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002263 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002264 } else if (is_broadcast &&
2265 (brcast_num !=
2266 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2267 /*
2268 * broadcasting opsize matches but the number of repeated memory
2269 * element does not match.
2270 * if 64b double precision float is broadcasted to zmm (512b),
2271 * broadcasting decorator must be {1to8}.
2272 */
2273 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002274 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002275 }
2276 }
2277
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002278 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002279 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002280
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002281 /*
2282 * Check operand sizes
2283 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002284 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2285 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002287 asize = itemp->opd[i] & SIZE_MASK;
2288 if (asize) {
2289 for (i = 0; i < oprs; i++)
2290 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 break;
2292 }
2293 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002294 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002296 }
2297
Keith Kaniosb7a89542007-04-12 02:40:54 +00002298 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 if (!(itemp->opd[i] & SIZE_MASK) &&
2300 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002301 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002302 }
2303
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002304 /*
2305 * Check template is okay at the set cpu level
2306 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002307 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002308 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002309
Keith Kaniosb7a89542007-04-12 02:40:54 +00002310 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002311 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002312 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002313 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002314 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002316 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002317 * If we have a HLE prefix, look for the NOHLE flag
2318 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002319 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002320 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2321 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2322 return MERR_BADHLE;
2323
2324 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002325 * Check if special handling needed for Jumps
2326 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002327 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002328 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329
Jin Kyu Song03041092013-10-15 19:38:51 -07002330 /*
2331 * Check if BND prefix is allowed
2332 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002333 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002334 has_prefix(instruction, PPS_REP, P_BND))
2335 return MERR_BADBND;
2336
H. Peter Anvin60926242009-07-26 16:25:38 -07002337 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002338}
2339
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002340/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002341 * Check if ModR/M.mod should/can be 01.
2342 * - EAF_BYTEOFFS is set
2343 * - offset can fit in a byte when EVEX is not used
2344 * - offset can be compressed when EVEX is used
2345 */
2346#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2347 (o >= -128 && o <= 127 && \
2348 seg == NO_SEG && !forw_ref && \
2349 !(input->eaflags & EAF_WORDOFFS) && \
2350 !(ins->rex & REX_EV)) || \
2351 (ins->rex & REX_EV && \
2352 is_disp8n(input, ins, &output->disp8)))
2353
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002354static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002355 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002356{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002357 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002358 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002359
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002360 output->type = EA_SCALAR;
2361 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002362 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002363
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002364 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002365 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002366 /* EVEX.R' flag for the REG operand */
2367 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002368
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002369 if (is_class(REGISTER, input->type)) {
2370 /*
2371 * It's a direct register.
2372 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002373 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002374 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002375
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002376 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002377 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002378
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002379 /* broadcasting is not available with a direct register operand. */
2380 if (input->decoflags & BRDCAST_MASK) {
2381 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2382 goto err;
2383 }
2384
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002386 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002387 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002388 output->bytes = 0; /* no offset necessary either */
2389 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2390 } else {
2391 /*
2392 * It's a memory reference.
2393 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002394
2395 /* Embedded rounding or SAE is not available with a mem ref operand. */
2396 if (input->decoflags & (ER | SAE)) {
2397 nasm_error(ERR_NONFATAL,
2398 "Embedded rounding is available only with reg-reg op.");
2399 return -1;
2400 }
2401
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002402 if (input->basereg == -1 &&
2403 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002404 /*
2405 * It's a pure offset.
2406 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002407 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2408 input->segment == NO_SEG) {
2409 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2410 input->type &= ~IP_REL;
2411 input->type |= MEMORY;
2412 }
2413
2414 if (input->eaflags & EAF_BYTEOFFS ||
2415 (input->eaflags & EAF_WORDOFFS &&
2416 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2417 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2418 }
2419
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002420 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002421 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002422 output->sib = GEN_SIB(0, 4, 5);
2423 output->bytes = 4;
2424 output->modrm = GEN_MODRM(0, rfield, 4);
2425 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002426 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002427 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002428 output->bytes = (addrbits != 16 ? 4 : 2);
2429 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2430 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002431 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002432 } else {
2433 /*
2434 * It's an indirection.
2435 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002437 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002439 int t, it, bt; /* register numbers */
2440 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002441
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 if (s == 0)
2443 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002444
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002445 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002446 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002447 ix = nasm_reg_flags[i];
2448 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002449 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002450 ix = 0;
2451 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002452
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002453 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002454 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002455 bx = nasm_reg_flags[b];
2456 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002457 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002458 bx = 0;
2459 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002460
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002461 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002462 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002463 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002464 int32_t o = input->offset;
2465 int mod, scale, index, base;
2466
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002467 /*
2468 * For a vector SIB, one has to be a vector and the other,
2469 * if present, a GPR. The vector must be the index operand.
2470 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002471 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002472 if (s == 0)
2473 s = 1;
2474 else if (s != 1)
2475 goto err;
2476
2477 t = bt, bt = it, it = t;
2478 x = bx, bx = ix, ix = x;
2479 }
2480
2481 if (bt != -1) {
2482 if (REG_GPR & ~bx)
2483 goto err;
2484 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2485 sok &= bx;
2486 else
2487 goto err;
2488 }
2489
2490 /*
2491 * While we're here, ensure the user didn't specify
2492 * WORD or QWORD
2493 */
2494 if (input->disp_size == 16 || input->disp_size == 64)
2495 goto err;
2496
2497 if (addrbits == 16 ||
2498 (addrbits == 32 && !(sok & BITS32)) ||
2499 (addrbits == 64 && !(sok & BITS64)))
2500 goto err;
2501
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002502 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2503 : ((ix & YMMREG & ~REG_EA)
2504 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002505
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002506 output->rex |= rexflags(it, ix, REX_X);
2507 output->rex |= rexflags(bt, bx, REX_B);
2508 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002509
2510 index = it & 7; /* it is known to be != -1 */
2511
2512 switch (s) {
2513 case 1:
2514 scale = 0;
2515 break;
2516 case 2:
2517 scale = 1;
2518 break;
2519 case 4:
2520 scale = 2;
2521 break;
2522 case 8:
2523 scale = 3;
2524 break;
2525 default: /* then what the smeg is it? */
2526 goto err; /* panic */
2527 }
2528
2529 if (bt == -1) {
2530 base = 5;
2531 mod = 0;
2532 } else {
2533 base = (bt & 7);
2534 if (base != REG_NUM_EBP && o == 0 &&
2535 seg == NO_SEG && !forw_ref &&
2536 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2537 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002538 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 mod = 1;
2540 else
2541 mod = 2;
2542 }
2543
2544 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002545 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2546 output->modrm = GEN_MODRM(mod, rfield, 4);
2547 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002548 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 /*
2550 * it must be a 32/64-bit memory reference. Firstly we have
2551 * to check that all registers involved are type E/Rxx.
2552 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002553 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002554 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002555
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002556 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002557 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2558 sok &= ix;
2559 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002560 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002561 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002562
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002563 if (bt != -1) {
2564 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002565 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002566 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002567 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002568 sok &= bx;
2569 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002570
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002571 /*
2572 * While we're here, ensure the user didn't specify
2573 * WORD or QWORD
2574 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002575 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002576 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002577
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002578 if (addrbits == 16 ||
2579 (addrbits == 32 && !(sok & BITS32)) ||
2580 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002581 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002582
Keith Kaniosb7a89542007-04-12 02:40:54 +00002583 /* now reorganize base/index */
2584 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002585 ((hb == b && ht == EAH_NOTBASE) ||
2586 (hb == i && ht == EAH_MAKEBASE))) {
2587 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002588 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 x = bx, bx = ix, ix = x;
2590 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002591 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002592 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002593 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002594 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002595 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002596 }
2597 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2598 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002599 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002600 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2601 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002602 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002604 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002605 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002606 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002607 x = ix, ix = bx, bx = x;
2608 }
2609 if (it == REG_NUM_ESP ||
2610 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002613 output->rex |= rexflags(it, ix, REX_X);
2614 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002615
Keith Kanios48af1772007-08-17 07:37:52 +00002616 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002617 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002619
Keith Kaniosb7a89542007-04-12 02:40:54 +00002620 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002623 } else {
2624 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002625 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002626 seg == NO_SEG && !forw_ref &&
2627 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002628 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002629 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002630 mod = 1;
2631 else
2632 mod = 2;
2633 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002634
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002635 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002636 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2637 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002638 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002639 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002641
Keith Kaniosb7a89542007-04-12 02:40:54 +00002642 if (it == -1)
2643 index = 4, s = 1;
2644 else
2645 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002646
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 switch (s) {
2648 case 1:
2649 scale = 0;
2650 break;
2651 case 2:
2652 scale = 1;
2653 break;
2654 case 4:
2655 scale = 2;
2656 break;
2657 case 8:
2658 scale = 3;
2659 break;
2660 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002661 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002663
Keith Kaniosb7a89542007-04-12 02:40:54 +00002664 if (bt == -1) {
2665 base = 5;
2666 mod = 0;
2667 } else {
2668 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002669 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 seg == NO_SEG && !forw_ref &&
2671 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002672 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002673 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002674 mod = 1;
2675 else
2676 mod = 2;
2677 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002678
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002679 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002680 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2681 output->modrm = GEN_MODRM(mod, rfield, 4);
2682 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 }
2684 } else { /* it's 16-bit */
2685 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002686 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002687
Keith Kaniosb7a89542007-04-12 02:40:54 +00002688 /* check for 64-bit long mode */
2689 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002690 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002691
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002693 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2694 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002695 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002696
Keith Kaniosb7a89542007-04-12 02:40:54 +00002697 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002698 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002699 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002700
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002702 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 if (b == -1 && i != -1) {
2704 int tmp = b;
2705 b = i;
2706 i = tmp;
2707 } /* swap */
2708 if ((b == R_SI || b == R_DI) && i != -1) {
2709 int tmp = b;
2710 b = i;
2711 i = tmp;
2712 }
2713 /* have BX/BP as base, SI/DI index */
2714 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002715 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 if (i != -1 && b != -1 &&
2717 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002718 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002719 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002720 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 rm = -1;
2723 if (i != -1)
2724 switch (i * 256 + b) {
2725 case R_SI * 256 + R_BX:
2726 rm = 0;
2727 break;
2728 case R_DI * 256 + R_BX:
2729 rm = 1;
2730 break;
2731 case R_SI * 256 + R_BP:
2732 rm = 2;
2733 break;
2734 case R_DI * 256 + R_BP:
2735 rm = 3;
2736 break;
2737 } else
2738 switch (b) {
2739 case R_SI:
2740 rm = 4;
2741 break;
2742 case R_DI:
2743 rm = 5;
2744 break;
2745 case R_BP:
2746 rm = 6;
2747 break;
2748 case R_BX:
2749 rm = 7;
2750 break;
2751 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002753 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002754
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002755 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2756 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002758 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 mod = 1;
2760 else
2761 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002762
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002763 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002764 output->bytes = mod; /* bytes of offset needed */
2765 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 }
2767 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002768 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002769
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002770 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002771 return output->type;
2772
2773err:
2774 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002775}
2776
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002777static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002778{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002779 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002780 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002781
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002782 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002783
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002784 switch (ins->prefixes[PPS_ASIZE]) {
2785 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002786 valid &= 16;
2787 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002788 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002789 valid &= 32;
2790 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002791 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002792 valid &= 64;
2793 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002794 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002795 valid &= (addrbits == 32) ? 16 : 32;
2796 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002797 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002798 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002799 }
2800
2801 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002802 if (is_class(MEMORY, ins->oprs[j].type)) {
2803 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002804
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002805 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002806 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002807 i = 0;
2808 else
2809 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002810
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002812 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 b = 0;
2814 else
2815 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002816
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if (ins->oprs[j].scale == 0)
2818 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002820 if (!i && !b) {
2821 int ds = ins->oprs[j].disp_size;
2822 if ((addrbits != 64 && ds > 8) ||
2823 (addrbits == 64 && ds == 16))
2824 valid &= ds;
2825 } else {
2826 if (!(REG16 & ~b))
2827 valid &= 16;
2828 if (!(REG32 & ~b))
2829 valid &= 32;
2830 if (!(REG64 & ~b))
2831 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002833 if (!(REG16 & ~i))
2834 valid &= 16;
2835 if (!(REG32 & ~i))
2836 valid &= 32;
2837 if (!(REG64 & ~i))
2838 valid &= 64;
2839 }
2840 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002841 }
2842
2843 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002845 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002847 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002848 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002849 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002850 /* Impossible... */
2851 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2852 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002853 }
2854
2855 defdisp = ins->addr_size == 16 ? 16 : 32;
2856
2857 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002858 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2859 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2860 /*
2861 * mem_offs sizes must match the address size; if not,
2862 * strip the MEM_OFFS bit and match only EA instructions
2863 */
2864 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2865 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002866 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002867}