blob: 4f0cd9c0f746e9a169ba4632593f284158f885f6 [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";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700268 case 64:
269 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700270 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000272 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700273}
274
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400275static void warn_overflow(int pass, int size)
276{
277 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
278 "%s data exceeds bounds", size_name(size));
279}
280
281static void warn_overflow_const(int64_t data, int size)
282{
283 if (overflow_general(data, size))
284 warn_overflow(ERR_PASS1, size);
285}
286
287static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700288{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100289 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400290 if (overflow_general(o->offset, size))
291 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700292 }
293}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400294
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000295/*
296 * This routine wrappers the real output format's output routine,
297 * in order to pass a copy of the data off to the listing file
298 * generator at the same time.
299 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800300static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800301 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400302 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000303{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000304 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000305 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800306 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000307
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800308 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400309 /*
310 * This is a non-relocated address, and we're going to
311 * convert it into RAWDATA format.
312 */
313 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800314
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400315 if (size > 8) {
316 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
317 return;
318 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400320 WRITEADDR(q, *(int64_t *)data, size);
321 data = p;
322 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000323 }
324
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800325 list->output(offset, data, type, size);
326
Frank Kotlerabebb082003-09-06 04:45:37 +0000327 /*
328 * this call to src_get determines when we call the
329 * debug-format-specific "linenum" function
330 * it updates lineno and lnfname to the current values
331 * returning 0 if "same as last time", -2 if lnfname
332 * changed, and the amount by which lineno changed,
333 * if it did. thus, these variables must be static
334 */
335
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400336 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000337 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800339 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340}
341
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400342static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
343{
344 if (opx->segment != NO_SEG) {
345 uint64_t data = opx->offset;
346 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
347 } else {
348 uint8_t byte = opx->offset;
349 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
350 }
351}
352
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700353static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800354 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800356 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800357 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000358 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000359
H. Peter Anvin755f5212012-02-25 11:41:34 -0800360 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700361 return false;
362 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400363 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700364 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400365 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700366
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800367 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100368
Victor van den Elzen154e5922009-02-25 17:32:00 +0100369 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100370 /* Be optimistic in pass 1 */
371 return true;
372
H. Peter Anvine2c80182005-01-15 22:15:51 +0000373 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700374 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000375
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700376 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
377 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000379
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800380int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400381 insn * instruction, struct ofmt *output, efunc error,
382 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000384 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700386 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800387 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000388 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800389 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300390 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000393 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 outfmt = output; /* likewise */
395 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000396
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300397 wsize = idata_bytes(instruction->opcode);
398 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000400
H. Peter Anvineba20a72002-04-30 20:53:55 +0000401 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000403 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 if (t < 0)
405 errfunc(ERR_PANIC,
406 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400409 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400411 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700412 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400413 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000414 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700415 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800417 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400418 offset += wsize;
419 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700420 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400421 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800425 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 if (align) {
429 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100430 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800431 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 }
433 offset += e->stringlen + align;
434 }
435 }
436 if (t > 0 && t == instruction->times - 1) {
437 /*
438 * Dummy call to list->output to give the offset to the
439 * listing module.
440 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800441 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 list->uplevel(LIST_TIMES);
443 }
444 }
445 if (instruction->times > 1)
446 list->downlevel(LIST_TIMES);
447 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000448 }
449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700451 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400454 fp = fopen(fname, "rb");
455 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
457 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400458 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
460 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200461 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400462 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700463 static char buf[4096];
464 size_t t = instruction->times;
465 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400466 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 len = ftell(fp);
469 if (instruction->eops->next) {
470 base = instruction->eops->next->offset;
471 len -= base;
472 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700473 len > (size_t)instruction->eops->next->next->offset)
474 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 }
476 /*
477 * Dummy call to list->output to give the offset to the
478 * listing module.
479 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800480 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481 list->uplevel(LIST_INCBIN);
482 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700483 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 fseek(fp, base, SEEK_SET);
486 l = len;
487 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700488 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400489 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 if (!m) {
491 /*
492 * This shouldn't happen unless the file
493 * actually changes while we are reading
494 * it.
495 */
496 error(ERR_NONFATAL,
497 "`incbin': unexpected EOF while"
498 " reading file `%s'", fname);
499 t = 0; /* Try to exit cleanly */
500 break;
501 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800502 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 NO_SEG, NO_SEG);
504 l -= m;
505 }
506 }
507 list->downlevel(LIST_INCBIN);
508 if (instruction->times > 1) {
509 /*
510 * Dummy call to list->output to give the offset to the
511 * listing module.
512 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800513 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 list->uplevel(LIST_TIMES);
515 list->downlevel(LIST_TIMES);
516 }
517 fclose(fp);
518 return instruction->times * len;
519 }
520 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 }
522
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700523 /* Check to see if we need an address-size prefix */
524 add_asp(instruction, bits);
525
H. Peter Anvin23595f52009-07-25 17:44:25 -0700526 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700527
H. Peter Anvin23595f52009-07-25 17:44:25 -0700528 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400529 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800530 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400531 itimes = instruction->times;
532 if (insn_size < 0) /* shouldn't be, on pass two */
533 error(ERR_PANIC, "errors made it through from pass one");
534 else
535 while (itimes--) {
536 for (j = 0; j < MAXPREFIX; j++) {
537 uint8_t c = 0;
538 switch (instruction->prefixes[j]) {
539 case P_WAIT:
540 c = 0x9B;
541 break;
542 case P_LOCK:
543 c = 0xF0;
544 break;
545 case P_REPNE:
546 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800547 case P_XACQUIRE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400548 c = 0xF2;
549 break;
550 case P_REPE:
551 case P_REPZ:
552 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800553 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400554 c = 0xF3;
555 break;
556 case R_CS:
557 if (bits == 64) {
558 error(ERR_WARNING | ERR_PASS2,
559 "cs segment base generated, but will be ignored in 64-bit mode");
560 }
561 c = 0x2E;
562 break;
563 case R_DS:
564 if (bits == 64) {
565 error(ERR_WARNING | ERR_PASS2,
566 "ds segment base generated, but will be ignored in 64-bit mode");
567 }
568 c = 0x3E;
569 break;
570 case R_ES:
571 if (bits == 64) {
572 error(ERR_WARNING | ERR_PASS2,
573 "es segment base generated, but will be ignored in 64-bit mode");
574 }
575 c = 0x26;
576 break;
577 case R_FS:
578 c = 0x64;
579 break;
580 case R_GS:
581 c = 0x65;
582 break;
583 case R_SS:
584 if (bits == 64) {
585 error(ERR_WARNING | ERR_PASS2,
586 "ss segment base generated, but will be ignored in 64-bit mode");
587 }
588 c = 0x36;
589 break;
590 case R_SEGR6:
591 case R_SEGR7:
592 error(ERR_NONFATAL,
593 "segr6 and segr7 cannot be used as prefixes");
594 break;
595 case P_A16:
596 if (bits == 64) {
597 error(ERR_NONFATAL,
598 "16-bit addressing is not supported "
599 "in 64-bit mode");
600 } else if (bits != 16)
601 c = 0x67;
602 break;
603 case P_A32:
604 if (bits != 32)
605 c = 0x67;
606 break;
607 case P_A64:
608 if (bits != 64) {
609 error(ERR_NONFATAL,
610 "64-bit addressing is only supported "
611 "in 64-bit mode");
612 }
613 break;
614 case P_ASP:
615 c = 0x67;
616 break;
617 case P_O16:
618 if (bits != 16)
619 c = 0x66;
620 break;
621 case P_O32:
622 if (bits == 16)
623 c = 0x66;
624 break;
625 case P_O64:
626 /* REX.W */
627 break;
628 case P_OSP:
629 c = 0x66;
630 break;
631 case P_none:
632 break;
633 default:
634 error(ERR_PANIC, "invalid instruction prefix");
635 }
636 if (c != 0) {
637 out(offset, segment, &c, OUT_RAWDATA, 1,
638 NO_SEG, NO_SEG);
639 offset++;
640 }
641 }
642 insn_end = offset + insn_size;
643 gencode(segment, offset, bits, instruction,
644 temp, insn_end);
645 offset += insn_size;
646 if (itimes > 0 && itimes == instruction->times - 1) {
647 /*
648 * Dummy call to list->output to give the offset to the
649 * listing module.
650 */
651 list->output(offset, NULL, OUT_RAWDATA, 0);
652 list->uplevel(LIST_TIMES);
653 }
654 }
655 if (instruction->times > 1)
656 list->downlevel(LIST_TIMES);
657 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700658 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400659 /* No match */
660 switch (m) {
661 case MERR_OPSIZEMISSING:
662 error(ERR_NONFATAL, "operation size not specified");
663 break;
664 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400666 break;
667 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400669 break;
670 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800671 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400672 bits);
673 break;
674 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 error(ERR_NONFATAL,
676 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400677 break;
678 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000679 }
680 return 0;
681}
682
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800683int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400684 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000686 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700687 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000688
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000690 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000691
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400692 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000694
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700695 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
696 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 instruction->opcode == I_DT || instruction->opcode == I_DO ||
698 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300700 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300703 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000704
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400705 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000706 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000707
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400709 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400711 warn_overflow_const(e->offset, wsize);
712 } else if (e->type == EOT_DB_STRING ||
713 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000715
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 align = (-osize) % wsize;
717 if (align < 0)
718 align += wsize;
719 isize += osize + align;
720 }
721 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000722 }
723
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400725 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300727 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700728 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400730 fp = fopen(fname, "rb");
731 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
733 fname);
734 else if (fseek(fp, 0L, SEEK_END) < 0)
735 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
736 fname);
737 else {
738 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 if (instruction->eops->next) {
740 len -= instruction->eops->next->offset;
741 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700742 len > (size_t)instruction->eops->next->next->offset) {
743 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 }
745 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300746 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300748 if (fp)
749 fclose(fp);
750 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000751 }
752
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700753 /* Check to see if we need an address-size prefix */
754 add_asp(instruction, bits);
755
H. Peter Anvin23595f52009-07-25 17:44:25 -0700756 m = find_match(&temp, instruction, segment, offset, bits);
757 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400758 /* we've matched an instruction. */
759 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400760 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100761
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800762 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400763 if (isize < 0)
764 return -1;
765 for (j = 0; j < MAXPREFIX; j++) {
766 switch (instruction->prefixes[j]) {
767 case P_A16:
768 if (bits != 16)
769 isize++;
770 break;
771 case P_A32:
772 if (bits != 32)
773 isize++;
774 break;
775 case P_O16:
776 if (bits != 16)
777 isize++;
778 break;
779 case P_O32:
780 if (bits == 16)
781 isize++;
782 break;
783 case P_A64:
784 case P_O64:
785 case P_none:
786 break;
787 default:
788 isize++;
789 break;
790 }
791 }
792 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700793 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400794 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796}
797
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800798static void bad_hle_warn(const insn * ins, uint8_t hleok)
799{
800 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800801 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800802 static const enum whatwarn warn[2][4] =
803 {
804 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
805 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
806 };
807 unsigned int n;
808
809 n = (unsigned int)rep_pfx - P_XACQUIRE;
810 if (n > 1)
811 return; /* Not XACQUIRE/XRELEASE */
812
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800813 ww = warn[n][hleok];
814 if (!is_class(MEMORY, ins->oprs[0].type))
815 ww = w_inval; /* HLE requires operand 0 to be memory */
816
817 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800818 case w_none:
819 break;
820
821 case w_lock:
822 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800823 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800824 "%s with this instruction requires lock",
825 prefix_name(rep_pfx));
826 }
827 break;
828
829 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800830 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800831 "%s invalid with this instruction",
832 prefix_name(rep_pfx));
833 break;
834 }
835}
836
H. Peter Anvin507ae032008-10-09 15:37:10 -0700837/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400838#define case3(x) case (x): case (x)+1: case (x)+2
839#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700840
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800841static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800842 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000843{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800844 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800845 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000846 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000847 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700848 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700849 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700850 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700851 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800852 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800853 bool lockcheck = true;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000854
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700855 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700856 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700857 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700858
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700859 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400860 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700861
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 (void)segment; /* Don't warn that this parameter is unused */
863 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000864
H. Peter Anvin839eca22007-10-29 23:12:47 -0700865 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400866 c = *codes++;
867 op1 = (c & 3) + ((opex & 1) << 2);
868 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
869 opx = &ins->oprs[op1];
870 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700871
H. Peter Anvin839eca22007-10-29 23:12:47 -0700872 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400873 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 codes += c, length += c;
875 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700876
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400877 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400878 opex = c;
879 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700880
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400881 case4(010):
882 ins->rex |=
883 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 codes++, length++;
885 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700886
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400887 case4(020):
888 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 length++;
890 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400892 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 length += 2;
894 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400896 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700897 if (opx->type & (BITS16 | BITS32 | BITS64))
898 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 else
900 length += (bits == 16) ? 2 : 4;
901 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700902
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400903 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 length += 4;
905 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700908 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400911 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 length++;
913 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400915 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000916 length += 8; /* MOV reg64/imm */
917 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700918
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400919 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 length += 2;
921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400923 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700924 if (opx->type & (BITS16 | BITS32 | BITS64))
925 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 else
927 length += (bits == 16) ? 2 : 4;
928 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400930 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 length += 4;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700935 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 case 0172:
939 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400940 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700941 length++;
942 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700943
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700944 case4(0174):
945 length++;
946 break;
947
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700948 case4(0240):
949 ins->rex |= REX_EV;
950 ins->vexreg = regval(opx);
951 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
952 ins->vex_cm = *codes++;
953 ins->vex_wlp = *codes++;
954 ins->evex_tuple = (*codes++ - 0300);
955 break;
956
957 case 0250:
958 ins->rex |= REX_EV;
959 ins->vexreg = 0;
960 ins->vex_cm = *codes++;
961 ins->vex_wlp = *codes++;
962 ins->evex_tuple = (*codes++ - 0300);
963 break;
964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(0254):
966 length += 4;
967 break;
968
969 case4(0260):
970 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700971 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 ins->vex_cm = *codes++;
973 ins->vex_wlp = *codes++;
974 break;
975
976 case 0270:
977 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700978 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 ins->vex_cm = *codes++;
980 ins->vex_wlp = *codes++;
981 break;
982
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400983 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800984 hleok = c & 3;
985 break;
986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case4(0274):
988 length++;
989 break;
990
991 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 if (bits == 64)
996 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700997 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001001 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Keith Kaniosb7a89542007-04-12 02:40:54 +00001007 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1009 has_prefix(ins, PPS_ASIZE, P_A32))
1010 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 case4(0314):
1014 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001017 {
1018 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1019 if (pfx == P_O16)
1020 break;
1021 if (pfx != P_none)
1022 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1023 else
1024 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001026 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001029 {
1030 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1031 if (pfx == P_O32)
1032 break;
1033 if (pfx != P_none)
1034 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1035 else
1036 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001038 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 case 0322:
1041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
Keith Kaniosb7a89542007-04-12 02:40:54 +00001043 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001044 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001045 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001046
Keith Kaniosb7a89542007-04-12 02:40:54 +00001047 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001048 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001049 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001050
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 case 0325:
1052 ins->rex |= REX_NH;
1053 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001054
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001055 case 0326:
1056 break;
1057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 case 0330:
1059 codes++, length++;
1060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001065 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 0333:
1067 length++;
1068 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001069
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001070 case 0334:
1071 ins->rex |= REX_L;
1072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001074 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001075 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001076
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001077 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001078 if (!ins->prefixes[PPS_REP])
1079 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001083 if (!ins->prefixes[PPS_REP])
1084 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 if (ins->oprs[0].segment != NO_SEG)
1089 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1090 " quantity of BSS space");
1091 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001092 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001094
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 case 0341:
1096 if (!ins->prefixes[PPS_WAIT])
1097 ins->prefixes[PPS_WAIT] = P_WAIT;
1098 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001099
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 case 0360:
1101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001103 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001104 length++;
1105 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001106
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001107 case 0364:
1108 case 0365:
1109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
Keith Kanios48af1772007-08-17 07:37:52 +00001111 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001112 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 length++;
1114 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001115
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001116 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 case 0373:
1120 length++;
1121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001123 case 0374:
1124 eat = EA_XMMVSIB;
1125 break;
1126
1127 case 0375:
1128 eat = EA_YMMVSIB;
1129 break;
1130
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001131 case 0376:
1132 eat = EA_ZMMVSIB;
1133 break;
1134
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 case4(0100):
1136 case4(0110):
1137 case4(0120):
1138 case4(0130):
1139 case4(0200):
1140 case4(0204):
1141 case4(0210):
1142 case4(0214):
1143 case4(0220):
1144 case4(0224):
1145 case4(0230):
1146 case4(0234):
1147 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001149 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 opflags_t rflags;
1151 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001152 struct operand *oplast;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001153
Keith Kaniosb7a89542007-04-12 02:40:54 +00001154 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 if (c <= 0177) {
1157 /* pick rfield from operand b (opx) */
1158 rflags = regflag(opx);
1159 rfield = nasm_regvals[opx->basereg];
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001160 /* find the last SIMD operand where ER decorator resides */
1161 oplast = &ins->oprs[op1 > op2 ? op1 : op2];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 } else {
1163 rflags = 0;
1164 rfield = c & 7;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001165 oplast = opy;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001167
1168 if (oplast->decoflags & ER) {
1169 /* set EVEX.RC (rounding control) and b */
1170 ins->evex_p[2] |= (((ins->evex_rm - BRC_RN) << 5) & EVEX_P2LL) |
1171 EVEX_P2B;
1172 } else {
1173 /* set EVEX.L'L (vector length) */
1174 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
1175 if ((oplast->decoflags & SAE) ||
1176 (opy->decoflags & BRDCAST_MASK)) {
1177 /* set EVEX.b */
1178 ins->evex_p[2] |= EVEX_P2B;
1179 }
1180 }
1181
1182 if (process_ea(opy, &ea_data, bits,
1183 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 errfunc(ERR_NONFATAL, "invalid effective address");
1185 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001186 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001187 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001189 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001190 }
1191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 default:
1194 errfunc(ERR_PANIC, "internal instruction table corrupt"
1195 ": instruction code \\%o (0x%02X) given", c, c);
1196 break;
1197 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001198 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001199
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001200 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001201
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001202 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 if (ins->rex & REX_H) {
1204 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1205 return -1;
1206 }
1207 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001208 }
1209
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001210 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001211 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001212
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001213 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001215 return -1;
1216 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001217 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001219 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001220 ins->rex &= ~REX_W;
1221 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001222 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001223 ins->rex |= REX_W;
1224 bad32 &= ~REX_W;
1225 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001226 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001227 /* Follow REX_W */
1228 break;
1229 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001230
H. Peter Anvinfc561202011-07-07 16:58:22 -07001231 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1233 return -1;
1234 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001235 if (ins->rex & REX_EV)
1236 length += 4;
1237 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 length += 3;
1239 else
1240 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001241 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001242 if (ins->rex & REX_H) {
1243 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1244 return -1;
1245 } else if (bits == 64) {
1246 length++;
1247 } else if ((ins->rex & REX_L) &&
1248 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1249 cpu >= IF_X86_64) {
1250 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001251 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001252 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 length++;
1254 } else {
1255 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1256 return -1;
1257 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001258 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001259
1260 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1261 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001262 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001263 "instruction is not lockable");
1264 }
1265
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001266 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001267
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001268 return length;
1269}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001270
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001271static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1272{
1273 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001274 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001275 ins->rex = (ins->rex & REX_REAL) | REX_P;
1276 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1277 ins->rex = 0;
1278 return 1;
1279 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001280 }
1281
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001282 return 0;
1283}
1284
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001285static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001286 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001289 uint8_t c;
1290 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001291 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001292 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001293 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001294 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001295 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001296 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001297 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001298
H. Peter Anvin839eca22007-10-29 23:12:47 -07001299 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 c = *codes++;
1301 op1 = (c & 3) + ((opex & 1) << 2);
1302 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1303 opx = &ins->oprs[op1];
1304 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001305
H. Peter Anvin839eca22007-10-29 23:12:47 -07001306 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 case 01:
1308 case 02:
1309 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001311 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001312 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 codes += c;
1314 offset += c;
1315 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001316
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 case 05:
1318 case 06:
1319 case 07:
1320 opex = c;
1321 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001322
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001324 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001325 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001326 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 offset += 1;
1328 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001329
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001331 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001332 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001335 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 offset += 1;
1337 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001340 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001341 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001343 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 offset += 1;
1345 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001346
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001348 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001349 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001350 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001351 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 offset += 2;
1353 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001354
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001356 if (opx->type & (BITS16 | BITS32))
1357 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 else
1359 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001360 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001361 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001362 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001363 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 offset += size;
1365 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001366
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001368 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001369 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001370 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001371 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 offset += 4;
1373 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001376 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001377 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001378 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001379 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001380 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 offset += size;
1382 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001385 if (opx->segment != segment) {
1386 data = opx->offset;
1387 out(offset, segment, &data,
1388 OUT_REL1ADR, insn_end - offset,
1389 opx->segment, opx->wrt);
1390 } else {
1391 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 if (data > 127 || data < -128)
1393 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001394 out(offset, segment, &data,
1395 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1396 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 offset += 1;
1398 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001399
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001400 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001401 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001402 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001403 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001404 offset += 8;
1405 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001407 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001408 if (opx->segment != segment) {
1409 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001411 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001412 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001414 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001416 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 }
1418 offset += 2;
1419 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001421 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001422 if (opx->type & (BITS16 | BITS32 | BITS64))
1423 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 else
1425 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001426 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001427 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001428 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1430 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001432 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001434 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 }
1436 offset += size;
1437 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001438
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001439 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001440 if (opx->segment != segment) {
1441 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001443 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001444 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001446 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001448 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 }
1450 offset += 4;
1451 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001452
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001453 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001454 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001455 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1456 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001457 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001458 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001459 outfmt->segbase(1 + opx->segment),
1460 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001461 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001464 case 0172:
1465 c = *codes++;
1466 opx = &ins->oprs[c >> 3];
1467 bytes[0] = nasm_regvals[opx->basereg] << 4;
1468 opx = &ins->oprs[c & 7];
1469 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1470 errfunc(ERR_NONFATAL,
1471 "non-absolute expression not permitted as argument %d",
1472 c & 7);
1473 } else {
1474 if (opx->offset & ~15) {
1475 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1476 "four-bit argument exceeds bounds");
1477 }
1478 bytes[0] |= opx->offset & 15;
1479 }
1480 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1481 offset++;
1482 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001483
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001484 case 0173:
1485 c = *codes++;
1486 opx = &ins->oprs[c >> 4];
1487 bytes[0] = nasm_regvals[opx->basereg] << 4;
1488 bytes[0] |= c & 15;
1489 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1490 offset++;
1491 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001492
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001493 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 bytes[0] = nasm_regvals[opx->basereg] << 4;
1495 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1496 offset++;
1497 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001500 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001501 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1502 (int32_t)data != (int64_t)data) {
1503 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1504 "signed dword immediate exceeds bounds");
1505 }
1506 out(offset, segment, &data, OUT_ADDRESS, 4,
1507 opx->segment, opx->wrt);
1508 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001509 break;
1510
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001511 case4(0240):
1512 case 0250:
1513 codes += 3;
1514 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1515 EVEX_P2Z | EVEX_P2AAA, 2);
1516 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1517 bytes[0] = 0x62;
1518 /* EVEX.X can be set by either REX or EVEX for different reasons */
1519 bytes[1] = (~(((ins->rex & 7) << 5) |
1520 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) & 0xf0) |
1521 (ins->vex_cm & 3);
1522 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1523 ((~ins->vexreg & 15) << 3) |
1524 (1 << 2) | (ins->vex_wlp & 3);
1525 bytes[3] = ins->evex_p[2];
1526 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1527 offset += 4;
1528 break;
1529
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001530 case4(0260):
1531 case 0270:
1532 codes += 2;
1533 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1534 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1535 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1536 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001537 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1539 offset += 3;
1540 } else {
1541 bytes[0] = 0xc5;
1542 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001543 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1545 offset += 2;
1546 }
1547 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001548
H. Peter Anvine014f352012-02-25 22:35:19 -08001549 case 0271:
1550 case 0272:
1551 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001552 break;
1553
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001554 case4(0274):
1555 {
1556 uint64_t uv, um;
1557 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001558
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 if (ins->rex & REX_W)
1560 s = 64;
1561 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1562 s = 16;
1563 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1564 s = 32;
1565 else
1566 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 um = (uint64_t)2 << (s-1);
1569 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001570
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001571 if (uv > 127 && uv < (uint64_t)-128 &&
1572 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001573 /* If this wasn't explicitly byte-sized, warn as though we
1574 * had fallen through to the imm16/32/64 case.
1575 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001576 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001577 "%s value exceeds bounds",
1578 (opx->type & BITS8) ? "signed byte" :
1579 s == 16 ? "word" :
1580 s == 32 ? "dword" :
1581 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001583 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001584 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001585 out(offset, segment, &data, OUT_ADDRESS, 1,
1586 opx->segment, opx->wrt);
1587 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001588 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001589 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1590 NO_SEG);
1591 }
1592 offset += 1;
1593 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001600 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001602 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 offset += 1;
1604 } else
1605 offset += 0;
1606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001609 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001611 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 offset += 1;
1613 } else
1614 offset += 0;
1615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 case 0312:
1618 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619
Keith Kaniosb7a89542007-04-12 02:40:54 +00001620 case 0313:
1621 ins->rex = 0;
1622 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case4(0314):
1625 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001626
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001630
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001632 case 0323:
1633 break;
1634
Keith Kaniosb7a89542007-04-12 02:40:54 +00001635 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001636 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 case 0325:
1640 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001641
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001642 case 0326:
1643 break;
1644
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001646 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001647 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 offset += 1;
1649 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001650
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001656 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001657 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 offset += 1;
1659 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660
Keith Kanios48af1772007-08-17 07:37:52 +00001661 case 0334:
1662 if (ins->rex & REX_R) {
1663 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001664 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001665 offset += 1;
1666 }
1667 ins->rex &= ~(REX_L|REX_R);
1668 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001669
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001670 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001672
H. Peter Anvin962e3052008-08-28 17:47:16 -07001673 case 0336:
1674 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001675 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 if (ins->oprs[0].segment != NO_SEG)
1679 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1680 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001681 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 if (size > 0)
1683 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001684 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 offset += size;
1686 }
1687 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001688
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 case 0341:
1690 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001691
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 case 0360:
1693 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001694
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001695 case 0361:
1696 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001697 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1698 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 case 0364:
1702 case 0365:
1703 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001704
Keith Kanios48af1772007-08-17 07:37:52 +00001705 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001707 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001708 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001709 offset += 1;
1710 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001711
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 case 0370:
1713 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001715
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 case 0373:
1717 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001718 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 offset += 1;
1720 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001722 case 0374:
1723 eat = EA_XMMVSIB;
1724 break;
1725
1726 case 0375:
1727 eat = EA_YMMVSIB;
1728 break;
1729
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001730 case 0376:
1731 eat = EA_ZMMVSIB;
1732 break;
1733
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 case4(0100):
1735 case4(0110):
1736 case4(0120):
1737 case4(0130):
1738 case4(0200):
1739 case4(0204):
1740 case4(0210):
1741 case4(0214):
1742 case4(0220):
1743 case4(0224):
1744 case4(0230):
1745 case4(0234):
1746 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 ea ea_data;
1748 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001750 uint8_t *p;
1751 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001752 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001753
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001754 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 /* pick rfield from operand b (opx) */
1756 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001757 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 } else {
1759 /* rfield is constant */
1760 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001764 if (process_ea(opy, &ea_data, bits,
1765 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 p = bytes;
1769 *p++ = ea_data.modrm;
1770 if (ea_data.sib_present)
1771 *p++ = ea_data.sib;
1772
1773 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001774 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001776 /*
1777 * Make sure the address gets the right offset in case
1778 * the line breaks in the .lst file (BR 1197827)
1779 */
1780 offset += s;
1781 s = 0;
1782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 switch (ea_data.bytes) {
1784 case 0:
1785 break;
1786 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case 2:
1788 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001789 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001790 /* use compressed displacement, if available */
1791 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 if (ea_data.rip) {
1794 if (opy->segment == segment) {
1795 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001796 if (overflow_signed(data, ea_data.bytes))
1797 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 out(offset, segment, &data, OUT_ADDRESS,
1799 ea_data.bytes, NO_SEG, NO_SEG);
1800 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001801 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 out(offset, segment, &data, OUT_REL4ADR,
1803 insn_end - offset, opy->segment, opy->wrt);
1804 }
1805 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001806 if (overflow_general(data, ins->addr_size >> 3) ||
1807 signed_bits(data, ins->addr_size) !=
1808 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001809 warn_overflow(ERR_PASS2, ea_data.bytes);
1810
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 out(offset, segment, &data, OUT_ADDRESS,
1812 ea_data.bytes, opy->segment, opy->wrt);
1813 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001815 default:
1816 /* Impossible! */
1817 errfunc(ERR_PANIC,
1818 "Invalid amount of bytes (%d) for offset?!",
1819 ea_data.bytes);
1820 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 }
1822 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 }
1824 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001825
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001826 default:
1827 errfunc(ERR_PANIC, "internal instruction table corrupt"
1828 ": instruction code \\%o (0x%02X) given", c, c);
1829 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001831 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001832}
1833
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001834static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001835{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001836 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001837 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001838 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001839}
1840
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001841static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001843 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001845 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001846}
1847
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001848static int op_rexflags(const operand * o, int mask)
1849{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001850 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001851 int val;
1852
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001853 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001854 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001855
H. Peter Anvina4835d42008-05-20 14:21:29 -07001856 flags = nasm_reg_flags[o->basereg];
1857 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001858
1859 return rexflags(val, flags, mask);
1860}
1861
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001862static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001863{
1864 int rex = 0;
1865
1866 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001868 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 rex |= REX_W;
1870 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1871 rex |= REX_H;
1872 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1873 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001874
1875 return rex & mask;
1876}
1877
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001878static int evexflags(int val, decoflags_t deco,
1879 int mask, uint8_t byte)
1880{
1881 int evex = 0;
1882
1883 switch(byte) {
1884 case 0:
1885 if (val >= 16)
1886 evex |= (EVEX_P0RP | EVEX_P0X);
1887 break;
1888 case 2:
1889 if (val >= 16)
1890 evex |= EVEX_P2VP;
1891 if (deco & Z)
1892 evex |= EVEX_P2Z;
1893 if (deco & OPMASK_MASK)
1894 evex |= deco & EVEX_P2AAA;
1895 break;
1896 }
1897 return evex & mask;
1898}
1899
1900static int op_evexflags(const operand * o, int mask, uint8_t byte)
1901{
1902 int val;
1903
1904 if (!is_register(o->basereg))
1905 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1906
1907 val = nasm_regvals[o->basereg];
1908
1909 return evexflags(val, o->decoflags, mask, byte);
1910}
1911
H. Peter Anvin23595f52009-07-25 17:44:25 -07001912static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 insn *instruction,
1914 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001915{
1916 const struct itemplate *temp;
1917 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001918 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001919 bool opsizemissing = false;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001920 int8_t broadcast = -1;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001921 int i;
1922
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001923 /* find the position of broadcasting operand */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001924 for (i = 0; i < instruction->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001925 if (instruction->oprs[i].decoflags & BRDCAST_MASK) {
1926 broadcast = i;
1927 break;
1928 }
1929
1930 /* broadcasting uses a different data element size */
1931 for (i = 0; i < instruction->operands; i++)
1932 if (i == broadcast)
1933 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1934 else
1935 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001936
1937 merr = MERR_INVALOP;
1938
1939 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 temp->opcode != I_none; temp++) {
1941 m = matches(temp, instruction, bits);
1942 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001943 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001944 m = MOK_GOOD;
1945 else
1946 m = MERR_INVALOP;
1947 } else if (m == MERR_OPSIZEMISSING &&
1948 (temp->flags & IF_SMASK) != IF_SX) {
1949 /*
1950 * Missing operand size and a candidate for fuzzy matching...
1951 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001952 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001953 if (i == broadcast)
1954 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
1955 else
1956 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 opsizemissing = true;
1958 }
1959 if (m > merr)
1960 merr = m;
1961 if (merr == MOK_GOOD)
1962 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001963 }
1964
1965 /* No match, but see if we can get a fuzzy operand size match... */
1966 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001968
1969 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 /*
1971 * We ignore extrinsic operand sizes on registers, so we should
1972 * never try to fuzzy-match on them. This also resolves the case
1973 * when we have e.g. "xmmrm128" in two different positions.
1974 */
1975 if (is_class(REGISTER, instruction->oprs[i].type))
1976 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07001977
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001978 /* This tests if xsizeflags[i] has more than one bit set */
1979 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1980 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001981
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001982 if (i == broadcast)
1983 instruction->oprs[i].decoflags |= xsizeflags[i];
1984 else
1985 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001986 }
1987
1988 /* Try matching again... */
1989 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001990 temp->opcode != I_none; temp++) {
1991 m = matches(temp, instruction, bits);
1992 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001993 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001994 m = MOK_GOOD;
1995 else
1996 m = MERR_INVALOP;
1997 }
1998 if (m > merr)
1999 merr = m;
2000 if (merr == MOK_GOOD)
2001 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002002 }
2003
H. Peter Anvina81655b2009-07-25 18:15:28 -07002004done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002005 *tempp = temp;
2006 return merr;
2007}
2008
H. Peter Anvin65289e82009-07-25 17:25:11 -07002009static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002010 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002011{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002012 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002013 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002014 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002015
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002016 /*
2017 * Check the opcode
2018 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002020 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002021
2022 /*
2023 * Count the operands
2024 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002026 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002027
2028 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002029 * Is it legal?
2030 */
2031 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2032 return MERR_INVALOP;
2033
2034 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002035 * Check that no spurious colons or TOs are present
2036 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 for (i = 0; i < itemp->operands; i++)
2038 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002039 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002040
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002041 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002042 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002043 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002044 switch (itemp->flags & IF_SMASK) {
2045 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002046 asize = BITS8;
2047 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002048 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002049 asize = BITS16;
2050 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002051 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002052 asize = BITS32;
2053 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002054 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002055 asize = BITS64;
2056 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002057 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002058 asize = BITS128;
2059 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002060 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002061 asize = BITS256;
2062 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002063 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002064 asize = BITS512;
2065 break;
2066 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002067 switch (bits) {
2068 case 16:
2069 asize = BITS16;
2070 break;
2071 case 32:
2072 asize = BITS32;
2073 break;
2074 case 64:
2075 asize = BITS64;
2076 break;
2077 default:
2078 asize = 0;
2079 break;
2080 }
2081 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002082 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002083 asize = 0;
2084 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002085 }
2086
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002087 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002088 /* S- flags only apply to a specific operand */
2089 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2090 memset(size, 0, sizeof size);
2091 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002092 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002093 /* S- flags apply to all operands */
2094 for (i = 0; i < MAX_OPERANDS; i++)
2095 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002096 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002097
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002098 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002099 * Check that the operand flags all match up,
2100 * it's a bit tricky so lets be verbose:
2101 *
2102 * 1) Find out the size of operand. If instruction
2103 * doesn't have one specified -- we're trying to
2104 * guess it either from template (IF_S* flag) or
2105 * from code bits.
2106 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002107 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002108 * template has an operand size specified AND this size differ
2109 * from which instruction has (perhaps we got it from code bits)
2110 * we are:
2111 * a) Check that only size of instruction and operand is differ
2112 * other characteristics do match
2113 * b) Perhaps it's a register specified in instruction so
2114 * for such a case we just mark that operand as "size
2115 * missing" and this will turn on fuzzy operand size
2116 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002117 */
2118 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002119 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002120 decoflags_t deco = instruction->oprs[i].decoflags;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002121 if (!(type & SIZE_MASK))
2122 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002123
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002124 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2125 (itemp->deco[i] & deco) != deco) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002126 return MERR_INVALOP;
2127 } else if ((itemp->opd[i] & SIZE_MASK) &&
2128 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2129 if (type & SIZE_MASK) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002130 /*
2131 * when broadcasting, the element size depends on
2132 * the instruction type. decorator flag should match.
2133 */
2134#define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2135 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2136 if (!((deco & BRDCAST_MASK) &&
2137 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2138 return MERR_INVALOP;
2139 }
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002140 } else if (!is_class(REGISTER, type)) {
2141 /*
2142 * Note: we don't honor extrinsic operand sizes for registers,
2143 * so "missing operand size" for a register should be
2144 * considered a wildcard match rather than an error.
2145 */
2146 opsizemissing = true;
2147 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002148 }
2149 }
2150
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002151 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002152 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002153
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002154 /*
2155 * Check operand sizes
2156 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002157 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002160 asize = itemp->opd[i] & SIZE_MASK;
2161 if (asize) {
2162 for (i = 0; i < oprs; i++)
2163 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 break;
2165 }
2166 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002167 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002169 }
2170
Keith Kaniosb7a89542007-04-12 02:40:54 +00002171 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 if (!(itemp->opd[i] & SIZE_MASK) &&
2173 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002174 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002175 }
2176
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002177 /*
2178 * Check template is okay at the set cpu level
2179 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002180 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002181 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002182
Keith Kaniosb7a89542007-04-12 02:40:54 +00002183 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002184 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002185 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002186 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002187 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002188
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002189 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002190 * If we have a HLE prefix, look for the NOHLE flag
2191 */
2192 if ((itemp->flags & IF_NOHLE) &&
2193 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2194 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2195 return MERR_BADHLE;
2196
2197 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002198 * Check if special handling needed for Jumps
2199 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002200 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002201 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202
H. Peter Anvin60926242009-07-26 16:25:38 -07002203 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204}
2205
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002206/*
2207 * Check if offset is a multiple of N with corresponding tuple type
2208 * if Disp8*N is available, compressed displacement is stored in compdisp
2209 */
2210static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2211{
2212 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2213 {{16, 32, 64}, {8, 8, 8}}};
2214 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2215 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2216
2217 bool evex_b = input->decoflags & BRDCAST_MASK;
2218 enum ttypes tuple = ins->evex_tuple;
2219 /* vex_wlp composed as [wwllpp] */
2220 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2221 /* wig(=2) is treated as w0(=0) */
2222 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2223 int32_t off = input->offset;
2224 uint8_t n = 0;
2225 int32_t disp8;
2226
2227 switch(tuple) {
2228 case FV:
2229 n = fv_n[evex_w][evex_b][vectlen];
2230 break;
2231 case HV:
2232 n = hv_n[evex_b][vectlen];
2233 break;
2234
2235 case FVM:
2236 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2237 n = 1 << (vectlen + 4);
2238 break;
2239 case T1S8: /* N = 1 */
2240 case T1S16: /* N = 2 */
2241 n = tuple - T1S8 + 1;
2242 break;
2243 case T1S:
2244 /* N = 4 for 32bit, 8 for 64bit */
2245 n = evex_w ? 8 : 4;
2246 break;
2247 case T1F32:
2248 case T1F64:
2249 /* N = 4 for 32bit, 8 for 64bit */
2250 n = (tuple == T1F32 ? 4 : 8);
2251 break;
2252 case T2:
2253 case T4:
2254 case T8:
2255 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2256 n = 0;
2257 else
2258 n = 1 << (tuple - T2 + evex_w + 4);
2259 break;
2260 case HVM:
2261 case QVM:
2262 case OVM:
2263 n = 1 << (OVM - tuple + vectlen + 1);
2264 break;
2265 case M128:
2266 n = 16;
2267 break;
2268 case DUP:
2269 n = dup_n[vectlen];
2270 break;
2271
2272 default:
2273 break;
2274 }
2275
2276 if (n && !(off & (n - 1))) {
2277 disp8 = off / n;
2278 /* if it fits in Disp8 */
2279 if (disp8 >= -128 && disp8 <= 127) {
2280 *compdisp = disp8;
2281 return true;
2282 }
2283 }
2284
2285 *compdisp = 0;
2286 return false;
2287}
2288
2289/*
2290 * Check if ModR/M.mod should/can be 01.
2291 * - EAF_BYTEOFFS is set
2292 * - offset can fit in a byte when EVEX is not used
2293 * - offset can be compressed when EVEX is used
2294 */
2295#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2296 (o >= -128 && o <= 127 && \
2297 seg == NO_SEG && !forw_ref && \
2298 !(input->eaflags & EAF_WORDOFFS) && \
2299 !(ins->rex & REX_EV)) || \
2300 (ins->rex & REX_EV && \
2301 is_disp8n(input, ins, &output->disp8)))
2302
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002303static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002304 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002305{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002306 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002307 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002308
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002309 output->type = EA_SCALAR;
2310 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002311
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002312 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002313 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002314 /* EVEX.R' flag for the REG operand */
2315 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002316
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002317 if (is_class(REGISTER, input->type)) {
2318 /*
2319 * It's a direct register.
2320 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002321 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002322 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002323
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002324 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002325 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002326
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002327 /* broadcasting is not available with a direct register operand. */
2328 if (input->decoflags & BRDCAST_MASK) {
2329 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2330 goto err;
2331 }
2332
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002333 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002334 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002335 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002336 output->bytes = 0; /* no offset necessary either */
2337 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2338 } else {
2339 /*
2340 * It's a memory reference.
2341 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002342
2343 /* Embedded rounding or SAE is not available with a mem ref operand. */
2344 if (input->decoflags & (ER | SAE)) {
2345 nasm_error(ERR_NONFATAL,
2346 "Embedded rounding is available only with reg-reg op.");
2347 return -1;
2348 }
2349
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002350 if (input->basereg == -1 &&
2351 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002352 /*
2353 * It's a pure offset.
2354 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002355 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2356 input->segment == NO_SEG) {
2357 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2358 input->type &= ~IP_REL;
2359 input->type |= MEMORY;
2360 }
2361
2362 if (input->eaflags & EAF_BYTEOFFS ||
2363 (input->eaflags & EAF_WORDOFFS &&
2364 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2365 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2366 }
2367
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002368 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002369 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002370 output->sib = GEN_SIB(0, 4, 5);
2371 output->bytes = 4;
2372 output->modrm = GEN_MODRM(0, rfield, 4);
2373 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002374 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002375 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002376 output->bytes = (addrbits != 16 ? 4 : 2);
2377 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2378 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002379 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002380 } else {
2381 /*
2382 * It's an indirection.
2383 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002385 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002387 int t, it, bt; /* register numbers */
2388 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 if (s == 0)
2391 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002392
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002393 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002394 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002395 ix = nasm_reg_flags[i];
2396 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002397 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002398 ix = 0;
2399 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002401 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002402 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002403 bx = nasm_reg_flags[b];
2404 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002405 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002406 bx = 0;
2407 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002408
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002409 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002410 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002411 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002412 int32_t o = input->offset;
2413 int mod, scale, index, base;
2414
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002415 /*
2416 * For a vector SIB, one has to be a vector and the other,
2417 * if present, a GPR. The vector must be the index operand.
2418 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002419 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002420 if (s == 0)
2421 s = 1;
2422 else if (s != 1)
2423 goto err;
2424
2425 t = bt, bt = it, it = t;
2426 x = bx, bx = ix, ix = x;
2427 }
2428
2429 if (bt != -1) {
2430 if (REG_GPR & ~bx)
2431 goto err;
2432 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2433 sok &= bx;
2434 else
2435 goto err;
2436 }
2437
2438 /*
2439 * While we're here, ensure the user didn't specify
2440 * WORD or QWORD
2441 */
2442 if (input->disp_size == 16 || input->disp_size == 64)
2443 goto err;
2444
2445 if (addrbits == 16 ||
2446 (addrbits == 32 && !(sok & BITS32)) ||
2447 (addrbits == 64 && !(sok & BITS64)))
2448 goto err;
2449
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002450 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2451 : ((ix & YMMREG & ~REG_EA)
2452 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002453
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002454 output->rex |= rexflags(it, ix, REX_X);
2455 output->rex |= rexflags(bt, bx, REX_B);
2456 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002457
2458 index = it & 7; /* it is known to be != -1 */
2459
2460 switch (s) {
2461 case 1:
2462 scale = 0;
2463 break;
2464 case 2:
2465 scale = 1;
2466 break;
2467 case 4:
2468 scale = 2;
2469 break;
2470 case 8:
2471 scale = 3;
2472 break;
2473 default: /* then what the smeg is it? */
2474 goto err; /* panic */
2475 }
2476
2477 if (bt == -1) {
2478 base = 5;
2479 mod = 0;
2480 } else {
2481 base = (bt & 7);
2482 if (base != REG_NUM_EBP && o == 0 &&
2483 seg == NO_SEG && !forw_ref &&
2484 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2485 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002486 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002487 mod = 1;
2488 else
2489 mod = 2;
2490 }
2491
2492 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002493 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2494 output->modrm = GEN_MODRM(mod, rfield, 4);
2495 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002496 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002497 /*
2498 * it must be a 32/64-bit memory reference. Firstly we have
2499 * to check that all registers involved are type E/Rxx.
2500 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002501 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002502 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002503
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002504 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002505 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2506 sok &= ix;
2507 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002508 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002509 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002510
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 if (bt != -1) {
2512 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002513 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002514 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002515 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002516 sok &= bx;
2517 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002518
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 /*
2520 * While we're here, ensure the user didn't specify
2521 * WORD or QWORD
2522 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002523 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002524 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002525
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 if (addrbits == 16 ||
2527 (addrbits == 32 && !(sok & BITS32)) ||
2528 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002529 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002530
Keith Kaniosb7a89542007-04-12 02:40:54 +00002531 /* now reorganize base/index */
2532 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 ((hb == b && ht == EAH_NOTBASE) ||
2534 (hb == i && ht == EAH_MAKEBASE))) {
2535 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002536 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002537 x = bx, bx = ix, ix = x;
2538 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002539 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002540 bt = -1, bx = 0, s++;
2541 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002543 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002544 }
2545 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2546 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002547 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002548 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2549 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002550 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002552 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002554 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 x = ix, ix = bx, bx = x;
2556 }
2557 if (it == REG_NUM_ESP ||
2558 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002560
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002561 output->rex |= rexflags(it, ix, REX_X);
2562 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002563
Keith Kanios48af1772007-08-17 07:37:52 +00002564 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002565 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002567
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002571 } else {
2572 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002573 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002574 seg == NO_SEG && !forw_ref &&
2575 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002576 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002577 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002578 mod = 1;
2579 else
2580 mod = 2;
2581 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002582
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002583 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002584 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2585 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002586 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002587 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002589
Keith Kaniosb7a89542007-04-12 02:40:54 +00002590 if (it == -1)
2591 index = 4, s = 1;
2592 else
2593 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002594
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 switch (s) {
2596 case 1:
2597 scale = 0;
2598 break;
2599 case 2:
2600 scale = 1;
2601 break;
2602 case 4:
2603 scale = 2;
2604 break;
2605 case 8:
2606 scale = 3;
2607 break;
2608 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002611
Keith Kaniosb7a89542007-04-12 02:40:54 +00002612 if (bt == -1) {
2613 base = 5;
2614 mod = 0;
2615 } else {
2616 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002617 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002618 seg == NO_SEG && !forw_ref &&
2619 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002620 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002621 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002622 mod = 1;
2623 else
2624 mod = 2;
2625 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002626
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002627 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002628 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2629 output->modrm = GEN_MODRM(mod, rfield, 4);
2630 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 }
2632 } else { /* it's 16-bit */
2633 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002634 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002635
Keith Kaniosb7a89542007-04-12 02:40:54 +00002636 /* check for 64-bit long mode */
2637 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002638 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002639
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002641 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2642 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002644
Keith Kaniosb7a89542007-04-12 02:40:54 +00002645 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002646 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002647 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002650 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 if (b == -1 && i != -1) {
2652 int tmp = b;
2653 b = i;
2654 i = tmp;
2655 } /* swap */
2656 if ((b == R_SI || b == R_DI) && i != -1) {
2657 int tmp = b;
2658 b = i;
2659 i = tmp;
2660 }
2661 /* have BX/BP as base, SI/DI index */
2662 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002663 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 if (i != -1 && b != -1 &&
2665 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002666 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002669
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 rm = -1;
2671 if (i != -1)
2672 switch (i * 256 + b) {
2673 case R_SI * 256 + R_BX:
2674 rm = 0;
2675 break;
2676 case R_DI * 256 + R_BX:
2677 rm = 1;
2678 break;
2679 case R_SI * 256 + R_BP:
2680 rm = 2;
2681 break;
2682 case R_DI * 256 + R_BP:
2683 rm = 3;
2684 break;
2685 } else
2686 switch (b) {
2687 case R_SI:
2688 rm = 4;
2689 break;
2690 case R_DI:
2691 rm = 5;
2692 break;
2693 case R_BP:
2694 rm = 6;
2695 break;
2696 case R_BX:
2697 rm = 7;
2698 break;
2699 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002700 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002701 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002702
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002703 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2704 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002706 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 mod = 1;
2708 else
2709 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002710
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002711 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002712 output->bytes = mod; /* bytes of offset needed */
2713 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 }
2715 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002716 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002717
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002718 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002719 return output->type;
2720
2721err:
2722 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002723}
2724
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002725static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002726{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002727 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002728 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002729
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002730 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002731
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002732 switch (ins->prefixes[PPS_ASIZE]) {
2733 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002734 valid &= 16;
2735 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002736 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002737 valid &= 32;
2738 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002739 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002740 valid &= 64;
2741 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002742 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002743 valid &= (addrbits == 32) ? 16 : 32;
2744 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002745 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002746 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002747 }
2748
2749 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002750 if (is_class(MEMORY, ins->oprs[j].type)) {
2751 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002752
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002753 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002754 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 i = 0;
2756 else
2757 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002758
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002759 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002760 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002761 b = 0;
2762 else
2763 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002764
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002765 if (ins->oprs[j].scale == 0)
2766 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002767
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002768 if (!i && !b) {
2769 int ds = ins->oprs[j].disp_size;
2770 if ((addrbits != 64 && ds > 8) ||
2771 (addrbits == 64 && ds == 16))
2772 valid &= ds;
2773 } else {
2774 if (!(REG16 & ~b))
2775 valid &= 16;
2776 if (!(REG32 & ~b))
2777 valid &= 32;
2778 if (!(REG64 & ~b))
2779 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002780
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002781 if (!(REG16 & ~i))
2782 valid &= 16;
2783 if (!(REG32 & ~i))
2784 valid &= 32;
2785 if (!(REG64 & ~i))
2786 valid &= 64;
2787 }
2788 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002789 }
2790
2791 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002792 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002793 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002794 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002795 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002796 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002797 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002798 /* Impossible... */
2799 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2800 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002801 }
2802
2803 defdisp = ins->addr_size == 16 ? 16 : 32;
2804
2805 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002806 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2807 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2808 /*
2809 * mem_offs sizes must match the address size; if not,
2810 * strip the MEM_OFFS bit and match only EA instructions
2811 */
2812 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2813 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002814 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815}