blob: f233108dbd924d38044f3b4d5f4b46796909747c [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;
Chris Lattner30bf3ae2007-08-03 17:51:03 +000051 class TypesCompatibleExpr;
52
Chris Lattner7016a702007-08-20 22:37:10 +000053 class ImplicitCastExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 class CastExpr;
55 class CallExpr;
56 class UnaryOperator;
57 class BinaryOperator;
58 class CompoundAssignOperator;
59 class ArraySubscriptExpr;
Chris Lattner6481a572007-08-03 17:31:20 +000060 class OCUVectorElementExpr;
Chris Lattnerb0a721a2007-07-13 05:18:11 +000061 class ConditionalOperator;
Chris Lattner94f05e32007-08-04 00:20:15 +000062 class ChooseExpr;
Anders Carlsson22742662007-07-21 05:21:51 +000063 class PreDefinedExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
65 class BlockVarDecl;
66 class EnumConstantDecl;
67 class ParmVarDecl;
68namespace CodeGen {
69 class CodeGenModule;
70
71
72/// RValue - This trivial value class is used to represent the result of an
73/// expression that is evaluated. It can be one of two things: either a simple
74/// LLVM SSA value, or the address of an aggregate value in memory. These two
75/// possibilities are discriminated by isAggregate/isScalar.
76class RValue {
77 llvm::Value *V;
78 // TODO: Encode this into the low bit of pointer for more efficient
79 // return-by-value.
80 bool IsAggregate;
81
82 // FIXME: Aggregate rvalues need to retain information about whether they are
83 // volatile or not.
84public:
85
86 bool isAggregate() const { return IsAggregate; }
87 bool isScalar() const { return !IsAggregate; }
88
89 /// getVal() - Return the Value* of this scalar value.
90 llvm::Value *getVal() const {
91 assert(!isAggregate() && "Not a scalar!");
92 return V;
93 }
94
95 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
96 llvm::Value *getAggregateAddr() const {
97 assert(isAggregate() && "Not an aggregate!");
98 return V;
99 }
100
101 static RValue get(llvm::Value *V) {
102 RValue ER;
103 ER.V = V;
104 ER.IsAggregate = false;
105 return ER;
106 }
107 static RValue getAggregate(llvm::Value *V) {
108 RValue ER;
109 ER.V = V;
110 ER.IsAggregate = true;
111 return ER;
112 }
113};
114
115
116/// LValue - This represents an lvalue references. Because C/C++ allow
117/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
118/// bitrange.
119class LValue {
120 // FIXME: Volatility. Restrict?
121 // alignment?
122
123 enum {
Chris Lattner349aaec2007-08-02 23:37:31 +0000124 Simple, // This is a normal l-value, use getAddress().
125 VectorElt, // This is a vector element l-value (V[i]), use getVector*
126 BitField, // This is a bitfield l-value, use getBitfield*.
Chris Lattner6481a572007-08-03 17:31:20 +0000127 OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 } LVType;
129
130 llvm::Value *V;
131
132 union {
Chris Lattner349aaec2007-08-02 23:37:31 +0000133 llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
Chris Lattner6481a572007-08-03 17:31:20 +0000134 unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 };
136public:
137 bool isSimple() const { return LVType == Simple; }
138 bool isVectorElt() const { return LVType == VectorElt; }
139 bool isBitfield() const { return LVType == BitField; }
Chris Lattner6481a572007-08-03 17:31:20 +0000140 bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000141
142 // simple lvalue
143 llvm::Value *getAddress() const { assert(isSimple()); return V; }
144 // vector elt lvalue
145 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
146 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Chris Lattner6481a572007-08-03 17:31:20 +0000147 // ocu vector elements.
148 llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
149 unsigned getOCUVectorElts() const {
150 assert(isOCUVectorElt());
151 return VectorElts;
Chris Lattner349aaec2007-08-02 23:37:31 +0000152 }
153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154
155 static LValue MakeAddr(llvm::Value *V) {
156 LValue R;
157 R.LVType = Simple;
158 R.V = V;
159 return R;
160 }
161
162 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
163 LValue R;
164 R.LVType = VectorElt;
165 R.V = Vec;
166 R.VectorIdx = Idx;
167 return R;
168 }
169
Chris Lattner6481a572007-08-03 17:31:20 +0000170 static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000171 LValue R;
Chris Lattner6481a572007-08-03 17:31:20 +0000172 R.LVType = OCUVectorElt;
Chris Lattner349aaec2007-08-02 23:37:31 +0000173 R.V = Vec;
Chris Lattner6481a572007-08-03 17:31:20 +0000174 R.VectorElts = Elements;
Chris Lattner349aaec2007-08-02 23:37:31 +0000175 return R;
176 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000177};
178
179/// CodeGenFunction - This class organizes the per-function state that is used
180/// while generating LLVM code.
181class CodeGenFunction {
182 CodeGenModule &CGM; // Per-module state.
183 TargetInfo &Target;
184 llvm::LLVMBuilder Builder;
185
186 const FunctionDecl *CurFuncDecl;
187 llvm::Function *CurFn;
188
189 /// AllocaInsertPoint - This is an instruction in the entry block before which
190 /// we prefer to insert allocas.
191 llvm::Instruction *AllocaInsertPt;
192
193 const llvm::Type *LLVMIntTy;
194 unsigned LLVMPointerWidth;
195
196 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
197 /// decls.
198 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
199
200 /// LabelMap - This keeps track of the LLVM basic block for each C label.
201 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000202
203 // BreakContinueStack - This keeps track of where break and continue
204 // statements should jump to.
205 struct BreakContinue {
206 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
207 : BreakBlock(bb), ContinueBlock(cb) {}
208
209 llvm::BasicBlock *BreakBlock;
210 llvm::BasicBlock *ContinueBlock;
211 };
212 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214public:
215 CodeGenFunction(CodeGenModule &cgm);
216
217 ASTContext &getContext() const;
218
219 void GenerateCode(const FunctionDecl *FD);
220
221 const llvm::Type *ConvertType(QualType T);
222
223 /// hasAggregateLLVMType - Return true if the specified AST type will map into
224 /// an aggregate LLVM type or is void.
225 static bool hasAggregateLLVMType(QualType T);
226
227 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
228 /// label maps to.
229 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
230
231
232 void EmitBlock(llvm::BasicBlock *BB);
233
234 //===--------------------------------------------------------------------===//
235 // Helpers
236 //===--------------------------------------------------------------------===//
237
238 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
239 /// block.
240 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
241 const char *Name = "tmp");
242
243 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
244 /// expression and compare the result against zero, returning an Int1Ty value.
245 llvm::Value *EvaluateExprAsBool(const Expr *E);
246
247
248 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
249 /// load the real and imaginary pieces, returning them as Real/Imag.
250 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
251
252 /// EmitStoreOfComplex - Store the specified real/imag parts into the
253 /// specified value pointer.
254 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
255 llvm::Value *ResPtr);
256
257 //===--------------------------------------------------------------------===//
258 // Conversions
259 //===--------------------------------------------------------------------===//
260
261 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
262 /// the type specified by DstTy, following the rules of C99 6.3.
263 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
264
265 /// ConvertScalarValueToBool - Convert the specified expression value to a
266 /// boolean (i1) truth value. This is equivalent to "Val == 0".
267 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
268
269 //===--------------------------------------------------------------------===//
270 // Declaration Emission
271 //===--------------------------------------------------------------------===//
272
273 void EmitDecl(const Decl &D);
274 void EmitEnumConstantDecl(const EnumConstantDecl &D);
275 void EmitBlockVarDecl(const BlockVarDecl &D);
276 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
277 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
278
279 //===--------------------------------------------------------------------===//
280 // Statement Emission
281 //===--------------------------------------------------------------------===//
282
283 void EmitStmt(const Stmt *S);
284 void EmitCompoundStmt(const CompoundStmt &S);
285 void EmitLabelStmt(const LabelStmt &S);
286 void EmitGotoStmt(const GotoStmt &S);
287 void EmitIfStmt(const IfStmt &S);
288 void EmitWhileStmt(const WhileStmt &S);
289 void EmitDoStmt(const DoStmt &S);
290 void EmitForStmt(const ForStmt &S);
291 void EmitReturnStmt(const ReturnStmt &S);
292 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000293 void EmitBreakStmt();
294 void EmitContinueStmt();
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 //===--------------------------------------------------------------------===//
297 // LValue Expression Emission
298 //===--------------------------------------------------------------------===//
299
300 /// EmitLValue - Emit code to compute a designator that specifies the location
301 /// of the expression.
302 ///
303 /// This can return one of two things: a simple address or a bitfield
304 /// reference. In either case, the LLVM Value* in the LValue structure is
305 /// guaranteed to be an LLVM pointer type.
306 ///
307 /// If this returns a bitfield reference, nothing about the pointee type of
308 /// the LLVM value is known: For example, it may not be a pointer to an
309 /// integer.
310 ///
311 /// If this returns a normal address, and if the lvalue's C type is fixed
312 /// size, this method guarantees that the returned pointer type will point to
313 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
314 /// variable length type, this is not possible.
315 ///
316 LValue EmitLValue(const Expr *E);
317
318 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
319 /// this method emits the address of the lvalue, then loads the result as an
320 /// rvalue, returning the rvalue.
321 RValue EmitLoadOfLValue(const Expr *E);
322 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattner6481a572007-08-03 17:31:20 +0000323 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000324
Chris Lattner34cdc862007-08-03 16:18:34 +0000325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
327 /// lvalue, where both are guaranteed to the have the same type, and that type
328 /// is 'Ty'.
329 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000330 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000331
332 LValue EmitDeclRefLValue(const DeclRefExpr *E);
333 LValue EmitStringLiteralLValue(const StringLiteral *E);
Anders Carlsson22742662007-07-21 05:21:51 +0000334 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 LValue EmitUnaryOpLValue(const UnaryOperator *E);
336 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattner6481a572007-08-03 17:31:20 +0000337 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338
339 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000340 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 //===--------------------------------------------------------------------===//
342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
344 LValue &LHSLV, RValue &LHS, RValue &RHS);
345 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
346 LValue LHSLV, RValue ResV);
347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 RValue EmitExpr(const Expr *E);
349 RValue EmitIntegerLiteral(const IntegerLiteral *E);
350 RValue EmitFloatingLiteral(const FloatingLiteral *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000351 RValue EmitCharacterLiteral(const CharacterLiteral *E);
Chris Lattner30bf3ae2007-08-03 17:51:03 +0000352 RValue EmitTypesCompatibleExpr(const TypesCompatibleExpr *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000353
Chris Lattner7016a702007-08-20 22:37:10 +0000354 RValue EmitImplicitCastExpr(const ImplicitCastExpr *Op);
Chris Lattnerd07eb3b2007-07-13 20:25:53 +0000355 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 RValue EmitCallExpr(const CallExpr *E);
Anders Carlsson022012e2007-08-20 18:05:56 +0000357 RValue EmitBuiltinExpr(unsigned builtinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
359
360 // Unary Operators.
361 RValue EmitUnaryOperator(const UnaryOperator *E);
Chris Lattner57274792007-07-11 23:43:46 +0000362 RValue EmitUnaryIncDec (const UnaryOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 RValue EmitUnaryAddrOf (const UnaryOperator *E);
364 RValue EmitUnaryPlus (const UnaryOperator *E);
365 RValue EmitUnaryMinus (const UnaryOperator *E);
366 RValue EmitUnaryNot (const UnaryOperator *E);
367 RValue EmitUnaryLNot (const UnaryOperator *E);
Chris Lattner5e3fbe52007-07-18 18:12:07 +0000368 RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // FIXME: real/imag
370
371 // Binary Operators.
372 RValue EmitBinaryOperator(const BinaryOperator *E);
373 RValue EmitBinaryMul(const BinaryOperator *E);
374 RValue EmitBinaryDiv(const BinaryOperator *E);
375 RValue EmitBinaryRem(const BinaryOperator *E);
376 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
377 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
378 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
379 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000380 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
381 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000383 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
384 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
386 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
387 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
388 unsigned SICmpOpc, unsigned FCmpOpc);
389 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
390 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
391 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
392 RValue EmitBinaryLAnd(const BinaryOperator *E);
393 RValue EmitBinaryLOr(const BinaryOperator *E);
394
395 RValue EmitBinaryAssign(const BinaryOperator *E);
396 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000397
398 // Conditional Operator.
399 RValue EmitConditionalOperator(const ConditionalOperator *E);
Chris Lattner94f05e32007-08-04 00:20:15 +0000400 RValue EmitChooseExpr(const ChooseExpr *E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000401
402 //===--------------------------------------------------------------------===//
403 // Aggregate Expression Emission
404 //===--------------------------------------------------------------------===//
405
406 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
407 QualType EltTy);
408
409 /// EmitAggExpr - Emit the computation of the specified expression of
410 /// aggregate type. The result is computed into DestPtr. Note that if
411 /// DestPtr is null, the value of the aggregate expression is not needed.
412 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
413
414 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
415 /// represents a value lvalue, this method emits the address of the lvalue,
416 /// then loads the result into DestPtr.
417 void EmitAggLoadOfLValue(const Expr *E, llvm::Value *DestPtr, bool VolDest);
418
419
420
421 // Binary Operators.
422 void EmitAggBinaryOperator(const BinaryOperator *E,
423 llvm::Value *DestPtr, bool VolatileDest);
424
425
426 void EmitAggBinaryAssign(const BinaryOperator *E, llvm::Value *DestPtr,
427 bool VolatileDest);
428
429 void EmitAggConditionalOperator(const ConditionalOperator *E,
430 llvm::Value *DestPtr, bool VolatileDest);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431};
432} // end namespace CodeGen
433} // end namespace clang
434
435#endif