Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +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 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
| 19 | #include "builder.h" |
| 20 | #include "code_generator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 21 | #include "compilers.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 22 | #include "driver/compiler_driver.h" |
| 23 | #include "nodes.h" |
| 24 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Used by the code generator, to allocate the code in a vector. |
| 30 | */ |
| 31 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 32 | public: |
| 33 | CodeVectorAllocator() { } |
| 34 | |
| 35 | virtual uint8_t* Allocate(size_t size) { |
| 36 | size_ = size; |
| 37 | memory_.reserve(size); |
| 38 | return &memory_[0]; |
| 39 | } |
| 40 | |
| 41 | size_t GetSize() const { return size_; } |
| 42 | std::vector<uint8_t>* GetMemory() { return &memory_; } |
| 43 | |
| 44 | private: |
| 45 | std::vector<uint8_t> memory_; |
| 46 | size_t size_; |
| 47 | |
| 48 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 49 | }; |
| 50 | |
| 51 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 52 | CompiledMethod* OptimizingCompiler::TryCompile(CompilerDriver& driver, |
| 53 | const DexFile::CodeItem* code_item, |
| 54 | uint32_t access_flags, |
| 55 | InvokeType invoke_type, |
| 56 | uint16_t class_def_idx, |
| 57 | uint32_t method_idx, |
| 58 | jobject class_loader, |
| 59 | const DexFile& dex_file) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 60 | ArenaPool pool; |
| 61 | ArenaAllocator arena(&pool); |
| 62 | HGraphBuilder builder(&arena); |
| 63 | HGraph* graph = builder.BuildGraph(*code_item); |
| 64 | if (graph == nullptr) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | InstructionSet instruction_set = driver.GetInstructionSet(); |
| 69 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 70 | if (codegen == nullptr) { |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | CodeVectorAllocator allocator; |
| 75 | codegen->Compile(&allocator); |
| 76 | |
| 77 | std::vector<uint8_t> mapping_table; |
| 78 | codegen->BuildMappingTable(&mapping_table); |
| 79 | std::vector<uint8_t> vmap_table; |
| 80 | codegen->BuildVMapTable(&vmap_table); |
| 81 | std::vector<uint8_t> gc_map; |
| 82 | codegen->BuildNativeGCMap(&gc_map); |
| 83 | |
| 84 | return new CompiledMethod(driver, |
| 85 | instruction_set, |
| 86 | *allocator.GetMemory(), |
| 87 | codegen->GetFrameSize(), |
| 88 | 0, /* GPR spill mask, unused */ |
| 89 | 0, /* FPR spill mask, unused */ |
| 90 | mapping_table, |
| 91 | vmap_table, |
| 92 | gc_map, |
| 93 | nullptr); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace art |