Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Decl nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
| 22 | |
| 23 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | switch (D.getKind()) { |
| 25 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 26 | case Decl::FileVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | assert(0 && "Should not see file-scope variables inside a function!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 28 | case Decl::ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | assert(0 && "Parmdecls should not be in declstmts!"); |
| 30 | case Decl::Typedef: // typedef int X; |
| 31 | case Decl::Function: // void X(); |
| 32 | case Decl::Struct: // struct X; |
| 33 | case Decl::Union: // union X; |
| 34 | case Decl::Class: // class X; |
| 35 | case Decl::Enum: // enum X; |
| 36 | // None of these decls require codegen support. |
| 37 | return; |
| 38 | |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 39 | case Decl::BlockVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | return EmitBlockVarDecl(cast<BlockVarDecl>(D)); |
| 41 | case Decl::EnumConstant: |
| 42 | return EmitEnumConstantDecl(cast<EnumConstantDecl>(D)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) { |
| 47 | assert(0 && "FIXME: Enum constant decls not implemented yet!"); |
| 48 | } |
| 49 | |
| 50 | /// EmitBlockVarDecl - This method handles emission of any variable declaration |
| 51 | /// inside a function, including static vars etc. |
| 52 | void CodeGenFunction::EmitBlockVarDecl(const BlockVarDecl &D) { |
| 53 | switch (D.getStorageClass()) { |
| 54 | case VarDecl::Static: |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 55 | return EmitStaticBlockVarDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | case VarDecl::Extern: |
| 57 | assert(0 && "FIXME: should call up to codegenmodule"); |
| 58 | default: |
| 59 | assert((D.getStorageClass() == VarDecl::None || |
| 60 | D.getStorageClass() == VarDecl::Auto || |
| 61 | D.getStorageClass() == VarDecl::Register) && |
| 62 | "Unknown storage class"); |
| 63 | return EmitLocalBlockVarDecl(D); |
| 64 | } |
| 65 | } |
| 66 | |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 67 | void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) { |
| 68 | QualType Ty = D.getCanonicalType(); |
| 69 | assert(Ty->isConstantSizeType(getContext()) && "VLAs can't be static"); |
| 70 | |
| 71 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 72 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 73 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 74 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 75 | llvm::Constant *Init = 0; |
| 76 | if (D.getInit() == 0) { |
| 77 | Init = llvm::Constant::getNullValue(LTy); |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 78 | } else { |
| 79 | Init = CGM.EmitGlobalInit(D.getInit()); |
Devang Patel | e40daa4 | 2007-10-26 17:50:58 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 82 | assert(Init && "Unable to create initialiser for static decl"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 83 | |
| 84 | DMEntry = |
| 85 | new llvm::GlobalVariable(LTy, false, |
| 86 | llvm::GlobalValue::InternalLinkage, |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame^] | 87 | Init, D.getName(), &CGM.getModule(), 0, |
| 88 | Ty.getAddressSpace()); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 89 | |
| 90 | } |
| 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a |
| 93 | /// variable declaration with auto, register, or no storage class specifier. |
| 94 | /// These turn into simple stack objects. |
| 95 | void CodeGenFunction::EmitLocalBlockVarDecl(const BlockVarDecl &D) { |
| 96 | QualType Ty = D.getCanonicalType(); |
| 97 | |
| 98 | llvm::Value *DeclPtr; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 99 | if (Ty->isConstantSizeType(getContext())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | // A normal fixed sized variable becomes an alloca in the entry block. |
| 101 | const llvm::Type *LTy = ConvertType(Ty); |
| 102 | // TODO: Alignment |
| 103 | DeclPtr = CreateTempAlloca(LTy, D.getName()); |
| 104 | } else { |
| 105 | // TODO: Create a dynamic alloca. |
| 106 | assert(0 && "FIXME: Local VLAs not implemented yet"); |
| 107 | } |
| 108 | |
| 109 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 110 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 111 | DMEntry = DeclPtr; |
| 112 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 113 | // If this local has an initializer, emit it now. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 114 | if (const Expr *Init = D.getInit()) { |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 115 | if (!hasAggregateLLVMType(Init->getType())) { |
| 116 | llvm::Value *V = EmitScalarExpr(Init); |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 117 | Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 118 | } else if (Init->getType()->isComplexType()) { |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 119 | EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 120 | } else { |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 121 | EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 123 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /// Emit an alloca for the specified parameter and set up LocalDeclMap. |
| 127 | void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) { |
| 128 | QualType Ty = D.getCanonicalType(); |
| 129 | |
| 130 | llvm::Value *DeclPtr; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 131 | if (!Ty->isConstantSizeType(getContext())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | // Variable sized values always are passed by-reference. |
| 133 | DeclPtr = Arg; |
| 134 | } else { |
| 135 | // A fixed sized first class variable becomes an alloca in the entry block. |
| 136 | const llvm::Type *LTy = ConvertType(Ty); |
| 137 | if (LTy->isFirstClassType()) { |
| 138 | // TODO: Alignment |
| 139 | DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr", |
| 140 | AllocaInsertPt); |
| 141 | |
| 142 | // Store the initial value into the alloca. |
| 143 | Builder.CreateStore(Arg, DeclPtr); |
| 144 | } else { |
| 145 | // Otherwise, if this is an aggregate, just use the input pointer. |
| 146 | DeclPtr = Arg; |
| 147 | } |
| 148 | Arg->setName(D.getName()); |
| 149 | } |
| 150 | |
| 151 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 152 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 153 | DMEntry = DeclPtr; |
| 154 | } |
| 155 | |