blob: 6a21b413ca52f115945f460aa02ddbcbf1a69522 [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 Lattner8394d792007-06-05 20:53:16 +000036 class DoStmt;
37 class ForStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000038 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000039 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000040
Chris Lattner208ae962007-05-30 17:57:17 +000041 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000042 class DeclRefExpr;
Chris Lattner4347e3692007-06-06 04:54:52 +000043 class StringLiteral;
Chris Lattner208ae962007-05-30 17:57:17 +000044 class IntegerLiteral;
Chris Lattner8394d792007-06-05 20:53:16 +000045 class CastExpr;
Chris Lattnerf0106d22007-06-02 19:33:17 +000046 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000047 class BinaryOperator;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000048 class ArraySubscriptExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000049
Chris Lattner84915fa2007-06-02 04:16:21 +000050 class BlockVarDecl;
51 class EnumConstantDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000052namespace CodeGen {
53 class CodeGenModule;
54
Chris Lattnerd7f58862007-06-02 05:24:33 +000055
Chris Lattner8394d792007-06-05 20:53:16 +000056/// RValue - This trivial value class is used to represent the result of an
Chris Lattnerd7f58862007-06-02 05:24:33 +000057/// expression that is evaluated. It can be one of two things: either a simple
58/// LLVM SSA value, or the address of an aggregate value in memory. These two
59/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner8394d792007-06-05 20:53:16 +000060class RValue {
Chris Lattner208ae962007-05-30 17:57:17 +000061 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000062 // TODO: Encode this into the low bit of pointer for more efficient
63 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000064 bool IsAggregate;
65public:
66
67 bool isAggregate() const { return IsAggregate; }
68 bool isScalar() const { return !IsAggregate; }
69
70 /// getVal() - Return the Value* of this scalar value.
71 Value *getVal() const {
72 assert(!isAggregate() && "Not a scalar!");
73 return V;
74 }
75
76 /// getAggregateVal() - Return the Value* of the address of the aggregate.
77 Value *getAggregateVal() const {
78 assert(isAggregate() && "Not an aggregate!");
79 return V;
80 }
Chris Lattner208ae962007-05-30 17:57:17 +000081
Chris Lattner8394d792007-06-05 20:53:16 +000082 static RValue get(Value *V) {
83 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000084 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000085 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000086 return ER;
87 }
Chris Lattner8394d792007-06-05 20:53:16 +000088 static RValue getAggregate(Value *V) {
89 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000090 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000091 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000092 return ER;
93 }
94};
Chris Lattnerd7f58862007-06-02 05:24:33 +000095
96
97/// LValue - This represents an lvalue references. Because C/C++ allow
98/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
99/// bitrange.
100class LValue {
101 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000102 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000103 llvm::Value *V;
104public:
105 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000106
Chris Lattnerd7f58862007-06-02 05:24:33 +0000107 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
108
109 static LValue getAddr(Value *V) {
110 LValue R;
111 R.V = V;
112 return R;
113 }
114};
115
Chris Lattnerbed31442007-05-28 01:07:47 +0000116/// CodeGenFunction - This class organizes the per-function state that is used
117/// while generating LLVM code.
118class CodeGenFunction {
119 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000120 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000121 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000122
123 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000124 llvm::Function *CurFn;
125
Chris Lattner03df1222007-06-02 04:53:11 +0000126 /// AllocaInsertPoint - This is an instruction in the entry block before which
127 /// we prefer to insert allocas.
128 llvm::Instruction *AllocaInsertPt;
129
Chris Lattner6db1fb82007-06-02 22:49:07 +0000130 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000131 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000132
Chris Lattner84915fa2007-06-02 04:16:21 +0000133 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
134 /// decls.
135 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
136
Chris Lattnerac248202007-05-30 00:13:02 +0000137 /// LabelMap - This keeps track of the LLVM basic block for each C label.
138 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000139public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000140 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000141
Chris Lattner6db1fb82007-06-02 22:49:07 +0000142 ASTContext &getContext() const;
143
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000144 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
145
Chris Lattner308f4312007-05-29 23:50:05 +0000146 void GenerateCode(const FunctionDecl *FD);
147
Chris Lattnerac248202007-05-30 00:13:02 +0000148
149 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
150 /// label maps to.
151 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
152
153
154 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000155
Chris Lattner8394d792007-06-05 20:53:16 +0000156
157 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
158 /// expression and compare the result against zero, returning an Int1Ty value.
159 Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnerac248202007-05-30 00:13:02 +0000160
Chris Lattner8394d792007-06-05 20:53:16 +0000161 //===--------------------------------------------------------------------===//
162 // Conversions
163 //===--------------------------------------------------------------------===//
164
165 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
166 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000167 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
168 SourceLocation Loc);
Chris Lattner8394d792007-06-05 20:53:16 +0000169
170 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000171 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +0000172 Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000173
Chris Lattner308f4312007-05-29 23:50:05 +0000174 //===--------------------------------------------------------------------===//
Chris Lattner84915fa2007-06-02 04:16:21 +0000175 // Local Declaration Emission
176 //===--------------------------------------------------------------------===//
177
178 void EmitDeclStmt(const DeclStmt &S);
Chris Lattner84915fa2007-06-02 04:16:21 +0000179 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000180 void EmitBlockVarDecl(const BlockVarDecl &D);
181 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
182
Chris Lattner84915fa2007-06-02 04:16:21 +0000183 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000184 // Statement Emission
185 //===--------------------------------------------------------------------===//
186
187 void EmitStmt(const Stmt *S);
188 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000189 void EmitLabelStmt(const LabelStmt &S);
190 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000191 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000192 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000193 void EmitDoStmt(const DoStmt &S);
194 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000195 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner208ae962007-05-30 17:57:17 +0000196
197 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000198 // LValue Expression Emission
199 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000200
201 /// EmitLValue - Emit code to compute a designator that specifies the location
202 /// of the expression.
203 ///
204 /// This can return one of two things: a simple address or a bitfield
205 /// reference. In either case, the LLVM Value* in the LValue structure is
206 /// guaranteed to be an LLVM pointer type.
207 ///
208 /// If this returns a bitfield reference, nothing about the pointee type of
209 /// the LLVM value is known: For example, it may not be a pointer to an
210 /// integer.
211 ///
212 /// If this returns a normal address, and if the lvalue's C type is fixed
213 /// size, this method guarantees that the returned pointer type will point to
214 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
215 /// variable length type, this is not possible.
216 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000217 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000218
219 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
220 /// this method emits the address of the lvalue, then loads the result as an
221 /// rvalue, returning the rvalue.
222 RValue EmitLoadOfLValue(const Expr *E);
223
224 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
225 /// lvalue, where both are guaranteed to the have the same type, and that type
226 /// is 'Ty'.
227 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
228
Chris Lattnerd7f58862007-06-02 05:24:33 +0000229 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000230 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000231 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000232 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000233
234 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000235 // Expression Emission
236 //===--------------------------------------------------------------------===//
237
Chris Lattner8394d792007-06-05 20:53:16 +0000238 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000239 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000240 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000241
Chris Lattner8394d792007-06-05 20:53:16 +0000242 RValue EmitExpr(const Expr *E);
243 RValue EmitIntegerLiteral(const IntegerLiteral *E);
244
245 RValue EmitCastExpr(const CastExpr *E);
246
Chris Lattnerf0106d22007-06-02 19:33:17 +0000247 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000248 RValue EmitUnaryOperator(const UnaryOperator *E);
249 // FIXME: pre/post inc/dec
250 RValue EmitUnaryAddrOf (const UnaryOperator *E);
251 RValue EmitUnaryPlus (const UnaryOperator *E);
252 RValue EmitUnaryMinus (const UnaryOperator *E);
253 RValue EmitUnaryNot (const UnaryOperator *E);
254 RValue EmitUnaryLNot (const UnaryOperator *E);
255 // FIXME: SIZEOF/ALIGNOF(expr).
256 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000257
Chris Lattnerdb91b162007-06-02 00:16:28 +0000258 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000259 RValue EmitBinaryOperator(const BinaryOperator *E);
260 RValue EmitBinaryMul(const BinaryOperator *E);
261 RValue EmitBinaryDiv(const BinaryOperator *E);
262 RValue EmitBinaryRem(const BinaryOperator *E);
263 RValue EmitBinaryAdd(const BinaryOperator *E);
264 RValue EmitBinarySub(const BinaryOperator *E);
265 RValue EmitBinaryShl(const BinaryOperator *E);
266 RValue EmitBinaryShr(const BinaryOperator *E);
267
268 // FIXME: relational
269
270 RValue EmitBinaryAnd(const BinaryOperator *E);
271 RValue EmitBinaryXor(const BinaryOperator *E);
272 RValue EmitBinaryOr (const BinaryOperator *E);
273 RValue EmitBinaryLAnd(const BinaryOperator *E);
274 RValue EmitBinaryLOr(const BinaryOperator *E);
275
276 RValue EmitBinaryAssign(const BinaryOperator *E);
277 // FIXME: Assignment.
278
279 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000280};
281} // end namespace CodeGen
282} // end namespace clang
283} // end namespace llvm
284
285#endif