blob: d60be72c3167b23d58c9b1c5abc965b1093d06f6 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
19#include "codegen_x86.h"
20#include "dex/quick/mir_to_lir-inl.h"
21#include "x86_lir.h"
22
23namespace art {
24
25void X86Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir,
26 SpecialCaseHandler special_case)
27{
28 // TODO
29}
30
31/*
32 * The sparse table in the literal pool is an array of <key,displacement>
33 * pairs.
34 */
35void X86Mir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
36 RegLocation rl_src)
37{
38 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
39 if (cu_->verbose) {
40 DumpSparseSwitchTable(table);
41 }
42 int entries = table[1];
43 const int* keys = reinterpret_cast<const int*>(&table[2]);
44 const int* targets = &keys[entries];
45 rl_src = LoadValue(rl_src, kCoreReg);
46 for (int i = 0; i < entries; i++) {
47 int key = keys[i];
48 BasicBlock* case_block =
49 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
50 OpCmpImmBranch(kCondEq, rl_src.low_reg, key,
51 &block_label_list_[case_block->id]);
52 }
53}
54
55/*
56 * Code pattern will look something like:
57 *
58 * mov r_val, ..
59 * call 0
60 * pop r_start_of_method
61 * sub r_start_of_method, ..
62 * mov r_key_reg, r_val
63 * sub r_key_reg, low_key
64 * cmp r_key_reg, size-1 ; bound check
65 * ja done
66 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
67 * add r_start_of_method, r_disp
68 * jmp r_start_of_method
69 * done:
70 */
71void X86Mir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
72 RegLocation rl_src)
73{
74 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
75 if (cu_->verbose) {
76 DumpPackedSwitchTable(table);
77 }
78 // Add the table to the list - we'll process it later
79 SwitchTable *tab_rec =
80 static_cast<SwitchTable *>(arena_->NewMem(sizeof(SwitchTable), true,
81 ArenaAllocator::kAllocData));
82 tab_rec->table = table;
83 tab_rec->vaddr = current_dalvik_offset_;
84 int size = table[1];
85 tab_rec->targets = static_cast<LIR**>(arena_->NewMem(size * sizeof(LIR*), true,
86 ArenaAllocator::kAllocLIR));
87 switch_tables_.Insert(tab_rec);
88
89 // Get the switch value
90 rl_src = LoadValue(rl_src, kCoreReg);
91 int start_of_method_reg = AllocTemp();
92 // Materialize a pointer to the switch table
93 //NewLIR0(kX86Bkpt);
94 NewLIR1(kX86StartOfMethod, start_of_method_reg);
95 int low_key = s4FromSwitchData(&table[2]);
96 int keyReg;
97 // Remove the bias, if necessary
98 if (low_key == 0) {
99 keyReg = rl_src.low_reg;
100 } else {
101 keyReg = AllocTemp();
102 OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key);
103 }
104 // Bounds check - if < 0 or >= size continue following switch
105 OpRegImm(kOpCmp, keyReg, size-1);
106 LIR* branch_over = OpCondBranch(kCondHi, NULL);
107
108 // Load the displacement from the switch table
109 int disp_reg = AllocTemp();
110 NewLIR5(kX86PcRelLoadRA, disp_reg, start_of_method_reg, keyReg, 2,
111 reinterpret_cast<uintptr_t>(tab_rec));
112 // Add displacement to start of method
113 OpRegReg(kOpAdd, start_of_method_reg, disp_reg);
114 // ..and go!
115 LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg);
116 tab_rec->anchor = switch_branch;
117
118 /* branch_over target here */
119 LIR* target = NewLIR0(kPseudoTargetLabel);
120 branch_over->target = target;
121}
122
123/*
124 * Array data table format:
125 * ushort ident = 0x0300 magic value
126 * ushort width width of each element in the table
127 * uint size number of elements in the table
128 * ubyte data[size*width] table of data values (may contain a single-byte
129 * padding at the end)
130 *
131 * Total size is 4+(width * size + 1)/2 16-bit code units.
132 */
133void X86Mir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src)
134{
135 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
136 // Add the table to the list - we'll process it later
137 FillArrayData *tab_rec =
138 static_cast<FillArrayData*>(arena_->NewMem(sizeof(FillArrayData), true,
139 ArenaAllocator::kAllocData));
140 tab_rec->table = table;
141 tab_rec->vaddr = current_dalvik_offset_;
142 uint16_t width = tab_rec->table[1];
143 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
144 tab_rec->size = (size * width) + 8;
145
146 fill_array_data_.Insert(tab_rec);
147
148 // Making a call - use explicit registers
149 FlushAllRegs(); /* Everything to home location */
150 LoadValueDirectFixed(rl_src, rX86_ARG0);
151 // Materialize a pointer to the fill data image
152 NewLIR1(kX86StartOfMethod, rX86_ARG2);
153 NewLIR2(kX86PcRelAdr, rX86_ARG1, reinterpret_cast<uintptr_t>(tab_rec));
154 NewLIR2(kX86Add32RR, rX86_ARG1, rX86_ARG2);
155 CallRuntimeHelperRegReg(ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), rX86_ARG0,
156 rX86_ARG1, true);
157}
158
159void X86Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src)
160{
161 FlushAllRegs();
162 LoadValueDirectFixed(rl_src, rCX); // Get obj
163 LockCallTemps(); // Prepare for explicit register usage
164 GenNullCheck(rl_src.s_reg_low, rCX, opt_flags);
165 // If lock is unheld, try to grab it quickly with compare and exchange
166 // TODO: copy and clear hash state?
167 NewLIR2(kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
168 NewLIR2(kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
169 NewLIR2(kX86Xor32RR, rAX, rAX);
170 NewLIR3(kX86LockCmpxchgMR, rCX, mirror::Object::MonitorOffset().Int32Value(), rDX);
171 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondEq);
172 // If lock is held, go the expensive route - artLockObjectFromCode(self, obj);
173 CallRuntimeHelperReg(ENTRYPOINT_OFFSET(pLockObjectFromCode), rCX, true);
174 branch->target = NewLIR0(kPseudoTargetLabel);
175}
176
177void X86Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src)
178{
179 FlushAllRegs();
180 LoadValueDirectFixed(rl_src, rAX); // Get obj
181 LockCallTemps(); // Prepare for explicit register usage
182 GenNullCheck(rl_src.s_reg_low, rAX, opt_flags);
183 // If lock is held by the current thread, clear it to quickly release it
184 // TODO: clear hash state?
185 NewLIR2(kX86Mov32RT, rDX, Thread::ThinLockIdOffset().Int32Value());
186 NewLIR2(kX86Sal32RI, rDX, LW_LOCK_OWNER_SHIFT);
187 NewLIR3(kX86Mov32RM, rCX, rAX, mirror::Object::MonitorOffset().Int32Value());
188 OpRegReg(kOpSub, rCX, rDX);
189 LIR* branch = NewLIR2(kX86Jcc8, 0, kX86CondNe);
190 NewLIR3(kX86Mov32MR, rAX, mirror::Object::MonitorOffset().Int32Value(), rCX);
191 LIR* branch2 = NewLIR1(kX86Jmp8, 0);
192 branch->target = NewLIR0(kPseudoTargetLabel);
193 // Otherwise, go the expensive route - UnlockObjectFromCode(obj);
194 CallRuntimeHelperReg(ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rAX, true);
195 branch2->target = NewLIR0(kPseudoTargetLabel);
196}
197
198void X86Mir2Lir::GenMoveException(RegLocation rl_dest)
199{
200 int ex_offset = Thread::ExceptionOffset().Int32Value();
201 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
202 NewLIR2(kX86Mov32RT, rl_result.low_reg, ex_offset);
203 NewLIR2(kX86Mov32TI, ex_offset, 0);
204 StoreValue(rl_dest, rl_result);
205}
206
207/*
208 * Mark garbage collection card. Skip if the value we're storing is null.
209 */
210void X86Mir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg)
211{
212 int reg_card_base = AllocTemp();
213 int reg_card_no = AllocTemp();
214 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
215 NewLIR2(kX86Mov32RT, reg_card_base, Thread::CardTableOffset().Int32Value());
216 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
217 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
218 kUnsignedByte);
219 LIR* target = NewLIR0(kPseudoTargetLabel);
220 branch_over->target = target;
221 FreeTemp(reg_card_base);
222 FreeTemp(reg_card_no);
223}
224
225void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method)
226{
227 /*
228 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
229 * allocation mechanism know so it doesn't try to use any of them when
230 * expanding the frame or flushing. This leaves the utility
231 * code with no spare temps.
232 */
233 LockTemp(rX86_ARG0);
234 LockTemp(rX86_ARG1);
235 LockTemp(rX86_ARG2);
236
237 /* Build frame, return address already on stack */
238 OpRegImm(kOpSub, rX86_SP, frame_size_ - 4);
239
240 /*
241 * We can safely skip the stack overflow check if we're
242 * a leaf *and* our frame size < fudge factor.
243 */
244 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
245 (static_cast<size_t>(frame_size_) <
246 Thread::kStackOverflowReservedBytes));
247 NewLIR0(kPseudoMethodEntry);
248 /* Spill core callee saves */
249 SpillCoreRegs();
250 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
251 DCHECK_EQ(num_fp_spills_, 0);
252 if (!skip_overflow_check) {
253 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
254 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
255 OpRegThreadMem(kOpCmp, rX86_SP, Thread::StackEndOffset().Int32Value());
256 OpCondBranch(kCondUlt, tgt);
257 // Remember branch target - will process later
258 throw_launchpads_.Insert(tgt);
259 }
260
261 FlushIns(ArgLocs, rl_method);
262
263 FreeTemp(rX86_ARG0);
264 FreeTemp(rX86_ARG1);
265 FreeTemp(rX86_ARG2);
266}
267
268void X86Mir2Lir::GenExitSequence() {
269 /*
270 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
271 * allocated by the register utilities as temps.
272 */
273 LockTemp(rX86_RET0);
274 LockTemp(rX86_RET1);
275
276 NewLIR0(kPseudoMethodExit);
277 UnSpillCoreRegs();
278 /* Remove frame except for return address */
279 OpRegImm(kOpAdd, rX86_SP, frame_size_ - 4);
280 NewLIR0(kX86Ret);
281}
282
283} // namespace art