blob: a9f6eb027a8a5ec6a62716dd87d1974938090394 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvinfea84d72010-05-06 15:32:20 -07003 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040039 * \1..\4 - that many literal bytes follow in the code stream
H. Peter Anvindcffe4b2008-10-10 22:10:31 -070040 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070043 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
45 * \14..\17 - a signed byte immediate operand, from operand 0..3
46 * \20..\23 - a byte immediate operand, from operand 0..3
47 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
48 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
H. Peter Anvin3ba46772002-05-27 23:19:35 +000050 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070051 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040053 * depending on the address size of the instruction.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070054 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
H. Peter Anvin17799b42002-05-21 03:31:21 +000058 * assembly mode or the operand-size override on the operand
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070059 * \70..\73 - a long relative operand, from operand 0..3
H. Peter Anvinc1377e92008-10-06 23:40:31 -070060 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
H. Peter Anvin7eb4a382007-09-17 15:49:30 -070063 * \140..\143 - an immediate word or signed byte for operand 0..3
H. Peter Anvina30cc072007-11-18 21:55:26 -080064 * \144..\147 - or 2 (s-field) into opcode byte if operand 0..3
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040065 * is a signed byte rather than a word. Opcode byte follows.
H. Peter Anvinc1377e92008-10-06 23:40:31 -070066 * \150..\153 - an immediate dword or signed byte for operand 0..3
H. Peter Anvina30cc072007-11-18 21:55:26 -080067 * \154..\157 - or 2 (s-field) into opcode byte if operand 0..3
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040068 * is a signed byte rather than a dword. Opcode byte follows.
H. Peter Anvin401c07e2007-09-17 16:55:04 -070069 * \160..\163 - this instruction uses DREX rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040070 * OC0 field set to 0, and the dest field taken from
H. Peter Anvin401c07e2007-09-17 16:55:04 -070071 * operand 0..3.
72 * \164..\167 - this instruction uses DREX rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040073 * OC0 field set to 1, and the dest field taken from
H. Peter Anvin401c07e2007-09-17 16:55:04 -070074 * operand 0..3.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040075 * \171 - placement of DREX suffix in the absence of an EA
76 * \172\ab - the register number from operand a in bits 7..4, with
H. Peter Anvin52dc3532008-05-20 19:29:04 -070077 * the 4-bit immediate from operand b in bits 3..0.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040078 * \173\xab - the register number from operand a in bits 7..4, with
79 * the value b in bits 3..0.
80 * \174\a - the register number from operand a in bits 7..4, and
81 * an arbitrary value in bits 3..0 (assembled as zero.)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000082 * \2ab - a ModRM, calculated on EA in operand a, with the spare
83 * field equal to digit b.
H. Peter Anvin32cd4c22008-04-04 13:34:53 -070084 * \250..\253 - same as \150..\153, except warn if the 64-bit operand
85 * is not equal to the truncated and sign-extended 32-bit
86 * operand; used for 32-bit immediates in 64-bit mode.
H. Peter Anvin588df782008-10-07 10:05:10 -070087 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
H. Peter Anvina04019c2009-05-03 21:42:34 -070088 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +040089 * V field taken from operand 0..3.
90 * \270 - this instruction uses VEX/XOP rather than REX, with the
91 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -070092 *
H. Peter Anvina04019c2009-05-03 21:42:34 -070093 * VEX/XOP prefixes are followed by the sequence:
94 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -070095 * 00 wwl lpp
96 * [l0] ll = 0 for L = 0 (.128, .lz)
97 * [l1] ll = 1 for L = 1 (.256)
98 * [lig] ll = 2 for L don't care (always assembled as 0)
99 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700100 * [w0] ww = 0 for W = 0
101 * [w1 ] ww = 1 for W = 1
102 * [wig] ww = 2 for W don't care (always assembled as 0)
103 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700104 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700105 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700106 *
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700107 * \274..\277 - a signed byte immediate operand, from operand 0..3,
108 * which is to be extended to the operand size.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000109 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
110 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
H. Peter Anvind28f07f2009-06-26 16:18:00 -0700111 * \312 - (disassembler only) invalid with non-default address size.
H. Peter Anvince2b3972007-05-30 22:21:11 +0000112 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
H. Peter Anvin23440102007-11-12 21:02:33 -0800113 * \314 - (disassembler only) invalid with REX.B
114 * \315 - (disassembler only) invalid with REX.X
115 * \316 - (disassembler only) invalid with REX.R
116 * \317 - (disassembler only) invalid with REX.W
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
118 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
119 * \322 - indicates that this instruction is only valid when the
120 * operand size is the default (instruction to disassembler,
121 * generates no code in the assembler)
H. Peter Anvince2b3972007-05-30 22:21:11 +0000122 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 * \324 - indicates 64-bit operand size requiring REX prefix.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400124 * \325 - instruction which always uses spl/bpl/sil/dil
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125 * \330 - a literal byte follows in the code stream, to be added
126 * to the condition code value of the instruction.
Keith Kanios48af1772007-08-17 07:37:52 +0000127 * \331 - instruction not valid with REP prefix. Hint for
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000128 * disassembler only; for SSE instructions.
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700129 * \332 - REP prefix (0xF2 byte) used as opcode extension.
130 * \333 - REP prefix (0xF3 byte) used as opcode extension.
H. Peter Anvin9472dab2009-06-24 21:38:29 -0700131 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
H. Peter Anvincb9b6902007-09-12 21:58:51 -0700132 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700133 * \336 - force a REP(E) prefix (0xF2) even if not specified.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400134 * \337 - force a REPNE prefix (0xF3) even if not specified.
H. Peter Anvin962e3052008-08-28 17:47:16 -0700135 * \336-\337 are still listed as prefixes in the disassembler.
Keith Kaniosb7a89542007-04-12 02:40:54 +0000136 * \340 - reserve <operand 0> bytes of uninitialized storage.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000137 * Operand 0 had better be a segmentless constant.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400138 * \341 - this instruction needs a WAIT "prefix"
H. Peter Anvinff6e12d2008-10-08 21:17:32 -0700139 * \344,\345 - the PUSH/POP (respectively) codes for CS, DS, ES, SS
140 * (POP is never used for CS) depending on operand 0
141 * \346,\347 - the second byte of PUSH/POP codes for FS, GS, depending
142 * on operand 0
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400143 * \360 - no SSE prefix (== \364\331)
H. Peter Anvinfff5a472008-05-20 09:46:24 -0700144 * \361 - 66 SSE prefix (== \366\331)
145 * \362 - F2 SSE prefix (== \364\332)
146 * \363 - F3 SSE prefix (== \364\333)
H. Peter Anvin62cb6062007-09-11 22:44:03 +0000147 * \364 - operand-size prefix (0x66) not permitted
148 * \365 - address-size prefix (0x67) not permitted
149 * \366 - operand-size prefix (0x66) used as opcode extension
150 * \367 - address-size prefix (0x67) used as opcode extension
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000151 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400152 * 370 is used for Jcc, 371 is used for JMP.
153 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
154 * used for conditional jump over longer jump
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 */
156
H. Peter Anvinfe501952007-10-02 21:53:51 -0700157#include "compiler.h"
158
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000159#include <stdio.h>
160#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000161#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000162
163#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000164#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165#include "assemble.h"
166#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700167#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000168
H. Peter Anvin65289e82009-07-25 17:25:11 -0700169enum match_result {
170 /*
171 * Matching errors. These should be sorted so that more specific
172 * errors come later in the sequence.
173 */
174 MERR_INVALOP,
175 MERR_OPSIZEMISSING,
176 MERR_OPSIZEMISMATCH,
177 MERR_BADCPU,
178 MERR_BADMODE,
179 /*
180 * Matching success; the conditional ones first
181 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400182 MOK_JUMP, /* Matching OK but needs jmp_match() */
183 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700184};
185
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186typedef struct {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000187 int sib_present; /* is a SIB byte necessary? */
188 int bytes; /* # of bytes of offset needed */
189 int size; /* lazy - this is sib+bytes+1 */
190 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191} ea;
192
Keith Kaniosb7a89542007-04-12 02:40:54 +0000193static uint32_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000194static efunc errfunc;
195static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000196static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000197
H. Peter Anvin3720f7b2008-05-12 11:00:50 -0700198static int64_t calcsize(int32_t, int64_t, int, insn *, const uint8_t *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700199static void gencode(int32_t segment, int64_t offset, int bits,
200 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400201 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700202static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400203 insn *instruction,
204 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700205static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700206static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000207static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700208static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000209static int op_rexflags(const operand *, int);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700210static ea *process_ea(operand *, ea *, int, int, int, opflags_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700211static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000212
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700213static int has_prefix(insn * ins, enum prefix_pos pos, enum prefixes prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000214{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700215 return ins->prefixes[pos] == prefix;
216}
217
218static void assert_no_prefix(insn * ins, enum prefix_pos pos)
219{
220 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400221 errfunc(ERR_NONFATAL, "invalid %s prefix",
222 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700223}
224
225static const char *size_name(int size)
226{
227 switch (size) {
228 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400229 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700230 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400231 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700232 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400233 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700234 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400235 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700236 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700238 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400239 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700240 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400241 return "yword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700242 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400243 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000244 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245}
246
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400247static void warn_overflow(int pass, int size)
248{
249 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
250 "%s data exceeds bounds", size_name(size));
251}
252
253static void warn_overflow_const(int64_t data, int size)
254{
255 if (overflow_general(data, size))
256 warn_overflow(ERR_PASS1, size);
257}
258
259static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700260{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100261 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400262 if (overflow_general(o->offset, size))
263 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264 }
265}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400266
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000267/*
268 * This routine wrappers the real output format's output routine,
269 * in order to pass a copy of the data off to the listing file
270 * generator at the same time.
271 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800272static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800273 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000275{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000276 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000277 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800278 uint8_t p[8];
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800280 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 /*
282 * This is a non-relocated address, and we're going to
283 * convert it into RAWDATA format.
284 */
285 uint8_t *q = p;
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800286
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 if (size > 8) {
288 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
289 return;
290 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700291
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400292 WRITEADDR(q, *(int64_t *)data, size);
293 data = p;
294 type = OUT_RAWDATA;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000295 }
296
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800297 list->output(offset, data, type, size);
298
Frank Kotlerabebb082003-09-06 04:45:37 +0000299 /*
300 * this call to src_get determines when we call the
301 * debug-format-specific "linenum" function
302 * it updates lineno and lnfname to the current values
303 * returning 0 if "same as last time", -2 if lnfname
304 * changed, and the amount by which lineno changed,
305 * if it did. thus, these variables must be static
306 */
307
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400308 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800311 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000312}
313
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700314static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin3720f7b2008-05-12 11:00:50 -0700315 insn * ins, const uint8_t *code)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800317 int64_t isize;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000318 uint8_t c = code[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000319
Charles Craynef1aefd82008-09-30 16:11:32 -0700320 if ((c != 0370 && c != 0371) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700321 return false;
322 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400323 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700324 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400325 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327 isize = calcsize(segment, offset, bits, ins, code);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100328
Victor van den Elzen154e5922009-02-25 17:32:00 +0100329 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100330 /* Be optimistic in pass 1 */
331 return true;
332
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700334 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000335
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700336 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
337 return (isize >= -128 && isize <= 127); /* is it byte size? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000339
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800340int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400341 insn * instruction, struct ofmt *output, efunc error,
342 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000344 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700346 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800347 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000348 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800349 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300350 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000353 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 outfmt = output; /* likewise */
355 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300357 wsize = idata_bytes(instruction->opcode);
358 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360
H. Peter Anvineba20a72002-04-30 20:53:55 +0000361 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000362 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000363 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 if (t < 0)
365 errfunc(ERR_PANIC,
366 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000367
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400369 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400371 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700372 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400373 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000374 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700375 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800377 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400378 offset += wsize;
379 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700380 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400381 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000383
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800385 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000387
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 if (align) {
389 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100390 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800391 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 }
393 offset += e->stringlen + align;
394 }
395 }
396 if (t > 0 && t == instruction->times - 1) {
397 /*
398 * Dummy call to list->output to give the offset to the
399 * listing module.
400 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800401 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 list->uplevel(LIST_TIMES);
403 }
404 }
405 if (instruction->times > 1)
406 list->downlevel(LIST_TIMES);
407 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000408 }
409
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700411 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000413
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400414 fp = fopen(fname, "rb");
415 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
417 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400418 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
420 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400421 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700422 static char buf[4096];
423 size_t t = instruction->times;
424 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400425 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 len = ftell(fp);
428 if (instruction->eops->next) {
429 base = instruction->eops->next->offset;
430 len -= base;
431 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700432 len > (size_t)instruction->eops->next->next->offset)
433 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 }
435 /*
436 * Dummy call to list->output to give the offset to the
437 * listing module.
438 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800439 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 list->uplevel(LIST_INCBIN);
441 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700442 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000443
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 fseek(fp, base, SEEK_SET);
445 l = len;
446 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700447 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400448 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449 if (!m) {
450 /*
451 * This shouldn't happen unless the file
452 * actually changes while we are reading
453 * it.
454 */
455 error(ERR_NONFATAL,
456 "`incbin': unexpected EOF while"
457 " reading file `%s'", fname);
458 t = 0; /* Try to exit cleanly */
459 break;
460 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800461 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 NO_SEG, NO_SEG);
463 l -= m;
464 }
465 }
466 list->downlevel(LIST_INCBIN);
467 if (instruction->times > 1) {
468 /*
469 * Dummy call to list->output to give the offset to the
470 * listing module.
471 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800472 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 list->uplevel(LIST_TIMES);
474 list->downlevel(LIST_TIMES);
475 }
476 fclose(fp);
477 return instruction->times * len;
478 }
479 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480 }
481
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700482 /* Check to see if we need an address-size prefix */
483 add_asp(instruction, bits);
484
H. Peter Anvin23595f52009-07-25 17:44:25 -0700485 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700486
H. Peter Anvin23595f52009-07-25 17:44:25 -0700487 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400488 /* Matches! */
489 int64_t insn_size = calcsize(segment, offset, bits,
490 instruction, temp->code);
491 itimes = instruction->times;
492 if (insn_size < 0) /* shouldn't be, on pass two */
493 error(ERR_PANIC, "errors made it through from pass one");
494 else
495 while (itimes--) {
496 for (j = 0; j < MAXPREFIX; j++) {
497 uint8_t c = 0;
498 switch (instruction->prefixes[j]) {
499 case P_WAIT:
500 c = 0x9B;
501 break;
502 case P_LOCK:
503 c = 0xF0;
504 break;
505 case P_REPNE:
506 case P_REPNZ:
507 c = 0xF2;
508 break;
509 case P_REPE:
510 case P_REPZ:
511 case P_REP:
512 c = 0xF3;
513 break;
514 case R_CS:
515 if (bits == 64) {
516 error(ERR_WARNING | ERR_PASS2,
517 "cs segment base generated, but will be ignored in 64-bit mode");
518 }
519 c = 0x2E;
520 break;
521 case R_DS:
522 if (bits == 64) {
523 error(ERR_WARNING | ERR_PASS2,
524 "ds segment base generated, but will be ignored in 64-bit mode");
525 }
526 c = 0x3E;
527 break;
528 case R_ES:
529 if (bits == 64) {
530 error(ERR_WARNING | ERR_PASS2,
531 "es segment base generated, but will be ignored in 64-bit mode");
532 }
533 c = 0x26;
534 break;
535 case R_FS:
536 c = 0x64;
537 break;
538 case R_GS:
539 c = 0x65;
540 break;
541 case R_SS:
542 if (bits == 64) {
543 error(ERR_WARNING | ERR_PASS2,
544 "ss segment base generated, but will be ignored in 64-bit mode");
545 }
546 c = 0x36;
547 break;
548 case R_SEGR6:
549 case R_SEGR7:
550 error(ERR_NONFATAL,
551 "segr6 and segr7 cannot be used as prefixes");
552 break;
553 case P_A16:
554 if (bits == 64) {
555 error(ERR_NONFATAL,
556 "16-bit addressing is not supported "
557 "in 64-bit mode");
558 } else if (bits != 16)
559 c = 0x67;
560 break;
561 case P_A32:
562 if (bits != 32)
563 c = 0x67;
564 break;
565 case P_A64:
566 if (bits != 64) {
567 error(ERR_NONFATAL,
568 "64-bit addressing is only supported "
569 "in 64-bit mode");
570 }
571 break;
572 case P_ASP:
573 c = 0x67;
574 break;
575 case P_O16:
576 if (bits != 16)
577 c = 0x66;
578 break;
579 case P_O32:
580 if (bits == 16)
581 c = 0x66;
582 break;
583 case P_O64:
584 /* REX.W */
585 break;
586 case P_OSP:
587 c = 0x66;
588 break;
589 case P_none:
590 break;
591 default:
592 error(ERR_PANIC, "invalid instruction prefix");
593 }
594 if (c != 0) {
595 out(offset, segment, &c, OUT_RAWDATA, 1,
596 NO_SEG, NO_SEG);
597 offset++;
598 }
599 }
600 insn_end = offset + insn_size;
601 gencode(segment, offset, bits, instruction,
602 temp, insn_end);
603 offset += insn_size;
604 if (itimes > 0 && itimes == instruction->times - 1) {
605 /*
606 * Dummy call to list->output to give the offset to the
607 * listing module.
608 */
609 list->output(offset, NULL, OUT_RAWDATA, 0);
610 list->uplevel(LIST_TIMES);
611 }
612 }
613 if (instruction->times > 1)
614 list->downlevel(LIST_TIMES);
615 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700616 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400617 /* No match */
618 switch (m) {
619 case MERR_OPSIZEMISSING:
620 error(ERR_NONFATAL, "operation size not specified");
621 break;
622 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400624 break;
625 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400627 break;
628 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800629 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400630 bits);
631 break;
632 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 error(ERR_NONFATAL,
634 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400635 break;
636 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000637 }
638 return 0;
639}
640
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800641int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400642 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000643{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000644 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700645 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000646
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000648 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000649
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400650 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000652
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700653 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
654 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400655 instruction->opcode == I_DT || instruction->opcode == I_DO ||
656 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300658 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300661 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000662
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400663 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000664 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000665
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400667 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400669 warn_overflow_const(e->offset, wsize);
670 } else if (e->type == EOT_DB_STRING ||
671 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000673
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 align = (-osize) % wsize;
675 if (align < 0)
676 align += wsize;
677 isize += osize + align;
678 }
679 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000680 }
681
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400683 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300685 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700686 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400688 fp = fopen(fname, "rb");
689 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
691 fname);
692 else if (fseek(fp, 0L, SEEK_END) < 0)
693 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
694 fname);
695 else {
696 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 if (instruction->eops->next) {
698 len -= instruction->eops->next->offset;
699 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700700 len > (size_t)instruction->eops->next->next->offset) {
701 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 }
703 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300704 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300706 if (fp)
707 fclose(fp);
708 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000709 }
710
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700711 /* Check to see if we need an address-size prefix */
712 add_asp(instruction, bits);
713
H. Peter Anvin23595f52009-07-25 17:44:25 -0700714 m = find_match(&temp, instruction, segment, offset, bits);
715 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400716 /* we've matched an instruction. */
717 int64_t isize;
718 const uint8_t *codes = temp->code;
719 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400721 isize = calcsize(segment, offset, bits, instruction, codes);
722 if (isize < 0)
723 return -1;
724 for (j = 0; j < MAXPREFIX; j++) {
725 switch (instruction->prefixes[j]) {
726 case P_A16:
727 if (bits != 16)
728 isize++;
729 break;
730 case P_A32:
731 if (bits != 32)
732 isize++;
733 break;
734 case P_O16:
735 if (bits != 16)
736 isize++;
737 break;
738 case P_O32:
739 if (bits == 16)
740 isize++;
741 break;
742 case P_A64:
743 case P_O64:
744 case P_none:
745 break;
746 default:
747 isize++;
748 break;
749 }
750 }
751 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700752 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400753 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000754 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000755}
756
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700757static bool possible_sbyte(operand *o)
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000758{
H. Peter Anvinad6b8592008-10-07 09:56:38 -0700759 return o->wrt == NO_SEG && o->segment == NO_SEG &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400760 !(o->opflags & OPFLAG_UNKNOWN) &&
761 optimizing >= 0 && !(o->type & STRICT);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000762}
763
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700764/* check that opn[op] is a signed byte of size 16 or 32 */
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700765static bool is_sbyte16(operand *o)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700766{
767 int16_t v;
768
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700769 if (!possible_sbyte(o))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400770 return false;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700771
H. Peter Anvin1c3277b2008-07-19 21:38:56 -0700772 v = o->offset;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700773 return v >= -128 && v <= 127;
774}
775
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700776static bool is_sbyte32(operand *o)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700777{
778 int32_t v;
779
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700780 if (!possible_sbyte(o))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400781 return false;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700782
H. Peter Anvin1c3277b2008-07-19 21:38:56 -0700783 v = o->offset;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700784 return v >= -128 && v <= 127;
785}
786
H. Peter Anvin507ae032008-10-09 15:37:10 -0700787/* Common construct */
788#define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
789
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800790static int64_t calcsize(int32_t segment, int64_t offset, int bits,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400791 insn * ins, const uint8_t *codes)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792{
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800793 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000794 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000795 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700796 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700797 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700798 uint8_t opex = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000799
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700800 ins->rex = 0; /* Ensure REX is reset */
801
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700802 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400803 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700804
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 (void)segment; /* Don't warn that this parameter is unused */
806 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000807
H. Peter Anvin839eca22007-10-29 23:12:47 -0700808 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400809 c = *codes++;
810 op1 = (c & 3) + ((opex & 1) << 2);
811 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
812 opx = &ins->oprs[op1];
813 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700814
H. Peter Anvin839eca22007-10-29 23:12:47 -0700815 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 case 01:
817 case 02:
818 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400819 case 04:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 codes += c, length += c;
821 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400823 case 05:
824 case 06:
825 case 07:
826 opex = c;
827 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700828
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400829 case4(010):
830 ins->rex |=
831 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 codes++, length++;
833 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700834
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400835 case4(014):
836 case4(020):
837 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 length++;
839 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700840
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400841 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 length += 2;
843 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700844
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400845 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700846 if (opx->type & (BITS16 | BITS32 | BITS64))
847 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 else
849 length += (bits == 16) ? 2 : 4;
850 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700851
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400852 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 length += 4;
854 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700855
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400856 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700857 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400860 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 length++;
862 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400864 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000865 length += 8; /* MOV reg64/imm */
866 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400868 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 length += 2;
870 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700871
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400872 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700873 if (opx->type & (BITS16 | BITS32 | BITS64))
874 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 else
876 length += (bits == 16) ? 2 : 4;
877 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 length += 4;
881 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400883 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700884 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700886
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400887 case4(0140):
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700888 length += is_sbyte16(opx) ? 1 : 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700890
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400891 case4(0144):
H. Peter Anvina30cc072007-11-18 21:55:26 -0800892 codes++;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 length++;
894 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400896 case4(0150):
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700897 length += is_sbyte32(opx) ? 1 : 4;
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700898 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700899
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400900 case4(0154):
H. Peter Anvina30cc072007-11-18 21:55:26 -0800901 codes++;
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700902 length++;
903 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400905 case4(0160):
906 length++;
907 ins->rex |= REX_D;
908 ins->drexdst = regval(opx);
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700909 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400911 case4(0164):
912 length++;
913 ins->rex |= REX_D|REX_OC;
914 ins->drexdst = regval(opx);
915 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400917 case 0171:
918 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400920 case 0172:
921 case 0173:
922 case 0174:
923 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700924 length++;
925 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 case4(0250):
928 length += is_sbyte32(opx) ? 1 : 4;
929 break;
930
931 case4(0254):
932 length += 4;
933 break;
934
935 case4(0260):
936 ins->rex |= REX_V;
937 ins->drexdst = regval(opx);
938 ins->vex_cm = *codes++;
939 ins->vex_wlp = *codes++;
940 break;
941
942 case 0270:
943 ins->rex |= REX_V;
944 ins->drexdst = 0;
945 ins->vex_cm = *codes++;
946 ins->vex_wlp = *codes++;
947 break;
948
949 case4(0274):
950 length++;
951 break;
952
953 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 if (bits == 64)
958 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700959 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700963 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -0700967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Keith Kaniosb7a89542007-04-12 02:40:54 +0000969 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
971 has_prefix(ins, PPS_ASIZE, P_A32))
972 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400975 case4(0314):
976 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000979 {
980 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
981 if (pfx == P_O16)
982 break;
983 if (pfx != P_none)
984 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
985 else
986 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000988 }
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +0000991 {
992 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
993 if (pfx == P_O32)
994 break;
995 if (pfx != P_none)
996 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
997 else
998 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001000 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 case 0322:
1003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
Keith Kaniosb7a89542007-04-12 02:40:54 +00001005 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001006 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
Keith Kaniosb7a89542007-04-12 02:40:54 +00001009 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 case 0325:
1014 ins->rex |= REX_NH;
1015 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 case 0330:
1018 codes++, length++;
1019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001023
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001024 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 case 0333:
1026 length++;
1027 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001028
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001029 case 0334:
1030 ins->rex |= REX_L;
1031 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001032
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001033 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 case 0336:
1037 if (!ins->prefixes[PPS_LREP])
1038 ins->prefixes[PPS_LREP] = P_REP;
1039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001041 case 0337:
1042 if (!ins->prefixes[PPS_LREP])
1043 ins->prefixes[PPS_LREP] = P_REPNE;
1044 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 if (ins->oprs[0].segment != NO_SEG)
1048 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1049 " quantity of BSS space");
1050 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001051 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001053
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001054 case 0341:
1055 if (!ins->prefixes[PPS_WAIT])
1056 ins->prefixes[PPS_WAIT] = P_WAIT;
1057 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001058
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001059 case4(0344):
H. Peter Anvinff6e12d2008-10-08 21:17:32 -07001060 length++;
1061 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001062
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001063 case 0360:
1064 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001066 case 0361:
1067 case 0362:
1068 case 0363:
1069 length++;
1070 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001071
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001072 case 0364:
1073 case 0365:
1074 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001075
Keith Kanios48af1772007-08-17 07:37:52 +00001076 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001077 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 length++;
1079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0370:
1082 case 0371:
1083 case 0372:
1084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0373:
1087 length++;
1088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001090 case4(0100):
1091 case4(0110):
1092 case4(0120):
1093 case4(0130):
1094 case4(0200):
1095 case4(0204):
1096 case4(0210):
1097 case4(0214):
1098 case4(0220):
1099 case4(0224):
1100 case4(0230):
1101 case4(0234):
1102 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001104 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 opflags_t rflags;
1106 struct operand *opy = &ins->oprs[op2];
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001107
Keith Kaniosb7a89542007-04-12 02:40:54 +00001108 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001109
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001110 if (c <= 0177) {
1111 /* pick rfield from operand b (opx) */
1112 rflags = regflag(opx);
1113 rfield = nasm_regvals[opx->basereg];
1114 } else {
1115 rflags = 0;
1116 rfield = c & 7;
1117 }
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001118 if (!process_ea(opy, &ea_data, bits,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 ins->addr_size, rfield, rflags)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 errfunc(ERR_NONFATAL, "invalid effective address");
1121 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001122 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001125 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 }
1127 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 default:
1130 errfunc(ERR_PANIC, "internal instruction table corrupt"
1131 ": instruction code \\%o (0x%02X) given", c, c);
1132 break;
1133 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001134 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001135
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001136 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001137
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001138 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001139 if (ins->rex & REX_H) {
1140 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1141 return -1;
1142 }
1143 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001144 }
1145
H. Peter Anvind85d2502008-05-04 17:53:31 -07001146 if (ins->rex & REX_V) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 if (ins->rex & REX_H) {
1150 errfunc(ERR_NONFATAL, "cannot use high register in vex instruction");
1151 return -1;
1152 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001153 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001155 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 ins->rex &= ~REX_W;
1157 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001158 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 ins->rex |= REX_W;
1160 bad32 &= ~REX_W;
1161 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001162 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 /* Follow REX_W */
1164 break;
1165 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 if (bits != 64 && ((ins->rex & bad32) || ins->drexdst > 7)) {
1168 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1169 return -1;
1170 }
H. Peter Anvin3cb0e8c2010-11-16 09:36:58 -08001171 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 length += 3;
1173 else
1174 length += 2;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001175 } else if (ins->rex & REX_D) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 if (ins->rex & REX_H) {
1177 errfunc(ERR_NONFATAL, "cannot use high register in drex instruction");
1178 return -1;
1179 }
1180 if (bits != 64 && ((ins->rex & (REX_R|REX_W|REX_X|REX_B)) ||
1181 ins->drexdst > 7)) {
1182 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1183 return -1;
1184 }
1185 length++;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001186 } else if (ins->rex & REX_REAL) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001187 if (ins->rex & REX_H) {
1188 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1189 return -1;
1190 } else if (bits == 64) {
1191 length++;
1192 } else if ((ins->rex & REX_L) &&
1193 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1194 cpu >= IF_X86_64) {
1195 /* LOCK-as-REX.R */
1196 assert_no_prefix(ins, PPS_LREP);
1197 length++;
1198 } else {
1199 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1200 return -1;
1201 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001202 }
1203
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001204 return length;
1205}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001206
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001207#define EMIT_REX() \
H. Peter Anvind85d2502008-05-04 17:53:31 -07001208 if (!(ins->rex & (REX_D|REX_V)) && (ins->rex & REX_REAL) && (bits == 64)) { \
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1210 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1211 ins->rex = 0; \
1212 offset += 1; \
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001213 }
1214
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001215static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001216 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001217 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001219 static char condval[] = { /* conditional opcodes */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1221 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1222 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001223 };
Keith Kaniosb7a89542007-04-12 02:40:54 +00001224 uint8_t c;
1225 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001226 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001227 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001228 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001229 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001230 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001231 uint8_t opex = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07001232
H. Peter Anvin839eca22007-10-29 23:12:47 -07001233 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 c = *codes++;
1235 op1 = (c & 3) + ((opex & 1) << 2);
1236 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1237 opx = &ins->oprs[op1];
1238 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001239
H. Peter Anvin839eca22007-10-29 23:12:47 -07001240 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 case 01:
1242 case 02:
1243 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001244 case 04:
1245 EMIT_REX();
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001246 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 codes += c;
1248 offset += c;
1249 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001250
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 case 05:
1252 case 06:
1253 case 07:
1254 opex = c;
1255 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001256
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 case4(010):
1258 EMIT_REX();
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001259 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001260 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 offset += 1;
1262 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001263
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001264 case4(014):
1265 /*
1266 * The test for BITS8 and SBYTE here is intended to avoid
1267 * warning on optimizer actions due to SBYTE, while still
1268 * warn on explicit BYTE directives. Also warn, obviously,
1269 * if the optimizer isn't enabled.
1270 */
H. Peter Anvin6c80ab62008-10-04 18:50:47 -07001271 if (((opx->type & BITS8) ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001272 !(opx->type & temp->opd[op1] & BYTENESS)) &&
1273 (opx->offset < -128 || opx->offset > 127)) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001274 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 "signed byte value exceeds bounds");
1276 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001277 if (opx->segment != NO_SEG) {
1278 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001279 out(offset, segment, &data, OUT_ADDRESS, 1,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001280 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001282 bytes[0] = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001283 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 NO_SEG);
1285 }
1286 offset += 1;
1287 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001290 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001291 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001294 if (opx->segment != NO_SEG) {
1295 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001296 out(offset, segment, &data, OUT_ADDRESS, 1,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001297 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001299 bytes[0] = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001300 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 NO_SEG);
1302 }
1303 offset += 1;
1304 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001305
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001307 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001308 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 "unsigned byte value exceeds bounds");
H. Peter Anvin839eca22007-10-29 23:12:47 -07001310 if (opx->segment != NO_SEG) {
1311 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001312 out(offset, segment, &data, OUT_ADDRESS, 1,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001313 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001315 bytes[0] = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001316 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 NO_SEG);
1318 }
1319 offset += 1;
1320 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001321
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001323 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001324 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001325 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001326 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 offset += 2;
1328 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001329
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001331 if (opx->type & (BITS16 | BITS32))
1332 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 else
1334 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001335 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001336 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001337 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001338 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 offset += size;
1340 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001343 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001344 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001345 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001346 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 offset += 4;
1348 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001349
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001351 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001352 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001353 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001354 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001355 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 offset += size;
1357 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001358
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001359 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001360 if (opx->segment != segment) {
1361 data = opx->offset;
1362 out(offset, segment, &data,
1363 OUT_REL1ADR, insn_end - offset,
1364 opx->segment, opx->wrt);
1365 } else {
1366 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 if (data > 127 || data < -128)
1368 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001369 out(offset, segment, &data,
1370 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1371 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 offset += 1;
1373 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001376 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001377 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001378 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001379 offset += 8;
1380 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001381
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001383 if (opx->segment != segment) {
1384 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001386 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001387 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001389 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001391 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 }
1393 offset += 2;
1394 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001396 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001397 if (opx->type & (BITS16 | BITS32 | BITS64))
1398 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 else
1400 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001401 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001402 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001403 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001404 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1405 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001407 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001409 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 }
1411 offset += size;
1412 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001414 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001415 if (opx->segment != segment) {
1416 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001418 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001419 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001421 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001423 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 }
1425 offset += 4;
1426 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001427
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001428 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001429 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001430 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1431 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001432 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001433 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001434 outfmt->segbase(1 + opx->segment),
1435 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001436 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001438
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001439 case4(0140):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001440 data = opx->offset;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001441 warn_overflow_opd(opx, 2);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07001442 if (is_sbyte16(opx)) {
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001443 bytes[0] = data;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001444 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001445 NO_SEG);
1446 offset++;
1447 } else {
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001448 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001449 opx->segment, opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001450 offset += 2;
1451 }
1452 break;
1453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 case4(0144):
1455 EMIT_REX();
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001456 bytes[0] = *codes++;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07001457 if (is_sbyte16(opx))
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001458 bytes[0] |= 2; /* s-bit */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001459 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001460 offset++;
1461 break;
1462
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001463 case4(0150):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001464 data = opx->offset;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001465 warn_overflow_opd(opx, 4);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07001466 if (is_sbyte32(opx)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 bytes[0] = data;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001468 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 NO_SEG);
1470 offset++;
1471 } else {
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001472 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001473 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 offset += 4;
1475 }
1476 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001477
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001478 case4(0154):
1479 EMIT_REX();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 bytes[0] = *codes++;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07001481 if (is_sbyte32(opx))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 bytes[0] |= 2; /* s-bit */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001483 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 offset++;
1485 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001486
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001487 case4(0160):
1488 case4(0164):
1489 break;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001490
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 case 0171:
1492 bytes[0] =
1493 (ins->drexdst << 4) |
1494 (ins->rex & REX_OC ? 0x08 : 0) |
1495 (ins->rex & (REX_R|REX_X|REX_B));
1496 ins->rex = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001497 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 offset++;
1499 break;
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001500
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001501 case 0172:
1502 c = *codes++;
1503 opx = &ins->oprs[c >> 3];
1504 bytes[0] = nasm_regvals[opx->basereg] << 4;
1505 opx = &ins->oprs[c & 7];
1506 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1507 errfunc(ERR_NONFATAL,
1508 "non-absolute expression not permitted as argument %d",
1509 c & 7);
1510 } else {
1511 if (opx->offset & ~15) {
1512 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1513 "four-bit argument exceeds bounds");
1514 }
1515 bytes[0] |= opx->offset & 15;
1516 }
1517 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1518 offset++;
1519 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001520
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001521 case 0173:
1522 c = *codes++;
1523 opx = &ins->oprs[c >> 4];
1524 bytes[0] = nasm_regvals[opx->basereg] << 4;
1525 bytes[0] |= c & 15;
1526 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1527 offset++;
1528 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001529
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001530 case 0174:
1531 c = *codes++;
1532 opx = &ins->oprs[c];
1533 bytes[0] = nasm_regvals[opx->basereg] << 4;
1534 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1535 offset++;
1536 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001537
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 case4(0250):
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001539 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001540 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1541 (int32_t)data != (int64_t)data) {
1542 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1543 "signed dword immediate exceeds bounds");
1544 }
H. Peter Anvinab5bd052010-07-25 12:43:30 -07001545 if (is_sbyte32(opx)) {
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07001546 bytes[0] = data;
1547 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1548 NO_SEG);
1549 offset++;
1550 } else {
1551 out(offset, segment, &data, OUT_ADDRESS, 4,
1552 opx->segment, opx->wrt);
1553 offset += 4;
1554 }
1555 break;
1556
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001558 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1560 (int32_t)data != (int64_t)data) {
1561 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1562 "signed dword immediate exceeds bounds");
1563 }
1564 out(offset, segment, &data, OUT_ADDRESS, 4,
1565 opx->segment, opx->wrt);
1566 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001567 break;
1568
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001569 case4(0260):
1570 case 0270:
1571 codes += 2;
1572 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1573 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1574 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1575 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
1576 ((~ins->drexdst & 15)<< 3) | (ins->vex_wlp & 07);
1577 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1578 offset += 3;
1579 } else {
1580 bytes[0] = 0xc5;
1581 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
1582 ((~ins->drexdst & 15) << 3) | (ins->vex_wlp & 07);
1583 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1584 offset += 2;
1585 }
1586 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(0274):
1589 {
1590 uint64_t uv, um;
1591 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 if (ins->rex & REX_W)
1594 s = 64;
1595 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1596 s = 16;
1597 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1598 s = 32;
1599 else
1600 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 um = (uint64_t)2 << (s-1);
1603 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 if (uv > 127 && uv < (uint64_t)-128 &&
1606 (uv < um-128 || uv > um-1)) {
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001607 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 "signed byte value exceeds bounds");
1609 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001610 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001611 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001612 out(offset, segment, &data, OUT_ADDRESS, 1,
1613 opx->segment, opx->wrt);
1614 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001615 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001616 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1617 NO_SEG);
1618 }
1619 offset += 1;
1620 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001621 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001622
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001627 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001629 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 offset += 1;
1631 } else
1632 offset += 0;
1633 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001634
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001636 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001638 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 offset += 1;
1640 } else
1641 offset += 0;
1642 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001643
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 case 0312:
1645 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001646
Keith Kaniosb7a89542007-04-12 02:40:54 +00001647 case 0313:
1648 ins->rex = 0;
1649 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001650
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 case4(0314):
1652 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001653
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001657
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001659 case 0323:
1660 break;
1661
Keith Kaniosb7a89542007-04-12 02:40:54 +00001662 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001663 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 case 0325:
1667 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001668
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 case 0330:
1670 *bytes = *codes++ ^ condval[ins->condition];
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001671 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 offset += 1;
1673 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001680 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001681 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 offset += 1;
1683 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001684
Keith Kanios48af1772007-08-17 07:37:52 +00001685 case 0334:
1686 if (ins->rex & REX_R) {
1687 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001688 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001689 offset += 1;
1690 }
1691 ins->rex &= ~(REX_L|REX_R);
1692 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001693
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001694 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001695 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001696
H. Peter Anvin962e3052008-08-28 17:47:16 -07001697 case 0336:
1698 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001700
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 if (ins->oprs[0].segment != NO_SEG)
1703 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1704 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001705 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 if (size > 0)
1707 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001708 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 offset += size;
1710 }
1711 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001713 case 0341:
1714 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001715
H. Peter Anvinff6e12d2008-10-08 21:17:32 -07001716 case 0344:
1717 case 0345:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001718 bytes[0] = c & 1;
H. Peter Anvinff6e12d2008-10-08 21:17:32 -07001719 switch (ins->oprs[0].basereg) {
1720 case R_CS:
1721 bytes[0] += 0x0E;
1722 break;
1723 case R_DS:
1724 bytes[0] += 0x1E;
1725 break;
1726 case R_ES:
1727 bytes[0] += 0x06;
1728 break;
1729 case R_SS:
1730 bytes[0] += 0x16;
1731 break;
1732 default:
1733 errfunc(ERR_PANIC,
1734 "bizarre 8086 segment register received");
1735 }
1736 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1737 offset++;
1738 break;
1739
1740 case 0346:
1741 case 0347:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 bytes[0] = c & 1;
H. Peter Anvinff6e12d2008-10-08 21:17:32 -07001743 switch (ins->oprs[0].basereg) {
1744 case R_FS:
1745 bytes[0] += 0xA0;
1746 break;
1747 case R_GS:
1748 bytes[0] += 0xA8;
1749 break;
1750 default:
1751 errfunc(ERR_PANIC,
1752 "bizarre 386 segment register received");
1753 }
1754 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1755 offset++;
1756 break;
1757
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 case 0360:
1759 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001760
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 case 0361:
1762 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001763 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1764 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001765 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001766
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001767 case 0362:
1768 case 0363:
1769 bytes[0] = c - 0362 + 0xf2;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001770 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1771 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001772 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001773
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 case 0364:
1775 case 0365:
1776 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001777
Keith Kanios48af1772007-08-17 07:37:52 +00001778 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001779 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001780 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001781 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001782 offset += 1;
1783 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 0370:
1786 case 0371:
1787 case 0372:
1788 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 case 0373:
1791 *bytes = bits == 16 ? 3 : 5;
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
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 case4(0100):
1797 case4(0110):
1798 case4(0120):
1799 case4(0130):
1800 case4(0200):
1801 case4(0204):
1802 case4(0210):
1803 case4(0214):
1804 case4(0220):
1805 case4(0224):
1806 case4(0230):
1807 case4(0234):
1808 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 ea ea_data;
1810 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001812 uint8_t *p;
1813 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001814 enum out_type type;
1815 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001816
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001817 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001818 /* pick rfield from operand b (opx) */
1819 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001820 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 } else {
1822 /* rfield is constant */
1823 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001827 if (!process_ea(opy, &ea_data, bits, ins->addr_size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 rfield, rflags)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 errfunc(ERR_NONFATAL, "invalid effective address");
1830 }
H. Peter Anvin70653092007-10-19 14:42:29 -07001831
Charles Crayne7e975552007-11-03 22:06:13 -07001832
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 p = bytes;
1834 *p++ = ea_data.modrm;
1835 if (ea_data.sib_present)
1836 *p++ = ea_data.sib;
1837
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 /* DREX suffixes come between the SIB and the displacement */
1839 if (ins->rex & REX_D) {
1840 *p++ = (ins->drexdst << 4) |
1841 (ins->rex & REX_OC ? 0x08 : 0) |
1842 (ins->rex & (REX_R|REX_X|REX_B));
1843 ins->rex = 0;
1844 }
H. Peter Anvin401c07e2007-09-17 16:55:04 -07001845
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001847 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001849 /*
1850 * Make sure the address gets the right offset in case
1851 * the line breaks in the .lst file (BR 1197827)
1852 */
1853 offset += s;
1854 s = 0;
1855
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 switch (ea_data.bytes) {
1857 case 0:
1858 break;
1859 case 1:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case 2:
1861 case 4:
Victor van den Elzen352fe062008-12-10 13:04:58 +01001862 case 8:
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001863 data = opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 if (ea_data.rip) {
1866 if (opy->segment == segment) {
1867 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001868 if (overflow_signed(data, ea_data.bytes))
1869 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 out(offset, segment, &data, OUT_ADDRESS,
1871 ea_data.bytes, NO_SEG, NO_SEG);
1872 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001873 /* overflow check in output/linker? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 out(offset, segment, &data, OUT_REL4ADR,
1875 insn_end - offset, opy->segment, opy->wrt);
1876 }
1877 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001878 if (overflow_general(opy->offset, ins->addr_size >> 3) ||
1879 signed_bits(opy->offset, ins->addr_size) !=
1880 signed_bits(opy->offset, ea_data.bytes * 8))
1881 warn_overflow(ERR_PASS2, ea_data.bytes);
1882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 type = OUT_ADDRESS;
1884 out(offset, segment, &data, OUT_ADDRESS,
1885 ea_data.bytes, opy->segment, opy->wrt);
1886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 break;
Victor van den Elzen352fe062008-12-10 13:04:58 +01001888 default:
1889 /* Impossible! */
1890 errfunc(ERR_PANIC,
1891 "Invalid amount of bytes (%d) for offset?!",
1892 ea_data.bytes);
1893 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 }
1895 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 }
1897 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001899 default:
1900 errfunc(ERR_PANIC, "internal instruction table corrupt"
1901 ": instruction code \\%o (0x%02X) given", c, c);
1902 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001904 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001905}
1906
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001907static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001908{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001909 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001910 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001911 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001912}
1913
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001914static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001915{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001916 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001918 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001919}
1920
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001921static int op_rexflags(const operand * o, int mask)
1922{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001923 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001924 int val;
1925
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001926 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001927 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001928
H. Peter Anvina4835d42008-05-20 14:21:29 -07001929 flags = nasm_reg_flags[o->basereg];
1930 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001931
1932 return rexflags(val, flags, mask);
1933}
1934
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001935static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001936{
1937 int rex = 0;
1938
1939 if (val >= 8)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001941 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 rex |= REX_W;
1943 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1944 rex |= REX_H;
1945 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1946 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947
1948 return rex & mask;
1949}
1950
H. Peter Anvin23595f52009-07-25 17:44:25 -07001951static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 insn *instruction,
1953 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001954{
1955 const struct itemplate *temp;
1956 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001957 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001958 bool opsizemissing = false;
1959 int i;
1960
1961 for (i = 0; i < instruction->operands; i++)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001962 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001963
1964 merr = MERR_INVALOP;
1965
1966 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 temp->opcode != I_none; temp++) {
1968 m = matches(temp, instruction, bits);
1969 if (m == MOK_JUMP) {
1970 if (jmp_match(segment, offset, bits, instruction, temp->code))
1971 m = MOK_GOOD;
1972 else
1973 m = MERR_INVALOP;
1974 } else if (m == MERR_OPSIZEMISSING &&
1975 (temp->flags & IF_SMASK) != IF_SX) {
1976 /*
1977 * Missing operand size and a candidate for fuzzy matching...
1978 */
1979 for (i = 0; i < temp->operands; i++) {
1980 if ((temp->opd[i] & SAME_AS) == 0)
1981 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
1982 }
1983 opsizemissing = true;
1984 }
1985 if (m > merr)
1986 merr = m;
1987 if (merr == MOK_GOOD)
1988 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001989 }
1990
1991 /* No match, but see if we can get a fuzzy operand size match... */
1992 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001993 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001994
1995 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 /*
1997 * We ignore extrinsic operand sizes on registers, so we should
1998 * never try to fuzzy-match on them. This also resolves the case
1999 * when we have e.g. "xmmrm128" in two different positions.
2000 */
2001 if (is_class(REGISTER, instruction->oprs[i].type))
2002 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002004 /* This tests if xsizeflags[i] has more than one bit set */
2005 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2006 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002008 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002009 }
2010
2011 /* Try matching again... */
2012 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 temp->opcode != I_none; temp++) {
2014 m = matches(temp, instruction, bits);
2015 if (m == MOK_JUMP) {
2016 if (jmp_match(segment, offset, bits, instruction, temp->code))
2017 m = MOK_GOOD;
2018 else
2019 m = MERR_INVALOP;
2020 }
2021 if (m > merr)
2022 merr = m;
2023 if (merr == MOK_GOOD)
2024 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002025 }
2026
H. Peter Anvina81655b2009-07-25 18:15:28 -07002027done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002028 *tempp = temp;
2029 return merr;
2030}
2031
H. Peter Anvin65289e82009-07-25 17:25:11 -07002032static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002033 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002034{
H. Peter Anvin60926242009-07-26 16:25:38 -07002035 int i, size[MAX_OPERANDS], asize, oprs;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002036 bool opsizemissing = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002037
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002038 /*
2039 * Check the opcode
2040 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002042 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002043
2044 /*
2045 * Count the operands
2046 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002048 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002049
2050 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002051 * Is it legal?
2052 */
2053 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2054 return MERR_INVALOP;
2055
2056 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002057 * Check that no spurious colons or TOs are present
2058 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 for (i = 0; i < itemp->operands; i++)
2060 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002061 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002062
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002063 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002064 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002065 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002066 switch (itemp->flags & IF_SMASK) {
2067 case IF_SB:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002068 asize = BITS8;
2069 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002070 case IF_SW:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002071 asize = BITS16;
2072 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002073 case IF_SD:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002074 asize = BITS32;
2075 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002076 case IF_SQ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002077 asize = BITS64;
2078 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002079 case IF_SO:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002080 asize = BITS128;
2081 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002082 case IF_SY:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002083 asize = BITS256;
2084 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002085 case IF_SZ:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002086 switch (bits) {
2087 case 16:
2088 asize = BITS16;
2089 break;
2090 case 32:
2091 asize = BITS32;
2092 break;
2093 case 64:
2094 asize = BITS64;
2095 break;
2096 default:
2097 asize = 0;
2098 break;
2099 }
2100 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002101 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002102 asize = 0;
2103 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002104 }
2105
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002106 if (itemp->flags & IF_ARMASK) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002107 /* S- flags only apply to a specific operand */
2108 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2109 memset(size, 0, sizeof size);
2110 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002111 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002112 /* S- flags apply to all operands */
2113 for (i = 0; i < MAX_OPERANDS; i++)
2114 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002115 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002116
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002117 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002118 * Check that the operand flags all match up,
2119 * it's a bit tricky so lets be verbose:
2120 *
2121 * 1) Find out the size of operand. If instruction
2122 * doesn't have one specified -- we're trying to
2123 * guess it either from template (IF_S* flag) or
2124 * from code bits.
2125 *
2126 * 2) If template operand (i) has SAME_AS flag [used for registers only]
2127 * (ie the same operand as was specified somewhere in template, and
2128 * this referred operand index is being achieved via ~SAME_AS)
2129 * we are to be sure that both registers (in template and instruction)
2130 * do exactly match.
2131 *
2132 * 3) If template operand do not match the instruction OR
2133 * template has an operand size specified AND this size differ
2134 * from which instruction has (perhaps we got it from code bits)
2135 * we are:
2136 * a) Check that only size of instruction and operand is differ
2137 * other characteristics do match
2138 * b) Perhaps it's a register specified in instruction so
2139 * for such a case we just mark that operand as "size
2140 * missing" and this will turn on fuzzy operand size
2141 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002142 */
2143 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002144 opflags_t type = instruction->oprs[i].type;
2145 if (!(type & SIZE_MASK))
2146 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002147
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002148 if (itemp->opd[i] & SAME_AS) {
2149 int j = itemp->opd[i] & ~SAME_AS;
2150 if (type != instruction->oprs[j].type ||
2151 instruction->oprs[i].basereg != instruction->oprs[j].basereg)
2152 return MERR_INVALOP;
2153 } else if (itemp->opd[i] & ~type ||
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002154 ((itemp->opd[i] & SIZE_MASK) &&
2155 ((itemp->opd[i] ^ type) & SIZE_MASK))) {
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002156 if ((itemp->opd[i] & ~type & ~SIZE_MASK) || (type & SIZE_MASK)) {
H. Peter Anvin65289e82009-07-25 17:25:11 -07002157 return MERR_INVALOP;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002158 } else if (!is_class(REGISTER, type)) {
2159 /*
2160 * Note: we don't honor extrinsic operand sizes for registers,
2161 * so "missing operand size" for a register should be
2162 * considered a wildcard match rather than an error.
2163 */
2164 opsizemissing = true;
2165 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002166 }
2167 }
2168
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002169 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002170 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002171
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002172 /*
2173 * Check operand sizes
2174 */
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002175 if (itemp->flags & (IF_SM | IF_SM2)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002178 asize = itemp->opd[i] & SIZE_MASK;
2179 if (asize) {
2180 for (i = 0; i < oprs; i++)
2181 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 break;
2183 }
2184 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002185 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187 }
2188
Keith Kaniosb7a89542007-04-12 02:40:54 +00002189 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 if (!(itemp->opd[i] & SIZE_MASK) &&
2191 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002192 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002193 }
2194
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002195 /*
2196 * Check template is okay at the set cpu level
2197 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002198 if (((itemp->flags & IF_PLEVEL) > cpu))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002199 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002200
Keith Kaniosb7a89542007-04-12 02:40:54 +00002201 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002202 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002203 */
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002204 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002205 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002207 /*
2208 * Check if special handling needed for Jumps
2209 */
H. Peter Anvin60926242009-07-26 16:25:38 -07002210 if ((itemp->code[0] & 0374) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002211 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212
H. Peter Anvin60926242009-07-26 16:25:38 -07002213 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002214}
2215
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002216static ea *process_ea(operand * input, ea * output, int bits,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002217 int addrbits, int rfield, opflags_t rflags)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002218{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002219 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002220
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002221 output->rip = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002222
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002223 /* REX flags for the rfield operand */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002224 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002225
Cyrill Gorcunov8a6345c2009-10-13 19:05:31 +04002226 if (is_class(REGISTER, input->type)) { /* register direct */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 int i;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002228 opflags_t f;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002229
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002230 if (!is_register(input->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002231 return NULL;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002232 f = regflag(input);
H. Peter Anvina4835d42008-05-20 14:21:29 -07002233 i = nasm_regvals[input->basereg];
Keith Kaniosb7a89542007-04-12 02:40:54 +00002234
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002235 if (REG_EA & ~f)
2236 return NULL; /* Invalid EA register */
H. Peter Anvin70653092007-10-19 14:42:29 -07002237
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002238 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002239
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002240 output->sib_present = false; /* no SIB necessary */
2241 output->bytes = 0; /* no offset necessary either */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002242 output->modrm = 0xC0 | ((rfield & 7) << 3) | (i & 7);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 } else { /* it's a memory reference */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002244 if (input->basereg == -1 &&
2245 (input->indexreg == -1 || input->scale == 0)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 /* it's a pure offset */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002247
2248 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2249 input->segment == NO_SEG) {
2250 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2251 input->type &= ~IP_REL;
2252 input->type |= MEMORY;
2253 }
2254
2255 if (input->eaflags & EAF_BYTEOFFS ||
2256 (input->eaflags & EAF_WORDOFFS &&
2257 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2258 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2259 }
2260
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002261 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002262 int scale, index, base;
2263 output->sib_present = true;
2264 scale = 0;
2265 index = 4;
2266 base = 5;
2267 output->sib = (scale << 6) | (index << 3) | base;
2268 output->bytes = 4;
2269 output->modrm = 4 | ((rfield & 7) << 3);
2270 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002271 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002272 output->sib_present = false;
2273 output->bytes = (addrbits != 16 ? 4 : 2);
2274 output->modrm = (addrbits != 16 ? 5 : 6) | ((rfield & 7) << 3);
2275 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002276 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 } else { /* it's an indirection */
2278 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002279 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002281 int t, it, bt; /* register numbers */
2282 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002283
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 if (s == 0)
2285 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002286
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002287 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002288 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002289 ix = nasm_reg_flags[i];
2290 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002291 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002292 ix = 0;
2293 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002294
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002295 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002296 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002297 bx = nasm_reg_flags[b];
2298 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002299 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002300 bx = 0;
2301 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002302
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002303 /* check for a 32/64-bit memory reference... */
2304 if ((ix|bx) & (BITS32|BITS64)) {
2305 /*
2306 * it must be a 32/64-bit memory reference. Firstly we have
2307 * to check that all registers involved are type E/Rxx.
2308 */
2309 int32_t sok = BITS32 | BITS64, o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002310
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002311 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002312 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2313 sok &= ix;
2314 else
2315 return NULL;
2316 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002317
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002318 if (bt != -1) {
2319 if (REG_GPR & ~bx)
2320 return NULL; /* Invalid register */
2321 if (~sok & bx & SIZE_MASK)
2322 return NULL; /* Invalid size */
2323 sok &= bx;
2324 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002325
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002326 /*
2327 * While we're here, ensure the user didn't specify
2328 * WORD or QWORD
2329 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002330 if (input->disp_size == 16 || input->disp_size == 64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002331 return NULL;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002332
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002333 if (addrbits == 16 ||
2334 (addrbits == 32 && !(sok & BITS32)) ||
2335 (addrbits == 64 && !(sok & BITS64)))
2336 return NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002337
Keith Kaniosb7a89542007-04-12 02:40:54 +00002338 /* now reorganize base/index */
2339 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002340 ((hb == b && ht == EAH_NOTBASE) ||
2341 (hb == i && ht == EAH_MAKEBASE))) {
2342 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002343 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002344 x = bx, bx = ix, ix = x;
2345 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002346 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002347 bt = -1, bx = 0, s++;
2348 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002349 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002350 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002351 }
2352 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2353 s == 3 || s == 5 || s == 9) && bt == -1)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002354 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002355 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2356 (input->eaflags & EAF_TIMESTWO))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002357 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
Keith Kanios48af1772007-08-17 07:37:52 +00002359 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002360 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002362 x = ix, ix = bx, bx = x;
2363 }
2364 if (it == REG_NUM_ESP ||
2365 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 return NULL; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002367
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002368 output->rex |= rexflags(it, ix, REX_X);
2369 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002370
Keith Kanios48af1772007-08-17 07:37:52 +00002371 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002372 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002374
Keith Kaniosb7a89542007-04-12 02:40:54 +00002375 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002378 } else {
2379 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002380 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002381 seg == NO_SEG && !forw_ref &&
2382 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002383 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002384 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002385 (o >= -128 && o <= 127 &&
2386 seg == NO_SEG && !forw_ref &&
2387 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002388 mod = 1;
2389 else
2390 mod = 2;
2391 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002392
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002393 output->sib_present = false;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002394 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2395 output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002396 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002397 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002399
Keith Kaniosb7a89542007-04-12 02:40:54 +00002400 if (it == -1)
2401 index = 4, s = 1;
2402 else
2403 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002404
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 switch (s) {
2406 case 1:
2407 scale = 0;
2408 break;
2409 case 2:
2410 scale = 1;
2411 break;
2412 case 4:
2413 scale = 2;
2414 break;
2415 case 8:
2416 scale = 3;
2417 break;
2418 default: /* then what the smeg is it? */
2419 return NULL; /* panic */
2420 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002421
Keith Kaniosb7a89542007-04-12 02:40:54 +00002422 if (bt == -1) {
2423 base = 5;
2424 mod = 0;
2425 } else {
2426 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002427 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002428 seg == NO_SEG && !forw_ref &&
2429 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002430 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002431 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002432 (o >= -128 && o <= 127 &&
2433 seg == NO_SEG && !forw_ref &&
2434 !(input->eaflags & EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002435 mod = 1;
2436 else
2437 mod = 2;
2438 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002439
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002440 output->sib_present = true;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002441 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002442 output->modrm = (mod << 6) | ((rfield & 7) << 3) | 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 output->sib = (scale << 6) | (index << 3) | base;
2444 }
2445 } else { /* it's 16-bit */
2446 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002447 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002448
Keith Kaniosb7a89542007-04-12 02:40:54 +00002449 /* check for 64-bit long mode */
2450 if (addrbits == 64)
2451 return NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002452
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002454 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2455 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 return NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002457
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002459 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 return NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 if (s != 1 && i != -1)
2463 return NULL; /* no can do, in 16-bit EA */
2464 if (b == -1 && i != -1) {
2465 int tmp = b;
2466 b = i;
2467 i = tmp;
2468 } /* swap */
2469 if ((b == R_SI || b == R_DI) && i != -1) {
2470 int tmp = b;
2471 b = i;
2472 i = tmp;
2473 }
2474 /* have BX/BP as base, SI/DI index */
2475 if (b == i)
2476 return NULL; /* shouldn't ever happen, in theory */
2477 if (i != -1 && b != -1 &&
2478 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
2479 return NULL; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002480 if (b == -1) /* pure offset: handled above */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 return NULL; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 rm = -1;
2484 if (i != -1)
2485 switch (i * 256 + b) {
2486 case R_SI * 256 + R_BX:
2487 rm = 0;
2488 break;
2489 case R_DI * 256 + R_BX:
2490 rm = 1;
2491 break;
2492 case R_SI * 256 + R_BP:
2493 rm = 2;
2494 break;
2495 case R_DI * 256 + R_BP:
2496 rm = 3;
2497 break;
2498 } else
2499 switch (b) {
2500 case R_SI:
2501 rm = 4;
2502 break;
2503 case R_DI:
2504 rm = 5;
2505 break;
2506 case R_BP:
2507 rm = 6;
2508 break;
2509 case R_BX:
2510 rm = 7;
2511 break;
2512 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002513 if (rm == -1) /* can't happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 return NULL; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002515
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002516 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2517 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 mod = 0;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002519 else if (input->eaflags & EAF_BYTEOFFS ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002520 (o >= -128 && o <= 127 && seg == NO_SEG &&
2521 !forw_ref && !(input->eaflags & EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 mod = 1;
2523 else
2524 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002525
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002526 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 output->bytes = mod; /* bytes of offset needed */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002528 output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 }
2530 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002531 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002532
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002533 output->size = 1 + output->sib_present + output->bytes;
2534 return output;
2535}
2536
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002537static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002538{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002539 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002540 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002541
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002542 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002543
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002544 switch (ins->prefixes[PPS_ASIZE]) {
2545 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 valid &= 16;
2547 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002548 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 valid &= 32;
2550 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002551 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 valid &= 64;
2553 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002554 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 valid &= (addrbits == 32) ? 16 : 32;
2556 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002557 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002559 }
2560
2561 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002562 if (is_class(MEMORY, ins->oprs[j].type)) {
2563 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002564
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002565 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002566 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002567 i = 0;
2568 else
2569 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002570
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002571 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002572 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002573 b = 0;
2574 else
2575 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002576
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002577 if (ins->oprs[j].scale == 0)
2578 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002579
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002580 if (!i && !b) {
2581 int ds = ins->oprs[j].disp_size;
2582 if ((addrbits != 64 && ds > 8) ||
2583 (addrbits == 64 && ds == 16))
2584 valid &= ds;
2585 } else {
2586 if (!(REG16 & ~b))
2587 valid &= 16;
2588 if (!(REG32 & ~b))
2589 valid &= 32;
2590 if (!(REG64 & ~b))
2591 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002593 if (!(REG16 & ~i))
2594 valid &= 16;
2595 if (!(REG32 & ~i))
2596 valid &= 32;
2597 if (!(REG64 & ~i))
2598 valid &= 64;
2599 }
2600 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002601 }
2602
2603 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002604 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002605 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002606 /* Add an address size prefix */
2607 enum prefixes pref = (addrbits == 32) ? P_A16 : P_A32;
2608 ins->prefixes[PPS_ASIZE] = pref;
2609 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002610 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002611 /* Impossible... */
2612 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2613 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002614 }
2615
2616 defdisp = ins->addr_size == 16 ? 16 : 32;
2617
2618 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002619 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2620 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2621 /*
2622 * mem_offs sizes must match the address size; if not,
2623 * strip the MEM_OFFS bit and match only EA instructions
2624 */
2625 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2626 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002627 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002628}