blob: b2a69d8ad57b9f9806df2be52e95a8e953307745 [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"
21#include "utils/assembler.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000022
23namespace art {
24
25void CodeGenerator::Compile(CodeAllocator* allocator) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026 const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks();
27 DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock());
28 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1)));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000029 CompileEntryBlock();
30 for (size_t i = 1; i < blocks->Size(); i++) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031 CompileBlock(blocks->Get(i));
32 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000033 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034 uint8_t* buffer = allocator->Allocate(code_size);
35 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000036 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037}
38
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039void CodeGenerator::CompileEntryBlock() {
40 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000041 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000042 // The entry block contains all locals for this method. By visiting the entry block,
43 // we're computing the required frame size.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044 for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000045 HInstruction* current = it.Current();
46 // Instructions in the entry block should not generate code.
47 if (kIsDebugBuild) {
48 current->Accept(location_builder);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049 DCHECK(current->GetLocations() == nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051 current->Accept(instruction_visitor);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000052 }
53 GenerateFrameEntry();
54}
55
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056void CodeGenerator::CompileBlock(HBasicBlock* block) {
57 Bind(GetLabelOf(block));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000058 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000059 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000060 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061 // For each instruction, we emulate a stack-based machine, where the inputs are popped from
62 // the runtime stack, and the result is pushed on the stack. We currently can do this because
63 // we do not perform any code motion, and the Dex format does not reference individual
64 // instructions but uses registers instead (our equivalent of HLocal).
65 HInstruction* current = it.Current();
66 current->Accept(location_builder);
67 InitLocations(current);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000068 current->Accept(instruction_visitor);
69 if (current->GetLocations() != nullptr && current->GetLocations()->Out().IsValid()) {
70 Push(current, current->GetLocations()->Out());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000071 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072 }
73}
74
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000075void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000076 if (instruction->GetLocations() == nullptr) return;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 for (int i = 0; i < instruction->InputCount(); i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000079 if (location.IsValid()) {
80 // Move the input to the desired location.
81 Move(instruction->InputAt(i), location);
82 }
83 }
84}
85
86bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000087 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000089}
90
91Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000093}
94
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
96 HGraph* graph,
97 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098 switch (instruction_set) {
99 case kArm:
100 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102 }
103 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000107 }
108 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110 }
111}
112
113} // namespace art