blob: 17924b0f0807e4e795c0e85e7f89893b448148ef [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,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070026 SpecialCaseHandler special_case) {
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);
87 int start_of_method_reg = AllocTemp();
88 // Materialize a pointer to the switch table
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089 // NewLIR0(kX86Bkpt);
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 NewLIR1(kX86StartOfMethod, start_of_method_reg);
91 int low_key = s4FromSwitchData(&table[2]);
92 int keyReg;
93 // Remove the bias, if necessary
94 if (low_key == 0) {
95 keyReg = rl_src.low_reg;
96 } else {
97 keyReg = AllocTemp();
98 OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key);
99 }
100 // Bounds check - if < 0 or >= size continue following switch
101 OpRegImm(kOpCmp, keyReg, size-1);
102 LIR* branch_over = OpCondBranch(kCondHi, NULL);
103
104 // Load the displacement from the switch table
105 int disp_reg = AllocTemp();
buzbee0d829482013-10-11 15:24:55 -0700106 NewLIR5(kX86PcRelLoadRA, disp_reg, start_of_method_reg, keyReg, 2, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 // Add displacement to start of method
108 OpRegReg(kOpAdd, start_of_method_reg, disp_reg);
109 // ..and go!
110 LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg);
111 tab_rec->anchor = switch_branch;
112
113 /* branch_over target here */
114 LIR* target = NewLIR0(kPseudoTargetLabel);
115 branch_over->target = target;
116}
117
118/*
119 * Array data table format:
120 * ushort ident = 0x0300 magic value
121 * ushort width width of each element in the table
122 * uint size number of elements in the table
123 * ubyte data[size*width] table of data values (may contain a single-byte
124 * padding at the end)
125 *
126 * Total size is 4+(width * size + 1)/2 16-bit code units.
127 */
buzbee0d829482013-10-11 15:24:55 -0700128void X86Mir2Lir::GenFillArrayData(DexOffset table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
130 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -0700131 FillArrayData* tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700132 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 tab_rec->table = table;
134 tab_rec->vaddr = current_dalvik_offset_;
135 uint16_t width = tab_rec->table[1];
136 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
137 tab_rec->size = (size * width) + 8;
138
139 fill_array_data_.Insert(tab_rec);
140
141 // Making a call - use explicit registers
142 FlushAllRegs(); /* Everything to home location */
143 LoadValueDirectFixed(rl_src, rX86_ARG0);
144 // Materialize a pointer to the fill data image
145 NewLIR1(kX86StartOfMethod, rX86_ARG2);
buzbee0d829482013-10-11 15:24:55 -0700146 NewLIR2(kX86PcRelAdr, rX86_ARG1, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 NewLIR2(kX86Add32RR, rX86_ARG1, rX86_ARG2);
Ian Rogers468532e2013-08-05 10:56:33 -0700148 CallRuntimeHelperRegReg(QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData), rX86_ARG0,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 rX86_ARG1, true);
150}
151
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700152void X86Mir2Lir::GenMoveException(RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 int ex_offset = Thread::ExceptionOffset().Int32Value();
154 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
155 NewLIR2(kX86Mov32RT, rl_result.low_reg, ex_offset);
156 NewLIR2(kX86Mov32TI, ex_offset, 0);
157 StoreValue(rl_dest, rl_result);
158}
159
160/*
161 * Mark garbage collection card. Skip if the value we're storing is null.
162 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700163void X86Mir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 int reg_card_base = AllocTemp();
165 int reg_card_no = AllocTemp();
166 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
167 NewLIR2(kX86Mov32RT, reg_card_base, Thread::CardTableOffset().Int32Value());
168 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
169 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
170 kUnsignedByte);
171 LIR* target = NewLIR0(kPseudoTargetLabel);
172 branch_over->target = target;
173 FreeTemp(reg_card_base);
174 FreeTemp(reg_card_no);
175}
176
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700177void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 /*
179 * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register
180 * allocation mechanism know so it doesn't try to use any of them when
181 * expanding the frame or flushing. This leaves the utility
182 * code with no spare temps.
183 */
184 LockTemp(rX86_ARG0);
185 LockTemp(rX86_ARG1);
186 LockTemp(rX86_ARG2);
187
188 /* Build frame, return address already on stack */
189 OpRegImm(kOpSub, rX86_SP, frame_size_ - 4);
190
191 /*
192 * We can safely skip the stack overflow check if we're
193 * a leaf *and* our frame size < fudge factor.
194 */
195 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
196 (static_cast<size_t>(frame_size_) <
197 Thread::kStackOverflowReservedBytes));
198 NewLIR0(kPseudoMethodEntry);
199 /* Spill core callee saves */
200 SpillCoreRegs();
201 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
202 DCHECK_EQ(num_fp_spills_, 0);
203 if (!skip_overflow_check) {
204 // cmp rX86_SP, fs:[stack_end_]; jcc throw_launchpad
205 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
Ian Rogers468532e2013-08-05 10:56:33 -0700206 OpRegThreadMem(kOpCmp, rX86_SP, Thread::StackEndOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 OpCondBranch(kCondUlt, tgt);
208 // Remember branch target - will process later
209 throw_launchpads_.Insert(tgt);
210 }
211
212 FlushIns(ArgLocs, rl_method);
213
214 FreeTemp(rX86_ARG0);
215 FreeTemp(rX86_ARG1);
216 FreeTemp(rX86_ARG2);
217}
218
219void X86Mir2Lir::GenExitSequence() {
220 /*
221 * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't
222 * allocated by the register utilities as temps.
223 */
224 LockTemp(rX86_RET0);
225 LockTemp(rX86_RET1);
226
227 NewLIR0(kPseudoMethodExit);
228 UnSpillCoreRegs();
229 /* Remove frame except for return address */
230 OpRegImm(kOpAdd, rX86_SP, frame_size_ - 4);
231 NewLIR0(kX86Ret);
232}
233
234} // namespace art