blob: 21a7bb95e9e224245c5b181faa26a64cdc5a340b [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 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 "compiler_llvm.h"
18
19#include "compiler.h"
20#include "ir_builder.h"
21#include "method_compiler.h"
Logan Chienf04364f2012-02-10 12:01:39 +080022#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023
24#include <llvm/ADT/OwningPtr.h>
25#include <llvm/Bitcode/ReaderWriter.h>
26#include <llvm/DerivedTypes.h>
27#include <llvm/LLVMContext.h>
28#include <llvm/Module.h>
29#include <llvm/Support/ToolOutputFile.h>
30
Logan Chien83426162011-12-09 09:29:50 +080031namespace art {
32namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080033
34
Logan Chien42e0e152012-01-13 15:42:36 +080035namespace {
36using namespace llvm;
37#include "art_module.cc"
38}
39
40
Shih-wei Liaod1fec812012-02-13 09:51:10 -080041CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
42: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
43 insn_set_(insn_set), context_(new llvm::LLVMContext()) {
44
45 // Create the module and include the runtime function declaration
46 module_ = new llvm::Module("art", *context_);
Logan Chien42e0e152012-01-13 15:42:36 +080047 makeLLVMModuleContents(module_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048
49 // Create IRBuilder
50 irb_.reset(new IRBuilder(*context_, *module_));
51}
52
53
54CompilerLLVM::~CompilerLLVM() {
55}
56
57
58void CompilerLLVM::MaterializeLLVMModule() {
59#if !defined(NDEBUG)
Logan Chien83426162011-12-09 09:29:50 +080060 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
61 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080062 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
63#endif
64}
65
66
67void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
68 std::string error_msg;
69
70 // Write the translated bitcode
71 llvm::OwningPtr<llvm::tool_output_file>
72 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
73 llvm::raw_fd_ostream::F_Binary));
74
75 if (!error_msg.empty()) {
76 LOG(FATAL) << "Unable to open file: " << error_msg;
77 return;
78 }
79
80 llvm::WriteBitcodeToFile(module_, out->os());
81 out->keep();
82
83 LOG(DEBUG) << "Bitcode Written At: " << filepath;
84}
85
86
87CompiledMethod*
88CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item,
89 uint32_t access_flags,
90 uint32_t method_idx,
91 ClassLoader const* class_loader,
92 DexFile const& dex_file) {
93
94 MutexLock GUARD(compiler_lock_);
95
96 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
97 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
98
99 UniquePtr<MethodCompiler> method_compiler(
100 new MethodCompiler(insn_set_, compiler_, class_linker, class_loader,
101 &dex_file, dex_cache, code_item, method_idx,
102 access_flags));
103
104 return method_compiler->Compile();
105}
Logan Chien83426162011-12-09 09:29:50 +0800106
107
Logan Chienf04364f2012-02-10 12:01:39 +0800108CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
109 char const *shorty) {
110
111 MutexLock GUARD(compiler_lock_);
112
113 UniquePtr<UpcallCompiler> upcall_compiler(
114 new UpcallCompiler(insn_set_, *compiler_));
115
116 return upcall_compiler->CreateStub(is_static, shorty);
117}
118
119
Logan Chien83426162011-12-09 09:29:50 +0800120} // namespace compiler_llvm
121} // namespace art