blob: 12c227bad9af22ab05742b26f59d5fa3c7eed6f4 [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;
Anders Carlsson55085182007-08-21 17:43:55 +000064 class ObjCStringLiteral;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 class BlockVarDecl;
67 class EnumConstantDecl;
68 class ParmVarDecl;
69namespace CodeGen {
70 class CodeGenModule;
71
72
73/// RValue - This trivial value class is used to represent the result of an
74/// expression that is evaluated. It can be one of two things: either a simple
75/// LLVM SSA value, or the address of an aggregate value in memory. These two
76/// possibilities are discriminated by isAggregate/isScalar.
77class RValue {
78 llvm::Value *V;
79 // TODO: Encode this into the low bit of pointer for more efficient
80 // return-by-value.
81 bool IsAggregate;
82
83 // FIXME: Aggregate rvalues need to retain information about whether they are
84 // volatile or not.
85public:
86
87 bool isAggregate() const { return IsAggregate; }
88 bool isScalar() const { return !IsAggregate; }
89
90 /// getVal() - Return the Value* of this scalar value.
91 llvm::Value *getVal() const {
92 assert(!isAggregate() && "Not a scalar!");
93 return V;
94 }
95
96 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
97 llvm::Value *getAggregateAddr() const {
98 assert(isAggregate() && "Not an aggregate!");
99 return V;
100 }
101
102 static RValue get(llvm::Value *V) {
103 RValue ER;
104 ER.V = V;
105 ER.IsAggregate = false;
106 return ER;
107 }
108 static RValue getAggregate(llvm::Value *V) {
109 RValue ER;
110 ER.V = V;
111 ER.IsAggregate = true;
112 return ER;
113 }
114};
115
116
117/// LValue - This represents an lvalue references. Because C/C++ allow
118/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
119/// bitrange.
120class LValue {
121 // FIXME: Volatility. Restrict?
122 // alignment?
123
124 enum {
Chris Lattner349aaec2007-08-02 23:37:31 +0000125 Simple, // This is a normal l-value, use getAddress().
126 VectorElt, // This is a vector element l-value (V[i]), use getVector*
127 BitField, // This is a bitfield l-value, use getBitfield*.
Chris Lattner6481a572007-08-03 17:31:20 +0000128 OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 } LVType;
130
131 llvm::Value *V;
132
133 union {
Chris Lattner349aaec2007-08-02 23:37:31 +0000134 llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
Chris Lattner6481a572007-08-03 17:31:20 +0000135 unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 };
137public:
138 bool isSimple() const { return LVType == Simple; }
139 bool isVectorElt() const { return LVType == VectorElt; }
140 bool isBitfield() const { return LVType == BitField; }
Chris Lattner6481a572007-08-03 17:31:20 +0000141 bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000142
143 // simple lvalue
144 llvm::Value *getAddress() const { assert(isSimple()); return V; }
145 // vector elt lvalue
146 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
147 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Chris Lattner6481a572007-08-03 17:31:20 +0000148 // ocu vector elements.
149 llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
150 unsigned getOCUVectorElts() const {
151 assert(isOCUVectorElt());
152 return VectorElts;
Chris Lattner349aaec2007-08-02 23:37:31 +0000153 }
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
156 static LValue MakeAddr(llvm::Value *V) {
157 LValue R;
158 R.LVType = Simple;
159 R.V = V;
160 return R;
161 }
162
163 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
164 LValue R;
165 R.LVType = VectorElt;
166 R.V = Vec;
167 R.VectorIdx = Idx;
168 return R;
169 }
170
Chris Lattner6481a572007-08-03 17:31:20 +0000171 static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000172 LValue R;
Chris Lattner6481a572007-08-03 17:31:20 +0000173 R.LVType = OCUVectorElt;
Chris Lattner349aaec2007-08-02 23:37:31 +0000174 R.V = Vec;
Chris Lattner6481a572007-08-03 17:31:20 +0000175 R.VectorElts = Elements;
Chris Lattner349aaec2007-08-02 23:37:31 +0000176 return R;
177 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000178};
179
180/// CodeGenFunction - This class organizes the per-function state that is used
181/// while generating LLVM code.
182class CodeGenFunction {
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000183public:
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 CodeGenModule &CGM; // Per-module state.
185 TargetInfo &Target;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000186
Chris Lattner58dee102007-08-21 16:57:55 +0000187 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 llvm::LLVMBuilder Builder;
189
190 const FunctionDecl *CurFuncDecl;
191 llvm::Function *CurFn;
192
193 /// AllocaInsertPoint - This is an instruction in the entry block before which
194 /// we prefer to insert allocas.
195 llvm::Instruction *AllocaInsertPt;
196
197 const llvm::Type *LLVMIntTy;
198 unsigned LLVMPointerWidth;
199
Chris Lattner7f02f722007-08-24 05:35:26 +0000200private:
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
202 /// decls.
203 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
204
205 /// LabelMap - This keeps track of the LLVM basic block for each C label.
206 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000207
208 // BreakContinueStack - This keeps track of where break and continue
209 // statements should jump to.
210 struct BreakContinue {
211 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
212 : BreakBlock(bb), ContinueBlock(cb) {}
213
214 llvm::BasicBlock *BreakBlock;
215 llvm::BasicBlock *ContinueBlock;
216 };
217 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219public:
220 CodeGenFunction(CodeGenModule &cgm);
221
222 ASTContext &getContext() const;
223
224 void GenerateCode(const FunctionDecl *FD);
225
226 const llvm::Type *ConvertType(QualType T);
227
228 /// hasAggregateLLVMType - Return true if the specified AST type will map into
229 /// an aggregate LLVM type or is void.
230 static bool hasAggregateLLVMType(QualType T);
231
232 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
233 /// label maps to.
234 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
235
236
237 void EmitBlock(llvm::BasicBlock *BB);
238
239 //===--------------------------------------------------------------------===//
240 // Helpers
241 //===--------------------------------------------------------------------===//
242
243 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
244 /// block.
245 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
246 const char *Name = "tmp");
247
248 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
249 /// expression and compare the result against zero, returning an Int1Ty value.
250 llvm::Value *EvaluateExprAsBool(const Expr *E);
251
252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // Declaration Emission
255 //===--------------------------------------------------------------------===//
256
257 void EmitDecl(const Decl &D);
258 void EmitEnumConstantDecl(const EnumConstantDecl &D);
259 void EmitBlockVarDecl(const BlockVarDecl &D);
260 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
261 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
262
263 //===--------------------------------------------------------------------===//
264 // Statement Emission
265 //===--------------------------------------------------------------------===//
266
267 void EmitStmt(const Stmt *S);
268 void EmitCompoundStmt(const CompoundStmt &S);
269 void EmitLabelStmt(const LabelStmt &S);
270 void EmitGotoStmt(const GotoStmt &S);
271 void EmitIfStmt(const IfStmt &S);
272 void EmitWhileStmt(const WhileStmt &S);
273 void EmitDoStmt(const DoStmt &S);
274 void EmitForStmt(const ForStmt &S);
275 void EmitReturnStmt(const ReturnStmt &S);
276 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000277 void EmitBreakStmt();
278 void EmitContinueStmt();
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 //===--------------------------------------------------------------------===//
281 // LValue Expression Emission
282 //===--------------------------------------------------------------------===//
283
284 /// EmitLValue - Emit code to compute a designator that specifies the location
285 /// of the expression.
286 ///
287 /// This can return one of two things: a simple address or a bitfield
288 /// reference. In either case, the LLVM Value* in the LValue structure is
289 /// guaranteed to be an LLVM pointer type.
290 ///
291 /// If this returns a bitfield reference, nothing about the pointee type of
292 /// the LLVM value is known: For example, it may not be a pointer to an
293 /// integer.
294 ///
295 /// If this returns a normal address, and if the lvalue's C type is fixed
296 /// size, this method guarantees that the returned pointer type will point to
297 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
298 /// variable length type, this is not possible.
299 ///
300 LValue EmitLValue(const Expr *E);
301
302 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
303 /// this method emits the address of the lvalue, then loads the result as an
304 /// rvalue, returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattner6481a572007-08-03 17:31:20 +0000306 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307
Chris Lattner34cdc862007-08-03 16:18:34 +0000308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
310 /// lvalue, where both are guaranteed to the have the same type, and that type
311 /// is 'Ty'.
312 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000313 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314
315 LValue EmitDeclRefLValue(const DeclRefExpr *E);
316 LValue EmitStringLiteralLValue(const StringLiteral *E);
Anders Carlsson22742662007-07-21 05:21:51 +0000317 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 LValue EmitUnaryOpLValue(const UnaryOperator *E);
319 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattner6481a572007-08-03 17:31:20 +0000320 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000321
322 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000323 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 //===--------------------------------------------------------------------===//
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 RValue EmitCallExpr(const CallExpr *E);
Chris Lattner1e4d21e2007-08-26 22:58:05 +0000327 RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000328 RValue EmitBuiltinLibFuncExpr(unsigned BuiltinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000329
Chris Lattner7f02f722007-08-24 05:35:26 +0000330 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Anders Carlsson55085182007-08-21 17:43:55 +0000331
Chris Lattner883f6a72007-08-11 00:04:45 +0000332 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000333 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +0000334 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000335
336 // Expressions are broken into three classes: scalar, complex, aggregate.
Chris Lattner883f6a72007-08-11 00:04:45 +0000337
Chris Lattner7f02f722007-08-24 05:35:26 +0000338 /// EmitScalarExpr - Emit the computation of the specified expression of
339 /// LLVM scalar type, returning the result.
340 llvm::Value *EmitScalarExpr(const Expr *E);
341
Chris Lattner3707b252007-08-26 06:48:56 +0000342 /// EmitScalarConversion - Emit a conversion from the specified type to the
343 /// specified destination type, both of which are LLVM scalar types.
344 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
345 QualType DstTy);
346
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000347 /// EmitComplexToScalarConversion - Emit a conversion from the specified
348 /// complex type to the specified destination type, where the destination
349 /// type is an LLVM scalar type.
350 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
351 QualType DstTy);
352
Chris Lattner3707b252007-08-26 06:48:56 +0000353
Chris Lattner883f6a72007-08-11 00:04:45 +0000354 /// EmitAggExpr - Emit the computation of the specified expression of
355 /// aggregate type. The result is computed into DestPtr. Note that if
356 /// DestPtr is null, the value of the aggregate expression is not needed.
357 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000358
359 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000360 /// complex type, returning the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000361 ComplexPairTy EmitComplexExpr(const Expr *E);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000362
363 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
364 /// of complex type, storing into the specified Value*.
Chris Lattner190dbe22007-08-26 16:22:13 +0000365 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
366 bool DestIsVolatile);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367};
368} // end namespace CodeGen
369} // end namespace clang
370
371#endif