blob: e24831dcd8e1b9df3bc41b3d828018593a9ec97d [file] [log] [blame]
buzbeeefc63692012-11-14 16:31:52 -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
17/* This file contains codegen for the X86 ISA */
18
buzbee1bc37c62012-11-20 13:35:41 -080019#include "x86_lir.h"
20#include "../codegen_util.h"
21#include "../ralloc_util.h"
22
buzbeeefc63692012-11-14 16:31:52 -080023namespace art {
24
buzbeefa57c472012-11-21 12:06:18 -080025void GenSpecialCase(CompilationUnit* cu, BasicBlock* bb, MIR* mir,
26 SpecialCaseHandler special_case)
buzbeeefc63692012-11-14 16:31:52 -080027{
28 // TODO
29}
30
31/*
32 * The sparse table in the literal pool is an array of <key,displacement>
33 * pairs.
34 */
buzbeefa57c472012-11-21 12:06:18 -080035BasicBlock *FindBlock(CompilationUnit* cu, unsigned int code_offset,
36 bool split, bool create, BasicBlock** immed_pred_block_p);
37void GenSparseSwitch(CompilationUnit* cu, uint32_t table_offset,
38 RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -080039{
buzbeefa57c472012-11-21 12:06:18 -080040 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
41 if (cu->verbose) {
buzbee52a77fc2012-11-20 19:50:46 -080042 DumpSparseSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -080043 }
44 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -080045 const int* keys = reinterpret_cast<const int*>(&table[2]);
46 const int* targets = &keys[entries];
buzbeefa57c472012-11-21 12:06:18 -080047 rl_src = LoadValue(cu, rl_src, kCoreReg);
buzbeeefc63692012-11-14 16:31:52 -080048 for (int i = 0; i < entries; i++) {
49 int key = keys[i];
buzbeefa57c472012-11-21 12:06:18 -080050 BasicBlock* case_block = FindBlock(cu,
51 cu->current_dalvik_offset + targets[i],
buzbeeefc63692012-11-14 16:31:52 -080052 false, false, NULL);
buzbeefa57c472012-11-21 12:06:18 -080053 LIR* label_list = cu->block_label_list;
54 OpCmpImmBranch(cu, kCondEq, rl_src.low_reg, key,
55 &label_list[case_block->id]);
buzbeeefc63692012-11-14 16:31:52 -080056 }
57}
58
59/*
60 * Code pattern will look something like:
61 *
buzbeefa57c472012-11-21 12:06:18 -080062 * mov r_val, ..
buzbeeefc63692012-11-14 16:31:52 -080063 * call 0
buzbeefa57c472012-11-21 12:06:18 -080064 * pop r_start_of_method
65 * sub r_start_of_method, ..
66 * mov r_key_reg, r_val
67 * sub r_key_reg, low_key
68 * cmp r_key_reg, size-1 ; bound check
buzbeeefc63692012-11-14 16:31:52 -080069 * ja done
buzbeefa57c472012-11-21 12:06:18 -080070 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
71 * add r_start_of_method, r_disp
72 * jmp r_start_of_method
buzbeeefc63692012-11-14 16:31:52 -080073 * done:
74 */
buzbeefa57c472012-11-21 12:06:18 -080075void GenPackedSwitch(CompilationUnit* cu, uint32_t table_offset,
76 RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -080077{
buzbeefa57c472012-11-21 12:06:18 -080078 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
79 if (cu->verbose) {
buzbee52a77fc2012-11-20 19:50:46 -080080 DumpPackedSwitchTable(table);
buzbeeefc63692012-11-14 16:31:52 -080081 }
82 // Add the table to the list - we'll process it later
buzbeefa57c472012-11-21 12:06:18 -080083 SwitchTable *tab_rec =
84 static_cast<SwitchTable *>(NewMem(cu, sizeof(SwitchTable), true, kAllocData));
85 tab_rec->table = table;
86 tab_rec->vaddr = cu->current_dalvik_offset;
buzbeeefc63692012-11-14 16:31:52 -080087 int size = table[1];
buzbeefa57c472012-11-21 12:06:18 -080088 tab_rec->targets = static_cast<LIR**>(NewMem(cu, size * sizeof(LIR*), true, kAllocLIR));
89 InsertGrowableList(cu, &cu->switch_tables, reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -080090
91 // Get the switch value
buzbeefa57c472012-11-21 12:06:18 -080092 rl_src = LoadValue(cu, rl_src, kCoreReg);
93 int start_of_method_reg = AllocTemp(cu);
buzbeeefc63692012-11-14 16:31:52 -080094 // Materialize a pointer to the switch table
buzbeefa57c472012-11-21 12:06:18 -080095 //NewLIR0(cu, kX86Bkpt);
96 NewLIR1(cu, kX86StartOfMethod, start_of_method_reg);
97 int low_key = s4FromSwitchData(&table[2]);
buzbeeefc63692012-11-14 16:31:52 -080098 int keyReg;
99 // Remove the bias, if necessary
buzbeefa57c472012-11-21 12:06:18 -0800100 if (low_key == 0) {
101 keyReg = rl_src.low_reg;
buzbeeefc63692012-11-14 16:31:52 -0800102 } else {
buzbeefa57c472012-11-21 12:06:18 -0800103 keyReg = AllocTemp(cu);
104 OpRegRegImm(cu, kOpSub, keyReg, rl_src.low_reg, low_key);
buzbeeefc63692012-11-14 16:31:52 -0800105 }
106 // Bounds check - if < 0 or >= size continue following switch
buzbeefa57c472012-11-21 12:06:18 -0800107 OpRegImm(cu, kOpCmp, keyReg, size-1);
108 LIR* branch_over = OpCondBranch(cu, kCondHi, NULL);
buzbeeefc63692012-11-14 16:31:52 -0800109
110 // Load the displacement from the switch table
buzbeefa57c472012-11-21 12:06:18 -0800111 int disp_reg = AllocTemp(cu);
112 NewLIR5(cu, kX86PcRelLoadRA, disp_reg, start_of_method_reg, keyReg, 2,
113 reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -0800114 // Add displacement to start of method
buzbeefa57c472012-11-21 12:06:18 -0800115 OpRegReg(cu, kOpAdd, start_of_method_reg, disp_reg);
buzbeeefc63692012-11-14 16:31:52 -0800116 // ..and go!
buzbeefa57c472012-11-21 12:06:18 -0800117 LIR* switch_branch = NewLIR1(cu, kX86JmpR, start_of_method_reg);
118 tab_rec->anchor = switch_branch;
buzbeeefc63692012-11-14 16:31:52 -0800119
buzbeefa57c472012-11-21 12:06:18 -0800120 /* branch_over target here */
121 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
122 branch_over->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800123}
124
buzbeefa57c472012-11-21 12:06:18 -0800125void CallRuntimeHelperRegReg(CompilationUnit* cu, int helper_offset,
126 int arg0, int arg1, bool safepoint_pc);
buzbeeefc63692012-11-14 16:31:52 -0800127/*
128 * Array data table format:
129 * ushort ident = 0x0300 magic value
130 * ushort width width of each element in the table
131 * uint size number of elements in the table
132 * ubyte data[size*width] table of data values (may contain a single-byte
133 * padding at the end)
134 *
135 * Total size is 4+(width * size + 1)/2 16-bit code units.
136 */
buzbeefa57c472012-11-21 12:06:18 -0800137void GenFillArrayData(CompilationUnit* cu, uint32_t table_offset,
138 RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800139{
buzbeefa57c472012-11-21 12:06:18 -0800140 const uint16_t* table = cu->insns + cu->current_dalvik_offset + table_offset;
buzbeeefc63692012-11-14 16:31:52 -0800141 // Add the table to the list - we'll process it later
buzbeefa57c472012-11-21 12:06:18 -0800142 FillArrayData *tab_rec =
143 static_cast<FillArrayData*>(NewMem(cu, sizeof(FillArrayData), true, kAllocData));
144 tab_rec->table = table;
145 tab_rec->vaddr = cu->current_dalvik_offset;
146 uint16_t width = tab_rec->table[1];
147 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
148 tab_rec->size = (size * width) + 8;
buzbeeefc63692012-11-14 16:31:52 -0800149
buzbeefa57c472012-11-21 12:06:18 -0800150 InsertGrowableList(cu, &cu->fill_array_data, reinterpret_cast<uintptr_t>(tab_rec));
buzbeeefc63692012-11-14 16:31:52 -0800151
152 // Making a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800153 FlushAllRegs(cu); /* Everything to home location */
154 LoadValueDirectFixed(cu, rl_src, rX86_ARG0);
buzbeeefc63692012-11-14 16:31:52 -0800155 // Materialize a pointer to the fill data image
buzbeefa57c472012-11-21 12:06:18 -0800156 NewLIR1(cu, kX86StartOfMethod, rX86_ARG2);
157 NewLIR2(cu, kX86PcRelAdr, rX86_ARG1, reinterpret_cast<uintptr_t>(tab_rec));
158 NewLIR2(cu, kX86Add32RR, rX86_ARG1, rX86_ARG2);
159 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), rX86_ARG0,
buzbeecbd6d442012-11-17 14:11:25 -0800160 rX86_ARG1, true);
buzbeeefc63692012-11-14 16:31:52 -0800161}
162
buzbeefa57c472012-11-21 12:06:18 -0800163void GenMonitorEnter(CompilationUnit* cu, int opt_flags, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800164{
buzbeefa57c472012-11-21 12:06:18 -0800165 FlushAllRegs(cu);
166 LoadValueDirectFixed(cu, rl_src, rCX); // Get obj
167 LockCallTemps(cu); // Prepare for explicit register usage
168 GenNullCheck(cu, rl_src.s_reg_low, rCX, opt_flags);
buzbeeefc63692012-11-14 16:31:52 -0800169 // If lock is unheld, try to grab it quickly with compare and exchange
170 // TODO: copy and clear hash state?
buzbeefa57c472012-11-21 12:06:18 -0800171 NewLIR2(cu, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
172 NewLIR2(cu, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
173 NewLIR2(cu, kX86Xor32RR, rAX, rAX);
174 NewLIR3(cu, kX86LockCmpxchgMR, rCX, Object::MonitorOffset().Int32Value(), rDX);
175 LIR* branch = NewLIR2(cu, kX86Jcc8, 0, kX86CondEq);
buzbeeefc63692012-11-14 16:31:52 -0800176 // If lock is held, go the expensive route - artLockObjectFromCode(self, obj);
buzbeefa57c472012-11-21 12:06:18 -0800177 CallRuntimeHelperReg(cu, ENTRYPOINT_OFFSET(pLockObjectFromCode), rCX, true);
178 branch->target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800179}
180
buzbeefa57c472012-11-21 12:06:18 -0800181void GenMonitorExit(CompilationUnit* cu, int opt_flags, RegLocation rl_src)
buzbeeefc63692012-11-14 16:31:52 -0800182{
buzbeefa57c472012-11-21 12:06:18 -0800183 FlushAllRegs(cu);
184 LoadValueDirectFixed(cu, rl_src, rAX); // Get obj
185 LockCallTemps(cu); // Prepare for explicit register usage
186 GenNullCheck(cu, rl_src.s_reg_low, rAX, opt_flags);
buzbeeefc63692012-11-14 16:31:52 -0800187 // If lock is held by the current thread, clear it to quickly release it
188 // TODO: clear hash state?
buzbeefa57c472012-11-21 12:06:18 -0800189 NewLIR2(cu, kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
190 NewLIR2(cu, kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
191 NewLIR3(cu, kX86Mov32RM, rCX, rAX, Object::MonitorOffset().Int32Value());
192 OpRegReg(cu, kOpSub, rCX, rDX);
193 LIR* branch = NewLIR2(cu, kX86Jcc8, 0, kX86CondNe);
194 NewLIR3(cu, kX86Mov32MR, rAX, Object::MonitorOffset().Int32Value(), rCX);
195 LIR* branch2 = NewLIR1(cu, kX86Jmp8, 0);
196 branch->target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800197 // Otherwise, go the expensive route - UnlockObjectFromCode(obj);
buzbeefa57c472012-11-21 12:06:18 -0800198 CallRuntimeHelperReg(cu, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rAX, true);
199 branch2->target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeefc63692012-11-14 16:31:52 -0800200}
201
202/*
203 * Mark garbage collection card. Skip if the value we're storing is null.
204 */
buzbeefa57c472012-11-21 12:06:18 -0800205void MarkGCCard(CompilationUnit* cu, int val_reg, int tgt_addr_reg)
buzbeeefc63692012-11-14 16:31:52 -0800206{
buzbeefa57c472012-11-21 12:06:18 -0800207 int reg_card_base = AllocTemp(cu);
208 int reg_card_no = AllocTemp(cu);
209 LIR* branch_over = OpCmpImmBranch(cu, kCondEq, val_reg, 0, NULL);
210 NewLIR2(cu, kX86Mov32RT, reg_card_base, Thread::CardTableOffset().Int32Value());
211 OpRegRegImm(cu, kOpLsr, reg_card_no, tgt_addr_reg, CardTable::kCardShift);
212 StoreBaseIndexed(cu, reg_card_base, reg_card_no, reg_card_base, 0,
buzbeeefc63692012-11-14 16:31:52 -0800213 kUnsignedByte);
buzbeefa57c472012-11-21 12:06:18 -0800214 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
215 branch_over->target = target;
216 FreeTemp(cu, reg_card_base);
217 FreeTemp(cu, reg_card_no);
buzbeeefc63692012-11-14 16:31:52 -0800218}
219
buzbeefa57c472012-11-21 12:06:18 -0800220void GenEntrySequence(CompilationUnit* cu, RegLocation* ArgLocs,
221 RegLocation rl_method)
buzbeeefc63692012-11-14 16:31:52 -0800222{
223 /*
224 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
225 * allocation mechanism know so it doesn't try to use any of them when
226 * expanding the frame or flushing. This leaves the utility
227 * code with no spare temps.
228 */
buzbeefa57c472012-11-21 12:06:18 -0800229 LockTemp(cu, rX86_ARG0);
230 LockTemp(cu, rX86_ARG1);
231 LockTemp(cu, rX86_ARG2);
buzbeeefc63692012-11-14 16:31:52 -0800232
233 /* Build frame, return address already on stack */
buzbeefa57c472012-11-21 12:06:18 -0800234 OpRegImm(cu, kOpSub, rX86_SP, cu->frame_size - 4);
buzbeeefc63692012-11-14 16:31:52 -0800235
236 /*
237 * We can safely skip the stack overflow check if we're
238 * a leaf *and* our frame size < fudge factor.
239 */
buzbeefa57c472012-11-21 12:06:18 -0800240 bool skip_overflow_check = ((cu->attrs & METHOD_IS_LEAF) &&
241 (static_cast<size_t>(cu->frame_size) <
buzbeeefc63692012-11-14 16:31:52 -0800242 Thread::kStackOverflowReservedBytes));
buzbeefa57c472012-11-21 12:06:18 -0800243 NewLIR0(cu, kPseudoMethodEntry);
buzbeeefc63692012-11-14 16:31:52 -0800244 /* Spill core callee saves */
buzbeefa57c472012-11-21 12:06:18 -0800245 SpillCoreRegs(cu);
buzbeeefc63692012-11-14 16:31:52 -0800246 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
buzbeefa57c472012-11-21 12:06:18 -0800247 DCHECK_EQ(cu->num_fp_spills, 0);
248 if (!skip_overflow_check) {
buzbeeefc63692012-11-14 16:31:52 -0800249 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
buzbeefa57c472012-11-21 12:06:18 -0800250 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
251 OpRegThreadMem(cu, kOpCmp, rX86_SP, Thread::StackEndOffset().Int32Value());
252 OpCondBranch(cu, kCondUlt, tgt);
buzbeeefc63692012-11-14 16:31:52 -0800253 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -0800254 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
buzbeeefc63692012-11-14 16:31:52 -0800255 }
256
buzbeefa57c472012-11-21 12:06:18 -0800257 FlushIns(cu, ArgLocs, rl_method);
buzbeeefc63692012-11-14 16:31:52 -0800258
buzbeefa57c472012-11-21 12:06:18 -0800259 FreeTemp(cu, rX86_ARG0);
260 FreeTemp(cu, rX86_ARG1);
261 FreeTemp(cu, rX86_ARG2);
buzbeeefc63692012-11-14 16:31:52 -0800262}
263
buzbeefa57c472012-11-21 12:06:18 -0800264void GenExitSequence(CompilationUnit* cu) {
buzbeeefc63692012-11-14 16:31:52 -0800265 /*
266 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
267 * allocated by the register utilities as temps.
268 */
buzbeefa57c472012-11-21 12:06:18 -0800269 LockTemp(cu, rX86_RET0);
270 LockTemp(cu, rX86_RET1);
buzbeeefc63692012-11-14 16:31:52 -0800271
buzbeefa57c472012-11-21 12:06:18 -0800272 NewLIR0(cu, kPseudoMethodExit);
273 UnSpillCoreRegs(cu);
buzbeeefc63692012-11-14 16:31:52 -0800274 /* Remove frame except for return address */
buzbeefa57c472012-11-21 12:06:18 -0800275 OpRegImm(cu, kOpAdd, rX86_SP, cu->frame_size - 4);
276 NewLIR0(cu, kX86Ret);
buzbeeefc63692012-11-14 16:31:52 -0800277}
278
279} // namespace art