blob: 67619b3b5bc31d697d31ce44d8c2ab3bf313a387 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin164d2462017-02-20 02:39:56 -08003 * Copyright 1996-2017 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:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * 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>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
184#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800186#include "error.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,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700201 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700202 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700203 MERR_MASKNOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700204 MERR_BADCPU,
205 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800206 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700207 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700208 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800209 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700248
H. Peter Anvin164d2462017-02-20 02:39:56 -0800249static inline bool absolute_op(const struct operand *o)
250{
251 return o->segment == NO_SEG && o->wrt == NO_SEG &&
252 !(o->opflags & OPFLAG_RELATIVE);
253}
254
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400255static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000256{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700257 return ins->prefixes[pos] == prefix;
258}
259
260static void assert_no_prefix(insn * ins, enum prefix_pos pos)
261{
262 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800263 nasm_error(ERR_NONFATAL, "invalid %s prefix",
264 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265}
266
267static const char *size_name(int size)
268{
269 switch (size) {
270 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700282 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700284 case 64:
285 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700286 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000288 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289}
290
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800293 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400294 "%s data exceeds bounds", size_name(size));
295}
296
297static void warn_overflow_const(int64_t data, int size)
298{
299 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800300 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301}
302
303static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700304{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800305 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400306 if (overflow_general(o->offset, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800307 warn_overflow(size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700308 }
309}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400310
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800311static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
312{
313 bool err;
314
315 switch (sign) {
316 case OUT_WRAP:
317 err = overflow_general(data, size);
318 break;
319 case OUT_SIGNED:
320 err = overflow_signed(data, size);
321 break;
322 case OUT_UNSIGNED:
323 err = overflow_unsigned(data, size);
324 break;
325 default:
326 panic();
327 break;
328 }
329
330 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800331 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800332}
333
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334/*
335 * This routine wrappers the real output format's output routine,
336 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800337 * generator at the same time, flatten unnecessary relocations,
338 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000339 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700340static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000341{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000342 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700343 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700344 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800345 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 union {
347 uint8_t b[8];
348 uint64_t q;
349 } xdata;
350 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800351 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800352 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000353
H. Peter Anvina77692b2016-09-20 14:04:33 -0700354 if (!data->size)
355 return; /* Nothing to do */
356
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700357 /*
358 * Convert addresses to RAWDATA if possible
359 * XXX: not all backends want this for global symbols!!!!
360 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700361 switch (data->type) {
362 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800363 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800364 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800365 goto address;
366
367 case OUT_RELADDR:
368 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800369 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800370 goto address;
371
372 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700373 asize = data->size;
374 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800375 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700376 uint8_t *q = xdata.b;
377
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800378 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700379
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800380 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700381 data->data = xdata.b;
382 data->type = OUT_RAWDATA;
383 asize = 0; /* No longer an address */
384 }
385 break;
386
387 default:
388 asize = 0; /* Not an address */
389 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000390 }
391
H. Peter Anvina77692b2016-09-20 14:04:33 -0700392 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800393
Frank Kotlerabebb082003-09-06 04:45:37 +0000394 /*
395 * this call to src_get determines when we call the
396 * debug-format-specific "linenum" function
397 * it updates lineno and lnfname to the current values
398 * returning 0 if "same as last time", -2 if lnfname
399 * changed, and the amount by which lineno changed,
400 * if it did. thus, these variables must be static
401 */
402
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400403 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700404 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000405
H. Peter Anvinb6412502016-02-11 21:07:40 -0800406 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700407 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800409 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800411 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800412 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800413 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800414 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700415 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700416 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700417 data->insoffs += amax;
418 data->offset += amax;
419 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800420 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700421 data->data = zero_buffer;
422 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800423 }
424
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700425 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700426 data->offset += size;
427 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000428}
429
H. Peter Anvina77692b2016-09-20 14:04:33 -0700430static inline void out_rawdata(struct out_data *data, const void *rawdata,
431 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400432{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700433 data->type = OUT_RAWDATA;
434 data->data = rawdata;
435 data->size = size;
436 out(data);
437}
438
439static void out_rawbyte(struct out_data *data, uint8_t byte)
440{
441 data->type = OUT_RAWDATA;
442 data->data = &byte;
443 data->size = 1;
444 out(data);
445}
446
447static inline void out_reserve(struct out_data *data, uint64_t size)
448{
449 data->type = OUT_RESERVE;
450 data->size = size;
451 out(data);
452}
453
H. Peter Anvin164d2462017-02-20 02:39:56 -0800454static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700455 int size, enum out_sign sign)
456{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800457 data->type =
458 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700459 data->sign = sign;
460 data->size = size;
461 data->toffset = opx->offset;
462 data->tsegment = opx->segment;
463 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800464 /*
465 * XXX: improve this if at some point in the future we can
466 * distinguish the subtrahend in expressions like [foo - bar]
467 * where bar is a symbol in the current segment. However, at the
468 * current point, if OPFLAG_RELATIVE is set that subtraction has
469 * already occurred.
470 */
471 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700472 out(data);
473}
474
H. Peter Anvin164d2462017-02-20 02:39:56 -0800475static void out_reladdr(struct out_data *data, const struct operand *opx,
476 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700477{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800478 if (opx->opflags & OPFLAG_RELATIVE)
479 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
480
H. Peter Anvina77692b2016-09-20 14:04:33 -0700481 data->type = OUT_RELADDR;
482 data->sign = OUT_SIGNED;
483 data->size = size;
484 data->toffset = opx->offset;
485 data->tsegment = opx->segment;
486 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800487 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700488 out(data);
489}
490
H. Peter Anvin164d2462017-02-20 02:39:56 -0800491static inline void out_segment(struct out_data *data,
492 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700493{
494 data->type = OUT_SEGMENT;
495 data->sign = OUT_UNSIGNED;
496 data->size = 2;
497 data->toffset = opx->offset;
498 data->tsegment = ofmt->segbase(opx->segment + 1);
499 data->twrt = opx->wrt;
500 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400501}
502
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700503static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800504 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800506 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800507 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000508 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800509 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000510
H. Peter Anvin755f5212012-02-25 11:41:34 -0800511 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700512 return false;
513 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400514 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700515 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400516 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700517
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800518 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100519
Victor van den Elzen154e5922009-02-25 17:32:00 +0100520 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100521 /* Be optimistic in pass 1 */
522 return true;
523
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700525 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000526
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700527 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800528 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
529
530 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
531 /* jmp short (opcode eb) cannot be used with bnd prefix. */
532 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800533 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800534 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800535 }
536
537 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000539
H. Peter Anvin04445362016-09-21 15:56:19 -0700540/* This is totally just a wild guess what is reasonable... */
541#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
542
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800543int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700545 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000546 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700547 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000548 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300549 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000550
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700551 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700552 data.offset = start;
553 data.segment = segment;
554 data.itemp = NULL;
555 data.sign = OUT_WRAP;
556 data.bits = bits;
557
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300558 wsize = idata_bytes(instruction->opcode);
559 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000561
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000564 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800566 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400569 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400571 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800572 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400573 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000574 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700575 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700576 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700577 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700578 data.inslen = data.size = wsize;
579 data.toffset = e->offset;
580 data.tsegment = e->segment;
581 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800582 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700583 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400584 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700585 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400586 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700587 int align = e->stringlen % wsize;
588 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700590
591 data.insoffs = 0;
592 data.inslen = e->stringlen + align;
593
594 out_rawdata(&data, e->stringval, e->stringlen);
595 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 }
597 }
598 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700599 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800600 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 }
602 }
603 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800604 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700605 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700606 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700608 size_t t = instruction->times;
609 off_t base = 0;
610 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700611 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700612 char *buf = NULL;
613 size_t blk = 0; /* Buffered I/O block size */
614 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615
H. Peter Anvind81a2352016-09-21 14:03:18 -0700616 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400617 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800618 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700620 goto done;
621 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622
H. Peter Anvind81a2352016-09-21 14:03:18 -0700623 len = nasm_file_size(fp);
624
625 if (len == (off_t)-1) {
626 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700627 fname);
628 goto close_done;
629 }
630
H. Peter Anvina77692b2016-09-20 14:04:33 -0700631 if (instruction->eops->next) {
632 base = instruction->eops->next->offset;
633 if (base >= len) {
634 len = 0;
635 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 len -= base;
637 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700638 len > (off_t)instruction->eops->next->next->offset)
639 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700642
H. Peter Anvina77692b2016-09-20 14:04:33 -0700643 lfmt->set_offset(data.offset);
644 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000645
H. Peter Anvind81a2352016-09-21 14:03:18 -0700646 if (!len)
647 goto end_incbin;
648
649 /* Try to map file data */
650 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700651 if (!map) {
652 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
653 buf = nasm_malloc(blk);
654 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700655
656 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700657 /*
658 * Consider these irrelevant for INCBIN, since it is fully
659 * possible that these might be (way) bigger than an int
660 * can hold; there is, however, no reason to widen these
661 * types just for INCBIN. data.inslen == 0 signals to the
662 * backend that these fields are meaningless, if at all
663 * needed.
664 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700665 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700666 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700667
H. Peter Anvind81a2352016-09-21 14:03:18 -0700668 if (map) {
669 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700670 } else if ((off_t)m == len) {
671 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700672 } else {
673 off_t l = len;
674
675 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700676 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700677 "`incbin': unable to seek on file `%s'",
678 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700679 goto end_incbin;
680 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700681 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700682 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683 if (!m || feof(fp)) {
684 /*
685 * This shouldn't happen unless the file
686 * actually changes while we are reading
687 * it.
688 */
689 nasm_error(ERR_NONFATAL,
690 "`incbin': unexpected EOF while"
691 " reading file `%s'", fname);
692 goto end_incbin;
693 }
694 out_rawdata(&data, buf, m);
695 l -= m;
696 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700697 }
698 }
699 end_incbin:
700 lfmt->downlevel(LIST_INCBIN);
701 if (instruction->times > 1) {
702 lfmt->set_offset(data.offset);
703 lfmt->uplevel(LIST_TIMES);
704 lfmt->downlevel(LIST_TIMES);
705 }
706 if (ferror(fp)) {
707 nasm_error(ERR_NONFATAL,
708 "`incbin': error while"
709 " reading file `%s'", fname);
710 }
711 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700712 if (buf)
713 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700714 if (map)
715 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700716 fclose(fp);
717 done:
718 ;
719 } else {
720 /* "Real" instruction */
721
722 /* Check to see if we need an address-size prefix */
723 add_asp(instruction, bits);
724
725 m = find_match(&temp, instruction, data.segment, data.offset, bits);
726
727 if (m == MOK_GOOD) {
728 /* Matches! */
729 int64_t insn_size = calcsize(data.segment, data.offset,
730 bits, instruction, temp);
731 itimes = instruction->times;
732 if (insn_size < 0) /* shouldn't be, on pass two */
733 nasm_panic(0, "errors made it through from pass one");
734
735 data.itemp = temp;
736 data.bits = bits;
737
738 while (itimes--) {
739 data.insoffs = 0;
740 data.inslen = insn_size;
741
742 gencode(&data, instruction);
743 nasm_assert(data.insoffs == insn_size);
744
745 if (itimes > 0 && itimes == instruction->times - 1) {
746 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800747 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400748 }
749 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700750 if (instruction->times > 1)
751 lfmt->downlevel(LIST_TIMES);
752 } else {
753 /* No match */
754 switch (m) {
755 case MERR_OPSIZEMISSING:
756 nasm_error(ERR_NONFATAL, "operation size not specified");
757 break;
758 case MERR_OPSIZEMISMATCH:
759 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
760 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700761 case MERR_BRNOTHERE:
762 nasm_error(ERR_NONFATAL,
763 "broadcast not permitted on this operand");
764 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700765 case MERR_BRNUMMISMATCH:
766 nasm_error(ERR_NONFATAL,
767 "mismatch in the number of broadcasting elements");
768 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700769 case MERR_MASKNOTHERE:
770 nasm_error(ERR_NONFATAL,
771 "mask not permitted on this operand");
772 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700773 case MERR_BADCPU:
774 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
775 break;
776 case MERR_BADMODE:
777 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
778 bits);
779 break;
780 case MERR_ENCMISMATCH:
781 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
782 break;
783 case MERR_BADBND:
784 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
785 break;
786 case MERR_BADREPNE:
787 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
788 (has_prefix(instruction, PPS_REP, P_REPNE) ?
789 "repne" : "repnz"));
790 break;
791 default:
792 nasm_error(ERR_NONFATAL,
793 "invalid combination of opcode and operands");
794 break;
795 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400796 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000797 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700798 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799}
800
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800801int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000802{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000803 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700804 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400806 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700809 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
810 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400811 instruction->opcode == I_DT || instruction->opcode == I_DO ||
812 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300814 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300817 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400819 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000820 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400823 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400825 warn_overflow_const(e->offset, wsize);
826 } else if (e->type == EOT_DB_STRING ||
827 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000829
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 align = (-osize) % wsize;
831 if (align < 0)
832 align += wsize;
833 isize += osize + align;
834 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700835 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000836 }
837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400839 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700840 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000841
H. Peter Anvind81a2352016-09-21 14:03:18 -0700842 len = nasm_file_size_by_path(fname);
843 if (len == (off_t)-1) {
844 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
845 fname);
846 return 0;
847 }
848
849 if (instruction->eops->next) {
850 if (len <= (off_t)instruction->eops->next->offset) {
851 len = 0;
852 } else {
853 len -= instruction->eops->next->offset;
854 if (instruction->eops->next->next &&
855 len > (off_t)instruction->eops->next->next->offset) {
856 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 }
858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700860
861 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 }
863
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700864 /* Check to see if we need an address-size prefix */
865 add_asp(instruction, bits);
866
H. Peter Anvin23595f52009-07-25 17:44:25 -0700867 m = find_match(&temp, instruction, segment, offset, bits);
868 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400869 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700870 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700871 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400872 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000873 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000874}
875
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800876static void bad_hle_warn(const insn * ins, uint8_t hleok)
877{
878 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800879 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800880 static const enum whatwarn warn[2][4] =
881 {
882 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
883 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
884 };
885 unsigned int n;
886
887 n = (unsigned int)rep_pfx - P_XACQUIRE;
888 if (n > 1)
889 return; /* Not XACQUIRE/XRELEASE */
890
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800891 ww = warn[n][hleok];
892 if (!is_class(MEMORY, ins->oprs[0].type))
893 ww = w_inval; /* HLE requires operand 0 to be memory */
894
895 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800896 case w_none:
897 break;
898
899 case w_lock:
900 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800901 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800902 "%s with this instruction requires lock",
903 prefix_name(rep_pfx));
904 }
905 break;
906
907 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800908 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800909 "%s invalid with this instruction",
910 prefix_name(rep_pfx));
911 break;
912 }
913}
914
H. Peter Anvin507ae032008-10-09 15:37:10 -0700915/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400916#define case3(x) case (x): case (x)+1: case (x)+2
917#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700918
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800919static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800920 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000921{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800922 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800923 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000924 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000925 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700926 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700927 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700928 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700929 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800930 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800931 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700932 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700934 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700935 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700936 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700937
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700938 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 (void)segment; /* Don't warn that this parameter is unused */
942 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000943
H. Peter Anvin839eca22007-10-29 23:12:47 -0700944 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400945 c = *codes++;
946 op1 = (c & 3) + ((opex & 1) << 2);
947 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
948 opx = &ins->oprs[op1];
949 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700950
H. Peter Anvin839eca22007-10-29 23:12:47 -0700951 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400952 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 codes += c, length += c;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400956 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 opex = c;
958 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 case4(010):
961 ins->rex |=
962 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 codes++, length++;
964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Jin Kyu Song164d6072013-10-15 19:10:13 -0700966 case4(014):
967 /* this is an index reg of MIB operand */
968 mib_index = opx->basereg;
969 break;
970
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 case4(020):
972 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 length++;
974 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 length += 2;
978 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400980 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700981 if (opx->type & (BITS16 | BITS32 | BITS64))
982 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 else
984 length += (bits == 16) ? 2 : 4;
985 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 length += 4;
989 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700990
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400991 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700992 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 length++;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001000 length += 8; /* MOV reg64/imm */
1001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 length += 2;
1005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001008 if (opx->type & (BITS16 | BITS32 | BITS64))
1009 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 else
1011 length += (bits == 16) ? 2 : 4;
1012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 length += 4;
1016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001019 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 case 0172:
1023 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001024 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001025 length++;
1026 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001027
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001028 case4(0174):
1029 length++;
1030 break;
1031
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001032 case4(0240):
1033 ins->rex |= REX_EV;
1034 ins->vexreg = regval(opx);
1035 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1036 ins->vex_cm = *codes++;
1037 ins->vex_wlp = *codes++;
1038 ins->evex_tuple = (*codes++ - 0300);
1039 break;
1040
1041 case 0250:
1042 ins->rex |= REX_EV;
1043 ins->vexreg = 0;
1044 ins->vex_cm = *codes++;
1045 ins->vex_wlp = *codes++;
1046 ins->evex_tuple = (*codes++ - 0300);
1047 break;
1048
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 case4(0254):
1050 length += 4;
1051 break;
1052
1053 case4(0260):
1054 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001055 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001056 ins->vex_cm = *codes++;
1057 ins->vex_wlp = *codes++;
1058 break;
1059
1060 case 0270:
1061 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001062 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001063 ins->vex_cm = *codes++;
1064 ins->vex_wlp = *codes++;
1065 break;
1066
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001067 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001068 hleok = c & 3;
1069 break;
1070
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001071 case4(0274):
1072 length++;
1073 break;
1074
1075 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 if (bits == 64)
1080 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001081 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
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 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001085 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001087
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
Keith Kaniosb7a89542007-04-12 02:40:54 +00001091 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1093 has_prefix(ins, PPS_ASIZE, P_A32))
1094 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 case4(0314):
1098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001101 {
1102 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1103 if (pfx == P_O16)
1104 break;
1105 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001106 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001107 else
1108 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001110 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001113 {
1114 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1115 if (pfx == P_O32)
1116 break;
1117 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001118 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001119 else
1120 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001122 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 case 0322:
1125 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001126
Keith Kaniosb7a89542007-04-12 02:40:54 +00001127 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001128 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
Keith Kaniosb7a89542007-04-12 02:40:54 +00001131 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 case 0325:
1136 ins->rex |= REX_NH;
1137 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001138
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001139 case 0326:
1140 break;
1141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 case 0330:
1143 codes++, length++;
1144 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001149 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 case 0333:
1151 length++;
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 case 0334:
1155 ins->rex |= REX_L;
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001158 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001162 if (!ins->prefixes[PPS_REP])
1163 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001167 if (!ins->prefixes[PPS_REP])
1168 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001172 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001173 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001175 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1176 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001177 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001179 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001181
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 case 0341:
1183 if (!ins->prefixes[PPS_WAIT])
1184 ins->prefixes[PPS_WAIT] = P_WAIT;
1185 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001186
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001187 case 0360:
1188 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001189
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001190 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 length++;
1192 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001193
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001194 case 0364:
1195 case 0365:
1196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
Keith Kanios48af1772007-08-17 07:37:52 +00001198 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001199 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 length++;
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001203 case 0370:
1204 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001206
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 case 0373:
1208 length++;
1209 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001211 case 0374:
1212 eat = EA_XMMVSIB;
1213 break;
1214
1215 case 0375:
1216 eat = EA_YMMVSIB;
1217 break;
1218
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001219 case 0376:
1220 eat = EA_ZMMVSIB;
1221 break;
1222
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001223 case4(0100):
1224 case4(0110):
1225 case4(0120):
1226 case4(0130):
1227 case4(0200):
1228 case4(0204):
1229 case4(0210):
1230 case4(0214):
1231 case4(0220):
1232 case4(0224):
1233 case4(0230):
1234 case4(0234):
1235 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001237 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 opflags_t rflags;
1239 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001240 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001241
Keith Kaniosb7a89542007-04-12 02:40:54 +00001242 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001243
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001244 if (c <= 0177) {
1245 /* pick rfield from operand b (opx) */
1246 rflags = regflag(opx);
1247 rfield = nasm_regvals[opx->basereg];
1248 } else {
1249 rflags = 0;
1250 rfield = c & 7;
1251 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001252
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001253 /* EVEX.b1 : evex_brerop contains the operand position */
1254 op_er_sae = (ins->evex_brerop >= 0 ?
1255 &ins->oprs[ins->evex_brerop] : NULL);
1256
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001257 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1258 /* set EVEX.b */
1259 ins->evex_p[2] |= EVEX_P2B;
1260 if (op_er_sae->decoflags & ER) {
1261 /* set EVEX.RC (rounding control) */
1262 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1263 & EVEX_P2RC;
1264 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001265 } else {
1266 /* set EVEX.L'L (vector length) */
1267 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001268 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001269 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001270 /* set EVEX.b */
1271 ins->evex_p[2] |= EVEX_P2B;
1272 }
1273 }
1274
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001275 if (itemp_has(temp, IF_MIB)) {
1276 opy->eaflags |= EAF_MIB;
1277 /*
1278 * if a separate form of MIB (ICC style) is used,
1279 * the index reg info is merged into mem operand
1280 */
1281 if (mib_index != R_none) {
1282 opy->indexreg = mib_index;
1283 opy->scale = 1;
1284 opy->hintbase = mib_index;
1285 opy->hinttype = EAH_NOTBASE;
1286 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001287 }
1288
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001289 if (process_ea(opy, &ea_data, bits,
1290 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001291 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001293 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001296 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 }
1298 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001299
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001301 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 ": instruction code \\%o (0x%02X) given", c, c);
1303 break;
1304 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001305 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001306
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001307 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001308
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001309 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001311 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 return -1;
1313 }
1314 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001315 }
1316
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001317 switch (ins->prefixes[PPS_VEX]) {
1318 case P_EVEX:
1319 if (!(ins->rex & REX_EV))
1320 return -1;
1321 break;
1322 case P_VEX3:
1323 case P_VEX2:
1324 if (!(ins->rex & REX_V))
1325 return -1;
1326 break;
1327 default:
1328 break;
1329 }
1330
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001331 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001332 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001333
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001335 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 return -1;
1337 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001338 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001340 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 ins->rex &= ~REX_W;
1342 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001343 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 ins->rex |= REX_W;
1345 bad32 &= ~REX_W;
1346 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001347 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 /* Follow REX_W */
1349 break;
1350 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001351
H. Peter Anvinfc561202011-07-07 16:58:22 -07001352 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001353 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001355 } else if (!(ins->rex & REX_EV) &&
1356 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001357 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001358 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001359 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001360 if (ins->rex & REX_EV)
1361 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001362 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1363 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 length += 3;
1365 else
1366 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001367 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001369 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 return -1;
1371 } else if (bits == 64) {
1372 length++;
1373 } else if ((ins->rex & REX_L) &&
1374 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001375 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001377 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001378 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 length++;
1380 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001381 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 return -1;
1383 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001384 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001385
1386 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001387 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001388 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001389 "instruction is not lockable");
1390 }
1391
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001392 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001393
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001394 /*
1395 * when BND prefix is set by DEFAULT directive,
1396 * BND prefix is added to every appropriate instruction line
1397 * unless it is overridden by NOBND prefix.
1398 */
1399 if (globalbnd &&
1400 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1401 ins->prefixes[PPS_REP] = P_BND;
1402
H. Peter Anvina77692b2016-09-20 14:04:33 -07001403 /*
1404 * Add length of legacy prefixes
1405 */
1406 length += emit_prefix(NULL, bits, ins);
1407
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001408 return length;
1409}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001410
H. Peter Anvina77692b2016-09-20 14:04:33 -07001411static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001412{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001413 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001414 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001415 !(ins->rex & (REX_V | REX_EV)) &&
1416 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001417 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1418 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001419 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001420 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001421 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001422}
1423
H. Peter Anvina77692b2016-09-20 14:04:33 -07001424static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1425{
1426 int bytes = 0;
1427 int j;
1428
1429 for (j = 0; j < MAXPREFIX; j++) {
1430 uint8_t c = 0;
1431 switch (ins->prefixes[j]) {
1432 case P_WAIT:
1433 c = 0x9B;
1434 break;
1435 case P_LOCK:
1436 c = 0xF0;
1437 break;
1438 case P_REPNE:
1439 case P_REPNZ:
1440 case P_XACQUIRE:
1441 case P_BND:
1442 c = 0xF2;
1443 break;
1444 case P_REPE:
1445 case P_REPZ:
1446 case P_REP:
1447 case P_XRELEASE:
1448 c = 0xF3;
1449 break;
1450 case R_CS:
1451 if (bits == 64) {
1452 nasm_error(ERR_WARNING | ERR_PASS2,
1453 "cs segment base generated, but will be ignored in 64-bit mode");
1454 }
1455 c = 0x2E;
1456 break;
1457 case R_DS:
1458 if (bits == 64) {
1459 nasm_error(ERR_WARNING | ERR_PASS2,
1460 "ds segment base generated, but will be ignored in 64-bit mode");
1461 }
1462 c = 0x3E;
1463 break;
1464 case R_ES:
1465 if (bits == 64) {
1466 nasm_error(ERR_WARNING | ERR_PASS2,
1467 "es segment base generated, but will be ignored in 64-bit mode");
1468 }
1469 c = 0x26;
1470 break;
1471 case R_FS:
1472 c = 0x64;
1473 break;
1474 case R_GS:
1475 c = 0x65;
1476 break;
1477 case R_SS:
1478 if (bits == 64) {
1479 nasm_error(ERR_WARNING | ERR_PASS2,
1480 "ss segment base generated, but will be ignored in 64-bit mode");
1481 }
1482 c = 0x36;
1483 break;
1484 case R_SEGR6:
1485 case R_SEGR7:
1486 nasm_error(ERR_NONFATAL,
1487 "segr6 and segr7 cannot be used as prefixes");
1488 break;
1489 case P_A16:
1490 if (bits == 64) {
1491 nasm_error(ERR_NONFATAL,
1492 "16-bit addressing is not supported "
1493 "in 64-bit mode");
1494 } else if (bits != 16)
1495 c = 0x67;
1496 break;
1497 case P_A32:
1498 if (bits != 32)
1499 c = 0x67;
1500 break;
1501 case P_A64:
1502 if (bits != 64) {
1503 nasm_error(ERR_NONFATAL,
1504 "64-bit addressing is only supported "
1505 "in 64-bit mode");
1506 }
1507 break;
1508 case P_ASP:
1509 c = 0x67;
1510 break;
1511 case P_O16:
1512 if (bits != 16)
1513 c = 0x66;
1514 break;
1515 case P_O32:
1516 if (bits == 16)
1517 c = 0x66;
1518 break;
1519 case P_O64:
1520 /* REX.W */
1521 break;
1522 case P_OSP:
1523 c = 0x66;
1524 break;
1525 case P_EVEX:
1526 case P_VEX3:
1527 case P_VEX2:
1528 case P_NOBND:
1529 case P_none:
1530 break;
1531 default:
1532 nasm_panic(0, "invalid instruction prefix");
1533 }
1534 if (c) {
1535 if (data)
1536 out_rawbyte(data, c);
1537 bytes++;
1538 }
1539 }
1540 return bytes;
1541}
1542
1543static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001544{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001545 uint8_t c;
1546 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001547 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001548 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001549 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001550 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001551 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001552 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001553 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001554 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001555
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001556 ins->rex_done = false;
1557
H. Peter Anvina77692b2016-09-20 14:04:33 -07001558 emit_prefix(data, bits, ins);
1559
H. Peter Anvin839eca22007-10-29 23:12:47 -07001560 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001561 c = *codes++;
1562 op1 = (c & 3) + ((opex & 1) << 2);
1563 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1564 opx = &ins->oprs[op1];
1565 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001566
H. Peter Anvina77692b2016-09-20 14:04:33 -07001567
H. Peter Anvin839eca22007-10-29 23:12:47 -07001568 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 case 01:
1570 case 02:
1571 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001572 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001573 emit_rex(data, ins);
1574 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001577
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 case 05:
1579 case 06:
1580 case 07:
1581 opex = c;
1582 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001583
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001584 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001585 emit_rex(data, ins);
1586 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588
Jin Kyu Song164d6072013-10-15 19:10:13 -07001589 case4(014):
1590 break;
1591
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001593 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001594 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001596 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001600 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001601 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001603 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001607 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001608 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001610
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001612 if (opx->type & (BITS16 | BITS32))
1613 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 else
1615 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001616 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001617 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001621 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001622 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001624
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001625 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001626 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001627 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001628 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001630
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001632 if (opx->segment == data->segment) {
1633 int64_t delta = opx->offset - data->offset
1634 - (data->inslen - data->insoffs);
1635 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001636 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001637 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001638 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001642 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001643 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001646 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001650 if (opx->type & (BITS16 | BITS32 | BITS64))
1651 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 else
1653 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001654
1655 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001659 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001662 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001663 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001664 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001665 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001666 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001668
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001670 {
1671 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1672 const struct operand *opy;
1673
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 c = *codes++;
1675 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001676 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001677 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001678 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 "non-absolute expression not permitted as argument %d",
1680 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001681 } else if (opy->offset & ~mask) {
1682 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1683 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001685 c = opy->offset & mask;
1686 goto emit_is4;
1687 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001688
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 case 0173:
1690 c = *codes++;
1691 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001692 c &= 15;
1693 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001694
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001695 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001696 c = 0;
1697 emit_is4:
1698 r = nasm_regvals[opx->basereg];
1699 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001700 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001701
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001702 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001703 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001704 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001705 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 "signed dword immediate exceeds bounds");
1707 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001708 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001709 break;
1710
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001711 case4(0240):
1712 case 0250:
1713 codes += 3;
1714 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1715 EVEX_P2Z | EVEX_P2AAA, 2);
1716 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1717 bytes[0] = 0x62;
1718 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001719 bytes[1] = ((((ins->rex & 7) << 5) |
1720 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001721 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001722 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1723 ((~ins->vexreg & 15) << 3) |
1724 (1 << 2) | (ins->vex_wlp & 3);
1725 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001726 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001727 break;
1728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 case4(0260):
1730 case 0270:
1731 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001732 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1733 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1735 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1736 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001737 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001738 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001739 } else {
1740 bytes[0] = 0xc5;
1741 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001742 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001743 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 }
1745 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001746
H. Peter Anvine014f352012-02-25 22:35:19 -08001747 case 0271:
1748 case 0272:
1749 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001750 break;
1751
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001752 case4(0274):
1753 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001754 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001756
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001757 if (absolute_op(opx)) {
1758 if (ins->rex & REX_W)
1759 s = 64;
1760 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1761 s = 16;
1762 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1763 s = 32;
1764 else
1765 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001766
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001767 um = (uint64_t)2 << (s-1);
1768 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001769
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001770 if (uv > 127 && uv < (uint64_t)-128 &&
1771 (uv < um-128 || uv > um-1)) {
1772 /* If this wasn't explicitly byte-sized, warn as though we
1773 * had fallen through to the imm16/32/64 case.
1774 */
1775 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1776 "%s value exceeds bounds",
1777 (opx->type & BITS8) ? "signed byte" :
1778 s == 16 ? "word" :
1779 s == 32 ? "dword" :
1780 "signed dword");
1781 }
1782
1783 /* Output as a raw byte to avoid byte overflow check */
1784 out_rawbyte(data, (uint8_t)uv);
1785 } else {
1786 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001787 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001788 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001790
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001791 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001795 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1796 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001798
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001800 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1801 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case 0312:
1805 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001806
Keith Kaniosb7a89542007-04-12 02:40:54 +00001807 case 0313:
1808 ins->rex = 0;
1809 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001810
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 case4(0314):
1812 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001813
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001817
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001819 case 0323:
1820 break;
1821
Keith Kaniosb7a89542007-04-12 02:40:54 +00001822 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001823 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001825
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001826 case 0325:
1827 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001828
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001829 case 0326:
1830 break;
1831
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001833 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001838
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001841 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001843
Keith Kanios48af1772007-08-17 07:37:52 +00001844 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001845 if (ins->rex & REX_R)
1846 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001847 ins->rex &= ~(REX_L|REX_R);
1848 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001849
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001850 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001852
H. Peter Anvin962e3052008-08-28 17:47:16 -07001853 case 0336:
1854 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001856
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001859 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001860
1861 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 case 0341:
1865 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001866
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 case 0360:
1868 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001871 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001872 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 case 0364:
1875 case 0365:
1876 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001877
Keith Kanios48af1772007-08-17 07:37:52 +00001878 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001880 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001881 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001882
Jin Kyu Song03041092013-10-15 19:38:51 -07001883 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001885
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001887 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001889
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001890 case 0374:
1891 eat = EA_XMMVSIB;
1892 break;
1893
1894 case 0375:
1895 eat = EA_YMMVSIB;
1896 break;
1897
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001898 case 0376:
1899 eat = EA_ZMMVSIB;
1900 break;
1901
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 case4(0100):
1903 case4(0110):
1904 case4(0120):
1905 case4(0130):
1906 case4(0200):
1907 case4(0204):
1908 case4(0210):
1909 case4(0214):
1910 case4(0220):
1911 case4(0224):
1912 case4(0230):
1913 case4(0234):
1914 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 ea ea_data;
1916 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001918 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001919 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001920
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001921 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 /* pick rfield from operand b (opx) */
1923 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001924 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001925 } else {
1926 /* rfield is constant */
1927 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001929 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001931 if (process_ea(opy, &ea_data, bits,
1932 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001933 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001934
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 p = bytes;
1936 *p++ = ea_data.modrm;
1937 if (ea_data.sib_present)
1938 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001939 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001941 /*
1942 * Make sure the address gets the right offset in case
1943 * the line breaks in the .lst file (BR 1197827)
1944 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001945
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001946 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001947 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001948 if (ea_data.disp8) {
1949 out_rawbyte(data, ea_data.disp8);
1950 } else if (ea_data.rip) {
1951 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001953 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001954
H. Peter Anvina77692b2016-09-20 14:04:33 -07001955 if (overflow_general(opy->offset, asize) ||
1956 signed_bits(opy->offset, ins->addr_size) !=
1957 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001958 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001959
H. Peter Anvina77692b2016-09-20 14:04:33 -07001960 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001961 (asize > ea_data.bytes)
1962 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001963 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001965 }
1966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001969 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 ": instruction code \\%o (0x%02X) given", c, c);
1971 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001973 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001974}
1975
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001976static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001978 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001979 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001980 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001981}
1982
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001983static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001984{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001985 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001986 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001987 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001988}
1989
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990static int op_rexflags(const operand * o, int mask)
1991{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001992 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001993 int val;
1994
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001995 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001996 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997
H. Peter Anvina4835d42008-05-20 14:21:29 -07001998 flags = nasm_reg_flags[o->basereg];
1999 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002000
2001 return rexflags(val, flags, mask);
2002}
2003
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002004static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002005{
2006 int rex = 0;
2007
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002008 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002009 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002010 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002011 rex |= REX_W;
2012 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2013 rex |= REX_H;
2014 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2015 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002016
2017 return rex & mask;
2018}
2019
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002020static int evexflags(int val, decoflags_t deco,
2021 int mask, uint8_t byte)
2022{
2023 int evex = 0;
2024
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002025 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002026 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002027 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002028 evex |= (EVEX_P0RP | EVEX_P0X);
2029 break;
2030 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002031 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002032 evex |= EVEX_P2VP;
2033 if (deco & Z)
2034 evex |= EVEX_P2Z;
2035 if (deco & OPMASK_MASK)
2036 evex |= deco & EVEX_P2AAA;
2037 break;
2038 }
2039 return evex & mask;
2040}
2041
2042static int op_evexflags(const operand * o, int mask, uint8_t byte)
2043{
2044 int val;
2045
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002046 val = nasm_regvals[o->basereg];
2047
2048 return evexflags(val, o->decoflags, mask, byte);
2049}
2050
H. Peter Anvin23595f52009-07-25 17:44:25 -07002051static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002052 insn *instruction,
2053 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002054{
2055 const struct itemplate *temp;
2056 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002057 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002058 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002059 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002060 int i;
2061
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002062 /* broadcasting uses a different data element size */
2063 for (i = 0; i < instruction->operands; i++)
2064 if (i == broadcast)
2065 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2066 else
2067 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002068
2069 merr = MERR_INVALOP;
2070
2071 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 temp->opcode != I_none; temp++) {
2073 m = matches(temp, instruction, bits);
2074 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002075 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002076 m = MOK_GOOD;
2077 else
2078 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002079 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002080 /*
2081 * Missing operand size and a candidate for fuzzy matching...
2082 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002083 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002084 if (i == broadcast)
2085 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2086 else
2087 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 opsizemissing = true;
2089 }
2090 if (m > merr)
2091 merr = m;
2092 if (merr == MOK_GOOD)
2093 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002094 }
2095
2096 /* No match, but see if we can get a fuzzy operand size match... */
2097 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002098 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002099
2100 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002101 /*
2102 * We ignore extrinsic operand sizes on registers, so we should
2103 * never try to fuzzy-match on them. This also resolves the case
2104 * when we have e.g. "xmmrm128" in two different positions.
2105 */
2106 if (is_class(REGISTER, instruction->oprs[i].type))
2107 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002109 /* This tests if xsizeflags[i] has more than one bit set */
2110 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2111 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002112
Jin Kyu Song7903c072013-10-30 03:00:12 -07002113 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002114 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002115 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2116 BITS32 : BITS64);
2117 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002118 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002119 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002120 }
2121
2122 /* Try matching again... */
2123 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002124 temp->opcode != I_none; temp++) {
2125 m = matches(temp, instruction, bits);
2126 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002127 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002128 m = MOK_GOOD;
2129 else
2130 m = MERR_INVALOP;
2131 }
2132 if (m > merr)
2133 merr = m;
2134 if (merr == MOK_GOOD)
2135 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002136 }
2137
H. Peter Anvina81655b2009-07-25 18:15:28 -07002138done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002139 *tempp = temp;
2140 return merr;
2141}
2142
Mark Charneydcaef4b2014-10-09 13:45:17 -04002143static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2144{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002145 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002146 uint8_t brcast_num;
2147
Mark Charneydcaef4b2014-10-09 13:45:17 -04002148 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002149 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002150 "size of broadcasting element is greater than 64 bits");
2151
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002152 /*
2153 * The shift term is to take care of the extra BITS80 inserted
2154 * between BITS64 and BITS128.
2155 */
2156 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2157 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002158
2159 return brcast_num;
2160}
2161
H. Peter Anvin65289e82009-07-25 17:25:11 -07002162static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002163 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002164{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002165 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002166 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002167 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002168
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002169 /*
2170 * Check the opcode
2171 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002173 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002174
2175 /*
2176 * Count the operands
2177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002179 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002180
2181 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002182 * Is it legal?
2183 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002184 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002185 return MERR_INVALOP;
2186
2187 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002188 * {evex} available?
2189 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002190 switch (instruction->prefixes[PPS_VEX]) {
2191 case P_EVEX:
2192 if (!itemp_has(itemp, IF_EVEX))
2193 return MERR_ENCMISMATCH;
2194 break;
2195 case P_VEX3:
2196 case P_VEX2:
2197 if (!itemp_has(itemp, IF_VEX))
2198 return MERR_ENCMISMATCH;
2199 break;
2200 default:
2201 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002202 }
2203
2204 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002205 * Check that no spurious colons or TOs are present
2206 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 for (i = 0; i < itemp->operands; i++)
2208 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002209 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002210
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002211 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002212 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002213 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 switch (itemp_smask(itemp)) {
2215 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002216 asize = BITS8;
2217 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002218 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 asize = BITS16;
2220 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002221 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 asize = BITS32;
2223 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002224 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = BITS64;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 asize = BITS128;
2229 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002230 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 asize = BITS256;
2232 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002233 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002234 asize = BITS512;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002237 switch (bits) {
2238 case 16:
2239 asize = BITS16;
2240 break;
2241 case 32:
2242 asize = BITS32;
2243 break;
2244 case 64:
2245 asize = BITS64;
2246 break;
2247 default:
2248 asize = 0;
2249 break;
2250 }
2251 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002252 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002253 asize = 0;
2254 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002255 }
2256
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002257 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002258 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002259 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002260 memset(size, 0, sizeof size);
2261 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002262 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002263 /* S- flags apply to all operands */
2264 for (i = 0; i < MAX_OPERANDS; i++)
2265 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002266 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002267
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002268 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 * Check that the operand flags all match up,
2270 * it's a bit tricky so lets be verbose:
2271 *
2272 * 1) Find out the size of operand. If instruction
2273 * doesn't have one specified -- we're trying to
2274 * guess it either from template (IF_S* flag) or
2275 * from code bits.
2276 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002277 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002278 * template has an operand size specified AND this size differ
2279 * from which instruction has (perhaps we got it from code bits)
2280 * we are:
2281 * a) Check that only size of instruction and operand is differ
2282 * other characteristics do match
2283 * b) Perhaps it's a register specified in instruction so
2284 * for such a case we just mark that operand as "size
2285 * missing" and this will turn on fuzzy operand size
2286 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002287 */
2288 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002289 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002290 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002291 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002292 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002293 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002294 opflags_t template_opsize, insn_opsize;
2295
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002296 if (!(type & SIZE_MASK))
2297 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002298
Jin Kyu Song7903c072013-10-30 03:00:12 -07002299 insn_opsize = type & SIZE_MASK;
2300 if (!is_broadcast) {
2301 template_opsize = itemp->opd[i] & SIZE_MASK;
2302 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002303 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2304
2305 if (~ideco & BRDCAST_MASK)
2306 return MERR_BRNOTHERE;
2307
Jin Kyu Song7903c072013-10-30 03:00:12 -07002308 /*
2309 * when broadcasting, the element size depends on
2310 * the instruction type. decorator flag should match.
2311 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002312 if (deco_brsize) {
2313 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002314 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002315 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002316 } else {
2317 template_opsize = 0;
2318 }
2319 }
2320
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002321 if (~ideco & deco & OPMASK_MASK)
2322 return MERR_MASKNOTHERE;
2323
2324 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002325 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002326 } else if (template_opsize) {
2327 if (template_opsize != insn_opsize) {
2328 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002329 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002330 } else if (!is_class(REGISTER, type)) {
2331 /*
2332 * Note: we don't honor extrinsic operand sizes for registers,
2333 * so "missing operand size" for a register should be
2334 * considered a wildcard match rather than an error.
2335 */
2336 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002337 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002338 } else if (is_broadcast &&
2339 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002340 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002341 /*
2342 * broadcasting opsize matches but the number of repeated memory
2343 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002344 * if 64b double precision float is broadcasted to ymm (256b),
2345 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002346 */
2347 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002348 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002349 }
2350 }
2351
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002352 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002353 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002354
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002355 /*
2356 * Check operand sizes
2357 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002358 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2359 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002361 asize = itemp->opd[i] & SIZE_MASK;
2362 if (asize) {
2363 for (i = 0; i < oprs; i++)
2364 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 break;
2366 }
2367 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002368 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002370 }
2371
Keith Kaniosb7a89542007-04-12 02:40:54 +00002372 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 if (!(itemp->opd[i] & SIZE_MASK) &&
2374 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002375 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002376 }
2377
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002378 /*
2379 * Check template is okay at the set cpu level
2380 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002381 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002382 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002383
Keith Kaniosb7a89542007-04-12 02:40:54 +00002384 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002385 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002386 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002387 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002388 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002390 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002391 * If we have a HLE prefix, look for the NOHLE flag
2392 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002393 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002394 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2395 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2396 return MERR_BADHLE;
2397
2398 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002399 * Check if special handling needed for Jumps
2400 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002401 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002402 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403
Jin Kyu Song03041092013-10-15 19:38:51 -07002404 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002405 * Check if BND prefix is allowed.
2406 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002407 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002408 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002409 (has_prefix(instruction, PPS_REP, P_BND) ||
2410 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002411 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002412 else if (itemp_has(itemp, IF_BND) &&
2413 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2414 has_prefix(instruction, PPS_REP, P_REPNZ)))
2415 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002416
H. Peter Anvin60926242009-07-26 16:25:38 -07002417 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002418}
2419
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002420/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002421 * Check if ModR/M.mod should/can be 01.
2422 * - EAF_BYTEOFFS is set
2423 * - offset can fit in a byte when EVEX is not used
2424 * - offset can be compressed when EVEX is used
2425 */
2426#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2427 (o >= -128 && o <= 127 && \
2428 seg == NO_SEG && !forw_ref && \
2429 !(input->eaflags & EAF_WORDOFFS) && \
2430 !(ins->rex & REX_EV)) || \
2431 (ins->rex & REX_EV && \
2432 is_disp8n(input, ins, &output->disp8)))
2433
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002434static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002435 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002436{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002437 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002438 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002439 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002440
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002441 output->type = EA_SCALAR;
2442 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002443 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002444
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002445 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002446 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002447 /* EVEX.R' flag for the REG operand */
2448 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002449
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 if (is_class(REGISTER, input->type)) {
2451 /*
2452 * It's a direct register.
2453 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002454 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002455 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002456
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002458 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002459
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002460 /* broadcasting is not available with a direct register operand. */
2461 if (input->decoflags & BRDCAST_MASK) {
2462 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2463 goto err;
2464 }
2465
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002466 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002467 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002468 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002469 output->bytes = 0; /* no offset necessary either */
2470 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2471 } else {
2472 /*
2473 * It's a memory reference.
2474 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002475
2476 /* Embedded rounding or SAE is not available with a mem ref operand. */
2477 if (input->decoflags & (ER | SAE)) {
2478 nasm_error(ERR_NONFATAL,
2479 "Embedded rounding is available only with reg-reg op.");
2480 return -1;
2481 }
2482
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002483 if (input->basereg == -1 &&
2484 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002485 /*
2486 * It's a pure offset.
2487 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002488 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
2489 if (input->segment == NO_SEG || (input->opflags & OPFLAG_RELATIVE)) {
2490 nasm_error(ERR_WARNING | ERR_PASS2, "absolute address can not be RIP-relative");
2491 input->type &= ~IP_REL;
2492 input->type |= MEMORY;
2493 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002494 }
2495
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002496 if (bits == 64 &&
2497 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2498 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2499 return -1;
2500 }
2501
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002502 if (eaflags & EAF_BYTEOFFS ||
2503 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002504 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2505 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2506 }
2507
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002508 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002509 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002510 output->sib = GEN_SIB(0, 4, 5);
2511 output->bytes = 4;
2512 output->modrm = GEN_MODRM(0, rfield, 4);
2513 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002514 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002516 output->bytes = (addrbits != 16 ? 4 : 2);
2517 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2518 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002519 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002520 } else {
2521 /*
2522 * It's an indirection.
2523 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002525 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 int t, it, bt; /* register numbers */
2528 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 if (s == 0)
2531 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002532
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002533 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002534 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002535 ix = nasm_reg_flags[i];
2536 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002537 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002538 ix = 0;
2539 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002540
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002542 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002543 bx = nasm_reg_flags[b];
2544 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002545 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 bx = 0;
2547 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002548
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002549 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002550 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002551 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002552 int32_t o = input->offset;
2553 int mod, scale, index, base;
2554
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 /*
2556 * For a vector SIB, one has to be a vector and the other,
2557 * if present, a GPR. The vector must be the index operand.
2558 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002559 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002560 if (s == 0)
2561 s = 1;
2562 else if (s != 1)
2563 goto err;
2564
2565 t = bt, bt = it, it = t;
2566 x = bx, bx = ix, ix = x;
2567 }
2568
2569 if (bt != -1) {
2570 if (REG_GPR & ~bx)
2571 goto err;
2572 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2573 sok &= bx;
2574 else
2575 goto err;
2576 }
2577
2578 /*
2579 * While we're here, ensure the user didn't specify
2580 * WORD or QWORD
2581 */
2582 if (input->disp_size == 16 || input->disp_size == 64)
2583 goto err;
2584
2585 if (addrbits == 16 ||
2586 (addrbits == 32 && !(sok & BITS32)) ||
2587 (addrbits == 64 && !(sok & BITS64)))
2588 goto err;
2589
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002590 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2591 : ((ix & YMMREG & ~REG_EA)
2592 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002593
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002594 output->rex |= rexflags(it, ix, REX_X);
2595 output->rex |= rexflags(bt, bx, REX_B);
2596 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002597
2598 index = it & 7; /* it is known to be != -1 */
2599
2600 switch (s) {
2601 case 1:
2602 scale = 0;
2603 break;
2604 case 2:
2605 scale = 1;
2606 break;
2607 case 4:
2608 scale = 2;
2609 break;
2610 case 8:
2611 scale = 3;
2612 break;
2613 default: /* then what the smeg is it? */
2614 goto err; /* panic */
2615 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002616
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002617 if (bt == -1) {
2618 base = 5;
2619 mod = 0;
2620 } else {
2621 base = (bt & 7);
2622 if (base != REG_NUM_EBP && o == 0 &&
2623 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002624 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002625 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002626 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002627 mod = 1;
2628 else
2629 mod = 2;
2630 }
2631
2632 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002633 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2634 output->modrm = GEN_MODRM(mod, rfield, 4);
2635 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002636 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002637 /*
2638 * it must be a 32/64-bit memory reference. Firstly we have
2639 * to check that all registers involved are type E/Rxx.
2640 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002641 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002642 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002643
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002644 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002645 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2646 sok &= ix;
2647 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002649 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002650
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 if (bt != -1) {
2652 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002655 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002656 sok &= bx;
2657 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 /*
2660 * While we're here, ensure the user didn't specify
2661 * WORD or QWORD
2662 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002663 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002664 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 if (addrbits == 16 ||
2667 (addrbits == 32 && !(sok & BITS32)) ||
2668 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002669 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002670
Keith Kaniosb7a89542007-04-12 02:40:54 +00002671 /* now reorganize base/index */
2672 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002673 ((hb == b && ht == EAH_NOTBASE) ||
2674 (hb == i && ht == EAH_MAKEBASE))) {
2675 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002676 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002677 x = bx, bx = ix, ix = x;
2678 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002679
Jin Kyu Song164d6072013-10-15 19:10:13 -07002680 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002681 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002682 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002684 if (eaflags & EAF_MIB) {
2685 /* only for mib operands */
2686 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2687 /*
2688 * make a single reg index [reg*1].
2689 * gas uses this form for an explicit index register.
2690 */
2691 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2692 }
2693 if ((ht == EAH_SUMMED) && bt == -1) {
2694 /* separate once summed index into [base, index] */
2695 bt = it, bx = ix, s--;
2696 }
2697 } else {
2698 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002699 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002700 s == 3 || s == 5 || s == 9) && bt == -1) {
2701 /* convert 3*EAX to EAX+2*EAX */
2702 bt = it, bx = ix, s--;
2703 }
2704 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002705 (eaflags & EAF_TIMESTWO) &&
2706 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002707 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002708 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002709 * to sib format with 0x0 displacement - [EAX*1+0].
2710 */
2711 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2712 }
2713 }
Keith Kanios48af1772007-08-17 07:37:52 +00002714 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002716 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002717 x = ix, ix = bx, bx = x;
2718 }
2719 if (it == REG_NUM_ESP ||
2720 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002721 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002722
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 output->rex |= rexflags(it, ix, REX_X);
2724 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002725
Keith Kanios48af1772007-08-17 07:37:52 +00002726 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002727 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002729
Keith Kaniosb7a89542007-04-12 02:40:54 +00002730 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 } else {
2734 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002735 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002736 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002737 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002738 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002739 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002740 mod = 1;
2741 else
2742 mod = 2;
2743 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002744
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002745 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002746 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2747 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002748 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002749 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002751
Keith Kaniosb7a89542007-04-12 02:40:54 +00002752 if (it == -1)
2753 index = 4, s = 1;
2754 else
2755 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 switch (s) {
2758 case 1:
2759 scale = 0;
2760 break;
2761 case 2:
2762 scale = 1;
2763 break;
2764 case 4:
2765 scale = 2;
2766 break;
2767 case 8:
2768 scale = 3;
2769 break;
2770 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002771 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002773
Keith Kaniosb7a89542007-04-12 02:40:54 +00002774 if (bt == -1) {
2775 base = 5;
2776 mod = 0;
2777 } else {
2778 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002779 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002780 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002781 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002782 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002783 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002784 mod = 1;
2785 else
2786 mod = 2;
2787 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002788
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002789 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002790 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2791 output->modrm = GEN_MODRM(mod, rfield, 4);
2792 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 }
2794 } else { /* it's 16-bit */
2795 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002796 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002797
Keith Kaniosb7a89542007-04-12 02:40:54 +00002798 /* check for 64-bit long mode */
2799 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002800 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002801
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002803 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2804 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002805 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002806
Keith Kaniosb7a89542007-04-12 02:40:54 +00002807 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002808 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 if (b == -1 && i != -1) {
2814 int tmp = b;
2815 b = i;
2816 i = tmp;
2817 } /* swap */
2818 if ((b == R_SI || b == R_DI) && i != -1) {
2819 int tmp = b;
2820 b = i;
2821 i = tmp;
2822 }
2823 /* have BX/BP as base, SI/DI index */
2824 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002825 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 if (i != -1 && b != -1 &&
2827 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002828 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002829 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002830 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002831
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 rm = -1;
2833 if (i != -1)
2834 switch (i * 256 + b) {
2835 case R_SI * 256 + R_BX:
2836 rm = 0;
2837 break;
2838 case R_DI * 256 + R_BX:
2839 rm = 1;
2840 break;
2841 case R_SI * 256 + R_BP:
2842 rm = 2;
2843 break;
2844 case R_DI * 256 + R_BP:
2845 rm = 3;
2846 break;
2847 } else
2848 switch (b) {
2849 case R_SI:
2850 rm = 4;
2851 break;
2852 case R_DI:
2853 rm = 5;
2854 break;
2855 case R_BP:
2856 rm = 6;
2857 break;
2858 case R_BX:
2859 rm = 7;
2860 break;
2861 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002863 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002865 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002866 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002868 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 mod = 1;
2870 else
2871 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002872
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002873 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002874 output->bytes = mod; /* bytes of offset needed */
2875 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 }
2877 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002878 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002879
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002880 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002881 return output->type;
2882
2883err:
2884 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002885}
2886
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002887static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002888{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002889 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002890 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002891
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002892 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002893
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002894 switch (ins->prefixes[PPS_ASIZE]) {
2895 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 valid &= 16;
2897 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002898 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 valid &= 32;
2900 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002901 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 valid &= 64;
2903 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 valid &= (addrbits == 32) ? 16 : 32;
2906 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002909 }
2910
2911 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 if (is_class(MEMORY, ins->oprs[j].type)) {
2913 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002916 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002917 i = 0;
2918 else
2919 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002922 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 b = 0;
2924 else
2925 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 if (ins->oprs[j].scale == 0)
2928 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 if (!i && !b) {
2931 int ds = ins->oprs[j].disp_size;
2932 if ((addrbits != 64 && ds > 8) ||
2933 (addrbits == 64 && ds == 16))
2934 valid &= ds;
2935 } else {
2936 if (!(REG16 & ~b))
2937 valid &= 16;
2938 if (!(REG32 & ~b))
2939 valid &= 32;
2940 if (!(REG64 & ~b))
2941 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002942
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 if (!(REG16 & ~i))
2944 valid &= 16;
2945 if (!(REG32 & ~i))
2946 valid &= 32;
2947 if (!(REG64 & ~i))
2948 valid &= 64;
2949 }
2950 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002951 }
2952
2953 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002954 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002955 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002956 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002957 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002958 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002959 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002960 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002961 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002963 }
2964
2965 defdisp = ins->addr_size == 16 ? 16 : 32;
2966
2967 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2969 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2970 /*
2971 * mem_offs sizes must match the address size; if not,
2972 * strip the MEM_OFFS bit and match only EA instructions
2973 */
2974 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2975 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002976 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002977}