blob: 0e02846b2ecce8d9d1b317c7e0e8550b55cc8fd1 [file] [log] [blame]
Logan Chien88894ee2012-02-13 16:42:22 +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 "jni_compiler.h"
18
19#include "class_linker.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Logan Chien88894ee2012-02-13 16:42:22 +080021#include "compiled_method.h"
22#include "compiler.h"
23#include "compiler_llvm.h"
24#include "ir_builder.h"
25#include "logging.h"
26#include "oat_compilation_unit.h"
27#include "object.h"
28#include "runtime.h"
29#include "utils_llvm.h"
30
31#include <llvm/DerivedTypes.h>
32#include <llvm/Function.h>
33#include <llvm/Type.h>
34
35namespace art {
36namespace compiler_llvm {
37
38
Logan Chien8b977d32012-02-21 19:14:55 +080039JniCompiler::JniCompiler(CompilationUnit* cunit,
Logan Chien88894ee2012-02-13 16:42:22 +080040 Compiler const& compiler,
41 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080042: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
43 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
Logan Chien88894ee2012-02-13 16:42:22 +080044 oat_compilation_unit_(oat_compilation_unit),
45 access_flags_(oat_compilation_unit->access_flags_),
46 method_idx_(oat_compilation_unit->method_idx_),
47 class_linker_(oat_compilation_unit->class_linker_),
48 class_loader_(oat_compilation_unit->class_loader_),
49 dex_cache_(oat_compilation_unit->dex_cache_),
50 dex_file_(oat_compilation_unit->dex_file_),
51 method_(dex_cache_->GetResolvedMethod(method_idx_)) {
52
53 // Check: Ensure that the method is resolved
54 CHECK_NE(method_, static_cast<art::Method*>(NULL));
55
56 // Check: Ensure that JNI compiler will only get "native" method
57 CHECK((access_flags_ & kAccNative) != 0);
58}
59
60
61CompiledMethod* JniCompiler::Compile() {
62 CreateFunction();
63
Logan Chien8b977d32012-02-21 19:14:55 +080064 return new CompiledMethod(cunit_->GetInstructionSet(), func_);
Logan Chien88894ee2012-02-13 16:42:22 +080065}
66
67
68void JniCompiler::CreateFunction() {
69 // LLVM function name
70 std::string func_name(LLVMLongName(method_));
71
72 // Get function type
73 llvm::FunctionType* func_type =
74 GetFunctionType(method_idx_, method_->IsStatic());
75
76 // Create function
77 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
78 func_name, module_);
79}
80
81
82llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
83 bool is_static) {
84 // Get method signature
85 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
86
87 uint32_t shorty_size;
88 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
89 CHECK_GE(shorty_size, 1u);
90
91 // Get return type
92 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
93
94 // Get argument type
95 std::vector<llvm::Type*> args_type;
96
97 args_type.push_back(irb_.getJObjectTy()); // method object pointer
98
99 if (!is_static) {
100 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
101 }
102
103 for (uint32_t i = 1; i < shorty_size; ++i) {
104 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
105 }
106
107 return llvm::FunctionType::get(ret_type, args_type, false);
108}
109
110
111} // namespace compiler_llvm
112} // namespace art