blob: 679a0f7404a0fa03d3f1aaac699afb60a84c759d [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerbed31442007-05-28 01:07:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-function state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Eli Friedman17630752008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/AST/AST.h"
Nate Begeman0a6192c2008-03-07 20:04:22 +000019#include "llvm/CallingConv.h"
Chris Lattner208ae962007-05-30 17:57:17 +000020#include "llvm/Constants.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000021#include "llvm/DerivedTypes.h"
Chris Lattner308f4312007-05-29 23:50:05 +000022#include "llvm/Function.h"
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000023#include "llvm/Analysis/Verifier.h"
Devang Patel8ec4f832007-09-28 21:49:18 +000024#include "llvm/Support/CFG.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000025using namespace clang;
26using namespace CodeGen;
27
Chris Lattnerd1af2d22007-05-29 23:17:50 +000028CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patel11663122007-10-08 20:57:48 +000029 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner5696e7b2008-06-17 18:05:57 +000030 CaseRangeBlock(NULL) {
31 LLVMIntTy = ConvertType(getContext().IntTy);
32 LLVMPointerWidth = Target.getPointerWidth(0);
33}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000034
Chris Lattner6db1fb82007-06-02 22:49:07 +000035ASTContext &CodeGenFunction::getContext() const {
36 return CGM.getContext();
37}
38
Chris Lattnerd1af2d22007-05-29 23:17:50 +000039
Chris Lattnerac248202007-05-30 00:13:02 +000040llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000041 llvm::BasicBlock *&BB = LabelMap[S];
Chris Lattnerac248202007-05-30 00:13:02 +000042 if (BB) return BB;
43
44 // Create, but don't insert, the new block.
Gabor Greifd36afd72008-04-06 20:42:52 +000045 return BB = llvm::BasicBlock::Create(S->getName());
Chris Lattnerac248202007-05-30 00:13:02 +000046}
47
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000048llvm::Constant *
Steve Naroff08899ff2008-04-15 22:42:06 +000049CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000050 return cast<llvm::Constant>(LocalDeclMap[BVD]);
51}
Chris Lattnerac248202007-05-30 00:13:02 +000052
Chris Lattnerf033c142007-06-22 19:05:19 +000053const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
54 return CGM.getTypes().ConvertType(T);
Chris Lattner45bb9142007-06-09 02:28:57 +000055}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000056
Chris Lattner5696e7b2008-06-17 18:05:57 +000057bool CodeGenFunction::isObjCPointerType(QualType T) {
58 // All Objective-C types are pointers.
59 return T->isObjCInterfaceType() ||
60 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
61}
62
Chris Lattner54fb19e2007-06-22 22:02:34 +000063bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Chris Lattner5696e7b2008-06-17 18:05:57 +000064 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
65 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Chris Lattner54fb19e2007-06-22 22:02:34 +000066}
67
Chris Lattner5696e7b2008-06-17 18:05:57 +000068void CodeGenFunction::GenerateFunction(const Stmt *Body) {
Chris Lattner4bd55962008-03-30 23:03:07 +000069 // Emit the function body.
Chris Lattner5696e7b2008-06-17 18:05:57 +000070 EmitStmt(Body);
Chris Lattner4bd55962008-03-30 23:03:07 +000071
Sanjiv Gupta1e8b6082008-07-04 11:04:26 +000072 // Emit debug descriptor for function end.
73 CGDebugInfo *DI = CGM.getDebugInfo();
74 if (DI) {
75 const CompoundStmt* s = dyn_cast<CompoundStmt>(Body);
76 if (s && s->getRBracLoc().isValid()) {
77 DI->setLocation(s->getRBracLoc());
78 }
79 DI->EmitRegionEnd(CurFn, Builder);
80 }
81
Chris Lattner4bd55962008-03-30 23:03:07 +000082 // Emit a return for code that falls off the end. If insert point
83 // is a dummy block with no predecessors then remove the block itself.
84 llvm::BasicBlock *BB = Builder.GetInsertBlock();
85 if (isDummyBlock(BB))
86 BB->eraseFromParent();
87 else {
Devang Patel8ec4f832007-09-28 21:49:18 +000088 // FIXME: if this is C++ main, this should return 0.
89 if (CurFn->getReturnType() == llvm::Type::VoidTy)
90 Builder.CreateRetVoid();
91 else
92 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
93 }
Chris Lattnere73e4322007-07-16 21:28:45 +000094 assert(BreakContinueStack.empty() &&
95 "mismatched push/pop in break/continue stack!");
96
Chris Lattnerc48023b2007-12-02 06:32:24 +000097 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
98 AllocaInsertPt->eraseFromParent();
99 AllocaInsertPt = 0;
100
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000101 // Verify that the function is well formed.
Chris Lattner4bd55962008-03-30 23:03:07 +0000102 assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000103}
Chris Lattner308f4312007-05-29 23:50:05 +0000104
Chris Lattner5696e7b2008-06-17 18:05:57 +0000105void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
106 CurFuncDecl = FD;
107 FnRetTy = FD->getResultType();
108 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
109 assert(CurFn->isDeclaration() && "Function already has body?");
110
111 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
112
113 // Create a marker to make it easy to insert allocas into the entryblock
114 // later. Don't create this with the builder, because we don't want it
115 // folded.
116 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
117 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
118 EntryBB);
119
120 Builder.SetInsertPoint(EntryBB);
121
Sanjiv Gupta1e8b6082008-07-04 11:04:26 +0000122 // Emit subprogram debug descriptor.
123 CGDebugInfo *DI = CGM.getDebugInfo();
124 if (DI) {
125 CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody());
126 if (body && body->getLBracLoc().isValid()) {
127 DI->setLocation(body->getLBracLoc());
128 }
129 DI->EmitFunctionStart(FD, CurFn, Builder);
130 }
131
Chris Lattner5696e7b2008-06-17 18:05:57 +0000132 // Emit allocs for param decls. Give the LLVM Argument nodes names.
133 llvm::Function::arg_iterator AI = CurFn->arg_begin();
134
135 // Name the struct return argument.
136 if (hasAggregateLLVMType(FD->getResultType())) {
137 AI->setName("agg.result");
138 ++AI;
139 }
140
141 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
142 assert(AI != CurFn->arg_end() && "Argument mismatch!");
143 EmitParmDecl(*FD->getParamDecl(i), AI);
144 }
145 GenerateFunction(FD->getBody());
146}
147
Devang Patel8ec4f832007-09-28 21:49:18 +0000148/// isDummyBlock - Return true if BB is an empty basic block
149/// with no predecessors.
150bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
Chris Lattner4591fd02008-07-25 23:40:10 +0000151 if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName())
Devang Patel8ec4f832007-09-28 21:49:18 +0000152 return true;
153 return false;
154}
155
Devang Patelda5d6bb2007-10-04 23:45:31 +0000156/// StartBlock - Start new block named N. If insert block is a dummy block
157/// then reuse it.
158void CodeGenFunction::StartBlock(const char *N) {
159 llvm::BasicBlock *BB = Builder.GetInsertBlock();
160 if (!isDummyBlock(BB))
Gabor Greifd36afd72008-04-06 20:42:52 +0000161 EmitBlock(llvm::BasicBlock::Create(N));
Devang Patelda5d6bb2007-10-04 23:45:31 +0000162 else
163 BB->setName(N);
164}
165
Devang Patele11664a2007-11-01 19:11:01 +0000166/// getCGRecordLayout - Return record layout info.
167const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattner86964a92008-02-05 06:55:31 +0000168 QualType Ty) {
169 const RecordType *RTy = Ty->getAsRecordType();
170 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patel3e11cce2007-10-23 02:10:49 +0000171
Chris Lattner86964a92008-02-05 06:55:31 +0000172 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patel3e11cce2007-10-23 02:10:49 +0000173}
Chris Lattnerfc944342007-12-02 01:43:38 +0000174
175/// WarnUnsupported - Print out a warning that codegen doesn't support the
176/// specified stmt yet.
Chris Lattnerf0780fa2007-12-02 01:49:16 +0000177void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000178 CGM.WarnUnsupported(S, Type);
Chris Lattnerfc944342007-12-02 01:43:38 +0000179}
180