blob: ff92722743c44156a06df99e097775195fc0e8fb [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
Jin Kyu Song164d6072013-10-15 19:10:13 -070045 * \14..\17 - the position of index register operand in MIB (BND insns)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070046 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040047 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070048 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040063 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070064 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070067 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000069 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070071 *
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 *
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
91 *
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
96 *
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
99 *
H. Peter Anvin588df782008-10-07 10:05:10 -0700100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -0700101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700105 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700108 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700112 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700117 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700119 *
H. Peter Anvin574784d2012-02-25 22:33:46 -0800120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700127 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000139 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400140 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000145 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000146 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700153 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400156 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400157 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700158 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800163 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700169 * \376 - this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 */
171
H. Peter Anvinfe501952007-10-02 21:53:51 -0700172#include "compiler.h"
173
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174#include <stdio.h>
175#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000176#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
178#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000179#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include "assemble.h"
181#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700182#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800183#include "disp8.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
H. Peter Anvin65289e82009-07-25 17:25:11 -0700185enum match_result {
186 /*
187 * Matching errors. These should be sorted so that more specific
188 * errors come later in the sequence.
189 */
190 MERR_INVALOP,
191 MERR_OPSIZEMISSING,
192 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700193 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700194 MERR_BADCPU,
195 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800196 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700197 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700198 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800199 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700200 /*
201 * Matching success; the conditional ones first
202 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400203 MOK_JUMP, /* Matching OK but needs jmp_match() */
204 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700205};
206
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000207typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700208 enum ea_type type; /* what kind of EA is this? */
209 int sib_present; /* is a SIB byte necessary? */
210 int bytes; /* # of bytes of offset needed */
211 int size; /* lazy - this is sib+bytes+1 */
212 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700213 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214} ea;
215
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400216#define GEN_SIB(scale, index, base) \
217 (((scale) << 6) | ((index) << 3) | ((base)))
218
219#define GEN_MODRM(mod, reg, rm) \
220 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
221
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400222static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000223static efunc errfunc;
224static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000225static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000226
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800227static int64_t calcsize(int32_t, int64_t, int, insn *,
228 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700229static void gencode(int32_t segment, int64_t offset, int bits,
230 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400231 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700232static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400233 insn *instruction,
234 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700235static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700236static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000237static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700238static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000239static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700240static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700241static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000242
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700243static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700244
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400245static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000246{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700247 return ins->prefixes[pos] == prefix;
248}
249
250static void assert_no_prefix(insn * ins, enum prefix_pos pos)
251{
252 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400253 errfunc(ERR_NONFATAL, "invalid %s prefix",
254 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700255}
256
257static const char *size_name(int size)
258{
259 switch (size) {
260 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400261 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400263 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400265 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400267 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700268 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400269 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700270 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700272 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700274 case 64:
275 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000278 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700279}
280
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400281static void warn_overflow(int pass, int size)
282{
283 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
284 "%s data exceeds bounds", size_name(size));
285}
286
287static void warn_overflow_const(int64_t data, int size)
288{
289 if (overflow_general(data, size))
290 warn_overflow(ERR_PASS1, size);
291}
292
293static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700294{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100295 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400296 if (overflow_general(o->offset, size))
297 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700298 }
299}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400300
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000301/*
302 * This routine wrappers the real output format's output routine,
303 * in order to pass a copy of the data off to the listing file
304 * generator at the same time.
305 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800306static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800307 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400308 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000309{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000310 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000311 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800312 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000313
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800314 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400315 /*
316 * This is a non-relocated address, and we're going to
317 * convert it into RAWDATA format.
318 */
319 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800320
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400321 if (size > 8) {
322 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
323 return;
324 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700325
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400326 WRITEADDR(q, *(int64_t *)data, size);
327 data = p;
328 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000329 }
330
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800331 list->output(offset, data, type, size);
332
Frank Kotlerabebb082003-09-06 04:45:37 +0000333 /*
334 * this call to src_get determines when we call the
335 * debug-format-specific "linenum" function
336 * it updates lineno and lnfname to the current values
337 * returning 0 if "same as last time", -2 if lnfname
338 * changed, and the amount by which lineno changed,
339 * if it did. thus, these variables must be static
340 */
341
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400342 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800345 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000346}
347
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400348static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
349{
350 if (opx->segment != NO_SEG) {
351 uint64_t data = opx->offset;
352 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
353 } else {
354 uint8_t byte = opx->offset;
355 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
356 }
357}
358
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700359static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800360 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800362 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800363 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000364 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800365 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000366
H. Peter Anvin755f5212012-02-25 11:41:34 -0800367 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700368 return false;
369 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400370 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700371 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400372 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700373
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800374 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100375
Victor van den Elzen154e5922009-02-25 17:32:00 +0100376 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100377 /* Be optimistic in pass 1 */
378 return true;
379
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700381 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000382
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700383 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800384 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
385
386 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
387 /* jmp short (opcode eb) cannot be used with bnd prefix. */
388 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800389 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
390 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800391 }
392
393 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000395
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400396int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400397 insn * instruction, struct ofmt *output, efunc error,
398 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000400 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700402 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800403 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000404 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800405 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300406 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000409 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 outfmt = output; /* likewise */
411 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000412
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300413 wsize = idata_bytes(instruction->opcode);
414 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000416
H. Peter Anvineba20a72002-04-30 20:53:55 +0000417 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000419 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 if (t < 0)
421 errfunc(ERR_PANIC,
422 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400425 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400427 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700428 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400429 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000430 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700431 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800433 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400434 offset += wsize;
435 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700436 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400437 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000439
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800441 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000443
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 if (align) {
445 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100446 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800447 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 }
449 offset += e->stringlen + align;
450 }
451 }
452 if (t > 0 && t == instruction->times - 1) {
453 /*
454 * Dummy call to list->output to give the offset to the
455 * listing module.
456 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800457 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 list->uplevel(LIST_TIMES);
459 }
460 }
461 if (instruction->times > 1)
462 list->downlevel(LIST_TIMES);
463 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000464 }
465
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700467 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000469
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400470 fp = fopen(fname, "rb");
471 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
473 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400474 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
476 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200477 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400478 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700479 static char buf[4096];
480 size_t t = instruction->times;
481 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400482 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000483
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 len = ftell(fp);
485 if (instruction->eops->next) {
486 base = instruction->eops->next->offset;
487 len -= base;
488 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700489 len > (size_t)instruction->eops->next->next->offset)
490 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 }
492 /*
493 * Dummy call to list->output to give the offset to the
494 * listing module.
495 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800496 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 list->uplevel(LIST_INCBIN);
498 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700499 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000500
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 fseek(fp, base, SEEK_SET);
502 l = len;
503 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700504 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400505 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 if (!m) {
507 /*
508 * This shouldn't happen unless the file
509 * actually changes while we are reading
510 * it.
511 */
512 error(ERR_NONFATAL,
513 "`incbin': unexpected EOF while"
514 " reading file `%s'", fname);
515 t = 0; /* Try to exit cleanly */
516 break;
517 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800518 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 NO_SEG, NO_SEG);
520 l -= m;
521 }
522 }
523 list->downlevel(LIST_INCBIN);
524 if (instruction->times > 1) {
525 /*
526 * Dummy call to list->output to give the offset to the
527 * listing module.
528 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800529 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 list->uplevel(LIST_TIMES);
531 list->downlevel(LIST_TIMES);
532 }
533 fclose(fp);
534 return instruction->times * len;
535 }
536 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000537 }
538
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700539 /* Check to see if we need an address-size prefix */
540 add_asp(instruction, bits);
541
H. Peter Anvin23595f52009-07-25 17:44:25 -0700542 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700543
H. Peter Anvin23595f52009-07-25 17:44:25 -0700544 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400545 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800546 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400547 itimes = instruction->times;
548 if (insn_size < 0) /* shouldn't be, on pass two */
549 error(ERR_PANIC, "errors made it through from pass one");
550 else
551 while (itimes--) {
552 for (j = 0; j < MAXPREFIX; j++) {
553 uint8_t c = 0;
554 switch (instruction->prefixes[j]) {
555 case P_WAIT:
556 c = 0x9B;
557 break;
558 case P_LOCK:
559 c = 0xF0;
560 break;
561 case P_REPNE:
562 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800563 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700564 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400565 c = 0xF2;
566 break;
567 case P_REPE:
568 case P_REPZ:
569 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800570 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400571 c = 0xF3;
572 break;
573 case R_CS:
574 if (bits == 64) {
575 error(ERR_WARNING | ERR_PASS2,
576 "cs segment base generated, but will be ignored in 64-bit mode");
577 }
578 c = 0x2E;
579 break;
580 case R_DS:
581 if (bits == 64) {
582 error(ERR_WARNING | ERR_PASS2,
583 "ds segment base generated, but will be ignored in 64-bit mode");
584 }
585 c = 0x3E;
586 break;
587 case R_ES:
588 if (bits == 64) {
589 error(ERR_WARNING | ERR_PASS2,
590 "es segment base generated, but will be ignored in 64-bit mode");
591 }
592 c = 0x26;
593 break;
594 case R_FS:
595 c = 0x64;
596 break;
597 case R_GS:
598 c = 0x65;
599 break;
600 case R_SS:
601 if (bits == 64) {
602 error(ERR_WARNING | ERR_PASS2,
603 "ss segment base generated, but will be ignored in 64-bit mode");
604 }
605 c = 0x36;
606 break;
607 case R_SEGR6:
608 case R_SEGR7:
609 error(ERR_NONFATAL,
610 "segr6 and segr7 cannot be used as prefixes");
611 break;
612 case P_A16:
613 if (bits == 64) {
614 error(ERR_NONFATAL,
615 "16-bit addressing is not supported "
616 "in 64-bit mode");
617 } else if (bits != 16)
618 c = 0x67;
619 break;
620 case P_A32:
621 if (bits != 32)
622 c = 0x67;
623 break;
624 case P_A64:
625 if (bits != 64) {
626 error(ERR_NONFATAL,
627 "64-bit addressing is only supported "
628 "in 64-bit mode");
629 }
630 break;
631 case P_ASP:
632 c = 0x67;
633 break;
634 case P_O16:
635 if (bits != 16)
636 c = 0x66;
637 break;
638 case P_O32:
639 if (bits == 16)
640 c = 0x66;
641 break;
642 case P_O64:
643 /* REX.W */
644 break;
645 case P_OSP:
646 c = 0x66;
647 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700648 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800649 case P_VEX3:
650 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800651 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400652 case P_none:
653 break;
654 default:
655 error(ERR_PANIC, "invalid instruction prefix");
656 }
657 if (c != 0) {
658 out(offset, segment, &c, OUT_RAWDATA, 1,
659 NO_SEG, NO_SEG);
660 offset++;
661 }
662 }
663 insn_end = offset + insn_size;
664 gencode(segment, offset, bits, instruction,
665 temp, insn_end);
666 offset += insn_size;
667 if (itimes > 0 && itimes == instruction->times - 1) {
668 /*
669 * Dummy call to list->output to give the offset to the
670 * listing module.
671 */
672 list->output(offset, NULL, OUT_RAWDATA, 0);
673 list->uplevel(LIST_TIMES);
674 }
675 }
676 if (instruction->times > 1)
677 list->downlevel(LIST_TIMES);
678 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700679 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400680 /* No match */
681 switch (m) {
682 case MERR_OPSIZEMISSING:
683 error(ERR_NONFATAL, "operation size not specified");
684 break;
685 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400687 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700688 case MERR_BRNUMMISMATCH:
689 error(ERR_NONFATAL,
690 "mismatch in the number of broadcasting elements");
691 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400692 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400694 break;
695 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800696 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 bits);
698 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800699 case MERR_ENCMISMATCH:
700 error(ERR_NONFATAL, "specific encoding scheme not available");
701 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800702 case MERR_BADBND:
703 error(ERR_NONFATAL, "bnd prefix is not allowed");
704 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800705 case MERR_BADREPNE:
706 error(ERR_NONFATAL, "%s prefix is not allowed",
707 (has_prefix(instruction, PPS_REP, P_REPNE) ?
708 "repne" : "repnz"));
709 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400710 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 error(ERR_NONFATAL,
712 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400713 break;
714 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000715 }
716 return 0;
717}
718
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400719int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400720 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000721{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000722 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700723 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000724
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000726 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400728 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700731 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
732 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400733 instruction->opcode == I_DT || instruction->opcode == I_DO ||
734 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300736 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300739 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400741 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000742 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400745 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400747 warn_overflow_const(e->offset, wsize);
748 } else if (e->type == EOT_DB_STRING ||
749 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 align = (-osize) % wsize;
753 if (align < 0)
754 align += wsize;
755 isize += osize + align;
756 }
757 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000758 }
759
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300763 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700764 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000765
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400766 fp = fopen(fname, "rb");
767 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
769 fname);
770 else if (fseek(fp, 0L, SEEK_END) < 0)
771 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
772 fname);
773 else {
774 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 if (instruction->eops->next) {
776 len -= instruction->eops->next->offset;
777 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700778 len > (size_t)instruction->eops->next->next->offset) {
779 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 }
781 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300782 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300784 if (fp)
785 fclose(fp);
786 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000787 }
788
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700789 /* Check to see if we need an address-size prefix */
790 add_asp(instruction, bits);
791
H. Peter Anvin23595f52009-07-25 17:44:25 -0700792 m = find_match(&temp, instruction, segment, offset, bits);
793 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400794 /* we've matched an instruction. */
795 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400796 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100797
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800798 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400799 if (isize < 0)
800 return -1;
801 for (j = 0; j < MAXPREFIX; j++) {
802 switch (instruction->prefixes[j]) {
803 case P_A16:
804 if (bits != 16)
805 isize++;
806 break;
807 case P_A32:
808 if (bits != 32)
809 isize++;
810 break;
811 case P_O16:
812 if (bits != 16)
813 isize++;
814 break;
815 case P_O32:
816 if (bits == 16)
817 isize++;
818 break;
819 case P_A64:
820 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700821 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800822 case P_VEX3:
823 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800824 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400825 case P_none:
826 break;
827 default:
828 isize++;
829 break;
830 }
831 }
832 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700833 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400834 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000835 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000836}
837
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800838static void bad_hle_warn(const insn * ins, uint8_t hleok)
839{
840 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800841 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800842 static const enum whatwarn warn[2][4] =
843 {
844 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
845 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
846 };
847 unsigned int n;
848
849 n = (unsigned int)rep_pfx - P_XACQUIRE;
850 if (n > 1)
851 return; /* Not XACQUIRE/XRELEASE */
852
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800853 ww = warn[n][hleok];
854 if (!is_class(MEMORY, ins->oprs[0].type))
855 ww = w_inval; /* HLE requires operand 0 to be memory */
856
857 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800858 case w_none:
859 break;
860
861 case w_lock:
862 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800863 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800864 "%s with this instruction requires lock",
865 prefix_name(rep_pfx));
866 }
867 break;
868
869 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800870 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800871 "%s invalid with this instruction",
872 prefix_name(rep_pfx));
873 break;
874 }
875}
876
H. Peter Anvin507ae032008-10-09 15:37:10 -0700877/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400878#define case3(x) case (x): case (x)+1: case (x)+2
879#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700880
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800881static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800882 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000883{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800884 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800885 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000886 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000887 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700888 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700889 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700890 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700891 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800892 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800893 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700894 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700896 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700897 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700898 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700899
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700900 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400901 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 (void)segment; /* Don't warn that this parameter is unused */
904 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000905
H. Peter Anvin839eca22007-10-29 23:12:47 -0700906 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 c = *codes++;
908 op1 = (c & 3) + ((opex & 1) << 2);
909 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
910 opx = &ins->oprs[op1];
911 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700912
H. Peter Anvin839eca22007-10-29 23:12:47 -0700913 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400914 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 codes += c, length += c;
916 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700917
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400918 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400919 opex = c;
920 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400922 case4(010):
923 ins->rex |=
924 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 codes++, length++;
926 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700927
Jin Kyu Song164d6072013-10-15 19:10:13 -0700928 case4(014):
929 /* this is an index reg of MIB operand */
930 mib_index = opx->basereg;
931 break;
932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400933 case4(020):
934 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 length++;
936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 length += 2;
940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700943 if (opx->type & (BITS16 | BITS32 | BITS64))
944 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 else
946 length += (bits == 16) ? 2 : 4;
947 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400949 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 length += 4;
951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700954 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 length++;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000962 length += 8; /* MOV reg64/imm */
963 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 length += 2;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700970 if (opx->type & (BITS16 | BITS32 | BITS64))
971 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 else
973 length += (bits == 16) ? 2 : 4;
974 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 length += 4;
978 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400980 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700981 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case 0172:
985 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400986 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700987 length++;
988 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700990 case4(0174):
991 length++;
992 break;
993
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700994 case4(0240):
995 ins->rex |= REX_EV;
996 ins->vexreg = regval(opx);
997 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
998 ins->vex_cm = *codes++;
999 ins->vex_wlp = *codes++;
1000 ins->evex_tuple = (*codes++ - 0300);
1001 break;
1002
1003 case 0250:
1004 ins->rex |= REX_EV;
1005 ins->vexreg = 0;
1006 ins->vex_cm = *codes++;
1007 ins->vex_wlp = *codes++;
1008 ins->evex_tuple = (*codes++ - 0300);
1009 break;
1010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(0254):
1012 length += 4;
1013 break;
1014
1015 case4(0260):
1016 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001017 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 ins->vex_cm = *codes++;
1019 ins->vex_wlp = *codes++;
1020 break;
1021
1022 case 0270:
1023 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001024 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001025 ins->vex_cm = *codes++;
1026 ins->vex_wlp = *codes++;
1027 break;
1028
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001029 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001030 hleok = c & 3;
1031 break;
1032
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001033 case4(0274):
1034 length++;
1035 break;
1036
1037 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001041 if (bits == 64)
1042 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001043 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001047 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001049
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001051 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001052
Keith Kaniosb7a89542007-04-12 02:40:54 +00001053 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001054 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1055 has_prefix(ins, PPS_ASIZE, P_A32))
1056 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001059 case4(0314):
1060 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001063 {
1064 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1065 if (pfx == P_O16)
1066 break;
1067 if (pfx != P_none)
1068 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1069 else
1070 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001072 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001075 {
1076 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1077 if (pfx == P_O32)
1078 break;
1079 if (pfx != P_none)
1080 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1081 else
1082 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001084 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0322:
1087 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
Keith Kaniosb7a89542007-04-12 02:40:54 +00001089 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001090 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
Keith Kaniosb7a89542007-04-12 02:40:54 +00001093 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 case 0325:
1098 ins->rex |= REX_NH;
1099 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001100
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001101 case 0326:
1102 break;
1103
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 case 0330:
1105 codes++, length++;
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001111 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 case 0333:
1113 length++;
1114 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 case 0334:
1117 ins->rex |= REX_L;
1118 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001120 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001124 if (!ins->prefixes[PPS_REP])
1125 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001127
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001128 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001129 if (!ins->prefixes[PPS_REP])
1130 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001132
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 if (ins->oprs[0].segment != NO_SEG)
1135 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1136 " quantity of BSS space");
1137 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001138 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001141 case 0341:
1142 if (!ins->prefixes[PPS_WAIT])
1143 ins->prefixes[PPS_WAIT] = P_WAIT;
1144 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001145
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001146 case 0360:
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001149 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 length++;
1151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001153 case 0364:
1154 case 0365:
1155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001156
Keith Kanios48af1772007-08-17 07:37:52 +00001157 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001158 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001162 case 0370:
1163 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 case 0373:
1167 length++;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001170 case 0374:
1171 eat = EA_XMMVSIB;
1172 break;
1173
1174 case 0375:
1175 eat = EA_YMMVSIB;
1176 break;
1177
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001178 case 0376:
1179 eat = EA_ZMMVSIB;
1180 break;
1181
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 case4(0100):
1183 case4(0110):
1184 case4(0120):
1185 case4(0130):
1186 case4(0200):
1187 case4(0204):
1188 case4(0210):
1189 case4(0214):
1190 case4(0220):
1191 case4(0224):
1192 case4(0230):
1193 case4(0234):
1194 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001196 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 opflags_t rflags;
1198 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001199 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001200
Keith Kaniosb7a89542007-04-12 02:40:54 +00001201 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001202
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 if (c <= 0177) {
1204 /* pick rfield from operand b (opx) */
1205 rflags = regflag(opx);
1206 rfield = nasm_regvals[opx->basereg];
1207 } else {
1208 rflags = 0;
1209 rfield = c & 7;
1210 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001211
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001212 /* EVEX.b1 : evex_brerop contains the operand position */
1213 op_er_sae = (ins->evex_brerop >= 0 ?
1214 &ins->oprs[ins->evex_brerop] : NULL);
1215
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001216 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1217 /* set EVEX.b */
1218 ins->evex_p[2] |= EVEX_P2B;
1219 if (op_er_sae->decoflags & ER) {
1220 /* set EVEX.RC (rounding control) */
1221 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1222 & EVEX_P2RC;
1223 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001224 } else {
1225 /* set EVEX.L'L (vector length) */
1226 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001227 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001228 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001229 /* set EVEX.b */
1230 ins->evex_p[2] |= EVEX_P2B;
1231 }
1232 }
1233
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001234 if (itemp_has(temp, IF_MIB)) {
1235 opy->eaflags |= EAF_MIB;
1236 /*
1237 * if a separate form of MIB (ICC style) is used,
1238 * the index reg info is merged into mem operand
1239 */
1240 if (mib_index != R_none) {
1241 opy->indexreg = mib_index;
1242 opy->scale = 1;
1243 opy->hintbase = mib_index;
1244 opy->hinttype = EAH_NOTBASE;
1245 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001246 }
1247
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001248 if (process_ea(opy, &ea_data, bits,
1249 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 errfunc(ERR_NONFATAL, "invalid effective address");
1251 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001252 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001255 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 }
1257 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001258
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001259 default:
1260 errfunc(ERR_PANIC, "internal instruction table corrupt"
1261 ": instruction code \\%o (0x%02X) given", c, c);
1262 break;
1263 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001264 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001265
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001266 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001267
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001268 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 if (ins->rex & REX_H) {
1270 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1271 return -1;
1272 }
1273 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001274 }
1275
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001276 switch (ins->prefixes[PPS_VEX]) {
1277 case P_EVEX:
1278 if (!(ins->rex & REX_EV))
1279 return -1;
1280 break;
1281 case P_VEX3:
1282 case P_VEX2:
1283 if (!(ins->rex & REX_V))
1284 return -1;
1285 break;
1286 default:
1287 break;
1288 }
1289
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001290 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001292
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001293 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001294 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001295 return -1;
1296 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001297 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001299 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 ins->rex &= ~REX_W;
1301 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001302 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 ins->rex |= REX_W;
1304 bad32 &= ~REX_W;
1305 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001306 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 /* Follow REX_W */
1308 break;
1309 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001310
H. Peter Anvinfc561202011-07-07 16:58:22 -07001311 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1313 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001314 } else if (!(ins->rex & REX_EV) &&
1315 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1316 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1317 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001319 if (ins->rex & REX_EV)
1320 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001321 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1322 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 length += 3;
1324 else
1325 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001326 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 if (ins->rex & REX_H) {
1328 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1329 return -1;
1330 } else if (bits == 64) {
1331 length++;
1332 } else if ((ins->rex & REX_L) &&
1333 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001334 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001335 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001336 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001337 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 length++;
1339 } else {
1340 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1341 return -1;
1342 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001343 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001344
1345 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001346 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001347 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001348 "instruction is not lockable");
1349 }
1350
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001351 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001352
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001353 /*
1354 * when BND prefix is set by DEFAULT directive,
1355 * BND prefix is added to every appropriate instruction line
1356 * unless it is overridden by NOBND prefix.
1357 */
1358 if (globalbnd &&
1359 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1360 ins->prefixes[PPS_REP] = P_BND;
1361
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001362 return length;
1363}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001364
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001365static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1366{
1367 if (bits == 64) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001368 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001369 int rex = (ins->rex & REX_REAL) | REX_P;
1370 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001371 return 1;
1372 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001373 }
1374
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001375 return 0;
1376}
1377
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001378static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001379 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001380 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001381{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001382 uint8_t c;
1383 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001384 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001385 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001386 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001387 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001388 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001389 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001390 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001391
H. Peter Anvin839eca22007-10-29 23:12:47 -07001392 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001393 c = *codes++;
1394 op1 = (c & 3) + ((opex & 1) << 2);
1395 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1396 opx = &ins->oprs[op1];
1397 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001398
H. Peter Anvin839eca22007-10-29 23:12:47 -07001399 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 case 01:
1401 case 02:
1402 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001403 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001404 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001405 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 codes += c;
1407 offset += c;
1408 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001409
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001410 case 05:
1411 case 06:
1412 case 07:
1413 opex = c;
1414 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001415
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001416 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001417 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001418 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001419 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 offset += 1;
1421 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422
Jin Kyu Song164d6072013-10-15 19:10:13 -07001423 case4(014):
1424 break;
1425
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001426 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001427 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001428 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001429 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001431 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 offset += 1;
1433 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001435 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001436 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001437 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001438 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001439 out_imm8(offset, segment, opx);
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(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001444 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001445 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001446 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001447 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 offset += 2;
1449 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001451 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001452 if (opx->type & (BITS16 | BITS32))
1453 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 else
1455 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001456 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001457 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001458 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001459 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 offset += size;
1461 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001463 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001464 warn_overflow_opd(opx, 4);
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, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001467 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 offset += 4;
1469 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001472 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001473 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001474 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001475 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001476 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 offset += size;
1478 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001479
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001480 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001481 if (opx->segment != segment) {
1482 data = opx->offset;
1483 out(offset, segment, &data,
1484 OUT_REL1ADR, insn_end - offset,
1485 opx->segment, opx->wrt);
1486 } else {
1487 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 if (data > 127 || data < -128)
1489 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001490 out(offset, segment, &data,
1491 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1492 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 offset += 1;
1494 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001495
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001497 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001498 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001499 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001500 offset += 8;
1501 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001502
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001503 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001504 if (opx->segment != segment) {
1505 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001507 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001508 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001510 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001512 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 }
1514 offset += 2;
1515 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001516
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001517 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001518 if (opx->type & (BITS16 | BITS32 | BITS64))
1519 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 else
1521 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001522 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001523 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001524 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001525 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1526 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001528 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001530 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 }
1532 offset += size;
1533 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001536 if (opx->segment != segment) {
1537 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001539 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001540 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001542 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001544 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 }
1546 offset += 4;
1547 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001548
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001549 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001551 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1552 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001554 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 outfmt->segbase(1 + opx->segment),
1556 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001557 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001559
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001560 case 0172:
1561 c = *codes++;
1562 opx = &ins->oprs[c >> 3];
1563 bytes[0] = nasm_regvals[opx->basereg] << 4;
1564 opx = &ins->oprs[c & 7];
1565 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1566 errfunc(ERR_NONFATAL,
1567 "non-absolute expression not permitted as argument %d",
1568 c & 7);
1569 } else {
1570 if (opx->offset & ~15) {
1571 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1572 "four-bit argument exceeds bounds");
1573 }
1574 bytes[0] |= opx->offset & 15;
1575 }
1576 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1577 offset++;
1578 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001579
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 case 0173:
1581 c = *codes++;
1582 opx = &ins->oprs[c >> 4];
1583 bytes[0] = nasm_regvals[opx->basereg] << 4;
1584 bytes[0] |= c & 15;
1585 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1586 offset++;
1587 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001588
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001589 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001590 bytes[0] = nasm_regvals[opx->basereg] << 4;
1591 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1592 offset++;
1593 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001596 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001597 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1598 (int32_t)data != (int64_t)data) {
1599 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1600 "signed dword immediate exceeds bounds");
1601 }
1602 out(offset, segment, &data, OUT_ADDRESS, 4,
1603 opx->segment, opx->wrt);
1604 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001605 break;
1606
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001607 case4(0240):
1608 case 0250:
1609 codes += 3;
1610 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1611 EVEX_P2Z | EVEX_P2AAA, 2);
1612 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1613 bytes[0] = 0x62;
1614 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001615 bytes[1] = ((((ins->rex & 7) << 5) |
1616 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1617 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001618 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1619 ((~ins->vexreg & 15) << 3) |
1620 (1 << 2) | (ins->vex_wlp & 3);
1621 bytes[3] = ins->evex_p[2];
1622 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1623 offset += 4;
1624 break;
1625
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001626 case4(0260):
1627 case 0270:
1628 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001629 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1630 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1632 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1633 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001634 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1636 offset += 3;
1637 } else {
1638 bytes[0] = 0xc5;
1639 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001640 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1642 offset += 2;
1643 }
1644 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001645
H. Peter Anvine014f352012-02-25 22:35:19 -08001646 case 0271:
1647 case 0272:
1648 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001649 break;
1650
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 case4(0274):
1652 {
1653 uint64_t uv, um;
1654 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 if (ins->rex & REX_W)
1657 s = 64;
1658 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1659 s = 16;
1660 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1661 s = 32;
1662 else
1663 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001665 um = (uint64_t)2 << (s-1);
1666 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001667
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 if (uv > 127 && uv < (uint64_t)-128 &&
1669 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001670 /* If this wasn't explicitly byte-sized, warn as though we
1671 * had fallen through to the imm16/32/64 case.
1672 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001673 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001674 "%s value exceeds bounds",
1675 (opx->type & BITS8) ? "signed byte" :
1676 s == 16 ? "word" :
1677 s == 32 ? "dword" :
1678 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001680 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001681 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001682 out(offset, segment, &data, OUT_ADDRESS, 1,
1683 opx->segment, opx->wrt);
1684 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001685 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001686 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1687 NO_SEG);
1688 }
1689 offset += 1;
1690 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001692
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001697 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001699 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 offset += 1;
1701 } else
1702 offset += 0;
1703 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001704
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001706 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001708 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 offset += 1;
1710 } else
1711 offset += 0;
1712 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001713
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 case 0312:
1715 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001716
Keith Kaniosb7a89542007-04-12 02:40:54 +00001717 case 0313:
1718 ins->rex = 0;
1719 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 case4(0314):
1722 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001723
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001727
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001729 case 0323:
1730 break;
1731
Keith Kaniosb7a89542007-04-12 02:40:54 +00001732 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001733 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001735
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 case 0325:
1737 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001738
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001739 case 0326:
1740 break;
1741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001743 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001744 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 offset += 1;
1746 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001753 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001754 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 offset += 1;
1756 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757
Keith Kanios48af1772007-08-17 07:37:52 +00001758 case 0334:
1759 if (ins->rex & REX_R) {
1760 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001761 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001762 offset += 1;
1763 }
1764 ins->rex &= ~(REX_L|REX_R);
1765 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001766
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001767 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001768 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001769
H. Peter Anvin962e3052008-08-28 17:47:16 -07001770 case 0336:
1771 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001772 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 if (ins->oprs[0].segment != NO_SEG)
1776 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1777 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001778 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 if (size > 0)
1780 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001781 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 offset += size;
1783 }
1784 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001785
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001786 case 0341:
1787 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case 0360:
1790 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case 0361:
1793 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001794 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1795 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001797
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 case 0364:
1799 case 0365:
1800 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001801
Keith Kanios48af1772007-08-17 07:37:52 +00001802 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001803 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001804 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001805 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001806 offset += 1;
1807 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001808
Jin Kyu Song03041092013-10-15 19:38:51 -07001809 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0373:
1813 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001814 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 offset += 1;
1816 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001817
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001818 case 0374:
1819 eat = EA_XMMVSIB;
1820 break;
1821
1822 case 0375:
1823 eat = EA_YMMVSIB;
1824 break;
1825
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001826 case 0376:
1827 eat = EA_ZMMVSIB;
1828 break;
1829
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001830 case4(0100):
1831 case4(0110):
1832 case4(0120):
1833 case4(0130):
1834 case4(0200):
1835 case4(0204):
1836 case4(0210):
1837 case4(0214):
1838 case4(0220):
1839 case4(0224):
1840 case4(0230):
1841 case4(0234):
1842 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 ea ea_data;
1844 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001846 uint8_t *p;
1847 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001849
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001850 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 /* pick rfield from operand b (opx) */
1852 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001853 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 } else {
1855 /* rfield is constant */
1856 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001860 if (process_ea(opy, &ea_data, bits,
1861 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001863
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 p = bytes;
1865 *p++ = ea_data.modrm;
1866 if (ea_data.sib_present)
1867 *p++ = ea_data.sib;
1868
1869 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001870 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001872 /*
1873 * Make sure the address gets the right offset in case
1874 * the line breaks in the .lst file (BR 1197827)
1875 */
1876 offset += s;
1877 s = 0;
1878
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 switch (ea_data.bytes) {
1880 case 0:
1881 break;
1882 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 case 2:
1884 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001885 case 8:
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001886 /* use compressed displacement, if available */
1887 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 if (ea_data.rip) {
1890 if (opy->segment == segment) {
1891 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001892 if (overflow_signed(data, ea_data.bytes))
1893 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001894 out(offset, segment, &data, OUT_ADDRESS,
1895 ea_data.bytes, NO_SEG, NO_SEG);
1896 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001897 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001898 out(offset, segment, &data, OUT_REL4ADR,
1899 insn_end - offset, opy->segment, opy->wrt);
1900 }
1901 } else {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001902 if (overflow_general(data, ins->addr_size >> 3) ||
1903 signed_bits(data, ins->addr_size) !=
1904 signed_bits(data, ea_data.bytes * 8))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001905 warn_overflow(ERR_PASS2, ea_data.bytes);
1906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 out(offset, segment, &data, OUT_ADDRESS,
1908 ea_data.bytes, opy->segment, opy->wrt);
1909 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001911 default:
1912 /* Impossible! */
1913 errfunc(ERR_PANIC,
1914 "Invalid amount of bytes (%d) for offset?!",
1915 ea_data.bytes);
1916 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 }
1918 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001919 }
1920 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 default:
1923 errfunc(ERR_PANIC, "internal instruction table corrupt"
1924 ": instruction code \\%o (0x%02X) given", c, c);
1925 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001927 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001928}
1929
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001930static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001931{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001932 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001933 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001934 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001935}
1936
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001937static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001938{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001939 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001941 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001942}
1943
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001944static int op_rexflags(const operand * o, int mask)
1945{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001946 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947 int val;
1948
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001949 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001950 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001951
H. Peter Anvina4835d42008-05-20 14:21:29 -07001952 flags = nasm_reg_flags[o->basereg];
1953 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001954
1955 return rexflags(val, flags, mask);
1956}
1957
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001958static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001959{
1960 int rex = 0;
1961
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001962 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001963 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001965 rex |= REX_W;
1966 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1967 rex |= REX_H;
1968 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1969 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001970
1971 return rex & mask;
1972}
1973
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001974static int evexflags(int val, decoflags_t deco,
1975 int mask, uint8_t byte)
1976{
1977 int evex = 0;
1978
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001979 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001980 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001981 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001982 evex |= (EVEX_P0RP | EVEX_P0X);
1983 break;
1984 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001985 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001986 evex |= EVEX_P2VP;
1987 if (deco & Z)
1988 evex |= EVEX_P2Z;
1989 if (deco & OPMASK_MASK)
1990 evex |= deco & EVEX_P2AAA;
1991 break;
1992 }
1993 return evex & mask;
1994}
1995
1996static int op_evexflags(const operand * o, int mask, uint8_t byte)
1997{
1998 int val;
1999
2000 if (!is_register(o->basereg))
2001 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
2002
2003 val = nasm_regvals[o->basereg];
2004
2005 return evexflags(val, o->decoflags, mask, byte);
2006}
2007
H. Peter Anvin23595f52009-07-25 17:44:25 -07002008static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002009 insn *instruction,
2010 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002011{
2012 const struct itemplate *temp;
2013 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002014 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002015 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002016 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002017 int i;
2018
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002019 /* broadcasting uses a different data element size */
2020 for (i = 0; i < instruction->operands; i++)
2021 if (i == broadcast)
2022 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2023 else
2024 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002025
2026 merr = MERR_INVALOP;
2027
2028 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002029 temp->opcode != I_none; temp++) {
2030 m = matches(temp, instruction, bits);
2031 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002032 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002033 m = MOK_GOOD;
2034 else
2035 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002036 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002037 /*
2038 * Missing operand size and a candidate for fuzzy matching...
2039 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002040 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002041 if (i == broadcast)
2042 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2043 else
2044 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002045 opsizemissing = true;
2046 }
2047 if (m > merr)
2048 merr = m;
2049 if (merr == MOK_GOOD)
2050 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002051 }
2052
2053 /* No match, but see if we can get a fuzzy operand size match... */
2054 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002055 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002056
2057 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002058 /*
2059 * We ignore extrinsic operand sizes on registers, so we should
2060 * never try to fuzzy-match on them. This also resolves the case
2061 * when we have e.g. "xmmrm128" in two different positions.
2062 */
2063 if (is_class(REGISTER, instruction->oprs[i].type))
2064 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002065
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002066 /* This tests if xsizeflags[i] has more than one bit set */
2067 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2068 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002069
Jin Kyu Song7903c072013-10-30 03:00:12 -07002070 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002071 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002072 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2073 BITS32 : BITS64);
2074 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002075 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002076 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002077 }
2078
2079 /* Try matching again... */
2080 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 temp->opcode != I_none; temp++) {
2082 m = matches(temp, instruction, bits);
2083 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002084 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 m = MOK_GOOD;
2086 else
2087 m = MERR_INVALOP;
2088 }
2089 if (m > merr)
2090 merr = m;
2091 if (merr == MOK_GOOD)
2092 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002093 }
2094
H. Peter Anvina81655b2009-07-25 18:15:28 -07002095done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002096 *tempp = temp;
2097 return merr;
2098}
2099
H. Peter Anvin65289e82009-07-25 17:25:11 -07002100static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002101 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002102{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002103 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002104 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002105 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002106
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002107 /*
2108 * Check the opcode
2109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002111 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002112
2113 /*
2114 * Count the operands
2115 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002117 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002118
2119 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002120 * Is it legal?
2121 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002122 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002123 return MERR_INVALOP;
2124
2125 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002126 * {evex} available?
2127 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002128 switch (instruction->prefixes[PPS_VEX]) {
2129 case P_EVEX:
2130 if (!itemp_has(itemp, IF_EVEX))
2131 return MERR_ENCMISMATCH;
2132 break;
2133 case P_VEX3:
2134 case P_VEX2:
2135 if (!itemp_has(itemp, IF_VEX))
2136 return MERR_ENCMISMATCH;
2137 break;
2138 default:
2139 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002140 }
2141
2142 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002143 * Check that no spurious colons or TOs are present
2144 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 for (i = 0; i < itemp->operands; i++)
2146 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002147 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002148
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002149 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002150 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002151 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002152 switch (itemp_smask(itemp)) {
2153 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002154 asize = BITS8;
2155 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002156 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002157 asize = BITS16;
2158 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002159 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002160 asize = BITS32;
2161 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002162 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002163 asize = BITS64;
2164 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002165 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002166 asize = BITS128;
2167 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002168 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002169 asize = BITS256;
2170 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002171 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002172 asize = BITS512;
2173 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002174 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002175 switch (bits) {
2176 case 16:
2177 asize = BITS16;
2178 break;
2179 case 32:
2180 asize = BITS32;
2181 break;
2182 case 64:
2183 asize = BITS64;
2184 break;
2185 default:
2186 asize = 0;
2187 break;
2188 }
2189 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002190 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002191 asize = 0;
2192 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002193 }
2194
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002195 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002196 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002197 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002198 memset(size, 0, sizeof size);
2199 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002200 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002201 /* S- flags apply to all operands */
2202 for (i = 0; i < MAX_OPERANDS; i++)
2203 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002204 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002205
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002206 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002207 * Check that the operand flags all match up,
2208 * it's a bit tricky so lets be verbose:
2209 *
2210 * 1) Find out the size of operand. If instruction
2211 * doesn't have one specified -- we're trying to
2212 * guess it either from template (IF_S* flag) or
2213 * from code bits.
2214 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002215 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002216 * template has an operand size specified AND this size differ
2217 * from which instruction has (perhaps we got it from code bits)
2218 * we are:
2219 * a) Check that only size of instruction and operand is differ
2220 * other characteristics do match
2221 * b) Perhaps it's a register specified in instruction so
2222 * for such a case we just mark that operand as "size
2223 * missing" and this will turn on fuzzy operand size
2224 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002225 */
2226 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002227 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002228 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002229 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002230 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002231 opflags_t template_opsize, insn_opsize;
2232
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002233 if (!(type & SIZE_MASK))
2234 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002235
Jin Kyu Song7903c072013-10-30 03:00:12 -07002236 insn_opsize = type & SIZE_MASK;
2237 if (!is_broadcast) {
2238 template_opsize = itemp->opd[i] & SIZE_MASK;
2239 } else {
2240 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2241 /*
2242 * when broadcasting, the element size depends on
2243 * the instruction type. decorator flag should match.
2244 */
2245
2246 if (deco_brsize) {
2247 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002248 /* calculate the proper number : {1to<brcast_num>} */
2249 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2250 BITS64 / template_opsize * 2;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002251 } else {
2252 template_opsize = 0;
2253 }
2254 }
2255
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002256 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002257 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002258 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002259 } else if (template_opsize) {
2260 if (template_opsize != insn_opsize) {
2261 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002262 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002263 } else if (!is_class(REGISTER, type)) {
2264 /*
2265 * Note: we don't honor extrinsic operand sizes for registers,
2266 * so "missing operand size" for a register should be
2267 * considered a wildcard match rather than an error.
2268 */
2269 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002270 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002271 } else if (is_broadcast &&
2272 (brcast_num !=
2273 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2274 /*
2275 * broadcasting opsize matches but the number of repeated memory
2276 * element does not match.
2277 * if 64b double precision float is broadcasted to zmm (512b),
2278 * broadcasting decorator must be {1to8}.
2279 */
2280 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002281 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002282 }
2283 }
2284
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002285 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002286 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002287
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002288 /*
2289 * Check operand sizes
2290 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002291 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2292 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002294 asize = itemp->opd[i] & SIZE_MASK;
2295 if (asize) {
2296 for (i = 0; i < oprs; i++)
2297 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 break;
2299 }
2300 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002301 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002303 }
2304
Keith Kaniosb7a89542007-04-12 02:40:54 +00002305 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 if (!(itemp->opd[i] & SIZE_MASK) &&
2307 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002308 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002309 }
2310
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002311 /*
2312 * Check template is okay at the set cpu level
2313 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002314 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002315 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002316
Keith Kaniosb7a89542007-04-12 02:40:54 +00002317 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002318 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002319 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002320 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002321 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002323 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002324 * If we have a HLE prefix, look for the NOHLE flag
2325 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002326 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002327 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2328 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2329 return MERR_BADHLE;
2330
2331 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002332 * Check if special handling needed for Jumps
2333 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002334 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002335 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336
Jin Kyu Song03041092013-10-15 19:38:51 -07002337 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002338 * Check if BND prefix is allowed.
2339 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002340 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002341 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002342 (has_prefix(instruction, PPS_REP, P_BND) ||
2343 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002344 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002345 else if (itemp_has(itemp, IF_BND) &&
2346 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2347 has_prefix(instruction, PPS_REP, P_REPNZ)))
2348 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002349
H. Peter Anvin60926242009-07-26 16:25:38 -07002350 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002351}
2352
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002353/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002354 * Check if ModR/M.mod should/can be 01.
2355 * - EAF_BYTEOFFS is set
2356 * - offset can fit in a byte when EVEX is not used
2357 * - offset can be compressed when EVEX is used
2358 */
2359#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2360 (o >= -128 && o <= 127 && \
2361 seg == NO_SEG && !forw_ref && \
2362 !(input->eaflags & EAF_WORDOFFS) && \
2363 !(ins->rex & REX_EV)) || \
2364 (ins->rex & REX_EV && \
2365 is_disp8n(input, ins, &output->disp8)))
2366
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002367static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002368 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002369{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002370 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002371 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002372 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002373
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002374 output->type = EA_SCALAR;
2375 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002376 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002377
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002378 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002379 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002380 /* EVEX.R' flag for the REG operand */
2381 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002382
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002383 if (is_class(REGISTER, input->type)) {
2384 /*
2385 * It's a direct register.
2386 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002387 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002388 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002389
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002390 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002391 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002392
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002393 /* broadcasting is not available with a direct register operand. */
2394 if (input->decoflags & BRDCAST_MASK) {
2395 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2396 goto err;
2397 }
2398
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002399 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002400 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002401 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002402 output->bytes = 0; /* no offset necessary either */
2403 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2404 } else {
2405 /*
2406 * It's a memory reference.
2407 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002408
2409 /* Embedded rounding or SAE is not available with a mem ref operand. */
2410 if (input->decoflags & (ER | SAE)) {
2411 nasm_error(ERR_NONFATAL,
2412 "Embedded rounding is available only with reg-reg op.");
2413 return -1;
2414 }
2415
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002416 if (input->basereg == -1 &&
2417 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002418 /*
2419 * It's a pure offset.
2420 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002421 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2422 input->segment == NO_SEG) {
2423 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2424 input->type &= ~IP_REL;
2425 input->type |= MEMORY;
2426 }
2427
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002428 if (bits == 64 &&
2429 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2430 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2431 return -1;
2432 }
2433
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002434 if (eaflags & EAF_BYTEOFFS ||
2435 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002436 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2437 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2438 }
2439
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002440 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002441 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002442 output->sib = GEN_SIB(0, 4, 5);
2443 output->bytes = 4;
2444 output->modrm = GEN_MODRM(0, rfield, 4);
2445 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002446 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002447 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002448 output->bytes = (addrbits != 16 ? 4 : 2);
2449 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2450 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002451 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002452 } else {
2453 /*
2454 * It's an indirection.
2455 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002457 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002459 int t, it, bt; /* register numbers */
2460 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 if (s == 0)
2463 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002464
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002465 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002466 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002467 ix = nasm_reg_flags[i];
2468 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002469 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 ix = 0;
2471 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002474 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002475 bx = nasm_reg_flags[b];
2476 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002477 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002478 bx = 0;
2479 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002480
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002481 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002482 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002483 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002484 int32_t o = input->offset;
2485 int mod, scale, index, base;
2486
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002487 /*
2488 * For a vector SIB, one has to be a vector and the other,
2489 * if present, a GPR. The vector must be the index operand.
2490 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002491 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002492 if (s == 0)
2493 s = 1;
2494 else if (s != 1)
2495 goto err;
2496
2497 t = bt, bt = it, it = t;
2498 x = bx, bx = ix, ix = x;
2499 }
2500
2501 if (bt != -1) {
2502 if (REG_GPR & ~bx)
2503 goto err;
2504 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2505 sok &= bx;
2506 else
2507 goto err;
2508 }
2509
2510 /*
2511 * While we're here, ensure the user didn't specify
2512 * WORD or QWORD
2513 */
2514 if (input->disp_size == 16 || input->disp_size == 64)
2515 goto err;
2516
2517 if (addrbits == 16 ||
2518 (addrbits == 32 && !(sok & BITS32)) ||
2519 (addrbits == 64 && !(sok & BITS64)))
2520 goto err;
2521
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002522 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2523 : ((ix & YMMREG & ~REG_EA)
2524 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002525
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002526 output->rex |= rexflags(it, ix, REX_X);
2527 output->rex |= rexflags(bt, bx, REX_B);
2528 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002529
2530 index = it & 7; /* it is known to be != -1 */
2531
2532 switch (s) {
2533 case 1:
2534 scale = 0;
2535 break;
2536 case 2:
2537 scale = 1;
2538 break;
2539 case 4:
2540 scale = 2;
2541 break;
2542 case 8:
2543 scale = 3;
2544 break;
2545 default: /* then what the smeg is it? */
2546 goto err; /* panic */
2547 }
2548
2549 if (bt == -1) {
2550 base = 5;
2551 mod = 0;
2552 } else {
2553 base = (bt & 7);
2554 if (base != REG_NUM_EBP && o == 0 &&
2555 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002556 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002557 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002558 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 mod = 1;
2560 else
2561 mod = 2;
2562 }
2563
2564 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002565 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2566 output->modrm = GEN_MODRM(mod, rfield, 4);
2567 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002568 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002569 /*
2570 * it must be a 32/64-bit memory reference. Firstly we have
2571 * to check that all registers involved are type E/Rxx.
2572 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002573 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002575
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002576 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002577 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2578 sok &= ix;
2579 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002581 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002582
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002583 if (bt != -1) {
2584 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002585 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002586 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002587 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002588 sok &= bx;
2589 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002590
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002591 /*
2592 * While we're here, ensure the user didn't specify
2593 * WORD or QWORD
2594 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002595 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002596 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002598 if (addrbits == 16 ||
2599 (addrbits == 32 && !(sok & BITS32)) ||
2600 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002602
Keith Kaniosb7a89542007-04-12 02:40:54 +00002603 /* now reorganize base/index */
2604 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002605 ((hb == b && ht == EAH_NOTBASE) ||
2606 (hb == i && ht == EAH_MAKEBASE))) {
2607 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002608 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 x = bx, bx = ix, ix = x;
2610 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002611
Jin Kyu Song164d6072013-10-15 19:10:13 -07002612 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002613 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002614 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002615 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002616 if (eaflags & EAF_MIB) {
2617 /* only for mib operands */
2618 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2619 /*
2620 * make a single reg index [reg*1].
2621 * gas uses this form for an explicit index register.
2622 */
2623 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2624 }
2625 if ((ht == EAH_SUMMED) && bt == -1) {
2626 /* separate once summed index into [base, index] */
2627 bt = it, bx = ix, s--;
2628 }
2629 } else {
2630 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002631 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002632 s == 3 || s == 5 || s == 9) && bt == -1) {
2633 /* convert 3*EAX to EAX+2*EAX */
2634 bt = it, bx = ix, s--;
2635 }
2636 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002637 (eaflags & EAF_TIMESTWO) &&
2638 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002639 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002640 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002641 * to sib format with 0x0 displacement - [EAX*1+0].
2642 */
2643 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2644 }
2645 }
Keith Kanios48af1772007-08-17 07:37:52 +00002646 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002647 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002648 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002649 x = ix, ix = bx, bx = x;
2650 }
2651 if (it == REG_NUM_ESP ||
2652 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002654
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 output->rex |= rexflags(it, ix, REX_X);
2656 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002657
Keith Kanios48af1772007-08-17 07:37:52 +00002658 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002661
Keith Kaniosb7a89542007-04-12 02:40:54 +00002662 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002665 } else {
2666 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002667 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002669 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002670 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002671 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002672 mod = 1;
2673 else
2674 mod = 2;
2675 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002676
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002677 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002678 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2679 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002680 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002681 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002682 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002683
Keith Kaniosb7a89542007-04-12 02:40:54 +00002684 if (it == -1)
2685 index = 4, s = 1;
2686 else
2687 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002688
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 switch (s) {
2690 case 1:
2691 scale = 0;
2692 break;
2693 case 2:
2694 scale = 1;
2695 break;
2696 case 4:
2697 scale = 2;
2698 break;
2699 case 8:
2700 scale = 3;
2701 break;
2702 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002703 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002705
Keith Kaniosb7a89542007-04-12 02:40:54 +00002706 if (bt == -1) {
2707 base = 5;
2708 mod = 0;
2709 } else {
2710 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002711 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002712 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002713 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002715 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002716 mod = 1;
2717 else
2718 mod = 2;
2719 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002720
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002721 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002722 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2723 output->modrm = GEN_MODRM(mod, rfield, 4);
2724 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 }
2726 } else { /* it's 16-bit */
2727 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002728 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002729
Keith Kaniosb7a89542007-04-12 02:40:54 +00002730 /* check for 64-bit long mode */
2731 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002732 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002733
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002735 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2736 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002737 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002738
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002740 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002741 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002742
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002744 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 if (b == -1 && i != -1) {
2746 int tmp = b;
2747 b = i;
2748 i = tmp;
2749 } /* swap */
2750 if ((b == R_SI || b == R_DI) && i != -1) {
2751 int tmp = b;
2752 b = i;
2753 i = tmp;
2754 }
2755 /* have BX/BP as base, SI/DI index */
2756 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002757 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 if (i != -1 && b != -1 &&
2759 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002760 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002761 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002762 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 rm = -1;
2765 if (i != -1)
2766 switch (i * 256 + b) {
2767 case R_SI * 256 + R_BX:
2768 rm = 0;
2769 break;
2770 case R_DI * 256 + R_BX:
2771 rm = 1;
2772 break;
2773 case R_SI * 256 + R_BP:
2774 rm = 2;
2775 break;
2776 case R_DI * 256 + R_BP:
2777 rm = 3;
2778 break;
2779 } else
2780 switch (b) {
2781 case R_SI:
2782 rm = 4;
2783 break;
2784 case R_DI:
2785 rm = 5;
2786 break;
2787 case R_BP:
2788 rm = 6;
2789 break;
2790 case R_BX:
2791 rm = 7;
2792 break;
2793 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002794 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002796
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002797 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002798 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002800 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 mod = 1;
2802 else
2803 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002804
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002805 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002806 output->bytes = mod; /* bytes of offset needed */
2807 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 }
2809 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002811
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002812 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002813 return output->type;
2814
2815err:
2816 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002817}
2818
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002819static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002820{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002821 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002822 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002823
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002824 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002825
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002826 switch (ins->prefixes[PPS_ASIZE]) {
2827 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002828 valid &= 16;
2829 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002830 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002831 valid &= 32;
2832 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002833 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002834 valid &= 64;
2835 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002836 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 valid &= (addrbits == 32) ? 16 : 32;
2838 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002839 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002840 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002841 }
2842
2843 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 if (is_class(MEMORY, ins->oprs[j].type)) {
2845 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002846
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002848 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 i = 0;
2850 else
2851 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002852
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002853 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002854 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 b = 0;
2856 else
2857 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002858
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002859 if (ins->oprs[j].scale == 0)
2860 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002861
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 if (!i && !b) {
2863 int ds = ins->oprs[j].disp_size;
2864 if ((addrbits != 64 && ds > 8) ||
2865 (addrbits == 64 && ds == 16))
2866 valid &= ds;
2867 } else {
2868 if (!(REG16 & ~b))
2869 valid &= 16;
2870 if (!(REG32 & ~b))
2871 valid &= 32;
2872 if (!(REG64 & ~b))
2873 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002874
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002875 if (!(REG16 & ~i))
2876 valid &= 16;
2877 if (!(REG32 & ~i))
2878 valid &= 32;
2879 if (!(REG64 & ~i))
2880 valid &= 64;
2881 }
2882 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002883 }
2884
2885 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002887 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002888 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002889 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002891 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 /* Impossible... */
2893 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2894 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002895 }
2896
2897 defdisp = ins->addr_size == 16 ? 16 : 32;
2898
2899 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002900 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2901 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2902 /*
2903 * mem_offs sizes must match the address size; if not,
2904 * strip the MEM_OFFS bit and match only EA instructions
2905 */
2906 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2907 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002908 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002909}