blob: de14842271f9b6fd7492460ec7506d83b83db521 [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenFunction.h - Per-Function state for LLVM CodeGen ----------===//
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 is the internal per-function state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_CODEGENFUNCTION_H
15#define CODEGEN_CODEGENFUNCTION_H
16
Chris Lattnerac248202007-05-30 00:13:02 +000017#include "llvm/ADT/DenseMap.h"
Chris Lattner308f4312007-05-29 23:50:05 +000018#include "llvm/Support/LLVMBuilder.h"
19
Chris Lattnerbed31442007-05-28 01:07:47 +000020namespace llvm {
21 class Module;
22namespace clang {
23 class ASTContext;
Chris Lattner84915fa2007-06-02 04:16:21 +000024 class Decl;
Chris Lattnerbed31442007-05-28 01:07:47 +000025 class FunctionDecl;
Chris Lattnerd1af2d22007-05-29 23:17:50 +000026 class QualType;
27 class SourceLocation;
28 class TargetInfo;
Chris Lattner84915fa2007-06-02 04:16:21 +000029
Chris Lattner308f4312007-05-29 23:50:05 +000030 class Stmt;
31 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000032 class LabelStmt;
33 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000034 class IfStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000035 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000036 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000037
Chris Lattner208ae962007-05-30 17:57:17 +000038 class Expr;
39 class IntegerLiteral;
Chris Lattnerdb91b162007-06-02 00:16:28 +000040 class BinaryOperator;
Chris Lattner208ae962007-05-30 17:57:17 +000041
Chris Lattner84915fa2007-06-02 04:16:21 +000042 class BlockVarDecl;
43 class EnumConstantDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000044namespace CodeGen {
45 class CodeGenModule;
46
Chris Lattner5269c032007-05-30 21:03:58 +000047class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000048 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000049 // TODO: Encode this into the low bit of pointer for more efficient
50 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000051 bool IsAggregate;
52public:
53
54 bool isAggregate() const { return IsAggregate; }
55 bool isScalar() const { return !IsAggregate; }
56
57 /// getVal() - Return the Value* of this scalar value.
58 Value *getVal() const {
59 assert(!isAggregate() && "Not a scalar!");
60 return V;
61 }
62
63 /// getAggregateVal() - Return the Value* of the address of the aggregate.
64 Value *getAggregateVal() const {
65 assert(isAggregate() && "Not an aggregate!");
66 return V;
67 }
Chris Lattner208ae962007-05-30 17:57:17 +000068
69 static ExprResult get(Value *V) {
70 ExprResult ER;
71 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000072 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000073 return ER;
74 }
75 static ExprResult getAggregate(Value *V) {
76 ExprResult ER;
77 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000078 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000079 return ER;
80 }
81};
82
Chris Lattnerbed31442007-05-28 01:07:47 +000083/// CodeGenFunction - This class organizes the per-function state that is used
84/// while generating LLVM code.
85class CodeGenFunction {
86 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +000087 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +000088 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000089
90 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +000091 llvm::Function *CurFn;
92
Chris Lattner03df1222007-06-02 04:53:11 +000093 /// AllocaInsertPoint - This is an instruction in the entry block before which
94 /// we prefer to insert allocas.
95 llvm::Instruction *AllocaInsertPt;
96
Chris Lattner84915fa2007-06-02 04:16:21 +000097 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
98 /// decls.
99 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
100
Chris Lattnerac248202007-05-30 00:13:02 +0000101 /// LabelMap - This keeps track of the LLVM basic block for each C label.
102 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000103public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000104 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000105
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000106 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
107
Chris Lattner308f4312007-05-29 23:50:05 +0000108 void GenerateCode(const FunctionDecl *FD);
109
Chris Lattnerac248202007-05-30 00:13:02 +0000110
111 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
112 /// label maps to.
113 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
114
115
116 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000117
Chris Lattnerac248202007-05-30 00:13:02 +0000118
Chris Lattner308f4312007-05-29 23:50:05 +0000119 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000120 // Local Declaration Emission
121 //===--------------------------------------------------------------------===//
122
123 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000124 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000125 void EmitBlockVarDecl(const BlockVarDecl &D);
126 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
127
Chris Lattner84915fa2007-06-02 04:16:21 +0000128 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000129 // Statement Emission
130 //===--------------------------------------------------------------------===//
131
132 void EmitStmt(const Stmt *S);
133 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000134 void EmitLabelStmt(const LabelStmt &S);
135 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000136 void EmitIfStmt(const IfStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000137 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000138
139 //===--------------------------------------------------------------------===//
140 // Expression Emission
141 //===--------------------------------------------------------------------===//
142
143 ExprResult EmitExpr(const Expr *E);
144 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000145 ExprResult EmitBinaryOperator(const BinaryOperator *E);
146
147
148 void EmitUsualArithmeticConversions(const BinaryOperator *E,
149 ExprResult &LHS, ExprResult &RHS);
150
151 // Binary Operators.
152 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000153};
154} // end namespace CodeGen
155} // end namespace clang
156} // end namespace llvm
157
158#endif