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" |
| 22 | #include "utils/arm/assembler_arm.h" |
| 23 | #include "utils/mips/assembler_mips.h" |
| 24 | #include "utils/x86/assembler_x86.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | void CodeGenerator::Compile(CodeAllocator* allocator) { |
| 29 | GenerateFrameEntry(); |
| 30 | const GrowableArray<HBasicBlock*>* blocks = graph()->blocks(); |
| 31 | for (size_t i = 0; i < blocks->Size(); i++) { |
| 32 | CompileBlock(blocks->Get(i)); |
| 33 | } |
| 34 | size_t code_size = assembler_->CodeSize(); |
| 35 | uint8_t* buffer = allocator->Allocate(code_size); |
| 36 | MemoryRegion code(buffer, code_size); |
| 37 | assembler_->FinalizeInstructions(code); |
| 38 | } |
| 39 | |
| 40 | void CodeGenerator::CompileBlock(HBasicBlock* block) { |
| 41 | Bind(GetLabelOf(block)); |
| 42 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 43 | it.Current()->Accept(this); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool CodeGenerator::GoesToNextBlock(HGoto* goto_instruction) const { |
| 48 | HBasicBlock* successor = goto_instruction->GetSuccessor(); |
| 49 | // We currently iterate over the block in insertion order. |
| 50 | return goto_instruction->block()->block_id() + 1 == successor->block_id(); |
| 51 | } |
| 52 | |
| 53 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
| 54 | return block_labels_.GetRawStorage() + block->block_id(); |
| 55 | } |
| 56 | |
| 57 | bool CodeGenerator::CompileGraph(HGraph* graph, |
| 58 | InstructionSet instruction_set, |
| 59 | CodeAllocator* allocator) { |
| 60 | switch (instruction_set) { |
| 61 | case kArm: |
| 62 | case kThumb2: { |
| 63 | arm::ArmAssembler assembler; |
| 64 | arm::CodeGeneratorARM(&assembler, graph).Compile(allocator); |
| 65 | return true; |
| 66 | } |
| 67 | case kMips: |
| 68 | return false; |
| 69 | case kX86: { |
| 70 | x86::X86Assembler assembler; |
| 71 | x86::CodeGeneratorX86(&assembler, graph).Compile(allocator); |
| 72 | return true; |
| 73 | } |
| 74 | default: |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } // namespace art |