blob: cda8c55b73f864916e4cb9299b83a770a9605e56 [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 Lattner9369a562007-06-29 16:31:29 +000052 class CompoundAssignOperator;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000053 class ArraySubscriptExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000054
Chris Lattner84915fa2007-06-02 04:16:21 +000055 class BlockVarDecl;
56 class EnumConstantDecl;
Chris Lattner53621a52007-06-13 20:44:40 +000057 class ParmVarDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000058namespace CodeGen {
59 class CodeGenModule;
60
Chris Lattnerd7f58862007-06-02 05:24:33 +000061
Chris Lattner8394d792007-06-05 20:53:16 +000062/// RValue - This trivial value class is used to represent the result of an
Chris Lattnerd7f58862007-06-02 05:24:33 +000063/// expression that is evaluated. It can be one of two things: either a simple
64/// LLVM SSA value, or the address of an aggregate value in memory. These two
65/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner8394d792007-06-05 20:53:16 +000066class RValue {
Chris Lattner23b7eb62007-06-15 23:05:46 +000067 llvm::Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000068 // TODO: Encode this into the low bit of pointer for more efficient
69 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000070 bool IsAggregate;
Chris Lattner09153c02007-06-22 18:48:09 +000071
72 // FIXME: Aggregate rvalues need to retain information about whether they are
73 // volatile or not.
Chris Lattner5269c032007-05-30 21:03:58 +000074public:
75
76 bool isAggregate() const { return IsAggregate; }
77 bool isScalar() const { return !IsAggregate; }
78
79 /// getVal() - Return the Value* of this scalar value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000080 llvm::Value *getVal() const {
Chris Lattner5269c032007-05-30 21:03:58 +000081 assert(!isAggregate() && "Not a scalar!");
82 return V;
83 }
84
Chris Lattner09153c02007-06-22 18:48:09 +000085 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
86 llvm::Value *getAggregateAddr() const {
Chris Lattner5269c032007-05-30 21:03:58 +000087 assert(isAggregate() && "Not an aggregate!");
88 return V;
89 }
Chris Lattner208ae962007-05-30 17:57:17 +000090
Chris Lattner23b7eb62007-06-15 23:05:46 +000091 static RValue get(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000092 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000093 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000094 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000095 return ER;
96 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000097 static RValue getAggregate(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000098 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000099 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +0000100 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +0000101 return ER;
102 }
103};
Chris Lattnerd7f58862007-06-02 05:24:33 +0000104
105
106/// LValue - This represents an lvalue references. Because C/C++ allow
107/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
108/// bitrange.
109class LValue {
110 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000111 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000112 llvm::Value *V;
113public:
114 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000115
Chris Lattnerd7f58862007-06-02 05:24:33 +0000116 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
117
Chris Lattner23b7eb62007-06-15 23:05:46 +0000118 static LValue getAddr(llvm::Value *V) {
Chris Lattnerd7f58862007-06-02 05:24:33 +0000119 LValue R;
120 R.V = V;
121 return R;
122 }
123};
124
Chris Lattnerbed31442007-05-28 01:07:47 +0000125/// CodeGenFunction - This class organizes the per-function state that is used
126/// while generating LLVM code.
127class CodeGenFunction {
128 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000129 TargetInfo &Target;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000130 llvm::LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000131
132 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000133 llvm::Function *CurFn;
134
Chris Lattner03df1222007-06-02 04:53:11 +0000135 /// AllocaInsertPoint - This is an instruction in the entry block before which
136 /// we prefer to insert allocas.
137 llvm::Instruction *AllocaInsertPt;
138
Chris Lattner6db1fb82007-06-02 22:49:07 +0000139 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000140 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000141
Chris Lattner84915fa2007-06-02 04:16:21 +0000142 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
143 /// decls.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000144 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Chris Lattner84915fa2007-06-02 04:16:21 +0000145
Chris Lattnerac248202007-05-30 00:13:02 +0000146 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000147 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000148public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000149 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000150
Chris Lattner6db1fb82007-06-02 22:49:07 +0000151 ASTContext &getContext() const;
152
Chris Lattner308f4312007-05-29 23:50:05 +0000153 void GenerateCode(const FunctionDecl *FD);
154
Chris Lattnerf033c142007-06-22 19:05:19 +0000155 const llvm::Type *ConvertType(QualType T);
Chris Lattnerac248202007-05-30 00:13:02 +0000156
Chris Lattner54fb19e2007-06-22 22:02:34 +0000157 /// hasAggregateLLVMType - Return true if the specified AST type will map into
158 /// an aggregate LLVM type or is void.
159 static bool hasAggregateLLVMType(QualType T);
160
Chris Lattnerac248202007-05-30 00:13:02 +0000161 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
162 /// label maps to.
163 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
164
165
Chris Lattner23b7eb62007-06-15 23:05:46 +0000166 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000167
Chris Lattnere9a64532007-06-22 21:44:33 +0000168 //===--------------------------------------------------------------------===//
169 // Helpers
170 //===--------------------------------------------------------------------===//
171
172 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
173 /// block.
174 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
175 const char *Name = "tmp");
176
Chris Lattner8394d792007-06-05 20:53:16 +0000177 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
178 /// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000179 llvm::Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnere9a64532007-06-22 21:44:33 +0000180
Chris Lattnerac248202007-05-30 00:13:02 +0000181
Chris Lattnere9a64532007-06-22 21:44:33 +0000182 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
183 /// load the real and imaginary pieces, returning them as Real/Imag.
184 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
185
186 /// EmitStoreOfComplex - Store the specified real/imag parts into the
187 /// specified value pointer.
188 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
189 llvm::Value *ResPtr);
190
Chris Lattner8394d792007-06-05 20:53:16 +0000191 //===--------------------------------------------------------------------===//
192 // Conversions
193 //===--------------------------------------------------------------------===//
194
195 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
196 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnerf033c142007-06-22 19:05:19 +0000197 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
Chris Lattner8394d792007-06-05 20:53:16 +0000198
199 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000200 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000201 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000202
Chris Lattner308f4312007-05-29 23:50:05 +0000203 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000204 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000205 //===--------------------------------------------------------------------===//
206
Chris Lattner1ad38f82007-06-09 01:20:56 +0000207 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000208 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000209 void EmitBlockVarDecl(const BlockVarDecl &D);
210 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000211 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000212
Chris Lattner84915fa2007-06-02 04:16:21 +0000213 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000214 // Statement Emission
215 //===--------------------------------------------------------------------===//
216
217 void EmitStmt(const Stmt *S);
218 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000219 void EmitLabelStmt(const LabelStmt &S);
220 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000221 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000222 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000223 void EmitDoStmt(const DoStmt &S);
224 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000225 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000226 void EmitDeclStmt(const DeclStmt &S);
227
Chris Lattner208ae962007-05-30 17:57:17 +0000228 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000229 // LValue Expression Emission
230 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000231
232 /// EmitLValue - Emit code to compute a designator that specifies the location
233 /// of the expression.
234 ///
235 /// This can return one of two things: a simple address or a bitfield
236 /// reference. In either case, the LLVM Value* in the LValue structure is
237 /// guaranteed to be an LLVM pointer type.
238 ///
239 /// If this returns a bitfield reference, nothing about the pointee type of
240 /// the LLVM value is known: For example, it may not be a pointer to an
241 /// integer.
242 ///
243 /// If this returns a normal address, and if the lvalue's C type is fixed
244 /// size, this method guarantees that the returned pointer type will point to
245 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
246 /// variable length type, this is not possible.
247 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000248 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000249
250 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
251 /// this method emits the address of the lvalue, then loads the result as an
252 /// rvalue, returning the rvalue.
253 RValue EmitLoadOfLValue(const Expr *E);
Chris Lattner9369a562007-06-29 16:31:29 +0000254 RValue EmitLoadOfLValue(LValue V, QualType LVType);
255
Chris Lattner8394d792007-06-05 20:53:16 +0000256 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
257 /// lvalue, where both are guaranteed to the have the same type, and that type
258 /// is 'Ty'.
259 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
260
Chris Lattnerd7f58862007-06-02 05:24:33 +0000261 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000262 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000263 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000264 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000265
266 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000267 // Expression Emission
268 //===--------------------------------------------------------------------===//
269
Chris Lattner8394d792007-06-05 20:53:16 +0000270 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000271 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000272 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000273
Chris Lattner8394d792007-06-05 20:53:16 +0000274 RValue EmitExpr(const Expr *E);
275 RValue EmitIntegerLiteral(const IntegerLiteral *E);
276
277 RValue EmitCastExpr(const CastExpr *E);
Chris Lattner2b228c92007-06-15 21:34:29 +0000278 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000279
Chris Lattnerf0106d22007-06-02 19:33:17 +0000280 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000281 RValue EmitUnaryOperator(const UnaryOperator *E);
282 // FIXME: pre/post inc/dec
283 RValue EmitUnaryAddrOf (const UnaryOperator *E);
284 RValue EmitUnaryPlus (const UnaryOperator *E);
285 RValue EmitUnaryMinus (const UnaryOperator *E);
286 RValue EmitUnaryNot (const UnaryOperator *E);
287 RValue EmitUnaryLNot (const UnaryOperator *E);
288 // FIXME: SIZEOF/ALIGNOF(expr).
289 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000290
Chris Lattnerdb91b162007-06-02 00:16:28 +0000291 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000292 RValue EmitBinaryOperator(const BinaryOperator *E);
293 RValue EmitBinaryMul(const BinaryOperator *E);
294 RValue EmitBinaryDiv(const BinaryOperator *E);
295 RValue EmitBinaryRem(const BinaryOperator *E);
296 RValue EmitBinaryAdd(const BinaryOperator *E);
297 RValue EmitBinarySub(const BinaryOperator *E);
298 RValue EmitBinaryShl(const BinaryOperator *E);
299 RValue EmitBinaryShr(const BinaryOperator *E);
Chris Lattner1fde0b32007-06-20 18:30:55 +0000300 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
301 unsigned SICmpOpc, unsigned FCmpOpc);
Chris Lattner8394d792007-06-05 20:53:16 +0000302 RValue EmitBinaryAnd(const BinaryOperator *E);
303 RValue EmitBinaryXor(const BinaryOperator *E);
304 RValue EmitBinaryOr (const BinaryOperator *E);
305 RValue EmitBinaryLAnd(const BinaryOperator *E);
306 RValue EmitBinaryLOr(const BinaryOperator *E);
307
308 RValue EmitBinaryAssign(const BinaryOperator *E);
Chris Lattner9369a562007-06-29 16:31:29 +0000309 RValue EmitBinaryAddAssign(const CompoundAssignOperator *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000310 // FIXME: Assignment.
311
312 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000313};
314} // end namespace CodeGen
315} // end namespace clang
Chris Lattnerbed31442007-05-28 01:07:47 +0000316
317#endif