blob: 9696bcab6fd118049573a2c5196b8acb89438b96 [file] [log] [blame]
buzbeeefc63692012-11-14 16:31:52 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This file contains codegen for the Thumb2 ISA. */
18
19#include "oat_compilation_unit.h"
20#include "oat/runtime/oat_support_entrypoints.h"
buzbee1bc37c62012-11-20 13:35:41 -080021#include "arm_lir.h"
buzbee02031b12012-11-23 09:41:35 -080022#include "codegen_arm.h"
buzbee1bc37c62012-11-20 13:35:41 -080023#include "../codegen_util.h"
24#include "../ralloc_util.h"
buzbeeefc63692012-11-14 16:31:52 -080025
26namespace art {
27
28
29/* Return the position of an ssa name within the argument list */
buzbeefa57c472012-11-21 12:06:18 -080030static int InPosition(CompilationUnit* cu, int s_reg)
buzbeeefc63692012-11-14 16:31:52 -080031{
buzbeefa57c472012-11-21 12:06:18 -080032 int v_reg = SRegToVReg(cu, s_reg);
33 return v_reg - cu->num_regs;
buzbeeefc63692012-11-14 16:31:52 -080034}
35
36/*
37 * Describe an argument. If it's already in an arg register, just leave it
38 * there. NOTE: all live arg registers must be locked prior to this call
39 * to avoid having them allocated as a temp by downstream utilities.
40 */
buzbee02031b12012-11-23 09:41:35 -080041RegLocation ArmCodegen::ArgLoc(CompilationUnit* cu, RegLocation loc)
buzbeeefc63692012-11-14 16:31:52 -080042{
buzbeefa57c472012-11-21 12:06:18 -080043 int arg_num = InPosition(cu, loc.s_reg_low);
buzbeeefc63692012-11-14 16:31:52 -080044 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -080045 if (arg_num == 2) {
buzbeeefc63692012-11-14 16:31:52 -080046 // Bad case - half in register, half in frame. Just punt
47 loc.location = kLocInvalid;
buzbeefa57c472012-11-21 12:06:18 -080048 } else if (arg_num < 2) {
49 loc.low_reg = rARM_ARG1 + arg_num;
50 loc.high_reg = loc.low_reg + 1;
buzbeeefc63692012-11-14 16:31:52 -080051 loc.location = kLocPhysReg;
52 } else {
53 loc.location = kLocDalvikFrame;
54 }
55 } else {
buzbeefa57c472012-11-21 12:06:18 -080056 if (arg_num < 3) {
57 loc.low_reg = rARM_ARG1 + arg_num;
buzbeeefc63692012-11-14 16:31:52 -080058 loc.location = kLocPhysReg;
59 } else {
60 loc.location = kLocDalvikFrame;
61 }
62 }
63 return loc;
64}
65
66/*
67 * Load an argument. If already in a register, just return. If in
buzbee52a77fc2012-11-20 19:50:46 -080068 * the frame, we can't use the normal LoadValue() because it assumed
buzbeeefc63692012-11-14 16:31:52 -080069 * a proper frame - and we're frameless.
70 */
buzbee02031b12012-11-23 09:41:35 -080071static RegLocation LoadArg(CompilationUnit* cu, RegLocation loc)
buzbeeefc63692012-11-14 16:31:52 -080072{
buzbee02031b12012-11-23 09:41:35 -080073 Codegen* cg = cu->cg.get();
buzbeeefc63692012-11-14 16:31:52 -080074 if (loc.location == kLocDalvikFrame) {
buzbeefa57c472012-11-21 12:06:18 -080075 int start = (InPosition(cu, loc.s_reg_low) + 1) * sizeof(uint32_t);
76 loc.low_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -080077 cg->LoadWordDisp(cu, rARM_SP, start, loc.low_reg);
buzbeeefc63692012-11-14 16:31:52 -080078 if (loc.wide) {
buzbeefa57c472012-11-21 12:06:18 -080079 loc.high_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -080080 cg->LoadWordDisp(cu, rARM_SP, start + sizeof(uint32_t), loc.high_reg);
buzbeeefc63692012-11-14 16:31:52 -080081 }
82 loc.location = kLocPhysReg;
83 }
84 return loc;
85}
86
87/* Lock any referenced arguments that arrive in registers */
buzbeefa57c472012-11-21 12:06:18 -080088static void LockLiveArgs(CompilationUnit* cu, MIR* mir)
buzbeeefc63692012-11-14 16:31:52 -080089{
buzbeefa57c472012-11-21 12:06:18 -080090 int first_in = cu->num_regs;
91 const int num_arg_regs = 3; // TODO: generalize & move to RegUtil.cc
92 for (int i = 0; i < mir->ssa_rep->num_uses; i++) {
93 int v_reg = SRegToVReg(cu, mir->ssa_rep->uses[i]);
94 int InPosition = v_reg - first_in;
95 if (InPosition < num_arg_regs) {
96 LockTemp(cu, rARM_ARG1 + InPosition);
buzbeeefc63692012-11-14 16:31:52 -080097 }
98 }
99}
100
101/* Find the next MIR, which may be in a following basic block */
buzbeefa57c472012-11-21 12:06:18 -0800102static MIR* GetNextMir(CompilationUnit* cu, BasicBlock** p_bb, MIR* mir)
buzbeeefc63692012-11-14 16:31:52 -0800103{
buzbeefa57c472012-11-21 12:06:18 -0800104 BasicBlock* bb = *p_bb;
105 MIR* orig_mir = mir;
buzbeeefc63692012-11-14 16:31:52 -0800106 while (bb != NULL) {
107 if (mir != NULL) {
108 mir = mir->next;
109 }
110 if (mir != NULL) {
111 return mir;
112 } else {
buzbeefa57c472012-11-21 12:06:18 -0800113 bb = bb->fall_through;
114 *p_bb = bb;
buzbeeefc63692012-11-14 16:31:52 -0800115 if (bb) {
buzbeefa57c472012-11-21 12:06:18 -0800116 mir = bb->first_mir_insn;
buzbeeefc63692012-11-14 16:31:52 -0800117 if (mir != NULL) {
118 return mir;
119 }
120 }
121 }
122 }
buzbeefa57c472012-11-21 12:06:18 -0800123 return orig_mir;
buzbeeefc63692012-11-14 16:31:52 -0800124}
125
buzbeefa57c472012-11-21 12:06:18 -0800126/* Used for the "verbose" listing */
buzbee02031b12012-11-23 09:41:35 -0800127//TODO: move to common code
128void ArmCodegen::GenPrintLabel(CompilationUnit *cu, MIR* mir)
buzbeeefc63692012-11-14 16:31:52 -0800129{
130 /* Mark the beginning of a Dalvik instruction for line tracking */
buzbeefa57c472012-11-21 12:06:18 -0800131 char* inst_str = cu->verbose ?
buzbeea169e1d2012-12-05 14:26:44 -0800132 GetDalvikDisassembly(cu, mir) : NULL;
buzbeefa57c472012-11-21 12:06:18 -0800133 MarkBoundary(cu, mir->offset, inst_str);
buzbeeefc63692012-11-14 16:31:52 -0800134}
135
buzbeefa57c472012-11-21 12:06:18 -0800136static MIR* SpecialIGet(CompilationUnit* cu, BasicBlock** bb, MIR* mir,
137 OpSize size, bool long_or_double, bool is_object)
buzbeeefc63692012-11-14 16:31:52 -0800138{
buzbee02031b12012-11-23 09:41:35 -0800139 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800140 int field_offset;
141 bool is_volatile;
142 uint32_t field_idx = mir->dalvikInsn.vC;
143 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
144 if (!fast_path || !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) {
buzbeeefc63692012-11-14 16:31:52 -0800145 return NULL;
146 }
buzbeefa57c472012-11-21 12:06:18 -0800147 RegLocation rl_obj = GetSrc(cu, mir, 0);
148 LockLiveArgs(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800149 rl_obj = ArmCodegen::ArgLoc(cu, rl_obj);
buzbeefa57c472012-11-21 12:06:18 -0800150 RegLocation rl_dest;
151 if (long_or_double) {
152 rl_dest = GetReturnWide(cu, false);
buzbeeefc63692012-11-14 16:31:52 -0800153 } else {
buzbeefa57c472012-11-21 12:06:18 -0800154 rl_dest = GetReturn(cu, false);
buzbeeefc63692012-11-14 16:31:52 -0800155 }
156 // Point of no return - no aborts after this
buzbee02031b12012-11-23 09:41:35 -0800157 ArmCodegen::GenPrintLabel(cu, mir);
buzbeefa57c472012-11-21 12:06:18 -0800158 rl_obj = LoadArg(cu, rl_obj);
buzbee02031b12012-11-23 09:41:35 -0800159 cg->GenIGet(cu, field_idx, mir->optimization_flags, size, rl_dest, rl_obj,
160 long_or_double, is_object);
buzbeefa57c472012-11-21 12:06:18 -0800161 return GetNextMir(cu, bb, mir);
buzbeeefc63692012-11-14 16:31:52 -0800162}
163
buzbeefa57c472012-11-21 12:06:18 -0800164static MIR* SpecialIPut(CompilationUnit* cu, BasicBlock** bb, MIR* mir,
165 OpSize size, bool long_or_double, bool is_object)
buzbeeefc63692012-11-14 16:31:52 -0800166{
buzbee02031b12012-11-23 09:41:35 -0800167 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800168 int field_offset;
169 bool is_volatile;
170 uint32_t field_idx = mir->dalvikInsn.vC;
171 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
172 if (!fast_path || !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) {
buzbeeefc63692012-11-14 16:31:52 -0800173 return NULL;
174 }
buzbeefa57c472012-11-21 12:06:18 -0800175 RegLocation rl_src;
176 RegLocation rl_obj;
177 LockLiveArgs(cu, mir);
178 if (long_or_double) {
179 rl_src = GetSrcWide(cu, mir, 0);
180 rl_obj = GetSrc(cu, mir, 2);
buzbeeefc63692012-11-14 16:31:52 -0800181 } else {
buzbeefa57c472012-11-21 12:06:18 -0800182 rl_src = GetSrc(cu, mir, 0);
183 rl_obj = GetSrc(cu, mir, 1);
buzbeeefc63692012-11-14 16:31:52 -0800184 }
buzbee02031b12012-11-23 09:41:35 -0800185 rl_src = ArmCodegen::ArgLoc(cu, rl_src);
186 rl_obj = ArmCodegen::ArgLoc(cu, rl_obj);
buzbeeefc63692012-11-14 16:31:52 -0800187 // Reject if source is split across registers & frame
buzbeefa57c472012-11-21 12:06:18 -0800188 if (rl_obj.location == kLocInvalid) {
189 ResetRegPool(cu);
buzbeeefc63692012-11-14 16:31:52 -0800190 return NULL;
191 }
192 // Point of no return - no aborts after this
buzbee02031b12012-11-23 09:41:35 -0800193 ArmCodegen::GenPrintLabel(cu, mir);
buzbeefa57c472012-11-21 12:06:18 -0800194 rl_obj = LoadArg(cu, rl_obj);
195 rl_src = LoadArg(cu, rl_src);
buzbee02031b12012-11-23 09:41:35 -0800196 cg->GenIPut(cu, field_idx, mir->optimization_flags, size, rl_src, rl_obj,
197 long_or_double, is_object);
buzbeefa57c472012-11-21 12:06:18 -0800198 return GetNextMir(cu, bb, mir);
buzbeeefc63692012-11-14 16:31:52 -0800199}
200
buzbeefa57c472012-11-21 12:06:18 -0800201static MIR* SpecialIdentity(CompilationUnit* cu, MIR* mir)
buzbeeefc63692012-11-14 16:31:52 -0800202{
buzbee02031b12012-11-23 09:41:35 -0800203 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800204 RegLocation rl_src;
205 RegLocation rl_dest;
206 bool wide = (mir->ssa_rep->num_uses == 2);
buzbeeefc63692012-11-14 16:31:52 -0800207 if (wide) {
buzbeefa57c472012-11-21 12:06:18 -0800208 rl_src = GetSrcWide(cu, mir, 0);
209 rl_dest = GetReturnWide(cu, false);
buzbeeefc63692012-11-14 16:31:52 -0800210 } else {
buzbeefa57c472012-11-21 12:06:18 -0800211 rl_src = GetSrc(cu, mir, 0);
212 rl_dest = GetReturn(cu, false);
buzbeeefc63692012-11-14 16:31:52 -0800213 }
buzbeefa57c472012-11-21 12:06:18 -0800214 LockLiveArgs(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800215 rl_src = ArmCodegen::ArgLoc(cu, rl_src);
buzbeefa57c472012-11-21 12:06:18 -0800216 if (rl_src.location == kLocInvalid) {
217 ResetRegPool(cu);
buzbeeefc63692012-11-14 16:31:52 -0800218 return NULL;
219 }
220 // Point of no return - no aborts after this
buzbee02031b12012-11-23 09:41:35 -0800221 ArmCodegen::GenPrintLabel(cu, mir);
buzbeefa57c472012-11-21 12:06:18 -0800222 rl_src = LoadArg(cu, rl_src);
buzbeeefc63692012-11-14 16:31:52 -0800223 if (wide) {
buzbee02031b12012-11-23 09:41:35 -0800224 cg->StoreValueWide(cu, rl_dest, rl_src);
buzbeeefc63692012-11-14 16:31:52 -0800225 } else {
buzbee02031b12012-11-23 09:41:35 -0800226 cg->StoreValue(cu, rl_dest, rl_src);
buzbeeefc63692012-11-14 16:31:52 -0800227 }
228 return mir;
229}
230
231/*
232 * Special-case code genration for simple non-throwing leaf methods.
233 */
buzbee02031b12012-11-23 09:41:35 -0800234void ArmCodegen::GenSpecialCase(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
235 SpecialCaseHandler special_case)
buzbeeefc63692012-11-14 16:31:52 -0800236{
buzbeefa57c472012-11-21 12:06:18 -0800237 cu->current_dalvik_offset = mir->offset;
238 MIR* next_mir = NULL;
239 switch (special_case) {
buzbeeefc63692012-11-14 16:31:52 -0800240 case kNullMethod:
241 DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID);
buzbeefa57c472012-11-21 12:06:18 -0800242 next_mir = mir;
buzbeeefc63692012-11-14 16:31:52 -0800243 break;
244 case kConstFunction:
buzbee02031b12012-11-23 09:41:35 -0800245 ArmCodegen::GenPrintLabel(cu, mir);
buzbeefa57c472012-11-21 12:06:18 -0800246 LoadConstant(cu, rARM_RET0, mir->dalvikInsn.vB);
247 next_mir = GetNextMir(cu, &bb, mir);
buzbeeefc63692012-11-14 16:31:52 -0800248 break;
249 case kIGet:
buzbeefa57c472012-11-21 12:06:18 -0800250 next_mir = SpecialIGet(cu, &bb, mir, kWord, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800251 break;
252 case kIGetBoolean:
253 case kIGetByte:
buzbeefa57c472012-11-21 12:06:18 -0800254 next_mir = SpecialIGet(cu, &bb, mir, kUnsignedByte, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800255 break;
256 case kIGetObject:
buzbeefa57c472012-11-21 12:06:18 -0800257 next_mir = SpecialIGet(cu, &bb, mir, kWord, false, true);
buzbeeefc63692012-11-14 16:31:52 -0800258 break;
259 case kIGetChar:
buzbeefa57c472012-11-21 12:06:18 -0800260 next_mir = SpecialIGet(cu, &bb, mir, kUnsignedHalf, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800261 break;
262 case kIGetShort:
buzbeefa57c472012-11-21 12:06:18 -0800263 next_mir = SpecialIGet(cu, &bb, mir, kSignedHalf, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800264 break;
265 case kIGetWide:
buzbeefa57c472012-11-21 12:06:18 -0800266 next_mir = SpecialIGet(cu, &bb, mir, kLong, true, false);
buzbeeefc63692012-11-14 16:31:52 -0800267 break;
268 case kIPut:
buzbeefa57c472012-11-21 12:06:18 -0800269 next_mir = SpecialIPut(cu, &bb, mir, kWord, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800270 break;
271 case kIPutBoolean:
272 case kIPutByte:
buzbeefa57c472012-11-21 12:06:18 -0800273 next_mir = SpecialIPut(cu, &bb, mir, kUnsignedByte, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800274 break;
275 case kIPutObject:
buzbeefa57c472012-11-21 12:06:18 -0800276 next_mir = SpecialIPut(cu, &bb, mir, kWord, false, true);
buzbeeefc63692012-11-14 16:31:52 -0800277 break;
278 case kIPutChar:
buzbeefa57c472012-11-21 12:06:18 -0800279 next_mir = SpecialIPut(cu, &bb, mir, kUnsignedHalf, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800280 break;
281 case kIPutShort:
buzbeefa57c472012-11-21 12:06:18 -0800282 next_mir = SpecialIPut(cu, &bb, mir, kSignedHalf, false, false);
buzbeeefc63692012-11-14 16:31:52 -0800283 break;
284 case kIPutWide:
buzbeefa57c472012-11-21 12:06:18 -0800285 next_mir = SpecialIPut(cu, &bb, mir, kLong, true, false);
buzbeeefc63692012-11-14 16:31:52 -0800286 break;
287 case kIdentity:
buzbeefa57c472012-11-21 12:06:18 -0800288 next_mir = SpecialIdentity(cu, mir);
buzbeeefc63692012-11-14 16:31:52 -0800289 break;
290 default:
291 return;
292 }
buzbeefa57c472012-11-21 12:06:18 -0800293 if (next_mir != NULL) {
294 cu->current_dalvik_offset = next_mir->offset;
295 if (special_case != kIdentity) {
buzbee02031b12012-11-23 09:41:35 -0800296 ArmCodegen::GenPrintLabel(cu, next_mir);
buzbeeefc63692012-11-14 16:31:52 -0800297 }
buzbeefa57c472012-11-21 12:06:18 -0800298 NewLIR1(cu, kThumbBx, rARM_LR);
299 cu->core_spill_mask = 0;
300 cu->num_core_spills = 0;
301 cu->fp_spill_mask = 0;
302 cu->num_fp_spills = 0;
303 cu->frame_size = 0;
304 cu->core_vmap_table.clear();
305 cu->fp_vmap_table.clear();
buzbeeefc63692012-11-14 16:31:52 -0800306 }
307}
308
309/*
310 * The sparse table in the literal pool is an array of <key,displacement>
311 * pairs. For each set, we'll load them as a pair using ldmia.
312 * This means that the register number of the temp we use for the key
313 * must be lower than the reg for the displacement.
314 *
315 * The test loop will look something like:
316 *
317 * adr rBase, <table>
buzbeefa57c472012-11-21 12:06:18 -0800318 * ldr r_val, [rARM_SP, v_reg_off]
319 * mov r_idx, #table_size
buzbeeefc63692012-11-14 16:31:52 -0800320 * lp:
buzbeefa57c472012-11-21 12:06:18 -0800321 * ldmia rBase!, {r_key, r_disp}
322 * sub r_idx, #1
323 * cmp r_val, r_key
buzbeeefc63692012-11-14 16:31:52 -0800324 * ifeq
buzbeefa57c472012-11-21 12:06:18 -0800325 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
326 * cbnz r_idx, lp
buzbeeefc63692012-11-14 16:31:52 -0800327 */
buzbee02031b12012-11-23 09:41:35 -0800328void ArmCodegen::GenSparseSwitch(CompilationUnit* cu, uint32_t table_offset, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800329{
buzbeefa57c472012-11-21 12:06:18 -0800330 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
331 if (cu->verbose) {
buzbee52a77fc2012-11-20 19:50:46 -0800332 DumpSparseSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -0800333 }
334 // Add the table to the list - we'll process it later
buzbeefa57c472012-11-21 12:06:18 -0800335 SwitchTable *tab_rec =
336 static_cast<SwitchTable*>(NewMem(cu, sizeof(SwitchTable), true, kAllocData));
337 tab_rec->table = table;
338 tab_rec->vaddr = cu->current_dalvik_offset;
buzbeeefc63692012-11-14 16:31:52 -0800339 int size = table[1];
buzbeefa57c472012-11-21 12:06:18 -0800340 tab_rec->targets = static_cast<LIR**>(NewMem(cu, size * sizeof(LIR*), true, kAllocLIR));
341 InsertGrowableList(cu, &cu->switch_tables, reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -0800342
343 // Get the switch value
buzbeefa57c472012-11-21 12:06:18 -0800344 rl_src = LoadValue(cu, rl_src, kCoreReg);
345 int rBase = AllocTemp(cu);
buzbeeefc63692012-11-14 16:31:52 -0800346 /* Allocate key and disp temps */
buzbeefa57c472012-11-21 12:06:18 -0800347 int r_key = AllocTemp(cu);
348 int r_disp = AllocTemp(cu);
349 // Make sure r_key's register number is less than r_disp's number for ldmia
350 if (r_key > r_disp) {
351 int tmp = r_disp;
352 r_disp = r_key;
353 r_key = tmp;
buzbeeefc63692012-11-14 16:31:52 -0800354 }
355 // Materialize a pointer to the switch table
buzbeefa57c472012-11-21 12:06:18 -0800356 NewLIR3(cu, kThumb2Adr, rBase, 0, reinterpret_cast<uintptr_t>(tab_rec));
357 // Set up r_idx
358 int r_idx = AllocTemp(cu);
359 LoadConstant(cu, r_idx, size);
buzbeeefc63692012-11-14 16:31:52 -0800360 // Establish loop branch target
buzbeefa57c472012-11-21 12:06:18 -0800361 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800362 // Load next key/disp
buzbeefa57c472012-11-21 12:06:18 -0800363 NewLIR2(cu, kThumb2LdmiaWB, rBase, (1 << r_key) | (1 << r_disp));
364 OpRegReg(cu, kOpCmp, r_key, rl_src.low_reg);
buzbeeefc63692012-11-14 16:31:52 -0800365 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
buzbee02031b12012-11-23 09:41:35 -0800366 OpIT(cu, kCondEq, "");
buzbeefa57c472012-11-21 12:06:18 -0800367 LIR* switch_branch = NewLIR1(cu, kThumb2AddPCR, r_disp);
368 tab_rec->anchor = switch_branch;
buzbeeefc63692012-11-14 16:31:52 -0800369 // Needs to use setflags encoding here
buzbeefa57c472012-11-21 12:06:18 -0800370 NewLIR3(cu, kThumb2SubsRRI12, r_idx, r_idx, 1);
371 OpCondBranch(cu, kCondNe, target);
buzbeeefc63692012-11-14 16:31:52 -0800372}
373
374
buzbee02031b12012-11-23 09:41:35 -0800375void ArmCodegen::GenPackedSwitch(CompilationUnit* cu, uint32_t table_offset, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800376{
buzbeefa57c472012-11-21 12:06:18 -0800377 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
378 if (cu->verbose) {
buzbee52a77fc2012-11-20 19:50:46 -0800379 DumpPackedSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -0800380 }
381 // Add the table to the list - we'll process it later
buzbeefa57c472012-11-21 12:06:18 -0800382 SwitchTable *tab_rec =
383 static_cast<SwitchTable*>(NewMem(cu, sizeof(SwitchTable), true, kAllocData));
384 tab_rec->table = table;
385 tab_rec->vaddr = cu->current_dalvik_offset;
buzbeeefc63692012-11-14 16:31:52 -0800386 int size = table[1];
buzbeefa57c472012-11-21 12:06:18 -0800387 tab_rec->targets = static_cast<LIR**>(NewMem(cu, size * sizeof(LIR*), true, kAllocLIR));
388 InsertGrowableList(cu, &cu->switch_tables, reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -0800389
390 // Get the switch value
buzbeefa57c472012-11-21 12:06:18 -0800391 rl_src = LoadValue(cu, rl_src, kCoreReg);
392 int table_base = AllocTemp(cu);
buzbeeefc63692012-11-14 16:31:52 -0800393 // Materialize a pointer to the switch table
buzbeefa57c472012-11-21 12:06:18 -0800394 NewLIR3(cu, kThumb2Adr, table_base, 0, reinterpret_cast<uintptr_t>(tab_rec));
395 int low_key = s4FromSwitchData(&table[2]);
buzbeeefc63692012-11-14 16:31:52 -0800396 int keyReg;
397 // Remove the bias, if necessary
buzbeefa57c472012-11-21 12:06:18 -0800398 if (low_key == 0) {
399 keyReg = rl_src.low_reg;
buzbeeefc63692012-11-14 16:31:52 -0800400 } else {
buzbeefa57c472012-11-21 12:06:18 -0800401 keyReg = AllocTemp(cu);
402 OpRegRegImm(cu, kOpSub, keyReg, rl_src.low_reg, low_key);
buzbeeefc63692012-11-14 16:31:52 -0800403 }
404 // Bounds check - if < 0 or >= size continue following switch
buzbeefa57c472012-11-21 12:06:18 -0800405 OpRegImm(cu, kOpCmp, keyReg, size-1);
406 LIR* branch_over = OpCondBranch(cu, kCondHi, NULL);
buzbeeefc63692012-11-14 16:31:52 -0800407
408 // Load the displacement from the switch table
buzbeefa57c472012-11-21 12:06:18 -0800409 int disp_reg = AllocTemp(cu);
410 LoadBaseIndexed(cu, table_base, keyReg, disp_reg, 2, kWord);
buzbeeefc63692012-11-14 16:31:52 -0800411
412 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbeefa57c472012-11-21 12:06:18 -0800413 LIR* switch_branch = NewLIR1(cu, kThumb2AddPCR, disp_reg);
414 tab_rec->anchor = switch_branch;
buzbeeefc63692012-11-14 16:31:52 -0800415
buzbeefa57c472012-11-21 12:06:18 -0800416 /* branch_over target here */
417 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
418 branch_over->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800419}
420
421/*
422 * Array data table format:
423 * ushort ident = 0x0300 magic value
424 * ushort width width of each element in the table
425 * uint size number of elements in the table
426 * ubyte data[size*width] table of data values (may contain a single-byte
427 * padding at the end)
428 *
429 * Total size is 4+(width * size + 1)/2 16-bit code units.
430 */
buzbee02031b12012-11-23 09:41:35 -0800431void ArmCodegen::GenFillArrayData(CompilationUnit* cu, uint32_t table_offset, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800432{
buzbeefa57c472012-11-21 12:06:18 -0800433 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
buzbeeefc63692012-11-14 16:31:52 -0800434 // Add the table to the list - we'll process it later
buzbeefa57c472012-11-21 12:06:18 -0800435 FillArrayData *tab_rec =
436 static_cast<FillArrayData*>(NewMem(cu, sizeof(FillArrayData), true, kAllocData));
437 tab_rec->table = table;
438 tab_rec->vaddr = cu->current_dalvik_offset;
439 uint16_t width = tab_rec->table[1];
440 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
441 tab_rec->size = (size * width) + 8;
buzbeeefc63692012-11-14 16:31:52 -0800442
buzbeefa57c472012-11-21 12:06:18 -0800443 InsertGrowableList(cu, &cu->fill_array_data, reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -0800444
445 // Making a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800446 FlushAllRegs(cu); /* Everything to home location */
447 LoadValueDirectFixed(cu, rl_src, r0);
448 LoadWordDisp(cu, rARM_SELF, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode),
buzbeeefc63692012-11-14 16:31:52 -0800449 rARM_LR);
450 // Materialize a pointer to the fill data image
buzbeefa57c472012-11-21 12:06:18 -0800451 NewLIR3(cu, kThumb2Adr, r1, 0, reinterpret_cast<uintptr_t>(tab_rec));
452 ClobberCalleeSave(cu);
453 LIR* call_inst = OpReg(cu, kOpBlx, rARM_LR);
454 MarkSafepointPC(cu, call_inst);
buzbeeefc63692012-11-14 16:31:52 -0800455}
456
457/*
458 * Handle simple case (thin lock) inline. If it's complicated, bail
459 * out to the heavyweight lock/unlock routines. We'll use dedicated
460 * registers here in order to be in the right position in case we
buzbeeeaf09bc2012-11-15 14:51:41 -0800461 * to bail to oat[Lock/Unlock]Object(self, object)
buzbeeefc63692012-11-14 16:31:52 -0800462 *
buzbeeeaf09bc2012-11-15 14:51:41 -0800463 * r0 -> self pointer [arg0 for oat[Lock/Unlock]Object
464 * r1 -> object [arg1 for oat[Lock/Unlock]Object
buzbeeefc63692012-11-14 16:31:52 -0800465 * r2 -> intial contents of object->lock, later result of strex
buzbeefa57c472012-11-21 12:06:18 -0800466 * r3 -> self->thread_id
buzbeeefc63692012-11-14 16:31:52 -0800467 * r12 -> allow to be used by utilities as general temp
468 *
469 * The result of the strex is 0 if we acquire the lock.
470 *
471 * See comments in Sync.c for the layout of the lock word.
472 * Of particular interest to this code is the test for the
473 * simple case - which we handle inline. For monitor enter, the
474 * simple case is thin lock, held by no-one. For monitor exit,
475 * the simple case is thin lock, held by the unlocking thread with
476 * a recurse count of 0.
477 *
478 * A minor complication is that there is a field in the lock word
479 * unrelated to locking: the hash state. This field must be ignored, but
480 * preserved.
481 *
482 */
buzbee02031b12012-11-23 09:41:35 -0800483void ArmCodegen::GenMonitorEnter(CompilationUnit* cu, int opt_flags, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800484{
buzbeefa57c472012-11-21 12:06:18 -0800485 FlushAllRegs(cu);
buzbeeefc63692012-11-14 16:31:52 -0800486 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbeefa57c472012-11-21 12:06:18 -0800487 LoadValueDirectFixed(cu, rl_src, r0); // Get obj
488 LockCallTemps(cu); // Prepare for explicit register usage
489 GenNullCheck(cu, rl_src.s_reg_low, r0, opt_flags);
490 LoadWordDisp(cu, rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
491 NewLIR3(cu, kThumb2Ldrex, r1, r0,
buzbeeefc63692012-11-14 16:31:52 -0800492 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
493 // Align owner
buzbeefa57c472012-11-21 12:06:18 -0800494 OpRegImm(cu, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
495 // Is lock unheld on lock or held by us (==thread_id) on unlock?
496 NewLIR4(cu, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1);
497 NewLIR3(cu, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
498 OpRegImm(cu, kOpCmp, r1, 0);
buzbee02031b12012-11-23 09:41:35 -0800499 OpIT(cu, kCondEq, "");
buzbeefa57c472012-11-21 12:06:18 -0800500 NewLIR4(cu, kThumb2Strex, r1, r2, r0,
buzbeeefc63692012-11-14 16:31:52 -0800501 Object::MonitorOffset().Int32Value() >> 2);
buzbeefa57c472012-11-21 12:06:18 -0800502 OpRegImm(cu, kOpCmp, r1, 0);
buzbee02031b12012-11-23 09:41:35 -0800503 OpIT(cu, kCondNe, "T");
buzbeeefc63692012-11-14 16:31:52 -0800504 // Go expensive route - artLockObjectFromCode(self, obj);
buzbeefa57c472012-11-21 12:06:18 -0800505 LoadWordDisp(cu, rARM_SELF, ENTRYPOINT_OFFSET(pLockObjectFromCode), rARM_LR);
506 ClobberCalleeSave(cu);
507 LIR* call_inst = OpReg(cu, kOpBlx, rARM_LR);
508 MarkSafepointPC(cu, call_inst);
509 GenMemBarrier(cu, kLoadLoad);
buzbeeefc63692012-11-14 16:31:52 -0800510}
511
512/*
513 * For monitor unlock, we don't have to use ldrex/strex. Once
514 * we've determined that the lock is thin and that we own it with
515 * a zero recursion count, it's safe to punch it back to the
516 * initial, unlock thin state with a store word.
517 */
buzbee02031b12012-11-23 09:41:35 -0800518void ArmCodegen::GenMonitorExit(CompilationUnit* cu, int opt_flags, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800519{
520 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbeefa57c472012-11-21 12:06:18 -0800521 FlushAllRegs(cu);
522 LoadValueDirectFixed(cu, rl_src, r0); // Get obj
523 LockCallTemps(cu); // Prepare for explicit register usage
524 GenNullCheck(cu, rl_src.s_reg_low, r0, opt_flags);
525 LoadWordDisp(cu, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock
526 LoadWordDisp(cu, rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2);
527 // Is lock unheld on lock or held by us (==thread_id) on unlock?
528 OpRegRegImm(cu, kOpAnd, r3, r1,
buzbeeefc63692012-11-14 16:31:52 -0800529 (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
530 // Align owner
buzbeefa57c472012-11-21 12:06:18 -0800531 OpRegImm(cu, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
532 NewLIR3(cu, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
533 OpRegReg(cu, kOpSub, r1, r2);
buzbee02031b12012-11-23 09:41:35 -0800534 OpIT(cu, kCondEq, "EE");
buzbeefa57c472012-11-21 12:06:18 -0800535 StoreWordDisp(cu, r0, Object::MonitorOffset().Int32Value(), r3);
buzbeeefc63692012-11-14 16:31:52 -0800536 // Go expensive route - UnlockObjectFromCode(obj);
buzbeefa57c472012-11-21 12:06:18 -0800537 LoadWordDisp(cu, rARM_SELF, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rARM_LR);
538 ClobberCalleeSave(cu);
539 LIR* call_inst = OpReg(cu, kOpBlx, rARM_LR);
540 MarkSafepointPC(cu, call_inst);
541 GenMemBarrier(cu, kStoreLoad);
buzbeeefc63692012-11-14 16:31:52 -0800542}
543
544/*
545 * Mark garbage collection card. Skip if the value we're storing is null.
546 */
buzbee02031b12012-11-23 09:41:35 -0800547void ArmCodegen::MarkGCCard(CompilationUnit* cu, int val_reg, int tgt_addr_reg)
buzbeeefc63692012-11-14 16:31:52 -0800548{
buzbeefa57c472012-11-21 12:06:18 -0800549 int reg_card_base = AllocTemp(cu);
550 int reg_card_no = AllocTemp(cu);
551 LIR* branch_over = OpCmpImmBranch(cu, kCondEq, val_reg, 0, NULL);
552 LoadWordDisp(cu, rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base);
553 OpRegRegImm(cu, kOpLsr, reg_card_no, tgt_addr_reg, CardTable::kCardShift);
554 StoreBaseIndexed(cu, reg_card_base, reg_card_no, reg_card_base, 0,
buzbeeefc63692012-11-14 16:31:52 -0800555 kUnsignedByte);
buzbeefa57c472012-11-21 12:06:18 -0800556 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
557 branch_over->target = target;
558 FreeTemp(cu, reg_card_base);
559 FreeTemp(cu, reg_card_no);
buzbeeefc63692012-11-14 16:31:52 -0800560}
561
buzbee02031b12012-11-23 09:41:35 -0800562void ArmCodegen::GenEntrySequence(CompilationUnit* cu, RegLocation* ArgLocs, RegLocation rl_method)
buzbeeefc63692012-11-14 16:31:52 -0800563{
buzbeefa57c472012-11-21 12:06:18 -0800564 int spill_count = cu->num_core_spills + cu->num_fp_spills;
buzbeeefc63692012-11-14 16:31:52 -0800565 /*
566 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
567 * mechanism know so it doesn't try to use any of them when
568 * expanding the frame or flushing. This leaves the utility
569 * code with a single temp: r12. This should be enough.
570 */
buzbeefa57c472012-11-21 12:06:18 -0800571 LockTemp(cu, r0);
572 LockTemp(cu, r1);
573 LockTemp(cu, r2);
574 LockTemp(cu, r3);
buzbeeefc63692012-11-14 16:31:52 -0800575
576 /*
577 * We can safely skip the stack overflow check if we're
578 * a leaf *and* our frame size < fudge factor.
579 */
buzbeefa57c472012-11-21 12:06:18 -0800580 bool skip_overflow_check = ((cu->attrs & METHOD_IS_LEAF) &&
581 (static_cast<size_t>(cu->frame_size) <
buzbeeefc63692012-11-14 16:31:52 -0800582 Thread::kStackOverflowReservedBytes));
buzbeefa57c472012-11-21 12:06:18 -0800583 NewLIR0(cu, kPseudoMethodEntry);
584 if (!skip_overflow_check) {
buzbeeefc63692012-11-14 16:31:52 -0800585 /* Load stack limit */
buzbeefa57c472012-11-21 12:06:18 -0800586 LoadWordDisp(cu, rARM_SELF, Thread::StackEndOffset().Int32Value(), r12);
buzbeeefc63692012-11-14 16:31:52 -0800587 }
588 /* Spill core callee saves */
buzbeefa57c472012-11-21 12:06:18 -0800589 NewLIR1(cu, kThumb2Push, cu->core_spill_mask);
buzbeeefc63692012-11-14 16:31:52 -0800590 /* Need to spill any FP regs? */
buzbeefa57c472012-11-21 12:06:18 -0800591 if (cu->num_fp_spills) {
buzbeeefc63692012-11-14 16:31:52 -0800592 /*
593 * NOTE: fp spills are a little different from core spills in that
594 * they are pushed as a contiguous block. When promoting from
595 * the fp set, we must allocate all singles from s16..highest-promoted
596 */
buzbeefa57c472012-11-21 12:06:18 -0800597 NewLIR1(cu, kThumb2VPushCS, cu->num_fp_spills);
buzbeeefc63692012-11-14 16:31:52 -0800598 }
buzbeefa57c472012-11-21 12:06:18 -0800599 if (!skip_overflow_check) {
600 OpRegRegImm(cu, kOpSub, rARM_LR, rARM_SP, cu->frame_size - (spill_count * 4));
601 GenRegRegCheck(cu, kCondCc, rARM_LR, r12, kThrowStackOverflow);
602 OpRegCopy(cu, rARM_SP, rARM_LR); // Establish stack
buzbeeefc63692012-11-14 16:31:52 -0800603 } else {
buzbeefa57c472012-11-21 12:06:18 -0800604 OpRegImm(cu, kOpSub, rARM_SP, cu->frame_size - (spill_count * 4));
buzbeeefc63692012-11-14 16:31:52 -0800605 }
606
buzbeefa57c472012-11-21 12:06:18 -0800607 FlushIns(cu, ArgLocs, rl_method);
buzbeeefc63692012-11-14 16:31:52 -0800608
buzbeefa57c472012-11-21 12:06:18 -0800609 FreeTemp(cu, r0);
610 FreeTemp(cu, r1);
611 FreeTemp(cu, r2);
612 FreeTemp(cu, r3);
buzbeeefc63692012-11-14 16:31:52 -0800613}
614
buzbee02031b12012-11-23 09:41:35 -0800615void ArmCodegen::GenExitSequence(CompilationUnit* cu)
buzbeeefc63692012-11-14 16:31:52 -0800616{
buzbeefa57c472012-11-21 12:06:18 -0800617 int spill_count = cu->num_core_spills + cu->num_fp_spills;
buzbeeefc63692012-11-14 16:31:52 -0800618 /*
619 * In the exit path, r0/r1 are live - make sure they aren't
620 * allocated by the register utilities as temps.
621 */
buzbeefa57c472012-11-21 12:06:18 -0800622 LockTemp(cu, r0);
623 LockTemp(cu, r1);
buzbeeefc63692012-11-14 16:31:52 -0800624
buzbeefa57c472012-11-21 12:06:18 -0800625 NewLIR0(cu, kPseudoMethodExit);
626 OpRegImm(cu, kOpAdd, rARM_SP, cu->frame_size - (spill_count * 4));
buzbeeefc63692012-11-14 16:31:52 -0800627 /* Need to restore any FP callee saves? */
buzbeefa57c472012-11-21 12:06:18 -0800628 if (cu->num_fp_spills) {
629 NewLIR1(cu, kThumb2VPopCS, cu->num_fp_spills);
buzbeeefc63692012-11-14 16:31:52 -0800630 }
buzbeefa57c472012-11-21 12:06:18 -0800631 if (cu->core_spill_mask & (1 << rARM_LR)) {
buzbeeefc63692012-11-14 16:31:52 -0800632 /* Unspill rARM_LR to rARM_PC */
buzbeefa57c472012-11-21 12:06:18 -0800633 cu->core_spill_mask &= ~(1 << rARM_LR);
634 cu->core_spill_mask |= (1 << rARM_PC);
buzbeeefc63692012-11-14 16:31:52 -0800635 }
buzbeefa57c472012-11-21 12:06:18 -0800636 NewLIR1(cu, kThumb2Pop, cu->core_spill_mask);
637 if (!(cu->core_spill_mask & (1 << rARM_PC))) {
buzbeeefc63692012-11-14 16:31:52 -0800638 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbeefa57c472012-11-21 12:06:18 -0800639 NewLIR1(cu, kThumbBx, rARM_LR);
buzbeeefc63692012-11-14 16:31:52 -0800640 }
641}
642
643} // namespace art