blob: b4123123c7081c3c16033187001b6c2c1f402855 [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;
Chris Lattner23b7eb62007-06-15 23:05:46 +000023}
24
Chris Lattnerbed31442007-05-28 01:07:47 +000025namespace clang {
Chris Lattner45bb9142007-06-09 02:28:57 +000026 class SourceLocation;
27 class TargetInfo;
Chris Lattnerbed31442007-05-28 01:07:47 +000028 class ASTContext;
Chris Lattner84915fa2007-06-02 04:16:21 +000029 class Decl;
Chris Lattnerbed31442007-05-28 01:07:47 +000030 class FunctionDecl;
Chris Lattnerd1af2d22007-05-29 23:17:50 +000031 class QualType;
Chris Lattner45bb9142007-06-09 02:28:57 +000032 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 Lattnerd1af2d22007-05-29 23:17:50 +0000150 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
Chris Lattner45bb9142007-06-09 02:28:57 +0000151 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
152 std::vector<const llvm::Type*> &ArgTys,
153 SourceLocation Loc);
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000154
Chris Lattner308f4312007-05-29 23:50:05 +0000155 void GenerateCode(const FunctionDecl *FD);
156
Chris Lattnerac248202007-05-30 00:13:02 +0000157
158 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
159 /// label maps to.
160 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
161
162
Chris Lattner23b7eb62007-06-15 23:05:46 +0000163 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000164
Chris Lattner8394d792007-06-05 20:53:16 +0000165
166 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
167 /// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000168 llvm::Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnerac248202007-05-30 00:13:02 +0000169
Chris Lattner8394d792007-06-05 20:53:16 +0000170 //===--------------------------------------------------------------------===//
171 // Conversions
172 //===--------------------------------------------------------------------===//
173
174 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
175 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000176 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
177 SourceLocation Loc);
Chris Lattner8394d792007-06-05 20:53:16 +0000178
179 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000180 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner23b7eb62007-06-15 23:05:46 +0000181 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000182
Chris Lattner308f4312007-05-29 23:50:05 +0000183 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000184 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000185 //===--------------------------------------------------------------------===//
186
Chris Lattner1ad38f82007-06-09 01:20:56 +0000187 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000188 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000189 void EmitBlockVarDecl(const BlockVarDecl &D);
190 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000191 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000192
Chris Lattner84915fa2007-06-02 04:16:21 +0000193 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000194 // Statement Emission
195 //===--------------------------------------------------------------------===//
196
197 void EmitStmt(const Stmt *S);
198 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000199 void EmitLabelStmt(const LabelStmt &S);
200 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000201 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000202 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000203 void EmitDoStmt(const DoStmt &S);
204 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000205 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000206 void EmitDeclStmt(const DeclStmt &S);
207
Chris Lattner208ae962007-05-30 17:57:17 +0000208 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000209 // LValue Expression Emission
210 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000211
212 /// EmitLValue - Emit code to compute a designator that specifies the location
213 /// of the expression.
214 ///
215 /// This can return one of two things: a simple address or a bitfield
216 /// reference. In either case, the LLVM Value* in the LValue structure is
217 /// guaranteed to be an LLVM pointer type.
218 ///
219 /// If this returns a bitfield reference, nothing about the pointee type of
220 /// the LLVM value is known: For example, it may not be a pointer to an
221 /// integer.
222 ///
223 /// If this returns a normal address, and if the lvalue's C type is fixed
224 /// size, this method guarantees that the returned pointer type will point to
225 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
226 /// variable length type, this is not possible.
227 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000228 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000229
230 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
231 /// this method emits the address of the lvalue, then loads the result as an
232 /// rvalue, returning the rvalue.
233 RValue EmitLoadOfLValue(const Expr *E);
234
235 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
236 /// lvalue, where both are guaranteed to the have the same type, and that type
237 /// is 'Ty'.
238 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
239
Chris Lattnerd7f58862007-06-02 05:24:33 +0000240 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000241 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000242 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000243 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000244
245 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000246 // Expression Emission
247 //===--------------------------------------------------------------------===//
248
Chris Lattner8394d792007-06-05 20:53:16 +0000249 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000250 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000251 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000252
Chris Lattner8394d792007-06-05 20:53:16 +0000253 RValue EmitExpr(const Expr *E);
254 RValue EmitIntegerLiteral(const IntegerLiteral *E);
255
256 RValue EmitCastExpr(const CastExpr *E);
Chris Lattner2b228c92007-06-15 21:34:29 +0000257 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000258
Chris Lattnerf0106d22007-06-02 19:33:17 +0000259 // Unary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000260 RValue EmitUnaryOperator(const UnaryOperator *E);
261 // FIXME: pre/post inc/dec
262 RValue EmitUnaryAddrOf (const UnaryOperator *E);
263 RValue EmitUnaryPlus (const UnaryOperator *E);
264 RValue EmitUnaryMinus (const UnaryOperator *E);
265 RValue EmitUnaryNot (const UnaryOperator *E);
266 RValue EmitUnaryLNot (const UnaryOperator *E);
267 // FIXME: SIZEOF/ALIGNOF(expr).
268 // FIXME: real/imag
Chris Lattnerf0106d22007-06-02 19:33:17 +0000269
Chris Lattnerdb91b162007-06-02 00:16:28 +0000270 // Binary Operators.
Chris Lattner8394d792007-06-05 20:53:16 +0000271 RValue EmitBinaryOperator(const BinaryOperator *E);
272 RValue EmitBinaryMul(const BinaryOperator *E);
273 RValue EmitBinaryDiv(const BinaryOperator *E);
274 RValue EmitBinaryRem(const BinaryOperator *E);
275 RValue EmitBinaryAdd(const BinaryOperator *E);
276 RValue EmitBinarySub(const BinaryOperator *E);
277 RValue EmitBinaryShl(const BinaryOperator *E);
278 RValue EmitBinaryShr(const BinaryOperator *E);
279
280 // FIXME: relational
281
282 RValue EmitBinaryAnd(const BinaryOperator *E);
283 RValue EmitBinaryXor(const BinaryOperator *E);
284 RValue EmitBinaryOr (const BinaryOperator *E);
285 RValue EmitBinaryLAnd(const BinaryOperator *E);
286 RValue EmitBinaryLOr(const BinaryOperator *E);
287
288 RValue EmitBinaryAssign(const BinaryOperator *E);
289 // FIXME: Assignment.
290
291 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerbed31442007-05-28 01:07:47 +0000292};
293} // end namespace CodeGen
294} // end namespace clang
Chris Lattnerbed31442007-05-28 01:07:47 +0000295
296#endif