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