blob: e7a629aa0b9e34c106984db993e9ade3d8f771b5 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000017#include <string>
18#include <inttypes.h>
19
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "codegen_x86.h"
21#include "dex/compiler_internals.h"
22#include "dex/quick/mir_to_lir-inl.h"
Mark Mendelle19c91f2014-02-25 08:19:08 -080023#include "mirror/array.h"
24#include "mirror/string.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "x86_lir.h"
26
Brian Carlstrom7940e442013-07-12 13:46:57 -070027namespace art {
28
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070029static const RegStorage core_regs_arr_32[] = {
30 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_32, rs_rBP, rs_rSI, rs_rDI,
31};
32static const RegStorage core_regs_arr_64[] = {
33 rs_rAX, rs_rCX, rs_rDX, rs_rBX, rs_rX86_SP_64, rs_rBP, rs_rSI, rs_rDI,
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#ifdef TARGET_REX_SUPPORT
buzbee091cc402014-03-31 10:14:40 -070035 rs_r8, rs_r9, rs_r10, rs_r11, rs_r12, rs_r13, rs_r14, rs_r15
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#endif
37};
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070038static const RegStorage sp_regs_arr_32[] = {
39 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
40};
41static const RegStorage sp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070042 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
Brian Carlstrom7940e442013-07-12 13:46:57 -070043#ifdef TARGET_REX_SUPPORT
buzbee091cc402014-03-31 10:14:40 -070044 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070045#endif
46};
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070047static const RegStorage dp_regs_arr_32[] = {
48 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
49};
50static const RegStorage dp_regs_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070051 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
Brian Carlstrom7940e442013-07-12 13:46:57 -070052#ifdef TARGET_REX_SUPPORT
buzbee091cc402014-03-31 10:14:40 -070053 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
Brian Carlstrom7940e442013-07-12 13:46:57 -070054#endif
55};
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070056static const RegStorage reserved_regs_arr_32[] = {rs_rX86_SP_32};
57static const RegStorage reserved_regs_arr_64[] = {rs_rX86_SP_64};
58static const RegStorage core_temps_arr_32[] = {rs_rAX, rs_rCX, rs_rDX, rs_rBX};
59static const RegStorage core_temps_arr_64[] = {
60 rs_rAX, rs_rCX, rs_rDX, rs_rSI, rs_rDI,
61#ifdef TARGET_REX_SUPPORT
62 rs_r8, rs_r9, rs_r10, rs_r11
63#endif
64};
65static const RegStorage sp_temps_arr_32[] = {
66 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
67};
68static const RegStorage sp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070069 rs_fr0, rs_fr1, rs_fr2, rs_fr3, rs_fr4, rs_fr5, rs_fr6, rs_fr7,
70#ifdef TARGET_REX_SUPPORT
71 rs_fr8, rs_fr9, rs_fr10, rs_fr11, rs_fr12, rs_fr13, rs_fr14, rs_fr15
72#endif
73};
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070074static const RegStorage dp_temps_arr_32[] = {
75 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
76};
77static const RegStorage dp_temps_arr_64[] = {
buzbee091cc402014-03-31 10:14:40 -070078 rs_dr0, rs_dr1, rs_dr2, rs_dr3, rs_dr4, rs_dr5, rs_dr6, rs_dr7,
79#ifdef TARGET_REX_SUPPORT
80 rs_dr8, rs_dr9, rs_dr10, rs_dr11, rs_dr12, rs_dr13, rs_dr14, rs_dr15
81#endif
82};
83
buzbeeb01bf152014-05-13 15:59:07 -070084static const std::vector<RegStorage> empty_pool;
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +070085static const std::vector<RegStorage> core_regs_32(core_regs_arr_32,
86 core_regs_arr_32 + sizeof(core_regs_arr_32) / sizeof(core_regs_arr_32[0]));
87static const std::vector<RegStorage> core_regs_64(core_regs_arr_64,
88 core_regs_arr_64 + sizeof(core_regs_arr_64) / sizeof(core_regs_arr_64[0]));
89static const std::vector<RegStorage> sp_regs_32(sp_regs_arr_32,
90 sp_regs_arr_32 + sizeof(sp_regs_arr_32) / sizeof(sp_regs_arr_32[0]));
91static const std::vector<RegStorage> sp_regs_64(sp_regs_arr_64,
92 sp_regs_arr_64 + sizeof(sp_regs_arr_64) / sizeof(sp_regs_arr_64[0]));
93static const std::vector<RegStorage> dp_regs_32(dp_regs_arr_32,
94 dp_regs_arr_32 + sizeof(dp_regs_arr_32) / sizeof(dp_regs_arr_32[0]));
95static const std::vector<RegStorage> dp_regs_64(dp_regs_arr_64,
96 dp_regs_arr_64 + sizeof(dp_regs_arr_64) / sizeof(dp_regs_arr_64[0]));
97static const std::vector<RegStorage> reserved_regs_32(reserved_regs_arr_32,
98 reserved_regs_arr_32 + sizeof(reserved_regs_arr_32) / sizeof(reserved_regs_arr_32[0]));
99static const std::vector<RegStorage> reserved_regs_64(reserved_regs_arr_64,
100 reserved_regs_arr_64 + sizeof(reserved_regs_arr_64) / sizeof(reserved_regs_arr_64[0]));
101static const std::vector<RegStorage> core_temps_32(core_temps_arr_32,
102 core_temps_arr_32 + sizeof(core_temps_arr_32) / sizeof(core_temps_arr_32[0]));
103static const std::vector<RegStorage> core_temps_64(core_temps_arr_64,
104 core_temps_arr_64 + sizeof(core_temps_arr_64) / sizeof(core_temps_arr_64[0]));
105static const std::vector<RegStorage> sp_temps_32(sp_temps_arr_32,
106 sp_temps_arr_32 + sizeof(sp_temps_arr_32) / sizeof(sp_temps_arr_32[0]));
107static const std::vector<RegStorage> sp_temps_64(sp_temps_arr_64,
108 sp_temps_arr_64 + sizeof(sp_temps_arr_64) / sizeof(sp_temps_arr_64[0]));
109static const std::vector<RegStorage> dp_temps_32(dp_temps_arr_32,
110 dp_temps_arr_32 + sizeof(dp_temps_arr_32) / sizeof(dp_temps_arr_32[0]));
111static const std::vector<RegStorage> dp_temps_64(dp_temps_arr_64,
112 dp_temps_arr_64 + sizeof(dp_temps_arr_64) / sizeof(dp_temps_arr_64[0]));
113
114RegStorage rs_rX86_SP;
115
116X86NativeRegisterPool rX86_ARG0;
117X86NativeRegisterPool rX86_ARG1;
118X86NativeRegisterPool rX86_ARG2;
119X86NativeRegisterPool rX86_ARG3;
120X86NativeRegisterPool rX86_FARG0;
121X86NativeRegisterPool rX86_FARG1;
122X86NativeRegisterPool rX86_FARG2;
123X86NativeRegisterPool rX86_FARG3;
124X86NativeRegisterPool rX86_RET0;
125X86NativeRegisterPool rX86_RET1;
126X86NativeRegisterPool rX86_INVOKE_TGT;
127X86NativeRegisterPool rX86_COUNT;
128
129RegStorage rs_rX86_ARG0;
130RegStorage rs_rX86_ARG1;
131RegStorage rs_rX86_ARG2;
132RegStorage rs_rX86_ARG3;
133RegStorage rs_rX86_FARG0;
134RegStorage rs_rX86_FARG1;
135RegStorage rs_rX86_FARG2;
136RegStorage rs_rX86_FARG3;
137RegStorage rs_rX86_RET0;
138RegStorage rs_rX86_RET1;
139RegStorage rs_rX86_INVOKE_TGT;
140RegStorage rs_rX86_COUNT;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700142RegLocation X86Mir2Lir::LocCReturn() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000143 return x86_loc_c_return;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144}
145
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146RegLocation X86Mir2Lir::LocCReturnWide() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000147 return x86_loc_c_return_wide;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148}
149
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150RegLocation X86Mir2Lir::LocCReturnFloat() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000151 return x86_loc_c_return_float;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152}
153
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700154RegLocation X86Mir2Lir::LocCReturnDouble() {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000155 return x86_loc_c_return_double;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156}
157
158// Return a target-dependent special register.
buzbee2700f7e2014-03-07 09:46:20 -0800159RegStorage X86Mir2Lir::TargetReg(SpecialTargetRegister reg) {
buzbee091cc402014-03-31 10:14:40 -0700160 RegStorage res_reg = RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 switch (reg) {
buzbee091cc402014-03-31 10:14:40 -0700162 case kSelf: res_reg = RegStorage::InvalidReg(); break;
163 case kSuspend: res_reg = RegStorage::InvalidReg(); break;
164 case kLr: res_reg = RegStorage::InvalidReg(); break;
165 case kPc: res_reg = RegStorage::InvalidReg(); break;
166 case kSp: res_reg = rs_rX86_SP; break;
167 case kArg0: res_reg = rs_rX86_ARG0; break;
168 case kArg1: res_reg = rs_rX86_ARG1; break;
169 case kArg2: res_reg = rs_rX86_ARG2; break;
170 case kArg3: res_reg = rs_rX86_ARG3; break;
171 case kFArg0: res_reg = rs_rX86_FARG0; break;
172 case kFArg1: res_reg = rs_rX86_FARG1; break;
173 case kFArg2: res_reg = rs_rX86_FARG2; break;
174 case kFArg3: res_reg = rs_rX86_FARG3; break;
175 case kRet0: res_reg = rs_rX86_RET0; break;
176 case kRet1: res_reg = rs_rX86_RET1; break;
177 case kInvokeTgt: res_reg = rs_rX86_INVOKE_TGT; break;
178 case kHiddenArg: res_reg = rs_rAX; break;
179 case kHiddenFpArg: res_reg = rs_fr0; break;
180 case kCount: res_reg = rs_rX86_COUNT; break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
buzbee091cc402014-03-31 10:14:40 -0700182 return res_reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183}
184
buzbee2700f7e2014-03-07 09:46:20 -0800185RegStorage X86Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800186 // For the 32-bit internal ABI, the first 3 arguments are passed in registers.
187 // TODO: This is not 64-bit compliant and depends on new internal ABI.
188 switch (arg_num) {
189 case 0:
buzbee2700f7e2014-03-07 09:46:20 -0800190 return rs_rX86_ARG1;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800191 case 1:
buzbee2700f7e2014-03-07 09:46:20 -0800192 return rs_rX86_ARG2;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800193 case 2:
buzbee2700f7e2014-03-07 09:46:20 -0800194 return rs_rX86_ARG3;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800195 default:
buzbee2700f7e2014-03-07 09:46:20 -0800196 return RegStorage::InvalidReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800197 }
198}
199
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200/*
201 * Decode the register id.
202 */
buzbee091cc402014-03-31 10:14:40 -0700203uint64_t X86Mir2Lir::GetRegMaskCommon(RegStorage reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 uint64_t seed;
205 int shift;
206 int reg_id;
207
buzbee091cc402014-03-31 10:14:40 -0700208 reg_id = reg.GetRegNum();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 /* Double registers in x86 are just a single FP register */
210 seed = 1;
211 /* FP register starts at bit position 16 */
buzbee091cc402014-03-31 10:14:40 -0700212 shift = reg.IsFloat() ? kX86FPReg0 : 0;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 /* Expand the double register id into single offset */
214 shift += reg_id;
215 return (seed << shift);
216}
217
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700218uint64_t X86Mir2Lir::GetPCUseDefEncoding() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 /*
220 * FIXME: might make sense to use a virtual resource encoding bit for pc. Might be
221 * able to clean up some of the x86/Arm_Mips differences
222 */
223 LOG(FATAL) << "Unexpected call to GetPCUseDefEncoding for x86";
224 return 0ULL;
225}
226
buzbeeb48819d2013-09-14 16:15:25 -0700227void X86Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700228 DCHECK(cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64);
buzbeeb48819d2013-09-14 16:15:25 -0700229 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230
231 // X86-specific resource map setup here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 if (flags & REG_USE_SP) {
buzbeeb48819d2013-09-14 16:15:25 -0700233 lir->u.m.use_mask |= ENCODE_X86_REG_SP;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 }
235
236 if (flags & REG_DEF_SP) {
buzbeeb48819d2013-09-14 16:15:25 -0700237 lir->u.m.def_mask |= ENCODE_X86_REG_SP;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 }
239
240 if (flags & REG_DEFA) {
buzbee091cc402014-03-31 10:14:40 -0700241 SetupRegMask(&lir->u.m.def_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243
244 if (flags & REG_DEFD) {
buzbee091cc402014-03-31 10:14:40 -0700245 SetupRegMask(&lir->u.m.def_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247 if (flags & REG_USEA) {
buzbee091cc402014-03-31 10:14:40 -0700248 SetupRegMask(&lir->u.m.use_mask, rs_rAX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 }
250
251 if (flags & REG_USEC) {
buzbee091cc402014-03-31 10:14:40 -0700252 SetupRegMask(&lir->u.m.use_mask, rs_rCX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 }
254
255 if (flags & REG_USED) {
buzbee091cc402014-03-31 10:14:40 -0700256 SetupRegMask(&lir->u.m.use_mask, rs_rDX.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 }
Vladimir Marko70b797d2013-12-03 15:25:24 +0000258
259 if (flags & REG_USEB) {
buzbee091cc402014-03-31 10:14:40 -0700260 SetupRegMask(&lir->u.m.use_mask, rs_rBX.GetReg());
Vladimir Marko70b797d2013-12-03 15:25:24 +0000261 }
Mark Mendell4028a6c2014-02-19 20:06:20 -0800262
263 // Fixup hard to describe instruction: Uses rAX, rCX, rDI; sets rDI.
264 if (lir->opcode == kX86RepneScasw) {
buzbee091cc402014-03-31 10:14:40 -0700265 SetupRegMask(&lir->u.m.use_mask, rs_rAX.GetReg());
266 SetupRegMask(&lir->u.m.use_mask, rs_rCX.GetReg());
267 SetupRegMask(&lir->u.m.use_mask, rs_rDI.GetReg());
268 SetupRegMask(&lir->u.m.def_mask, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800269 }
Serguei Katkove90501d2014-03-12 15:56:54 +0700270
271 if (flags & USE_FP_STACK) {
272 lir->u.m.use_mask |= ENCODE_X86_FP_STACK;
273 lir->u.m.def_mask |= ENCODE_X86_FP_STACK;
274 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275}
276
277/* For dumping instructions */
278static const char* x86RegName[] = {
279 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
280 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
281};
282
283static const char* x86CondName[] = {
284 "O",
285 "NO",
286 "B/NAE/C",
287 "NB/AE/NC",
288 "Z/EQ",
289 "NZ/NE",
290 "BE/NA",
291 "NBE/A",
292 "S",
293 "NS",
294 "P/PE",
295 "NP/PO",
296 "L/NGE",
297 "NL/GE",
298 "LE/NG",
299 "NLE/G"
300};
301
302/*
303 * Interpret a format string and build a string no longer than size
304 * See format key in Assemble.cc.
305 */
306std::string X86Mir2Lir::BuildInsnString(const char *fmt, LIR *lir, unsigned char* base_addr) {
307 std::string buf;
308 size_t i = 0;
309 size_t fmt_len = strlen(fmt);
310 while (i < fmt_len) {
311 if (fmt[i] != '!') {
312 buf += fmt[i];
313 i++;
314 } else {
315 i++;
316 DCHECK_LT(i, fmt_len);
317 char operand_number_ch = fmt[i];
318 i++;
319 if (operand_number_ch == '!') {
320 buf += "!";
321 } else {
322 int operand_number = operand_number_ch - '0';
323 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
324 DCHECK_LT(i, fmt_len);
325 int operand = lir->operands[operand_number];
326 switch (fmt[i]) {
327 case 'c':
328 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
329 buf += x86CondName[operand];
330 break;
331 case 'd':
332 buf += StringPrintf("%d", operand);
333 break;
334 case 'p': {
buzbee0d829482013-10-11 15:24:55 -0700335 EmbeddedData *tab_rec = reinterpret_cast<EmbeddedData*>(UnwrapPointer(operand));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 buf += StringPrintf("0x%08x", tab_rec->offset);
337 break;
338 }
339 case 'r':
buzbee091cc402014-03-31 10:14:40 -0700340 if (RegStorage::IsFloat(operand)) {
341 int fp_reg = RegStorage::RegNum(operand);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 buf += StringPrintf("xmm%d", fp_reg);
343 } else {
buzbee091cc402014-03-31 10:14:40 -0700344 int reg_num = RegStorage::RegNum(operand);
345 DCHECK_LT(static_cast<size_t>(reg_num), sizeof(x86RegName));
346 buf += x86RegName[reg_num];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 }
348 break;
349 case 't':
Ian Rogers107c31e2014-01-23 20:55:29 -0800350 buf += StringPrintf("0x%08" PRIxPTR " (L%p)",
351 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + operand,
352 lir->target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 break;
354 default:
355 buf += StringPrintf("DecodeError '%c'", fmt[i]);
356 break;
357 }
358 i++;
359 }
360 }
361 }
362 return buf;
363}
364
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700365void X86Mir2Lir::DumpResourceMask(LIR *x86LIR, uint64_t mask, const char *prefix) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 char buf[256];
367 buf[0] = 0;
368
369 if (mask == ENCODE_ALL) {
370 strcpy(buf, "all");
371 } else {
372 char num[8];
373 int i;
374
375 for (i = 0; i < kX86RegEnd; i++) {
376 if (mask & (1ULL << i)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800377 snprintf(num, arraysize(num), "%d ", i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 strcat(buf, num);
379 }
380 }
381
382 if (mask & ENCODE_CCODE) {
383 strcat(buf, "cc ");
384 }
385 /* Memory bits */
386 if (x86LIR && (mask & ENCODE_DALVIK_REG)) {
Ian Rogers988e6ea2014-01-08 11:30:50 -0800387 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
388 DECODE_ALIAS_INFO_REG(x86LIR->flags.alias_info),
389 (DECODE_ALIAS_INFO_WIDE(x86LIR->flags.alias_info)) ? "(+1)" : "");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 }
391 if (mask & ENCODE_LITERAL) {
392 strcat(buf, "lit ");
393 }
394
395 if (mask & ENCODE_HEAP_REF) {
396 strcat(buf, "heap ");
397 }
398 if (mask & ENCODE_MUST_NOT_ALIAS) {
399 strcat(buf, "noalias ");
400 }
401 }
402 if (buf[0]) {
403 LOG(INFO) << prefix << ": " << buf;
404 }
405}
406
407void X86Mir2Lir::AdjustSpillMask() {
408 // Adjustment for LR spilling, x86 has no LR so nothing to do here
buzbee091cc402014-03-31 10:14:40 -0700409 core_spill_mask_ |= (1 << rs_rRET.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 num_core_spills_++;
411}
412
413/*
414 * Mark a callee-save fp register as promoted. Note that
415 * vpush/vpop uses contiguous register lists so we must
416 * include any holes in the mask. Associate holes with
417 * Dalvik register INVALID_VREG (0xFFFFU).
418 */
buzbee091cc402014-03-31 10:14:40 -0700419void X86Mir2Lir::MarkPreservedSingle(int v_reg, RegStorage reg) {
420 UNIMPLEMENTED(FATAL) << "MarkPreservedSingle";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421}
422
buzbee091cc402014-03-31 10:14:40 -0700423void X86Mir2Lir::MarkPreservedDouble(int v_reg, RegStorage reg) {
424 UNIMPLEMENTED(FATAL) << "MarkPreservedDouble";
buzbee2700f7e2014-03-07 09:46:20 -0800425}
426
Mark Mendelle87f9b52014-04-30 14:13:18 -0400427RegStorage X86Mir2Lir::AllocateByteRegister() {
428 return AllocTypedTemp(false, kCoreReg);
429}
430
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431/* Clobber all regs that might be used by an external C call */
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000432void X86Mir2Lir::ClobberCallerSave() {
buzbee091cc402014-03-31 10:14:40 -0700433 Clobber(rs_rAX);
434 Clobber(rs_rCX);
435 Clobber(rs_rDX);
436 Clobber(rs_rBX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437}
438
439RegLocation X86Mir2Lir::GetReturnWideAlt() {
440 RegLocation res = LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -0700441 DCHECK(res.reg.GetLowReg() == rs_rAX.GetReg());
442 DCHECK(res.reg.GetHighReg() == rs_rDX.GetReg());
443 Clobber(rs_rAX);
444 Clobber(rs_rDX);
445 MarkInUse(rs_rAX);
446 MarkInUse(rs_rDX);
447 MarkWide(res.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 return res;
449}
450
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700451RegLocation X86Mir2Lir::GetReturnAlt() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 RegLocation res = LocCReturn();
buzbee091cc402014-03-31 10:14:40 -0700453 res.reg.SetReg(rs_rDX.GetReg());
454 Clobber(rs_rDX);
455 MarkInUse(rs_rDX);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 return res;
457}
458
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700460void X86Mir2Lir::LockCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700461 LockTemp(rs_rX86_ARG0);
462 LockTemp(rs_rX86_ARG1);
463 LockTemp(rs_rX86_ARG2);
464 LockTemp(rs_rX86_ARG3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465}
466
467/* To be used when explicitly managing register use */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468void X86Mir2Lir::FreeCallTemps() {
buzbee091cc402014-03-31 10:14:40 -0700469 FreeTemp(rs_rX86_ARG0);
470 FreeTemp(rs_rX86_ARG1);
471 FreeTemp(rs_rX86_ARG2);
472 FreeTemp(rs_rX86_ARG3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473}
474
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800475bool X86Mir2Lir::ProvidesFullMemoryBarrier(X86OpCode opcode) {
476 switch (opcode) {
477 case kX86LockCmpxchgMR:
478 case kX86LockCmpxchgAR:
479 case kX86LockCmpxchg8bM:
480 case kX86LockCmpxchg8bA:
481 case kX86XchgMR:
482 case kX86Mfence:
483 // Atomic memory instructions provide full barrier.
484 return true;
485 default:
486 break;
487 }
488
489 // Conservative if cannot prove it provides full barrier.
490 return false;
491}
492
Andreas Gampeb14329f2014-05-15 11:16:06 -0700493bool X86Mir2Lir::GenMemBarrier(MemBarrierKind barrier_kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494#if ANDROID_SMP != 0
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800495 // Start off with using the last LIR as the barrier. If it is not enough, then we will update it.
496 LIR* mem_barrier = last_lir_insn_;
497
Andreas Gampeb14329f2014-05-15 11:16:06 -0700498 bool ret = false;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800499 /*
500 * According to the JSR-133 Cookbook, for x86 only StoreLoad barriers need memory fence. All other barriers
501 * (LoadLoad, LoadStore, StoreStore) are nops due to the x86 memory model. For those cases, all we need
502 * to ensure is that there is a scheduling barrier in place.
503 */
504 if (barrier_kind == kStoreLoad) {
505 // If no LIR exists already that can be used a barrier, then generate an mfence.
506 if (mem_barrier == nullptr) {
507 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700508 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800509 }
510
511 // If last instruction does not provide full barrier, then insert an mfence.
512 if (ProvidesFullMemoryBarrier(static_cast<X86OpCode>(mem_barrier->opcode)) == false) {
513 mem_barrier = NewLIR0(kX86Mfence);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700514 ret = true;
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800515 }
516 }
517
518 // Now ensure that a scheduling barrier is in place.
519 if (mem_barrier == nullptr) {
520 GenBarrier();
521 } else {
522 // Mark as a scheduling barrier.
523 DCHECK(!mem_barrier->flags.use_def_invalid);
524 mem_barrier->u.m.def_mask = ENCODE_ALL;
525 }
Andreas Gampeb14329f2014-05-15 11:16:06 -0700526 return ret;
527#else
528 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529#endif
530}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000531
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532void X86Mir2Lir::CompilerInitializeRegAlloc() {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700533 if (Gen64Bit()) {
buzbeeb01bf152014-05-13 15:59:07 -0700534 reg_pool_ = new (arena_) RegisterPool(this, arena_, empty_pool, core_regs_64, sp_regs_64,
535 dp_regs_64, empty_pool, reserved_regs_64,
536 empty_pool, core_temps_64, sp_temps_64, dp_temps_64);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700537 } else {
buzbeeb01bf152014-05-13 15:59:07 -0700538 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs_32, empty_pool, sp_regs_32,
539 dp_regs_32, reserved_regs_32, empty_pool,
540 core_temps_32, empty_pool, sp_temps_32, dp_temps_32);
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700541 }
buzbee091cc402014-03-31 10:14:40 -0700542
543 // Target-specific adjustments.
544
545 // Alias single precision xmm to double xmms.
546 // TODO: as needed, add larger vector sizes - alias all to the largest.
547 GrowableArray<RegisterInfo*>::Iterator it(&reg_pool_->sp_regs_);
548 for (RegisterInfo* info = it.Next(); info != nullptr; info = it.Next()) {
549 int sp_reg_num = info->GetReg().GetRegNum();
550 RegStorage dp_reg = RegStorage::Solo64(RegStorage::kFloatingPoint | sp_reg_num);
551 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
552 // 64-bit xmm vector register's master storage should refer to itself.
553 DCHECK_EQ(dp_reg_info, dp_reg_info->Master());
554 // Redirect 32-bit vector's master storage to 64-bit vector.
555 info->SetMaster(dp_reg_info);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700556 }
buzbee091cc402014-03-31 10:14:40 -0700557
558 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
559 // TODO: adjust for x86/hard float calling convention.
560 reg_pool_->next_core_reg_ = 2;
561 reg_pool_->next_sp_reg_ = 2;
562 reg_pool_->next_dp_reg_ = 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563}
564
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565void X86Mir2Lir::SpillCoreRegs() {
566 if (num_core_spills_ == 0) {
567 return;
568 }
569 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700570 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700571 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700572 for (int reg = 0; mask; mask >>= 1, reg++) {
573 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800574 StoreWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700575 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 }
577 }
578}
579
580void X86Mir2Lir::UnSpillCoreRegs() {
581 if (num_core_spills_ == 0) {
582 return;
583 }
584 // Spill mask not including fake return address register
buzbee091cc402014-03-31 10:14:40 -0700585 uint32_t mask = core_spill_mask_ & ~(1 << rs_rRET.GetRegNum());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700586 int offset = frame_size_ - (GetInstructionSetPointerSize(cu_->instruction_set) * num_core_spills_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 for (int reg = 0; mask; mask >>= 1, reg++) {
588 if (mask & 0x1) {
buzbee2700f7e2014-03-07 09:46:20 -0800589 LoadWordDisp(rs_rX86_SP, offset, RegStorage::Solo32(reg));
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700590 offset += GetInstructionSetPointerSize(cu_->instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 }
592 }
593}
594
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700595bool X86Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 return (lir->opcode == kX86Jmp8 || lir->opcode == kX86Jmp32);
597}
598
Vladimir Marko674744e2014-04-24 15:18:26 +0100599bool X86Mir2Lir::SupportsVolatileLoadStore(OpSize size) {
600 return true;
601}
602
603RegisterClass X86Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
604 if (UNLIKELY(is_volatile)) {
605 // On x86, atomic 64-bit load/store requires an fp register.
606 // Smaller aligned load/store is atomic for both core and fp registers.
607 if (size == k64 || size == kDouble) {
608 return kFPReg;
609 }
610 }
611 return RegClassBySize(size);
612}
613
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700614X86Mir2Lir::X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena, bool gen64bit)
Mark Mendell55d0eac2014-02-06 11:02:52 -0800615 : Mir2Lir(cu, mir_graph, arena),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700616 base_of_code_(nullptr), store_method_addr_(false), store_method_addr_used_(false),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800617 method_address_insns_(arena, 100, kGrowableArrayMisc),
618 class_type_address_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendellae9fd932014-02-10 16:14:35 -0800619 call_method_insns_(arena, 100, kGrowableArrayMisc),
Mark Mendelld65c51a2014-04-29 16:55:20 -0400620 stack_decrement_(nullptr), stack_increment_(nullptr), gen64bit_(gen64bit),
621 const_vectors_(nullptr) {
622 store_method_addr_used_ = false;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700623 if (kIsDebugBuild) {
624 for (int i = 0; i < kX86Last; i++) {
625 if (X86Mir2Lir::EncodingMap[i].opcode != i) {
626 LOG(FATAL) << "Encoding order for " << X86Mir2Lir::EncodingMap[i].name
Mark Mendelld65c51a2014-04-29 16:55:20 -0400627 << " is wrong: expecting " << i << ", seeing "
628 << static_cast<int>(X86Mir2Lir::EncodingMap[i].opcode);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700629 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 }
631 }
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700632 if (Gen64Bit()) {
633 rs_rX86_SP = rs_rX86_SP_64;
634
635 rs_rX86_ARG0 = rs_rDI;
636 rs_rX86_ARG1 = rs_rSI;
637 rs_rX86_ARG2 = rs_rDX;
638 rs_rX86_ARG3 = rs_rCX;
639 rX86_ARG0 = rDI;
640 rX86_ARG1 = rSI;
641 rX86_ARG2 = rDX;
642 rX86_ARG3 = rCX;
643 // TODO: ARG4(r8), ARG5(r9), floating point args.
644 } else {
645 rs_rX86_SP = rs_rX86_SP_32;
646
647 rs_rX86_ARG0 = rs_rAX;
648 rs_rX86_ARG1 = rs_rCX;
649 rs_rX86_ARG2 = rs_rDX;
650 rs_rX86_ARG3 = rs_rBX;
651 rX86_ARG0 = rAX;
652 rX86_ARG1 = rCX;
653 rX86_ARG2 = rDX;
654 rX86_ARG3 = rBX;
655 }
656 rs_rX86_FARG0 = rs_rAX;
657 rs_rX86_FARG1 = rs_rCX;
658 rs_rX86_FARG2 = rs_rDX;
659 rs_rX86_FARG3 = rs_rBX;
660 rs_rX86_RET0 = rs_rAX;
661 rs_rX86_RET1 = rs_rDX;
662 rs_rX86_INVOKE_TGT = rs_rAX;
663 rs_rX86_COUNT = rs_rCX;
664 rX86_FARG0 = rAX;
665 rX86_FARG1 = rCX;
666 rX86_FARG2 = rDX;
667 rX86_FARG3 = rBX;
668 rX86_RET0 = rAX;
669 rX86_RET1 = rDX;
670 rX86_INVOKE_TGT = rAX;
671 rX86_COUNT = rCX;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672}
673
674Mir2Lir* X86CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
675 ArenaAllocator* const arena) {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700676 return new X86Mir2Lir(cu, mir_graph, arena, false);
677}
678
679Mir2Lir* X86_64CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
680 ArenaAllocator* const arena) {
681 return new X86Mir2Lir(cu, mir_graph, arena, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682}
683
684// Not used in x86
Ian Rogersdd7624d2014-03-14 17:43:00 -0700685RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<4> offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
buzbee2700f7e2014-03-07 09:46:20 -0800687 return RegStorage::InvalidReg();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688}
689
Andreas Gampe2f244e92014-05-08 03:35:25 -0700690// Not used in x86
691RegStorage X86Mir2Lir::LoadHelper(ThreadOffset<8> offset) {
692 LOG(FATAL) << "Unexpected use of LoadHelper in x86";
693 return RegStorage::InvalidReg();
694}
695
Dave Allisonb373e092014-02-20 16:06:36 -0800696LIR* X86Mir2Lir::CheckSuspendUsingLoad() {
697 LOG(FATAL) << "Unexpected use of CheckSuspendUsingLoad in x86";
698 return nullptr;
699}
700
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700701uint64_t X86Mir2Lir::GetTargetInstFlags(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700702 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 return X86Mir2Lir::EncodingMap[opcode].flags;
704}
705
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700706const char* X86Mir2Lir::GetTargetInstName(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700707 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700708 return X86Mir2Lir::EncodingMap[opcode].name;
709}
710
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700711const char* X86Mir2Lir::GetTargetInstFmt(int opcode) {
buzbee409fe942013-10-11 10:49:56 -0700712 DCHECK(!IsPseudoLirOp(opcode));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713 return X86Mir2Lir::EncodingMap[opcode].fmt;
714}
715
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000716void X86Mir2Lir::GenConstWide(RegLocation rl_dest, int64_t value) {
717 // Can we do this directly to memory?
718 rl_dest = UpdateLocWide(rl_dest);
719 if ((rl_dest.location == kLocDalvikFrame) ||
720 (rl_dest.location == kLocCompilerTemp)) {
721 int32_t val_lo = Low32Bits(value);
722 int32_t val_hi = High32Bits(value);
buzbee2700f7e2014-03-07 09:46:20 -0800723 int r_base = TargetReg(kSp).GetReg();
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000724 int displacement = SRegOffset(rl_dest.s_reg_low);
725
buzbee2700f7e2014-03-07 09:46:20 -0800726 LIR * store = NewLIR3(kX86Mov32MI, r_base, displacement + LOWORD_OFFSET, val_lo);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000727 AnnotateDalvikRegAccess(store, (displacement + LOWORD_OFFSET) >> 2,
728 false /* is_load */, true /* is64bit */);
buzbee2700f7e2014-03-07 09:46:20 -0800729 store = NewLIR3(kX86Mov32MI, r_base, displacement + HIWORD_OFFSET, val_hi);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000730 AnnotateDalvikRegAccess(store, (displacement + HIWORD_OFFSET) >> 2,
731 false /* is_load */, true /* is64bit */);
732 return;
733 }
734
735 // Just use the standard code to do the generation.
736 Mir2Lir::GenConstWide(rl_dest, value);
737}
Mark Mendelle02d48f2014-01-15 11:19:23 -0800738
739// TODO: Merge with existing RegLocation dumper in vreg_analysis.cc
740void X86Mir2Lir::DumpRegLocation(RegLocation loc) {
741 LOG(INFO) << "location: " << loc.location << ','
742 << (loc.wide ? " w" : " ")
743 << (loc.defined ? " D" : " ")
744 << (loc.is_const ? " c" : " ")
745 << (loc.fp ? " F" : " ")
746 << (loc.core ? " C" : " ")
747 << (loc.ref ? " r" : " ")
748 << (loc.high_word ? " h" : " ")
749 << (loc.home ? " H" : " ")
buzbee2700f7e2014-03-07 09:46:20 -0800750 << ", low: " << static_cast<int>(loc.reg.GetLowReg())
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000751 << ", high: " << static_cast<int>(loc.reg.GetHighReg())
Mark Mendelle02d48f2014-01-15 11:19:23 -0800752 << ", s_reg: " << loc.s_reg_low
753 << ", orig: " << loc.orig_sreg;
754}
755
Mark Mendell67c39c42014-01-31 17:28:00 -0800756void X86Mir2Lir::Materialize() {
757 // A good place to put the analysis before starting.
758 AnalyzeMIR();
759
760 // Now continue with regular code generation.
761 Mir2Lir::Materialize();
762}
763
Jeff Hao49161ce2014-03-12 11:05:25 -0700764void X86Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800765 SpecialTargetRegister symbolic_reg) {
766 /*
767 * For x86, just generate a 32 bit move immediate instruction, that will be filled
768 * in at 'link time'. For now, put a unique value based on target to ensure that
769 * code deduplication works.
770 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700771 int target_method_idx = target_method.dex_method_index;
772 const DexFile* target_dex_file = target_method.dex_file;
773 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
774 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800775
Jeff Hao49161ce2014-03-12 11:05:25 -0700776 // Generate the move instruction with the unique pointer and save index, dex_file, and type.
buzbee2700f7e2014-03-07 09:46:20 -0800777 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg).GetReg(),
Jeff Hao49161ce2014-03-12 11:05:25 -0700778 static_cast<int>(target_method_id_ptr), target_method_idx,
779 WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800780 AppendLIR(move);
781 method_address_insns_.Insert(move);
782}
783
784void X86Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
785 /*
786 * For x86, just generate a 32 bit move immediate instruction, that will be filled
787 * in at 'link time'. For now, put a unique value based on target to ensure that
788 * code deduplication works.
789 */
790 const DexFile::TypeId& id = cu_->dex_file->GetTypeId(type_idx);
791 uintptr_t ptr = reinterpret_cast<uintptr_t>(&id);
792
793 // Generate the move instruction with the unique pointer and save index and type.
buzbee2700f7e2014-03-07 09:46:20 -0800794 LIR *move = RawLIR(current_dalvik_offset_, kX86Mov32RI, TargetReg(symbolic_reg).GetReg(),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800795 static_cast<int>(ptr), type_idx);
796 AppendLIR(move);
797 class_type_address_insns_.Insert(move);
798}
799
Jeff Hao49161ce2014-03-12 11:05:25 -0700800LIR *X86Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
Mark Mendell55d0eac2014-02-06 11:02:52 -0800801 /*
802 * For x86, just generate a 32 bit call relative instruction, that will be filled
803 * in at 'link time'. For now, put a unique value based on target to ensure that
804 * code deduplication works.
805 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700806 int target_method_idx = target_method.dex_method_index;
807 const DexFile* target_dex_file = target_method.dex_file;
808 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
809 uintptr_t target_method_id_ptr = reinterpret_cast<uintptr_t>(&target_method_id);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800810
Jeff Hao49161ce2014-03-12 11:05:25 -0700811 // Generate the call instruction with the unique pointer and save index, dex_file, and type.
812 LIR *call = RawLIR(current_dalvik_offset_, kX86CallI, static_cast<int>(target_method_id_ptr),
813 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800814 AppendLIR(call);
815 call_method_insns_.Insert(call);
816 return call;
817}
818
Mark Mendelld65c51a2014-04-29 16:55:20 -0400819/*
820 * @brief Enter a 32 bit quantity into a buffer
821 * @param buf buffer.
822 * @param data Data value.
823 */
824
825static void PushWord(std::vector<uint8_t>&buf, int32_t data) {
826 buf.push_back(data & 0xff);
827 buf.push_back((data >> 8) & 0xff);
828 buf.push_back((data >> 16) & 0xff);
829 buf.push_back((data >> 24) & 0xff);
830}
831
Mark Mendell55d0eac2014-02-06 11:02:52 -0800832void X86Mir2Lir::InstallLiteralPools() {
833 // These are handled differently for x86.
834 DCHECK(code_literal_list_ == nullptr);
835 DCHECK(method_literal_list_ == nullptr);
836 DCHECK(class_literal_list_ == nullptr);
837
Mark Mendelld65c51a2014-04-29 16:55:20 -0400838 // Align to 16 byte boundary. We have implicit knowledge that the start of the method is
839 // on a 4 byte boundary. How can I check this if it changes (other than aligned loads
840 // will fail at runtime)?
841 if (const_vectors_ != nullptr) {
842 int align_size = (16-4) - (code_buffer_.size() & 0xF);
843 if (align_size < 0) {
844 align_size += 16;
845 }
846
847 while (align_size > 0) {
848 code_buffer_.push_back(0);
849 align_size--;
850 }
851 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
852 PushWord(code_buffer_, p->operands[0]);
853 PushWord(code_buffer_, p->operands[1]);
854 PushWord(code_buffer_, p->operands[2]);
855 PushWord(code_buffer_, p->operands[3]);
856 }
857 }
858
Mark Mendell55d0eac2014-02-06 11:02:52 -0800859 // Handle the fixups for methods.
860 for (uint32_t i = 0; i < method_address_insns_.Size(); i++) {
861 LIR* p = method_address_insns_.Get(i);
862 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700863 uint32_t target_method_idx = p->operands[2];
864 const DexFile* target_dex_file =
865 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[3]));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800866
867 // The offset to patch is the last 4 bytes of the instruction.
868 int patch_offset = p->offset + p->flags.size - 4;
869 cu_->compiler_driver->AddMethodPatch(cu_->dex_file, cu_->class_def_idx,
870 cu_->method_idx, cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700871 target_method_idx, target_dex_file,
872 static_cast<InvokeType>(p->operands[4]),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800873 patch_offset);
874 }
875
876 // Handle the fixups for class types.
877 for (uint32_t i = 0; i < class_type_address_insns_.Size(); i++) {
878 LIR* p = class_type_address_insns_.Get(i);
879 DCHECK_EQ(p->opcode, kX86Mov32RI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700880 uint32_t target_method_idx = p->operands[2];
Mark Mendell55d0eac2014-02-06 11:02:52 -0800881
882 // The offset to patch is the last 4 bytes of the instruction.
883 int patch_offset = p->offset + p->flags.size - 4;
884 cu_->compiler_driver->AddClassPatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700885 cu_->method_idx, target_method_idx, patch_offset);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800886 }
887
888 // And now the PC-relative calls to methods.
889 for (uint32_t i = 0; i < call_method_insns_.Size(); i++) {
890 LIR* p = call_method_insns_.Get(i);
891 DCHECK_EQ(p->opcode, kX86CallI);
Jeff Hao49161ce2014-03-12 11:05:25 -0700892 uint32_t target_method_idx = p->operands[1];
893 const DexFile* target_dex_file =
894 reinterpret_cast<const DexFile*>(UnwrapPointer(p->operands[2]));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800895
896 // The offset to patch is the last 4 bytes of the instruction.
897 int patch_offset = p->offset + p->flags.size - 4;
898 cu_->compiler_driver->AddRelativeCodePatch(cu_->dex_file, cu_->class_def_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700899 cu_->method_idx, cu_->invoke_type,
900 target_method_idx, target_dex_file,
901 static_cast<InvokeType>(p->operands[3]),
Mark Mendell55d0eac2014-02-06 11:02:52 -0800902 patch_offset, -4 /* offset */);
903 }
904
905 // And do the normal processing.
906 Mir2Lir::InstallLiteralPools();
907}
908
Mark Mendell4028a6c2014-02-19 20:06:20 -0800909/*
910 * Fast string.index_of(I) & (II). Inline check for simple case of char <= 0xffff,
911 * otherwise bails to standard library code.
912 */
913bool X86Mir2Lir::GenInlinedIndexOf(CallInfo* info, bool zero_based) {
914 ClobberCallerSave();
915 LockCallTemps(); // Using fixed registers
916
917 // EAX: 16 bit character being searched.
918 // ECX: count: number of words to be searched.
919 // EDI: String being searched.
920 // EDX: temporary during execution.
921 // EBX: temporary during execution.
922
923 RegLocation rl_obj = info->args[0];
924 RegLocation rl_char = info->args[1];
buzbeea44d4f52014-03-05 11:26:39 -0800925 RegLocation rl_start; // Note: only present in III flavor or IndexOf.
Mark Mendell4028a6c2014-02-19 20:06:20 -0800926
927 uint32_t char_value =
928 rl_char.is_const ? mir_graph_->ConstantValue(rl_char.orig_sreg) : 0;
929
930 if (char_value > 0xFFFF) {
931 // We have to punt to the real String.indexOf.
932 return false;
933 }
934
935 // Okay, we are commited to inlining this.
936 RegLocation rl_return = GetReturn(false);
937 RegLocation rl_dest = InlineTarget(info);
938
939 // Is the string non-NULL?
buzbee2700f7e2014-03-07 09:46:20 -0800940 LoadValueDirectFixed(rl_obj, rs_rDX);
941 GenNullCheck(rs_rDX, info->opt_flags);
Vladimir Marko3bc86152014-03-13 14:11:28 +0000942 info->opt_flags |= MIR_IGNORE_NULL_CHECK; // Record that we've null checked.
Mark Mendell4028a6c2014-02-19 20:06:20 -0800943
944 // Does the character fit in 16 bits?
Mingyao Yang3a74d152014-04-21 15:39:44 -0700945 LIR* slowpath_branch = nullptr;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800946 if (rl_char.is_const) {
947 // We need the value in EAX.
buzbee2700f7e2014-03-07 09:46:20 -0800948 LoadConstantNoClobber(rs_rAX, char_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800949 } else {
950 // Character is not a constant; compare at runtime.
buzbee2700f7e2014-03-07 09:46:20 -0800951 LoadValueDirectFixed(rl_char, rs_rAX);
Mingyao Yang3a74d152014-04-21 15:39:44 -0700952 slowpath_branch = OpCmpImmBranch(kCondGt, rs_rAX, 0xFFFF, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800953 }
954
955 // From here down, we know that we are looking for a char that fits in 16 bits.
Mark Mendelle19c91f2014-02-25 08:19:08 -0800956 // Location of reference to data array within the String object.
957 int value_offset = mirror::String::ValueOffset().Int32Value();
958 // Location of count within the String object.
959 int count_offset = mirror::String::CountOffset().Int32Value();
960 // Starting offset within data array.
961 int offset_offset = mirror::String::OffsetOffset().Int32Value();
962 // Start of char data with array_.
963 int data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value();
Mark Mendell4028a6c2014-02-19 20:06:20 -0800964
965 // Character is in EAX.
966 // Object pointer is in EDX.
967
968 // We need to preserve EDI, but have no spare registers, so push it on the stack.
969 // We have to remember that all stack addresses after this are offset by sizeof(EDI).
buzbee091cc402014-03-31 10:14:40 -0700970 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -0800971
972 // Compute the number of words to search in to rCX.
buzbee695d13a2014-04-19 13:32:20 -0700973 Load32Disp(rs_rDX, count_offset, rs_rCX);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800974 LIR *length_compare = nullptr;
975 int start_value = 0;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700976 bool is_index_on_stack = false;
Mark Mendell4028a6c2014-02-19 20:06:20 -0800977 if (zero_based) {
978 // We have to handle an empty string. Use special instruction JECXZ.
979 length_compare = NewLIR0(kX86Jecxz8);
980 } else {
buzbeea44d4f52014-03-05 11:26:39 -0800981 rl_start = info->args[2];
Mark Mendell4028a6c2014-02-19 20:06:20 -0800982 // We have to offset by the start index.
983 if (rl_start.is_const) {
984 start_value = mir_graph_->ConstantValue(rl_start.orig_sreg);
985 start_value = std::max(start_value, 0);
986
987 // Is the start > count?
buzbee2700f7e2014-03-07 09:46:20 -0800988 length_compare = OpCmpImmBranch(kCondLe, rs_rCX, start_value, nullptr);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800989
990 if (start_value != 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800991 OpRegImm(kOpSub, rs_rCX, start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800992 }
993 } else {
994 // Runtime start index.
buzbee30adc732014-05-09 15:10:18 -0700995 rl_start = UpdateLocTyped(rl_start, kCoreReg);
Mark Mendell4028a6c2014-02-19 20:06:20 -0800996 if (rl_start.location == kLocPhysReg) {
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700997 // Handle "start index < 0" case.
998 OpRegReg(kOpXor, rs_rBX, rs_rBX);
999 OpRegReg(kOpCmp, rl_start.reg, rs_rBX);
1000 OpCondRegReg(kOpCmov, kCondLt, rl_start.reg, rs_rBX);
1001
1002 // The length of the string should be greater than the start index.
buzbee2700f7e2014-03-07 09:46:20 -08001003 length_compare = OpCmpBranch(kCondLe, rs_rCX, rl_start.reg, nullptr);
1004 OpRegReg(kOpSub, rs_rCX, rl_start.reg);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001005 if (rl_start.reg == rs_rDI) {
1006 // The special case. We will use EDI further, so lets put start index to stack.
buzbee091cc402014-03-31 10:14:40 -07001007 NewLIR1(kX86Push32R, rs_rDI.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001008 is_index_on_stack = true;
1009 }
Mark Mendell4028a6c2014-02-19 20:06:20 -08001010 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001011 // Load the start index from stack, remembering that we pushed EDI.
Mark Mendell4028a6c2014-02-19 20:06:20 -08001012 int displacement = SRegOffset(rl_start.s_reg_low) + sizeof(uint32_t);
buzbee695d13a2014-04-19 13:32:20 -07001013 Load32Disp(rs_rX86_SP, displacement, rs_rBX);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001014 OpRegReg(kOpXor, rs_rDI, rs_rDI);
1015 OpRegReg(kOpCmp, rs_rBX, rs_rDI);
1016 OpCondRegReg(kOpCmov, kCondLt, rs_rBX, rs_rDI);
1017
1018 length_compare = OpCmpBranch(kCondLe, rs_rCX, rs_rBX, nullptr);
1019 OpRegReg(kOpSub, rs_rCX, rs_rBX);
1020 // Put the start index to stack.
buzbee091cc402014-03-31 10:14:40 -07001021 NewLIR1(kX86Push32R, rs_rBX.GetReg());
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001022 is_index_on_stack = true;
Mark Mendell4028a6c2014-02-19 20:06:20 -08001023 }
1024 }
1025 }
1026 DCHECK(length_compare != nullptr);
1027
1028 // ECX now contains the count in words to be searched.
1029
1030 // Load the address of the string into EBX.
Mark Mendelle19c91f2014-02-25 08:19:08 -08001031 // The string starts at VALUE(String) + 2 * OFFSET(String) + DATA_OFFSET.
buzbee695d13a2014-04-19 13:32:20 -07001032 Load32Disp(rs_rDX, value_offset, rs_rDI);
1033 Load32Disp(rs_rDX, offset_offset, rs_rBX);
buzbee2700f7e2014-03-07 09:46:20 -08001034 OpLea(rs_rBX, rs_rDI, rs_rBX, 1, data_offset);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001035
1036 // Now compute into EDI where the search will start.
1037 if (zero_based || rl_start.is_const) {
1038 if (start_value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -08001039 OpRegCopy(rs_rDI, rs_rBX);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001040 } else {
buzbee091cc402014-03-31 10:14:40 -07001041 NewLIR3(kX86Lea32RM, rs_rDI.GetReg(), rs_rBX.GetReg(), 2 * start_value);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001042 }
1043 } else {
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001044 if (is_index_on_stack == true) {
1045 // Load the start index from stack.
buzbee091cc402014-03-31 10:14:40 -07001046 NewLIR1(kX86Pop32R, rs_rDX.GetReg());
buzbee2700f7e2014-03-07 09:46:20 -08001047 OpLea(rs_rDI, rs_rBX, rs_rDX, 1, 0);
Alexei Zavjalova1758d82014-04-17 01:55:43 +07001048 } else {
1049 OpLea(rs_rDI, rs_rBX, rl_start.reg, 1, 0);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001050 }
1051 }
1052
1053 // EDI now contains the start of the string to be searched.
1054 // We are all prepared to do the search for the character.
1055 NewLIR0(kX86RepneScasw);
1056
1057 // Did we find a match?
1058 LIR* failed_branch = OpCondBranch(kCondNe, nullptr);
1059
1060 // yes, we matched. Compute the index of the result.
1061 // index = ((curr_ptr - orig_ptr) / 2) - 1.
buzbee2700f7e2014-03-07 09:46:20 -08001062 OpRegReg(kOpSub, rs_rDI, rs_rBX);
1063 OpRegImm(kOpAsr, rs_rDI, 1);
buzbee091cc402014-03-31 10:14:40 -07001064 NewLIR3(kX86Lea32RM, rl_return.reg.GetReg(), rs_rDI.GetReg(), -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001065 LIR *all_done = NewLIR1(kX86Jmp8, 0);
1066
1067 // Failed to match; return -1.
1068 LIR *not_found = NewLIR0(kPseudoTargetLabel);
1069 length_compare->target = not_found;
1070 failed_branch->target = not_found;
buzbee2700f7e2014-03-07 09:46:20 -08001071 LoadConstantNoClobber(rl_return.reg, -1);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001072
1073 // And join up at the end.
1074 all_done->target = NewLIR0(kPseudoTargetLabel);
1075 // Restore EDI from the stack.
buzbee091cc402014-03-31 10:14:40 -07001076 NewLIR1(kX86Pop32R, rs_rDI.GetReg());
Mark Mendell4028a6c2014-02-19 20:06:20 -08001077
1078 // Out of line code returns here.
Mingyao Yang3a74d152014-04-21 15:39:44 -07001079 if (slowpath_branch != nullptr) {
Mark Mendell4028a6c2014-02-19 20:06:20 -08001080 LIR *return_point = NewLIR0(kPseudoTargetLabel);
Mingyao Yang3a74d152014-04-21 15:39:44 -07001081 AddIntrinsicSlowPath(info, slowpath_branch, return_point);
Mark Mendell4028a6c2014-02-19 20:06:20 -08001082 }
1083
1084 StoreValue(rl_dest, rl_return);
1085 return true;
1086}
1087
Mark Mendellae9fd932014-02-10 16:14:35 -08001088/*
Mark Mendellae9fd932014-02-10 16:14:35 -08001089 * @brief Enter an 'advance LOC' into the FDE buffer
1090 * @param buf FDE buffer.
1091 * @param increment Amount by which to increase the current location.
1092 */
1093static void AdvanceLoc(std::vector<uint8_t>&buf, uint32_t increment) {
1094 if (increment < 64) {
1095 // Encoding in opcode.
1096 buf.push_back(0x1 << 6 | increment);
1097 } else if (increment < 256) {
1098 // Single byte delta.
1099 buf.push_back(0x02);
1100 buf.push_back(increment);
1101 } else if (increment < 256 * 256) {
1102 // Two byte delta.
1103 buf.push_back(0x03);
1104 buf.push_back(increment & 0xff);
1105 buf.push_back((increment >> 8) & 0xff);
1106 } else {
1107 // Four byte delta.
1108 buf.push_back(0x04);
1109 PushWord(buf, increment);
1110 }
1111}
1112
1113
1114std::vector<uint8_t>* X86CFIInitialization() {
1115 return X86Mir2Lir::ReturnCommonCallFrameInformation();
1116}
1117
1118std::vector<uint8_t>* X86Mir2Lir::ReturnCommonCallFrameInformation() {
1119 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1120
1121 // Length of the CIE (except for this field).
1122 PushWord(*cfi_info, 16);
1123
1124 // CIE id.
1125 PushWord(*cfi_info, 0xFFFFFFFFU);
1126
1127 // Version: 3.
1128 cfi_info->push_back(0x03);
1129
1130 // Augmentation: empty string.
1131 cfi_info->push_back(0x0);
1132
1133 // Code alignment: 1.
1134 cfi_info->push_back(0x01);
1135
1136 // Data alignment: -4.
1137 cfi_info->push_back(0x7C);
1138
1139 // Return address register (R8).
1140 cfi_info->push_back(0x08);
1141
1142 // Initial return PC is 4(ESP): DW_CFA_def_cfa R4 4.
1143 cfi_info->push_back(0x0C);
1144 cfi_info->push_back(0x04);
1145 cfi_info->push_back(0x04);
1146
1147 // Return address location: 0(SP): DW_CFA_offset R8 1 (* -4);.
1148 cfi_info->push_back(0x2 << 6 | 0x08);
1149 cfi_info->push_back(0x01);
1150
1151 // And 2 Noops to align to 4 byte boundary.
1152 cfi_info->push_back(0x0);
1153 cfi_info->push_back(0x0);
1154
1155 DCHECK_EQ(cfi_info->size() & 3, 0U);
1156 return cfi_info;
1157}
1158
1159static void EncodeUnsignedLeb128(std::vector<uint8_t>& buf, uint32_t value) {
1160 uint8_t buffer[12];
1161 uint8_t *ptr = EncodeUnsignedLeb128(buffer, value);
1162 for (uint8_t *p = buffer; p < ptr; p++) {
1163 buf.push_back(*p);
1164 }
1165}
1166
1167std::vector<uint8_t>* X86Mir2Lir::ReturnCallFrameInformation() {
1168 std::vector<uint8_t>*cfi_info = new std::vector<uint8_t>;
1169
1170 // Generate the FDE for the method.
1171 DCHECK_NE(data_offset_, 0U);
1172
1173 // Length (will be filled in later in this routine).
1174 PushWord(*cfi_info, 0);
1175
1176 // CIE_pointer (can be filled in by linker); might be left at 0 if there is only
1177 // one CIE for the whole debug_frame section.
1178 PushWord(*cfi_info, 0);
1179
1180 // 'initial_location' (filled in by linker).
1181 PushWord(*cfi_info, 0);
1182
1183 // 'address_range' (number of bytes in the method).
1184 PushWord(*cfi_info, data_offset_);
1185
1186 // The instructions in the FDE.
1187 if (stack_decrement_ != nullptr) {
1188 // Advance LOC to just past the stack decrement.
1189 uint32_t pc = NEXT_LIR(stack_decrement_)->offset;
1190 AdvanceLoc(*cfi_info, pc);
1191
1192 // Now update the offset to the call frame: DW_CFA_def_cfa_offset frame_size.
1193 cfi_info->push_back(0x0e);
1194 EncodeUnsignedLeb128(*cfi_info, frame_size_);
1195
1196 // We continue with that stack until the epilogue.
1197 if (stack_increment_ != nullptr) {
1198 uint32_t new_pc = NEXT_LIR(stack_increment_)->offset;
1199 AdvanceLoc(*cfi_info, new_pc - pc);
1200
1201 // We probably have code snippets after the epilogue, so save the
1202 // current state: DW_CFA_remember_state.
1203 cfi_info->push_back(0x0a);
1204
1205 // We have now popped the stack: DW_CFA_def_cfa_offset 4. There is only the return
1206 // PC on the stack now.
1207 cfi_info->push_back(0x0e);
1208 EncodeUnsignedLeb128(*cfi_info, 4);
1209
1210 // Everything after that is the same as before the epilogue.
1211 // Stack bump was followed by RET instruction.
1212 LIR *post_ret_insn = NEXT_LIR(NEXT_LIR(stack_increment_));
1213 if (post_ret_insn != nullptr) {
1214 pc = new_pc;
1215 new_pc = post_ret_insn->offset;
1216 AdvanceLoc(*cfi_info, new_pc - pc);
1217 // Restore the state: DW_CFA_restore_state.
1218 cfi_info->push_back(0x0b);
1219 }
1220 }
1221 }
1222
1223 // Padding to a multiple of 4
1224 while ((cfi_info->size() & 3) != 0) {
1225 // DW_CFA_nop is encoded as 0.
1226 cfi_info->push_back(0);
1227 }
1228
1229 // Set the length of the FDE inside the generated bytes.
1230 uint32_t length = cfi_info->size() - 4;
1231 (*cfi_info)[0] = length;
1232 (*cfi_info)[1] = length >> 8;
1233 (*cfi_info)[2] = length >> 16;
1234 (*cfi_info)[3] = length >> 24;
1235 return cfi_info;
1236}
1237
Mark Mendelld65c51a2014-04-29 16:55:20 -04001238void X86Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1239 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
1240 case kMirOpConstVector:
1241 GenConst128(bb, mir);
1242 break;
1243 default:
1244 break;
1245 }
1246}
1247
1248void X86Mir2Lir::GenConst128(BasicBlock* bb, MIR* mir) {
1249 int type_size = mir->dalvikInsn.vA;
1250 // We support 128 bit vectors.
1251 DCHECK_EQ(type_size & 0xFFFF, 128);
1252 int reg = mir->dalvikInsn.vB;
1253 DCHECK_LT(reg, 8);
1254 uint32_t *args = mir->dalvikInsn.arg;
1255 // Check for all 0 case.
1256 if (args[0] == 0 && args[1] == 0 && args[2] == 0 && args[3] == 0) {
1257 NewLIR2(kX86XorpsRR, reg, reg);
1258 return;
1259 }
1260 // Okay, load it from the constant vector area.
1261 LIR *data_target = ScanVectorLiteral(mir);
1262 if (data_target == nullptr) {
1263 data_target = AddVectorLiteral(mir);
1264 }
1265
1266 // Address the start of the method.
1267 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
1268 rl_method = LoadValue(rl_method, kCoreReg);
1269
1270 // Load the proper value from the literal area.
1271 // We don't know the proper offset for the value, so pick one that will force
1272 // 4 byte offset. We will fix this up in the assembler later to have the right
1273 // value.
1274 LIR *load = NewLIR3(kX86Mova128RM, reg, rl_method.reg.GetReg(), 256 /* bogus */);
1275 load->flags.fixup = kFixupLoad;
1276 load->target = data_target;
1277 SetMemRefType(load, true, kLiteral);
1278}
1279
1280LIR *X86Mir2Lir::ScanVectorLiteral(MIR *mir) {
1281 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1282 for (LIR *p = const_vectors_; p != nullptr; p = p->next) {
1283 if (args[0] == p->operands[0] && args[1] == p->operands[1] &&
1284 args[2] == p->operands[2] && args[3] == p->operands[3]) {
1285 return p;
1286 }
1287 }
1288 return nullptr;
1289}
1290
1291LIR *X86Mir2Lir::AddVectorLiteral(MIR *mir) {
1292 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
1293 int *args = reinterpret_cast<int*>(mir->dalvikInsn.arg);
1294 new_value->operands[0] = args[0];
1295 new_value->operands[1] = args[1];
1296 new_value->operands[2] = args[2];
1297 new_value->operands[3] = args[3];
1298 new_value->next = const_vectors_;
1299 if (const_vectors_ == nullptr) {
1300 estimated_native_code_size_ += 12; // Amount needed to align to 16 byte boundary.
1301 }
1302 estimated_native_code_size_ += 16; // Space for one vector.
1303 const_vectors_ = new_value;
1304 return new_value;
1305}
1306
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001307} // namespace art