blob: 4df50af90ac1936236ba3658f49aa9c5b904b212 [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 Lattner946aa312007-06-05 03:59:43 +000035 class WhileStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000036 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000037 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000038
Chris Lattner208ae962007-05-30 17:57:17 +000039 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000040 class DeclRefExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000041 class IntegerLiteral;
Chris Lattnerf0106d22007-06-02 19:33:17 +000042 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000043 class BinaryOperator;
Chris Lattner208ae962007-05-30 17:57:17 +000044
Chris Lattner84915fa2007-06-02 04:16:21 +000045 class BlockVarDecl;
46 class EnumConstantDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000047namespace CodeGen {
48 class CodeGenModule;
49
Chris Lattnerd7f58862007-06-02 05:24:33 +000050
51/// ExprResult - This trivial value class is used to represent the result of an
52/// expression that is evaluated. It can be one of two things: either a simple
53/// LLVM SSA value, or the address of an aggregate value in memory. These two
54/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner5269c032007-05-30 21:03:58 +000055class ExprResult {
Chris Lattner208ae962007-05-30 17:57:17 +000056 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000057 // TODO: Encode this into the low bit of pointer for more efficient
58 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000059 bool IsAggregate;
60public:
61
62 bool isAggregate() const { return IsAggregate; }
63 bool isScalar() const { return !IsAggregate; }
64
65 /// getVal() - Return the Value* of this scalar value.
66 Value *getVal() const {
67 assert(!isAggregate() && "Not a scalar!");
68 return V;
69 }
70
71 /// getAggregateVal() - Return the Value* of the address of the aggregate.
72 Value *getAggregateVal() const {
73 assert(isAggregate() && "Not an aggregate!");
74 return V;
75 }
Chris Lattner208ae962007-05-30 17:57:17 +000076
77 static ExprResult get(Value *V) {
78 ExprResult ER;
79 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000080 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000081 return ER;
82 }
83 static ExprResult getAggregate(Value *V) {
84 ExprResult ER;
85 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000086 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000087 return ER;
88 }
89};
Chris Lattnerd7f58862007-06-02 05:24:33 +000090
91
92/// LValue - This represents an lvalue references. Because C/C++ allow
93/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
94/// bitrange.
95class LValue {
96 // FIXME: Volatility. Restrict?
97 llvm::Value *V;
98public:
99 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000100
Chris Lattnerd7f58862007-06-02 05:24:33 +0000101 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
102
103 static LValue getAddr(Value *V) {
104 LValue R;
105 R.V = V;
106 return R;
107 }
108};
109
Chris Lattnerbed31442007-05-28 01:07:47 +0000110/// CodeGenFunction - This class organizes the per-function state that is used
111/// while generating LLVM code.
112class CodeGenFunction {
113 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000114 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000115 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000116
117 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000118 llvm::Function *CurFn;
119
Chris Lattner03df1222007-06-02 04:53:11 +0000120 /// AllocaInsertPoint - This is an instruction in the entry block before which
121 /// we prefer to insert allocas.
122 llvm::Instruction *AllocaInsertPt;
123
Chris Lattner6db1fb82007-06-02 22:49:07 +0000124 const llvm::Type *LLVMIntTy;
125
Chris Lattner84915fa2007-06-02 04:16:21 +0000126 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
127 /// decls.
128 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
129
Chris Lattnerac248202007-05-30 00:13:02 +0000130 /// LabelMap - This keeps track of the LLVM basic block for each C label.
131 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000132public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000133 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000134
Chris Lattner6db1fb82007-06-02 22:49:07 +0000135 ASTContext &getContext() const;
136
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000137 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
138
Chris Lattner308f4312007-05-29 23:50:05 +0000139 void GenerateCode(const FunctionDecl *FD);
140
Chris Lattnerac248202007-05-30 00:13:02 +0000141
142 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
143 /// label maps to.
144 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
145
146
147 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000148
Chris Lattnerac248202007-05-30 00:13:02 +0000149
Chris Lattnerf0106d22007-06-02 19:33:17 +0000150 /// EvaluateScalarValueToBool - Evaluate the specified expression value to a
151 /// boolean (i1) truth value. This is equivalent to "Val == 0".
152 Value *EvaluateScalarValueToBool(ExprResult Val, QualType Ty);
153
Chris Lattner308f4312007-05-29 23:50:05 +0000154 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000155 // Local Declaration Emission
156 //===--------------------------------------------------------------------===//
157
158 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000159 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000160 void EmitBlockVarDecl(const BlockVarDecl &D);
161 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
162
Chris Lattner84915fa2007-06-02 04:16:21 +0000163 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000164 // Statement Emission
165 //===--------------------------------------------------------------------===//
166
167 void EmitStmt(const Stmt *S);
168 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000169 void EmitLabelStmt(const LabelStmt &S);
170 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000171 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000172 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000173 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000174
175 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000176 // LValue Expression Emission
177 //===--------------------------------------------------------------------===//
178
179 LValue EmitLValue(const Expr *E);
180 LValue EmitDeclRefLValue(const DeclRefExpr *E);
181
182 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000183 // Expression Emission
184 //===--------------------------------------------------------------------===//
185
186 ExprResult EmitExpr(const Expr *E);
187 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000188
Chris Lattner6db1fb82007-06-02 22:49:07 +0000189 ExprResult EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000190 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
191 ExprResult &LHS, ExprResult &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000192
Chris Lattnerf0106d22007-06-02 19:33:17 +0000193 // Unary Operators.
194 ExprResult EmitUnaryOperator(const UnaryOperator *E);
195 ExprResult EmitUnaryLNot(const UnaryOperator *E);
196
Chris Lattnerdb91b162007-06-02 00:16:28 +0000197 // Binary Operators.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000198 ExprResult EmitBinaryOperator(const BinaryOperator *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000199 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000200};
201} // end namespace CodeGen
202} // end namespace clang
203} // end namespace llvm
204
205#endif