blob: 6099841a49cf3fbcdf01d0b144983230393bcb29 [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 Chien4dd96f52012-02-29 01:26:58 +080022#include "oat_compilation_unit.h"
Logan Chienf04364f2012-02-10 12:01:39 +080023#include "upcall_compiler.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024
25#include <llvm/ADT/OwningPtr.h>
26#include <llvm/Bitcode/ReaderWriter.h>
27#include <llvm/DerivedTypes.h>
28#include <llvm/LLVMContext.h>
29#include <llvm/Module.h>
30#include <llvm/Support/ToolOutputFile.h>
31
Logan Chien83426162011-12-09 09:29:50 +080032namespace art {
33namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080034
35
Logan Chiene75a8cc2012-02-24 12:26:43 +080036llvm::Module* makeLLVMModuleContents(llvm::Module* module);
Logan Chien42e0e152012-01-13 15:42:36 +080037
38
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
40: compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
41 insn_set_(insn_set), context_(new llvm::LLVMContext()) {
42
43 // Create the module and include the runtime function declaration
44 module_ = new llvm::Module("art", *context_);
Logan Chien42e0e152012-01-13 15:42:36 +080045 makeLLVMModuleContents(module_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
47 // Create IRBuilder
48 irb_.reset(new IRBuilder(*context_, *module_));
49}
50
51
52CompilerLLVM::~CompilerLLVM() {
53}
54
55
56void CompilerLLVM::MaterializeLLVMModule() {
57#if !defined(NDEBUG)
Logan Chien83426162011-12-09 09:29:50 +080058 // TODO: Add options to JNI_CreateJavaVM() and dex2oat, so that we don't
59 // have to hard-code the path.
Shih-wei Liaod1fec812012-02-13 09:51:10 -080060 WriteBitcodeToFile("/tmp/art_llvm_module.bc");
61#endif
62}
63
64
65void CompilerLLVM::WriteBitcodeToFile(std::string const &filepath) {
66 std::string error_msg;
67
68 // Write the translated bitcode
69 llvm::OwningPtr<llvm::tool_output_file>
70 out(new llvm::tool_output_file(filepath.c_str(), error_msg,
71 llvm::raw_fd_ostream::F_Binary));
72
73 if (!error_msg.empty()) {
74 LOG(FATAL) << "Unable to open file: " << error_msg;
75 return;
76 }
77
78 llvm::WriteBitcodeToFile(module_, out->os());
79 out->keep();
80
81 LOG(DEBUG) << "Bitcode Written At: " << filepath;
82}
83
84
Logan Chien4dd96f52012-02-29 01:26:58 +080085CompiledMethod* CompilerLLVM::CompileDexMethod(OatCompilationUnit* oat_compilation_unit) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080086 MutexLock GUARD(compiler_lock_);
87
Shih-wei Liaod1fec812012-02-13 09:51:10 -080088 UniquePtr<MethodCompiler> method_compiler(
Logan Chien4dd96f52012-02-29 01:26:58 +080089 new MethodCompiler(insn_set_, compiler_, oat_compilation_unit));
Shih-wei Liaod1fec812012-02-13 09:51:10 -080090
91 return method_compiler->Compile();
92}
Logan Chien83426162011-12-09 09:29:50 +080093
94
Logan Chienf04364f2012-02-10 12:01:39 +080095CompiledInvokeStub* CompilerLLVM::CreateInvokeStub(bool is_static,
96 char const *shorty) {
97
98 MutexLock GUARD(compiler_lock_);
99
100 UniquePtr<UpcallCompiler> upcall_compiler(
101 new UpcallCompiler(insn_set_, *compiler_));
102
103 return upcall_compiler->CreateStub(is_static, shorty);
104}
105
106
Logan Chien83426162011-12-09 09:29:50 +0800107} // namespace compiler_llvm
108} // namespace art