blob: 0a2685c853acf66fd6b7f40b59c08145fa3b8b50 [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 Lattner6db1fb82007-06-02 22:49:07 +0000123 const llvm::Type *LLVMIntTy;
124
Chris Lattner84915fa2007-06-02 04:16:21 +0000125 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
126 /// decls.
127 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
128
Chris Lattnerac248202007-05-30 00:13:02 +0000129 /// LabelMap - This keeps track of the LLVM basic block for each C label.
130 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
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 Lattner6db1fb82007-06-02 22:49:07 +0000134 ASTContext &getContext() const;
135
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000136 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
137
Chris Lattner308f4312007-05-29 23:50:05 +0000138 void GenerateCode(const FunctionDecl *FD);
139
Chris Lattnerac248202007-05-30 00:13:02 +0000140
141 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
142 /// label maps to.
143 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
144
145
146 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000147
Chris Lattnerac248202007-05-30 00:13:02 +0000148
Chris Lattnerf0106d22007-06-02 19:33:17 +0000149 /// EvaluateScalarValueToBool - Evaluate the specified expression value to a
150 /// boolean (i1) truth value. This is equivalent to "Val == 0".
151 Value *EvaluateScalarValueToBool(ExprResult Val, QualType Ty);
152
Chris Lattner308f4312007-05-29 23:50:05 +0000153 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000154 // Local Declaration Emission
155 //===--------------------------------------------------------------------===//
156
157 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000158 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000159 void EmitBlockVarDecl(const BlockVarDecl &D);
160 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
161
Chris Lattner84915fa2007-06-02 04:16:21 +0000162 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000163 // Statement Emission
164 //===--------------------------------------------------------------------===//
165
166 void EmitStmt(const Stmt *S);
167 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000168 void EmitLabelStmt(const LabelStmt &S);
169 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000170 void EmitIfStmt(const IfStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000171 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000172
173 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000174 // LValue Expression Emission
175 //===--------------------------------------------------------------------===//
176
177 LValue EmitLValue(const Expr *E);
178 LValue EmitDeclRefLValue(const DeclRefExpr *E);
179
180 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000181 // Expression Emission
182 //===--------------------------------------------------------------------===//
183
184 ExprResult EmitExpr(const Expr *E);
185 ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000186
Chris Lattner6db1fb82007-06-02 22:49:07 +0000187 ExprResult EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000188 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
189 ExprResult &LHS, ExprResult &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000190
Chris Lattnerf0106d22007-06-02 19:33:17 +0000191 // Unary Operators.
192 ExprResult EmitUnaryOperator(const UnaryOperator *E);
193 ExprResult EmitUnaryLNot(const UnaryOperator *E);
194
Chris Lattnerdb91b162007-06-02 00:16:28 +0000195 // Binary Operators.
Chris Lattnerf0106d22007-06-02 19:33:17 +0000196 ExprResult EmitBinaryOperator(const BinaryOperator *E);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000197 ExprResult EmitBinaryAdd(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000198};
199} // end namespace CodeGen
200} // end namespace clang
201} // end namespace llvm
202
203#endif