blob: 255e99a36b41d2f260178213b90b7d8cfec5c441 [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"
TDYa12728f1a142012-03-15 21:51:52 -070029#include "runtime_support_func.h"
TDYa12731a99332012-03-19 02:58:02 -070030#include "shadow_frame.h"
Logan Chien88894ee2012-02-13 16:42:22 +080031#include "utils_llvm.h"
32
TDYa12728f1a142012-03-15 21:51:52 -070033#include <llvm/Analysis/Verifier.h>
34#include <llvm/BasicBlock.h>
Logan Chien88894ee2012-02-13 16:42:22 +080035#include <llvm/DerivedTypes.h>
36#include <llvm/Function.h>
37#include <llvm/Type.h>
38
39namespace art {
40namespace compiler_llvm {
41
42
Logan Chien8b977d32012-02-21 19:14:55 +080043JniCompiler::JniCompiler(CompilationUnit* cunit,
Logan Chien88894ee2012-02-13 16:42:22 +080044 Compiler const& compiler,
45 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080046: cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()),
47 context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()),
Logan Chien88894ee2012-02-13 16:42:22 +080048 oat_compilation_unit_(oat_compilation_unit),
49 access_flags_(oat_compilation_unit->access_flags_),
50 method_idx_(oat_compilation_unit->method_idx_),
51 class_linker_(oat_compilation_unit->class_linker_),
52 class_loader_(oat_compilation_unit->class_loader_),
53 dex_cache_(oat_compilation_unit->dex_cache_),
54 dex_file_(oat_compilation_unit->dex_file_),
55 method_(dex_cache_->GetResolvedMethod(method_idx_)) {
56
57 // Check: Ensure that the method is resolved
58 CHECK_NE(method_, static_cast<art::Method*>(NULL));
59
60 // Check: Ensure that JNI compiler will only get "native" method
61 CHECK((access_flags_ & kAccNative) != 0);
62}
63
64
65CompiledMethod* JniCompiler::Compile() {
TDYa12728f1a142012-03-15 21:51:52 -070066 bool is_static = method_->IsStatic();
67
Logan Chien88894ee2012-02-13 16:42:22 +080068 CreateFunction();
69
TDYa12728f1a142012-03-15 21:51:52 -070070 // Set argument name
71 llvm::Function::arg_iterator arg_begin(func_->arg_begin());
72 llvm::Function::arg_iterator arg_end(func_->arg_end());
73 llvm::Function::arg_iterator arg_iter(arg_begin);
74
75 DCHECK_NE(arg_iter, arg_end);
76 arg_iter->setName("method");
77 llvm::Value* method_object_addr = arg_iter++;
78
79 // Actual argument (ignore method)
80 arg_begin = arg_iter;
81
82 // Count the number of Object* arguments
83 uint32_t sirt_size = (is_static ? 1 : 0); // Class object for static function
84 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
85 arg_iter->setName(StringPrintf("a%u", i));
86 if (arg_iter->getType() == irb_.getJObjectTy()) {
87 ++sirt_size;
88 }
89 }
90
91 // Start to build IR
92 irb_.SetInsertPoint(basic_block_);
93
TDYa12731a99332012-03-19 02:58:02 -070094 // Get thread object
TDYa12728f1a142012-03-15 21:51:52 -070095 llvm::Value* thread_object_addr =
96 irb_.CreateCall(irb_.GetRuntime(runtime_support::GetCurrentThread));
97
98 // Shadow stack
99 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
100 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
101
102 // Zero-initialization of the shadow frame
103 llvm::ConstantAggregateZero* zero_initializer =
104 llvm::ConstantAggregateZero::get(shadow_frame_type);
TDYa12728f1a142012-03-15 21:51:52 -0700105 irb_.CreateStore(zero_initializer, shadow_frame_);
106
TDYa12728f1a142012-03-15 21:51:52 -0700107 // Store the method pointer
TDYa12731a99332012-03-19 02:58:02 -0700108 llvm::Value* method_field_addr =
109 irb_.CreatePtrDisp(shadow_frame_,
110 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
111 irb_.getJObjectTy()->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700112 irb_.CreateStore(method_object_addr, method_field_addr);
113
114 // Store the number of the pointer slots
TDYa12731a99332012-03-19 02:58:02 -0700115 StoreToObjectOffset(shadow_frame_,
116 ShadowFrame::NumberOfReferencesOffset(),
117 irb_.getInt32(sirt_size));
TDYa12728f1a142012-03-15 21:51:52 -0700118
119 // Push the shadow frame
120 llvm::Value* shadow_frame_upcast = irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
121 irb_.CreateCall(irb_.GetRuntime(runtime_support::PushShadowFrame), shadow_frame_upcast);
122
123 // Set top of managed stack to the method field in the SIRT
TDYa12731a99332012-03-19 02:58:02 -0700124 StoreToObjectOffset(thread_object_addr,
125 Thread::TopOfManagedStackOffset().Int32Value(),
126 method_field_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700127
128 // Get JNIEnv
129 llvm::Value* jni_env_object_addr = LoadFromObjectOffset(thread_object_addr,
130 Thread::JniEnvOffset().Int32Value(),
131 irb_.getJObjectTy());
132
133 // Set thread state to kNative
TDYa12731a99332012-03-19 02:58:02 -0700134 StoreToObjectOffset(thread_object_addr,
135 Thread::StateOffset().Int32Value(),
136 irb_.getInt32(Thread::kNative));
TDYa12728f1a142012-03-15 21:51:52 -0700137
138 // Get callee code_addr
139 llvm::Value* code_addr =
140 LoadFromObjectOffset(method_object_addr,
141 Method::NativeMethodOffset().Int32Value(),
142 GetFunctionType(method_idx_, is_static, true)->getPointerTo());
143
144
145 // Load actual parameters
146 std::vector<llvm::Value*> args;
147
TDYa12731a99332012-03-19 02:58:02 -0700148 // The 1st parameter: JNIEnv*
TDYa12728f1a142012-03-15 21:51:52 -0700149 args.push_back(jni_env_object_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700150
TDYa12731a99332012-03-19 02:58:02 -0700151 // Variables for GetElementPtr
152 llvm::Value* gep_index[] = {
153 irb_.getInt32(0), // No displacement for shadow frame pointer
154 irb_.getInt32(1), // SIRT
155 NULL,
156 };
157
TDYa12728f1a142012-03-15 21:51:52 -0700158 size_t sirt_member_index = 0;
159
160 // Push class argument if this method is static
161 if (is_static) {
TDYa12731a99332012-03-19 02:58:02 -0700162 // Load class object
TDYa12728f1a142012-03-15 21:51:52 -0700163 llvm::Value* class_object_addr =
164 LoadFromObjectOffset(method_object_addr,
165 Method::DeclaringClassOffset().Int32Value(),
166 irb_.getJObjectTy());
167 gep_index[2] = irb_.getInt32(sirt_member_index++);
TDYa12731a99332012-03-19 02:58:02 -0700168 // Store the class argument to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700169 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
170 irb_.CreateStore(class_object_addr, sirt_field_addr);
171 args.push_back(irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
172 }
TDYa12731a99332012-03-19 02:58:02 -0700173 // Store arguments to SIRT, and push back to args
TDYa12728f1a142012-03-15 21:51:52 -0700174 for (arg_iter = arg_begin; arg_iter != arg_end; ++arg_iter) {
175 if (arg_iter->getType() == irb_.getJObjectTy()) {
TDYa12731a99332012-03-19 02:58:02 -0700176 // Store the reference type arguments to SIRT
TDYa12728f1a142012-03-15 21:51:52 -0700177 gep_index[2] = irb_.getInt32(sirt_member_index++);
178 llvm::Value* sirt_field_addr = irb_.CreateGEP(shadow_frame_, gep_index);
179 irb_.CreateStore(arg_iter, sirt_field_addr);
180 // Note null is placed in the SIRT but the jobject passed to the native code must be null
181 // (not a pointer into the SIRT as with regular references).
182 llvm::Value* equal_null = irb_.CreateICmpEQ(arg_iter, irb_.getJNull());
183 llvm::Value* arg =
184 irb_.CreateSelect(equal_null,
185 irb_.getJNull(),
186 irb_.CreateBitCast(sirt_field_addr, irb_.getJObjectTy()));
187 args.push_back(arg);
188 } else {
189 args.push_back(arg_iter);
190 }
191 }
192
193
194 // saved_local_ref_cookie = env->local_ref_cookie
195 llvm::Value* saved_local_ref_cookie =
196 LoadFromObjectOffset(jni_env_object_addr,
197 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
198 irb_.getInt32Ty());
199
200 // env->local_ref_cookie = env->locals.segment_state
201 llvm::Value* segment_state =
202 LoadFromObjectOffset(jni_env_object_addr,
203 JNIEnvExt::SegmentStateOffset().Int32Value(),
204 irb_.getInt32Ty());
205 StoreToObjectOffset(jni_env_object_addr,
206 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700207 segment_state);
208
209
210 // Call!!!
211 llvm::Value* retval = irb_.CreateCall(code_addr, args);
212
213
214 // Set thread state to kRunnable
TDYa12731a99332012-03-19 02:58:02 -0700215 StoreToObjectOffset(thread_object_addr,
216 Thread::StateOffset().Int32Value(),
217 irb_.getInt32(Thread::kRunnable));
TDYa12728f1a142012-03-15 21:51:52 -0700218
219 // Get return shorty
220 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx_);
221 uint32_t shorty_size;
222 char ret_shorty = dex_file_->GetMethodShorty(method_id, &shorty_size)[0];
223 CHECK_GE(shorty_size, 1u);
224
225 if (ret_shorty == 'L') {
226 // If the return value is reference, it may point to SIRT, we should decode it.
227 retval = irb_.CreateCall2(irb_.GetRuntime(runtime_support::DecodeJObjectInThread),
TDYa12731a99332012-03-19 02:58:02 -0700228 thread_object_addr,
229 retval);
TDYa12728f1a142012-03-15 21:51:52 -0700230 }
231
232 // env->locals.segment_state = env->local_ref_cookie
233 llvm::Value* local_ref_cookie =
234 LoadFromObjectOffset(jni_env_object_addr,
235 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
236 irb_.getInt32Ty());
237 StoreToObjectOffset(jni_env_object_addr,
238 JNIEnvExt::SegmentStateOffset().Int32Value(),
TDYa12728f1a142012-03-15 21:51:52 -0700239 local_ref_cookie);
240
241 // env->local_ref_cookie = saved_local_ref_cookie
TDYa12731a99332012-03-19 02:58:02 -0700242 StoreToObjectOffset(jni_env_object_addr,
243 JNIEnvExt::LocalRefCookieOffset().Int32Value(),
244 saved_local_ref_cookie);
TDYa12728f1a142012-03-15 21:51:52 -0700245
246 // Pop the shadow frame
247 irb_.CreateCall(irb_.GetRuntime(runtime_support::PopShadowFrame));
248
249 // Return!
250 if (ret_shorty != 'V') {
251 irb_.CreateRet(retval);
252 } else {
253 irb_.CreateRetVoid();
254 }
255
256 // For debug
257 //func_->dump();
258
259 // Verify the generated bitcode
260 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
261
Logan Chien6920bce2012-03-17 21:44:01 +0800262 return new CompiledMethod(cunit_->GetInstructionSet(),
263 cunit_->GetElfIndex());
Logan Chien88894ee2012-02-13 16:42:22 +0800264}
265
266
267void JniCompiler::CreateFunction() {
268 // LLVM function name
269 std::string func_name(LLVMLongName(method_));
270
271 // Get function type
272 llvm::FunctionType* func_type =
TDYa12728f1a142012-03-15 21:51:52 -0700273 GetFunctionType(method_idx_, method_->IsStatic(), false);
Logan Chien88894ee2012-02-13 16:42:22 +0800274
275 // Create function
276 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
277 func_name, module_);
TDYa12728f1a142012-03-15 21:51:52 -0700278
279 // Create basic block
280 basic_block_ = llvm::BasicBlock::Create(*context_, "B0", func_);
Logan Chien88894ee2012-02-13 16:42:22 +0800281}
282
283
284llvm::FunctionType* JniCompiler::GetFunctionType(uint32_t method_idx,
TDYa12728f1a142012-03-15 21:51:52 -0700285 bool is_static, bool is_target_function) {
Logan Chien88894ee2012-02-13 16:42:22 +0800286 // Get method signature
287 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
288
289 uint32_t shorty_size;
290 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
291 CHECK_GE(shorty_size, 1u);
292
293 // Get return type
294 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
295
296 // Get argument type
297 std::vector<llvm::Type*> args_type;
298
299 args_type.push_back(irb_.getJObjectTy()); // method object pointer
300
TDYa12728f1a142012-03-15 21:51:52 -0700301 if (!is_static || is_target_function) {
302 // "this" object pointer for non-static
303 // "class" object pointer for static
304 args_type.push_back(irb_.getJType('L', kAccurate));
Logan Chien88894ee2012-02-13 16:42:22 +0800305 }
306
307 for (uint32_t i = 1; i < shorty_size; ++i) {
308 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
309 }
310
311 return llvm::FunctionType::get(ret_type, args_type, false);
312}
313
TDYa12731a99332012-03-19 02:58:02 -0700314llvm::Value* JniCompiler::LoadFromObjectOffset(llvm::Value* object_addr,
315 int32_t offset,
TDYa12728f1a142012-03-15 21:51:52 -0700316 llvm::Type* type) {
317 // Convert offset to llvm::value
318 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
319 // Calculate the value's address
320 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
321 // Load
322 return irb_.CreateLoad(value_addr);
323}
324
TDYa12731a99332012-03-19 02:58:02 -0700325void JniCompiler::StoreToObjectOffset(llvm::Value* object_addr,
326 int32_t offset,
327 llvm::Value* new_value) {
TDYa12728f1a142012-03-15 21:51:52 -0700328 // Convert offset to llvm::value
329 llvm::Value* llvm_offset = irb_.getPtrEquivInt(offset);
330 // Calculate the value's address
TDYa12731a99332012-03-19 02:58:02 -0700331 llvm::Value* value_addr = irb_.CreatePtrDisp(object_addr,
332 llvm_offset,
333 new_value->getType()->getPointerTo());
TDYa12728f1a142012-03-15 21:51:52 -0700334 // Store
TDYa12731a99332012-03-19 02:58:02 -0700335 irb_.CreateStore(new_value, value_addr);
TDYa12728f1a142012-03-15 21:51:52 -0700336}
Logan Chien88894ee2012-02-13 16:42:22 +0800337
338} // namespace compiler_llvm
339} // namespace art