blob: a09b9643efe8b74b36ee9984f7ccf45e9f4df0fa [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000176#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
178#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000179#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include "assemble.h"
181#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700182#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800183#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
H. Peter Anvin65289e82009-07-25 17:25:11 -0700185enum match_result {
186 /*
187 * Matching errors. These should be sorted so that more specific
188 * errors come later in the sequence.
189 */
190 MERR_INVALOP,
191 MERR_OPSIZEMISSING,
192 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700193 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700194 MERR_BADCPU,
195 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800196 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700197 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700198 MERR_BADBND,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700199 /*
200 * Matching success; the conditional ones first
201 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400202 MOK_JUMP, /* Matching OK but needs jmp_match() */
203 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700204};
205
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700207 enum ea_type type; /* what kind of EA is this? */
208 int sib_present; /* is a SIB byte necessary? */
209 int bytes; /* # of bytes of offset needed */
210 int size; /* lazy - this is sib+bytes+1 */
211 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700212 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000213} ea;
214
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400215#define GEN_SIB(scale, index, base) \
216 (((scale) << 6) | ((index) << 3) | ((base)))
217
218#define GEN_MODRM(mod, reg, rm) \
219 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
220
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700221static iflags_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000222static efunc errfunc;
223static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000224static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800226static int64_t calcsize(int32_t, int64_t, int, insn *,
227 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700228static void gencode(int32_t segment, int64_t offset, int bits,
229 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400230 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700231static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400232 insn *instruction,
233 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700234static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700235static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000236static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700237static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000238static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700239static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700240static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000241
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700242static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700243
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400244static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000245{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700246 return ins->prefixes[pos] == prefix;
247}
248
249static void assert_no_prefix(insn * ins, enum prefix_pos pos)
250{
251 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400252 errfunc(ERR_NONFATAL, "invalid %s prefix",
253 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700254}
255
256static const char *size_name(int size)
257{
258 switch (size) {
259 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700271 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700273 case 64:
274 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000277 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700278}
279
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400280static void warn_overflow(int pass, int size)
281{
282 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
283 "%s data exceeds bounds", size_name(size));
284}
285
286static void warn_overflow_const(int64_t data, int size)
287{
288 if (overflow_general(data, size))
289 warn_overflow(ERR_PASS1, size);
290}
291
292static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700293{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100294 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295 if (overflow_general(o->offset, size))
296 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700297 }
298}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400299
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000300/*
301 * This routine wrappers the real output format's output routine,
302 * in order to pass a copy of the data off to the listing file
303 * generator at the same time.
304 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800305static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800306 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400307 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000308{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000309 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000310 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800311 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000312
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800313 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400314 /*
315 * This is a non-relocated address, and we're going to
316 * convert it into RAWDATA format.
317 */
318 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400320 if (size > 8) {
321 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
322 return;
323 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700324
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400325 WRITEADDR(q, *(int64_t *)data, size);
326 data = p;
327 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000328 }
329
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800330 list->output(offset, data, type, size);
331
Frank Kotlerabebb082003-09-06 04:45:37 +0000332 /*
333 * this call to src_get determines when we call the
334 * debug-format-specific "linenum" function
335 * it updates lineno and lnfname to the current values
336 * returning 0 if "same as last time", -2 if lnfname
337 * changed, and the amount by which lineno changed,
338 * if it did. thus, these variables must be static
339 */
340
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400341 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000342 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800344 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000345}
346
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400347static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
348{
349 if (opx->segment != NO_SEG) {
350 uint64_t data = opx->offset;
351 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
352 } else {
353 uint8_t byte = opx->offset;
354 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
355 }
356}
357
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700358static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800359 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800361 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800362 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000363 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000364
H. Peter Anvin755f5212012-02-25 11:41:34 -0800365 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700366 return false;
367 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400368 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700369 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400370 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700371
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800372 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100373
Victor van den Elzen154e5922009-02-25 17:32:00 +0100374 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100375 /* Be optimistic in pass 1 */
376 return true;
377
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700379 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000380
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700381 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
382 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000384
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700385int64_t assemble(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400386 insn * instruction, struct ofmt *output, efunc error,
387 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000389 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700391 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800392 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000393 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800394 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300395 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000396
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000398 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 outfmt = output; /* likewise */
400 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300402 wsize = idata_bytes(instruction->opcode);
403 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000405
H. Peter Anvineba20a72002-04-30 20:53:55 +0000406 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000408 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 if (t < 0)
410 errfunc(ERR_PANIC,
411 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000412
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400414 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400416 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700417 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400418 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000419 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700420 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800422 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400423 offset += wsize;
424 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700425 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400426 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000428
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800430 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 if (align) {
434 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100435 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800436 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 }
438 offset += e->stringlen + align;
439 }
440 }
441 if (t > 0 && t == instruction->times - 1) {
442 /*
443 * Dummy call to list->output to give the offset to the
444 * listing module.
445 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800446 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 list->uplevel(LIST_TIMES);
448 }
449 }
450 if (instruction->times > 1)
451 list->downlevel(LIST_TIMES);
452 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000453 }
454
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700456 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000458
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400459 fp = fopen(fname, "rb");
460 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
462 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400463 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
465 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200466 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400467 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700468 static char buf[4096];
469 size_t t = instruction->times;
470 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400471 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 len = ftell(fp);
474 if (instruction->eops->next) {
475 base = instruction->eops->next->offset;
476 len -= base;
477 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700478 len > (size_t)instruction->eops->next->next->offset)
479 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 }
481 /*
482 * Dummy call to list->output to give the offset to the
483 * listing module.
484 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800485 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 list->uplevel(LIST_INCBIN);
487 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700488 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 fseek(fp, base, SEEK_SET);
491 l = len;
492 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700493 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400494 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 if (!m) {
496 /*
497 * This shouldn't happen unless the file
498 * actually changes while we are reading
499 * it.
500 */
501 error(ERR_NONFATAL,
502 "`incbin': unexpected EOF while"
503 " reading file `%s'", fname);
504 t = 0; /* Try to exit cleanly */
505 break;
506 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800507 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 NO_SEG, NO_SEG);
509 l -= m;
510 }
511 }
512 list->downlevel(LIST_INCBIN);
513 if (instruction->times > 1) {
514 /*
515 * Dummy call to list->output to give the offset to the
516 * listing module.
517 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800518 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 list->uplevel(LIST_TIMES);
520 list->downlevel(LIST_TIMES);
521 }
522 fclose(fp);
523 return instruction->times * len;
524 }
525 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000526 }
527
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700528 /* Check to see if we need an address-size prefix */
529 add_asp(instruction, bits);
530
H. Peter Anvin23595f52009-07-25 17:44:25 -0700531 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700532
H. Peter Anvin23595f52009-07-25 17:44:25 -0700533 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800535 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400536 itimes = instruction->times;
537 if (insn_size < 0) /* shouldn't be, on pass two */
538 error(ERR_PANIC, "errors made it through from pass one");
539 else
540 while (itimes--) {
541 for (j = 0; j < MAXPREFIX; j++) {
542 uint8_t c = 0;
543 switch (instruction->prefixes[j]) {
544 case P_WAIT:
545 c = 0x9B;
546 break;
547 case P_LOCK:
548 c = 0xF0;
549 break;
550 case P_REPNE:
551 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800552 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700553 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400554 c = 0xF2;
555 break;
556 case P_REPE:
557 case P_REPZ:
558 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800559 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400560 c = 0xF3;
561 break;
562 case R_CS:
563 if (bits == 64) {
564 error(ERR_WARNING | ERR_PASS2,
565 "cs segment base generated, but will be ignored in 64-bit mode");
566 }
567 c = 0x2E;
568 break;
569 case R_DS:
570 if (bits == 64) {
571 error(ERR_WARNING | ERR_PASS2,
572 "ds segment base generated, but will be ignored in 64-bit mode");
573 }
574 c = 0x3E;
575 break;
576 case R_ES:
577 if (bits == 64) {
578 error(ERR_WARNING | ERR_PASS2,
579 "es segment base generated, but will be ignored in 64-bit mode");
580 }
581 c = 0x26;
582 break;
583 case R_FS:
584 c = 0x64;
585 break;
586 case R_GS:
587 c = 0x65;
588 break;
589 case R_SS:
590 if (bits == 64) {
591 error(ERR_WARNING | ERR_PASS2,
592 "ss segment base generated, but will be ignored in 64-bit mode");
593 }
594 c = 0x36;
595 break;
596 case R_SEGR6:
597 case R_SEGR7:
598 error(ERR_NONFATAL,
599 "segr6 and segr7 cannot be used as prefixes");
600 break;
601 case P_A16:
602 if (bits == 64) {
603 error(ERR_NONFATAL,
604 "16-bit addressing is not supported "
605 "in 64-bit mode");
606 } else if (bits != 16)
607 c = 0x67;
608 break;
609 case P_A32:
610 if (bits != 32)
611 c = 0x67;
612 break;
613 case P_A64:
614 if (bits != 64) {
615 error(ERR_NONFATAL,
616 "64-bit addressing is only supported "
617 "in 64-bit mode");
618 }
619 break;
620 case P_ASP:
621 c = 0x67;
622 break;
623 case P_O16:
624 if (bits != 16)
625 c = 0x66;
626 break;
627 case P_O32:
628 if (bits == 16)
629 c = 0x66;
630 break;
631 case P_O64:
632 /* REX.W */
633 break;
634 case P_OSP:
635 c = 0x66;
636 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700637 case P_EVEX:
638 /* EVEX */
639 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400640 case P_none:
641 break;
642 default:
643 error(ERR_PANIC, "invalid instruction prefix");
644 }
645 if (c != 0) {
646 out(offset, segment, &c, OUT_RAWDATA, 1,
647 NO_SEG, NO_SEG);
648 offset++;
649 }
650 }
651 insn_end = offset + insn_size;
652 gencode(segment, offset, bits, instruction,
653 temp, insn_end);
654 offset += insn_size;
655 if (itimes > 0 && itimes == instruction->times - 1) {
656 /*
657 * Dummy call to list->output to give the offset to the
658 * listing module.
659 */
660 list->output(offset, NULL, OUT_RAWDATA, 0);
661 list->uplevel(LIST_TIMES);
662 }
663 }
664 if (instruction->times > 1)
665 list->downlevel(LIST_TIMES);
666 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700667 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400668 /* No match */
669 switch (m) {
670 case MERR_OPSIZEMISSING:
671 error(ERR_NONFATAL, "operation size not specified");
672 break;
673 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400675 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700676 case MERR_BRNUMMISMATCH:
677 error(ERR_NONFATAL,
678 "mismatch in the number of broadcasting elements");
679 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400680 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400682 break;
683 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800684 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400685 bits);
686 break;
687 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 error(ERR_NONFATAL,
689 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 break;
691 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692 }
693 return 0;
694}
695
Jin Kyu Song9bb987d2013-08-26 20:28:42 -0700696int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000698{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000699 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700700 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000703 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000704
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400705 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000707
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700708 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
709 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400710 instruction->opcode == I_DT || instruction->opcode == I_DO ||
711 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300713 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300716 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000717
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400718 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000719 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000720
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400722 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000723 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400724 warn_overflow_const(e->offset, wsize);
725 } else if (e->type == EOT_DB_STRING ||
726 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 align = (-osize) % wsize;
730 if (align < 0)
731 align += wsize;
732 isize += osize + align;
733 }
734 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000735 }
736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400738 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300740 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700741 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400743 fp = fopen(fname, "rb");
744 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
746 fname);
747 else if (fseek(fp, 0L, SEEK_END) < 0)
748 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
749 fname);
750 else {
751 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 if (instruction->eops->next) {
753 len -= instruction->eops->next->offset;
754 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700755 len > (size_t)instruction->eops->next->next->offset) {
756 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 }
758 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300759 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300761 if (fp)
762 fclose(fp);
763 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000764 }
765
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700766 /* Check to see if we need an address-size prefix */
767 add_asp(instruction, bits);
768
H. Peter Anvin23595f52009-07-25 17:44:25 -0700769 m = find_match(&temp, instruction, segment, offset, bits);
770 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400771 /* we've matched an instruction. */
772 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400773 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100774
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800775 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400776 if (isize < 0)
777 return -1;
778 for (j = 0; j < MAXPREFIX; j++) {
779 switch (instruction->prefixes[j]) {
780 case P_A16:
781 if (bits != 16)
782 isize++;
783 break;
784 case P_A32:
785 if (bits != 32)
786 isize++;
787 break;
788 case P_O16:
789 if (bits != 16)
790 isize++;
791 break;
792 case P_O32:
793 if (bits == 16)
794 isize++;
795 break;
796 case P_A64:
797 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700798 case P_EVEX:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400799 case P_none:
800 break;
801 default:
802 isize++;
803 break;
804 }
805 }
806 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700807 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400808 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810}
811
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800812static void bad_hle_warn(const insn * ins, uint8_t hleok)
813{
814 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800815 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800816 static const enum whatwarn warn[2][4] =
817 {
818 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
819 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
820 };
821 unsigned int n;
822
823 n = (unsigned int)rep_pfx - P_XACQUIRE;
824 if (n > 1)
825 return; /* Not XACQUIRE/XRELEASE */
826
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800827 ww = warn[n][hleok];
828 if (!is_class(MEMORY, ins->oprs[0].type))
829 ww = w_inval; /* HLE requires operand 0 to be memory */
830
831 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800832 case w_none:
833 break;
834
835 case w_lock:
836 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800837 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800838 "%s with this instruction requires lock",
839 prefix_name(rep_pfx));
840 }
841 break;
842
843 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800844 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800845 "%s invalid with this instruction",
846 prefix_name(rep_pfx));
847 break;
848 }
849}
850
H. Peter Anvin507ae032008-10-09 15:37:10 -0700851/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400852#define case3(x) case (x): case (x)+1: case (x)+2
853#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700854
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800855static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800856 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000857{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800858 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800859 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000860 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000861 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700862 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700863 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700864 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700865 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800866 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800867 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700868 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000869
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700870 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700871 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700872 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700873
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700874 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400875 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700876
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 (void)segment; /* Don't warn that this parameter is unused */
878 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000879
H. Peter Anvin839eca22007-10-29 23:12:47 -0700880 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400881 c = *codes++;
882 op1 = (c & 3) + ((opex & 1) << 2);
883 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
884 opx = &ins->oprs[op1];
885 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700886
H. Peter Anvin839eca22007-10-29 23:12:47 -0700887 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400888 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 codes += c, length += c;
890 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700891
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400892 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400893 opex = c;
894 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400896 case4(010):
897 ins->rex |=
898 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 codes++, length++;
900 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700901
Jin Kyu Song164d6072013-10-15 19:10:13 -0700902 case4(014):
903 /* this is an index reg of MIB operand */
904 mib_index = opx->basereg;
905 break;
906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 case4(020):
908 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 length++;
910 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400912 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 length += 2;
914 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700915
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400916 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700917 if (opx->type & (BITS16 | BITS32 | BITS64))
918 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 else
920 length += (bits == 16) ? 2 : 4;
921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400923 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 length += 4;
925 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700928 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400931 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 length++;
933 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000936 length += 8; /* MOV reg64/imm */
937 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 length += 2;
941 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700942
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400943 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700944 if (opx->type & (BITS16 | BITS32 | BITS64))
945 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 else
947 length += (bits == 16) ? 2 : 4;
948 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 length += 4;
952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700955 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 case 0172:
959 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700961 length++;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700964 case4(0174):
965 length++;
966 break;
967
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700968 case4(0240):
969 ins->rex |= REX_EV;
970 ins->vexreg = regval(opx);
971 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
972 ins->vex_cm = *codes++;
973 ins->vex_wlp = *codes++;
974 ins->evex_tuple = (*codes++ - 0300);
975 break;
976
977 case 0250:
978 ins->rex |= REX_EV;
979 ins->vexreg = 0;
980 ins->vex_cm = *codes++;
981 ins->vex_wlp = *codes++;
982 ins->evex_tuple = (*codes++ - 0300);
983 break;
984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(0254):
986 length += 4;
987 break;
988
989 case4(0260):
990 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700991 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 ins->vex_cm = *codes++;
993 ins->vex_wlp = *codes++;
994 break;
995
996 case 0270:
997 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700998 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 ins->vex_cm = *codes++;
1000 ins->vex_wlp = *codes++;
1001 break;
1002
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001003 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001004 hleok = c & 3;
1005 break;
1006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(0274):
1008 length++;
1009 break;
1010
1011 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 if (bits == 64)
1016 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001017 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001019
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001021 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001023
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001025 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001026
Keith Kaniosb7a89542007-04-12 02:40:54 +00001027 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1029 has_prefix(ins, PPS_ASIZE, P_A32))
1030 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001032
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001033 case4(0314):
1034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001037 {
1038 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1039 if (pfx == P_O16)
1040 break;
1041 if (pfx != P_none)
1042 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1043 else
1044 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001046 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001049 {
1050 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1051 if (pfx == P_O32)
1052 break;
1053 if (pfx != P_none)
1054 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1055 else
1056 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001058 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 case 0322:
1061 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001062
Keith Kaniosb7a89542007-04-12 02:40:54 +00001063 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001064 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001065 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001066
Keith Kaniosb7a89542007-04-12 02:40:54 +00001067 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001069 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001071 case 0325:
1072 ins->rex |= REX_NH;
1073 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001074
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001075 case 0326:
1076 break;
1077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0330:
1079 codes++, length++;
1080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001085 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0333:
1087 length++;
1088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001090 case 0334:
1091 ins->rex |= REX_L;
1092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001094 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001098 if (!ins->prefixes[PPS_REP])
1099 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001102 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001103 if (!ins->prefixes[PPS_REP])
1104 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001106
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 if (ins->oprs[0].segment != NO_SEG)
1109 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1110 " quantity of BSS space");
1111 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001112 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001114
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001115 case 0341:
1116 if (!ins->prefixes[PPS_WAIT])
1117 ins->prefixes[PPS_WAIT] = P_WAIT;
1118 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001119
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001120 case 0360:
1121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001123 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001124 length++;
1125 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001126
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 case 0364:
1128 case 0365:
1129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
Keith Kanios48af1772007-08-17 07:37:52 +00001131 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001132 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001133 length++;
1134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001136 case 0370:
1137 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 0373:
1141 length++;
1142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001144 case 0374:
1145 eat = EA_XMMVSIB;
1146 break;
1147
1148 case 0375:
1149 eat = EA_YMMVSIB;
1150 break;
1151
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001152 case 0376:
1153 eat = EA_ZMMVSIB;
1154 break;
1155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case4(0100):
1157 case4(0110):
1158 case4(0120):
1159 case4(0130):
1160 case4(0200):
1161 case4(0204):
1162 case4(0210):
1163 case4(0214):
1164 case4(0220):
1165 case4(0224):
1166 case4(0230):
1167 case4(0234):
1168 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001170 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 opflags_t rflags;
1172 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001173 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001174
Keith Kaniosb7a89542007-04-12 02:40:54 +00001175 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001176
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 if (c <= 0177) {
1178 /* pick rfield from operand b (opx) */
1179 rflags = regflag(opx);
1180 rfield = nasm_regvals[opx->basereg];
1181 } else {
1182 rflags = 0;
1183 rfield = c & 7;
1184 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001185
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001186 /* EVEX.b1 : evex_brerop contains the operand position */
1187 op_er_sae = (ins->evex_brerop >= 0 ?
1188 &ins->oprs[ins->evex_brerop] : NULL);
1189
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001190 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1191 /* set EVEX.b */
1192 ins->evex_p[2] |= EVEX_P2B;
1193 if (op_er_sae->decoflags & ER) {
1194 /* set EVEX.RC (rounding control) */
1195 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1196 & EVEX_P2RC;
1197 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001198 } else {
1199 /* set EVEX.L'L (vector length) */
1200 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001201 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001202 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001203 /* set EVEX.b */
1204 ins->evex_p[2] |= EVEX_P2B;
1205 }
1206 }
1207
Jin Kyu Song164d6072013-10-15 19:10:13 -07001208 /*
1209 * if a separate form of MIB (ICC style) is used,
1210 * the index reg info is merged into mem operand
1211 */
1212 if (mib_index != R_none) {
1213 opy->indexreg = mib_index;
1214 opy->scale = 1;
1215 opy->hintbase = mib_index;
1216 opy->hinttype = EAH_NOTBASE;
1217 }
1218
Jin Kyu Song3b653232013-11-08 11:41:12 -08001219 /*
1220 * only for mib operands, make a single reg index [reg*1].
1221 * gas uses this form to explicitly denote index register.
1222 */
1223 if ((temp->flags & IF_MIB) &&
1224 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1225 opy->hinttype == EAH_NOTBASE)) {
1226 opy->indexreg = opy->basereg;
1227 opy->basereg = -1;
1228 opy->scale = 1;
1229 }
1230
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001231 if (process_ea(opy, &ea_data, bits,
1232 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 errfunc(ERR_NONFATAL, "invalid effective address");
1234 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001235 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001236 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001238 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001239 }
1240 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001241
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001242 default:
1243 errfunc(ERR_PANIC, "internal instruction table corrupt"
1244 ": instruction code \\%o (0x%02X) given", c, c);
1245 break;
1246 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001247 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001248
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001249 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001250
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001251 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 if (ins->rex & REX_H) {
1253 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1254 return -1;
1255 }
1256 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001257 }
1258
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001259 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001261
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001263 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001264 return -1;
1265 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001266 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001267 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001268 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 ins->rex &= ~REX_W;
1270 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001271 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001272 ins->rex |= REX_W;
1273 bad32 &= ~REX_W;
1274 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001275 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001276 /* Follow REX_W */
1277 break;
1278 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001279
H. Peter Anvinfc561202011-07-07 16:58:22 -07001280 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001281 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1282 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001283 } else if (!(ins->rex & REX_EV) &&
1284 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1285 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1286 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001288 if (ins->rex & REX_EV)
1289 length += 4;
1290 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 length += 3;
1292 else
1293 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001294 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001295 if (ins->rex & REX_H) {
1296 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1297 return -1;
1298 } else if (bits == 64) {
1299 length++;
1300 } else if ((ins->rex & REX_L) &&
1301 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1302 cpu >= IF_X86_64) {
1303 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001304 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001305 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 length++;
1307 } else {
1308 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1309 return -1;
1310 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001311 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001312
1313 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1314 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001315 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001316 "instruction is not lockable");
1317 }
1318
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001319 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001320
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001321 return length;
1322}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001323
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001324static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1325{
1326 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001327 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001328 ins->rex = (ins->rex & REX_REAL) | REX_P;
1329 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1330 ins->rex = 0;
1331 return 1;
1332 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001333 }
1334
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001335 return 0;
1336}
1337
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001338static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001339 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001342 uint8_t c;
1343 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001344 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001345 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001346 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001347 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001348 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001349 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001350 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001351
H. Peter Anvin839eca22007-10-29 23:12:47 -07001352 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001353 c = *codes++;
1354 op1 = (c & 3) + ((opex & 1) << 2);
1355 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1356 opx = &ins->oprs[op1];
1357 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001358
H. Peter Anvin839eca22007-10-29 23:12:47 -07001359 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 case 01:
1361 case 02:
1362 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001364 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001365 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 codes += c;
1367 offset += c;
1368 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001369
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 case 05:
1371 case 06:
1372 case 07:
1373 opex = c;
1374 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001377 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001378 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001379 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 offset += 1;
1381 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001382
Jin Kyu Song164d6072013-10-15 19:10:13 -07001383 case4(014):
1384 break;
1385
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001387 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001388 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001389 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001391 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 offset += 1;
1393 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001394
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001396 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001397 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001398 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001399 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 offset += 1;
1401 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001402
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001403 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001404 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001405 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001406 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001407 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 offset += 2;
1409 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001411 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001412 if (opx->type & (BITS16 | BITS32))
1413 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 else
1415 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001416 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001417 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001418 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001419 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 offset += size;
1421 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001423 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001424 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001425 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001426 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001427 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 offset += 4;
1429 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001430
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001431 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001432 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001433 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001434 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001435 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001436 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 offset += size;
1438 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001439
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001440 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001441 if (opx->segment != segment) {
1442 data = opx->offset;
1443 out(offset, segment, &data,
1444 OUT_REL1ADR, insn_end - offset,
1445 opx->segment, opx->wrt);
1446 } else {
1447 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 if (data > 127 || data < -128)
1449 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001450 out(offset, segment, &data,
1451 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1452 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 offset += 1;
1454 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001455
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001458 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001459 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001460 offset += 8;
1461 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001463 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001464 if (opx->segment != segment) {
1465 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001467 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001468 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001470 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001472 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 }
1474 offset += 2;
1475 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001476
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001477 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001478 if (opx->type & (BITS16 | BITS32 | BITS64))
1479 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 else
1481 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001482 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001483 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001484 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1486 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001488 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001490 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 }
1492 offset += size;
1493 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001494
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001495 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001496 if (opx->segment != segment) {
1497 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001499 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001502 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001504 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 }
1506 offset += 4;
1507 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001508
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001509 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001510 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001511 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1512 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001513 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001514 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001515 outfmt->segbase(1 + opx->segment),
1516 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001517 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 case 0172:
1521 c = *codes++;
1522 opx = &ins->oprs[c >> 3];
1523 bytes[0] = nasm_regvals[opx->basereg] << 4;
1524 opx = &ins->oprs[c & 7];
1525 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1526 errfunc(ERR_NONFATAL,
1527 "non-absolute expression not permitted as argument %d",
1528 c & 7);
1529 } else {
1530 if (opx->offset & ~15) {
1531 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1532 "four-bit argument exceeds bounds");
1533 }
1534 bytes[0] |= opx->offset & 15;
1535 }
1536 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1537 offset++;
1538 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001539
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001540 case 0173:
1541 c = *codes++;
1542 opx = &ins->oprs[c >> 4];
1543 bytes[0] = nasm_regvals[opx->basereg] << 4;
1544 bytes[0] |= c & 15;
1545 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1546 offset++;
1547 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001548
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001549 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001550 bytes[0] = nasm_regvals[opx->basereg] << 4;
1551 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1552 offset++;
1553 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001554
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001556 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1558 (int32_t)data != (int64_t)data) {
1559 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1560 "signed dword immediate exceeds bounds");
1561 }
1562 out(offset, segment, &data, OUT_ADDRESS, 4,
1563 opx->segment, opx->wrt);
1564 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001565 break;
1566
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001567 case4(0240):
1568 case 0250:
1569 codes += 3;
1570 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1571 EVEX_P2Z | EVEX_P2AAA, 2);
1572 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1573 bytes[0] = 0x62;
1574 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001575 bytes[1] = ((((ins->rex & 7) << 5) |
1576 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1577 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001578 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1579 ((~ins->vexreg & 15) << 3) |
1580 (1 << 2) | (ins->vex_wlp & 3);
1581 bytes[3] = ins->evex_p[2];
1582 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1583 offset += 4;
1584 break;
1585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(0260):
1587 case 0270:
1588 codes += 2;
1589 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1590 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1591 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1592 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001593 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1595 offset += 3;
1596 } else {
1597 bytes[0] = 0xc5;
1598 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001599 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1601 offset += 2;
1602 }
1603 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001604
H. Peter Anvine014f352012-02-25 22:35:19 -08001605 case 0271:
1606 case 0272:
1607 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001608 break;
1609
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 case4(0274):
1611 {
1612 uint64_t uv, um;
1613 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 if (ins->rex & REX_W)
1616 s = 64;
1617 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1618 s = 16;
1619 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1620 s = 32;
1621 else
1622 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 um = (uint64_t)2 << (s-1);
1625 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001626
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001627 if (uv > 127 && uv < (uint64_t)-128 &&
1628 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001629 /* If this wasn't explicitly byte-sized, warn as though we
1630 * had fallen through to the imm16/32/64 case.
1631 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001632 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001633 "%s value exceeds bounds",
1634 (opx->type & BITS8) ? "signed byte" :
1635 s == 16 ? "word" :
1636 s == 32 ? "dword" :
1637 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001639 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001640 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001641 out(offset, segment, &data, OUT_ADDRESS, 1,
1642 opx->segment, opx->wrt);
1643 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001644 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001645 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1646 NO_SEG);
1647 }
1648 offset += 1;
1649 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001654
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001656 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001658 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 offset += 1;
1660 } else
1661 offset += 0;
1662 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001663
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001665 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001667 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 offset += 1;
1669 } else
1670 offset += 0;
1671 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001672
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 case 0312:
1674 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675
Keith Kaniosb7a89542007-04-12 02:40:54 +00001676 case 0313:
1677 ins->rex = 0;
1678 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001679
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001680 case4(0314):
1681 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001688 case 0323:
1689 break;
1690
Keith Kaniosb7a89542007-04-12 02:40:54 +00001691 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001692 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001694
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001695 case 0325:
1696 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001697
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001698 case 0326:
1699 break;
1700
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001702 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001703 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 offset += 1;
1705 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001709
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001710 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001712 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001713 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 offset += 1;
1715 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001716
Keith Kanios48af1772007-08-17 07:37:52 +00001717 case 0334:
1718 if (ins->rex & REX_R) {
1719 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001720 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001721 offset += 1;
1722 }
1723 ins->rex &= ~(REX_L|REX_R);
1724 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001725
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001726 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001728
H. Peter Anvin962e3052008-08-28 17:47:16 -07001729 case 0336:
1730 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001731 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001732
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 if (ins->oprs[0].segment != NO_SEG)
1735 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1736 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001737 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 if (size > 0)
1739 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001740 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 offset += size;
1742 }
1743 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001744
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001745 case 0341:
1746 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001747
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 case 0360:
1749 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001750
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 case 0361:
1752 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001753 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1754 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001756
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001757 case 0364:
1758 case 0365:
1759 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001760
Keith Kanios48af1772007-08-17 07:37:52 +00001761 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001763 *bytes = c - 0366 + 0x66;
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 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001767
Jin Kyu Song03041092013-10-15 19:38:51 -07001768 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 case 0373:
1772 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001773 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 offset += 1;
1775 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001776
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001777 case 0374:
1778 eat = EA_XMMVSIB;
1779 break;
1780
1781 case 0375:
1782 eat = EA_YMMVSIB;
1783 break;
1784
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001785 case 0376:
1786 eat = EA_ZMMVSIB;
1787 break;
1788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case4(0100):
1790 case4(0110):
1791 case4(0120):
1792 case4(0130):
1793 case4(0200):
1794 case4(0204):
1795 case4(0210):
1796 case4(0214):
1797 case4(0220):
1798 case4(0224):
1799 case4(0230):
1800 case4(0234):
1801 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 ea ea_data;
1803 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001804 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001805 uint8_t *p;
1806 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001808
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001809 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001810 /* pick rfield from operand b (opx) */
1811 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001812 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 } else {
1814 /* rfield is constant */
1815 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001819 if (process_ea(opy, &ea_data, bits,
1820 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 p = bytes;
1824 *p++ = ea_data.modrm;
1825 if (ea_data.sib_present)
1826 *p++ = ea_data.sib;
1827
1828 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001829 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001831 /*
1832 * Make sure the address gets the right offset in case
1833 * the line breaks in the .lst file (BR 1197827)
1834 */
1835 offset += s;
1836 s = 0;
1837
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 switch (ea_data.bytes) {
1839 case 0:
1840 break;
1841 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 case 2:
1843 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001844 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001845 /* use compressed displacement, if available */
1846 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 if (ea_data.rip) {
1849 if (opy->segment == segment) {
1850 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001851 if (overflow_signed(data, ea_data.bytes))
1852 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 out(offset, segment, &data, OUT_ADDRESS,
1854 ea_data.bytes, NO_SEG, NO_SEG);
1855 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001856 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 out(offset, segment, &data, OUT_REL4ADR,
1858 insn_end - offset, opy->segment, opy->wrt);
1859 }
1860 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001861 if (overflow_general(data, ins->addr_size >> 3) ||
1862 signed_bits(data, ins->addr_size) !=
1863 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001864 warn_overflow(ERR_PASS2, ea_data.bytes);
1865
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 out(offset, segment, &data, OUT_ADDRESS,
1867 ea_data.bytes, opy->segment, opy->wrt);
1868 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001870 default:
1871 /* Impossible! */
1872 errfunc(ERR_PANIC,
1873 "Invalid amount of bytes (%d) for offset?!",
1874 ea_data.bytes);
1875 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 }
1877 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 }
1879 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001880
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 default:
1882 errfunc(ERR_PANIC, "internal instruction table corrupt"
1883 ": instruction code \\%o (0x%02X) given", c, c);
1884 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001886 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887}
1888
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001889static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001890{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001891 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001892 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001893 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001894}
1895
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001896static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001897{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001898 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001900 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001901}
1902
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001903static int op_rexflags(const operand * o, int mask)
1904{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001905 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001906 int val;
1907
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001908 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001909 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001910
H. Peter Anvina4835d42008-05-20 14:21:29 -07001911 flags = nasm_reg_flags[o->basereg];
1912 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001913
1914 return rexflags(val, flags, mask);
1915}
1916
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001917static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001918{
1919 int rex = 0;
1920
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001921 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001923 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 rex |= REX_W;
1925 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1926 rex |= REX_H;
1927 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1928 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001929
1930 return rex & mask;
1931}
1932
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001933static int evexflags(int val, decoflags_t deco,
1934 int mask, uint8_t byte)
1935{
1936 int evex = 0;
1937
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001938 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001939 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001940 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001941 evex |= (EVEX_P0RP | EVEX_P0X);
1942 break;
1943 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001944 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001945 evex |= EVEX_P2VP;
1946 if (deco & Z)
1947 evex |= EVEX_P2Z;
1948 if (deco & OPMASK_MASK)
1949 evex |= deco & EVEX_P2AAA;
1950 break;
1951 }
1952 return evex & mask;
1953}
1954
1955static int op_evexflags(const operand * o, int mask, uint8_t byte)
1956{
1957 int val;
1958
1959 if (!is_register(o->basereg))
1960 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1961
1962 val = nasm_regvals[o->basereg];
1963
1964 return evexflags(val, o->decoflags, mask, byte);
1965}
1966
H. Peter Anvin23595f52009-07-25 17:44:25 -07001967static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 insn *instruction,
1969 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001970{
1971 const struct itemplate *temp;
1972 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001973 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001974 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001975 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001976 int i;
1977
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001978 /* broadcasting uses a different data element size */
1979 for (i = 0; i < instruction->operands; i++)
1980 if (i == broadcast)
1981 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1982 else
1983 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001984
1985 merr = MERR_INVALOP;
1986
1987 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001988 temp->opcode != I_none; temp++) {
1989 m = matches(temp, instruction, bits);
1990 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001991 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001992 m = MOK_GOOD;
1993 else
1994 m = MERR_INVALOP;
1995 } else if (m == MERR_OPSIZEMISSING &&
1996 (temp->flags & IF_SMASK) != IF_SX) {
1997 /*
1998 * Missing operand size and a candidate for fuzzy matching...
1999 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002000 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002001 if (i == broadcast)
2002 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2003 else
2004 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 opsizemissing = true;
2006 }
2007 if (m > merr)
2008 merr = m;
2009 if (merr == MOK_GOOD)
2010 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002011 }
2012
2013 /* No match, but see if we can get a fuzzy operand size match... */
2014 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002016
2017 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 /*
2019 * We ignore extrinsic operand sizes on registers, so we should
2020 * never try to fuzzy-match on them. This also resolves the case
2021 * when we have e.g. "xmmrm128" in two different positions.
2022 */
2023 if (is_class(REGISTER, instruction->oprs[i].type))
2024 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002026 /* This tests if xsizeflags[i] has more than one bit set */
2027 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2028 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002029
Jin Kyu Song7903c072013-10-30 03:00:12 -07002030 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002031 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002032 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2033 BITS32 : BITS64);
2034 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002035 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002036 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002037 }
2038
2039 /* Try matching again... */
2040 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002041 temp->opcode != I_none; temp++) {
2042 m = matches(temp, instruction, bits);
2043 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002044 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002045 m = MOK_GOOD;
2046 else
2047 m = MERR_INVALOP;
2048 }
2049 if (m > merr)
2050 merr = m;
2051 if (merr == MOK_GOOD)
2052 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002053 }
2054
H. Peter Anvina81655b2009-07-25 18:15:28 -07002055done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002056 *tempp = temp;
2057 return merr;
2058}
2059
H. Peter Anvin65289e82009-07-25 17:25:11 -07002060static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002061 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002062{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002063 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002064 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002065 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002066
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002067 /*
2068 * Check the opcode
2069 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002071 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002072
2073 /*
2074 * Count the operands
2075 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002077 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002078
2079 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002080 * Is it legal?
2081 */
2082 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2083 return MERR_INVALOP;
2084
2085 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002086 * Check that no spurious colons or TOs are present
2087 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 for (i = 0; i < itemp->operands; i++)
2089 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002090 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002091
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002092 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002093 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002094 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002095 switch (itemp->flags & IF_SMASK) {
2096 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002097 asize = BITS8;
2098 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002099 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002100 asize = BITS16;
2101 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002102 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002103 asize = BITS32;
2104 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002105 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002106 asize = BITS64;
2107 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002108 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002109 asize = BITS128;
2110 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002111 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002112 asize = BITS256;
2113 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002114 case IF_SZ:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002115 asize = BITS512;
2116 break;
2117 case IF_SIZE:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002118 switch (bits) {
2119 case 16:
2120 asize = BITS16;
2121 break;
2122 case 32:
2123 asize = BITS32;
2124 break;
2125 case 64:
2126 asize = BITS64;
2127 break;
2128 default:
2129 asize = 0;
2130 break;
2131 }
2132 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002133 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002134 asize = 0;
2135 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002136 }
2137
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002138 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002139 /* S- flags only apply to a specific operand */
2140 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2141 memset(size, 0, sizeof size);
2142 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002143 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002144 /* S- flags apply to all operands */
2145 for (i = 0; i < MAX_OPERANDS; i++)
2146 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002147 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002148
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002149 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002150 * Check that the operand flags all match up,
2151 * it's a bit tricky so lets be verbose:
2152 *
2153 * 1) Find out the size of operand. If instruction
2154 * doesn't have one specified -- we're trying to
2155 * guess it either from template (IF_S* flag) or
2156 * from code bits.
2157 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002158 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002159 * template has an operand size specified AND this size differ
2160 * from which instruction has (perhaps we got it from code bits)
2161 * we are:
2162 * a) Check that only size of instruction and operand is differ
2163 * other characteristics do match
2164 * b) Perhaps it's a register specified in instruction so
2165 * for such a case we just mark that operand as "size
2166 * missing" and this will turn on fuzzy operand size
2167 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002168 */
2169 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002170 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002171 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002172 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002173 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002174 opflags_t template_opsize, insn_opsize;
2175
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002176 if (!(type & SIZE_MASK))
2177 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002178
Jin Kyu Song7903c072013-10-30 03:00:12 -07002179 insn_opsize = type & SIZE_MASK;
2180 if (!is_broadcast) {
2181 template_opsize = itemp->opd[i] & SIZE_MASK;
2182 } else {
2183 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2184 /*
2185 * when broadcasting, the element size depends on
2186 * the instruction type. decorator flag should match.
2187 */
2188
2189 if (deco_brsize) {
2190 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002191 /* calculate the proper number : {1to<brcast_num>} */
2192 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2193 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002194 } else {
2195 template_opsize = 0;
2196 }
2197 }
2198
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002199 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002200 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002201 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002202 } else if (template_opsize) {
2203 if (template_opsize != insn_opsize) {
2204 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002205 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002206 } else if (!is_class(REGISTER, type)) {
2207 /*
2208 * Note: we don't honor extrinsic operand sizes for registers,
2209 * so "missing operand size" for a register should be
2210 * considered a wildcard match rather than an error.
2211 */
2212 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002213 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002214 } else if (is_broadcast &&
2215 (brcast_num !=
2216 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2217 /*
2218 * broadcasting opsize matches but the number of repeated memory
2219 * element does not match.
2220 * if 64b double precision float is broadcasted to zmm (512b),
2221 * broadcasting decorator must be {1to8}.
2222 */
2223 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 }
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002225 } else if (is_register(instruction->oprs[i].basereg) &&
2226 nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
Jin Kyu Songdd1c0c12013-09-13 14:12:56 -07002227 !(itemp->flags & IF_AVX512)) {
Jin Kyu Song66c61922013-08-26 20:28:43 -07002228 return MERR_ENCMISMATCH;
Jin Kyu Song945b1b82013-10-25 19:29:53 -07002229 } else if (instruction->prefixes[PPS_EVEX] &&
2230 !(itemp->flags & IF_AVX512)) {
2231 return MERR_ENCMISMATCH;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002232 }
2233 }
2234
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002235 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002237
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002238 /*
2239 * Check operand sizes
2240 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002241 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002244 asize = itemp->opd[i] & SIZE_MASK;
2245 if (asize) {
2246 for (i = 0; i < oprs; i++)
2247 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 break;
2249 }
2250 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002251 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002253 }
2254
Keith Kaniosb7a89542007-04-12 02:40:54 +00002255 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 if (!(itemp->opd[i] & SIZE_MASK) &&
2257 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002258 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002259 }
2260
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002261 /*
2262 * Check template is okay at the set cpu level
2263 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002264 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002265 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002266
Keith Kaniosb7a89542007-04-12 02:40:54 +00002267 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002268 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002269 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002270 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002271 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002273 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002274 * If we have a HLE prefix, look for the NOHLE flag
2275 */
2276 if ((itemp->flags & IF_NOHLE) &&
2277 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2278 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2279 return MERR_BADHLE;
2280
2281 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002282 * Check if special handling needed for Jumps
2283 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002284 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002285 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286
Jin Kyu Song03041092013-10-15 19:38:51 -07002287 /*
2288 * Check if BND prefix is allowed
2289 */
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08002290 if ((IF_BND & ~itemp->flags) &&
Jin Kyu Song03041092013-10-15 19:38:51 -07002291 has_prefix(instruction, PPS_REP, P_BND))
2292 return MERR_BADBND;
2293
H. Peter Anvin60926242009-07-26 16:25:38 -07002294 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002295}
2296
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002297/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002298 * Check if ModR/M.mod should/can be 01.
2299 * - EAF_BYTEOFFS is set
2300 * - offset can fit in a byte when EVEX is not used
2301 * - offset can be compressed when EVEX is used
2302 */
2303#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2304 (o >= -128 && o <= 127 && \
2305 seg == NO_SEG && !forw_ref && \
2306 !(input->eaflags & EAF_WORDOFFS) && \
2307 !(ins->rex & REX_EV)) || \
2308 (ins->rex & REX_EV && \
2309 is_disp8n(input, ins, &output->disp8)))
2310
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002311static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002312 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002313{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002314 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002315 int addrbits = ins->addr_size;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002316
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002317 output->type = EA_SCALAR;
2318 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002319 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002320
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002321 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002322 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002323 /* EVEX.R' flag for the REG operand */
2324 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002325
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002326 if (is_class(REGISTER, input->type)) {
2327 /*
2328 * It's a direct register.
2329 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002330 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002331 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002332
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002333 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002334 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002335
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002336 /* broadcasting is not available with a direct register operand. */
2337 if (input->decoflags & BRDCAST_MASK) {
2338 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2339 goto err;
2340 }
2341
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002342 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002343 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002344 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002345 output->bytes = 0; /* no offset necessary either */
2346 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2347 } else {
2348 /*
2349 * It's a memory reference.
2350 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002351
2352 /* Embedded rounding or SAE is not available with a mem ref operand. */
2353 if (input->decoflags & (ER | SAE)) {
2354 nasm_error(ERR_NONFATAL,
2355 "Embedded rounding is available only with reg-reg op.");
2356 return -1;
2357 }
2358
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002359 if (input->basereg == -1 &&
2360 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002361 /*
2362 * It's a pure offset.
2363 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002364 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2365 input->segment == NO_SEG) {
2366 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2367 input->type &= ~IP_REL;
2368 input->type |= MEMORY;
2369 }
2370
2371 if (input->eaflags & EAF_BYTEOFFS ||
2372 (input->eaflags & EAF_WORDOFFS &&
2373 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2374 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2375 }
2376
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002377 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002378 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002379 output->sib = GEN_SIB(0, 4, 5);
2380 output->bytes = 4;
2381 output->modrm = GEN_MODRM(0, rfield, 4);
2382 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002383 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002384 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 output->bytes = (addrbits != 16 ? 4 : 2);
2386 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2387 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002388 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002389 } else {
2390 /*
2391 * It's an indirection.
2392 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002394 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002396 int t, it, bt; /* register numbers */
2397 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 if (s == 0)
2400 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002401
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002402 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002403 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002404 ix = nasm_reg_flags[i];
2405 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002406 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002407 ix = 0;
2408 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002409
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002410 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002411 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002412 bx = nasm_reg_flags[b];
2413 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002414 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002415 bx = 0;
2416 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002417
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002418 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002419 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002420 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002421 int32_t o = input->offset;
2422 int mod, scale, index, base;
2423
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002424 /*
2425 * For a vector SIB, one has to be a vector and the other,
2426 * if present, a GPR. The vector must be the index operand.
2427 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002428 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002429 if (s == 0)
2430 s = 1;
2431 else if (s != 1)
2432 goto err;
2433
2434 t = bt, bt = it, it = t;
2435 x = bx, bx = ix, ix = x;
2436 }
2437
2438 if (bt != -1) {
2439 if (REG_GPR & ~bx)
2440 goto err;
2441 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2442 sok &= bx;
2443 else
2444 goto err;
2445 }
2446
2447 /*
2448 * While we're here, ensure the user didn't specify
2449 * WORD or QWORD
2450 */
2451 if (input->disp_size == 16 || input->disp_size == 64)
2452 goto err;
2453
2454 if (addrbits == 16 ||
2455 (addrbits == 32 && !(sok & BITS32)) ||
2456 (addrbits == 64 && !(sok & BITS64)))
2457 goto err;
2458
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2460 : ((ix & YMMREG & ~REG_EA)
2461 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002462
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002463 output->rex |= rexflags(it, ix, REX_X);
2464 output->rex |= rexflags(bt, bx, REX_B);
2465 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002466
2467 index = it & 7; /* it is known to be != -1 */
2468
2469 switch (s) {
2470 case 1:
2471 scale = 0;
2472 break;
2473 case 2:
2474 scale = 1;
2475 break;
2476 case 4:
2477 scale = 2;
2478 break;
2479 case 8:
2480 scale = 3;
2481 break;
2482 default: /* then what the smeg is it? */
2483 goto err; /* panic */
2484 }
2485
2486 if (bt == -1) {
2487 base = 5;
2488 mod = 0;
2489 } else {
2490 base = (bt & 7);
2491 if (base != REG_NUM_EBP && o == 0 &&
2492 seg == NO_SEG && !forw_ref &&
2493 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2494 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002495 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002496 mod = 1;
2497 else
2498 mod = 2;
2499 }
2500
2501 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002502 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2503 output->modrm = GEN_MODRM(mod, rfield, 4);
2504 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002505 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002506 /*
2507 * it must be a 32/64-bit memory reference. Firstly we have
2508 * to check that all registers involved are type E/Rxx.
2509 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002510 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002511 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002512
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002513 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002514 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2515 sok &= ix;
2516 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002517 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002518 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002520 if (bt != -1) {
2521 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002522 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002524 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 sok &= bx;
2526 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002528 /*
2529 * While we're here, ensure the user didn't specify
2530 * WORD or QWORD
2531 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002532 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002534
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002535 if (addrbits == 16 ||
2536 (addrbits == 32 && !(sok & BITS32)) ||
2537 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002538 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002539
Keith Kaniosb7a89542007-04-12 02:40:54 +00002540 /* now reorganize base/index */
2541 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 ((hb == b && ht == EAH_NOTBASE) ||
2543 (hb == i && ht == EAH_MAKEBASE))) {
2544 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002545 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 x = bx, bx = ix, ix = x;
2547 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002548 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002549 bt = -1, bx = 0, s++;
Jin Kyu Song164d6072013-10-15 19:10:13 -07002550 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002551 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002552 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 }
2554 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2555 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002556 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002557 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2558 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002559 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002561 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002562 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002563 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002564 x = ix, ix = bx, bx = x;
2565 }
2566 if (it == REG_NUM_ESP ||
2567 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002568 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002569
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002570 output->rex |= rexflags(it, ix, REX_X);
2571 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002572
Keith Kanios48af1772007-08-17 07:37:52 +00002573 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002574 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002576
Keith Kaniosb7a89542007-04-12 02:40:54 +00002577 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002580 } else {
2581 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002582 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002583 seg == NO_SEG && !forw_ref &&
2584 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002585 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002586 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002587 mod = 1;
2588 else
2589 mod = 2;
2590 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002591
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002592 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002593 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2594 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002595 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002596 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002598
Keith Kaniosb7a89542007-04-12 02:40:54 +00002599 if (it == -1)
2600 index = 4, s = 1;
2601 else
2602 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 switch (s) {
2605 case 1:
2606 scale = 0;
2607 break;
2608 case 2:
2609 scale = 1;
2610 break;
2611 case 4:
2612 scale = 2;
2613 break;
2614 case 8:
2615 scale = 3;
2616 break;
2617 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002618 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002620
Keith Kaniosb7a89542007-04-12 02:40:54 +00002621 if (bt == -1) {
2622 base = 5;
2623 mod = 0;
2624 } else {
2625 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002626 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002627 seg == NO_SEG && !forw_ref &&
2628 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002629 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002630 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002631 mod = 1;
2632 else
2633 mod = 2;
2634 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002635
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002636 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002637 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2638 output->modrm = GEN_MODRM(mod, rfield, 4);
2639 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 }
2641 } else { /* it's 16-bit */
2642 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002643 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002644
Keith Kaniosb7a89542007-04-12 02:40:54 +00002645 /* check for 64-bit long mode */
2646 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002647 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2651 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002653
Keith Kaniosb7a89542007-04-12 02:40:54 +00002654 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002655 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002657
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 if (b == -1 && i != -1) {
2661 int tmp = b;
2662 b = i;
2663 i = tmp;
2664 } /* swap */
2665 if ((b == R_SI || b == R_DI) && i != -1) {
2666 int tmp = b;
2667 b = i;
2668 i = tmp;
2669 }
2670 /* have BX/BP as base, SI/DI index */
2671 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002672 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 if (i != -1 && b != -1 &&
2674 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002675 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002677 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002678
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 rm = -1;
2680 if (i != -1)
2681 switch (i * 256 + b) {
2682 case R_SI * 256 + R_BX:
2683 rm = 0;
2684 break;
2685 case R_DI * 256 + R_BX:
2686 rm = 1;
2687 break;
2688 case R_SI * 256 + R_BP:
2689 rm = 2;
2690 break;
2691 case R_DI * 256 + R_BP:
2692 rm = 3;
2693 break;
2694 } else
2695 switch (b) {
2696 case R_SI:
2697 rm = 4;
2698 break;
2699 case R_DI:
2700 rm = 5;
2701 break;
2702 case R_BP:
2703 rm = 6;
2704 break;
2705 case R_BX:
2706 rm = 7;
2707 break;
2708 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002709 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002710 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002711
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002712 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2713 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002715 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 mod = 1;
2717 else
2718 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002719
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002720 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002721 output->bytes = mod; /* bytes of offset needed */
2722 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 }
2724 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002725 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002726
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002727 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002728 return output->type;
2729
2730err:
2731 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002732}
2733
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002734static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002735{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002736 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002737 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002738
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002739 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002740
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002741 switch (ins->prefixes[PPS_ASIZE]) {
2742 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002743 valid &= 16;
2744 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002745 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002746 valid &= 32;
2747 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002748 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002749 valid &= 64;
2750 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002751 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 valid &= (addrbits == 32) ? 16 : 32;
2753 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002754 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002756 }
2757
2758 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002759 if (is_class(MEMORY, ins->oprs[j].type)) {
2760 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002761
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002762 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002763 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 i = 0;
2765 else
2766 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002767
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002768 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002769 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002770 b = 0;
2771 else
2772 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002773
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002774 if (ins->oprs[j].scale == 0)
2775 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002777 if (!i && !b) {
2778 int ds = ins->oprs[j].disp_size;
2779 if ((addrbits != 64 && ds > 8) ||
2780 (addrbits == 64 && ds == 16))
2781 valid &= ds;
2782 } else {
2783 if (!(REG16 & ~b))
2784 valid &= 16;
2785 if (!(REG32 & ~b))
2786 valid &= 32;
2787 if (!(REG64 & ~b))
2788 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002789
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002790 if (!(REG16 & ~i))
2791 valid &= 16;
2792 if (!(REG32 & ~i))
2793 valid &= 32;
2794 if (!(REG64 & ~i))
2795 valid &= 64;
2796 }
2797 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002798 }
2799
2800 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002801 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002802 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002803 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002804 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002805 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002806 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002807 /* Impossible... */
2808 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2809 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002810 }
2811
2812 defdisp = ins->addr_size == 16 ? 16 : 32;
2813
2814 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002815 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2816 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2817 /*
2818 * mem_offs sizes must match the address size; if not,
2819 * strip the MEM_OFFS bit and match only EA instructions
2820 */
2821 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2822 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002823 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002824}