blob: bb6ac84a9fff4ef755ac23809e62beb5e53550cb [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 Geoffrayd4dd2552014-02-28 10:23:58 +000024#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000025#include "verifier/dex_gc_map.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026
27namespace art {
28
29void CodeGenerator::Compile(CodeAllocator* allocator) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000030 const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks();
31 DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock());
32 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1)));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000033 CompileEntryBlock();
34 for (size_t i = 1; i < blocks->Size(); i++) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035 CompileBlock(blocks->Get(i));
36 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000037 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038 uint8_t* buffer = allocator->Allocate(code_size);
39 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041}
42
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000043void CodeGenerator::CompileEntryBlock() {
44 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000045 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000046 // The entry block contains all locals for this method. By visiting the entry block,
47 // we're computing the required frame size.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000048 for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000049 HInstruction* current = it.Current();
50 // Instructions in the entry block should not generate code.
51 if (kIsDebugBuild) {
52 current->Accept(location_builder);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000053 DCHECK(current->GetLocations() == nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000054 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000055 current->Accept(instruction_visitor);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000056 }
57 GenerateFrameEntry();
58}
59
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000060void CodeGenerator::CompileBlock(HBasicBlock* block) {
61 Bind(GetLabelOf(block));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000063 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000064 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000065 // For each instruction, we emulate a stack-based machine, where the inputs are popped from
66 // the runtime stack, and the result is pushed on the stack. We currently can do this because
67 // we do not perform any code motion, and the Dex format does not reference individual
68 // instructions but uses registers instead (our equivalent of HLocal).
69 HInstruction* current = it.Current();
70 current->Accept(location_builder);
71 InitLocations(current);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072 current->Accept(instruction_visitor);
73 if (current->GetLocations() != nullptr && current->GetLocations()->Out().IsValid()) {
74 Push(current, current->GetLocations()->Out());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000075 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 }
77}
78
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000079void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 if (instruction->GetLocations() == nullptr) return;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000081 for (int i = 0; i < instruction->InputCount(); i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000083 if (location.IsValid()) {
84 // Move the input to the desired location.
85 Move(instruction->InputAt(i), location);
86 }
87 }
88}
89
90bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000093}
94
95Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097}
98
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
100 HGraph* graph,
101 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102 switch (instruction_set) {
103 case kArm:
104 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000105 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106 }
107 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111 }
112 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114 }
115}
116
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000117void CodeGenerator::BuildNativeGCMap(
118 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
119 const std::vector<uint8_t>& gc_map_raw =
120 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
121 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
122
123 GcMapBuilder builder(data, 0, 0, dex_gc_map.RegWidth());
124}
125
126
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000127} // namespace art