blob: 1489ec472de5a9bd83d49a16191102ab3959c905 [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"
Logan Chien88894ee2012-02-13 16:42:22 +080021#include "jni_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022#include "method_compiler.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080023#include "oat_compilation_unit.h"
Logan Chienf04364f2012-02-10 12:01:39 +080024#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025
26#include <llvm/ADT/OwningPtr.h>
27#include <llvm/Bitcode/ReaderWriter.h>
28#include <llvm/DerivedTypes.h>
29#include <llvm/LLVMContext.h>
30#include <llvm/Module.h>
31#include <llvm/Support/ToolOutputFile.h>
32
Logan Chien83426162011-12-09 09:29:50 +080033namespace art {
34namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36
Logan Chiene75a8cc2012-02-24 12:26:43 +080037llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080038
39
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
41: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
42 insn_set_(insn_set), context_(new llvm::LLVMContext()) {
43
44 // Create the module and include the runtime function declaration
45 module_ = new llvm::Module("art", *context_);
Logan Chien42e0e152012-01-13 15:42:36 +080046 makeLLVMModuleContents(module_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080047
48 // Create IRBuilder
49 irb_.reset(new IRBuilder(*context_, *module_));
50}
51
52
53CompilerLLVM::~CompilerLLVM() {
54}
55
56
57void CompilerLLVM::MaterializeLLVMModule() {
58#if !defined(NDEBUG)
Logan Chien83426162011-12-09 09:29:50 +080059 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
60 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080061 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
62#endif
63}
64
65
66void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
67 std::string error_msg;
68
69 // Write the translated bitcode
70 llvm::OwningPtr<llvm::tool_output_file>
71 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
72 llvm::raw_fd_ostream::F_Binary));
73
74 if (!error_msg.empty()) {
75 LOG(FATAL) << "Unable to open file: " << error_msg;
76 return;
77 }
78
79 llvm::WriteBitcodeToFile(module_, out->os());
80 out->keep();
81
82 LOG(DEBUG) << "Bitcode Written At: " << filepath;
83}
84
85
Logan Chien4dd96f52012-02-29 01:26:58 +080086CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080087 MutexLock GUARD(compiler_lock_);
88
Shih-wei Liaod1fec812012-02-13 09:51:10 -080089 UniquePtr<MethodCompiler> method_compiler(
Logan Chien4dd96f52012-02-29 01:26:58 +080090 new MethodCompiler(insn_set_, compiler_, oat_compilation_unit));
Shih-wei Liaod1fec812012-02-13 09:51:10 -080091
92 return method_compiler->Compile();
93}
Logan Chien83426162011-12-09 09:29:50 +080094
95
Logan Chien88894ee2012-02-13 16:42:22 +080096CompiledMethod* CompilerLLVM::CompileNativeMethod(OatCompilationUnit* oat_compilation_unit) {
97 MutexLock GUARD(compiler_lock_);
98
99 UniquePtr<JniCompiler> jni_compiler(
100 new JniCompiler(insn_set_, *compiler_, oat_compilation_unit));
101
102 return jni_compiler->Compile();
103}
104
105
Logan Chienf04364f2012-02-10 12:01:39 +0800106CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
107 char const *shorty) {
108
109 MutexLock GUARD(compiler_lock_);
110
111 UniquePtr<UpcallCompiler> upcall_compiler(
112 new UpcallCompiler(insn_set_, *compiler_));
113
114 return upcall_compiler->CreateStub(is_static, shorty);
115}
116
117
Logan Chien83426162011-12-09 09:29:50 +0800118} // namespace compiler_llvm
119} // namespace art