blob: c0e316ae6f03c5e6af5ab04f57009bd8ff994ea3 [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
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070045 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040046 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070047 * \30..\33 - a word immediate operand, from operand 0..3
48 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000049 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070050 * \40..\43 - a long immediate operand, from operand 0..3
51 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040052 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070053 * \50..\53 - a byte relative operand, from operand 0..3
54 * \54..\57 - a qword immediate operand, from operand 0..3
55 * \60..\63 - a word relative operand, from operand 0..3
56 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000057 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070058 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070059 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060 * \1ab - a ModRM, calculated on EA in operand a, with the spare
61 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040062 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070063 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040064 * \173\xab - the register number from operand a in bits 7..4, with
65 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070066 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040067 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068 * \2ab - a ModRM, calculated on EA in operand a, with the spare
69 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070070 *
71 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
72 * V field taken from operand 0..3.
73 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
74 * V field set to 1111b.
75 * EVEX prefixes are followed by the sequence:
76 * \cm\wlp\tup where cm is:
77 * cc 000 0mm
78 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
79 * and wlp is:
80 * 00 wwl lpp
81 * [l0] ll = 0 (.128, .lz)
82 * [l1] ll = 1 (.256)
83 * [l2] ll = 2 (.512)
84 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
85 *
86 * [w0] ww = 0 for W = 0
87 * [w1] ww = 1 for W = 1
88 * [wig] ww = 2 for W don't care (always assembled as 0)
89 * [ww] ww = 3 for W used as REX.W
90 *
91 * [p0] pp = 0 for no prefix
92 * [60] pp = 1 for legacy prefix 60
93 * [f3] pp = 2
94 * [f2] pp = 3
95 *
96 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
97 * (compressed displacement encoding)
98 *
H. Peter Anvin588df782008-10-07 10:05:10 -070099 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700100 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400101 * V field taken from operand 0..3.
102 * \270 - this instruction uses VEX/XOP rather than REX, with the
103 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700104 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700105 * VEX/XOP prefixes are followed by the sequence:
106 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700107 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700108 * [l0] ll = 0 for L = 0 (.128, .lz)
109 * [l1] ll = 1 for L = 1 (.256)
110 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700111 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700112 * [w0] ww = 0 for W = 0
113 * [w1 ] ww = 1 for W = 1
114 * [wig] ww = 2 for W don't care (always assembled as 0)
115 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700116 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700117 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700118 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800119 * \271 - instruction takes XRELEASE (F3) with or without lock
120 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
121 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400122 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
123 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
125 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700126 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000127 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800128 * \314 - (disassembler only) invalid with REX.B
129 * \315 - (disassembler only) invalid with REX.X
130 * \316 - (disassembler only) invalid with REX.R
131 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000132 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
133 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
134 * \322 - indicates that this instruction is only valid when the
135 * operand size is the default (instruction to disassembler,
136 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000137 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000138 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400139 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400140 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
141 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142 * \330 - a literal byte follows in the code stream, to be added
143 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000144 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000145 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700146 * \332 - REP prefix (0xF2 byte) used as opcode extension.
147 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700148 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700149 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800150 * \336 - force a REP(E) prefix (0xF3) even if not specified.
151 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700152 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000153 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400155 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700157 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000158 * \364 - operand-size prefix (0x66) not permitted
159 * \365 - address-size prefix (0x67) not permitted
160 * \366 - operand-size prefix (0x66) used as opcode extension
161 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800162 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400163 * 370 is used for Jcc, 371 is used for JMP.
164 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
165 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700166 * \374 - this instruction takes an XMM VSIB memory EA
167 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700168 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000169 */
170
H. Peter Anvinfe501952007-10-02 21:53:51 -0700171#include "compiler.h"
172
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000173#include <stdio.h>
174#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000175#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176
177#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000178#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000179#include "assemble.h"
180#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700181#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000182
H. Peter Anvin65289e82009-07-25 17:25:11 -0700183enum match_result {
184 /*
185 * Matching errors. These should be sorted so that more specific
186 * errors come later in the sequence.
187 */
188 MERR_INVALOP,
189 MERR_OPSIZEMISSING,
190 MERR_OPSIZEMISMATCH,
191 MERR_BADCPU,
192 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800193 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700194 MERR_ENCMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700195 /*
196 * Matching success; the conditional ones first
197 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400198 MOK_JUMP, /* Matching OK but needs jmp_match() */
199 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700200};
201
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000202typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700203 enum ea_type type; /* what kind of EA is this? */
204 int sib_present; /* is a SIB byte necessary? */
205 int bytes; /* # of bytes of offset needed */
206 int size; /* lazy - this is sib+bytes+1 */
207 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700208 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000209} ea;
210
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400211#define GEN_SIB(scale, index, base) \
212 (((scale) << 6) | ((index) << 3) | ((base)))
213
214#define GEN_MODRM(mod, reg, rm) \
215 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
216
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700217static iflags_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218static efunc errfunc;
219static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000220static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800222static int64_t calcsize(int32_t, int64_t, int, insn *,
223 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700224static void gencode(int32_t segment, int64_t offset, int bits,
225 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400226 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700227static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400228 insn *instruction,
229 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700230static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700231static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000232static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700233static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000234static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700235static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700236static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700238static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700239
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400240static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000241{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700242 return ins->prefixes[pos] == prefix;
243}
244
245static void assert_no_prefix(insn * ins, enum prefix_pos pos)
246{
247 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400248 errfunc(ERR_NONFATAL, "invalid %s prefix",
249 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700250}
251
252static const char *size_name(int size)
253{
254 switch (size) {
255 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400256 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700257 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400258 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700267 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700269 case 64:
270 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000273 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700274}
275
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400276static void warn_overflow(int pass, int size)
277{
278 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
279 "%s data exceeds bounds", size_name(size));
280}
281
282static void warn_overflow_const(int64_t data, int size)
283{
284 if (overflow_general(data, size))
285 warn_overflow(ERR_PASS1, size);
286}
287
288static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100290 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400291 if (overflow_general(o->offset, size))
292 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700293 }
294}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000296/*
297 * This routine wrappers the real output format's output routine,
298 * in order to pass a copy of the data off to the listing file
299 * generator at the same time.
300 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800301static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800302 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400303 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000304{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000305 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000306 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800307 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000308
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800309 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400310 /*
311 * This is a non-relocated address, and we're going to
312 * convert it into RAWDATA format.
313 */
314 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800315
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400316 if (size > 8) {
317 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
318 return;
319 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700320
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400321 WRITEADDR(q, *(int64_t *)data, size);
322 data = p;
323 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000324 }
325
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800326 list->output(offset, data, type, size);
327
Frank Kotlerabebb082003-09-06 04:45:37 +0000328 /*
329 * this call to src_get determines when we call the
330 * debug-format-specific "linenum" function
331 * it updates lineno and lnfname to the current values
332 * returning 0 if "same as last time", -2 if lnfname
333 * changed, and the amount by which lineno changed,
334 * if it did. thus, these variables must be static
335 */
336
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400337 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000339
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800340 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341}
342
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400343static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
344{
345 if (opx->segment != NO_SEG) {
346 uint64_t data = opx->offset;
347 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
348 } else {
349 uint8_t byte = opx->offset;
350 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
351 }
352}
353
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700354static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800355 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800357 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800358 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000359 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000360
H. Peter Anvin755f5212012-02-25 11:41:34 -0800361 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700362 return false;
363 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400364 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700365 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400366 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700367
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800368 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100369
Victor van den Elzen154e5922009-02-25 17:32:00 +0100370 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100371 /* Be optimistic in pass 1 */
372 return true;
373
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700375 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000376
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700377 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
378 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000380
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700381int64_t assemble(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400382 insn * instruction, struct ofmt *output, efunc error,
383 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000384{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000385 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700387 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800388 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000389 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800390 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300391 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000392
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000394 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 outfmt = output; /* likewise */
396 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300398 wsize = idata_bytes(instruction->opcode);
399 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000404 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 if (t < 0)
406 errfunc(ERR_PANIC,
407 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000408
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400410 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400412 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700413 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400414 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000415 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700416 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800418 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400419 offset += wsize;
420 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700421 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400422 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000424
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800426 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000428
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 if (align) {
430 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100431 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800432 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 }
434 offset += e->stringlen + align;
435 }
436 }
437 if (t > 0 && t == instruction->times - 1) {
438 /*
439 * Dummy call to list->output to give the offset to the
440 * listing module.
441 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800442 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 list->uplevel(LIST_TIMES);
444 }
445 }
446 if (instruction->times > 1)
447 list->downlevel(LIST_TIMES);
448 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000449 }
450
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700452 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000454
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400455 fp = fopen(fname, "rb");
456 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
458 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400459 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
461 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200462 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400463 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700464 static char buf[4096];
465 size_t t = instruction->times;
466 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400467 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000468
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 len = ftell(fp);
470 if (instruction->eops->next) {
471 base = instruction->eops->next->offset;
472 len -= base;
473 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700474 len > (size_t)instruction->eops->next->next->offset)
475 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 }
477 /*
478 * Dummy call to list->output to give the offset to the
479 * listing module.
480 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800481 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 list->uplevel(LIST_INCBIN);
483 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700484 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000485
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 fseek(fp, base, SEEK_SET);
487 l = len;
488 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700489 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400490 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 if (!m) {
492 /*
493 * This shouldn't happen unless the file
494 * actually changes while we are reading
495 * it.
496 */
497 error(ERR_NONFATAL,
498 "`incbin': unexpected EOF while"
499 " reading file `%s'", fname);
500 t = 0; /* Try to exit cleanly */
501 break;
502 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800503 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 NO_SEG, NO_SEG);
505 l -= m;
506 }
507 }
508 list->downlevel(LIST_INCBIN);
509 if (instruction->times > 1) {
510 /*
511 * Dummy call to list->output to give the offset to the
512 * listing module.
513 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800514 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 list->uplevel(LIST_TIMES);
516 list->downlevel(LIST_TIMES);
517 }
518 fclose(fp);
519 return instruction->times * len;
520 }
521 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000522 }
523
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700524 /* Check to see if we need an address-size prefix */
525 add_asp(instruction, bits);
526
H. Peter Anvin23595f52009-07-25 17:44:25 -0700527 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700528
H. Peter Anvin23595f52009-07-25 17:44:25 -0700529 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400530 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800531 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 itimes = instruction->times;
533 if (insn_size < 0) /* shouldn't be, on pass two */
534 error(ERR_PANIC, "errors made it through from pass one");
535 else
536 while (itimes--) {
537 for (j = 0; j < MAXPREFIX; j++) {
538 uint8_t c = 0;
539 switch (instruction->prefixes[j]) {
540 case P_WAIT:
541 c = 0x9B;
542 break;
543 case P_LOCK:
544 c = 0xF0;
545 break;
546 case P_REPNE:
547 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800548 case P_XACQUIRE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400549 c = 0xF2;
550 break;
551 case P_REPE:
552 case P_REPZ:
553 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800554 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400555 c = 0xF3;
556 break;
557 case R_CS:
558 if (bits == 64) {
559 error(ERR_WARNING | ERR_PASS2,
560 "cs segment base generated, but will be ignored in 64-bit mode");
561 }
562 c = 0x2E;
563 break;
564 case R_DS:
565 if (bits == 64) {
566 error(ERR_WARNING | ERR_PASS2,
567 "ds segment base generated, but will be ignored in 64-bit mode");
568 }
569 c = 0x3E;
570 break;
571 case R_ES:
572 if (bits == 64) {
573 error(ERR_WARNING | ERR_PASS2,
574 "es segment base generated, but will be ignored in 64-bit mode");
575 }
576 c = 0x26;
577 break;
578 case R_FS:
579 c = 0x64;
580 break;
581 case R_GS:
582 c = 0x65;
583 break;
584 case R_SS:
585 if (bits == 64) {
586 error(ERR_WARNING | ERR_PASS2,
587 "ss segment base generated, but will be ignored in 64-bit mode");
588 }
589 c = 0x36;
590 break;
591 case R_SEGR6:
592 case R_SEGR7:
593 error(ERR_NONFATAL,
594 "segr6 and segr7 cannot be used as prefixes");
595 break;
596 case P_A16:
597 if (bits == 64) {
598 error(ERR_NONFATAL,
599 "16-bit addressing is not supported "
600 "in 64-bit mode");
601 } else if (bits != 16)
602 c = 0x67;
603 break;
604 case P_A32:
605 if (bits != 32)
606 c = 0x67;
607 break;
608 case P_A64:
609 if (bits != 64) {
610 error(ERR_NONFATAL,
611 "64-bit addressing is only supported "
612 "in 64-bit mode");
613 }
614 break;
615 case P_ASP:
616 c = 0x67;
617 break;
618 case P_O16:
619 if (bits != 16)
620 c = 0x66;
621 break;
622 case P_O32:
623 if (bits == 16)
624 c = 0x66;
625 break;
626 case P_O64:
627 /* REX.W */
628 break;
629 case P_OSP:
630 c = 0x66;
631 break;
632 case P_none:
633 break;
634 default:
635 error(ERR_PANIC, "invalid instruction prefix");
636 }
637 if (c != 0) {
638 out(offset, segment, &c, OUT_RAWDATA, 1,
639 NO_SEG, NO_SEG);
640 offset++;
641 }
642 }
643 insn_end = offset + insn_size;
644 gencode(segment, offset, bits, instruction,
645 temp, insn_end);
646 offset += insn_size;
647 if (itimes > 0 && itimes == instruction->times - 1) {
648 /*
649 * Dummy call to list->output to give the offset to the
650 * listing module.
651 */
652 list->output(offset, NULL, OUT_RAWDATA, 0);
653 list->uplevel(LIST_TIMES);
654 }
655 }
656 if (instruction->times > 1)
657 list->downlevel(LIST_TIMES);
658 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700659 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400660 /* No match */
661 switch (m) {
662 case MERR_OPSIZEMISSING:
663 error(ERR_NONFATAL, "operation size not specified");
664 break;
665 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400667 break;
668 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400670 break;
671 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800672 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400673 bits);
674 break;
675 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 error(ERR_NONFATAL,
677 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400678 break;
679 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000680 }
681 return 0;
682}
683
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700684int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400685 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000687 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700688 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000689
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000691 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400693 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000695
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700696 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
697 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400698 instruction->opcode == I_DT || instruction->opcode == I_DO ||
699 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300701 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000702
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300704 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000705
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400706 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000707 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400710 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400712 warn_overflow_const(e->offset, wsize);
713 } else if (e->type == EOT_DB_STRING ||
714 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000716
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 align = (-osize) % wsize;
718 if (align < 0)
719 align += wsize;
720 isize += osize + align;
721 }
722 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723 }
724
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400726 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300728 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700729 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000730
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400731 fp = fopen(fname, "rb");
732 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
734 fname);
735 else if (fseek(fp, 0L, SEEK_END) < 0)
736 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
737 fname);
738 else {
739 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 if (instruction->eops->next) {
741 len -= instruction->eops->next->offset;
742 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700743 len > (size_t)instruction->eops->next->next->offset) {
744 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 }
746 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300747 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300749 if (fp)
750 fclose(fp);
751 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000752 }
753
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700754 /* Check to see if we need an address-size prefix */
755 add_asp(instruction, bits);
756
H. Peter Anvin23595f52009-07-25 17:44:25 -0700757 m = find_match(&temp, instruction, segment, offset, bits);
758 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400759 /* we've matched an instruction. */
760 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100762
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800763 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400764 if (isize < 0)
765 return -1;
766 for (j = 0; j < MAXPREFIX; j++) {
767 switch (instruction->prefixes[j]) {
768 case P_A16:
769 if (bits != 16)
770 isize++;
771 break;
772 case P_A32:
773 if (bits != 32)
774 isize++;
775 break;
776 case P_O16:
777 if (bits != 16)
778 isize++;
779 break;
780 case P_O32:
781 if (bits == 16)
782 isize++;
783 break;
784 case P_A64:
785 case P_O64:
786 case P_none:
787 break;
788 default:
789 isize++;
790 break;
791 }
792 }
793 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700794 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400795 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000797}
798
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800799static void bad_hle_warn(const insn * ins, uint8_t hleok)
800{
801 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800802 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800803 static const enum whatwarn warn[2][4] =
804 {
805 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
806 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
807 };
808 unsigned int n;
809
810 n = (unsigned int)rep_pfx - P_XACQUIRE;
811 if (n > 1)
812 return; /* Not XACQUIRE/XRELEASE */
813
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800814 ww = warn[n][hleok];
815 if (!is_class(MEMORY, ins->oprs[0].type))
816 ww = w_inval; /* HLE requires operand 0 to be memory */
817
818 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800819 case w_none:
820 break;
821
822 case w_lock:
823 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800824 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800825 "%s with this instruction requires lock",
826 prefix_name(rep_pfx));
827 }
828 break;
829
830 case w_inval:
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 invalid with this instruction",
833 prefix_name(rep_pfx));
834 break;
835 }
836}
837
H. Peter Anvin507ae032008-10-09 15:37:10 -0700838/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400839#define case3(x) case (x): case (x)+1: case (x)+2
840#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700841
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800842static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800843 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000844{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800845 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800846 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000847 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000848 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700849 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700850 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700851 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700852 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800853 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800854 bool lockcheck = true;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000855
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700856 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700857 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700858 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700859
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700860 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400861 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700862
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 (void)segment; /* Don't warn that this parameter is unused */
864 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000865
H. Peter Anvin839eca22007-10-29 23:12:47 -0700866 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400867 c = *codes++;
868 op1 = (c & 3) + ((opex & 1) << 2);
869 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
870 opx = &ins->oprs[op1];
871 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700872
H. Peter Anvin839eca22007-10-29 23:12:47 -0700873 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400874 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 codes += c, length += c;
876 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700877
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400878 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 opex = c;
880 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700881
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400882 case4(010):
883 ins->rex |=
884 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 codes++, length++;
886 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700887
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400888 case4(020):
889 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 length++;
891 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700892
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400893 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 length += 2;
895 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700896
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400897 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700898 if (opx->type & (BITS16 | BITS32 | BITS64))
899 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 else
901 length += (bits == 16) ? 2 : 4;
902 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700903
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 length += 4;
906 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700907
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400908 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700909 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400912 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 length++;
914 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700915
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400916 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000917 length += 8; /* MOV reg64/imm */
918 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400920 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 length += 2;
922 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700923
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400924 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700925 if (opx->type & (BITS16 | BITS32 | BITS64))
926 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 else
928 length += (bits == 16) ? 2 : 4;
929 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400931 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 length += 4;
933 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700936 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 case 0172:
940 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700942 length++;
943 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700944
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700945 case4(0174):
946 length++;
947 break;
948
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700949 case4(0240):
950 ins->rex |= REX_EV;
951 ins->vexreg = regval(opx);
952 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
953 ins->vex_cm = *codes++;
954 ins->vex_wlp = *codes++;
955 ins->evex_tuple = (*codes++ - 0300);
956 break;
957
958 case 0250:
959 ins->rex |= REX_EV;
960 ins->vexreg = 0;
961 ins->vex_cm = *codes++;
962 ins->vex_wlp = *codes++;
963 ins->evex_tuple = (*codes++ - 0300);
964 break;
965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(0254):
967 length += 4;
968 break;
969
970 case4(0260):
971 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700972 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 ins->vex_cm = *codes++;
974 ins->vex_wlp = *codes++;
975 break;
976
977 case 0270:
978 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700979 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400980 ins->vex_cm = *codes++;
981 ins->vex_wlp = *codes++;
982 break;
983
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400984 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800985 hleok = c & 3;
986 break;
987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(0274):
989 length++;
990 break;
991
992 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 if (bits == 64)
997 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700998 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001000
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001002 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Keith Kaniosb7a89542007-04-12 02:40:54 +00001008 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1010 has_prefix(ins, PPS_ASIZE, P_A32))
1011 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(0314):
1015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001018 {
1019 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1020 if (pfx == P_O16)
1021 break;
1022 if (pfx != P_none)
1023 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1024 else
1025 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001027 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001030 {
1031 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1032 if (pfx == P_O32)
1033 break;
1034 if (pfx != P_none)
1035 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1036 else
1037 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001039 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 case 0322:
1042 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001043
Keith Kaniosb7a89542007-04-12 02:40:54 +00001044 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001045 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001046 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001047
Keith Kaniosb7a89542007-04-12 02:40:54 +00001048 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001050 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001051
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001052 case 0325:
1053 ins->rex |= REX_NH;
1054 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001055
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001056 case 0326:
1057 break;
1058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 0330:
1060 codes++, length++;
1061 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001062
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001066 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 case 0333:
1068 length++;
1069 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001071 case 0334:
1072 ins->rex |= REX_L;
1073 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001074
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001075 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001079 if (!ins->prefixes[PPS_REP])
1080 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001081 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001083 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001084 if (!ins->prefixes[PPS_REP])
1085 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001086 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001087
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 if (ins->oprs[0].segment != NO_SEG)
1090 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1091 " quantity of BSS space");
1092 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001093 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001096 case 0341:
1097 if (!ins->prefixes[PPS_WAIT])
1098 ins->prefixes[PPS_WAIT] = P_WAIT;
1099 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001100
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001101 case 0360:
1102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001104 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 length++;
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001108 case 0364:
1109 case 0365:
1110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
Keith Kanios48af1772007-08-17 07:37:52 +00001112 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001113 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 length++;
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001117 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 case 0373:
1121 length++;
1122 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001123
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001124 case 0374:
1125 eat = EA_XMMVSIB;
1126 break;
1127
1128 case 0375:
1129 eat = EA_YMMVSIB;
1130 break;
1131
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001132 case 0376:
1133 eat = EA_ZMMVSIB;
1134 break;
1135
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001136 case4(0100):
1137 case4(0110):
1138 case4(0120):
1139 case4(0130):
1140 case4(0200):
1141 case4(0204):
1142 case4(0210):
1143 case4(0214):
1144 case4(0220):
1145 case4(0224):
1146 case4(0230):
1147 case4(0234):
1148 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001150 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 opflags_t rflags;
1152 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001153 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001154
Keith Kaniosb7a89542007-04-12 02:40:54 +00001155 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001156
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001157 if (c <= 0177) {
1158 /* pick rfield from operand b (opx) */
1159 rflags = regflag(opx);
1160 rfield = nasm_regvals[opx->basereg];
1161 } else {
1162 rflags = 0;
1163 rfield = c & 7;
1164 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001165
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001166 /* EVEX.b1 : evex_brerop contains the operand position */
1167 op_er_sae = (ins->evex_brerop >= 0 ?
1168 &ins->oprs[ins->evex_brerop] : NULL);
1169
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001170 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1171 /* set EVEX.b */
1172 ins->evex_p[2] |= EVEX_P2B;
1173 if (op_er_sae->decoflags & ER) {
1174 /* set EVEX.RC (rounding control) */
1175 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1176 & EVEX_P2RC;
1177 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001178 } else {
1179 /* set EVEX.L'L (vector length) */
1180 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001181 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001182 /* set EVEX.b */
1183 ins->evex_p[2] |= EVEX_P2B;
1184 }
1185 }
1186
1187 if (process_ea(opy, &ea_data, bits,
1188 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 errfunc(ERR_NONFATAL, "invalid effective address");
1190 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001191 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001192 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001194 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 }
1196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001198 default:
1199 errfunc(ERR_PANIC, "internal instruction table corrupt"
1200 ": instruction code \\%o (0x%02X) given", c, c);
1201 break;
1202 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001203 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001204
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001205 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001206
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001207 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001208 if (ins->rex & REX_H) {
1209 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1210 return -1;
1211 }
1212 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001213 }
1214
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001215 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001216 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001217
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001219 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001220 return -1;
1221 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001222 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001223 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001224 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 ins->rex &= ~REX_W;
1226 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001227 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001228 ins->rex |= REX_W;
1229 bad32 &= ~REX_W;
1230 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001231 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 /* Follow REX_W */
1233 break;
1234 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001235
H. Peter Anvinfc561202011-07-07 16:58:22 -07001236 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001237 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1238 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001239 } else if (!(ins->rex & REX_EV) &&
1240 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1241 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1242 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001243 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001244 if (ins->rex & REX_EV)
1245 length += 4;
1246 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001247 length += 3;
1248 else
1249 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001250 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 if (ins->rex & REX_H) {
1252 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1253 return -1;
1254 } else if (bits == 64) {
1255 length++;
1256 } else if ((ins->rex & REX_L) &&
1257 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1258 cpu >= IF_X86_64) {
1259 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001260 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001261 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 length++;
1263 } else {
1264 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1265 return -1;
1266 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001267 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001268
1269 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1270 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001271 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001272 "instruction is not lockable");
1273 }
1274
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001275 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001276
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001277 return length;
1278}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001279
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001280static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1281{
1282 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001283 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001284 ins->rex = (ins->rex & REX_REAL) | REX_P;
1285 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1286 ins->rex = 0;
1287 return 1;
1288 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001289 }
1290
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001291 return 0;
1292}
1293
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001294static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001295 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001297{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001298 uint8_t c;
1299 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001300 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001301 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001302 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001303 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001304 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001305 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001306 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001307
H. Peter Anvin839eca22007-10-29 23:12:47 -07001308 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 c = *codes++;
1310 op1 = (c & 3) + ((opex & 1) << 2);
1311 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1312 opx = &ins->oprs[op1];
1313 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001314
H. Peter Anvin839eca22007-10-29 23:12:47 -07001315 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 case 01:
1317 case 02:
1318 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001320 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001321 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 codes += c;
1323 offset += c;
1324 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001325
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 case 05:
1327 case 06:
1328 case 07:
1329 opex = c;
1330 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001331
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001332 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001333 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001334 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001335 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 offset += 1;
1337 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001340 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001341 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001344 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 offset += 1;
1346 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001349 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001350 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001352 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 offset += 1;
1354 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001355
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001357 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001358 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001359 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001360 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 offset += 2;
1362 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001363
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001365 if (opx->type & (BITS16 | BITS32))
1366 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 else
1368 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001369 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001370 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001371 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001372 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 offset += size;
1374 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001377 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001378 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001379 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001380 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 offset += 4;
1382 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001385 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001386 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001387 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001388 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001389 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 offset += size;
1391 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001393 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001394 if (opx->segment != segment) {
1395 data = opx->offset;
1396 out(offset, segment, &data,
1397 OUT_REL1ADR, insn_end - offset,
1398 opx->segment, opx->wrt);
1399 } else {
1400 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001401 if (data > 127 || data < -128)
1402 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001403 out(offset, segment, &data,
1404 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1405 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 offset += 1;
1407 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001408
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001409 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001410 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001411 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001412 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001413 offset += 8;
1414 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001415
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001416 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001417 if (opx->segment != segment) {
1418 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001420 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001421 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001423 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001425 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 }
1427 offset += 2;
1428 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001429
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001430 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001431 if (opx->type & (BITS16 | BITS32 | BITS64))
1432 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 else
1434 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001435 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001436 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001437 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001438 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1439 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001441 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001443 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 }
1445 offset += size;
1446 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001447
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001449 if (opx->segment != segment) {
1450 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001452 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001453 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001455 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001457 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 }
1459 offset += 4;
1460 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001461
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001463 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001464 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1465 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001466 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001467 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001468 outfmt->segbase(1 + opx->segment),
1469 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001470 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001473 case 0172:
1474 c = *codes++;
1475 opx = &ins->oprs[c >> 3];
1476 bytes[0] = nasm_regvals[opx->basereg] << 4;
1477 opx = &ins->oprs[c & 7];
1478 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1479 errfunc(ERR_NONFATAL,
1480 "non-absolute expression not permitted as argument %d",
1481 c & 7);
1482 } else {
1483 if (opx->offset & ~15) {
1484 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1485 "four-bit argument exceeds bounds");
1486 }
1487 bytes[0] |= opx->offset & 15;
1488 }
1489 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1490 offset++;
1491 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001492
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001493 case 0173:
1494 c = *codes++;
1495 opx = &ins->oprs[c >> 4];
1496 bytes[0] = nasm_regvals[opx->basereg] << 4;
1497 bytes[0] |= c & 15;
1498 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1499 offset++;
1500 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001501
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001502 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001503 bytes[0] = nasm_regvals[opx->basereg] << 4;
1504 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1505 offset++;
1506 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001507
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001508 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001509 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1511 (int32_t)data != (int64_t)data) {
1512 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1513 "signed dword immediate exceeds bounds");
1514 }
1515 out(offset, segment, &data, OUT_ADDRESS, 4,
1516 opx->segment, opx->wrt);
1517 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001518 break;
1519
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001520 case4(0240):
1521 case 0250:
1522 codes += 3;
1523 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1524 EVEX_P2Z | EVEX_P2AAA, 2);
1525 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1526 bytes[0] = 0x62;
1527 /* EVEX.X can be set by either REX or EVEX for different reasons */
1528 bytes[1] = (~(((ins->rex & 7) << 5) |
1529 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) & 0xf0) |
1530 (ins->vex_cm & 3);
1531 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1532 ((~ins->vexreg & 15) << 3) |
1533 (1 << 2) | (ins->vex_wlp & 3);
1534 bytes[3] = ins->evex_p[2];
1535 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1536 offset += 4;
1537 break;
1538
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 case4(0260):
1540 case 0270:
1541 codes += 2;
1542 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1543 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1544 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1545 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001546 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001547 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1548 offset += 3;
1549 } else {
1550 bytes[0] = 0xc5;
1551 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001552 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1554 offset += 2;
1555 }
1556 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001557
H. Peter Anvine014f352012-02-25 22:35:19 -08001558 case 0271:
1559 case 0272:
1560 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001561 break;
1562
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 case4(0274):
1564 {
1565 uint64_t uv, um;
1566 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 if (ins->rex & REX_W)
1569 s = 64;
1570 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1571 s = 16;
1572 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1573 s = 32;
1574 else
1575 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001576
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001577 um = (uint64_t)2 << (s-1);
1578 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001579
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 if (uv > 127 && uv < (uint64_t)-128 &&
1581 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001582 /* If this wasn't explicitly byte-sized, warn as though we
1583 * had fallen through to the imm16/32/64 case.
1584 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001585 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001586 "%s value exceeds bounds",
1587 (opx->type & BITS8) ? "signed byte" :
1588 s == 16 ? "word" :
1589 s == 32 ? "dword" :
1590 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001591 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001592 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001593 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001594 out(offset, segment, &data, OUT_ADDRESS, 1,
1595 opx->segment, opx->wrt);
1596 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001597 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001598 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1599 NO_SEG);
1600 }
1601 offset += 1;
1602 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001609 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001611 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 offset += 1;
1613 } else
1614 offset += 0;
1615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001618 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001620 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 offset += 1;
1622 } else
1623 offset += 0;
1624 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 case 0312:
1627 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628
Keith Kaniosb7a89542007-04-12 02:40:54 +00001629 case 0313:
1630 ins->rex = 0;
1631 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case4(0314):
1634 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001635
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001639
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001641 case 0323:
1642 break;
1643
Keith Kaniosb7a89542007-04-12 02:40:54 +00001644 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001645 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001647
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 case 0325:
1649 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001650
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001651 case 0326:
1652 break;
1653
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001655 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001656 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 offset += 1;
1658 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001659
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001662
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001665 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001666 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 offset += 1;
1668 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669
Keith Kanios48af1772007-08-17 07:37:52 +00001670 case 0334:
1671 if (ins->rex & REX_R) {
1672 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001673 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001674 offset += 1;
1675 }
1676 ins->rex &= ~(REX_L|REX_R);
1677 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001678
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001679 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001680 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001681
H. Peter Anvin962e3052008-08-28 17:47:16 -07001682 case 0336:
1683 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 if (ins->oprs[0].segment != NO_SEG)
1688 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1689 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001690 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 if (size > 0)
1692 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001693 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 offset += size;
1695 }
1696 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001697
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 case 0341:
1699 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 case 0360:
1702 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001703
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 case 0361:
1705 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001706 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1707 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001708 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001709
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001710 case 0364:
1711 case 0365:
1712 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001713
Keith Kanios48af1772007-08-17 07:37:52 +00001714 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001715 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001716 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001717 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001718 offset += 1;
1719 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001720
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 case 0370:
1722 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001724
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 case 0373:
1726 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001727 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 offset += 1;
1729 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001730
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001731 case 0374:
1732 eat = EA_XMMVSIB;
1733 break;
1734
1735 case 0375:
1736 eat = EA_YMMVSIB;
1737 break;
1738
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001739 case 0376:
1740 eat = EA_ZMMVSIB;
1741 break;
1742
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001743 case4(0100):
1744 case4(0110):
1745 case4(0120):
1746 case4(0130):
1747 case4(0200):
1748 case4(0204):
1749 case4(0210):
1750 case4(0214):
1751 case4(0220):
1752 case4(0224):
1753 case4(0230):
1754 case4(0234):
1755 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 ea ea_data;
1757 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001759 uint8_t *p;
1760 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001762
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001763 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001764 /* pick rfield from operand b (opx) */
1765 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001766 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001767 } else {
1768 /* rfield is constant */
1769 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001771 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001773 if (process_ea(opy, &ea_data, bits,
1774 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 p = bytes;
1778 *p++ = ea_data.modrm;
1779 if (ea_data.sib_present)
1780 *p++ = ea_data.sib;
1781
1782 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001783 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001785 /*
1786 * Make sure the address gets the right offset in case
1787 * the line breaks in the .lst file (BR 1197827)
1788 */
1789 offset += s;
1790 s = 0;
1791
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 switch (ea_data.bytes) {
1793 case 0:
1794 break;
1795 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 case 2:
1797 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001798 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001799 /* use compressed displacement, if available */
1800 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 if (ea_data.rip) {
1803 if (opy->segment == segment) {
1804 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001805 if (overflow_signed(data, ea_data.bytes))
1806 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 out(offset, segment, &data, OUT_ADDRESS,
1808 ea_data.bytes, NO_SEG, NO_SEG);
1809 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001810 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 out(offset, segment, &data, OUT_REL4ADR,
1812 insn_end - offset, opy->segment, opy->wrt);
1813 }
1814 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001815 if (overflow_general(data, ins->addr_size >> 3) ||
1816 signed_bits(data, ins->addr_size) !=
1817 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001818 warn_overflow(ERR_PASS2, ea_data.bytes);
1819
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 out(offset, segment, &data, OUT_ADDRESS,
1821 ea_data.bytes, opy->segment, opy->wrt);
1822 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001824 default:
1825 /* Impossible! */
1826 errfunc(ERR_PANIC,
1827 "Invalid amount of bytes (%d) for offset?!",
1828 ea_data.bytes);
1829 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
1831 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001832 }
1833 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001834
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001835 default:
1836 errfunc(ERR_PANIC, "internal instruction table corrupt"
1837 ": instruction code \\%o (0x%02X) given", c, c);
1838 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001840 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001841}
1842
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001843static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001844{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001845 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001846 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001847 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001848}
1849
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001850static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001851{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001852 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001854 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001855}
1856
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001857static int op_rexflags(const operand * o, int mask)
1858{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001859 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001860 int val;
1861
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001862 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001863 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001864
H. Peter Anvina4835d42008-05-20 14:21:29 -07001865 flags = nasm_reg_flags[o->basereg];
1866 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001867
1868 return rexflags(val, flags, mask);
1869}
1870
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001871static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001872{
1873 int rex = 0;
1874
1875 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001877 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 rex |= REX_W;
1879 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1880 rex |= REX_H;
1881 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1882 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001883
1884 return rex & mask;
1885}
1886
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001887static int evexflags(int val, decoflags_t deco,
1888 int mask, uint8_t byte)
1889{
1890 int evex = 0;
1891
1892 switch(byte) {
1893 case 0:
1894 if (val >= 16)
1895 evex |= (EVEX_P0RP | EVEX_P0X);
1896 break;
1897 case 2:
1898 if (val >= 16)
1899 evex |= EVEX_P2VP;
1900 if (deco & Z)
1901 evex |= EVEX_P2Z;
1902 if (deco & OPMASK_MASK)
1903 evex |= deco & EVEX_P2AAA;
1904 break;
1905 }
1906 return evex & mask;
1907}
1908
1909static int op_evexflags(const operand * o, int mask, uint8_t byte)
1910{
1911 int val;
1912
1913 if (!is_register(o->basereg))
1914 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1915
1916 val = nasm_regvals[o->basereg];
1917
1918 return evexflags(val, o->decoflags, mask, byte);
1919}
1920
H. Peter Anvin23595f52009-07-25 17:44:25 -07001921static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 insn *instruction,
1923 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001924{
1925 const struct itemplate *temp;
1926 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001927 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001928 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001929 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001930 int i;
1931
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001932 /* broadcasting uses a different data element size */
1933 for (i = 0; i < instruction->operands; i++)
1934 if (i == broadcast)
1935 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1936 else
1937 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001938
1939 merr = MERR_INVALOP;
1940
1941 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 temp->opcode != I_none; temp++) {
1943 m = matches(temp, instruction, bits);
1944 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001945 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001946 m = MOK_GOOD;
1947 else
1948 m = MERR_INVALOP;
1949 } else if (m == MERR_OPSIZEMISSING &&
1950 (temp->flags & IF_SMASK) != IF_SX) {
1951 /*
1952 * Missing operand size and a candidate for fuzzy matching...
1953 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001954 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001955 if (i == broadcast)
1956 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
1957 else
1958 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 opsizemissing = true;
1960 }
1961 if (m > merr)
1962 merr = m;
1963 if (merr == MOK_GOOD)
1964 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001965 }
1966
1967 /* No match, but see if we can get a fuzzy operand size match... */
1968 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001970
1971 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 /*
1973 * We ignore extrinsic operand sizes on registers, so we should
1974 * never try to fuzzy-match on them. This also resolves the case
1975 * when we have e.g. "xmmrm128" in two different positions.
1976 */
1977 if (is_class(REGISTER, instruction->oprs[i].type))
1978 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07001979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001980 /* This tests if xsizeflags[i] has more than one bit set */
1981 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1982 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001983
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001984 if (i == broadcast)
1985 instruction->oprs[i].decoflags |= xsizeflags[i];
1986 else
1987 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001988 }
1989
1990 /* Try matching again... */
1991 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001992 temp->opcode != I_none; temp++) {
1993 m = matches(temp, instruction, bits);
1994 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001995 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 m = MOK_GOOD;
1997 else
1998 m = MERR_INVALOP;
1999 }
2000 if (m > merr)
2001 merr = m;
2002 if (merr == MOK_GOOD)
2003 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002004 }
2005
H. Peter Anvina81655b2009-07-25 18:15:28 -07002006done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002007 *tempp = temp;
2008 return merr;
2009}
2010
H. Peter Anvin65289e82009-07-25 17:25:11 -07002011static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002012 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002013{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002014 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002015 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002016 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002017
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002018 /*
2019 * Check the opcode
2020 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002022 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002023
2024 /*
2025 * Count the operands
2026 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002028 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002029
2030 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002031 * Is it legal?
2032 */
2033 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2034 return MERR_INVALOP;
2035
2036 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002037 * Check that no spurious colons or TOs are present
2038 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 for (i = 0; i < itemp->operands; i++)
2040 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002041 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002042
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002043 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002044 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002045 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002046 switch (itemp->flags & IF_SMASK) {
2047 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002048 asize = BITS8;
2049 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002050 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002051 asize = BITS16;
2052 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002053 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002054 asize = BITS32;
2055 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002056 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002057 asize = BITS64;
2058 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002059 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002060 asize = BITS128;
2061 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002062 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002063 asize = BITS256;
2064 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002065 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002066 asize = BITS512;
2067 break;
2068 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002069 switch (bits) {
2070 case 16:
2071 asize = BITS16;
2072 break;
2073 case 32:
2074 asize = BITS32;
2075 break;
2076 case 64:
2077 asize = BITS64;
2078 break;
2079 default:
2080 asize = 0;
2081 break;
2082 }
2083 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002084 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002085 asize = 0;
2086 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002087 }
2088
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002089 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002090 /* S- flags only apply to a specific operand */
2091 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2092 memset(size, 0, sizeof size);
2093 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002094 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002095 /* S- flags apply to all operands */
2096 for (i = 0; i < MAX_OPERANDS; i++)
2097 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002098 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002099
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002100 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002101 * Check that the operand flags all match up,
2102 * it's a bit tricky so lets be verbose:
2103 *
2104 * 1) Find out the size of operand. If instruction
2105 * doesn't have one specified -- we're trying to
2106 * guess it either from template (IF_S* flag) or
2107 * from code bits.
2108 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002109 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002110 * template has an operand size specified AND this size differ
2111 * from which instruction has (perhaps we got it from code bits)
2112 * we are:
2113 * a) Check that only size of instruction and operand is differ
2114 * other characteristics do match
2115 * b) Perhaps it's a register specified in instruction so
2116 * for such a case we just mark that operand as "size
2117 * missing" and this will turn on fuzzy operand size
2118 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002119 */
2120 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002121 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002122 decoflags_t deco = instruction->oprs[i].decoflags;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002123 if (!(type & SIZE_MASK))
2124 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002125
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002126 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2127 (itemp->deco[i] & deco) != deco) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002128 return MERR_INVALOP;
2129 } else if ((itemp->opd[i] & SIZE_MASK) &&
2130 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2131 if (type & SIZE_MASK) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002132 /*
2133 * when broadcasting, the element size depends on
2134 * the instruction type. decorator flag should match.
2135 */
2136#define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2137 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2138 if (!((deco & BRDCAST_MASK) &&
2139 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2140 return MERR_INVALOP;
2141 }
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002142 } else if (!is_class(REGISTER, type)) {
2143 /*
2144 * Note: we don't honor extrinsic operand sizes for registers,
2145 * so "missing operand size" for a register should be
2146 * considered a wildcard match rather than an error.
2147 */
2148 opsizemissing = true;
2149 }
Jin Kyu Song7abc78d2013-08-28 19:15:25 -07002150 } else if (nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
Jin Kyu Songdd1c0c12013-09-13 14:12:56 -07002151 !(itemp->flags & IF_AVX512)) {
Jin Kyu Song66c61922013-08-26 20:28:43 -07002152 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002153 }
2154 }
2155
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002156 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002157 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002158
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002159 /*
2160 * Check operand sizes
2161 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002162 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002165 asize = itemp->opd[i] & SIZE_MASK;
2166 if (asize) {
2167 for (i = 0; i < oprs; i++)
2168 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 break;
2170 }
2171 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002172 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002174 }
2175
Keith Kaniosb7a89542007-04-12 02:40:54 +00002176 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 if (!(itemp->opd[i] & SIZE_MASK) &&
2178 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002179 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002180 }
2181
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002182 /*
2183 * Check template is okay at the set cpu level
2184 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002185 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002186 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002187
Keith Kaniosb7a89542007-04-12 02:40:54 +00002188 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002189 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002190 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002191 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002192 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002194 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002195 * If we have a HLE prefix, look for the NOHLE flag
2196 */
2197 if ((itemp->flags & IF_NOHLE) &&
2198 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2199 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2200 return MERR_BADHLE;
2201
2202 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002203 * Check if special handling needed for Jumps
2204 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002205 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002206 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207
H. Peter Anvin60926242009-07-26 16:25:38 -07002208 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002209}
2210
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002211/*
2212 * Check if offset is a multiple of N with corresponding tuple type
2213 * if Disp8*N is available, compressed displacement is stored in compdisp
2214 */
2215static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2216{
2217 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2218 {{16, 32, 64}, {8, 8, 8}}};
2219 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2220 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2221
2222 bool evex_b = input->decoflags & BRDCAST_MASK;
2223 enum ttypes tuple = ins->evex_tuple;
2224 /* vex_wlp composed as [wwllpp] */
2225 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2226 /* wig(=2) is treated as w0(=0) */
2227 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2228 int32_t off = input->offset;
2229 uint8_t n = 0;
2230 int32_t disp8;
2231
2232 switch(tuple) {
2233 case FV:
2234 n = fv_n[evex_w][evex_b][vectlen];
2235 break;
2236 case HV:
2237 n = hv_n[evex_b][vectlen];
2238 break;
2239
2240 case FVM:
2241 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2242 n = 1 << (vectlen + 4);
2243 break;
2244 case T1S8: /* N = 1 */
2245 case T1S16: /* N = 2 */
2246 n = tuple - T1S8 + 1;
2247 break;
2248 case T1S:
2249 /* N = 4 for 32bit, 8 for 64bit */
2250 n = evex_w ? 8 : 4;
2251 break;
2252 case T1F32:
2253 case T1F64:
2254 /* N = 4 for 32bit, 8 for 64bit */
2255 n = (tuple == T1F32 ? 4 : 8);
2256 break;
2257 case T2:
2258 case T4:
2259 case T8:
2260 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2261 n = 0;
2262 else
Jin Kyu Songd2d9c3e2013-08-26 20:28:41 -07002263 n = 1 << (tuple - T2 + evex_w + 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002264 break;
2265 case HVM:
2266 case QVM:
2267 case OVM:
2268 n = 1 << (OVM - tuple + vectlen + 1);
2269 break;
2270 case M128:
2271 n = 16;
2272 break;
2273 case DUP:
2274 n = dup_n[vectlen];
2275 break;
2276
2277 default:
2278 break;
2279 }
2280
2281 if (n && !(off & (n - 1))) {
2282 disp8 = off / n;
2283 /* if it fits in Disp8 */
2284 if (disp8 >= -128 && disp8 <= 127) {
2285 *compdisp = disp8;
2286 return true;
2287 }
2288 }
2289
2290 *compdisp = 0;
2291 return false;
2292}
2293
2294/*
2295 * Check if ModR/M.mod should/can be 01.
2296 * - EAF_BYTEOFFS is set
2297 * - offset can fit in a byte when EVEX is not used
2298 * - offset can be compressed when EVEX is used
2299 */
2300#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2301 (o >= -128 && o <= 127 && \
2302 seg == NO_SEG && !forw_ref && \
2303 !(input->eaflags & EAF_WORDOFFS) && \
2304 !(ins->rex & REX_EV)) || \
2305 (ins->rex & REX_EV && \
2306 is_disp8n(input, ins, &output->disp8)))
2307
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002308static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002309 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002310{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002311 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002312 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002313
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002314 output->type = EA_SCALAR;
2315 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002316
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002317 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002318 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002319 /* EVEX.R' flag for the REG operand */
2320 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002321
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002322 if (is_class(REGISTER, input->type)) {
2323 /*
2324 * It's a direct register.
2325 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002326 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002327 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002328
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002329 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002330 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002331
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002332 /* broadcasting is not available with a direct register operand. */
2333 if (input->decoflags & BRDCAST_MASK) {
2334 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2335 goto err;
2336 }
2337
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002338 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002339 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002340 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002341 output->bytes = 0; /* no offset necessary either */
2342 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2343 } else {
2344 /*
2345 * It's a memory reference.
2346 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002347
2348 /* Embedded rounding or SAE is not available with a mem ref operand. */
2349 if (input->decoflags & (ER | SAE)) {
2350 nasm_error(ERR_NONFATAL,
2351 "Embedded rounding is available only with reg-reg op.");
2352 return -1;
2353 }
2354
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002355 if (input->basereg == -1 &&
2356 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002357 /*
2358 * It's a pure offset.
2359 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002360 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2361 input->segment == NO_SEG) {
2362 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2363 input->type &= ~IP_REL;
2364 input->type |= MEMORY;
2365 }
2366
2367 if (input->eaflags & EAF_BYTEOFFS ||
2368 (input->eaflags & EAF_WORDOFFS &&
2369 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2370 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2371 }
2372
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002373 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002374 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002375 output->sib = GEN_SIB(0, 4, 5);
2376 output->bytes = 4;
2377 output->modrm = GEN_MODRM(0, rfield, 4);
2378 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002379 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002380 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002381 output->bytes = (addrbits != 16 ? 4 : 2);
2382 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2383 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002384 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 } else {
2386 /*
2387 * It's an indirection.
2388 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002390 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002392 int t, it, bt; /* register numbers */
2393 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002394
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 if (s == 0)
2396 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002397
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002398 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002399 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002400 ix = nasm_reg_flags[i];
2401 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002402 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002403 ix = 0;
2404 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002405
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002406 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002407 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002408 bx = nasm_reg_flags[b];
2409 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002410 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002411 bx = 0;
2412 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002413
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002414 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002415 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002416 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002417 int32_t o = input->offset;
2418 int mod, scale, index, base;
2419
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002420 /*
2421 * For a vector SIB, one has to be a vector and the other,
2422 * if present, a GPR. The vector must be the index operand.
2423 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002424 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002425 if (s == 0)
2426 s = 1;
2427 else if (s != 1)
2428 goto err;
2429
2430 t = bt, bt = it, it = t;
2431 x = bx, bx = ix, ix = x;
2432 }
2433
2434 if (bt != -1) {
2435 if (REG_GPR & ~bx)
2436 goto err;
2437 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2438 sok &= bx;
2439 else
2440 goto err;
2441 }
2442
2443 /*
2444 * While we're here, ensure the user didn't specify
2445 * WORD or QWORD
2446 */
2447 if (input->disp_size == 16 || input->disp_size == 64)
2448 goto err;
2449
2450 if (addrbits == 16 ||
2451 (addrbits == 32 && !(sok & BITS32)) ||
2452 (addrbits == 64 && !(sok & BITS64)))
2453 goto err;
2454
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002455 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2456 : ((ix & YMMREG & ~REG_EA)
2457 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002458
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459 output->rex |= rexflags(it, ix, REX_X);
2460 output->rex |= rexflags(bt, bx, REX_B);
2461 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002462
2463 index = it & 7; /* it is known to be != -1 */
2464
2465 switch (s) {
2466 case 1:
2467 scale = 0;
2468 break;
2469 case 2:
2470 scale = 1;
2471 break;
2472 case 4:
2473 scale = 2;
2474 break;
2475 case 8:
2476 scale = 3;
2477 break;
2478 default: /* then what the smeg is it? */
2479 goto err; /* panic */
2480 }
2481
2482 if (bt == -1) {
2483 base = 5;
2484 mod = 0;
2485 } else {
2486 base = (bt & 7);
2487 if (base != REG_NUM_EBP && o == 0 &&
2488 seg == NO_SEG && !forw_ref &&
2489 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2490 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002491 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002492 mod = 1;
2493 else
2494 mod = 2;
2495 }
2496
2497 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002498 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2499 output->modrm = GEN_MODRM(mod, rfield, 4);
2500 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002501 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002502 /*
2503 * it must be a 32/64-bit memory reference. Firstly we have
2504 * to check that all registers involved are type E/Rxx.
2505 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002506 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002507 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002508
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002509 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002510 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2511 sok &= ix;
2512 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002513 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002514 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002515
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002516 if (bt != -1) {
2517 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002518 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002520 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002521 sok &= bx;
2522 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002523
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 /*
2525 * While we're here, ensure the user didn't specify
2526 * WORD or QWORD
2527 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002528 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002529 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002530
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002531 if (addrbits == 16 ||
2532 (addrbits == 32 && !(sok & BITS32)) ||
2533 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002534 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002535
Keith Kaniosb7a89542007-04-12 02:40:54 +00002536 /* now reorganize base/index */
2537 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002538 ((hb == b && ht == EAH_NOTBASE) ||
2539 (hb == i && ht == EAH_MAKEBASE))) {
2540 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002541 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 x = bx, bx = ix, ix = x;
2543 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002544 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002545 bt = -1, bx = 0, s++;
2546 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002548 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 }
2550 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2551 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002552 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2554 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002555 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002557 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002559 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002560 x = ix, ix = bx, bx = x;
2561 }
2562 if (it == REG_NUM_ESP ||
2563 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002565
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002566 output->rex |= rexflags(it, ix, REX_X);
2567 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568
Keith Kanios48af1772007-08-17 07:37:52 +00002569 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002570 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002572
Keith Kaniosb7a89542007-04-12 02:40:54 +00002573 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002576 } else {
2577 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002578 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002579 seg == NO_SEG && !forw_ref &&
2580 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002581 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002582 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002583 mod = 1;
2584 else
2585 mod = 2;
2586 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002587
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002588 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002589 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2590 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002591 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002594
Keith Kaniosb7a89542007-04-12 02:40:54 +00002595 if (it == -1)
2596 index = 4, s = 1;
2597 else
2598 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 switch (s) {
2601 case 1:
2602 scale = 0;
2603 break;
2604 case 2:
2605 scale = 1;
2606 break;
2607 case 4:
2608 scale = 2;
2609 break;
2610 case 8:
2611 scale = 3;
2612 break;
2613 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002614 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002616
Keith Kaniosb7a89542007-04-12 02:40:54 +00002617 if (bt == -1) {
2618 base = 5;
2619 mod = 0;
2620 } else {
2621 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002622 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002623 seg == NO_SEG && !forw_ref &&
2624 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002625 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002626 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002627 mod = 1;
2628 else
2629 mod = 2;
2630 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002631
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002632 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002633 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2634 output->modrm = GEN_MODRM(mod, rfield, 4);
2635 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 }
2637 } else { /* it's 16-bit */
2638 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002639 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002640
Keith Kaniosb7a89542007-04-12 02:40:54 +00002641 /* check for 64-bit long mode */
2642 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2647 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002649
Keith Kaniosb7a89542007-04-12 02:40:54 +00002650 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002651 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002653
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002655 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 if (b == -1 && i != -1) {
2657 int tmp = b;
2658 b = i;
2659 i = tmp;
2660 } /* swap */
2661 if ((b == R_SI || b == R_DI) && i != -1) {
2662 int tmp = b;
2663 b = i;
2664 i = tmp;
2665 }
2666 /* have BX/BP as base, SI/DI index */
2667 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 if (i != -1 && b != -1 &&
2670 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002671 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002673 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 rm = -1;
2676 if (i != -1)
2677 switch (i * 256 + b) {
2678 case R_SI * 256 + R_BX:
2679 rm = 0;
2680 break;
2681 case R_DI * 256 + R_BX:
2682 rm = 1;
2683 break;
2684 case R_SI * 256 + R_BP:
2685 rm = 2;
2686 break;
2687 case R_DI * 256 + R_BP:
2688 rm = 3;
2689 break;
2690 } else
2691 switch (b) {
2692 case R_SI:
2693 rm = 4;
2694 break;
2695 case R_DI:
2696 rm = 5;
2697 break;
2698 case R_BP:
2699 rm = 6;
2700 break;
2701 case R_BX:
2702 rm = 7;
2703 break;
2704 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002705 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002706 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002707
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002708 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2709 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002711 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 mod = 1;
2713 else
2714 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002715
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002716 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002717 output->bytes = mod; /* bytes of offset needed */
2718 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 }
2720 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002721 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002722
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002723 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002724 return output->type;
2725
2726err:
2727 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002728}
2729
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002730static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002731{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002732 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002733 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002734
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002735 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002736
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002737 switch (ins->prefixes[PPS_ASIZE]) {
2738 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 valid &= 16;
2740 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002741 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002742 valid &= 32;
2743 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002744 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002745 valid &= 64;
2746 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002747 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002748 valid &= (addrbits == 32) ? 16 : 32;
2749 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002750 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002751 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002752 }
2753
2754 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 if (is_class(MEMORY, ins->oprs[j].type)) {
2756 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002759 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002760 i = 0;
2761 else
2762 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002763
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002765 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002766 b = 0;
2767 else
2768 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002769
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002770 if (ins->oprs[j].scale == 0)
2771 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002773 if (!i && !b) {
2774 int ds = ins->oprs[j].disp_size;
2775 if ((addrbits != 64 && ds > 8) ||
2776 (addrbits == 64 && ds == 16))
2777 valid &= ds;
2778 } else {
2779 if (!(REG16 & ~b))
2780 valid &= 16;
2781 if (!(REG32 & ~b))
2782 valid &= 32;
2783 if (!(REG64 & ~b))
2784 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002785
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002786 if (!(REG16 & ~i))
2787 valid &= 16;
2788 if (!(REG32 & ~i))
2789 valid &= 32;
2790 if (!(REG64 & ~i))
2791 valid &= 64;
2792 }
2793 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002794 }
2795
2796 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002797 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002798 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002800 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002801 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002802 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002803 /* Impossible... */
2804 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2805 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002806 }
2807
2808 defdisp = ins->addr_size == 16 ? 16 : 32;
2809
2810 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2812 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2813 /*
2814 * mem_offs sizes must match the address size; if not,
2815 * strip the MEM_OFFS bit and match only EA instructions
2816 */
2817 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2818 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002819 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820}