blob: 4673cc0f7e897b147a72127550f715c954461e4b [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());
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700159 if (Is64BitInstructionSet(cu_->instruction_set)) {
160 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(8, pHandleFillArrayData), rs_rX86_ARG0,
161 rs_rX86_ARG1, true);
162 } else {
163 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(4, pHandleFillArrayData), rs_rX86_ARG0,
164 rs_rX86_ARG1, true);
165 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166}
167
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700169 int ex_offset = Is64BitInstructionSet(cu_->instruction_set) ?
170 Thread::ExceptionOffset<8>().Int32Value() :
171 Thread::ExceptionOffset<4>().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000173 NewLIR2(kX86Mov32RT, rl_result.reg.GetReg(), ex_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 NewLIR2(kX86Mov32TI, ex_offset, 0);
175 StoreValue(rl_dest, rl_result);
176}
177
178/*
179 * Mark garbage collection card. Skip if the value we're storing is null.
180 */
buzbee2700f7e2014-03-07 09:46:20 -0800181void X86Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
182 RegStorage reg_card_base = AllocTemp();
183 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700185 int ct_offset = Is64BitInstructionSet(cu_->instruction_set) ?
186 Thread::CardTableOffset<8>().Int32Value() :
187 Thread::CardTableOffset<4>().Int32Value();
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700188 if (Gen64Bit()) {
189 NewLIR2(kX86Mov64RT, reg_card_base.GetReg(), ct_offset);
190 } else {
191 NewLIR2(kX86Mov32RT, reg_card_base.GetReg(), ct_offset);
192 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800194 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 LIR* target = NewLIR0(kPseudoTargetLabel);
196 branch_over->target = target;
197 FreeTemp(reg_card_base);
198 FreeTemp(reg_card_no);
199}
200
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700201void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 /*
203 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
204 * allocation mechanism know so it doesn't try to use any of them when
205 * expanding the frame or flushing. This leaves the utility
206 * code with no spare temps.
207 */
buzbee091cc402014-03-31 10:14:40 -0700208 LockTemp(rs_rX86_ARG0);
209 LockTemp(rs_rX86_ARG1);
210 LockTemp(rs_rX86_ARG2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211
212 /* Build frame, return address already on stack */
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700213 stack_decrement_ = OpRegImm(kOpSub, rs_rX86_SP, frame_size_ - GetInstructionSetPointerSize(cu_->instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214
215 /*
216 * We can safely skip the stack overflow check if we're
217 * a leaf *and* our frame size < fudge factor.
218 */
Brian Carlstrom60d7a652014-03-13 18:10:08 -0700219 const bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
220 (static_cast<size_t>(frame_size_) < Thread::kStackOverflowReservedBytes));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 NewLIR0(kPseudoMethodEntry);
222 /* Spill core callee saves */
223 SpillCoreRegs();
224 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
225 DCHECK_EQ(num_fp_spills_, 0);
226 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700227 class StackOverflowSlowPath : public LIRSlowPath {
228 public:
229 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
230 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
231 }
232 void Compile() OVERRIDE {
233 m2l_->ResetRegPool();
234 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700235 GenerateTargetLabel(kPseudoThrowTarget);
buzbee2700f7e2014-03-07 09:46:20 -0800236 m2l_->OpRegImm(kOpAdd, rs_rX86_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700237 m2l_->ClobberCallerSave();
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700238 // Assumes codegen and target are in thumb2 mode.
Andreas Gampe2f244e92014-05-08 03:35:25 -0700239 if (Is64BitInstructionSet(cu_->instruction_set)) {
240 m2l_->CallHelper(RegStorage::InvalidReg(), QUICK_ENTRYPOINT_OFFSET(8, pThrowStackOverflow),
241 false /* MarkSafepointPC */, false /* UseLink */);
242 } else {
243 m2l_->CallHelper(RegStorage::InvalidReg(), QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow),
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700244 false /* MarkSafepointPC */, false /* UseLink */);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700245 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700246 }
247
248 private:
249 const size_t sp_displace_;
250 };
251 // TODO: for large frames we should do something like:
252 // spill ebp
253 // lea ebp, [esp + frame_size]
254 // cmp ebp, fs:[stack_end_]
255 // jcc stack_overflow_exception
256 // mov esp, ebp
257 // in case a signal comes in that's not using an alternate signal stack and the large frame may
258 // have moved us outside of the reserved area at the end of the stack.
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700259 // cmp rs_rX86_SP, fs:[stack_end_]; jcc throw_slowpath
Andreas Gampe2f244e92014-05-08 03:35:25 -0700260 if (Is64BitInstructionSet(cu_->instruction_set)) {
261 OpRegThreadMem(kOpCmp, rs_rX86_SP, Thread::StackEndOffset<8>());
262 } else {
263 OpRegThreadMem(kOpCmp, rs_rX86_SP, Thread::StackEndOffset<4>());
264 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700265 LIR* branch = OpCondBranch(kCondUlt, nullptr);
Andreas Gampe2f244e92014-05-08 03:35:25 -0700266 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch,
267 frame_size_ -
268 GetInstructionSetPointerSize(cu_->instruction_set)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 }
270
271 FlushIns(ArgLocs, rl_method);
272
Mark Mendell67c39c42014-01-31 17:28:00 -0800273 if (base_of_code_ != nullptr) {
274 // We have been asked to save the address of the method start for later use.
buzbee091cc402014-03-31 10:14:40 -0700275 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, rs_rX86_ARG0.GetReg());
Mark Mendell67c39c42014-01-31 17:28:00 -0800276 int displacement = SRegOffset(base_of_code_->s_reg_low);
buzbee695d13a2014-04-19 13:32:20 -0700277 // Native pointer - must be natural word size.
278 setup_method_address_[1] = StoreWordDisp(rs_rX86_SP, displacement, rs_rX86_ARG0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800279 }
280
buzbee091cc402014-03-31 10:14:40 -0700281 FreeTemp(rs_rX86_ARG0);
282 FreeTemp(rs_rX86_ARG1);
283 FreeTemp(rs_rX86_ARG2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
286void X86Mir2Lir::GenExitSequence() {
287 /*
288 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
289 * allocated by the register utilities as temps.
290 */
buzbee091cc402014-03-31 10:14:40 -0700291 LockTemp(rs_rX86_RET0);
292 LockTemp(rs_rX86_RET1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293
294 NewLIR0(kPseudoMethodExit);
295 UnSpillCoreRegs();
296 /* Remove frame except for return address */
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700297 stack_increment_ = OpRegImm(kOpAdd, rs_rX86_SP, frame_size_ - GetInstructionSetPointerSize(cu_->instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 NewLIR0(kX86Ret);
299}
300
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800301void X86Mir2Lir::GenSpecialExitSequence() {
302 NewLIR0(kX86Ret);
303}
304
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305} // namespace art