blob: 2bd2caaff51f83993ca7352f2daf7184b836224f [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
Brian Carlstrom7940e442013-07-12 13:46:57 -070025/*
26 * The sparse table in the literal pool is an array of <key,displacement>
27 * pairs.
28 */
buzbee0d829482013-10-11 15:24:55 -070029void X86Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
32 if (cu_->verbose) {
33 DumpSparseSwitchTable(table);
34 }
35 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -070036 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
37 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 rl_src = LoadValue(rl_src, kCoreReg);
39 for (int i = 0; i < entries; i++) {
40 int key = keys[i];
41 BasicBlock* case_block =
42 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
buzbee2700f7e2014-03-07 09:46:20 -080043 OpCmpImmBranch(kCondEq, rl_src.reg, key, &block_label_list_[case_block->id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 }
45}
46
47/*
48 * Code pattern will look something like:
49 *
50 * mov r_val, ..
51 * call 0
52 * pop r_start_of_method
53 * sub r_start_of_method, ..
54 * mov r_key_reg, r_val
55 * sub r_key_reg, low_key
56 * cmp r_key_reg, size-1 ; bound check
57 * ja done
58 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
59 * add r_start_of_method, r_disp
60 * jmp r_start_of_method
61 * done:
62 */
buzbee0d829482013-10-11 15:24:55 -070063void X86Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
66 if (cu_->verbose) {
67 DumpPackedSwitchTable(table);
68 }
69 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -070070 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000071 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 tab_rec->table = table;
73 tab_rec->vaddr = current_dalvik_offset_;
74 int size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070075 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000076 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 switch_tables_.Insert(tab_rec);
78
79 // Get the switch value
80 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081 // NewLIR0(kX86Bkpt);
Mark Mendell67c39c42014-01-31 17:28:00 -080082
83 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080084 RegStorage start_of_method_reg;
Mark Mendell67c39c42014-01-31 17:28:00 -080085 if (base_of_code_ != nullptr) {
86 // We can use the saved value.
87 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
88 rl_method = LoadValue(rl_method, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080089 start_of_method_reg = rl_method.reg;
Mark Mendell55d0eac2014-02-06 11:02:52 -080090 store_method_addr_used_ = true;
Mark Mendell67c39c42014-01-31 17:28:00 -080091 } else {
92 start_of_method_reg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -080093 NewLIR1(kX86StartOfMethod, start_of_method_reg.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -080094 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -080096 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 // Remove the bias, if necessary
98 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -080099 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 } else {
101 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800102 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 }
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
buzbee2700f7e2014-03-07 09:46:20 -0800109 RegStorage disp_reg = AllocTemp();
110 NewLIR5(kX86PcRelLoadRA, disp_reg.GetReg(), start_of_method_reg.GetReg(), keyReg.GetReg(), 2, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 // Add displacement to start of method
112 OpRegReg(kOpAdd, start_of_method_reg, disp_reg);
113 // ..and go!
buzbee2700f7e2014-03-07 09:46:20 -0800114 LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 tab_rec->anchor = switch_branch;
116
117 /* branch_over target here */
118 LIR* target = NewLIR0(kPseudoTargetLabel);
119 branch_over->target = target;
120}
121
122/*
123 * Array data table format:
124 * ushort ident = 0x0300 magic value
125 * ushort width width of each element in the table
126 * uint size number of elements in the table
127 * ubyte data[size*width] table of data values (may contain a single-byte
128 * padding at the end)
129 *
130 * Total size is 4+(width * size + 1)/2 16-bit code units.
131 */
buzbee0d829482013-10-11 15:24:55 -0700132void X86Mir2Lir::GenFillArrayData(DexOffset table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
134 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -0700135 FillArrayData* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000136 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 tab_rec->table = table;
138 tab_rec->vaddr = current_dalvik_offset_;
139 uint16_t width = tab_rec->table[1];
140 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
141 tab_rec->size = (size * width) + 8;
142
143 fill_array_data_.Insert(tab_rec);
144
145 // Making a call - use explicit registers
146 FlushAllRegs(); /* Everything to home location */
buzbee2700f7e2014-03-07 09:46:20 -0800147 LoadValueDirectFixed(rl_src, rs_rX86_ARG0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 // Materialize a pointer to the fill data image
Mark Mendell67c39c42014-01-31 17:28:00 -0800149 if (base_of_code_ != nullptr) {
150 // We can use the saved value.
151 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
buzbee2700f7e2014-03-07 09:46:20 -0800152 LoadValueDirect(rl_method, rs_rX86_ARG2);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800153 store_method_addr_used_ = true;
Mark Mendell67c39c42014-01-31 17:28:00 -0800154 } else {
155 NewLIR1(kX86StartOfMethod, rX86_ARG2);
156 }
buzbee0d829482013-10-11 15:24:55 -0700157 NewLIR2(kX86PcRelAdr, rX86_ARG1, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 NewLIR2(kX86Add32RR, rX86_ARG1, rX86_ARG2);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700159 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pHandleFillArrayData), rs_rX86_ARG0,
buzbee2700f7e2014-03-07 09:46:20 -0800160 rs_rX86_ARG1, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161}
162
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700163void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700164 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000166 NewLIR2(kX86Mov32RT, rl_result.reg.GetReg(), ex_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 NewLIR2(kX86Mov32TI, ex_offset, 0);
168 StoreValue(rl_dest, rl_result);
169}
170
171/*
172 * Mark garbage collection card. Skip if the value we're storing is null.
173 */
buzbee2700f7e2014-03-07 09:46:20 -0800174void X86Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
175 RegStorage reg_card_base = AllocTemp();
176 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700178 NewLIR2(kX86Mov32RT, reg_card_base.GetReg(), Thread::CardTableOffset<4>().Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800180 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 LIR* target = NewLIR0(kPseudoTargetLabel);
182 branch_over->target = target;
183 FreeTemp(reg_card_base);
184 FreeTemp(reg_card_no);
185}
186
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700187void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 /*
189 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
190 * allocation mechanism know so it doesn't try to use any of them when
191 * expanding the frame or flushing. This leaves the utility
192 * code with no spare temps.
193 */
194 LockTemp(rX86_ARG0);
195 LockTemp(rX86_ARG1);
196 LockTemp(rX86_ARG2);
197
198 /* Build frame, return address already on stack */
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700199 // TODO: 64 bit.
buzbee2700f7e2014-03-07 09:46:20 -0800200 stack_decrement_ = OpRegImm(kOpSub, rs_rX86_SP, frame_size_ - 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201
202 /*
203 * We can safely skip the stack overflow check if we're
204 * a leaf *and* our frame size < fudge factor.
205 */
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700206 const bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
207 (static_cast<size_t>(frame_size_) < Thread::kStackOverflowReservedBytes));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 NewLIR0(kPseudoMethodEntry);
209 /* Spill core callee saves */
210 SpillCoreRegs();
211 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
212 DCHECK_EQ(num_fp_spills_, 0);
213 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700214 class StackOverflowSlowPath : public LIRSlowPath {
215 public:
216 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
217 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
218 }
219 void Compile() OVERRIDE {
220 m2l_->ResetRegPool();
221 m2l_->ResetDefTracking();
222 GenerateTargetLabel();
buzbee2700f7e2014-03-07 09:46:20 -0800223 m2l_->OpRegImm(kOpAdd, rs_rX86_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700224 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700225 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700226 // Assumes codegen and target are in thumb2 mode.
buzbee2700f7e2014-03-07 09:46:20 -0800227 m2l_->CallHelper(RegStorage::InvalidReg(), func_offset, false /* MarkSafepointPC */,
228 false /* UseLink */);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700229 }
230
231 private:
232 const size_t sp_displace_;
233 };
234 // TODO: for large frames we should do something like:
235 // spill ebp
236 // lea ebp, [esp + frame_size]
237 // cmp ebp, fs:[stack_end_]
238 // jcc stack_overflow_exception
239 // mov esp, ebp
240 // in case a signal comes in that's not using an alternate signal stack and the large frame may
241 // have moved us outside of the reserved area at the end of the stack.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
Ian Rogersdd7624d2014-03-14 17:43:00 -0700243 OpRegThreadMem(kOpCmp, rX86_SP, Thread::StackEndOffset<4>());
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700244 LIR* branch = OpCondBranch(kCondUlt, nullptr);
245 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, frame_size_ - 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247
248 FlushIns(ArgLocs, rl_method);
249
Mark Mendell67c39c42014-01-31 17:28:00 -0800250 if (base_of_code_ != nullptr) {
251 // We have been asked to save the address of the method start for later use.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800252 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, rX86_ARG0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800253 int displacement = SRegOffset(base_of_code_->s_reg_low);
buzbee2700f7e2014-03-07 09:46:20 -0800254 setup_method_address_[1] = StoreBaseDisp(rs_rX86_SP, displacement, rs_rX86_ARG0, kWord);
Mark Mendell67c39c42014-01-31 17:28:00 -0800255 }
256
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 FreeTemp(rX86_ARG0);
258 FreeTemp(rX86_ARG1);
259 FreeTemp(rX86_ARG2);
260}
261
262void X86Mir2Lir::GenExitSequence() {
263 /*
264 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
265 * allocated by the register utilities as temps.
266 */
267 LockTemp(rX86_RET0);
268 LockTemp(rX86_RET1);
269
270 NewLIR0(kPseudoMethodExit);
271 UnSpillCoreRegs();
272 /* Remove frame except for return address */
buzbee2700f7e2014-03-07 09:46:20 -0800273 stack_increment_ = OpRegImm(kOpAdd, rs_rX86_SP, frame_size_ - 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 NewLIR0(kX86Ret);
275}
276
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800277void X86Mir2Lir::GenSpecialExitSequence() {
278 NewLIR0(kX86Ret);
279}
280
Dave Allisonf9487c02014-04-08 23:08:12 +0000281RegStorage X86Mir2Lir::CallHelperSetup(ThreadOffset<4> helper_offset) {
282 return RegStorage::InvalidReg();
283}
284
285LIR* X86Mir2Lir::CallHelper(RegStorage r_tgt, ThreadOffset<4> helper_offset, bool safepoint_pc,
286 bool use_link) {
287 LIR* call_inst = OpThreadMem(use_link ? kOpBlx : kOpBx, helper_offset);
288 if (safepoint_pc) {
289 MarkSafepointPC(call_inst);
290 }
291 return call_inst;
292}
293
294
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295} // namespace art