blob: af05e5246fc185a2988dc0acc030be8c38125433 [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
TDYa127eead4ac2012-06-03 07:15:25 -070017#include "stub_compiler.h"
Logan Chienf04364f2012-02-10 12:01:39 +080018
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
Logan Chienf04364f2012-02-10 12:01:39 +080029#include <llvm/BasicBlock.h>
30#include <llvm/Function.h>
31#include <llvm/GlobalVariable.h>
32#include <llvm/Intrinsics.h>
33
34#include <string>
35#include <string.h>
36
37namespace art {
38namespace compiler_llvm {
39
40using namespace runtime_support;
41
42
TDYa127eead4ac2012-06-03 07:15:25 -070043StubCompiler::StubCompiler(CompilationUnit* cunit, Compiler& compiler)
Logan Chien8b977d32012-02-21 19:14:55 +080044: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
TDYa127eead4ac2012-06-03 07:15:25 -070045 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()) {
Logan Chienf04364f2012-02-10 12:01:39 +080046}
47
48
Logan Chien7a2a23a2012-06-06 11:01:00 +080049CompiledInvokeStub* StubCompiler::CreateInvokeStub(bool is_static,
50 char const* shorty) {
Logan Chien7a2a23a2012-06-06 11:01:00 +080051 CHECK(shorty != NULL);
Logan Chienf04364f2012-02-10 12:01:39 +080052 size_t shorty_size = strlen(shorty);
53
54 // Function name
Logan Chien971bf3f2012-05-01 15:47:55 +080055 std::string func_name(ElfFuncName(cunit_->GetIndex()));
Logan Chienf04364f2012-02-10 12:01:39 +080056
57 // Get argument types
58 llvm::Type* arg_types[] = {
Logan Chienf04364f2012-02-10 12:01:39 +080059 irb_.getJObjectTy(), // Method object pointer
60 irb_.getJObjectTy(), // "this" object pointer (NULL for static)
61 irb_.getJObjectTy(), // Thread object pointer
62 irb_.getJValueTy()->getPointerTo(),
63 irb_.getJValueTy()->getPointerTo(),
64 };
65
66 // Function type
67 llvm::FunctionType* func_type =
68 llvm::FunctionType::get(irb_.getVoidTy(), arg_types, false);
69
70 // Create function
71 llvm::Function* func =
72 llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
73 func_name, module_);
74
75
76 // Create basic block for the body of this function
77 llvm::BasicBlock* block_body =
78 llvm::BasicBlock::Create(*context_, "upcall", func);
79
80 irb_.SetInsertPoint(block_body);
81
82 // Actual arguments
83 llvm::Function::arg_iterator arg_iter = func->arg_begin();
84
Logan Chienf04364f2012-02-10 12:01:39 +080085 llvm::Value* method_object_addr = arg_iter++;
86 llvm::Value* callee_this_addr = arg_iter++;
87 llvm::Value* thread_object_addr = arg_iter++;
88 llvm::Value* actual_args_array_addr = arg_iter++;
89 llvm::Value* retval_addr = arg_iter++;
90
91 // Setup thread pointer
TDYa127c1478262012-06-20 20:22:27 -070092 llvm::Value* old_thread_register = irb_.Runtime().EmitSetCurrentThread(thread_object_addr);
Logan Chienf04364f2012-02-10 12:01:39 +080093
94 // Accurate function type
95 llvm::Type* accurate_ret_type = irb_.getJType(shorty[0], kAccurate);
96
97 std::vector<llvm::Type*> accurate_arg_types;
98
99 accurate_arg_types.push_back(irb_.getJObjectTy()); // method object pointer
100
101 if (!is_static) {
102 accurate_arg_types.push_back(irb_.getJObjectTy());
103 }
104
105 for (size_t i = 1; i < shorty_size; ++i) {
106 accurate_arg_types.push_back(irb_.getJType(shorty[i], kAccurate));
107 }
108
109 llvm::FunctionType* accurate_func_type =
110 llvm::FunctionType::get(accurate_ret_type, accurate_arg_types, false);
111
112 // Load actual arguments
113 std::vector<llvm::Value*> args;
114
115 args.push_back(method_object_addr);
116
117 if (!is_static) {
118 args.push_back(callee_this_addr);
119 }
120
121 for (size_t i = 1; i < shorty_size; ++i) {
122 char arg_shorty = shorty[i];
123
124 if (arg_shorty == 'Z' || arg_shorty == 'B' || arg_shorty == 'C' ||
125 arg_shorty == 'S' || arg_shorty == 'I' || arg_shorty == 'J' ||
126 arg_shorty == 'F' || arg_shorty == 'D' || arg_shorty == 'L') {
127
128 llvm::Type* arg_type =
129 irb_.getJType(shorty[i], kAccurate)->getPointerTo();
130
131 llvm::Value* arg_jvalue_addr =
132 irb_.CreateConstGEP1_32(actual_args_array_addr, i - 1);
133
134 llvm::Value* arg_addr = irb_.CreateBitCast(arg_jvalue_addr, arg_type);
135
TDYa127aba61122012-05-04 18:28:36 -0700136 args.push_back(irb_.CreateLoad(arg_addr, kTBAAStackTemp));
Logan Chienf04364f2012-02-10 12:01:39 +0800137
138 } else {
Shih-wei Liao90d50992012-02-19 03:32:05 -0800139 LOG(FATAL) << "Unexpected arg shorty for invoke stub: " << shorty[i];
Logan Chienf04364f2012-02-10 12:01:39 +0800140 }
141 }
142
143 // Invoke managed method now!
TDYa1270b686e52012-04-09 22:43:35 -0700144 // TODO: If we solve the trampoline related problems, we can just get the code address and call.
145#if 0
Logan Chienf04364f2012-02-10 12:01:39 +0800146 llvm::Value* code_field_offset_value =
147 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
148
149 llvm::Value* code_field_addr =
150 irb_.CreatePtrDisp(method_object_addr, code_field_offset_value,
151 accurate_func_type->getPointerTo()->getPointerTo());
152
TDYa1278ca10052012-05-05 19:57:06 -0700153 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr, kTBAAJRuntime);
TDYa1270b686e52012-04-09 22:43:35 -0700154#else
155 llvm::Value* result = irb_.CreateCall(irb_.GetRuntime(FixStub), method_object_addr);
156 llvm::Value* code_addr = irb_.CreatePointerCast(result, accurate_func_type->getPointerTo());
157
158 // Exception unwind.
TDYa127de479be2012-05-31 08:03:26 -0700159 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
TDYa1270b686e52012-04-09 22:43:35 -0700160 llvm::BasicBlock* block_unwind = llvm::BasicBlock::Create(*context_, "exception_unwind", func);
161 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "cont", func);
162 irb_.CreateCondBr(exception_pending, block_unwind, block_cont);
163 irb_.SetInsertPoint(block_unwind);
TDYa127c1478262012-06-20 20:22:27 -0700164 // Restore thread register
165 irb_.Runtime().EmitSetCurrentThread(old_thread_register);
TDYa1270b686e52012-04-09 22:43:35 -0700166 irb_.CreateRetVoid();
167 irb_.SetInsertPoint(block_cont);
168#endif
Logan Chienf04364f2012-02-10 12:01:39 +0800169
170 llvm::Value* retval = irb_.CreateCall(code_addr, args);
171
172 // Store the returned value
173 if (shorty[0] != 'V') {
174 llvm::Value* ret_addr =
175 irb_.CreateBitCast(retval_addr, accurate_ret_type->getPointerTo());
176
TDYa127aba61122012-05-04 18:28:36 -0700177 irb_.CreateStore(retval, ret_addr, kTBAAStackTemp);
Logan Chienf04364f2012-02-10 12:01:39 +0800178 }
179
TDYa127c1478262012-06-20 20:22:27 -0700180 // Restore thread register
181 irb_.Runtime().EmitSetCurrentThread(old_thread_register);
Logan Chienf04364f2012-02-10 12:01:39 +0800182 irb_.CreateRetVoid();
183
Logan Chien8b977d32012-02-21 19:14:55 +0800184 // Verify the generated function
TDYa127853cd092012-04-21 22:15:31 -0700185 VERIFY_LLVM_FUNCTION(*func);
Logan Chienf04364f2012-02-10 12:01:39 +0800186
Logan Chien971bf3f2012-05-01 15:47:55 +0800187 cunit_->Materialize();
Logan Chien8b977d32012-02-21 19:14:55 +0800188
Logan Chien598c5132012-04-28 22:00:44 +0800189 return new CompiledInvokeStub(cunit_->GetInstructionSet(),
Logan Chien971bf3f2012-05-01 15:47:55 +0800190 cunit_->GetCompiledCode());
TDYa127eead4ac2012-06-03 07:15:25 -0700191}
192
TDYa127eead4ac2012-06-03 07:15:25 -0700193
Logan Chien7a2a23a2012-06-06 11:01:00 +0800194CompiledInvokeStub* StubCompiler::CreateProxyStub(char const* shorty) {
195 CHECK(shorty != NULL);
TDYa127eead4ac2012-06-03 07:15:25 -0700196 size_t shorty_size = strlen(shorty);
197
TDYa127eead4ac2012-06-03 07:15:25 -0700198 // Function name
Logan Chien971bf3f2012-05-01 15:47:55 +0800199 std::string func_name(ElfFuncName(cunit_->GetIndex()));
TDYa127eead4ac2012-06-03 07:15:25 -0700200
201 // Accurate function type
202 llvm::Type* accurate_ret_type = irb_.getJType(shorty[0], kAccurate);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800203
TDYa127eead4ac2012-06-03 07:15:25 -0700204 std::vector<llvm::Type*> accurate_arg_types;
205 accurate_arg_types.push_back(irb_.getJObjectTy()); // method
206 accurate_arg_types.push_back(irb_.getJObjectTy()); // this
Logan Chien7a2a23a2012-06-06 11:01:00 +0800207
TDYa127eead4ac2012-06-03 07:15:25 -0700208 for (size_t i = 1; i < shorty_size; ++i) {
209 accurate_arg_types.push_back(irb_.getJType(shorty[i], kAccurate));
210 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800211
TDYa127eead4ac2012-06-03 07:15:25 -0700212 llvm::FunctionType* accurate_func_type =
213 llvm::FunctionType::get(accurate_ret_type, accurate_arg_types, false);
214
215 // Create function
216 llvm::Function* func =
217 llvm::Function::Create(accurate_func_type, llvm::Function::ExternalLinkage,
218 func_name, module_);
219
220 // Create basic block for the body of this function
221 llvm::BasicBlock* block_body =
222 llvm::BasicBlock::Create(*context_, "proxy", func);
223 irb_.SetInsertPoint(block_body);
224
225 // JValue for proxy return
226 llvm::AllocaInst* jvalue_temp = irb_.CreateAlloca(irb_.getJValueTy());
227
228 // Load actual arguments
229 llvm::Function::arg_iterator arg_iter = func->arg_begin();
Logan Chien7a2a23a2012-06-06 11:01:00 +0800230
TDYa127eead4ac2012-06-03 07:15:25 -0700231 std::vector<llvm::Value*> args;
232 args.push_back(arg_iter++); // method
233 args.push_back(arg_iter++); // this
234 args.push_back(irb_.Runtime().EmitGetCurrentThread()); // thread
Logan Chien7a2a23a2012-06-06 11:01:00 +0800235
TDYa127eead4ac2012-06-03 07:15:25 -0700236 for (size_t i = 1; i < shorty_size; ++i) {
237 args.push_back(arg_iter++);
238 }
Logan Chien7a2a23a2012-06-06 11:01:00 +0800239
TDYa127eead4ac2012-06-03 07:15:25 -0700240 if (shorty[0] != 'V') {
241 args.push_back(jvalue_temp);
242 }
243
244 // Call ProxyInvokeHandler
245 // TODO: Partial inline ProxyInvokeHandler, don't use VarArg.
246 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
Logan Chien7a2a23a2012-06-06 11:01:00 +0800247
TDYa127eead4ac2012-06-03 07:15:25 -0700248 if (shorty[0] != 'V') {
249 llvm::Value* result_addr =
250 irb_.CreateBitCast(jvalue_temp, accurate_ret_type->getPointerTo());
251 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
252 irb_.CreateRet(retval);
253 } else {
254 irb_.CreateRetVoid();
255 }
256
257 // Verify the generated function
258 VERIFY_LLVM_FUNCTION(*func);
259
Logan Chien971bf3f2012-05-01 15:47:55 +0800260 cunit_->Materialize();
TDYa127eead4ac2012-06-03 07:15:25 -0700261
Logan Chien598c5132012-04-28 22:00:44 +0800262 return new CompiledInvokeStub(cunit_->GetInstructionSet(),
Logan Chien971bf3f2012-05-01 15:47:55 +0800263 cunit_->GetCompiledCode());
Logan Chienf04364f2012-02-10 12:01:39 +0800264}
265
266
267} // namespace compiler_llvm
268} // namespace art