blob: ebc1ff500d09ae8417c3c5a7cdac8aa146e8f888 [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"
Eli Friedman3f2af102008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/AST/AST.h"
Nate Begeman440b4562008-03-07 20:04:22 +000019#include "llvm/CallingConv.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000024#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26using namespace CodeGen;
27
28CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000029 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner41110242008-06-17 18:05:57 +000030 CaseRangeBlock(NULL) {
31 LLVMIntTy = ConvertType(getContext().IntTy);
32 LLVMPointerWidth = Target.getPointerWidth(0);
33}
Reid Spencer5f016e22007-07-11 17:01:13 +000034
35ASTContext &CodeGenFunction::getContext() const {
36 return CGM.getContext();
37}
38
39
40llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
41 llvm::BasicBlock *&BB = LabelMap[S];
42 if (BB) return BB;
43
44 // Create, but don't insert, the new block.
Gabor Greif984d0b42008-04-06 20:42:52 +000045 return BB = llvm::BasicBlock::Create(S->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000048llvm::Constant *
Steve Naroff248a7532008-04-15 22:42:06 +000049CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000050 return cast<llvm::Constant>(LocalDeclMap[BVD]);
51}
Reid Spencer5f016e22007-07-11 17:01:13 +000052
53const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
54 return CGM.getTypes().ConvertType(T);
55}
56
Chris Lattner41110242008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000063bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Chris Lattner41110242008-06-17 18:05:57 +000064 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
65 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
Chris Lattner41110242008-06-17 18:05:57 +000068void CodeGenFunction::GenerateFunction(const Stmt *Body) {
Chris Lattner391d77a2008-03-30 23:03:07 +000069 // Emit the function body.
Chris Lattner41110242008-06-17 18:05:57 +000070 EmitStmt(Body);
Chris Lattner391d77a2008-03-30 23:03:07 +000071
72 // Emit a return for code that falls off the end. If insert point
73 // is a dummy block with no predecessors then remove the block itself.
74 llvm::BasicBlock *BB = Builder.GetInsertBlock();
75 if (isDummyBlock(BB))
76 BB->eraseFromParent();
77 else {
Devang Pateld9363c32007-09-28 21:49:18 +000078 // FIXME: if this is C++ main, this should return 0.
79 if (CurFn->getReturnType() == llvm::Type::VoidTy)
80 Builder.CreateRetVoid();
81 else
82 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
83 }
Chris Lattnerda138702007-07-16 21:28:45 +000084 assert(BreakContinueStack.empty() &&
85 "mismatched push/pop in break/continue stack!");
86
Chris Lattner5a2fa142007-12-02 06:32:24 +000087 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
88 AllocaInsertPt->eraseFromParent();
89 AllocaInsertPt = 0;
90
Reid Spencer5f016e22007-07-11 17:01:13 +000091 // Verify that the function is well formed.
Chris Lattner391d77a2008-03-30 23:03:07 +000092 assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
Chris Lattner41110242008-06-17 18:05:57 +000095void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
96 CurFuncDecl = FD;
97 FnRetTy = FD->getResultType();
98 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
99 assert(CurFn->isDeclaration() && "Function already has body?");
100
101 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
102
103 // Create a marker to make it easy to insert allocas into the entryblock
104 // later. Don't create this with the builder, because we don't want it
105 // folded.
106 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
107 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
108 EntryBB);
109
110 Builder.SetInsertPoint(EntryBB);
111
112 // Emit allocs for param decls. Give the LLVM Argument nodes names.
113 llvm::Function::arg_iterator AI = CurFn->arg_begin();
114
115 // Name the struct return argument.
116 if (hasAggregateLLVMType(FD->getResultType())) {
117 AI->setName("agg.result");
118 ++AI;
119 }
120
121 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
122 assert(AI != CurFn->arg_end() && "Argument mismatch!");
123 EmitParmDecl(*FD->getParamDecl(i), AI);
124 }
125 GenerateFunction(FD->getBody());
126}
127
Devang Pateld9363c32007-09-28 21:49:18 +0000128/// isDummyBlock - Return true if BB is an empty basic block
129/// with no predecessors.
130bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
131 if (BB->empty() && pred_begin(BB) == pred_end(BB))
132 return true;
133 return false;
134}
135
Devang Patel51b09f22007-10-04 23:45:31 +0000136/// StartBlock - Start new block named N. If insert block is a dummy block
137/// then reuse it.
138void CodeGenFunction::StartBlock(const char *N) {
139 llvm::BasicBlock *BB = Builder.GetInsertBlock();
140 if (!isDummyBlock(BB))
Gabor Greif984d0b42008-04-06 20:42:52 +0000141 EmitBlock(llvm::BasicBlock::Create(N));
Devang Patel51b09f22007-10-04 23:45:31 +0000142 else
143 BB->setName(N);
144}
145
Devang Patel88a981b2007-11-01 19:11:01 +0000146/// getCGRecordLayout - Return record layout info.
147const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000148 QualType Ty) {
149 const RecordType *RTy = Ty->getAsRecordType();
150 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000151
Chris Lattneraf319132008-02-05 06:55:31 +0000152 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000153}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000154
155/// WarnUnsupported - Print out a warning that codegen doesn't support the
156/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000157void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000158 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000159}
160