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