blob: 235be13a54313b40788e59e4c9e0f2ca5db71bd0 [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);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400427 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700428 static char buf[4096];
429 size_t t = instruction->times;
430 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400431 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 len = ftell(fp);
434 if (instruction->eops->next) {
435 base = instruction->eops->next->offset;
436 len -= base;
437 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700438 len > (size_t)instruction->eops->next->next->offset)
439 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 }
441 /*
442 * Dummy call to list->output to give the offset to the
443 * listing module.
444 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800445 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 list->uplevel(LIST_INCBIN);
447 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700448 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 fseek(fp, base, SEEK_SET);
451 l = len;
452 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700453 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400454 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 if (!m) {
456 /*
457 * This shouldn't happen unless the file
458 * actually changes while we are reading
459 * it.
460 */
461 error(ERR_NONFATAL,
462 "`incbin': unexpected EOF while"
463 " reading file `%s'", fname);
464 t = 0; /* Try to exit cleanly */
465 break;
466 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800467 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 NO_SEG, NO_SEG);
469 l -= m;
470 }
471 }
472 list->downlevel(LIST_INCBIN);
473 if (instruction->times > 1) {
474 /*
475 * Dummy call to list->output to give the offset to the
476 * listing module.
477 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800478 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 list->uplevel(LIST_TIMES);
480 list->downlevel(LIST_TIMES);
481 }
482 fclose(fp);
483 return instruction->times * len;
484 }
485 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486 }
487
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700488 /* Check to see if we need an address-size prefix */
489 add_asp(instruction, bits);
490
H. Peter Anvin23595f52009-07-25 17:44:25 -0700491 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700492
H. Peter Anvin23595f52009-07-25 17:44:25 -0700493 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400494 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800495 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400496 itimes = instruction->times;
497 if (insn_size < 0) /* shouldn't be, on pass two */
498 error(ERR_PANIC, "errors made it through from pass one");
499 else
500 while (itimes--) {
501 for (j = 0; j < MAXPREFIX; j++) {
502 uint8_t c = 0;
503 switch (instruction->prefixes[j]) {
504 case P_WAIT:
505 c = 0x9B;
506 break;
507 case P_LOCK:
508 c = 0xF0;
509 break;
510 case P_REPNE:
511 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800512 case P_XACQUIRE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400513 c = 0xF2;
514 break;
515 case P_REPE:
516 case P_REPZ:
517 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800518 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400519 c = 0xF3;
520 break;
521 case R_CS:
522 if (bits == 64) {
523 error(ERR_WARNING | ERR_PASS2,
524 "cs segment base generated, but will be ignored in 64-bit mode");
525 }
526 c = 0x2E;
527 break;
528 case R_DS:
529 if (bits == 64) {
530 error(ERR_WARNING | ERR_PASS2,
531 "ds segment base generated, but will be ignored in 64-bit mode");
532 }
533 c = 0x3E;
534 break;
535 case R_ES:
536 if (bits == 64) {
537 error(ERR_WARNING | ERR_PASS2,
538 "es segment base generated, but will be ignored in 64-bit mode");
539 }
540 c = 0x26;
541 break;
542 case R_FS:
543 c = 0x64;
544 break;
545 case R_GS:
546 c = 0x65;
547 break;
548 case R_SS:
549 if (bits == 64) {
550 error(ERR_WARNING | ERR_PASS2,
551 "ss segment base generated, but will be ignored in 64-bit mode");
552 }
553 c = 0x36;
554 break;
555 case R_SEGR6:
556 case R_SEGR7:
557 error(ERR_NONFATAL,
558 "segr6 and segr7 cannot be used as prefixes");
559 break;
560 case P_A16:
561 if (bits == 64) {
562 error(ERR_NONFATAL,
563 "16-bit addressing is not supported "
564 "in 64-bit mode");
565 } else if (bits != 16)
566 c = 0x67;
567 break;
568 case P_A32:
569 if (bits != 32)
570 c = 0x67;
571 break;
572 case P_A64:
573 if (bits != 64) {
574 error(ERR_NONFATAL,
575 "64-bit addressing is only supported "
576 "in 64-bit mode");
577 }
578 break;
579 case P_ASP:
580 c = 0x67;
581 break;
582 case P_O16:
583 if (bits != 16)
584 c = 0x66;
585 break;
586 case P_O32:
587 if (bits == 16)
588 c = 0x66;
589 break;
590 case P_O64:
591 /* REX.W */
592 break;
593 case P_OSP:
594 c = 0x66;
595 break;
596 case P_none:
597 break;
598 default:
599 error(ERR_PANIC, "invalid instruction prefix");
600 }
601 if (c != 0) {
602 out(offset, segment, &c, OUT_RAWDATA, 1,
603 NO_SEG, NO_SEG);
604 offset++;
605 }
606 }
607 insn_end = offset + insn_size;
608 gencode(segment, offset, bits, instruction,
609 temp, insn_end);
610 offset += insn_size;
611 if (itimes > 0 && itimes == instruction->times - 1) {
612 /*
613 * Dummy call to list->output to give the offset to the
614 * listing module.
615 */
616 list->output(offset, NULL, OUT_RAWDATA, 0);
617 list->uplevel(LIST_TIMES);
618 }
619 }
620 if (instruction->times > 1)
621 list->downlevel(LIST_TIMES);
622 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700623 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400624 /* No match */
625 switch (m) {
626 case MERR_OPSIZEMISSING:
627 error(ERR_NONFATAL, "operation size not specified");
628 break;
629 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400631 break;
632 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400634 break;
635 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800636 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400637 bits);
638 break;
639 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 error(ERR_NONFATAL,
641 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400642 break;
643 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000644 }
645 return 0;
646}
647
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800648int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400649 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000650{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000651 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700652 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000653
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000655 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000656
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400657 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000659
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700660 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
661 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400662 instruction->opcode == I_DT || instruction->opcode == I_DO ||
663 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300665 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000666
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300668 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000669
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400670 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000671 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400674 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400676 warn_overflow_const(e->offset, wsize);
677 } else if (e->type == EOT_DB_STRING ||
678 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000680
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 align = (-osize) % wsize;
682 if (align < 0)
683 align += wsize;
684 isize += osize + align;
685 }
686 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000687 }
688
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400690 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300692 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700693 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400695 fp = fopen(fname, "rb");
696 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
698 fname);
699 else if (fseek(fp, 0L, SEEK_END) < 0)
700 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
701 fname);
702 else {
703 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 if (instruction->eops->next) {
705 len -= instruction->eops->next->offset;
706 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700707 len > (size_t)instruction->eops->next->next->offset) {
708 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 }
710 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300711 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300713 if (fp)
714 fclose(fp);
715 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000716 }
717
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700718 /* Check to see if we need an address-size prefix */
719 add_asp(instruction, bits);
720
H. Peter Anvin23595f52009-07-25 17:44:25 -0700721 m = find_match(&temp, instruction, segment, offset, bits);
722 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400723 /* we've matched an instruction. */
724 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400725 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100726
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800727 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400728 if (isize < 0)
729 return -1;
730 for (j = 0; j < MAXPREFIX; j++) {
731 switch (instruction->prefixes[j]) {
732 case P_A16:
733 if (bits != 16)
734 isize++;
735 break;
736 case P_A32:
737 if (bits != 32)
738 isize++;
739 break;
740 case P_O16:
741 if (bits != 16)
742 isize++;
743 break;
744 case P_O32:
745 if (bits == 16)
746 isize++;
747 break;
748 case P_A64:
749 case P_O64:
750 case P_none:
751 break;
752 default:
753 isize++;
754 break;
755 }
756 }
757 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700758 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400759 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000761}
762
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800763static void bad_hle_warn(const insn * ins, uint8_t hleok)
764{
765 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800766 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800767 static const enum whatwarn warn[2][4] =
768 {
769 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
770 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
771 };
772 unsigned int n;
773
774 n = (unsigned int)rep_pfx - P_XACQUIRE;
775 if (n > 1)
776 return; /* Not XACQUIRE/XRELEASE */
777
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800778 ww = warn[n][hleok];
779 if (!is_class(MEMORY, ins->oprs[0].type))
780 ww = w_inval; /* HLE requires operand 0 to be memory */
781
782 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800783 case w_none:
784 break;
785
786 case w_lock:
787 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800788 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800789 "%s with this instruction requires lock",
790 prefix_name(rep_pfx));
791 }
792 break;
793
794 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800795 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800796 "%s invalid with this instruction",
797 prefix_name(rep_pfx));
798 break;
799 }
800}
801
H. Peter Anvin507ae032008-10-09 15:37:10 -0700802/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400803#define case3(x) case (x): case (x)+1: case (x)+2
804#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700805
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800806static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800807 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800809 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800810 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000811 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000812 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700813 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700814 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700815 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700816 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800817 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800818 bool lockcheck = true;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000819
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700820 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700821 eat = EA_SCALAR; /* Expect a scalar EA */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700822
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700823 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400824 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 (void)segment; /* Don't warn that this parameter is unused */
827 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000828
H. Peter Anvin839eca22007-10-29 23:12:47 -0700829 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400830 c = *codes++;
831 op1 = (c & 3) + ((opex & 1) << 2);
832 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
833 opx = &ins->oprs[op1];
834 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700835
H. Peter Anvin839eca22007-10-29 23:12:47 -0700836 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400837 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 codes += c, length += c;
839 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700840
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400841 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400842 opex = c;
843 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700844
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400845 case4(010):
846 ins->rex |=
847 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 codes++, length++;
849 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400851 case4(020):
852 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 length++;
854 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700855
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400856 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 length += 2;
858 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400860 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700861 if (opx->type & (BITS16 | BITS32 | BITS64))
862 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 else
864 length += (bits == 16) ? 2 : 4;
865 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700866
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400867 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 length += 4;
869 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400871 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700872 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700874
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400875 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 length++;
877 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000880 length += 8; /* MOV reg64/imm */
881 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400883 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 length += 2;
885 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700886
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400887 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700888 if (opx->type & (BITS16 | BITS32 | BITS64))
889 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 else
891 length += (bits == 16) ? 2 : 4;
892 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700893
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400894 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 length += 4;
896 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700897
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400898 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700899 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700901
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400902 case 0172:
903 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700905 length++;
906 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700907
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700908 case4(0174):
909 length++;
910 break;
911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400912 case4(0254):
913 length += 4;
914 break;
915
916 case4(0260):
917 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700918 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400919 ins->vex_cm = *codes++;
920 ins->vex_wlp = *codes++;
921 break;
922
923 case 0270:
924 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -0700925 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400926 ins->vex_cm = *codes++;
927 ins->vex_wlp = *codes++;
928 break;
929
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400930 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -0800931 hleok = c & 3;
932 break;
933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(0274):
935 length++;
936 break;
937
938 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 if (bits == 64)
943 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700944 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700946
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700948 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -0700952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
Keith Kaniosb7a89542007-04-12 02:40:54 +0000954 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400955 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
956 has_prefix(ins, PPS_ASIZE, P_A32))
957 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 case4(0314):
961 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000964 {
965 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
966 if (pfx == P_O16)
967 break;
968 if (pfx != P_none)
969 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
970 else
971 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000973 }
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000976 {
977 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
978 if (pfx == P_O32)
979 break;
980 if (pfx != P_none)
981 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
982 else
983 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000985 }
H. Peter Anvin507ae032008-10-09 15:37:10 -0700986
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 case 0322:
988 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
Keith Kaniosb7a89542007-04-12 02:40:54 +0000990 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000991 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
Keith Kaniosb7a89542007-04-12 02:40:54 +0000994 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +0000996 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 case 0325:
999 ins->rex |= REX_NH;
1000 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001001
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001002 case 0326:
1003 break;
1004
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 case 0330:
1006 codes++, length++;
1007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001012 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 case 0333:
1014 length++;
1015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 case 0334:
1018 ins->rex |= REX_L;
1019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001021 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001023
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001024 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001025 if (!ins->prefixes[PPS_REP])
1026 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001028
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001029 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001030 if (!ins->prefixes[PPS_REP])
1031 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001032 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001033
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 if (ins->oprs[0].segment != NO_SEG)
1036 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1037 " quantity of BSS space");
1038 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001039 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001041
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001042 case 0341:
1043 if (!ins->prefixes[PPS_WAIT])
1044 ins->prefixes[PPS_WAIT] = P_WAIT;
1045 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001046
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001047 case 0360:
1048 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001049
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001050 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 length++;
1052 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001053
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001054 case 0364:
1055 case 0365:
1056 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001057
Keith Kanios48af1772007-08-17 07:37:52 +00001058 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001059 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001060 length++;
1061 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001062
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001063 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 0373:
1067 length++;
1068 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001069
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001070 case 0374:
1071 eat = EA_XMMVSIB;
1072 break;
1073
1074 case 0375:
1075 eat = EA_YMMVSIB;
1076 break;
1077
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 case4(0100):
1079 case4(0110):
1080 case4(0120):
1081 case4(0130):
1082 case4(0200):
1083 case4(0204):
1084 case4(0210):
1085 case4(0214):
1086 case4(0220):
1087 case4(0224):
1088 case4(0230):
1089 case4(0234):
1090 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001092 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001093 opflags_t rflags;
1094 struct operand *opy = &ins->oprs[op2];
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001095
Keith Kaniosb7a89542007-04-12 02:40:54 +00001096 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001097
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001098 if (c <= 0177) {
1099 /* pick rfield from operand b (opx) */
1100 rflags = regflag(opx);
1101 rfield = nasm_regvals[opx->basereg];
1102 } else {
1103 rflags = 0;
1104 rfield = c & 7;
1105 }
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001106 if (process_ea(opy, &ea_data, bits,ins->addr_size,
1107 rfield, rflags) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 errfunc(ERR_NONFATAL, "invalid effective address");
1109 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001110 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001113 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 }
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001117 default:
1118 errfunc(ERR_PANIC, "internal instruction table corrupt"
1119 ": instruction code \\%o (0x%02X) given", c, c);
1120 break;
1121 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001122 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001123
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001124 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001125
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001126 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 if (ins->rex & REX_H) {
1128 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1129 return -1;
1130 }
1131 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001132 }
1133
H. Peter Anvind85d2502008-05-04 17:53:31 -07001134 if (ins->rex & REX_V) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001136
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 if (ins->rex & REX_H) {
1138 errfunc(ERR_NONFATAL, "cannot use high register in vex instruction");
1139 return -1;
1140 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001141 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001142 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001143 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 ins->rex &= ~REX_W;
1145 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001146 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 ins->rex |= REX_W;
1148 bad32 &= ~REX_W;
1149 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001150 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 /* Follow REX_W */
1152 break;
1153 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001154
H. Peter Anvinfc561202011-07-07 16:58:22 -07001155 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1157 return -1;
1158 }
H. Peter Anvin3cb0e8c2010-11-16 09:36:58 -08001159 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001160 length += 3;
1161 else
1162 length += 2;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001163 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 if (ins->rex & REX_H) {
1165 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1166 return -1;
1167 } else if (bits == 64) {
1168 length++;
1169 } else if ((ins->rex & REX_L) &&
1170 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1171 cpu >= IF_X86_64) {
1172 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001173 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001174 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 length++;
1176 } else {
1177 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1178 return -1;
1179 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001180 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001181
1182 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1183 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001184 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001185 "instruction is not lockable");
1186 }
1187
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001188 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001189
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001190 return length;
1191}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001192
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001193static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1194{
1195 if (bits == 64) {
1196 if ((ins->rex & REX_REAL) && !(ins->rex & REX_V)) {
1197 ins->rex = (ins->rex & REX_REAL) | REX_P;
1198 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1199 ins->rex = 0;
1200 return 1;
1201 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001202 }
1203
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001204 return 0;
1205}
1206
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001207static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001208 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001210{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001211 uint8_t c;
1212 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001213 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001214 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001215 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001216 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001217 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001218 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001219 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001220
H. Peter Anvin839eca22007-10-29 23:12:47 -07001221 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001222 c = *codes++;
1223 op1 = (c & 3) + ((opex & 1) << 2);
1224 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1225 opx = &ins->oprs[op1];
1226 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001227
H. Peter Anvin839eca22007-10-29 23:12:47 -07001228 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 case 01:
1230 case 02:
1231 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001233 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001234 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 codes += c;
1236 offset += c;
1237 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001238
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001239 case 05:
1240 case 06:
1241 case 07:
1242 opex = c;
1243 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001244
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001245 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001246 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001247 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001248 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 offset += 1;
1250 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001253 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001254 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 }
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001257 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 offset += 1;
1259 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001260
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001261 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001262 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001263 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001264 "unsigned byte value exceeds bounds");
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001265 out_imm8(offset, segment, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 offset += 1;
1267 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001268
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001270 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001271 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001272 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001273 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 offset += 2;
1275 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001277 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001278 if (opx->type & (BITS16 | BITS32))
1279 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 else
1281 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001282 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001283 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001284 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001285 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 offset += size;
1287 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001290 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001291 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001292 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001293 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 offset += 4;
1295 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001296
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001298 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001299 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001300 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001301 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001302 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 offset += size;
1304 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001305
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001307 if (opx->segment != segment) {
1308 data = opx->offset;
1309 out(offset, segment, &data,
1310 OUT_REL1ADR, insn_end - offset,
1311 opx->segment, opx->wrt);
1312 } else {
1313 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 if (data > 127 || data < -128)
1315 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001316 out(offset, segment, &data,
1317 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1318 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 offset += 1;
1320 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001321
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001323 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001324 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001325 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001326 offset += 8;
1327 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001328
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001330 if (opx->segment != segment) {
1331 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001333 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001334 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001336 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001338 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 }
1340 offset += 2;
1341 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001342
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001344 if (opx->type & (BITS16 | BITS32 | BITS64))
1345 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 else
1347 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001348 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001349 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001350 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1352 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001354 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001356 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 }
1358 offset += size;
1359 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001362 if (opx->segment != segment) {
1363 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001365 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001366 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001368 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001370 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 }
1372 offset += 4;
1373 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001376 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001377 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1378 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001380 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001381 outfmt->segbase(1 + opx->segment),
1382 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001383 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001385
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 case 0172:
1387 c = *codes++;
1388 opx = &ins->oprs[c >> 3];
1389 bytes[0] = nasm_regvals[opx->basereg] << 4;
1390 opx = &ins->oprs[c & 7];
1391 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1392 errfunc(ERR_NONFATAL,
1393 "non-absolute expression not permitted as argument %d",
1394 c & 7);
1395 } else {
1396 if (opx->offset & ~15) {
1397 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1398 "four-bit argument exceeds bounds");
1399 }
1400 bytes[0] |= opx->offset & 15;
1401 }
1402 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1403 offset++;
1404 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001405
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001406 case 0173:
1407 c = *codes++;
1408 opx = &ins->oprs[c >> 4];
1409 bytes[0] = nasm_regvals[opx->basereg] << 4;
1410 bytes[0] |= c & 15;
1411 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1412 offset++;
1413 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001414
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001415 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001416 bytes[0] = nasm_regvals[opx->basereg] << 4;
1417 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1418 offset++;
1419 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001420
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001421 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001422 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001423 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1424 (int32_t)data != (int64_t)data) {
1425 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1426 "signed dword immediate exceeds bounds");
1427 }
1428 out(offset, segment, &data, OUT_ADDRESS, 4,
1429 opx->segment, opx->wrt);
1430 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001431 break;
1432
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001433 case4(0260):
1434 case 0270:
1435 codes += 2;
1436 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1437 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1438 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1439 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001440 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001441 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1442 offset += 3;
1443 } else {
1444 bytes[0] = 0xc5;
1445 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001446 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001447 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1448 offset += 2;
1449 }
1450 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001451
H. Peter Anvine014f352012-02-25 22:35:19 -08001452 case 0271:
1453 case 0272:
1454 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001455 break;
1456
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001457 case4(0274):
1458 {
1459 uint64_t uv, um;
1460 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001461
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 if (ins->rex & REX_W)
1463 s = 64;
1464 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1465 s = 16;
1466 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1467 s = 32;
1468 else
1469 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 um = (uint64_t)2 << (s-1);
1472 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001473
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001474 if (uv > 127 && uv < (uint64_t)-128 &&
1475 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001476 /* If this wasn't explicitly byte-sized, warn as though we
1477 * had fallen through to the imm16/32/64 case.
1478 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001479 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001480 "%s value exceeds bounds",
1481 (opx->type & BITS8) ? "signed byte" :
1482 s == 16 ? "word" :
1483 s == 32 ? "dword" :
1484 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001486 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001487 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001488 out(offset, segment, &data, OUT_ADDRESS, 1,
1489 opx->segment, opx->wrt);
1490 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001491 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001492 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1493 NO_SEG);
1494 }
1495 offset += 1;
1496 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001497 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001503 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001505 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 offset += 1;
1507 } else
1508 offset += 0;
1509 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001510
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001512 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001514 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 offset += 1;
1516 } else
1517 offset += 0;
1518 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 case 0312:
1521 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001522
Keith Kaniosb7a89542007-04-12 02:40:54 +00001523 case 0313:
1524 ins->rex = 0;
1525 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001526
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001527 case4(0314):
1528 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001529
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001533
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001535 case 0323:
1536 break;
1537
Keith Kaniosb7a89542007-04-12 02:40:54 +00001538 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001539 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001541
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 case 0325:
1543 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001544
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001545 case 0326:
1546 break;
1547
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001549 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001550 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 offset += 1;
1552 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001553
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001556
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001559 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001560 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 offset += 1;
1562 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001563
Keith Kanios48af1772007-08-17 07:37:52 +00001564 case 0334:
1565 if (ins->rex & REX_R) {
1566 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001567 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001568 offset += 1;
1569 }
1570 ins->rex &= ~(REX_L|REX_R);
1571 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001572
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001573 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001575
H. Peter Anvin962e3052008-08-28 17:47:16 -07001576 case 0336:
1577 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001579
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 if (ins->oprs[0].segment != NO_SEG)
1582 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1583 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001584 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 if (size > 0)
1586 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001587 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 offset += size;
1589 }
1590 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001591
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 case 0341:
1593 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 case 0360:
1596 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case 0361:
1599 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001600 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1601 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001603
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 case 0364:
1605 case 0365:
1606 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001607
Keith Kanios48af1772007-08-17 07:37:52 +00001608 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001610 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001611 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001612 offset += 1;
1613 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001614
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 case 0370:
1616 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 case 0373:
1620 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001621 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 offset += 1;
1623 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001624
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001625 case 0374:
1626 eat = EA_XMMVSIB;
1627 break;
1628
1629 case 0375:
1630 eat = EA_YMMVSIB;
1631 break;
1632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case4(0100):
1634 case4(0110):
1635 case4(0120):
1636 case4(0130):
1637 case4(0200):
1638 case4(0204):
1639 case4(0210):
1640 case4(0214):
1641 case4(0220):
1642 case4(0224):
1643 case4(0230):
1644 case4(0234):
1645 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 ea ea_data;
1647 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001649 uint8_t *p;
1650 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001652
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001653 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 /* pick rfield from operand b (opx) */
1655 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001656 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 } else {
1658 /* rfield is constant */
1659 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001663 if (process_ea(opy, &ea_data, bits, ins->addr_size,
Cyrill Gorcunovcdb8cd72011-08-28 16:33:39 +04001664 rfield, rflags) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001666
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 p = bytes;
1668 *p++ = ea_data.modrm;
1669 if (ea_data.sib_present)
1670 *p++ = ea_data.sib;
1671
1672 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001673 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001675 /*
1676 * Make sure the address gets the right offset in case
1677 * the line breaks in the .lst file (BR 1197827)
1678 */
1679 offset += s;
1680 s = 0;
1681
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 switch (ea_data.bytes) {
1683 case 0:
1684 break;
1685 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 case 2:
1687 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001688 case 8:
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001689 data = opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 if (ea_data.rip) {
1692 if (opy->segment == segment) {
1693 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001694 if (overflow_signed(data, ea_data.bytes))
1695 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 out(offset, segment, &data, OUT_ADDRESS,
1697 ea_data.bytes, NO_SEG, NO_SEG);
1698 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001699 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001700 out(offset, segment, &data, OUT_REL4ADR,
1701 insn_end - offset, opy->segment, opy->wrt);
1702 }
1703 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001704 if (overflow_general(opy->offset, ins->addr_size >> 3) ||
1705 signed_bits(opy->offset, ins->addr_size) !=
1706 signed_bits(opy->offset, ea_data.bytes * 8))
1707 warn_overflow(ERR_PASS2, ea_data.bytes);
1708
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 out(offset, segment, &data, OUT_ADDRESS,
1710 ea_data.bytes, opy->segment, opy->wrt);
1711 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001713 default:
1714 /* Impossible! */
1715 errfunc(ERR_PANIC,
1716 "Invalid amount of bytes (%d) for offset?!",
1717 ea_data.bytes);
1718 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 }
1720 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 }
1722 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 default:
1725 errfunc(ERR_PANIC, "internal instruction table corrupt"
1726 ": instruction code \\%o (0x%02X) given", c, c);
1727 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001729 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001730}
1731
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001732static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001733{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001734 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001735 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001736 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001737}
1738
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001739static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001740{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001741 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001743 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001744}
1745
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001746static int op_rexflags(const operand * o, int mask)
1747{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001748 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001749 int val;
1750
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001751 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001752 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001753
H. Peter Anvina4835d42008-05-20 14:21:29 -07001754 flags = nasm_reg_flags[o->basereg];
1755 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001756
1757 return rexflags(val, flags, mask);
1758}
1759
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001760static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001761{
1762 int rex = 0;
1763
1764 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001765 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001766 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001767 rex |= REX_W;
1768 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1769 rex |= REX_H;
1770 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1771 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001772
1773 return rex & mask;
1774}
1775
H. Peter Anvin23595f52009-07-25 17:44:25 -07001776static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 insn *instruction,
1778 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001779{
1780 const struct itemplate *temp;
1781 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001782 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001783 bool opsizemissing = false;
1784 int i;
1785
1786 for (i = 0; i < instruction->operands; i++)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001787 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001788
1789 merr = MERR_INVALOP;
1790
1791 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 temp->opcode != I_none; temp++) {
1793 m = matches(temp, instruction, bits);
1794 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001795 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 m = MOK_GOOD;
1797 else
1798 m = MERR_INVALOP;
1799 } else if (m == MERR_OPSIZEMISSING &&
1800 (temp->flags & IF_SMASK) != IF_SX) {
1801 /*
1802 * Missing operand size and a candidate for fuzzy matching...
1803 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001804 for (i = 0; i < temp->operands; i++)
1805 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 opsizemissing = true;
1807 }
1808 if (m > merr)
1809 merr = m;
1810 if (merr == MOK_GOOD)
1811 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001812 }
1813
1814 /* No match, but see if we can get a fuzzy operand size match... */
1815 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001816 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001817
1818 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001819 /*
1820 * We ignore extrinsic operand sizes on registers, so we should
1821 * never try to fuzzy-match on them. This also resolves the case
1822 * when we have e.g. "xmmrm128" in two different positions.
1823 */
1824 if (is_class(REGISTER, instruction->oprs[i].type))
1825 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07001826
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001827 /* This tests if xsizeflags[i] has more than one bit set */
1828 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1829 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001830
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001831 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07001832 }
1833
1834 /* Try matching again... */
1835 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 temp->opcode != I_none; temp++) {
1837 m = matches(temp, instruction, bits);
1838 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001839 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001840 m = MOK_GOOD;
1841 else
1842 m = MERR_INVALOP;
1843 }
1844 if (m > merr)
1845 merr = m;
1846 if (merr == MOK_GOOD)
1847 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001848 }
1849
H. Peter Anvina81655b2009-07-25 18:15:28 -07001850done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07001851 *tempp = temp;
1852 return merr;
1853}
1854
H. Peter Anvin65289e82009-07-25 17:25:11 -07001855static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001856 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001857{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001858 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001859 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001860 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001861
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001862 /*
1863 * Check the opcode
1864 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07001866 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001867
1868 /*
1869 * Count the operands
1870 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07001872 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001873
1874 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07001875 * Is it legal?
1876 */
1877 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
1878 return MERR_INVALOP;
1879
1880 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001881 * Check that no spurious colons or TOs are present
1882 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 for (i = 0; i < itemp->operands; i++)
1884 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07001885 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07001886
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001888 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001889 */
H. Peter Anvin60926242009-07-26 16:25:38 -07001890 switch (itemp->flags & IF_SMASK) {
1891 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001892 asize = BITS8;
1893 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001894 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001895 asize = BITS16;
1896 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001897 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001898 asize = BITS32;
1899 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001900 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001901 asize = BITS64;
1902 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001903 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001904 asize = BITS128;
1905 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001906 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001907 asize = BITS256;
1908 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001909 case IF_SZ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001910 switch (bits) {
1911 case 16:
1912 asize = BITS16;
1913 break;
1914 case 32:
1915 asize = BITS32;
1916 break;
1917 case 64:
1918 asize = BITS64;
1919 break;
1920 default:
1921 asize = 0;
1922 break;
1923 }
1924 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001925 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001926 asize = 0;
1927 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07001928 }
1929
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001930 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001931 /* S- flags only apply to a specific operand */
1932 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
1933 memset(size, 0, sizeof size);
1934 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001935 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001936 /* S- flags apply to all operands */
1937 for (i = 0; i < MAX_OPERANDS; i++)
1938 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001939 }
H. Peter Anvin70653092007-10-19 14:42:29 -07001940
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001941 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001942 * Check that the operand flags all match up,
1943 * it's a bit tricky so lets be verbose:
1944 *
1945 * 1) Find out the size of operand. If instruction
1946 * doesn't have one specified -- we're trying to
1947 * guess it either from template (IF_S* flag) or
1948 * from code bits.
1949 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001950 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001951 * template has an operand size specified AND this size differ
1952 * from which instruction has (perhaps we got it from code bits)
1953 * we are:
1954 * a) Check that only size of instruction and operand is differ
1955 * other characteristics do match
1956 * b) Perhaps it's a register specified in instruction so
1957 * for such a case we just mark that operand as "size
1958 * missing" and this will turn on fuzzy operand size
1959 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001960 */
1961 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001962 opflags_t type = instruction->oprs[i].type;
1963 if (!(type & SIZE_MASK))
1964 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07001965
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08001966 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001967 return MERR_INVALOP;
1968 } else if ((itemp->opd[i] & SIZE_MASK) &&
1969 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
1970 if (type & SIZE_MASK) {
H. Peter Anvin65289e82009-07-25 17:25:11 -07001971 return MERR_INVALOP;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001972 } else if (!is_class(REGISTER, type)) {
1973 /*
1974 * Note: we don't honor extrinsic operand sizes for registers,
1975 * so "missing operand size" for a register should be
1976 * considered a wildcard match rather than an error.
1977 */
1978 opsizemissing = true;
1979 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001980 }
1981 }
1982
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001983 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03001984 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07001985
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001986 /*
1987 * Check operand sizes
1988 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001989 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03001992 asize = itemp->opd[i] & SIZE_MASK;
1993 if (asize) {
1994 for (i = 0; i < oprs; i++)
1995 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 break;
1997 }
1998 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001999 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002001 }
2002
Keith Kaniosb7a89542007-04-12 02:40:54 +00002003 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 if (!(itemp->opd[i] & SIZE_MASK) &&
2005 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002006 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002007 }
2008
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002009 /*
2010 * Check template is okay at the set cpu level
2011 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002012 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002013 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002014
Keith Kaniosb7a89542007-04-12 02:40:54 +00002015 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002016 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002017 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002018 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002019 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002021 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002022 * If we have a HLE prefix, look for the NOHLE flag
2023 */
2024 if ((itemp->flags & IF_NOHLE) &&
2025 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2026 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2027 return MERR_BADHLE;
2028
2029 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002030 * Check if special handling needed for Jumps
2031 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002032 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002033 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034
H. Peter Anvin60926242009-07-26 16:25:38 -07002035 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002036}
2037
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002038static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002039 int addrbits, int rfield, opflags_t rflags)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002040{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002041 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002042
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002043 output->type = EA_SCALAR;
2044 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002045
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002046 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002047 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002048
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002049 if (is_class(REGISTER, input->type)) {
2050 /*
2051 * It's a direct register.
2052 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002053 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002054 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002055
Cyrill Gorcunovc7ce6a42012-12-01 19:38:47 +04002056 if (!is_class(REG_EA, regflag(input)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002057 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002058
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002059 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002060 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002061 output->bytes = 0; /* no offset necessary either */
2062 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2063 } else {
2064 /*
2065 * It's a memory reference.
2066 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 if (input->basereg == -1 &&
2068 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002069 /*
2070 * It's a pure offset.
2071 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002072 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2073 input->segment == NO_SEG) {
2074 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2075 input->type &= ~IP_REL;
2076 input->type |= MEMORY;
2077 }
2078
2079 if (input->eaflags & EAF_BYTEOFFS ||
2080 (input->eaflags & EAF_WORDOFFS &&
2081 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2082 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2083 }
2084
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002085 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002086 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002087 output->sib = GEN_SIB(0, 4, 5);
2088 output->bytes = 4;
2089 output->modrm = GEN_MODRM(0, rfield, 4);
2090 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002091 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002092 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002093 output->bytes = (addrbits != 16 ? 4 : 2);
2094 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2095 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002096 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002097 } else {
2098 /*
2099 * It's an indirection.
2100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002102 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002104 int t, it, bt; /* register numbers */
2105 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002106
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 if (s == 0)
2108 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002109
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002110 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002111 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002112 ix = nasm_reg_flags[i];
2113 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002114 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002115 ix = 0;
2116 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002119 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002120 bx = nasm_reg_flags[b];
2121 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002122 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002123 bx = 0;
2124 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002125
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002126 /* if either one are a vector register... */
2127 if ((ix|bx) & (XMMREG|YMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002128 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002129 int32_t o = input->offset;
2130 int mod, scale, index, base;
2131
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002132 /*
2133 * For a vector SIB, one has to be a vector and the other,
2134 * if present, a GPR. The vector must be the index operand.
2135 */
2136 if (it == -1 || (bx & (XMMREG|YMMREG) & ~REG_EA)) {
2137 if (s == 0)
2138 s = 1;
2139 else if (s != 1)
2140 goto err;
2141
2142 t = bt, bt = it, it = t;
2143 x = bx, bx = ix, ix = x;
2144 }
2145
2146 if (bt != -1) {
2147 if (REG_GPR & ~bx)
2148 goto err;
2149 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2150 sok &= bx;
2151 else
2152 goto err;
2153 }
2154
2155 /*
2156 * While we're here, ensure the user didn't specify
2157 * WORD or QWORD
2158 */
2159 if (input->disp_size == 16 || input->disp_size == 64)
2160 goto err;
2161
2162 if (addrbits == 16 ||
2163 (addrbits == 32 && !(sok & BITS32)) ||
2164 (addrbits == 64 && !(sok & BITS64)))
2165 goto err;
2166
2167 output->type = (ix & YMMREG & ~REG_EA)
2168 ? EA_YMMVSIB : EA_XMMVSIB;
2169
2170 output->rex |= rexflags(it, ix, REX_X);
2171 output->rex |= rexflags(bt, bx, REX_B);
2172
2173 index = it & 7; /* it is known to be != -1 */
2174
2175 switch (s) {
2176 case 1:
2177 scale = 0;
2178 break;
2179 case 2:
2180 scale = 1;
2181 break;
2182 case 4:
2183 scale = 2;
2184 break;
2185 case 8:
2186 scale = 3;
2187 break;
2188 default: /* then what the smeg is it? */
2189 goto err; /* panic */
2190 }
2191
2192 if (bt == -1) {
2193 base = 5;
2194 mod = 0;
2195 } else {
2196 base = (bt & 7);
2197 if (base != REG_NUM_EBP && o == 0 &&
2198 seg == NO_SEG && !forw_ref &&
2199 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2200 mod = 0;
2201 else if (input->eaflags & EAF_BYTEOFFS ||
2202 (o >= -128 && o <= 127 &&
2203 seg == NO_SEG && !forw_ref &&
2204 !(input->eaflags & EAF_WORDOFFS)))
2205 mod = 1;
2206 else
2207 mod = 2;
2208 }
2209
2210 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002211 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2212 output->modrm = GEN_MODRM(mod, rfield, 4);
2213 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002214 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002215 /*
2216 * it must be a 32/64-bit memory reference. Firstly we have
2217 * to check that all registers involved are type E/Rxx.
2218 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002219 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002220 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002221
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002222 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002223 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2224 sok &= ix;
2225 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002226 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002227 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002228
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002229 if (bt != -1) {
2230 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002231 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002232 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002233 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002234 sok &= bx;
2235 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002236
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002237 /*
2238 * While we're here, ensure the user didn't specify
2239 * WORD or QWORD
2240 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002241 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002242 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002243
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002244 if (addrbits == 16 ||
2245 (addrbits == 32 && !(sok & BITS32)) ||
2246 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002247 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002248
Keith Kaniosb7a89542007-04-12 02:40:54 +00002249 /* now reorganize base/index */
2250 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002251 ((hb == b && ht == EAH_NOTBASE) ||
2252 (hb == i && ht == EAH_MAKEBASE))) {
2253 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002254 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002255 x = bx, bx = ix, ix = x;
2256 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002257 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002258 bt = -1, bx = 0, s++;
2259 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002260 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002261 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002262 }
2263 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2264 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002265 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002266 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2267 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002268 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002270 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002271 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002272 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002273 x = ix, ix = bx, bx = x;
2274 }
2275 if (it == REG_NUM_ESP ||
2276 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002277 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002278
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002279 output->rex |= rexflags(it, ix, REX_X);
2280 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002281
Keith Kanios48af1772007-08-17 07:37:52 +00002282 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002283 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002285
Keith Kaniosb7a89542007-04-12 02:40:54 +00002286 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002287 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002289 } else {
2290 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002291 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002292 seg == NO_SEG && !forw_ref &&
2293 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002294 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002295 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002296 (o >= -128 && o <= 127 &&
2297 seg == NO_SEG && !forw_ref &&
2298 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002299 mod = 1;
2300 else
2301 mod = 2;
2302 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002303
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002304 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002305 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2306 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002307 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002308 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002310
Keith Kaniosb7a89542007-04-12 02:40:54 +00002311 if (it == -1)
2312 index = 4, s = 1;
2313 else
2314 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 switch (s) {
2317 case 1:
2318 scale = 0;
2319 break;
2320 case 2:
2321 scale = 1;
2322 break;
2323 case 4:
2324 scale = 2;
2325 break;
2326 case 8:
2327 scale = 3;
2328 break;
2329 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002330 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002332
Keith Kaniosb7a89542007-04-12 02:40:54 +00002333 if (bt == -1) {
2334 base = 5;
2335 mod = 0;
2336 } else {
2337 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002338 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002339 seg == NO_SEG && !forw_ref &&
2340 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002341 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002342 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002343 (o >= -128 && o <= 127 &&
2344 seg == NO_SEG && !forw_ref &&
2345 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002346 mod = 1;
2347 else
2348 mod = 2;
2349 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002350
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002351 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002352 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2353 output->modrm = GEN_MODRM(mod, rfield, 4);
2354 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 }
2356 } else { /* it's 16-bit */
2357 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002358 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002359
Keith Kaniosb7a89542007-04-12 02:40:54 +00002360 /* check for 64-bit long mode */
2361 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002362 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002363
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002365 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2366 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002367 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002368
Keith Kaniosb7a89542007-04-12 02:40:54 +00002369 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002370 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002371 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002372
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002374 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 if (b == -1 && i != -1) {
2376 int tmp = b;
2377 b = i;
2378 i = tmp;
2379 } /* swap */
2380 if ((b == R_SI || b == R_DI) && i != -1) {
2381 int tmp = b;
2382 b = i;
2383 i = tmp;
2384 }
2385 /* have BX/BP as base, SI/DI index */
2386 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002387 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 if (i != -1 && b != -1 &&
2389 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002390 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002391 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002392 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002393
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 rm = -1;
2395 if (i != -1)
2396 switch (i * 256 + b) {
2397 case R_SI * 256 + R_BX:
2398 rm = 0;
2399 break;
2400 case R_DI * 256 + R_BX:
2401 rm = 1;
2402 break;
2403 case R_SI * 256 + R_BP:
2404 rm = 2;
2405 break;
2406 case R_DI * 256 + R_BP:
2407 rm = 3;
2408 break;
2409 } else
2410 switch (b) {
2411 case R_SI:
2412 rm = 4;
2413 break;
2414 case R_DI:
2415 rm = 5;
2416 break;
2417 case R_BP:
2418 rm = 6;
2419 break;
2420 case R_BX:
2421 rm = 7;
2422 break;
2423 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002424 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002425 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002426
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002427 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2428 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002430 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002431 (o >= -128 && o <= 127 && seg == NO_SEG &&
2432 !forw_ref && !(input->eaflags & EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 mod = 1;
2434 else
2435 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002436
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002437 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002438 output->bytes = mod; /* bytes of offset needed */
2439 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 }
2441 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002442 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002443
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002444 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002445 return output->type;
2446
2447err:
2448 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002449}
2450
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002451static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002452{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002453 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002454 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002455
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002456 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002457
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002458 switch (ins->prefixes[PPS_ASIZE]) {
2459 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002460 valid &= 16;
2461 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002462 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002463 valid &= 32;
2464 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002465 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002466 valid &= 64;
2467 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002468 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002469 valid &= (addrbits == 32) ? 16 : 32;
2470 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002471 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002472 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002473 }
2474
2475 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002476 if (is_class(MEMORY, ins->oprs[j].type)) {
2477 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002478
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002479 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002480 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002481 i = 0;
2482 else
2483 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002484
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002486 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002487 b = 0;
2488 else
2489 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002491 if (ins->oprs[j].scale == 0)
2492 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002493
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002494 if (!i && !b) {
2495 int ds = ins->oprs[j].disp_size;
2496 if ((addrbits != 64 && ds > 8) ||
2497 (addrbits == 64 && ds == 16))
2498 valid &= ds;
2499 } else {
2500 if (!(REG16 & ~b))
2501 valid &= 16;
2502 if (!(REG32 & ~b))
2503 valid &= 32;
2504 if (!(REG64 & ~b))
2505 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002506
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002507 if (!(REG16 & ~i))
2508 valid &= 16;
2509 if (!(REG32 & ~i))
2510 valid &= 32;
2511 if (!(REG64 & ~i))
2512 valid &= 64;
2513 }
2514 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002515 }
2516
2517 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002518 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002519 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002520 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002521 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002523 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 /* Impossible... */
2525 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2526 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002527 }
2528
2529 defdisp = ins->addr_size == 16 ? 16 : 32;
2530
2531 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002532 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2533 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2534 /*
2535 * mem_offs sizes must match the address size; if not,
2536 * strip the MEM_OFFS bit and match only EA instructions
2537 */
2538 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2539 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002540 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002541}