blob: b86665b9eed66f0151694d2766cbf4c1e81c526f [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 Geoffray787c3072014-03-17 10:20:19 +000033 const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks();
34 DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock());
35 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1)));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036 CompileEntryBlock();
37 for (size_t i = 1; i < blocks->Size(); i++) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038 CompileBlock(blocks->Get(i));
39 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041 uint8_t* buffer = allocator->Allocate(code_size);
42 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000043 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044}
45
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000046void CodeGenerator::CompileEntryBlock() {
47 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000048 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000049 // The entry block contains all locals for this method. By visiting the entry block,
50 // we're computing the required frame size.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051 for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000052 HInstruction* current = it.Current();
53 // Instructions in the entry block should not generate code.
54 if (kIsDebugBuild) {
55 current->Accept(location_builder);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000056 DCHECK(current->GetLocations() == nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000057 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058 current->Accept(instruction_visitor);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059 }
60 GenerateFrameEntry();
61}
62
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000063void CodeGenerator::CompileBlock(HBasicBlock* block) {
64 Bind(GetLabelOf(block));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000065 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000066 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000067 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068 // For each instruction, we emulate a stack-based machine, where the inputs are popped from
69 // the runtime stack, and the result is pushed on the stack. We currently can do this because
70 // we do not perform any code motion, and the Dex format does not reference individual
71 // instructions but uses registers instead (our equivalent of HLocal).
72 HInstruction* current = it.Current();
73 current->Accept(location_builder);
74 InitLocations(current);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 current->Accept(instruction_visitor);
76 if (current->GetLocations() != nullptr && current->GetLocations()->Out().IsValid()) {
77 Push(current, current->GetLocations()->Out());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079 }
80}
81
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000082void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 if (instruction->GetLocations() == nullptr) return;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000084 for (int i = 0; i < instruction->InputCount(); i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000085 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000086 if (location.IsValid()) {
87 // Move the input to the desired location.
88 Move(instruction->InputAt(i), location);
89 }
90 }
91}
92
93bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000096}
97
98Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100}
101
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000102CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
103 HGraph* graph,
104 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 switch (instruction_set) {
106 case kArm:
107 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109 }
110 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114 }
115 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117 }
118}
119
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000120void CodeGenerator::BuildNativeGCMap(
121 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
122 const std::vector<uint8_t>& gc_map_raw =
123 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
124 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
125
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000126 uint32_t max_native_offset = 0;
127 for (size_t i = 0; i < pc_infos_.Size(); i++) {
128 uint32_t native_offset = pc_infos_.Get(i).native_pc;
129 if (native_offset > max_native_offset) {
130 max_native_offset = native_offset;
131 }
132 }
133
134 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
135 for (size_t i = 0; i < pc_infos_.Size(); i++) {
136 struct PcInfo pc_info = pc_infos_.Get(i);
137 uint32_t native_offset = pc_info.native_pc;
138 uint32_t dex_pc = pc_info.dex_pc;
139 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
140 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
141 builder.AddEntry(native_offset, references);
142 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000143}
144
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000145void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
146 uint32_t pc2dex_data_size = 0u;
147 uint32_t pc2dex_entries = pc_infos_.Size();
148 uint32_t pc2dex_offset = 0u;
149 int32_t pc2dex_dalvik_offset = 0;
150 uint32_t dex2pc_data_size = 0u;
151 uint32_t dex2pc_entries = 0u;
152
153 // We currently only have pc2dex entries.
154 for (size_t i = 0; i < pc2dex_entries; i++) {
155 struct PcInfo pc_info = pc_infos_.Get(i);
156 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
157 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
158 pc2dex_offset = pc_info.native_pc;
159 pc2dex_dalvik_offset = pc_info.dex_pc;
160 }
161
162 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
163 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
164 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
165 data->resize(data_size);
166
167 uint8_t* data_ptr = &(*data)[0];
168 uint8_t* write_pos = data_ptr;
169 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
170 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
171 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
172 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
173
174 pc2dex_offset = 0u;
175 pc2dex_dalvik_offset = 0u;
176 for (size_t i = 0; i < pc2dex_entries; i++) {
177 struct PcInfo pc_info = pc_infos_.Get(i);
178 DCHECK(pc2dex_offset <= pc_info.native_pc);
179 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
180 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
181 pc2dex_offset = pc_info.native_pc;
182 pc2dex_dalvik_offset = pc_info.dex_pc;
183 }
184 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
185 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
186
187 if (kIsDebugBuild) {
188 // Verify the encoded table holds the expected data.
189 MappingTable table(data_ptr);
190 CHECK_EQ(table.TotalSize(), total_entries);
191 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
192 auto it = table.PcToDexBegin();
193 auto it2 = table.DexToPcBegin();
194 for (size_t i = 0; i < pc2dex_entries; i++) {
195 struct PcInfo pc_info = pc_infos_.Get(i);
196 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
197 CHECK_EQ(pc_info.dex_pc, it.DexPc());
198 ++it;
199 }
200 CHECK(it == table.PcToDexEnd());
201 CHECK(it2 == table.DexToPcEnd());
202 }
203}
204
205void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
206 Leb128EncodingVector vmap_encoder;
207 size_t size = 1 + 1 /* marker */ + 0;
208 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
209 vmap_encoder.PushBackUnsigned(size);
210 // We're currently always saving the frame pointer, so set it in the table as a temporary.
211 vmap_encoder.PushBackUnsigned(kVRegTempBaseReg + VmapTable::kEntryAdjustment);
212 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