blob: 038e826a09f2e45c5101facc551ac7b02cc20592 [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 Chiene75a8cc2012-02-24 12:26:43 +080035llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080036
37
Shih-wei Liaod1fec812012-02-13 09:51:10 -080038CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
39: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
40 insn_set_(insn_set), context_(new llvm::LLVMContext()) {
41
42 // Create the module and include the runtime function declaration
43 module_ = new llvm::Module("art", *context_);
Logan Chien42e0e152012-01-13 15:42:36 +080044 makeLLVMModuleContents(module_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080045
46 // Create IRBuilder
47 irb_.reset(new IRBuilder(*context_, *module_));
48}
49
50
51CompilerLLVM::~CompilerLLVM() {
52}
53
54
55void CompilerLLVM::MaterializeLLVMModule() {
56#if !defined(NDEBUG)
Logan Chien83426162011-12-09 09:29:50 +080057 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
58 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080059 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
60#endif
61}
62
63
64void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
65 std::string error_msg;
66
67 // Write the translated bitcode
68 llvm::OwningPtr<llvm::tool_output_file>
69 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
70 llvm::raw_fd_ostream::F_Binary));
71
72 if (!error_msg.empty()) {
73 LOG(FATAL) << "Unable to open file: " << error_msg;
74 return;
75 }
76
77 llvm::WriteBitcodeToFile(module_, out->os());
78 out->keep();
79
80 LOG(DEBUG) << "Bitcode Written At: " << filepath;
81}
82
83
84CompiledMethod*
85CompilerLLVM::CompileDexMethod(DexFile::CodeItem const* code_item,
86 uint32_t access_flags,
87 uint32_t method_idx,
88 ClassLoader const* class_loader,
89 DexFile const& dex_file) {
90
91 MutexLock GUARD(compiler_lock_);
92
93 ClassLinker *class_linker = Runtime::Current()->GetClassLinker();
94 DexCache *dex_cache = class_linker->FindDexCache(dex_file);
95
96 UniquePtr<MethodCompiler> method_compiler(
97 new MethodCompiler(insn_set_, compiler_, class_linker, class_loader,
98 &dex_file, dex_cache, code_item, method_idx,
99 access_flags));
100
101 return method_compiler->Compile();
102}
Logan Chien83426162011-12-09 09:29:50 +0800103
104
Logan Chienf04364f2012-02-10 12:01:39 +0800105CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
106 char const *shorty) {
107
108 MutexLock GUARD(compiler_lock_);
109
110 UniquePtr<UpcallCompiler> upcall_compiler(
111 new UpcallCompiler(insn_set_, *compiler_));
112
113 return upcall_compiler->CreateStub(is_static, shorty);
114}
115
116
Logan Chien83426162011-12-09 09:29:50 +0800117} // namespace compiler_llvm
118} // namespace art