blob: 7f646e0d25bd7337a9b2bf94cd5f959222416e5d [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,
Vladimir Marko5816ed42013-11-27 17:04:20 +000026 const InlineMethod& special) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070027 // TODO
28}
29
30/*
31 * The sparse table in the literal pool is an array of <key,displacement>
32 * pairs.
33 */
buzbee0d829482013-10-11 15:24:55 -070034void X86Mir2Lir::GenSparseSwitch(MIR* mir, DexOffset table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070035 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
37 if (cu_->verbose) {
38 DumpSparseSwitchTable(table);
39 }
40 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -070041 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
42 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 rl_src = LoadValue(rl_src, kCoreReg);
44 for (int i = 0; i < entries; i++) {
45 int key = keys[i];
46 BasicBlock* case_block =
47 mir_graph_->FindBlock(current_dalvik_offset_ + targets[i]);
48 OpCmpImmBranch(kCondEq, rl_src.low_reg, key,
49 &block_label_list_[case_block->id]);
50 }
51}
52
53/*
54 * Code pattern will look something like:
55 *
56 * mov r_val, ..
57 * call 0
58 * pop r_start_of_method
59 * sub r_start_of_method, ..
60 * mov r_key_reg, r_val
61 * sub r_key_reg, low_key
62 * cmp r_key_reg, size-1 ; bound check
63 * ja done
64 * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset]
65 * add r_start_of_method, r_disp
66 * jmp r_start_of_method
67 * done:
68 */
buzbee0d829482013-10-11 15:24:55 -070069void X86Mir2Lir::GenPackedSwitch(MIR* mir, DexOffset table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070070 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
72 if (cu_->verbose) {
73 DumpPackedSwitchTable(table);
74 }
75 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -070076 SwitchTable* tab_rec =
77 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 tab_rec->table = table;
79 tab_rec->vaddr = current_dalvik_offset_;
80 int size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070081 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
82 ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 switch_tables_.Insert(tab_rec);
84
85 // Get the switch value
86 rl_src = LoadValue(rl_src, kCoreReg);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070087 // NewLIR0(kX86Bkpt);
Mark Mendell67c39c42014-01-31 17:28:00 -080088
89 // Materialize a pointer to the switch table
90 int start_of_method_reg;
91 if (base_of_code_ != nullptr) {
92 // We can use the saved value.
93 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
94 rl_method = LoadValue(rl_method, kCoreReg);
95 start_of_method_reg = rl_method.low_reg;
Mark Mendell55d0eac2014-02-06 11:02:52 -080096 store_method_addr_used_ = true;
Mark Mendell67c39c42014-01-31 17:28:00 -080097 } else {
98 start_of_method_reg = AllocTemp();
99 NewLIR1(kX86StartOfMethod, start_of_method_reg);
100 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 int low_key = s4FromSwitchData(&table[2]);
102 int keyReg;
103 // Remove the bias, if necessary
104 if (low_key == 0) {
105 keyReg = rl_src.low_reg;
106 } else {
107 keyReg = AllocTemp();
108 OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key);
109 }
110 // Bounds check - if < 0 or >= size continue following switch
111 OpRegImm(kOpCmp, keyReg, size-1);
112 LIR* branch_over = OpCondBranch(kCondHi, NULL);
113
114 // Load the displacement from the switch table
115 int disp_reg = AllocTemp();
buzbee0d829482013-10-11 15:24:55 -0700116 NewLIR5(kX86PcRelLoadRA, disp_reg, start_of_method_reg, keyReg, 2, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 // Add displacement to start of method
118 OpRegReg(kOpAdd, start_of_method_reg, disp_reg);
119 // ..and go!
120 LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg);
121 tab_rec->anchor = switch_branch;
122
123 /* branch_over target here */
124 LIR* target = NewLIR0(kPseudoTargetLabel);
125 branch_over->target = target;
126}
127
128/*
129 * Array data table format:
130 * ushort ident = 0x0300 magic value
131 * ushort width width of each element in the table
132 * uint size number of elements in the table
133 * ubyte data[size*width] table of data values (may contain a single-byte
134 * padding at the end)
135 *
136 * Total size is 4+(width * size + 1)/2 16-bit code units.
137 */
buzbee0d829482013-10-11 15:24:55 -0700138void X86Mir2Lir::GenFillArrayData(DexOffset table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
140 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -0700141 FillArrayData* tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700142 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 tab_rec->table = table;
144 tab_rec->vaddr = current_dalvik_offset_;
145 uint16_t width = tab_rec->table[1];
146 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
147 tab_rec->size = (size * width) + 8;
148
149 fill_array_data_.Insert(tab_rec);
150
151 // Making a call - use explicit registers
152 FlushAllRegs(); /* Everything to home location */
153 LoadValueDirectFixed(rl_src, rX86_ARG0);
154 // Materialize a pointer to the fill data image
Mark Mendell67c39c42014-01-31 17:28:00 -0800155 if (base_of_code_ != nullptr) {
156 // We can use the saved value.
157 RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low);
158 LoadValueDirect(rl_method, rX86_ARG2);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800159 store_method_addr_used_ = true;
Mark Mendell67c39c42014-01-31 17:28:00 -0800160 } else {
161 NewLIR1(kX86StartOfMethod, rX86_ARG2);
162 }
buzbee0d829482013-10-11 15:24:55 -0700163 NewLIR2(kX86PcRelAdr, rX86_ARG1, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 NewLIR2(kX86Add32RR, rX86_ARG1, rX86_ARG2);
Ian Rogers468532e2013-08-05 10:56:33 -0700165 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData), rX86_ARG0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 rX86_ARG1, true);
167}
168
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700169void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 int ex_offset = Thread::ExceptionOffset().Int32Value();
171 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
172 NewLIR2(kX86Mov32RT, rl_result.low_reg, ex_offset);
173 NewLIR2(kX86Mov32TI, ex_offset, 0);
174 StoreValue(rl_dest, rl_result);
175}
176
177/*
178 * Mark garbage collection card. Skip if the value we're storing is null.
179 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180void X86Mir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 int reg_card_base = AllocTemp();
182 int reg_card_no = AllocTemp();
183 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
184 NewLIR2(kX86Mov32RT, reg_card_base, Thread::CardTableOffset().Int32Value());
185 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
186 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
187 kUnsignedByte);
188 LIR* target = NewLIR0(kPseudoTargetLabel);
189 branch_over->target = target;
190 FreeTemp(reg_card_base);
191 FreeTemp(reg_card_no);
192}
193
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700194void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195 /*
196 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
197 * allocation mechanism know so it doesn't try to use any of them when
198 * expanding the frame or flushing. This leaves the utility
199 * code with no spare temps.
200 */
201 LockTemp(rX86_ARG0);
202 LockTemp(rX86_ARG1);
203 LockTemp(rX86_ARG2);
204
205 /* Build frame, return address already on stack */
206 OpRegImm(kOpSub, rX86_SP, frame_size_ - 4);
207
208 /*
209 * We can safely skip the stack overflow check if we're
210 * a leaf *and* our frame size < fudge factor.
211 */
212 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
213 (static_cast<size_t>(frame_size_) <
214 Thread::kStackOverflowReservedBytes));
215 NewLIR0(kPseudoMethodEntry);
216 /* Spill core callee saves */
217 SpillCoreRegs();
218 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
219 DCHECK_EQ(num_fp_spills_, 0);
220 if (!skip_overflow_check) {
221 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
222 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
Ian Rogers468532e2013-08-05 10:56:33 -0700223 OpRegThreadMem(kOpCmp, rX86_SP, Thread::StackEndOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 OpCondBranch(kCondUlt, tgt);
225 // Remember branch target - will process later
226 throw_launchpads_.Insert(tgt);
227 }
228
229 FlushIns(ArgLocs, rl_method);
230
Mark Mendell67c39c42014-01-31 17:28:00 -0800231 if (base_of_code_ != nullptr) {
232 // We have been asked to save the address of the method start for later use.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800233 setup_method_address_[0] = NewLIR1(kX86StartOfMethod, rX86_ARG0);
Mark Mendell67c39c42014-01-31 17:28:00 -0800234 int displacement = SRegOffset(base_of_code_->s_reg_low);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800235 setup_method_address_[1] = StoreBaseDisp(rX86_SP, displacement, rX86_ARG0, kWord);
Mark Mendell67c39c42014-01-31 17:28:00 -0800236 }
237
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 FreeTemp(rX86_ARG0);
239 FreeTemp(rX86_ARG1);
240 FreeTemp(rX86_ARG2);
241}
242
243void X86Mir2Lir::GenExitSequence() {
244 /*
245 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
246 * allocated by the register utilities as temps.
247 */
248 LockTemp(rX86_RET0);
249 LockTemp(rX86_RET1);
250
251 NewLIR0(kPseudoMethodExit);
252 UnSpillCoreRegs();
253 /* Remove frame except for return address */
254 OpRegImm(kOpAdd, rX86_SP, frame_size_ - 4);
255 NewLIR0(kX86Ret);
256}
257
258} // namespace art