blob: ad80b382e27e3f186d111ca4cdb3b51073fe1212 [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 Lattnerf0106d22007-06-02 19:33:17 +000048 class UnaryOperator;
Chris Lattnerdb91b162007-06-02 00:16:28 +000049 class BinaryOperator;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +000050 class ArraySubscriptExpr;
Chris Lattner208ae962007-05-30 17:57:17 +000051
Chris Lattner84915fa2007-06-02 04:16:21 +000052 class BlockVarDecl;
53 class EnumConstantDecl;
Chris Lattner53621a52007-06-13 20:44:40 +000054 class ParmVarDecl;
Chris Lattnerbed31442007-05-28 01:07:47 +000055namespace CodeGen {
56 class CodeGenModule;
57
Chris Lattnerd7f58862007-06-02 05:24:33 +000058
Chris Lattner8394d792007-06-05 20:53:16 +000059/// RValue - This trivial value class is used to represent the result of an
Chris Lattnerd7f58862007-06-02 05:24:33 +000060/// expression that is evaluated. It can be one of two things: either a simple
61/// LLVM SSA value, or the address of an aggregate value in memory. These two
62/// possibilities are discriminated by isAggregate/isScalar.
Chris Lattner8394d792007-06-05 20:53:16 +000063class RValue {
Chris Lattner208ae962007-05-30 17:57:17 +000064 Value *V;
Chris Lattnerdb91b162007-06-02 00:16:28 +000065 // TODO: Encode this into the low bit of pointer for more efficient
66 // return-by-value.
Chris Lattner5269c032007-05-30 21:03:58 +000067 bool IsAggregate;
68public:
69
70 bool isAggregate() const { return IsAggregate; }
71 bool isScalar() const { return !IsAggregate; }
72
73 /// getVal() - Return the Value* of this scalar value.
74 Value *getVal() const {
75 assert(!isAggregate() && "Not a scalar!");
76 return V;
77 }
78
79 /// getAggregateVal() - Return the Value* of the address of the aggregate.
80 Value *getAggregateVal() const {
81 assert(isAggregate() && "Not an aggregate!");
82 return V;
83 }
Chris Lattner208ae962007-05-30 17:57:17 +000084
Chris Lattner8394d792007-06-05 20:53:16 +000085 static RValue get(Value *V) {
86 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000087 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000088 ER.IsAggregate = false;
Chris Lattner208ae962007-05-30 17:57:17 +000089 return ER;
90 }
Chris Lattner8394d792007-06-05 20:53:16 +000091 static RValue getAggregate(Value *V) {
92 RValue ER;
Chris Lattner208ae962007-05-30 17:57:17 +000093 ER.V = V;
Chris Lattner5269c032007-05-30 21:03:58 +000094 ER.IsAggregate = true;
Chris Lattner208ae962007-05-30 17:57:17 +000095 return ER;
96 }
97};
Chris Lattnerd7f58862007-06-02 05:24:33 +000098
99
100/// LValue - This represents an lvalue references. Because C/C++ allow
101/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
102/// bitrange.
103class LValue {
104 // FIXME: Volatility. Restrict?
Chris Lattner8394d792007-06-05 20:53:16 +0000105 // alignment?
Chris Lattnerd7f58862007-06-02 05:24:33 +0000106 llvm::Value *V;
107public:
108 bool isBitfield() const { return false; }
Chris Lattner208ae962007-05-30 17:57:17 +0000109
Chris Lattnerd7f58862007-06-02 05:24:33 +0000110 llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
111
112 static LValue getAddr(Value *V) {
113 LValue R;
114 R.V = V;
115 return R;
116 }
117};
118
Chris Lattnerbed31442007-05-28 01:07:47 +0000119/// CodeGenFunction - This class organizes the per-function state that is used
120/// while generating LLVM code.
121class CodeGenFunction {
122 CodeGenModule &CGM; // Per-module state.
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000123 TargetInfo &Target;
Chris Lattner308f4312007-05-29 23:50:05 +0000124 LLVMBuilder Builder;
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000125
126 const FunctionDecl *CurFuncDecl;
Chris Lattnerac248202007-05-30 00:13:02 +0000127 llvm::Function *CurFn;
128
Chris Lattner03df1222007-06-02 04:53:11 +0000129 /// AllocaInsertPoint - This is an instruction in the entry block before which
130 /// we prefer to insert allocas.
131 llvm::Instruction *AllocaInsertPt;
132
Chris Lattner6db1fb82007-06-02 22:49:07 +0000133 const llvm::Type *LLVMIntTy;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000134 unsigned LLVMPointerWidth;
Chris Lattner6db1fb82007-06-02 22:49:07 +0000135
Chris Lattner84915fa2007-06-02 04:16:21 +0000136 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
137 /// decls.
138 DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
139
Chris Lattnerac248202007-05-30 00:13:02 +0000140 /// LabelMap - This keeps track of the LLVM basic block for each C label.
141 DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerbed31442007-05-28 01:07:47 +0000142public:
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000143 CodeGenFunction(CodeGenModule &cgm);
Chris Lattnerbed31442007-05-28 01:07:47 +0000144
Chris Lattner6db1fb82007-06-02 22:49:07 +0000145 ASTContext &getContext() const;
146
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000147 const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
Chris Lattner45bb9142007-06-09 02:28:57 +0000148 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
149 std::vector<const llvm::Type*> &ArgTys,
150 SourceLocation Loc);
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000151
Chris Lattner308f4312007-05-29 23:50:05 +0000152 void GenerateCode(const FunctionDecl *FD);
153
Chris Lattnerac248202007-05-30 00:13:02 +0000154
155 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
156 /// label maps to.
157 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
158
159
160 void EmitBlock(BasicBlock *BB);
Chris Lattner84915fa2007-06-02 04:16:21 +0000161
Chris Lattner8394d792007-06-05 20:53:16 +0000162
163 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
164 /// expression and compare the result against zero, returning an Int1Ty value.
165 Value *EvaluateExprAsBool(const Expr *E);
Chris Lattnerac248202007-05-30 00:13:02 +0000166
Chris Lattner8394d792007-06-05 20:53:16 +0000167 //===--------------------------------------------------------------------===//
168 // Conversions
169 //===--------------------------------------------------------------------===//
170
171 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
172 /// the type specified by DstTy, following the rules of C99 6.3.
Chris Lattnercf106ab2007-06-06 04:05:39 +0000173 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy,
174 SourceLocation Loc);
Chris Lattner8394d792007-06-05 20:53:16 +0000175
176 /// ConvertScalarValueToBool - Convert the specified expression value to a
Chris Lattnerf0106d22007-06-02 19:33:17 +0000177 /// boolean (i1) truth value. This is equivalent to "Val == 0".
Chris Lattner8394d792007-06-05 20:53:16 +0000178 Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
Chris Lattnerf0106d22007-06-02 19:33:17 +0000179
Chris Lattner308f4312007-05-29 23:50:05 +0000180 //===--------------------------------------------------------------------===//
Chris Lattner53621a52007-06-13 20:44:40 +0000181 // Declaration Emission
Chris Lattner84915fa2007-06-02 04:16:21 +0000182 //===--------------------------------------------------------------------===//
183
Chris Lattner1ad38f82007-06-09 01:20:56 +0000184 void EmitDecl(const Decl &D);
Chris Lattner84915fa2007-06-02 04:16:21 +0000185 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Chris Lattner03df1222007-06-02 04:53:11 +0000186 void EmitBlockVarDecl(const BlockVarDecl &D);
187 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
Chris Lattner53621a52007-06-13 20:44:40 +0000188 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
Chris Lattner03df1222007-06-02 04:53:11 +0000189
Chris Lattner84915fa2007-06-02 04:16:21 +0000190 //===--------------------------------------------------------------------===//
Chris Lattner308f4312007-05-29 23:50:05 +0000191 // Statement Emission
192 //===--------------------------------------------------------------------===//
193
194 void EmitStmt(const Stmt *S);
195 void EmitCompoundStmt(const CompoundStmt &S);
Chris Lattnerac248202007-05-30 00:13:02 +0000196 void EmitLabelStmt(const LabelStmt &S);
197 void EmitGotoStmt(const GotoStmt &S);
Chris Lattner5269c032007-05-30 21:03:58 +0000198 void EmitIfStmt(const IfStmt &S);
Chris Lattner946aa312007-06-05 03:59:43 +0000199 void EmitWhileStmt(const WhileStmt &S);
Chris Lattner8394d792007-06-05 20:53:16 +0000200 void EmitDoStmt(const DoStmt &S);
201 void EmitForStmt(const ForStmt &S);
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000202 void EmitReturnStmt(const ReturnStmt &S);
Chris Lattner1ad38f82007-06-09 01:20:56 +0000203 void EmitDeclStmt(const DeclStmt &S);
204
Chris Lattner208ae962007-05-30 17:57:17 +0000205 //===--------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000206 // LValue Expression Emission
207 //===--------------------------------------------------------------------===//
Chris Lattner8394d792007-06-05 20:53:16 +0000208
209 /// EmitLValue - Emit code to compute a designator that specifies the location
210 /// of the expression.
211 ///
212 /// This can return one of two things: a simple address or a bitfield
213 /// reference. In either case, the LLVM Value* in the LValue structure is
214 /// guaranteed to be an LLVM pointer type.
215 ///
216 /// If this returns a bitfield reference, nothing about the pointee type of
217 /// the LLVM value is known: For example, it may not be a pointer to an
218 /// integer.
219 ///
220 /// If this returns a normal address, and if the lvalue's C type is fixed
221 /// size, this method guarantees that the returned pointer type will point to
222 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
223 /// variable length type, this is not possible.
224 ///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000225 LValue EmitLValue(const Expr *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000226
227 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
228 /// this method emits the address of the lvalue, then loads the result as an
229 /// rvalue, returning the rvalue.
230 RValue EmitLoadOfLValue(const Expr *E);
231
232 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
233 /// lvalue, where both are guaranteed to the have the same type, and that type
234 /// is 'Ty'.
235 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
236
Chris Lattnerd7f58862007-06-02 05:24:33 +0000237 LValue EmitDeclRefLValue(const DeclRefExpr *E);
Chris Lattner4347e3692007-06-06 04:54:52 +0000238 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattner8394d792007-06-05 20:53:16 +0000239 LValue EmitUnaryOpLValue(const UnaryOperator *E);
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000240 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnerd7f58862007-06-02 05:24:33 +0000241
242 //===--------------------------------------------------------------------===//
Chris Lattner208ae962007-05-30 17:57:17 +0000243 // Expression Emission
244 //===--------------------------------------------------------------------===//
245
Chris Lattner8394d792007-06-05 20:53:16 +0000246 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
Chris Lattnercf250242007-06-03 02:02:44 +0000247 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
Chris Lattner8394d792007-06-05 20:53:16 +0000248 RValue &LHS, RValue &RHS);
Chris Lattnerdb91b162007-06-02 00:16:28 +0000249
Chris Lattner8394d792007-06-05 20:53:16 +0000250 RValue EmitExpr(const Expr *E);
251 RValue EmitIntegerLiteral(const IntegerLiteral *E);
252
253 RValue EmitCastExpr(const CastExpr *E);
254
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
291} // end namespace llvm
292
293#endif