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 "runtime_support_builder_x86.h"
18 
19 #include "base/stringprintf.h"
20 #include "ir_builder.h"
21 #include "thread.h"
22 #include "utils_llvm.h"
23 
24 #include <llvm/IR/DerivedTypes.h>
25 #include <llvm/IR/Function.h>
26 #include <llvm/IR/InlineAsm.h>
27 #include <llvm/IR/Module.h>
28 #include <llvm/IR/Type.h>
29 
30 #include <vector>
31 
32 using ::llvm::CallInst;
33 using ::llvm::Function;
34 using ::llvm::FunctionType;
35 using ::llvm::InlineAsm;
36 using ::llvm::Type;
37 using ::llvm::UndefValue;
38 using ::llvm::Value;
39 
40 namespace art {
41 namespace llvm {
42 
43 
EmitGetCurrentThread()44 Value* RuntimeSupportBuilderX86::EmitGetCurrentThread() {
45   Function* ori_func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
46   std::string inline_asm(StringPrintf("mov %%fs:%d, $0", Thread::SelfOffset().Int32Value()));
47   InlineAsm* func = InlineAsm::get(ori_func->getFunctionType(), inline_asm, "=r", false);
48   CallInst* thread = irb_.CreateCall(func);
49   thread->setDoesNotAccessMemory();
50   irb_.SetTBAA(thread, kTBAAConstJObject);
51   return thread;
52 }
53 
EmitLoadFromThreadOffset(int64_t offset,Type * type,TBAASpecialType s_ty)54 Value* RuntimeSupportBuilderX86::EmitLoadFromThreadOffset(int64_t offset, Type* type,
55                                                           TBAASpecialType s_ty) {
56   FunctionType* func_ty = FunctionType::get(/*Result=*/type,
57                                             /*isVarArg=*/false);
58   std::string inline_asm(StringPrintf("mov %%fs:%d, $0", static_cast<int>(offset)));
59   InlineAsm* func = InlineAsm::get(func_ty, inline_asm, "=r", true);
60   CallInst* result = irb_.CreateCall(func);
61   result->setOnlyReadsMemory();
62   irb_.SetTBAA(result, s_ty);
63   return result;
64 }
65 
EmitStoreToThreadOffset(int64_t offset,Value * value,TBAASpecialType s_ty)66 void RuntimeSupportBuilderX86::EmitStoreToThreadOffset(int64_t offset, Value* value,
67                                                        TBAASpecialType s_ty) {
68   FunctionType* func_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_),
69                                             /*Params=*/value->getType(),
70                                             /*isVarArg=*/false);
71   std::string inline_asm(StringPrintf("mov $0, %%fs:%d", static_cast<int>(offset)));
72   InlineAsm* func = InlineAsm::get(func_ty, inline_asm, "r", true);
73   CallInst* call_inst = irb_.CreateCall(func, value);
74   irb_.SetTBAA(call_inst, s_ty);
75 }
76 
EmitSetCurrentThread(Value *)77 Value* RuntimeSupportBuilderX86::EmitSetCurrentThread(Value*) {
78   /* Nothing to be done. */
79   return UndefValue::get(irb_.getJObjectTy());
80 }
81 
82 
83 }  // namespace llvm
84 }  // namespace art
85