blob: f9df00ba8044803e7abac7ff08a90a573ec8e926 [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"
Chris Lattner2ccb73b2007-06-16 00:16:26 +000019#include <vector>
Chris Lattner308f4312007-05-29 23:50:05 +000020
Chris Lattnerbed31442007-05-28 01:07:47 +000021namespace llvm {
22 class Module;
Chris Lattner23b7eb62007-06-15 23:05:46 +000023}
24
Chris Lattnerbed31442007-05-28 01:07:47 +000025namespace clang {
26 class ASTContext;
Chris Lattner84915fa2007-06-02 04:16:21 +000027 class Decl;
Chris Lattnerbed31442007-05-28 01:07:47 +000028 class FunctionDecl;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000029 class TargetInfo;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000030 class QualType;
31 class FunctionTypeProto;
Chris Lattner84915fa2007-06-02 04:16:21 +000032
Chris Lattner308f4312007-05-29 23:50:05 +000033 class Stmt;
34 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000035 class LabelStmt;
36 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000037 class IfStmt;
Chris Lattner946aa312007-06-05 03:59:43 +000038 class WhileStmt;
Chris Lattner8394d792007-06-05 20:53:16 +000039 class DoStmt;
40 class ForStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000041 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000042 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000043
Chris Lattner208ae962007-05-30 17:57:17 +000044 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000045 class DeclRefExpr;
Chris Lattner4347e3692007-06-06 04:54:52 +000046 class StringLiteral;
Chris Lattner208ae962007-05-30 17:57:17 +000047 class IntegerLiteral;
Chris Lattner8394d792007-06-05 20:53:16 +000048 class CastExpr;
Chris Lattner2b228c92007-06-15 21:34:29 +000049 class CallExpr;
Chris Lattnerf0106d22007-06-02 19:33:17 +000050 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000051 class BinaryOperator;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000052 class ArraySubscriptExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000053
Chris Lattner84915fa2007-06-02 04:16:21 +000054 class BlockVarDecl;
55 class EnumConstantDecl;
Chris Lattner53621a52007-06-13 20:44:40 +000056 class ParmVarDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000057namespace CodeGen {
58 class CodeGenModule;
59
Chris Lattnerd7f58862007-06-02 05:24:33 +000060
Chris Lattner8394d792007-06-05 20:53:16 +000061/// RValue - This trivial value class is used to represent the result of an
Chris Lattnerd7f58862007-06-02 05:24:33 +000062/// expression that is evaluated. It can be one of two things: either a simple
63/// LLVM SSA value, or the address of an aggregate value in memory. These two
64/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner8394d792007-06-05 20:53:16 +000065class RValue {
Chris Lattner23b7eb62007-06-15 23:05:46 +000066 llvm::Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000067 // TODO: Encode this into the low bit of pointer for more efficient
68 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000069 bool IsAggregate;
Chris Lattner09153c02007-06-22 18:48:09 +000070
71 // FIXME: Aggregate rvalues need to retain information about whether they are
72 // volatile or not.
Chris Lattner5269c032007-05-30 21:03:58 +000073public:
74
75 bool isAggregate() const { return IsAggregate; }
76 bool isScalar() const { return !IsAggregate; }
77
78 /// getVal() - Return the Value* of this scalar value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000079 llvm::Value *getVal() const {
Chris Lattner5269c032007-05-30 21:03:58 +000080 assert(!isAggregate() && "Not a scalar!");
81 return V;
82 }
83
Chris Lattner09153c02007-06-22 18:48:09 +000084 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
85 llvm::Value *getAggregateAddr() const {
Chris Lattner5269c032007-05-30 21:03:58 +000086 assert(isAggregate() && "Not an aggregate!");
87 return V;
88 }
Chris Lattner208ae962007-05-30 17:57:17 +000089
Chris Lattner23b7eb62007-06-15 23:05:46 +000090 static RValue get(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000091 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000092 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000093 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000094 return ER;
95 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000096 static RValue getAggregate(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000097 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000098 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000099 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +0000100 return ER;
101 }
102};
Chris Lattnerd7f58862007-06-02 05:24:33 +0000103
104
105/// LValue - This represents an lvalue references. Because C/C++ allow
106/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
107/// bitrange.
108class LValue {
109 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000110 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000111 llvm::Value *V;
112public:
113 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000114
Chris Lattnerd7f58862007-06-02 05:24:33 +0000115 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
116
Chris Lattner23b7eb62007-06-15 23:05:46 +0000117 static LValue getAddr(llvm::Value *V) {
Chris Lattnerd7f58862007-06-02 05:24:33 +0000118 LValue R;
119 R.V = V;
120 return R;
121 }
122};
123
Chris Lattnerbed31442007-05-28 01:07:47 +0000124/// CodeGenFunction - This class organizes the per-function state that is used
125/// while generating LLVM code.
126class CodeGenFunction {
127 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000128 TargetInfo &Target;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000129 llvm::LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000130
131 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000132 llvm::Function *CurFn;
133
Chris Lattner03df1222007-06-02 04:53:11 +0000134 /// AllocaInsertPoint - This is an instruction in the entry block before which
135 /// we prefer to insert allocas.
136 llvm::Instruction *AllocaInsertPt;
137
Chris Lattner6db1fb82007-06-02 22:49:07 +0000138 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000139 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000140
Chris Lattner84915fa2007-06-02 04:16:21 +0000141 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
142 /// decls.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000143 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Chris Lattner84915fa2007-06-02 04:16:21 +0000144
Chris Lattnerac248202007-05-30 00:13:02 +0000145 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000146 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000147public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000148 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000149
Chris Lattner6db1fb82007-06-02 22:49:07 +0000150 ASTContext &getContext() const;
151
Chris Lattner308f4312007-05-29 23:50:05 +0000152 void GenerateCode(const FunctionDecl *FD);
153
Chris Lattnerf033c142007-06-22 19:05:19 +0000154 const llvm::Type *ConvertType(QualType T);
Chris Lattnerac248202007-05-30 00:13:02 +0000155
Chris Lattner54fb19e2007-06-22 22:02:34 +0000156 /// hasAggregateLLVMType - Return true if the specified AST type will map into
157 /// an aggregate LLVM type or is void.
158 static bool hasAggregateLLVMType(QualType T);
159
Chris Lattnerac248202007-05-30 00:13:02 +0000160 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
161 /// label maps to.
162 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
163
164
Chris Lattner23b7eb62007-06-15 23:05:46 +0000165 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000166
Chris Lattnere9a64532007-06-22 21:44:33 +0000167 //===--------------------------------------------------------------------===//
168 // Helpers
169 //===--------------------------------------------------------------------===//
170
171 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
172 /// block.
173 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
174 const char *Name = "tmp");
175
Chris Lattner8394d792007-06-05 20:53:16 +0000176 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
177 /// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000178 llvm::Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnere9a64532007-06-22 21:44:33 +0000179
Chris Lattnerac248202007-05-30 00:13:02 +0000180
Chris Lattnere9a64532007-06-22 21:44:33 +0000181 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
182 /// load the real and imaginary pieces, returning them as Real/Imag.
183 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
184
185 /// EmitStoreOfComplex - Store the specified real/imag parts into the
186 /// specified value pointer.
187 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
188 llvm::Value *ResPtr);
189
Chris Lattner8394d792007-06-05 20:53:16 +0000190 //===--------------------------------------------------------------------===//
191 // Conversions
192 //===--------------------------------------------------------------------===//
193
194 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
195 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnerf033c142007-06-22 19:05:19 +0000196 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000197
198 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000199 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000200 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000201
Chris Lattner308f4312007-05-29 23:50:05 +0000202 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000203 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000204 //===--------------------------------------------------------------------===//
205
Chris Lattner1ad38f82007-06-09 01:20:56 +0000206 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000207 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000208 void EmitBlockVarDecl(const BlockVarDecl &D);
209 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000210 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000211
Chris Lattner84915fa2007-06-02 04:16:21 +0000212 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000213 // Statement Emission
214 //===--------------------------------------------------------------------===//
215
216 void EmitStmt(const Stmt *S);
217 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000218 void EmitLabelStmt(const LabelStmt &S);
219 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000220 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000221 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000222 void EmitDoStmt(const DoStmt &S);
223 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000224 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000225 void EmitDeclStmt(const DeclStmt &S);
226
Chris Lattner208ae962007-05-30 17:57:17 +0000227 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000228 // LValue Expression Emission
229 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000230
231 /// EmitLValue - Emit code to compute a designator that specifies the location
232 /// of the expression.
233 ///
234 /// This can return one of two things: a simple address or a bitfield
235 /// reference. In either case, the LLVM Value* in the LValue structure is
236 /// guaranteed to be an LLVM pointer type.
237 ///
238 /// If this returns a bitfield reference, nothing about the pointee type of
239 /// the LLVM value is known: For example, it may not be a pointer to an
240 /// integer.
241 ///
242 /// If this returns a normal address, and if the lvalue's C type is fixed
243 /// size, this method guarantees that the returned pointer type will point to
244 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
245 /// variable length type, this is not possible.
246 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000247 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000248
249 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
250 /// this method emits the address of the lvalue, then loads the result as an
251 /// rvalue, returning the rvalue.
252 RValue EmitLoadOfLValue(const Expr *E);
253
254 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
255 /// lvalue, where both are guaranteed to the have the same type, and that type
256 /// is 'Ty'.
257 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
258
Chris Lattnerd7f58862007-06-02 05:24:33 +0000259 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000260 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000261 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000262 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000263
264 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000265 // Expression Emission
266 //===--------------------------------------------------------------------===//
267
Chris Lattner8394d792007-06-05 20:53:16 +0000268 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000269 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000270 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000271
Chris Lattner8394d792007-06-05 20:53:16 +0000272 RValue EmitExpr(const Expr *E);
273 RValue EmitIntegerLiteral(const IntegerLiteral *E);
274
275 RValue EmitCastExpr(const CastExpr *E);
Chris Lattner2b228c92007-06-15 21:34:29 +0000276 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000277
Chris Lattnerf0106d22007-06-02 19:33:17 +0000278 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000279 RValue EmitUnaryOperator(const UnaryOperator *E);
280 // FIXME: pre/post inc/dec
281 RValue EmitUnaryAddrOf (const UnaryOperator *E);
282 RValue EmitUnaryPlus (const UnaryOperator *E);
283 RValue EmitUnaryMinus (const UnaryOperator *E);
284 RValue EmitUnaryNot (const UnaryOperator *E);
285 RValue EmitUnaryLNot (const UnaryOperator *E);
286 // FIXME: SIZEOF/ALIGNOF(expr).
287 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000288
Chris Lattnerdb91b162007-06-02 00:16:28 +0000289 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000290 RValue EmitBinaryOperator(const BinaryOperator *E);
291 RValue EmitBinaryMul(const BinaryOperator *E);
292 RValue EmitBinaryDiv(const BinaryOperator *E);
293 RValue EmitBinaryRem(const BinaryOperator *E);
294 RValue EmitBinaryAdd(const BinaryOperator *E);
295 RValue EmitBinarySub(const BinaryOperator *E);
296 RValue EmitBinaryShl(const BinaryOperator *E);
297 RValue EmitBinaryShr(const BinaryOperator *E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000298 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
299 unsigned SICmpOpc, unsigned FCmpOpc);
Chris Lattner8394d792007-06-05 20:53:16 +0000300 RValue EmitBinaryAnd(const BinaryOperator *E);
301 RValue EmitBinaryXor(const BinaryOperator *E);
302 RValue EmitBinaryOr (const BinaryOperator *E);
303 RValue EmitBinaryLAnd(const BinaryOperator *E);
304 RValue EmitBinaryLOr(const BinaryOperator *E);
305
306 RValue EmitBinaryAssign(const BinaryOperator *E);
307 // FIXME: Assignment.
308
309 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000310};
311} // end namespace CodeGen
312} // end namespace clang
Chris Lattnerbed31442007-05-28 01:07:47 +0000313
314#endif