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) { |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 51 | const ObjCInterfaceDecl *OID = E->getClassInfo().first; |
| 52 | |
| 53 | // Very special case, super send in class method. The receiver is |
| 54 | // self (the class object) and the send uses super semantics. |
| 55 | if (!OID) { |
| 56 | assert(!strcmp(E->getClassName()->getName(), "super") && |
| 57 | "Unexpected missing class interface in message send."); |
| 58 | OID = E->getMethodDecl()->getClassInterface(); |
| 59 | isSuperMessage = true; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 60 | } |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 61 | |
| 62 | Receiver = Runtime.GetClass(Builder, OID); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 63 | } else if (const PredefinedExpr *PDE = |
| 64 | dyn_cast<PredefinedExpr>(E->getReceiver())) { |
| 65 | assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 66 | isSuperMessage = true; |
| 67 | Receiver = LoadObjCSelf(); |
| 68 | } else { |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 69 | Receiver = EmitScalarExpr(E->getReceiver()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Process the arguments |
| 73 | unsigned ArgC = E->getNumArgs(); |
| 74 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 75 | for (unsigned i = 0; i != ArgC; ++i) { |
| 76 | const Expr *ArgExpr = E->getArg(i); |
| 77 | QualType ArgTy = ArgExpr->getType(); |
| 78 | if (!hasAggregateLLVMType(ArgTy)) { |
| 79 | // Scalar argument is passed by-value. |
| 80 | Args.push_back(EmitScalarExpr(ArgExpr)); |
| 81 | } else if (ArgTy->isAnyComplexType()) { |
| 82 | // Make a temporary alloca to pass the argument. |
| 83 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 84 | EmitComplexExprIntoAddr(ArgExpr, DestMem, false); |
| 85 | Args.push_back(DestMem); |
| 86 | } else { |
| 87 | llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); |
| 88 | EmitAggExpr(ArgExpr, DestMem, false); |
| 89 | Args.push_back(DestMem); |
| 90 | } |
| 91 | } |
| 92 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 93 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 94 | // super is only valid in an Objective-C method |
| 95 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 96 | return Runtime.GenerateMessageSendSuper(Builder, ConvertType(E->getType()), |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 97 | OMD->getClassInterface()->getSuperClass(), |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 98 | Receiver, E->getSelector(), |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 99 | &Args[0], Args.size()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 100 | } |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 101 | return Runtime.GenerateMessageSend(Builder, ConvertType(E->getType()), |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 102 | Receiver, E->getSelector(), |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 103 | &Args[0], Args.size()); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 106 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 107 | /// its pointer, name, and types registered in the class struture. |
| 108 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 109 | CurFn = CGM.getObjCRuntime().GenerateMethod(OMD); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 110 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 111 | |
| 112 | // Create a marker to make it easy to insert allocas into the entryblock |
| 113 | // later. Don't create this with the builder, because we don't want it |
| 114 | // folded. |
| 115 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 116 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 117 | EntryBB); |
| 118 | |
| 119 | FnRetTy = OMD->getResultType(); |
| 120 | CurFuncDecl = OMD; |
| 121 | |
| 122 | Builder.SetInsertPoint(EntryBB); |
| 123 | |
| 124 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 125 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 126 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 127 | // Name the struct return argument. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 128 | if (hasAggregateLLVMType(OMD->getResultType())) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 129 | AI->setName("agg.result"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 130 | ++AI; |
| 131 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 132 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 133 | // Add implicit parameters to the decl map. |
| 134 | EmitParmDecl(*OMD->getSelfDecl(), AI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 135 | ++AI; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 136 | |
| 137 | EmitParmDecl(*OMD->getCmdDecl(), AI); |
| 138 | ++AI; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 139 | |
| 140 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 141 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 142 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 143 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 144 | assert(AI == CurFn->arg_end() && "Argument mismatch"); |
| 145 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 146 | GenerateFunction(OMD->getBody()); |
| 147 | } |
| 148 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame^] | 149 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) { |
| 150 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 151 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 154 | CGObjCRuntime::~CGObjCRuntime() {} |