blob: 2d30ed574609b075ae8c464450af1b574513dbb0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "CodeGenFunction.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/AST.h"
Nate Begeman8bd4afe2008-04-19 04:17:09 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner2621fd12008-05-08 05:58:21 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson1a86b332007-10-17 00:52:43 +000020#include "llvm/GlobalVariable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/Type.h"
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000022#include "llvm/Support/Dwarf.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26
27void CodeGenFunction::EmitDecl(const Decl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000028 switch (D.getKind()) {
29 default: assert(0 && "Unknown decl kind!");
Chris Lattneraa9fc462007-10-08 21:37:32 +000030 case Decl::ParmVar:
Reid Spencer5f016e22007-07-11 17:01:13 +000031 assert(0 && "Parmdecls should not be in declstmts!");
32 case Decl::Typedef: // typedef int X;
33 case Decl::Function: // void X();
34 case Decl::Struct: // struct X;
35 case Decl::Union: // union X;
36 case Decl::Class: // class X;
37 case Decl::Enum: // enum X;
Argyrios Kyrtzidisa7a6dc02008-06-09 23:42:47 +000038 case Decl::CXXStruct: // struct X; [C++]
39 case Decl::CXXUnion: // union X; [C++]
40 case Decl::CXXClass: // class X; [C++]
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // None of these decls require codegen support.
42 return;
43
Steve Naroff248a7532008-04-15 22:42:06 +000044 case Decl::Var:
45 if (cast<VarDecl>(D).isBlockVarDecl())
46 return EmitBlockVarDecl(cast<VarDecl>(D));
47 assert(0 && "Should not see file-scope variables inside a function!");
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049 case Decl::EnumConstant:
50 return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
51 }
52}
53
54void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
55 assert(0 && "FIXME: Enum constant decls not implemented yet!");
56}
57
58/// EmitBlockVarDecl - This method handles emission of any variable declaration
59/// inside a function, including static vars etc.
Steve Naroff248a7532008-04-15 22:42:06 +000060void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 switch (D.getStorageClass()) {
62 case VarDecl::Static:
Anders Carlsson1a86b332007-10-17 00:52:43 +000063 return EmitStaticBlockVarDecl(D);
Reid Spencer5f016e22007-07-11 17:01:13 +000064 case VarDecl::Extern:
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +000065 // Don't emit it now, allow it to be emitted lazily on its first use.
66 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 default:
68 assert((D.getStorageClass() == VarDecl::None ||
69 D.getStorageClass() == VarDecl::Auto ||
70 D.getStorageClass() == VarDecl::Register) &&
71 "Unknown storage class");
72 return EmitLocalBlockVarDecl(D);
73 }
74}
75
Chris Lattner2621fd12008-05-08 05:58:21 +000076llvm::GlobalValue *
77CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
78 bool NoInit,
79 const char *Separator) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +000080 QualType Ty = D.getType();
Eli Friedman3c2b3172008-02-15 12:20:59 +000081 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Anders Carlsson1a86b332007-10-17 00:52:43 +000082
Chris Lattner19009e62008-01-09 18:47:25 +000083 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Anders Carlsson1a86b332007-10-17 00:52:43 +000084 llvm::Constant *Init = 0;
Chris Lattner2621fd12008-05-08 05:58:21 +000085 if ((D.getInit() == 0) || NoInit) {
Anders Carlsson1a86b332007-10-17 00:52:43 +000086 Init = llvm::Constant::getNullValue(LTy);
Oliver Hunt28247232007-12-02 00:11:25 +000087 } else {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000088 Init = CGM.EmitConstantExpr(D.getInit(), this);
Devang Patele40daa42007-10-26 17:50:58 +000089 }
90
Oliver Hunt28247232007-12-02 00:11:25 +000091 assert(Init && "Unable to create initialiser for static decl");
Nate Begeman8bd4afe2008-04-19 04:17:09 +000092
Chris Lattner352ffde22008-02-06 04:54:32 +000093 std::string ContextName;
Chris Lattnerc8aa5f12008-04-04 04:07:35 +000094 if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
95 ContextName = FD->getName();
Chris Lattner352ffde22008-02-06 04:54:32 +000096 else
97 assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
Nate Begeman8bd4afe2008-04-19 04:17:09 +000098
Eli Friedman26590522008-06-08 01:23:18 +000099 llvm::GlobalValue *GV =
100 new llvm::GlobalVariable(Init->getType(), false,
101 llvm::GlobalValue::InternalLinkage,
Chris Lattner2621fd12008-05-08 05:58:21 +0000102 Init, ContextName + Separator + D.getName(),
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000103 &CGM.getModule(), 0, Ty.getAddressSpace());
104
Chris Lattner2621fd12008-05-08 05:58:21 +0000105 return GV;
106}
107
108void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
109
110 llvm::Value *&DMEntry = LocalDeclMap[&D];
111 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
112
113 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
114
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000115 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
116 SourceManager &SM = CGM.getContext().getSourceManager();
117 llvm::Constant *Ann =
118 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
119 CGM.AddAnnotation(Ann);
120 }
121
Eli Friedman26590522008-06-08 01:23:18 +0000122 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
123 const llvm::Type *LPtrTy =
124 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
125 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000126
127 // Emit global variable debug descriptor for static vars.
128 CGDebugInfo *DI = CGM.getDebugInfo();
129 if(DI) {
130 if(D.getLocation().isValid())
131 DI->setLocation(D.getLocation());
132 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
133 }
134
Anders Carlsson1a86b332007-10-17 00:52:43 +0000135}
136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
138/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner2621fd12008-05-08 05:58:21 +0000139/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff248a7532008-04-15 22:42:06 +0000140void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000141 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000142
143 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000144 if (Ty->isConstantSizeType()) {
Chris Lattner2621fd12008-05-08 05:58:21 +0000145 if (!Target.useGlobalsForAutomaticVariables()) {
146 // A normal fixed sized variable becomes an alloca in the entry block.
147 const llvm::Type *LTy = ConvertType(Ty);
Eli Friedman77eedd62008-05-31 21:20:41 +0000148 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
149 unsigned align = getContext().getTypeAlign(Ty);
150 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
151 align = std::max(align, AA->getAlignment());
152 Alloc->setAlignment(align >> 3);
153 DeclPtr = Alloc;
Chris Lattner2621fd12008-05-08 05:58:21 +0000154 } else {
155 // Targets that don't support recursion emit locals as globals.
156 const char *Class =
157 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
158 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
159 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 } else {
161 // TODO: Create a dynamic alloca.
162 assert(0 && "FIXME: Local VLAs not implemented yet");
163 }
164
165 llvm::Value *&DMEntry = LocalDeclMap[&D];
166 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
167 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000168
169 // Emit debug info for local var declaration.
170 CGDebugInfo *DI = CGM.getDebugInfo();
171 if(DI) {
172 if(D.getLocation().isValid())
173 DI->setLocation(D.getLocation());
174 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
175 DeclPtr, Builder);
176 }
177
Chris Lattner19785962007-07-12 00:39:48 +0000178 // If this local has an initializer, emit it now.
Chris Lattner7f02f722007-08-24 05:35:26 +0000179 if (const Expr *Init = D.getInit()) {
Chris Lattnerd31beb12007-08-26 07:29:23 +0000180 if (!hasAggregateLLVMType(Init->getType())) {
181 llvm::Value *V = EmitScalarExpr(Init);
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000182 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9b2dc282008-04-04 16:54:41 +0000183 } else if (Init->getType()->isAnyComplexType()) {
Chris Lattner190dbe22007-08-26 16:22:13 +0000184 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000185 } else {
Chris Lattner8b2f3b72007-08-26 07:30:49 +0000186 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
Chris Lattner9a19edf2007-08-26 05:13:54 +0000187 }
Chris Lattner7f02f722007-08-24 05:35:26 +0000188 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000189}
190
Chris Lattner2621fd12008-05-08 05:58:21 +0000191/// Emit an alloca (or GlobalValue depending on target)
192/// for the specified parameter and set up LocalDeclMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000193void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000194 QualType Ty = D.getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000195
196 llvm::Value *DeclPtr;
Eli Friedman3c2b3172008-02-15 12:20:59 +0000197 if (!Ty->isConstantSizeType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // Variable sized values always are passed by-reference.
199 DeclPtr = Arg;
Chris Lattner2621fd12008-05-08 05:58:21 +0000200 } else if (Target.useGlobalsForAutomaticVariables()) {
201 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 } else {
Dan Gohmand79a7262008-05-22 22:12:56 +0000203 // A fixed sized single-value variable becomes an alloca in the entry block.
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohmand79a7262008-05-22 22:12:56 +0000205 if (LTy->isSingleValueType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 // TODO: Alignment
207 DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
208 AllocaInsertPt);
209
210 // Store the initial value into the alloca.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000211 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 } else {
213 // Otherwise, if this is an aggregate, just use the input pointer.
214 DeclPtr = Arg;
215 }
216 Arg->setName(D.getName());
217 }
218
219 llvm::Value *&DMEntry = LocalDeclMap[&D];
220 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
221 DMEntry = DeclPtr;
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000222
223 // Emit debug info for param declaration.
224 CGDebugInfo *DI = CGM.getDebugInfo();
225 if(DI) {
226 if(D.getLocation().isValid())
227 DI->setLocation(D.getLocation());
228 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
229 DeclPtr, Builder);
230 }
231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232}
233