blob: 6054d4aac5196d27d6f19d8c2941a783fbc48cb1 [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,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700194 /*
195 * Matching success; the conditional ones first
196 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400197 MOK_JUMP, /* Matching OK but needs jmp_match() */
198 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700199};
200
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000201typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700202 enum ea_type type; /* what kind of EA is this? */
203 int sib_present; /* is a SIB byte necessary? */
204 int bytes; /* # of bytes of offset needed */
205 int size; /* lazy - this is sib+bytes+1 */
206 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700207 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208} ea;
209
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400210#define GEN_SIB(scale, index, base) \
211 (((scale) << 6) | ((index) << 3) | ((base)))
212
213#define GEN_MODRM(mod, reg, rm) \
214 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
215
Keith Kaniosb7a89542007-04-12 02:40:54 +0000216static uint32_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217static efunc errfunc;
218static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000219static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800221static int64_t calcsize(int32_t, int64_t, int, insn *,
222 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700223static void gencode(int32_t segment, int64_t offset, int bits,
224 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400225 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700226static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400227 insn *instruction,
228 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700229static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700230static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000231static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700232static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000233static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700234static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700235static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000236
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700237static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700238
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400239static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000240{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700241 return ins->prefixes[pos] == prefix;
242}
243
244static void assert_no_prefix(insn * ins, enum prefix_pos pos)
245{
246 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400247 errfunc(ERR_NONFATAL, "invalid %s prefix",
248 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700249}
250
251static const char *size_name(int size)
252{
253 switch (size) {
254 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400255 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400257 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400259 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700260 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400261 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400263 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400265 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700266 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400267 return "yword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700268 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400269 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000270 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700271}
272
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400273static void warn_overflow(int pass, int size)
274{
275 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
276 "%s data exceeds bounds", size_name(size));
277}
278
279static void warn_overflow_const(int64_t data, int size)
280{
281 if (overflow_general(data, size))
282 warn_overflow(ERR_PASS1, size);
283}
284
285static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700286{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100287 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400288 if (overflow_general(o->offset, size))
289 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700290 }
291}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000293/*
294 * This routine wrappers the real output format's output routine,
295 * in order to pass a copy of the data off to the listing file
296 * generator at the same time.
297 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800298static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800299 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400300 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000301{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000302 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000303 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800304 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000305
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800306 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400307 /*
308 * This is a non-relocated address, and we're going to
309 * convert it into RAWDATA format.
310 */
311 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800312
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400313 if (size > 8) {
314 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
315 return;
316 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700317
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400318 WRITEADDR(q, *(int64_t *)data, size);
319 data = p;
320 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000321 }
322
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800323 list->output(offset, data, type, size);
324
Frank Kotlerabebb082003-09-06 04:45:37 +0000325 /*
326 * this call to src_get determines when we call the
327 * debug-format-specific "linenum" function
328 * it updates lineno and lnfname to the current values
329 * returning 0 if "same as last time", -2 if lnfname
330 * changed, and the amount by which lineno changed,
331 * if it did. thus, these variables must be static
332 */
333
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400334 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000336
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800337 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000338}
339
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400340static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
341{
342 if (opx->segment != NO_SEG) {
343 uint64_t data = opx->offset;
344 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
345 } else {
346 uint8_t byte = opx->offset;
347 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
348 }
349}
350
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700351static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800352 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800354 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800355 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000356 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000357
H. Peter Anvin755f5212012-02-25 11:41:34 -0800358 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700359 return false;
360 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400361 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700362 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400363 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700364
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800365 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100366
Victor van den Elzen154e5922009-02-25 17:32:00 +0100367 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100368 /* Be optimistic in pass 1 */
369 return true;
370
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700372 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000373
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700374 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
375 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000377
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800378int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400379 insn * instruction, struct ofmt *output, efunc error,
380 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000381{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000382 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700384 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800385 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000386 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800387 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300388 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000389
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000391 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 outfmt = output; /* likewise */
393 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000394
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300395 wsize = idata_bytes(instruction->opcode);
396 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000398
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000401 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 if (t < 0)
403 errfunc(ERR_PANIC,
404 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000405
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400407 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400409 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700410 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400411 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000412 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700413 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800415 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400416 offset += wsize;
417 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700418 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400419 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800423 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000425
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 if (align) {
427 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100428 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800429 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 }
431 offset += e->stringlen + align;
432 }
433 }
434 if (t > 0 && t == instruction->times - 1) {
435 /*
436 * Dummy call to list->output to give the offset to the
437 * listing module.
438 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800439 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 list->uplevel(LIST_TIMES);
441 }
442 }
443 if (instruction->times > 1)
444 list->downlevel(LIST_TIMES);
445 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000446 }
447
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700449 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400452 fp = fopen(fname, "rb");
453 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
455 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400456 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
458 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200459 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400460 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700461 static char buf[4096];
462 size_t t = instruction->times;
463 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400464 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000465
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 len = ftell(fp);
467 if (instruction->eops->next) {
468 base = instruction->eops->next->offset;
469 len -= base;
470 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700471 len > (size_t)instruction->eops->next->next->offset)
472 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 }
474 /*
475 * Dummy call to list->output to give the offset to the
476 * listing module.
477 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800478 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 list->uplevel(LIST_INCBIN);
480 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700481 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000482
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 fseek(fp, base, SEEK_SET);
484 l = len;
485 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700486 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400487 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (!m) {
489 /*
490 * This shouldn't happen unless the file
491 * actually changes while we are reading
492 * it.
493 */
494 error(ERR_NONFATAL,
495 "`incbin': unexpected EOF while"
496 " reading file `%s'", fname);
497 t = 0; /* Try to exit cleanly */
498 break;
499 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800500 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 NO_SEG, NO_SEG);
502 l -= m;
503 }
504 }
505 list->downlevel(LIST_INCBIN);
506 if (instruction->times > 1) {
507 /*
508 * Dummy call to list->output to give the offset to the
509 * listing module.
510 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800511 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 list->uplevel(LIST_TIMES);
513 list->downlevel(LIST_TIMES);
514 }
515 fclose(fp);
516 return instruction->times * len;
517 }
518 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 }
520
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700521 /* Check to see if we need an address-size prefix */
522 add_asp(instruction, bits);
523
H. Peter Anvin23595f52009-07-25 17:44:25 -0700524 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700525
H. Peter Anvin23595f52009-07-25 17:44:25 -0700526 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400527 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800528 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400529 itimes = instruction->times;
530 if (insn_size < 0) /* shouldn't be, on pass two */
531 error(ERR_PANIC, "errors made it through from pass one");
532 else
533 while (itimes--) {
534 for (j = 0; j < MAXPREFIX; j++) {
535 uint8_t c = 0;
536 switch (instruction->prefixes[j]) {
537 case P_WAIT:
538 c = 0x9B;
539 break;
540 case P_LOCK:
541 c = 0xF0;
542 break;
543 case P_REPNE:
544 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800545 case P_XACQUIRE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400546 c = 0xF2;
547 break;
548 case P_REPE:
549 case P_REPZ:
550 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800551 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400552 c = 0xF3;
553 break;
554 case R_CS:
555 if (bits == 64) {
556 error(ERR_WARNING | ERR_PASS2,
557 "cs segment base generated, but will be ignored in 64-bit mode");
558 }
559 c = 0x2E;
560 break;
561 case R_DS:
562 if (bits == 64) {
563 error(ERR_WARNING | ERR_PASS2,
564 "ds segment base generated, but will be ignored in 64-bit mode");
565 }
566 c = 0x3E;
567 break;
568 case R_ES:
569 if (bits == 64) {
570 error(ERR_WARNING | ERR_PASS2,
571 "es segment base generated, but will be ignored in 64-bit mode");
572 }
573 c = 0x26;
574 break;
575 case R_FS:
576 c = 0x64;
577 break;
578 case R_GS:
579 c = 0x65;
580 break;
581 case R_SS:
582 if (bits == 64) {
583 error(ERR_WARNING | ERR_PASS2,
584 "ss segment base generated, but will be ignored in 64-bit mode");
585 }
586 c = 0x36;
587 break;
588 case R_SEGR6:
589 case R_SEGR7:
590 error(ERR_NONFATAL,
591 "segr6 and segr7 cannot be used as prefixes");
592 break;
593 case P_A16:
594 if (bits == 64) {
595 error(ERR_NONFATAL,
596 "16-bit addressing is not supported "
597 "in 64-bit mode");
598 } else if (bits != 16)
599 c = 0x67;
600 break;
601 case P_A32:
602 if (bits != 32)
603 c = 0x67;
604 break;
605 case P_A64:
606 if (bits != 64) {
607 error(ERR_NONFATAL,
608 "64-bit addressing is only supported "
609 "in 64-bit mode");
610 }
611 break;
612 case P_ASP:
613 c = 0x67;
614 break;
615 case P_O16:
616 if (bits != 16)
617 c = 0x66;
618 break;
619 case P_O32:
620 if (bits == 16)
621 c = 0x66;
622 break;
623 case P_O64:
624 /* REX.W */
625 break;
626 case P_OSP:
627 c = 0x66;
628 break;
629 case P_none:
630 break;
631 default:
632 error(ERR_PANIC, "invalid instruction prefix");
633 }
634 if (c != 0) {
635 out(offset, segment, &c, OUT_RAWDATA, 1,
636 NO_SEG, NO_SEG);
637 offset++;
638 }
639 }
640 insn_end = offset + insn_size;
641 gencode(segment, offset, bits, instruction,
642 temp, insn_end);
643 offset += insn_size;
644 if (itimes > 0 && itimes == instruction->times - 1) {
645 /*
646 * Dummy call to list->output to give the offset to the
647 * listing module.
648 */
649 list->output(offset, NULL, OUT_RAWDATA, 0);
650 list->uplevel(LIST_TIMES);
651 }
652 }
653 if (instruction->times > 1)
654 list->downlevel(LIST_TIMES);
655 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700656 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400657 /* No match */
658 switch (m) {
659 case MERR_OPSIZEMISSING:
660 error(ERR_NONFATAL, "operation size not specified");
661 break;
662 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400664 break;
665 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400667 break;
668 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800669 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400670 bits);
671 break;
672 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 error(ERR_NONFATAL,
674 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400675 break;
676 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000677 }
678 return 0;
679}
680
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800681int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400682 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000683{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000684 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700685 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000686
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000688 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000689
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400690 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700693 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
694 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400695 instruction->opcode == I_DT || instruction->opcode == I_DO ||
696 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300698 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000699
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300701 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000702
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400703 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000704 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000705
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400707 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400709 warn_overflow_const(e->offset, wsize);
710 } else if (e->type == EOT_DB_STRING ||
711 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 align = (-osize) % wsize;
715 if (align < 0)
716 align += wsize;
717 isize += osize + align;
718 }
719 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000720 }
721
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400723 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300725 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700726 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000727
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400728 fp = fopen(fname, "rb");
729 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000730 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
731 fname);
732 else if (fseek(fp, 0L, SEEK_END) < 0)
733 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
734 fname);
735 else {
736 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 if (instruction->eops->next) {
738 len -= instruction->eops->next->offset;
739 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700740 len > (size_t)instruction->eops->next->next->offset) {
741 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742 }
743 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300744 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300746 if (fp)
747 fclose(fp);
748 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749 }
750
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700751 /* Check to see if we need an address-size prefix */
752 add_asp(instruction, bits);
753
H. Peter Anvin23595f52009-07-25 17:44:25 -0700754 m = find_match(&temp, instruction, segment, offset, bits);
755 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400756 /* we've matched an instruction. */
757 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400758 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100759
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800760 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 if (isize < 0)
762 return -1;
763 for (j = 0; j < MAXPREFIX; j++) {
764 switch (instruction->prefixes[j]) {
765 case P_A16:
766 if (bits != 16)
767 isize++;
768 break;
769 case P_A32:
770 if (bits != 32)
771 isize++;
772 break;
773 case P_O16:
774 if (bits != 16)
775 isize++;
776 break;
777 case P_O32:
778 if (bits == 16)
779 isize++;
780 break;
781 case P_A64:
782 case P_O64:
783 case P_none:
784 break;
785 default:
786 isize++;
787 break;
788 }
789 }
790 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700791 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400792 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000793 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000794}
795
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800796static void bad_hle_warn(const insn * ins, uint8_t hleok)
797{
798 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800799 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800800 static const enum whatwarn warn[2][4] =
801 {
802 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
803 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
804 };
805 unsigned int n;
806
807 n = (unsigned int)rep_pfx - P_XACQUIRE;
808 if (n > 1)
809 return; /* Not XACQUIRE/XRELEASE */
810
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800811 ww = warn[n][hleok];
812 if (!is_class(MEMORY, ins->oprs[0].type))
813 ww = w_inval; /* HLE requires operand 0 to be memory */
814
815 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800816 case w_none:
817 break;
818
819 case w_lock:
820 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800821 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800822 "%s with this instruction requires lock",
823 prefix_name(rep_pfx));
824 }
825 break;
826
827 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800828 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800829 "%s invalid with this instruction",
830 prefix_name(rep_pfx));
831 break;
832 }
833}
834
H. Peter Anvin507ae032008-10-09 15:37:10 -0700835/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400836#define case3(x) case (x): case (x)+1: case (x)+2
837#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700838
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800839static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800840 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000841{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800842 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800843 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000844 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000845 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700846 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700847 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700848 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700849 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800850 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800851 bool lockcheck = true;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000852
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700853 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700854 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700855 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700856
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700857 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400858 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700859
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 (void)segment; /* Don't warn that this parameter is unused */
861 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000862
H. Peter Anvin839eca22007-10-29 23:12:47 -0700863 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400864 c = *codes++;
865 op1 = (c & 3) + ((opex & 1) << 2);
866 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
867 opx = &ins->oprs[op1];
868 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700869
H. Peter Anvin839eca22007-10-29 23:12:47 -0700870 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400871 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 codes += c, length += c;
873 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700874
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400875 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400876 opex = c;
877 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 case4(010):
880 ins->rex |=
881 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 codes++, length++;
883 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400885 case4(020):
886 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 length++;
888 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700889
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400890 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 length += 2;
892 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700893
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400894 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700895 if (opx->type & (BITS16 | BITS32 | BITS64))
896 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 else
898 length += (bits == 16) ? 2 : 4;
899 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700900
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400901 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 length += 4;
903 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400905 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700906 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700908
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400909 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 length++;
911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400913 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000914 length += 8; /* MOV reg64/imm */
915 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400917 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 length += 2;
919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400921 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700922 if (opx->type & (BITS16 | BITS32 | BITS64))
923 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 else
925 length += (bits == 16) ? 2 : 4;
926 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400928 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 length += 4;
930 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700931
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400932 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700933 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400936 case 0172:
937 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700939 length++;
940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700942 case4(0174):
943 length++;
944 break;
945
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700946 case4(0240):
947 ins->rex |= REX_EV;
948 ins->vexreg = regval(opx);
949 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
950 ins->vex_cm = *codes++;
951 ins->vex_wlp = *codes++;
952 ins->evex_tuple = (*codes++ - 0300);
953 break;
954
955 case 0250:
956 ins->rex |= REX_EV;
957 ins->vexreg = 0;
958 ins->vex_cm = *codes++;
959 ins->vex_wlp = *codes++;
960 ins->evex_tuple = (*codes++ - 0300);
961 break;
962
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400963 case4(0254):
964 length += 4;
965 break;
966
967 case4(0260):
968 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700969 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 ins->vex_cm = *codes++;
971 ins->vex_wlp = *codes++;
972 break;
973
974 case 0270:
975 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700976 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 ins->vex_cm = *codes++;
978 ins->vex_wlp = *codes++;
979 break;
980
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400981 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800982 hleok = c & 3;
983 break;
984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(0274):
986 length++;
987 break;
988
989 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 if (bits == 64)
994 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700995 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700997
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700999 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
Keith Kaniosb7a89542007-04-12 02:40:54 +00001005 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1007 has_prefix(ins, PPS_ASIZE, P_A32))
1008 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(0314):
1012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001015 {
1016 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1017 if (pfx == P_O16)
1018 break;
1019 if (pfx != P_none)
1020 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1021 else
1022 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001024 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001027 {
1028 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1029 if (pfx == P_O32)
1030 break;
1031 if (pfx != P_none)
1032 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1033 else
1034 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001036 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 case 0322:
1039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
Keith Kaniosb7a89542007-04-12 02:40:54 +00001041 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001042 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001043 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001044
Keith Kaniosb7a89542007-04-12 02:40:54 +00001045 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001046 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 case 0325:
1050 ins->rex |= REX_NH;
1051 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001052
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001053 case 0326:
1054 break;
1055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 case 0330:
1057 codes++, length++;
1058 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001062
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001063 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 case 0333:
1065 length++;
1066 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001067
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 case 0334:
1069 ins->rex |= REX_L;
1070 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001071
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001072 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001073 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001074
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001075 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001076 if (!ins->prefixes[PPS_REP])
1077 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001079
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001080 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001081 if (!ins->prefixes[PPS_REP])
1082 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 if (ins->oprs[0].segment != NO_SEG)
1087 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1088 " quantity of BSS space");
1089 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001090 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001093 case 0341:
1094 if (!ins->prefixes[PPS_WAIT])
1095 ins->prefixes[PPS_WAIT] = P_WAIT;
1096 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001097
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001098 case 0360:
1099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001101 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001102 length++;
1103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 case 0364:
1106 case 0365:
1107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
Keith Kanios48af1772007-08-17 07:37:52 +00001109 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001110 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 length++;
1112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001114 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 case 0373:
1118 length++;
1119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001121 case 0374:
1122 eat = EA_XMMVSIB;
1123 break;
1124
1125 case 0375:
1126 eat = EA_YMMVSIB;
1127 break;
1128
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001129 case 0376:
1130 eat = EA_ZMMVSIB;
1131 break;
1132
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001133 case4(0100):
1134 case4(0110):
1135 case4(0120):
1136 case4(0130):
1137 case4(0200):
1138 case4(0204):
1139 case4(0210):
1140 case4(0214):
1141 case4(0220):
1142 case4(0224):
1143 case4(0230):
1144 case4(0234):
1145 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001147 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 opflags_t rflags;
1149 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001150 struct operand *oplast;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001151
Keith Kaniosb7a89542007-04-12 02:40:54 +00001152 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001153
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 if (c <= 0177) {
1155 /* pick rfield from operand b (opx) */
1156 rflags = regflag(opx);
1157 rfield = nasm_regvals[opx->basereg];
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001158 /* find the last SIMD operand where ER decorator resides */
1159 oplast = &ins->oprs[op1 > op2 ? op1 : op2];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001160 } else {
1161 rflags = 0;
1162 rfield = c & 7;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001163 oplast = opy;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001165
1166 if (oplast->decoflags & ER) {
1167 /* set EVEX.RC (rounding control) and b */
1168 ins->evex_p[2] |= (((ins->evex_rm - BRC_RN) << 5) & EVEX_P2LL) |
1169 EVEX_P2B;
1170 } else {
1171 /* set EVEX.L'L (vector length) */
1172 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
1173 if ((oplast->decoflags & SAE) ||
1174 (opy->decoflags & BRDCAST_MASK)) {
1175 /* set EVEX.b */
1176 ins->evex_p[2] |= EVEX_P2B;
1177 }
1178 }
1179
1180 if (process_ea(opy, &ea_data, bits,
1181 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 errfunc(ERR_NONFATAL, "invalid effective address");
1183 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001184 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001187 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 }
1189 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 default:
1192 errfunc(ERR_PANIC, "internal instruction table corrupt"
1193 ": instruction code \\%o (0x%02X) given", c, c);
1194 break;
1195 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001196 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001197
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001198 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001199
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001200 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001201 if (ins->rex & REX_H) {
1202 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1203 return -1;
1204 }
1205 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001206 }
1207
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001208 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001210
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001211 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001212 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001213 return -1;
1214 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001215 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001216 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001217 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 ins->rex &= ~REX_W;
1219 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001220 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001221 ins->rex |= REX_W;
1222 bad32 &= ~REX_W;
1223 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001224 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 /* Follow REX_W */
1226 break;
1227 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001228
H. Peter Anvinfc561202011-07-07 16:58:22 -07001229 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001230 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1231 return -1;
1232 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001233 if (ins->rex & REX_EV)
1234 length += 4;
1235 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001236 length += 3;
1237 else
1238 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001239 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 if (ins->rex & REX_H) {
1241 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1242 return -1;
1243 } else if (bits == 64) {
1244 length++;
1245 } else if ((ins->rex & REX_L) &&
1246 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1247 cpu >= IF_X86_64) {
1248 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001249 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001250 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 length++;
1252 } else {
1253 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1254 return -1;
1255 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001256 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001257
1258 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1259 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001260 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001261 "instruction is not lockable");
1262 }
1263
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001264 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001265
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001266 return length;
1267}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001268
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001269static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1270{
1271 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001272 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001273 ins->rex = (ins->rex & REX_REAL) | REX_P;
1274 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1275 ins->rex = 0;
1276 return 1;
1277 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001278 }
1279
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001280 return 0;
1281}
1282
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001283static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001284 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001285 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001287 uint8_t c;
1288 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001289 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001290 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001291 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001292 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001293 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001294 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001295 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001296
H. Peter Anvin839eca22007-10-29 23:12:47 -07001297 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 c = *codes++;
1299 op1 = (c & 3) + ((opex & 1) << 2);
1300 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1301 opx = &ins->oprs[op1];
1302 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001303
H. Peter Anvin839eca22007-10-29 23:12:47 -07001304 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 case 01:
1306 case 02:
1307 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001309 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001310 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 codes += c;
1312 offset += c;
1313 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001314
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 case 05:
1316 case 06:
1317 case 07:
1318 opex = c;
1319 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001320
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001322 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001323 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001324 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 offset += 1;
1326 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001327
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001329 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001330 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001333 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 offset += 1;
1335 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001338 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001339 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001341 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 offset += 1;
1343 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001344
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001346 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001347 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001348 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001349 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 offset += 2;
1351 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001353 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001354 if (opx->type & (BITS16 | BITS32))
1355 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 else
1357 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001358 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001359 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001360 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001361 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 offset += size;
1363 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001364
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001366 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001367 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001368 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001369 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 offset += 4;
1371 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001372
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001374 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001375 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001376 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001377 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001378 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 offset += size;
1380 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001381
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001383 if (opx->segment != segment) {
1384 data = opx->offset;
1385 out(offset, segment, &data,
1386 OUT_REL1ADR, insn_end - offset,
1387 opx->segment, opx->wrt);
1388 } else {
1389 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 if (data > 127 || data < -128)
1391 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001392 out(offset, segment, &data,
1393 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1394 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 offset += 1;
1396 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001397
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001398 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001399 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001400 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001401 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001402 offset += 8;
1403 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001405 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001406 if (opx->segment != segment) {
1407 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001409 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001410 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001412 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001414 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 }
1416 offset += 2;
1417 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001418
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001419 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001420 if (opx->type & (BITS16 | BITS32 | BITS64))
1421 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 else
1423 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001424 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001426 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001427 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1428 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001430 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001432 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 }
1434 offset += size;
1435 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001436
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001437 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001438 if (opx->segment != segment) {
1439 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001441 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001442 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001444 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001446 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 }
1448 offset += 4;
1449 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001451 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001452 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001453 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1454 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001455 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001456 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 outfmt->segbase(1 + opx->segment),
1458 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001459 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 case 0172:
1463 c = *codes++;
1464 opx = &ins->oprs[c >> 3];
1465 bytes[0] = nasm_regvals[opx->basereg] << 4;
1466 opx = &ins->oprs[c & 7];
1467 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1468 errfunc(ERR_NONFATAL,
1469 "non-absolute expression not permitted as argument %d",
1470 c & 7);
1471 } else {
1472 if (opx->offset & ~15) {
1473 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1474 "four-bit argument exceeds bounds");
1475 }
1476 bytes[0] |= opx->offset & 15;
1477 }
1478 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1479 offset++;
1480 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001481
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001482 case 0173:
1483 c = *codes++;
1484 opx = &ins->oprs[c >> 4];
1485 bytes[0] = nasm_regvals[opx->basereg] << 4;
1486 bytes[0] |= c & 15;
1487 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1488 offset++;
1489 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001490
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001491 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001492 bytes[0] = nasm_regvals[opx->basereg] << 4;
1493 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1494 offset++;
1495 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001496
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001497 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001498 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1500 (int32_t)data != (int64_t)data) {
1501 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1502 "signed dword immediate exceeds bounds");
1503 }
1504 out(offset, segment, &data, OUT_ADDRESS, 4,
1505 opx->segment, opx->wrt);
1506 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001507 break;
1508
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001509 case4(0240):
1510 case 0250:
1511 codes += 3;
1512 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1513 EVEX_P2Z | EVEX_P2AAA, 2);
1514 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1515 bytes[0] = 0x62;
1516 /* EVEX.X can be set by either REX or EVEX for different reasons */
1517 bytes[1] = (~(((ins->rex & 7) << 5) |
1518 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) & 0xf0) |
1519 (ins->vex_cm & 3);
1520 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1521 ((~ins->vexreg & 15) << 3) |
1522 (1 << 2) | (ins->vex_wlp & 3);
1523 bytes[3] = ins->evex_p[2];
1524 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1525 offset += 4;
1526 break;
1527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(0260):
1529 case 0270:
1530 codes += 2;
1531 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1532 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1533 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1534 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001535 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1537 offset += 3;
1538 } else {
1539 bytes[0] = 0xc5;
1540 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001541 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1543 offset += 2;
1544 }
1545 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001546
H. Peter Anvine014f352012-02-25 22:35:19 -08001547 case 0271:
1548 case 0272:
1549 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001550 break;
1551
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 case4(0274):
1553 {
1554 uint64_t uv, um;
1555 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001556
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 if (ins->rex & REX_W)
1558 s = 64;
1559 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1560 s = 16;
1561 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1562 s = 32;
1563 else
1564 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001565
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001566 um = (uint64_t)2 << (s-1);
1567 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001568
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001569 if (uv > 127 && uv < (uint64_t)-128 &&
1570 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001571 /* If this wasn't explicitly byte-sized, warn as though we
1572 * had fallen through to the imm16/32/64 case.
1573 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001574 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001575 "%s value exceeds bounds",
1576 (opx->type & BITS8) ? "signed byte" :
1577 s == 16 ? "word" :
1578 s == 32 ? "dword" :
1579 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001581 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001582 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001583 out(offset, segment, &data, OUT_ADDRESS, 1,
1584 opx->segment, opx->wrt);
1585 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001586 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001587 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1588 NO_SEG);
1589 }
1590 offset += 1;
1591 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001596
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001598 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001600 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 offset += 1;
1602 } else
1603 offset += 0;
1604 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001607 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001609 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 offset += 1;
1611 } else
1612 offset += 0;
1613 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 case 0312:
1616 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617
Keith Kaniosb7a89542007-04-12 02:40:54 +00001618 case 0313:
1619 ins->rex = 0;
1620 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001621
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001622 case4(0314):
1623 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001624
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001628
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001630 case 0323:
1631 break;
1632
Keith Kaniosb7a89542007-04-12 02:40:54 +00001633 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001634 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001636
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 case 0325:
1638 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001639
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001640 case 0326:
1641 break;
1642
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001644 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001645 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 offset += 1;
1647 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001654 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001655 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 offset += 1;
1657 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001658
Keith Kanios48af1772007-08-17 07:37:52 +00001659 case 0334:
1660 if (ins->rex & REX_R) {
1661 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001662 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001663 offset += 1;
1664 }
1665 ins->rex &= ~(REX_L|REX_R);
1666 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001667
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001668 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001670
H. Peter Anvin962e3052008-08-28 17:47:16 -07001671 case 0336:
1672 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 if (ins->oprs[0].segment != NO_SEG)
1677 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1678 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001679 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 if (size > 0)
1681 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001682 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 offset += size;
1684 }
1685 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001686
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 case 0341:
1688 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001689
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001690 case 0360:
1691 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001692
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 case 0361:
1694 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001695 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1696 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001698
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 case 0364:
1700 case 0365:
1701 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001702
Keith Kanios48af1772007-08-17 07:37:52 +00001703 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001705 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001706 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001707 offset += 1;
1708 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001709
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 case 0370:
1711 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001713
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 case 0373:
1715 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001716 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 offset += 1;
1718 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001720 case 0374:
1721 eat = EA_XMMVSIB;
1722 break;
1723
1724 case 0375:
1725 eat = EA_YMMVSIB;
1726 break;
1727
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001728 case 0376:
1729 eat = EA_ZMMVSIB;
1730 break;
1731
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001732 case4(0100):
1733 case4(0110):
1734 case4(0120):
1735 case4(0130):
1736 case4(0200):
1737 case4(0204):
1738 case4(0210):
1739 case4(0214):
1740 case4(0220):
1741 case4(0224):
1742 case4(0230):
1743 case4(0234):
1744 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 ea ea_data;
1746 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001747 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001748 uint8_t *p;
1749 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001751
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001752 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 /* pick rfield from operand b (opx) */
1754 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001755 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001756 } else {
1757 /* rfield is constant */
1758 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001762 if (process_ea(opy, &ea_data, bits,
1763 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 p = bytes;
1767 *p++ = ea_data.modrm;
1768 if (ea_data.sib_present)
1769 *p++ = ea_data.sib;
1770
1771 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001772 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001774 /*
1775 * Make sure the address gets the right offset in case
1776 * the line breaks in the .lst file (BR 1197827)
1777 */
1778 offset += s;
1779 s = 0;
1780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 switch (ea_data.bytes) {
1782 case 0:
1783 break;
1784 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 2:
1786 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001787 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001788 /* use compressed displacement, if available */
1789 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001791 if (ea_data.rip) {
1792 if (opy->segment == segment) {
1793 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001794 if (overflow_signed(data, ea_data.bytes))
1795 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 out(offset, segment, &data, OUT_ADDRESS,
1797 ea_data.bytes, NO_SEG, NO_SEG);
1798 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001799 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 out(offset, segment, &data, OUT_REL4ADR,
1801 insn_end - offset, opy->segment, opy->wrt);
1802 }
1803 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001804 if (overflow_general(data, ins->addr_size >> 3) ||
1805 signed_bits(data, ins->addr_size) !=
1806 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001807 warn_overflow(ERR_PASS2, ea_data.bytes);
1808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001809 out(offset, segment, &data, OUT_ADDRESS,
1810 ea_data.bytes, opy->segment, opy->wrt);
1811 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001813 default:
1814 /* Impossible! */
1815 errfunc(ERR_PANIC,
1816 "Invalid amount of bytes (%d) for offset?!",
1817 ea_data.bytes);
1818 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 }
1820 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 }
1822 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001823
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001824 default:
1825 errfunc(ERR_PANIC, "internal instruction table corrupt"
1826 ": instruction code \\%o (0x%02X) given", c, c);
1827 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001829 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830}
1831
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001832static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001833{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001834 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001835 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001836 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001837}
1838
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001839static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001841 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001843 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001844}
1845
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001846static int op_rexflags(const operand * o, int mask)
1847{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001848 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001849 int val;
1850
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001851 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001852 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001853
H. Peter Anvina4835d42008-05-20 14:21:29 -07001854 flags = nasm_reg_flags[o->basereg];
1855 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001856
1857 return rexflags(val, flags, mask);
1858}
1859
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001860static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001861{
1862 int rex = 0;
1863
1864 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001866 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 rex |= REX_W;
1868 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1869 rex |= REX_H;
1870 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1871 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001872
1873 return rex & mask;
1874}
1875
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001876static int evexflags(int val, decoflags_t deco,
1877 int mask, uint8_t byte)
1878{
1879 int evex = 0;
1880
1881 switch(byte) {
1882 case 0:
1883 if (val >= 16)
1884 evex |= (EVEX_P0RP | EVEX_P0X);
1885 break;
1886 case 2:
1887 if (val >= 16)
1888 evex |= EVEX_P2VP;
1889 if (deco & Z)
1890 evex |= EVEX_P2Z;
1891 if (deco & OPMASK_MASK)
1892 evex |= deco & EVEX_P2AAA;
1893 break;
1894 }
1895 return evex & mask;
1896}
1897
1898static int op_evexflags(const operand * o, int mask, uint8_t byte)
1899{
1900 int val;
1901
1902 if (!is_register(o->basereg))
1903 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1904
1905 val = nasm_regvals[o->basereg];
1906
1907 return evexflags(val, o->decoflags, mask, byte);
1908}
1909
H. Peter Anvin23595f52009-07-25 17:44:25 -07001910static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 insn *instruction,
1912 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001913{
1914 const struct itemplate *temp;
1915 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001916 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001917 bool opsizemissing = false;
1918 int i;
1919
1920 for (i = 0; i < instruction->operands; i++)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001921 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001922
1923 merr = MERR_INVALOP;
1924
1925 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 temp->opcode != I_none; temp++) {
1927 m = matches(temp, instruction, bits);
1928 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001929 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001930 m = MOK_GOOD;
1931 else
1932 m = MERR_INVALOP;
1933 } else if (m == MERR_OPSIZEMISSING &&
1934 (temp->flags & IF_SMASK) != IF_SX) {
1935 /*
1936 * Missing operand size and a candidate for fuzzy matching...
1937 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001938 for (i = 0; i < temp->operands; i++)
1939 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 opsizemissing = true;
1941 }
1942 if (m > merr)
1943 merr = m;
1944 if (merr == MOK_GOOD)
1945 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001946 }
1947
1948 /* No match, but see if we can get a fuzzy operand size match... */
1949 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001951
1952 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001953 /*
1954 * We ignore extrinsic operand sizes on registers, so we should
1955 * never try to fuzzy-match on them. This also resolves the case
1956 * when we have e.g. "xmmrm128" in two different positions.
1957 */
1958 if (is_class(REGISTER, instruction->oprs[i].type))
1959 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07001960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 /* This tests if xsizeflags[i] has more than one bit set */
1962 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1963 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001965 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001966 }
1967
1968 /* Try matching again... */
1969 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 temp->opcode != I_none; temp++) {
1971 m = matches(temp, instruction, bits);
1972 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001973 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001974 m = MOK_GOOD;
1975 else
1976 m = MERR_INVALOP;
1977 }
1978 if (m > merr)
1979 merr = m;
1980 if (merr == MOK_GOOD)
1981 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001982 }
1983
H. Peter Anvina81655b2009-07-25 18:15:28 -07001984done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07001985 *tempp = temp;
1986 return merr;
1987}
1988
H. Peter Anvin65289e82009-07-25 17:25:11 -07001989static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001990 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001991{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001992 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001993 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001994 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001995
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001996 /*
1997 * Check the opcode
1998 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002000 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002001
2002 /*
2003 * Count the operands
2004 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002006 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002007
2008 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002009 * Is it legal?
2010 */
2011 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2012 return MERR_INVALOP;
2013
2014 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002015 * Check that no spurious colons or TOs are present
2016 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 for (i = 0; i < itemp->operands; i++)
2018 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002019 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002020
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002021 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002022 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002023 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002024 switch (itemp->flags & IF_SMASK) {
2025 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002026 asize = BITS8;
2027 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002028 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002029 asize = BITS16;
2030 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002031 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002032 asize = BITS32;
2033 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002034 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002035 asize = BITS64;
2036 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002037 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002038 asize = BITS128;
2039 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002040 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002041 asize = BITS256;
2042 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002043 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002044 asize = BITS512;
2045 break;
2046 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002047 switch (bits) {
2048 case 16:
2049 asize = BITS16;
2050 break;
2051 case 32:
2052 asize = BITS32;
2053 break;
2054 case 64:
2055 asize = BITS64;
2056 break;
2057 default:
2058 asize = 0;
2059 break;
2060 }
2061 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002062 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002063 asize = 0;
2064 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002065 }
2066
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002067 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002068 /* S- flags only apply to a specific operand */
2069 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2070 memset(size, 0, sizeof size);
2071 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002072 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002073 /* S- flags apply to all operands */
2074 for (i = 0; i < MAX_OPERANDS; i++)
2075 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002076 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002077
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002078 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002079 * Check that the operand flags all match up,
2080 * it's a bit tricky so lets be verbose:
2081 *
2082 * 1) Find out the size of operand. If instruction
2083 * doesn't have one specified -- we're trying to
2084 * guess it either from template (IF_S* flag) or
2085 * from code bits.
2086 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002087 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002088 * template has an operand size specified AND this size differ
2089 * from which instruction has (perhaps we got it from code bits)
2090 * we are:
2091 * a) Check that only size of instruction and operand is differ
2092 * other characteristics do match
2093 * b) Perhaps it's a register specified in instruction so
2094 * for such a case we just mark that operand as "size
2095 * missing" and this will turn on fuzzy operand size
2096 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002097 */
2098 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002099 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002100 decoflags_t deco = instruction->oprs[i].decoflags;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002101 if (!(type & SIZE_MASK))
2102 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002103
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002104 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2105 (itemp->deco[i] & deco) != deco) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002106 return MERR_INVALOP;
2107 } else if ((itemp->opd[i] & SIZE_MASK) &&
2108 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2109 if (type & SIZE_MASK) {
H. Peter Anvin65289e82009-07-25 17:25:11 -07002110 return MERR_INVALOP;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002111 } else if (!is_class(REGISTER, type)) {
2112 /*
2113 * Note: we don't honor extrinsic operand sizes for registers,
2114 * so "missing operand size" for a register should be
2115 * considered a wildcard match rather than an error.
2116 */
2117 opsizemissing = true;
2118 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002119 }
2120 }
2121
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002122 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002123 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002124
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002125 /*
2126 * Check operand sizes
2127 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002128 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002131 asize = itemp->opd[i] & SIZE_MASK;
2132 if (asize) {
2133 for (i = 0; i < oprs; i++)
2134 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 break;
2136 }
2137 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002138 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002140 }
2141
Keith Kaniosb7a89542007-04-12 02:40:54 +00002142 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 if (!(itemp->opd[i] & SIZE_MASK) &&
2144 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002145 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002146 }
2147
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002148 /*
2149 * Check template is okay at the set cpu level
2150 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002151 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002152 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002153
Keith Kaniosb7a89542007-04-12 02:40:54 +00002154 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002155 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002156 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002157 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002158 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002160 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002161 * If we have a HLE prefix, look for the NOHLE flag
2162 */
2163 if ((itemp->flags & IF_NOHLE) &&
2164 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2165 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2166 return MERR_BADHLE;
2167
2168 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002169 * Check if special handling needed for Jumps
2170 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002171 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002172 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173
H. Peter Anvin60926242009-07-26 16:25:38 -07002174 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002175}
2176
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002177/*
2178 * Check if offset is a multiple of N with corresponding tuple type
2179 * if Disp8*N is available, compressed displacement is stored in compdisp
2180 */
2181static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2182{
2183 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2184 {{16, 32, 64}, {8, 8, 8}}};
2185 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2186 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2187
2188 bool evex_b = input->decoflags & BRDCAST_MASK;
2189 enum ttypes tuple = ins->evex_tuple;
2190 /* vex_wlp composed as [wwllpp] */
2191 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2192 /* wig(=2) is treated as w0(=0) */
2193 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2194 int32_t off = input->offset;
2195 uint8_t n = 0;
2196 int32_t disp8;
2197
2198 switch(tuple) {
2199 case FV:
2200 n = fv_n[evex_w][evex_b][vectlen];
2201 break;
2202 case HV:
2203 n = hv_n[evex_b][vectlen];
2204 break;
2205
2206 case FVM:
2207 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2208 n = 1 << (vectlen + 4);
2209 break;
2210 case T1S8: /* N = 1 */
2211 case T1S16: /* N = 2 */
2212 n = tuple - T1S8 + 1;
2213 break;
2214 case T1S:
2215 /* N = 4 for 32bit, 8 for 64bit */
2216 n = evex_w ? 8 : 4;
2217 break;
2218 case T1F32:
2219 case T1F64:
2220 /* N = 4 for 32bit, 8 for 64bit */
2221 n = (tuple == T1F32 ? 4 : 8);
2222 break;
2223 case T2:
2224 case T4:
2225 case T8:
2226 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2227 n = 0;
2228 else
2229 n = 1 << (tuple - T2 + evex_w + 4);
2230 break;
2231 case HVM:
2232 case QVM:
2233 case OVM:
2234 n = 1 << (OVM - tuple + vectlen + 1);
2235 break;
2236 case M128:
2237 n = 16;
2238 break;
2239 case DUP:
2240 n = dup_n[vectlen];
2241 break;
2242
2243 default:
2244 break;
2245 }
2246
2247 if (n && !(off & (n - 1))) {
2248 disp8 = off / n;
2249 /* if it fits in Disp8 */
2250 if (disp8 >= -128 && disp8 <= 127) {
2251 *compdisp = disp8;
2252 return true;
2253 }
2254 }
2255
2256 *compdisp = 0;
2257 return false;
2258}
2259
2260/*
2261 * Check if ModR/M.mod should/can be 01.
2262 * - EAF_BYTEOFFS is set
2263 * - offset can fit in a byte when EVEX is not used
2264 * - offset can be compressed when EVEX is used
2265 */
2266#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2267 (o >= -128 && o <= 127 && \
2268 seg == NO_SEG && !forw_ref && \
2269 !(input->eaflags & EAF_WORDOFFS) && \
2270 !(ins->rex & REX_EV)) || \
2271 (ins->rex & REX_EV && \
2272 is_disp8n(input, ins, &output->disp8)))
2273
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002274static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002275 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002276{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002277 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002278 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002279
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002280 output->type = EA_SCALAR;
2281 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002282
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002283 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002284 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002285 /* EVEX.R' flag for the REG operand */
2286 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002287
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002288 if (is_class(REGISTER, input->type)) {
2289 /*
2290 * It's a direct register.
2291 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002292 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002293 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002294
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002295 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002296 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002297
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002298 /* broadcasting is not available with a direct register operand. */
2299 if (input->decoflags & BRDCAST_MASK) {
2300 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2301 goto err;
2302 }
2303
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002304 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002305 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002306 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002307 output->bytes = 0; /* no offset necessary either */
2308 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2309 } else {
2310 /*
2311 * It's a memory reference.
2312 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002313
2314 /* Embedded rounding or SAE is not available with a mem ref operand. */
2315 if (input->decoflags & (ER | SAE)) {
2316 nasm_error(ERR_NONFATAL,
2317 "Embedded rounding is available only with reg-reg op.");
2318 return -1;
2319 }
2320
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002321 if (input->basereg == -1 &&
2322 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002323 /*
2324 * It's a pure offset.
2325 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002326 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2327 input->segment == NO_SEG) {
2328 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2329 input->type &= ~IP_REL;
2330 input->type |= MEMORY;
2331 }
2332
2333 if (input->eaflags & EAF_BYTEOFFS ||
2334 (input->eaflags & EAF_WORDOFFS &&
2335 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2336 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2337 }
2338
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002339 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002340 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002341 output->sib = GEN_SIB(0, 4, 5);
2342 output->bytes = 4;
2343 output->modrm = GEN_MODRM(0, rfield, 4);
2344 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002345 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002346 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002347 output->bytes = (addrbits != 16 ? 4 : 2);
2348 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2349 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002350 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002351 } else {
2352 /*
2353 * It's an indirection.
2354 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002356 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002358 int t, it, bt; /* register numbers */
2359 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002360
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 if (s == 0)
2362 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002363
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002364 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002365 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002366 ix = nasm_reg_flags[i];
2367 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002368 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002369 ix = 0;
2370 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002371
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002372 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002373 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002374 bx = nasm_reg_flags[b];
2375 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002376 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002377 bx = 0;
2378 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002379
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002380 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002381 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002382 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002383 int32_t o = input->offset;
2384 int mod, scale, index, base;
2385
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002386 /*
2387 * For a vector SIB, one has to be a vector and the other,
2388 * if present, a GPR. The vector must be the index operand.
2389 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002390 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002391 if (s == 0)
2392 s = 1;
2393 else if (s != 1)
2394 goto err;
2395
2396 t = bt, bt = it, it = t;
2397 x = bx, bx = ix, ix = x;
2398 }
2399
2400 if (bt != -1) {
2401 if (REG_GPR & ~bx)
2402 goto err;
2403 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2404 sok &= bx;
2405 else
2406 goto err;
2407 }
2408
2409 /*
2410 * While we're here, ensure the user didn't specify
2411 * WORD or QWORD
2412 */
2413 if (input->disp_size == 16 || input->disp_size == 64)
2414 goto err;
2415
2416 if (addrbits == 16 ||
2417 (addrbits == 32 && !(sok & BITS32)) ||
2418 (addrbits == 64 && !(sok & BITS64)))
2419 goto err;
2420
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002421 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2422 : ((ix & YMMREG & ~REG_EA)
2423 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002424
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002425 output->rex |= rexflags(it, ix, REX_X);
2426 output->rex |= rexflags(bt, bx, REX_B);
2427 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002428
2429 index = it & 7; /* it is known to be != -1 */
2430
2431 switch (s) {
2432 case 1:
2433 scale = 0;
2434 break;
2435 case 2:
2436 scale = 1;
2437 break;
2438 case 4:
2439 scale = 2;
2440 break;
2441 case 8:
2442 scale = 3;
2443 break;
2444 default: /* then what the smeg is it? */
2445 goto err; /* panic */
2446 }
2447
2448 if (bt == -1) {
2449 base = 5;
2450 mod = 0;
2451 } else {
2452 base = (bt & 7);
2453 if (base != REG_NUM_EBP && o == 0 &&
2454 seg == NO_SEG && !forw_ref &&
2455 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2456 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002458 mod = 1;
2459 else
2460 mod = 2;
2461 }
2462
2463 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002464 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2465 output->modrm = GEN_MODRM(mod, rfield, 4);
2466 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002467 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002468 /*
2469 * it must be a 32/64-bit memory reference. Firstly we have
2470 * to check that all registers involved are type E/Rxx.
2471 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002472 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002473 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002474
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002475 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002476 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2477 sok &= ix;
2478 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002479 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002480 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002481
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002482 if (bt != -1) {
2483 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002484 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002486 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002487 sok &= bx;
2488 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002489
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002490 /*
2491 * While we're here, ensure the user didn't specify
2492 * WORD or QWORD
2493 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002494 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002495 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002496
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002497 if (addrbits == 16 ||
2498 (addrbits == 32 && !(sok & BITS32)) ||
2499 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002500 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002501
Keith Kaniosb7a89542007-04-12 02:40:54 +00002502 /* now reorganize base/index */
2503 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002504 ((hb == b && ht == EAH_NOTBASE) ||
2505 (hb == i && ht == EAH_MAKEBASE))) {
2506 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002507 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002508 x = bx, bx = ix, ix = x;
2509 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002510 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002511 bt = -1, bx = 0, s++;
2512 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002513 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002514 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 }
2516 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2517 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002518 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2520 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002521 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002523 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002525 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 x = ix, ix = bx, bx = x;
2527 }
2528 if (it == REG_NUM_ESP ||
2529 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002530 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002531
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002532 output->rex |= rexflags(it, ix, REX_X);
2533 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002534
Keith Kanios48af1772007-08-17 07:37:52 +00002535 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002536 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002538
Keith Kaniosb7a89542007-04-12 02:40:54 +00002539 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002542 } else {
2543 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002544 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 seg == NO_SEG && !forw_ref &&
2546 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002547 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002548 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002549 mod = 1;
2550 else
2551 mod = 2;
2552 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002553
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002554 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002555 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2556 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002557 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002560
Keith Kaniosb7a89542007-04-12 02:40:54 +00002561 if (it == -1)
2562 index = 4, s = 1;
2563 else
2564 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 switch (s) {
2567 case 1:
2568 scale = 0;
2569 break;
2570 case 2:
2571 scale = 1;
2572 break;
2573 case 4:
2574 scale = 2;
2575 break;
2576 case 8:
2577 scale = 3;
2578 break;
2579 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002582
Keith Kaniosb7a89542007-04-12 02:40:54 +00002583 if (bt == -1) {
2584 base = 5;
2585 mod = 0;
2586 } else {
2587 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002588 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 seg == NO_SEG && !forw_ref &&
2590 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002591 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002592 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002593 mod = 1;
2594 else
2595 mod = 2;
2596 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002597
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002598 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002599 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2600 output->modrm = GEN_MODRM(mod, rfield, 4);
2601 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 }
2603 } else { /* it's 16-bit */
2604 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002605 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002606
Keith Kaniosb7a89542007-04-12 02:40:54 +00002607 /* check for 64-bit long mode */
2608 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002612 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2613 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002614 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002615
Keith Kaniosb7a89542007-04-12 02:40:54 +00002616 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002617 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002618 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002621 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 if (b == -1 && i != -1) {
2623 int tmp = b;
2624 b = i;
2625 i = tmp;
2626 } /* swap */
2627 if ((b == R_SI || b == R_DI) && i != -1) {
2628 int tmp = b;
2629 b = i;
2630 i = tmp;
2631 }
2632 /* have BX/BP as base, SI/DI index */
2633 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 if (i != -1 && b != -1 &&
2636 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 rm = -1;
2642 if (i != -1)
2643 switch (i * 256 + b) {
2644 case R_SI * 256 + R_BX:
2645 rm = 0;
2646 break;
2647 case R_DI * 256 + R_BX:
2648 rm = 1;
2649 break;
2650 case R_SI * 256 + R_BP:
2651 rm = 2;
2652 break;
2653 case R_DI * 256 + R_BP:
2654 rm = 3;
2655 break;
2656 } else
2657 switch (b) {
2658 case R_SI:
2659 rm = 4;
2660 break;
2661 case R_DI:
2662 rm = 5;
2663 break;
2664 case R_BP:
2665 rm = 6;
2666 break;
2667 case R_BX:
2668 rm = 7;
2669 break;
2670 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002671 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002672 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002673
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002674 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2675 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002677 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 mod = 1;
2679 else
2680 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002681
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002682 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002683 output->bytes = mod; /* bytes of offset needed */
2684 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 }
2686 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002687 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002688
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002689 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002690 return output->type;
2691
2692err:
2693 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002694}
2695
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002696static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002697{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002698 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002699 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002700
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002701 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002702
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002703 switch (ins->prefixes[PPS_ASIZE]) {
2704 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002705 valid &= 16;
2706 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002707 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002708 valid &= 32;
2709 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002710 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002711 valid &= 64;
2712 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002713 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002714 valid &= (addrbits == 32) ? 16 : 32;
2715 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002716 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002717 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002718 }
2719
2720 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002721 if (is_class(MEMORY, ins->oprs[j].type)) {
2722 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002724 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002725 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 i = 0;
2727 else
2728 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002729
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002730 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002731 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002732 b = 0;
2733 else
2734 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002736 if (ins->oprs[j].scale == 0)
2737 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002738
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 if (!i && !b) {
2740 int ds = ins->oprs[j].disp_size;
2741 if ((addrbits != 64 && ds > 8) ||
2742 (addrbits == 64 && ds == 16))
2743 valid &= ds;
2744 } else {
2745 if (!(REG16 & ~b))
2746 valid &= 16;
2747 if (!(REG32 & ~b))
2748 valid &= 32;
2749 if (!(REG64 & ~b))
2750 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002751
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 if (!(REG16 & ~i))
2753 valid &= 16;
2754 if (!(REG32 & ~i))
2755 valid &= 32;
2756 if (!(REG64 & ~i))
2757 valid &= 64;
2758 }
2759 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002760 }
2761
2762 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002763 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002764 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002765 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002766 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002767 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002768 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002769 /* Impossible... */
2770 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2771 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002772 }
2773
2774 defdisp = ins->addr_size == 16 ? 16 : 32;
2775
2776 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002777 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2778 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2779 /*
2780 * mem_offs sizes must match the address size; if not,
2781 * strip the MEM_OFFS bit and match only EA instructions
2782 */
2783 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2784 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002785 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002786}