blob: 67cefc2714b9308cfb825a2bb3a44d2dac989c61 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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
17#include "llvm/ADT/DenseMap.h"
Chris Lattnerda138702007-07-16 21:28:45 +000018#include "llvm/ADT/SmallVector.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Support/LLVMBuilder.h"
20#include <vector>
21
22namespace llvm {
23 class Module;
24}
25
26namespace clang {
27 class ASTContext;
28 class Decl;
29 class FunctionDecl;
30 class TargetInfo;
31 class QualType;
32 class FunctionTypeProto;
33
34 class Stmt;
35 class CompoundStmt;
36 class LabelStmt;
37 class GotoStmt;
38 class IfStmt;
39 class WhileStmt;
40 class DoStmt;
41 class ForStmt;
42 class ReturnStmt;
43 class DeclStmt;
44
45 class Expr;
46 class DeclRefExpr;
47 class StringLiteral;
48 class IntegerLiteral;
49 class FloatingLiteral;
Chris Lattnerb0a721a2007-07-13 05:18:11 +000050 class CharacterLiteral;
Reid Spencer5f016e22007-07-11 17:01:13 +000051 class CastExpr;
52 class CallExpr;
53 class UnaryOperator;
54 class BinaryOperator;
55 class CompoundAssignOperator;
56 class ArraySubscriptExpr;
Chris Lattnerb0a721a2007-07-13 05:18:11 +000057 class ConditionalOperator;
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59 class BlockVarDecl;
60 class EnumConstantDecl;
61 class ParmVarDecl;
62namespace CodeGen {
63 class CodeGenModule;
64
65
66/// RValue - This trivial value class is used to represent the result of an
67/// expression that is evaluated. It can be one of two things: either a simple
68/// LLVM SSA value, or the address of an aggregate value in memory. These two
69/// possibilities are discriminated by isAggregate/isScalar.
70class RValue {
71 llvm::Value *V;
72 // TODO: Encode this into the low bit of pointer for more efficient
73 // return-by-value.
74 bool IsAggregate;
75
76 // FIXME: Aggregate rvalues need to retain information about whether they are
77 // volatile or not.
78public:
79
80 bool isAggregate() const { return IsAggregate; }
81 bool isScalar() const { return !IsAggregate; }
82
83 /// getVal() - Return the Value* of this scalar value.
84 llvm::Value *getVal() const {
85 assert(!isAggregate() && "Not a scalar!");
86 return V;
87 }
88
89 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
90 llvm::Value *getAggregateAddr() const {
91 assert(isAggregate() && "Not an aggregate!");
92 return V;
93 }
94
95 static RValue get(llvm::Value *V) {
96 RValue ER;
97 ER.V = V;
98 ER.IsAggregate = false;
99 return ER;
100 }
101 static RValue getAggregate(llvm::Value *V) {
102 RValue ER;
103 ER.V = V;
104 ER.IsAggregate = true;
105 return ER;
106 }
107};
108
109
110/// LValue - This represents an lvalue references. Because C/C++ allow
111/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
112/// bitrange.
113class LValue {
114 // FIXME: Volatility. Restrict?
115 // alignment?
116
117 enum {
118 Simple, // This is a normal l-value, use getAddress().
119 VectorElt, // This is a vector element l-value (V[i]), use getVector*
120 BitField // This is a bitfield l-value, use getBitfield*.
121 } LVType;
122
123 llvm::Value *V;
124
125 union {
126 llvm::Value *VectorIdx;
127 };
128public:
129 bool isSimple() const { return LVType == Simple; }
130 bool isVectorElt() const { return LVType == VectorElt; }
131 bool isBitfield() const { return LVType == BitField; }
132
133 // simple lvalue
134 llvm::Value *getAddress() const { assert(isSimple()); return V; }
135 // vector elt lvalue
136 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
137 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
138
139 static LValue MakeAddr(llvm::Value *V) {
140 LValue R;
141 R.LVType = Simple;
142 R.V = V;
143 return R;
144 }
145
146 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
147 LValue R;
148 R.LVType = VectorElt;
149 R.V = Vec;
150 R.VectorIdx = Idx;
151 return R;
152 }
153
154};
155
156/// CodeGenFunction - This class organizes the per-function state that is used
157/// while generating LLVM code.
158class CodeGenFunction {
159 CodeGenModule &CGM; // Per-module state.
160 TargetInfo &Target;
161 llvm::LLVMBuilder Builder;
162
163 const FunctionDecl *CurFuncDecl;
164 llvm::Function *CurFn;
165
166 /// AllocaInsertPoint - This is an instruction in the entry block before which
167 /// we prefer to insert allocas.
168 llvm::Instruction *AllocaInsertPt;
169
170 const llvm::Type *LLVMIntTy;
171 unsigned LLVMPointerWidth;
172
173 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
174 /// decls.
175 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
176
177 /// LabelMap - This keeps track of the LLVM basic block for each C label.
178 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000179
180 // BreakContinueStack - This keeps track of where break and continue
181 // statements should jump to.
182 struct BreakContinue {
183 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
184 : BreakBlock(bb), ContinueBlock(cb) {}
185
186 llvm::BasicBlock *BreakBlock;
187 llvm::BasicBlock *ContinueBlock;
188 };
189 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191public:
192 CodeGenFunction(CodeGenModule &cgm);
193
194 ASTContext &getContext() const;
195
196 void GenerateCode(const FunctionDecl *FD);
197
198 const llvm::Type *ConvertType(QualType T);
199
200 /// hasAggregateLLVMType - Return true if the specified AST type will map into
201 /// an aggregate LLVM type or is void.
202 static bool hasAggregateLLVMType(QualType T);
203
204 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
205 /// label maps to.
206 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
207
208
209 void EmitBlock(llvm::BasicBlock *BB);
210
211 //===--------------------------------------------------------------------===//
212 // Helpers
213 //===--------------------------------------------------------------------===//
214
215 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
216 /// block.
217 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
218 const char *Name = "tmp");
219
220 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
221 /// expression and compare the result against zero, returning an Int1Ty value.
222 llvm::Value *EvaluateExprAsBool(const Expr *E);
223
224
225 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
226 /// load the real and imaginary pieces, returning them as Real/Imag.
227 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
228
229 /// EmitStoreOfComplex - Store the specified real/imag parts into the
230 /// specified value pointer.
231 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
232 llvm::Value *ResPtr);
233
234 //===--------------------------------------------------------------------===//
235 // Conversions
236 //===--------------------------------------------------------------------===//
237
238 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
239 /// the type specified by DstTy, following the rules of C99 6.3.
240 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
241
242 /// ConvertScalarValueToBool - Convert the specified expression value to a
243 /// boolean (i1) truth value. This is equivalent to "Val == 0".
244 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
245
246 //===--------------------------------------------------------------------===//
247 // Declaration Emission
248 //===--------------------------------------------------------------------===//
249
250 void EmitDecl(const Decl &D);
251 void EmitEnumConstantDecl(const EnumConstantDecl &D);
252 void EmitBlockVarDecl(const BlockVarDecl &D);
253 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
254 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
255
256 //===--------------------------------------------------------------------===//
257 // Statement Emission
258 //===--------------------------------------------------------------------===//
259
260 void EmitStmt(const Stmt *S);
261 void EmitCompoundStmt(const CompoundStmt &S);
262 void EmitLabelStmt(const LabelStmt &S);
263 void EmitGotoStmt(const GotoStmt &S);
264 void EmitIfStmt(const IfStmt &S);
265 void EmitWhileStmt(const WhileStmt &S);
266 void EmitDoStmt(const DoStmt &S);
267 void EmitForStmt(const ForStmt &S);
268 void EmitReturnStmt(const ReturnStmt &S);
269 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000270 void EmitBreakStmt();
271 void EmitContinueStmt();
272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 //===--------------------------------------------------------------------===//
274 // LValue Expression Emission
275 //===--------------------------------------------------------------------===//
276
277 /// EmitLValue - Emit code to compute a designator that specifies the location
278 /// of the expression.
279 ///
280 /// This can return one of two things: a simple address or a bitfield
281 /// reference. In either case, the LLVM Value* in the LValue structure is
282 /// guaranteed to be an LLVM pointer type.
283 ///
284 /// If this returns a bitfield reference, nothing about the pointee type of
285 /// the LLVM value is known: For example, it may not be a pointer to an
286 /// integer.
287 ///
288 /// If this returns a normal address, and if the lvalue's C type is fixed
289 /// size, this method guarantees that the returned pointer type will point to
290 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
291 /// variable length type, this is not possible.
292 ///
293 LValue EmitLValue(const Expr *E);
294
295 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
296 /// this method emits the address of the lvalue, then loads the result as an
297 /// rvalue, returning the rvalue.
298 RValue EmitLoadOfLValue(const Expr *E);
299 RValue EmitLoadOfLValue(LValue V, QualType LVType);
300
301 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
302 /// lvalue, where both are guaranteed to the have the same type, and that type
303 /// is 'Ty'.
304 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
305
306 LValue EmitDeclRefLValue(const DeclRefExpr *E);
307 LValue EmitStringLiteralLValue(const StringLiteral *E);
308 LValue EmitUnaryOpLValue(const UnaryOperator *E);
309 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
310
311 //===--------------------------------------------------------------------===//
312 // Expression Emission
313 //===--------------------------------------------------------------------===//
314
315 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
316 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
317 RValue &LHS, RValue &RHS);
318 void EmitShiftOperands(const BinaryOperator *E, RValue &LHS, RValue &RHS);
319
320 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
321 LValue &LHSLV, RValue &LHS, RValue &RHS);
322 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
323 LValue LHSLV, RValue ResV);
324
325
326 RValue EmitExpr(const Expr *E);
327 RValue EmitIntegerLiteral(const IntegerLiteral *E);
328 RValue EmitFloatingLiteral(const FloatingLiteral *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000329 RValue EmitCharacterLiteral(const CharacterLiteral *E);
330
Chris Lattnerd07eb3b2007-07-13 20:25:53 +0000331 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 RValue EmitCallExpr(const CallExpr *E);
333 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
334
335 // Unary Operators.
336 RValue EmitUnaryOperator(const UnaryOperator *E);
Chris Lattner57274792007-07-11 23:43:46 +0000337 RValue EmitUnaryIncDec (const UnaryOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 RValue EmitUnaryAddrOf (const UnaryOperator *E);
339 RValue EmitUnaryPlus (const UnaryOperator *E);
340 RValue EmitUnaryMinus (const UnaryOperator *E);
341 RValue EmitUnaryNot (const UnaryOperator *E);
342 RValue EmitUnaryLNot (const UnaryOperator *E);
343 // FIXME: SIZEOF/ALIGNOF(expr).
344 // FIXME: real/imag
345
346 // Binary Operators.
347 RValue EmitBinaryOperator(const BinaryOperator *E);
348 RValue EmitBinaryMul(const BinaryOperator *E);
349 RValue EmitBinaryDiv(const BinaryOperator *E);
350 RValue EmitBinaryRem(const BinaryOperator *E);
351 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
352 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
353 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
354 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000355 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
356 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000358 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
359 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
361 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
362 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
363 unsigned SICmpOpc, unsigned FCmpOpc);
364 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
365 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
366 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
367 RValue EmitBinaryLAnd(const BinaryOperator *E);
368 RValue EmitBinaryLOr(const BinaryOperator *E);
369
370 RValue EmitBinaryAssign(const BinaryOperator *E);
371 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000372
373 // Conditional Operator.
374 RValue EmitConditionalOperator(const ConditionalOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375};
376} // end namespace CodeGen
377} // end namespace clang
378
379#endif