blob: a1aef1cdc0028bb34ad5b1585f6e6712ba38b403 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 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 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000183#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
185#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000186#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800191#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvin65289e82009-07-25 17:25:11 -0700193enum match_result {
194 /*
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
197 */
198 MERR_INVALOP,
199 MERR_OPSIZEMISSING,
200 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700201 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700208 /*
209 * Matching success; the conditional ones first
210 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400211 MOK_JUMP, /* Matching OK but needs jmp_match() */
212 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700213};
214
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000215typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700216 enum ea_type type; /* what kind of EA is this? */
217 int sib_present; /* is a SIB byte necessary? */
218 int bytes; /* # of bytes of offset needed */
219 int size; /* lazy - this is sib+bytes+1 */
220 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700221 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000222} ea;
223
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400224#define GEN_SIB(scale, index, base) \
225 (((scale) << 6) | ((index) << 3) | ((base)))
226
227#define GEN_MODRM(mod, reg, rm) \
228 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
229
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400230static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700234static void gencode(int32_t segment, int64_t offset, int bits,
235 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400236 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700237static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 insn *instruction,
239 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700240static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700245static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700246static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000247
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700248static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400250static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000251{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700252 return ins->prefixes[pos] == prefix;
253}
254
255static void assert_no_prefix(insn * ins, enum prefix_pos pos)
256{
257 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800258 nasm_error(ERR_NONFATAL, "invalid %s prefix",
259 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700260}
261
262static const char *size_name(int size)
263{
264 switch (size) {
265 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700277 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700279 case 64:
280 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700281 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400282 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000283 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700284}
285
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400286static void warn_overflow(int pass, int size)
287{
H. Peter Anvin215186f2016-02-17 20:27:41 -0800288 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400289 "%s data exceeds bounds", size_name(size));
290}
291
292static void warn_overflow_const(int64_t data, int size)
293{
294 if (overflow_general(data, size))
295 warn_overflow(ERR_PASS1, size);
296}
297
298static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700299{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100300 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301 if (overflow_general(o->offset, size))
302 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700303 }
304}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400305
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000306/*
H. Peter Anvinb6412502016-02-11 21:07:40 -0800307 * Size of an address relocation, or zero if not an address
308 */
309static int addrsize(enum out_type type, uint64_t size)
310{
311 switch (type) {
312 case OUT_ADDRESS:
313 return abs((int)size);
314 case OUT_REL1ADR:
315 return 1;
316 case OUT_REL2ADR:
317 return 2;
318 case OUT_REL4ADR:
319 return 4;
320 case OUT_REL8ADR:
321 return 8;
322 default:
323 return 0;
324 }
325}
326
327/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000328 * This routine wrappers the real output format's output routine,
329 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800330 * generator at the same time, flatten unnecessary relocations,
331 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000332 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800333static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800334 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400335 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000336{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000337 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700338 static const char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800339 uint8_t p[8];
H. Peter Anvinb6412502016-02-11 21:07:40 -0800340 int asize = addrsize(type, size); /* Address size in bytes */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800341 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000342
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800343 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400344 /*
345 * This is a non-relocated address, and we're going to
346 * convert it into RAWDATA format.
347 */
348 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800349
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800350 if (asize > 8) {
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800351 nasm_panic(0, "OUT_ADDRESS with size > 8");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400352 return;
353 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700354
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800355 WRITEADDR(q, *(int64_t *)data, asize);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400356 data = p;
357 type = OUT_RAWDATA;
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800358 size = asize;
H. Peter Anvinb6412502016-02-11 21:07:40 -0800359 asize = 0; /* No longer an address */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000360 }
361
H. Peter Anvin172b8402016-02-18 01:16:18 -0800362 lfmt->output(offset, data, type, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800363
Frank Kotlerabebb082003-09-06 04:45:37 +0000364 /*
365 * this call to src_get determines when we call the
366 * debug-format-specific "linenum" function
367 * it updates lineno and lnfname to the current values
368 * returning 0 if "same as last time", -2 if lnfname
369 * changed, and the amount by which lineno changed,
370 * if it did. thus, these variables must be static
371 */
372
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400373 if (src_get(&lineno, &lnfname))
H. Peter Anvin335c4852016-02-17 20:55:08 -0800374 dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000375
H. Peter Anvinb6412502016-02-11 21:07:40 -0800376 if (asize && asize > amax) {
377 if (type != OUT_ADDRESS || (int)size < 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800378 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800379 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800380 asize << 3, ofmt->shortname);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800381 size = asize;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800382 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800383 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800384 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800385 asize << 3, ofmt->maxbits);
386 ofmt->output(segto, data, type, amax, segment, wrt);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800387 size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800388 }
389 data = zero_buffer;
390 type = OUT_RAWDATA;
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800391 segment = wrt = NO_SEG;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800392 }
393
H. Peter Anvin215186f2016-02-17 20:27:41 -0800394 ofmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395}
396
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800397static void out_imm8(int64_t offset, int32_t segment,
398 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400399{
400 if (opx->segment != NO_SEG) {
401 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800402 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400403 } else {
404 uint8_t byte = opx->offset;
405 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
406 }
407}
408
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700409static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800410 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800412 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800413 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800415 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000416
H. Peter Anvin755f5212012-02-25 11:41:34 -0800417 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700418 return false;
419 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400420 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700421 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400422 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700423
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800424 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100425
Victor van den Elzen154e5922009-02-25 17:32:00 +0100426 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100427 /* Be optimistic in pass 1 */
428 return true;
429
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700431 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000432
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700433 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800434 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
435
436 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
437 /* jmp short (opcode eb) cannot be used with bnd prefix. */
438 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800439 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800440 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800441 }
442
443 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000445
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400446int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800447 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000448{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000449 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700451 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800452 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000453 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800454 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300455 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000456
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000457 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000458
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300459 wsize = idata_bytes(instruction->opcode);
460 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000462
H. Peter Anvineba20a72002-04-30 20:53:55 +0000463 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000465 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800467 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000468
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400470 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400472 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800473 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400474 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000475 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700476 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800478 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400479 offset += wsize;
480 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700481 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400482 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800486 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000488
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 if (align) {
490 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100491 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800492 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 }
494 offset += e->stringlen + align;
495 }
496 }
497 if (t > 0 && t == instruction->times - 1) {
498 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800499 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 * listing module.
501 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800502 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
503 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 }
505 }
506 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800507 lfmt->downlevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000509 }
510
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700512 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400515 fp = fopen(fname, "rb");
516 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800517 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400519 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800520 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200522 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400523 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700524 static char buf[4096];
525 size_t t = instruction->times;
526 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400527 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 len = ftell(fp);
530 if (instruction->eops->next) {
531 base = instruction->eops->next->offset;
532 len -= base;
533 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700534 len > (size_t)instruction->eops->next->next->offset)
535 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 }
537 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800538 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 * listing module.
540 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800541 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
542 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700544 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 fseek(fp, base, SEEK_SET);
547 l = len;
548 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700549 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400550 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 if (!m) {
552 /*
553 * This shouldn't happen unless the file
554 * actually changes while we are reading
555 * it.
556 */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800557 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 "`incbin': unexpected EOF while"
559 " reading file `%s'", fname);
560 t = 0; /* Try to exit cleanly */
561 break;
562 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800563 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 NO_SEG, NO_SEG);
565 l -= m;
566 }
567 }
H. Peter Anvin172b8402016-02-18 01:16:18 -0800568 lfmt->downlevel(LIST_INCBIN);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 if (instruction->times > 1) {
570 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800571 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 * listing module.
573 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800574 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
575 lfmt->uplevel(LIST_TIMES);
576 lfmt->downlevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 }
578 fclose(fp);
579 return instruction->times * len;
580 }
581 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 }
583
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700584 /* Check to see if we need an address-size prefix */
585 add_asp(instruction, bits);
586
H. Peter Anvin23595f52009-07-25 17:44:25 -0700587 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700588
H. Peter Anvin23595f52009-07-25 17:44:25 -0700589 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400590 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800591 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400592 itimes = instruction->times;
593 if (insn_size < 0) /* shouldn't be, on pass two */
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800594 nasm_panic(0, "errors made it through from pass one");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400595 else
596 while (itimes--) {
597 for (j = 0; j < MAXPREFIX; j++) {
598 uint8_t c = 0;
599 switch (instruction->prefixes[j]) {
600 case P_WAIT:
601 c = 0x9B;
602 break;
603 case P_LOCK:
604 c = 0xF0;
605 break;
606 case P_REPNE:
607 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800608 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700609 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400610 c = 0xF2;
611 break;
612 case P_REPE:
613 case P_REPZ:
614 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800615 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400616 c = 0xF3;
617 break;
618 case R_CS:
619 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800620 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400621 "cs segment base generated, but will be ignored in 64-bit mode");
622 }
623 c = 0x2E;
624 break;
625 case R_DS:
626 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800627 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400628 "ds segment base generated, but will be ignored in 64-bit mode");
629 }
630 c = 0x3E;
631 break;
632 case R_ES:
633 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800634 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400635 "es segment base generated, but will be ignored in 64-bit mode");
636 }
637 c = 0x26;
638 break;
639 case R_FS:
640 c = 0x64;
641 break;
642 case R_GS:
643 c = 0x65;
644 break;
645 case R_SS:
646 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800647 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400648 "ss segment base generated, but will be ignored in 64-bit mode");
649 }
650 c = 0x36;
651 break;
652 case R_SEGR6:
653 case R_SEGR7:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800654 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400655 "segr6 and segr7 cannot be used as prefixes");
656 break;
657 case P_A16:
658 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800659 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400660 "16-bit addressing is not supported "
661 "in 64-bit mode");
662 } else if (bits != 16)
663 c = 0x67;
664 break;
665 case P_A32:
666 if (bits != 32)
667 c = 0x67;
668 break;
669 case P_A64:
670 if (bits != 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800671 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400672 "64-bit addressing is only supported "
673 "in 64-bit mode");
674 }
675 break;
676 case P_ASP:
677 c = 0x67;
678 break;
679 case P_O16:
680 if (bits != 16)
681 c = 0x66;
682 break;
683 case P_O32:
684 if (bits == 16)
685 c = 0x66;
686 break;
687 case P_O64:
688 /* REX.W */
689 break;
690 case P_OSP:
691 c = 0x66;
692 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700693 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800694 case P_VEX3:
695 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800696 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400697 case P_none:
698 break;
699 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800700 nasm_panic(0, "invalid instruction prefix");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400701 }
702 if (c != 0) {
703 out(offset, segment, &c, OUT_RAWDATA, 1,
704 NO_SEG, NO_SEG);
705 offset++;
706 }
707 }
708 insn_end = offset + insn_size;
709 gencode(segment, offset, bits, instruction,
710 temp, insn_end);
711 offset += insn_size;
712 if (itimes > 0 && itimes == instruction->times - 1) {
713 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800714 * Dummy call to lfmt->output to give the offset to the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400715 * listing module.
716 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800717 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
718 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400719 }
720 }
721 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800722 lfmt->downlevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400723 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700724 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400725 /* No match */
726 switch (m) {
727 case MERR_OPSIZEMISSING:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800728 nasm_error(ERR_NONFATAL, "operation size not specified");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400729 break;
730 case MERR_OPSIZEMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800731 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400732 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700733 case MERR_BRNUMMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800734 nasm_error(ERR_NONFATAL,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700735 "mismatch in the number of broadcasting elements");
736 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400737 case MERR_BADCPU:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800738 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400739 break;
740 case MERR_BADMODE:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800741 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400742 bits);
743 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800744 case MERR_ENCMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800745 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800746 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800747 case MERR_BADBND:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800748 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800749 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800750 case MERR_BADREPNE:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800751 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800752 (has_prefix(instruction, PPS_REP, P_REPNE) ?
753 "repne" : "repnz"));
754 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400755 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800756 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400758 break;
759 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760 }
761 return 0;
762}
763
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400764int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800765 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000766{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000767 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700768 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000769
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000770 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400772 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000774
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700775 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
776 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400777 instruction->opcode == I_DT || instruction->opcode == I_DO ||
778 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300780 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300783 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000784
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400785 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000786 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000787
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400789 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400791 warn_overflow_const(e->offset, wsize);
792 } else if (e->type == EOT_DB_STRING ||
793 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 align = (-osize) % wsize;
797 if (align < 0)
798 align += wsize;
799 isize += osize + align;
800 }
801 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802 }
803
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400805 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300807 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700808 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400810 fp = fopen(fname, "rb");
811 if (!fp)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800812 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 fname);
814 else if (fseek(fp, 0L, SEEK_END) < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800815 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 fname);
817 else {
818 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 if (instruction->eops->next) {
820 len -= instruction->eops->next->offset;
821 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700822 len > (size_t)instruction->eops->next->next->offset) {
823 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 }
825 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300826 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300828 if (fp)
829 fclose(fp);
830 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831 }
832
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700833 /* Check to see if we need an address-size prefix */
834 add_asp(instruction, bits);
835
H. Peter Anvin23595f52009-07-25 17:44:25 -0700836 m = find_match(&temp, instruction, segment, offset, bits);
837 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400838 /* we've matched an instruction. */
839 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400840 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100841
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800842 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400843 if (isize < 0)
844 return -1;
845 for (j = 0; j < MAXPREFIX; j++) {
846 switch (instruction->prefixes[j]) {
847 case P_A16:
848 if (bits != 16)
849 isize++;
850 break;
851 case P_A32:
852 if (bits != 32)
853 isize++;
854 break;
855 case P_O16:
856 if (bits != 16)
857 isize++;
858 break;
859 case P_O32:
860 if (bits == 16)
861 isize++;
862 break;
863 case P_A64:
864 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700865 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800866 case P_VEX3:
867 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800868 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400869 case P_none:
870 break;
871 default:
872 isize++;
873 break;
874 }
875 }
876 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700877 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400878 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000879 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000880}
881
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800882static void bad_hle_warn(const insn * ins, uint8_t hleok)
883{
884 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800886 static const enum whatwarn warn[2][4] =
887 {
888 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
889 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
890 };
891 unsigned int n;
892
893 n = (unsigned int)rep_pfx - P_XACQUIRE;
894 if (n > 1)
895 return; /* Not XACQUIRE/XRELEASE */
896
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800897 ww = warn[n][hleok];
898 if (!is_class(MEMORY, ins->oprs[0].type))
899 ww = w_inval; /* HLE requires operand 0 to be memory */
900
901 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800902 case w_none:
903 break;
904
905 case w_lock:
906 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800907 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800908 "%s with this instruction requires lock",
909 prefix_name(rep_pfx));
910 }
911 break;
912
913 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800914 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800915 "%s invalid with this instruction",
916 prefix_name(rep_pfx));
917 break;
918 }
919}
920
H. Peter Anvin507ae032008-10-09 15:37:10 -0700921/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400922#define case3(x) case (x): case (x)+1: case (x)+2
923#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700924
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800925static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800926 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000927{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800928 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800929 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000930 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000931 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700932 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700933 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700934 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700935 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800936 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800937 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700938 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000939
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700940 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700941 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700942 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700943
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700944 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400945 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700946
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 (void)segment; /* Don't warn that this parameter is unused */
948 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000949
H. Peter Anvin839eca22007-10-29 23:12:47 -0700950 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400951 c = *codes++;
952 op1 = (c & 3) + ((opex & 1) << 2);
953 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
954 opx = &ins->oprs[op1];
955 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700956
H. Peter Anvin839eca22007-10-29 23:12:47 -0700957 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400958 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 codes += c, length += c;
960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400962 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400963 opex = c;
964 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(010):
967 ins->rex |=
968 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 codes++, length++;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Jin Kyu Song164d6072013-10-15 19:10:13 -0700972 case4(014):
973 /* this is an index reg of MIB operand */
974 mib_index = opx->basereg;
975 break;
976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(020):
978 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 length++;
980 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700981
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400982 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 length += 2;
984 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700985
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400986 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700987 if (opx->type & (BITS16 | BITS32 | BITS64))
988 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 else
990 length += (bits == 16) ? 2 : 4;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 length += 4;
995 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700998 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 length++;
1003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001006 length += 8; /* MOV reg64/imm */
1007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 length += 2;
1011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001014 if (opx->type & (BITS16 | BITS32 | BITS64))
1015 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 else
1017 length += (bits == 16) ? 2 : 4;
1018 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001019
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 length += 4;
1022 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001023
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001024 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001025 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001027
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 case 0172:
1029 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001031 length++;
1032 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001033
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001034 case4(0174):
1035 length++;
1036 break;
1037
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001038 case4(0240):
1039 ins->rex |= REX_EV;
1040 ins->vexreg = regval(opx);
1041 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1042 ins->vex_cm = *codes++;
1043 ins->vex_wlp = *codes++;
1044 ins->evex_tuple = (*codes++ - 0300);
1045 break;
1046
1047 case 0250:
1048 ins->rex |= REX_EV;
1049 ins->vexreg = 0;
1050 ins->vex_cm = *codes++;
1051 ins->vex_wlp = *codes++;
1052 ins->evex_tuple = (*codes++ - 0300);
1053 break;
1054
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001055 case4(0254):
1056 length += 4;
1057 break;
1058
1059 case4(0260):
1060 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001061 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 ins->vex_cm = *codes++;
1063 ins->vex_wlp = *codes++;
1064 break;
1065
1066 case 0270:
1067 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001068 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001069 ins->vex_cm = *codes++;
1070 ins->vex_wlp = *codes++;
1071 break;
1072
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001073 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001074 hleok = c & 3;
1075 break;
1076
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001077 case4(0274):
1078 length++;
1079 break;
1080
1081 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001083
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001085 if (bits == 64)
1086 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001087 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001091 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
Keith Kaniosb7a89542007-04-12 02:40:54 +00001097 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001098 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1099 has_prefix(ins, PPS_ASIZE, P_A32))
1100 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001103 case4(0314):
1104 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001107 {
1108 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1109 if (pfx == P_O16)
1110 break;
1111 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001112 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001113 else
1114 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001116 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001119 {
1120 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1121 if (pfx == P_O32)
1122 break;
1123 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001124 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001125 else
1126 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001128 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 case 0322:
1131 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001132
Keith Kaniosb7a89542007-04-12 02:40:54 +00001133 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001134 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001135 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
Keith Kaniosb7a89542007-04-12 02:40:54 +00001137 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001141 case 0325:
1142 ins->rex |= REX_NH;
1143 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001144
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001145 case 0326:
1146 break;
1147
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 case 0330:
1149 codes++, length++;
1150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001155 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 case 0333:
1157 length++;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001160 case 0334:
1161 ins->rex |= REX_L;
1162 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001163
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001164 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001165 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001168 if (!ins->prefixes[PPS_REP])
1169 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001171
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001173 if (!ins->prefixes[PPS_REP])
1174 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001176
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001179 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001181 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1182 nasm_error(ERR_WARNING | ERR_PASS1,
1183 "forward reference in RESx can result in unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001185 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001187
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 case 0341:
1189 if (!ins->prefixes[PPS_WAIT])
1190 ins->prefixes[PPS_WAIT] = P_WAIT;
1191 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 case 0360:
1194 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001195
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001196 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 length++;
1198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 case 0364:
1201 case 0365:
1202 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001203
Keith Kanios48af1772007-08-17 07:37:52 +00001204 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001205 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001206 length++;
1207 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001208
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001209 case 0370:
1210 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 case 0373:
1214 length++;
1215 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001216
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001217 case 0374:
1218 eat = EA_XMMVSIB;
1219 break;
1220
1221 case 0375:
1222 eat = EA_YMMVSIB;
1223 break;
1224
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001225 case 0376:
1226 eat = EA_ZMMVSIB;
1227 break;
1228
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001229 case4(0100):
1230 case4(0110):
1231 case4(0120):
1232 case4(0130):
1233 case4(0200):
1234 case4(0204):
1235 case4(0210):
1236 case4(0214):
1237 case4(0220):
1238 case4(0224):
1239 case4(0230):
1240 case4(0234):
1241 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001243 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001244 opflags_t rflags;
1245 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001246 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001247
Keith Kaniosb7a89542007-04-12 02:40:54 +00001248 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001249
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 if (c <= 0177) {
1251 /* pick rfield from operand b (opx) */
1252 rflags = regflag(opx);
1253 rfield = nasm_regvals[opx->basereg];
1254 } else {
1255 rflags = 0;
1256 rfield = c & 7;
1257 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001258
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001259 /* EVEX.b1 : evex_brerop contains the operand position */
1260 op_er_sae = (ins->evex_brerop >= 0 ?
1261 &ins->oprs[ins->evex_brerop] : NULL);
1262
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001263 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1264 /* set EVEX.b */
1265 ins->evex_p[2] |= EVEX_P2B;
1266 if (op_er_sae->decoflags & ER) {
1267 /* set EVEX.RC (rounding control) */
1268 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1269 & EVEX_P2RC;
1270 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001271 } else {
1272 /* set EVEX.L'L (vector length) */
1273 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001274 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001275 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001276 /* set EVEX.b */
1277 ins->evex_p[2] |= EVEX_P2B;
1278 }
1279 }
1280
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001281 if (itemp_has(temp, IF_MIB)) {
1282 opy->eaflags |= EAF_MIB;
1283 /*
1284 * if a separate form of MIB (ICC style) is used,
1285 * the index reg info is merged into mem operand
1286 */
1287 if (mib_index != R_none) {
1288 opy->indexreg = mib_index;
1289 opy->scale = 1;
1290 opy->hintbase = mib_index;
1291 opy->hinttype = EAH_NOTBASE;
1292 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001293 }
1294
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001295 if (process_ea(opy, &ea_data, bits,
1296 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001297 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001299 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001302 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 }
1304 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001305
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001307 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 ": instruction code \\%o (0x%02X) given", c, c);
1309 break;
1310 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001311 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001312
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001313 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001314
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001315 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001317 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 return -1;
1319 }
1320 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001321 }
1322
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001323 switch (ins->prefixes[PPS_VEX]) {
1324 case P_EVEX:
1325 if (!(ins->rex & REX_EV))
1326 return -1;
1327 break;
1328 case P_VEX3:
1329 case P_VEX2:
1330 if (!(ins->rex & REX_V))
1331 return -1;
1332 break;
1333 default:
1334 break;
1335 }
1336
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001337 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001339
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001341 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 return -1;
1343 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001344 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001346 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 ins->rex &= ~REX_W;
1348 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001349 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 ins->rex |= REX_W;
1351 bad32 &= ~REX_W;
1352 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001353 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 /* Follow REX_W */
1355 break;
1356 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001357
H. Peter Anvinfc561202011-07-07 16:58:22 -07001358 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001359 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001361 } else if (!(ins->rex & REX_EV) &&
1362 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001363 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001364 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001366 if (ins->rex & REX_EV)
1367 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001368 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1369 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 length += 3;
1371 else
1372 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001373 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001375 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 return -1;
1377 } else if (bits == 64) {
1378 length++;
1379 } else if ((ins->rex & REX_L) &&
1380 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001381 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001383 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001384 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001385 length++;
1386 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001387 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001388 return -1;
1389 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001390 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001391
1392 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001393 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001394 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001395 "instruction is not lockable");
1396 }
1397
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001398 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001399
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001400 /*
1401 * when BND prefix is set by DEFAULT directive,
1402 * BND prefix is added to every appropriate instruction line
1403 * unless it is overridden by NOBND prefix.
1404 */
1405 if (globalbnd &&
1406 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1407 ins->prefixes[PPS_REP] = P_BND;
1408
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001409 return length;
1410}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001411
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001412static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1413{
1414 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001415 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001416 !(ins->rex & (REX_V | REX_EV)) &&
1417 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001418 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001419 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001420 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001421 return 1;
1422 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001423 }
1424
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001425 return 0;
1426}
1427
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001428static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001429 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001430 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001431{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001432 uint8_t c;
1433 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001434 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001435 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001436 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001437 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001438 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001439 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001440 enum ea_type eat = EA_SCALAR;
H. Peter Anvin976ba732016-09-20 16:39:46 -07001441 int r;
H. Peter Anvin70653092007-10-19 14:42:29 -07001442
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001443 ins->rex_done = false;
1444
H. Peter Anvin839eca22007-10-29 23:12:47 -07001445 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 c = *codes++;
1447 op1 = (c & 3) + ((opex & 1) << 2);
1448 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1449 opx = &ins->oprs[op1];
1450 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001451
H. Peter Anvin839eca22007-10-29 23:12:47 -07001452 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 case 01:
1454 case 02:
1455 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001457 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001458 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 codes += c;
1460 offset += c;
1461 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001462
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001463 case 05:
1464 case 06:
1465 case 07:
1466 opex = c;
1467 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001468
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001469 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001470 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001471 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001472 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 offset += 1;
1474 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001475
Jin Kyu Song164d6072013-10-15 19:10:13 -07001476 case4(014):
1477 break;
1478
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001479 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001480 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001481 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001482 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001484 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 offset += 1;
1486 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001489 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001490 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001492 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 offset += 1;
1494 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001495
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001497 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001498 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001499 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 offset += 2;
1502 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001503
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001505 if (opx->type & (BITS16 | BITS32))
1506 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 else
1508 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001509 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001510 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001511 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001512 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 offset += size;
1514 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001515
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001517 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001518 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001519 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001520 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 offset += 4;
1522 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001523
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001524 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001525 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001526 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001527 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001528 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001529 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 offset += size;
1531 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001532
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001534 if (opx->segment != segment) {
1535 data = opx->offset;
1536 out(offset, segment, &data,
1537 OUT_REL1ADR, insn_end - offset,
1538 opx->segment, opx->wrt);
1539 } else {
1540 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001541 if (data > 127 || data < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001542 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001543 out(offset, segment, &data,
1544 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1545 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 offset += 1;
1547 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001548
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001549 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001551 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001552 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001553 offset += 8;
1554 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001555
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001556 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001557 if (opx->segment != segment) {
1558 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001560 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001561 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001563 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001565 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 }
1567 offset += 2;
1568 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001571 if (opx->type & (BITS16 | BITS32 | BITS64))
1572 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 else
1574 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001575 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001576 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001577 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1579 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001581 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001583 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 }
1585 offset += size;
1586 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001589 if (opx->segment != segment) {
1590 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001592 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001593 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001595 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001597 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 }
1599 offset += 4;
1600 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001603 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001604 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001605 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001607 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001608 ofmt->segbase(1 + opx->segment),
H. Peter Anvin839eca22007-10-29 23:12:47 -07001609 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001610 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case 0172:
H. Peter Anvin976ba732016-09-20 16:39:46 -07001614 {
1615 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1616 const struct operand *opy;
1617
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001618 c = *codes++;
1619 opx = &ins->oprs[c >> 3];
H. Peter Anvin976ba732016-09-20 16:39:46 -07001620 opy = &ins->oprs[c & 7];
1621 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001622 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 "non-absolute expression not permitted as argument %d",
1624 c & 7);
H. Peter Anvin976ba732016-09-20 16:39:46 -07001625 } else if (opy->offset & ~mask) {
1626 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1627 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 }
H. Peter Anvin976ba732016-09-20 16:39:46 -07001629 c = opy->offset & mask;
1630 goto emit_is4;
1631 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case 0173:
1634 c = *codes++;
1635 opx = &ins->oprs[c >> 4];
H. Peter Anvin976ba732016-09-20 16:39:46 -07001636 c &= 15;
1637 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001638
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001639 case4(0174):
H. Peter Anvin976ba732016-09-20 16:39:46 -07001640 c = 0;
1641 emit_is4:
1642 r = nasm_regvals[opx->basereg];
1643 bytes[0] = (r << 4) | ((r & 0x10) >> 1) | c;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1645 offset++;
1646 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001647
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001649 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1651 (int32_t)data != (int64_t)data) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001652 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001653 "signed dword immediate exceeds bounds");
1654 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001655 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 opx->segment, opx->wrt);
1657 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001658 break;
1659
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001660 case4(0240):
1661 case 0250:
1662 codes += 3;
1663 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1664 EVEX_P2Z | EVEX_P2AAA, 2);
1665 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1666 bytes[0] = 0x62;
1667 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001668 bytes[1] = ((((ins->rex & 7) << 5) |
1669 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1670 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001671 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1672 ((~ins->vexreg & 15) << 3) |
1673 (1 << 2) | (ins->vex_wlp & 3);
1674 bytes[3] = ins->evex_p[2];
1675 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1676 offset += 4;
1677 break;
1678
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 case4(0260):
1680 case 0270:
1681 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001682 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1683 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1685 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1686 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001687 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1689 offset += 3;
1690 } else {
1691 bytes[0] = 0xc5;
1692 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001693 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1695 offset += 2;
1696 }
1697 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001698
H. Peter Anvine014f352012-02-25 22:35:19 -08001699 case 0271:
1700 case 0272:
1701 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001702 break;
1703
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 case4(0274):
1705 {
1706 uint64_t uv, um;
1707 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001708
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 if (ins->rex & REX_W)
1710 s = 64;
1711 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1712 s = 16;
1713 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1714 s = 32;
1715 else
1716 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001717
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001718 um = (uint64_t)2 << (s-1);
1719 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 if (uv > 127 && uv < (uint64_t)-128 &&
1722 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001723 /* If this wasn't explicitly byte-sized, warn as though we
1724 * had fallen through to the imm16/32/64 case.
1725 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001726 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001727 "%s value exceeds bounds",
1728 (opx->type & BITS8) ? "signed byte" :
1729 s == 16 ? "word" :
1730 s == 32 ? "dword" :
1731 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001732 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001733 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001734 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001735 out(offset, segment, &data, OUT_ADDRESS, 1,
1736 opx->segment, opx->wrt);
1737 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001738 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001739 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1740 NO_SEG);
1741 }
1742 offset += 1;
1743 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001748
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001750 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001752 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 offset += 1;
1754 } else
1755 offset += 0;
1756 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001759 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001761 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 offset += 1;
1763 } else
1764 offset += 0;
1765 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 case 0312:
1768 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001769
Keith Kaniosb7a89542007-04-12 02:40:54 +00001770 case 0313:
1771 ins->rex = 0;
1772 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001773
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 case4(0314):
1775 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001782 case 0323:
1783 break;
1784
Keith Kaniosb7a89542007-04-12 02:40:54 +00001785 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001786 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case 0325:
1790 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001791
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001792 case 0326:
1793 break;
1794
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001796 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001797 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 offset += 1;
1799 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001803
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001804 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001806 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001807 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 offset += 1;
1809 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
Keith Kanios48af1772007-08-17 07:37:52 +00001811 case 0334:
1812 if (ins->rex & REX_R) {
1813 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001814 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001815 offset += 1;
1816 }
1817 ins->rex &= ~(REX_L|REX_R);
1818 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001819
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001820 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001822
H. Peter Anvin962e3052008-08-28 17:47:16 -07001823 case 0336:
1824 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001826
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001829 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001831 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 if (size > 0)
1833 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001834 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 offset += size;
1836 }
1837 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001838
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 case 0341:
1840 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001841
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 case 0360:
1843 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001844
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 case 0361:
1846 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001847 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1848 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001849 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 case 0364:
1852 case 0365:
1853 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001854
Keith Kanios48af1772007-08-17 07:37:52 +00001855 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001856 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001857 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001858 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001859 offset += 1;
1860 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001861
Jin Kyu Song03041092013-10-15 19:38:51 -07001862 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001864
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 case 0373:
1866 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001867 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 offset += 1;
1869 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001870
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001871 case 0374:
1872 eat = EA_XMMVSIB;
1873 break;
1874
1875 case 0375:
1876 eat = EA_YMMVSIB;
1877 break;
1878
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001879 case 0376:
1880 eat = EA_ZMMVSIB;
1881 break;
1882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 case4(0100):
1884 case4(0110):
1885 case4(0120):
1886 case4(0130):
1887 case4(0200):
1888 case4(0204):
1889 case4(0210):
1890 case4(0214):
1891 case4(0220):
1892 case4(0224):
1893 case4(0230):
1894 case4(0234):
1895 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 ea ea_data;
1897 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001898 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001899 uint8_t *p;
1900 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001902
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001903 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001904 /* pick rfield from operand b (opx) */
1905 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001906 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 } else {
1908 /* rfield is constant */
1909 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001913 if (process_ea(opy, &ea_data, bits,
1914 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001915 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001916
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 p = bytes;
1918 *p++ = ea_data.modrm;
1919 if (ea_data.sib_present)
1920 *p++ = ea_data.sib;
1921
1922 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001923 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001925 /*
1926 * Make sure the address gets the right offset in case
1927 * the line breaks in the .lst file (BR 1197827)
1928 */
1929 offset += s;
1930 s = 0;
1931
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001932 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001933 /* use compressed displacement, if available */
1934 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001936 if (ea_data.rip) {
1937 if (opy->segment == segment) {
1938 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001939 if (overflow_signed(data, ea_data.bytes))
1940 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001941 out(offset, segment, &data, OUT_ADDRESS,
1942 ea_data.bytes, NO_SEG, NO_SEG);
1943 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001944 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001945 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001946 insn_end - offset, opy->segment, opy->wrt);
1947 }
1948 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001949 int asize = ins->addr_size >> 3;
1950 int atype = ea_data.bytes;
1951
1952 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001953 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001954 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001955 warn_overflow(ERR_PASS2, ea_data.bytes);
1956
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001957 if (asize > ea_data.bytes) {
1958 /*
1959 * If the address isn't the full width of
1960 * the address size, treat is as signed...
1961 */
1962 atype = -atype;
1963 }
1964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001965 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001966 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 }
1969 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 }
1971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001973 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001974 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001975 ": instruction code \\%o (0x%02X) given", c, c);
1976 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001978 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001979}
1980
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001981static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001982{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001983 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001984 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001985 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986}
1987
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001988static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001989{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001990 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001991 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001992 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001993}
1994
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995static int op_rexflags(const operand * o, int mask)
1996{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001997 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001998 int val;
1999
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002000 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08002001 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002002
H. Peter Anvina4835d42008-05-20 14:21:29 -07002003 flags = nasm_reg_flags[o->basereg];
2004 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002005
2006 return rexflags(val, flags, mask);
2007}
2008
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002009static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002010{
2011 int rex = 0;
2012
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002013 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002014 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002015 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002016 rex |= REX_W;
2017 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2018 rex |= REX_H;
2019 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2020 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002021
2022 return rex & mask;
2023}
2024
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002025static int evexflags(int val, decoflags_t deco,
2026 int mask, uint8_t byte)
2027{
2028 int evex = 0;
2029
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002030 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002031 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002032 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002033 evex |= (EVEX_P0RP | EVEX_P0X);
2034 break;
2035 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002036 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002037 evex |= EVEX_P2VP;
2038 if (deco & Z)
2039 evex |= EVEX_P2Z;
2040 if (deco & OPMASK_MASK)
2041 evex |= deco & EVEX_P2AAA;
2042 break;
2043 }
2044 return evex & mask;
2045}
2046
2047static int op_evexflags(const operand * o, int mask, uint8_t byte)
2048{
2049 int val;
2050
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002051 val = nasm_regvals[o->basereg];
2052
2053 return evexflags(val, o->decoflags, mask, byte);
2054}
2055
H. Peter Anvin23595f52009-07-25 17:44:25 -07002056static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002057 insn *instruction,
2058 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002059{
2060 const struct itemplate *temp;
2061 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002062 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002063 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002064 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002065 int i;
2066
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002067 /* broadcasting uses a different data element size */
2068 for (i = 0; i < instruction->operands; i++)
2069 if (i == broadcast)
2070 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2071 else
2072 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002073
2074 merr = MERR_INVALOP;
2075
2076 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002077 temp->opcode != I_none; temp++) {
2078 m = matches(temp, instruction, bits);
2079 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002080 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 m = MOK_GOOD;
2082 else
2083 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002084 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 /*
2086 * Missing operand size and a candidate for fuzzy matching...
2087 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002088 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002089 if (i == broadcast)
2090 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2091 else
2092 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002093 opsizemissing = true;
2094 }
2095 if (m > merr)
2096 merr = m;
2097 if (merr == MOK_GOOD)
2098 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002099 }
2100
2101 /* No match, but see if we can get a fuzzy operand size match... */
2102 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002103 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002104
2105 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002106 /*
2107 * We ignore extrinsic operand sizes on registers, so we should
2108 * never try to fuzzy-match on them. This also resolves the case
2109 * when we have e.g. "xmmrm128" in two different positions.
2110 */
2111 if (is_class(REGISTER, instruction->oprs[i].type))
2112 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002113
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002114 /* This tests if xsizeflags[i] has more than one bit set */
2115 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2116 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002117
Jin Kyu Song7903c072013-10-30 03:00:12 -07002118 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002119 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002120 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2121 BITS32 : BITS64);
2122 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002123 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002124 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002125 }
2126
2127 /* Try matching again... */
2128 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002129 temp->opcode != I_none; temp++) {
2130 m = matches(temp, instruction, bits);
2131 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002132 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 m = MOK_GOOD;
2134 else
2135 m = MERR_INVALOP;
2136 }
2137 if (m > merr)
2138 merr = m;
2139 if (merr == MOK_GOOD)
2140 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002141 }
2142
H. Peter Anvina81655b2009-07-25 18:15:28 -07002143done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002144 *tempp = temp;
2145 return merr;
2146}
2147
Mark Charneydcaef4b2014-10-09 13:45:17 -04002148static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2149{
2150 opflags_t opsize = opflags & SIZE_MASK;
2151 uint8_t brcast_num;
2152
2153 /*
2154 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2155 * this cannot be a simple arithmetic calculation.
2156 */
2157 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002158 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002159 "size of broadcasting element is greater than 64 bits");
2160
2161 switch (opsize) {
2162 case BITS64:
2163 brcast_num = BITS64 / brsize;
2164 break;
2165 default:
2166 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2167 break;
2168 }
2169
2170 return brcast_num;
2171}
2172
H. Peter Anvin65289e82009-07-25 17:25:11 -07002173static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002174 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002175{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002176 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002177 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002178 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002180 /*
2181 * Check the opcode
2182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002183 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002184 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002185
2186 /*
2187 * Count the operands
2188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002189 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002190 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002191
2192 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002193 * Is it legal?
2194 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002195 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002196 return MERR_INVALOP;
2197
2198 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002199 * {evex} available?
2200 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002201 switch (instruction->prefixes[PPS_VEX]) {
2202 case P_EVEX:
2203 if (!itemp_has(itemp, IF_EVEX))
2204 return MERR_ENCMISMATCH;
2205 break;
2206 case P_VEX3:
2207 case P_VEX2:
2208 if (!itemp_has(itemp, IF_VEX))
2209 return MERR_ENCMISMATCH;
2210 break;
2211 default:
2212 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002213 }
2214
2215 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002216 * Check that no spurious colons or TOs are present
2217 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002218 for (i = 0; i < itemp->operands; i++)
2219 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002220 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002221
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002222 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002223 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002224 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002225 switch (itemp_smask(itemp)) {
2226 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002227 asize = BITS8;
2228 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 asize = BITS16;
2231 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002232 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002233 asize = BITS32;
2234 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002235 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 asize = BITS64;
2237 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002238 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002239 asize = BITS128;
2240 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002241 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002242 asize = BITS256;
2243 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002244 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002245 asize = BITS512;
2246 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002247 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002248 switch (bits) {
2249 case 16:
2250 asize = BITS16;
2251 break;
2252 case 32:
2253 asize = BITS32;
2254 break;
2255 case 64:
2256 asize = BITS64;
2257 break;
2258 default:
2259 asize = 0;
2260 break;
2261 }
2262 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002263 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002264 asize = 0;
2265 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002266 }
2267
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002268 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002270 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002271 memset(size, 0, sizeof size);
2272 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002273 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002274 /* S- flags apply to all operands */
2275 for (i = 0; i < MAX_OPERANDS; i++)
2276 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002277 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002278
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002279 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002280 * Check that the operand flags all match up,
2281 * it's a bit tricky so lets be verbose:
2282 *
2283 * 1) Find out the size of operand. If instruction
2284 * doesn't have one specified -- we're trying to
2285 * guess it either from template (IF_S* flag) or
2286 * from code bits.
2287 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002288 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002289 * template has an operand size specified AND this size differ
2290 * from which instruction has (perhaps we got it from code bits)
2291 * we are:
2292 * a) Check that only size of instruction and operand is differ
2293 * other characteristics do match
2294 * b) Perhaps it's a register specified in instruction so
2295 * for such a case we just mark that operand as "size
2296 * missing" and this will turn on fuzzy operand size
2297 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002298 */
2299 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002300 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002301 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002302 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002303 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002304 opflags_t template_opsize, insn_opsize;
2305
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002306 if (!(type & SIZE_MASK))
2307 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002308
Jin Kyu Song7903c072013-10-30 03:00:12 -07002309 insn_opsize = type & SIZE_MASK;
2310 if (!is_broadcast) {
2311 template_opsize = itemp->opd[i] & SIZE_MASK;
2312 } else {
2313 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2314 /*
2315 * when broadcasting, the element size depends on
2316 * the instruction type. decorator flag should match.
2317 */
2318
2319 if (deco_brsize) {
2320 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002321 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002322 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002323 } else {
2324 template_opsize = 0;
2325 }
2326 }
2327
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002328 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002329 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002330 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002331 } else if (template_opsize) {
2332 if (template_opsize != insn_opsize) {
2333 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002334 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002335 } else if (!is_class(REGISTER, type)) {
2336 /*
2337 * Note: we don't honor extrinsic operand sizes for registers,
2338 * so "missing operand size" for a register should be
2339 * considered a wildcard match rather than an error.
2340 */
2341 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002342 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002343 } else if (is_broadcast &&
2344 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002345 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002346 /*
2347 * broadcasting opsize matches but the number of repeated memory
2348 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002349 * if 64b double precision float is broadcasted to ymm (256b),
2350 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002351 */
2352 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002353 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002354 }
2355 }
2356
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002357 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002358 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002359
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002360 /*
2361 * Check operand sizes
2362 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002363 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2364 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002366 asize = itemp->opd[i] & SIZE_MASK;
2367 if (asize) {
2368 for (i = 0; i < oprs; i++)
2369 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 break;
2371 }
2372 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002373 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002375 }
2376
Keith Kaniosb7a89542007-04-12 02:40:54 +00002377 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 if (!(itemp->opd[i] & SIZE_MASK) &&
2379 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002380 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002381 }
2382
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002383 /*
2384 * Check template is okay at the set cpu level
2385 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002386 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002387 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002388
Keith Kaniosb7a89542007-04-12 02:40:54 +00002389 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002390 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002391 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002392 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002393 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002395 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002396 * If we have a HLE prefix, look for the NOHLE flag
2397 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002398 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002399 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2400 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2401 return MERR_BADHLE;
2402
2403 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002404 * Check if special handling needed for Jumps
2405 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002406 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002407 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408
Jin Kyu Song03041092013-10-15 19:38:51 -07002409 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002410 * Check if BND prefix is allowed.
2411 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002412 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002413 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002414 (has_prefix(instruction, PPS_REP, P_BND) ||
2415 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002416 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002417 else if (itemp_has(itemp, IF_BND) &&
2418 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2419 has_prefix(instruction, PPS_REP, P_REPNZ)))
2420 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002421
H. Peter Anvin60926242009-07-26 16:25:38 -07002422 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002423}
2424
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002425/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002426 * Check if ModR/M.mod should/can be 01.
2427 * - EAF_BYTEOFFS is set
2428 * - offset can fit in a byte when EVEX is not used
2429 * - offset can be compressed when EVEX is used
2430 */
2431#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2432 (o >= -128 && o <= 127 && \
2433 seg == NO_SEG && !forw_ref && \
2434 !(input->eaflags & EAF_WORDOFFS) && \
2435 !(ins->rex & REX_EV)) || \
2436 (ins->rex & REX_EV && \
2437 is_disp8n(input, ins, &output->disp8)))
2438
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002439static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002440 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002441{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002442 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002443 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002444 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002445
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002446 output->type = EA_SCALAR;
2447 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002448 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002449
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002450 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 /* EVEX.R' flag for the REG operand */
2453 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002454
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002455 if (is_class(REGISTER, input->type)) {
2456 /*
2457 * It's a direct register.
2458 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002459 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002460 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002461
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002462 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002463 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002464
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 /* broadcasting is not available with a direct register operand. */
2466 if (input->decoflags & BRDCAST_MASK) {
2467 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2468 goto err;
2469 }
2470
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002471 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002472 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002474 output->bytes = 0; /* no offset necessary either */
2475 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2476 } else {
2477 /*
2478 * It's a memory reference.
2479 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002480
2481 /* Embedded rounding or SAE is not available with a mem ref operand. */
2482 if (input->decoflags & (ER | SAE)) {
2483 nasm_error(ERR_NONFATAL,
2484 "Embedded rounding is available only with reg-reg op.");
2485 return -1;
2486 }
2487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 if (input->basereg == -1 &&
2489 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002490 /*
2491 * It's a pure offset.
2492 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002493 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2494 input->segment == NO_SEG) {
2495 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2496 input->type &= ~IP_REL;
2497 input->type |= MEMORY;
2498 }
2499
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002500 if (bits == 64 &&
2501 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2502 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2503 return -1;
2504 }
2505
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002506 if (eaflags & EAF_BYTEOFFS ||
2507 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002508 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2509 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2510 }
2511
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002512 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002513 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002514 output->sib = GEN_SIB(0, 4, 5);
2515 output->bytes = 4;
2516 output->modrm = GEN_MODRM(0, rfield, 4);
2517 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002518 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002520 output->bytes = (addrbits != 16 ? 4 : 2);
2521 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2522 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002523 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002524 } else {
2525 /*
2526 * It's an indirection.
2527 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002529 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002531 int t, it, bt; /* register numbers */
2532 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 if (s == 0)
2535 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002536
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002537 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002538 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002539 ix = nasm_reg_flags[i];
2540 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002541 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 ix = 0;
2543 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002544
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002546 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 bx = nasm_reg_flags[b];
2548 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002549 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002550 bx = 0;
2551 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002552
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002553 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002554 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002555 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002556 int32_t o = input->offset;
2557 int mod, scale, index, base;
2558
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 /*
2560 * For a vector SIB, one has to be a vector and the other,
2561 * if present, a GPR. The vector must be the index operand.
2562 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002563 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 if (s == 0)
2565 s = 1;
2566 else if (s != 1)
2567 goto err;
2568
2569 t = bt, bt = it, it = t;
2570 x = bx, bx = ix, ix = x;
2571 }
2572
2573 if (bt != -1) {
2574 if (REG_GPR & ~bx)
2575 goto err;
2576 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2577 sok &= bx;
2578 else
2579 goto err;
2580 }
2581
2582 /*
2583 * While we're here, ensure the user didn't specify
2584 * WORD or QWORD
2585 */
2586 if (input->disp_size == 16 || input->disp_size == 64)
2587 goto err;
2588
2589 if (addrbits == 16 ||
2590 (addrbits == 32 && !(sok & BITS32)) ||
2591 (addrbits == 64 && !(sok & BITS64)))
2592 goto err;
2593
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002594 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2595 : ((ix & YMMREG & ~REG_EA)
2596 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002597
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002598 output->rex |= rexflags(it, ix, REX_X);
2599 output->rex |= rexflags(bt, bx, REX_B);
2600 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601
2602 index = it & 7; /* it is known to be != -1 */
2603
2604 switch (s) {
2605 case 1:
2606 scale = 0;
2607 break;
2608 case 2:
2609 scale = 1;
2610 break;
2611 case 4:
2612 scale = 2;
2613 break;
2614 case 8:
2615 scale = 3;
2616 break;
2617 default: /* then what the smeg is it? */
2618 goto err; /* panic */
2619 }
2620
2621 if (bt == -1) {
2622 base = 5;
2623 mod = 0;
2624 } else {
2625 base = (bt & 7);
2626 if (base != REG_NUM_EBP && o == 0 &&
2627 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002628 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002629 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002630 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002631 mod = 1;
2632 else
2633 mod = 2;
2634 }
2635
2636 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002637 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2638 output->modrm = GEN_MODRM(mod, rfield, 4);
2639 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002640 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002641 /*
2642 * it must be a 32/64-bit memory reference. Firstly we have
2643 * to check that all registers involved are type E/Rxx.
2644 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002645 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002646 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002647
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002648 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002649 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2650 sok &= ix;
2651 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002654
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 if (bt != -1) {
2656 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002657 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002660 sok &= bx;
2661 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002662
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 /*
2664 * While we're here, ensure the user didn't specify
2665 * WORD or QWORD
2666 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002667 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 if (addrbits == 16 ||
2671 (addrbits == 32 && !(sok & BITS32)) ||
2672 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002673 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002674
Keith Kaniosb7a89542007-04-12 02:40:54 +00002675 /* now reorganize base/index */
2676 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002677 ((hb == b && ht == EAH_NOTBASE) ||
2678 (hb == i && ht == EAH_MAKEBASE))) {
2679 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002680 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002681 x = bx, bx = ix, ix = x;
2682 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002683
Jin Kyu Song164d6072013-10-15 19:10:13 -07002684 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002685 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002686 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002688 if (eaflags & EAF_MIB) {
2689 /* only for mib operands */
2690 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2691 /*
2692 * make a single reg index [reg*1].
2693 * gas uses this form for an explicit index register.
2694 */
2695 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2696 }
2697 if ((ht == EAH_SUMMED) && bt == -1) {
2698 /* separate once summed index into [base, index] */
2699 bt = it, bx = ix, s--;
2700 }
2701 } else {
2702 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002703 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002704 s == 3 || s == 5 || s == 9) && bt == -1) {
2705 /* convert 3*EAX to EAX+2*EAX */
2706 bt = it, bx = ix, s--;
2707 }
2708 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002709 (eaflags & EAF_TIMESTWO) &&
2710 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002711 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002712 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002713 * to sib format with 0x0 displacement - [EAX*1+0].
2714 */
2715 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2716 }
2717 }
Keith Kanios48af1772007-08-17 07:37:52 +00002718 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002719 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002720 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002721 x = ix, ix = bx, bx = x;
2722 }
2723 if (it == REG_NUM_ESP ||
2724 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002725 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002726
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002727 output->rex |= rexflags(it, ix, REX_X);
2728 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002729
Keith Kanios48af1772007-08-17 07:37:52 +00002730 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002733
Keith Kaniosb7a89542007-04-12 02:40:54 +00002734 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 } else {
2738 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002739 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002740 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002741 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002742 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002743 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744 mod = 1;
2745 else
2746 mod = 2;
2747 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002748
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002749 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002750 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2751 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002752 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002753 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002755
Keith Kaniosb7a89542007-04-12 02:40:54 +00002756 if (it == -1)
2757 index = 4, s = 1;
2758 else
2759 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002760
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 switch (s) {
2762 case 1:
2763 scale = 0;
2764 break;
2765 case 2:
2766 scale = 1;
2767 break;
2768 case 4:
2769 scale = 2;
2770 break;
2771 case 8:
2772 scale = 3;
2773 break;
2774 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002775 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002777
Keith Kaniosb7a89542007-04-12 02:40:54 +00002778 if (bt == -1) {
2779 base = 5;
2780 mod = 0;
2781 } else {
2782 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002783 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002784 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002785 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002786 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002787 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 mod = 1;
2789 else
2790 mod = 2;
2791 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002792
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002793 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002794 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2795 output->modrm = GEN_MODRM(mod, rfield, 4);
2796 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 }
2798 } else { /* it's 16-bit */
2799 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002800 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002801
Keith Kaniosb7a89542007-04-12 02:40:54 +00002802 /* check for 64-bit long mode */
2803 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002804 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002805
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002807 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2808 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810
Keith Kaniosb7a89542007-04-12 02:40:54 +00002811 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002812 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002813 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002816 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 if (b == -1 && i != -1) {
2818 int tmp = b;
2819 b = i;
2820 i = tmp;
2821 } /* swap */
2822 if ((b == R_SI || b == R_DI) && i != -1) {
2823 int tmp = b;
2824 b = i;
2825 i = tmp;
2826 }
2827 /* have BX/BP as base, SI/DI index */
2828 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002829 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 if (i != -1 && b != -1 &&
2831 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002832 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002833 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 rm = -1;
2837 if (i != -1)
2838 switch (i * 256 + b) {
2839 case R_SI * 256 + R_BX:
2840 rm = 0;
2841 break;
2842 case R_DI * 256 + R_BX:
2843 rm = 1;
2844 break;
2845 case R_SI * 256 + R_BP:
2846 rm = 2;
2847 break;
2848 case R_DI * 256 + R_BP:
2849 rm = 3;
2850 break;
2851 } else
2852 switch (b) {
2853 case R_SI:
2854 rm = 4;
2855 break;
2856 case R_DI:
2857 rm = 5;
2858 break;
2859 case R_BP:
2860 rm = 6;
2861 break;
2862 case R_BX:
2863 rm = 7;
2864 break;
2865 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002866 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002867 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002868
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002869 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002870 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002872 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 mod = 1;
2874 else
2875 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002876
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002877 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002878 output->bytes = mod; /* bytes of offset needed */
2879 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 }
2881 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002882 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002883
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002885 return output->type;
2886
2887err:
2888 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002889}
2890
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002892{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002893 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002894 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002895
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002896 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002897
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002898 switch (ins->prefixes[PPS_ASIZE]) {
2899 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002900 valid &= 16;
2901 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002902 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 valid &= 32;
2904 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002905 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002906 valid &= 64;
2907 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 valid &= (addrbits == 32) ? 16 : 32;
2910 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002911 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002913 }
2914
2915 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 if (is_class(MEMORY, ins->oprs[j].type)) {
2917 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002918
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002920 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 i = 0;
2922 else
2923 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002926 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 b = 0;
2928 else
2929 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002931 if (ins->oprs[j].scale == 0)
2932 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 if (!i && !b) {
2935 int ds = ins->oprs[j].disp_size;
2936 if ((addrbits != 64 && ds > 8) ||
2937 (addrbits == 64 && ds == 16))
2938 valid &= ds;
2939 } else {
2940 if (!(REG16 & ~b))
2941 valid &= 16;
2942 if (!(REG32 & ~b))
2943 valid &= 32;
2944 if (!(REG64 & ~b))
2945 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002946
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002947 if (!(REG16 & ~i))
2948 valid &= 16;
2949 if (!(REG32 & ~i))
2950 valid &= 32;
2951 if (!(REG64 & ~i))
2952 valid &= 64;
2953 }
2954 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002955 }
2956
2957 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002958 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002959 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002960 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002961 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002963 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002965 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002967 }
2968
2969 defdisp = ins->addr_size == 16 ? 16 : 32;
2970
2971 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002972 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2973 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2974 /*
2975 * mem_offs sizes must match the address size; if not,
2976 * strip the MEM_OFFS bit and match only EA instructions
2977 */
2978 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2979 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002980 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002981}