blob: 7ba38693d76d5d978cf61de190591847ae4e521c [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07003 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800176#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000177#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000178
179#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000180#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181#include "assemble.h"
182#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700183#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800184#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvin65289e82009-07-25 17:25:11 -0700186enum match_result {
187 /*
188 * Matching errors. These should be sorted so that more specific
189 * errors come later in the sequence.
190 */
191 MERR_INVALOP,
192 MERR_OPSIZEMISSING,
193 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700194 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700195 MERR_BADCPU,
196 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800197 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700198 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700199 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800200 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 /*
202 * Matching success; the conditional ones first
203 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400204 MOK_JUMP, /* Matching OK but needs jmp_match() */
205 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700206};
207
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700209 enum ea_type type; /* what kind of EA is this? */
210 int sib_present; /* is a SIB byte necessary? */
211 int bytes; /* # of bytes of offset needed */
212 int size; /* lazy - this is sib+bytes+1 */
213 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700214 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000215} ea;
216
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400217#define GEN_SIB(scale, index, base) \
218 (((scale) << 6) | ((index) << 3) | ((base)))
219
220#define GEN_MODRM(mod, reg, rm) \
221 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
222
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400223static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224static efunc errfunc;
225static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000226static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000227
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800228static int64_t calcsize(int32_t, int64_t, int, insn *,
229 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700230static void gencode(int32_t segment, int64_t offset, int bits,
231 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400232 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700233static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400234 insn *instruction,
235 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700236static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700237static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000238static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700239static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000240static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700241static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700242static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000243
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700245
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400246static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000247{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700248 return ins->prefixes[pos] == prefix;
249}
250
251static void assert_no_prefix(insn * ins, enum prefix_pos pos)
252{
253 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400254 errfunc(ERR_NONFATAL, "invalid %s prefix",
255 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256}
257
258static const char *size_name(int size)
259{
260 switch (size) {
261 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400262 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700273 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700275 case 64:
276 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000279 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700280}
281
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400282static void warn_overflow(int pass, int size)
283{
284 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
285 "%s data exceeds bounds", size_name(size));
286}
287
288static void warn_overflow_const(int64_t data, int size)
289{
290 if (overflow_general(data, size))
291 warn_overflow(ERR_PASS1, size);
292}
293
294static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700295{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100296 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400297 if (overflow_general(o->offset, size))
298 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700299 }
300}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000302/*
303 * This routine wrappers the real output format's output routine,
304 * in order to pass a copy of the data off to the listing file
305 * generator at the same time.
306 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800307static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800308 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400309 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000311 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000312 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800313 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000314
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800315 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400316 /*
317 * This is a non-relocated address, and we're going to
318 * convert it into RAWDATA format.
319 */
320 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800321
322 size = abs((int)size);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400323 if (size > 8) {
324 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
325 return;
326 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700327
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400328 WRITEADDR(q, *(int64_t *)data, size);
329 data = p;
330 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 }
332
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800333 list->output(offset, data, type, size);
334
Frank Kotlerabebb082003-09-06 04:45:37 +0000335 /*
336 * this call to src_get determines when we call the
337 * debug-format-specific "linenum" function
338 * it updates lineno and lnfname to the current values
339 * returning 0 if "same as last time", -2 if lnfname
340 * changed, and the amount by which lineno changed,
341 * if it did. thus, these variables must be static
342 */
343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400344 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800347 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000348}
349
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800350static void out_imm8(int64_t offset, int32_t segment,
351 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400352{
353 if (opx->segment != NO_SEG) {
354 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800355 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400356 } else {
357 uint8_t byte = opx->offset;
358 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
359 }
360}
361
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700362static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800363 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800365 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800366 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000367 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800368 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000369
H. Peter Anvin755f5212012-02-25 11:41:34 -0800370 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700371 return false;
372 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400373 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700374 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400375 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700376
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800377 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100378
Victor van den Elzen154e5922009-02-25 17:32:00 +0100379 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100380 /* Be optimistic in pass 1 */
381 return true;
382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700384 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000385
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700386 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800387 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
388
389 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
390 /* jmp short (opcode eb) cannot be used with bnd prefix. */
391 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800392 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
393 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800394 }
395
396 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000398
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400399int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400400 insn * instruction, struct ofmt *output, efunc error,
401 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000403 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700405 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800406 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000407 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800408 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300409 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000412 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 outfmt = output; /* likewise */
414 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000415
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300416 wsize = idata_bytes(instruction->opcode);
417 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419
H. Peter Anvineba20a72002-04-30 20:53:55 +0000420 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000422 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 if (t < 0)
424 errfunc(ERR_PANIC,
425 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400428 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400430 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700431 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400432 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000433 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700434 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800436 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400437 offset += wsize;
438 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700439 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400440 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800444 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000446
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 if (align) {
448 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100449 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800450 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 }
452 offset += e->stringlen + align;
453 }
454 }
455 if (t > 0 && t == instruction->times - 1) {
456 /*
457 * Dummy call to list->output to give the offset to the
458 * listing module.
459 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800460 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 list->uplevel(LIST_TIMES);
462 }
463 }
464 if (instruction->times > 1)
465 list->downlevel(LIST_TIMES);
466 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467 }
468
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700470 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400473 fp = fopen(fname, "rb");
474 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
476 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400477 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
479 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200480 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400481 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700482 static char buf[4096];
483 size_t t = instruction->times;
484 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400485 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 len = ftell(fp);
488 if (instruction->eops->next) {
489 base = instruction->eops->next->offset;
490 len -= base;
491 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700492 len > (size_t)instruction->eops->next->next->offset)
493 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 }
495 /*
496 * Dummy call to list->output to give the offset to the
497 * listing module.
498 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800499 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 list->uplevel(LIST_INCBIN);
501 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700502 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 fseek(fp, base, SEEK_SET);
505 l = len;
506 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700507 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400508 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 if (!m) {
510 /*
511 * This shouldn't happen unless the file
512 * actually changes while we are reading
513 * it.
514 */
515 error(ERR_NONFATAL,
516 "`incbin': unexpected EOF while"
517 " reading file `%s'", fname);
518 t = 0; /* Try to exit cleanly */
519 break;
520 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800521 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 NO_SEG, NO_SEG);
523 l -= m;
524 }
525 }
526 list->downlevel(LIST_INCBIN);
527 if (instruction->times > 1) {
528 /*
529 * Dummy call to list->output to give the offset to the
530 * listing module.
531 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800532 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 list->uplevel(LIST_TIMES);
534 list->downlevel(LIST_TIMES);
535 }
536 fclose(fp);
537 return instruction->times * len;
538 }
539 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000540 }
541
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700542 /* Check to see if we need an address-size prefix */
543 add_asp(instruction, bits);
544
H. Peter Anvin23595f52009-07-25 17:44:25 -0700545 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700546
H. Peter Anvin23595f52009-07-25 17:44:25 -0700547 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400548 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800549 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400550 itimes = instruction->times;
551 if (insn_size < 0) /* shouldn't be, on pass two */
552 error(ERR_PANIC, "errors made it through from pass one");
553 else
554 while (itimes--) {
555 for (j = 0; j < MAXPREFIX; j++) {
556 uint8_t c = 0;
557 switch (instruction->prefixes[j]) {
558 case P_WAIT:
559 c = 0x9B;
560 break;
561 case P_LOCK:
562 c = 0xF0;
563 break;
564 case P_REPNE:
565 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800566 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700567 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400568 c = 0xF2;
569 break;
570 case P_REPE:
571 case P_REPZ:
572 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800573 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400574 c = 0xF3;
575 break;
576 case R_CS:
577 if (bits == 64) {
578 error(ERR_WARNING | ERR_PASS2,
579 "cs segment base generated, but will be ignored in 64-bit mode");
580 }
581 c = 0x2E;
582 break;
583 case R_DS:
584 if (bits == 64) {
585 error(ERR_WARNING | ERR_PASS2,
586 "ds segment base generated, but will be ignored in 64-bit mode");
587 }
588 c = 0x3E;
589 break;
590 case R_ES:
591 if (bits == 64) {
592 error(ERR_WARNING | ERR_PASS2,
593 "es segment base generated, but will be ignored in 64-bit mode");
594 }
595 c = 0x26;
596 break;
597 case R_FS:
598 c = 0x64;
599 break;
600 case R_GS:
601 c = 0x65;
602 break;
603 case R_SS:
604 if (bits == 64) {
605 error(ERR_WARNING | ERR_PASS2,
606 "ss segment base generated, but will be ignored in 64-bit mode");
607 }
608 c = 0x36;
609 break;
610 case R_SEGR6:
611 case R_SEGR7:
612 error(ERR_NONFATAL,
613 "segr6 and segr7 cannot be used as prefixes");
614 break;
615 case P_A16:
616 if (bits == 64) {
617 error(ERR_NONFATAL,
618 "16-bit addressing is not supported "
619 "in 64-bit mode");
620 } else if (bits != 16)
621 c = 0x67;
622 break;
623 case P_A32:
624 if (bits != 32)
625 c = 0x67;
626 break;
627 case P_A64:
628 if (bits != 64) {
629 error(ERR_NONFATAL,
630 "64-bit addressing is only supported "
631 "in 64-bit mode");
632 }
633 break;
634 case P_ASP:
635 c = 0x67;
636 break;
637 case P_O16:
638 if (bits != 16)
639 c = 0x66;
640 break;
641 case P_O32:
642 if (bits == 16)
643 c = 0x66;
644 break;
645 case P_O64:
646 /* REX.W */
647 break;
648 case P_OSP:
649 c = 0x66;
650 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700651 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800652 case P_VEX3:
653 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800654 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400655 case P_none:
656 break;
657 default:
658 error(ERR_PANIC, "invalid instruction prefix");
659 }
660 if (c != 0) {
661 out(offset, segment, &c, OUT_RAWDATA, 1,
662 NO_SEG, NO_SEG);
663 offset++;
664 }
665 }
666 insn_end = offset + insn_size;
667 gencode(segment, offset, bits, instruction,
668 temp, insn_end);
669 offset += insn_size;
670 if (itimes > 0 && itimes == instruction->times - 1) {
671 /*
672 * Dummy call to list->output to give the offset to the
673 * listing module.
674 */
675 list->output(offset, NULL, OUT_RAWDATA, 0);
676 list->uplevel(LIST_TIMES);
677 }
678 }
679 if (instruction->times > 1)
680 list->downlevel(LIST_TIMES);
681 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700682 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400683 /* No match */
684 switch (m) {
685 case MERR_OPSIZEMISSING:
686 error(ERR_NONFATAL, "operation size not specified");
687 break;
688 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700691 case MERR_BRNUMMISMATCH:
692 error(ERR_NONFATAL,
693 "mismatch in the number of broadcasting elements");
694 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400695 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 break;
698 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800699 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400700 bits);
701 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800702 case MERR_ENCMISMATCH:
703 error(ERR_NONFATAL, "specific encoding scheme not available");
704 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800705 case MERR_BADBND:
706 error(ERR_NONFATAL, "bnd prefix is not allowed");
707 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800708 case MERR_BADREPNE:
709 error(ERR_NONFATAL, "%s prefix is not allowed",
710 (has_prefix(instruction, PPS_REP, P_REPNE) ?
711 "repne" : "repnz"));
712 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400713 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 error(ERR_NONFATAL,
715 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400716 break;
717 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718 }
719 return 0;
720}
721
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400722int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400723 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000725 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700726 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000729 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400731 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700734 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
735 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400736 instruction->opcode == I_DT || instruction->opcode == I_DO ||
737 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300739 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300742 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400744 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000745 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400748 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400750 warn_overflow_const(e->offset, wsize);
751 } else if (e->type == EOT_DB_STRING ||
752 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000754
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 align = (-osize) % wsize;
756 if (align < 0)
757 align += wsize;
758 isize += osize + align;
759 }
760 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000761 }
762
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400764 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300766 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700767 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400769 fp = fopen(fname, "rb");
770 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
772 fname);
773 else if (fseek(fp, 0L, SEEK_END) < 0)
774 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
775 fname);
776 else {
777 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 if (instruction->eops->next) {
779 len -= instruction->eops->next->offset;
780 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700781 len > (size_t)instruction->eops->next->next->offset) {
782 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 }
784 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300785 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300787 if (fp)
788 fclose(fp);
789 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000790 }
791
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700792 /* Check to see if we need an address-size prefix */
793 add_asp(instruction, bits);
794
H. Peter Anvin23595f52009-07-25 17:44:25 -0700795 m = find_match(&temp, instruction, segment, offset, bits);
796 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400797 /* we've matched an instruction. */
798 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400799 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100800
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800801 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400802 if (isize < 0)
803 return -1;
804 for (j = 0; j < MAXPREFIX; j++) {
805 switch (instruction->prefixes[j]) {
806 case P_A16:
807 if (bits != 16)
808 isize++;
809 break;
810 case P_A32:
811 if (bits != 32)
812 isize++;
813 break;
814 case P_O16:
815 if (bits != 16)
816 isize++;
817 break;
818 case P_O32:
819 if (bits == 16)
820 isize++;
821 break;
822 case P_A64:
823 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700824 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800825 case P_VEX3:
826 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800827 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400828 case P_none:
829 break;
830 default:
831 isize++;
832 break;
833 }
834 }
835 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700836 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400837 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000838 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000839}
840
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800841static void bad_hle_warn(const insn * ins, uint8_t hleok)
842{
843 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800844 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800845 static const enum whatwarn warn[2][4] =
846 {
847 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
848 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
849 };
850 unsigned int n;
851
852 n = (unsigned int)rep_pfx - P_XACQUIRE;
853 if (n > 1)
854 return; /* Not XACQUIRE/XRELEASE */
855
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800856 ww = warn[n][hleok];
857 if (!is_class(MEMORY, ins->oprs[0].type))
858 ww = w_inval; /* HLE requires operand 0 to be memory */
859
860 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800861 case w_none:
862 break;
863
864 case w_lock:
865 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800866 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800867 "%s with this instruction requires lock",
868 prefix_name(rep_pfx));
869 }
870 break;
871
872 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800873 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800874 "%s invalid with this instruction",
875 prefix_name(rep_pfx));
876 break;
877 }
878}
879
H. Peter Anvin507ae032008-10-09 15:37:10 -0700880/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400881#define case3(x) case (x): case (x)+1: case (x)+2
882#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700883
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800884static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000886{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800887 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800888 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000890 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700891 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700892 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700893 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700894 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800895 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800896 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700897 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000898
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700899 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700900 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700901 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700902
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700903 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700905
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 (void)segment; /* Don't warn that this parameter is unused */
907 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000908
H. Peter Anvin839eca22007-10-29 23:12:47 -0700909 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400910 c = *codes++;
911 op1 = (c & 3) + ((opex & 1) << 2);
912 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
913 opx = &ins->oprs[op1];
914 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700915
H. Peter Anvin839eca22007-10-29 23:12:47 -0700916 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400917 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 codes += c, length += c;
919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400921 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400922 opex = c;
923 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 case4(010):
926 ins->rex |=
927 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 codes++, length++;
929 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Jin Kyu Song164d6072013-10-15 19:10:13 -0700931 case4(014):
932 /* this is an index reg of MIB operand */
933 mib_index = opx->basereg;
934 break;
935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400936 case4(020):
937 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 length++;
939 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 length += 2;
943 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700944
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400945 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700946 if (opx->type & (BITS16 | BITS32 | BITS64))
947 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 else
949 length += (bits == 16) ? 2 : 4;
950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 length += 4;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400956 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700957 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 length++;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400964 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000965 length += 8; /* MOV reg64/imm */
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 length += 2;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700973 if (opx->type & (BITS16 | BITS32 | BITS64))
974 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 else
976 length += (bits == 16) ? 2 : 4;
977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 length += 4;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700984 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case 0172:
988 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700990 length++;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700993 case4(0174):
994 length++;
995 break;
996
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700997 case4(0240):
998 ins->rex |= REX_EV;
999 ins->vexreg = regval(opx);
1000 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1001 ins->vex_cm = *codes++;
1002 ins->vex_wlp = *codes++;
1003 ins->evex_tuple = (*codes++ - 0300);
1004 break;
1005
1006 case 0250:
1007 ins->rex |= REX_EV;
1008 ins->vexreg = 0;
1009 ins->vex_cm = *codes++;
1010 ins->vex_wlp = *codes++;
1011 ins->evex_tuple = (*codes++ - 0300);
1012 break;
1013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(0254):
1015 length += 4;
1016 break;
1017
1018 case4(0260):
1019 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001020 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 ins->vex_cm = *codes++;
1022 ins->vex_wlp = *codes++;
1023 break;
1024
1025 case 0270:
1026 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001027 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 ins->vex_cm = *codes++;
1029 ins->vex_wlp = *codes++;
1030 break;
1031
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001032 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001033 hleok = c & 3;
1034 break;
1035
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 case4(0274):
1037 length++;
1038 break;
1039
1040 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 if (bits == 64)
1045 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001046 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001050 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001054 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001055
Keith Kaniosb7a89542007-04-12 02:40:54 +00001056 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1058 has_prefix(ins, PPS_ASIZE, P_A32))
1059 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 case4(0314):
1063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001066 {
1067 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1068 if (pfx == P_O16)
1069 break;
1070 if (pfx != P_none)
1071 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1072 else
1073 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001075 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001076
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001078 {
1079 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1080 if (pfx == P_O32)
1081 break;
1082 if (pfx != P_none)
1083 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1084 else
1085 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001087 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 case 0322:
1090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
Keith Kaniosb7a89542007-04-12 02:40:54 +00001092 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001093 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Keith Kaniosb7a89542007-04-12 02:40:54 +00001096 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 case 0325:
1101 ins->rex |= REX_NH;
1102 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001103
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001104 case 0326:
1105 break;
1106
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0330:
1108 codes++, length++;
1109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001114 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 case 0333:
1116 length++;
1117 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 case 0334:
1120 ins->rex |= REX_L;
1121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001123 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001127 if (!ins->prefixes[PPS_REP])
1128 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001132 if (!ins->prefixes[PPS_REP])
1133 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 if (ins->oprs[0].segment != NO_SEG)
1138 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1139 " quantity of BSS space");
1140 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001141 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 case 0341:
1145 if (!ins->prefixes[PPS_WAIT])
1146 ins->prefixes[PPS_WAIT] = P_WAIT;
1147 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case 0360:
1150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001152 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001153 length++;
1154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0364:
1157 case 0365:
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Keith Kanios48af1772007-08-17 07:37:52 +00001160 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001161 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 length++;
1163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001165 case 0370:
1166 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 case 0373:
1170 length++;
1171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001173 case 0374:
1174 eat = EA_XMMVSIB;
1175 break;
1176
1177 case 0375:
1178 eat = EA_YMMVSIB;
1179 break;
1180
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001181 case 0376:
1182 eat = EA_ZMMVSIB;
1183 break;
1184
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 case4(0100):
1186 case4(0110):
1187 case4(0120):
1188 case4(0130):
1189 case4(0200):
1190 case4(0204):
1191 case4(0210):
1192 case4(0214):
1193 case4(0220):
1194 case4(0224):
1195 case4(0230):
1196 case4(0234):
1197 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001199 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 opflags_t rflags;
1201 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001202 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001203
Keith Kaniosb7a89542007-04-12 02:40:54 +00001204 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001205
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001206 if (c <= 0177) {
1207 /* pick rfield from operand b (opx) */
1208 rflags = regflag(opx);
1209 rfield = nasm_regvals[opx->basereg];
1210 } else {
1211 rflags = 0;
1212 rfield = c & 7;
1213 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001215 /* EVEX.b1 : evex_brerop contains the operand position */
1216 op_er_sae = (ins->evex_brerop >= 0 ?
1217 &ins->oprs[ins->evex_brerop] : NULL);
1218
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001219 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1220 /* set EVEX.b */
1221 ins->evex_p[2] |= EVEX_P2B;
1222 if (op_er_sae->decoflags & ER) {
1223 /* set EVEX.RC (rounding control) */
1224 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1225 & EVEX_P2RC;
1226 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001227 } else {
1228 /* set EVEX.L'L (vector length) */
1229 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001230 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001231 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001232 /* set EVEX.b */
1233 ins->evex_p[2] |= EVEX_P2B;
1234 }
1235 }
1236
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001237 if (itemp_has(temp, IF_MIB)) {
1238 opy->eaflags |= EAF_MIB;
1239 /*
1240 * if a separate form of MIB (ICC style) is used,
1241 * the index reg info is merged into mem operand
1242 */
1243 if (mib_index != R_none) {
1244 opy->indexreg = mib_index;
1245 opy->scale = 1;
1246 opy->hintbase = mib_index;
1247 opy->hinttype = EAH_NOTBASE;
1248 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001249 }
1250
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001251 if (process_ea(opy, &ea_data, bits,
1252 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 errfunc(ERR_NONFATAL, "invalid effective address");
1254 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001255 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001258 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001259 }
1260 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001261
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 default:
1263 errfunc(ERR_PANIC, "internal instruction table corrupt"
1264 ": instruction code \\%o (0x%02X) given", c, c);
1265 break;
1266 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001267 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001268
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001269 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001270
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001271 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001272 if (ins->rex & REX_H) {
1273 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1274 return -1;
1275 }
1276 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001277 }
1278
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001279 switch (ins->prefixes[PPS_VEX]) {
1280 case P_EVEX:
1281 if (!(ins->rex & REX_EV))
1282 return -1;
1283 break;
1284 case P_VEX3:
1285 case P_VEX2:
1286 if (!(ins->rex & REX_V))
1287 return -1;
1288 break;
1289 default:
1290 break;
1291 }
1292
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001293 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001295
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001297 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 return -1;
1299 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001300 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001302 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 ins->rex &= ~REX_W;
1304 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001305 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 ins->rex |= REX_W;
1307 bad32 &= ~REX_W;
1308 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001309 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 /* Follow REX_W */
1311 break;
1312 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001313
H. Peter Anvinfc561202011-07-07 16:58:22 -07001314 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1316 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001317 } else if (!(ins->rex & REX_EV) &&
1318 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1319 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1320 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001322 if (ins->rex & REX_EV)
1323 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001324 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1325 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 length += 3;
1327 else
1328 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001329 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 if (ins->rex & REX_H) {
1331 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1332 return -1;
1333 } else if (bits == 64) {
1334 length++;
1335 } else if ((ins->rex & REX_L) &&
1336 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001337 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001339 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001340 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 length++;
1342 } else {
1343 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1344 return -1;
1345 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001346 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001347
1348 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001349 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001350 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001351 "instruction is not lockable");
1352 }
1353
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001354 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001355
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001356 /*
1357 * when BND prefix is set by DEFAULT directive,
1358 * BND prefix is added to every appropriate instruction line
1359 * unless it is overridden by NOBND prefix.
1360 */
1361 if (globalbnd &&
1362 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1363 ins->prefixes[PPS_REP] = P_BND;
1364
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001365 return length;
1366}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001367
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001368static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1369{
1370 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001371 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001372 !(ins->rex & (REX_V | REX_EV)) &&
1373 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001374 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001375 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001376 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001377 return 1;
1378 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001379 }
1380
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001381 return 0;
1382}
1383
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001384static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001385 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001388 uint8_t c;
1389 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001390 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001391 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001392 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001393 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001394 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001395 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001396 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001397
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001398 ins->rex_done = false;
1399
H. Peter Anvin839eca22007-10-29 23:12:47 -07001400 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001401 c = *codes++;
1402 op1 = (c & 3) + ((opex & 1) << 2);
1403 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1404 opx = &ins->oprs[op1];
1405 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001406
H. Peter Anvin839eca22007-10-29 23:12:47 -07001407 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 case 01:
1409 case 02:
1410 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001411 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001412 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001413 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 codes += c;
1415 offset += c;
1416 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001417
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001418 case 05:
1419 case 06:
1420 case 07:
1421 opex = c;
1422 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001423
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001424 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001425 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001426 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001427 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 offset += 1;
1429 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001430
Jin Kyu Song164d6072013-10-15 19:10:13 -07001431 case4(014):
1432 break;
1433
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001434 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001435 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001436 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001437 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001439 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 offset += 1;
1441 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001442
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001443 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001444 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001445 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001447 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 offset += 1;
1449 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001451 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001452 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001453 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001454 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001455 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 offset += 2;
1457 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001458
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001460 if (opx->type & (BITS16 | BITS32))
1461 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 else
1463 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001464 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001465 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001466 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001467 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 offset += size;
1469 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001472 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001473 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001474 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001475 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 offset += 4;
1477 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001478
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001479 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001480 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001481 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001482 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001483 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001484 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 offset += size;
1486 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001489 if (opx->segment != segment) {
1490 data = opx->offset;
1491 out(offset, segment, &data,
1492 OUT_REL1ADR, insn_end - offset,
1493 opx->segment, opx->wrt);
1494 } else {
1495 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 if (data > 127 || data < -128)
1497 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001498 out(offset, segment, &data,
1499 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1500 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 offset += 1;
1502 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001503
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001505 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001506 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001507 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001508 offset += 8;
1509 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001510
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001511 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001512 if (opx->segment != segment) {
1513 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001516 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001518 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001520 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 }
1522 offset += 2;
1523 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001524
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001525 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001526 if (opx->type & (BITS16 | BITS32 | BITS64))
1527 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 else
1529 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001530 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001531 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001532 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1534 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001536 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001538 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 }
1540 offset += size;
1541 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001544 if (opx->segment != segment) {
1545 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001547 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001548 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001552 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 }
1554 offset += 4;
1555 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001556
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001558 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001559 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1560 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001561 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001562 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001563 outfmt->segbase(1 + opx->segment),
1564 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001565 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 case 0172:
1569 c = *codes++;
1570 opx = &ins->oprs[c >> 3];
1571 bytes[0] = nasm_regvals[opx->basereg] << 4;
1572 opx = &ins->oprs[c & 7];
1573 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1574 errfunc(ERR_NONFATAL,
1575 "non-absolute expression not permitted as argument %d",
1576 c & 7);
1577 } else {
1578 if (opx->offset & ~15) {
1579 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1580 "four-bit argument exceeds bounds");
1581 }
1582 bytes[0] |= opx->offset & 15;
1583 }
1584 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1585 offset++;
1586 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case 0173:
1589 c = *codes++;
1590 opx = &ins->oprs[c >> 4];
1591 bytes[0] = nasm_regvals[opx->basereg] << 4;
1592 bytes[0] |= c & 15;
1593 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1594 offset++;
1595 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001596
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001597 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 bytes[0] = nasm_regvals[opx->basereg] << 4;
1599 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1600 offset++;
1601 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001604 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1606 (int32_t)data != (int64_t)data) {
1607 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1608 "signed dword immediate exceeds bounds");
1609 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001610 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 opx->segment, opx->wrt);
1612 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001613 break;
1614
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001615 case4(0240):
1616 case 0250:
1617 codes += 3;
1618 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1619 EVEX_P2Z | EVEX_P2AAA, 2);
1620 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1621 bytes[0] = 0x62;
1622 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001623 bytes[1] = ((((ins->rex & 7) << 5) |
1624 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1625 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001626 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1627 ((~ins->vexreg & 15) << 3) |
1628 (1 << 2) | (ins->vex_wlp & 3);
1629 bytes[3] = ins->evex_p[2];
1630 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1631 offset += 4;
1632 break;
1633
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case4(0260):
1635 case 0270:
1636 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001637 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1638 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1640 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1641 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001642 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1644 offset += 3;
1645 } else {
1646 bytes[0] = 0xc5;
1647 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001648 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1650 offset += 2;
1651 }
1652 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001653
H. Peter Anvine014f352012-02-25 22:35:19 -08001654 case 0271:
1655 case 0272:
1656 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001657 break;
1658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(0274):
1660 {
1661 uint64_t uv, um;
1662 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001663
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001664 if (ins->rex & REX_W)
1665 s = 64;
1666 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1667 s = 16;
1668 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1669 s = 32;
1670 else
1671 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 um = (uint64_t)2 << (s-1);
1674 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001675
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 if (uv > 127 && uv < (uint64_t)-128 &&
1677 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001678 /* If this wasn't explicitly byte-sized, warn as though we
1679 * had fallen through to the imm16/32/64 case.
1680 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001681 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001682 "%s value exceeds bounds",
1683 (opx->type & BITS8) ? "signed byte" :
1684 s == 16 ? "word" :
1685 s == 32 ? "dword" :
1686 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001688 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001689 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001690 out(offset, segment, &data, OUT_ADDRESS, 1,
1691 opx->segment, opx->wrt);
1692 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001693 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001694 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1695 NO_SEG);
1696 }
1697 offset += 1;
1698 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001703
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001705 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001707 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 offset += 1;
1709 } else
1710 offset += 0;
1711 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001714 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001716 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 offset += 1;
1718 } else
1719 offset += 0;
1720 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 case 0312:
1723 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001724
Keith Kaniosb7a89542007-04-12 02:40:54 +00001725 case 0313:
1726 ins->rex = 0;
1727 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 case4(0314):
1730 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001731
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001735
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001737 case 0323:
1738 break;
1739
Keith Kaniosb7a89542007-04-12 02:40:54 +00001740 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001741 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001743
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 case 0325:
1745 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001746
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001747 case 0326:
1748 break;
1749
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001751 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001752 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 offset += 1;
1754 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001758
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001761 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001762 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 offset += 1;
1764 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
Keith Kanios48af1772007-08-17 07:37:52 +00001766 case 0334:
1767 if (ins->rex & REX_R) {
1768 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001769 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001770 offset += 1;
1771 }
1772 ins->rex &= ~(REX_L|REX_R);
1773 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001774
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001775 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001776 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001777
H. Peter Anvin962e3052008-08-28 17:47:16 -07001778 case 0336:
1779 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001780 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001781
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 if (ins->oprs[0].segment != NO_SEG)
1784 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1785 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001786 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 if (size > 0)
1788 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001789 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 offset += size;
1791 }
1792 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 case 0341:
1795 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001796
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001797 case 0360:
1798 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001799
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 case 0361:
1801 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001802 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1803 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001804 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case 0364:
1807 case 0365:
1808 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001809
Keith Kanios48af1772007-08-17 07:37:52 +00001810 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001812 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001813 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001814 offset += 1;
1815 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001816
Jin Kyu Song03041092013-10-15 19:38:51 -07001817 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 case 0373:
1821 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001822 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 offset += 1;
1824 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001826 case 0374:
1827 eat = EA_XMMVSIB;
1828 break;
1829
1830 case 0375:
1831 eat = EA_YMMVSIB;
1832 break;
1833
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001834 case 0376:
1835 eat = EA_ZMMVSIB;
1836 break;
1837
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 case4(0100):
1839 case4(0110):
1840 case4(0120):
1841 case4(0130):
1842 case4(0200):
1843 case4(0204):
1844 case4(0210):
1845 case4(0214):
1846 case4(0220):
1847 case4(0224):
1848 case4(0230):
1849 case4(0234):
1850 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 ea ea_data;
1852 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001854 uint8_t *p;
1855 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001856 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001857
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001858 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 /* pick rfield from operand b (opx) */
1860 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001861 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 } else {
1863 /* rfield is constant */
1864 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001868 if (process_ea(opy, &ea_data, bits,
1869 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001871
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 p = bytes;
1873 *p++ = ea_data.modrm;
1874 if (ea_data.sib_present)
1875 *p++ = ea_data.sib;
1876
1877 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001878 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001880 /*
1881 * Make sure the address gets the right offset in case
1882 * the line breaks in the .lst file (BR 1197827)
1883 */
1884 offset += s;
1885 s = 0;
1886
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001887 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001888 /* use compressed displacement, if available */
1889 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 if (ea_data.rip) {
1892 if (opy->segment == segment) {
1893 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001894 if (overflow_signed(data, ea_data.bytes))
1895 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 out(offset, segment, &data, OUT_ADDRESS,
1897 ea_data.bytes, NO_SEG, NO_SEG);
1898 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001899 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001900 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 insn_end - offset, opy->segment, opy->wrt);
1902 }
1903 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001904 int asize = ins->addr_size >> 3;
1905 int atype = ea_data.bytes;
1906
1907 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001908 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001909 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001910 warn_overflow(ERR_PASS2, ea_data.bytes);
1911
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001912 if (asize > ea_data.bytes) {
1913 /*
1914 * If the address isn't the full width of
1915 * the address size, treat is as signed...
1916 */
1917 atype = -atype;
1918 }
1919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001921 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
1924 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001925 }
1926 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 default:
1929 errfunc(ERR_PANIC, "internal instruction table corrupt"
1930 ": instruction code \\%o (0x%02X) given", c, c);
1931 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001933 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001934}
1935
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001936static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001937{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001938 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001939 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001940 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001941}
1942
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001943static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001944{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001945 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001947 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001948}
1949
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001950static int op_rexflags(const operand * o, int mask)
1951{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001952 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001953 int val;
1954
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001955 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001956 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001957
H. Peter Anvina4835d42008-05-20 14:21:29 -07001958 flags = nasm_reg_flags[o->basereg];
1959 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001960
1961 return rexflags(val, flags, mask);
1962}
1963
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001964static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001965{
1966 int rex = 0;
1967
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001968 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001970 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001971 rex |= REX_W;
1972 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1973 rex |= REX_H;
1974 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1975 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001976
1977 return rex & mask;
1978}
1979
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001980static int evexflags(int val, decoflags_t deco,
1981 int mask, uint8_t byte)
1982{
1983 int evex = 0;
1984
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001985 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001986 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001987 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001988 evex |= (EVEX_P0RP | EVEX_P0X);
1989 break;
1990 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001991 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001992 evex |= EVEX_P2VP;
1993 if (deco & Z)
1994 evex |= EVEX_P2Z;
1995 if (deco & OPMASK_MASK)
1996 evex |= deco & EVEX_P2AAA;
1997 break;
1998 }
1999 return evex & mask;
2000}
2001
2002static int op_evexflags(const operand * o, int mask, uint8_t byte)
2003{
2004 int val;
2005
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002006 val = nasm_regvals[o->basereg];
2007
2008 return evexflags(val, o->decoflags, mask, byte);
2009}
2010
H. Peter Anvin23595f52009-07-25 17:44:25 -07002011static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002012 insn *instruction,
2013 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002014{
2015 const struct itemplate *temp;
2016 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002017 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002018 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002019 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002020 int i;
2021
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002022 /* broadcasting uses a different data element size */
2023 for (i = 0; i < instruction->operands; i++)
2024 if (i == broadcast)
2025 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2026 else
2027 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002028
2029 merr = MERR_INVALOP;
2030
2031 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002032 temp->opcode != I_none; temp++) {
2033 m = matches(temp, instruction, bits);
2034 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002035 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002036 m = MOK_GOOD;
2037 else
2038 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002039 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002040 /*
2041 * Missing operand size and a candidate for fuzzy matching...
2042 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002043 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002044 if (i == broadcast)
2045 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2046 else
2047 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 opsizemissing = true;
2049 }
2050 if (m > merr)
2051 merr = m;
2052 if (merr == MOK_GOOD)
2053 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002054 }
2055
2056 /* No match, but see if we can get a fuzzy operand size match... */
2057 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002058 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002059
2060 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 /*
2062 * We ignore extrinsic operand sizes on registers, so we should
2063 * never try to fuzzy-match on them. This also resolves the case
2064 * when we have e.g. "xmmrm128" in two different positions.
2065 */
2066 if (is_class(REGISTER, instruction->oprs[i].type))
2067 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002068
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002069 /* This tests if xsizeflags[i] has more than one bit set */
2070 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2071 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002072
Jin Kyu Song7903c072013-10-30 03:00:12 -07002073 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002074 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002075 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2076 BITS32 : BITS64);
2077 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002078 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002079 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002080 }
2081
2082 /* Try matching again... */
2083 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002084 temp->opcode != I_none; temp++) {
2085 m = matches(temp, instruction, bits);
2086 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002087 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 m = MOK_GOOD;
2089 else
2090 m = MERR_INVALOP;
2091 }
2092 if (m > merr)
2093 merr = m;
2094 if (merr == MOK_GOOD)
2095 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002096 }
2097
H. Peter Anvina81655b2009-07-25 18:15:28 -07002098done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002099 *tempp = temp;
2100 return merr;
2101}
2102
H. Peter Anvin65289e82009-07-25 17:25:11 -07002103static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002104 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002105{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002106 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002107 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002108 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002109
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002110 /*
2111 * Check the opcode
2112 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002114 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002115
2116 /*
2117 * Count the operands
2118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002120 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002121
2122 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002123 * Is it legal?
2124 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002125 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002126 return MERR_INVALOP;
2127
2128 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002129 * {evex} available?
2130 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002131 switch (instruction->prefixes[PPS_VEX]) {
2132 case P_EVEX:
2133 if (!itemp_has(itemp, IF_EVEX))
2134 return MERR_ENCMISMATCH;
2135 break;
2136 case P_VEX3:
2137 case P_VEX2:
2138 if (!itemp_has(itemp, IF_VEX))
2139 return MERR_ENCMISMATCH;
2140 break;
2141 default:
2142 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002143 }
2144
2145 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002146 * Check that no spurious colons or TOs are present
2147 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 for (i = 0; i < itemp->operands; i++)
2149 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002150 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002151
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002152 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002153 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002154 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002155 switch (itemp_smask(itemp)) {
2156 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002157 asize = BITS8;
2158 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002159 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002160 asize = BITS16;
2161 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002162 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002163 asize = BITS32;
2164 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002165 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002166 asize = BITS64;
2167 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002168 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002169 asize = BITS128;
2170 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002171 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002172 asize = BITS256;
2173 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002174 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002175 asize = BITS512;
2176 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002177 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002178 switch (bits) {
2179 case 16:
2180 asize = BITS16;
2181 break;
2182 case 32:
2183 asize = BITS32;
2184 break;
2185 case 64:
2186 asize = BITS64;
2187 break;
2188 default:
2189 asize = 0;
2190 break;
2191 }
2192 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002193 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002194 asize = 0;
2195 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002196 }
2197
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002198 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002199 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002200 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002201 memset(size, 0, sizeof size);
2202 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002203 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002204 /* S- flags apply to all operands */
2205 for (i = 0; i < MAX_OPERANDS; i++)
2206 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002207 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002208
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002209 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002210 * Check that the operand flags all match up,
2211 * it's a bit tricky so lets be verbose:
2212 *
2213 * 1) Find out the size of operand. If instruction
2214 * doesn't have one specified -- we're trying to
2215 * guess it either from template (IF_S* flag) or
2216 * from code bits.
2217 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002218 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 * template has an operand size specified AND this size differ
2220 * from which instruction has (perhaps we got it from code bits)
2221 * we are:
2222 * a) Check that only size of instruction and operand is differ
2223 * other characteristics do match
2224 * b) Perhaps it's a register specified in instruction so
2225 * for such a case we just mark that operand as "size
2226 * missing" and this will turn on fuzzy operand size
2227 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002228 */
2229 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002231 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002232 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002233 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002234 opflags_t template_opsize, insn_opsize;
2235
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 if (!(type & SIZE_MASK))
2237 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002238
Jin Kyu Song7903c072013-10-30 03:00:12 -07002239 insn_opsize = type & SIZE_MASK;
2240 if (!is_broadcast) {
2241 template_opsize = itemp->opd[i] & SIZE_MASK;
2242 } else {
2243 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2244 /*
2245 * when broadcasting, the element size depends on
2246 * the instruction type. decorator flag should match.
2247 */
2248
2249 if (deco_brsize) {
2250 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002251 /* calculate the proper number : {1to<brcast_num>} */
2252 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2253 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002254 } else {
2255 template_opsize = 0;
2256 }
2257 }
2258
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002259 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002260 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002261 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002262 } else if (template_opsize) {
2263 if (template_opsize != insn_opsize) {
2264 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002265 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002266 } else if (!is_class(REGISTER, type)) {
2267 /*
2268 * Note: we don't honor extrinsic operand sizes for registers,
2269 * so "missing operand size" for a register should be
2270 * considered a wildcard match rather than an error.
2271 */
2272 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002273 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002274 } else if (is_broadcast &&
2275 (brcast_num !=
2276 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2277 /*
2278 * broadcasting opsize matches but the number of repeated memory
2279 * element does not match.
2280 * if 64b double precision float is broadcasted to zmm (512b),
2281 * broadcasting decorator must be {1to8}.
2282 */
2283 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002284 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002285 }
2286 }
2287
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002288 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002289 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002290
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002291 /*
2292 * Check operand sizes
2293 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002294 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2295 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002297 asize = itemp->opd[i] & SIZE_MASK;
2298 if (asize) {
2299 for (i = 0; i < oprs; i++)
2300 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 break;
2302 }
2303 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002304 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002306 }
2307
Keith Kaniosb7a89542007-04-12 02:40:54 +00002308 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 if (!(itemp->opd[i] & SIZE_MASK) &&
2310 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002311 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002312 }
2313
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002314 /*
2315 * Check template is okay at the set cpu level
2316 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002317 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002318 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002319
Keith Kaniosb7a89542007-04-12 02:40:54 +00002320 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002321 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002322 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002323 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002324 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002326 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002327 * If we have a HLE prefix, look for the NOHLE flag
2328 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002329 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002330 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2331 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2332 return MERR_BADHLE;
2333
2334 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002335 * Check if special handling needed for Jumps
2336 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002337 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002338 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339
Jin Kyu Song03041092013-10-15 19:38:51 -07002340 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002341 * Check if BND prefix is allowed.
2342 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002343 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002344 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002345 (has_prefix(instruction, PPS_REP, P_BND) ||
2346 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002347 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002348 else if (itemp_has(itemp, IF_BND) &&
2349 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2350 has_prefix(instruction, PPS_REP, P_REPNZ)))
2351 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002352
H. Peter Anvin60926242009-07-26 16:25:38 -07002353 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002354}
2355
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002356/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002357 * Check if ModR/M.mod should/can be 01.
2358 * - EAF_BYTEOFFS is set
2359 * - offset can fit in a byte when EVEX is not used
2360 * - offset can be compressed when EVEX is used
2361 */
2362#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2363 (o >= -128 && o <= 127 && \
2364 seg == NO_SEG && !forw_ref && \
2365 !(input->eaflags & EAF_WORDOFFS) && \
2366 !(ins->rex & REX_EV)) || \
2367 (ins->rex & REX_EV && \
2368 is_disp8n(input, ins, &output->disp8)))
2369
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002370static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002371 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002372{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002373 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002374 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002375 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002376
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002377 output->type = EA_SCALAR;
2378 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002379 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002380
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002381 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002382 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002383 /* EVEX.R' flag for the REG operand */
2384 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002385
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002386 if (is_class(REGISTER, input->type)) {
2387 /*
2388 * It's a direct register.
2389 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002390 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002391 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002392
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002393 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002394 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002395
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002396 /* broadcasting is not available with a direct register operand. */
2397 if (input->decoflags & BRDCAST_MASK) {
2398 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2399 goto err;
2400 }
2401
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002402 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002403 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002404 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002405 output->bytes = 0; /* no offset necessary either */
2406 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2407 } else {
2408 /*
2409 * It's a memory reference.
2410 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002411
2412 /* Embedded rounding or SAE is not available with a mem ref operand. */
2413 if (input->decoflags & (ER | SAE)) {
2414 nasm_error(ERR_NONFATAL,
2415 "Embedded rounding is available only with reg-reg op.");
2416 return -1;
2417 }
2418
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002419 if (input->basereg == -1 &&
2420 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002421 /*
2422 * It's a pure offset.
2423 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002424 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2425 input->segment == NO_SEG) {
2426 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2427 input->type &= ~IP_REL;
2428 input->type |= MEMORY;
2429 }
2430
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002431 if (bits == 64 &&
2432 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2433 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2434 return -1;
2435 }
2436
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002437 if (eaflags & EAF_BYTEOFFS ||
2438 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002439 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2440 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2441 }
2442
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002443 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002444 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002445 output->sib = GEN_SIB(0, 4, 5);
2446 output->bytes = 4;
2447 output->modrm = GEN_MODRM(0, rfield, 4);
2448 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002449 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002450 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->bytes = (addrbits != 16 ? 4 : 2);
2452 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2453 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002454 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002455 } else {
2456 /*
2457 * It's an indirection.
2458 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002460 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002462 int t, it, bt; /* register numbers */
2463 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 if (s == 0)
2466 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002467
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002468 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002469 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 ix = nasm_reg_flags[i];
2471 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002472 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 ix = 0;
2474 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002475
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002476 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002477 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002478 bx = nasm_reg_flags[b];
2479 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002480 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002481 bx = 0;
2482 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002483
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002484 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002485 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002486 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002487 int32_t o = input->offset;
2488 int mod, scale, index, base;
2489
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002490 /*
2491 * For a vector SIB, one has to be a vector and the other,
2492 * if present, a GPR. The vector must be the index operand.
2493 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002494 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002495 if (s == 0)
2496 s = 1;
2497 else if (s != 1)
2498 goto err;
2499
2500 t = bt, bt = it, it = t;
2501 x = bx, bx = ix, ix = x;
2502 }
2503
2504 if (bt != -1) {
2505 if (REG_GPR & ~bx)
2506 goto err;
2507 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2508 sok &= bx;
2509 else
2510 goto err;
2511 }
2512
2513 /*
2514 * While we're here, ensure the user didn't specify
2515 * WORD or QWORD
2516 */
2517 if (input->disp_size == 16 || input->disp_size == 64)
2518 goto err;
2519
2520 if (addrbits == 16 ||
2521 (addrbits == 32 && !(sok & BITS32)) ||
2522 (addrbits == 64 && !(sok & BITS64)))
2523 goto err;
2524
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002525 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2526 : ((ix & YMMREG & ~REG_EA)
2527 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002528
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002529 output->rex |= rexflags(it, ix, REX_X);
2530 output->rex |= rexflags(bt, bx, REX_B);
2531 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002532
2533 index = it & 7; /* it is known to be != -1 */
2534
2535 switch (s) {
2536 case 1:
2537 scale = 0;
2538 break;
2539 case 2:
2540 scale = 1;
2541 break;
2542 case 4:
2543 scale = 2;
2544 break;
2545 case 8:
2546 scale = 3;
2547 break;
2548 default: /* then what the smeg is it? */
2549 goto err; /* panic */
2550 }
2551
2552 if (bt == -1) {
2553 base = 5;
2554 mod = 0;
2555 } else {
2556 base = (bt & 7);
2557 if (base != REG_NUM_EBP && o == 0 &&
2558 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002559 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002560 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002561 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562 mod = 1;
2563 else
2564 mod = 2;
2565 }
2566
2567 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002568 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2569 output->modrm = GEN_MODRM(mod, rfield, 4);
2570 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002571 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002572 /*
2573 * it must be a 32/64-bit memory reference. Firstly we have
2574 * to check that all registers involved are type E/Rxx.
2575 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002576 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002578
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002579 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002580 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2581 sok &= ix;
2582 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002583 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002584 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002586 if (bt != -1) {
2587 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002588 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002589 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002590 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002591 sok &= bx;
2592 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002594 /*
2595 * While we're here, ensure the user didn't specify
2596 * WORD or QWORD
2597 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002598 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002599 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002601 if (addrbits == 16 ||
2602 (addrbits == 32 && !(sok & BITS32)) ||
2603 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002604 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002605
Keith Kaniosb7a89542007-04-12 02:40:54 +00002606 /* now reorganize base/index */
2607 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002608 ((hb == b && ht == EAH_NOTBASE) ||
2609 (hb == i && ht == EAH_MAKEBASE))) {
2610 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002611 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002612 x = bx, bx = ix, ix = x;
2613 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002614
Jin Kyu Song164d6072013-10-15 19:10:13 -07002615 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002616 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002617 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002618 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002619 if (eaflags & EAF_MIB) {
2620 /* only for mib operands */
2621 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2622 /*
2623 * make a single reg index [reg*1].
2624 * gas uses this form for an explicit index register.
2625 */
2626 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2627 }
2628 if ((ht == EAH_SUMMED) && bt == -1) {
2629 /* separate once summed index into [base, index] */
2630 bt = it, bx = ix, s--;
2631 }
2632 } else {
2633 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002634 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002635 s == 3 || s == 5 || s == 9) && bt == -1) {
2636 /* convert 3*EAX to EAX+2*EAX */
2637 bt = it, bx = ix, s--;
2638 }
2639 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002640 (eaflags & EAF_TIMESTWO) &&
2641 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002642 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002643 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002644 * to sib format with 0x0 displacement - [EAX*1+0].
2645 */
2646 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2647 }
2648 }
Keith Kanios48af1772007-08-17 07:37:52 +00002649 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002651 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002652 x = ix, ix = bx, bx = x;
2653 }
2654 if (it == REG_NUM_ESP ||
2655 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 output->rex |= rexflags(it, ix, REX_X);
2659 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002660
Keith Kanios48af1772007-08-17 07:37:52 +00002661 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002664
Keith Kaniosb7a89542007-04-12 02:40:54 +00002665 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668 } else {
2669 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002670 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002671 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002672 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002673 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002674 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002675 mod = 1;
2676 else
2677 mod = 2;
2678 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002679
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002680 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002681 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2682 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002683 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002684 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002686
Keith Kaniosb7a89542007-04-12 02:40:54 +00002687 if (it == -1)
2688 index = 4, s = 1;
2689 else
2690 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002691
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 switch (s) {
2693 case 1:
2694 scale = 0;
2695 break;
2696 case 2:
2697 scale = 1;
2698 break;
2699 case 4:
2700 scale = 2;
2701 break;
2702 case 8:
2703 scale = 3;
2704 break;
2705 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002706 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002708
Keith Kaniosb7a89542007-04-12 02:40:54 +00002709 if (bt == -1) {
2710 base = 5;
2711 mod = 0;
2712 } else {
2713 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002714 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002716 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002718 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002719 mod = 1;
2720 else
2721 mod = 2;
2722 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002723
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002724 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002725 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2726 output->modrm = GEN_MODRM(mod, rfield, 4);
2727 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 }
2729 } else { /* it's 16-bit */
2730 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002731 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002732
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 /* check for 64-bit long mode */
2734 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002735 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002738 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2739 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002740 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002741
Keith Kaniosb7a89542007-04-12 02:40:54 +00002742 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002743 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002744 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002745
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002747 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 if (b == -1 && i != -1) {
2749 int tmp = b;
2750 b = i;
2751 i = tmp;
2752 } /* swap */
2753 if ((b == R_SI || b == R_DI) && i != -1) {
2754 int tmp = b;
2755 b = i;
2756 i = tmp;
2757 }
2758 /* have BX/BP as base, SI/DI index */
2759 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002760 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 if (i != -1 && b != -1 &&
2762 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002763 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002765 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002766
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 rm = -1;
2768 if (i != -1)
2769 switch (i * 256 + b) {
2770 case R_SI * 256 + R_BX:
2771 rm = 0;
2772 break;
2773 case R_DI * 256 + R_BX:
2774 rm = 1;
2775 break;
2776 case R_SI * 256 + R_BP:
2777 rm = 2;
2778 break;
2779 case R_DI * 256 + R_BP:
2780 rm = 3;
2781 break;
2782 } else
2783 switch (b) {
2784 case R_SI:
2785 rm = 4;
2786 break;
2787 case R_DI:
2788 rm = 5;
2789 break;
2790 case R_BP:
2791 rm = 6;
2792 break;
2793 case R_BX:
2794 rm = 7;
2795 break;
2796 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002797 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002798 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002799
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002800 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002801 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002803 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 mod = 1;
2805 else
2806 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002807
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002808 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002809 output->bytes = mod; /* bytes of offset needed */
2810 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 }
2812 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002813 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002814
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002816 return output->type;
2817
2818err:
2819 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820}
2821
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002822static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002823{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002824 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002825 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002826
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002827 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002828
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002829 switch (ins->prefixes[PPS_ASIZE]) {
2830 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002831 valid &= 16;
2832 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002833 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002834 valid &= 32;
2835 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002836 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 valid &= 64;
2838 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002839 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002840 valid &= (addrbits == 32) ? 16 : 32;
2841 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002842 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002844 }
2845
2846 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 if (is_class(MEMORY, ins->oprs[j].type)) {
2848 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002849
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002850 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002851 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002852 i = 0;
2853 else
2854 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002855
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002856 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002857 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002858 b = 0;
2859 else
2860 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002861
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 if (ins->oprs[j].scale == 0)
2863 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002865 if (!i && !b) {
2866 int ds = ins->oprs[j].disp_size;
2867 if ((addrbits != 64 && ds > 8) ||
2868 (addrbits == 64 && ds == 16))
2869 valid &= ds;
2870 } else {
2871 if (!(REG16 & ~b))
2872 valid &= 16;
2873 if (!(REG32 & ~b))
2874 valid &= 32;
2875 if (!(REG64 & ~b))
2876 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002877
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002878 if (!(REG16 & ~i))
2879 valid &= 16;
2880 if (!(REG32 & ~i))
2881 valid &= 32;
2882 if (!(REG64 & ~i))
2883 valid &= 64;
2884 }
2885 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002886 }
2887
2888 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002890 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002891 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002892 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002893 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002894 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 /* Impossible... */
2896 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2897 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002898 }
2899
2900 defdisp = ins->addr_size == 16 ? 16 : 32;
2901
2902 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2904 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2905 /*
2906 * mem_offs sizes must match the address size; if not,
2907 * strip the MEM_OFFS bit and match only EA instructions
2908 */
2909 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2910 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002911 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002912}