blob: f804aaa6262b1099fc2d42a4f64c712864cfcbed [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
18#include "llvm/ADT/SmallVector.h"
19#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;
Devang Patele58e0802007-10-04 23:45:31 +000044 class CaseStmt;
45 class DefaultStmt;
46 class SwitchStmt;
47
Chris Lattner4b009652007-07-25 00:24:17 +000048 class Expr;
49 class DeclRefExpr;
50 class StringLiteral;
51 class IntegerLiteral;
52 class FloatingLiteral;
53 class CharacterLiteral;
Chris Lattner4ca7e752007-08-03 17:51:03 +000054 class TypesCompatibleExpr;
55
Chris Lattnerb2cb9cb2007-08-20 22:37:10 +000056 class ImplicitCastExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000057 class CastExpr;
58 class CallExpr;
59 class UnaryOperator;
60 class BinaryOperator;
61 class CompoundAssignOperator;
62 class ArraySubscriptExpr;
Chris Lattnera0d03a72007-08-03 17:31:20 +000063 class OCUVectorElementExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000064 class ConditionalOperator;
Chris Lattner44fcf4f2007-08-04 00:20:15 +000065 class ChooseExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000066 class PreDefinedExpr;
Anders Carlssona66cad42007-08-21 17:43:55 +000067 class ObjCStringLiteral;
Chris Lattner4b009652007-07-25 00:24:17 +000068
69 class BlockVarDecl;
70 class EnumConstantDecl;
71 class ParmVarDecl;
72namespace CodeGen {
73 class CodeGenModule;
74
75
76/// RValue - This trivial value class is used to represent the result of an
Chris Lattnere24c4cf2007-08-31 22:49:20 +000077/// expression that is evaluated. It can be one of three things: either a
78/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
79/// address of an aggregate value in memory.
Chris Lattner4b009652007-07-25 00:24:17 +000080class RValue {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000081 llvm::Value *V1, *V2;
Chris Lattner4b009652007-07-25 00:24:17 +000082 // TODO: Encode this into the low bit of pointer for more efficient
83 // return-by-value.
Chris Lattnere24c4cf2007-08-31 22:49:20 +000084 enum { Scalar, Complex, Aggregate } Flavor;
Chris Lattner4b009652007-07-25 00:24:17 +000085
86 // FIXME: Aggregate rvalues need to retain information about whether they are
87 // volatile or not.
88public:
89
Chris Lattnere24c4cf2007-08-31 22:49:20 +000090 bool isScalar() const { return Flavor == Scalar; }
91 bool isComplex() const { return Flavor == Complex; }
92 bool isAggregate() const { return Flavor == Aggregate; }
Chris Lattner4b009652007-07-25 00:24:17 +000093
Chris Lattnere24c4cf2007-08-31 22:49:20 +000094 /// getScalar() - Return the Value* of this scalar value.
95 llvm::Value *getScalarVal() const {
96 assert(isScalar() && "Not a scalar!");
97 return V1;
Chris Lattner4b009652007-07-25 00:24:17 +000098 }
99
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000100 /// getComplexVal - Return the real/imag components of this complex value.
101 ///
102 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
103 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
104 }
105
Chris Lattner4b009652007-07-25 00:24:17 +0000106 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
107 llvm::Value *getAggregateAddr() const {
108 assert(isAggregate() && "Not an aggregate!");
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000109 return V1;
Chris Lattner4b009652007-07-25 00:24:17 +0000110 }
111
112 static RValue get(llvm::Value *V) {
113 RValue ER;
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000114 ER.V1 = V;
115 ER.Flavor = Scalar;
116 return ER;
117 }
118 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
119 RValue ER;
120 ER.V1 = V1;
121 ER.V2 = V2;
122 ER.Flavor = Complex;
123 return ER;
124 }
125 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
126 RValue ER;
127 ER.V1 = C.first;
128 ER.V2 = C.second;
129 ER.Flavor = Complex;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 return ER;
131 }
132 static RValue getAggregate(llvm::Value *V) {
133 RValue ER;
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000134 ER.V1 = V;
135 ER.Flavor = Aggregate;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 return ER;
137 }
138};
139
140
141/// LValue - This represents an lvalue references. Because C/C++ allow
142/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
143/// bitrange.
144class LValue {
145 // FIXME: Volatility. Restrict?
146 // alignment?
147
148 enum {
Chris Lattner65520192007-08-02 23:37:31 +0000149 Simple, // This is a normal l-value, use getAddress().
150 VectorElt, // This is a vector element l-value (V[i]), use getVector*
151 BitField, // This is a bitfield l-value, use getBitfield*.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000152 OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
Chris Lattner4b009652007-07-25 00:24:17 +0000153 } LVType;
154
155 llvm::Value *V;
156
157 union {
Chris Lattner65520192007-08-02 23:37:31 +0000158 llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
Chris Lattnera0d03a72007-08-03 17:31:20 +0000159 unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
Chris Lattner4b009652007-07-25 00:24:17 +0000160 };
161public:
162 bool isSimple() const { return LVType == Simple; }
163 bool isVectorElt() const { return LVType == VectorElt; }
164 bool isBitfield() const { return LVType == BitField; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000165 bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
Chris Lattner4b009652007-07-25 00:24:17 +0000166
167 // simple lvalue
168 llvm::Value *getAddress() const { assert(isSimple()); return V; }
169 // vector elt lvalue
170 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
171 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000172 // ocu vector elements.
173 llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
174 unsigned getOCUVectorElts() const {
175 assert(isOCUVectorElt());
176 return VectorElts;
Chris Lattner65520192007-08-02 23:37:31 +0000177 }
178
Chris Lattner4b009652007-07-25 00:24:17 +0000179
180 static LValue MakeAddr(llvm::Value *V) {
181 LValue R;
182 R.LVType = Simple;
183 R.V = V;
184 return R;
185 }
186
187 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
188 LValue R;
189 R.LVType = VectorElt;
190 R.V = Vec;
191 R.VectorIdx = Idx;
192 return R;
193 }
194
Chris Lattnera0d03a72007-08-03 17:31:20 +0000195 static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
Chris Lattner65520192007-08-02 23:37:31 +0000196 LValue R;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000197 R.LVType = OCUVectorElt;
Chris Lattner65520192007-08-02 23:37:31 +0000198 R.V = Vec;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000199 R.VectorElts = Elements;
Chris Lattner65520192007-08-02 23:37:31 +0000200 return R;
201 }
Chris Lattner4b009652007-07-25 00:24:17 +0000202};
203
204/// CodeGenFunction - This class organizes the per-function state that is used
205/// while generating LLVM code.
206class CodeGenFunction {
Chris Lattner41b1fca2007-08-26 23:13:56 +0000207public:
Chris Lattner4b009652007-07-25 00:24:17 +0000208 CodeGenModule &CGM; // Per-module state.
209 TargetInfo &Target;
Chris Lattner41b1fca2007-08-26 23:13:56 +0000210
Chris Lattner5280c5f2007-08-21 16:57:55 +0000211 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000212 llvm::LLVMBuilder Builder;
213
214 const FunctionDecl *CurFuncDecl;
215 llvm::Function *CurFn;
216
217 /// AllocaInsertPoint - This is an instruction in the entry block before which
218 /// we prefer to insert allocas.
219 llvm::Instruction *AllocaInsertPt;
220
221 const llvm::Type *LLVMIntTy;
222 unsigned LLVMPointerWidth;
223
Chris Lattner9fba49a2007-08-24 05:35:26 +0000224private:
Chris Lattner4b009652007-07-25 00:24:17 +0000225 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
226 /// decls.
227 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
228
229 /// LabelMap - This keeps track of the LLVM basic block for each C label.
230 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
231
232 // BreakContinueStack - This keeps track of where break and continue
233 // statements should jump to.
234 struct BreakContinue {
235 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
236 : BreakBlock(bb), ContinueBlock(cb) {}
237
238 llvm::BasicBlock *BreakBlock;
239 llvm::BasicBlock *ContinueBlock;
240 };
241 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
242
Devang Patele58e0802007-10-04 23:45:31 +0000243 // SwitchInsn - This is used by EmitCaseStmt() and EmitDefaultStmt() to
244 // populate switch instruction
245 llvm::SwitchInst *SwitchInsn;
246
Chris Lattner4b009652007-07-25 00:24:17 +0000247public:
248 CodeGenFunction(CodeGenModule &cgm);
249
250 ASTContext &getContext() const;
251
252 void GenerateCode(const FunctionDecl *FD);
253
254 const llvm::Type *ConvertType(QualType T);
255
256 /// hasAggregateLLVMType - Return true if the specified AST type will map into
257 /// an aggregate LLVM type or is void.
258 static bool hasAggregateLLVMType(QualType T);
259
260 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
261 /// label maps to.
262 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
263
264
265 void EmitBlock(llvm::BasicBlock *BB);
266
267 //===--------------------------------------------------------------------===//
268 // Helpers
269 //===--------------------------------------------------------------------===//
270
271 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
272 /// block.
273 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
274 const char *Name = "tmp");
275
276 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
277 /// expression and compare the result against zero, returning an Int1Ty value.
278 llvm::Value *EvaluateExprAsBool(const Expr *E);
279
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000280 /// EmitAnyExpr - Emit code to compute the specified expression which can have
281 /// any type. The result is returned as an RValue struct. If this is an
282 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
283 /// the result should be returned.
284 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
285 bool isAggLocVolatile = false);
Devang Patel97299362007-09-28 21:49:18 +0000286
287 /// isDummyBlock - Return true if BB is an empty basic block
288 /// with no predecessors.
289 static bool isDummyBlock(const llvm::BasicBlock *BB);
290
Devang Patele58e0802007-10-04 23:45:31 +0000291 /// StartBlock - Start new block named N. If insert block is a dummy block
292 /// then reuse it.
293 void StartBlock(const char *N);
294
Chris Lattner4b009652007-07-25 00:24:17 +0000295 //===--------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000296 // Declaration Emission
297 //===--------------------------------------------------------------------===//
298
299 void EmitDecl(const Decl &D);
300 void EmitEnumConstantDecl(const EnumConstantDecl &D);
301 void EmitBlockVarDecl(const BlockVarDecl &D);
302 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
303 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
304
305 //===--------------------------------------------------------------------===//
306 // Statement Emission
307 //===--------------------------------------------------------------------===//
308
309 void EmitStmt(const Stmt *S);
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000310 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
311 llvm::Value *AggLoc = 0, bool isAggVol = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000312 void EmitLabelStmt(const LabelStmt &S);
313 void EmitGotoStmt(const GotoStmt &S);
314 void EmitIfStmt(const IfStmt &S);
315 void EmitWhileStmt(const WhileStmt &S);
316 void EmitDoStmt(const DoStmt &S);
317 void EmitForStmt(const ForStmt &S);
318 void EmitReturnStmt(const ReturnStmt &S);
319 void EmitDeclStmt(const DeclStmt &S);
320 void EmitBreakStmt();
321 void EmitContinueStmt();
Devang Patele58e0802007-10-04 23:45:31 +0000322 void EmitSwitchStmt(const SwitchStmt &S);
323 void EmitDefaultStmt(const DefaultStmt &S);
324 void EmitCaseStmt(const CaseStmt &S);
325
Chris Lattner4b009652007-07-25 00:24:17 +0000326 //===--------------------------------------------------------------------===//
327 // LValue Expression Emission
328 //===--------------------------------------------------------------------===//
329
330 /// EmitLValue - Emit code to compute a designator that specifies the location
331 /// of the expression.
332 ///
333 /// This can return one of two things: a simple address or a bitfield
334 /// reference. In either case, the LLVM Value* in the LValue structure is
335 /// guaranteed to be an LLVM pointer type.
336 ///
337 /// If this returns a bitfield reference, nothing about the pointee type of
338 /// the LLVM value is known: For example, it may not be a pointer to an
339 /// integer.
340 ///
341 /// If this returns a normal address, and if the lvalue's C type is fixed
342 /// size, this method guarantees that the returned pointer type will point to
343 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
344 /// variable length type, this is not possible.
345 ///
346 LValue EmitLValue(const Expr *E);
347
348 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
349 /// this method emits the address of the lvalue, then loads the result as an
350 /// rvalue, returning the rvalue.
Chris Lattner4b009652007-07-25 00:24:17 +0000351 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000352 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Chris Lattner4b009652007-07-25 00:24:17 +0000353
Chris Lattner944f7962007-08-03 16:18:34 +0000354
Chris Lattner4b009652007-07-25 00:24:17 +0000355 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
356 /// lvalue, where both are guaranteed to the have the same type, and that type
357 /// is 'Ty'.
358 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000359 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000360
361 LValue EmitDeclRefLValue(const DeclRefExpr *E);
362 LValue EmitStringLiteralLValue(const StringLiteral *E);
363 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
364 LValue EmitUnaryOpLValue(const UnaryOperator *E);
365 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000366 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000367
368 //===--------------------------------------------------------------------===//
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000369 // Scalar Expression Emission
Chris Lattner4b009652007-07-25 00:24:17 +0000370 //===--------------------------------------------------------------------===//
371
Chris Lattner4b009652007-07-25 00:24:17 +0000372 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner02c60f52007-08-31 04:44:06 +0000373 RValue EmitCallExpr(llvm::Value *Callee, const CallExpr *E);
Chris Lattner35055b82007-08-26 22:58:05 +0000374 RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000375
Chris Lattner9fba49a2007-08-24 05:35:26 +0000376 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Anders Carlssona66cad42007-08-21 17:43:55 +0000377
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000378 //===--------------------------------------------------------------------===//
Chris Lattner41b1fca2007-08-26 23:13:56 +0000379 // Expression Emission
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000380 //===--------------------------------------------------------------------===//
Chris Lattner41b1fca2007-08-26 23:13:56 +0000381
382 // Expressions are broken into three classes: scalar, complex, aggregate.
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000383
Chris Lattner9fba49a2007-08-24 05:35:26 +0000384 /// EmitScalarExpr - Emit the computation of the specified expression of
385 /// LLVM scalar type, returning the result.
386 llvm::Value *EmitScalarExpr(const Expr *E);
387
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000388 /// EmitScalarConversion - Emit a conversion from the specified type to the
389 /// specified destination type, both of which are LLVM scalar types.
390 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
391 QualType DstTy);
392
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000393 /// EmitComplexToScalarConversion - Emit a conversion from the specified
394 /// complex type to the specified destination type, where the destination
395 /// type is an LLVM scalar type.
396 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
397 QualType DstTy);
398
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000399
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000400 /// EmitAggExpr - Emit the computation of the specified expression of
401 /// aggregate type. The result is computed into DestPtr. Note that if
402 /// DestPtr is null, the value of the aggregate expression is not needed.
403 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattner8d0cc2f2007-08-21 05:54:00 +0000404
405 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner348c8a22007-08-23 23:43:33 +0000406 /// complex type, returning the result.
Chris Lattner5280c5f2007-08-21 16:57:55 +0000407 ComplexPairTy EmitComplexExpr(const Expr *E);
Chris Lattner348c8a22007-08-23 23:43:33 +0000408
409 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
410 /// of complex type, storing into the specified Value*.
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000411 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
412 bool DestIsVolatile);
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000413 /// LoadComplexFromAddr - Load a complex number from the specified address.
414 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
Chris Lattner4b009652007-07-25 00:24:17 +0000415};
416} // end namespace CodeGen
417} // end namespace clang
418
419#endif