blob: b119f86987f76828cd6be2248f249cf6cc4faf05 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08003 * Copyright 1996-2012 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070045 * \20..\23 - a byte immediate operand, from operand 0..3
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040046 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070047 * \30..\33 - a word immediate operand, from operand 0..3
48 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000049 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070050 * \40..\43 - a long immediate operand, from operand 0..3
51 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040052 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070053 * \50..\53 - a byte relative operand, from operand 0..3
54 * \54..\57 - a qword immediate operand, from operand 0..3
55 * \60..\63 - a word relative operand, from operand 0..3
56 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000057 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070058 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070059 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060 * \1ab - a ModRM, calculated on EA in operand a, with the spare
61 * field the register value of operand b.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040062 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070063 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040064 * \173\xab - the register number from operand a in bits 7..4, with
65 * the value b in bits 3..0.
H. Peter Anvincffe61e2011-07-07 17:21:24 -070066 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040067 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068 * \2ab - a ModRM, calculated on EA in operand a, with the spare
69 * field equal to digit b.
H. Peter Anvin588df782008-10-07 10:05:10 -070070 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -070071 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040072 * V field taken from operand 0..3.
73 * \270 - this instruction uses VEX/XOP rather than REX, with the
74 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -070075 *
H. Peter Anvina04019c2009-05-03 21:42:34 -070076 * VEX/XOP prefixes are followed by the sequence:
77 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -070078 * 00 wwl lpp
79 * [l0] ll = 0 for L = 0 (.128, .lz)
80 * [l1] ll = 1 for L = 1 (.256)
81 * [lig] ll = 2 for L don't care (always assembled as 0)
82 *
H. Peter Anvin978c2172010-08-16 13:48:43 -070083 * [w0] ww = 0 for W = 0
84 * [w1 ] ww = 1 for W = 1
85 * [wig] ww = 2 for W don't care (always assembled as 0)
86 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -070087 *
H. Peter Anvina04019c2009-05-03 21:42:34 -070088 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -070089 *
H. Peter Anvin574784d2012-02-25 22:33:46 -080090 * \271 - instruction takes XRELEASE (F3) with or without lock
91 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
92 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +040093 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
94 * to the operand size (if o16/o32/o64 present) or the bit size
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000095 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
96 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -070097 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +000098 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -080099 * \314 - (disassembler only) invalid with REX.B
100 * \315 - (disassembler only) invalid with REX.X
101 * \316 - (disassembler only) invalid with REX.R
102 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000103 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
104 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
105 * \322 - indicates that this instruction is only valid when the
106 * operand size is the default (instruction to disassembler,
107 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000108 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000109 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400110 * \325 - instruction which always uses spl/bpl/sil/dil
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +0400111 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
112 disassembler only; for SSE instructions.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113 * \330 - a literal byte follows in the code stream, to be added
114 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000115 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000116 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700117 * \332 - REP prefix (0xF2 byte) used as opcode extension.
118 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700119 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700120 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin755f5212012-02-25 11:41:34 -0800121 * \336 - force a REP(E) prefix (0xF3) even if not specified.
122 * \337 - force a REPNE prefix (0xF2) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700123 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000124 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400126 * \341 - this instruction needs a WAIT "prefix"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400127 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700128 * \361 - 66 SSE prefix (== \366\331)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000129 * \364 - operand-size prefix (0x66) not permitted
130 * \365 - address-size prefix (0x67) not permitted
131 * \366 - operand-size prefix (0x66) used as opcode extension
132 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin755f5212012-02-25 11:41:34 -0800133 * \370,\371 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400134 * 370 is used for Jcc, 371 is used for JMP.
135 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
136 * used for conditional jump over longer jump
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700137 * \374 - this instruction takes an XMM VSIB memory EA
138 * \375 - this instruction takes an YMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000139 */
140
H. Peter Anvinfe501952007-10-02 21:53:51 -0700141#include "compiler.h"
142
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143#include <stdio.h>
144#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000145#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000146
147#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000149#include "assemble.h"
150#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700151#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000152
H. Peter Anvin65289e82009-07-25 17:25:11 -0700153enum match_result {
154 /*
155 * Matching errors. These should be sorted so that more specific
156 * errors come later in the sequence.
157 */
158 MERR_INVALOP,
159 MERR_OPSIZEMISSING,
160 MERR_OPSIZEMISMATCH,
161 MERR_BADCPU,
162 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800163 MERR_BADHLE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700164 /*
165 * Matching success; the conditional ones first
166 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400167 MOK_JUMP, /* Matching OK but needs jmp_match() */
168 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700169};
170
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000171typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700172 enum ea_type type; /* what kind of EA is this? */
173 int sib_present; /* is a SIB byte necessary? */
174 int bytes; /* # of bytes of offset needed */
175 int size; /* lazy - this is sib+bytes+1 */
176 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177} ea;
178
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400179#define GEN_SIB(scale, index, base) \
180 (((scale) << 6) | ((index) << 3) | ((base)))
181
182#define GEN_MODRM(mod, reg, rm) \
183 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
184
Keith Kaniosb7a89542007-04-12 02:40:54 +0000185static uint32_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186static efunc errfunc;
187static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000188static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800190static int64_t calcsize(int32_t, int64_t, int, insn *,
191 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700192static void gencode(int32_t segment, int64_t offset, int bits,
193 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400194 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700195static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400196 insn *instruction,
197 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700198static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700199static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000200static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700201static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000202static int op_rexflags(const operand *, int);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700203static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000204
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700205static enum ea_type process_ea(operand *, ea *, int, int, int, opflags_t);
206
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400207static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000208{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700209 return ins->prefixes[pos] == prefix;
210}
211
212static void assert_no_prefix(insn * ins, enum prefix_pos pos)
213{
214 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400215 errfunc(ERR_NONFATAL, "invalid %s prefix",
216 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700217}
218
219static const char *size_name(int size)
220{
221 switch (size) {
222 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400223 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700224 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400225 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700226 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400227 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700228 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400229 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700230 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400231 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700232 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400233 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700234 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400235 return "yword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700236 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000238 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700239}
240
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400241static void warn_overflow(int pass, int size)
242{
243 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
244 "%s data exceeds bounds", size_name(size));
245}
246
247static void warn_overflow_const(int64_t data, int size)
248{
249 if (overflow_general(data, size))
250 warn_overflow(ERR_PASS1, size);
251}
252
253static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700254{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100255 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400256 if (overflow_general(o->offset, size))
257 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 }
259}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400260
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000261/*
262 * This routine wrappers the real output format's output routine,
263 * in order to pass a copy of the data off to the listing file
264 * generator at the same time.
265 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800266static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800267 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000269{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000270 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000271 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800272 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000273
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800274 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 /*
276 * This is a non-relocated address, and we're going to
277 * convert it into RAWDATA format.
278 */
279 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800280
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 if (size > 8) {
282 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
283 return;
284 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700285
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400286 WRITEADDR(q, *(int64_t *)data, size);
287 data = p;
288 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000289 }
290
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800291 list->output(offset, data, type, size);
292
Frank Kotlerabebb082003-09-06 04:45:37 +0000293 /*
294 * this call to src_get determines when we call the
295 * debug-format-specific "linenum" function
296 * it updates lineno and lnfname to the current values
297 * returning 0 if "same as last time", -2 if lnfname
298 * changed, and the amount by which lineno changed,
299 * if it did. thus, these variables must be static
300 */
301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400302 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000304
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800305 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000306}
307
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400308static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
309{
310 if (opx->segment != NO_SEG) {
311 uint64_t data = opx->offset;
312 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
313 } else {
314 uint8_t byte = opx->offset;
315 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
316 }
317}
318
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700319static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800320 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800322 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800323 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000324 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000325
H. Peter Anvin755f5212012-02-25 11:41:34 -0800326 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700327 return false;
328 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400329 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700330 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400331 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700332
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800333 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100334
Victor van den Elzen154e5922009-02-25 17:32:00 +0100335 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100336 /* Be optimistic in pass 1 */
337 return true;
338
H. Peter Anvine2c80182005-01-15 22:15:51 +0000339 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700340 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000341
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700342 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
343 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000344}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000345
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800346int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400347 insn * instruction, struct ofmt *output, efunc error,
348 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000349{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000350 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700352 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800353 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000354 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800355 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300356 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000357
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000359 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 outfmt = output; /* likewise */
361 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300363 wsize = idata_bytes(instruction->opcode);
364 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000366
H. Peter Anvineba20a72002-04-30 20:53:55 +0000367 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000369 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370 if (t < 0)
371 errfunc(ERR_PANIC,
372 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000373
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400375 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400377 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700378 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400379 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000380 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700381 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800383 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400384 offset += wsize;
385 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700386 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400387 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000389
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800391 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 if (align) {
395 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100396 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800397 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 }
399 offset += e->stringlen + align;
400 }
401 }
402 if (t > 0 && t == instruction->times - 1) {
403 /*
404 * Dummy call to list->output to give the offset to the
405 * listing module.
406 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800407 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 list->uplevel(LIST_TIMES);
409 }
410 }
411 if (instruction->times > 1)
412 list->downlevel(LIST_TIMES);
413 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414 }
415
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700417 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000419
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400420 fp = fopen(fname, "rb");
421 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
423 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400424 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
426 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200427 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400428 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700429 static char buf[4096];
430 size_t t = instruction->times;
431 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400432 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000433
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 len = ftell(fp);
435 if (instruction->eops->next) {
436 base = instruction->eops->next->offset;
437 len -= base;
438 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700439 len > (size_t)instruction->eops->next->next->offset)
440 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 }
442 /*
443 * Dummy call to list->output to give the offset to the
444 * listing module.
445 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800446 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 list->uplevel(LIST_INCBIN);
448 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700449 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000450
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 fseek(fp, base, SEEK_SET);
452 l = len;
453 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700454 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400455 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 if (!m) {
457 /*
458 * This shouldn't happen unless the file
459 * actually changes while we are reading
460 * it.
461 */
462 error(ERR_NONFATAL,
463 "`incbin': unexpected EOF while"
464 " reading file `%s'", fname);
465 t = 0; /* Try to exit cleanly */
466 break;
467 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800468 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 NO_SEG, NO_SEG);
470 l -= m;
471 }
472 }
473 list->downlevel(LIST_INCBIN);
474 if (instruction->times > 1) {
475 /*
476 * Dummy call to list->output to give the offset to the
477 * listing module.
478 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800479 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 list->uplevel(LIST_TIMES);
481 list->downlevel(LIST_TIMES);
482 }
483 fclose(fp);
484 return instruction->times * len;
485 }
486 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000487 }
488
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700489 /* Check to see if we need an address-size prefix */
490 add_asp(instruction, bits);
491
H. Peter Anvin23595f52009-07-25 17:44:25 -0700492 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700493
H. Peter Anvin23595f52009-07-25 17:44:25 -0700494 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400495 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800496 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400497 itimes = instruction->times;
498 if (insn_size < 0) /* shouldn't be, on pass two */
499 error(ERR_PANIC, "errors made it through from pass one");
500 else
501 while (itimes--) {
502 for (j = 0; j < MAXPREFIX; j++) {
503 uint8_t c = 0;
504 switch (instruction->prefixes[j]) {
505 case P_WAIT:
506 c = 0x9B;
507 break;
508 case P_LOCK:
509 c = 0xF0;
510 break;
511 case P_REPNE:
512 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800513 case P_XACQUIRE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400514 c = 0xF2;
515 break;
516 case P_REPE:
517 case P_REPZ:
518 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800519 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400520 c = 0xF3;
521 break;
522 case R_CS:
523 if (bits == 64) {
524 error(ERR_WARNING | ERR_PASS2,
525 "cs segment base generated, but will be ignored in 64-bit mode");
526 }
527 c = 0x2E;
528 break;
529 case R_DS:
530 if (bits == 64) {
531 error(ERR_WARNING | ERR_PASS2,
532 "ds segment base generated, but will be ignored in 64-bit mode");
533 }
534 c = 0x3E;
535 break;
536 case R_ES:
537 if (bits == 64) {
538 error(ERR_WARNING | ERR_PASS2,
539 "es segment base generated, but will be ignored in 64-bit mode");
540 }
541 c = 0x26;
542 break;
543 case R_FS:
544 c = 0x64;
545 break;
546 case R_GS:
547 c = 0x65;
548 break;
549 case R_SS:
550 if (bits == 64) {
551 error(ERR_WARNING | ERR_PASS2,
552 "ss segment base generated, but will be ignored in 64-bit mode");
553 }
554 c = 0x36;
555 break;
556 case R_SEGR6:
557 case R_SEGR7:
558 error(ERR_NONFATAL,
559 "segr6 and segr7 cannot be used as prefixes");
560 break;
561 case P_A16:
562 if (bits == 64) {
563 error(ERR_NONFATAL,
564 "16-bit addressing is not supported "
565 "in 64-bit mode");
566 } else if (bits != 16)
567 c = 0x67;
568 break;
569 case P_A32:
570 if (bits != 32)
571 c = 0x67;
572 break;
573 case P_A64:
574 if (bits != 64) {
575 error(ERR_NONFATAL,
576 "64-bit addressing is only supported "
577 "in 64-bit mode");
578 }
579 break;
580 case P_ASP:
581 c = 0x67;
582 break;
583 case P_O16:
584 if (bits != 16)
585 c = 0x66;
586 break;
587 case P_O32:
588 if (bits == 16)
589 c = 0x66;
590 break;
591 case P_O64:
592 /* REX.W */
593 break;
594 case P_OSP:
595 c = 0x66;
596 break;
597 case P_none:
598 break;
599 default:
600 error(ERR_PANIC, "invalid instruction prefix");
601 }
602 if (c != 0) {
603 out(offset, segment, &c, OUT_RAWDATA, 1,
604 NO_SEG, NO_SEG);
605 offset++;
606 }
607 }
608 insn_end = offset + insn_size;
609 gencode(segment, offset, bits, instruction,
610 temp, insn_end);
611 offset += insn_size;
612 if (itimes > 0 && itimes == instruction->times - 1) {
613 /*
614 * Dummy call to list->output to give the offset to the
615 * listing module.
616 */
617 list->output(offset, NULL, OUT_RAWDATA, 0);
618 list->uplevel(LIST_TIMES);
619 }
620 }
621 if (instruction->times > 1)
622 list->downlevel(LIST_TIMES);
623 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700624 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400625 /* No match */
626 switch (m) {
627 case MERR_OPSIZEMISSING:
628 error(ERR_NONFATAL, "operation size not specified");
629 break;
630 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400632 break;
633 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400635 break;
636 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800637 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400638 bits);
639 break;
640 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 error(ERR_NONFATAL,
642 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400643 break;
644 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000645 }
646 return 0;
647}
648
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800649int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400650 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000652 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700653 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000656 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000657
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400658 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000660
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700661 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
662 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400663 instruction->opcode == I_DT || instruction->opcode == I_DO ||
664 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300666 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000667
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300669 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000670
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400671 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000672 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000673
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400675 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400677 warn_overflow_const(e->offset, wsize);
678 } else if (e->type == EOT_DB_STRING ||
679 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000681
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 align = (-osize) % wsize;
683 if (align < 0)
684 align += wsize;
685 isize += osize + align;
686 }
687 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000688 }
689
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400691 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300693 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700694 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400696 fp = fopen(fname, "rb");
697 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
699 fname);
700 else if (fseek(fp, 0L, SEEK_END) < 0)
701 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
702 fname);
703 else {
704 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 if (instruction->eops->next) {
706 len -= instruction->eops->next->offset;
707 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700708 len > (size_t)instruction->eops->next->next->offset) {
709 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 }
711 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300712 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300714 if (fp)
715 fclose(fp);
716 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000717 }
718
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700719 /* Check to see if we need an address-size prefix */
720 add_asp(instruction, bits);
721
H. Peter Anvin23595f52009-07-25 17:44:25 -0700722 m = find_match(&temp, instruction, segment, offset, bits);
723 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400724 /* we've matched an instruction. */
725 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400726 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100727
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800728 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400729 if (isize < 0)
730 return -1;
731 for (j = 0; j < MAXPREFIX; j++) {
732 switch (instruction->prefixes[j]) {
733 case P_A16:
734 if (bits != 16)
735 isize++;
736 break;
737 case P_A32:
738 if (bits != 32)
739 isize++;
740 break;
741 case P_O16:
742 if (bits != 16)
743 isize++;
744 break;
745 case P_O32:
746 if (bits == 16)
747 isize++;
748 break;
749 case P_A64:
750 case P_O64:
751 case P_none:
752 break;
753 default:
754 isize++;
755 break;
756 }
757 }
758 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700759 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400760 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000761 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000762}
763
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800764static void bad_hle_warn(const insn * ins, uint8_t hleok)
765{
766 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800767 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800768 static const enum whatwarn warn[2][4] =
769 {
770 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
771 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
772 };
773 unsigned int n;
774
775 n = (unsigned int)rep_pfx - P_XACQUIRE;
776 if (n > 1)
777 return; /* Not XACQUIRE/XRELEASE */
778
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800779 ww = warn[n][hleok];
780 if (!is_class(MEMORY, ins->oprs[0].type))
781 ww = w_inval; /* HLE requires operand 0 to be memory */
782
783 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800784 case w_none:
785 break;
786
787 case w_lock:
788 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800789 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800790 "%s with this instruction requires lock",
791 prefix_name(rep_pfx));
792 }
793 break;
794
795 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800796 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800797 "%s invalid with this instruction",
798 prefix_name(rep_pfx));
799 break;
800 }
801}
802
H. Peter Anvin507ae032008-10-09 15:37:10 -0700803/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400804#define case3(x) case (x): case (x)+1: case (x)+2
805#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700806
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800807static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800808 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000809{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800810 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800811 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000812 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000813 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700814 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700815 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700816 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700817 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800818 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800819 bool lockcheck = true;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000820
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700821 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700822 eat = EA_SCALAR; /* Expect a scalar EA */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700823
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700824 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400825 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 (void)segment; /* Don't warn that this parameter is unused */
828 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000829
H. Peter Anvin839eca22007-10-29 23:12:47 -0700830 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400831 c = *codes++;
832 op1 = (c & 3) + ((opex & 1) << 2);
833 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
834 opx = &ins->oprs[op1];
835 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700836
H. Peter Anvin839eca22007-10-29 23:12:47 -0700837 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400838 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 codes += c, length += c;
840 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700841
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400842 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400843 opex = c;
844 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700845
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400846 case4(010):
847 ins->rex |=
848 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 codes++, length++;
850 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700851
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400852 case4(020):
853 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 length++;
855 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400857 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 length += 2;
859 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700860
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400861 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700862 if (opx->type & (BITS16 | BITS32 | BITS64))
863 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 else
865 length += (bits == 16) ? 2 : 4;
866 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400868 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 length += 4;
870 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700871
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400872 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700873 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700875
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400876 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 length++;
878 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700879
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400880 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000881 length += 8; /* MOV reg64/imm */
882 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700883
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400884 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 length += 2;
886 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700887
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400888 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700889 if (opx->type & (BITS16 | BITS32 | BITS64))
890 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 else
892 length += (bits == 16) ? 2 : 4;
893 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400895 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 length += 4;
897 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400899 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700900 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700902
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400903 case 0172:
904 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400905 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700906 length++;
907 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700908
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700909 case4(0174):
910 length++;
911 break;
912
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400913 case4(0254):
914 length += 4;
915 break;
916
917 case4(0260):
918 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700919 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400920 ins->vex_cm = *codes++;
921 ins->vex_wlp = *codes++;
922 break;
923
924 case 0270:
925 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700926 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 ins->vex_cm = *codes++;
928 ins->vex_wlp = *codes++;
929 break;
930
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400931 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800932 hleok = c & 3;
933 break;
934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 case4(0274):
936 length++;
937 break;
938
939 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400943 if (bits == 64)
944 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700945 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700949 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -0700953 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700954
Keith Kaniosb7a89542007-04-12 02:40:54 +0000955 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400956 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
957 has_prefix(ins, PPS_ASIZE, P_A32))
958 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(0314):
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000965 {
966 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
967 if (pfx == P_O16)
968 break;
969 if (pfx != P_none)
970 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
971 else
972 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000974 }
H. Peter Anvin507ae032008-10-09 15:37:10 -0700975
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000977 {
978 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
979 if (pfx == P_O32)
980 break;
981 if (pfx != P_none)
982 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
983 else
984 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000986 }
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 case 0322:
989 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700990
Keith Kaniosb7a89542007-04-12 02:40:54 +0000991 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000992 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
Keith Kaniosb7a89542007-04-12 02:40:54 +0000995 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +0000997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case 0325:
1000 ins->rex |= REX_NH;
1001 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001002
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001003 case 0326:
1004 break;
1005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 case 0330:
1007 codes++, length++;
1008 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001013 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 case 0333:
1015 length++;
1016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 case 0334:
1019 ins->rex |= REX_L;
1020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001022 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001025 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001026 if (!ins->prefixes[PPS_REP])
1027 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001031 if (!ins->prefixes[PPS_REP])
1032 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 if (ins->oprs[0].segment != NO_SEG)
1037 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1038 " quantity of BSS space");
1039 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001040 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001043 case 0341:
1044 if (!ins->prefixes[PPS_WAIT])
1045 ins->prefixes[PPS_WAIT] = P_WAIT;
1046 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001047
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001048 case 0360:
1049 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001050
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001051 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001052 length++;
1053 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001054
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001055 case 0364:
1056 case 0365:
1057 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
Keith Kanios48af1772007-08-17 07:37:52 +00001059 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001060 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 length++;
1062 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001063
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001064 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001066
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 case 0373:
1068 length++;
1069 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001071 case 0374:
1072 eat = EA_XMMVSIB;
1073 break;
1074
1075 case 0375:
1076 eat = EA_YMMVSIB;
1077 break;
1078
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 case4(0100):
1080 case4(0110):
1081 case4(0120):
1082 case4(0130):
1083 case4(0200):
1084 case4(0204):
1085 case4(0210):
1086 case4(0214):
1087 case4(0220):
1088 case4(0224):
1089 case4(0230):
1090 case4(0234):
1091 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001093 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 opflags_t rflags;
1095 struct operand *opy = &ins->oprs[op2];
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001096
Keith Kaniosb7a89542007-04-12 02:40:54 +00001097 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001098
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001099 if (c <= 0177) {
1100 /* pick rfield from operand b (opx) */
1101 rflags = regflag(opx);
1102 rfield = nasm_regvals[opx->basereg];
1103 } else {
1104 rflags = 0;
1105 rfield = c & 7;
1106 }
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001107 if (process_ea(opy, &ea_data, bits,ins->addr_size,
1108 rfield, rflags) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 errfunc(ERR_NONFATAL, "invalid effective address");
1110 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001111 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001112 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001114 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001115 }
1116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 default:
1119 errfunc(ERR_PANIC, "internal instruction table corrupt"
1120 ": instruction code \\%o (0x%02X) given", c, c);
1121 break;
1122 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001123 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001124
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001125 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001126
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001127 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001128 if (ins->rex & REX_H) {
1129 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1130 return -1;
1131 }
1132 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001133 }
1134
H. Peter Anvind85d2502008-05-04 17:53:31 -07001135 if (ins->rex & REX_V) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001136 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 if (ins->rex & REX_H) {
1139 errfunc(ERR_NONFATAL, "cannot use high register in vex instruction");
1140 return -1;
1141 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001142 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001144 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 ins->rex &= ~REX_W;
1146 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001147 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 ins->rex |= REX_W;
1149 bad32 &= ~REX_W;
1150 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001151 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001152 /* Follow REX_W */
1153 break;
1154 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001155
H. Peter Anvinfc561202011-07-07 16:58:22 -07001156 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001157 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1158 return -1;
1159 }
H. Peter Anvin3cb0e8c2010-11-16 09:36:58 -08001160 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 length += 3;
1162 else
1163 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001164 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001165 if (ins->rex & REX_H) {
1166 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1167 return -1;
1168 } else if (bits == 64) {
1169 length++;
1170 } else if ((ins->rex & REX_L) &&
1171 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1172 cpu >= IF_X86_64) {
1173 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001174 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001175 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 length++;
1177 } else {
1178 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1179 return -1;
1180 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001181 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001182
1183 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1184 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001185 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001186 "instruction is not lockable");
1187 }
1188
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001189 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001190
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001191 return length;
1192}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001193
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001194static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1195{
1196 if (bits == 64) {
1197 if ((ins->rex & REX_REAL) && !(ins->rex & REX_V)) {
1198 ins->rex = (ins->rex & REX_REAL) | REX_P;
1199 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1200 ins->rex = 0;
1201 return 1;
1202 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001203 }
1204
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001205 return 0;
1206}
1207
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001208static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001209 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001210 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001211{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001212 uint8_t c;
1213 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001214 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001215 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001216 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001217 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001218 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001219 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001220 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001221
H. Peter Anvin839eca22007-10-29 23:12:47 -07001222 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001223 c = *codes++;
1224 op1 = (c & 3) + ((opex & 1) << 2);
1225 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1226 opx = &ins->oprs[op1];
1227 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001228
H. Peter Anvin839eca22007-10-29 23:12:47 -07001229 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 case 01:
1231 case 02:
1232 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001233 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001234 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001235 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 codes += c;
1237 offset += c;
1238 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001239
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 case 05:
1241 case 06:
1242 case 07:
1243 opex = c;
1244 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001245
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001247 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001248 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001249 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 offset += 1;
1251 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001254 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001255 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001258 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 offset += 1;
1260 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001261
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001263 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001264 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001266 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 offset += 1;
1268 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001269
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001271 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001272 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001273 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001274 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 offset += 2;
1276 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001277
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001279 if (opx->type & (BITS16 | BITS32))
1280 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 else
1282 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001283 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001284 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001285 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001286 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 offset += size;
1288 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001289
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001290 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001291 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001292 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001293 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001294 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 offset += 4;
1296 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001297
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001299 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001300 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001301 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001302 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001303 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 offset += size;
1305 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001306
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001308 if (opx->segment != segment) {
1309 data = opx->offset;
1310 out(offset, segment, &data,
1311 OUT_REL1ADR, insn_end - offset,
1312 opx->segment, opx->wrt);
1313 } else {
1314 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 if (data > 127 || data < -128)
1316 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001317 out(offset, segment, &data,
1318 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1319 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 offset += 1;
1321 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001322
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001324 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001325 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001326 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001327 offset += 8;
1328 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001329
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001331 if (opx->segment != segment) {
1332 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001334 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001335 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001337 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001339 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 }
1341 offset += 2;
1342 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001345 if (opx->type & (BITS16 | BITS32 | BITS64))
1346 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 else
1348 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001349 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001350 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001351 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1353 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001355 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001357 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 }
1359 offset += size;
1360 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001361
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001363 if (opx->segment != segment) {
1364 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001366 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001367 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001369 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001371 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 }
1373 offset += 4;
1374 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001377 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001378 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1379 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001380 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001381 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001382 outfmt->segbase(1 + opx->segment),
1383 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001384 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 case 0172:
1388 c = *codes++;
1389 opx = &ins->oprs[c >> 3];
1390 bytes[0] = nasm_regvals[opx->basereg] << 4;
1391 opx = &ins->oprs[c & 7];
1392 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1393 errfunc(ERR_NONFATAL,
1394 "non-absolute expression not permitted as argument %d",
1395 c & 7);
1396 } else {
1397 if (opx->offset & ~15) {
1398 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1399 "four-bit argument exceeds bounds");
1400 }
1401 bytes[0] |= opx->offset & 15;
1402 }
1403 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1404 offset++;
1405 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001407 case 0173:
1408 c = *codes++;
1409 opx = &ins->oprs[c >> 4];
1410 bytes[0] = nasm_regvals[opx->basereg] << 4;
1411 bytes[0] |= c & 15;
1412 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1413 offset++;
1414 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001415
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001416 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001417 bytes[0] = nasm_regvals[opx->basereg] << 4;
1418 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1419 offset++;
1420 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001421
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001422 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001423 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001424 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1425 (int32_t)data != (int64_t)data) {
1426 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1427 "signed dword immediate exceeds bounds");
1428 }
1429 out(offset, segment, &data, OUT_ADDRESS, 4,
1430 opx->segment, opx->wrt);
1431 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001432 break;
1433
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001434 case4(0260):
1435 case 0270:
1436 codes += 2;
1437 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1438 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1439 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1440 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001441 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001442 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1443 offset += 3;
1444 } else {
1445 bytes[0] = 0xc5;
1446 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001447 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1449 offset += 2;
1450 }
1451 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001452
H. Peter Anvine014f352012-02-25 22:35:19 -08001453 case 0271:
1454 case 0272:
1455 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001456 break;
1457
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001458 case4(0274):
1459 {
1460 uint64_t uv, um;
1461 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001462
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001463 if (ins->rex & REX_W)
1464 s = 64;
1465 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1466 s = 16;
1467 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1468 s = 32;
1469 else
1470 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001471
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001472 um = (uint64_t)2 << (s-1);
1473 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001474
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001475 if (uv > 127 && uv < (uint64_t)-128 &&
1476 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001477 /* If this wasn't explicitly byte-sized, warn as though we
1478 * had fallen through to the imm16/32/64 case.
1479 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001480 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001481 "%s value exceeds bounds",
1482 (opx->type & BITS8) ? "signed byte" :
1483 s == 16 ? "word" :
1484 s == 32 ? "dword" :
1485 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001486 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001487 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001488 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001489 out(offset, segment, &data, OUT_ADDRESS, 1,
1490 opx->segment, opx->wrt);
1491 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001492 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001493 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1494 NO_SEG);
1495 }
1496 offset += 1;
1497 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001499
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001500 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001504 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001506 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 offset += 1;
1508 } else
1509 offset += 0;
1510 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001511
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001513 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001515 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 offset += 1;
1517 } else
1518 offset += 0;
1519 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 case 0312:
1522 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001523
Keith Kaniosb7a89542007-04-12 02:40:54 +00001524 case 0313:
1525 ins->rex = 0;
1526 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(0314):
1529 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001530
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001534
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001536 case 0323:
1537 break;
1538
Keith Kaniosb7a89542007-04-12 02:40:54 +00001539 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001540 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001542
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 case 0325:
1544 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001545
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001546 case 0326:
1547 break;
1548
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001550 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001551 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 offset += 1;
1553 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001554
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001557
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001560 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001561 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 offset += 1;
1563 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564
Keith Kanios48af1772007-08-17 07:37:52 +00001565 case 0334:
1566 if (ins->rex & REX_R) {
1567 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001568 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001569 offset += 1;
1570 }
1571 ins->rex &= ~(REX_L|REX_R);
1572 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001573
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001574 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001576
H. Peter Anvin962e3052008-08-28 17:47:16 -07001577 case 0336:
1578 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001579 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001580
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 if (ins->oprs[0].segment != NO_SEG)
1583 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1584 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001585 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 if (size > 0)
1587 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001588 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 offset += size;
1590 }
1591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case 0341:
1594 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 case 0360:
1597 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001598
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 case 0361:
1600 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001601 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1602 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 case 0364:
1606 case 0365:
1607 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001608
Keith Kanios48af1772007-08-17 07:37:52 +00001609 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001611 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001612 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001613 offset += 1;
1614 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001615
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 case 0370:
1617 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 case 0373:
1621 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001622 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 offset += 1;
1624 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001626 case 0374:
1627 eat = EA_XMMVSIB;
1628 break;
1629
1630 case 0375:
1631 eat = EA_YMMVSIB;
1632 break;
1633
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case4(0100):
1635 case4(0110):
1636 case4(0120):
1637 case4(0130):
1638 case4(0200):
1639 case4(0204):
1640 case4(0210):
1641 case4(0214):
1642 case4(0220):
1643 case4(0224):
1644 case4(0230):
1645 case4(0234):
1646 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 ea ea_data;
1648 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001650 uint8_t *p;
1651 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001653
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001654 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001655 /* pick rfield from operand b (opx) */
1656 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001657 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 } else {
1659 /* rfield is constant */
1660 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001662 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001664 if (process_ea(opy, &ea_data, bits, ins->addr_size,
Cyrill Gorcunovcdb8cd72011-08-28 16:33:39 +04001665 rfield, rflags) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001667
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 p = bytes;
1669 *p++ = ea_data.modrm;
1670 if (ea_data.sib_present)
1671 *p++ = ea_data.sib;
1672
1673 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001674 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001676 /*
1677 * Make sure the address gets the right offset in case
1678 * the line breaks in the .lst file (BR 1197827)
1679 */
1680 offset += s;
1681 s = 0;
1682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 switch (ea_data.bytes) {
1684 case 0:
1685 break;
1686 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 case 2:
1688 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001689 case 8:
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001690 data = opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 if (ea_data.rip) {
1693 if (opy->segment == segment) {
1694 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001695 if (overflow_signed(data, ea_data.bytes))
1696 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 out(offset, segment, &data, OUT_ADDRESS,
1698 ea_data.bytes, NO_SEG, NO_SEG);
1699 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001700 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 out(offset, segment, &data, OUT_REL4ADR,
1702 insn_end - offset, opy->segment, opy->wrt);
1703 }
1704 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001705 if (overflow_general(opy->offset, ins->addr_size >> 3) ||
1706 signed_bits(opy->offset, ins->addr_size) !=
1707 signed_bits(opy->offset, ea_data.bytes * 8))
1708 warn_overflow(ERR_PASS2, ea_data.bytes);
1709
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001710 out(offset, segment, &data, OUT_ADDRESS,
1711 ea_data.bytes, opy->segment, opy->wrt);
1712 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001714 default:
1715 /* Impossible! */
1716 errfunc(ERR_PANIC,
1717 "Invalid amount of bytes (%d) for offset?!",
1718 ea_data.bytes);
1719 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 }
1721 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001722 }
1723 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001724
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001725 default:
1726 errfunc(ERR_PANIC, "internal instruction table corrupt"
1727 ": instruction code \\%o (0x%02X) given", c, c);
1728 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001730 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001731}
1732
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001733static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001734{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001735 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001736 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001737 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001738}
1739
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001740static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001741{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001742 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001744 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001745}
1746
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001747static int op_rexflags(const operand * o, int mask)
1748{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001749 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001750 int val;
1751
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001752 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001753 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001754
H. Peter Anvina4835d42008-05-20 14:21:29 -07001755 flags = nasm_reg_flags[o->basereg];
1756 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001757
1758 return rexflags(val, flags, mask);
1759}
1760
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001761static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001762{
1763 int rex = 0;
1764
1765 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001766 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001767 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001768 rex |= REX_W;
1769 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1770 rex |= REX_H;
1771 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1772 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001773
1774 return rex & mask;
1775}
1776
H. Peter Anvin23595f52009-07-25 17:44:25 -07001777static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001778 insn *instruction,
1779 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001780{
1781 const struct itemplate *temp;
1782 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001783 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001784 bool opsizemissing = false;
1785 int i;
1786
1787 for (i = 0; i < instruction->operands; i++)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001789
1790 merr = MERR_INVALOP;
1791
1792 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 temp->opcode != I_none; temp++) {
1794 m = matches(temp, instruction, bits);
1795 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001796 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001797 m = MOK_GOOD;
1798 else
1799 m = MERR_INVALOP;
1800 } else if (m == MERR_OPSIZEMISSING &&
1801 (temp->flags & IF_SMASK) != IF_SX) {
1802 /*
1803 * Missing operand size and a candidate for fuzzy matching...
1804 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001805 for (i = 0; i < temp->operands; i++)
1806 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 opsizemissing = true;
1808 }
1809 if (m > merr)
1810 merr = m;
1811 if (merr == MOK_GOOD)
1812 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001813 }
1814
1815 /* No match, but see if we can get a fuzzy operand size match... */
1816 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001818
1819 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 /*
1821 * We ignore extrinsic operand sizes on registers, so we should
1822 * never try to fuzzy-match on them. This also resolves the case
1823 * when we have e.g. "xmmrm128" in two different positions.
1824 */
1825 if (is_class(REGISTER, instruction->oprs[i].type))
1826 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07001827
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 /* This tests if xsizeflags[i] has more than one bit set */
1829 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1830 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001831
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001832 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001833 }
1834
1835 /* Try matching again... */
1836 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 temp->opcode != I_none; temp++) {
1838 m = matches(temp, instruction, bits);
1839 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001840 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 m = MOK_GOOD;
1842 else
1843 m = MERR_INVALOP;
1844 }
1845 if (m > merr)
1846 merr = m;
1847 if (merr == MOK_GOOD)
1848 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001849 }
1850
H. Peter Anvina81655b2009-07-25 18:15:28 -07001851done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07001852 *tempp = temp;
1853 return merr;
1854}
1855
H. Peter Anvin65289e82009-07-25 17:25:11 -07001856static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001857 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001858{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001859 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001860 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001861 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001862
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001863 /*
1864 * Check the opcode
1865 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07001867 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001868
1869 /*
1870 * Count the operands
1871 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07001873 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001874
1875 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07001876 * Is it legal?
1877 */
1878 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
1879 return MERR_INVALOP;
1880
1881 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001882 * Check that no spurious colons or TOs are present
1883 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 for (i = 0; i < itemp->operands; i++)
1885 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07001886 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07001887
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001888 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001889 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001890 */
H. Peter Anvin60926242009-07-26 16:25:38 -07001891 switch (itemp->flags & IF_SMASK) {
1892 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001893 asize = BITS8;
1894 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001895 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001896 asize = BITS16;
1897 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001898 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001899 asize = BITS32;
1900 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001901 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001902 asize = BITS64;
1903 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001904 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001905 asize = BITS128;
1906 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001907 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001908 asize = BITS256;
1909 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001910 case IF_SZ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001911 switch (bits) {
1912 case 16:
1913 asize = BITS16;
1914 break;
1915 case 32:
1916 asize = BITS32;
1917 break;
1918 case 64:
1919 asize = BITS64;
1920 break;
1921 default:
1922 asize = 0;
1923 break;
1924 }
1925 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001926 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001927 asize = 0;
1928 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001929 }
1930
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001931 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001932 /* S- flags only apply to a specific operand */
1933 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
1934 memset(size, 0, sizeof size);
1935 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001936 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001937 /* S- flags apply to all operands */
1938 for (i = 0; i < MAX_OPERANDS; i++)
1939 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001940 }
H. Peter Anvin70653092007-10-19 14:42:29 -07001941
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001942 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001943 * Check that the operand flags all match up,
1944 * it's a bit tricky so lets be verbose:
1945 *
1946 * 1) Find out the size of operand. If instruction
1947 * doesn't have one specified -- we're trying to
1948 * guess it either from template (IF_S* flag) or
1949 * from code bits.
1950 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001951 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001952 * template has an operand size specified AND this size differ
1953 * from which instruction has (perhaps we got it from code bits)
1954 * we are:
1955 * a) Check that only size of instruction and operand is differ
1956 * other characteristics do match
1957 * b) Perhaps it's a register specified in instruction so
1958 * for such a case we just mark that operand as "size
1959 * missing" and this will turn on fuzzy operand size
1960 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001961 */
1962 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001963 opflags_t type = instruction->oprs[i].type;
1964 if (!(type & SIZE_MASK))
1965 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07001966
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001967 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001968 return MERR_INVALOP;
1969 } else if ((itemp->opd[i] & SIZE_MASK) &&
1970 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
1971 if (type & SIZE_MASK) {
H. Peter Anvin65289e82009-07-25 17:25:11 -07001972 return MERR_INVALOP;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001973 } else if (!is_class(REGISTER, type)) {
1974 /*
1975 * Note: we don't honor extrinsic operand sizes for registers,
1976 * so "missing operand size" for a register should be
1977 * considered a wildcard match rather than an error.
1978 */
1979 opsizemissing = true;
1980 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001981 }
1982 }
1983
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001984 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001985 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001986
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001987 /*
1988 * Check operand sizes
1989 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001990 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03001993 asize = itemp->opd[i] & SIZE_MASK;
1994 if (asize) {
1995 for (i = 0; i < oprs; i++)
1996 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 break;
1998 }
1999 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002000 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002002 }
2003
Keith Kaniosb7a89542007-04-12 02:40:54 +00002004 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 if (!(itemp->opd[i] & SIZE_MASK) &&
2006 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002007 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002008 }
2009
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002010 /*
2011 * Check template is okay at the set cpu level
2012 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002013 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002014 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002015
Keith Kaniosb7a89542007-04-12 02:40:54 +00002016 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002017 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002018 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002019 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002020 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002022 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002023 * If we have a HLE prefix, look for the NOHLE flag
2024 */
2025 if ((itemp->flags & IF_NOHLE) &&
2026 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2027 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2028 return MERR_BADHLE;
2029
2030 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002031 * Check if special handling needed for Jumps
2032 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002033 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002034 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035
H. Peter Anvin60926242009-07-26 16:25:38 -07002036 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002037}
2038
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002039static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002040 int addrbits, int rfield, opflags_t rflags)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002041{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002042 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002043
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002044 output->type = EA_SCALAR;
2045 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002046
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002047 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002048 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002049
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002050 if (is_class(REGISTER, input->type)) {
2051 /*
2052 * It's a direct register.
2053 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002054 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002055 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002056
Cyrill Gorcunovc7ce6a42012-12-01 19:38:47 +04002057 if (!is_class(REG_EA, regflag(input)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002058 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002059
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002060 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002062 output->bytes = 0; /* no offset necessary either */
2063 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2064 } else {
2065 /*
2066 * It's a memory reference.
2067 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002068 if (input->basereg == -1 &&
2069 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002070 /*
2071 * It's a pure offset.
2072 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002073 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2074 input->segment == NO_SEG) {
2075 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2076 input->type &= ~IP_REL;
2077 input->type |= MEMORY;
2078 }
2079
2080 if (input->eaflags & EAF_BYTEOFFS ||
2081 (input->eaflags & EAF_WORDOFFS &&
2082 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2083 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2084 }
2085
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002086 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002087 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002088 output->sib = GEN_SIB(0, 4, 5);
2089 output->bytes = 4;
2090 output->modrm = GEN_MODRM(0, rfield, 4);
2091 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002092 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002093 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002094 output->bytes = (addrbits != 16 ? 4 : 2);
2095 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2096 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002097 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002098 } else {
2099 /*
2100 * It's an indirection.
2101 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002103 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 int t, it, bt; /* register numbers */
2106 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002107
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 if (s == 0)
2109 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002110
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002111 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002112 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002113 ix = nasm_reg_flags[i];
2114 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002115 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002116 ix = 0;
2117 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002118
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002119 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002120 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002121 bx = nasm_reg_flags[b];
2122 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002123 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002124 bx = 0;
2125 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002126
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002127 /* if either one are a vector register... */
2128 if ((ix|bx) & (XMMREG|YMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002129 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002130 int32_t o = input->offset;
2131 int mod, scale, index, base;
2132
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002133 /*
2134 * For a vector SIB, one has to be a vector and the other,
2135 * if present, a GPR. The vector must be the index operand.
2136 */
2137 if (it == -1 || (bx & (XMMREG|YMMREG) & ~REG_EA)) {
2138 if (s == 0)
2139 s = 1;
2140 else if (s != 1)
2141 goto err;
2142
2143 t = bt, bt = it, it = t;
2144 x = bx, bx = ix, ix = x;
2145 }
2146
2147 if (bt != -1) {
2148 if (REG_GPR & ~bx)
2149 goto err;
2150 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2151 sok &= bx;
2152 else
2153 goto err;
2154 }
2155
2156 /*
2157 * While we're here, ensure the user didn't specify
2158 * WORD or QWORD
2159 */
2160 if (input->disp_size == 16 || input->disp_size == 64)
2161 goto err;
2162
2163 if (addrbits == 16 ||
2164 (addrbits == 32 && !(sok & BITS32)) ||
2165 (addrbits == 64 && !(sok & BITS64)))
2166 goto err;
2167
2168 output->type = (ix & YMMREG & ~REG_EA)
2169 ? EA_YMMVSIB : EA_XMMVSIB;
2170
2171 output->rex |= rexflags(it, ix, REX_X);
2172 output->rex |= rexflags(bt, bx, REX_B);
2173
2174 index = it & 7; /* it is known to be != -1 */
2175
2176 switch (s) {
2177 case 1:
2178 scale = 0;
2179 break;
2180 case 2:
2181 scale = 1;
2182 break;
2183 case 4:
2184 scale = 2;
2185 break;
2186 case 8:
2187 scale = 3;
2188 break;
2189 default: /* then what the smeg is it? */
2190 goto err; /* panic */
2191 }
2192
2193 if (bt == -1) {
2194 base = 5;
2195 mod = 0;
2196 } else {
2197 base = (bt & 7);
2198 if (base != REG_NUM_EBP && o == 0 &&
2199 seg == NO_SEG && !forw_ref &&
2200 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2201 mod = 0;
2202 else if (input->eaflags & EAF_BYTEOFFS ||
2203 (o >= -128 && o <= 127 &&
2204 seg == NO_SEG && !forw_ref &&
2205 !(input->eaflags & EAF_WORDOFFS)))
2206 mod = 1;
2207 else
2208 mod = 2;
2209 }
2210
2211 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002212 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2213 output->modrm = GEN_MODRM(mod, rfield, 4);
2214 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002215 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002216 /*
2217 * it must be a 32/64-bit memory reference. Firstly we have
2218 * to check that all registers involved are type E/Rxx.
2219 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002220 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002221 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002222
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002223 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002224 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2225 sok &= ix;
2226 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002227 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002228 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002229
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002230 if (bt != -1) {
2231 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002232 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002233 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002234 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002235 sok &= bx;
2236 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002237
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002238 /*
2239 * While we're here, ensure the user didn't specify
2240 * WORD or QWORD
2241 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002242 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002243 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002244
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002245 if (addrbits == 16 ||
2246 (addrbits == 32 && !(sok & BITS32)) ||
2247 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002248 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002249
Keith Kaniosb7a89542007-04-12 02:40:54 +00002250 /* now reorganize base/index */
2251 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002252 ((hb == b && ht == EAH_NOTBASE) ||
2253 (hb == i && ht == EAH_MAKEBASE))) {
2254 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002255 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002256 x = bx, bx = ix, ix = x;
2257 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002258 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002259 bt = -1, bx = 0, s++;
2260 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002261 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002262 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002263 }
2264 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2265 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002266 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002267 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2268 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002269 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002271 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002272 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002273 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002274 x = ix, ix = bx, bx = x;
2275 }
2276 if (it == REG_NUM_ESP ||
2277 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002278 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002279
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002280 output->rex |= rexflags(it, ix, REX_X);
2281 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002282
Keith Kanios48af1772007-08-17 07:37:52 +00002283 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002284 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002286
Keith Kaniosb7a89542007-04-12 02:40:54 +00002287 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002289 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002290 } else {
2291 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002292 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002293 seg == NO_SEG && !forw_ref &&
2294 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002295 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002296 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002297 (o >= -128 && o <= 127 &&
2298 seg == NO_SEG && !forw_ref &&
2299 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002300 mod = 1;
2301 else
2302 mod = 2;
2303 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002304
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002305 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002306 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2307 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002308 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002309 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002311
Keith Kaniosb7a89542007-04-12 02:40:54 +00002312 if (it == -1)
2313 index = 4, s = 1;
2314 else
2315 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 switch (s) {
2318 case 1:
2319 scale = 0;
2320 break;
2321 case 2:
2322 scale = 1;
2323 break;
2324 case 4:
2325 scale = 2;
2326 break;
2327 case 8:
2328 scale = 3;
2329 break;
2330 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002331 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002333
Keith Kaniosb7a89542007-04-12 02:40:54 +00002334 if (bt == -1) {
2335 base = 5;
2336 mod = 0;
2337 } else {
2338 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002339 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002340 seg == NO_SEG && !forw_ref &&
2341 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002342 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002343 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002344 (o >= -128 && o <= 127 &&
2345 seg == NO_SEG && !forw_ref &&
2346 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002347 mod = 1;
2348 else
2349 mod = 2;
2350 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002351
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002352 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002353 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2354 output->modrm = GEN_MODRM(mod, rfield, 4);
2355 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 }
2357 } else { /* it's 16-bit */
2358 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002359 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002360
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 /* check for 64-bit long mode */
2362 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002363 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002366 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2367 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002368 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002369
Keith Kaniosb7a89542007-04-12 02:40:54 +00002370 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002371 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002372 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002375 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 if (b == -1 && i != -1) {
2377 int tmp = b;
2378 b = i;
2379 i = tmp;
2380 } /* swap */
2381 if ((b == R_SI || b == R_DI) && i != -1) {
2382 int tmp = b;
2383 b = i;
2384 i = tmp;
2385 }
2386 /* have BX/BP as base, SI/DI index */
2387 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002388 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 if (i != -1 && b != -1 &&
2390 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002391 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002392 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002393 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002394
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 rm = -1;
2396 if (i != -1)
2397 switch (i * 256 + b) {
2398 case R_SI * 256 + R_BX:
2399 rm = 0;
2400 break;
2401 case R_DI * 256 + R_BX:
2402 rm = 1;
2403 break;
2404 case R_SI * 256 + R_BP:
2405 rm = 2;
2406 break;
2407 case R_DI * 256 + R_BP:
2408 rm = 3;
2409 break;
2410 } else
2411 switch (b) {
2412 case R_SI:
2413 rm = 4;
2414 break;
2415 case R_DI:
2416 rm = 5;
2417 break;
2418 case R_BP:
2419 rm = 6;
2420 break;
2421 case R_BX:
2422 rm = 7;
2423 break;
2424 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002425 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002426 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002427
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002428 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2429 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002431 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002432 (o >= -128 && o <= 127 && seg == NO_SEG &&
2433 !forw_ref && !(input->eaflags & EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 mod = 1;
2435 else
2436 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002437
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002438 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002439 output->bytes = mod; /* bytes of offset needed */
2440 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 }
2442 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002443 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002444
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002445 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002446 return output->type;
2447
2448err:
2449 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002450}
2451
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002452static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002453{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002454 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002455 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002456
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002457 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002458
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002459 switch (ins->prefixes[PPS_ASIZE]) {
2460 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002461 valid &= 16;
2462 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002463 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002464 valid &= 32;
2465 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002466 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002467 valid &= 64;
2468 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002469 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 valid &= (addrbits == 32) ? 16 : 32;
2471 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002472 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002474 }
2475
2476 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002477 if (is_class(MEMORY, ins->oprs[j].type)) {
2478 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002479
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002480 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002481 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002482 i = 0;
2483 else
2484 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002485
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002486 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002487 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 b = 0;
2489 else
2490 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002491
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002492 if (ins->oprs[j].scale == 0)
2493 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002494
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002495 if (!i && !b) {
2496 int ds = ins->oprs[j].disp_size;
2497 if ((addrbits != 64 && ds > 8) ||
2498 (addrbits == 64 && ds == 16))
2499 valid &= ds;
2500 } else {
2501 if (!(REG16 & ~b))
2502 valid &= 16;
2503 if (!(REG32 & ~b))
2504 valid &= 32;
2505 if (!(REG64 & ~b))
2506 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002507
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002508 if (!(REG16 & ~i))
2509 valid &= 16;
2510 if (!(REG32 & ~i))
2511 valid &= 32;
2512 if (!(REG64 & ~i))
2513 valid &= 64;
2514 }
2515 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002516 }
2517
2518 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002520 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002521 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002522 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002524 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 /* Impossible... */
2526 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2527 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002528 }
2529
2530 defdisp = ins->addr_size == 16 ? 16 : 32;
2531
2532 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2534 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2535 /*
2536 * mem_offs sizes must match the address size; if not,
2537 * strip the MEM_OFFS bit and match only EA instructions
2538 */
2539 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2540 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002541 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002542}