blob: 1b078879dfff8f7fbfe374a6ba5b39b5207fd12b [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 Lattner45bb9142007-06-09 02:28:57 +000019#include <vector>
Chris Lattner308f4312007-05-29 23:50:05 +000020
Chris Lattnerbed31442007-05-28 01:07:47 +000021namespace llvm {
22 class Module;
23namespace clang {
Chris Lattner45bb9142007-06-09 02:28:57 +000024 class SourceLocation;
25 class TargetInfo;
Chris Lattnerbed31442007-05-28 01:07:47 +000026 class ASTContext;
Chris Lattner84915fa2007-06-02 04:16:21 +000027 class Decl;
Chris Lattnerbed31442007-05-28 01:07:47 +000028 class FunctionDecl;
Chris Lattnerd1af2d22007-05-29 23:17:50 +000029 class QualType;
Chris Lattner45bb9142007-06-09 02:28:57 +000030 class FunctionTypeProto;
Chris Lattner84915fa2007-06-02 04:16:21 +000031
Chris Lattner308f4312007-05-29 23:50:05 +000032 class Stmt;
33 class CompoundStmt;
Chris Lattnerac248202007-05-30 00:13:02 +000034 class LabelStmt;
35 class GotoStmt;
Chris Lattner5269c032007-05-30 21:03:58 +000036 class IfStmt;
Chris Lattner946aa312007-06-05 03:59:43 +000037 class WhileStmt;
Chris Lattner8394d792007-06-05 20:53:16 +000038 class DoStmt;
39 class ForStmt;
Chris Lattner3f3dbee2007-06-02 03:19:07 +000040 class ReturnStmt;
Chris Lattner84915fa2007-06-02 04:16:21 +000041 class DeclStmt;
Chris Lattnerbed31442007-05-28 01:07:47 +000042
Chris Lattner208ae962007-05-30 17:57:17 +000043 class Expr;
Chris Lattnerd7f58862007-06-02 05:24:33 +000044 class DeclRefExpr;
Chris Lattner4347e3692007-06-06 04:54:52 +000045 class StringLiteral;
Chris Lattner208ae962007-05-30 17:57:17 +000046 class IntegerLiteral;
Chris Lattner8394d792007-06-05 20:53:16 +000047 class CastExpr;
Chris Lattner2b228c92007-06-15 21:34:29 +000048 class CallExpr;
Chris Lattnerf0106d22007-06-02 19:33:17 +000049 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000050 class BinaryOperator;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000051 class ArraySubscriptExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000052
Chris Lattner84915fa2007-06-02 04:16:21 +000053 class BlockVarDecl;
54 class EnumConstantDecl;
Chris Lattner53621a52007-06-13 20:44:40 +000055 class ParmVarDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000056namespace CodeGen {
57 class CodeGenModule;
58
Chris Lattnerd7f58862007-06-02 05:24:33 +000059
Chris Lattner8394d792007-06-05 20:53:16 +000060/// RValue - This trivial value class is used to represent the result of an
Chris Lattnerd7f58862007-06-02 05:24:33 +000061/// expression that is evaluated. It can be one of two things: either a simple
62/// LLVM SSA value, or the address of an aggregate value in memory. These two
63/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner8394d792007-06-05 20:53:16 +000064class RValue {
Chris Lattner208ae962007-05-30 17:57:17 +000065 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000066 // TODO: Encode this into the low bit of pointer for more efficient
67 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000068 bool IsAggregate;
69public:
70
71 bool isAggregate() const { return IsAggregate; }
72 bool isScalar() const { return !IsAggregate; }
73
74 /// getVal() - Return the Value* of this scalar value.
75 Value *getVal() const {
76 assert(!isAggregate() && "Not a scalar!");
77 return V;
78 }
79
80 /// getAggregateVal() - Return the Value* of the address of the aggregate.
81 Value *getAggregateVal() const {
82 assert(isAggregate() && "Not an aggregate!");
83 return V;
84 }
Chris Lattner208ae962007-05-30 17:57:17 +000085
Chris Lattner8394d792007-06-05 20:53:16 +000086 static RValue get(Value *V) {
87 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000088 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000089 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000090 return ER;
91 }
Chris Lattner8394d792007-06-05 20:53:16 +000092 static RValue getAggregate(Value *V) {
93 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000094 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000095 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000096 return ER;
97 }
98};
Chris Lattnerd7f58862007-06-02 05:24:33 +000099
100
101/// LValue - This represents an lvalue references. Because C/C++ allow
102/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
103/// bitrange.
104class LValue {
105 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000106 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000107 llvm::Value *V;
108public:
109 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000110
Chris Lattnerd7f58862007-06-02 05:24:33 +0000111 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
112
113 static LValue getAddr(Value *V) {
114 LValue R;
115 R.V = V;
116 return R;
117 }
118};
119
Chris Lattnerbed31442007-05-28 01:07:47 +0000120/// CodeGenFunction - This class organizes the per-function state that is used
121/// while generating LLVM code.
122class CodeGenFunction {
123 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000124 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000125 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000126
127 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000128 llvm::Function *CurFn;
129
Chris Lattner03df1222007-06-02 04:53:11 +0000130 /// AllocaInsertPoint - This is an instruction in the entry block before which
131 /// we prefer to insert allocas.
132 llvm::Instruction *AllocaInsertPt;
133
Chris Lattner6db1fb82007-06-02 22:49:07 +0000134 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000135 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000136
Chris Lattner84915fa2007-06-02 04:16:21 +0000137 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
138 /// decls.
139 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
140
Chris Lattnerac248202007-05-30 00:13:02 +0000141 /// LabelMap - This keeps track of the LLVM basic block for each C label.
142 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000143public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000144 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000145
Chris Lattner6db1fb82007-06-02 22:49:07 +0000146 ASTContext &getContext() const;
147
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000148 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
Chris Lattner45bb9142007-06-09 02:28:57 +0000149 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
150 std::vector<const llvm::Type*> &ArgTys,
151 SourceLocation Loc);
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000152
Chris Lattner308f4312007-05-29 23:50:05 +0000153 void GenerateCode(const FunctionDecl *FD);
154
Chris Lattnerac248202007-05-30 00:13:02 +0000155
156 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
157 /// label maps to.
158 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
159
160
161 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000162
Chris Lattner8394d792007-06-05 20:53:16 +0000163
164 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
165 /// expression and compare the result against zero, returning an Int1Ty value.
166 Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnerac248202007-05-30 00:13:02 +0000167
Chris Lattner8394d792007-06-05 20:53:16 +0000168 //===--------------------------------------------------------------------===//
169 // Conversions
170 //===--------------------------------------------------------------------===//
171
172 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
173 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000174 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
175 SourceLocation Loc);
Chris Lattner8394d792007-06-05 20:53:16 +0000176
177 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000178 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +0000179 Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000180
Chris Lattner308f4312007-05-29 23:50:05 +0000181 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000182 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000183 //===--------------------------------------------------------------------===//
184
Chris Lattner1ad38f82007-06-09 01:20:56 +0000185 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000186 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000187 void EmitBlockVarDecl(const BlockVarDecl &D);
188 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000189 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000190
Chris Lattner84915fa2007-06-02 04:16:21 +0000191 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000192 // Statement Emission
193 //===--------------------------------------------------------------------===//
194
195 void EmitStmt(const Stmt *S);
196 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000197 void EmitLabelStmt(const LabelStmt &S);
198 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000199 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000200 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000201 void EmitDoStmt(const DoStmt &S);
202 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000203 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000204 void EmitDeclStmt(const DeclStmt &S);
205
Chris Lattner208ae962007-05-30 17:57:17 +0000206 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000207 // LValue Expression Emission
208 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000209
210 /// EmitLValue - Emit code to compute a designator that specifies the location
211 /// of the expression.
212 ///
213 /// This can return one of two things: a simple address or a bitfield
214 /// reference. In either case, the LLVM Value* in the LValue structure is
215 /// guaranteed to be an LLVM pointer type.
216 ///
217 /// If this returns a bitfield reference, nothing about the pointee type of
218 /// the LLVM value is known: For example, it may not be a pointer to an
219 /// integer.
220 ///
221 /// If this returns a normal address, and if the lvalue's C type is fixed
222 /// size, this method guarantees that the returned pointer type will point to
223 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
224 /// variable length type, this is not possible.
225 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000226 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000227
228 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
229 /// this method emits the address of the lvalue, then loads the result as an
230 /// rvalue, returning the rvalue.
231 RValue EmitLoadOfLValue(const Expr *E);
232
233 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
234 /// lvalue, where both are guaranteed to the have the same type, and that type
235 /// is 'Ty'.
236 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
237
Chris Lattnerd7f58862007-06-02 05:24:33 +0000238 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000239 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000240 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000241 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000242
243 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000244 // Expression Emission
245 //===--------------------------------------------------------------------===//
246
Chris Lattner8394d792007-06-05 20:53:16 +0000247 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000248 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000249 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000250
Chris Lattner8394d792007-06-05 20:53:16 +0000251 RValue EmitExpr(const Expr *E);
252 RValue EmitIntegerLiteral(const IntegerLiteral *E);
253
254 RValue EmitCastExpr(const CastExpr *E);
Chris Lattner2b228c92007-06-15 21:34:29 +0000255 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000256
Chris Lattnerf0106d22007-06-02 19:33:17 +0000257 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000258 RValue EmitUnaryOperator(const UnaryOperator *E);
259 // FIXME: pre/post inc/dec
260 RValue EmitUnaryAddrOf (const UnaryOperator *E);
261 RValue EmitUnaryPlus (const UnaryOperator *E);
262 RValue EmitUnaryMinus (const UnaryOperator *E);
263 RValue EmitUnaryNot (const UnaryOperator *E);
264 RValue EmitUnaryLNot (const UnaryOperator *E);
265 // FIXME: SIZEOF/ALIGNOF(expr).
266 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000267
Chris Lattnerdb91b162007-06-02 00:16:28 +0000268 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000269 RValue EmitBinaryOperator(const BinaryOperator *E);
270 RValue EmitBinaryMul(const BinaryOperator *E);
271 RValue EmitBinaryDiv(const BinaryOperator *E);
272 RValue EmitBinaryRem(const BinaryOperator *E);
273 RValue EmitBinaryAdd(const BinaryOperator *E);
274 RValue EmitBinarySub(const BinaryOperator *E);
275 RValue EmitBinaryShl(const BinaryOperator *E);
276 RValue EmitBinaryShr(const BinaryOperator *E);
277
278 // FIXME: relational
279
280 RValue EmitBinaryAnd(const BinaryOperator *E);
281 RValue EmitBinaryXor(const BinaryOperator *E);
282 RValue EmitBinaryOr (const BinaryOperator *E);
283 RValue EmitBinaryLAnd(const BinaryOperator *E);
284 RValue EmitBinaryLOr(const BinaryOperator *E);
285
286 RValue EmitBinaryAssign(const BinaryOperator *E);
287 // FIXME: Assignment.
288
289 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000290};
291} // end namespace CodeGen
292} // end namespace clang
293} // end namespace llvm
294
295#endif