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