blob: 0fe323cfbfe403d34e21c2ad9c03d1331ce88dd4 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
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 Anvinea6e34d2002-04-30 20:51:32 +0000186#include "assemble.h"
187#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700188#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800189#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800190#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800231static int64_t calcsize(int32_t, int64_t, int, insn *,
232 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700233static void gencode(int32_t segment, int64_t offset, int bits,
234 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400235 int64_t insn_end);
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
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400249static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000250{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700251 return ins->prefixes[pos] == prefix;
252}
253
254static void assert_no_prefix(insn * ins, enum prefix_pos pos)
255{
256 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800257 nasm_error(ERR_NONFATAL, "invalid %s prefix",
258 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259}
260
261static const char *size_name(int size)
262{
263 switch (size) {
264 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400265 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400267 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700268 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400269 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700270 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700276 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700278 case 64:
279 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000282 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700283}
284
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400285static void warn_overflow(int pass, int size)
286{
H. Peter Anvin215186f2016-02-17 20:27:41 -0800287 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400288 "%s data exceeds bounds", size_name(size));
289}
290
291static void warn_overflow_const(int64_t data, int size)
292{
293 if (overflow_general(data, size))
294 warn_overflow(ERR_PASS1, size);
295}
296
297static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700298{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100299 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400300 if (overflow_general(o->offset, size))
301 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700302 }
303}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400304
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000305/*
H. Peter Anvinb6412502016-02-11 21:07:40 -0800306 * Size of an address relocation, or zero if not an address
307 */
308static int addrsize(enum out_type type, uint64_t size)
309{
310 switch (type) {
311 case OUT_ADDRESS:
312 return abs((int)size);
313 case OUT_REL1ADR:
314 return 1;
315 case OUT_REL2ADR:
316 return 2;
317 case OUT_REL4ADR:
318 return 4;
319 case OUT_REL8ADR:
320 return 8;
321 default:
322 return 0;
323 }
324}
325
326/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800332static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800333 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400334 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000335{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000336 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000337 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800338 uint8_t p[8];
H. Peter Anvinb6412502016-02-11 21:07:40 -0800339 int asize = addrsize(type, size); /* Address size in bytes */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800340 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000341
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800342 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400343 /*
344 * This is a non-relocated address, and we're going to
345 * convert it into RAWDATA format.
346 */
347 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800348
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800349 if (asize > 8) {
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800350 nasm_panic(0, "OUT_ADDRESS with size > 8");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400351 return;
352 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700353
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800354 WRITEADDR(q, *(int64_t *)data, asize);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400355 data = p;
356 type = OUT_RAWDATA;
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800357 size = asize;
H. Peter Anvinb6412502016-02-11 21:07:40 -0800358 asize = 0; /* No longer an address */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000359 }
360
H. Peter Anvin172b8402016-02-18 01:16:18 -0800361 lfmt->output(offset, data, type, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800362
Frank Kotlerabebb082003-09-06 04:45:37 +0000363 /*
364 * this call to src_get determines when we call the
365 * debug-format-specific "linenum" function
366 * it updates lineno and lnfname to the current values
367 * returning 0 if "same as last time", -2 if lnfname
368 * changed, and the amount by which lineno changed,
369 * if it did. thus, these variables must be static
370 */
371
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400372 if (src_get(&lineno, &lnfname))
H. Peter Anvin335c4852016-02-17 20:55:08 -0800373 dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000374
H. Peter Anvinb6412502016-02-11 21:07:40 -0800375 if (asize && asize > amax) {
376 if (type != OUT_ADDRESS || (int)size < 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800377 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800378 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800379 asize << 3, ofmt->shortname);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800380 size = asize;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800381 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800382 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800383 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800384 asize << 3, ofmt->maxbits);
385 ofmt->output(segto, data, type, amax, segment, wrt);
H. Peter Anvinca351fa2016-02-12 13:46:39 -0800386 size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800387 }
388 data = zero_buffer;
389 type = OUT_RAWDATA;
H. Peter Anvinb03d91e2016-02-11 21:13:54 -0800390 segment = wrt = NO_SEG;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800391 }
392
H. Peter Anvin215186f2016-02-17 20:27:41 -0800393 ofmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394}
395
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800396static void out_imm8(int64_t offset, int32_t segment,
397 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400398{
399 if (opx->segment != NO_SEG) {
400 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800401 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400402 } else {
403 uint8_t byte = opx->offset;
404 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
405 }
406}
407
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700408static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800409 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800411 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800412 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800414 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000415
H. Peter Anvin755f5212012-02-25 11:41:34 -0800416 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700417 return false;
418 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400419 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700420 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400421 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700422
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800423 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100424
Victor van den Elzen154e5922009-02-25 17:32:00 +0100425 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100426 /* Be optimistic in pass 1 */
427 return true;
428
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700430 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000431
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700432 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800433 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
434
435 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
436 /* jmp short (opcode eb) cannot be used with bnd prefix. */
437 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800438 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800439 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800440 }
441
442 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000444
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400445int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800446 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000447{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000448 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700450 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800451 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000452 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800453 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300454 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000455
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000456 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000457
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300458 wsize = idata_bytes(instruction->opcode);
459 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000461
H. Peter Anvineba20a72002-04-30 20:53:55 +0000462 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000464 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800466 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400469 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400471 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800472 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400473 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000474 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700475 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800477 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400478 offset += wsize;
479 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700480 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400481 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800485 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000487
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 if (align) {
489 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100490 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800491 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 }
493 offset += e->stringlen + align;
494 }
495 }
496 if (t > 0 && t == instruction->times - 1) {
497 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800498 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 * listing module.
500 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800501 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
502 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 }
504 }
505 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800506 lfmt->downlevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000508 }
509
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700511 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400514 fp = fopen(fname, "rb");
515 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800516 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400518 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800519 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200521 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400522 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700523 static char buf[4096];
524 size_t t = instruction->times;
525 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400526 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000527
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 len = ftell(fp);
529 if (instruction->eops->next) {
530 base = instruction->eops->next->offset;
531 len -= base;
532 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700533 len > (size_t)instruction->eops->next->next->offset)
534 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 }
536 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800537 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 * listing module.
539 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800540 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
541 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700543 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 fseek(fp, base, SEEK_SET);
546 l = len;
547 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700548 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400549 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 if (!m) {
551 /*
552 * This shouldn't happen unless the file
553 * actually changes while we are reading
554 * it.
555 */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800556 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 "`incbin': unexpected EOF while"
558 " reading file `%s'", fname);
559 t = 0; /* Try to exit cleanly */
560 break;
561 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800562 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 NO_SEG, NO_SEG);
564 l -= m;
565 }
566 }
H. Peter Anvin172b8402016-02-18 01:16:18 -0800567 lfmt->downlevel(LIST_INCBIN);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (instruction->times > 1) {
569 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800570 * Dummy call to lfmt->output to give the offset to the
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 * listing module.
572 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800573 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
574 lfmt->uplevel(LIST_TIMES);
575 lfmt->downlevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 }
577 fclose(fp);
578 return instruction->times * len;
579 }
580 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 }
582
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700583 /* Check to see if we need an address-size prefix */
584 add_asp(instruction, bits);
585
H. Peter Anvin23595f52009-07-25 17:44:25 -0700586 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700587
H. Peter Anvin23595f52009-07-25 17:44:25 -0700588 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400589 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800590 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400591 itimes = instruction->times;
592 if (insn_size < 0) /* shouldn't be, on pass two */
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800593 nasm_panic(0, "errors made it through from pass one");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400594 else
595 while (itimes--) {
596 for (j = 0; j < MAXPREFIX; j++) {
597 uint8_t c = 0;
598 switch (instruction->prefixes[j]) {
599 case P_WAIT:
600 c = 0x9B;
601 break;
602 case P_LOCK:
603 c = 0xF0;
604 break;
605 case P_REPNE:
606 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800607 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700608 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400609 c = 0xF2;
610 break;
611 case P_REPE:
612 case P_REPZ:
613 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800614 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400615 c = 0xF3;
616 break;
617 case R_CS:
618 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800619 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400620 "cs segment base generated, but will be ignored in 64-bit mode");
621 }
622 c = 0x2E;
623 break;
624 case R_DS:
625 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800626 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400627 "ds segment base generated, but will be ignored in 64-bit mode");
628 }
629 c = 0x3E;
630 break;
631 case R_ES:
632 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800633 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400634 "es segment base generated, but will be ignored in 64-bit mode");
635 }
636 c = 0x26;
637 break;
638 case R_FS:
639 c = 0x64;
640 break;
641 case R_GS:
642 c = 0x65;
643 break;
644 case R_SS:
645 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800646 nasm_error(ERR_WARNING | ERR_PASS2,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400647 "ss segment base generated, but will be ignored in 64-bit mode");
648 }
649 c = 0x36;
650 break;
651 case R_SEGR6:
652 case R_SEGR7:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800653 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400654 "segr6 and segr7 cannot be used as prefixes");
655 break;
656 case P_A16:
657 if (bits == 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800658 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400659 "16-bit addressing is not supported "
660 "in 64-bit mode");
661 } else if (bits != 16)
662 c = 0x67;
663 break;
664 case P_A32:
665 if (bits != 32)
666 c = 0x67;
667 break;
668 case P_A64:
669 if (bits != 64) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800670 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400671 "64-bit addressing is only supported "
672 "in 64-bit mode");
673 }
674 break;
675 case P_ASP:
676 c = 0x67;
677 break;
678 case P_O16:
679 if (bits != 16)
680 c = 0x66;
681 break;
682 case P_O32:
683 if (bits == 16)
684 c = 0x66;
685 break;
686 case P_O64:
687 /* REX.W */
688 break;
689 case P_OSP:
690 c = 0x66;
691 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700692 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800693 case P_VEX3:
694 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800695 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400696 case P_none:
697 break;
698 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800699 nasm_panic(0, "invalid instruction prefix");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400700 }
701 if (c != 0) {
702 out(offset, segment, &c, OUT_RAWDATA, 1,
703 NO_SEG, NO_SEG);
704 offset++;
705 }
706 }
707 insn_end = offset + insn_size;
708 gencode(segment, offset, bits, instruction,
709 temp, insn_end);
710 offset += insn_size;
711 if (itimes > 0 && itimes == instruction->times - 1) {
712 /*
H. Peter Anvin172b8402016-02-18 01:16:18 -0800713 * Dummy call to lfmt->output to give the offset to the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400714 * listing module.
715 */
H. Peter Anvin172b8402016-02-18 01:16:18 -0800716 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
717 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400718 }
719 }
720 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800721 lfmt->downlevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400722 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700723 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400724 /* No match */
725 switch (m) {
726 case MERR_OPSIZEMISSING:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800727 nasm_error(ERR_NONFATAL, "operation size not specified");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400728 break;
729 case MERR_OPSIZEMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800730 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400731 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700732 case MERR_BRNUMMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800733 nasm_error(ERR_NONFATAL,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700734 "mismatch in the number of broadcasting elements");
735 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400736 case MERR_BADCPU:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800737 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400738 break;
739 case MERR_BADMODE:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800740 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400741 bits);
742 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800743 case MERR_ENCMISMATCH:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800744 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800745 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800746 case MERR_BADBND:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800747 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800748 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800749 case MERR_BADREPNE:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800750 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800751 (has_prefix(instruction, PPS_REP, P_REPNE) ?
752 "repne" : "repnz"));
753 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400754 default:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800755 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400757 break;
758 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000759 }
760 return 0;
761}
762
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400763int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800764 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000766 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700767 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000768
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000769 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000770
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400771 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700774 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
775 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400776 instruction->opcode == I_DT || instruction->opcode == I_DO ||
777 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300779 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000780
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300782 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000783
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400784 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000785 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400788 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400790 warn_overflow_const(e->offset, wsize);
791 } else if (e->type == EOT_DB_STRING ||
792 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 align = (-osize) % wsize;
796 if (align < 0)
797 align += wsize;
798 isize += osize + align;
799 }
800 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000801 }
802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400804 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300806 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700807 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400809 fp = fopen(fname, "rb");
810 if (!fp)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800811 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 fname);
813 else if (fseek(fp, 0L, SEEK_END) < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800814 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 fname);
816 else {
817 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 if (instruction->eops->next) {
819 len -= instruction->eops->next->offset;
820 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700821 len > (size_t)instruction->eops->next->next->offset) {
822 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 }
824 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300825 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300827 if (fp)
828 fclose(fp);
829 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000830 }
831
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700832 /* Check to see if we need an address-size prefix */
833 add_asp(instruction, bits);
834
H. Peter Anvin23595f52009-07-25 17:44:25 -0700835 m = find_match(&temp, instruction, segment, offset, bits);
836 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400837 /* we've matched an instruction. */
838 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400839 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100840
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800841 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400842 if (isize < 0)
843 return -1;
844 for (j = 0; j < MAXPREFIX; j++) {
845 switch (instruction->prefixes[j]) {
846 case P_A16:
847 if (bits != 16)
848 isize++;
849 break;
850 case P_A32:
851 if (bits != 32)
852 isize++;
853 break;
854 case P_O16:
855 if (bits != 16)
856 isize++;
857 break;
858 case P_O32:
859 if (bits == 16)
860 isize++;
861 break;
862 case P_A64:
863 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700864 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800865 case P_VEX3:
866 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800867 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400868 case P_none:
869 break;
870 default:
871 isize++;
872 break;
873 }
874 }
875 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700876 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400877 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000878 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000879}
880
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800881static void bad_hle_warn(const insn * ins, uint8_t hleok)
882{
883 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800884 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800885 static const enum whatwarn warn[2][4] =
886 {
887 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
888 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
889 };
890 unsigned int n;
891
892 n = (unsigned int)rep_pfx - P_XACQUIRE;
893 if (n > 1)
894 return; /* Not XACQUIRE/XRELEASE */
895
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800896 ww = warn[n][hleok];
897 if (!is_class(MEMORY, ins->oprs[0].type))
898 ww = w_inval; /* HLE requires operand 0 to be memory */
899
900 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800901 case w_none:
902 break;
903
904 case w_lock:
905 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800906 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800907 "%s with this instruction requires lock",
908 prefix_name(rep_pfx));
909 }
910 break;
911
912 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800913 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800914 "%s invalid with this instruction",
915 prefix_name(rep_pfx));
916 break;
917 }
918}
919
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400921#define case3(x) case (x): case (x)+1: case (x)+2
922#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700923
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800924static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800925 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000926{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800927 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800928 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000929 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000930 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700931 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700932 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700933 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700934 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800935 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800936 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700937 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000938
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700939 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700940 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700941 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700942
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700943 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400944 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 (void)segment; /* Don't warn that this parameter is unused */
947 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000948
H. Peter Anvin839eca22007-10-29 23:12:47 -0700949 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 c = *codes++;
951 op1 = (c & 3) + ((opex & 1) << 2);
952 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
953 opx = &ins->oprs[op1];
954 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700955
H. Peter Anvin839eca22007-10-29 23:12:47 -0700956 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400957 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 codes += c, length += c;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400961 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 opex = c;
963 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(010):
966 ins->rex |=
967 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 codes++, length++;
969 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700970
Jin Kyu Song164d6072013-10-15 19:10:13 -0700971 case4(014):
972 /* this is an index reg of MIB operand */
973 mib_index = opx->basereg;
974 break;
975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case4(020):
977 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 length++;
979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 length += 2;
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700986 if (opx->type & (BITS16 | BITS32 | BITS64))
987 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 else
989 length += (bits == 16) ? 2 : 4;
990 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700991
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 length += 4;
994 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700997 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 length++;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001005 length += 8; /* MOV reg64/imm */
1006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 length += 2;
1010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001013 if (opx->type & (BITS16 | BITS32 | BITS64))
1014 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 else
1016 length += (bits == 16) ? 2 : 4;
1017 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001019 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 length += 4;
1021 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001022
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001023 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001024 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001026
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 case 0172:
1028 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001029 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001030 length++;
1031 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001032
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001033 case4(0174):
1034 length++;
1035 break;
1036
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001037 case4(0240):
1038 ins->rex |= REX_EV;
1039 ins->vexreg = regval(opx);
1040 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1041 ins->vex_cm = *codes++;
1042 ins->vex_wlp = *codes++;
1043 ins->evex_tuple = (*codes++ - 0300);
1044 break;
1045
1046 case 0250:
1047 ins->rex |= REX_EV;
1048 ins->vexreg = 0;
1049 ins->vex_cm = *codes++;
1050 ins->vex_wlp = *codes++;
1051 ins->evex_tuple = (*codes++ - 0300);
1052 break;
1053
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001054 case4(0254):
1055 length += 4;
1056 break;
1057
1058 case4(0260):
1059 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001060 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 ins->vex_cm = *codes++;
1062 ins->vex_wlp = *codes++;
1063 break;
1064
1065 case 0270:
1066 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001067 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 ins->vex_cm = *codes++;
1069 ins->vex_wlp = *codes++;
1070 break;
1071
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001072 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001073 hleok = c & 3;
1074 break;
1075
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 case4(0274):
1077 length++;
1078 break;
1079
1080 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001084 if (bits == 64)
1085 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001086 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001090 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Keith Kaniosb7a89542007-04-12 02:40:54 +00001096 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001097 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1098 has_prefix(ins, PPS_ASIZE, P_A32))
1099 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001102 case4(0314):
1103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001106 {
1107 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1108 if (pfx == P_O16)
1109 break;
1110 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001111 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001112 else
1113 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001115 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001118 {
1119 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1120 if (pfx == P_O32)
1121 break;
1122 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001123 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001124 else
1125 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001127 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 case 0322:
1130 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
Keith Kaniosb7a89542007-04-12 02:40:54 +00001132 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001133 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
Keith Kaniosb7a89542007-04-12 02:40:54 +00001136 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 case 0325:
1141 ins->rex |= REX_NH;
1142 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001143
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001144 case 0326:
1145 break;
1146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 case 0330:
1148 codes++, length++;
1149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001154 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 case 0333:
1156 length++;
1157 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001158
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 case 0334:
1160 ins->rex |= REX_L;
1161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001163 case 0335:
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 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001167 if (!ins->prefixes[PPS_REP])
1168 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001172 if (!ins->prefixes[PPS_REP])
1173 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001175
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001178 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 " quantity of BSS space");
1180 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001181 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001184 case 0341:
1185 if (!ins->prefixes[PPS_WAIT])
1186 ins->prefixes[PPS_WAIT] = P_WAIT;
1187 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001188
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001189 case 0360:
1190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001192 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 length++;
1194 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001195
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001196 case 0364:
1197 case 0365:
1198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Keith Kanios48af1772007-08-17 07:37:52 +00001200 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001201 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 length++;
1203 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001204
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001205 case 0370:
1206 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 case 0373:
1210 length++;
1211 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001213 case 0374:
1214 eat = EA_XMMVSIB;
1215 break;
1216
1217 case 0375:
1218 eat = EA_YMMVSIB;
1219 break;
1220
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001221 case 0376:
1222 eat = EA_ZMMVSIB;
1223 break;
1224
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 case4(0100):
1226 case4(0110):
1227 case4(0120):
1228 case4(0130):
1229 case4(0200):
1230 case4(0204):
1231 case4(0210):
1232 case4(0214):
1233 case4(0220):
1234 case4(0224):
1235 case4(0230):
1236 case4(0234):
1237 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001239 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 opflags_t rflags;
1241 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001242 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001243
Keith Kaniosb7a89542007-04-12 02:40:54 +00001244 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001245
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 if (c <= 0177) {
1247 /* pick rfield from operand b (opx) */
1248 rflags = regflag(opx);
1249 rfield = nasm_regvals[opx->basereg];
1250 } else {
1251 rflags = 0;
1252 rfield = c & 7;
1253 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001254
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001255 /* EVEX.b1 : evex_brerop contains the operand position */
1256 op_er_sae = (ins->evex_brerop >= 0 ?
1257 &ins->oprs[ins->evex_brerop] : NULL);
1258
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001259 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1260 /* set EVEX.b */
1261 ins->evex_p[2] |= EVEX_P2B;
1262 if (op_er_sae->decoflags & ER) {
1263 /* set EVEX.RC (rounding control) */
1264 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1265 & EVEX_P2RC;
1266 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001267 } else {
1268 /* set EVEX.L'L (vector length) */
1269 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001270 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001271 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001272 /* set EVEX.b */
1273 ins->evex_p[2] |= EVEX_P2B;
1274 }
1275 }
1276
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001277 if (itemp_has(temp, IF_MIB)) {
1278 opy->eaflags |= EAF_MIB;
1279 /*
1280 * if a separate form of MIB (ICC style) is used,
1281 * the index reg info is merged into mem operand
1282 */
1283 if (mib_index != R_none) {
1284 opy->indexreg = mib_index;
1285 opy->scale = 1;
1286 opy->hintbase = mib_index;
1287 opy->hinttype = EAH_NOTBASE;
1288 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001289 }
1290
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001291 if (process_ea(opy, &ea_data, bits,
1292 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001293 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001295 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001298 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 }
1300 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001303 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 ": instruction code \\%o (0x%02X) given", c, c);
1305 break;
1306 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001307 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001308
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001309 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001310
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001311 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001313 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 return -1;
1315 }
1316 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001317 }
1318
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001319 switch (ins->prefixes[PPS_VEX]) {
1320 case P_EVEX:
1321 if (!(ins->rex & REX_EV))
1322 return -1;
1323 break;
1324 case P_VEX3:
1325 case P_VEX2:
1326 if (!(ins->rex & REX_V))
1327 return -1;
1328 break;
1329 default:
1330 break;
1331 }
1332
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001333 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001335
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001337 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 return -1;
1339 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001340 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001342 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 ins->rex &= ~REX_W;
1344 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001345 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 ins->rex |= REX_W;
1347 bad32 &= ~REX_W;
1348 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001349 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 /* Follow REX_W */
1351 break;
1352 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001353
H. Peter Anvinfc561202011-07-07 16:58:22 -07001354 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001355 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001357 } else if (!(ins->rex & REX_EV) &&
1358 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001359 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001360 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001362 if (ins->rex & REX_EV)
1363 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001364 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1365 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 length += 3;
1367 else
1368 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001369 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001371 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 return -1;
1373 } else if (bits == 64) {
1374 length++;
1375 } else if ((ins->rex & REX_L) &&
1376 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001377 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001379 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001380 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 length++;
1382 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001383 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 return -1;
1385 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001386 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001387
1388 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001389 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001390 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001391 "instruction is not lockable");
1392 }
1393
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001394 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001395
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001396 /*
1397 * when BND prefix is set by DEFAULT directive,
1398 * BND prefix is added to every appropriate instruction line
1399 * unless it is overridden by NOBND prefix.
1400 */
1401 if (globalbnd &&
1402 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1403 ins->prefixes[PPS_REP] = P_BND;
1404
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001405 return length;
1406}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001407
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001408static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1409{
1410 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001411 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001412 !(ins->rex & (REX_V | REX_EV)) &&
1413 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001414 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001415 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001416 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001417 return 1;
1418 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001419 }
1420
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001421 return 0;
1422}
1423
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001424static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001425 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001426 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001427{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001428 uint8_t c;
1429 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001430 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001431 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001432 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001433 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001434 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001435 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001436 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001437
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001438 ins->rex_done = false;
1439
H. Peter Anvin839eca22007-10-29 23:12:47 -07001440 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001441 c = *codes++;
1442 op1 = (c & 3) + ((opex & 1) << 2);
1443 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1444 opx = &ins->oprs[op1];
1445 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001446
H. Peter Anvin839eca22007-10-29 23:12:47 -07001447 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 case 01:
1449 case 02:
1450 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001451 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001452 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001453 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 codes += c;
1455 offset += c;
1456 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001457
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001458 case 05:
1459 case 06:
1460 case 07:
1461 opex = c;
1462 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001463
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001464 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001465 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001466 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001467 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 offset += 1;
1469 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001470
Jin Kyu Song164d6072013-10-15 19:10:13 -07001471 case4(014):
1472 break;
1473
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001474 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001475 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001476 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001477 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001479 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 offset += 1;
1481 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001482
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001483 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001484 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001485 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001486 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001487 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 offset += 1;
1489 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001492 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001493 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001494 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001495 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 offset += 2;
1497 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001499 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 if (opx->type & (BITS16 | BITS32))
1501 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 else
1503 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001504 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001505 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001506 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001507 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 offset += size;
1509 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001510
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001511 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001512 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001513 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001514 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001515 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 offset += 4;
1517 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001518
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001519 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001520 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001521 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001522 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001523 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001524 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 offset += size;
1526 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001529 if (opx->segment != segment) {
1530 data = opx->offset;
1531 out(offset, segment, &data,
1532 OUT_REL1ADR, insn_end - offset,
1533 opx->segment, opx->wrt);
1534 } else {
1535 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 if (data > 127 || data < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001537 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001538 out(offset, segment, &data,
1539 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1540 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 offset += 1;
1542 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001543
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001545 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001546 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001547 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001548 offset += 8;
1549 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001551 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001552 if (opx->segment != segment) {
1553 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001555 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001558 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001560 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 }
1562 offset += 2;
1563 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001565 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001566 if (opx->type & (BITS16 | BITS32 | BITS64))
1567 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 else
1569 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001570 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001571 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001572 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001573 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1574 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001576 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001578 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 }
1580 offset += size;
1581 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001582
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001583 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001584 if (opx->segment != segment) {
1585 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001587 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001588 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001590 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001592 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 }
1594 offset += 4;
1595 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001596
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001597 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001598 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001599 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001600 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001601 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001602 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001603 ofmt->segbase(1 + opx->segment),
H. Peter Anvin839eca22007-10-29 23:12:47 -07001604 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001605 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case 0172:
1609 c = *codes++;
1610 opx = &ins->oprs[c >> 3];
1611 bytes[0] = nasm_regvals[opx->basereg] << 4;
1612 opx = &ins->oprs[c & 7];
1613 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001614 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 "non-absolute expression not permitted as argument %d",
1616 c & 7);
1617 } else {
1618 if (opx->offset & ~15) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001619 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 "four-bit argument exceeds bounds");
1621 }
1622 bytes[0] |= opx->offset & 15;
1623 }
1624 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1625 offset++;
1626 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case 0173:
1629 c = *codes++;
1630 opx = &ins->oprs[c >> 4];
1631 bytes[0] = nasm_regvals[opx->basereg] << 4;
1632 bytes[0] |= c & 15;
1633 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1634 offset++;
1635 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001636
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001637 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 bytes[0] = nasm_regvals[opx->basereg] << 4;
1639 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1640 offset++;
1641 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001644 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1646 (int32_t)data != (int64_t)data) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001647 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 "signed dword immediate exceeds bounds");
1649 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001650 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 opx->segment, opx->wrt);
1652 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001653 break;
1654
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001655 case4(0240):
1656 case 0250:
1657 codes += 3;
1658 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1659 EVEX_P2Z | EVEX_P2AAA, 2);
1660 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1661 bytes[0] = 0x62;
1662 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001663 bytes[1] = ((((ins->rex & 7) << 5) |
1664 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1665 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001666 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1667 ((~ins->vexreg & 15) << 3) |
1668 (1 << 2) | (ins->vex_wlp & 3);
1669 bytes[3] = ins->evex_p[2];
1670 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1671 offset += 4;
1672 break;
1673
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 case4(0260):
1675 case 0270:
1676 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001677 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1678 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1680 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1681 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001682 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1684 offset += 3;
1685 } else {
1686 bytes[0] = 0xc5;
1687 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001688 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1690 offset += 2;
1691 }
1692 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001693
H. Peter Anvine014f352012-02-25 22:35:19 -08001694 case 0271:
1695 case 0272:
1696 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001697 break;
1698
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 case4(0274):
1700 {
1701 uint64_t uv, um;
1702 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001703
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 if (ins->rex & REX_W)
1705 s = 64;
1706 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1707 s = 16;
1708 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1709 s = 32;
1710 else
1711 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001713 um = (uint64_t)2 << (s-1);
1714 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001715
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001716 if (uv > 127 && uv < (uint64_t)-128 &&
1717 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001718 /* If this wasn't explicitly byte-sized, warn as though we
1719 * had fallen through to the imm16/32/64 case.
1720 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001721 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001722 "%s value exceeds bounds",
1723 (opx->type & BITS8) ? "signed byte" :
1724 s == 16 ? "word" :
1725 s == 32 ? "dword" :
1726 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001728 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001729 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001730 out(offset, segment, &data, OUT_ADDRESS, 1,
1731 opx->segment, opx->wrt);
1732 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001733 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001734 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1735 NO_SEG);
1736 }
1737 offset += 1;
1738 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001739 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001740
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001741 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001745 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001747 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 offset += 1;
1749 } else
1750 offset += 0;
1751 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001752
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001754 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001756 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 offset += 1;
1758 } else
1759 offset += 0;
1760 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 case 0312:
1763 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764
Keith Kaniosb7a89542007-04-12 02:40:54 +00001765 case 0313:
1766 ins->rex = 0;
1767 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001768
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001769 case4(0314):
1770 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001771
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001777 case 0323:
1778 break;
1779
Keith Kaniosb7a89542007-04-12 02:40:54 +00001780 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001781 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001783
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001784 case 0325:
1785 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001786
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001787 case 0326:
1788 break;
1789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001791 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001792 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 offset += 1;
1794 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001798
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001799 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001801 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001802 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 offset += 1;
1804 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805
Keith Kanios48af1772007-08-17 07:37:52 +00001806 case 0334:
1807 if (ins->rex & REX_R) {
1808 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001809 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001810 offset += 1;
1811 }
1812 ins->rex &= ~(REX_L|REX_R);
1813 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001814
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001815 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001816 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001817
H. Peter Anvin962e3052008-08-28 17:47:16 -07001818 case 0336:
1819 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001824 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001826 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 if (size > 0)
1828 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001829 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 offset += size;
1831 }
1832 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001834 case 0341:
1835 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001836
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 case 0360:
1838 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001839
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001840 case 0361:
1841 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001842 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1843 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001845
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 case 0364:
1847 case 0365:
1848 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001849
Keith Kanios48af1772007-08-17 07:37:52 +00001850 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001852 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001853 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001854 offset += 1;
1855 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001856
Jin Kyu Song03041092013-10-15 19:38:51 -07001857 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001859
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case 0373:
1861 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001862 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 offset += 1;
1864 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001865
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001866 case 0374:
1867 eat = EA_XMMVSIB;
1868 break;
1869
1870 case 0375:
1871 eat = EA_YMMVSIB;
1872 break;
1873
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001874 case 0376:
1875 eat = EA_ZMMVSIB;
1876 break;
1877
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 case4(0100):
1879 case4(0110):
1880 case4(0120):
1881 case4(0130):
1882 case4(0200):
1883 case4(0204):
1884 case4(0210):
1885 case4(0214):
1886 case4(0220):
1887 case4(0224):
1888 case4(0230):
1889 case4(0234):
1890 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 ea ea_data;
1892 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001893 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001894 uint8_t *p;
1895 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001897
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001898 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001899 /* pick rfield from operand b (opx) */
1900 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001901 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 } else {
1903 /* rfield is constant */
1904 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001906 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001908 if (process_ea(opy, &ea_data, bits,
1909 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001910 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001911
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 p = bytes;
1913 *p++ = ea_data.modrm;
1914 if (ea_data.sib_present)
1915 *p++ = ea_data.sib;
1916
1917 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001918 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001920 /*
1921 * Make sure the address gets the right offset in case
1922 * the line breaks in the .lst file (BR 1197827)
1923 */
1924 offset += s;
1925 s = 0;
1926
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001927 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001928 /* use compressed displacement, if available */
1929 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 if (ea_data.rip) {
1932 if (opy->segment == segment) {
1933 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001934 if (overflow_signed(data, ea_data.bytes))
1935 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001936 out(offset, segment, &data, OUT_ADDRESS,
1937 ea_data.bytes, NO_SEG, NO_SEG);
1938 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001939 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001940 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001941 insn_end - offset, opy->segment, opy->wrt);
1942 }
1943 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001944 int asize = ins->addr_size >> 3;
1945 int atype = ea_data.bytes;
1946
1947 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001948 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001949 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001950 warn_overflow(ERR_PASS2, ea_data.bytes);
1951
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001952 if (asize > ea_data.bytes) {
1953 /*
1954 * If the address isn't the full width of
1955 * the address size, treat is as signed...
1956 */
1957 atype = -atype;
1958 }
1959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001960 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001961 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 }
1964 offset += s;
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{
2145 opflags_t opsize = opflags & SIZE_MASK;
2146 uint8_t brcast_num;
2147
2148 /*
2149 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2150 * this cannot be a simple arithmetic calculation.
2151 */
2152 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002153 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002154 "size of broadcasting element is greater than 64 bits");
2155
2156 switch (opsize) {
2157 case BITS64:
2158 brcast_num = BITS64 / brsize;
2159 break;
2160 default:
2161 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2162 break;
2163 }
2164
2165 return brcast_num;
2166}
2167
H. Peter Anvin65289e82009-07-25 17:25:11 -07002168static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002169 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002170{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002171 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002172 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002173 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002174
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002175 /*
2176 * Check the opcode
2177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002179 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002180
2181 /*
2182 * Count the operands
2183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002185 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002186
2187 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002188 * Is it legal?
2189 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002190 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002191 return MERR_INVALOP;
2192
2193 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002194 * {evex} available?
2195 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002196 switch (instruction->prefixes[PPS_VEX]) {
2197 case P_EVEX:
2198 if (!itemp_has(itemp, IF_EVEX))
2199 return MERR_ENCMISMATCH;
2200 break;
2201 case P_VEX3:
2202 case P_VEX2:
2203 if (!itemp_has(itemp, IF_VEX))
2204 return MERR_ENCMISMATCH;
2205 break;
2206 default:
2207 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002208 }
2209
2210 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002211 * Check that no spurious colons or TOs are present
2212 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 for (i = 0; i < itemp->operands; i++)
2214 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002215 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002217 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002218 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002219 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 switch (itemp_smask(itemp)) {
2221 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 asize = BITS8;
2223 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002224 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = BITS16;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 asize = BITS32;
2229 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002230 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 asize = BITS64;
2232 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002233 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 asize = BITS128;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002237 asize = BITS256;
2238 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002239 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002240 asize = BITS512;
2241 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 switch (bits) {
2244 case 16:
2245 asize = BITS16;
2246 break;
2247 case 32:
2248 asize = BITS32;
2249 break;
2250 case 64:
2251 asize = BITS64;
2252 break;
2253 default:
2254 asize = 0;
2255 break;
2256 }
2257 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002258 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002259 asize = 0;
2260 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002261 }
2262
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002263 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002264 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002265 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002266 memset(size, 0, sizeof size);
2267 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002268 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 /* S- flags apply to all operands */
2270 for (i = 0; i < MAX_OPERANDS; i++)
2271 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002272 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002273
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002274 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002275 * Check that the operand flags all match up,
2276 * it's a bit tricky so lets be verbose:
2277 *
2278 * 1) Find out the size of operand. If instruction
2279 * doesn't have one specified -- we're trying to
2280 * guess it either from template (IF_S* flag) or
2281 * from code bits.
2282 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002283 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002284 * template has an operand size specified AND this size differ
2285 * from which instruction has (perhaps we got it from code bits)
2286 * we are:
2287 * a) Check that only size of instruction and operand is differ
2288 * other characteristics do match
2289 * b) Perhaps it's a register specified in instruction so
2290 * for such a case we just mark that operand as "size
2291 * missing" and this will turn on fuzzy operand size
2292 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002293 */
2294 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002295 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002296 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002297 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002298 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002299 opflags_t template_opsize, insn_opsize;
2300
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002301 if (!(type & SIZE_MASK))
2302 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002303
Jin Kyu Song7903c072013-10-30 03:00:12 -07002304 insn_opsize = type & SIZE_MASK;
2305 if (!is_broadcast) {
2306 template_opsize = itemp->opd[i] & SIZE_MASK;
2307 } else {
2308 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2309 /*
2310 * when broadcasting, the element size depends on
2311 * the instruction type. decorator flag should match.
2312 */
2313
2314 if (deco_brsize) {
2315 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002316 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002317 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002318 } else {
2319 template_opsize = 0;
2320 }
2321 }
2322
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002323 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002324 (deco & ~itemp->deco[i] & ~BRNUM_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 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002488 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2489 input->segment == NO_SEG) {
2490 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2491 input->type &= ~IP_REL;
2492 input->type |= MEMORY;
2493 }
2494
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002495 if (bits == 64 &&
2496 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2497 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2498 return -1;
2499 }
2500
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002501 if (eaflags & EAF_BYTEOFFS ||
2502 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002503 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2504 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2505 }
2506
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002507 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002508 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002509 output->sib = GEN_SIB(0, 4, 5);
2510 output->bytes = 4;
2511 output->modrm = GEN_MODRM(0, rfield, 4);
2512 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002513 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002514 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002515 output->bytes = (addrbits != 16 ? 4 : 2);
2516 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2517 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002518 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002519 } else {
2520 /*
2521 * It's an indirection.
2522 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002524 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 int t, it, bt; /* register numbers */
2527 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 if (s == 0)
2530 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002531
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002532 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002533 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 ix = nasm_reg_flags[i];
2535 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002536 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002537 ix = 0;
2538 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002539
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002540 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002541 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 bx = nasm_reg_flags[b];
2543 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002544 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 bx = 0;
2546 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002547
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002548 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002549 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002550 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002551 int32_t o = input->offset;
2552 int mod, scale, index, base;
2553
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002554 /*
2555 * For a vector SIB, one has to be a vector and the other,
2556 * if present, a GPR. The vector must be the index operand.
2557 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002558 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 if (s == 0)
2560 s = 1;
2561 else if (s != 1)
2562 goto err;
2563
2564 t = bt, bt = it, it = t;
2565 x = bx, bx = ix, ix = x;
2566 }
2567
2568 if (bt != -1) {
2569 if (REG_GPR & ~bx)
2570 goto err;
2571 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2572 sok &= bx;
2573 else
2574 goto err;
2575 }
2576
2577 /*
2578 * While we're here, ensure the user didn't specify
2579 * WORD or QWORD
2580 */
2581 if (input->disp_size == 16 || input->disp_size == 64)
2582 goto err;
2583
2584 if (addrbits == 16 ||
2585 (addrbits == 32 && !(sok & BITS32)) ||
2586 (addrbits == 64 && !(sok & BITS64)))
2587 goto err;
2588
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002589 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2590 : ((ix & YMMREG & ~REG_EA)
2591 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002592
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002593 output->rex |= rexflags(it, ix, REX_X);
2594 output->rex |= rexflags(bt, bx, REX_B);
2595 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002596
2597 index = it & 7; /* it is known to be != -1 */
2598
2599 switch (s) {
2600 case 1:
2601 scale = 0;
2602 break;
2603 case 2:
2604 scale = 1;
2605 break;
2606 case 4:
2607 scale = 2;
2608 break;
2609 case 8:
2610 scale = 3;
2611 break;
2612 default: /* then what the smeg is it? */
2613 goto err; /* panic */
2614 }
2615
2616 if (bt == -1) {
2617 base = 5;
2618 mod = 0;
2619 } else {
2620 base = (bt & 7);
2621 if (base != REG_NUM_EBP && o == 0 &&
2622 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002623 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002625 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 mod = 1;
2627 else
2628 mod = 2;
2629 }
2630
2631 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002632 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2633 output->modrm = GEN_MODRM(mod, rfield, 4);
2634 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002635 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002636 /*
2637 * it must be a 32/64-bit memory reference. Firstly we have
2638 * to check that all registers involved are type E/Rxx.
2639 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002640 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002641 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002642
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002643 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2645 sok &= ix;
2646 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002647 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002648 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 if (bt != -1) {
2651 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002654 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 sok &= bx;
2656 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 /*
2659 * While we're here, ensure the user didn't specify
2660 * WORD or QWORD
2661 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002662 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002663 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 if (addrbits == 16 ||
2666 (addrbits == 32 && !(sok & BITS32)) ||
2667 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002669
Keith Kaniosb7a89542007-04-12 02:40:54 +00002670 /* now reorganize base/index */
2671 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 ((hb == b && ht == EAH_NOTBASE) ||
2673 (hb == i && ht == EAH_MAKEBASE))) {
2674 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002675 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 x = bx, bx = ix, ix = x;
2677 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002678
Jin Kyu Song164d6072013-10-15 19:10:13 -07002679 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002681 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002683 if (eaflags & EAF_MIB) {
2684 /* only for mib operands */
2685 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2686 /*
2687 * make a single reg index [reg*1].
2688 * gas uses this form for an explicit index register.
2689 */
2690 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2691 }
2692 if ((ht == EAH_SUMMED) && bt == -1) {
2693 /* separate once summed index into [base, index] */
2694 bt = it, bx = ix, s--;
2695 }
2696 } else {
2697 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002698 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002699 s == 3 || s == 5 || s == 9) && bt == -1) {
2700 /* convert 3*EAX to EAX+2*EAX */
2701 bt = it, bx = ix, s--;
2702 }
2703 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002704 (eaflags & EAF_TIMESTWO) &&
2705 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002706 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002707 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002708 * to sib format with 0x0 displacement - [EAX*1+0].
2709 */
2710 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2711 }
2712 }
Keith Kanios48af1772007-08-17 07:37:52 +00002713 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002714 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002715 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002716 x = ix, ix = bx, bx = x;
2717 }
2718 if (it == REG_NUM_ESP ||
2719 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002720 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002721
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002722 output->rex |= rexflags(it, ix, REX_X);
2723 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002724
Keith Kanios48af1772007-08-17 07:37:52 +00002725 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002728
Keith Kaniosb7a89542007-04-12 02:40:54 +00002729 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002732 } else {
2733 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002734 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002735 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002736 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002738 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 mod = 1;
2740 else
2741 mod = 2;
2742 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002743
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002744 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002745 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2746 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002747 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002748 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002750
Keith Kaniosb7a89542007-04-12 02:40:54 +00002751 if (it == -1)
2752 index = 4, s = 1;
2753 else
2754 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 switch (s) {
2757 case 1:
2758 scale = 0;
2759 break;
2760 case 2:
2761 scale = 1;
2762 break;
2763 case 4:
2764 scale = 2;
2765 break;
2766 case 8:
2767 scale = 3;
2768 break;
2769 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002770 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002772
Keith Kaniosb7a89542007-04-12 02:40:54 +00002773 if (bt == -1) {
2774 base = 5;
2775 mod = 0;
2776 } else {
2777 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002778 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002779 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002780 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002781 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002782 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002783 mod = 1;
2784 else
2785 mod = 2;
2786 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002787
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002788 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002789 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2790 output->modrm = GEN_MODRM(mod, rfield, 4);
2791 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 }
2793 } else { /* it's 16-bit */
2794 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002795 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002796
Keith Kaniosb7a89542007-04-12 02:40:54 +00002797 /* check for 64-bit long mode */
2798 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002799 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002802 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2803 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002804 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002805
Keith Kaniosb7a89542007-04-12 02:40:54 +00002806 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002807 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002808 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002811 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 if (b == -1 && i != -1) {
2813 int tmp = b;
2814 b = i;
2815 i = tmp;
2816 } /* swap */
2817 if ((b == R_SI || b == R_DI) && i != -1) {
2818 int tmp = b;
2819 b = i;
2820 i = tmp;
2821 }
2822 /* have BX/BP as base, SI/DI index */
2823 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002824 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 if (i != -1 && b != -1 &&
2826 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002827 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002828 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002829 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002830
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 rm = -1;
2832 if (i != -1)
2833 switch (i * 256 + b) {
2834 case R_SI * 256 + R_BX:
2835 rm = 0;
2836 break;
2837 case R_DI * 256 + R_BX:
2838 rm = 1;
2839 break;
2840 case R_SI * 256 + R_BP:
2841 rm = 2;
2842 break;
2843 case R_DI * 256 + R_BP:
2844 rm = 3;
2845 break;
2846 } else
2847 switch (b) {
2848 case R_SI:
2849 rm = 4;
2850 break;
2851 case R_DI:
2852 rm = 5;
2853 break;
2854 case R_BP:
2855 rm = 6;
2856 break;
2857 case R_BX:
2858 rm = 7;
2859 break;
2860 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002861 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002862 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002863
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002864 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002865 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002867 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 mod = 1;
2869 else
2870 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002871
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002872 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002873 output->bytes = mod; /* bytes of offset needed */
2874 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 }
2876 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002877 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002878
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002879 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002880 return output->type;
2881
2882err:
2883 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884}
2885
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002886static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002887{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002888 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002889 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002890
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002891 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002892
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 switch (ins->prefixes[PPS_ASIZE]) {
2894 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 valid &= 16;
2896 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 valid &= 32;
2899 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002900 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 valid &= 64;
2902 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002903 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002904 valid &= (addrbits == 32) ? 16 : 32;
2905 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002906 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 }
2909
2910 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (is_class(MEMORY, ins->oprs[j].type)) {
2912 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002915 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 i = 0;
2917 else
2918 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002920 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002921 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 b = 0;
2923 else
2924 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002925
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002926 if (ins->oprs[j].scale == 0)
2927 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002929 if (!i && !b) {
2930 int ds = ins->oprs[j].disp_size;
2931 if ((addrbits != 64 && ds > 8) ||
2932 (addrbits == 64 && ds == 16))
2933 valid &= ds;
2934 } else {
2935 if (!(REG16 & ~b))
2936 valid &= 16;
2937 if (!(REG32 & ~b))
2938 valid &= 32;
2939 if (!(REG64 & ~b))
2940 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 if (!(REG16 & ~i))
2943 valid &= 16;
2944 if (!(REG32 & ~i))
2945 valid &= 32;
2946 if (!(REG64 & ~i))
2947 valid &= 64;
2948 }
2949 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002950 }
2951
2952 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002953 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002954 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002955 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002956 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002958 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002959 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002960 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002961 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002962 }
2963
2964 defdisp = ins->addr_size == 16 ? 16 : 32;
2965
2966 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002967 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2968 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2969 /*
2970 * mem_offs sizes must match the address size; if not,
2971 * strip the MEM_OFFS bit and match only EA instructions
2972 */
2973 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2974 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002975 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002976}