Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 1 | /* |
| 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 Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 19 | #include "compilation_unit.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 20 | #include "compiled_method.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 21 | #include "compiler.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 22 | #include "compiler_llvm.h" |
| 23 | #include "ir_builder.h" |
| 24 | #include "logging.h" |
| 25 | #include "object.h" |
| 26 | #include "runtime_support_func.h" |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 27 | #include "utils_llvm.h" |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 28 | |
| 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 | |
| 38 | namespace art { |
| 39 | namespace compiler_llvm { |
| 40 | |
| 41 | using namespace runtime_support; |
| 42 | |
| 43 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 44 | UpcallCompiler::UpcallCompiler(CompilationUnit* cunit, Compiler& compiler) |
| 45 | : cunit_(cunit), compiler_(&compiler), module_(cunit_->GetModule()), |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 46 | context_(cunit_->GetLLVMContext()), irb_(*cunit_->GetIRBuilder()), |
| 47 | elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) { |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | CompiledInvokeStub* 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 Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 58 | std::string func_name(ElfFuncName(elf_func_idx_)); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 59 | |
| 60 | // Get argument types |
| 61 | llvm::Type* arg_types[] = { |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 62 | 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 Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 88 | 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 Liao | 90d5099 | 2012-02-19 03:32:05 -0800 | [diff] [blame] | 142 | LOG(FATAL) << "Unexpected arg shorty for invoke stub: " << shorty[i]; |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 143 | } |
| 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 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 154 | 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 Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 163 | |
| 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 Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 176 | // Verify the generated function |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 177 | llvm::verifyFunction(*func, llvm::PrintMessageAction); |
| 178 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 179 | // 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 Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 186 | return new CompiledInvokeStub(cunit_->GetElfIndex(), elf_func_idx_); |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | } // namespace compiler_llvm |
| 191 | } // namespace art |