blob: 20bd94b71d8b4ff0e69252f2bc150cfba637b80d [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"
27
28#include <llvm/Analysis/Verifier.h>
29#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
Logan Chien8b977d32012-02-21 19:14:55 +080043UpcallCompiler::UpcallCompiler(CompilationUnit* cunit, Compiler& compiler)
44: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
45 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()) {
Logan Chienf04364f2012-02-10 12:01:39 +080046}
47
48
49CompiledInvokeStub* UpcallCompiler::CreateStub(bool is_static,
50 char const* shorty) {
51
52 CHECK_NE(shorty, static_cast<char const*>(NULL));
53 size_t shorty_size = strlen(shorty);
54
55 // Function name
56 std::string func_name;
57
58 if (is_static) {
59 StringAppendF(&func_name, "ArtSUpcall_%s", shorty);
60 } else {
61 StringAppendF(&func_name, "ArtUpcall_%s", shorty);
62 }
63
64 // Get argument types
65 llvm::Type* arg_types[] = {
Logan Chienf04364f2012-02-10 12:01:39 +080066 irb_.getJObjectTy(), // Method object pointer
67 irb_.getJObjectTy(), // "this" object pointer (NULL for static)
68 irb_.getJObjectTy(), // Thread object pointer
69 irb_.getJValueTy()->getPointerTo(),
70 irb_.getJValueTy()->getPointerTo(),
71 };
72
73 // Function type
74 llvm::FunctionType* func_type =
75 llvm::FunctionType::get(irb_.getVoidTy(), arg_types, false);
76
77 // Create function
78 llvm::Function* func =
79 llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
80 func_name, module_);
81
82
83 // Create basic block for the body of this function
84 llvm::BasicBlock* block_body =
85 llvm::BasicBlock::Create(*context_, "upcall", func);
86
87 irb_.SetInsertPoint(block_body);
88
89 // Actual arguments
90 llvm::Function::arg_iterator arg_iter = func->arg_begin();
91
Logan Chienf04364f2012-02-10 12:01:39 +080092 llvm::Value* method_object_addr = arg_iter++;
93 llvm::Value* callee_this_addr = arg_iter++;
94 llvm::Value* thread_object_addr = arg_iter++;
95 llvm::Value* actual_args_array_addr = arg_iter++;
96 llvm::Value* retval_addr = arg_iter++;
97
98 // Setup thread pointer
99 irb_.CreateCall(irb_.GetRuntime(SetCurrentThread), thread_object_addr);
100
101 // Accurate function type
102 llvm::Type* accurate_ret_type = irb_.getJType(shorty[0], kAccurate);
103
104 std::vector<llvm::Type*> accurate_arg_types;
105
106 accurate_arg_types.push_back(irb_.getJObjectTy()); // method object pointer
107
108 if (!is_static) {
109 accurate_arg_types.push_back(irb_.getJObjectTy());
110 }
111
112 for (size_t i = 1; i < shorty_size; ++i) {
113 accurate_arg_types.push_back(irb_.getJType(shorty[i], kAccurate));
114 }
115
116 llvm::FunctionType* accurate_func_type =
117 llvm::FunctionType::get(accurate_ret_type, accurate_arg_types, false);
118
119 // Load actual arguments
120 std::vector<llvm::Value*> args;
121
122 args.push_back(method_object_addr);
123
124 if (!is_static) {
125 args.push_back(callee_this_addr);
126 }
127
128 for (size_t i = 1; i < shorty_size; ++i) {
129 char arg_shorty = shorty[i];
130
131 if (arg_shorty == 'Z' || arg_shorty == 'B' || arg_shorty == 'C' ||
132 arg_shorty == 'S' || arg_shorty == 'I' || arg_shorty == 'J' ||
133 arg_shorty == 'F' || arg_shorty == 'D' || arg_shorty == 'L') {
134
135 llvm::Type* arg_type =
136 irb_.getJType(shorty[i], kAccurate)->getPointerTo();
137
138 llvm::Value* arg_jvalue_addr =
139 irb_.CreateConstGEP1_32(actual_args_array_addr, i - 1);
140
141 llvm::Value* arg_addr = irb_.CreateBitCast(arg_jvalue_addr, arg_type);
142
143 args.push_back(irb_.CreateLoad(arg_addr));
144
145 } else {
Shih-wei Liao90d50992012-02-19 03:32:05 -0800146 LOG(FATAL) << "Unexpected arg shorty for invoke stub: " << shorty[i];
Logan Chienf04364f2012-02-10 12:01:39 +0800147 }
148 }
149
150 // Invoke managed method now!
151 llvm::Value* code_field_offset_value =
152 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
153
154 llvm::Value* code_field_addr =
155 irb_.CreatePtrDisp(method_object_addr, code_field_offset_value,
156 accurate_func_type->getPointerTo()->getPointerTo());
157
158 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
159
160 llvm::Value* retval = irb_.CreateCall(code_addr, args);
161
162 // Store the returned value
163 if (shorty[0] != 'V') {
164 llvm::Value* ret_addr =
165 irb_.CreateBitCast(retval_addr, accurate_ret_type->getPointerTo());
166
167 irb_.CreateStore(retval, ret_addr);
168 }
169
170 irb_.CreateRetVoid();
171
Logan Chien8b977d32012-02-21 19:14:55 +0800172 // Verify the generated function
Logan Chienf04364f2012-02-10 12:01:39 +0800173 llvm::verifyFunction(*func, llvm::PrintMessageAction);
174
Logan Chien8b977d32012-02-21 19:14:55 +0800175 // Add the memory usage approximation of the compilation unit
176 cunit_->AddMemUsageApproximation((shorty_size * 3 + 8) * 500);
177 // NOTE: We will emit 3 LLVM instructions per shorty for the argument,
178 // plus 3 for pointer arithmetic, and 5 for code_addr, retval, ret_addr,
179 // store ret_addr, and ret_void. Beside, we guess that we have to use
180 // 50 bytes to represent one LLVM instruction.
181
Logan Chien6920bce2012-03-17 21:44:01 +0800182 return new CompiledInvokeStub(cunit_->GetElfIndex());
Logan Chienf04364f2012-02-10 12:01:39 +0800183}
184
185
186} // namespace compiler_llvm
187} // namespace art