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); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 26 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 27 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +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 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 39 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 40 | // FIXME: This should pass the Decl not the name. |
| 41 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 42 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 43 | |
| 44 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame^] | 45 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 46 | // Only the lookup mechanism and first two arguments of the method |
| 47 | // implementation vary between runtimes. We can get the receiver and |
| 48 | // arguments in generic code. |
| 49 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 50 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 51 | const Expr *ReceiverExpr = E->getReceiver(); |
| 52 | bool isSuperMessage = false; |
| 53 | // Find the receiver |
| 54 | llvm::Value *Receiver; |
| 55 | if (!ReceiverExpr) { |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 56 | const ObjCInterfaceDecl *OID = E->getClassInfo().first; |
| 57 | |
| 58 | // Very special case, super send in class method. The receiver is |
| 59 | // self (the class object) and the send uses super semantics. |
| 60 | if (!OID) { |
| 61 | assert(!strcmp(E->getClassName()->getName(), "super") && |
| 62 | "Unexpected missing class interface in message send."); |
| 63 | OID = E->getMethodDecl()->getClassInterface(); |
| 64 | isSuperMessage = true; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 65 | } |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 66 | |
| 67 | Receiver = Runtime.GetClass(Builder, OID); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 68 | } else if (const PredefinedExpr *PDE = |
| 69 | dyn_cast<PredefinedExpr>(E->getReceiver())) { |
| 70 | assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 71 | isSuperMessage = true; |
| 72 | Receiver = LoadObjCSelf(); |
| 73 | } else { |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 74 | Receiver = EmitScalarExpr(E->getReceiver()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 77 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 78 | // super is only valid in an Objective-C method |
| 79 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame^] | 80 | return Runtime.GenerateMessageSendSuper(*this, E, |
| 81 | OMD->getClassInterface()->getSuperClass(), |
| 82 | Receiver); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 83 | } |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame^] | 84 | return Runtime.GenerateMessageSend(*this, E, Receiver); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 87 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 88 | /// its pointer, name, and types registered in the class struture. |
| 89 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 90 | CurFn = CGM.getObjCRuntime().GenerateMethod(OMD); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 91 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 92 | |
| 93 | // Create a marker to make it easy to insert allocas into the entryblock |
| 94 | // later. Don't create this with the builder, because we don't want it |
| 95 | // folded. |
| 96 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 97 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 98 | EntryBB); |
| 99 | |
| 100 | FnRetTy = OMD->getResultType(); |
| 101 | CurFuncDecl = OMD; |
| 102 | |
| 103 | Builder.SetInsertPoint(EntryBB); |
| 104 | |
| 105 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 106 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 107 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 108 | // Name the struct return argument. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 109 | if (hasAggregateLLVMType(OMD->getResultType())) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 110 | AI->setName("agg.result"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 111 | ++AI; |
| 112 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 113 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 114 | // Add implicit parameters to the decl map. |
| 115 | EmitParmDecl(*OMD->getSelfDecl(), AI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 116 | ++AI; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 117 | |
| 118 | EmitParmDecl(*OMD->getCmdDecl(), AI); |
| 119 | ++AI; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 120 | |
| 121 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 122 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 123 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 124 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 125 | assert(AI == CurFn->arg_end() && "Argument mismatch"); |
| 126 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 127 | GenerateFunction(OMD->getBody()); |
| 128 | } |
| 129 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 130 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) { |
| 131 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 132 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 135 | CGObjCRuntime::~CGObjCRuntime() {} |