blob: acaad5b26b0ff0734dd1ee0728e79bdcbc9468a6 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
buzbee1bc37c62012-11-20 13:35:41 -080018#include "../compiler_ir.h"
19#include "ralloc_util.h"
20#include "codegen_util.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021
buzbee31a4a6f2012-02-28 15:36:15 -080022namespace art {
23
24/*
25 * This source files contains "gen" codegen routines that should
26 * be applicable to most targets. Only mid-level support utilities
27 * and "op" calls may be used here.
28 */
buzbee31a4a6f2012-02-28 15:36:15 -080029
buzbee31a4a6f2012-02-28 15:36:15 -080030/*
31 * Generate an kPseudoBarrier marker to indicate the boundary of special
32 * blocks.
33 */
buzbee02031b12012-11-23 09:41:35 -080034void Codegen::GenBarrier(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -080035{
buzbeefa57c472012-11-21 12:06:18 -080036 LIR* barrier = NewLIR0(cu, kPseudoBarrier);
Bill Buzbeea114add2012-05-03 15:00:40 -070037 /* Mark all resources as being clobbered */
buzbeefa57c472012-11-21 12:06:18 -080038 barrier->def_mask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -080039}
40
buzbee5de34942012-03-01 14:51:57 -080041// FIXME: need to do some work to split out targets with
42// condition codes and those without
buzbee02031b12012-11-23 09:41:35 -080043LIR* Codegen::GenCheck(CompilationUnit* cu, ConditionCode c_code, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080044{
buzbeefa57c472012-11-21 12:06:18 -080045 DCHECK_NE(cu->instruction_set, kMips);
46 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
47 cu->current_dalvik_offset);
48 LIR* branch = OpCondBranch(cu, c_code, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070049 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080050 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070051 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080052}
53
buzbee02031b12012-11-23 09:41:35 -080054LIR* Codegen::GenImmedCheck(CompilationUnit* cu, ConditionCode c_code, int reg, int imm_val,
55 ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080056{
buzbeefa57c472012-11-21 12:06:18 -080057 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
58 cu->current_dalvik_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -070059 LIR* branch;
buzbeefa57c472012-11-21 12:06:18 -080060 if (c_code == kCondAl) {
61 branch = OpUnconditionalBranch(cu, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070062 } else {
buzbeefa57c472012-11-21 12:06:18 -080063 branch = OpCmpImmBranch(cu, c_code, reg, imm_val, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070064 }
65 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080066 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070067 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080068}
69
70/* Perform null-check on a register. */
buzbee02031b12012-11-23 09:41:35 -080071LIR* Codegen::GenNullCheck(CompilationUnit* cu, int s_reg, int m_reg, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -080072{
buzbeefa57c472012-11-21 12:06:18 -080073 if (!(cu->disable_opt & (1 << kNullCheckElimination)) &&
74 opt_flags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -070075 return NULL;
76 }
buzbeefa57c472012-11-21 12:06:18 -080077 return GenImmedCheck(cu, kCondEq, m_reg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -080078}
79
80/* Perform check on two registers */
buzbee02031b12012-11-23 09:41:35 -080081LIR* Codegen::GenRegRegCheck(CompilationUnit* cu, ConditionCode c_code, int reg1, int reg2,
82 ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080083{
buzbeefa57c472012-11-21 12:06:18 -080084 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
85 cu->current_dalvik_offset, reg1, reg2);
86 LIR* branch = OpCmpBranch(cu, c_code, reg1, reg2, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070087 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080088 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070089 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080090}
91
buzbeee6285f92012-12-06 15:57:46 -080092// Convert relation of src1/src2 to src2/src1
93ConditionCode FlipComparisonOrder(ConditionCode before) {
94 ConditionCode res;
95 switch (before) {
96 case kCondEq: res = kCondEq; break;
97 case kCondNe: res = kCondNe; break;
98 case kCondLt: res = kCondGt; break;
99 case kCondGt: res = kCondLt; break;
100 case kCondLe: res = kCondGe; break;
101 case kCondGe: res = kCondLe; break;
102 default:
103 res = static_cast<ConditionCode>(0);
104 LOG(FATAL) << "Unexpected ccode " << before;
105 }
106 return res;
107}
108
buzbee02031b12012-11-23 09:41:35 -0800109void Codegen::GenCompareAndBranch(CompilationUnit* cu, Instruction::Code opcode,
110 RegLocation rl_src1, RegLocation rl_src2, LIR* taken,
111 LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800112{
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 ConditionCode cond;
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 switch (opcode) {
115 case Instruction::IF_EQ:
116 cond = kCondEq;
117 break;
118 case Instruction::IF_NE:
119 cond = kCondNe;
120 break;
121 case Instruction::IF_LT:
122 cond = kCondLt;
123 break;
124 case Instruction::IF_GE:
125 cond = kCondGe;
126 break;
127 case Instruction::IF_GT:
128 cond = kCondGt;
129 break;
130 case Instruction::IF_LE:
131 cond = kCondLe;
132 break;
133 default:
buzbeecbd6d442012-11-17 14:11:25 -0800134 cond = static_cast<ConditionCode>(0);
135 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 }
buzbeee6285f92012-12-06 15:57:46 -0800137
138 // Normalize such that if either operand is constant, src2 will be constant
139 if (rl_src1.is_const) {
140 RegLocation rl_temp = rl_src1;
141 rl_src1 = rl_src2;
142 rl_src2 = rl_temp;
143 cond = FlipComparisonOrder(cond);
144 }
145
146 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
147 // Is this really an immediate comparison?
148 if (rl_src2.is_const) {
149 int immval = cu->constant_values[rl_src2.orig_sreg];
150 // If it's already live in a register or not easily materialized, just keep going
151 RegLocation rl_temp = UpdateLoc(cu, rl_src2);
152 if ((rl_temp.location == kLocDalvikFrame) && InexpensiveConstant(rl_src1.low_reg, immval)) {
153 // OK - convert this to a compare immediate and branch
154 OpCmpImmBranch(cu, cond, rl_src1.low_reg, immval, taken);
155 OpUnconditionalBranch(cu, fall_through);
156 return;
157 }
158 }
159 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -0800160 OpCmpBranch(cu, cond, rl_src1.low_reg, rl_src2.low_reg, taken);
161 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800162}
163
buzbee02031b12012-11-23 09:41:35 -0800164void Codegen::GenCompareZeroAndBranch(CompilationUnit* cu, Instruction::Code opcode,
165 RegLocation rl_src, LIR* taken, LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800166{
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 ConditionCode cond;
buzbeefa57c472012-11-21 12:06:18 -0800168 rl_src = LoadValue(cu, rl_src, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 switch (opcode) {
170 case Instruction::IF_EQZ:
171 cond = kCondEq;
172 break;
173 case Instruction::IF_NEZ:
174 cond = kCondNe;
175 break;
176 case Instruction::IF_LTZ:
177 cond = kCondLt;
178 break;
179 case Instruction::IF_GEZ:
180 cond = kCondGe;
181 break;
182 case Instruction::IF_GTZ:
183 cond = kCondGt;
184 break;
185 case Instruction::IF_LEZ:
186 cond = kCondLe;
187 break;
188 default:
buzbeecbd6d442012-11-17 14:11:25 -0800189 cond = static_cast<ConditionCode>(0);
190 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
buzbeee6285f92012-12-06 15:57:46 -0800192 OpCmpImmBranch(cu, cond, rl_src.low_reg, 0, taken);
buzbeefa57c472012-11-21 12:06:18 -0800193 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800194}
195
buzbee02031b12012-11-23 09:41:35 -0800196void Codegen::GenIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800197{
buzbeefa57c472012-11-21 12:06:18 -0800198 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
199 if (rl_src.location == kLocPhysReg) {
200 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 } else {
buzbeefa57c472012-11-21 12:06:18 -0800202 LoadValueDirect(cu, rl_src, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 }
buzbeefa57c472012-11-21 12:06:18 -0800204 OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
205 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800206}
207
buzbee02031b12012-11-23 09:41:35 -0800208void Codegen::GenIntNarrowing(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
209 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800210{
buzbeefa57c472012-11-21 12:06:18 -0800211 rl_src = LoadValue(cu, rl_src, kCoreReg);
212 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700214 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 case Instruction::INT_TO_BYTE:
216 op = kOp2Byte;
217 break;
218 case Instruction::INT_TO_SHORT:
219 op = kOp2Short;
220 break;
221 case Instruction::INT_TO_CHAR:
222 op = kOp2Char;
223 break;
224 default:
225 LOG(ERROR) << "Bad int conversion type";
226 }
buzbeefa57c472012-11-21 12:06:18 -0800227 OpRegReg(cu, op, rl_result.low_reg, rl_src.low_reg);
228 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800229}
230
231/*
232 * Let helper function take care of everything. Will call
233 * Array::AllocFromCode(type_idx, method, count);
234 * Note: AllocFromCode will handle checks for errNegativeArraySize.
235 */
buzbee02031b12012-11-23 09:41:35 -0800236void Codegen::GenNewArray(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
237 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800238{
buzbeefa57c472012-11-21 12:06:18 -0800239 FlushAllRegs(cu); /* Everything to home location */
240 int func_offset;
241 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
242 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800244 func_offset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 } else {
buzbeefa57c472012-11-21 12:06:18 -0800246 func_offset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 }
buzbeefa57c472012-11-21 12:06:18 -0800248 CallRuntimeHelperImmMethodRegLocation(cu, func_offset, type_idx, rl_src, true);
249 RegLocation rl_result = GetReturn(cu, false);
250 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800251}
252
253/*
buzbee52a77fc2012-11-20 19:50:46 -0800254 * Similar to GenNewArray, but with post-allocation initialization.
buzbee31a4a6f2012-02-28 15:36:15 -0800255 * Verifier guarantees we're dealing with an array class. Current
256 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
257 * Current code also throws internal unimp if not 'L', '[' or 'I'.
258 */
buzbee02031b12012-11-23 09:41:35 -0800259void Codegen::GenFilledNewArray(CompilationUnit* cu, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800260{
buzbeefa57c472012-11-21 12:06:18 -0800261 int elems = info->num_arg_words;
262 int type_idx = info->index;
263 FlushAllRegs(cu); /* Everything to home location */
264 int func_offset;
265 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
266 *cu->dex_file,
267 type_idx)) {
268 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 } else {
buzbeefa57c472012-11-21 12:06:18 -0800270 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 }
buzbeefa57c472012-11-21 12:06:18 -0800272 CallRuntimeHelperImmMethodImm(cu, func_offset, type_idx, elems, true);
273 FreeTemp(cu, TargetReg(kArg2));
274 FreeTemp(cu, TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 /*
276 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
277 * return region. Because AllocFromCode placed the new array
buzbeef0504cd2012-11-13 16:31:10 -0800278 * in kRet0, we'll just lock it into place. When debugger support is
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 * added, it may be necessary to additionally copy all return
280 * values to a home location in thread-local storage
281 */
buzbeefa57c472012-11-21 12:06:18 -0800282 LockTemp(cu, TargetReg(kRet0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700283
284 // TODO: use the correct component size, currently all supported types
285 // share array alignment with ints (see comment at head of function)
286 size_t component_size = sizeof(int32_t);
287
288 // Having a range of 0 is legal
buzbeefa57c472012-11-21 12:06:18 -0800289 if (info->is_range && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800290 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 * Bit of ugliness here. We're going generate a mem copy loop
292 * on the register range, but it is possible that some regs
293 * in the range have been promoted. This is unlikely, but
294 * before generating the copy, we'll just force a flush
295 * of any regs in the source range that have been promoted to
296 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800297 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700298 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800299 RegLocation loc = UpdateLoc(cu, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800301 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
302 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 }
buzbee31a4a6f2012-02-28 15:36:15 -0800304 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 /*
306 * TUNING note: generated code here could be much improved, but
307 * this is an uncommon operation and isn't especially performance
308 * critical.
309 */
buzbeefa57c472012-11-21 12:06:18 -0800310 int r_src = AllocTemp(cu);
311 int r_dst = AllocTemp(cu);
312 int r_idx = AllocTemp(cu);
313 int r_val = INVALID_REG;
314 switch(cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700315 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800316 r_val = TargetReg(kLr);
buzbeeb046e162012-10-30 15:48:42 -0700317 break;
318 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800319 FreeTemp(cu, TargetReg(kRet0));
320 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700321 break;
322 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800323 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700324 break;
buzbeefa57c472012-11-21 12:06:18 -0800325 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700326 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 // Set up source pointer
buzbeefa57c472012-11-21 12:06:18 -0800328 RegLocation rl_first = info->args[0];
329 OpRegRegImm(cu, kOpAdd, r_src, TargetReg(kSp),
330 SRegOffset(cu, rl_first.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 // Set up the target pointer
buzbeefa57c472012-11-21 12:06:18 -0800332 OpRegRegImm(cu, kOpAdd, r_dst, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 mirror::Array::DataOffset(component_size).Int32Value());
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 // Set up the loop counter (known to be > 0)
buzbeefa57c472012-11-21 12:06:18 -0800335 LoadConstant(cu, r_idx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 // Generate the copy loop. Going backwards for convenience
buzbeefa57c472012-11-21 12:06:18 -0800337 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 // Copy next element
buzbeefa57c472012-11-21 12:06:18 -0800339 LoadBaseIndexed(cu, r_src, r_idx, r_val, 2, kWord);
340 StoreBaseIndexed(cu, r_dst, r_idx, r_val, 2, kWord);
341 FreeTemp(cu, r_val);
342 OpDecAndBranch(cu, kCondGe, r_idx, target);
343 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700344 // Restore the target pointer
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 OpRegRegImm(cu, kOpAdd, TargetReg(kRet0), r_dst,
346 -mirror::Array::DataOffset(component_size).Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700347 }
buzbeefa57c472012-11-21 12:06:18 -0800348 } else if (!info->is_range) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700350 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800351 RegLocation rl_arg = LoadValue(cu, info->args[i], kCoreReg);
352 StoreBaseDisp(cu, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 mirror::Array::DataOffset(component_size).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800354 i * 4, rl_arg.low_reg, kWord);
buzbee52a77fc2012-11-20 19:50:46 -0800355 // If the LoadValue caused a temp to be allocated, free it
buzbeefa57c472012-11-21 12:06:18 -0800356 if (IsTemp(cu, rl_arg.low_reg)) {
357 FreeTemp(cu, rl_arg.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
359 }
360 }
buzbeee5f01222012-06-14 15:19:35 -0700361 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800362 StoreValue(cu, info->result, GetReturn(cu, false /* not fp */));
buzbeee5f01222012-06-14 15:19:35 -0700363 }
buzbee31a4a6f2012-02-28 15:36:15 -0800364}
365
buzbee02031b12012-11-23 09:41:35 -0800366void Codegen::GenSput(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_src,
367 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800368{
buzbeefa57c472012-11-21 12:06:18 -0800369 int field_offset;
370 int ssb_index;
371 bool is_volatile;
372 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800373
TDYa127dc5daa02013-01-09 21:31:37 +0800374 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file, cu->code_item,
375 cu->class_def_idx, cu->method_idx, cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800376
buzbeefa57c472012-11-21 12:06:18 -0800377 bool fast_path =
378 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
379 field_offset, ssb_index,
380 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 true);
buzbeefa57c472012-11-21 12:06:18 -0800382 if (fast_path && !SLOW_FIELD_PATH) {
383 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800385 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800387 RegLocation rl_method = LoadCurrMethod(cu);
388 rBase = AllocTemp(cu);
389 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbeefa57c472012-11-21 12:06:18 -0800391 if (IsTemp(cu, rl_method.low_reg)) {
392 FreeTemp(cu, rl_method.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700393 }
buzbee31a4a6f2012-02-28 15:36:15 -0800394 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 // Medium path, static storage base in a different class which
396 // requires checks that the other class is initialized.
buzbeefa57c472012-11-21 12:06:18 -0800397 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800399 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 // Using fixed register to sync with possible call to runtime
401 // support.
buzbeefa57c472012-11-21 12:06:18 -0800402 int r_method = TargetReg(kArg1);
403 LockTemp(cu, r_method);
404 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800405 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800406 LockTemp(cu, rBase);
407 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800410 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800412 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 // rBase now points at appropriate static storage base (Class*)
414 // or NULL if not initialized. Check for NULL and call helper if NULL.
415 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800416 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
417 LoadConstant(cu, TargetReg(kArg0), ssb_index);
418 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
419 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800420 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800421 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700422 }
buzbeefa57c472012-11-21 12:06:18 -0800423 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
424 branch_over->target = skip_target;
425 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800426 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800428 if (is_long_or_double) {
429 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 } else {
buzbeefa57c472012-11-21 12:06:18 -0800431 rl_src = LoadValue(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 }
buzbeefa57c472012-11-21 12:06:18 -0800433 if (is_volatile) {
434 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 }
buzbeefa57c472012-11-21 12:06:18 -0800436 if (is_long_or_double) {
437 StoreBaseDispWide(cu, rBase, field_offset, rl_src.low_reg,
438 rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 } else {
buzbeefa57c472012-11-21 12:06:18 -0800440 StoreWordDisp(cu, rBase, field_offset, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 }
buzbeefa57c472012-11-21 12:06:18 -0800442 if (is_volatile) {
443 GenMemBarrier(cu, kStoreLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 }
buzbeefa57c472012-11-21 12:06:18 -0800445 if (is_object) {
446 MarkGCCard(cu, rl_src.low_reg, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 }
buzbeefa57c472012-11-21 12:06:18 -0800448 FreeTemp(cu, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 } else {
buzbeefa57c472012-11-21 12:06:18 -0800450 FlushAllRegs(cu); // Everything to home locations
451 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Static) :
452 (is_object ? ENTRYPOINT_OFFSET(pSetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 : ENTRYPOINT_OFFSET(pSet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800454 CallRuntimeHelperImmRegLocation(cu, setter_offset, field_idx, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 }
buzbee31a4a6f2012-02-28 15:36:15 -0800456}
457
buzbee02031b12012-11-23 09:41:35 -0800458void Codegen::GenSget(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_dest,
459 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800460{
buzbeefa57c472012-11-21 12:06:18 -0800461 int field_offset;
462 int ssb_index;
463 bool is_volatile;
464 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800465
buzbeefa57c472012-11-21 12:06:18 -0800466 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +0800467 *cu->dex_file, cu->code_item,
468 cu->class_def_idx, cu->method_idx,
469 cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800470
buzbeefa57c472012-11-21 12:06:18 -0800471 bool fast_path =
472 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
473 field_offset, ssb_index,
474 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 false);
buzbeefa57c472012-11-21 12:06:18 -0800476 if (fast_path && !SLOW_FIELD_PATH) {
477 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800479 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800481 RegLocation rl_method = LoadCurrMethod(cu);
482 rBase = AllocTemp(cu);
483 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800484 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800485 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 // Medium path, static storage base in a different class which
487 // requires checks that the other class is initialized
buzbeefa57c472012-11-21 12:06:18 -0800488 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800490 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 // Using fixed register to sync with possible call to runtime
492 // support
buzbeefa57c472012-11-21 12:06:18 -0800493 int r_method = TargetReg(kArg1);
494 LockTemp(cu, r_method);
495 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800496 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800497 LockTemp(cu, rBase);
498 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800501 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800503 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 // rBase now points at appropriate static storage base (Class*)
505 // or NULL if not initialized. Check for NULL and call helper if NULL.
506 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800507 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
508 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
509 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800510 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800511 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700512 }
buzbeefa57c472012-11-21 12:06:18 -0800513 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
514 branch_over->target = skip_target;
515 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800516 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800518 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
519 if (is_volatile) {
520 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 }
buzbeefa57c472012-11-21 12:06:18 -0800522 if (is_long_or_double) {
523 LoadBaseDispWide(cu, rBase, field_offset, rl_result.low_reg,
524 rl_result.high_reg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 } else {
buzbeefa57c472012-11-21 12:06:18 -0800526 LoadWordDisp(cu, rBase, field_offset, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 }
buzbeefa57c472012-11-21 12:06:18 -0800528 FreeTemp(cu, rBase);
529 if (is_long_or_double) {
530 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700531 } else {
buzbeefa57c472012-11-21 12:06:18 -0800532 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 }
534 } else {
buzbeefa57c472012-11-21 12:06:18 -0800535 FlushAllRegs(cu); // Everything to home locations
536 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Static) :
537 (is_object ? ENTRYPOINT_OFFSET(pGetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 : ENTRYPOINT_OFFSET(pGet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800539 CallRuntimeHelperImm(cu, getterOffset, field_idx, true);
540 if (is_long_or_double) {
541 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
542 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 } else {
buzbeefa57c472012-11-21 12:06:18 -0800544 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
545 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700546 }
547 }
buzbee31a4a6f2012-02-28 15:36:15 -0800548}
549
550
551// Debugging routine - if null target, branch to DebugMe
buzbee02031b12012-11-23 09:41:35 -0800552void Codegen::GenShowTarget(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800553{
buzbeefa57c472012-11-21 12:06:18 -0800554 DCHECK_NE(cu->instruction_set, kX86) << "unimplemented GenShowTarget";
555 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, TargetReg(kInvokeTgt), 0, NULL);
556 LoadWordDisp(cu, TargetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), TargetReg(kInvokeTgt));
557 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
558 branch_over->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800559}
560
buzbee02031b12012-11-23 09:41:35 -0800561void Codegen::HandleSuspendLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800562{
buzbeefa57c472012-11-21 12:06:18 -0800563 LIR** suspend_label = reinterpret_cast<LIR**>(cu->suspend_launchpads.elem_list);
564 int num_elems = cu->suspend_launchpads.num_used;
565 int helper_offset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
566 for (int i = 0; i < num_elems; i++) {
567 ResetRegPool(cu);
568 ResetDefTracking(cu);
569 LIR* lab = suspend_label[i];
570 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[0]);
571 cu->current_dalvik_offset = lab->operands[1];
572 AppendLIR(cu, lab);
573 int r_tgt = CallHelperSetup(cu, helper_offset);
574 CallHelper(cu, r_tgt, helper_offset, true /* MarkSafepointPC */);
575 OpUnconditionalBranch(cu, resume_lab);
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 }
buzbee31a4a6f2012-02-28 15:36:15 -0800577}
578
buzbee02031b12012-11-23 09:41:35 -0800579void Codegen::HandleIntrinsicLaunchPads(CompilationUnit *cu)
buzbeefc9e6fa2012-03-23 15:14:29 -0700580{
buzbeefa57c472012-11-21 12:06:18 -0800581 LIR** intrinsic_label = reinterpret_cast<LIR**>(cu->intrinsic_launchpads.elem_list);
582 int num_elems = cu->intrinsic_launchpads.num_used;
583 for (int i = 0; i < num_elems; i++) {
584 ResetRegPool(cu);
585 ResetDefTracking(cu);
586 LIR* lab = intrinsic_label[i];
buzbeecbd6d442012-11-17 14:11:25 -0800587 CallInfo* info = reinterpret_cast<CallInfo*>(lab->operands[0]);
buzbeefa57c472012-11-21 12:06:18 -0800588 cu->current_dalvik_offset = info->offset;
589 AppendLIR(cu, lab);
buzbee52a77fc2012-11-20 19:50:46 -0800590 // NOTE: GenInvoke handles MarkSafepointPC
buzbeefa57c472012-11-21 12:06:18 -0800591 GenInvoke(cu, info);
592 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[2]);
593 if (resume_lab != NULL) {
594 OpUnconditionalBranch(cu, resume_lab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700595 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700597}
598
buzbee02031b12012-11-23 09:41:35 -0800599void Codegen::HandleThrowLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800600{
buzbeefa57c472012-11-21 12:06:18 -0800601 LIR** throw_label = reinterpret_cast<LIR**>(cu->throw_launchpads.elem_list);
602 int num_elems = cu->throw_launchpads.num_used;
603 for (int i = 0; i < num_elems; i++) {
604 ResetRegPool(cu);
605 ResetDefTracking(cu);
606 LIR* lab = throw_label[i];
607 cu->current_dalvik_offset = lab->operands[1];
608 AppendLIR(cu, lab);
609 int func_offset = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700610 int v1 = lab->operands[2];
611 int v2 = lab->operands[3];
buzbeefa57c472012-11-21 12:06:18 -0800612 bool target_x86 = (cu->instruction_set == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700613 switch (lab->operands[0]) {
614 case kThrowNullPointer:
buzbeefa57c472012-11-21 12:06:18 -0800615 func_offset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 break;
617 case kThrowArrayBounds:
buzbeef0504cd2012-11-13 16:31:10 -0800618 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee52a77fc2012-11-20 19:50:46 -0800619 if (v2 != TargetReg(kArg0)) {
buzbeefa57c472012-11-21 12:06:18 -0800620 OpRegCopy(cu, TargetReg(kArg0), v1);
621 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700622 // x86 leaves the array pointer in v2, so load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800623 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700624 } else {
buzbeefa57c472012-11-21 12:06:18 -0800625 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700626 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700627 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800628 if (v1 == TargetReg(kArg1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800629 // Swap v1 and v2, using kArg2 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800630 OpRegCopy(cu, TargetReg(kArg2), v1);
631 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700632 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700634 } else {
buzbeefa57c472012-11-21 12:06:18 -0800635 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700636 }
buzbeefa57c472012-11-21 12:06:18 -0800637 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 } else {
buzbeefa57c472012-11-21 12:06:18 -0800639 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700640 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700642 } else {
buzbeefa57c472012-11-21 12:06:18 -0800643 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700644 }
buzbeefa57c472012-11-21 12:06:18 -0800645 OpRegCopy(cu, TargetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 }
buzbee31a4a6f2012-02-28 15:36:15 -0800647 }
buzbeefa57c472012-11-21 12:06:18 -0800648 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700649 break;
650 case kThrowDivZero:
buzbeefa57c472012-11-21 12:06:18 -0800651 func_offset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 case kThrowNoSuchMethod:
buzbeefa57c472012-11-21 12:06:18 -0800654 OpRegCopy(cu, TargetReg(kArg0), v1);
655 func_offset =
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
657 break;
658 case kThrowStackOverflow:
buzbeefa57c472012-11-21 12:06:18 -0800659 func_offset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 // Restore stack alignment
buzbeefa57c472012-11-21 12:06:18 -0800661 if (target_x86) {
662 OpRegImm(cu, kOpAdd, TargetReg(kSp), cu->frame_size);
buzbeeb046e162012-10-30 15:48:42 -0700663 } else {
buzbeefa57c472012-11-21 12:06:18 -0800664 OpRegImm(cu, kOpAdd, TargetReg(kSp), (cu->num_core_spills + cu->num_fp_spills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700665 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 break;
667 default:
668 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800669 }
buzbeefa57c472012-11-21 12:06:18 -0800670 ClobberCalleeSave(cu);
671 int r_tgt = CallHelperSetup(cu, func_offset);
672 CallHelper(cu, r_tgt, func_offset, true /* MarkSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700673 }
buzbee31a4a6f2012-02-28 15:36:15 -0800674}
675
buzbee02031b12012-11-23 09:41:35 -0800676void Codegen::GenIGet(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
677 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
678 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800679{
buzbeefa57c472012-11-21 12:06:18 -0800680 int field_offset;
681 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800682
buzbeefa57c472012-11-21 12:06:18 -0800683 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800684
buzbeefa57c472012-11-21 12:06:18 -0800685 if (fast_path && !SLOW_FIELD_PATH) {
686 RegLocation rl_result;
687 RegisterClass reg_class = oat_reg_class_by_size(size);
688 DCHECK_GE(field_offset, 0);
689 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
690 if (is_long_or_double) {
691 DCHECK(rl_dest.wide);
692 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
693 if (cu->instruction_set == kX86) {
694 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
695 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
696 LoadBaseDispWide(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
697 rl_result.high_reg, rl_obj.s_reg_low);
698 if (is_volatile) {
699 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700700 }
701 } else {
buzbeefa57c472012-11-21 12:06:18 -0800702 int reg_ptr = AllocTemp(cu);
703 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
704 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
buzbeee6285f92012-12-06 15:57:46 -0800705 LoadBaseDispWide(cu, reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
buzbeefa57c472012-11-21 12:06:18 -0800706 if (is_volatile) {
707 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700708 }
buzbeefa57c472012-11-21 12:06:18 -0800709 FreeTemp(cu, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700710 }
buzbeefa57c472012-11-21 12:06:18 -0800711 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800712 } else {
buzbeefa57c472012-11-21 12:06:18 -0800713 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
714 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
715 LoadBaseDisp(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
716 kWord, rl_obj.s_reg_low);
717 if (is_volatile) {
718 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700719 }
buzbeefa57c472012-11-21 12:06:18 -0800720 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800721 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700722 } else {
buzbeefa57c472012-11-21 12:06:18 -0800723 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Instance) :
724 (is_object ? ENTRYPOINT_OFFSET(pGetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800726 CallRuntimeHelperImmRegLocation(cu, getterOffset, field_idx, rl_obj, true);
727 if (is_long_or_double) {
728 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
729 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700730 } else {
buzbeefa57c472012-11-21 12:06:18 -0800731 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
732 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 }
734 }
buzbee31a4a6f2012-02-28 15:36:15 -0800735}
736
buzbee02031b12012-11-23 09:41:35 -0800737void Codegen::GenIPut(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
738 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
739 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800740{
buzbeefa57c472012-11-21 12:06:18 -0800741 int field_offset;
742 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800743
buzbeefa57c472012-11-21 12:06:18 -0800744 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700745 true);
buzbeefa57c472012-11-21 12:06:18 -0800746 if (fast_path && !SLOW_FIELD_PATH) {
747 RegisterClass reg_class = oat_reg_class_by_size(size);
748 DCHECK_GE(field_offset, 0);
749 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
750 if (is_long_or_double) {
751 int reg_ptr;
752 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
753 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
754 reg_ptr = AllocTemp(cu);
755 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
756 if (is_volatile) {
757 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 }
buzbeefa57c472012-11-21 12:06:18 -0800759 StoreBaseDispWide(cu, reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
760 if (is_volatile) {
761 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 }
buzbeefa57c472012-11-21 12:06:18 -0800763 FreeTemp(cu, reg_ptr);
buzbee31a4a6f2012-02-28 15:36:15 -0800764 } else {
buzbeefa57c472012-11-21 12:06:18 -0800765 rl_src = LoadValue(cu, rl_src, reg_class);
766 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
767 if (is_volatile) {
768 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700769 }
buzbeefa57c472012-11-21 12:06:18 -0800770 StoreBaseDisp(cu, rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
771 if (is_volatile) {
772 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 }
buzbeefa57c472012-11-21 12:06:18 -0800774 if (is_object) {
775 MarkGCCard(cu, rl_src.low_reg, rl_obj.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700776 }
buzbee31a4a6f2012-02-28 15:36:15 -0800777 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 } else {
buzbeefa57c472012-11-21 12:06:18 -0800779 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Instance) :
780 (is_object ? ENTRYPOINT_OFFSET(pSetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700781 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800782 CallRuntimeHelperImmRegLocationRegLocation(cu, setter_offset, field_idx, rl_obj, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700783 }
buzbee31a4a6f2012-02-28 15:36:15 -0800784}
785
buzbee02031b12012-11-23 09:41:35 -0800786void Codegen::GenConstClass(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800787{
buzbeefa57c472012-11-21 12:06:18 -0800788 RegLocation rl_method = LoadCurrMethod(cu);
789 int res_reg = AllocTemp(cu);
790 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
791 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
792 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700793 type_idx)) {
794 // Call out to helper which resolves type and verifies access.
buzbeef0504cd2012-11-13 16:31:10 -0800795 // Resolved type returned in kRet0.
buzbeefa57c472012-11-21 12:06:18 -0800796 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
797 type_idx, rl_method.low_reg, true);
798 RegLocation rl_result = GetReturn(cu, false);
799 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 } else {
801 // We're don't need access checks, load type from dex cache
802 int32_t dex_cache_offset =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800803 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeefa57c472012-11-21 12:06:18 -0800804 LoadWordDisp(cu, rl_method.low_reg, dex_cache_offset, res_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800806 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800808 LoadWordDisp(cu, res_reg, offset_of_type, rl_result.low_reg);
809 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(*cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700810 type_idx) || SLOW_TYPE_PATH) {
811 // Slow path, at runtime test if type is null and if so initialize
buzbeefa57c472012-11-21 12:06:18 -0800812 FlushAllRegs(cu);
813 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, rl_result.low_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700814 // Resolved, store and hop over following code
buzbeefa57c472012-11-21 12:06:18 -0800815 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 /*
817 * Because we have stores of the target value on two paths,
818 * clobber temp tracking for the destination using the ssa name
819 */
buzbeefa57c472012-11-21 12:06:18 -0800820 ClobberSReg(cu, rl_dest.s_reg_low);
821 LIR* branch2 = OpUnconditionalBranch(cu,0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700822 // TUNING: move slow path to end & remove unconditional branch
buzbeefa57c472012-11-21 12:06:18 -0800823 LIR* target1 = NewLIR0(cu, kPseudoTargetLabel);
buzbeef0504cd2012-11-13 16:31:10 -0800824 // Call out to helper, which will return resolved type in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800825 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
826 rl_method.low_reg, true);
827 RegLocation rl_result = GetReturn(cu, false);
828 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 /*
830 * Because we have stores of the target value on two paths,
831 * clobber temp tracking for the destination using the ssa name
832 */
buzbeefa57c472012-11-21 12:06:18 -0800833 ClobberSReg(cu, rl_dest.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800835 LIR* target2 = NewLIR0(cu, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800836 branch1->target = target1;
837 branch2->target = target2;
buzbee31a4a6f2012-02-28 15:36:15 -0800838 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 // Fast path, we're done - just store result
buzbeefa57c472012-11-21 12:06:18 -0800840 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800841 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 }
buzbee31a4a6f2012-02-28 15:36:15 -0800843}
Ian Rogersab2b55d2012-03-18 00:06:11 -0700844
buzbee02031b12012-11-23 09:41:35 -0800845void Codegen::GenConstString(CompilationUnit* cu, uint32_t string_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800846{
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 /* NOTE: Most strings should be available at compile time */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800848 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
849 (sizeof(mirror::String*) * string_idx);
buzbeefa57c472012-11-21 12:06:18 -0800850 if (!cu->compiler->CanAssumeStringIsPresentInDexCache(
851 *cu->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 // slow path, resolve string if not in dex cache
buzbeefa57c472012-11-21 12:06:18 -0800853 FlushAllRegs(cu);
854 LockCallTemps(cu); // Using explicit registers
855 LoadCurrMethodDirect(cu, TargetReg(kArg2));
856 LoadWordDisp(cu, TargetReg(kArg2),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800857 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800858 // Might call out to helper, which will return resolved string in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800859 int r_tgt = CallHelperSetup(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode));
860 LoadWordDisp(cu, TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
861 LoadConstant(cu, TargetReg(kArg1), string_idx);
862 if (cu->instruction_set == kThumb2) {
863 OpRegImm(cu, kOpCmp, TargetReg(kRet0), 0); // Is resolved?
864 GenBarrier(cu);
buzbeeb046e162012-10-30 15:48:42 -0700865 // For testing, always force through helper
866 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee02031b12012-11-23 09:41:35 -0800867 OpIT(cu, kCondEq, "T");
buzbeeb046e162012-10-30 15:48:42 -0700868 }
buzbeefa57c472012-11-21 12:06:18 -0800869 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
870 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
871 MarkSafepointPC(cu, call_inst);
872 FreeTemp(cu, r_tgt);
873 } else if (cu->instruction_set == kMips) {
874 LIR* branch = OpCmpImmBranch(cu, kCondNe, TargetReg(kRet0), 0, NULL);
875 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
876 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
877 MarkSafepointPC(cu, call_inst);
878 FreeTemp(cu, r_tgt);
879 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeb046e162012-10-30 15:48:42 -0700880 branch->target = target;
881 } else {
buzbeefa57c472012-11-21 12:06:18 -0800882 DCHECK_EQ(cu->instruction_set, kX86);
883 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode), TargetReg(kArg2), TargetReg(kArg1), true);
buzbee31a4a6f2012-02-28 15:36:15 -0800884 }
buzbeefa57c472012-11-21 12:06:18 -0800885 GenBarrier(cu);
886 StoreValue(cu, rl_dest, GetReturn(cu, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 } else {
buzbeefa57c472012-11-21 12:06:18 -0800888 RegLocation rl_method = LoadCurrMethod(cu);
889 int res_reg = AllocTemp(cu);
890 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
891 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeefa57c472012-11-21 12:06:18 -0800893 LoadWordDisp(cu, res_reg, offset_of_string, rl_result.low_reg);
894 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 }
buzbee31a4a6f2012-02-28 15:36:15 -0800896}
897
898/*
899 * Let helper function take care of everything. Will
900 * call Class::NewInstanceFromCode(type_idx, method);
901 */
buzbee02031b12012-11-23 09:41:35 -0800902void Codegen::GenNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800903{
buzbeefa57c472012-11-21 12:06:18 -0800904 FlushAllRegs(cu); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700905 // alloc will always check for resolution, do we also need to verify
906 // access because the verifier was unable to?
buzbeefa57c472012-11-21 12:06:18 -0800907 int func_offset;
908 if (cu->compiler->CanAccessInstantiableTypeWithoutChecks(
909 cu->method_idx, *cu->dex_file, type_idx)) {
910 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700911 } else {
buzbeefa57c472012-11-21 12:06:18 -0800912 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 }
buzbeefa57c472012-11-21 12:06:18 -0800914 CallRuntimeHelperImmMethod(cu, func_offset, type_idx, true);
915 RegLocation rl_result = GetReturn(cu, false);
916 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800917}
918
buzbee02031b12012-11-23 09:41:35 -0800919void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700920{
buzbeefa57c472012-11-21 12:06:18 -0800921 FlushAllRegs(cu);
922 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700923}
924
buzbee02031b12012-11-23 09:41:35 -0800925void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
926 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800927{
buzbeefa57c472012-11-21 12:06:18 -0800928 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800930 LockCallTemps(cu);
931 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
932 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
933 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
934 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700935 type_idx)) {
936 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800937 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800938 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700939 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800940 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
941 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 } else {
buzbeefa57c472012-11-21 12:06:18 -0800943 // Load dex cache entry into class_reg (kArg2)
944 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
945 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800946 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800948 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700949 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800950 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
951 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
952 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700953 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800954 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800956 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800957 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
958 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
959 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800961 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
962 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800963 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 }
buzbeef0504cd2012-11-13 16:31:10 -0800965 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800966 RegLocation rl_result = GetReturn(cu, false);
967 if (cu->instruction_set == kMips) {
968 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700969 }
buzbeefa57c472012-11-21 12:06:18 -0800970 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800972 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
973 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800974 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800975 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700976 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800977 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700978 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800979 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
980 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800981 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800982 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
983 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
984 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
985 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700986 } else {
987 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -0800988 LoadConstant(cu, rl_result.low_reg, 1); // assume true
989 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
990 if (cu->instruction_set != kX86) {
991 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
992 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
993 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
994 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700995 } else {
buzbeefa57c472012-11-21 12:06:18 -0800996 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
997 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -0700998 }
999 }
buzbeefa57c472012-11-21 12:06:18 -08001000 MarkSafepointPC(cu, call_inst);
1001 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001002 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -08001003 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
1004 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001005 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001006 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001007 branchover->target = target;
1008 }
buzbee31a4a6f2012-02-28 15:36:15 -08001009}
1010
buzbee02031b12012-11-23 09:41:35 -08001011void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001012{
buzbeefa57c472012-11-21 12:06:18 -08001013 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001015 LockCallTemps(cu);
1016 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1017 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1018 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
1019 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001020 type_idx)) {
1021 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001022 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001023 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001024 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001025 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001026 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 } else {
buzbeefa57c472012-11-21 12:06:18 -08001028 // Load dex cache entry into class_reg (kArg2)
1029 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001030 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001032 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1033 (sizeof(mirror::Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001034 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
1035 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
1036 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001038 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001040 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001041 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001042 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001043 true);
buzbeefa57c472012-11-21 12:06:18 -08001044 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001046 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1047 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001048 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001049 }
buzbeefa57c472012-11-21 12:06:18 -08001050 // At this point, class_reg (kArg2) has class
1051 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001053 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001055 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1056 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001057 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001058 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001059 if (cu->instruction_set == kThumb2) {
1060 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1061 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1062 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1063 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1064 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1065 ClobberCalleeSave(cu);
1066 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1067 MarkSafepointPC(cu, call_inst);
1068 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001069 } else {
buzbeefa57c472012-11-21 12:06:18 -08001070 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1071 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001072 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001073 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001074 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 branch1->target = target;
1076 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001077}
1078
buzbee02031b12012-11-23 09:41:35 -08001079void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1080 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001081{
buzbeefa57c472012-11-21 12:06:18 -08001082 RegLocation rl_result;
1083 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001084 /*
1085 * NOTE: This is the one place in the code in which we might have
1086 * as many as six live temporary registers. There are 5 in the normal
1087 * set for Arm. Until we have spill capabilities, temporarily add
1088 * lr to the temp set. It is safe to do this locally, but note that
1089 * lr is used explicitly elsewhere in the code generator and cannot
1090 * normally be used as a general temp register.
1091 */
buzbeefa57c472012-11-21 12:06:18 -08001092 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1093 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001094 }
buzbeefa57c472012-11-21 12:06:18 -08001095 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1096 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1097 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001098 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001099 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1100 int t_reg = AllocTemp(cu);
1101 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1102 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1103 OpRegCopy(cu, rl_result.low_reg, t_reg);
1104 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001105 } else {
buzbeefa57c472012-11-21 12:06:18 -08001106 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1107 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1108 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001109 }
1110 /*
buzbeefa57c472012-11-21 12:06:18 -08001111 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001112 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001113 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001114 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001115 * Remove when spill is functional.
1116 */
buzbeefa57c472012-11-21 12:06:18 -08001117 FreeRegLocTemps(cu, rl_result, rl_src1);
1118 FreeRegLocTemps(cu, rl_result, rl_src2);
1119 StoreValueWide(cu, rl_dest, rl_result);
1120 if (cu->instruction_set == kThumb2) {
1121 Clobber(cu, TargetReg(kLr));
1122 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001123 }
buzbee31a4a6f2012-02-28 15:36:15 -08001124}
1125
1126
buzbee02031b12012-11-23 09:41:35 -08001127bool Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1128 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001129{
buzbeefa57c472012-11-21 12:06:18 -08001130 int func_offset;
buzbee31a4a6f2012-02-28 15:36:15 -08001131
buzbee408ad162012-06-06 16:45:18 -07001132 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001133 case Instruction::SHL_LONG:
1134 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001135 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001136 break;
1137 case Instruction::SHR_LONG:
1138 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001139 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001140 break;
1141 case Instruction::USHR_LONG:
1142 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001143 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001144 break;
1145 default:
1146 LOG(FATAL) << "Unexpected case";
1147 return true;
1148 }
buzbeefa57c472012-11-21 12:06:18 -08001149 FlushAllRegs(cu); /* Send everything to home location */
1150 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1151 RegLocation rl_result = GetReturnWide(cu, false);
1152 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001154}
1155
1156
buzbee02031b12012-11-23 09:41:35 -08001157bool Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1158 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001159{
Bill Buzbeea114add2012-05-03 15:00:40 -07001160 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001161 bool is_div_rem = false;
1162 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001163 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001164 RegLocation rl_result;
1165 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001166 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001167 case Instruction::NEG_INT:
1168 op = kOpNeg;
1169 unary = true;
1170 break;
1171 case Instruction::NOT_INT:
1172 op = kOpMvn;
1173 unary = true;
1174 break;
1175 case Instruction::ADD_INT:
1176 case Instruction::ADD_INT_2ADDR:
1177 op = kOpAdd;
1178 break;
1179 case Instruction::SUB_INT:
1180 case Instruction::SUB_INT_2ADDR:
1181 op = kOpSub;
1182 break;
1183 case Instruction::MUL_INT:
1184 case Instruction::MUL_INT_2ADDR:
1185 op = kOpMul;
1186 break;
1187 case Instruction::DIV_INT:
1188 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001189 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001190 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001191 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001192 break;
buzbeef0504cd2012-11-13 16:31:10 -08001193 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001194 case Instruction::REM_INT:
1195 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001196 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001197 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001198 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001199 break;
1200 case Instruction::AND_INT:
1201 case Instruction::AND_INT_2ADDR:
1202 op = kOpAnd;
1203 break;
1204 case Instruction::OR_INT:
1205 case Instruction::OR_INT_2ADDR:
1206 op = kOpOr;
1207 break;
1208 case Instruction::XOR_INT:
1209 case Instruction::XOR_INT_2ADDR:
1210 op = kOpXor;
1211 break;
1212 case Instruction::SHL_INT:
1213 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001214 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001215 op = kOpLsl;
1216 break;
1217 case Instruction::SHR_INT:
1218 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001219 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 op = kOpAsr;
1221 break;
1222 case Instruction::USHR_INT:
1223 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001224 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001225 op = kOpLsr;
1226 break;
1227 default:
buzbeecbd6d442012-11-17 14:11:25 -08001228 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001229 }
buzbeefa57c472012-11-21 12:06:18 -08001230 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001231 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001232 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1233 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1234 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001235 } else {
buzbeefa57c472012-11-21 12:06:18 -08001236 if (shift_op) {
1237 int t_reg = INVALID_REG;
1238 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001239 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001240 t_reg = TargetReg(kCount); // rCX
1241 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001242 } else {
buzbeefa57c472012-11-21 12:06:18 -08001243 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1244 t_reg = AllocTemp(cu);
1245 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001246 }
buzbeefa57c472012-11-21 12:06:18 -08001247 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1248 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1249 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1250 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001251 } else {
buzbeefa57c472012-11-21 12:06:18 -08001252 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1253 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1254 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1255 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001256 }
buzbee31a4a6f2012-02-28 15:36:15 -08001257 }
buzbeefa57c472012-11-21 12:06:18 -08001258 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001259 } else {
buzbeefa57c472012-11-21 12:06:18 -08001260 if (cu->instruction_set == kMips) {
1261 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1262 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1263 if (check_zero) {
1264 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001265 }
buzbeefa57c472012-11-21 12:06:18 -08001266 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001267 } else {
buzbeefa57c472012-11-21 12:06:18 -08001268 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1269 FlushAllRegs(cu); /* Send everything to home location */
1270 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1271 int r_tgt = CallHelperSetup(cu, func_offset);
1272 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1273 if (check_zero) {
1274 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001275 }
1276 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001277 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001278 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001279 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001280 else
buzbeefa57c472012-11-21 12:06:18 -08001281 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001282 }
buzbeefa57c472012-11-21 12:06:18 -08001283 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001284 }
1285 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001286}
1287
1288/*
1289 * The following are the first-level codegen routines that analyze the format
1290 * of each bytecode then either dispatch special purpose codegen routines
1291 * or produce corresponding Thumb instructions directly.
1292 */
1293
buzbeeaad94382012-11-21 07:40:50 -08001294static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001295{
Bill Buzbeea114add2012-05-03 15:00:40 -07001296 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001297}
1298
1299// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001300static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001301{
Bill Buzbeea114add2012-05-03 15:00:40 -07001302 x &= x - 1;
1303 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001304}
1305
1306// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001307static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001308 int bit_posn = 0;
1309 while ((x & 0xf) == 0) {
1310 bit_posn += 4;
1311 x >>= 4;
1312 }
1313 while ((x & 1) == 0) {
1314 bit_posn++;
1315 x >>= 1;
1316 }
1317 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001318}
1319
buzbeefa57c472012-11-21 12:06:18 -08001320// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1321// and store the result in 'rl_dest'.
1322static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1323 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001324{
buzbeefa57c472012-11-21 12:06:18 -08001325 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001326 return false;
buzbee0f79d722012-11-01 15:35:27 -07001327 }
buzbee02031b12012-11-23 09:41:35 -08001328 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001329 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001330 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001331 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001332 }
buzbee52a77fc2012-11-20 19:50:46 -08001333 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001334 if (k >= 30) {
1335 // Avoid special cases.
1336 return false;
1337 }
buzbeefa57c472012-11-21 12:06:18 -08001338 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1339 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001340 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001341 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001342 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001343 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001344 if (lit == 2) {
1345 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001346 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1347 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1348 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001349 } else {
buzbee02031b12012-11-23 09:41:35 -08001350 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1351 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1352 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1353 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001354 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001355 } else {
buzbeefa57c472012-11-21 12:06:18 -08001356 int t_reg1 = AllocTemp(cu);
1357 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001358 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001359 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1360 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1361 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1362 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001363 } else {
buzbee02031b12012-11-23 09:41:35 -08001364 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1365 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1366 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1367 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1368 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001369 }
1370 }
buzbee02031b12012-11-23 09:41:35 -08001371 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001373}
1374
buzbeefa57c472012-11-21 12:06:18 -08001375// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1376// and store the result in 'rl_dest'.
1377static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1378 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001379{
Bill Buzbeea114add2012-05-03 15:00:40 -07001380 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001381 bool power_of_two = false;
1382 bool pop_count_le2 = false;
1383 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001384 if (lit < 2) {
1385 // Avoid special cases.
1386 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001387 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001388 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001389 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001390 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001391 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001392 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001393 } else {
1394 return false;
1395 }
buzbee02031b12012-11-23 09:41:35 -08001396 Codegen* cg = cu->cg.get();
1397 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001398 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1399 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001400 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001401 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001402 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001403 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001404 int first_bit = LowestSetBit(lit);
1405 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001406 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001407 } else {
1408 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001409 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001410 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001411 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001412 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1413 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001414 }
buzbee02031b12012-11-23 09:41:35 -08001415 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001416 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001417}
1418
buzbee02031b12012-11-23 09:41:35 -08001419bool Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
1420 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001421{
buzbeefa57c472012-11-21 12:06:18 -08001422 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001423 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001424 int shift_op = false;
1425 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001426
buzbee408ad162012-06-06 16:45:18 -07001427 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001428 case Instruction::RSUB_INT_LIT8:
1429 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001430 int t_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001431 //TUNING: add support for use of Arm rsub op
buzbeefa57c472012-11-21 12:06:18 -08001432 rl_src = LoadValue(cu, rl_src, kCoreReg);
1433 t_reg = AllocTemp(cu);
1434 LoadConstant(cu, t_reg, lit);
1435 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1436 OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1437 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001438 return false;
1439 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001440 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001441
buzbeee6285f92012-12-06 15:57:46 -08001442 case Instruction::SUB_INT:
1443 case Instruction::SUB_INT_2ADDR:
1444 lit = -lit;
1445 // Intended fallthrough
1446 case Instruction::ADD_INT:
1447 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001448 case Instruction::ADD_INT_LIT8:
1449 case Instruction::ADD_INT_LIT16:
1450 op = kOpAdd;
1451 break;
buzbeee6285f92012-12-06 15:57:46 -08001452 case Instruction::MUL_INT:
1453 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001454 case Instruction::MUL_INT_LIT8:
1455 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001456 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001457 return false;
1458 }
1459 op = kOpMul;
1460 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001461 }
buzbeee6285f92012-12-06 15:57:46 -08001462 case Instruction::AND_INT:
1463 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001464 case Instruction::AND_INT_LIT8:
1465 case Instruction::AND_INT_LIT16:
1466 op = kOpAnd;
1467 break;
buzbeee6285f92012-12-06 15:57:46 -08001468 case Instruction::OR_INT:
1469 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001470 case Instruction::OR_INT_LIT8:
1471 case Instruction::OR_INT_LIT16:
1472 op = kOpOr;
1473 break;
buzbeee6285f92012-12-06 15:57:46 -08001474 case Instruction::XOR_INT:
1475 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001476 case Instruction::XOR_INT_LIT8:
1477 case Instruction::XOR_INT_LIT16:
1478 op = kOpXor;
1479 break;
1480 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001481 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001482 case Instruction::SHL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001483 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001484 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001485 op = kOpLsl;
1486 break;
1487 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001488 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001489 case Instruction::SHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001490 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001491 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001492 op = kOpAsr;
1493 break;
1494 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001495 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001496 case Instruction::USHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001497 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001498 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001499 op = kOpLsr;
1500 break;
1501
buzbeee6285f92012-12-06 15:57:46 -08001502 case Instruction::DIV_INT:
1503 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001504 case Instruction::DIV_INT_LIT8:
1505 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001506 case Instruction::REM_INT:
1507 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001508 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001509 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001511 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001512 return false;
1513 }
buzbeefa57c472012-11-21 12:06:18 -08001514 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001515 return false;
1516 }
buzbee408ad162012-06-06 16:45:18 -07001517 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001518 (opcode == Instruction::DIV_INT) ||
1519 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001520 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001521 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001522 } else {
buzbeefa57c472012-11-21 12:06:18 -08001523 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001524 }
buzbeefa57c472012-11-21 12:06:18 -08001525 if (cu->instruction_set == kMips) {
1526 rl_src = LoadValue(cu, rl_src, kCoreReg);
1527 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001528 } else {
buzbeefa57c472012-11-21 12:06:18 -08001529 FlushAllRegs(cu); /* Everything to home location */
1530 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1531 Clobber(cu, TargetReg(kArg0));
1532 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1533 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1534 if (is_div)
1535 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001536 else
buzbeefa57c472012-11-21 12:06:18 -08001537 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001538 }
buzbeefa57c472012-11-21 12:06:18 -08001539 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001540 return false;
1541 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07001542 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001543 default:
buzbeee6285f92012-12-06 15:57:46 -08001544 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001545 }
buzbeefa57c472012-11-21 12:06:18 -08001546 rl_src = LoadValue(cu, rl_src, kCoreReg);
1547 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001548 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001549 if (shift_op && (lit == 0)) {
1550 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001551 } else {
buzbeefa57c472012-11-21 12:06:18 -08001552 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001553 }
buzbeefa57c472012-11-21 12:06:18 -08001554 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001555 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001556}
1557
buzbee02031b12012-11-23 09:41:35 -08001558bool Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1559 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001560{
buzbeefa57c472012-11-21 12:06:18 -08001561 RegLocation rl_result;
1562 OpKind first_op = kOpBkpt;
1563 OpKind second_op = kOpBkpt;
1564 bool call_out = false;
1565 bool check_zero = false;
1566 int func_offset;
1567 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001568
buzbee408ad162012-06-06 16:45:18 -07001569 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001570 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001571 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1572 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001573 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001574 if (rl_result.low_reg == rl_src2.high_reg) {
1575 int t_reg = AllocTemp(cu);
1576 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1577 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1578 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1579 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001580 } else {
buzbeefa57c472012-11-21 12:06:18 -08001581 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1582 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001583 }
buzbeefa57c472012-11-21 12:06:18 -08001584 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001585 return false;
1586 break;
1587 case Instruction::ADD_LONG:
1588 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001589 if (cu->instruction_set != kThumb2) {
1590 return GenAddLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001591 }
buzbeefa57c472012-11-21 12:06:18 -08001592 first_op = kOpAdd;
1593 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001594 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001595 case Instruction::SUB_LONG:
1596 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001597 if (cu->instruction_set != kThumb2) {
1598 return GenSubLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001599 }
buzbeefa57c472012-11-21 12:06:18 -08001600 first_op = kOpSub;
1601 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001602 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001603 case Instruction::MUL_LONG:
1604 case Instruction::MUL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001605 call_out = true;
1606 ret_reg = TargetReg(kRet0);
1607 func_offset = ENTRYPOINT_OFFSET(pLmul);
Bill Buzbeea114add2012-05-03 15:00:40 -07001608 break;
1609 case Instruction::DIV_LONG:
1610 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001611 call_out = true;
1612 check_zero = true;
1613 ret_reg = TargetReg(kRet0);
1614 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001615 break;
1616 case Instruction::REM_LONG:
1617 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001618 call_out = true;
1619 check_zero = true;
1620 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001621 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001622 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001623 break;
1624 case Instruction::AND_LONG_2ADDR:
1625 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001626 if (cu->instruction_set == kX86) {
1627 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001628 }
buzbeefa57c472012-11-21 12:06:18 -08001629 first_op = kOpAnd;
1630 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001631 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001632 case Instruction::OR_LONG:
1633 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001634 if (cu->instruction_set == kX86) {
1635 return GenOrLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001636 }
buzbeefa57c472012-11-21 12:06:18 -08001637 first_op = kOpOr;
1638 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001640 case Instruction::XOR_LONG:
1641 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001642 if (cu->instruction_set == kX86) {
1643 return GenXorLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001644 }
buzbeefa57c472012-11-21 12:06:18 -08001645 first_op = kOpXor;
1646 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001647 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001648 case Instruction::NEG_LONG: {
buzbeefa57c472012-11-21 12:06:18 -08001649 return GenNegLong(cu, rl_dest, rl_src2);
buzbee31a4a6f2012-02-28 15:36:15 -08001650 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001651 default:
1652 LOG(FATAL) << "Invalid long arith op";
1653 }
buzbeefa57c472012-11-21 12:06:18 -08001654 if (!call_out) {
1655 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001656 } else {
buzbeefa57c472012-11-21 12:06:18 -08001657 FlushAllRegs(cu); /* Send everything to home location */
1658 if (check_zero) {
1659 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1660 int r_tgt = CallHelperSetup(cu, func_offset);
1661 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1662 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001663 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001664 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001665 } else {
buzbeefa57c472012-11-21 12:06:18 -08001666 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1667 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001668 }
buzbeef0504cd2012-11-13 16:31:10 -08001669 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001670 if (ret_reg == TargetReg(kRet0))
1671 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001672 else
buzbeefa57c472012-11-21 12:06:18 -08001673 rl_result = GetReturnWideAlt(cu);
1674 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001675 }
1676 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001677}
1678
buzbee02031b12012-11-23 09:41:35 -08001679bool Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
1680 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001681{
Bill Buzbeea114add2012-05-03 15:00:40 -07001682 /*
1683 * Don't optimize the register usage since it calls out to support
1684 * functions
1685 */
buzbeefa57c472012-11-21 12:06:18 -08001686 FlushAllRegs(cu); /* Send everything to home location */
1687 if (rl_src.wide) {
1688 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1689 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001690 } else {
buzbeefa57c472012-11-21 12:06:18 -08001691 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001692 }
buzbeefa57c472012-11-21 12:06:18 -08001693 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1694 if (rl_dest.wide) {
1695 RegLocation rl_result;
1696 rl_result = GetReturnWide(cu, rl_dest.fp);
1697 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001698 } else {
buzbeefa57c472012-11-21 12:06:18 -08001699 RegLocation rl_result;
1700 rl_result = GetReturn(cu, rl_dest.fp);
1701 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001702 }
1703 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001704}
1705
buzbee31a4a6f2012-02-28 15:36:15 -08001706/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001707void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001708{
buzbeefa57c472012-11-21 12:06:18 -08001709 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001710 return;
1711 }
buzbeefa57c472012-11-21 12:06:18 -08001712 FlushAllRegs(cu);
1713 LIR* branch = OpTestSuspend(cu, NULL);
1714 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1715 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1716 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001717 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001718 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001719}
1720
buzbeefead2932012-03-30 14:02:01 -07001721/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001722void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001723{
buzbeefa57c472012-11-21 12:06:18 -08001724 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1725 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001726 return;
1727 }
buzbeefa57c472012-11-21 12:06:18 -08001728 OpTestSuspend(cu, target);
1729 LIR* launch_pad =
1730 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1731 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1732 FlushAllRegs(cu);
1733 OpUnconditionalBranch(cu, launch_pad);
1734 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001735}
1736
buzbee31a4a6f2012-02-28 15:36:15 -08001737} // namespace art