blob: 334b185b44d40cbaff619a4b85458ea1404fe0d7 [file] [log] [blame]
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001/*
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 Geoffray787c3072014-03-17 10:20:19 +000017#include <stdint.h>
18
19#include "builder.h"
20#include "code_generator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000021#include "compilers.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000022#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000023#include "driver/dex_compilation_unit.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000024#include "nodes.h"
25#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000026
27namespace art {
28
Nicolas Geoffray787c3072014-03-17 10:20:19 +000029/**
30 * Used by the code generator, to allocate the code in a vector.
31 */
32class CodeVectorAllocator FINAL : public CodeAllocator {
33 public:
34 CodeVectorAllocator() { }
35
36 virtual uint8_t* Allocate(size_t size) {
37 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000038 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000039 return &memory_[0];
40 }
41
42 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000043 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044
45 private:
46 std::vector<uint8_t> memory_;
47 size_t size_;
48
49 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
50};
51
52
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000053CompiledMethod* 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 Geoffray92cf83e2014-03-18 17:59:20 +000061 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 Geoffray787c3072014-03-17 10:20:19 +000065 ArenaPool pool;
66 ArenaAllocator arena(&pool);
67 HGraphBuilder builder(&arena);
68 HGraph* graph = builder.BuildGraph(*code_item);
69 if (graph == nullptr) {
70 return nullptr;
71 }
72
73 InstructionSet instruction_set = driver.GetInstructionSet();
74 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
75 if (codegen == nullptr) {
76 return nullptr;
77 }
78
79 CodeVectorAllocator allocator;
80 codegen->Compile(&allocator);
81
82 std::vector<uint8_t> mapping_table;
83 codegen->BuildMappingTable(&mapping_table);
84 std::vector<uint8_t> vmap_table;
85 codegen->BuildVMapTable(&vmap_table);
86 std::vector<uint8_t> gc_map;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000087 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088
89 return new CompiledMethod(driver,
90 instruction_set,
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000091 allocator.GetMemory(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 codegen->GetFrameSize(),
93 0, /* GPR spill mask, unused */
94 0, /* FPR spill mask, unused */
95 mapping_table,
96 vmap_table,
97 gc_map,
98 nullptr);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000099}
100
101} // namespace art