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" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 23 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 24 | #include "nodes.h" |
| 25 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Used by the code generator, to allocate the code in a vector. |
| 31 | */ |
| 32 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 33 | public: |
| 34 | CodeVectorAllocator() { } |
| 35 | |
| 36 | virtual uint8_t* Allocate(size_t size) { |
| 37 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 38 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 39 | return &memory_[0]; |
| 40 | } |
| 41 | |
| 42 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 43 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | std::vector<uint8_t> memory_; |
| 47 | size_t size_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 50 | }; |
| 51 | |
| 52 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 53 | CompiledMethod* OptimizingCompiler::TryCompile(CompilerDriver& driver, |
| 54 | const DexFile::CodeItem* code_item, |
| 55 | uint32_t access_flags, |
| 56 | InvokeType invoke_type, |
| 57 | uint16_t class_def_idx, |
| 58 | uint32_t method_idx, |
| 59 | jobject class_loader, |
| 60 | const DexFile& dex_file) const { |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 61 | DexCompilationUnit dex_compilation_unit( |
| 62 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
| 63 | class_def_idx, method_idx, access_flags, driver.GetVerifiedMethod(&dex_file, method_idx)); |
| 64 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 65 | // For testing purposes, we put a special marker on method names that should be compiled |
| 66 | // with this compiler. This makes sure we're not regressing. |
| 67 | bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; |
| 68 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 69 | ArenaPool pool; |
| 70 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 71 | HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 72 | HGraph* graph = builder.BuildGraph(*code_item); |
| 73 | if (graph == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 74 | if (shouldCompile) { |
| 75 | LOG(FATAL) << "Could not build graph in optimizing compiler"; |
| 76 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | InstructionSet instruction_set = driver.GetInstructionSet(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 81 | // The optimizing compiler currently does not have a Thumb2 assembler. |
| 82 | if (instruction_set == kThumb2) { |
| 83 | instruction_set = kArm; |
| 84 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 85 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 86 | if (codegen == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 87 | if (shouldCompile) { |
| 88 | LOG(FATAL) << "Could not find code generator for optimizing compiler"; |
| 89 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | CodeVectorAllocator allocator; |
| 94 | codegen->Compile(&allocator); |
| 95 | |
| 96 | std::vector<uint8_t> mapping_table; |
| 97 | codegen->BuildMappingTable(&mapping_table); |
| 98 | std::vector<uint8_t> vmap_table; |
| 99 | codegen->BuildVMapTable(&vmap_table); |
| 100 | std::vector<uint8_t> gc_map; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 101 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 102 | |
| 103 | return new CompiledMethod(driver, |
| 104 | instruction_set, |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 105 | allocator.GetMemory(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 106 | codegen->GetFrameSize(), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 107 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 108 | 0, /* FPR spill mask, unused */ |
| 109 | mapping_table, |
| 110 | vmap_table, |
| 111 | gc_map, |
| 112 | nullptr); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } // namespace art |