blob: d40ce4b3ced2977599e188d72db2df9336ecd704 [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;
30 class SourceLocation;
31 class QualType;
32 class FunctionTypeProto;
Chris Lattner84915fa2007-06-02 04:16:21 +000033
Chris Lattner308f4312007-05-29 23:50:05 +000034 class Stmt;
35 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000036 class LabelStmt;
37 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000038 class IfStmt;
Chris Lattner946aa312007-06-05 03:59:43 +000039 class WhileStmt;
Chris Lattner8394d792007-06-05 20:53:16 +000040 class DoStmt;
41 class ForStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000042 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000043 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000044
Chris Lattner208ae962007-05-30 17:57:17 +000045 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000046 class DeclRefExpr;
Chris Lattner4347e3692007-06-06 04:54:52 +000047 class StringLiteral;
Chris Lattner208ae962007-05-30 17:57:17 +000048 class IntegerLiteral;
Chris Lattner8394d792007-06-05 20:53:16 +000049 class CastExpr;
Chris Lattner2b228c92007-06-15 21:34:29 +000050 class CallExpr;
Chris Lattnerf0106d22007-06-02 19:33:17 +000051 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000052 class BinaryOperator;
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;
71public:
72
73 bool isAggregate() const { return IsAggregate; }
74 bool isScalar() const { return !IsAggregate; }
75
76 /// getVal() - Return the Value* of this scalar value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000077 llvm::Value *getVal() const {
Chris Lattner5269c032007-05-30 21:03:58 +000078 assert(!isAggregate() && "Not a scalar!");
79 return V;
80 }
81
82 /// getAggregateVal() - Return the Value* of the address of the aggregate.
Chris Lattner23b7eb62007-06-15 23:05:46 +000083 llvm::Value *getAggregateVal() const {
Chris Lattner5269c032007-05-30 21:03:58 +000084 assert(isAggregate() && "Not an aggregate!");
85 return V;
86 }
Chris Lattner208ae962007-05-30 17:57:17 +000087
Chris Lattner23b7eb62007-06-15 23:05:46 +000088 static RValue get(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000089 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000090 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000091 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000092 return ER;
93 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000094 static RValue getAggregate(llvm::Value *V) {
Chris Lattner8394d792007-06-05 20:53:16 +000095 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000096 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000097 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000098 return ER;
99 }
100};
Chris Lattnerd7f58862007-06-02 05:24:33 +0000101
102
103/// LValue - This represents an lvalue references. Because C/C++ allow
104/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
105/// bitrange.
106class LValue {
107 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000108 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000109 llvm::Value *V;
110public:
111 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000112
Chris Lattnerd7f58862007-06-02 05:24:33 +0000113 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
114
Chris Lattner23b7eb62007-06-15 23:05:46 +0000115 static LValue getAddr(llvm::Value *V) {
Chris Lattnerd7f58862007-06-02 05:24:33 +0000116 LValue R;
117 R.V = V;
118 return R;
119 }
120};
121
Chris Lattnerbed31442007-05-28 01:07:47 +0000122/// CodeGenFunction - This class organizes the per-function state that is used
123/// while generating LLVM code.
124class CodeGenFunction {
125 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000126 TargetInfo &Target;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000127 llvm::LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000128
129 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000130 llvm::Function *CurFn;
131
Chris Lattner03df1222007-06-02 04:53:11 +0000132 /// AllocaInsertPoint - This is an instruction in the entry block before which
133 /// we prefer to insert allocas.
134 llvm::Instruction *AllocaInsertPt;
135
Chris Lattner6db1fb82007-06-02 22:49:07 +0000136 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000137 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000138
Chris Lattner84915fa2007-06-02 04:16:21 +0000139 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
140 /// decls.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000141 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Chris Lattner84915fa2007-06-02 04:16:21 +0000142
Chris Lattnerac248202007-05-30 00:13:02 +0000143 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000144 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000145public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000146 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000147
Chris Lattner6db1fb82007-06-02 22:49:07 +0000148 ASTContext &getContext() const;
149
Chris Lattner308f4312007-05-29 23:50:05 +0000150 void GenerateCode(const FunctionDecl *FD);
151
Chris Lattner2052bc82007-06-16 00:12:05 +0000152 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
Chris Lattnerac248202007-05-30 00:13:02 +0000153
154 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
155 /// label maps to.
156 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
157
158
Chris Lattner23b7eb62007-06-15 23:05:46 +0000159 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000160
Chris Lattner8394d792007-06-05 20:53:16 +0000161
162 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
163 /// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000164 llvm::Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnerac248202007-05-30 00:13:02 +0000165
Chris Lattner8394d792007-06-05 20:53:16 +0000166 //===--------------------------------------------------------------------===//
167 // Conversions
168 //===--------------------------------------------------------------------===//
169
170 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
171 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000172 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
173 SourceLocation Loc);
Chris Lattner8394d792007-06-05 20:53:16 +0000174
175 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000176 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000177 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000178
Chris Lattner308f4312007-05-29 23:50:05 +0000179 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000180 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000181 //===--------------------------------------------------------------------===//
182
Chris Lattner1ad38f82007-06-09 01:20:56 +0000183 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000184 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000185 void EmitBlockVarDecl(const BlockVarDecl &D);
186 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000187 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000188
Chris Lattner84915fa2007-06-02 04:16:21 +0000189 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000190 // Statement Emission
191 //===--------------------------------------------------------------------===//
192
193 void EmitStmt(const Stmt *S);
194 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000195 void EmitLabelStmt(const LabelStmt &S);
196 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000197 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000198 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000199 void EmitDoStmt(const DoStmt &S);
200 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000201 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000202 void EmitDeclStmt(const DeclStmt &S);
203
Chris Lattner208ae962007-05-30 17:57:17 +0000204 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000205 // LValue Expression Emission
206 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000207
208 /// EmitLValue - Emit code to compute a designator that specifies the location
209 /// of the expression.
210 ///
211 /// This can return one of two things: a simple address or a bitfield
212 /// reference. In either case, the LLVM Value* in the LValue structure is
213 /// guaranteed to be an LLVM pointer type.
214 ///
215 /// If this returns a bitfield reference, nothing about the pointee type of
216 /// the LLVM value is known: For example, it may not be a pointer to an
217 /// integer.
218 ///
219 /// If this returns a normal address, and if the lvalue's C type is fixed
220 /// size, this method guarantees that the returned pointer type will point to
221 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
222 /// variable length type, this is not possible.
223 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000224 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000225
226 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
227 /// this method emits the address of the lvalue, then loads the result as an
228 /// rvalue, returning the rvalue.
229 RValue EmitLoadOfLValue(const Expr *E);
230
231 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
232 /// lvalue, where both are guaranteed to the have the same type, and that type
233 /// is 'Ty'.
234 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
235
Chris Lattnerd7f58862007-06-02 05:24:33 +0000236 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000237 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000238 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000239 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000240
241 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000242 // Expression Emission
243 //===--------------------------------------------------------------------===//
244
Chris Lattner8394d792007-06-05 20:53:16 +0000245 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000246 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000247 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000248
Chris Lattner8394d792007-06-05 20:53:16 +0000249 RValue EmitExpr(const Expr *E);
250 RValue EmitIntegerLiteral(const IntegerLiteral *E);
251
252 RValue EmitCastExpr(const CastExpr *E);
Chris Lattner2b228c92007-06-15 21:34:29 +0000253 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000254
Chris Lattnerf0106d22007-06-02 19:33:17 +0000255 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000256 RValue EmitUnaryOperator(const UnaryOperator *E);
257 // FIXME: pre/post inc/dec
258 RValue EmitUnaryAddrOf (const UnaryOperator *E);
259 RValue EmitUnaryPlus (const UnaryOperator *E);
260 RValue EmitUnaryMinus (const UnaryOperator *E);
261 RValue EmitUnaryNot (const UnaryOperator *E);
262 RValue EmitUnaryLNot (const UnaryOperator *E);
263 // FIXME: SIZEOF/ALIGNOF(expr).
264 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000265
Chris Lattnerdb91b162007-06-02 00:16:28 +0000266 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000267 RValue EmitBinaryOperator(const BinaryOperator *E);
268 RValue EmitBinaryMul(const BinaryOperator *E);
269 RValue EmitBinaryDiv(const BinaryOperator *E);
270 RValue EmitBinaryRem(const BinaryOperator *E);
271 RValue EmitBinaryAdd(const BinaryOperator *E);
272 RValue EmitBinarySub(const BinaryOperator *E);
273 RValue EmitBinaryShl(const BinaryOperator *E);
274 RValue EmitBinaryShr(const BinaryOperator *E);
275
276 // FIXME: relational
277
278 RValue EmitBinaryAnd(const BinaryOperator *E);
279 RValue EmitBinaryXor(const BinaryOperator *E);
280 RValue EmitBinaryOr (const BinaryOperator *E);
281 RValue EmitBinaryLAnd(const BinaryOperator *E);
282 RValue EmitBinaryLOr(const BinaryOperator *E);
283
284 RValue EmitBinaryAssign(const BinaryOperator *E);
285 // FIXME: Assignment.
286
287 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000288};
289} // end namespace CodeGen
290} // end namespace clang
Chris Lattnerbed31442007-05-28 01:07:47 +0000291
292#endif