blob: 7a9d90d26e59c4f9fed8a596a23b460dc56bded8 [file] [log] [blame]
Ian Rogerse32ca232012-03-05 10:20:23 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_
18#define ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_
19
20#include "../../Dalvik.h"
21#include "../../CompilerInternals.h"
22
23namespace art {
24
25// Set to 1 to measure cost of suspend check
26#define NO_SUSPEND 0
27
28/*
29 * Runtime register conventions. We consider both x86, x86-64 and x32 (32bit mode x86-64), although
30 * we currently only target x86. The ABI has different conventions and we hope to have a single
31 * convention to simplify code generation. Changing something that is callee save and making it
32 * caller save places a burden on up-calls to save/restore the callee save register, however, there
33 * are few registers that are callee save in the ABI. Changing something that is caller save and
34 * making it callee save places a burden on down-calls to save/restore the callee save register.
Ian Rogersb41b33b2012-03-20 14:22:54 -070035 * For these reasons we aim to match native conventions for caller and callee save. The first 4
36 * registers can be used for byte operations, for this reason they are preferred for temporary
37 * scratch registers.
Ian Rogerse32ca232012-03-05 10:20:23 -080038 *
39 * General Purpose Register:
40 * Native: x86 | x86-64 / x32 | ART
41 * r0/eax: caller save | caller save | caller, Method*, scratch, return value
Ian Rogersb41b33b2012-03-20 14:22:54 -070042 * r1/ecx: caller save | caller save, arg4 | caller, arg1, scratch
43 * r2/edx: caller save | caller save, arg3 | caller, arg2, scratch, high half of long return
44 * r3/ebx: callEE save | callEE save | callER, arg3, scratch
Ian Rogerse32ca232012-03-05 10:20:23 -080045 * r4/esp: stack pointer
46 * r5/ebp: callee save | callee save | callee, available for dalvik register promotion
47 * r6/esi: callEE save | callER save, arg2 | callee, available for dalvik register promotion
48 * r7/edi: callEE save | callER save, arg1 | callee, available for dalvik register promotion
49 * --- x86-64/x32 registers
50 * Native: x86-64 / x32 | ART
51 * r8: caller save, arg5 | caller, scratch
52 * r9: caller save, arg6 | caller, scratch
53 * r10: caller save | caller, scratch
54 * r11: caller save | caller, scratch
55 * r12: callee save | callee, available for dalvik register promotion
56 * r13: callee save | callee, available for dalvik register promotion
57 * r14: callee save | callee, available for dalvik register promotion
58 * r15: callee save | callee, available for dalvik register promotion
59 *
60 * There is no rSELF, instead on x86 fs: has a base address of Thread::Current, whereas on
61 * x86-64/x32 gs: holds it.
62 *
63 * For floating point we don't support CPUs without SSE2 support (ie newer than PIII):
64 * Native: x86 | x86-64 / x32 | ART
65 * XMM0: caller save |caller save, arg1 | caller, float/double return value (except for native x86 code)
66 * XMM1: caller save |caller save, arg2 | caller, scratch
67 * XMM2: caller save |caller save, arg3 | caller, scratch
68 * XMM3: caller save |caller save, arg4 | caller, scratch
69 * XMM4: caller save |caller save, arg5 | caller, scratch
70 * XMM5: caller save |caller save, arg6 | caller, scratch
71 * XMM6: caller save |caller save, arg7 | caller, scratch
72 * XMM7: caller save |caller save, arg8 | caller, scratch
73 * --- x86-64/x32 registers
74 * XMM8 .. 15: caller save
75 *
76 * X87 is a necessary evil outside of ART code:
77 * ST0: x86 float/double native return value, caller save
78 * ST1 .. ST7: caller save
79 *
80 * Stack frame diagram (stack grows down, higher addresses at top):
81 *
82 * +------------------------+
83 * | IN[ins-1] | {Note: resides in caller's frame}
84 * | . |
85 * | IN[0] |
86 * | caller's Method* |
87 * +========================+ {Note: start of callee's frame}
88 * | return address | {pushed by call}
89 * | spill region | {variable sized}
90 * +------------------------+
91 * | ...filler word... | {Note: used as 2nd word of V[locals-1] if long]
92 * +------------------------+
93 * | V[locals-1] |
94 * | V[locals-2] |
95 * | . |
96 * | . |
97 * | V[1] |
98 * | V[0] |
99 * +------------------------+
100 * | 0 to 3 words padding |
101 * +------------------------+
102 * | OUT[outs-1] |
103 * | OUT[outs-2] |
104 * | . |
105 * | OUT[0] |
106 * | curMethod* | <<== sp w/ 16-byte alignment
107 * +========================+
108 */
109
110/* Offset to distingish FP regs */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700111#define FP_REG_OFFSET 32
Ian Rogerse32ca232012-03-05 10:20:23 -0800112/* Offset to distinguish DP FP regs */
jeffhaofdffdf82012-07-11 16:08:43 -0700113#define FP_DOUBLE (FP_REG_OFFSET + 16)
Ian Rogerse32ca232012-03-05 10:20:23 -0800114/* Offset to distingish the extra regs */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700115#define EXTRA_REG_OFFSET (FP_DOUBLE + 16)
Ian Rogerse32ca232012-03-05 10:20:23 -0800116/* Reg types */
117#define REGTYPE(x) (x & (FP_REG_OFFSET | FP_DOUBLE))
118#define FPREG(x) ((x & FP_REG_OFFSET) == FP_REG_OFFSET)
119#define EXTRAREG(x) ((x & EXTRA_REG_OFFSET) == EXTRA_REG_OFFSET)
120#define LOWREG(x) ((x & 0x1f) == x)
121#define DOUBLEREG(x) ((x & FP_DOUBLE) == FP_DOUBLE)
122#define SINGLEREG(x) (FPREG(x) && !DOUBLEREG(x))
Ian Rogersb5d09b22012-03-06 22:14:17 -0800123
Ian Rogerse32ca232012-03-05 10:20:23 -0800124/*
125 * Note: the low register of a floating point pair is sufficient to
126 * create the name of a double, but require both names to be passed to
127 * allow for asserts to verify that the pair is consecutive if significant
128 * rework is done in this area. Also, it is a good reminder in the calling
129 * code that reg locations always describe doubles as a pair of singles.
130 */
131#define S2D(x,y) ((x) | FP_DOUBLE)
132/* Mask to strip off fp flags */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700133#define FP_REG_MASK 0xF
Ian Rogerse32ca232012-03-05 10:20:23 -0800134/* non-existent Dalvik register */
135#define vNone (-1)
136/* non-existant physical register */
137#define rNone (-1)
138
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700139/* RegisterLocation templates return values (rAX, rAX/rDX or XMM0) */
buzbee2cfc6392012-05-07 14:51:40 -0700140// location, wide, defined, const, fp, core, ref, highWord, home, lowReg, highReg, sRegLow
141#define LOC_C_RETURN {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, rAX, INVALID_REG, INVALID_SREG, INVALID_SREG}
142#define LOC_C_RETURN_WIDE {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rAX, rDX, INVALID_SREG, INVALID_SREG}
143#define LOC_C_RETURN_FLOAT {kLocPhysReg, 0, 0, 0, 1, 0, 0, 0, 1, fr0, INVALID_REG, INVALID_SREG, INVALID_SREG}
144#define LOC_C_RETURN_WIDE_DOUBLE {kLocPhysReg, 1, 0, 0, 1, 0, 0, 0, 1, fr0, fr1, INVALID_SREG, INVALID_SREG}
Ian Rogerse32ca232012-03-05 10:20:23 -0800145
Elliott Hughes719ace42012-03-09 18:06:03 -0800146enum ResourceEncodingPos {
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 kGPReg0 = 0,
148 kRegSP = 4,
149 kRegLR = -1,
150 kFPReg0 = 16, // xmm0 .. xmm7/xmm15
151 kFPRegEnd = 32,
152 kRegEnd = kFPRegEnd,
153 kCCode = kRegEnd,
154 // The following four bits are for memory disambiguation
155 kDalvikReg, // 1 Dalvik Frame (can be fully disambiguated)
156 kLiteral, // 2 Literal pool (can be fully disambiguated)
157 kHeapRef, // 3 Somewhere on the heap (alias with any other heap)
158 kMustNotAlias, // 4 Guaranteed to be non-alias (eg *(r6+x))
Elliott Hughes719ace42012-03-09 18:06:03 -0800159};
Ian Rogerse32ca232012-03-05 10:20:23 -0800160
161#define ENCODE_REG_LIST(N) ((u8) N)
162#define ENCODE_REG_SP (1ULL << kRegSP)
163#define ENCODE_CCODE (1ULL << kCCode)
164#define ENCODE_FP_STATUS (1ULL << kFPStatus)
165
166/* Abstract memory locations */
167#define ENCODE_DALVIK_REG (1ULL << kDalvikReg)
168#define ENCODE_LITERAL (1ULL << kLiteral)
169#define ENCODE_HEAP_REF (1ULL << kHeapRef)
170#define ENCODE_MUST_NOT_ALIAS (1ULL << kMustNotAlias)
171
172#define ENCODE_ALL (~0ULL)
173#define ENCODE_MEM (ENCODE_DALVIK_REG | ENCODE_LITERAL | \
174 ENCODE_HEAP_REF | ENCODE_MUST_NOT_ALIAS)
175
176#define DECODE_ALIAS_INFO_REG(X) (X & 0xffff)
177#define DECODE_ALIAS_INFO_WIDE(X) ((X & 0x80000000) ? 1 : 0)
178
179/*
180 * Annotate special-purpose core registers:
181 */
182
Elliott Hughes719ace42012-03-09 18:06:03 -0800183enum NativeRegisterPool {
Ian Rogerse32ca232012-03-05 10:20:23 -0800184 r0 = 0,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800185 rAX = r0,
Ian Rogerse32ca232012-03-05 10:20:23 -0800186 r1 = 1,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800187 rCX = r1,
Ian Rogerse32ca232012-03-05 10:20:23 -0800188 r2 = 2,
189 rDX = r2,
190 r3 = 3,
191 rBX = r3,
192 r4sp = 4,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800193 rSP = r4sp,
194 r4sib_no_index = r4sp,
Ian Rogerse32ca232012-03-05 10:20:23 -0800195 r5 = 5,
196 rBP = r5,
Ian Rogers7caad772012-03-30 01:07:54 -0700197 r5sib_no_base = r5,
Ian Rogerse32ca232012-03-05 10:20:23 -0800198 r6 = 6,
199 rSI = r6,
200 r7 = 7,
201 rDI = r7,
jeffhao703f2cd2012-07-13 17:25:52 -0700202#ifndef TARGET_REX_SUPPORT
203 rRET = 8, // fake return address register for core spill mask
204#else
Ian Rogerse32ca232012-03-05 10:20:23 -0800205 r8 = 8,
206 r9 = 9,
207 r10 = 10,
208 r11 = 11,
209 r12 = 12,
210 r13 = 13,
211 r14 = 14,
212 r15 = 15,
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700213 rRET = 16, // fake return address register for core spill mask
jeffhao703f2cd2012-07-13 17:25:52 -0700214#endif
Ian Rogerse32ca232012-03-05 10:20:23 -0800215 fr0 = 0 + FP_REG_OFFSET,
216 fr1 = 1 + FP_REG_OFFSET,
217 fr2 = 2 + FP_REG_OFFSET,
218 fr3 = 3 + FP_REG_OFFSET,
219 fr4 = 4 + FP_REG_OFFSET,
220 fr5 = 5 + FP_REG_OFFSET,
221 fr6 = 6 + FP_REG_OFFSET,
222 fr7 = 7 + FP_REG_OFFSET,
223 fr8 = 8 + FP_REG_OFFSET,
224 fr9 = 9 + FP_REG_OFFSET,
225 fr10 = 10 + FP_REG_OFFSET,
226 fr11 = 11 + FP_REG_OFFSET,
227 fr12 = 12 + FP_REG_OFFSET,
228 fr13 = 13 + FP_REG_OFFSET,
229 fr14 = 14 + FP_REG_OFFSET,
230 fr15 = 15 + FP_REG_OFFSET,
Elliott Hughes719ace42012-03-09 18:06:03 -0800231};
Ian Rogerse32ca232012-03-05 10:20:23 -0800232
233/*
234 * Target-independent aliases
235 */
236
237#define rARG0 rAX
Ian Rogersb41b33b2012-03-20 14:22:54 -0700238#define rARG1 rCX
239#define rARG2 rDX
240#define rARG3 rBX
Ian Rogerse32ca232012-03-05 10:20:23 -0800241#define rRET0 rAX
242#define rRET1 rDX
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700243#define rINVOKE_TGT rAX
Ian Rogerse32ca232012-03-05 10:20:23 -0800244
245#define isPseudoOpcode(opCode) ((int)(opCode) < 0)
246
Ian Rogersb5d09b22012-03-06 22:14:17 -0800247/* X86 condition encodings */
248enum X86ConditionCode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 kX86CondO = 0x0, // overflow
250 kX86CondNo = 0x1, // not overflow
Ian Rogersb5d09b22012-03-06 22:14:17 -0800251
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 kX86CondB = 0x2, // below
253 kX86CondNae = kX86CondB, // not-above-equal
254 kX86CondC = kX86CondB, // carry
Ian Rogersb5d09b22012-03-06 22:14:17 -0800255
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 kX86CondNb = 0x3, // not-below
257 kX86CondAe = kX86CondNb, // above-equal
258 kX86CondNc = kX86CondNb, // not-carry
Ian Rogersb5d09b22012-03-06 22:14:17 -0800259
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 kX86CondZ = 0x4, // zero
261 kX86CondEq = kX86CondZ, // equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800262
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 kX86CondNz = 0x5, // not-zero
264 kX86CondNe = kX86CondNz, // not-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800265
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 kX86CondBe = 0x6, // below-equal
267 kX86CondNa = kX86CondBe, // not-above
Ian Rogersb5d09b22012-03-06 22:14:17 -0800268
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 kX86CondNbe = 0x7, // not-below-equal
270 kX86CondA = kX86CondNbe,// above
Ian Rogersb5d09b22012-03-06 22:14:17 -0800271
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 kX86CondS = 0x8, // sign
273 kX86CondNs = 0x9, // not-sign
Ian Rogersb5d09b22012-03-06 22:14:17 -0800274
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 kX86CondP = 0xA, // 8-bit parity even
276 kX86CondPE = kX86CondP,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800277
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 kX86CondNp = 0xB, // 8-bit parity odd
279 kX86CondPo = kX86CondNp,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800280
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 kX86CondL = 0xC, // less-than
282 kX86CondNge = kX86CondL, // not-greater-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800283
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 kX86CondNl = 0xD, // not-less-than
285 kX86CondGe = kX86CondNl, // not-greater-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800286
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 kX86CondLe = 0xE, // less-than-equal
288 kX86CondNg = kX86CondLe, // not-greater
Ian Rogersb5d09b22012-03-06 22:14:17 -0800289
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 kX86CondNle = 0xF, // not-less-than
291 kX86CondG = kX86CondNle,// greater
Ian Rogersb5d09b22012-03-06 22:14:17 -0800292};
293
Ian Rogerse32ca232012-03-05 10:20:23 -0800294/*
Ian Rogersde797832012-03-06 10:18:10 -0800295 * The following enum defines the list of supported X86 instructions by the
296 * assembler. Their corresponding EncodingMap positions will be defined in
297 * Assemble.cc.
Ian Rogerse32ca232012-03-05 10:20:23 -0800298 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800299enum X86OpCode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 kPseudoIntrinsicRetry = -16,
301 kPseudoSuspendTarget = -15,
302 kPseudoThrowTarget = -14,
303 kPseudoCaseLabel = -13,
304 kPseudoMethodEntry = -12,
305 kPseudoMethodExit = -11,
306 kPseudoBarrier = -10,
307 kPseudoExtended = -9,
308 kPseudoSSARep = -8,
309 kPseudoEntryBlock = -7,
310 kPseudoExitBlock = -6,
311 kPseudoTargetLabel = -5,
312 kPseudoDalvikByteCodeBoundary = -4,
313 kPseudoPseudoAlign4 = -3,
314 kPseudoEHBlockLabel = -2,
315 kPseudoNormalBlockLabel = -1,
316 kX86First,
317 kX8632BitData = kX86First, /* data [31..0] */
318 kX86Bkpt,
319 kX86Nop,
320 // Define groups of binary operations
321 // MR - Memory Register - opcode [base + disp], reg
322 // - lir operands - 0: base, 1: disp, 2: reg
323 // AR - Array Register - opcode [base + index * scale + disp], reg
324 // - lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
325 // TR - Thread Register - opcode fs:[disp], reg - where fs: is equal to Thread::Current()
326 // - lir operands - 0: disp, 1: reg
327 // RR - Register Register - opcode reg1, reg2
328 // - lir operands - 0: reg1, 1: reg2
329 // RM - Register Memory - opcode reg, [base + disp]
330 // - lir operands - 0: reg, 1: base, 2: disp
331 // RA - Register Array - opcode reg, [base + index * scale + disp]
332 // - lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
333 // RT - Register Thread - opcode reg, fs:[disp] - where fs: is equal to Thread::Current()
334 // - lir operands - 0: reg, 1: disp
335 // RI - Register Immediate - opcode reg, #immediate
336 // - lir operands - 0: reg, 1: immediate
337 // MI - Memory Immediate - opcode [base + disp], #immediate
338 // - lir operands - 0: base, 1: disp, 2: immediate
339 // AI - Array Immediate - opcode [base + index * scale + disp], #immediate
340 // - lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
341 // TI - Thread Register - opcode fs:[disp], imm - where fs: is equal to Thread::Current()
342 // - lir operands - 0: disp, 1: imm
Ian Rogers96ab4202012-03-05 19:51:02 -0800343#define BinaryOpCode(opcode) \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800344 opcode ## 8MR, opcode ## 8AR, opcode ## 8TR, \
345 opcode ## 8RR, opcode ## 8RM, opcode ## 8RA, opcode ## 8RT, \
346 opcode ## 8RI, opcode ## 8MI, opcode ## 8AI, opcode ## 8TI, \
347 opcode ## 16MR, opcode ## 16AR, opcode ## 16TR, \
348 opcode ## 16RR, opcode ## 16RM, opcode ## 16RA, opcode ## 16RT, \
349 opcode ## 16RI, opcode ## 16MI, opcode ## 16AI, opcode ## 16TI, \
350 opcode ## 16RI8, opcode ## 16MI8, opcode ## 16AI8, opcode ## 16TI8, \
351 opcode ## 32MR, opcode ## 32AR, opcode ## 32TR, \
352 opcode ## 32RR, opcode ## 32RM, opcode ## 32RA, opcode ## 32RT, \
353 opcode ## 32RI, opcode ## 32MI, opcode ## 32AI, opcode ## 32TI, \
354 opcode ## 32RI8, opcode ## 32MI8, opcode ## 32AI8, opcode ## 32TI8
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 BinaryOpCode(kX86Add),
356 BinaryOpCode(kX86Or),
357 BinaryOpCode(kX86Adc),
358 BinaryOpCode(kX86Sbb),
359 BinaryOpCode(kX86And),
360 BinaryOpCode(kX86Sub),
361 BinaryOpCode(kX86Xor),
362 BinaryOpCode(kX86Cmp),
Ian Rogers96ab4202012-03-05 19:51:02 -0800363#undef BinaryOpCode
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 kX86Imul16RRI, kX86Imul16RMI, kX86Imul16RAI,
365 kX86Imul32RRI, kX86Imul32RMI, kX86Imul32RAI,
366 kX86Imul32RRI8, kX86Imul32RMI8, kX86Imul32RAI8,
367 kX86Mov8MR, kX86Mov8AR, kX86Mov8TR,
368 kX86Mov8RR, kX86Mov8RM, kX86Mov8RA, kX86Mov8RT,
369 kX86Mov8RI, kX86Mov8MI, kX86Mov8AI, kX86Mov8TI,
370 kX86Mov16MR, kX86Mov16AR, kX86Mov16TR,
371 kX86Mov16RR, kX86Mov16RM, kX86Mov16RA, kX86Mov16RT,
372 kX86Mov16RI, kX86Mov16MI, kX86Mov16AI, kX86Mov16TI,
373 kX86Mov32MR, kX86Mov32AR, kX86Mov32TR,
374 kX86Mov32RR, kX86Mov32RM, kX86Mov32RA, kX86Mov32RT,
375 kX86Mov32RI, kX86Mov32MI, kX86Mov32AI, kX86Mov32TI,
376 kX86Lea32RA,
377 // RC - Register CL - opcode reg, CL
378 // - lir operands - 0: reg, 1: CL
379 // MC - Memory CL - opcode [base + disp], CL
380 // - lir operands - 0: base, 1: disp, 2: CL
381 // AC - Array CL - opcode [base + index * scale + disp], CL
382 // - lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: CL
Ian Rogersb5d09b22012-03-06 22:14:17 -0800383#define BinaryShiftOpCode(opcode) \
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 opcode ## 8RI, opcode ## 8MI, opcode ## 8AI, \
385 opcode ## 8RC, opcode ## 8MC, opcode ## 8AC, \
386 opcode ## 16RI, opcode ## 16MI, opcode ## 16AI, \
387 opcode ## 16RC, opcode ## 16MC, opcode ## 16AC, \
388 opcode ## 32RI, opcode ## 32MI, opcode ## 32AI, \
389 opcode ## 32RC, opcode ## 32MC, opcode ## 32AC
390 BinaryShiftOpCode(kX86Rol),
391 BinaryShiftOpCode(kX86Ror),
392 BinaryShiftOpCode(kX86Rcl),
393 BinaryShiftOpCode(kX86Rcr),
394 BinaryShiftOpCode(kX86Sal),
395 BinaryShiftOpCode(kX86Shr),
396 BinaryShiftOpCode(kX86Sar),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800397#undef BinaryShiftOpcode
jeffhao77ae36b2012-08-07 14:18:16 -0700398 kX86Cmc,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800399#define UnaryOpcode(opcode, reg, mem, array) \
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 opcode ## 8 ## reg, opcode ## 8 ## mem, opcode ## 8 ## array, \
401 opcode ## 16 ## reg, opcode ## 16 ## mem, opcode ## 16 ## array, \
402 opcode ## 32 ## reg, opcode ## 32 ## mem, opcode ## 32 ## array
403 UnaryOpcode(kX86Test, RI, MI, AI),
404 UnaryOpcode(kX86Not, R, M, A),
405 UnaryOpcode(kX86Neg, R, M, A),
406 UnaryOpcode(kX86Mul, DaR, DaM, DaA),
407 UnaryOpcode(kX86Imul, DaR, DaM, DaA),
408 UnaryOpcode(kX86Divmod, DaR, DaM, DaA),
409 UnaryOpcode(kX86Idivmod, DaR, DaM, DaA),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800410#undef UnaryOpcode
411#define Binary0fOpCode(opcode) \
412 opcode ## RR, opcode ## RM, opcode ## RA
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 Binary0fOpCode(kX86Movsd),
414 kX86MovsdMR,
415 kX86MovsdAR,
416 Binary0fOpCode(kX86Movss),
417 kX86MovssMR,
418 kX86MovssAR,
419 Binary0fOpCode(kX86Cvtsi2sd), // int to double
420 Binary0fOpCode(kX86Cvtsi2ss), // int to float
421 Binary0fOpCode(kX86Cvttsd2si),// truncating double to int
422 Binary0fOpCode(kX86Cvttss2si),// truncating float to int
423 Binary0fOpCode(kX86Cvtsd2si), // rounding double to int
424 Binary0fOpCode(kX86Cvtss2si), // rounding float to int
425 Binary0fOpCode(kX86Ucomisd), // unordered double compare
426 Binary0fOpCode(kX86Ucomiss), // unordered float compare
427 Binary0fOpCode(kX86Comisd), // double compare
428 Binary0fOpCode(kX86Comiss), // float compare
429 Binary0fOpCode(kX86Orps), // or of floating point registers
430 Binary0fOpCode(kX86Xorps), // xor of floating point registers
431 Binary0fOpCode(kX86Addsd), // double add
432 Binary0fOpCode(kX86Addss), // float add
433 Binary0fOpCode(kX86Mulsd), // double multiply
434 Binary0fOpCode(kX86Mulss), // float multiply
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 Binary0fOpCode(kX86Cvtsd2ss), // double to float
jeffhao292188d2012-05-17 15:45:04 -0700436 Binary0fOpCode(kX86Cvtss2sd), // float to double
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 Binary0fOpCode(kX86Subsd), // double subtract
438 Binary0fOpCode(kX86Subss), // float subtract
439 Binary0fOpCode(kX86Divsd), // double divide
440 Binary0fOpCode(kX86Divss), // float divide
jeffhaofdffdf82012-07-11 16:08:43 -0700441 kX86PsrlqRI, // right shift of floating point registers
442 kX86PsllqRI, // left shift of floating point registers
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 Binary0fOpCode(kX86Movdxr), // move into xmm from gpr
jeffhaofdffdf82012-07-11 16:08:43 -0700444 kX86MovdrxRR, kX86MovdrxMR, kX86MovdrxAR,// move into reg from xmm
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 kX86Set8R, kX86Set8M, kX86Set8A,// set byte depending on condition operand
446 kX86Mfence, // memory barrier
447 Binary0fOpCode(kX86Imul16), // 16bit multiply
448 Binary0fOpCode(kX86Imul32), // 32bit multiply
jeffhao83025762012-08-02 11:08:56 -0700449 kX86CmpxchgRR, kX86CmpxchgMR, kX86CmpxchgAR,// compare and exchange
450 kX86LockCmpxchgRR, kX86LockCmpxchgMR, kX86LockCmpxchgAR,// locked compare and exchange
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 Binary0fOpCode(kX86Movzx8), // zero-extend 8-bit value
452 Binary0fOpCode(kX86Movzx16), // zero-extend 16-bit value
453 Binary0fOpCode(kX86Movsx8), // sign-extend 8-bit value
454 Binary0fOpCode(kX86Movsx16), // sign-extend 16-bit value
Ian Rogersb5d09b22012-03-06 22:14:17 -0800455#undef Binary0fOpCode
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 kX86Jcc8, kX86Jcc32, // jCC rel8/32; lir operands - 0: rel, 1: CC, target assigned
457 kX86Jmp8, kX86Jmp32, // jmp rel8/32; lir operands - 0: rel, target assigned
458 kX86JmpR, // jmp reg; lir operands - 0: reg
459 kX86CallR, // call reg; lir operands - 0: reg
460 kX86CallM, // call [base + disp]; lir operands - 0: base, 1: disp
461 kX86CallA, // call [base + index * scale + disp]
462 // lir operands - 0: base, 1: index, 2: scale, 3: disp
463 kX86CallT, // call fs:[disp]; fs: is equal to Thread::Current(); lir operands - 0: disp
464 kX86Ret, // ret; no lir operands
465 kX86StartOfMethod, // call 0; pop reg; sub reg, # - generate start of method into reg
466 // lir operands - 0: reg
467 kX86PcRelLoadRA, // mov reg, [base + index * scale + PC relative displacement]
468 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
469 kX86PcRelAdr, // mov reg, PC relative displacement; lir operands - 0: reg, 1: table
470 kX86Last
Elliott Hughes719ace42012-03-09 18:06:03 -0800471};
Ian Rogerse32ca232012-03-05 10:20:23 -0800472
Ian Rogersde797832012-03-06 10:18:10 -0800473/* Instruction assembly fieldLoc kind */
Elliott Hughes719ace42012-03-09 18:06:03 -0800474enum X86EncodingKind {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800475 kData, // Special case for raw data.
476 kNop, // Special case for variable length nop.
477 kNullary, // Opcode that takes no arguments.
478 kReg, kMem, kArray, // R, M and A instruction kinds.
479 kMemReg, kArrayReg, kThreadReg, // MR, AR and TR instruction kinds.
480 kRegReg, kRegMem, kRegArray, kRegThread, // RR, RM, RA and RT instruction kinds.
jeffhaofdffdf82012-07-11 16:08:43 -0700481 kRegRegStore, // RR following the store modrm reg-reg encoding rather than the load.
Ian Rogersb5d09b22012-03-06 22:14:17 -0800482 kRegImm, kMemImm, kArrayImm, kThreadImm, // RI, MI, AI and TI instruction kinds.
483 kRegRegImm, kRegMemImm, kRegArrayImm, // RRI, RMI and RAI instruction kinds.
484 kMovRegImm, // Shorter form move RI.
485 kShiftRegImm, kShiftMemImm, kShiftArrayImm, // Shift opcode with immediate.
486 kShiftRegCl, kShiftMemCl, kShiftArrayCl, // Shift opcode with register CL.
487 kRegRegReg, kRegRegMem, kRegRegArray, // RRR, RRM, RRA instruction kinds.
488 kRegCond, kMemCond, kArrayCond, // R, M, A instruction kinds following by a condition.
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 kJmp, kJcc, kCall, // Branch instruction kinds.
490 kPcRel, // Operation with displacement that is PC relative
491 kMacro, // An instruction composing multiple others
492 kUnimplemented // Encoding used when an instruction isn't yet implemented.
Elliott Hughes719ace42012-03-09 18:06:03 -0800493};
Ian Rogersde797832012-03-06 10:18:10 -0800494
Ian Rogersde797832012-03-06 10:18:10 -0800495/* Struct used to define the EncodingMap positions for each X86 opcode */
Elliott Hughes719ace42012-03-09 18:06:03 -0800496struct X86EncodingMap {
Ian Rogersde797832012-03-06 10:18:10 -0800497 X86OpCode opcode; // e.g. kOpAddRI
498 X86EncodingKind kind; // Used to discriminate in the union below
499 int flags;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800500 struct {
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 uint8_t prefix1; // non-zero => a prefix byte
502 uint8_t prefix2; // non-zero => a second prefix byte
503 uint8_t opcode; // 1 byte opcode
504 uint8_t extra_opcode1; // possible extra opcode byte
505 uint8_t extra_opcode2; // possible second extra opcode byte
506 // 3bit opcode that gets encoded in the register bits of the modrm byte, use determined by the
507 // encoding kind
508 uint8_t modrm_opcode;
509 uint8_t ax_opcode; // non-zero => shorter encoding for AX as a destination
510 uint8_t immediate_bytes; // number of bytes of immediate
Ian Rogersde797832012-03-06 10:18:10 -0800511 } skeleton;
512 const char *name;
513 const char* fmt;
Elliott Hughes719ace42012-03-09 18:06:03 -0800514};
Ian Rogersde797832012-03-06 10:18:10 -0800515
516extern X86EncodingMap EncodingMap[kX86Last];
517
buzbeea7678db2012-03-05 15:35:46 -0800518// FIXME: mem barrier type - what do we do for x86?
519#define kSY 0
520#define kST 0
521
Ian Rogerse32ca232012-03-05 10:20:23 -0800522/* Bit flags describing the behavior of each native opcode */
Elliott Hughes719ace42012-03-09 18:06:03 -0800523enum X86OpFeatureFlags {
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 kIsBranch = 0,
525 kRegDef0,
526 kRegDef1,
jeffhaoe2962482012-06-28 11:29:57 -0700527 kRegDefA,
528 kRegDefD,
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 kRegDefSP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 kRegUse0,
531 kRegUse1,
532 kRegUse2,
533 kRegUse3,
jeffhaoe2962482012-06-28 11:29:57 -0700534 kRegUse4,
535 kRegUseA,
536 kRegUseC,
537 kRegUseD,
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 kRegUseSP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700539 kNoOperand,
540 kIsUnaryOp,
541 kIsBinaryOp,
542 kIsTertiaryOp,
543 kIsQuadOp,
544 kIsQuinOp,
545 kIsSextupleOp,
546 kIsIT,
547 kSetsCCodes,
548 kUsesCCodes,
549 kMemLoad,
550 kMemStore,
551 kPCRelFixup,
Ian Rogerse32ca232012-03-05 10:20:23 -0800552// FIXME: add NEEDS_FIXUP to instruction attributes
Elliott Hughes719ace42012-03-09 18:06:03 -0800553};
Ian Rogerse32ca232012-03-05 10:20:23 -0800554
555#define IS_LOAD (1 << kMemLoad)
556#define IS_STORE (1 << kMemStore)
557#define IS_BRANCH (1 << kIsBranch)
558#define REG_DEF0 (1 << kRegDef0)
559#define REG_DEF1 (1 << kRegDef1)
jeffhaoe2962482012-06-28 11:29:57 -0700560#define REG_DEFA (1 << kRegDefA)
561#define REG_DEFD (1 << kRegDefD)
Ian Rogerse32ca232012-03-05 10:20:23 -0800562#define REG_DEF_SP (1 << kRegDefSP)
Ian Rogerse32ca232012-03-05 10:20:23 -0800563#define REG_USE0 (1 << kRegUse0)
564#define REG_USE1 (1 << kRegUse1)
565#define REG_USE2 (1 << kRegUse2)
566#define REG_USE3 (1 << kRegUse3)
jeffhaoe2962482012-06-28 11:29:57 -0700567#define REG_USE4 (1 << kRegUse4)
568#define REG_USEA (1 << kRegUseA)
569#define REG_USEC (1 << kRegUseC)
570#define REG_USED (1 << kRegUseD)
Ian Rogerse32ca232012-03-05 10:20:23 -0800571#define REG_USE_SP (1 << kRegUseSP)
Ian Rogerse32ca232012-03-05 10:20:23 -0800572#define NO_OPERAND (1 << kNoOperand)
573#define IS_UNARY_OP (1 << kIsUnaryOp)
574#define IS_BINARY_OP (1 << kIsBinaryOp)
575#define IS_TERTIARY_OP (1 << kIsTertiaryOp)
576#define IS_QUAD_OP (1 << kIsQuadOp)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800577#define IS_QUIN_OP (1 << kIsQuinOp)
578#define IS_SEXTUPLE_OP (1 << kIsSextupleOp)
Ian Rogerse32ca232012-03-05 10:20:23 -0800579#define IS_IT (1 << kIsIT)
580#define SETS_CCODES (1 << kSetsCCodes)
581#define USES_CCODES (1 << kUsesCCodes)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800582#define NEEDS_FIXUP (1 << kPCRelFixup)
Ian Rogerse32ca232012-03-05 10:20:23 -0800583
584/* attributes, included for compatibility */
585#define REG_DEF_FPCS_LIST0 (0)
586#define REG_DEF_FPCS_LIST2 (0)
587
588
589/* Common combo register usage patterns */
590#define REG_USE01 (REG_USE0 | REG_USE1)
591#define REG_USE02 (REG_USE0 | REG_USE2)
592#define REG_USE012 (REG_USE01 | REG_USE2)
jeffhaoe2962482012-06-28 11:29:57 -0700593#define REG_USE014 (REG_USE01 | REG_USE4)
Ian Rogerse32ca232012-03-05 10:20:23 -0800594#define REG_DEF0_USE0 (REG_DEF0 | REG_USE0)
595#define REG_DEF0_USE1 (REG_DEF0 | REG_USE1)
jeffhaoe2962482012-06-28 11:29:57 -0700596#define REG_DEF0_USE12 (REG_DEF0_USE1 | REG_USE2)
597#define REG_DEFA_USEA (REG_DEFA | REG_USEA)
598#define REG_DEFAD_USEA (REG_DEFA_USEA | REG_DEFD)
599#define REG_DEFAD_USEAD (REG_DEFAD_USEA | REG_USED)
Ian Rogerse32ca232012-03-05 10:20:23 -0800600
Ian Rogerse32ca232012-03-05 10:20:23 -0800601/* Keys for target-specific scheduling and other optimization hints */
Elliott Hughes719ace42012-03-09 18:06:03 -0800602enum X86TargetOptHints {
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 kMaxHoistDistance,
Elliott Hughes719ace42012-03-09 18:06:03 -0800604};
Ian Rogerse32ca232012-03-05 10:20:23 -0800605
Ian Rogersb5d09b22012-03-06 22:14:17 -0800606/* Offsets of high and low halves of a 64bit value */
607#define LOWORD_OFFSET 0
608#define HIWORD_OFFSET 4
609
610/* Segment override instruction prefix used for quick TLS access to Thread::Current() */
611#define THREAD_PREFIX 0x64
Ian Rogerse32ca232012-03-05 10:20:23 -0800612
Ian Rogersde797832012-03-06 10:18:10 -0800613#define IS_SIMM8(v) ((-128 <= (v)) && ((v) <= 127))
Ian Rogersb5d09b22012-03-06 22:14:17 -0800614#define IS_SIMM16(v) ((-32768 <= (v)) && ((v) <= 32767))
Ian Rogerse32ca232012-03-05 10:20:23 -0800615
616} // namespace art
617
618#endif // ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_