blob: 6efc103f4041e1b93441682b80031fcd222aec60 [file] [log] [blame]
Brian Carlstrom751d4ed2013-07-18 16:20:16 -07001/*
2 * Copyright (C) 2013 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
Brian Carlstrom7940e442013-07-12 13:46:57 -070017#ifdef ART_SEA_IR_MODE
18#include <llvm/Support/Threading.h>
Dragos Sbirleabd136a22013-08-13 18:07:04 -070019#include <llvm/Support/raw_ostream.h>
20#include <llvm/Bitcode/ReaderWriter.h>
21
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "base/logging.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070023#include "llvm/llvm_compilation_unit.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/portable/mir_to_gbc.h"
25#include "driver/compiler_driver.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070026#include "verifier/method_verifier.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "mirror/object.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070028#include "utils.h"
29
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "runtime.h"
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070031#include "safe_map.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070032
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -070033#include "sea_ir/ir/sea.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070034#include "sea_ir/debug/dot_gen.h"
35#include "sea_ir/types/types.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070036#include "sea_ir/code_gen/code_gen.h"
37
Brian Carlstrom7940e442013-07-12 13:46:57 -070038namespace art {
39
40static CompiledMethod* CompileMethodWithSeaIr(CompilerDriver& compiler,
41 const CompilerBackend compiler_backend,
42 const DexFile::CodeItem* code_item,
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070043 uint32_t method_access_flags, InvokeType invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 uint32_t class_def_idx, uint32_t method_idx,
45 jobject class_loader, const DexFile& dex_file
46#if defined(ART_USE_PORTABLE_COMPILER)
47 , llvm::LlvmCompilationUnit* llvm_compilation_unit
48#endif
49) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070050 LOG(INFO) << "Compiling " << PrettyMethod(method_idx, dex_file) << ".";
Dragos Sbirleaa3519a42013-08-07 14:41:55 -070051 sea_ir::SeaGraph* ir_graph = sea_ir::SeaGraph::GetGraph(dex_file);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070052 std::string symbol = "dex_" + MangleForJni(PrettyMethod(method_idx, dex_file));
53 sea_ir::CodeGenData* llvm_data = ir_graph->CompileMethod(symbol,
54 code_item, class_def_idx, method_idx, method_access_flags, dex_file);
Dragos Sbirlea64479192013-08-01 15:38:43 -070055 sea_ir::DotConversion dc;
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070056 SafeMap<int, const sea_ir::Type*>* types = ir_graph->ti_->GetTypeMap();
57 dc.DumpSea(ir_graph, "/tmp/temp.dot", types);
Dragos Sbirleabd136a22013-08-13 18:07:04 -070058 MethodReference mref(&dex_file, method_idx);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070059 std::string llvm_code = llvm_data->GetElf(compiler.GetInstructionSet());
Dragos Sbirleabd136a22013-08-13 18:07:04 -070060 CompiledMethod* compiled_method = new CompiledMethod(
61 compiler.GetInstructionSet(),
62 llvm_code,
63 *verifier::MethodVerifier::GetDexGcMap(mref),
64 symbol);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070065 LOG(INFO) << "Compiled SEA IR method " << PrettyMethod(method_idx, dex_file) << ".";
Dragos Sbirleabd136a22013-08-13 18:07:04 -070066 return compiled_method;
Brian Carlstrom7940e442013-07-12 13:46:57 -070067}
68
Brian Carlstrom7940e442013-07-12 13:46:57 -070069CompiledMethod* SeaIrCompileOneMethod(CompilerDriver& compiler,
70 const CompilerBackend backend,
71 const DexFile::CodeItem* code_item,
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070072 uint32_t method_access_flags,
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 InvokeType invoke_type,
74 uint32_t class_def_idx,
75 uint32_t method_idx,
76 jobject class_loader,
77 const DexFile& dex_file,
78 llvm::LlvmCompilationUnit* llvm_compilation_unit) {
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070079 return CompileMethodWithSeaIr(compiler, backend, code_item, method_access_flags, invoke_type,
Dragos Sbirleac16c5b42013-07-26 18:08:35 -070080 class_def_idx, method_idx, class_loader, dex_file
Brian Carlstrom7940e442013-07-12 13:46:57 -070081#if defined(ART_USE_PORTABLE_COMPILER)
82 , llvm_compilation_unit
83#endif
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084 ); // NOLINT
Brian Carlstrom7940e442013-07-12 13:46:57 -070085}
86
87extern "C" art::CompiledMethod*
88 SeaIrCompileMethod(art::CompilerDriver& compiler,
89 const art::DexFile::CodeItem* code_item,
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070090 uint32_t method_access_flags, art::InvokeType invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
92 const art::DexFile& dex_file) {
Dragos Sbirleac16c5b42013-07-26 18:08:35 -070093 // TODO: Check method fingerprint here to determine appropriate backend type.
94 // Until then, use build default
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 art::CompilerBackend backend = compiler.GetCompilerBackend();
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070096 return art::SeaIrCompileOneMethod(compiler, backend, code_item, method_access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 class_def_idx, method_idx, class_loader, dex_file,
98 NULL /* use thread llvm_info */);
99}
100#endif
101
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700102} // namespace art