blob: 86253a4d9593496ea2ff2c08e00e85b325afb5a8 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800176#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000177#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000178
179#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000180#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181#include "assemble.h"
182#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700183#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800184#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvin65289e82009-07-25 17:25:11 -0700186enum match_result {
187 /*
188 * Matching errors. These should be sorted so that more specific
189 * errors come later in the sequence.
190 */
191 MERR_INVALOP,
192 MERR_OPSIZEMISSING,
193 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700194 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700195 MERR_BADCPU,
196 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800197 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700198 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700199 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800200 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 /*
202 * Matching success; the conditional ones first
203 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400204 MOK_JUMP, /* Matching OK but needs jmp_match() */
205 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700206};
207
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700209 enum ea_type type; /* what kind of EA is this? */
210 int sib_present; /* is a SIB byte necessary? */
211 int bytes; /* # of bytes of offset needed */
212 int size; /* lazy - this is sib+bytes+1 */
213 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700214 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000215} ea;
216
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400217#define GEN_SIB(scale, index, base) \
218 (((scale) << 6) | ((index) << 3) | ((base)))
219
220#define GEN_MODRM(mod, reg, rm) \
221 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
222
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400223static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224static efunc errfunc;
225static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000226static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000227
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800228static int64_t calcsize(int32_t, int64_t, int, insn *,
229 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700230static void gencode(int32_t segment, int64_t offset, int bits,
231 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400232 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700233static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400234 insn *instruction,
235 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700236static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700237static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000238static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700239static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000240static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700241static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700242static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000243
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700245
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400246static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000247{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700248 return ins->prefixes[pos] == prefix;
249}
250
251static void assert_no_prefix(insn * ins, enum prefix_pos pos)
252{
253 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400254 errfunc(ERR_NONFATAL, "invalid %s prefix",
255 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256}
257
258static const char *size_name(int size)
259{
260 switch (size) {
261 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700273 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700275 case 64:
276 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000279 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700280}
281
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400282static void warn_overflow(int pass, int size)
283{
284 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
285 "%s data exceeds bounds", size_name(size));
286}
287
288static void warn_overflow_const(int64_t data, int size)
289{
290 if (overflow_general(data, size))
291 warn_overflow(ERR_PASS1, size);
292}
293
294static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700295{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100296 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400297 if (overflow_general(o->offset, size))
298 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700299 }
300}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000302/*
303 * This routine wrappers the real output format's output routine,
304 * in order to pass a copy of the data off to the listing file
305 * generator at the same time.
306 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800307static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800308 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400309 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000311 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000312 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800313 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000314
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800315 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400316 /*
317 * This is a non-relocated address, and we're going to
318 * convert it into RAWDATA format.
319 */
320 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800321
322 size = abs((int)size);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400323 if (size > 8) {
324 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
325 return;
326 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700327
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400328 WRITEADDR(q, *(int64_t *)data, size);
329 data = p;
330 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 }
332
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800333 list->output(offset, data, type, size);
334
Frank Kotlerabebb082003-09-06 04:45:37 +0000335 /*
336 * this call to src_get determines when we call the
337 * debug-format-specific "linenum" function
338 * it updates lineno and lnfname to the current values
339 * returning 0 if "same as last time", -2 if lnfname
340 * changed, and the amount by which lineno changed,
341 * if it did. thus, these variables must be static
342 */
343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400344 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800347 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000348}
349
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800350static void out_imm8(int64_t offset, int32_t segment,
351 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400352{
353 if (opx->segment != NO_SEG) {
354 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800355 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400356 } else {
357 uint8_t byte = opx->offset;
358 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
359 }
360}
361
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700362static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800363 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800365 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800366 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000367 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800368 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000369
H. Peter Anvin755f5212012-02-25 11:41:34 -0800370 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700371 return false;
372 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400373 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700374 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400375 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700376
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800377 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100378
Victor van den Elzen154e5922009-02-25 17:32:00 +0100379 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100380 /* Be optimistic in pass 1 */
381 return true;
382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700384 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000385
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700386 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800387 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
388
389 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
390 /* jmp short (opcode eb) cannot be used with bnd prefix. */
391 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800392 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
393 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800394 }
395
396 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000398
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400399int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400400 insn * instruction, struct ofmt *output, efunc error,
401 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000403 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700405 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800406 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000407 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800408 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300409 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000412 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 outfmt = output; /* likewise */
414 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000415
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300416 wsize = idata_bytes(instruction->opcode);
417 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419
H. Peter Anvineba20a72002-04-30 20:53:55 +0000420 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000422 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 if (t < 0)
424 errfunc(ERR_PANIC,
425 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400428 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400430 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700431 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400432 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000433 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700434 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800436 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400437 offset += wsize;
438 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700439 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400440 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800444 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000446
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 if (align) {
448 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100449 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800450 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 }
452 offset += e->stringlen + align;
453 }
454 }
455 if (t > 0 && t == instruction->times - 1) {
456 /*
457 * Dummy call to list->output to give the offset to the
458 * listing module.
459 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800460 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 list->uplevel(LIST_TIMES);
462 }
463 }
464 if (instruction->times > 1)
465 list->downlevel(LIST_TIMES);
466 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467 }
468
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700470 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400473 fp = fopen(fname, "rb");
474 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
476 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400477 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
479 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200480 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400481 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700482 static char buf[4096];
483 size_t t = instruction->times;
484 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400485 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 len = ftell(fp);
488 if (instruction->eops->next) {
489 base = instruction->eops->next->offset;
490 len -= base;
491 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700492 len > (size_t)instruction->eops->next->next->offset)
493 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 }
495 /*
496 * Dummy call to list->output to give the offset to the
497 * listing module.
498 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800499 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 list->uplevel(LIST_INCBIN);
501 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700502 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 fseek(fp, base, SEEK_SET);
505 l = len;
506 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700507 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400508 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 if (!m) {
510 /*
511 * This shouldn't happen unless the file
512 * actually changes while we are reading
513 * it.
514 */
515 error(ERR_NONFATAL,
516 "`incbin': unexpected EOF while"
517 " reading file `%s'", fname);
518 t = 0; /* Try to exit cleanly */
519 break;
520 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800521 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 NO_SEG, NO_SEG);
523 l -= m;
524 }
525 }
526 list->downlevel(LIST_INCBIN);
527 if (instruction->times > 1) {
528 /*
529 * Dummy call to list->output to give the offset to the
530 * listing module.
531 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800532 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 list->uplevel(LIST_TIMES);
534 list->downlevel(LIST_TIMES);
535 }
536 fclose(fp);
537 return instruction->times * len;
538 }
539 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000540 }
541
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700542 /* Check to see if we need an address-size prefix */
543 add_asp(instruction, bits);
544
H. Peter Anvin23595f52009-07-25 17:44:25 -0700545 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700546
H. Peter Anvin23595f52009-07-25 17:44:25 -0700547 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400548 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800549 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400550 itimes = instruction->times;
551 if (insn_size < 0) /* shouldn't be, on pass two */
552 error(ERR_PANIC, "errors made it through from pass one");
553 else
554 while (itimes--) {
555 for (j = 0; j < MAXPREFIX; j++) {
556 uint8_t c = 0;
557 switch (instruction->prefixes[j]) {
558 case P_WAIT:
559 c = 0x9B;
560 break;
561 case P_LOCK:
562 c = 0xF0;
563 break;
564 case P_REPNE:
565 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800566 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700567 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400568 c = 0xF2;
569 break;
570 case P_REPE:
571 case P_REPZ:
572 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800573 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400574 c = 0xF3;
575 break;
576 case R_CS:
577 if (bits == 64) {
578 error(ERR_WARNING | ERR_PASS2,
579 "cs segment base generated, but will be ignored in 64-bit mode");
580 }
581 c = 0x2E;
582 break;
583 case R_DS:
584 if (bits == 64) {
585 error(ERR_WARNING | ERR_PASS2,
586 "ds segment base generated, but will be ignored in 64-bit mode");
587 }
588 c = 0x3E;
589 break;
590 case R_ES:
591 if (bits == 64) {
592 error(ERR_WARNING | ERR_PASS2,
593 "es segment base generated, but will be ignored in 64-bit mode");
594 }
595 c = 0x26;
596 break;
597 case R_FS:
598 c = 0x64;
599 break;
600 case R_GS:
601 c = 0x65;
602 break;
603 case R_SS:
604 if (bits == 64) {
605 error(ERR_WARNING | ERR_PASS2,
606 "ss segment base generated, but will be ignored in 64-bit mode");
607 }
608 c = 0x36;
609 break;
610 case R_SEGR6:
611 case R_SEGR7:
612 error(ERR_NONFATAL,
613 "segr6 and segr7 cannot be used as prefixes");
614 break;
615 case P_A16:
616 if (bits == 64) {
617 error(ERR_NONFATAL,
618 "16-bit addressing is not supported "
619 "in 64-bit mode");
620 } else if (bits != 16)
621 c = 0x67;
622 break;
623 case P_A32:
624 if (bits != 32)
625 c = 0x67;
626 break;
627 case P_A64:
628 if (bits != 64) {
629 error(ERR_NONFATAL,
630 "64-bit addressing is only supported "
631 "in 64-bit mode");
632 }
633 break;
634 case P_ASP:
635 c = 0x67;
636 break;
637 case P_O16:
638 if (bits != 16)
639 c = 0x66;
640 break;
641 case P_O32:
642 if (bits == 16)
643 c = 0x66;
644 break;
645 case P_O64:
646 /* REX.W */
647 break;
648 case P_OSP:
649 c = 0x66;
650 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700651 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800652 case P_VEX3:
653 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800654 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400655 case P_none:
656 break;
657 default:
658 error(ERR_PANIC, "invalid instruction prefix");
659 }
660 if (c != 0) {
661 out(offset, segment, &c, OUT_RAWDATA, 1,
662 NO_SEG, NO_SEG);
663 offset++;
664 }
665 }
666 insn_end = offset + insn_size;
667 gencode(segment, offset, bits, instruction,
668 temp, insn_end);
669 offset += insn_size;
670 if (itimes > 0 && itimes == instruction->times - 1) {
671 /*
672 * Dummy call to list->output to give the offset to the
673 * listing module.
674 */
675 list->output(offset, NULL, OUT_RAWDATA, 0);
676 list->uplevel(LIST_TIMES);
677 }
678 }
679 if (instruction->times > 1)
680 list->downlevel(LIST_TIMES);
681 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700682 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400683 /* No match */
684 switch (m) {
685 case MERR_OPSIZEMISSING:
686 error(ERR_NONFATAL, "operation size not specified");
687 break;
688 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700691 case MERR_BRNUMMISMATCH:
692 error(ERR_NONFATAL,
693 "mismatch in the number of broadcasting elements");
694 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400695 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 break;
698 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800699 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400700 bits);
701 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800702 case MERR_ENCMISMATCH:
703 error(ERR_NONFATAL, "specific encoding scheme not available");
704 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800705 case MERR_BADBND:
706 error(ERR_NONFATAL, "bnd prefix is not allowed");
707 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800708 case MERR_BADREPNE:
709 error(ERR_NONFATAL, "%s prefix is not allowed",
710 (has_prefix(instruction, PPS_REP, P_REPNE) ?
711 "repne" : "repnz"));
712 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400713 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 error(ERR_NONFATAL,
715 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400716 break;
717 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718 }
719 return 0;
720}
721
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400722int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400723 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000725 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700726 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000729 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400731 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700734 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
735 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400736 instruction->opcode == I_DT || instruction->opcode == I_DO ||
737 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300739 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300742 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400744 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000745 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400748 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400750 warn_overflow_const(e->offset, wsize);
751 } else if (e->type == EOT_DB_STRING ||
752 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000754
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 align = (-osize) % wsize;
756 if (align < 0)
757 align += wsize;
758 isize += osize + align;
759 }
760 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000761 }
762
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400764 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300766 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700767 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400769 fp = fopen(fname, "rb");
770 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
772 fname);
773 else if (fseek(fp, 0L, SEEK_END) < 0)
774 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
775 fname);
776 else {
777 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (instruction->eops->next) {
779 len -= instruction->eops->next->offset;
780 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700781 len > (size_t)instruction->eops->next->next->offset) {
782 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 }
784 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300785 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300787 if (fp)
788 fclose(fp);
789 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000790 }
791
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700792 /* Check to see if we need an address-size prefix */
793 add_asp(instruction, bits);
794
H. Peter Anvin23595f52009-07-25 17:44:25 -0700795 m = find_match(&temp, instruction, segment, offset, bits);
796 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400797 /* we've matched an instruction. */
798 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400799 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100800
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800801 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400802 if (isize < 0)
803 return -1;
804 for (j = 0; j < MAXPREFIX; j++) {
805 switch (instruction->prefixes[j]) {
806 case P_A16:
807 if (bits != 16)
808 isize++;
809 break;
810 case P_A32:
811 if (bits != 32)
812 isize++;
813 break;
814 case P_O16:
815 if (bits != 16)
816 isize++;
817 break;
818 case P_O32:
819 if (bits == 16)
820 isize++;
821 break;
822 case P_A64:
823 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700824 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800825 case P_VEX3:
826 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800827 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400828 case P_none:
829 break;
830 default:
831 isize++;
832 break;
833 }
834 }
835 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700836 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400837 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000838 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000839}
840
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800841static void bad_hle_warn(const insn * ins, uint8_t hleok)
842{
843 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800844 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800845 static const enum whatwarn warn[2][4] =
846 {
847 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
848 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
849 };
850 unsigned int n;
851
852 n = (unsigned int)rep_pfx - P_XACQUIRE;
853 if (n > 1)
854 return; /* Not XACQUIRE/XRELEASE */
855
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800856 ww = warn[n][hleok];
857 if (!is_class(MEMORY, ins->oprs[0].type))
858 ww = w_inval; /* HLE requires operand 0 to be memory */
859
860 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800861 case w_none:
862 break;
863
864 case w_lock:
865 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800866 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800867 "%s with this instruction requires lock",
868 prefix_name(rep_pfx));
869 }
870 break;
871
872 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800873 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800874 "%s invalid with this instruction",
875 prefix_name(rep_pfx));
876 break;
877 }
878}
879
H. Peter Anvin507ae032008-10-09 15:37:10 -0700880/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400881#define case3(x) case (x): case (x)+1: case (x)+2
882#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700883
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800884static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000886{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800887 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800888 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000890 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700891 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700892 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700893 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700894 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800895 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800896 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700897 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000898
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700899 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700900 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700901 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700902
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700903 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700905
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 (void)segment; /* Don't warn that this parameter is unused */
907 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000908
H. Peter Anvin839eca22007-10-29 23:12:47 -0700909 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400910 c = *codes++;
911 op1 = (c & 3) + ((opex & 1) << 2);
912 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
913 opx = &ins->oprs[op1];
914 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700915
H. Peter Anvin839eca22007-10-29 23:12:47 -0700916 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400917 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 codes += c, length += c;
919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400921 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400922 opex = c;
923 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 case4(010):
926 ins->rex |=
927 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 codes++, length++;
929 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Jin Kyu Song164d6072013-10-15 19:10:13 -0700931 case4(014):
932 /* this is an index reg of MIB operand */
933 mib_index = opx->basereg;
934 break;
935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400936 case4(020):
937 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 length++;
939 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 length += 2;
943 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700944
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400945 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700946 if (opx->type & (BITS16 | BITS32 | BITS64))
947 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 else
949 length += (bits == 16) ? 2 : 4;
950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 length += 4;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400956 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700957 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 length++;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400964 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000965 length += 8; /* MOV reg64/imm */
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 length += 2;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700973 if (opx->type & (BITS16 | BITS32 | BITS64))
974 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 else
976 length += (bits == 16) ? 2 : 4;
977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 length += 4;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700984 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case 0172:
988 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700990 length++;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700993 case4(0174):
994 length++;
995 break;
996
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700997 case4(0240):
998 ins->rex |= REX_EV;
999 ins->vexreg = regval(opx);
1000 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1001 ins->vex_cm = *codes++;
1002 ins->vex_wlp = *codes++;
1003 ins->evex_tuple = (*codes++ - 0300);
1004 break;
1005
1006 case 0250:
1007 ins->rex |= REX_EV;
1008 ins->vexreg = 0;
1009 ins->vex_cm = *codes++;
1010 ins->vex_wlp = *codes++;
1011 ins->evex_tuple = (*codes++ - 0300);
1012 break;
1013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(0254):
1015 length += 4;
1016 break;
1017
1018 case4(0260):
1019 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001020 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 ins->vex_cm = *codes++;
1022 ins->vex_wlp = *codes++;
1023 break;
1024
1025 case 0270:
1026 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001027 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 ins->vex_cm = *codes++;
1029 ins->vex_wlp = *codes++;
1030 break;
1031
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001032 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001033 hleok = c & 3;
1034 break;
1035
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 case4(0274):
1037 length++;
1038 break;
1039
1040 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 if (bits == 64)
1045 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001046 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001050 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001054 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001055
Keith Kaniosb7a89542007-04-12 02:40:54 +00001056 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1058 has_prefix(ins, PPS_ASIZE, P_A32))
1059 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 case4(0314):
1063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001066 {
1067 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1068 if (pfx == P_O16)
1069 break;
1070 if (pfx != P_none)
1071 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1072 else
1073 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001075 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001076
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001078 {
1079 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1080 if (pfx == P_O32)
1081 break;
1082 if (pfx != P_none)
1083 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1084 else
1085 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001087 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 case 0322:
1090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
Keith Kaniosb7a89542007-04-12 02:40:54 +00001092 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001093 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Keith Kaniosb7a89542007-04-12 02:40:54 +00001096 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 case 0325:
1101 ins->rex |= REX_NH;
1102 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001103
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001104 case 0326:
1105 break;
1106
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0330:
1108 codes++, length++;
1109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001114 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 case 0333:
1116 length++;
1117 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 case 0334:
1120 ins->rex |= REX_L;
1121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001123 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001127 if (!ins->prefixes[PPS_REP])
1128 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001132 if (!ins->prefixes[PPS_REP])
1133 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 if (ins->oprs[0].segment != NO_SEG)
1138 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1139 " quantity of BSS space");
1140 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001141 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 case 0341:
1145 if (!ins->prefixes[PPS_WAIT])
1146 ins->prefixes[PPS_WAIT] = P_WAIT;
1147 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case 0360:
1150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001152 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001153 length++;
1154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0364:
1157 case 0365:
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Keith Kanios48af1772007-08-17 07:37:52 +00001160 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001161 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 length++;
1163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001165 case 0370:
1166 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 case 0373:
1170 length++;
1171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001173 case 0374:
1174 eat = EA_XMMVSIB;
1175 break;
1176
1177 case 0375:
1178 eat = EA_YMMVSIB;
1179 break;
1180
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001181 case 0376:
1182 eat = EA_ZMMVSIB;
1183 break;
1184
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 case4(0100):
1186 case4(0110):
1187 case4(0120):
1188 case4(0130):
1189 case4(0200):
1190 case4(0204):
1191 case4(0210):
1192 case4(0214):
1193 case4(0220):
1194 case4(0224):
1195 case4(0230):
1196 case4(0234):
1197 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001199 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 opflags_t rflags;
1201 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001202 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001203
Keith Kaniosb7a89542007-04-12 02:40:54 +00001204 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001205
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001206 if (c <= 0177) {
1207 /* pick rfield from operand b (opx) */
1208 rflags = regflag(opx);
1209 rfield = nasm_regvals[opx->basereg];
1210 } else {
1211 rflags = 0;
1212 rfield = c & 7;
1213 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001215 /* EVEX.b1 : evex_brerop contains the operand position */
1216 op_er_sae = (ins->evex_brerop >= 0 ?
1217 &ins->oprs[ins->evex_brerop] : NULL);
1218
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001219 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1220 /* set EVEX.b */
1221 ins->evex_p[2] |= EVEX_P2B;
1222 if (op_er_sae->decoflags & ER) {
1223 /* set EVEX.RC (rounding control) */
1224 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1225 & EVEX_P2RC;
1226 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001227 } else {
1228 /* set EVEX.L'L (vector length) */
1229 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001230 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001231 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001232 /* set EVEX.b */
1233 ins->evex_p[2] |= EVEX_P2B;
1234 }
1235 }
1236
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001237 if (itemp_has(temp, IF_MIB)) {
1238 opy->eaflags |= EAF_MIB;
1239 /*
1240 * if a separate form of MIB (ICC style) is used,
1241 * the index reg info is merged into mem operand
1242 */
1243 if (mib_index != R_none) {
1244 opy->indexreg = mib_index;
1245 opy->scale = 1;
1246 opy->hintbase = mib_index;
1247 opy->hinttype = EAH_NOTBASE;
1248 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001249 }
1250
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001251 if (process_ea(opy, &ea_data, bits,
1252 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 errfunc(ERR_NONFATAL, "invalid effective address");
1254 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001255 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001258 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001259 }
1260 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001261
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 default:
1263 errfunc(ERR_PANIC, "internal instruction table corrupt"
1264 ": instruction code \\%o (0x%02X) given", c, c);
1265 break;
1266 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001267 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001268
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001269 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001270
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001271 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001272 if (ins->rex & REX_H) {
1273 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1274 return -1;
1275 }
1276 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001277 }
1278
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001279 switch (ins->prefixes[PPS_VEX]) {
1280 case P_EVEX:
1281 if (!(ins->rex & REX_EV))
1282 return -1;
1283 break;
1284 case P_VEX3:
1285 case P_VEX2:
1286 if (!(ins->rex & REX_V))
1287 return -1;
1288 break;
1289 default:
1290 break;
1291 }
1292
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001293 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001295
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001297 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 return -1;
1299 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001300 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001302 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 ins->rex &= ~REX_W;
1304 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001305 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 ins->rex |= REX_W;
1307 bad32 &= ~REX_W;
1308 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001309 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 /* Follow REX_W */
1311 break;
1312 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001313
H. Peter Anvinfc561202011-07-07 16:58:22 -07001314 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1316 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001317 } else if (!(ins->rex & REX_EV) &&
1318 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1319 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1320 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001322 if (ins->rex & REX_EV)
1323 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001324 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1325 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 length += 3;
1327 else
1328 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001329 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 if (ins->rex & REX_H) {
1331 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1332 return -1;
1333 } else if (bits == 64) {
1334 length++;
1335 } else if ((ins->rex & REX_L) &&
1336 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001337 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001339 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001340 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 length++;
1342 } else {
1343 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1344 return -1;
1345 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001346 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001347
1348 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001349 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001350 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001351 "instruction is not lockable");
1352 }
1353
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001354 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001355
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001356 /*
1357 * when BND prefix is set by DEFAULT directive,
1358 * BND prefix is added to every appropriate instruction line
1359 * unless it is overridden by NOBND prefix.
1360 */
1361 if (globalbnd &&
1362 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1363 ins->prefixes[PPS_REP] = P_BND;
1364
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001365 return length;
1366}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001367
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001368static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1369{
1370 if (bits == 64) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001371 if ((ins->rex & REX_MASK) && !(ins->rex & (REX_V | REX_EV))) {
1372 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001373 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001374 return 1;
1375 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001376 }
1377
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001378 return 0;
1379}
1380
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001381static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001382 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001383 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001385 uint8_t c;
1386 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001387 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001388 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001389 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001390 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001391 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001392 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001393 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001394
H. Peter Anvin839eca22007-10-29 23:12:47 -07001395 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001396 c = *codes++;
1397 op1 = (c & 3) + ((opex & 1) << 2);
1398 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1399 opx = &ins->oprs[op1];
1400 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001401
H. Peter Anvin839eca22007-10-29 23:12:47 -07001402 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 case 01:
1404 case 02:
1405 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001406 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001407 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001408 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 codes += c;
1410 offset += c;
1411 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001412
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001413 case 05:
1414 case 06:
1415 case 07:
1416 opex = c;
1417 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001418
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001419 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001420 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001421 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001422 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 offset += 1;
1424 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425
Jin Kyu Song164d6072013-10-15 19:10:13 -07001426 case4(014):
1427 break;
1428
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001430 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001431 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001432 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001434 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 offset += 1;
1436 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001437
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001438 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001439 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001440 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001441 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001442 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 offset += 1;
1444 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001445
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001447 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001448 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001449 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001450 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 offset += 2;
1452 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001455 if (opx->type & (BITS16 | BITS32))
1456 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 else
1458 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001459 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001460 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001461 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001462 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 offset += size;
1464 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001466 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001467 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001468 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001469 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001470 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 offset += 4;
1472 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001473
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001474 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001475 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001476 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001477 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001478 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001479 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 offset += size;
1481 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001482
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001483 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001484 if (opx->segment != segment) {
1485 data = opx->offset;
1486 out(offset, segment, &data,
1487 OUT_REL1ADR, insn_end - offset,
1488 opx->segment, opx->wrt);
1489 } else {
1490 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 if (data > 127 || data < -128)
1492 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001493 out(offset, segment, &data,
1494 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1495 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 offset += 1;
1497 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001501 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001502 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001503 offset += 8;
1504 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001505
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001507 if (opx->segment != segment) {
1508 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001510 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001511 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
1517 offset += 2;
1518 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001521 if (opx->type & (BITS16 | BITS32 | BITS64))
1522 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 else
1524 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001525 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001526 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001527 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1529 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001531 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001533 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 }
1535 offset += size;
1536 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001537
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001539 if (opx->segment != segment) {
1540 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001542 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001543 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001545 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001547 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 }
1549 offset += 4;
1550 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001551
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001553 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001554 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1555 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001556 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001557 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001558 outfmt->segbase(1 + opx->segment),
1559 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001560 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001562
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 case 0172:
1564 c = *codes++;
1565 opx = &ins->oprs[c >> 3];
1566 bytes[0] = nasm_regvals[opx->basereg] << 4;
1567 opx = &ins->oprs[c & 7];
1568 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1569 errfunc(ERR_NONFATAL,
1570 "non-absolute expression not permitted as argument %d",
1571 c & 7);
1572 } else {
1573 if (opx->offset & ~15) {
1574 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1575 "four-bit argument exceeds bounds");
1576 }
1577 bytes[0] |= opx->offset & 15;
1578 }
1579 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1580 offset++;
1581 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001582
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001583 case 0173:
1584 c = *codes++;
1585 opx = &ins->oprs[c >> 4];
1586 bytes[0] = nasm_regvals[opx->basereg] << 4;
1587 bytes[0] |= c & 15;
1588 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1589 offset++;
1590 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001591
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001592 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 bytes[0] = nasm_regvals[opx->basereg] << 4;
1594 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1595 offset++;
1596 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001599 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1601 (int32_t)data != (int64_t)data) {
1602 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1603 "signed dword immediate exceeds bounds");
1604 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001605 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 opx->segment, opx->wrt);
1607 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001608 break;
1609
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001610 case4(0240):
1611 case 0250:
1612 codes += 3;
1613 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1614 EVEX_P2Z | EVEX_P2AAA, 2);
1615 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1616 bytes[0] = 0x62;
1617 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001618 bytes[1] = ((((ins->rex & 7) << 5) |
1619 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1620 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001621 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1622 ((~ins->vexreg & 15) << 3) |
1623 (1 << 2) | (ins->vex_wlp & 3);
1624 bytes[3] = ins->evex_p[2];
1625 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1626 offset += 4;
1627 break;
1628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 case4(0260):
1630 case 0270:
1631 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001632 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1633 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1635 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1636 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001637 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1639 offset += 3;
1640 } else {
1641 bytes[0] = 0xc5;
1642 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001643 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1645 offset += 2;
1646 }
1647 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001648
H. Peter Anvine014f352012-02-25 22:35:19 -08001649 case 0271:
1650 case 0272:
1651 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001652 break;
1653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 case4(0274):
1655 {
1656 uint64_t uv, um;
1657 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 if (ins->rex & REX_W)
1660 s = 64;
1661 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1662 s = 16;
1663 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1664 s = 32;
1665 else
1666 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001667
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 um = (uint64_t)2 << (s-1);
1669 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001670
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 if (uv > 127 && uv < (uint64_t)-128 &&
1672 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001673 /* If this wasn't explicitly byte-sized, warn as though we
1674 * had fallen through to the imm16/32/64 case.
1675 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001676 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001677 "%s value exceeds bounds",
1678 (opx->type & BITS8) ? "signed byte" :
1679 s == 16 ? "word" :
1680 s == 32 ? "dword" :
1681 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001683 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001684 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001685 out(offset, segment, &data, OUT_ADDRESS, 1,
1686 opx->segment, opx->wrt);
1687 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001688 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001689 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1690 NO_SEG);
1691 }
1692 offset += 1;
1693 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001698
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001700 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001702 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 offset += 1;
1704 } else
1705 offset += 0;
1706 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001707
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001709 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001711 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 offset += 1;
1713 } else
1714 offset += 0;
1715 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 case 0312:
1718 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719
Keith Kaniosb7a89542007-04-12 02:40:54 +00001720 case 0313:
1721 ins->rex = 0;
1722 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 case4(0314):
1725 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001726
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001730
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001732 case 0323:
1733 break;
1734
Keith Kaniosb7a89542007-04-12 02:40:54 +00001735 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001736 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001738
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001739 case 0325:
1740 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001741
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001742 case 0326:
1743 break;
1744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001746 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001747 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 offset += 1;
1749 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001754 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001756 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001757 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 offset += 1;
1759 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760
Keith Kanios48af1772007-08-17 07:37:52 +00001761 case 0334:
1762 if (ins->rex & REX_R) {
1763 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001764 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001765 offset += 1;
1766 }
1767 ins->rex &= ~(REX_L|REX_R);
1768 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001769
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001770 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001771 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001772
H. Peter Anvin962e3052008-08-28 17:47:16 -07001773 case 0336:
1774 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001775 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 if (ins->oprs[0].segment != NO_SEG)
1779 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1780 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001781 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 if (size > 0)
1783 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001784 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 offset += size;
1786 }
1787 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case 0341:
1790 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case 0360:
1793 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001794
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001795 case 0361:
1796 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001797 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1798 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001799 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001800
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001801 case 0364:
1802 case 0365:
1803 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001804
Keith Kanios48af1772007-08-17 07:37:52 +00001805 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001807 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001808 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001809 offset += 1;
1810 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001811
Jin Kyu Song03041092013-10-15 19:38:51 -07001812 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 case 0373:
1816 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001817 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 offset += 1;
1819 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001820
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001821 case 0374:
1822 eat = EA_XMMVSIB;
1823 break;
1824
1825 case 0375:
1826 eat = EA_YMMVSIB;
1827 break;
1828
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001829 case 0376:
1830 eat = EA_ZMMVSIB;
1831 break;
1832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case4(0100):
1834 case4(0110):
1835 case4(0120):
1836 case4(0130):
1837 case4(0200):
1838 case4(0204):
1839 case4(0210):
1840 case4(0214):
1841 case4(0220):
1842 case4(0224):
1843 case4(0230):
1844 case4(0234):
1845 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 ea ea_data;
1847 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001849 uint8_t *p;
1850 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001852
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001853 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 /* pick rfield from operand b (opx) */
1855 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001856 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 } else {
1858 /* rfield is constant */
1859 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001861 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001863 if (process_ea(opy, &ea_data, bits,
1864 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001866
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 p = bytes;
1868 *p++ = ea_data.modrm;
1869 if (ea_data.sib_present)
1870 *p++ = ea_data.sib;
1871
1872 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001873 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001875 /*
1876 * Make sure the address gets the right offset in case
1877 * the line breaks in the .lst file (BR 1197827)
1878 */
1879 offset += s;
1880 s = 0;
1881
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001882 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001883 /* use compressed displacement, if available */
1884 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001886 if (ea_data.rip) {
1887 if (opy->segment == segment) {
1888 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001889 if (overflow_signed(data, ea_data.bytes))
1890 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 out(offset, segment, &data, OUT_ADDRESS,
1892 ea_data.bytes, NO_SEG, NO_SEG);
1893 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001894 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001895 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 insn_end - offset, opy->segment, opy->wrt);
1897 }
1898 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001899 int asize = ins->addr_size >> 3;
1900 int atype = ea_data.bytes;
1901
1902 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001903 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001904 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001905 warn_overflow(ERR_PASS2, ea_data.bytes);
1906
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001907 if (asize > ea_data.bytes) {
1908 /*
1909 * If the address isn't the full width of
1910 * the address size, treat is as signed...
1911 */
1912 atype = -atype;
1913 }
1914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001916 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 }
1919 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 }
1921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 default:
1924 errfunc(ERR_PANIC, "internal instruction table corrupt"
1925 ": instruction code \\%o (0x%02X) given", c, c);
1926 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001928 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001929}
1930
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001931static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001932{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001933 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001934 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001935 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001936}
1937
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001938static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001939{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001940 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001942 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001943}
1944
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001945static int op_rexflags(const operand * o, int mask)
1946{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001947 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001948 int val;
1949
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001950 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001951 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001952
H. Peter Anvina4835d42008-05-20 14:21:29 -07001953 flags = nasm_reg_flags[o->basereg];
1954 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001955
1956 return rexflags(val, flags, mask);
1957}
1958
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001959static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001960{
1961 int rex = 0;
1962
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001963 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001964 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001965 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 rex |= REX_W;
1967 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1968 rex |= REX_H;
1969 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1970 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001971
1972 return rex & mask;
1973}
1974
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001975static int evexflags(int val, decoflags_t deco,
1976 int mask, uint8_t byte)
1977{
1978 int evex = 0;
1979
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001980 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001981 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001982 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001983 evex |= (EVEX_P0RP | EVEX_P0X);
1984 break;
1985 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001986 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001987 evex |= EVEX_P2VP;
1988 if (deco & Z)
1989 evex |= EVEX_P2Z;
1990 if (deco & OPMASK_MASK)
1991 evex |= deco & EVEX_P2AAA;
1992 break;
1993 }
1994 return evex & mask;
1995}
1996
1997static int op_evexflags(const operand * o, int mask, uint8_t byte)
1998{
1999 int val;
2000
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002001 val = nasm_regvals[o->basereg];
2002
2003 return evexflags(val, o->decoflags, mask, byte);
2004}
2005
H. Peter Anvin23595f52009-07-25 17:44:25 -07002006static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 insn *instruction,
2008 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002009{
2010 const struct itemplate *temp;
2011 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002012 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002013 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002014 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002015 int i;
2016
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002017 /* broadcasting uses a different data element size */
2018 for (i = 0; i < instruction->operands; i++)
2019 if (i == broadcast)
2020 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2021 else
2022 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002023
2024 merr = MERR_INVALOP;
2025
2026 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002027 temp->opcode != I_none; temp++) {
2028 m = matches(temp, instruction, bits);
2029 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002030 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002031 m = MOK_GOOD;
2032 else
2033 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002034 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 /*
2036 * Missing operand size and a candidate for fuzzy matching...
2037 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002038 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002039 if (i == broadcast)
2040 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2041 else
2042 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002043 opsizemissing = true;
2044 }
2045 if (m > merr)
2046 merr = m;
2047 if (merr == MOK_GOOD)
2048 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002049 }
2050
2051 /* No match, but see if we can get a fuzzy operand size match... */
2052 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002053 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002054
2055 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002056 /*
2057 * We ignore extrinsic operand sizes on registers, so we should
2058 * never try to fuzzy-match on them. This also resolves the case
2059 * when we have e.g. "xmmrm128" in two different positions.
2060 */
2061 if (is_class(REGISTER, instruction->oprs[i].type))
2062 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002063
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002064 /* This tests if xsizeflags[i] has more than one bit set */
2065 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2066 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002067
Jin Kyu Song7903c072013-10-30 03:00:12 -07002068 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002069 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002070 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2071 BITS32 : BITS64);
2072 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002073 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002074 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002075 }
2076
2077 /* Try matching again... */
2078 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002079 temp->opcode != I_none; temp++) {
2080 m = matches(temp, instruction, bits);
2081 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002082 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 m = MOK_GOOD;
2084 else
2085 m = MERR_INVALOP;
2086 }
2087 if (m > merr)
2088 merr = m;
2089 if (merr == MOK_GOOD)
2090 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002091 }
2092
H. Peter Anvina81655b2009-07-25 18:15:28 -07002093done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002094 *tempp = temp;
2095 return merr;
2096}
2097
H. Peter Anvin65289e82009-07-25 17:25:11 -07002098static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002099 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002100{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002101 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002102 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002103 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002104
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002105 /*
2106 * Check the opcode
2107 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002109 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002110
2111 /*
2112 * Count the operands
2113 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002115 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002116
2117 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002118 * Is it legal?
2119 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002120 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002121 return MERR_INVALOP;
2122
2123 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002124 * {evex} available?
2125 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002126 switch (instruction->prefixes[PPS_VEX]) {
2127 case P_EVEX:
2128 if (!itemp_has(itemp, IF_EVEX))
2129 return MERR_ENCMISMATCH;
2130 break;
2131 case P_VEX3:
2132 case P_VEX2:
2133 if (!itemp_has(itemp, IF_VEX))
2134 return MERR_ENCMISMATCH;
2135 break;
2136 default:
2137 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002138 }
2139
2140 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002141 * Check that no spurious colons or TOs are present
2142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 for (i = 0; i < itemp->operands; i++)
2144 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002145 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002146
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002147 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002148 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002149 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002150 switch (itemp_smask(itemp)) {
2151 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002152 asize = BITS8;
2153 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002154 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002155 asize = BITS16;
2156 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002157 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002158 asize = BITS32;
2159 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002160 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002161 asize = BITS64;
2162 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002163 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002164 asize = BITS128;
2165 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002166 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002167 asize = BITS256;
2168 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002169 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002170 asize = BITS512;
2171 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002172 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002173 switch (bits) {
2174 case 16:
2175 asize = BITS16;
2176 break;
2177 case 32:
2178 asize = BITS32;
2179 break;
2180 case 64:
2181 asize = BITS64;
2182 break;
2183 default:
2184 asize = 0;
2185 break;
2186 }
2187 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002188 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002189 asize = 0;
2190 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002191 }
2192
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002193 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002194 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002195 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002196 memset(size, 0, sizeof size);
2197 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002198 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002199 /* S- flags apply to all operands */
2200 for (i = 0; i < MAX_OPERANDS; i++)
2201 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002202 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002203
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002204 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002205 * Check that the operand flags all match up,
2206 * it's a bit tricky so lets be verbose:
2207 *
2208 * 1) Find out the size of operand. If instruction
2209 * doesn't have one specified -- we're trying to
2210 * guess it either from template (IF_S* flag) or
2211 * from code bits.
2212 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002213 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002214 * template has an operand size specified AND this size differ
2215 * from which instruction has (perhaps we got it from code bits)
2216 * we are:
2217 * a) Check that only size of instruction and operand is differ
2218 * other characteristics do match
2219 * b) Perhaps it's a register specified in instruction so
2220 * for such a case we just mark that operand as "size
2221 * missing" and this will turn on fuzzy operand size
2222 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002223 */
2224 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002226 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002227 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002228 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002229 opflags_t template_opsize, insn_opsize;
2230
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 if (!(type & SIZE_MASK))
2232 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002233
Jin Kyu Song7903c072013-10-30 03:00:12 -07002234 insn_opsize = type & SIZE_MASK;
2235 if (!is_broadcast) {
2236 template_opsize = itemp->opd[i] & SIZE_MASK;
2237 } else {
2238 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2239 /*
2240 * when broadcasting, the element size depends on
2241 * the instruction type. decorator flag should match.
2242 */
2243
2244 if (deco_brsize) {
2245 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002246 /* calculate the proper number : {1to<brcast_num>} */
2247 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2248 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002249 } else {
2250 template_opsize = 0;
2251 }
2252 }
2253
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002254 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002255 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002256 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002257 } else if (template_opsize) {
2258 if (template_opsize != insn_opsize) {
2259 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002260 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002261 } else if (!is_class(REGISTER, type)) {
2262 /*
2263 * Note: we don't honor extrinsic operand sizes for registers,
2264 * so "missing operand size" for a register should be
2265 * considered a wildcard match rather than an error.
2266 */
2267 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002268 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002269 } else if (is_broadcast &&
2270 (brcast_num !=
2271 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2272 /*
2273 * broadcasting opsize matches but the number of repeated memory
2274 * element does not match.
2275 * if 64b double precision float is broadcasted to zmm (512b),
2276 * broadcasting decorator must be {1to8}.
2277 */
2278 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002279 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002280 }
2281 }
2282
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002283 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002284 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002285
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002286 /*
2287 * Check operand sizes
2288 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002289 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2290 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002292 asize = itemp->opd[i] & SIZE_MASK;
2293 if (asize) {
2294 for (i = 0; i < oprs; i++)
2295 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 break;
2297 }
2298 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002299 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002301 }
2302
Keith Kaniosb7a89542007-04-12 02:40:54 +00002303 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 if (!(itemp->opd[i] & SIZE_MASK) &&
2305 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002306 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002307 }
2308
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002309 /*
2310 * Check template is okay at the set cpu level
2311 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002312 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002313 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002314
Keith Kaniosb7a89542007-04-12 02:40:54 +00002315 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002316 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002317 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002318 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002319 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002321 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002322 * If we have a HLE prefix, look for the NOHLE flag
2323 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002324 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002325 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2326 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2327 return MERR_BADHLE;
2328
2329 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002330 * Check if special handling needed for Jumps
2331 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002332 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002333 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334
Jin Kyu Song03041092013-10-15 19:38:51 -07002335 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002336 * Check if BND prefix is allowed.
2337 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002338 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002339 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002340 (has_prefix(instruction, PPS_REP, P_BND) ||
2341 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002342 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002343 else if (itemp_has(itemp, IF_BND) &&
2344 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2345 has_prefix(instruction, PPS_REP, P_REPNZ)))
2346 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002347
H. Peter Anvin60926242009-07-26 16:25:38 -07002348 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002349}
2350
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002351/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002352 * Check if ModR/M.mod should/can be 01.
2353 * - EAF_BYTEOFFS is set
2354 * - offset can fit in a byte when EVEX is not used
2355 * - offset can be compressed when EVEX is used
2356 */
2357#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2358 (o >= -128 && o <= 127 && \
2359 seg == NO_SEG && !forw_ref && \
2360 !(input->eaflags & EAF_WORDOFFS) && \
2361 !(ins->rex & REX_EV)) || \
2362 (ins->rex & REX_EV && \
2363 is_disp8n(input, ins, &output->disp8)))
2364
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002365static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002366 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002367{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002368 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002369 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002370 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002371
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002372 output->type = EA_SCALAR;
2373 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002374 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002375
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002376 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002377 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002378 /* EVEX.R' flag for the REG operand */
2379 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002380
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002381 if (is_class(REGISTER, input->type)) {
2382 /*
2383 * It's a direct register.
2384 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002385 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002386 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002388 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002389 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002390
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002391 /* broadcasting is not available with a direct register operand. */
2392 if (input->decoflags & BRDCAST_MASK) {
2393 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2394 goto err;
2395 }
2396
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002397 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002398 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002399 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002400 output->bytes = 0; /* no offset necessary either */
2401 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2402 } else {
2403 /*
2404 * It's a memory reference.
2405 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002406
2407 /* Embedded rounding or SAE is not available with a mem ref operand. */
2408 if (input->decoflags & (ER | SAE)) {
2409 nasm_error(ERR_NONFATAL,
2410 "Embedded rounding is available only with reg-reg op.");
2411 return -1;
2412 }
2413
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002414 if (input->basereg == -1 &&
2415 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002416 /*
2417 * It's a pure offset.
2418 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002419 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2420 input->segment == NO_SEG) {
2421 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2422 input->type &= ~IP_REL;
2423 input->type |= MEMORY;
2424 }
2425
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002426 if (bits == 64 &&
2427 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2428 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2429 return -1;
2430 }
2431
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002432 if (eaflags & EAF_BYTEOFFS ||
2433 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002434 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2435 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2436 }
2437
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002438 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002439 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002440 output->sib = GEN_SIB(0, 4, 5);
2441 output->bytes = 4;
2442 output->modrm = GEN_MODRM(0, rfield, 4);
2443 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002444 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002445 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002446 output->bytes = (addrbits != 16 ? 4 : 2);
2447 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2448 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002449 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 } else {
2451 /*
2452 * It's an indirection.
2453 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002455 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002457 int t, it, bt; /* register numbers */
2458 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 if (s == 0)
2461 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002462
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002463 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002464 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002465 ix = nasm_reg_flags[i];
2466 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002467 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002468 ix = 0;
2469 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002471 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002472 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 bx = nasm_reg_flags[b];
2474 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002475 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002476 bx = 0;
2477 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002478
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002479 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002480 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002481 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002482 int32_t o = input->offset;
2483 int mod, scale, index, base;
2484
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002485 /*
2486 * For a vector SIB, one has to be a vector and the other,
2487 * if present, a GPR. The vector must be the index operand.
2488 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002489 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002490 if (s == 0)
2491 s = 1;
2492 else if (s != 1)
2493 goto err;
2494
2495 t = bt, bt = it, it = t;
2496 x = bx, bx = ix, ix = x;
2497 }
2498
2499 if (bt != -1) {
2500 if (REG_GPR & ~bx)
2501 goto err;
2502 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2503 sok &= bx;
2504 else
2505 goto err;
2506 }
2507
2508 /*
2509 * While we're here, ensure the user didn't specify
2510 * WORD or QWORD
2511 */
2512 if (input->disp_size == 16 || input->disp_size == 64)
2513 goto err;
2514
2515 if (addrbits == 16 ||
2516 (addrbits == 32 && !(sok & BITS32)) ||
2517 (addrbits == 64 && !(sok & BITS64)))
2518 goto err;
2519
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002520 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2521 : ((ix & YMMREG & ~REG_EA)
2522 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002523
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002524 output->rex |= rexflags(it, ix, REX_X);
2525 output->rex |= rexflags(bt, bx, REX_B);
2526 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002527
2528 index = it & 7; /* it is known to be != -1 */
2529
2530 switch (s) {
2531 case 1:
2532 scale = 0;
2533 break;
2534 case 2:
2535 scale = 1;
2536 break;
2537 case 4:
2538 scale = 2;
2539 break;
2540 case 8:
2541 scale = 3;
2542 break;
2543 default: /* then what the smeg is it? */
2544 goto err; /* panic */
2545 }
2546
2547 if (bt == -1) {
2548 base = 5;
2549 mod = 0;
2550 } else {
2551 base = (bt & 7);
2552 if (base != REG_NUM_EBP && o == 0 &&
2553 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002554 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002556 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002557 mod = 1;
2558 else
2559 mod = 2;
2560 }
2561
2562 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002563 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2564 output->modrm = GEN_MODRM(mod, rfield, 4);
2565 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002566 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002567 /*
2568 * it must be a 32/64-bit memory reference. Firstly we have
2569 * to check that all registers involved are type E/Rxx.
2570 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002571 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002572 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002573
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002574 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002575 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2576 sok &= ix;
2577 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002578 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002579 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002580
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002581 if (bt != -1) {
2582 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002583 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002584 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002585 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002586 sok &= bx;
2587 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002588
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 /*
2590 * While we're here, ensure the user didn't specify
2591 * WORD or QWORD
2592 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002593 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002594 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002596 if (addrbits == 16 ||
2597 (addrbits == 32 && !(sok & BITS32)) ||
2598 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002599 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002600
Keith Kaniosb7a89542007-04-12 02:40:54 +00002601 /* now reorganize base/index */
2602 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002603 ((hb == b && ht == EAH_NOTBASE) ||
2604 (hb == i && ht == EAH_MAKEBASE))) {
2605 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002606 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002607 x = bx, bx = ix, ix = x;
2608 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002609
Jin Kyu Song164d6072013-10-15 19:10:13 -07002610 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002611 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002612 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002613 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002614 if (eaflags & EAF_MIB) {
2615 /* only for mib operands */
2616 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2617 /*
2618 * make a single reg index [reg*1].
2619 * gas uses this form for an explicit index register.
2620 */
2621 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2622 }
2623 if ((ht == EAH_SUMMED) && bt == -1) {
2624 /* separate once summed index into [base, index] */
2625 bt = it, bx = ix, s--;
2626 }
2627 } else {
2628 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002629 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002630 s == 3 || s == 5 || s == 9) && bt == -1) {
2631 /* convert 3*EAX to EAX+2*EAX */
2632 bt = it, bx = ix, s--;
2633 }
2634 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002635 (eaflags & EAF_TIMESTWO) &&
2636 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002637 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002638 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002639 * to sib format with 0x0 displacement - [EAX*1+0].
2640 */
2641 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2642 }
2643 }
Keith Kanios48af1772007-08-17 07:37:52 +00002644 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002645 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002646 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002647 x = ix, ix = bx, bx = x;
2648 }
2649 if (it == REG_NUM_ESP ||
2650 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002651 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002652
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 output->rex |= rexflags(it, ix, REX_X);
2654 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002655
Keith Kanios48af1772007-08-17 07:37:52 +00002656 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002659
Keith Kaniosb7a89542007-04-12 02:40:54 +00002660 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002663 } else {
2664 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002665 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002667 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002669 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002670 mod = 1;
2671 else
2672 mod = 2;
2673 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002674
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002675 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002676 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2677 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002678 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002679 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002681
Keith Kaniosb7a89542007-04-12 02:40:54 +00002682 if (it == -1)
2683 index = 4, s = 1;
2684 else
2685 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002686
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 switch (s) {
2688 case 1:
2689 scale = 0;
2690 break;
2691 case 2:
2692 scale = 1;
2693 break;
2694 case 4:
2695 scale = 2;
2696 break;
2697 case 8:
2698 scale = 3;
2699 break;
2700 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002701 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002703
Keith Kaniosb7a89542007-04-12 02:40:54 +00002704 if (bt == -1) {
2705 base = 5;
2706 mod = 0;
2707 } else {
2708 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002709 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002710 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002711 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002712 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002713 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 mod = 1;
2715 else
2716 mod = 2;
2717 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002718
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002719 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002720 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2721 output->modrm = GEN_MODRM(mod, rfield, 4);
2722 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 }
2724 } else { /* it's 16-bit */
2725 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002726 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002727
Keith Kaniosb7a89542007-04-12 02:40:54 +00002728 /* check for 64-bit long mode */
2729 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002730 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002731
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2734 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002735 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002736
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002738 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002739 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002742 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 if (b == -1 && i != -1) {
2744 int tmp = b;
2745 b = i;
2746 i = tmp;
2747 } /* swap */
2748 if ((b == R_SI || b == R_DI) && i != -1) {
2749 int tmp = b;
2750 b = i;
2751 i = tmp;
2752 }
2753 /* have BX/BP as base, SI/DI index */
2754 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 if (i != -1 && b != -1 &&
2757 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002758 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002759 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002760 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 rm = -1;
2763 if (i != -1)
2764 switch (i * 256 + b) {
2765 case R_SI * 256 + R_BX:
2766 rm = 0;
2767 break;
2768 case R_DI * 256 + R_BX:
2769 rm = 1;
2770 break;
2771 case R_SI * 256 + R_BP:
2772 rm = 2;
2773 break;
2774 case R_DI * 256 + R_BP:
2775 rm = 3;
2776 break;
2777 } else
2778 switch (b) {
2779 case R_SI:
2780 rm = 4;
2781 break;
2782 case R_DI:
2783 rm = 5;
2784 break;
2785 case R_BP:
2786 rm = 6;
2787 break;
2788 case R_BX:
2789 rm = 7;
2790 break;
2791 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002792 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002795 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002796 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002798 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 mod = 1;
2800 else
2801 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002802
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002803 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002804 output->bytes = mod; /* bytes of offset needed */
2805 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 }
2807 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002809
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002811 return output->type;
2812
2813err:
2814 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815}
2816
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002817static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002818{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002819 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002820 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002821
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002822 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002823
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002824 switch (ins->prefixes[PPS_ASIZE]) {
2825 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002826 valid &= 16;
2827 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002828 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002829 valid &= 32;
2830 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002831 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002832 valid &= 64;
2833 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002834 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 valid &= (addrbits == 32) ? 16 : 32;
2836 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002838 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002839 }
2840
2841 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002842 if (is_class(MEMORY, ins->oprs[j].type)) {
2843 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002844
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002845 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002846 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 i = 0;
2848 else
2849 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002851 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002852 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002853 b = 0;
2854 else
2855 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 if (ins->oprs[j].scale == 0)
2858 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002860 if (!i && !b) {
2861 int ds = ins->oprs[j].disp_size;
2862 if ((addrbits != 64 && ds > 8) ||
2863 (addrbits == 64 && ds == 16))
2864 valid &= ds;
2865 } else {
2866 if (!(REG16 & ~b))
2867 valid &= 16;
2868 if (!(REG32 & ~b))
2869 valid &= 32;
2870 if (!(REG64 & ~b))
2871 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002873 if (!(REG16 & ~i))
2874 valid &= 16;
2875 if (!(REG32 & ~i))
2876 valid &= 32;
2877 if (!(REG64 & ~i))
2878 valid &= 64;
2879 }
2880 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002881 }
2882
2883 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002884 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002885 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002887 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002888 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002889 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 /* Impossible... */
2891 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2892 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 }
2894
2895 defdisp = ins->addr_size == 16 ? 16 : 32;
2896
2897 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2899 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2900 /*
2901 * mem_offs sizes must match the address size; if not,
2902 * strip the MEM_OFFS bit and match only EA instructions
2903 */
2904 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2905 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002906 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002907}