blob: e3791ce1010e7eede188d6d12c8914b51489f8e5 [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
buzbee1bc37c62012-11-20 13:35:41 -080017#include "codegen_util.h"
buzbee395116c2013-02-27 14:30:25 -080018#include "compiler/dex/compiler_ir.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080019#include "oat/runtime/oat_support_entrypoints.h"
20#include "ralloc_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,
buzbee4ef3e452012-12-14 13:35:28 -080058 cu->current_dalvik_offset, reg, imm_val);
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
buzbee02031b12012-11-23 09:41:35 -080092void Codegen::GenCompareAndBranch(CompilationUnit* cu, Instruction::Code opcode,
93 RegLocation rl_src1, RegLocation rl_src2, LIR* taken,
94 LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -080095{
Bill Buzbeea114add2012-05-03 15:00:40 -070096 ConditionCode cond;
Bill Buzbeea114add2012-05-03 15:00:40 -070097 switch (opcode) {
98 case Instruction::IF_EQ:
99 cond = kCondEq;
100 break;
101 case Instruction::IF_NE:
102 cond = kCondNe;
103 break;
104 case Instruction::IF_LT:
105 cond = kCondLt;
106 break;
107 case Instruction::IF_GE:
108 cond = kCondGe;
109 break;
110 case Instruction::IF_GT:
111 cond = kCondGt;
112 break;
113 case Instruction::IF_LE:
114 cond = kCondLe;
115 break;
116 default:
buzbeecbd6d442012-11-17 14:11:25 -0800117 cond = static_cast<ConditionCode>(0);
118 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 }
buzbeee6285f92012-12-06 15:57:46 -0800120
121 // Normalize such that if either operand is constant, src2 will be constant
122 if (rl_src1.is_const) {
123 RegLocation rl_temp = rl_src1;
124 rl_src1 = rl_src2;
125 rl_src2 = rl_temp;
126 cond = FlipComparisonOrder(cond);
127 }
128
129 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
130 // Is this really an immediate comparison?
131 if (rl_src2.is_const) {
buzbeee6285f92012-12-06 15:57:46 -0800132 // If it's already live in a register or not easily materialized, just keep going
133 RegLocation rl_temp = UpdateLoc(cu, rl_src2);
buzbee4ef3e452012-12-14 13:35:28 -0800134 if ((rl_temp.location == kLocDalvikFrame) &&
135 InexpensiveConstantInt(ConstantValue(cu, rl_src2))) {
buzbeee6285f92012-12-06 15:57:46 -0800136 // OK - convert this to a compare immediate and branch
buzbee4ef3e452012-12-14 13:35:28 -0800137 OpCmpImmBranch(cu, cond, rl_src1.low_reg, ConstantValue(cu, rl_src2), taken);
buzbeee6285f92012-12-06 15:57:46 -0800138 OpUnconditionalBranch(cu, fall_through);
139 return;
140 }
141 }
142 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -0800143 OpCmpBranch(cu, cond, rl_src1.low_reg, rl_src2.low_reg, taken);
144 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800145}
146
buzbee02031b12012-11-23 09:41:35 -0800147void Codegen::GenCompareZeroAndBranch(CompilationUnit* cu, Instruction::Code opcode,
148 RegLocation rl_src, LIR* taken, LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800149{
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 ConditionCode cond;
buzbeefa57c472012-11-21 12:06:18 -0800151 rl_src = LoadValue(cu, rl_src, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 switch (opcode) {
153 case Instruction::IF_EQZ:
154 cond = kCondEq;
155 break;
156 case Instruction::IF_NEZ:
157 cond = kCondNe;
158 break;
159 case Instruction::IF_LTZ:
160 cond = kCondLt;
161 break;
162 case Instruction::IF_GEZ:
163 cond = kCondGe;
164 break;
165 case Instruction::IF_GTZ:
166 cond = kCondGt;
167 break;
168 case Instruction::IF_LEZ:
169 cond = kCondLe;
170 break;
171 default:
buzbeecbd6d442012-11-17 14:11:25 -0800172 cond = static_cast<ConditionCode>(0);
173 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 }
buzbeee6285f92012-12-06 15:57:46 -0800175 OpCmpImmBranch(cu, cond, rl_src.low_reg, 0, taken);
buzbeefa57c472012-11-21 12:06:18 -0800176 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800177}
178
buzbee02031b12012-11-23 09:41:35 -0800179void Codegen::GenIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800180{
buzbeefa57c472012-11-21 12:06:18 -0800181 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
182 if (rl_src.location == kLocPhysReg) {
183 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 } else {
buzbeefa57c472012-11-21 12:06:18 -0800185 LoadValueDirect(cu, rl_src, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 }
buzbeefa57c472012-11-21 12:06:18 -0800187 OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
188 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800189}
190
buzbee02031b12012-11-23 09:41:35 -0800191void Codegen::GenIntNarrowing(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
192 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800193{
buzbeefa57c472012-11-21 12:06:18 -0800194 rl_src = LoadValue(cu, rl_src, kCoreReg);
195 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700197 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 case Instruction::INT_TO_BYTE:
199 op = kOp2Byte;
200 break;
201 case Instruction::INT_TO_SHORT:
202 op = kOp2Short;
203 break;
204 case Instruction::INT_TO_CHAR:
205 op = kOp2Char;
206 break;
207 default:
208 LOG(ERROR) << "Bad int conversion type";
209 }
buzbeefa57c472012-11-21 12:06:18 -0800210 OpRegReg(cu, op, rl_result.low_reg, rl_src.low_reg);
211 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800212}
213
214/*
215 * Let helper function take care of everything. Will call
216 * Array::AllocFromCode(type_idx, method, count);
217 * Note: AllocFromCode will handle checks for errNegativeArraySize.
218 */
buzbee02031b12012-11-23 09:41:35 -0800219void Codegen::GenNewArray(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
220 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800221{
buzbeefa57c472012-11-21 12:06:18 -0800222 FlushAllRegs(cu); /* Everything to home location */
223 int func_offset;
Ian Rogers1212a022013-03-04 10:48:41 -0800224 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
225 *cu->dex_file,
226 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800227 func_offset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 } else {
buzbeefa57c472012-11-21 12:06:18 -0800229 func_offset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 }
buzbeefa57c472012-11-21 12:06:18 -0800231 CallRuntimeHelperImmMethodRegLocation(cu, func_offset, type_idx, rl_src, true);
232 RegLocation rl_result = GetReturn(cu, false);
233 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800234}
235
236/*
buzbee52a77fc2012-11-20 19:50:46 -0800237 * Similar to GenNewArray, but with post-allocation initialization.
buzbee31a4a6f2012-02-28 15:36:15 -0800238 * Verifier guarantees we're dealing with an array class. Current
239 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
240 * Current code also throws internal unimp if not 'L', '[' or 'I'.
241 */
buzbee02031b12012-11-23 09:41:35 -0800242void Codegen::GenFilledNewArray(CompilationUnit* cu, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800243{
buzbeefa57c472012-11-21 12:06:18 -0800244 int elems = info->num_arg_words;
245 int type_idx = info->index;
246 FlushAllRegs(cu); /* Everything to home location */
247 int func_offset;
Ian Rogers1212a022013-03-04 10:48:41 -0800248 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
249 *cu->dex_file,
250 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800251 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 } else {
buzbeefa57c472012-11-21 12:06:18 -0800253 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
buzbeefa57c472012-11-21 12:06:18 -0800255 CallRuntimeHelperImmMethodImm(cu, func_offset, type_idx, elems, true);
256 FreeTemp(cu, TargetReg(kArg2));
257 FreeTemp(cu, TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 /*
259 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
260 * return region. Because AllocFromCode placed the new array
buzbeef0504cd2012-11-13 16:31:10 -0800261 * in kRet0, we'll just lock it into place. When debugger support is
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 * added, it may be necessary to additionally copy all return
263 * values to a home location in thread-local storage
264 */
buzbeefa57c472012-11-21 12:06:18 -0800265 LockTemp(cu, TargetReg(kRet0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700266
267 // TODO: use the correct component size, currently all supported types
268 // share array alignment with ints (see comment at head of function)
269 size_t component_size = sizeof(int32_t);
270
271 // Having a range of 0 is legal
buzbeefa57c472012-11-21 12:06:18 -0800272 if (info->is_range && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800273 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 * Bit of ugliness here. We're going generate a mem copy loop
275 * on the register range, but it is possible that some regs
276 * in the range have been promoted. This is unlikely, but
277 * before generating the copy, we'll just force a flush
278 * of any regs in the source range that have been promoted to
279 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800280 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700281 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800282 RegLocation loc = UpdateLoc(cu, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800284 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
285 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 }
buzbee31a4a6f2012-02-28 15:36:15 -0800287 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 /*
289 * TUNING note: generated code here could be much improved, but
290 * this is an uncommon operation and isn't especially performance
291 * critical.
292 */
buzbeefa57c472012-11-21 12:06:18 -0800293 int r_src = AllocTemp(cu);
294 int r_dst = AllocTemp(cu);
295 int r_idx = AllocTemp(cu);
296 int r_val = INVALID_REG;
297 switch(cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700298 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800299 r_val = TargetReg(kLr);
buzbeeb046e162012-10-30 15:48:42 -0700300 break;
301 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800302 FreeTemp(cu, TargetReg(kRet0));
303 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700304 break;
305 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800306 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700307 break;
buzbeefa57c472012-11-21 12:06:18 -0800308 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700309 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 // Set up source pointer
buzbeefa57c472012-11-21 12:06:18 -0800311 RegLocation rl_first = info->args[0];
312 OpRegRegImm(cu, kOpAdd, r_src, TargetReg(kSp),
313 SRegOffset(cu, rl_first.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 // Set up the target pointer
buzbeefa57c472012-11-21 12:06:18 -0800315 OpRegRegImm(cu, kOpAdd, r_dst, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 mirror::Array::DataOffset(component_size).Int32Value());
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 // Set up the loop counter (known to be > 0)
buzbeefa57c472012-11-21 12:06:18 -0800318 LoadConstant(cu, r_idx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 // Generate the copy loop. Going backwards for convenience
buzbeefa57c472012-11-21 12:06:18 -0800320 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 // Copy next element
buzbeefa57c472012-11-21 12:06:18 -0800322 LoadBaseIndexed(cu, r_src, r_idx, r_val, 2, kWord);
323 StoreBaseIndexed(cu, r_dst, r_idx, r_val, 2, kWord);
324 FreeTemp(cu, r_val);
325 OpDecAndBranch(cu, kCondGe, r_idx, target);
326 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700327 // Restore the target pointer
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 OpRegRegImm(cu, kOpAdd, TargetReg(kRet0), r_dst,
329 -mirror::Array::DataOffset(component_size).Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700330 }
buzbeefa57c472012-11-21 12:06:18 -0800331 } else if (!info->is_range) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700333 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800334 RegLocation rl_arg = LoadValue(cu, info->args[i], kCoreReg);
335 StoreBaseDisp(cu, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 mirror::Array::DataOffset(component_size).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800337 i * 4, rl_arg.low_reg, kWord);
buzbee52a77fc2012-11-20 19:50:46 -0800338 // If the LoadValue caused a temp to be allocated, free it
buzbeefa57c472012-11-21 12:06:18 -0800339 if (IsTemp(cu, rl_arg.low_reg)) {
340 FreeTemp(cu, rl_arg.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 }
342 }
343 }
buzbeee5f01222012-06-14 15:19:35 -0700344 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800345 StoreValue(cu, info->result, GetReturn(cu, false /* not fp */));
buzbeee5f01222012-06-14 15:19:35 -0700346 }
buzbee31a4a6f2012-02-28 15:36:15 -0800347}
348
buzbee02031b12012-11-23 09:41:35 -0800349void Codegen::GenSput(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_src,
350 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800351{
buzbeefa57c472012-11-21 12:06:18 -0800352 int field_offset;
353 int ssb_index;
354 bool is_volatile;
355 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800356
TDYa127dc5daa02013-01-09 21:31:37 +0800357 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file, cu->code_item,
358 cu->class_def_idx, cu->method_idx, cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800359
buzbeefa57c472012-11-21 12:06:18 -0800360 bool fast_path =
Ian Rogers1212a022013-03-04 10:48:41 -0800361 cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
362 field_offset, ssb_index,
363 is_referrers_class, is_volatile,
364 true);
buzbeefa57c472012-11-21 12:06:18 -0800365 if (fast_path && !SLOW_FIELD_PATH) {
366 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800368 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800370 RegLocation rl_method = LoadCurrMethod(cu);
371 rBase = AllocTemp(cu);
372 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbeefa57c472012-11-21 12:06:18 -0800374 if (IsTemp(cu, rl_method.low_reg)) {
375 FreeTemp(cu, rl_method.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 }
buzbee31a4a6f2012-02-28 15:36:15 -0800377 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 // Medium path, static storage base in a different class which
379 // requires checks that the other class is initialized.
buzbeefa57c472012-11-21 12:06:18 -0800380 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800382 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 // Using fixed register to sync with possible call to runtime
384 // support.
buzbeefa57c472012-11-21 12:06:18 -0800385 int r_method = TargetReg(kArg1);
386 LockTemp(cu, r_method);
387 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800388 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800389 LockTemp(cu, rBase);
390 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800393 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800395 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 // rBase now points at appropriate static storage base (Class*)
397 // or NULL if not initialized. Check for NULL and call helper if NULL.
398 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800399 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
400 LoadConstant(cu, TargetReg(kArg0), ssb_index);
401 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
402 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800403 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800404 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700405 }
buzbeefa57c472012-11-21 12:06:18 -0800406 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
407 branch_over->target = skip_target;
408 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800409 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800411 if (is_long_or_double) {
412 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 } else {
buzbeefa57c472012-11-21 12:06:18 -0800414 rl_src = LoadValue(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 }
buzbeefa57c472012-11-21 12:06:18 -0800416 if (is_volatile) {
417 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 }
buzbeefa57c472012-11-21 12:06:18 -0800419 if (is_long_or_double) {
420 StoreBaseDispWide(cu, rBase, field_offset, rl_src.low_reg,
421 rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 } else {
buzbeefa57c472012-11-21 12:06:18 -0800423 StoreWordDisp(cu, rBase, field_offset, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 }
buzbeefa57c472012-11-21 12:06:18 -0800425 if (is_volatile) {
426 GenMemBarrier(cu, kStoreLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 }
buzbee6a791b22013-02-07 05:35:08 -0800428 if (is_object && !IsConstantNullRef(cu, rl_src)) {
buzbeefa57c472012-11-21 12:06:18 -0800429 MarkGCCard(cu, rl_src.low_reg, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 }
buzbeefa57c472012-11-21 12:06:18 -0800431 FreeTemp(cu, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 } else {
buzbeefa57c472012-11-21 12:06:18 -0800433 FlushAllRegs(cu); // Everything to home locations
434 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Static) :
435 (is_object ? ENTRYPOINT_OFFSET(pSetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 : ENTRYPOINT_OFFSET(pSet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800437 CallRuntimeHelperImmRegLocation(cu, setter_offset, field_idx, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 }
buzbee31a4a6f2012-02-28 15:36:15 -0800439}
440
buzbee02031b12012-11-23 09:41:35 -0800441void Codegen::GenSget(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_dest,
442 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800443{
buzbeefa57c472012-11-21 12:06:18 -0800444 int field_offset;
445 int ssb_index;
446 bool is_volatile;
447 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800448
buzbeefa57c472012-11-21 12:06:18 -0800449 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +0800450 *cu->dex_file, cu->code_item,
451 cu->class_def_idx, cu->method_idx,
452 cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800453
buzbeefa57c472012-11-21 12:06:18 -0800454 bool fast_path =
Ian Rogers1212a022013-03-04 10:48:41 -0800455 cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
456 field_offset, ssb_index,
457 is_referrers_class, is_volatile,
458 false);
buzbeefa57c472012-11-21 12:06:18 -0800459 if (fast_path && !SLOW_FIELD_PATH) {
460 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800462 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800464 RegLocation rl_method = LoadCurrMethod(cu);
465 rBase = AllocTemp(cu);
466 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800468 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 // Medium path, static storage base in a different class which
470 // requires checks that the other class is initialized
buzbeefa57c472012-11-21 12:06:18 -0800471 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800473 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 // Using fixed register to sync with possible call to runtime
475 // support
buzbeefa57c472012-11-21 12:06:18 -0800476 int r_method = TargetReg(kArg1);
477 LockTemp(cu, r_method);
478 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800479 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800480 LockTemp(cu, rBase);
481 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800484 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800486 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 // rBase now points at appropriate static storage base (Class*)
488 // or NULL if not initialized. Check for NULL and call helper if NULL.
489 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800490 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
491 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
492 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800493 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800494 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700495 }
buzbeefa57c472012-11-21 12:06:18 -0800496 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
497 branch_over->target = skip_target;
498 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800499 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800501 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
502 if (is_volatile) {
503 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 }
buzbeefa57c472012-11-21 12:06:18 -0800505 if (is_long_or_double) {
506 LoadBaseDispWide(cu, rBase, field_offset, rl_result.low_reg,
507 rl_result.high_reg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 } else {
buzbeefa57c472012-11-21 12:06:18 -0800509 LoadWordDisp(cu, rBase, field_offset, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 }
buzbeefa57c472012-11-21 12:06:18 -0800511 FreeTemp(cu, rBase);
512 if (is_long_or_double) {
513 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 } else {
buzbeefa57c472012-11-21 12:06:18 -0800515 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 }
517 } else {
buzbeefa57c472012-11-21 12:06:18 -0800518 FlushAllRegs(cu); // Everything to home locations
519 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Static) :
520 (is_object ? ENTRYPOINT_OFFSET(pGetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 : ENTRYPOINT_OFFSET(pGet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800522 CallRuntimeHelperImm(cu, getterOffset, field_idx, true);
523 if (is_long_or_double) {
524 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
525 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 } else {
buzbeefa57c472012-11-21 12:06:18 -0800527 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
528 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 }
530 }
buzbee31a4a6f2012-02-28 15:36:15 -0800531}
532
533
534// Debugging routine - if null target, branch to DebugMe
buzbee02031b12012-11-23 09:41:35 -0800535void Codegen::GenShowTarget(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800536{
buzbeefa57c472012-11-21 12:06:18 -0800537 DCHECK_NE(cu->instruction_set, kX86) << "unimplemented GenShowTarget";
538 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, TargetReg(kInvokeTgt), 0, NULL);
539 LoadWordDisp(cu, TargetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), TargetReg(kInvokeTgt));
540 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
541 branch_over->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800542}
543
buzbee02031b12012-11-23 09:41:35 -0800544void Codegen::HandleSuspendLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800545{
buzbeefa57c472012-11-21 12:06:18 -0800546 LIR** suspend_label = reinterpret_cast<LIR**>(cu->suspend_launchpads.elem_list);
547 int num_elems = cu->suspend_launchpads.num_used;
548 int helper_offset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
549 for (int i = 0; i < num_elems; i++) {
550 ResetRegPool(cu);
551 ResetDefTracking(cu);
552 LIR* lab = suspend_label[i];
553 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[0]);
554 cu->current_dalvik_offset = lab->operands[1];
555 AppendLIR(cu, lab);
556 int r_tgt = CallHelperSetup(cu, helper_offset);
557 CallHelper(cu, r_tgt, helper_offset, true /* MarkSafepointPC */);
558 OpUnconditionalBranch(cu, resume_lab);
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 }
buzbee31a4a6f2012-02-28 15:36:15 -0800560}
561
buzbee02031b12012-11-23 09:41:35 -0800562void Codegen::HandleIntrinsicLaunchPads(CompilationUnit *cu)
buzbeefc9e6fa2012-03-23 15:14:29 -0700563{
buzbeefa57c472012-11-21 12:06:18 -0800564 LIR** intrinsic_label = reinterpret_cast<LIR**>(cu->intrinsic_launchpads.elem_list);
565 int num_elems = cu->intrinsic_launchpads.num_used;
566 for (int i = 0; i < num_elems; i++) {
567 ResetRegPool(cu);
568 ResetDefTracking(cu);
569 LIR* lab = intrinsic_label[i];
buzbeecbd6d442012-11-17 14:11:25 -0800570 CallInfo* info = reinterpret_cast<CallInfo*>(lab->operands[0]);
buzbeefa57c472012-11-21 12:06:18 -0800571 cu->current_dalvik_offset = info->offset;
572 AppendLIR(cu, lab);
buzbee52a77fc2012-11-20 19:50:46 -0800573 // NOTE: GenInvoke handles MarkSafepointPC
buzbeefa57c472012-11-21 12:06:18 -0800574 GenInvoke(cu, info);
575 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[2]);
576 if (resume_lab != NULL) {
577 OpUnconditionalBranch(cu, resume_lab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700578 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700580}
581
buzbee02031b12012-11-23 09:41:35 -0800582void Codegen::HandleThrowLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800583{
buzbeefa57c472012-11-21 12:06:18 -0800584 LIR** throw_label = reinterpret_cast<LIR**>(cu->throw_launchpads.elem_list);
585 int num_elems = cu->throw_launchpads.num_used;
586 for (int i = 0; i < num_elems; i++) {
587 ResetRegPool(cu);
588 ResetDefTracking(cu);
589 LIR* lab = throw_label[i];
590 cu->current_dalvik_offset = lab->operands[1];
591 AppendLIR(cu, lab);
592 int func_offset = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700593 int v1 = lab->operands[2];
594 int v2 = lab->operands[3];
buzbeefa57c472012-11-21 12:06:18 -0800595 bool target_x86 = (cu->instruction_set == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 switch (lab->operands[0]) {
597 case kThrowNullPointer:
buzbeefa57c472012-11-21 12:06:18 -0800598 func_offset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 break;
buzbee4ef3e452012-12-14 13:35:28 -0800600 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
601 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
602 if (target_x86) {
603 OpRegMem(cu, kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
604 } else {
605 OpRegCopy(cu, TargetReg(kArg1), v1);
606 }
607 // Make sure the following LoadConstant doesn't mess with kArg1.
608 LockTemp(cu, TargetReg(kArg1));
609 LoadConstant(cu, TargetReg(kArg0), v2);
610 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
611 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 case kThrowArrayBounds:
buzbeef0504cd2012-11-13 16:31:10 -0800613 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee52a77fc2012-11-20 19:50:46 -0800614 if (v2 != TargetReg(kArg0)) {
buzbeefa57c472012-11-21 12:06:18 -0800615 OpRegCopy(cu, TargetReg(kArg0), v1);
616 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700617 // x86 leaves the array pointer in v2, so load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700619 } else {
buzbeefa57c472012-11-21 12:06:18 -0800620 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700621 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800623 if (v1 == TargetReg(kArg1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800624 // Swap v1 and v2, using kArg2 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800625 OpRegCopy(cu, TargetReg(kArg2), v1);
626 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700627 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700629 } else {
buzbeefa57c472012-11-21 12:06:18 -0800630 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700631 }
buzbeefa57c472012-11-21 12:06:18 -0800632 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 } else {
buzbeefa57c472012-11-21 12:06:18 -0800634 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700635 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700637 } else {
buzbeefa57c472012-11-21 12:06:18 -0800638 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700639 }
buzbeefa57c472012-11-21 12:06:18 -0800640 OpRegCopy(cu, TargetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 }
buzbee31a4a6f2012-02-28 15:36:15 -0800642 }
buzbeefa57c472012-11-21 12:06:18 -0800643 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 break;
645 case kThrowDivZero:
buzbeefa57c472012-11-21 12:06:18 -0800646 func_offset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 case kThrowNoSuchMethod:
buzbeefa57c472012-11-21 12:06:18 -0800649 OpRegCopy(cu, TargetReg(kArg0), v1);
650 func_offset =
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
652 break;
653 case kThrowStackOverflow:
buzbeefa57c472012-11-21 12:06:18 -0800654 func_offset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 // Restore stack alignment
buzbeefa57c472012-11-21 12:06:18 -0800656 if (target_x86) {
657 OpRegImm(cu, kOpAdd, TargetReg(kSp), cu->frame_size);
buzbeeb046e162012-10-30 15:48:42 -0700658 } else {
buzbeefa57c472012-11-21 12:06:18 -0800659 OpRegImm(cu, kOpAdd, TargetReg(kSp), (cu->num_core_spills + cu->num_fp_spills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700660 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700661 break;
662 default:
663 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800664 }
buzbeefa57c472012-11-21 12:06:18 -0800665 ClobberCalleeSave(cu);
666 int r_tgt = CallHelperSetup(cu, func_offset);
667 CallHelper(cu, r_tgt, func_offset, true /* MarkSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 }
buzbee31a4a6f2012-02-28 15:36:15 -0800669}
670
buzbee02031b12012-11-23 09:41:35 -0800671void Codegen::GenIGet(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
672 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
673 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800674{
buzbeefa57c472012-11-21 12:06:18 -0800675 int field_offset;
676 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800677
buzbeefa57c472012-11-21 12:06:18 -0800678 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800679
buzbeefa57c472012-11-21 12:06:18 -0800680 if (fast_path && !SLOW_FIELD_PATH) {
681 RegLocation rl_result;
682 RegisterClass reg_class = oat_reg_class_by_size(size);
683 DCHECK_GE(field_offset, 0);
684 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
685 if (is_long_or_double) {
686 DCHECK(rl_dest.wide);
687 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
688 if (cu->instruction_set == kX86) {
689 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
690 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
691 LoadBaseDispWide(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
692 rl_result.high_reg, rl_obj.s_reg_low);
693 if (is_volatile) {
694 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700695 }
696 } else {
buzbeefa57c472012-11-21 12:06:18 -0800697 int reg_ptr = AllocTemp(cu);
698 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
699 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
buzbeee6285f92012-12-06 15:57:46 -0800700 LoadBaseDispWide(cu, reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
buzbeefa57c472012-11-21 12:06:18 -0800701 if (is_volatile) {
702 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700703 }
buzbeefa57c472012-11-21 12:06:18 -0800704 FreeTemp(cu, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
buzbeefa57c472012-11-21 12:06:18 -0800706 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800707 } else {
buzbeefa57c472012-11-21 12:06:18 -0800708 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
709 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
710 LoadBaseDisp(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
711 kWord, rl_obj.s_reg_low);
712 if (is_volatile) {
713 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 }
buzbeefa57c472012-11-21 12:06:18 -0800715 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800716 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700717 } else {
buzbeefa57c472012-11-21 12:06:18 -0800718 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Instance) :
719 (is_object ? ENTRYPOINT_OFFSET(pGetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700720 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800721 CallRuntimeHelperImmRegLocation(cu, getterOffset, field_idx, rl_obj, true);
722 if (is_long_or_double) {
723 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
724 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 } else {
buzbeefa57c472012-11-21 12:06:18 -0800726 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
727 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 }
729 }
buzbee31a4a6f2012-02-28 15:36:15 -0800730}
731
buzbee02031b12012-11-23 09:41:35 -0800732void Codegen::GenIPut(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
733 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
734 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800735{
buzbeefa57c472012-11-21 12:06:18 -0800736 int field_offset;
737 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800738
buzbeefa57c472012-11-21 12:06:18 -0800739 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700740 true);
buzbeefa57c472012-11-21 12:06:18 -0800741 if (fast_path && !SLOW_FIELD_PATH) {
742 RegisterClass reg_class = oat_reg_class_by_size(size);
743 DCHECK_GE(field_offset, 0);
744 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
745 if (is_long_or_double) {
746 int reg_ptr;
747 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
748 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
749 reg_ptr = AllocTemp(cu);
750 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
751 if (is_volatile) {
752 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 }
buzbeefa57c472012-11-21 12:06:18 -0800754 StoreBaseDispWide(cu, reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
755 if (is_volatile) {
756 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 }
buzbeefa57c472012-11-21 12:06:18 -0800758 FreeTemp(cu, reg_ptr);
buzbee31a4a6f2012-02-28 15:36:15 -0800759 } else {
buzbeefa57c472012-11-21 12:06:18 -0800760 rl_src = LoadValue(cu, rl_src, reg_class);
761 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
762 if (is_volatile) {
763 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700764 }
buzbeefa57c472012-11-21 12:06:18 -0800765 StoreBaseDisp(cu, rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
766 if (is_volatile) {
767 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 }
buzbee6a791b22013-02-07 05:35:08 -0800769 if (is_object && !IsConstantNullRef(cu, rl_src)) {
buzbeefa57c472012-11-21 12:06:18 -0800770 MarkGCCard(cu, rl_src.low_reg, rl_obj.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700771 }
buzbee31a4a6f2012-02-28 15:36:15 -0800772 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 } else {
buzbeefa57c472012-11-21 12:06:18 -0800774 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Instance) :
775 (is_object ? ENTRYPOINT_OFFSET(pSetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700776 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800777 CallRuntimeHelperImmRegLocationRegLocation(cu, setter_offset, field_idx, rl_obj, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 }
buzbee31a4a6f2012-02-28 15:36:15 -0800779}
780
buzbee02031b12012-11-23 09:41:35 -0800781void Codegen::GenConstClass(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800782{
buzbeefa57c472012-11-21 12:06:18 -0800783 RegLocation rl_method = LoadCurrMethod(cu);
784 int res_reg = AllocTemp(cu);
785 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Ian Rogers1212a022013-03-04 10:48:41 -0800786 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -0800787 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700788 type_idx)) {
789 // Call out to helper which resolves type and verifies access.
buzbeef0504cd2012-11-13 16:31:10 -0800790 // Resolved type returned in kRet0.
buzbeefa57c472012-11-21 12:06:18 -0800791 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
792 type_idx, rl_method.low_reg, true);
793 RegLocation rl_result = GetReturn(cu, false);
794 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 } else {
796 // We're don't need access checks, load type from dex cache
797 int32_t dex_cache_offset =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800798 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeefa57c472012-11-21 12:06:18 -0800799 LoadWordDisp(cu, rl_method.low_reg, dex_cache_offset, res_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800801 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700802 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800803 LoadWordDisp(cu, res_reg, offset_of_type, rl_result.low_reg);
Ian Rogers1212a022013-03-04 10:48:41 -0800804 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 type_idx) || SLOW_TYPE_PATH) {
806 // Slow path, at runtime test if type is null and if so initialize
buzbeefa57c472012-11-21 12:06:18 -0800807 FlushAllRegs(cu);
808 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, rl_result.low_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 // Resolved, store and hop over following code
buzbeefa57c472012-11-21 12:06:18 -0800810 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700811 /*
812 * Because we have stores of the target value on two paths,
813 * clobber temp tracking for the destination using the ssa name
814 */
buzbeefa57c472012-11-21 12:06:18 -0800815 ClobberSReg(cu, rl_dest.s_reg_low);
816 LIR* branch2 = OpUnconditionalBranch(cu,0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700817 // TUNING: move slow path to end & remove unconditional branch
buzbeefa57c472012-11-21 12:06:18 -0800818 LIR* target1 = NewLIR0(cu, kPseudoTargetLabel);
buzbeef0504cd2012-11-13 16:31:10 -0800819 // Call out to helper, which will return resolved type in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800820 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
821 rl_method.low_reg, true);
822 RegLocation rl_result = GetReturn(cu, false);
823 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700824 /*
825 * Because we have stores of the target value on two paths,
826 * clobber temp tracking for the destination using the ssa name
827 */
buzbeefa57c472012-11-21 12:06:18 -0800828 ClobberSReg(cu, rl_dest.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800830 LIR* target2 = NewLIR0(cu, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800831 branch1->target = target1;
832 branch2->target = target2;
buzbee31a4a6f2012-02-28 15:36:15 -0800833 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 // Fast path, we're done - just store result
buzbeefa57c472012-11-21 12:06:18 -0800835 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800836 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 }
buzbee31a4a6f2012-02-28 15:36:15 -0800838}
Ian Rogersab2b55d2012-03-18 00:06:11 -0700839
buzbee02031b12012-11-23 09:41:35 -0800840void Codegen::GenConstString(CompilationUnit* cu, uint32_t string_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800841{
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 /* NOTE: Most strings should be available at compile time */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
844 (sizeof(mirror::String*) * string_idx);
Ian Rogers1212a022013-03-04 10:48:41 -0800845 if (!cu->compiler_driver->CanAssumeStringIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -0800846 *cu->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 // slow path, resolve string if not in dex cache
buzbeefa57c472012-11-21 12:06:18 -0800848 FlushAllRegs(cu);
849 LockCallTemps(cu); // Using explicit registers
850 LoadCurrMethodDirect(cu, TargetReg(kArg2));
851 LoadWordDisp(cu, TargetReg(kArg2),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800852 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800853 // Might call out to helper, which will return resolved string in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800854 int r_tgt = CallHelperSetup(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode));
855 LoadWordDisp(cu, TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
856 LoadConstant(cu, TargetReg(kArg1), string_idx);
857 if (cu->instruction_set == kThumb2) {
858 OpRegImm(cu, kOpCmp, TargetReg(kRet0), 0); // Is resolved?
859 GenBarrier(cu);
buzbeeb046e162012-10-30 15:48:42 -0700860 // For testing, always force through helper
861 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee02031b12012-11-23 09:41:35 -0800862 OpIT(cu, kCondEq, "T");
buzbeeb046e162012-10-30 15:48:42 -0700863 }
buzbeefa57c472012-11-21 12:06:18 -0800864 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
865 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
866 MarkSafepointPC(cu, call_inst);
867 FreeTemp(cu, r_tgt);
868 } else if (cu->instruction_set == kMips) {
869 LIR* branch = OpCmpImmBranch(cu, kCondNe, TargetReg(kRet0), 0, NULL);
870 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
871 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
872 MarkSafepointPC(cu, call_inst);
873 FreeTemp(cu, r_tgt);
874 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeb046e162012-10-30 15:48:42 -0700875 branch->target = target;
876 } else {
buzbeefa57c472012-11-21 12:06:18 -0800877 DCHECK_EQ(cu->instruction_set, kX86);
878 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode), TargetReg(kArg2), TargetReg(kArg1), true);
buzbee31a4a6f2012-02-28 15:36:15 -0800879 }
buzbeefa57c472012-11-21 12:06:18 -0800880 GenBarrier(cu);
881 StoreValue(cu, rl_dest, GetReturn(cu, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 } else {
buzbeefa57c472012-11-21 12:06:18 -0800883 RegLocation rl_method = LoadCurrMethod(cu);
884 int res_reg = AllocTemp(cu);
885 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
886 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeefa57c472012-11-21 12:06:18 -0800888 LoadWordDisp(cu, res_reg, offset_of_string, rl_result.low_reg);
889 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 }
buzbee31a4a6f2012-02-28 15:36:15 -0800891}
892
893/*
894 * Let helper function take care of everything. Will
895 * call Class::NewInstanceFromCode(type_idx, method);
896 */
buzbee02031b12012-11-23 09:41:35 -0800897void Codegen::GenNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800898{
buzbeefa57c472012-11-21 12:06:18 -0800899 FlushAllRegs(cu); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 // alloc will always check for resolution, do we also need to verify
901 // access because the verifier was unable to?
buzbeefa57c472012-11-21 12:06:18 -0800902 int func_offset;
Ian Rogers1212a022013-03-04 10:48:41 -0800903 if (cu->compiler_driver->CanAccessInstantiableTypeWithoutChecks(
buzbeefa57c472012-11-21 12:06:18 -0800904 cu->method_idx, *cu->dex_file, type_idx)) {
905 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 } else {
buzbeefa57c472012-11-21 12:06:18 -0800907 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
buzbeefa57c472012-11-21 12:06:18 -0800909 CallRuntimeHelperImmMethod(cu, func_offset, type_idx, true);
910 RegLocation rl_result = GetReturn(cu, false);
911 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800912}
913
buzbee02031b12012-11-23 09:41:35 -0800914void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700915{
buzbeefa57c472012-11-21 12:06:18 -0800916 FlushAllRegs(cu);
917 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700918}
919
buzbee02031b12012-11-23 09:41:35 -0800920void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
921 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800922{
buzbeefa57c472012-11-21 12:06:18 -0800923 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800925 LockCallTemps(cu);
926 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
927 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Ian Rogers1212a022013-03-04 10:48:41 -0800928 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -0800929 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 type_idx)) {
931 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800932 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800933 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700934 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800935 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
936 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700937 } else {
buzbeefa57c472012-11-21 12:06:18 -0800938 // Load dex cache entry into class_reg (kArg2)
939 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
940 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800943 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800945 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
Ian Rogers1212a022013-03-04 10:48:41 -0800946 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -0800947 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800949 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800951 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800952 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
953 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
954 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800956 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
957 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800958 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 }
buzbeef0504cd2012-11-13 16:31:10 -0800960 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800961 RegLocation rl_result = GetReturn(cu, false);
962 if (cu->instruction_set == kMips) {
963 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700964 }
buzbeefa57c472012-11-21 12:06:18 -0800965 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800967 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
968 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800969 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800970 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700971 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800972 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700973 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800974 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
975 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800976 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800977 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
978 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
979 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
980 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700981 } else {
982 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -0800983 LoadConstant(cu, rl_result.low_reg, 1); // assume true
984 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
985 if (cu->instruction_set != kX86) {
986 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
987 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
988 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
989 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700990 } else {
buzbeefa57c472012-11-21 12:06:18 -0800991 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
992 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -0700993 }
994 }
buzbeefa57c472012-11-21 12:06:18 -0800995 MarkSafepointPC(cu, call_inst);
996 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700997 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -0800998 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
999 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001000 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001001 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001002 branchover->target = target;
1003 }
buzbee31a4a6f2012-02-28 15:36:15 -08001004}
1005
buzbee02031b12012-11-23 09:41:35 -08001006void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001007{
buzbeefa57c472012-11-21 12:06:18 -08001008 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001009 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001010 LockCallTemps(cu);
1011 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1012 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Ian Rogers1212a022013-03-04 10:48:41 -08001013 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -08001014 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001015 type_idx)) {
1016 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001017 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001019 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001020 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001021 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 } else {
buzbeefa57c472012-11-21 12:06:18 -08001023 // Load dex cache entry into class_reg (kArg2)
1024 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001025 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001027 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1028 (sizeof(mirror::Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001029 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
Ian Rogers1212a022013-03-04 10:48:41 -08001030 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -08001031 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001033 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001034 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001035 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001037 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001038 true);
buzbeefa57c472012-11-21 12:06:18 -08001039 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001041 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1042 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001043 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 }
buzbeefa57c472012-11-21 12:06:18 -08001045 // At this point, class_reg (kArg2) has class
1046 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001048 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001049 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001050 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1051 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001052 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001053 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001054 if (cu->instruction_set == kThumb2) {
1055 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1056 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1057 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1058 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1059 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1060 ClobberCalleeSave(cu);
1061 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1062 MarkSafepointPC(cu, call_inst);
1063 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001064 } else {
buzbeefa57c472012-11-21 12:06:18 -08001065 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1066 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001067 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001069 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 branch1->target = target;
1071 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001072}
1073
buzbee02031b12012-11-23 09:41:35 -08001074void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1075 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001076{
buzbeefa57c472012-11-21 12:06:18 -08001077 RegLocation rl_result;
1078 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001079 /*
1080 * NOTE: This is the one place in the code in which we might have
1081 * as many as six live temporary registers. There are 5 in the normal
1082 * set for Arm. Until we have spill capabilities, temporarily add
1083 * lr to the temp set. It is safe to do this locally, but note that
1084 * lr is used explicitly elsewhere in the code generator and cannot
1085 * normally be used as a general temp register.
1086 */
buzbeefa57c472012-11-21 12:06:18 -08001087 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1088 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001089 }
buzbeefa57c472012-11-21 12:06:18 -08001090 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1091 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1092 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001093 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001094 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1095 int t_reg = AllocTemp(cu);
1096 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1097 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1098 OpRegCopy(cu, rl_result.low_reg, t_reg);
1099 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 } else {
buzbeefa57c472012-11-21 12:06:18 -08001101 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1102 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1103 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 }
1105 /*
buzbeefa57c472012-11-21 12:06:18 -08001106 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001107 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001109 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001110 * Remove when spill is functional.
1111 */
buzbeefa57c472012-11-21 12:06:18 -08001112 FreeRegLocTemps(cu, rl_result, rl_src1);
1113 FreeRegLocTemps(cu, rl_result, rl_src2);
1114 StoreValueWide(cu, rl_dest, rl_result);
1115 if (cu->instruction_set == kThumb2) {
1116 Clobber(cu, TargetReg(kLr));
1117 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001118 }
buzbee31a4a6f2012-02-28 15:36:15 -08001119}
1120
1121
buzbeea5954be2013-02-07 10:41:40 -08001122void Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001123 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001124{
buzbeea5954be2013-02-07 10:41:40 -08001125 int func_offset = -1; // Make gcc happy
buzbee31a4a6f2012-02-28 15:36:15 -08001126
buzbee408ad162012-06-06 16:45:18 -07001127 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001128 case Instruction::SHL_LONG:
1129 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001130 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001131 break;
1132 case Instruction::SHR_LONG:
1133 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001134 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001135 break;
1136 case Instruction::USHR_LONG:
1137 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001138 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 break;
1140 default:
1141 LOG(FATAL) << "Unexpected case";
Bill Buzbeea114add2012-05-03 15:00:40 -07001142 }
buzbeefa57c472012-11-21 12:06:18 -08001143 FlushAllRegs(cu); /* Send everything to home location */
1144 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1145 RegLocation rl_result = GetReturnWide(cu, false);
1146 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -08001147}
1148
1149
buzbeea5954be2013-02-07 10:41:40 -08001150void Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001151 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001152{
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001154 bool is_div_rem = false;
1155 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001156 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001157 RegLocation rl_result;
1158 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001159 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001160 case Instruction::NEG_INT:
1161 op = kOpNeg;
1162 unary = true;
1163 break;
1164 case Instruction::NOT_INT:
1165 op = kOpMvn;
1166 unary = true;
1167 break;
1168 case Instruction::ADD_INT:
1169 case Instruction::ADD_INT_2ADDR:
1170 op = kOpAdd;
1171 break;
1172 case Instruction::SUB_INT:
1173 case Instruction::SUB_INT_2ADDR:
1174 op = kOpSub;
1175 break;
1176 case Instruction::MUL_INT:
1177 case Instruction::MUL_INT_2ADDR:
1178 op = kOpMul;
1179 break;
1180 case Instruction::DIV_INT:
1181 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001182 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001184 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001185 break;
buzbeef0504cd2012-11-13 16:31:10 -08001186 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001187 case Instruction::REM_INT:
1188 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001189 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001190 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001191 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001192 break;
1193 case Instruction::AND_INT:
1194 case Instruction::AND_INT_2ADDR:
1195 op = kOpAnd;
1196 break;
1197 case Instruction::OR_INT:
1198 case Instruction::OR_INT_2ADDR:
1199 op = kOpOr;
1200 break;
1201 case Instruction::XOR_INT:
1202 case Instruction::XOR_INT_2ADDR:
1203 op = kOpXor;
1204 break;
1205 case Instruction::SHL_INT:
1206 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001207 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001208 op = kOpLsl;
1209 break;
1210 case Instruction::SHR_INT:
1211 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001212 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001213 op = kOpAsr;
1214 break;
1215 case Instruction::USHR_INT:
1216 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001217 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001218 op = kOpLsr;
1219 break;
1220 default:
buzbeecbd6d442012-11-17 14:11:25 -08001221 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001222 }
buzbeefa57c472012-11-21 12:06:18 -08001223 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001225 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1226 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1227 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001228 } else {
buzbeefa57c472012-11-21 12:06:18 -08001229 if (shift_op) {
1230 int t_reg = INVALID_REG;
1231 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001232 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001233 t_reg = TargetReg(kCount); // rCX
1234 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001235 } else {
buzbeefa57c472012-11-21 12:06:18 -08001236 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1237 t_reg = AllocTemp(cu);
1238 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001239 }
buzbeefa57c472012-11-21 12:06:18 -08001240 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1241 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1242 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1243 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001244 } else {
buzbeefa57c472012-11-21 12:06:18 -08001245 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1246 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1247 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1248 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001249 }
buzbee31a4a6f2012-02-28 15:36:15 -08001250 }
buzbeefa57c472012-11-21 12:06:18 -08001251 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001252 } else {
buzbeefa57c472012-11-21 12:06:18 -08001253 if (cu->instruction_set == kMips) {
1254 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1255 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1256 if (check_zero) {
1257 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001258 }
buzbeefa57c472012-11-21 12:06:18 -08001259 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001260 } else {
buzbeefa57c472012-11-21 12:06:18 -08001261 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1262 FlushAllRegs(cu); /* Send everything to home location */
1263 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1264 int r_tgt = CallHelperSetup(cu, func_offset);
1265 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1266 if (check_zero) {
1267 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001268 }
1269 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001270 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001271 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001272 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001273 else
buzbeefa57c472012-11-21 12:06:18 -08001274 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001275 }
buzbeefa57c472012-11-21 12:06:18 -08001276 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001277 }
buzbee31a4a6f2012-02-28 15:36:15 -08001278}
1279
1280/*
1281 * The following are the first-level codegen routines that analyze the format
1282 * of each bytecode then either dispatch special purpose codegen routines
1283 * or produce corresponding Thumb instructions directly.
1284 */
1285
buzbeeaad94382012-11-21 07:40:50 -08001286static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001287{
Bill Buzbeea114add2012-05-03 15:00:40 -07001288 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001289}
1290
1291// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001292static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001293{
Bill Buzbeea114add2012-05-03 15:00:40 -07001294 x &= x - 1;
1295 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001296}
1297
1298// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001299static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001300 int bit_posn = 0;
1301 while ((x & 0xf) == 0) {
1302 bit_posn += 4;
1303 x >>= 4;
1304 }
1305 while ((x & 1) == 0) {
1306 bit_posn++;
1307 x >>= 1;
1308 }
1309 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001310}
1311
buzbeefa57c472012-11-21 12:06:18 -08001312// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1313// and store the result in 'rl_dest'.
1314static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1315 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001316{
buzbeefa57c472012-11-21 12:06:18 -08001317 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001318 return false;
buzbee0f79d722012-11-01 15:35:27 -07001319 }
buzbee02031b12012-11-23 09:41:35 -08001320 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001321 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001322 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001323 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001324 }
buzbee52a77fc2012-11-20 19:50:46 -08001325 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001326 if (k >= 30) {
1327 // Avoid special cases.
1328 return false;
1329 }
buzbeefa57c472012-11-21 12:06:18 -08001330 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1331 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001332 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001333 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001334 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001335 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001336 if (lit == 2) {
1337 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001338 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1339 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1340 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001341 } else {
buzbee02031b12012-11-23 09:41:35 -08001342 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1343 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1344 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1345 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001346 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001347 } else {
buzbeefa57c472012-11-21 12:06:18 -08001348 int t_reg1 = AllocTemp(cu);
1349 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001350 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001351 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1352 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1353 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1354 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001355 } else {
buzbee02031b12012-11-23 09:41:35 -08001356 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1357 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1358 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1359 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1360 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001361 }
1362 }
buzbee02031b12012-11-23 09:41:35 -08001363 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001364 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001365}
1366
buzbeefa57c472012-11-21 12:06:18 -08001367// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1368// and store the result in 'rl_dest'.
1369static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1370 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001371{
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001373 bool power_of_two = false;
1374 bool pop_count_le2 = false;
1375 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001376 if (lit < 2) {
1377 // Avoid special cases.
1378 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001379 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001380 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001381 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001382 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001383 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001384 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001385 } else {
1386 return false;
1387 }
buzbee02031b12012-11-23 09:41:35 -08001388 Codegen* cg = cu->cg.get();
1389 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001390 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1391 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001392 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001393 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001394 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001395 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001396 int first_bit = LowestSetBit(lit);
1397 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001398 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001399 } else {
1400 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001401 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001402 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001403 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001404 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1405 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001406 }
buzbee02031b12012-11-23 09:41:35 -08001407 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001408 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001409}
1410
buzbeea5954be2013-02-07 10:41:40 -08001411void Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
buzbee02031b12012-11-23 09:41:35 -08001412 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001413{
buzbeefa57c472012-11-21 12:06:18 -08001414 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001415 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001416 int shift_op = false;
1417 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001418
buzbee408ad162012-06-06 16:45:18 -07001419 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001420 case Instruction::RSUB_INT_LIT8:
1421 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001422 rl_src = LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001423 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
buzbeec7d1f912013-02-07 15:22:39 -08001424 if (cu->instruction_set == kThumb2) {
1425 OpRegRegImm(cu, kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1426 } else {
1427 OpRegReg(cu, kOpNeg, rl_result.low_reg, rl_src.low_reg);
1428 OpRegImm(cu, kOpAdd, rl_result.low_reg, lit);
1429 }
buzbeefa57c472012-11-21 12:06:18 -08001430 StoreValue(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001431 return;
buzbee31a4a6f2012-02-28 15:36:15 -08001432 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001433
buzbeee6285f92012-12-06 15:57:46 -08001434 case Instruction::SUB_INT:
1435 case Instruction::SUB_INT_2ADDR:
1436 lit = -lit;
1437 // Intended fallthrough
1438 case Instruction::ADD_INT:
1439 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001440 case Instruction::ADD_INT_LIT8:
1441 case Instruction::ADD_INT_LIT16:
1442 op = kOpAdd;
1443 break;
buzbeee6285f92012-12-06 15:57:46 -08001444 case Instruction::MUL_INT:
1445 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001446 case Instruction::MUL_INT_LIT8:
1447 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001448 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
buzbeea5954be2013-02-07 10:41:40 -08001449 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001450 }
1451 op = kOpMul;
1452 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001453 }
buzbeee6285f92012-12-06 15:57:46 -08001454 case Instruction::AND_INT:
1455 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001456 case Instruction::AND_INT_LIT8:
1457 case Instruction::AND_INT_LIT16:
1458 op = kOpAnd;
1459 break;
buzbeee6285f92012-12-06 15:57:46 -08001460 case Instruction::OR_INT:
1461 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001462 case Instruction::OR_INT_LIT8:
1463 case Instruction::OR_INT_LIT16:
1464 op = kOpOr;
1465 break;
buzbeee6285f92012-12-06 15:57:46 -08001466 case Instruction::XOR_INT:
1467 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001468 case Instruction::XOR_INT_LIT8:
1469 case Instruction::XOR_INT_LIT16:
1470 op = kOpXor;
1471 break;
1472 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001473 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001474 case Instruction::SHL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001475 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001476 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001477 op = kOpLsl;
1478 break;
1479 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001480 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001481 case Instruction::SHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001482 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001483 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001484 op = kOpAsr;
1485 break;
1486 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001487 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001488 case Instruction::USHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001490 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001491 op = kOpLsr;
1492 break;
1493
buzbeee6285f92012-12-06 15:57:46 -08001494 case Instruction::DIV_INT:
1495 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001496 case Instruction::DIV_INT_LIT8:
1497 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001498 case Instruction::REM_INT:
1499 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001500 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001501 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001502 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001503 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
buzbeea5954be2013-02-07 10:41:40 -08001504 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001505 }
buzbeefa57c472012-11-21 12:06:18 -08001506 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
buzbeea5954be2013-02-07 10:41:40 -08001507 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001508 }
buzbee408ad162012-06-06 16:45:18 -07001509 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001510 (opcode == Instruction::DIV_INT) ||
1511 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001512 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001513 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001514 } else {
buzbeefa57c472012-11-21 12:06:18 -08001515 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001516 }
buzbeefa57c472012-11-21 12:06:18 -08001517 if (cu->instruction_set == kMips) {
1518 rl_src = LoadValue(cu, rl_src, kCoreReg);
1519 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001520 } else {
buzbeefa57c472012-11-21 12:06:18 -08001521 FlushAllRegs(cu); /* Everything to home location */
1522 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1523 Clobber(cu, TargetReg(kArg0));
1524 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1525 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1526 if (is_div)
1527 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001528 else
buzbeefa57c472012-11-21 12:06:18 -08001529 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001530 }
buzbeefa57c472012-11-21 12:06:18 -08001531 StoreValue(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001532 return;
jeffhao4f8f04a2012-10-02 18:10:35 -07001533 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001534 default:
buzbeee6285f92012-12-06 15:57:46 -08001535 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001536 }
buzbeefa57c472012-11-21 12:06:18 -08001537 rl_src = LoadValue(cu, rl_src, kCoreReg);
1538 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001539 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001540 if (shift_op && (lit == 0)) {
1541 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001542 } else {
buzbeefa57c472012-11-21 12:06:18 -08001543 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001544 }
buzbeefa57c472012-11-21 12:06:18 -08001545 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -08001546}
1547
buzbeea5954be2013-02-07 10:41:40 -08001548void Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001549 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001550{
buzbeefa57c472012-11-21 12:06:18 -08001551 RegLocation rl_result;
1552 OpKind first_op = kOpBkpt;
1553 OpKind second_op = kOpBkpt;
1554 bool call_out = false;
1555 bool check_zero = false;
1556 int func_offset;
1557 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001558
buzbee408ad162012-06-06 16:45:18 -07001559 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001560 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001561 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1562 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001563 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001564 if (rl_result.low_reg == rl_src2.high_reg) {
1565 int t_reg = AllocTemp(cu);
1566 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1567 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1568 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1569 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001570 } else {
buzbeefa57c472012-11-21 12:06:18 -08001571 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1572 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001573 }
buzbeefa57c472012-11-21 12:06:18 -08001574 StoreValueWide(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001575 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001576 case Instruction::ADD_LONG:
1577 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001578 if (cu->instruction_set != kThumb2) {
buzbeea5954be2013-02-07 10:41:40 -08001579 GenAddLong(cu, rl_dest, rl_src1, rl_src2);
1580 return;
buzbeeb046e162012-10-30 15:48:42 -07001581 }
buzbeefa57c472012-11-21 12:06:18 -08001582 first_op = kOpAdd;
1583 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001584 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001585 case Instruction::SUB_LONG:
1586 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001587 if (cu->instruction_set != kThumb2) {
buzbeea5954be2013-02-07 10:41:40 -08001588 GenSubLong(cu, rl_dest, rl_src1, rl_src2);
1589 return;
buzbeeb046e162012-10-30 15:48:42 -07001590 }
buzbeefa57c472012-11-21 12:06:18 -08001591 first_op = kOpSub;
1592 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001593 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001594 case Instruction::MUL_LONG:
1595 case Instruction::MUL_LONG_2ADDR:
buzbee4ef3e452012-12-14 13:35:28 -08001596 if (cu->instruction_set == kThumb2) {
1597 GenMulLong(cu, rl_dest, rl_src1, rl_src2);
buzbeea5954be2013-02-07 10:41:40 -08001598 return;
buzbee4ef3e452012-12-14 13:35:28 -08001599 } else {
1600 call_out = true;
1601 ret_reg = TargetReg(kRet0);
1602 func_offset = ENTRYPOINT_OFFSET(pLmul);
1603 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001604 break;
1605 case Instruction::DIV_LONG:
1606 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001607 call_out = true;
1608 check_zero = true;
1609 ret_reg = TargetReg(kRet0);
1610 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001611 break;
1612 case Instruction::REM_LONG:
1613 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001614 call_out = true;
1615 check_zero = true;
1616 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001617 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001618 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001619 break;
1620 case Instruction::AND_LONG_2ADDR:
1621 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001622 if (cu->instruction_set == kX86) {
1623 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001624 }
buzbeefa57c472012-11-21 12:06:18 -08001625 first_op = kOpAnd;
1626 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001627 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001628 case Instruction::OR_LONG:
1629 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001630 if (cu->instruction_set == kX86) {
buzbeea5954be2013-02-07 10:41:40 -08001631 GenOrLong(cu, rl_dest, rl_src1, rl_src2);
1632 return;
buzbeeb046e162012-10-30 15:48:42 -07001633 }
buzbeefa57c472012-11-21 12:06:18 -08001634 first_op = kOpOr;
1635 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001636 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001637 case Instruction::XOR_LONG:
1638 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001639 if (cu->instruction_set == kX86) {
buzbeea5954be2013-02-07 10:41:40 -08001640 GenXorLong(cu, rl_dest, rl_src1, rl_src2);
1641 return;
buzbeeb046e162012-10-30 15:48:42 -07001642 }
buzbeefa57c472012-11-21 12:06:18 -08001643 first_op = kOpXor;
1644 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001645 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001646 case Instruction::NEG_LONG: {
buzbeea5954be2013-02-07 10:41:40 -08001647 GenNegLong(cu, rl_dest, rl_src2);
1648 return;
buzbee31a4a6f2012-02-28 15:36:15 -08001649 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001650 default:
1651 LOG(FATAL) << "Invalid long arith op";
1652 }
buzbeefa57c472012-11-21 12:06:18 -08001653 if (!call_out) {
1654 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001655 } else {
buzbeefa57c472012-11-21 12:06:18 -08001656 FlushAllRegs(cu); /* Send everything to home location */
1657 if (check_zero) {
1658 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1659 int r_tgt = CallHelperSetup(cu, func_offset);
1660 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1661 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001662 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001663 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001664 } else {
buzbeefa57c472012-11-21 12:06:18 -08001665 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1666 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001667 }
buzbeef0504cd2012-11-13 16:31:10 -08001668 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001669 if (ret_reg == TargetReg(kRet0))
1670 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001671 else
buzbeefa57c472012-11-21 12:06:18 -08001672 rl_result = GetReturnWideAlt(cu);
1673 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001674 }
buzbee31a4a6f2012-02-28 15:36:15 -08001675}
1676
buzbeea5954be2013-02-07 10:41:40 -08001677void Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
buzbee02031b12012-11-23 09:41:35 -08001678 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001679{
Bill Buzbeea114add2012-05-03 15:00:40 -07001680 /*
1681 * Don't optimize the register usage since it calls out to support
1682 * functions
1683 */
buzbeefa57c472012-11-21 12:06:18 -08001684 FlushAllRegs(cu); /* Send everything to home location */
1685 if (rl_src.wide) {
1686 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1687 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001688 } else {
buzbeefa57c472012-11-21 12:06:18 -08001689 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001690 }
buzbeefa57c472012-11-21 12:06:18 -08001691 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1692 if (rl_dest.wide) {
1693 RegLocation rl_result;
1694 rl_result = GetReturnWide(cu, rl_dest.fp);
1695 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001696 } else {
buzbeefa57c472012-11-21 12:06:18 -08001697 RegLocation rl_result;
1698 rl_result = GetReturn(cu, rl_dest.fp);
1699 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001700 }
buzbee31a4a6f2012-02-28 15:36:15 -08001701}
1702
buzbee31a4a6f2012-02-28 15:36:15 -08001703/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001704void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001705{
buzbeefa57c472012-11-21 12:06:18 -08001706 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001707 return;
1708 }
buzbeefa57c472012-11-21 12:06:18 -08001709 FlushAllRegs(cu);
1710 LIR* branch = OpTestSuspend(cu, NULL);
1711 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1712 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1713 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001714 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001715 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001716}
1717
buzbeefead2932012-03-30 14:02:01 -07001718/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001719void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001720{
buzbeefa57c472012-11-21 12:06:18 -08001721 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1722 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001723 return;
1724 }
buzbeefa57c472012-11-21 12:06:18 -08001725 OpTestSuspend(cu, target);
1726 LIR* launch_pad =
1727 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1728 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1729 FlushAllRegs(cu);
1730 OpUnconditionalBranch(cu, launch_pad);
1731 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001732}
1733
buzbee31a4a6f2012-02-28 15:36:15 -08001734} // namespace art