Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | void CodeGenerator::Compile(CodeAllocator* allocator) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 26 | const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks(); |
| 27 | DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock()); |
| 28 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1))); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 29 | CompileEntryBlock(); |
| 30 | for (size_t i = 1; i < blocks->Size(); i++) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 31 | CompileBlock(blocks->Get(i)); |
| 32 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 33 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 34 | uint8_t* buffer = allocator->Allocate(code_size); |
| 35 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 36 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 39 | void CodeGenerator::CompileEntryBlock() { |
| 40 | HGraphVisitor* location_builder = GetLocationBuilder(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 41 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 42 | // The entry block contains all locals for this method. By visiting the entry block, |
| 43 | // we're computing the required frame size. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 44 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 45 | HInstruction* current = it.Current(); |
| 46 | // Instructions in the entry block should not generate code. |
| 47 | if (kIsDebugBuild) { |
| 48 | current->Accept(location_builder); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | DCHECK(current->GetLocations() == nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 50 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 51 | current->Accept(instruction_visitor); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 52 | } |
| 53 | GenerateFrameEntry(); |
| 54 | } |
| 55 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 56 | void CodeGenerator::CompileBlock(HBasicBlock* block) { |
| 57 | Bind(GetLabelOf(block)); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 58 | HGraphVisitor* location_builder = GetLocationBuilder(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 59 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 60 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 61 | // 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 Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 68 | current->Accept(instruction_visitor); |
| 69 | if (current->GetLocations() != nullptr && current->GetLocations()->Out().IsValid()) { |
| 70 | Push(current, current->GetLocations()->Out()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 71 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 75 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 76 | if (instruction->GetLocations() == nullptr) return; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 77 | for (int i = 0; i < instruction->InputCount(); i++) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 78 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 79 | if (location.IsValid()) { |
| 80 | // Move the input to the desired location. |
| 81 | Move(instruction->InputAt(i), location); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 87 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 88 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 92 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 95 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 96 | HGraph* graph, |
| 97 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 98 | switch (instruction_set) { |
| 99 | case kArm: |
| 100 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 101 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 102 | } |
| 103 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 104 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 105 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 106 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 107 | } |
| 108 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 109 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace art |