blob: db5072be0b9e0fc3d4b880d09cc1eea472f6b25e [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 Lattnerb6984c42007-06-20 04:44:43 +000061 CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
Chris Lattner3f3dbee2007-06-02 03:19:07 +000062 CurFuncDecl = FD;
Chris Lattnerb6984c42007-06-20 04:44:43 +000063
Chris Lattnerb6984c42007-06-20 04:44:43 +000064 assert(CurFn->isDeclaration() && "Function already has body?");
Chris Lattnerd1af2d22007-05-29 23:17:50 +000065
Chris Lattnerf04b69b2007-11-27 06:46:51 +000066 // TODO: Set up linkage and many other things. Note, this is a simple
67 // approximation of what we really want.
68 if (FD->getStorageClass() == FunctionDecl::Static)
69 CurFn->setLinkage(llvm::Function::InternalLinkage);
70 else if (FD->isInline())
71 CurFn->setLinkage(llvm::Function::WeakLinkage);
72
Chris Lattner23b7eb62007-06-15 23:05:46 +000073 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
Chris Lattner308f4312007-05-29 23:50:05 +000074
Chris Lattner308f4312007-05-29 23:50:05 +000075 Builder.SetInsertPoint(EntryBB);
Chris Lattner03df1222007-06-02 04:53:11 +000076
77 // Create a marker to make it easy to insert allocas into the entryblock
78 // later.
Chris Lattner23b7eb62007-06-15 23:05:46 +000079 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
80 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
Chris Lattner308f4312007-05-29 23:50:05 +000081
Chris Lattner54fb19e2007-06-22 22:02:34 +000082 // Emit allocs for param decls. Give the LLVM Argument nodes names.
Chris Lattner53621a52007-06-13 20:44:40 +000083 llvm::Function::arg_iterator AI = CurFn->arg_begin();
Chris Lattner54fb19e2007-06-22 22:02:34 +000084
85 // Name the struct return argument.
86 if (hasAggregateLLVMType(FD->getResultType())) {
87 AI->setName("agg.result");
88 ++AI;
89 }
90
Chris Lattner53621a52007-06-13 20:44:40 +000091 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
92 assert(AI != CurFn->arg_end() && "Argument mismatch!");
93 EmitParmDecl(*FD->getParamDecl(i), AI);
94 }
Chris Lattner84915fa2007-06-02 04:16:21 +000095
96 // Emit the function body.
Chris Lattner308f4312007-05-29 23:50:05 +000097 EmitStmt(FD->getBody());
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000098
Devang Patel8ec4f832007-09-28 21:49:18 +000099 // Emit a return for code that falls off the end. If insert point
100 // is a dummy block with no predecessors then remove the block itself.
101 llvm::BasicBlock *BB = Builder.GetInsertBlock();
102 if (isDummyBlock(BB))
103 BB->eraseFromParent();
104 else {
105 // FIXME: if this is C++ main, this should return 0.
106 if (CurFn->getReturnType() == llvm::Type::VoidTy)
107 Builder.CreateRetVoid();
108 else
109 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
110 }
Chris Lattnere73e4322007-07-16 21:28:45 +0000111 assert(BreakContinueStack.empty() &&
112 "mismatched push/pop in break/continue stack!");
113
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000114 // Verify that the function is well formed.
115 assert(!verifyFunction(*CurFn));
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000116}
Chris Lattner308f4312007-05-29 23:50:05 +0000117
Devang Patel8ec4f832007-09-28 21:49:18 +0000118/// isDummyBlock - Return true if BB is an empty basic block
119/// with no predecessors.
120bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
121 if (BB->empty() && pred_begin(BB) == pred_end(BB))
122 return true;
123 return false;
124}
125
Devang Patelda5d6bb2007-10-04 23:45:31 +0000126/// StartBlock - Start new block named N. If insert block is a dummy block
127/// then reuse it.
128void CodeGenFunction::StartBlock(const char *N) {
129 llvm::BasicBlock *BB = Builder.GetInsertBlock();
130 if (!isDummyBlock(BB))
131 EmitBlock(new llvm::BasicBlock(N));
132 else
133 BB->setName(N);
134}
135
Devang Patele11664a2007-11-01 19:11:01 +0000136/// getCGRecordLayout - Return record layout info.
137const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
138 QualType RTy) {
Devang Patel3e11cce2007-10-23 02:10:49 +0000139 assert (isa<RecordType>(RTy)
140 && "Unexpected type. RecordType expected here.");
141
142 const llvm::Type *Ty = ConvertType(RTy);
143 assert (Ty && "Unable to find llvm::Type");
144
Devang Patele11664a2007-11-01 19:11:01 +0000145 return CGT.getCGRecordLayout(Ty);
Devang Patel3e11cce2007-10-23 02:10:49 +0000146}
Chris Lattnerfc944342007-12-02 01:43:38 +0000147
148/// WarnUnsupported - Print out a warning that codegen doesn't support the
149/// specified stmt yet.
150void CodeGenFunction::WarnUnsupported(const Stmt *S) {
151 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Warning,
152 "cannot codegen this yet");
153 SourceRange Range = S->getSourceRange();
154 CGM.getDiags().Report(S->getLocStart(), DiagID, 0, 0, &Range, 1);
155}
156