CodeGenFunction.cpp revision dc5e8268292046114ffe02e48773572a91a310f1
1//===--- 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"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/AST/AST.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/Verifier.h"
23#include "llvm/Support/CFG.h"
24using namespace clang;
25using namespace CodeGen;
26
27CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
28  : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
29    CaseRangeBlock(NULL) {}
30
31ASTContext &CodeGenFunction::getContext() const {
32  return CGM.getContext();
33}
34
35
36llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
37  llvm::BasicBlock *&BB = LabelMap[S];
38  if (BB) return BB;
39
40  // Create, but don't insert, the new block.
41  return BB = new llvm::BasicBlock(S->getName());
42}
43
44
45const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
46  return CGM.getTypes().ConvertType(T);
47}
48
49bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
50  return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
51         !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
52}
53
54
55void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
56  LLVMIntTy = ConvertType(getContext().IntTy);
57  LLVMPointerWidth = static_cast<unsigned>(
58    getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
59                             SourceLocation()));
60
61  CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
62  CurFuncDecl = FD;
63
64  assert(CurFn->isDeclaration() && "Function already has body?");
65
66  // 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
73  llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
74
75  Builder.SetInsertPoint(EntryBB);
76
77  // Create a marker to make it easy to insert allocas into the entryblock
78  // later.
79  llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
80  AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
81
82  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
83  llvm::Function::arg_iterator AI = CurFn->arg_begin();
84
85  // Name the struct return argument.
86  if (hasAggregateLLVMType(FD->getResultType())) {
87    AI->setName("agg.result");
88    ++AI;
89  }
90
91  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  }
95
96  // Emit the function body.
97  EmitStmt(FD->getBody());
98
99  // 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  }
111  assert(BreakContinueStack.empty() &&
112         "mismatched push/pop in break/continue stack!");
113
114  // Verify that the function is well formed.
115  assert(!verifyFunction(*CurFn));
116}
117
118/// 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
126/// 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
136/// getCGRecordLayout - Return record layout info.
137const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
138                                                         QualType RTy) {
139  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
145  return CGT.getCGRecordLayout(Ty);
146}
147
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
157