blob: cf2b10aafed4201f88e0b3ed1ae5e5e95df549a5 [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 {
buzbee091cc402014-03-31 10:14:40 -0700155 NewLIR1(kX86StartOfMethod, rs_rX86_ARG2.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -0800156 }
buzbee091cc402014-03-31 10:14:40 -0700157 NewLIR2(kX86PcRelAdr, rs_rX86_ARG1.GetReg(), WrapPointer(tab_rec));
158 NewLIR2(kX86Add32RR, rs_rX86_ARG1.GetReg(), rs_rX86_ARG2.GetReg());
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) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700164 int ex_offset = Is64BitInstructionSet(cu_->instruction_set) ?
165 Thread::ExceptionOffset<8>().Int32Value() :
166 Thread::ExceptionOffset<4>().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000168 NewLIR2(kX86Mov32RT, rl_result.reg.GetReg(), ex_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 NewLIR2(kX86Mov32TI, ex_offset, 0);
170 StoreValue(rl_dest, rl_result);
171}
172
173/*
174 * Mark garbage collection card. Skip if the value we're storing is null.
175 */
buzbee2700f7e2014-03-07 09:46:20 -0800176void X86Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
177 RegStorage reg_card_base = AllocTemp();
178 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700180 int ct_offset = Is64BitInstructionSet(cu_->instruction_set) ?
181 Thread::CardTableOffset<8>().Int32Value() :
182 Thread::CardTableOffset<4>().Int32Value();
183 NewLIR2(kX86Mov32RT, reg_card_base.GetReg(), ct_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800185 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 LIR* target = NewLIR0(kPseudoTargetLabel);
187 branch_over->target = target;
188 FreeTemp(reg_card_base);
189 FreeTemp(reg_card_no);
190}
191
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700192void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 /*
194 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
195 * allocation mechanism know so it doesn't try to use any of them when
196 * expanding the frame or flushing. This leaves the utility
197 * code with no spare temps.
198 */
buzbee091cc402014-03-31 10:14:40 -0700199 LockTemp(rs_rX86_ARG0);
200 LockTemp(rs_rX86_ARG1);
201 LockTemp(rs_rX86_ARG2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202
203 /* Build frame, return address already on stack */
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700204 // TODO: 64 bit.
buzbee2700f7e2014-03-07 09:46:20 -0800205 stack_decrement_ = OpRegImm(kOpSub, rs_rX86_SP, frame_size_ - 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206
207 /*
208 * We can safely skip the stack overflow check if we're
209 * a leaf *and* our frame size < fudge factor.
210 */
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700211 const bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
212 (static_cast<size_t>(frame_size_) < Thread::kStackOverflowReservedBytes));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 NewLIR0(kPseudoMethodEntry);
214 /* Spill core callee saves */
215 SpillCoreRegs();
216 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
217 DCHECK_EQ(num_fp_spills_, 0);
218 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700219 class StackOverflowSlowPath : public LIRSlowPath {
220 public:
221 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
222 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
223 }
224 void Compile() OVERRIDE {
225 m2l_->ResetRegPool();
226 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700227 GenerateTargetLabel(kPseudoThrowTarget);
buzbee2700f7e2014-03-07 09:46:20 -0800228 m2l_->OpRegImm(kOpAdd, rs_rX86_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700229 m2l_->ClobberCallerSave();
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700230 // Assumes codegen and target are in thumb2 mode.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700231 if (Is64BitInstructionSet(cu_->instruction_set)) {
232 m2l_->CallHelper(RegStorage::InvalidReg(), QUICK_ENTRYPOINT_OFFSET(8, pThrowStackOverflow),
233 false /* MarkSafepointPC */, false /* UseLink */);
234 } else {
235 m2l_->CallHelper(RegStorage::InvalidReg(), QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow),
236 false /* MarkSafepointPC */, false /* UseLink */);
237 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700238 }
239
240 private:
241 const size_t sp_displace_;
242 };
243 // TODO: for large frames we should do something like:
244 // spill ebp
245 // lea ebp, [esp + frame_size]
246 // cmp ebp, fs:[stack_end_]
247 // jcc stack_overflow_exception
248 // mov esp, ebp
249 // in case a signal comes in that's not using an alternate signal stack and the large frame may
250 // have moved us outside of the reserved area at the end of the stack.
Mingyao Yang3a74d152014-04-21 15:39:44 -0700251 // cmp rX86_SP, fs:[stack_end_]; jcc throw_slowpath
Andreas Gampe2f244e92014-05-08 03:35:25 -0700252 if (Is64BitInstructionSet(cu_->instruction_set)) {
253 OpRegThreadMem(kOpCmp, rs_rX86_SP, Thread::StackEndOffset<8>());
254 } else {
255 OpRegThreadMem(kOpCmp, rs_rX86_SP, Thread::StackEndOffset<4>());
256 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700257 LIR* branch = OpCondBranch(kCondUlt, nullptr);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700258 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch,
259 frame_size_ -
260 GetInstructionSetPointerSize(cu_->instruction_set)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 }
262
263 FlushIns(ArgLocs, rl_method);
264
Mark Mendell67c39c42014-01-31 17:28:00 -0800265 if (base_of_code_ != nullptr) {
266 // We have been asked to save the address of the method start for later use.
buzbee091cc402014-03-31 10:14:40 -0700267 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, rs_rX86_ARG0.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -0800268 int displacement = SRegOffset(base_of_code_->s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700269 // Native pointer - must be natural word size.
270 setup_method_address_[1] = StoreWordDisp(rs_rX86_SP, displacement, rs_rX86_ARG0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800271 }
272
buzbee091cc402014-03-31 10:14:40 -0700273 FreeTemp(rs_rX86_ARG0);
274 FreeTemp(rs_rX86_ARG1);
275 FreeTemp(rs_rX86_ARG2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276}
277
278void X86Mir2Lir::GenExitSequence() {
279 /*
280 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
281 * allocated by the register utilities as temps.
282 */
buzbee091cc402014-03-31 10:14:40 -0700283 LockTemp(rs_rX86_RET0);
284 LockTemp(rs_rX86_RET1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
286 NewLIR0(kPseudoMethodExit);
287 UnSpillCoreRegs();
288 /* Remove frame except for return address */
buzbee2700f7e2014-03-07 09:46:20 -0800289 stack_increment_ = OpRegImm(kOpAdd, rs_rX86_SP, frame_size_ - 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 NewLIR0(kX86Ret);
291}
292
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800293void X86Mir2Lir::GenSpecialExitSequence() {
294 NewLIR0(kX86Ret);
295}
296
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297} // namespace art