blob: e2e24c3b32003a2b87eff71ebecce2edcdc16d33 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000176#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
178#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000179#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include "assemble.h"
181#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700182#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
H. Peter Anvin65289e82009-07-25 17:25:11 -0700184enum match_result {
185 /*
186 * Matching errors. These should be sorted so that more specific
187 * errors come later in the sequence.
188 */
189 MERR_INVALOP,
190 MERR_OPSIZEMISSING,
191 MERR_OPSIZEMISMATCH,
192 MERR_BADCPU,
193 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800194 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700195 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700196 MERR_BADBND,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700197 /*
198 * Matching success; the conditional ones first
199 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400200 MOK_JUMP, /* Matching OK but needs jmp_match() */
201 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202};
203
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000204typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700205 enum ea_type type; /* what kind of EA is this? */
206 int sib_present; /* is a SIB byte necessary? */
207 int bytes; /* # of bytes of offset needed */
208 int size; /* lazy - this is sib+bytes+1 */
209 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700210 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000211} ea;
212
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400213#define GEN_SIB(scale, index, base) \
214 (((scale) << 6) | ((index) << 3) | ((base)))
215
216#define GEN_MODRM(mod, reg, rm) \
217 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
218
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700219static iflags_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220static efunc errfunc;
221static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000222static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000223
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800224static int64_t calcsize(int32_t, int64_t, int, insn *,
225 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700226static void gencode(int32_t segment, int64_t offset, int bits,
227 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400228 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700229static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400230 insn *instruction,
231 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700232static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700233static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000234static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700235static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000236static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700237static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700238static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000239
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700240static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700241
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400242static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000243{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700244 return ins->prefixes[pos] == prefix;
245}
246
247static void assert_no_prefix(insn * ins, enum prefix_pos pos)
248{
249 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400250 errfunc(ERR_NONFATAL, "invalid %s prefix",
251 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700252}
253
254static const char *size_name(int size)
255{
256 switch (size) {
257 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400258 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700269 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700271 case 64:
272 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000275 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700276}
277
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400278static void warn_overflow(int pass, int size)
279{
280 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
281 "%s data exceeds bounds", size_name(size));
282}
283
284static void warn_overflow_const(int64_t data, int size)
285{
286 if (overflow_general(data, size))
287 warn_overflow(ERR_PASS1, size);
288}
289
290static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700291{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100292 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400293 if (overflow_general(o->offset, size))
294 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700295 }
296}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400297
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000298/*
299 * This routine wrappers the real output format's output routine,
300 * in order to pass a copy of the data off to the listing file
301 * generator at the same time.
302 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800303static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800304 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400305 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000306{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000307 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000308 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800309 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800311 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400312 /*
313 * This is a non-relocated address, and we're going to
314 * convert it into RAWDATA format.
315 */
316 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800317
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400318 if (size > 8) {
319 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
320 return;
321 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700322
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400323 WRITEADDR(q, *(int64_t *)data, size);
324 data = p;
325 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000326 }
327
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800328 list->output(offset, data, type, size);
329
Frank Kotlerabebb082003-09-06 04:45:37 +0000330 /*
331 * this call to src_get determines when we call the
332 * debug-format-specific "linenum" function
333 * it updates lineno and lnfname to the current values
334 * returning 0 if "same as last time", -2 if lnfname
335 * changed, and the amount by which lineno changed,
336 * if it did. thus, these variables must be static
337 */
338
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400339 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000340 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000341
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800342 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000343}
344
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400345static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
346{
347 if (opx->segment != NO_SEG) {
348 uint64_t data = opx->offset;
349 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
350 } else {
351 uint8_t byte = opx->offset;
352 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
353 }
354}
355
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700356static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800357 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800359 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800360 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000361 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000362
H. Peter Anvin755f5212012-02-25 11:41:34 -0800363 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700364 return false;
365 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400366 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700367 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400368 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700369
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800370 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100371
Victor van den Elzen154e5922009-02-25 17:32:00 +0100372 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100373 /* Be optimistic in pass 1 */
374 return true;
375
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700377 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000378
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700379 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
380 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000382
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700383int64_t assemble(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400384 insn * instruction, struct ofmt *output, efunc error,
385 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000387 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700389 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800390 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000391 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800392 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300393 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000394
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000396 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 outfmt = output; /* likewise */
398 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000399
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300400 wsize = idata_bytes(instruction->opcode);
401 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000403
H. Peter Anvineba20a72002-04-30 20:53:55 +0000404 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000406 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 if (t < 0)
408 errfunc(ERR_PANIC,
409 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400412 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400414 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700415 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400416 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000417 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700418 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800420 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400421 offset += wsize;
422 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700423 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400424 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800428 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000430
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 if (align) {
432 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100433 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800434 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 }
436 offset += e->stringlen + align;
437 }
438 }
439 if (t > 0 && t == instruction->times - 1) {
440 /*
441 * Dummy call to list->output to give the offset to the
442 * listing module.
443 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800444 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 list->uplevel(LIST_TIMES);
446 }
447 }
448 if (instruction->times > 1)
449 list->downlevel(LIST_TIMES);
450 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000451 }
452
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700454 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000456
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400457 fp = fopen(fname, "rb");
458 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
460 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400461 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
463 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200464 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400465 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700466 static char buf[4096];
467 size_t t = instruction->times;
468 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400469 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 len = ftell(fp);
472 if (instruction->eops->next) {
473 base = instruction->eops->next->offset;
474 len -= base;
475 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700476 len > (size_t)instruction->eops->next->next->offset)
477 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 }
479 /*
480 * Dummy call to list->output to give the offset to the
481 * listing module.
482 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800483 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 list->uplevel(LIST_INCBIN);
485 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700486 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000487
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 fseek(fp, base, SEEK_SET);
489 l = len;
490 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700491 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400492 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 if (!m) {
494 /*
495 * This shouldn't happen unless the file
496 * actually changes while we are reading
497 * it.
498 */
499 error(ERR_NONFATAL,
500 "`incbin': unexpected EOF while"
501 " reading file `%s'", fname);
502 t = 0; /* Try to exit cleanly */
503 break;
504 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800505 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 NO_SEG, NO_SEG);
507 l -= m;
508 }
509 }
510 list->downlevel(LIST_INCBIN);
511 if (instruction->times > 1) {
512 /*
513 * Dummy call to list->output to give the offset to the
514 * listing module.
515 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800516 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 list->uplevel(LIST_TIMES);
518 list->downlevel(LIST_TIMES);
519 }
520 fclose(fp);
521 return instruction->times * len;
522 }
523 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000524 }
525
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700526 /* Check to see if we need an address-size prefix */
527 add_asp(instruction, bits);
528
H. Peter Anvin23595f52009-07-25 17:44:25 -0700529 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700530
H. Peter Anvin23595f52009-07-25 17:44:25 -0700531 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800533 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 itimes = instruction->times;
535 if (insn_size < 0) /* shouldn't be, on pass two */
536 error(ERR_PANIC, "errors made it through from pass one");
537 else
538 while (itimes--) {
539 for (j = 0; j < MAXPREFIX; j++) {
540 uint8_t c = 0;
541 switch (instruction->prefixes[j]) {
542 case P_WAIT:
543 c = 0x9B;
544 break;
545 case P_LOCK:
546 c = 0xF0;
547 break;
548 case P_REPNE:
549 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800550 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700551 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400552 c = 0xF2;
553 break;
554 case P_REPE:
555 case P_REPZ:
556 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800557 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400558 c = 0xF3;
559 break;
560 case R_CS:
561 if (bits == 64) {
562 error(ERR_WARNING | ERR_PASS2,
563 "cs segment base generated, but will be ignored in 64-bit mode");
564 }
565 c = 0x2E;
566 break;
567 case R_DS:
568 if (bits == 64) {
569 error(ERR_WARNING | ERR_PASS2,
570 "ds segment base generated, but will be ignored in 64-bit mode");
571 }
572 c = 0x3E;
573 break;
574 case R_ES:
575 if (bits == 64) {
576 error(ERR_WARNING | ERR_PASS2,
577 "es segment base generated, but will be ignored in 64-bit mode");
578 }
579 c = 0x26;
580 break;
581 case R_FS:
582 c = 0x64;
583 break;
584 case R_GS:
585 c = 0x65;
586 break;
587 case R_SS:
588 if (bits == 64) {
589 error(ERR_WARNING | ERR_PASS2,
590 "ss segment base generated, but will be ignored in 64-bit mode");
591 }
592 c = 0x36;
593 break;
594 case R_SEGR6:
595 case R_SEGR7:
596 error(ERR_NONFATAL,
597 "segr6 and segr7 cannot be used as prefixes");
598 break;
599 case P_A16:
600 if (bits == 64) {
601 error(ERR_NONFATAL,
602 "16-bit addressing is not supported "
603 "in 64-bit mode");
604 } else if (bits != 16)
605 c = 0x67;
606 break;
607 case P_A32:
608 if (bits != 32)
609 c = 0x67;
610 break;
611 case P_A64:
612 if (bits != 64) {
613 error(ERR_NONFATAL,
614 "64-bit addressing is only supported "
615 "in 64-bit mode");
616 }
617 break;
618 case P_ASP:
619 c = 0x67;
620 break;
621 case P_O16:
622 if (bits != 16)
623 c = 0x66;
624 break;
625 case P_O32:
626 if (bits == 16)
627 c = 0x66;
628 break;
629 case P_O64:
630 /* REX.W */
631 break;
632 case P_OSP:
633 c = 0x66;
634 break;
635 case P_none:
636 break;
637 default:
638 error(ERR_PANIC, "invalid instruction prefix");
639 }
640 if (c != 0) {
641 out(offset, segment, &c, OUT_RAWDATA, 1,
642 NO_SEG, NO_SEG);
643 offset++;
644 }
645 }
646 insn_end = offset + insn_size;
647 gencode(segment, offset, bits, instruction,
648 temp, insn_end);
649 offset += insn_size;
650 if (itimes > 0 && itimes == instruction->times - 1) {
651 /*
652 * Dummy call to list->output to give the offset to the
653 * listing module.
654 */
655 list->output(offset, NULL, OUT_RAWDATA, 0);
656 list->uplevel(LIST_TIMES);
657 }
658 }
659 if (instruction->times > 1)
660 list->downlevel(LIST_TIMES);
661 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700662 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400663 /* No match */
664 switch (m) {
665 case MERR_OPSIZEMISSING:
666 error(ERR_NONFATAL, "operation size not specified");
667 break;
668 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400670 break;
671 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400673 break;
674 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800675 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400676 bits);
677 break;
678 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 error(ERR_NONFATAL,
680 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400681 break;
682 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000683 }
684 return 0;
685}
686
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700687int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400688 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000689{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000690 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700691 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000694 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000695
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400696 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000698
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700699 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
700 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400701 instruction->opcode == I_DT || instruction->opcode == I_DO ||
702 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300704 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000705
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300707 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000708
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400709 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000710 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000711
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400713 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400715 warn_overflow_const(e->offset, wsize);
716 } else if (e->type == EOT_DB_STRING ||
717 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000719
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 align = (-osize) % wsize;
721 if (align < 0)
722 align += wsize;
723 isize += osize + align;
724 }
725 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000726 }
727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400729 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000730 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300731 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700732 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000733
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400734 fp = fopen(fname, "rb");
735 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
737 fname);
738 else if (fseek(fp, 0L, SEEK_END) < 0)
739 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
740 fname);
741 else {
742 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 if (instruction->eops->next) {
744 len -= instruction->eops->next->offset;
745 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700746 len > (size_t)instruction->eops->next->next->offset) {
747 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 }
749 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300750 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300752 if (fp)
753 fclose(fp);
754 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000755 }
756
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700757 /* Check to see if we need an address-size prefix */
758 add_asp(instruction, bits);
759
H. Peter Anvin23595f52009-07-25 17:44:25 -0700760 m = find_match(&temp, instruction, segment, offset, bits);
761 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400762 /* we've matched an instruction. */
763 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400764 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100765
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800766 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400767 if (isize < 0)
768 return -1;
769 for (j = 0; j < MAXPREFIX; j++) {
770 switch (instruction->prefixes[j]) {
771 case P_A16:
772 if (bits != 16)
773 isize++;
774 break;
775 case P_A32:
776 if (bits != 32)
777 isize++;
778 break;
779 case P_O16:
780 if (bits != 16)
781 isize++;
782 break;
783 case P_O32:
784 if (bits == 16)
785 isize++;
786 break;
787 case P_A64:
788 case P_O64:
789 case P_none:
790 break;
791 default:
792 isize++;
793 break;
794 }
795 }
796 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700797 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400798 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800}
801
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800802static void bad_hle_warn(const insn * ins, uint8_t hleok)
803{
804 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800805 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800806 static const enum whatwarn warn[2][4] =
807 {
808 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
809 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
810 };
811 unsigned int n;
812
813 n = (unsigned int)rep_pfx - P_XACQUIRE;
814 if (n > 1)
815 return; /* Not XACQUIRE/XRELEASE */
816
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800817 ww = warn[n][hleok];
818 if (!is_class(MEMORY, ins->oprs[0].type))
819 ww = w_inval; /* HLE requires operand 0 to be memory */
820
821 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800822 case w_none:
823 break;
824
825 case w_lock:
826 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800827 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800828 "%s with this instruction requires lock",
829 prefix_name(rep_pfx));
830 }
831 break;
832
833 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800834 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800835 "%s invalid with this instruction",
836 prefix_name(rep_pfx));
837 break;
838 }
839}
840
H. Peter Anvin507ae032008-10-09 15:37:10 -0700841/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400842#define case3(x) case (x): case (x)+1: case (x)+2
843#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700844
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800845static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800846 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000847{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800848 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800849 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000850 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000851 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700852 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700853 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700854 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700855 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800856 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800857 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700858 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000859
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700860 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700861 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700862 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700863
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700864 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400865 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 (void)segment; /* Don't warn that this parameter is unused */
868 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000869
H. Peter Anvin839eca22007-10-29 23:12:47 -0700870 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400871 c = *codes++;
872 op1 = (c & 3) + ((opex & 1) << 2);
873 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
874 opx = &ins->oprs[op1];
875 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700876
H. Peter Anvin839eca22007-10-29 23:12:47 -0700877 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400878 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 codes += c, length += c;
880 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700881
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400882 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400883 opex = c;
884 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700885
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400886 case4(010):
887 ins->rex |=
888 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 codes++, length++;
890 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700891
Jin Kyu Song164d6072013-10-15 19:10:13 -0700892 case4(014):
893 /* this is an index reg of MIB operand */
894 mib_index = opx->basereg;
895 break;
896
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400897 case4(020):
898 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 length++;
900 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700901
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400902 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 length += 2;
904 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700905
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400906 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700907 if (opx->type & (BITS16 | BITS32 | BITS64))
908 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 else
910 length += (bits == 16) ? 2 : 4;
911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400913 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 length += 4;
915 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400917 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700918 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400921 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 length++;
923 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000926 length += 8; /* MOV reg64/imm */
927 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400929 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 length += 2;
931 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400933 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700934 if (opx->type & (BITS16 | BITS32 | BITS64))
935 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 else
937 length += (bits == 16) ? 2 : 4;
938 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700939
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400940 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 length += 4;
942 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400944 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700945 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400948 case 0172:
949 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700951 length++;
952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700954 case4(0174):
955 length++;
956 break;
957
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700958 case4(0240):
959 ins->rex |= REX_EV;
960 ins->vexreg = regval(opx);
961 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
962 ins->vex_cm = *codes++;
963 ins->vex_wlp = *codes++;
964 ins->evex_tuple = (*codes++ - 0300);
965 break;
966
967 case 0250:
968 ins->rex |= REX_EV;
969 ins->vexreg = 0;
970 ins->vex_cm = *codes++;
971 ins->vex_wlp = *codes++;
972 ins->evex_tuple = (*codes++ - 0300);
973 break;
974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400975 case4(0254):
976 length += 4;
977 break;
978
979 case4(0260):
980 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700981 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400982 ins->vex_cm = *codes++;
983 ins->vex_wlp = *codes++;
984 break;
985
986 case 0270:
987 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700988 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 ins->vex_cm = *codes++;
990 ins->vex_wlp = *codes++;
991 break;
992
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400993 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800994 hleok = c & 3;
995 break;
996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(0274):
998 length++;
999 break;
1000
1001 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 if (bits == 64)
1006 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001007 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001011 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
Keith Kaniosb7a89542007-04-12 02:40:54 +00001017 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1019 has_prefix(ins, PPS_ASIZE, P_A32))
1020 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001022
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001023 case4(0314):
1024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001027 {
1028 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1029 if (pfx == P_O16)
1030 break;
1031 if (pfx != P_none)
1032 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1033 else
1034 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001036 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001039 {
1040 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1041 if (pfx == P_O32)
1042 break;
1043 if (pfx != P_none)
1044 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1045 else
1046 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001048 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001049
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 case 0322:
1051 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001052
Keith Kaniosb7a89542007-04-12 02:40:54 +00001053 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001054 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001055 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001056
Keith Kaniosb7a89542007-04-12 02:40:54 +00001057 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001058 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001059 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001060
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 case 0325:
1062 ins->rex |= REX_NH;
1063 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001064
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001065 case 0326:
1066 break;
1067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 case 0330:
1069 codes++, length++;
1070 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001074
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001075 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 case 0333:
1077 length++;
1078 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001079
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001080 case 0334:
1081 ins->rex |= REX_L;
1082 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001083
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001084 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001088 if (!ins->prefixes[PPS_REP])
1089 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001093 if (!ins->prefixes[PPS_REP])
1094 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 if (ins->oprs[0].segment != NO_SEG)
1099 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1100 " quantity of BSS space");
1101 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001102 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 case 0341:
1106 if (!ins->prefixes[PPS_WAIT])
1107 ins->prefixes[PPS_WAIT] = P_WAIT;
1108 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001109
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001110 case 0360:
1111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001113 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 length++;
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001117 case 0364:
1118 case 0365:
1119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Keith Kanios48af1772007-08-17 07:37:52 +00001121 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001122 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 length++;
1124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001126 case 0370:
1127 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 case 0373:
1131 length++;
1132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001134 case 0374:
1135 eat = EA_XMMVSIB;
1136 break;
1137
1138 case 0375:
1139 eat = EA_YMMVSIB;
1140 break;
1141
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001142 case 0376:
1143 eat = EA_ZMMVSIB;
1144 break;
1145
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001146 case4(0100):
1147 case4(0110):
1148 case4(0120):
1149 case4(0130):
1150 case4(0200):
1151 case4(0204):
1152 case4(0210):
1153 case4(0214):
1154 case4(0220):
1155 case4(0224):
1156 case4(0230):
1157 case4(0234):
1158 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001160 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 opflags_t rflags;
1162 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001163 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001164
Keith Kaniosb7a89542007-04-12 02:40:54 +00001165 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 if (c <= 0177) {
1168 /* pick rfield from operand b (opx) */
1169 rflags = regflag(opx);
1170 rfield = nasm_regvals[opx->basereg];
1171 } else {
1172 rflags = 0;
1173 rfield = c & 7;
1174 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001175
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001176 /* EVEX.b1 : evex_brerop contains the operand position */
1177 op_er_sae = (ins->evex_brerop >= 0 ?
1178 &ins->oprs[ins->evex_brerop] : NULL);
1179
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001180 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1181 /* set EVEX.b */
1182 ins->evex_p[2] |= EVEX_P2B;
1183 if (op_er_sae->decoflags & ER) {
1184 /* set EVEX.RC (rounding control) */
1185 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1186 & EVEX_P2RC;
1187 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001188 } else {
1189 /* set EVEX.L'L (vector length) */
1190 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001191 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001192 /* set EVEX.b */
1193 ins->evex_p[2] |= EVEX_P2B;
1194 }
1195 }
1196
Jin Kyu Song164d6072013-10-15 19:10:13 -07001197 /*
1198 * if a separate form of MIB (ICC style) is used,
1199 * the index reg info is merged into mem operand
1200 */
1201 if (mib_index != R_none) {
1202 opy->indexreg = mib_index;
1203 opy->scale = 1;
1204 opy->hintbase = mib_index;
1205 opy->hinttype = EAH_NOTBASE;
1206 }
1207
Jin Kyu Song3b653232013-11-08 11:41:12 -08001208 /*
1209 * only for mib operands, make a single reg index [reg*1].
1210 * gas uses this form to explicitly denote index register.
1211 */
1212 if ((temp->flags & IF_MIB) &&
1213 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1214 opy->hinttype == EAH_NOTBASE)) {
1215 opy->indexreg = opy->basereg;
1216 opy->basereg = -1;
1217 opy->scale = 1;
1218 }
1219
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001220 if (process_ea(opy, &ea_data, bits,
1221 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 errfunc(ERR_NONFATAL, "invalid effective address");
1223 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001224 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001227 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001228 }
1229 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001230
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001231 default:
1232 errfunc(ERR_PANIC, "internal instruction table corrupt"
1233 ": instruction code \\%o (0x%02X) given", c, c);
1234 break;
1235 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001236 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001237
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001238 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001239
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001240 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 if (ins->rex & REX_H) {
1242 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1243 return -1;
1244 }
1245 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001246 }
1247
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001248 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001249 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001250
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001252 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 return -1;
1254 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001255 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001257 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001258 ins->rex &= ~REX_W;
1259 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001260 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001261 ins->rex |= REX_W;
1262 bad32 &= ~REX_W;
1263 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001264 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 /* Follow REX_W */
1266 break;
1267 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001268
H. Peter Anvinfc561202011-07-07 16:58:22 -07001269 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1271 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001272 } else if (!(ins->rex & REX_EV) &&
1273 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1274 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1275 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001276 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001277 if (ins->rex & REX_EV)
1278 length += 4;
1279 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 length += 3;
1281 else
1282 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001283 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 if (ins->rex & REX_H) {
1285 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1286 return -1;
1287 } else if (bits == 64) {
1288 length++;
1289 } else if ((ins->rex & REX_L) &&
1290 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1291 cpu >= IF_X86_64) {
1292 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001293 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001294 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001295 length++;
1296 } else {
1297 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1298 return -1;
1299 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001300 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001301
1302 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1303 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001304 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001305 "instruction is not lockable");
1306 }
1307
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001308 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001309
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001310 return length;
1311}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001312
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001313static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1314{
1315 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001316 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001317 ins->rex = (ins->rex & REX_REAL) | REX_P;
1318 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1319 ins->rex = 0;
1320 return 1;
1321 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001322 }
1323
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001324 return 0;
1325}
1326
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001327static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001328 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001330{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001331 uint8_t c;
1332 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001333 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001334 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001335 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001336 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001337 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001338 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001339 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001340
H. Peter Anvin839eca22007-10-29 23:12:47 -07001341 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 c = *codes++;
1343 op1 = (c & 3) + ((opex & 1) << 2);
1344 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1345 opx = &ins->oprs[op1];
1346 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001347
H. Peter Anvin839eca22007-10-29 23:12:47 -07001348 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 case 01:
1350 case 02:
1351 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001353 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001354 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 codes += c;
1356 offset += c;
1357 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001358
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001359 case 05:
1360 case 06:
1361 case 07:
1362 opex = c;
1363 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001364
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001366 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001367 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001368 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 offset += 1;
1370 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001371
Jin Kyu Song164d6072013-10-15 19:10:13 -07001372 case4(014):
1373 break;
1374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001376 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001377 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001380 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 offset += 1;
1382 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001385 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001386 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001388 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 offset += 1;
1390 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001393 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001394 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001395 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001396 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 offset += 2;
1398 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001399
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001400 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001401 if (opx->type & (BITS16 | BITS32))
1402 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 else
1404 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001405 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001406 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001407 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001408 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 offset += size;
1410 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001412 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001413 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001414 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001415 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001416 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 offset += 4;
1418 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001419
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001420 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001421 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001422 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001423 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001424 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 offset += size;
1427 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001430 if (opx->segment != segment) {
1431 data = opx->offset;
1432 out(offset, segment, &data,
1433 OUT_REL1ADR, insn_end - offset,
1434 opx->segment, opx->wrt);
1435 } else {
1436 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001437 if (data > 127 || data < -128)
1438 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001439 out(offset, segment, &data,
1440 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1441 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 offset += 1;
1443 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001444
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001445 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001446 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001447 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001448 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001449 offset += 8;
1450 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001451
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001452 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001453 if (opx->segment != segment) {
1454 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001456 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001459 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001461 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 }
1463 offset += 2;
1464 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001466 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001467 if (opx->type & (BITS16 | BITS32 | BITS64))
1468 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 else
1470 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001471 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001472 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001473 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001474 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1475 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001477 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001479 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 }
1481 offset += size;
1482 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001483
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001484 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001485 if (opx->segment != segment) {
1486 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001488 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001489 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001491 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001493 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 }
1495 offset += 4;
1496 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001497
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001499 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001500 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1501 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001502 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001503 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001504 outfmt->segbase(1 + opx->segment),
1505 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001506 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001508
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001509 case 0172:
1510 c = *codes++;
1511 opx = &ins->oprs[c >> 3];
1512 bytes[0] = nasm_regvals[opx->basereg] << 4;
1513 opx = &ins->oprs[c & 7];
1514 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1515 errfunc(ERR_NONFATAL,
1516 "non-absolute expression not permitted as argument %d",
1517 c & 7);
1518 } else {
1519 if (opx->offset & ~15) {
1520 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1521 "four-bit argument exceeds bounds");
1522 }
1523 bytes[0] |= opx->offset & 15;
1524 }
1525 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1526 offset++;
1527 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001528
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001529 case 0173:
1530 c = *codes++;
1531 opx = &ins->oprs[c >> 4];
1532 bytes[0] = nasm_regvals[opx->basereg] << 4;
1533 bytes[0] |= c & 15;
1534 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1535 offset++;
1536 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001537
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001538 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 bytes[0] = nasm_regvals[opx->basereg] << 4;
1540 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1541 offset++;
1542 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001543
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001545 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001546 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1547 (int32_t)data != (int64_t)data) {
1548 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1549 "signed dword immediate exceeds bounds");
1550 }
1551 out(offset, segment, &data, OUT_ADDRESS, 4,
1552 opx->segment, opx->wrt);
1553 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001554 break;
1555
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001556 case4(0240):
1557 case 0250:
1558 codes += 3;
1559 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1560 EVEX_P2Z | EVEX_P2AAA, 2);
1561 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1562 bytes[0] = 0x62;
1563 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001564 bytes[1] = ((((ins->rex & 7) << 5) |
1565 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1566 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001567 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1568 ((~ins->vexreg & 15) << 3) |
1569 (1 << 2) | (ins->vex_wlp & 3);
1570 bytes[3] = ins->evex_p[2];
1571 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1572 offset += 4;
1573 break;
1574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 case4(0260):
1576 case 0270:
1577 codes += 2;
1578 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1579 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1580 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1581 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001582 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001583 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1584 offset += 3;
1585 } else {
1586 bytes[0] = 0xc5;
1587 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001588 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1590 offset += 2;
1591 }
1592 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001593
H. Peter Anvine014f352012-02-25 22:35:19 -08001594 case 0271:
1595 case 0272:
1596 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001597 break;
1598
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 case4(0274):
1600 {
1601 uint64_t uv, um;
1602 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001603
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 if (ins->rex & REX_W)
1605 s = 64;
1606 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1607 s = 16;
1608 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1609 s = 32;
1610 else
1611 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 um = (uint64_t)2 << (s-1);
1614 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001615
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001616 if (uv > 127 && uv < (uint64_t)-128 &&
1617 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001618 /* If this wasn't explicitly byte-sized, warn as though we
1619 * had fallen through to the imm16/32/64 case.
1620 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001621 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001622 "%s value exceeds bounds",
1623 (opx->type & BITS8) ? "signed byte" :
1624 s == 16 ? "word" :
1625 s == 32 ? "dword" :
1626 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001627 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001628 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001629 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001630 out(offset, segment, &data, OUT_ADDRESS, 1,
1631 opx->segment, opx->wrt);
1632 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001633 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001634 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1635 NO_SEG);
1636 }
1637 offset += 1;
1638 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001643
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001645 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001647 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 offset += 1;
1649 } else
1650 offset += 0;
1651 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001652
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001654 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001656 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 offset += 1;
1658 } else
1659 offset += 0;
1660 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 case 0312:
1663 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664
Keith Kaniosb7a89542007-04-12 02:40:54 +00001665 case 0313:
1666 ins->rex = 0;
1667 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001668
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 case4(0314):
1670 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001671
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001677 case 0323:
1678 break;
1679
Keith Kaniosb7a89542007-04-12 02:40:54 +00001680 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001681 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001683
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 case 0325:
1685 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001686
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001687 case 0326:
1688 break;
1689
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001691 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
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 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001698
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001701 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001702 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 offset += 1;
1704 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001705
Keith Kanios48af1772007-08-17 07:37:52 +00001706 case 0334:
1707 if (ins->rex & REX_R) {
1708 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001709 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001710 offset += 1;
1711 }
1712 ins->rex &= ~(REX_L|REX_R);
1713 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001714
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001715 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001716 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001717
H. Peter Anvin962e3052008-08-28 17:47:16 -07001718 case 0336:
1719 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001720 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001721
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 if (ins->oprs[0].segment != NO_SEG)
1724 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1725 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001726 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 if (size > 0)
1728 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001729 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 offset += size;
1731 }
1732 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001733
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 case 0341:
1735 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001736
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 case 0360:
1738 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001739
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001740 case 0361:
1741 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001742 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1743 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case 0364:
1747 case 0365:
1748 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001749
Keith Kanios48af1772007-08-17 07:37:52 +00001750 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001752 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001753 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001754 offset += 1;
1755 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001756
Jin Kyu Song03041092013-10-15 19:38:51 -07001757 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001759
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 case 0373:
1761 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001762 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 offset += 1;
1764 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001766 case 0374:
1767 eat = EA_XMMVSIB;
1768 break;
1769
1770 case 0375:
1771 eat = EA_YMMVSIB;
1772 break;
1773
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001774 case 0376:
1775 eat = EA_ZMMVSIB;
1776 break;
1777
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001778 case4(0100):
1779 case4(0110):
1780 case4(0120):
1781 case4(0130):
1782 case4(0200):
1783 case4(0204):
1784 case4(0210):
1785 case4(0214):
1786 case4(0220):
1787 case4(0224):
1788 case4(0230):
1789 case4(0234):
1790 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 ea ea_data;
1792 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001794 uint8_t *p;
1795 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001797
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001798 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001799 /* pick rfield from operand b (opx) */
1800 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001801 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 } else {
1803 /* rfield is constant */
1804 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001808 if (process_ea(opy, &ea_data, bits,
1809 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 p = bytes;
1813 *p++ = ea_data.modrm;
1814 if (ea_data.sib_present)
1815 *p++ = ea_data.sib;
1816
1817 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001818 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001820 /*
1821 * Make sure the address gets the right offset in case
1822 * the line breaks in the .lst file (BR 1197827)
1823 */
1824 offset += s;
1825 s = 0;
1826
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 switch (ea_data.bytes) {
1828 case 0:
1829 break;
1830 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 case 2:
1832 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001833 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001834 /* use compressed displacement, if available */
1835 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 if (ea_data.rip) {
1838 if (opy->segment == segment) {
1839 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001840 if (overflow_signed(data, ea_data.bytes))
1841 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 out(offset, segment, &data, OUT_ADDRESS,
1843 ea_data.bytes, NO_SEG, NO_SEG);
1844 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001845 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 out(offset, segment, &data, OUT_REL4ADR,
1847 insn_end - offset, opy->segment, opy->wrt);
1848 }
1849 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001850 if (overflow_general(data, ins->addr_size >> 3) ||
1851 signed_bits(data, ins->addr_size) !=
1852 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001853 warn_overflow(ERR_PASS2, ea_data.bytes);
1854
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 out(offset, segment, &data, OUT_ADDRESS,
1856 ea_data.bytes, opy->segment, opy->wrt);
1857 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001859 default:
1860 /* Impossible! */
1861 errfunc(ERR_PANIC,
1862 "Invalid amount of bytes (%d) for offset?!",
1863 ea_data.bytes);
1864 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 }
1866 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 }
1868 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 default:
1871 errfunc(ERR_PANIC, "internal instruction table corrupt"
1872 ": instruction code \\%o (0x%02X) given", c, c);
1873 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001875 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001876}
1877
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001878static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001879{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001880 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001881 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001882 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001883}
1884
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001885static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001886{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001887 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001889 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001890}
1891
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001892static int op_rexflags(const operand * o, int mask)
1893{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001894 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001895 int val;
1896
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001897 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001898 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001899
H. Peter Anvina4835d42008-05-20 14:21:29 -07001900 flags = nasm_reg_flags[o->basereg];
1901 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001902
1903 return rexflags(val, flags, mask);
1904}
1905
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001906static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001907{
1908 int rex = 0;
1909
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001910 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001912 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 rex |= REX_W;
1914 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1915 rex |= REX_H;
1916 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1917 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001918
1919 return rex & mask;
1920}
1921
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001922static int evexflags(int val, decoflags_t deco,
1923 int mask, uint8_t byte)
1924{
1925 int evex = 0;
1926
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001927 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001928 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001929 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001930 evex |= (EVEX_P0RP | EVEX_P0X);
1931 break;
1932 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001933 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001934 evex |= EVEX_P2VP;
1935 if (deco & Z)
1936 evex |= EVEX_P2Z;
1937 if (deco & OPMASK_MASK)
1938 evex |= deco & EVEX_P2AAA;
1939 break;
1940 }
1941 return evex & mask;
1942}
1943
1944static int op_evexflags(const operand * o, int mask, uint8_t byte)
1945{
1946 int val;
1947
1948 if (!is_register(o->basereg))
1949 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1950
1951 val = nasm_regvals[o->basereg];
1952
1953 return evexflags(val, o->decoflags, mask, byte);
1954}
1955
H. Peter Anvin23595f52009-07-25 17:44:25 -07001956static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 insn *instruction,
1958 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001959{
1960 const struct itemplate *temp;
1961 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001962 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001963 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001964 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001965 int i;
1966
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001967 /* broadcasting uses a different data element size */
1968 for (i = 0; i < instruction->operands; i++)
1969 if (i == broadcast)
1970 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1971 else
1972 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001973
1974 merr = MERR_INVALOP;
1975
1976 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001977 temp->opcode != I_none; temp++) {
1978 m = matches(temp, instruction, bits);
1979 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001980 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001981 m = MOK_GOOD;
1982 else
1983 m = MERR_INVALOP;
1984 } else if (m == MERR_OPSIZEMISSING &&
1985 (temp->flags & IF_SMASK) != IF_SX) {
1986 /*
1987 * Missing operand size and a candidate for fuzzy matching...
1988 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001989 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001990 if (i == broadcast)
1991 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
1992 else
1993 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001994 opsizemissing = true;
1995 }
1996 if (m > merr)
1997 merr = m;
1998 if (merr == MOK_GOOD)
1999 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002000 }
2001
2002 /* No match, but see if we can get a fuzzy operand size match... */
2003 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002004 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002005
2006 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 /*
2008 * We ignore extrinsic operand sizes on registers, so we should
2009 * never try to fuzzy-match on them. This also resolves the case
2010 * when we have e.g. "xmmrm128" in two different positions.
2011 */
2012 if (is_class(REGISTER, instruction->oprs[i].type))
2013 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 /* This tests if xsizeflags[i] has more than one bit set */
2016 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2017 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002018
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002019 if (i == broadcast)
2020 instruction->oprs[i].decoflags |= xsizeflags[i];
2021 else
2022 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002023 }
2024
2025 /* Try matching again... */
2026 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002027 temp->opcode != I_none; temp++) {
2028 m = matches(temp, instruction, bits);
2029 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002030 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002031 m = MOK_GOOD;
2032 else
2033 m = MERR_INVALOP;
2034 }
2035 if (m > merr)
2036 merr = m;
2037 if (merr == MOK_GOOD)
2038 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002039 }
2040
H. Peter Anvina81655b2009-07-25 18:15:28 -07002041done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002042 *tempp = temp;
2043 return merr;
2044}
2045
H. Peter Anvin65289e82009-07-25 17:25:11 -07002046static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002047 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002048{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002049 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002050 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002051 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002052
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002053 /*
2054 * Check the opcode
2055 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002057 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002058
2059 /*
2060 * Count the operands
2061 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002063 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002064
2065 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002066 * Is it legal?
2067 */
2068 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2069 return MERR_INVALOP;
2070
2071 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002072 * Check that no spurious colons or TOs are present
2073 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 for (i = 0; i < itemp->operands; i++)
2075 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002076 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002077
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002078 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002079 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002080 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002081 switch (itemp->flags & IF_SMASK) {
2082 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002083 asize = BITS8;
2084 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002085 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002086 asize = BITS16;
2087 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002088 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002089 asize = BITS32;
2090 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002091 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002092 asize = BITS64;
2093 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002094 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002095 asize = BITS128;
2096 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002097 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002098 asize = BITS256;
2099 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002100 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002101 asize = BITS512;
2102 break;
2103 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002104 switch (bits) {
2105 case 16:
2106 asize = BITS16;
2107 break;
2108 case 32:
2109 asize = BITS32;
2110 break;
2111 case 64:
2112 asize = BITS64;
2113 break;
2114 default:
2115 asize = 0;
2116 break;
2117 }
2118 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002119 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002120 asize = 0;
2121 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002122 }
2123
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002124 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002125 /* S- flags only apply to a specific operand */
2126 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2127 memset(size, 0, sizeof size);
2128 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002129 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002130 /* S- flags apply to all operands */
2131 for (i = 0; i < MAX_OPERANDS; i++)
2132 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002133 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002134
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002135 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002136 * Check that the operand flags all match up,
2137 * it's a bit tricky so lets be verbose:
2138 *
2139 * 1) Find out the size of operand. If instruction
2140 * doesn't have one specified -- we're trying to
2141 * guess it either from template (IF_S* flag) or
2142 * from code bits.
2143 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002144 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002145 * template has an operand size specified AND this size differ
2146 * from which instruction has (perhaps we got it from code bits)
2147 * we are:
2148 * a) Check that only size of instruction and operand is differ
2149 * other characteristics do match
2150 * b) Perhaps it's a register specified in instruction so
2151 * for such a case we just mark that operand as "size
2152 * missing" and this will turn on fuzzy operand size
2153 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002154 */
2155 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002156 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002157 decoflags_t deco = instruction->oprs[i].decoflags;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002158 if (!(type & SIZE_MASK))
2159 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002160
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002161 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2162 (itemp->deco[i] & deco) != deco) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002163 return MERR_INVALOP;
2164 } else if ((itemp->opd[i] & SIZE_MASK) &&
2165 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2166 if (type & SIZE_MASK) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002167 /*
2168 * when broadcasting, the element size depends on
2169 * the instruction type. decorator flag should match.
2170 */
2171#define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2172 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2173 if (!((deco & BRDCAST_MASK) &&
2174 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2175 return MERR_INVALOP;
2176 }
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002177 } else if (!is_class(REGISTER, type)) {
2178 /*
2179 * Note: we don't honor extrinsic operand sizes for registers,
2180 * so "missing operand size" for a register should be
2181 * considered a wildcard match rather than an error.
2182 */
2183 opsizemissing = true;
2184 }
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002185 } else if (is_register(instruction->oprs[i].basereg) &&
2186 nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
Jin Kyu Songdd1c0c12013-09-13 14:12:56 -07002187 !(itemp->flags & IF_AVX512)) {
Jin Kyu Song66c61922013-08-26 20:28:43 -07002188 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002189 }
2190 }
2191
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002192 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002193 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002194
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002195 /*
2196 * Check operand sizes
2197 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002198 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002199 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002201 asize = itemp->opd[i] & SIZE_MASK;
2202 if (asize) {
2203 for (i = 0; i < oprs; i++)
2204 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 break;
2206 }
2207 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002208 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002210 }
2211
Keith Kaniosb7a89542007-04-12 02:40:54 +00002212 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 if (!(itemp->opd[i] & SIZE_MASK) &&
2214 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002215 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002216 }
2217
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002218 /*
2219 * Check template is okay at the set cpu level
2220 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002221 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002222 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002223
Keith Kaniosb7a89542007-04-12 02:40:54 +00002224 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002225 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002226 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002227 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002228 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002230 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002231 * If we have a HLE prefix, look for the NOHLE flag
2232 */
2233 if ((itemp->flags & IF_NOHLE) &&
2234 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2235 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2236 return MERR_BADHLE;
2237
2238 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002239 * Check if special handling needed for Jumps
2240 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002241 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002242 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243
Jin Kyu Song03041092013-10-15 19:38:51 -07002244 /*
2245 * Check if BND prefix is allowed
2246 */
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08002247 if ((IF_BND & ~itemp->flags) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002248 has_prefix(instruction, PPS_REP, P_BND))
2249 return MERR_BADBND;
2250
H. Peter Anvin60926242009-07-26 16:25:38 -07002251 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002252}
2253
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002254/*
2255 * Check if offset is a multiple of N with corresponding tuple type
2256 * if Disp8*N is available, compressed displacement is stored in compdisp
2257 */
2258static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2259{
2260 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2261 {{16, 32, 64}, {8, 8, 8}}};
2262 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2263 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2264
2265 bool evex_b = input->decoflags & BRDCAST_MASK;
2266 enum ttypes tuple = ins->evex_tuple;
2267 /* vex_wlp composed as [wwllpp] */
2268 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2269 /* wig(=2) is treated as w0(=0) */
2270 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2271 int32_t off = input->offset;
2272 uint8_t n = 0;
2273 int32_t disp8;
2274
2275 switch(tuple) {
2276 case FV:
2277 n = fv_n[evex_w][evex_b][vectlen];
2278 break;
2279 case HV:
2280 n = hv_n[evex_b][vectlen];
2281 break;
2282
2283 case FVM:
2284 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2285 n = 1 << (vectlen + 4);
2286 break;
2287 case T1S8: /* N = 1 */
2288 case T1S16: /* N = 2 */
2289 n = tuple - T1S8 + 1;
2290 break;
2291 case T1S:
2292 /* N = 4 for 32bit, 8 for 64bit */
2293 n = evex_w ? 8 : 4;
2294 break;
2295 case T1F32:
2296 case T1F64:
2297 /* N = 4 for 32bit, 8 for 64bit */
2298 n = (tuple == T1F32 ? 4 : 8);
2299 break;
2300 case T2:
2301 case T4:
2302 case T8:
2303 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2304 n = 0;
2305 else
Jin Kyu Songd2d9c3e2013-08-26 20:28:41 -07002306 n = 1 << (tuple - T2 + evex_w + 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002307 break;
2308 case HVM:
2309 case QVM:
2310 case OVM:
2311 n = 1 << (OVM - tuple + vectlen + 1);
2312 break;
2313 case M128:
2314 n = 16;
2315 break;
2316 case DUP:
2317 n = dup_n[vectlen];
2318 break;
2319
2320 default:
2321 break;
2322 }
2323
2324 if (n && !(off & (n - 1))) {
2325 disp8 = off / n;
2326 /* if it fits in Disp8 */
2327 if (disp8 >= -128 && disp8 <= 127) {
2328 *compdisp = disp8;
2329 return true;
2330 }
2331 }
2332
2333 *compdisp = 0;
2334 return false;
2335}
2336
2337/*
2338 * Check if ModR/M.mod should/can be 01.
2339 * - EAF_BYTEOFFS is set
2340 * - offset can fit in a byte when EVEX is not used
2341 * - offset can be compressed when EVEX is used
2342 */
2343#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2344 (o >= -128 && o <= 127 && \
2345 seg == NO_SEG && !forw_ref && \
2346 !(input->eaflags & EAF_WORDOFFS) && \
2347 !(ins->rex & REX_EV)) || \
2348 (ins->rex & REX_EV && \
2349 is_disp8n(input, ins, &output->disp8)))
2350
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002351static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002352 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002353{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002354 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002355 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002356
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002357 output->type = EA_SCALAR;
2358 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002359 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002360
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002361 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002362 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002363 /* EVEX.R' flag for the REG operand */
2364 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002365
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002366 if (is_class(REGISTER, input->type)) {
2367 /*
2368 * It's a direct register.
2369 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002370 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002371 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002372
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002373 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002374 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002375
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002376 /* broadcasting is not available with a direct register operand. */
2377 if (input->decoflags & BRDCAST_MASK) {
2378 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2379 goto err;
2380 }
2381
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002382 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002383 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002384 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 output->bytes = 0; /* no offset necessary either */
2386 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2387 } else {
2388 /*
2389 * It's a memory reference.
2390 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002391
2392 /* Embedded rounding or SAE is not available with a mem ref operand. */
2393 if (input->decoflags & (ER | SAE)) {
2394 nasm_error(ERR_NONFATAL,
2395 "Embedded rounding is available only with reg-reg op.");
2396 return -1;
2397 }
2398
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002399 if (input->basereg == -1 &&
2400 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002401 /*
2402 * It's a pure offset.
2403 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002404 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2405 input->segment == NO_SEG) {
2406 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2407 input->type &= ~IP_REL;
2408 input->type |= MEMORY;
2409 }
2410
2411 if (input->eaflags & EAF_BYTEOFFS ||
2412 (input->eaflags & EAF_WORDOFFS &&
2413 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2414 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2415 }
2416
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002417 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002418 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002419 output->sib = GEN_SIB(0, 4, 5);
2420 output->bytes = 4;
2421 output->modrm = GEN_MODRM(0, rfield, 4);
2422 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002423 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002424 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002425 output->bytes = (addrbits != 16 ? 4 : 2);
2426 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2427 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002428 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002429 } else {
2430 /*
2431 * It's an indirection.
2432 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002434 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002436 int t, it, bt; /* register numbers */
2437 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 if (s == 0)
2440 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002441
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002442 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002443 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002444 ix = nasm_reg_flags[i];
2445 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002446 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002447 ix = 0;
2448 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002449
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002450 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002451 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002452 bx = nasm_reg_flags[b];
2453 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002454 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002455 bx = 0;
2456 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002457
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002458 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002460 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002461 int32_t o = input->offset;
2462 int mod, scale, index, base;
2463
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002464 /*
2465 * For a vector SIB, one has to be a vector and the other,
2466 * if present, a GPR. The vector must be the index operand.
2467 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002468 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002469 if (s == 0)
2470 s = 1;
2471 else if (s != 1)
2472 goto err;
2473
2474 t = bt, bt = it, it = t;
2475 x = bx, bx = ix, ix = x;
2476 }
2477
2478 if (bt != -1) {
2479 if (REG_GPR & ~bx)
2480 goto err;
2481 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2482 sok &= bx;
2483 else
2484 goto err;
2485 }
2486
2487 /*
2488 * While we're here, ensure the user didn't specify
2489 * WORD or QWORD
2490 */
2491 if (input->disp_size == 16 || input->disp_size == 64)
2492 goto err;
2493
2494 if (addrbits == 16 ||
2495 (addrbits == 32 && !(sok & BITS32)) ||
2496 (addrbits == 64 && !(sok & BITS64)))
2497 goto err;
2498
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002499 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2500 : ((ix & YMMREG & ~REG_EA)
2501 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002502
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002503 output->rex |= rexflags(it, ix, REX_X);
2504 output->rex |= rexflags(bt, bx, REX_B);
2505 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002506
2507 index = it & 7; /* it is known to be != -1 */
2508
2509 switch (s) {
2510 case 1:
2511 scale = 0;
2512 break;
2513 case 2:
2514 scale = 1;
2515 break;
2516 case 4:
2517 scale = 2;
2518 break;
2519 case 8:
2520 scale = 3;
2521 break;
2522 default: /* then what the smeg is it? */
2523 goto err; /* panic */
2524 }
2525
2526 if (bt == -1) {
2527 base = 5;
2528 mod = 0;
2529 } else {
2530 base = (bt & 7);
2531 if (base != REG_NUM_EBP && o == 0 &&
2532 seg == NO_SEG && !forw_ref &&
2533 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2534 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002535 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536 mod = 1;
2537 else
2538 mod = 2;
2539 }
2540
2541 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002542 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2543 output->modrm = GEN_MODRM(mod, rfield, 4);
2544 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002545 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 /*
2547 * it must be a 32/64-bit memory reference. Firstly we have
2548 * to check that all registers involved are type E/Rxx.
2549 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002550 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002551 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002552
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002553 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002554 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2555 sok &= ix;
2556 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002557 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002559
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002560 if (bt != -1) {
2561 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002563 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002565 sok &= bx;
2566 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002568 /*
2569 * While we're here, ensure the user didn't specify
2570 * WORD or QWORD
2571 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002572 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002573 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002575 if (addrbits == 16 ||
2576 (addrbits == 32 && !(sok & BITS32)) ||
2577 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002578 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002579
Keith Kaniosb7a89542007-04-12 02:40:54 +00002580 /* now reorganize base/index */
2581 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002582 ((hb == b && ht == EAH_NOTBASE) ||
2583 (hb == i && ht == EAH_MAKEBASE))) {
2584 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002585 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002586 x = bx, bx = ix, ix = x;
2587 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002588 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002589 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002590 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002591 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002592 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002593 }
2594 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2595 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002596 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002597 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2598 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002599 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002601 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002602 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002603 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002604 x = ix, ix = bx, bx = x;
2605 }
2606 if (it == REG_NUM_ESP ||
2607 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002608 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002609
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002610 output->rex |= rexflags(it, ix, REX_X);
2611 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002612
Keith Kanios48af1772007-08-17 07:37:52 +00002613 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002614 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002616
Keith Kaniosb7a89542007-04-12 02:40:54 +00002617 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002620 } else {
2621 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002622 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002623 seg == NO_SEG && !forw_ref &&
2624 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002625 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002626 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002627 mod = 1;
2628 else
2629 mod = 2;
2630 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002631
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002632 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002633 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2634 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002635 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002636 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002638
Keith Kaniosb7a89542007-04-12 02:40:54 +00002639 if (it == -1)
2640 index = 4, s = 1;
2641 else
2642 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002643
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 switch (s) {
2645 case 1:
2646 scale = 0;
2647 break;
2648 case 2:
2649 scale = 1;
2650 break;
2651 case 4:
2652 scale = 2;
2653 break;
2654 case 8:
2655 scale = 3;
2656 break;
2657 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002658 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002660
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661 if (bt == -1) {
2662 base = 5;
2663 mod = 0;
2664 } else {
2665 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002666 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 seg == NO_SEG && !forw_ref &&
2668 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002669 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002670 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002671 mod = 1;
2672 else
2673 mod = 2;
2674 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002675
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002676 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002677 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2678 output->modrm = GEN_MODRM(mod, rfield, 4);
2679 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 }
2681 } else { /* it's 16-bit */
2682 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002683 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002684
Keith Kaniosb7a89542007-04-12 02:40:54 +00002685 /* check for 64-bit long mode */
2686 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002687 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002688
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002690 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2691 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002692 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002693
Keith Kaniosb7a89542007-04-12 02:40:54 +00002694 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002695 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002696 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002697
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002699 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 if (b == -1 && i != -1) {
2701 int tmp = b;
2702 b = i;
2703 i = tmp;
2704 } /* swap */
2705 if ((b == R_SI || b == R_DI) && i != -1) {
2706 int tmp = b;
2707 b = i;
2708 i = tmp;
2709 }
2710 /* have BX/BP as base, SI/DI index */
2711 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002712 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 if (i != -1 && b != -1 &&
2714 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002715 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002716 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002717 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002718
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 rm = -1;
2720 if (i != -1)
2721 switch (i * 256 + b) {
2722 case R_SI * 256 + R_BX:
2723 rm = 0;
2724 break;
2725 case R_DI * 256 + R_BX:
2726 rm = 1;
2727 break;
2728 case R_SI * 256 + R_BP:
2729 rm = 2;
2730 break;
2731 case R_DI * 256 + R_BP:
2732 rm = 3;
2733 break;
2734 } else
2735 switch (b) {
2736 case R_SI:
2737 rm = 4;
2738 break;
2739 case R_DI:
2740 rm = 5;
2741 break;
2742 case R_BP:
2743 rm = 6;
2744 break;
2745 case R_BX:
2746 rm = 7;
2747 break;
2748 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002749 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002750 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002751
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002752 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2753 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002755 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 mod = 1;
2757 else
2758 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002759
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002760 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002761 output->bytes = mod; /* bytes of offset needed */
2762 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 }
2764 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002765 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002766
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002767 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002768 return output->type;
2769
2770err:
2771 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772}
2773
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002774static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002775{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002776 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002777 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002778
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002779 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002780
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002781 switch (ins->prefixes[PPS_ASIZE]) {
2782 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002783 valid &= 16;
2784 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002785 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002786 valid &= 32;
2787 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002788 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002789 valid &= 64;
2790 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002791 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002792 valid &= (addrbits == 32) ? 16 : 32;
2793 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002794 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002795 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002796 }
2797
2798 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 if (is_class(MEMORY, ins->oprs[j].type)) {
2800 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002801
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002802 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002803 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002804 i = 0;
2805 else
2806 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002807
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002808 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002809 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002810 b = 0;
2811 else
2812 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002813
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002814 if (ins->oprs[j].scale == 0)
2815 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002816
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if (!i && !b) {
2818 int ds = ins->oprs[j].disp_size;
2819 if ((addrbits != 64 && ds > 8) ||
2820 (addrbits == 64 && ds == 16))
2821 valid &= ds;
2822 } else {
2823 if (!(REG16 & ~b))
2824 valid &= 16;
2825 if (!(REG32 & ~b))
2826 valid &= 32;
2827 if (!(REG64 & ~b))
2828 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002829
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002830 if (!(REG16 & ~i))
2831 valid &= 16;
2832 if (!(REG32 & ~i))
2833 valid &= 32;
2834 if (!(REG64 & ~i))
2835 valid &= 64;
2836 }
2837 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002838 }
2839
2840 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002841 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002842 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002844 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002845 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002846 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 /* Impossible... */
2848 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2849 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002850 }
2851
2852 defdisp = ins->addr_size == 16 ? 16 : 32;
2853
2854 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2856 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2857 /*
2858 * mem_offs sizes must match the address size; if not,
2859 * strip the MEM_OFFS bit and match only EA instructions
2860 */
2861 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2862 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002863 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864}