blob: 087dfc6e86ef026c1cc5d222c9778cd09c85d6b1 [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;
Chris Lattnerd7f58862007-06-02 05:24:33 +000039 class DeclRefExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000040 class IntegerLiteral;
Chris Lattnerf0106d22007-06-02 19:33:17 +000041 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000042 class BinaryOperator;
Chris Lattner208ae962007-05-30 17:57:17 +000043
Chris Lattner84915fa2007-06-02 04:16:21 +000044 class BlockVarDecl;
45 class EnumConstantDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000046namespace CodeGen {
47 class CodeGenModule;
48
Chris Lattnerd7f58862007-06-02 05:24:33 +000049
50/// ExprResult - This trivial value class is used to represent the result of an
51/// expression that is evaluated. It can be one of two things: either a simple
52/// LLVM SSA value, or the address of an aggregate value in memory. These two
53/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner5269c032007-05-30 21:03:58 +000054class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000055 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000056 // TODO: Encode this into the low bit of pointer for more efficient
57 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000058 bool IsAggregate;
59public:
60
61 bool isAggregate() const { return IsAggregate; }
62 bool isScalar() const { return !IsAggregate; }
63
64 /// getVal() - Return the Value* of this scalar value.
65 Value *getVal() const {
66 assert(!isAggregate() && "Not a scalar!");
67 return V;
68 }
69
70 /// getAggregateVal() - Return the Value* of the address of the aggregate.
71 Value *getAggregateVal() const {
72 assert(isAggregate() && "Not an aggregate!");
73 return V;
74 }
Chris Lattner208ae962007-05-30 17:57:17 +000075
76 static ExprResult get(Value *V) {
77 ExprResult ER;
78 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000079 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000080 return ER;
81 }
82 static ExprResult getAggregate(Value *V) {
83 ExprResult ER;
84 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000085 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000086 return ER;
87 }
88};
Chris Lattnerd7f58862007-06-02 05:24:33 +000089
90
91/// LValue - This represents an lvalue references. Because C/C++ allow
92/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
93/// bitrange.
94class LValue {
95 // FIXME: Volatility. Restrict?
96 llvm::Value *V;
97public:
98 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +000099
Chris Lattnerd7f58862007-06-02 05:24:33 +0000100 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
101
102 static LValue getAddr(Value *V) {
103 LValue R;
104 R.V = V;
105 return R;
106 }
107};
108
Chris Lattnerbed31442007-05-28 01:07:47 +0000109/// CodeGenFunction - This class organizes the per-function state that is used
110/// while generating LLVM code.
111class CodeGenFunction {
112 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000113 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000114 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000115
116 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000117 llvm::Function *CurFn;
118
Chris Lattner03df1222007-06-02 04:53:11 +0000119 /// AllocaInsertPoint - This is an instruction in the entry block before which
120 /// we prefer to insert allocas.
121 llvm::Instruction *AllocaInsertPt;
122
Chris Lattner84915fa2007-06-02 04:16:21 +0000123 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
124 /// decls.
125 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
126
Chris Lattnerac248202007-05-30 00:13:02 +0000127 /// LabelMap - This keeps track of the LLVM basic block for each C label.
128 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerf0106d22007-06-02 19:33:17 +0000129
130 const llvm::Type *LLVMIntTy;
Chris Lattnerbed31442007-05-28 01:07:47 +0000131public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000132 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000133
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000134 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
135
Chris Lattner308f4312007-05-29 23:50:05 +0000136 void GenerateCode(const FunctionDecl *FD);
137
Chris Lattnerac248202007-05-30 00:13:02 +0000138
139 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
140 /// label maps to.
141 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
142
143
144 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000145
Chris Lattnerac248202007-05-30 00:13:02 +0000146
Chris Lattnerf0106d22007-06-02 19:33:17 +0000147 /// EvaluateScalarValueToBool - Evaluate the specified expression value to a
148 /// boolean (i1) truth value. This is equivalent to "Val == 0".
149 Value *EvaluateScalarValueToBool(ExprResult Val, QualType Ty);
150
Chris Lattner308f4312007-05-29 23:50:05 +0000151 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000152 // Local Declaration Emission
153 //===--------------------------------------------------------------------===//
154
155 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000156 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000157 void EmitBlockVarDecl(const BlockVarDecl &D);
158 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
159
Chris Lattner84915fa2007-06-02 04:16:21 +0000160 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000161 // Statement Emission
162 //===--------------------------------------------------------------------===//
163
164 void EmitStmt(const Stmt *S);
165 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000166 void EmitLabelStmt(const LabelStmt &S);
167 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000168 void EmitIfStmt(const IfStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000169 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000170
171 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000172 // LValue Expression Emission
173 //===--------------------------------------------------------------------===//
174
175 LValue EmitLValue(const Expr *E);
176 LValue EmitDeclRefLValue(const DeclRefExpr *E);
177
178 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000179 // Expression Emission
180 //===--------------------------------------------------------------------===//
181
182 ExprResult EmitExpr(const Expr *E);
183 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000184
185
186 void EmitUsualArithmeticConversions(const BinaryOperator *E,
187 ExprResult &LHS, ExprResult &RHS);
188
Chris Lattnerf0106d22007-06-02 19:33:17 +0000189 // Unary Operators.
190 ExprResult EmitUnaryOperator(const UnaryOperator *E);
191 ExprResult EmitUnaryLNot(const UnaryOperator *E);
192
Chris Lattnerdb91b162007-06-02 00:16:28 +0000193 // Binary Operators.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000194 ExprResult EmitBinaryOperator(const BinaryOperator *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000195 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000196};
197} // end namespace CodeGen
198} // end namespace clang
199} // end namespace llvm
200
201#endif