Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Objective-C code as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 14 | #include "CGObjCRuntime.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 18 | #include "llvm/Constant.h" |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
| 20 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 24 | /// Emits an instance of NSConstantString representing the object. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 25 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E){ |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 26 | return CGM.getObjCRuntime()->GenerateConstantString( |
| 27 | E->getString()->getStrData(), E->getString()->getByteLength()); |
| 28 | } |
| 29 | |
| 30 | /// Emit a selector. |
| 31 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 32 | // Untyped selector. |
| 33 | // Note that this implementation allows for non-constant strings to be passed |
| 34 | // as arguments to @selector(). Currently, the only thing preventing this |
| 35 | // behaviour is the type checking in the front end. |
Chris Lattner | 42ba3e7 | 2008-06-26 04:38:58 +0000 | [diff] [blame^] | 36 | return CGM.getObjCRuntime()->GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | llvm::Value *CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { |
| 42 | // Only the lookup mechanism and first two arguments of the method |
| 43 | // implementation vary between runtimes. We can get the receiver and |
| 44 | // arguments in generic code. |
| 45 | |
| 46 | CGObjCRuntime *Runtime = CGM.getObjCRuntime(); |
| 47 | const Expr *ReceiverExpr = E->getReceiver(); |
| 48 | bool isSuperMessage = false; |
| 49 | // Find the receiver |
| 50 | llvm::Value *Receiver; |
| 51 | if (!ReceiverExpr) { |
| 52 | const char * classname = E->getClassName()->getName(); |
| 53 | if (!strcmp(classname, "super")) { |
| 54 | classname = E->getMethodDecl()->getClassInterface()->getName(); |
| 55 | } |
| 56 | llvm::Value *ClassName = CGM.GetAddrOfConstantString(classname); |
| 57 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 58 | Receiver = Runtime->LookupClass(Builder, ClassName); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 59 | } else if (isa<PreDefinedExpr>(E->getReceiver())) { |
| 60 | assert(cast<PreDefinedExpr>(E->getReceiver())->getIdentType() == |
| 61 | PreDefinedExpr::ObjCSuper); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 62 | isSuperMessage = true; |
| 63 | Receiver = LoadObjCSelf(); |
| 64 | } else { |
| 65 | Receiver = EmitScalarExpr(E->getReceiver()); |
| 66 | } |
| 67 | |
| 68 | // Process the arguments |
| 69 | unsigned ArgC = E->getNumArgs(); |
| 70 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 71 | for (unsigned i = 0; i != ArgC; ++i) { |
| 72 | const Expr *ArgExpr = E->getArg(i); |
| 73 | QualType ArgTy = ArgExpr->getType(); |
| 74 | if (!hasAggregateLLVMType(ArgTy)) { |
| 75 | // Scalar argument is passed by-value. |
| 76 | Args.push_back(EmitScalarExpr(ArgExpr)); |
| 77 | } else if (ArgTy->isAnyComplexType()) { |
| 78 | // Make a temporary alloca to pass the argument. |
| 79 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 80 | EmitComplexExprIntoAddr(ArgExpr, DestMem, false); |
| 81 | Args.push_back(DestMem); |
| 82 | } else { |
| 83 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 84 | EmitAggExpr(ArgExpr, DestMem, false); |
| 85 | Args.push_back(DestMem); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Get the selector string |
| 90 | std::string SelStr = E->getSelector().getName(); |
| 91 | llvm::Constant *Selector = CGM.GetAddrOfConstantString(SelStr); |
| 92 | |
| 93 | llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0); |
| 94 | if (isSuperMessage) { |
| 95 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl); |
| 96 | assert(OMD && "super is only valid in an Objective-C method"); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 97 | const char *SuperClass = |
| 98 | OMD->getClassInterface()->getSuperClass()->getName(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 99 | return Runtime->GenerateMessageSendSuper(Builder, ConvertType(E->getType()), |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 100 | Receiver, SuperClass, |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 101 | Receiver, E->getSelector(), |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 102 | &Args[0], Args.size()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 103 | } |
| 104 | return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()), |
| 105 | LoadObjCSelf(), |
| 106 | Receiver, SelPtr, |
| 107 | &Args[0], Args.size()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 110 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 111 | /// its pointer, name, and types registered in the class struture. |
| 112 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 113 | |
| 114 | llvm::SmallVector<const llvm::Type *, 16> ParamTypes; |
| 115 | for (unsigned i=0 ; i<OMD->param_size() ; i++) { |
| 116 | const llvm::Type *Ty = ConvertType(OMD->getParamDecl(i)->getType()); |
| 117 | if (Ty->isFirstClassType()) |
| 118 | ParamTypes.push_back(Ty); |
| 119 | else |
| 120 | ParamTypes.push_back(llvm::PointerType::getUnqual(Ty)); |
| 121 | } |
| 122 | std::string CategoryName = ""; |
| 123 | if (ObjCCategoryImplDecl *OCD = |
| 124 | dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext())) { |
| 125 | CategoryName = OCD->getName(); |
| 126 | } |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 127 | const llvm::Type *ReturnTy = |
| 128 | CGM.getTypes().ConvertReturnType(OMD->getResultType()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 129 | CurFn = CGM.getObjCRuntime()->MethodPreamble( |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 130 | OMD->getClassInterface()->getName(), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 131 | CategoryName, |
| 132 | OMD->getSelector().getName(), |
| 133 | ReturnTy, |
| 134 | llvm::PointerType::getUnqual( |
| 135 | llvm::Type::Int32Ty), |
| 136 | ParamTypes.begin(), |
| 137 | OMD->param_size(), |
| 138 | !OMD->isInstance(), |
| 139 | OMD->isVariadic()); |
| 140 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 141 | |
| 142 | // Create a marker to make it easy to insert allocas into the entryblock |
| 143 | // later. Don't create this with the builder, because we don't want it |
| 144 | // folded. |
| 145 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 146 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 147 | EntryBB); |
| 148 | |
| 149 | FnRetTy = OMD->getResultType(); |
| 150 | CurFuncDecl = OMD; |
| 151 | |
| 152 | Builder.SetInsertPoint(EntryBB); |
| 153 | |
| 154 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 155 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 156 | |
| 157 | if (hasAggregateLLVMType(OMD->getResultType())) { |
| 158 | ++AI; |
| 159 | } |
| 160 | // Add implicit parameters to the decl map. |
| 161 | // TODO: Add something to AST to let the runtime specify the names and types |
| 162 | // of these. |
| 163 | |
| 164 | llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()]; |
| 165 | const llvm::Type *IPTy = AI->getType(); |
| 166 | llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() + |
| 167 | ".addr", AllocaInsertPt); |
| 168 | // Store the initial value into the alloca. |
| 169 | Builder.CreateStore(AI, DeclPtr); |
| 170 | SelfEntry = DeclPtr; |
| 171 | ++AI; |
| 172 | llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()]; |
| 173 | IPTy = AI->getType(); |
| 174 | DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() + |
| 175 | ".addr", AllocaInsertPt); |
| 176 | // Store the initial value into the alloca. |
| 177 | Builder.CreateStore(AI, DeclPtr); |
| 178 | CmdEntry = DeclPtr; |
| 179 | |
| 180 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 181 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 182 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 183 | } |
| 184 | |
| 185 | GenerateFunction(OMD->getBody()); |
| 186 | } |
| 187 | |
| 188 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) |
| 189 | { |
| 190 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) { |
| 191 | ValueDecl *Decl = OMD->getSelfDecl(); |
| 192 | llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))]; |
| 193 | return Builder.CreateLoad(SelfPtr, "self"); |
| 194 | } |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 198 | CGObjCRuntime::~CGObjCRuntime() {} |