blob: aa9d2c6054a9b47c60c0dd91e5fe4b3a1a5f3aa4 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the per-function state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000016#include "clang/Basic/TargetInfo.h"
Chris Lattnerfc944342007-12-02 01:43:38 +000017#include "clang/Basic/Diagnostic.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000018#include "clang/AST/AST.h"
Chris Lattner208ae962007-05-30 17:57:17 +000019#include "llvm/Constants.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000020#include "llvm/DerivedTypes.h"
Chris Lattner308f4312007-05-29 23:50:05 +000021#include "llvm/Function.h"
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000022#include "llvm/Analysis/Verifier.h"
Devang Patel8ec4f832007-09-28 21:49:18 +000023#include "llvm/Support/CFG.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattnerd1af2d22007-05-29 23:17:50 +000027CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patel11663122007-10-08 20:57:48 +000028 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
29 CaseRangeBlock(NULL) {}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000030
Chris Lattner6db1fb82007-06-02 22:49:07 +000031ASTContext &CodeGenFunction::getContext() const {
32 return CGM.getContext();
33}
34
Chris Lattnerd1af2d22007-05-29 23:17:50 +000035
Chris Lattnerac248202007-05-30 00:13:02 +000036llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000037 llvm::BasicBlock *&BB = LabelMap[S];
Chris Lattnerac248202007-05-30 00:13:02 +000038 if (BB) return BB;
39
40 // Create, but don't insert, the new block.
Chris Lattner23b7eb62007-06-15 23:05:46 +000041 return BB = new llvm::BasicBlock(S->getName());
Chris Lattnerac248202007-05-30 00:13:02 +000042}
43
44
Chris Lattnerf033c142007-06-22 19:05:19 +000045const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
46 return CGM.getTypes().ConvertType(T);
Chris Lattner45bb9142007-06-09 02:28:57 +000047}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000048
Chris Lattner54fb19e2007-06-22 22:02:34 +000049bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Anders Carlsson24ebce62007-10-12 23:56:29 +000050 return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
51 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Chris Lattner54fb19e2007-06-22 22:02:34 +000052}
53
54
Chris Lattner308f4312007-05-29 23:50:05 +000055void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
Chris Lattnerf033c142007-06-22 19:05:19 +000056 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattnerfb2eb692007-09-04 02:34:27 +000057 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattner4481b422007-07-14 01:29:45 +000058 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
Chris Lattnerfb2eb692007-09-04 02:34:27 +000059 SourceLocation()));
Chris Lattner6db1fb82007-06-02 22:49:07 +000060
Chris Lattner3f3dbee2007-06-02 03:19:07 +000061 CurFuncDecl = FD;
Chris Lattner41af8182007-12-02 06:27:33 +000062 llvm::Constant *CurFnC = CGM.GetAddrOfGlobalDecl(FD);
63 if (!(CurFn = dyn_cast<llvm::Function>(CurFnC))) {
64 // If CurFnC is not a constant, it must be a bitcast of another function.
65 llvm::ConstantExpr *CurFnCE = cast<llvm::ConstantExpr>(CurFnC);
66 assert(CurFnCE->getOpcode() == llvm::Instruction::BitCast &&
67 "Unexpected name collision");
68 llvm::Function *OtherFunc = cast<llvm::Function>(CurFnCE->getOperand(0));
69
70 // This happens if there is a prototype for a function (e.g. "int f()") and
71 // then a definition of a different type (e.g. "int f(int x)"). Start by
72 // making a new function of the correct type, RAUW, then steal the name.
73 const llvm::PointerType *PTy = cast<llvm::PointerType>(CurFnC->getType());
74 const llvm::FunctionType *FTy =
75 cast<llvm::FunctionType>(PTy->getElementType());
76 CurFn = new llvm::Function(FTy, llvm::Function::ExternalLinkage, "",
77 &CGM.getModule());
78 CurFn->takeName(OtherFunc);
79
80 // Replace uses of OtherFunc with the Function we will endow with a body.
81 llvm::Constant *NewPtrForOldDecl =
82 llvm::ConstantExpr::getBitCast(CurFn, OtherFunc->getType());
83 OtherFunc->replaceAllUsesWith(NewPtrForOldDecl);
84
85 // Make sure the GlobalDecl map for FD is up-to-date.
86 CGM.ChangeGlobalDeclMap(FD, CurFn);
87
88 // FIXME: Update the globaldeclmap for the previous decl of this name. We
89 // really want a way to walk all of these, but we don't have it yet. This
90 // is incredibly slow!
91 CGM.ReplaceMapValuesWith(OtherFunc, NewPtrForOldDecl);
92
93 // Ok, delete the old function now, which is dead.
94 assert(OtherFunc->isDeclaration() && "Shouldn't replace non-declaration");
95 OtherFunc->eraseFromParent();
96 }
Chris Lattnerb6984c42007-06-20 04:44:43 +000097
Chris Lattnerb6984c42007-06-20 04:44:43 +000098 assert(CurFn->isDeclaration() && "Function already has body?");
Chris Lattnerd1af2d22007-05-29 23:17:50 +000099
Chris Lattnerf04b69b2007-11-27 06:46:51 +0000100 // TODO: Set up linkage and many other things. Note, this is a simple
101 // approximation of what we really want.
102 if (FD->getStorageClass() == FunctionDecl::Static)
103 CurFn->setLinkage(llvm::Function::InternalLinkage);
104 else if (FD->isInline())
105 CurFn->setLinkage(llvm::Function::WeakLinkage);
106
Chris Lattner23b7eb62007-06-15 23:05:46 +0000107 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
Chris Lattner308f4312007-05-29 23:50:05 +0000108
Chris Lattner308f4312007-05-29 23:50:05 +0000109 Builder.SetInsertPoint(EntryBB);
Chris Lattner03df1222007-06-02 04:53:11 +0000110
111 // Create a marker to make it easy to insert allocas into the entryblock
112 // later.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000113 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
114 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
Chris Lattner308f4312007-05-29 23:50:05 +0000115
Chris Lattner54fb19e2007-06-22 22:02:34 +0000116 // Emit allocs for param decls. Give the LLVM Argument nodes names.
Chris Lattner53621a52007-06-13 20:44:40 +0000117 llvm::Function::arg_iterator AI = CurFn->arg_begin();
Chris Lattner54fb19e2007-06-22 22:02:34 +0000118
119 // Name the struct return argument.
120 if (hasAggregateLLVMType(FD->getResultType())) {
121 AI->setName("agg.result");
122 ++AI;
123 }
124
Chris Lattner53621a52007-06-13 20:44:40 +0000125 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
126 assert(AI != CurFn->arg_end() && "Argument mismatch!");
127 EmitParmDecl(*FD->getParamDecl(i), AI);
128 }
Chris Lattner84915fa2007-06-02 04:16:21 +0000129
130 // Emit the function body.
Chris Lattner308f4312007-05-29 23:50:05 +0000131 EmitStmt(FD->getBody());
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000132
Devang Patel8ec4f832007-09-28 21:49:18 +0000133 // Emit a return for code that falls off the end. If insert point
134 // is a dummy block with no predecessors then remove the block itself.
135 llvm::BasicBlock *BB = Builder.GetInsertBlock();
136 if (isDummyBlock(BB))
137 BB->eraseFromParent();
138 else {
139 // FIXME: if this is C++ main, this should return 0.
140 if (CurFn->getReturnType() == llvm::Type::VoidTy)
141 Builder.CreateRetVoid();
142 else
143 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
144 }
Chris Lattnere73e4322007-07-16 21:28:45 +0000145 assert(BreakContinueStack.empty() &&
146 "mismatched push/pop in break/continue stack!");
147
Chris Lattnerc48023b2007-12-02 06:32:24 +0000148 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
149 AllocaInsertPt->eraseFromParent();
150 AllocaInsertPt = 0;
151
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000152 // Verify that the function is well formed.
153 assert(!verifyFunction(*CurFn));
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000154}
Chris Lattner308f4312007-05-29 23:50:05 +0000155
Devang Patel8ec4f832007-09-28 21:49:18 +0000156/// isDummyBlock - Return true if BB is an empty basic block
157/// with no predecessors.
158bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
159 if (BB->empty() && pred_begin(BB) == pred_end(BB))
160 return true;
161 return false;
162}
163
Devang Patelda5d6bb2007-10-04 23:45:31 +0000164/// StartBlock - Start new block named N. If insert block is a dummy block
165/// then reuse it.
166void CodeGenFunction::StartBlock(const char *N) {
167 llvm::BasicBlock *BB = Builder.GetInsertBlock();
168 if (!isDummyBlock(BB))
169 EmitBlock(new llvm::BasicBlock(N));
170 else
171 BB->setName(N);
172}
173
Devang Patele11664a2007-11-01 19:11:01 +0000174/// getCGRecordLayout - Return record layout info.
175const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
176 QualType RTy) {
Devang Patel3e11cce2007-10-23 02:10:49 +0000177 assert (isa<RecordType>(RTy)
178 && "Unexpected type. RecordType expected here.");
179
180 const llvm::Type *Ty = ConvertType(RTy);
181 assert (Ty && "Unable to find llvm::Type");
182
Devang Patele11664a2007-11-01 19:11:01 +0000183 return CGT.getCGRecordLayout(Ty);
Devang Patel3e11cce2007-10-23 02:10:49 +0000184}
Chris Lattnerfc944342007-12-02 01:43:38 +0000185
186/// WarnUnsupported - Print out a warning that codegen doesn't support the
187/// specified stmt yet.
Chris Lattnerf0780fa2007-12-02 01:49:16 +0000188void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattnerfc944342007-12-02 01:43:38 +0000189 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Warning,
Chris Lattnerf0780fa2007-12-02 01:49:16 +0000190 "cannot codegen this %0 yet");
Chris Lattnerfc944342007-12-02 01:43:38 +0000191 SourceRange Range = S->getSourceRange();
Chris Lattnerf0780fa2007-12-02 01:49:16 +0000192 std::string Msg = Type;
193 CGM.getDiags().Report(S->getLocStart(), DiagID, &Msg, 1, &Range, 1);
Chris Lattnerfc944342007-12-02 01:43:38 +0000194}
195