blob: a0a3c610e8d014de3dbe527d17298630fc664719 [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;
Chris Lattner9c033562007-08-21 04:25:47 +0000184public:
Chris Lattner58dee102007-08-21 16:57:55 +0000185 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 llvm::LLVMBuilder Builder;
Chris Lattner9c033562007-08-21 04:25:47 +0000187private:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188
189 const FunctionDecl *CurFuncDecl;
190 llvm::Function *CurFn;
191
192 /// AllocaInsertPoint - This is an instruction in the entry block before which
193 /// we prefer to insert allocas.
194 llvm::Instruction *AllocaInsertPt;
195
196 const llvm::Type *LLVMIntTy;
197 unsigned LLVMPointerWidth;
198
199 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
200 /// decls.
201 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
202
203 /// LabelMap - This keeps track of the LLVM basic block for each C label.
204 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000205
206 // BreakContinueStack - This keeps track of where break and continue
207 // statements should jump to.
208 struct BreakContinue {
209 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
210 : BreakBlock(bb), ContinueBlock(cb) {}
211
212 llvm::BasicBlock *BreakBlock;
213 llvm::BasicBlock *ContinueBlock;
214 };
215 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217public:
218 CodeGenFunction(CodeGenModule &cgm);
219
220 ASTContext &getContext() const;
221
222 void GenerateCode(const FunctionDecl *FD);
223
224 const llvm::Type *ConvertType(QualType T);
225
226 /// hasAggregateLLVMType - Return true if the specified AST type will map into
227 /// an aggregate LLVM type or is void.
228 static bool hasAggregateLLVMType(QualType T);
229
230 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
231 /// label maps to.
232 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
233
234
235 void EmitBlock(llvm::BasicBlock *BB);
236
237 //===--------------------------------------------------------------------===//
238 // Helpers
239 //===--------------------------------------------------------------------===//
240
241 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
242 /// block.
243 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
244 const char *Name = "tmp");
245
246 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
247 /// expression and compare the result against zero, returning an Int1Ty value.
248 llvm::Value *EvaluateExprAsBool(const Expr *E);
249
250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 //===--------------------------------------------------------------------===//
252 // Conversions
253 //===--------------------------------------------------------------------===//
254
255 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
256 /// the type specified by DstTy, following the rules of C99 6.3.
257 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
258
259 /// ConvertScalarValueToBool - Convert the specified expression value to a
260 /// boolean (i1) truth value. This is equivalent to "Val == 0".
261 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
262
263 //===--------------------------------------------------------------------===//
264 // Declaration Emission
265 //===--------------------------------------------------------------------===//
266
267 void EmitDecl(const Decl &D);
268 void EmitEnumConstantDecl(const EnumConstantDecl &D);
269 void EmitBlockVarDecl(const BlockVarDecl &D);
270 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
271 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
272
273 //===--------------------------------------------------------------------===//
274 // Statement Emission
275 //===--------------------------------------------------------------------===//
276
277 void EmitStmt(const Stmt *S);
278 void EmitCompoundStmt(const CompoundStmt &S);
279 void EmitLabelStmt(const LabelStmt &S);
280 void EmitGotoStmt(const GotoStmt &S);
281 void EmitIfStmt(const IfStmt &S);
282 void EmitWhileStmt(const WhileStmt &S);
283 void EmitDoStmt(const DoStmt &S);
284 void EmitForStmt(const ForStmt &S);
285 void EmitReturnStmt(const ReturnStmt &S);
286 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000287 void EmitBreakStmt();
288 void EmitContinueStmt();
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 //===--------------------------------------------------------------------===//
291 // LValue Expression Emission
292 //===--------------------------------------------------------------------===//
293
294 /// EmitLValue - Emit code to compute a designator that specifies the location
295 /// of the expression.
296 ///
297 /// This can return one of two things: a simple address or a bitfield
298 /// reference. In either case, the LLVM Value* in the LValue structure is
299 /// guaranteed to be an LLVM pointer type.
300 ///
301 /// If this returns a bitfield reference, nothing about the pointee type of
302 /// the LLVM value is known: For example, it may not be a pointer to an
303 /// integer.
304 ///
305 /// If this returns a normal address, and if the lvalue's C type is fixed
306 /// size, this method guarantees that the returned pointer type will point to
307 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
308 /// variable length type, this is not possible.
309 ///
310 LValue EmitLValue(const Expr *E);
311
312 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
313 /// this method emits the address of the lvalue, then loads the result as an
314 /// rvalue, returning the rvalue.
315 RValue EmitLoadOfLValue(const Expr *E);
316 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattner6481a572007-08-03 17:31:20 +0000317 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318
Chris Lattner34cdc862007-08-03 16:18:34 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
321 /// lvalue, where both are guaranteed to the have the same type, and that type
322 /// is 'Ty'.
323 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000324 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000325
326 LValue EmitDeclRefLValue(const DeclRefExpr *E);
327 LValue EmitStringLiteralLValue(const StringLiteral *E);
Anders Carlsson22742662007-07-21 05:21:51 +0000328 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 LValue EmitUnaryOpLValue(const UnaryOperator *E);
330 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattner6481a572007-08-03 17:31:20 +0000331 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332
333 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000334 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 //===--------------------------------------------------------------------===//
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
338 LValue &LHSLV, RValue &LHS, RValue &RHS);
339 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
340 LValue LHSLV, RValue ResV);
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 RValue EmitExpr(const Expr *E);
343 RValue EmitIntegerLiteral(const IntegerLiteral *E);
344 RValue EmitFloatingLiteral(const FloatingLiteral *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000345 RValue EmitCharacterLiteral(const CharacterLiteral *E);
Chris Lattner30bf3ae2007-08-03 17:51:03 +0000346 RValue EmitTypesCompatibleExpr(const TypesCompatibleExpr *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000347
Chris Lattner7016a702007-08-20 22:37:10 +0000348 RValue EmitImplicitCastExpr(const ImplicitCastExpr *Op);
Chris Lattnerd07eb3b2007-07-13 20:25:53 +0000349 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 RValue EmitCallExpr(const CallExpr *E);
Anders Carlsson022012e2007-08-20 18:05:56 +0000351 RValue EmitBuiltinExpr(unsigned builtinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
353
354 // Unary Operators.
355 RValue EmitUnaryOperator(const UnaryOperator *E);
Chris Lattner57274792007-07-11 23:43:46 +0000356 RValue EmitUnaryIncDec (const UnaryOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 RValue EmitUnaryAddrOf (const UnaryOperator *E);
358 RValue EmitUnaryPlus (const UnaryOperator *E);
359 RValue EmitUnaryMinus (const UnaryOperator *E);
360 RValue EmitUnaryNot (const UnaryOperator *E);
361 RValue EmitUnaryLNot (const UnaryOperator *E);
Chris Lattner5e3fbe52007-07-18 18:12:07 +0000362 RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // FIXME: real/imag
364
365 // Binary Operators.
366 RValue EmitBinaryOperator(const BinaryOperator *E);
367 RValue EmitBinaryMul(const BinaryOperator *E);
368 RValue EmitBinaryDiv(const BinaryOperator *E);
369 RValue EmitBinaryRem(const BinaryOperator *E);
370 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
371 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
372 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
373 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000374 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
375 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000377 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
378 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
380 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
381 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
382 unsigned SICmpOpc, unsigned FCmpOpc);
383 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
384 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
385 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
386 RValue EmitBinaryLAnd(const BinaryOperator *E);
387 RValue EmitBinaryLOr(const BinaryOperator *E);
388
389 RValue EmitBinaryAssign(const BinaryOperator *E);
390 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000391
392 // Conditional Operator.
393 RValue EmitConditionalOperator(const ConditionalOperator *E);
Chris Lattner94f05e32007-08-04 00:20:15 +0000394 RValue EmitChooseExpr(const ChooseExpr *E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000395
396 //===--------------------------------------------------------------------===//
397 // Aggregate Expression Emission
398 //===--------------------------------------------------------------------===//
399
400 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
401 QualType EltTy);
402
403 /// EmitAggExpr - Emit the computation of the specified expression of
404 /// aggregate type. The result is computed into DestPtr. Note that if
405 /// DestPtr is null, the value of the aggregate expression is not needed.
406 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000407
408 /// EmitComplexExpr - Emit the computation of the specified expression of
409 /// complex type, ignoring the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000410 ComplexPairTy EmitComplexExpr(const Expr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000411};
412} // end namespace CodeGen
413} // end namespace clang
414
415#endif