blob: 6d14a19e8ce636890374814e95d35bfbdd855844 [file] [log] [blame]
Logan Chienf04364f2012-02-10 12:01:39 +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 "upcall_compiler.h"
18
Logan Chien8b977d32012-02-21 19:14:55 +080019#include "compilation_unit.h"
Logan Chienf04364f2012-02-10 12:01:39 +080020#include "compiled_method.h"
Logan Chien8b977d32012-02-21 19:14:55 +080021#include "compiler.h"
Logan Chienf04364f2012-02-10 12:01:39 +080022#include "compiler_llvm.h"
23#include "ir_builder.h"
24#include "logging.h"
25#include "object.h"
26#include "runtime_support_func.h"
Logan Chien937105a2012-04-02 02:37:37 +080027#include "utils_llvm.h"
Logan Chienf04364f2012-02-10 12:01:39 +080028
29#include <llvm/Analysis/Verifier.h>
30#include <llvm/BasicBlock.h>
31#include <llvm/Function.h>
32#include <llvm/GlobalVariable.h>
33#include <llvm/Intrinsics.h>
34
35#include <string>
36#include <string.h>
37
38namespace art {
39namespace compiler_llvm {
40
41using namespace runtime_support;
42
43
Logan Chien8b977d32012-02-21 19:14:55 +080044UpcallCompiler::UpcallCompiler(CompilationUnit* cunit, Compiler& compiler)
45: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
Logan Chien937105a2012-04-02 02:37:37 +080046 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
47 elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Logan Chienf04364f2012-02-10 12:01:39 +080048}
49
50
51CompiledInvokeStub* UpcallCompiler::CreateStub(bool is_static,
52 char const* shorty) {
53
54 CHECK_NE(shorty, static_cast<char const*>(NULL));
55 size_t shorty_size = strlen(shorty);
56
57 // Function name
Logan Chien937105a2012-04-02 02:37:37 +080058 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chienf04364f2012-02-10 12:01:39 +080059
60 // Get argument types
61 llvm::Type* arg_types[] = {
Logan Chienf04364f2012-02-10 12:01:39 +080062 irb_.getJObjectTy(), // Method object pointer
63 irb_.getJObjectTy(), // "this" object pointer (NULL for static)
64 irb_.getJObjectTy(), // Thread object pointer
65 irb_.getJValueTy()->getPointerTo(),
66 irb_.getJValueTy()->getPointerTo(),
67 };
68
69 // Function type
70 llvm::FunctionType* func_type =
71 llvm::FunctionType::get(irb_.getVoidTy(), arg_types, false);
72
73 // Create function
74 llvm::Function* func =
75 llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
76 func_name, module_);
77
78
79 // Create basic block for the body of this function
80 llvm::BasicBlock* block_body =
81 llvm::BasicBlock::Create(*context_, "upcall", func);
82
83 irb_.SetInsertPoint(block_body);
84
85 // Actual arguments
86 llvm::Function::arg_iterator arg_iter = func->arg_begin();
87
Logan Chienf04364f2012-02-10 12:01:39 +080088 llvm::Value* method_object_addr = arg_iter++;
89 llvm::Value* callee_this_addr = arg_iter++;
90 llvm::Value* thread_object_addr = arg_iter++;
91 llvm::Value* actual_args_array_addr = arg_iter++;
92 llvm::Value* retval_addr = arg_iter++;
93
94 // Setup thread pointer
95 irb_.CreateCall(irb_.GetRuntime(SetCurrentThread), thread_object_addr);
96
97 // Accurate function type
98 llvm::Type* accurate_ret_type = irb_.getJType(shorty[0], kAccurate);
99
100 std::vector<llvm::Type*> accurate_arg_types;
101
102 accurate_arg_types.push_back(irb_.getJObjectTy()); // method object pointer
103
104 if (!is_static) {
105 accurate_arg_types.push_back(irb_.getJObjectTy());
106 }
107
108 for (size_t i = 1; i < shorty_size; ++i) {
109 accurate_arg_types.push_back(irb_.getJType(shorty[i], kAccurate));
110 }
111
112 llvm::FunctionType* accurate_func_type =
113 llvm::FunctionType::get(accurate_ret_type, accurate_arg_types, false);
114
115 // Load actual arguments
116 std::vector<llvm::Value*> args;
117
118 args.push_back(method_object_addr);
119
120 if (!is_static) {
121 args.push_back(callee_this_addr);
122 }
123
124 for (size_t i = 1; i < shorty_size; ++i) {
125 char arg_shorty = shorty[i];
126
127 if (arg_shorty == 'Z' || arg_shorty == 'B' || arg_shorty == 'C' ||
128 arg_shorty == 'S' || arg_shorty == 'I' || arg_shorty == 'J' ||
129 arg_shorty == 'F' || arg_shorty == 'D' || arg_shorty == 'L') {
130
131 llvm::Type* arg_type =
132 irb_.getJType(shorty[i], kAccurate)->getPointerTo();
133
134 llvm::Value* arg_jvalue_addr =
135 irb_.CreateConstGEP1_32(actual_args_array_addr, i - 1);
136
137 llvm::Value* arg_addr = irb_.CreateBitCast(arg_jvalue_addr, arg_type);
138
139 args.push_back(irb_.CreateLoad(arg_addr));
140
141 } else {
Shih-wei Liao90d50992012-02-19 03:32:05 -0800142 LOG(FATAL) << "Unexpected arg shorty for invoke stub: " << shorty[i];
Logan Chienf04364f2012-02-10 12:01:39 +0800143 }
144 }
145
146 // Invoke managed method now!
147 llvm::Value* code_field_offset_value =
148 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
149
150 llvm::Value* code_field_addr =
151 irb_.CreatePtrDisp(method_object_addr, code_field_offset_value,
152 accurate_func_type->getPointerTo()->getPointerTo());
153
TDYa12785321912012-04-01 15:24:56 -0700154 llvm::Value* code_addr_ = irb_.CreateLoad(code_field_addr);
155 llvm::Value* code_addr;
156 // TODO: Inline check
157 llvm::Value* runtime_func = irb_.GetRuntime(runtime_support::EnsureInitialized);
158 llvm::Value* result = irb_.CreateCall2(runtime_func,
159 method_object_addr,
160 irb_.CreatePointerCast(code_addr_,
161 irb_.getJObjectTy()));
162 code_addr = irb_.CreatePointerCast(result, accurate_func_type->getPointerTo());
Logan Chienf04364f2012-02-10 12:01:39 +0800163
164 llvm::Value* retval = irb_.CreateCall(code_addr, args);
165
166 // Store the returned value
167 if (shorty[0] != 'V') {
168 llvm::Value* ret_addr =
169 irb_.CreateBitCast(retval_addr, accurate_ret_type->getPointerTo());
170
171 irb_.CreateStore(retval, ret_addr);
172 }
173
174 irb_.CreateRetVoid();
175
Logan Chien8b977d32012-02-21 19:14:55 +0800176 // Verify the generated function
Logan Chienf04364f2012-02-10 12:01:39 +0800177 llvm::verifyFunction(*func, llvm::PrintMessageAction);
178
Logan Chien8b977d32012-02-21 19:14:55 +0800179 // Add the memory usage approximation of the compilation unit
180 cunit_->AddMemUsageApproximation((shorty_size * 3 + 8) * 500);
181 // NOTE: We will emit 3 LLVM instructions per shorty for the argument,
182 // plus 3 for pointer arithmetic, and 5 for code_addr, retval, ret_addr,
183 // store ret_addr, and ret_void. Beside, we guess that we have to use
184 // 50 bytes to represent one LLVM instruction.
185
Logan Chien937105a2012-04-02 02:37:37 +0800186 return new CompiledInvokeStub(cunit_->GetElfIndex(), elf_func_idx_);
Logan Chienf04364f2012-02-10 12:01:39 +0800187}
188
189
190} // namespace compiler_llvm
191} // namespace art