blob: 100d131a653d7020880a228e569eb17896daafec [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;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700635 case P_EVEX:
636 /* EVEX */
637 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400638 case P_none:
639 break;
640 default:
641 error(ERR_PANIC, "invalid instruction prefix");
642 }
643 if (c != 0) {
644 out(offset, segment, &c, OUT_RAWDATA, 1,
645 NO_SEG, NO_SEG);
646 offset++;
647 }
648 }
649 insn_end = offset + insn_size;
650 gencode(segment, offset, bits, instruction,
651 temp, insn_end);
652 offset += insn_size;
653 if (itimes > 0 && itimes == instruction->times - 1) {
654 /*
655 * Dummy call to list->output to give the offset to the
656 * listing module.
657 */
658 list->output(offset, NULL, OUT_RAWDATA, 0);
659 list->uplevel(LIST_TIMES);
660 }
661 }
662 if (instruction->times > 1)
663 list->downlevel(LIST_TIMES);
664 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700665 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400666 /* No match */
667 switch (m) {
668 case MERR_OPSIZEMISSING:
669 error(ERR_NONFATAL, "operation size not specified");
670 break;
671 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400673 break;
674 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400676 break;
677 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800678 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400679 bits);
680 break;
681 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 error(ERR_NONFATAL,
683 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400684 break;
685 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000686 }
687 return 0;
688}
689
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700690int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400691 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000692{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000693 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700694 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000695
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000697 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000698
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400699 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700702 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
703 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400704 instruction->opcode == I_DT || instruction->opcode == I_DO ||
705 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300707 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300710 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000711
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400712 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000713 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400716 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400718 warn_overflow_const(e->offset, wsize);
719 } else if (e->type == EOT_DB_STRING ||
720 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000722
H. Peter Anvine2c80182005-01-15 22:15:51 +0000723 align = (-osize) % wsize;
724 if (align < 0)
725 align += wsize;
726 isize += osize + align;
727 }
728 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000729 }
730
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400732 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300734 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700735 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000736
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400737 fp = fopen(fname, "rb");
738 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
740 fname);
741 else if (fseek(fp, 0L, SEEK_END) < 0)
742 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
743 fname);
744 else {
745 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 if (instruction->eops->next) {
747 len -= instruction->eops->next->offset;
748 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700749 len > (size_t)instruction->eops->next->next->offset) {
750 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 }
752 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300753 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300755 if (fp)
756 fclose(fp);
757 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000758 }
759
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700760 /* Check to see if we need an address-size prefix */
761 add_asp(instruction, bits);
762
H. Peter Anvin23595f52009-07-25 17:44:25 -0700763 m = find_match(&temp, instruction, segment, offset, bits);
764 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400765 /* we've matched an instruction. */
766 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400767 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100768
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800769 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400770 if (isize < 0)
771 return -1;
772 for (j = 0; j < MAXPREFIX; j++) {
773 switch (instruction->prefixes[j]) {
774 case P_A16:
775 if (bits != 16)
776 isize++;
777 break;
778 case P_A32:
779 if (bits != 32)
780 isize++;
781 break;
782 case P_O16:
783 if (bits != 16)
784 isize++;
785 break;
786 case P_O32:
787 if (bits == 16)
788 isize++;
789 break;
790 case P_A64:
791 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700792 case P_EVEX:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400793 case P_none:
794 break;
795 default:
796 isize++;
797 break;
798 }
799 }
800 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700801 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400802 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000804}
805
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800806static void bad_hle_warn(const insn * ins, uint8_t hleok)
807{
808 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800809 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800810 static const enum whatwarn warn[2][4] =
811 {
812 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
813 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
814 };
815 unsigned int n;
816
817 n = (unsigned int)rep_pfx - P_XACQUIRE;
818 if (n > 1)
819 return; /* Not XACQUIRE/XRELEASE */
820
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800821 ww = warn[n][hleok];
822 if (!is_class(MEMORY, ins->oprs[0].type))
823 ww = w_inval; /* HLE requires operand 0 to be memory */
824
825 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800826 case w_none:
827 break;
828
829 case w_lock:
830 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800831 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800832 "%s with this instruction requires lock",
833 prefix_name(rep_pfx));
834 }
835 break;
836
837 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800838 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800839 "%s invalid with this instruction",
840 prefix_name(rep_pfx));
841 break;
842 }
843}
844
H. Peter Anvin507ae032008-10-09 15:37:10 -0700845/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400846#define case3(x) case (x): case (x)+1: case (x)+2
847#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700848
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800849static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800850 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000851{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800852 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800853 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000854 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000855 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700856 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700857 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700858 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700859 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800860 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800861 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700862 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000863
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700864 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700865 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700866 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700867
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700868 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400869 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700870
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 (void)segment; /* Don't warn that this parameter is unused */
872 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000873
H. Peter Anvin839eca22007-10-29 23:12:47 -0700874 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400875 c = *codes++;
876 op1 = (c & 3) + ((opex & 1) << 2);
877 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
878 opx = &ins->oprs[op1];
879 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700880
H. Peter Anvin839eca22007-10-29 23:12:47 -0700881 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400882 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 codes += c, length += c;
884 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700885
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400886 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400887 opex = c;
888 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700889
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400890 case4(010):
891 ins->rex |=
892 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 codes++, length++;
894 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700895
Jin Kyu Song164d6072013-10-15 19:10:13 -0700896 case4(014):
897 /* this is an index reg of MIB operand */
898 mib_index = opx->basereg;
899 break;
900
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400901 case4(020):
902 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 length++;
904 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700905
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400906 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 length += 2;
908 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700909
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400910 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700911 if (opx->type & (BITS16 | BITS32 | BITS64))
912 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 else
914 length += (bits == 16) ? 2 : 4;
915 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400917 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 length += 4;
919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400921 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700922 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 length++;
927 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400929 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000930 length += 8; /* MOV reg64/imm */
931 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400933 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 length += 2;
935 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400937 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700938 if (opx->type & (BITS16 | BITS32 | BITS64))
939 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 else
941 length += (bits == 16) ? 2 : 4;
942 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400944 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 length += 4;
946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400948 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700949 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 case 0172:
953 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700955 length++;
956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700958 case4(0174):
959 length++;
960 break;
961
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700962 case4(0240):
963 ins->rex |= REX_EV;
964 ins->vexreg = regval(opx);
965 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
966 ins->vex_cm = *codes++;
967 ins->vex_wlp = *codes++;
968 ins->evex_tuple = (*codes++ - 0300);
969 break;
970
971 case 0250:
972 ins->rex |= REX_EV;
973 ins->vexreg = 0;
974 ins->vex_cm = *codes++;
975 ins->vex_wlp = *codes++;
976 ins->evex_tuple = (*codes++ - 0300);
977 break;
978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(0254):
980 length += 4;
981 break;
982
983 case4(0260):
984 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700985 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400986 ins->vex_cm = *codes++;
987 ins->vex_wlp = *codes++;
988 break;
989
990 case 0270:
991 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700992 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 ins->vex_cm = *codes++;
994 ins->vex_wlp = *codes++;
995 break;
996
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400997 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800998 hleok = c & 3;
999 break;
1000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(0274):
1002 length++;
1003 break;
1004
1005 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 if (bits == 64)
1010 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001011 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
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 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001015 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
Keith Kaniosb7a89542007-04-12 02:40:54 +00001021 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1023 has_prefix(ins, PPS_ASIZE, P_A32))
1024 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001026
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 case4(0314):
1028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001031 {
1032 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1033 if (pfx == P_O16)
1034 break;
1035 if (pfx != P_none)
1036 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1037 else
1038 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001040 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001041
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001043 {
1044 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1045 if (pfx == P_O32)
1046 break;
1047 if (pfx != P_none)
1048 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1049 else
1050 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001052 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 case 0322:
1055 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001056
Keith Kaniosb7a89542007-04-12 02:40:54 +00001057 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001058 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001059 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001060
Keith Kaniosb7a89542007-04-12 02:40:54 +00001061 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 case 0325:
1066 ins->rex |= REX_NH;
1067 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001068
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001069 case 0326:
1070 break;
1071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 0330:
1073 codes++, length++;
1074 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001075
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001078
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001079 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 case 0333:
1081 length++;
1082 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001083
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001084 case 0334:
1085 ins->rex |= REX_L;
1086 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001087
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001088 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001091 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001092 if (!ins->prefixes[PPS_REP])
1093 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001096 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001097 if (!ins->prefixes[PPS_REP])
1098 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 if (ins->oprs[0].segment != NO_SEG)
1103 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1104 " quantity of BSS space");
1105 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001106 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001109 case 0341:
1110 if (!ins->prefixes[PPS_WAIT])
1111 ins->prefixes[PPS_WAIT] = P_WAIT;
1112 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001113
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 case 0360:
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001117 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 length++;
1119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 case 0364:
1122 case 0365:
1123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
Keith Kanios48af1772007-08-17 07:37:52 +00001125 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001126 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 length++;
1128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001130 case 0370:
1131 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 case 0373:
1135 length++;
1136 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001138 case 0374:
1139 eat = EA_XMMVSIB;
1140 break;
1141
1142 case 0375:
1143 eat = EA_YMMVSIB;
1144 break;
1145
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001146 case 0376:
1147 eat = EA_ZMMVSIB;
1148 break;
1149
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 case4(0100):
1151 case4(0110):
1152 case4(0120):
1153 case4(0130):
1154 case4(0200):
1155 case4(0204):
1156 case4(0210):
1157 case4(0214):
1158 case4(0220):
1159 case4(0224):
1160 case4(0230):
1161 case4(0234):
1162 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001164 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001165 opflags_t rflags;
1166 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001167 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001168
Keith Kaniosb7a89542007-04-12 02:40:54 +00001169 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001170
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 if (c <= 0177) {
1172 /* pick rfield from operand b (opx) */
1173 rflags = regflag(opx);
1174 rfield = nasm_regvals[opx->basereg];
1175 } else {
1176 rflags = 0;
1177 rfield = c & 7;
1178 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001179
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001180 /* EVEX.b1 : evex_brerop contains the operand position */
1181 op_er_sae = (ins->evex_brerop >= 0 ?
1182 &ins->oprs[ins->evex_brerop] : NULL);
1183
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001184 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1185 /* set EVEX.b */
1186 ins->evex_p[2] |= EVEX_P2B;
1187 if (op_er_sae->decoflags & ER) {
1188 /* set EVEX.RC (rounding control) */
1189 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1190 & EVEX_P2RC;
1191 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001192 } else {
1193 /* set EVEX.L'L (vector length) */
1194 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001195 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001196 /* set EVEX.b */
1197 ins->evex_p[2] |= EVEX_P2B;
1198 }
1199 }
1200
Jin Kyu Song164d6072013-10-15 19:10:13 -07001201 /*
1202 * if a separate form of MIB (ICC style) is used,
1203 * the index reg info is merged into mem operand
1204 */
1205 if (mib_index != R_none) {
1206 opy->indexreg = mib_index;
1207 opy->scale = 1;
1208 opy->hintbase = mib_index;
1209 opy->hinttype = EAH_NOTBASE;
1210 }
1211
Jin Kyu Song3b653232013-11-08 11:41:12 -08001212 /*
1213 * only for mib operands, make a single reg index [reg*1].
1214 * gas uses this form to explicitly denote index register.
1215 */
1216 if ((temp->flags & IF_MIB) &&
1217 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1218 opy->hinttype == EAH_NOTBASE)) {
1219 opy->indexreg = opy->basereg;
1220 opy->basereg = -1;
1221 opy->scale = 1;
1222 }
1223
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001224 if (process_ea(opy, &ea_data, bits,
1225 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 errfunc(ERR_NONFATAL, "invalid effective address");
1227 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001228 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001229 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001231 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 }
1233 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001234
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001235 default:
1236 errfunc(ERR_PANIC, "internal instruction table corrupt"
1237 ": instruction code \\%o (0x%02X) given", c, c);
1238 break;
1239 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001240 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001241
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001242 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001243
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001244 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001245 if (ins->rex & REX_H) {
1246 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1247 return -1;
1248 }
1249 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001250 }
1251
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001252 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001254
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001256 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 return -1;
1258 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001259 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001261 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 ins->rex &= ~REX_W;
1263 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001264 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 ins->rex |= REX_W;
1266 bad32 &= ~REX_W;
1267 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001268 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 /* Follow REX_W */
1270 break;
1271 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001272
H. Peter Anvinfc561202011-07-07 16:58:22 -07001273 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001274 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1275 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001276 } else if (!(ins->rex & REX_EV) &&
1277 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1278 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1279 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001281 if (ins->rex & REX_EV)
1282 length += 4;
1283 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 length += 3;
1285 else
1286 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001287 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 if (ins->rex & REX_H) {
1289 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1290 return -1;
1291 } else if (bits == 64) {
1292 length++;
1293 } else if ((ins->rex & REX_L) &&
1294 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1295 cpu >= IF_X86_64) {
1296 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001297 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001298 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 length++;
1300 } else {
1301 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1302 return -1;
1303 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001304 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001305
1306 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1307 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001308 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001309 "instruction is not lockable");
1310 }
1311
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001312 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001313
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001314 return length;
1315}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001316
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001317static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1318{
1319 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001320 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001321 ins->rex = (ins->rex & REX_REAL) | REX_P;
1322 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1323 ins->rex = 0;
1324 return 1;
1325 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001326 }
1327
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001328 return 0;
1329}
1330
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001331static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001332 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001334{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001335 uint8_t c;
1336 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001337 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001338 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001339 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001340 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001341 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001342 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001343 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001344
H. Peter Anvin839eca22007-10-29 23:12:47 -07001345 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 c = *codes++;
1347 op1 = (c & 3) + ((opex & 1) << 2);
1348 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1349 opx = &ins->oprs[op1];
1350 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001351
H. Peter Anvin839eca22007-10-29 23:12:47 -07001352 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 case 01:
1354 case 02:
1355 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001357 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001358 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 codes += c;
1360 offset += c;
1361 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001362
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 case 05:
1364 case 06:
1365 case 07:
1366 opex = c;
1367 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001368
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001370 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001371 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001372 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 offset += 1;
1374 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375
Jin Kyu Song164d6072013-10-15 19:10:13 -07001376 case4(014):
1377 break;
1378
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001380 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001381 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001384 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 offset += 1;
1386 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001388 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001389 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001390 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001391 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001392 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 offset += 1;
1394 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001396 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001397 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001398 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001399 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001400 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 offset += 2;
1402 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001403
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001404 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001405 if (opx->type & (BITS16 | BITS32))
1406 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 else
1408 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001409 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001410 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001411 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001412 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 offset += size;
1414 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001415
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001416 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001417 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001418 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001419 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001420 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 offset += 4;
1422 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001423
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001424 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001426 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001427 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001428 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001429 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 offset += size;
1431 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001432
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001433 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001434 if (opx->segment != segment) {
1435 data = opx->offset;
1436 out(offset, segment, &data,
1437 OUT_REL1ADR, insn_end - offset,
1438 opx->segment, opx->wrt);
1439 } else {
1440 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001441 if (data > 127 || data < -128)
1442 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001443 out(offset, segment, &data,
1444 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1445 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 offset += 1;
1447 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001448
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001449 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001450 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001451 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001452 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001453 offset += 8;
1454 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001455
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 if (opx->segment != segment) {
1458 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001460 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001461 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001463 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001465 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 }
1467 offset += 2;
1468 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001469
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001470 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001471 if (opx->type & (BITS16 | BITS32 | BITS64))
1472 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 else
1474 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001475 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001476 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001477 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001478 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1479 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001481 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001483 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 }
1485 offset += size;
1486 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001489 if (opx->segment != segment) {
1490 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001492 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001493 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001495 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001497 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 }
1499 offset += 4;
1500 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001501
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001502 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001503 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001504 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1505 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001507 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001508 outfmt->segbase(1 + opx->segment),
1509 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001510 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001512
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001513 case 0172:
1514 c = *codes++;
1515 opx = &ins->oprs[c >> 3];
1516 bytes[0] = nasm_regvals[opx->basereg] << 4;
1517 opx = &ins->oprs[c & 7];
1518 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1519 errfunc(ERR_NONFATAL,
1520 "non-absolute expression not permitted as argument %d",
1521 c & 7);
1522 } else {
1523 if (opx->offset & ~15) {
1524 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1525 "four-bit argument exceeds bounds");
1526 }
1527 bytes[0] |= opx->offset & 15;
1528 }
1529 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1530 offset++;
1531 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001532
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 case 0173:
1534 c = *codes++;
1535 opx = &ins->oprs[c >> 4];
1536 bytes[0] = nasm_regvals[opx->basereg] << 4;
1537 bytes[0] |= c & 15;
1538 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1539 offset++;
1540 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001541
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001542 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 bytes[0] = nasm_regvals[opx->basereg] << 4;
1544 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1545 offset++;
1546 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001547
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001549 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001550 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1551 (int32_t)data != (int64_t)data) {
1552 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1553 "signed dword immediate exceeds bounds");
1554 }
1555 out(offset, segment, &data, OUT_ADDRESS, 4,
1556 opx->segment, opx->wrt);
1557 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001558 break;
1559
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001560 case4(0240):
1561 case 0250:
1562 codes += 3;
1563 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1564 EVEX_P2Z | EVEX_P2AAA, 2);
1565 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1566 bytes[0] = 0x62;
1567 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001568 bytes[1] = ((((ins->rex & 7) << 5) |
1569 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1570 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001571 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1572 ((~ins->vexreg & 15) << 3) |
1573 (1 << 2) | (ins->vex_wlp & 3);
1574 bytes[3] = ins->evex_p[2];
1575 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1576 offset += 4;
1577 break;
1578
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001579 case4(0260):
1580 case 0270:
1581 codes += 2;
1582 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1583 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1584 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1585 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001586 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1588 offset += 3;
1589 } else {
1590 bytes[0] = 0xc5;
1591 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001592 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1594 offset += 2;
1595 }
1596 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001597
H. Peter Anvine014f352012-02-25 22:35:19 -08001598 case 0271:
1599 case 0272:
1600 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001601 break;
1602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(0274):
1604 {
1605 uint64_t uv, um;
1606 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 if (ins->rex & REX_W)
1609 s = 64;
1610 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1611 s = 16;
1612 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1613 s = 32;
1614 else
1615 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 um = (uint64_t)2 << (s-1);
1618 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001619
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 if (uv > 127 && uv < (uint64_t)-128 &&
1621 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001622 /* If this wasn't explicitly byte-sized, warn as though we
1623 * had fallen through to the imm16/32/64 case.
1624 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001625 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001626 "%s value exceeds bounds",
1627 (opx->type & BITS8) ? "signed byte" :
1628 s == 16 ? "word" :
1629 s == 32 ? "dword" :
1630 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001632 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001633 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001634 out(offset, segment, &data, OUT_ADDRESS, 1,
1635 opx->segment, opx->wrt);
1636 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001637 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001638 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1639 NO_SEG);
1640 }
1641 offset += 1;
1642 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001644
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001647
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001649 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001651 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 offset += 1;
1653 } else
1654 offset += 0;
1655 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001656
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001658 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001660 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 offset += 1;
1662 } else
1663 offset += 0;
1664 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001665
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 case 0312:
1667 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001668
Keith Kaniosb7a89542007-04-12 02:40:54 +00001669 case 0313:
1670 ins->rex = 0;
1671 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 case4(0314):
1674 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001679
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001681 case 0323:
1682 break;
1683
Keith Kaniosb7a89542007-04-12 02:40:54 +00001684 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001685 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001687
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 case 0325:
1689 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001690
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001691 case 0326:
1692 break;
1693
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001695 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001696 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 offset += 1;
1698 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001699
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001702
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001703 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001705 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001706 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 offset += 1;
1708 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001709
Keith Kanios48af1772007-08-17 07:37:52 +00001710 case 0334:
1711 if (ins->rex & REX_R) {
1712 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001713 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001714 offset += 1;
1715 }
1716 ins->rex &= ~(REX_L|REX_R);
1717 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001718
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001719 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001720 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001721
H. Peter Anvin962e3052008-08-28 17:47:16 -07001722 case 0336:
1723 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001725
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 if (ins->oprs[0].segment != NO_SEG)
1728 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1729 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001730 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 if (size > 0)
1732 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001733 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 offset += size;
1735 }
1736 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001737
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 case 0341:
1739 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001740
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001741 case 0360:
1742 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001743
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 case 0361:
1745 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001746 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1747 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001749
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 case 0364:
1751 case 0365:
1752 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001753
Keith Kanios48af1772007-08-17 07:37:52 +00001754 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001756 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001757 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001758 offset += 1;
1759 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001760
Jin Kyu Song03041092013-10-15 19:38:51 -07001761 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001763
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 case 0373:
1765 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001766 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 offset += 1;
1768 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001769
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001770 case 0374:
1771 eat = EA_XMMVSIB;
1772 break;
1773
1774 case 0375:
1775 eat = EA_YMMVSIB;
1776 break;
1777
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001778 case 0376:
1779 eat = EA_ZMMVSIB;
1780 break;
1781
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001782 case4(0100):
1783 case4(0110):
1784 case4(0120):
1785 case4(0130):
1786 case4(0200):
1787 case4(0204):
1788 case4(0210):
1789 case4(0214):
1790 case4(0220):
1791 case4(0224):
1792 case4(0230):
1793 case4(0234):
1794 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 ea ea_data;
1796 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001797 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001798 uint8_t *p;
1799 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001801
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001802 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001803 /* pick rfield from operand b (opx) */
1804 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001805 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 } else {
1807 /* rfield is constant */
1808 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001810 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001812 if (process_ea(opy, &ea_data, bits,
1813 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001815
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 p = bytes;
1817 *p++ = ea_data.modrm;
1818 if (ea_data.sib_present)
1819 *p++ = ea_data.sib;
1820
1821 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001822 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001824 /*
1825 * Make sure the address gets the right offset in case
1826 * the line breaks in the .lst file (BR 1197827)
1827 */
1828 offset += s;
1829 s = 0;
1830
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 switch (ea_data.bytes) {
1832 case 0:
1833 break;
1834 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case 2:
1836 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001837 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001838 /* use compressed displacement, if available */
1839 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 if (ea_data.rip) {
1842 if (opy->segment == segment) {
1843 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001844 if (overflow_signed(data, ea_data.bytes))
1845 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 out(offset, segment, &data, OUT_ADDRESS,
1847 ea_data.bytes, NO_SEG, NO_SEG);
1848 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001849 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 out(offset, segment, &data, OUT_REL4ADR,
1851 insn_end - offset, opy->segment, opy->wrt);
1852 }
1853 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001854 if (overflow_general(data, ins->addr_size >> 3) ||
1855 signed_bits(data, ins->addr_size) !=
1856 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001857 warn_overflow(ERR_PASS2, ea_data.bytes);
1858
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 out(offset, segment, &data, OUT_ADDRESS,
1860 ea_data.bytes, opy->segment, opy->wrt);
1861 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001863 default:
1864 /* Impossible! */
1865 errfunc(ERR_PANIC,
1866 "Invalid amount of bytes (%d) for offset?!",
1867 ea_data.bytes);
1868 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 }
1870 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 }
1872 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 default:
1875 errfunc(ERR_PANIC, "internal instruction table corrupt"
1876 ": instruction code \\%o (0x%02X) given", c, c);
1877 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001879 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001880}
1881
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001882static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001883{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001884 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001885 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001886 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001887}
1888
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001889static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001890{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001891 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001893 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001894}
1895
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001896static int op_rexflags(const operand * o, int mask)
1897{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001898 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001899 int val;
1900
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001901 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001902 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001903
H. Peter Anvina4835d42008-05-20 14:21:29 -07001904 flags = nasm_reg_flags[o->basereg];
1905 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001906
1907 return rexflags(val, flags, mask);
1908}
1909
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001910static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001911{
1912 int rex = 0;
1913
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001914 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001916 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 rex |= REX_W;
1918 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1919 rex |= REX_H;
1920 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1921 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001922
1923 return rex & mask;
1924}
1925
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001926static int evexflags(int val, decoflags_t deco,
1927 int mask, uint8_t byte)
1928{
1929 int evex = 0;
1930
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001931 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001932 case 0:
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_P0RP | EVEX_P0X);
1935 break;
1936 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001937 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001938 evex |= EVEX_P2VP;
1939 if (deco & Z)
1940 evex |= EVEX_P2Z;
1941 if (deco & OPMASK_MASK)
1942 evex |= deco & EVEX_P2AAA;
1943 break;
1944 }
1945 return evex & mask;
1946}
1947
1948static int op_evexflags(const operand * o, int mask, uint8_t byte)
1949{
1950 int val;
1951
1952 if (!is_register(o->basereg))
1953 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1954
1955 val = nasm_regvals[o->basereg];
1956
1957 return evexflags(val, o->decoflags, mask, byte);
1958}
1959
H. Peter Anvin23595f52009-07-25 17:44:25 -07001960static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 insn *instruction,
1962 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001963{
1964 const struct itemplate *temp;
1965 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001966 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001967 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001968 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001969 int i;
1970
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001971 /* broadcasting uses a different data element size */
1972 for (i = 0; i < instruction->operands; i++)
1973 if (i == broadcast)
1974 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1975 else
1976 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001977
1978 merr = MERR_INVALOP;
1979
1980 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001981 temp->opcode != I_none; temp++) {
1982 m = matches(temp, instruction, bits);
1983 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001984 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001985 m = MOK_GOOD;
1986 else
1987 m = MERR_INVALOP;
1988 } else if (m == MERR_OPSIZEMISSING &&
1989 (temp->flags & IF_SMASK) != IF_SX) {
1990 /*
1991 * Missing operand size and a candidate for fuzzy matching...
1992 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001993 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001994 if (i == broadcast)
1995 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
1996 else
1997 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001998 opsizemissing = true;
1999 }
2000 if (m > merr)
2001 merr = m;
2002 if (merr == MOK_GOOD)
2003 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002004 }
2005
2006 /* No match, but see if we can get a fuzzy operand size match... */
2007 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002008 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002009
2010 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002011 /*
2012 * We ignore extrinsic operand sizes on registers, so we should
2013 * never try to fuzzy-match on them. This also resolves the case
2014 * when we have e.g. "xmmrm128" in two different positions.
2015 */
2016 if (is_class(REGISTER, instruction->oprs[i].type))
2017 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002019 /* This tests if xsizeflags[i] has more than one bit set */
2020 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2021 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002022
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002023 if (i == broadcast)
2024 instruction->oprs[i].decoflags |= xsizeflags[i];
2025 else
2026 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002027 }
2028
2029 /* Try matching again... */
2030 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002031 temp->opcode != I_none; temp++) {
2032 m = matches(temp, instruction, bits);
2033 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002034 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 m = MOK_GOOD;
2036 else
2037 m = MERR_INVALOP;
2038 }
2039 if (m > merr)
2040 merr = m;
2041 if (merr == MOK_GOOD)
2042 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002043 }
2044
H. Peter Anvina81655b2009-07-25 18:15:28 -07002045done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002046 *tempp = temp;
2047 return merr;
2048}
2049
H. Peter Anvin65289e82009-07-25 17:25:11 -07002050static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002051 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002052{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002053 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002054 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002055 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002056
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002057 /*
2058 * Check the opcode
2059 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002060 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002061 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002062
2063 /*
2064 * Count the operands
2065 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002067 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002068
2069 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002070 * Is it legal?
2071 */
2072 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2073 return MERR_INVALOP;
2074
2075 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002076 * Check that no spurious colons or TOs are present
2077 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 for (i = 0; i < itemp->operands; i++)
2079 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002080 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002081
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002082 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002083 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002084 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002085 switch (itemp->flags & IF_SMASK) {
2086 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002087 asize = BITS8;
2088 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002089 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002090 asize = BITS16;
2091 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002092 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002093 asize = BITS32;
2094 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002095 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002096 asize = BITS64;
2097 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002098 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002099 asize = BITS128;
2100 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002101 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002102 asize = BITS256;
2103 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002104 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002105 asize = BITS512;
2106 break;
2107 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002108 switch (bits) {
2109 case 16:
2110 asize = BITS16;
2111 break;
2112 case 32:
2113 asize = BITS32;
2114 break;
2115 case 64:
2116 asize = BITS64;
2117 break;
2118 default:
2119 asize = 0;
2120 break;
2121 }
2122 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002123 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002124 asize = 0;
2125 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002126 }
2127
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002128 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002129 /* S- flags only apply to a specific operand */
2130 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2131 memset(size, 0, sizeof size);
2132 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002133 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002134 /* S- flags apply to all operands */
2135 for (i = 0; i < MAX_OPERANDS; i++)
2136 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002137 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002138
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002139 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002140 * Check that the operand flags all match up,
2141 * it's a bit tricky so lets be verbose:
2142 *
2143 * 1) Find out the size of operand. If instruction
2144 * doesn't have one specified -- we're trying to
2145 * guess it either from template (IF_S* flag) or
2146 * from code bits.
2147 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002148 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002149 * template has an operand size specified AND this size differ
2150 * from which instruction has (perhaps we got it from code bits)
2151 * we are:
2152 * a) Check that only size of instruction and operand is differ
2153 * other characteristics do match
2154 * b) Perhaps it's a register specified in instruction so
2155 * for such a case we just mark that operand as "size
2156 * missing" and this will turn on fuzzy operand size
2157 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002158 */
2159 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002160 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002161 decoflags_t deco = instruction->oprs[i].decoflags;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002162 if (!(type & SIZE_MASK))
2163 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002164
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002165 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2166 (itemp->deco[i] & deco) != deco) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002167 return MERR_INVALOP;
2168 } else if ((itemp->opd[i] & SIZE_MASK) &&
2169 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2170 if (type & SIZE_MASK) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002171 /*
2172 * when broadcasting, the element size depends on
2173 * the instruction type. decorator flag should match.
2174 */
2175#define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2176 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2177 if (!((deco & BRDCAST_MASK) &&
2178 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2179 return MERR_INVALOP;
2180 }
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002181 } else if (!is_class(REGISTER, type)) {
2182 /*
2183 * Note: we don't honor extrinsic operand sizes for registers,
2184 * so "missing operand size" for a register should be
2185 * considered a wildcard match rather than an error.
2186 */
2187 opsizemissing = true;
2188 }
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002189 } else if (is_register(instruction->oprs[i].basereg) &&
2190 nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
Jin Kyu Songdd1c0c12013-09-13 14:12:56 -07002191 !(itemp->flags & IF_AVX512)) {
Jin Kyu Song66c61922013-08-26 20:28:43 -07002192 return MERR_ENCMISMATCH;
Jin Kyu Song945b1b82013-10-25 19:29:53 -07002193 } else if (instruction->prefixes[PPS_EVEX] &&
2194 !(itemp->flags & IF_AVX512)) {
2195 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002196 }
2197 }
2198
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002199 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002200 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002201
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002202 /*
2203 * Check operand sizes
2204 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002205 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002208 asize = itemp->opd[i] & SIZE_MASK;
2209 if (asize) {
2210 for (i = 0; i < oprs; i++)
2211 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 break;
2213 }
2214 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002215 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002216 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002217 }
2218
Keith Kaniosb7a89542007-04-12 02:40:54 +00002219 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 if (!(itemp->opd[i] & SIZE_MASK) &&
2221 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002222 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002223 }
2224
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002225 /*
2226 * Check template is okay at the set cpu level
2227 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002228 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002229 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002230
Keith Kaniosb7a89542007-04-12 02:40:54 +00002231 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002232 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002233 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002234 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002235 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002237 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002238 * If we have a HLE prefix, look for the NOHLE flag
2239 */
2240 if ((itemp->flags & IF_NOHLE) &&
2241 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2242 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2243 return MERR_BADHLE;
2244
2245 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002246 * Check if special handling needed for Jumps
2247 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002248 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250
Jin Kyu Song03041092013-10-15 19:38:51 -07002251 /*
2252 * Check if BND prefix is allowed
2253 */
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08002254 if ((IF_BND & ~itemp->flags) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002255 has_prefix(instruction, PPS_REP, P_BND))
2256 return MERR_BADBND;
2257
H. Peter Anvin60926242009-07-26 16:25:38 -07002258 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002259}
2260
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002261/*
2262 * Check if offset is a multiple of N with corresponding tuple type
2263 * if Disp8*N is available, compressed displacement is stored in compdisp
2264 */
2265static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2266{
2267 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2268 {{16, 32, 64}, {8, 8, 8}}};
2269 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2270 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2271
2272 bool evex_b = input->decoflags & BRDCAST_MASK;
2273 enum ttypes tuple = ins->evex_tuple;
2274 /* vex_wlp composed as [wwllpp] */
2275 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2276 /* wig(=2) is treated as w0(=0) */
2277 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2278 int32_t off = input->offset;
2279 uint8_t n = 0;
2280 int32_t disp8;
2281
2282 switch(tuple) {
2283 case FV:
2284 n = fv_n[evex_w][evex_b][vectlen];
2285 break;
2286 case HV:
2287 n = hv_n[evex_b][vectlen];
2288 break;
2289
2290 case FVM:
2291 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2292 n = 1 << (vectlen + 4);
2293 break;
2294 case T1S8: /* N = 1 */
2295 case T1S16: /* N = 2 */
2296 n = tuple - T1S8 + 1;
2297 break;
2298 case T1S:
2299 /* N = 4 for 32bit, 8 for 64bit */
2300 n = evex_w ? 8 : 4;
2301 break;
2302 case T1F32:
2303 case T1F64:
2304 /* N = 4 for 32bit, 8 for 64bit */
2305 n = (tuple == T1F32 ? 4 : 8);
2306 break;
2307 case T2:
2308 case T4:
2309 case T8:
2310 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2311 n = 0;
2312 else
Jin Kyu Songd2d9c3e2013-08-26 20:28:41 -07002313 n = 1 << (tuple - T2 + evex_w + 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002314 break;
2315 case HVM:
2316 case QVM:
2317 case OVM:
2318 n = 1 << (OVM - tuple + vectlen + 1);
2319 break;
2320 case M128:
2321 n = 16;
2322 break;
2323 case DUP:
2324 n = dup_n[vectlen];
2325 break;
2326
2327 default:
2328 break;
2329 }
2330
2331 if (n && !(off & (n - 1))) {
2332 disp8 = off / n;
2333 /* if it fits in Disp8 */
2334 if (disp8 >= -128 && disp8 <= 127) {
2335 *compdisp = disp8;
2336 return true;
2337 }
2338 }
2339
2340 *compdisp = 0;
2341 return false;
2342}
2343
2344/*
2345 * Check if ModR/M.mod should/can be 01.
2346 * - EAF_BYTEOFFS is set
2347 * - offset can fit in a byte when EVEX is not used
2348 * - offset can be compressed when EVEX is used
2349 */
2350#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2351 (o >= -128 && o <= 127 && \
2352 seg == NO_SEG && !forw_ref && \
2353 !(input->eaflags & EAF_WORDOFFS) && \
2354 !(ins->rex & REX_EV)) || \
2355 (ins->rex & REX_EV && \
2356 is_disp8n(input, ins, &output->disp8)))
2357
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002358static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002359 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002360{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002361 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002362 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002363
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002364 output->type = EA_SCALAR;
2365 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002366 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002367
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002368 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002369 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002370 /* EVEX.R' flag for the REG operand */
2371 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002372
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002373 if (is_class(REGISTER, input->type)) {
2374 /*
2375 * It's a direct register.
2376 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002377 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002378 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002379
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002380 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002381 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002382
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002383 /* broadcasting is not available with a direct register operand. */
2384 if (input->decoflags & BRDCAST_MASK) {
2385 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2386 goto err;
2387 }
2388
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002389 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002390 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002391 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002392 output->bytes = 0; /* no offset necessary either */
2393 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2394 } else {
2395 /*
2396 * It's a memory reference.
2397 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002398
2399 /* Embedded rounding or SAE is not available with a mem ref operand. */
2400 if (input->decoflags & (ER | SAE)) {
2401 nasm_error(ERR_NONFATAL,
2402 "Embedded rounding is available only with reg-reg op.");
2403 return -1;
2404 }
2405
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002406 if (input->basereg == -1 &&
2407 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002408 /*
2409 * It's a pure offset.
2410 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002411 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2412 input->segment == NO_SEG) {
2413 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2414 input->type &= ~IP_REL;
2415 input->type |= MEMORY;
2416 }
2417
2418 if (input->eaflags & EAF_BYTEOFFS ||
2419 (input->eaflags & EAF_WORDOFFS &&
2420 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2421 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2422 }
2423
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002424 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002425 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 output->sib = GEN_SIB(0, 4, 5);
2427 output->bytes = 4;
2428 output->modrm = GEN_MODRM(0, rfield, 4);
2429 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002430 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002431 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002432 output->bytes = (addrbits != 16 ? 4 : 2);
2433 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2434 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002435 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002436 } else {
2437 /*
2438 * It's an indirection.
2439 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002441 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002443 int t, it, bt; /* register numbers */
2444 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 if (s == 0)
2447 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002448
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002449 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002450 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002451 ix = nasm_reg_flags[i];
2452 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002453 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002454 ix = 0;
2455 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002456
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002457 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002458 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002459 bx = nasm_reg_flags[b];
2460 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002461 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002462 bx = 0;
2463 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002464
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002465 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002466 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002467 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002468 int32_t o = input->offset;
2469 int mod, scale, index, base;
2470
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002471 /*
2472 * For a vector SIB, one has to be a vector and the other,
2473 * if present, a GPR. The vector must be the index operand.
2474 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002475 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002476 if (s == 0)
2477 s = 1;
2478 else if (s != 1)
2479 goto err;
2480
2481 t = bt, bt = it, it = t;
2482 x = bx, bx = ix, ix = x;
2483 }
2484
2485 if (bt != -1) {
2486 if (REG_GPR & ~bx)
2487 goto err;
2488 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2489 sok &= bx;
2490 else
2491 goto err;
2492 }
2493
2494 /*
2495 * While we're here, ensure the user didn't specify
2496 * WORD or QWORD
2497 */
2498 if (input->disp_size == 16 || input->disp_size == 64)
2499 goto err;
2500
2501 if (addrbits == 16 ||
2502 (addrbits == 32 && !(sok & BITS32)) ||
2503 (addrbits == 64 && !(sok & BITS64)))
2504 goto err;
2505
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002506 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2507 : ((ix & YMMREG & ~REG_EA)
2508 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002509
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002510 output->rex |= rexflags(it, ix, REX_X);
2511 output->rex |= rexflags(bt, bx, REX_B);
2512 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002513
2514 index = it & 7; /* it is known to be != -1 */
2515
2516 switch (s) {
2517 case 1:
2518 scale = 0;
2519 break;
2520 case 2:
2521 scale = 1;
2522 break;
2523 case 4:
2524 scale = 2;
2525 break;
2526 case 8:
2527 scale = 3;
2528 break;
2529 default: /* then what the smeg is it? */
2530 goto err; /* panic */
2531 }
2532
2533 if (bt == -1) {
2534 base = 5;
2535 mod = 0;
2536 } else {
2537 base = (bt & 7);
2538 if (base != REG_NUM_EBP && o == 0 &&
2539 seg == NO_SEG && !forw_ref &&
2540 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2541 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002542 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002543 mod = 1;
2544 else
2545 mod = 2;
2546 }
2547
2548 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002549 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2550 output->modrm = GEN_MODRM(mod, rfield, 4);
2551 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002552 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 /*
2554 * it must be a 32/64-bit memory reference. Firstly we have
2555 * to check that all registers involved are type E/Rxx.
2556 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002557 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002559
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002560 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002561 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2562 sok &= ix;
2563 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002565 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002566
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002567 if (bt != -1) {
2568 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002569 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002570 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002571 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002572 sok &= bx;
2573 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002575 /*
2576 * While we're here, ensure the user didn't specify
2577 * WORD or QWORD
2578 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002579 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002582 if (addrbits == 16 ||
2583 (addrbits == 32 && !(sok & BITS32)) ||
2584 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002585 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002586
Keith Kaniosb7a89542007-04-12 02:40:54 +00002587 /* now reorganize base/index */
2588 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 ((hb == b && ht == EAH_NOTBASE) ||
2590 (hb == i && ht == EAH_MAKEBASE))) {
2591 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002592 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002593 x = bx, bx = ix, ix = x;
2594 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002595 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002596 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002597 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002598 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002599 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002600 }
2601 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2602 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002603 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002604 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2605 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002606 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002608 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002610 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002611 x = ix, ix = bx, bx = x;
2612 }
2613 if (it == REG_NUM_ESP ||
2614 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002615 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002617 output->rex |= rexflags(it, ix, REX_X);
2618 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002619
Keith Kanios48af1772007-08-17 07:37:52 +00002620 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002623
Keith Kaniosb7a89542007-04-12 02:40:54 +00002624 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002627 } else {
2628 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002629 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002630 seg == NO_SEG && !forw_ref &&
2631 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002632 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002633 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002634 mod = 1;
2635 else
2636 mod = 2;
2637 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002638
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002639 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002640 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2641 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002642 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002645
Keith Kaniosb7a89542007-04-12 02:40:54 +00002646 if (it == -1)
2647 index = 4, s = 1;
2648 else
2649 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 switch (s) {
2652 case 1:
2653 scale = 0;
2654 break;
2655 case 2:
2656 scale = 1;
2657 break;
2658 case 4:
2659 scale = 2;
2660 break;
2661 case 8:
2662 scale = 3;
2663 break;
2664 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002665 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002667
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668 if (bt == -1) {
2669 base = 5;
2670 mod = 0;
2671 } else {
2672 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002673 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002674 seg == NO_SEG && !forw_ref &&
2675 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002676 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002677 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002678 mod = 1;
2679 else
2680 mod = 2;
2681 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002682
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002683 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002684 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2685 output->modrm = GEN_MODRM(mod, rfield, 4);
2686 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 }
2688 } else { /* it's 16-bit */
2689 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002690 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002691
Keith Kaniosb7a89542007-04-12 02:40:54 +00002692 /* check for 64-bit long mode */
2693 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002694 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002695
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002697 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2698 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002699 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002700
Keith Kaniosb7a89542007-04-12 02:40:54 +00002701 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002702 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002703 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002704
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002706 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 if (b == -1 && i != -1) {
2708 int tmp = b;
2709 b = i;
2710 i = tmp;
2711 } /* swap */
2712 if ((b == R_SI || b == R_DI) && i != -1) {
2713 int tmp = b;
2714 b = i;
2715 i = tmp;
2716 }
2717 /* have BX/BP as base, SI/DI index */
2718 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002719 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 if (i != -1 && b != -1 &&
2721 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002722 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002724 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 rm = -1;
2727 if (i != -1)
2728 switch (i * 256 + b) {
2729 case R_SI * 256 + R_BX:
2730 rm = 0;
2731 break;
2732 case R_DI * 256 + R_BX:
2733 rm = 1;
2734 break;
2735 case R_SI * 256 + R_BP:
2736 rm = 2;
2737 break;
2738 case R_DI * 256 + R_BP:
2739 rm = 3;
2740 break;
2741 } else
2742 switch (b) {
2743 case R_SI:
2744 rm = 4;
2745 break;
2746 case R_DI:
2747 rm = 5;
2748 break;
2749 case R_BP:
2750 rm = 6;
2751 break;
2752 case R_BX:
2753 rm = 7;
2754 break;
2755 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002756 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002757 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002758
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002759 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2760 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002762 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 mod = 1;
2764 else
2765 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002766
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002767 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002768 output->bytes = mod; /* bytes of offset needed */
2769 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 }
2771 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002773
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002774 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002775 return output->type;
2776
2777err:
2778 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002779}
2780
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002781static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002782{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002783 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002784 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002785
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002786 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002787
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002788 switch (ins->prefixes[PPS_ASIZE]) {
2789 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002790 valid &= 16;
2791 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002792 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002793 valid &= 32;
2794 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002795 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002796 valid &= 64;
2797 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002798 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 valid &= (addrbits == 32) ? 16 : 32;
2800 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002801 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002802 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002803 }
2804
2805 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002806 if (is_class(MEMORY, ins->oprs[j].type)) {
2807 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002810 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 i = 0;
2812 else
2813 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002814
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002815 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002816 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 b = 0;
2818 else
2819 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002820
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002821 if (ins->oprs[j].scale == 0)
2822 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002823
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002824 if (!i && !b) {
2825 int ds = ins->oprs[j].disp_size;
2826 if ((addrbits != 64 && ds > 8) ||
2827 (addrbits == 64 && ds == 16))
2828 valid &= ds;
2829 } else {
2830 if (!(REG16 & ~b))
2831 valid &= 16;
2832 if (!(REG32 & ~b))
2833 valid &= 32;
2834 if (!(REG64 & ~b))
2835 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002836
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 if (!(REG16 & ~i))
2838 valid &= 16;
2839 if (!(REG32 & ~i))
2840 valid &= 32;
2841 if (!(REG64 & ~i))
2842 valid &= 64;
2843 }
2844 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002845 }
2846
2847 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002848 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002849 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002850 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002851 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002852 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002853 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002854 /* Impossible... */
2855 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2856 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002857 }
2858
2859 defdisp = ins->addr_size == 16 ? 16 : 32;
2860
2861 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2863 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2864 /*
2865 * mem_offs sizes must match the address size; if not,
2866 * strip the MEM_OFFS bit and match only EA instructions
2867 */
2868 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2869 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002870 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002871}