blob: e2846aebc3a3b4fe48443394a472651b97648b55 [file] [log] [blame]
Matteo Franchin43ec8732014-03-31 15:00:14 +01001/*
2 * Copyright (C) 2011 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#include "codegen_arm64.h"
18
19#include <inttypes.h>
20
21#include <string>
22
23#include "dex/compiler_internals.h"
24#include "dex/quick/mir_to_lir-inl.h"
25
26namespace art {
27
Vladimir Marko089142c2014-06-05 10:57:05 +010028static constexpr RegStorage core_regs_arr[] =
buzbeeb01bf152014-05-13 15:59:07 -070029 {rs_w0, rs_w1, rs_w2, rs_w3, rs_w4, rs_w5, rs_w6, rs_w7,
30 rs_w8, rs_w9, rs_w10, rs_w11, rs_w12, rs_w13, rs_w14, rs_w15,
31 rs_w16, rs_w17, rs_w18, rs_w19, rs_w20, rs_w21, rs_w22, rs_w23,
32 rs_w24, rs_w25, rs_w26, rs_w27, rs_w28, rs_w29, rs_w30, rs_w31,
33 rs_wzr};
Vladimir Marko089142c2014-06-05 10:57:05 +010034static constexpr RegStorage core64_regs_arr[] =
Matteo Franchine45fb9e2014-05-06 10:10:30 +010035 {rs_x0, rs_x1, rs_x2, rs_x3, rs_x4, rs_x5, rs_x6, rs_x7,
36 rs_x8, rs_x9, rs_x10, rs_x11, rs_x12, rs_x13, rs_x14, rs_x15,
37 rs_x16, rs_x17, rs_x18, rs_x19, rs_x20, rs_x21, rs_x22, rs_x23,
Matteo Franchinbc6d1972014-05-13 12:33:28 +010038 rs_x24, rs_x25, rs_x26, rs_x27, rs_x28, rs_x29, rs_x30, rs_x31,
39 rs_xzr};
Vladimir Marko089142c2014-06-05 10:57:05 +010040static constexpr RegStorage sp_regs_arr[] =
Matteo Franchine45fb9e2014-05-06 10:10:30 +010041 {rs_f0, rs_f1, rs_f2, rs_f3, rs_f4, rs_f5, rs_f6, rs_f7,
42 rs_f8, rs_f9, rs_f10, rs_f11, rs_f12, rs_f13, rs_f14, rs_f15,
43 rs_f16, rs_f17, rs_f18, rs_f19, rs_f20, rs_f21, rs_f22, rs_f23,
44 rs_f24, rs_f25, rs_f26, rs_f27, rs_f28, rs_f29, rs_f30, rs_f31};
Vladimir Marko089142c2014-06-05 10:57:05 +010045static constexpr RegStorage dp_regs_arr[] =
Matteo Franchine45fb9e2014-05-06 10:10:30 +010046 {rs_d0, rs_d1, rs_d2, rs_d3, rs_d4, rs_d5, rs_d6, rs_d7,
Zheng Xuc8304302014-05-15 17:21:01 +010047 rs_d8, rs_d9, rs_d10, rs_d11, rs_d12, rs_d13, rs_d14, rs_d15,
48 rs_d16, rs_d17, rs_d18, rs_d19, rs_d20, rs_d21, rs_d22, rs_d23,
49 rs_d24, rs_d25, rs_d26, rs_d27, rs_d28, rs_d29, rs_d30, rs_d31};
Vladimir Marko089142c2014-06-05 10:57:05 +010050static constexpr RegStorage reserved_regs_arr[] =
buzbeeb01bf152014-05-13 15:59:07 -070051 {rs_rA32_SUSPEND, rs_rA32_SELF, rs_rA32_SP, rs_rA32_LR, rs_wzr};
Vladimir Marko089142c2014-06-05 10:57:05 +010052static constexpr RegStorage reserved64_regs_arr[] =
Matteo Franchinbc6d1972014-05-13 12:33:28 +010053 {rs_rA64_SUSPEND, rs_rA64_SELF, rs_rA64_SP, rs_rA64_LR, rs_xzr};
54// TUNING: Are there too many temp registers and too less promote target?
Zheng Xuc8304302014-05-15 17:21:01 +010055// This definition need to be matched with runtime.cc, quick entry assembly and JNI compiler
56// Note: we are not able to call to C function directly if it un-match C ABI.
57// Currently, rs_rA64_SELF is not a callee save register which does not match C ABI.
Vladimir Marko089142c2014-06-05 10:57:05 +010058static constexpr RegStorage core_temps_arr[] =
buzbeeb01bf152014-05-13 15:59:07 -070059 {rs_w0, rs_w1, rs_w2, rs_w3, rs_w4, rs_w5, rs_w6, rs_w7,
60 rs_w8, rs_w9, rs_w10, rs_w11, rs_w12, rs_w13, rs_w14, rs_w15, rs_w16,
61 rs_w17};
Vladimir Marko089142c2014-06-05 10:57:05 +010062static constexpr RegStorage core64_temps_arr[] =
Zheng Xuc8304302014-05-15 17:21:01 +010063 {rs_x0, rs_x1, rs_x2, rs_x3, rs_x4, rs_x5, rs_x6, rs_x7,
64 rs_x8, rs_x9, rs_x10, rs_x11, rs_x12, rs_x13, rs_x14, rs_x15, rs_x16,
65 rs_x17};
Vladimir Marko089142c2014-06-05 10:57:05 +010066static constexpr RegStorage sp_temps_arr[] =
Matteo Franchine45fb9e2014-05-06 10:10:30 +010067 {rs_f0, rs_f1, rs_f2, rs_f3, rs_f4, rs_f5, rs_f6, rs_f7,
Zheng Xuc8304302014-05-15 17:21:01 +010068 rs_f16, rs_f17, rs_f18, rs_f19, rs_f20, rs_f21, rs_f22, rs_f23,
69 rs_f24, rs_f25, rs_f26, rs_f27, rs_f28, rs_f29, rs_f30, rs_f31};
Vladimir Marko089142c2014-06-05 10:57:05 +010070static constexpr RegStorage dp_temps_arr[] =
Zheng Xuc8304302014-05-15 17:21:01 +010071 {rs_d0, rs_d1, rs_d2, rs_d3, rs_d4, rs_d5, rs_d6, rs_d7,
72 rs_d16, rs_d17, rs_d18, rs_d19, rs_d20, rs_d21, rs_d22, rs_d23,
73 rs_d24, rs_d25, rs_d26, rs_d27, rs_d28, rs_d29, rs_d30, rs_d31};
Matteo Franchin43ec8732014-03-31 15:00:14 +010074
Vladimir Marko089142c2014-06-05 10:57:05 +010075static constexpr ArrayRef<const RegStorage> core_regs(core_regs_arr);
76static constexpr ArrayRef<const RegStorage> core64_regs(core64_regs_arr);
77static constexpr ArrayRef<const RegStorage> sp_regs(sp_regs_arr);
78static constexpr ArrayRef<const RegStorage> dp_regs(dp_regs_arr);
79static constexpr ArrayRef<const RegStorage> reserved_regs(reserved_regs_arr);
80static constexpr ArrayRef<const RegStorage> reserved64_regs(reserved64_regs_arr);
81static constexpr ArrayRef<const RegStorage> core_temps(core_temps_arr);
82static constexpr ArrayRef<const RegStorage> core64_temps(core64_temps_arr);
83static constexpr ArrayRef<const RegStorage> sp_temps(sp_temps_arr);
84static constexpr ArrayRef<const RegStorage> dp_temps(dp_temps_arr);
Matteo Franchin43ec8732014-03-31 15:00:14 +010085
86RegLocation Arm64Mir2Lir::LocCReturn() {
87 return arm_loc_c_return;
88}
89
buzbeea0cd2d72014-06-01 09:33:49 -070090RegLocation Arm64Mir2Lir::LocCReturnRef() {
91 return arm_loc_c_return;
92}
93
Matteo Franchin43ec8732014-03-31 15:00:14 +010094RegLocation Arm64Mir2Lir::LocCReturnWide() {
95 return arm_loc_c_return_wide;
96}
97
98RegLocation Arm64Mir2Lir::LocCReturnFloat() {
99 return arm_loc_c_return_float;
100}
101
102RegLocation Arm64Mir2Lir::LocCReturnDouble() {
103 return arm_loc_c_return_double;
104}
105
106// Return a target-dependent special register.
107RegStorage Arm64Mir2Lir::TargetReg(SpecialTargetRegister reg) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100108 // TODO(Arm64): this function doesn't work for hard-float ABI.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100109 RegStorage res_reg = RegStorage::InvalidReg();
110 switch (reg) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100111 case kSelf: res_reg = rs_rA64_SELF; break;
112 case kSuspend: res_reg = rs_rA64_SUSPEND; break;
113 case kLr: res_reg = rs_rA64_LR; break;
114 case kPc: res_reg = RegStorage::InvalidReg(); break;
115 case kSp: res_reg = rs_rA64_SP; break;
116 case kArg0: res_reg = rs_x0; break;
117 case kArg1: res_reg = rs_x1; break;
118 case kArg2: res_reg = rs_x2; break;
119 case kArg3: res_reg = rs_x3; break;
120 case kFArg0: res_reg = rs_f0; break;
121 case kFArg1: res_reg = rs_f1; break;
122 case kFArg2: res_reg = rs_f2; break;
123 case kFArg3: res_reg = rs_f3; break;
124 case kRet0: res_reg = rs_x0; break;
125 case kRet1: res_reg = rs_x0; break;
126 case kInvokeTgt: res_reg = rs_rA64_LR; break;
127 case kHiddenArg: res_reg = rs_x12; break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100128 case kHiddenFpArg: res_reg = RegStorage::InvalidReg(); break;
129 case kCount: res_reg = RegStorage::InvalidReg(); break;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700130 default: res_reg = RegStorage::InvalidReg();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100131 }
132 return res_reg;
133}
134
135RegStorage Arm64Mir2Lir::GetArgMappingToPhysicalReg(int arg_num) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100136 return RegStorage::InvalidReg();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100137}
138
139/*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100140 * Decode the register id. This routine makes assumptions on the encoding made by RegStorage.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100141 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100142ResourceMask Arm64Mir2Lir::GetRegMaskCommon(const RegStorage& reg) const {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100143 // TODO(Arm64): this function depends too much on the internal RegStorage encoding. Refactor.
144
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100145 // Check if the shape mask is zero (i.e. invalid).
146 if (UNLIKELY(reg == rs_wzr || reg == rs_xzr)) {
147 // The zero register is not a true register. It is just an immediate zero.
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 return kEncodeNone;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100149 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100150
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100151 return ResourceMask::Bit(
152 // FP register starts at bit position 32.
153 (reg.IsFloat() ? kArm64FPReg0 : 0) + reg.GetRegNum());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100154}
155
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100156ResourceMask Arm64Mir2Lir::GetPCUseDefEncoding() const {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100157 LOG(FATAL) << "Unexpected call to GetPCUseDefEncoding for Arm64";
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100158 return kEncodeNone;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100159}
160
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100161// Arm64 specific setup. TODO: inline?:
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100162void Arm64Mir2Lir::SetupTargetResourceMasks(LIR* lir, uint64_t flags,
163 ResourceMask* use_mask, ResourceMask* def_mask) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100164 DCHECK_EQ(cu_->instruction_set, kArm64);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100165 DCHECK(!lir->flags.use_def_invalid);
166
Matteo Franchin43ec8732014-03-31 15:00:14 +0100167 // These flags are somewhat uncommon - bypass if we can.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100168 if ((flags & (REG_DEF_SP | REG_USE_SP | REG_DEF_LR)) != 0) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100169 if (flags & REG_DEF_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100170 def_mask->SetBit(kArm64RegSP);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100171 }
172
173 if (flags & REG_USE_SP) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100174 use_mask->SetBit(kArm64RegSP);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100175 }
176
Matteo Franchin43ec8732014-03-31 15:00:14 +0100177 if (flags & REG_DEF_LR) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100178 def_mask->SetBit(kArm64RegLR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100179 }
180 }
181}
182
183ArmConditionCode Arm64Mir2Lir::ArmConditionEncoding(ConditionCode ccode) {
184 ArmConditionCode res;
185 switch (ccode) {
186 case kCondEq: res = kArmCondEq; break;
187 case kCondNe: res = kArmCondNe; break;
188 case kCondCs: res = kArmCondCs; break;
189 case kCondCc: res = kArmCondCc; break;
190 case kCondUlt: res = kArmCondCc; break;
191 case kCondUge: res = kArmCondCs; break;
192 case kCondMi: res = kArmCondMi; break;
193 case kCondPl: res = kArmCondPl; break;
194 case kCondVs: res = kArmCondVs; break;
195 case kCondVc: res = kArmCondVc; break;
196 case kCondHi: res = kArmCondHi; break;
197 case kCondLs: res = kArmCondLs; break;
198 case kCondGe: res = kArmCondGe; break;
199 case kCondLt: res = kArmCondLt; break;
200 case kCondGt: res = kArmCondGt; break;
201 case kCondLe: res = kArmCondLe; break;
202 case kCondAl: res = kArmCondAl; break;
203 case kCondNv: res = kArmCondNv; break;
204 default:
205 LOG(FATAL) << "Bad condition code " << ccode;
206 res = static_cast<ArmConditionCode>(0); // Quiet gcc
207 }
208 return res;
209}
210
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100211static const char *shift_names[4] = {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100212 "lsl",
213 "lsr",
214 "asr",
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100215 "ror"
216};
Matteo Franchin43ec8732014-03-31 15:00:14 +0100217
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100218static const char* extend_names[8] = {
219 "uxtb",
220 "uxth",
221 "uxtw",
222 "uxtx",
223 "sxtb",
224 "sxth",
225 "sxtw",
226 "sxtx",
227};
228
229/* Decode and print a register extension (e.g. ", uxtb #1") */
230static void DecodeRegExtendOrShift(int operand, char *buf, size_t buf_size) {
231 if ((operand & (1 << 6)) == 0) {
232 const char *shift_name = shift_names[(operand >> 7) & 0x3];
233 int amount = operand & 0x3f;
234 snprintf(buf, buf_size, ", %s #%d", shift_name, amount);
235 } else {
236 const char *extend_name = extend_names[(operand >> 3) & 0x7];
237 int amount = operand & 0x7;
238 if (amount == 0) {
239 snprintf(buf, buf_size, ", %s", extend_name);
240 } else {
241 snprintf(buf, buf_size, ", %s #%d", extend_name, amount);
242 }
243 }
244}
245
246#define BIT_MASK(w) ((UINT64_C(1) << (w)) - UINT64_C(1))
247
248static uint64_t RotateRight(uint64_t value, unsigned rotate, unsigned width) {
249 DCHECK_LE(width, 64U);
250 rotate &= 63;
251 value = value & BIT_MASK(width);
252 return ((value & BIT_MASK(rotate)) << (width - rotate)) | (value >> rotate);
253}
254
255static uint64_t RepeatBitsAcrossReg(bool is_wide, uint64_t value, unsigned width) {
256 unsigned i;
257 unsigned reg_size = (is_wide) ? 64 : 32;
258 uint64_t result = value & BIT_MASK(width);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100259 for (i = width; i < reg_size; i *= 2) {
260 result |= (result << i);
261 }
262 DCHECK_EQ(i, reg_size);
263 return result;
264}
265
266/**
267 * @brief Decode an immediate in the form required by logical instructions.
268 *
269 * @param is_wide Whether @p value encodes a 64-bit (as opposed to 32-bit) immediate.
270 * @param value The encoded logical immediates that is to be decoded.
271 * @return The decoded logical immediate.
272 * @note This is the inverse of Arm64Mir2Lir::EncodeLogicalImmediate().
273 */
274uint64_t Arm64Mir2Lir::DecodeLogicalImmediate(bool is_wide, int value) {
275 unsigned n = (value >> 12) & 0x01;
276 unsigned imm_r = (value >> 6) & 0x3f;
277 unsigned imm_s = (value >> 0) & 0x3f;
278
279 // An integer is constructed from the n, imm_s and imm_r bits according to
280 // the following table:
281 //
282 // N imms immr size S R
283 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
284 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
285 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
286 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
287 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
288 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
289 // (s bits must not be all set)
290 //
291 // A pattern is constructed of size bits, where the least significant S+1
292 // bits are set. The pattern is rotated right by R, and repeated across a
293 // 32 or 64-bit value, depending on destination register width.
294
295 if (n == 1) {
296 DCHECK_NE(imm_s, 0x3fU);
297 uint64_t bits = BIT_MASK(imm_s + 1);
298 return RotateRight(bits, imm_r, 64);
299 } else {
300 DCHECK_NE((imm_s >> 1), 0x1fU);
301 for (unsigned width = 0x20; width >= 0x2; width >>= 1) {
302 if ((imm_s & width) == 0) {
303 unsigned mask = (unsigned)(width - 1);
304 DCHECK_NE((imm_s & mask), mask);
305 uint64_t bits = BIT_MASK((imm_s & mask) + 1);
306 return RepeatBitsAcrossReg(is_wide, RotateRight(bits, imm_r & mask, width), width);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100307 }
308 }
309 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100310 return 0;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100311}
312
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100313/**
314 * @brief Decode an 8-bit single point number encoded with EncodeImmSingle().
315 */
316static float DecodeImmSingle(uint8_t small_float) {
317 int mantissa = (small_float & 0x0f) + 0x10;
318 int sign = ((small_float & 0x80) == 0) ? 1 : -1;
319 float signed_mantissa = static_cast<float>(sign*mantissa);
320 int exponent = (((small_float >> 4) & 0x7) + 4) & 0x7;
321 return signed_mantissa*static_cast<float>(1 << exponent)*0.0078125f;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100322}
323
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100324static const char* cc_names[] = {"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
325 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"};
Matteo Franchin43ec8732014-03-31 15:00:14 +0100326/*
327 * Interpret a format string and build a string no longer than size
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100328 * See format key in assemble_arm64.cc.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100329 */
330std::string Arm64Mir2Lir::BuildInsnString(const char* fmt, LIR* lir, unsigned char* base_addr) {
331 std::string buf;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100332 const char* fmt_end = &fmt[strlen(fmt)];
333 char tbuf[256];
334 const char* name;
335 char nc;
336 while (fmt < fmt_end) {
337 int operand;
338 if (*fmt == '!') {
339 fmt++;
340 DCHECK_LT(fmt, fmt_end);
341 nc = *fmt++;
342 if (nc == '!') {
343 strcpy(tbuf, "!");
344 } else {
345 DCHECK_LT(fmt, fmt_end);
346 DCHECK_LT(static_cast<unsigned>(nc-'0'), 4U);
347 operand = lir->operands[nc-'0'];
348 switch (*fmt++) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100349 case 'e': {
350 // Omit ", uxtw #0" in strings like "add w0, w1, w3, uxtw #0" and
351 // ", uxtx #0" in strings like "add x0, x1, x3, uxtx #0"
352 int omittable = ((IS_WIDE(lir->opcode)) ? EncodeExtend(kA64Uxtw, 0) :
353 EncodeExtend(kA64Uxtw, 0));
354 if (LIKELY(operand == omittable)) {
355 strcpy(tbuf, "");
356 } else {
357 DecodeRegExtendOrShift(operand, tbuf, arraysize(tbuf));
358 }
359 }
360 break;
361 case 'o':
362 // Omit ", lsl #0"
363 if (LIKELY(operand == EncodeShift(kA64Lsl, 0))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100364 strcpy(tbuf, "");
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100365 } else {
366 DecodeRegExtendOrShift(operand, tbuf, arraysize(tbuf));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100367 }
368 break;
369 case 'B':
370 switch (operand) {
371 case kSY:
372 name = "sy";
373 break;
374 case kST:
375 name = "st";
376 break;
377 case kISH:
378 name = "ish";
379 break;
380 case kISHST:
381 name = "ishst";
382 break;
383 case kNSH:
384 name = "nsh";
385 break;
386 case kNSHST:
387 name = "shst";
388 break;
389 default:
390 name = "DecodeError2";
391 break;
392 }
393 strcpy(tbuf, name);
394 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100395 case 's':
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100396 snprintf(tbuf, arraysize(tbuf), "s%d", operand & RegStorage::kRegNumMask);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100397 break;
398 case 'S':
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100399 snprintf(tbuf, arraysize(tbuf), "d%d", operand & RegStorage::kRegNumMask);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100400 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100401 case 'f':
402 snprintf(tbuf, arraysize(tbuf), "%c%d", (IS_FWIDE(lir->opcode)) ? 'd' : 's',
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100403 operand & RegStorage::kRegNumMask);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100404 break;
405 case 'l': {
406 bool is_wide = IS_WIDE(lir->opcode);
407 uint64_t imm = DecodeLogicalImmediate(is_wide, operand);
408 snprintf(tbuf, arraysize(tbuf), "%" PRId64 " (%#" PRIx64 ")", imm, imm);
409 }
410 break;
411 case 'I':
412 snprintf(tbuf, arraysize(tbuf), "%f", DecodeImmSingle(operand));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100413 break;
414 case 'M':
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100415 if (LIKELY(operand == 0))
416 strcpy(tbuf, "");
417 else
418 snprintf(tbuf, arraysize(tbuf), ", lsl #%d", 16*operand);
419 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100420 case 'd':
421 snprintf(tbuf, arraysize(tbuf), "%d", operand);
422 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100423 case 'w':
424 if (LIKELY(operand != rwzr))
425 snprintf(tbuf, arraysize(tbuf), "w%d", operand & RegStorage::kRegNumMask);
426 else
427 strcpy(tbuf, "wzr");
428 break;
429 case 'W':
430 if (LIKELY(operand != rwsp))
431 snprintf(tbuf, arraysize(tbuf), "w%d", operand & RegStorage::kRegNumMask);
432 else
433 strcpy(tbuf, "wsp");
434 break;
435 case 'x':
436 if (LIKELY(operand != rxzr))
437 snprintf(tbuf, arraysize(tbuf), "x%d", operand & RegStorage::kRegNumMask);
438 else
439 strcpy(tbuf, "xzr");
440 break;
441 case 'X':
442 if (LIKELY(operand != rsp))
443 snprintf(tbuf, arraysize(tbuf), "x%d", operand & RegStorage::kRegNumMask);
444 else
445 strcpy(tbuf, "sp");
446 break;
447 case 'D':
448 snprintf(tbuf, arraysize(tbuf), "%d", operand*((IS_WIDE(lir->opcode)) ? 8 : 4));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100449 break;
450 case 'E':
451 snprintf(tbuf, arraysize(tbuf), "%d", operand*4);
452 break;
453 case 'F':
454 snprintf(tbuf, arraysize(tbuf), "%d", operand*2);
455 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100456 case 'G':
457 if (LIKELY(operand == 0))
458 strcpy(tbuf, "");
459 else
460 strcpy(tbuf, (IS_WIDE(lir->opcode)) ? ", lsl #3" : ", lsl #2");
461 break;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100462 case 'c':
463 strcpy(tbuf, cc_names[operand]);
464 break;
465 case 't':
466 snprintf(tbuf, arraysize(tbuf), "0x%08" PRIxPTR " (L%p)",
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100467 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + (operand << 2),
Matteo Franchin43ec8732014-03-31 15:00:14 +0100468 lir->target);
469 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100470 case 'r': {
471 bool is_wide = IS_WIDE(lir->opcode);
472 if (LIKELY(operand != rwzr && operand != rxzr)) {
473 snprintf(tbuf, arraysize(tbuf), "%c%d", (is_wide) ? 'x' : 'w',
474 operand & RegStorage::kRegNumMask);
475 } else {
476 strcpy(tbuf, (is_wide) ? "xzr" : "wzr");
477 }
478 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100479 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100480 case 'R': {
481 bool is_wide = IS_WIDE(lir->opcode);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100482 if (LIKELY(operand != rwsp && operand != rsp)) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100483 snprintf(tbuf, arraysize(tbuf), "%c%d", (is_wide) ? 'x' : 'w',
484 operand & RegStorage::kRegNumMask);
485 } else {
486 strcpy(tbuf, (is_wide) ? "sp" : "wsp");
487 }
488 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100489 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100490 case 'p':
491 snprintf(tbuf, arraysize(tbuf), ".+%d (addr %#" PRIxPTR ")", 4*operand,
492 reinterpret_cast<uintptr_t>(base_addr) + lir->offset + 4*operand);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100493 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100494 case 'T':
495 if (LIKELY(operand == 0))
496 strcpy(tbuf, "");
497 else if (operand == 1)
498 strcpy(tbuf, ", lsl #12");
499 else
500 strcpy(tbuf, ", DecodeError3");
Matteo Franchin43ec8732014-03-31 15:00:14 +0100501 break;
502 default:
503 strcpy(tbuf, "DecodeError1");
504 break;
505 }
506 buf += tbuf;
507 }
508 } else {
509 buf += *fmt++;
510 }
511 }
512 return buf;
513}
514
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100515void Arm64Mir2Lir::DumpResourceMask(LIR* arm_lir, const ResourceMask& mask, const char* prefix) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100516 char buf[256];
517 buf[0] = 0;
518
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100519 if (mask.Equals(kEncodeAll)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100520 strcpy(buf, "all");
521 } else {
522 char num[8];
523 int i;
524
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100525 for (i = 0; i < kArm64RegEnd; i++) {
526 if (mask.HasBit(i)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100527 snprintf(num, arraysize(num), "%d ", i);
528 strcat(buf, num);
529 }
530 }
531
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100532 if (mask.HasBit(ResourceMask::kCCode)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100533 strcat(buf, "cc ");
534 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100535 if (mask.HasBit(ResourceMask::kFPStatus)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100536 strcat(buf, "fpcc ");
537 }
538
539 /* Memory bits */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100540 if (arm_lir && (mask.HasBit(ResourceMask::kDalvikReg))) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100541 snprintf(buf + strlen(buf), arraysize(buf) - strlen(buf), "dr%d%s",
542 DECODE_ALIAS_INFO_REG(arm_lir->flags.alias_info),
543 DECODE_ALIAS_INFO_WIDE(arm_lir->flags.alias_info) ? "(+1)" : "");
544 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100545 if (mask.HasBit(ResourceMask::kLiteral)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100546 strcat(buf, "lit ");
547 }
548
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100549 if (mask.HasBit(ResourceMask::kHeapRef)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100550 strcat(buf, "heap ");
551 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100552 if (mask.HasBit(ResourceMask::kMustNotAlias)) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100553 strcat(buf, "noalias ");
554 }
555 }
556 if (buf[0]) {
557 LOG(INFO) << prefix << ": " << buf;
558 }
559}
560
561bool Arm64Mir2Lir::IsUnconditionalBranch(LIR* lir) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100562 return (lir->opcode == kA64B1t);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100563}
564
Vladimir Marko674744e2014-04-24 15:18:26 +0100565bool Arm64Mir2Lir::SupportsVolatileLoadStore(OpSize size) {
566 return true;
567}
568
569RegisterClass Arm64Mir2Lir::RegClassForFieldLoadStore(OpSize size, bool is_volatile) {
570 if (UNLIKELY(is_volatile)) {
571 // On arm64, fp register load/store is atomic only for single bytes.
572 if (size != kSignedByte && size != kUnsignedByte) {
buzbeea0cd2d72014-06-01 09:33:49 -0700573 return (size == kReference) ? kRefReg : kCoreReg;
Vladimir Marko674744e2014-04-24 15:18:26 +0100574 }
575 }
576 return RegClassBySize(size);
577}
578
Matteo Franchin43ec8732014-03-31 15:00:14 +0100579Arm64Mir2Lir::Arm64Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
580 : Mir2Lir(cu, mir_graph, arena) {
581 // Sanity check - make sure encoding map lines up.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100582 for (int i = 0; i < kA64Last; i++) {
583 if (UNWIDE(Arm64Mir2Lir::EncodingMap[i].opcode) != i) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100584 LOG(FATAL) << "Encoding order for " << Arm64Mir2Lir::EncodingMap[i].name
585 << " is wrong: expecting " << i << ", seeing "
586 << static_cast<int>(Arm64Mir2Lir::EncodingMap[i].opcode);
587 }
588 }
589}
590
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100591Mir2Lir* Arm64CodeGenerator(CompilationUnit* const cu, MIRGraph* const mir_graph,
592 ArenaAllocator* const arena) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100593 return new Arm64Mir2Lir(cu, mir_graph, arena);
594}
595
Matteo Franchin43ec8732014-03-31 15:00:14 +0100596void Arm64Mir2Lir::CompilerInitializeRegAlloc() {
buzbeeb01bf152014-05-13 15:59:07 -0700597 reg_pool_ = new (arena_) RegisterPool(this, arena_, core_regs, core64_regs, sp_regs, dp_regs,
598 reserved_regs, reserved64_regs, core_temps, core64_temps,
599 sp_temps, dp_temps);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100600
601 // Target-specific adjustments.
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100602 // Alias single precision float registers to corresponding double registers.
603 GrowableArray<RegisterInfo*>::Iterator fp_it(&reg_pool_->sp_regs_);
604 for (RegisterInfo* info = fp_it.Next(); info != nullptr; info = fp_it.Next()) {
605 int fp_reg_num = info->GetReg().GetRegNum();
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100606 RegStorage dp_reg = RegStorage::FloatSolo64(fp_reg_num);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100607 RegisterInfo* dp_reg_info = GetRegInfo(dp_reg);
608 // Double precision register's master storage should refer to itself.
609 DCHECK_EQ(dp_reg_info, dp_reg_info->Master());
610 // Redirect single precision's master storage to master.
611 info->SetMaster(dp_reg_info);
612 // Singles should show a single 32-bit mask bit, at first referring to the low half.
613 DCHECK_EQ(info->StorageMask(), 0x1U);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100614 }
615
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100616 // Alias 32bit W registers to corresponding 64bit X registers.
617 GrowableArray<RegisterInfo*>::Iterator w_it(&reg_pool_->core_regs_);
618 for (RegisterInfo* info = w_it.Next(); info != nullptr; info = w_it.Next()) {
619 int x_reg_num = info->GetReg().GetRegNum();
620 RegStorage x_reg = RegStorage::Solo64(x_reg_num);
621 RegisterInfo* x_reg_info = GetRegInfo(x_reg);
622 // 64bit X register's master storage should refer to itself.
623 DCHECK_EQ(x_reg_info, x_reg_info->Master());
624 // Redirect 32bit W master storage to 64bit X.
625 info->SetMaster(x_reg_info);
626 // 32bit W should show a single 32-bit mask bit, at first referring to the low half.
627 DCHECK_EQ(info->StorageMask(), 0x1U);
628 }
629
Matteo Franchin43ec8732014-03-31 15:00:14 +0100630 // Don't start allocating temps at r0/s0/d0 or you may clobber return regs in early-exit methods.
631 // TODO: adjust when we roll to hard float calling convention.
632 reg_pool_->next_core_reg_ = 2;
633 reg_pool_->next_sp_reg_ = 0;
634 reg_pool_->next_dp_reg_ = 0;
635}
636
Matteo Franchin43ec8732014-03-31 15:00:14 +0100637/*
638 * TUNING: is true leaf? Can't just use METHOD_IS_LEAF to determine as some
639 * instructions might call out to C/assembly helper functions. Until
640 * machinery is in place, always spill lr.
641 */
642
643void Arm64Mir2Lir::AdjustSpillMask() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100644 core_spill_mask_ |= (1 << rs_rA64_LR.GetRegNum());
Matteo Franchin43ec8732014-03-31 15:00:14 +0100645 num_core_spills_++;
646}
647
648/*
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100649 * Mark a callee-save fp register as promoted.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100650 */
651void Arm64Mir2Lir::MarkPreservedSingle(int v_reg, RegStorage reg) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100652 DCHECK(reg.IsFloat());
653 int adjusted_reg_num = reg.GetRegNum() - A64_FP_CALLEE_SAVE_BASE;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100654 // Ensure fp_vmap_table is large enough
655 int table_size = fp_vmap_table_.size();
656 for (int i = table_size; i < (adjusted_reg_num + 1); i++) {
657 fp_vmap_table_.push_back(INVALID_VREG);
658 }
659 // Add the current mapping
660 fp_vmap_table_[adjusted_reg_num] = v_reg;
661 // Size of fp_vmap_table is high-water mark, use to set mask
662 num_fp_spills_ = fp_vmap_table_.size();
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100663 fp_spill_mask_ = ((1 << num_fp_spills_) - 1) << A64_FP_CALLEE_SAVE_BASE;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100664}
665
666void Arm64Mir2Lir::MarkPreservedDouble(int v_reg, RegStorage reg) {
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100667 DCHECK(reg.IsDouble());
668 MarkPreservedSingle(v_reg, reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100669}
670
671/* Clobber all regs that might be used by an external C call */
672void Arm64Mir2Lir::ClobberCallerSave() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100673 Clobber(rs_x0);
674 Clobber(rs_x1);
675 Clobber(rs_x2);
676 Clobber(rs_x3);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100677 Clobber(rs_x4);
678 Clobber(rs_x5);
679 Clobber(rs_x6);
680 Clobber(rs_x7);
681 Clobber(rs_x8);
682 Clobber(rs_x9);
683 Clobber(rs_x10);
684 Clobber(rs_x11);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100685 Clobber(rs_x12);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100686 Clobber(rs_x13);
687 Clobber(rs_x14);
688 Clobber(rs_x15);
689 Clobber(rs_x16);
690 Clobber(rs_x17);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100691 Clobber(rs_x30);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100692
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100693 Clobber(rs_f0);
694 Clobber(rs_f1);
695 Clobber(rs_f2);
696 Clobber(rs_f3);
697 Clobber(rs_f4);
698 Clobber(rs_f5);
699 Clobber(rs_f6);
700 Clobber(rs_f7);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100701 Clobber(rs_f16);
702 Clobber(rs_f17);
703 Clobber(rs_f18);
704 Clobber(rs_f19);
705 Clobber(rs_f20);
706 Clobber(rs_f21);
707 Clobber(rs_f22);
708 Clobber(rs_f23);
709 Clobber(rs_f24);
710 Clobber(rs_f25);
711 Clobber(rs_f26);
712 Clobber(rs_f27);
713 Clobber(rs_f28);
714 Clobber(rs_f29);
715 Clobber(rs_f30);
716 Clobber(rs_f31);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100717}
718
719RegLocation Arm64Mir2Lir::GetReturnWideAlt() {
720 RegLocation res = LocCReturnWide();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100721 res.reg.SetReg(rx2);
722 res.reg.SetHighReg(rx3);
723 Clobber(rs_x2);
724 Clobber(rs_x3);
725 MarkInUse(rs_x2);
726 MarkInUse(rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100727 MarkWide(res.reg);
728 return res;
729}
730
731RegLocation Arm64Mir2Lir::GetReturnAlt() {
732 RegLocation res = LocCReturn();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100733 res.reg.SetReg(rx1);
734 Clobber(rs_x1);
735 MarkInUse(rs_x1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100736 return res;
737}
738
739/* To be used when explicitly managing register use */
740void Arm64Mir2Lir::LockCallTemps() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100741 LockTemp(rs_x0);
742 LockTemp(rs_x1);
743 LockTemp(rs_x2);
744 LockTemp(rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100745}
746
747/* To be used when explicitly managing register use */
748void Arm64Mir2Lir::FreeCallTemps() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100749 FreeTemp(rs_x0);
750 FreeTemp(rs_x1);
751 FreeTemp(rs_x2);
752 FreeTemp(rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100753}
754
Andreas Gampe2f244e92014-05-08 03:35:25 -0700755RegStorage Arm64Mir2Lir::LoadHelper(ThreadOffset<4> offset) {
756 UNIMPLEMENTED(FATAL) << "Should not be called.";
757 return RegStorage::InvalidReg();
758}
759
760RegStorage Arm64Mir2Lir::LoadHelper(ThreadOffset<8> offset) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100761 // TODO(Arm64): use LoadWordDisp instead.
762 // e.g. LoadWordDisp(rs_rA64_SELF, offset.Int32Value(), rs_rA64_LR);
763 LoadBaseDisp(rs_rA64_SELF, offset.Int32Value(), rs_rA64_LR, k64);
764 return rs_rA64_LR;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100765}
766
767LIR* Arm64Mir2Lir::CheckSuspendUsingLoad() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100768 RegStorage tmp = rs_x0;
Andreas Gampe2f244e92014-05-08 03:35:25 -0700769 LoadWordDisp(rs_rA64_SELF, Thread::ThreadSuspendTriggerOffset<8>().Int32Value(), tmp);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100770 LIR* load2 = LoadWordDisp(tmp, 0, tmp);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100771 return load2;
772}
773
774uint64_t Arm64Mir2Lir::GetTargetInstFlags(int opcode) {
775 DCHECK(!IsPseudoLirOp(opcode));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100776 return Arm64Mir2Lir::EncodingMap[UNWIDE(opcode)].flags;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100777}
778
779const char* Arm64Mir2Lir::GetTargetInstName(int opcode) {
780 DCHECK(!IsPseudoLirOp(opcode));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100781 return Arm64Mir2Lir::EncodingMap[UNWIDE(opcode)].name;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100782}
783
784const char* Arm64Mir2Lir::GetTargetInstFmt(int opcode) {
785 DCHECK(!IsPseudoLirOp(opcode));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100786 return Arm64Mir2Lir::EncodingMap[UNWIDE(opcode)].fmt;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100787}
788
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100789// TODO(Arm64): reuse info in QuickArgumentVisitor?
790static RegStorage GetArgPhysicalReg(RegLocation* loc, int* num_gpr_used, int* num_fpr_used,
791 OpSize* op_size) {
792 if (loc->fp) {
793 int n = *num_fpr_used;
794 if (n < 8) {
795 *num_fpr_used = n + 1;
796 RegStorage::RegStorageKind reg_kind;
797 if (loc->wide) {
798 *op_size = kDouble;
799 reg_kind = RegStorage::k64BitSolo;
800 } else {
801 *op_size = kSingle;
802 reg_kind = RegStorage::k32BitSolo;
803 }
804 return RegStorage(RegStorage::kValid | reg_kind | RegStorage::kFloatingPoint | n);
805 }
806 } else {
807 int n = *num_gpr_used;
808 if (n < 7) {
809 *num_gpr_used = n + 1;
810 if (loc->wide) {
811 *op_size = k64;
812 return RegStorage::Solo64(n);
813 } else {
814 *op_size = k32;
815 return RegStorage::Solo32(n);
816 }
817 }
818 }
Ian Rogers54874942014-06-10 16:31:03 -0700819 *op_size = kWord;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100820 return RegStorage::InvalidReg();
821}
822
823/*
824 * If there are any ins passed in registers that have not been promoted
825 * to a callee-save register, flush them to the frame. Perform initial
826 * assignment of promoted arguments.
827 *
828 * ArgLocs is an array of location records describing the incoming arguments
829 * with one location record per word of argument.
830 */
831void Arm64Mir2Lir::FlushIns(RegLocation* ArgLocs, RegLocation rl_method) {
832 int num_gpr_used = 1;
833 int num_fpr_used = 0;
834
835 /*
Zheng Xu511c8a62014-06-03 16:22:23 +0800836 * Dummy up a RegLocation for the incoming StackReference<mirror::ArtMethod>
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100837 * It will attempt to keep kArg0 live (or copy it to home location
838 * if promoted).
839 */
840 RegLocation rl_src = rl_method;
841 rl_src.location = kLocPhysReg;
842 rl_src.reg = TargetReg(kArg0);
843 rl_src.home = false;
844 MarkLive(rl_src);
Zheng Xu511c8a62014-06-03 16:22:23 +0800845 StoreValue(rl_method, rl_src);
846 // If Method* has been promoted, explicitly flush
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100847 if (rl_method.location == kLocPhysReg) {
Zheng Xu511c8a62014-06-03 16:22:23 +0800848 StoreRefDisp(TargetReg(kSp), 0, TargetReg(kArg0));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100849 }
850
851 if (cu_->num_ins == 0) {
852 return;
853 }
854
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100855 // Handle dalvik registers.
856 ScopedMemRefType mem_ref_type(this, ResourceMask::kDalvikReg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100857 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
858 for (int i = 0; i < cu_->num_ins; i++) {
859 PromotionMap* v_map = &promotion_map_[start_vreg + i];
860 RegLocation* t_loc = &ArgLocs[i];
861 OpSize op_size;
862 RegStorage reg = GetArgPhysicalReg(t_loc, &num_gpr_used, &num_fpr_used, &op_size);
863
864 if (reg.Valid()) {
865 if ((v_map->core_location == kLocPhysReg) && !t_loc->fp) {
866 OpRegCopy(RegStorage::Solo32(v_map->core_reg), reg);
867 } else if ((v_map->fp_location == kLocPhysReg) && t_loc->fp) {
868 OpRegCopy(RegStorage::Solo32(v_map->FpReg), reg);
869 } else {
870 StoreBaseDisp(TargetReg(kSp), SRegOffset(start_vreg + i), reg, op_size);
871 if (reg.Is64Bit()) {
872 if (SRegOffset(start_vreg + i) + 4 != SRegOffset(start_vreg + i + 1)) {
873 LOG(FATAL) << "64 bit value stored in non-consecutive 4 bytes slots";
874 }
875 i += 1;
876 }
877 }
878 } else {
879 // If arriving in frame & promoted
880 if (v_map->core_location == kLocPhysReg) {
881 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i),
882 RegStorage::Solo32(v_map->core_reg));
883 }
884 if (v_map->fp_location == kLocPhysReg) {
885 LoadWordDisp(TargetReg(kSp), SRegOffset(start_vreg + i), RegStorage::Solo32(v_map->FpReg));
886 }
887 }
888 }
889}
890
891int Arm64Mir2Lir::LoadArgRegs(CallInfo* info, int call_state,
892 NextCallInsn next_call_insn,
893 const MethodReference& target_method,
894 uint32_t vtable_idx, uintptr_t direct_code,
895 uintptr_t direct_method, InvokeType type, bool skip_this) {
896 int last_arg_reg = TargetReg(kArg3).GetReg();
897 int next_reg = TargetReg(kArg1).GetReg();
898 int next_arg = 0;
899 if (skip_this) {
900 next_reg++;
901 next_arg++;
902 }
903 for (; (next_reg <= last_arg_reg) && (next_arg < info->num_arg_words); next_reg++) {
904 RegLocation rl_arg = info->args[next_arg++];
905 rl_arg = UpdateRawLoc(rl_arg);
906 if (rl_arg.wide && (next_reg <= TargetReg(kArg2).GetReg())) {
Zheng Xu511c8a62014-06-03 16:22:23 +0800907 LoadValueDirectWideFixed(rl_arg, RegStorage::Solo64(next_reg));
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100908 next_arg++;
909 } else {
910 if (rl_arg.wide) {
911 rl_arg = NarrowRegLoc(rl_arg);
912 rl_arg.is_const = false;
913 }
914 LoadValueDirectFixed(rl_arg, RegStorage::Solo32(next_reg));
915 }
916 call_state = next_call_insn(cu_, info, call_state, target_method, vtable_idx,
917 direct_code, direct_method, type);
918 }
919 return call_state;
920}
921
Matteo Franchin43ec8732014-03-31 15:00:14 +0100922} // namespace art