blob: c2a303c7557c850bef7b0db4035282cf54c39356 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
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 coordinates the per-function state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/AST/AST.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000022#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000027 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
28 CaseRangeBlock(NULL) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000029
30ASTContext &CodeGenFunction::getContext() const {
31 return CGM.getContext();
32}
33
34
35llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
36 llvm::BasicBlock *&BB = LabelMap[S];
37 if (BB) return BB;
38
39 // Create, but don't insert, the new block.
40 return BB = new llvm::BasicBlock(S->getName());
41}
42
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000043llvm::Constant *
44CodeGenFunction::GetAddrOfStaticLocalVar(const BlockVarDecl *BVD) {
45 return cast<llvm::Constant>(LocalDeclMap[BVD]);
46}
Reid Spencer5f016e22007-07-11 17:01:13 +000047
48const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
49 return CGM.getTypes().ConvertType(T);
50}
51
52bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Anders Carlsson793680e2007-10-12 23:56:29 +000053 return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
54 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000055}
56
57
58void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
59 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattner47f7dbf2007-09-04 02:34:27 +000060 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattnerd2d2a112007-07-14 01:29:45 +000061 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
Chris Lattner47f7dbf2007-09-04 02:34:27 +000062 SourceLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +000063
Reid Spencer5f016e22007-07-11 17:01:13 +000064 CurFuncDecl = FD;
Chris Lattner9cd4fe42007-12-02 07:09:19 +000065 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
Reid Spencer5f016e22007-07-11 17:01:13 +000066 assert(CurFn->isDeclaration() && "Function already has body?");
67
Chris Lattner5c6a42a2007-11-27 06:46:51 +000068 // TODO: Set up linkage and many other things. Note, this is a simple
69 // approximation of what we really want.
70 if (FD->getStorageClass() == FunctionDecl::Static)
71 CurFn->setLinkage(llvm::Function::InternalLinkage);
72 else if (FD->isInline())
73 CurFn->setLinkage(llvm::Function::WeakLinkage);
74
Reid Spencer5f016e22007-07-11 17:01:13 +000075 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077 // Create a marker to make it easy to insert allocas into the entryblock
Chris Lattner55352a22007-12-17 20:50:59 +000078 // later. Don't create this with the builder, because we don't want it
79 // folded.
Reid Spencer5f016e22007-07-11 17:01:13 +000080 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
Chris Lattner55352a22007-12-17 20:50:59 +000081 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
82 EntryBB);
83
84 Builder.SetInsertPoint(EntryBB);
Reid Spencer5f016e22007-07-11 17:01:13 +000085
86 // Emit allocs for param decls. Give the LLVM Argument nodes names.
87 llvm::Function::arg_iterator AI = CurFn->arg_begin();
88
89 // Name the struct return argument.
90 if (hasAggregateLLVMType(FD->getResultType())) {
91 AI->setName("agg.result");
92 ++AI;
93 }
94
95 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
96 assert(AI != CurFn->arg_end() && "Argument mismatch!");
97 EmitParmDecl(*FD->getParamDecl(i), AI);
98 }
99
100 // Emit the function body.
101 EmitStmt(FD->getBody());
102
Devang Pateld9363c32007-09-28 21:49:18 +0000103 // Emit a return for code that falls off the end. If insert point
104 // is a dummy block with no predecessors then remove the block itself.
105 llvm::BasicBlock *BB = Builder.GetInsertBlock();
106 if (isDummyBlock(BB))
107 BB->eraseFromParent();
108 else {
109 // FIXME: if this is C++ main, this should return 0.
110 if (CurFn->getReturnType() == llvm::Type::VoidTy)
111 Builder.CreateRetVoid();
112 else
113 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
114 }
Chris Lattnerda138702007-07-16 21:28:45 +0000115 assert(BreakContinueStack.empty() &&
116 "mismatched push/pop in break/continue stack!");
117
Chris Lattner5a2fa142007-12-02 06:32:24 +0000118 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
119 AllocaInsertPt->eraseFromParent();
120 AllocaInsertPt = 0;
121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // Verify that the function is well formed.
123 assert(!verifyFunction(*CurFn));
124}
125
Devang Pateld9363c32007-09-28 21:49:18 +0000126/// isDummyBlock - Return true if BB is an empty basic block
127/// with no predecessors.
128bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
129 if (BB->empty() && pred_begin(BB) == pred_end(BB))
130 return true;
131 return false;
132}
133
Devang Patel51b09f22007-10-04 23:45:31 +0000134/// StartBlock - Start new block named N. If insert block is a dummy block
135/// then reuse it.
136void CodeGenFunction::StartBlock(const char *N) {
137 llvm::BasicBlock *BB = Builder.GetInsertBlock();
138 if (!isDummyBlock(BB))
139 EmitBlock(new llvm::BasicBlock(N));
140 else
141 BB->setName(N);
142}
143
Devang Patel88a981b2007-11-01 19:11:01 +0000144/// getCGRecordLayout - Return record layout info.
145const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000146 QualType Ty) {
147 const RecordType *RTy = Ty->getAsRecordType();
148 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000149
Chris Lattneraf319132008-02-05 06:55:31 +0000150 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000151}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000152
153/// WarnUnsupported - Print out a warning that codegen doesn't support the
154/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000155void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000156 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000157}
158