blob: d6295dbf43fbb49f0bea0aff3ce4f9c2f5370ddf [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 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#include "code_generator.h"
18
19#include "code_generator_arm.h"
20#include "code_generator_x86.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000021#include "dex/verified_method.h"
22#include "driver/dex_compilation_unit.h"
23#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000024#include "leb128.h"
25#include "mapping_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000027#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000028#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
30namespace art {
31
32void CodeGenerator::Compile(CodeAllocator* allocator) {
Nicolas Geoffray707c8092014-04-04 10:50:14 +010033 frame_size_ = GetGraph()->GetMaximumNumberOfOutVRegs() * GetWordSize();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000034 const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks();
35 DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock());
36 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1)));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037 CompileEntryBlock();
38 for (size_t i = 1; i < blocks->Size(); i++) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039 CompileBlock(blocks->Get(i));
40 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000041 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042 uint8_t* buffer = allocator->Allocate(code_size);
43 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045}
46
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000047void CodeGenerator::CompileEntryBlock() {
48 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050 // The entry block contains all locals for this method. By visiting the entry block,
51 // we're computing the required frame size.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000052 for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000053 HInstruction* current = it.Current();
54 // Instructions in the entry block should not generate code.
55 if (kIsDebugBuild) {
56 current->Accept(location_builder);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057 DCHECK(current->GetLocations() == nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000058 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000059 current->Accept(instruction_visitor);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060 }
61 GenerateFrameEntry();
62}
63
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000064void CodeGenerator::CompileBlock(HBasicBlock* block) {
65 Bind(GetLabelOf(block));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000066 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069 // For each instruction, we emulate a stack-based machine, where the inputs are popped from
70 // the runtime stack, and the result is pushed on the stack. We currently can do this because
71 // we do not perform any code motion, and the Dex format does not reference individual
72 // instructions but uses registers instead (our equivalent of HLocal).
73 HInstruction* current = it.Current();
74 current->Accept(location_builder);
75 InitLocations(current);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000076 current->Accept(instruction_visitor);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077 }
78}
79
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000080void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000081 if (instruction->GetLocations() == nullptr) return;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000082 for (int i = 0; i < instruction->InputCount(); i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000084 if (location.IsValid()) {
85 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010086 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000087 }
88 }
89}
90
91bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000092 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094}
95
96Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000097 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098}
99
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
101 HGraph* graph,
102 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103 switch (instruction_set) {
104 case kArm:
105 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000107 }
108 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700113 case kX86_64: {
114 return new (allocator) x86::CodeGeneratorX86(graph);
115 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000116 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000118 }
119}
120
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000121void CodeGenerator::BuildNativeGCMap(
122 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
123 const std::vector<uint8_t>& gc_map_raw =
124 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
125 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
126
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000127 uint32_t max_native_offset = 0;
128 for (size_t i = 0; i < pc_infos_.Size(); i++) {
129 uint32_t native_offset = pc_infos_.Get(i).native_pc;
130 if (native_offset > max_native_offset) {
131 max_native_offset = native_offset;
132 }
133 }
134
135 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
136 for (size_t i = 0; i < pc_infos_.Size(); i++) {
137 struct PcInfo pc_info = pc_infos_.Get(i);
138 uint32_t native_offset = pc_info.native_pc;
139 uint32_t dex_pc = pc_info.dex_pc;
140 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
141 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
142 builder.AddEntry(native_offset, references);
143 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000144}
145
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000146void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
147 uint32_t pc2dex_data_size = 0u;
148 uint32_t pc2dex_entries = pc_infos_.Size();
149 uint32_t pc2dex_offset = 0u;
150 int32_t pc2dex_dalvik_offset = 0;
151 uint32_t dex2pc_data_size = 0u;
152 uint32_t dex2pc_entries = 0u;
153
154 // We currently only have pc2dex entries.
155 for (size_t i = 0; i < pc2dex_entries; i++) {
156 struct PcInfo pc_info = pc_infos_.Get(i);
157 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
158 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
159 pc2dex_offset = pc_info.native_pc;
160 pc2dex_dalvik_offset = pc_info.dex_pc;
161 }
162
163 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
164 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
165 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
166 data->resize(data_size);
167
168 uint8_t* data_ptr = &(*data)[0];
169 uint8_t* write_pos = data_ptr;
170 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
171 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
172 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
173 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
174
175 pc2dex_offset = 0u;
176 pc2dex_dalvik_offset = 0u;
177 for (size_t i = 0; i < pc2dex_entries; i++) {
178 struct PcInfo pc_info = pc_infos_.Get(i);
179 DCHECK(pc2dex_offset <= pc_info.native_pc);
180 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
181 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
182 pc2dex_offset = pc_info.native_pc;
183 pc2dex_dalvik_offset = pc_info.dex_pc;
184 }
185 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
186 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
187
188 if (kIsDebugBuild) {
189 // Verify the encoded table holds the expected data.
190 MappingTable table(data_ptr);
191 CHECK_EQ(table.TotalSize(), total_entries);
192 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
193 auto it = table.PcToDexBegin();
194 auto it2 = table.DexToPcBegin();
195 for (size_t i = 0; i < pc2dex_entries; i++) {
196 struct PcInfo pc_info = pc_infos_.Get(i);
197 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
198 CHECK_EQ(pc_info.dex_pc, it.DexPc());
199 ++it;
200 }
201 CHECK(it == table.PcToDexEnd());
202 CHECK(it2 == table.DexToPcEnd());
203 }
204}
205
206void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
207 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100208 // We currently don't use callee-saved registers.
209 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000210 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
211 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000212 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
213
214 *data = vmap_encoder.GetData();
215}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000216
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000217} // namespace art