blob: 5ece5cd41b8842b19a7e4e1856a16316de4ccab8 [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
251 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
252 /// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattneree755f92007-08-21 04:59:27 +0000253 void EmitLoadOfComplex(llvm::Value *SrcPtr, llvm::Value *&Real,
254 llvm::Value *&Imag);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255
256 /// EmitStoreOfComplex - Store the specified real/imag parts into the
257 /// specified value pointer.
258 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
259 llvm::Value *ResPtr);
260
261 //===--------------------------------------------------------------------===//
262 // Conversions
263 //===--------------------------------------------------------------------===//
264
265 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
266 /// the type specified by DstTy, following the rules of C99 6.3.
267 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
268
269 /// ConvertScalarValueToBool - Convert the specified expression value to a
270 /// boolean (i1) truth value. This is equivalent to "Val == 0".
271 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
272
273 //===--------------------------------------------------------------------===//
274 // Declaration Emission
275 //===--------------------------------------------------------------------===//
276
277 void EmitDecl(const Decl &D);
278 void EmitEnumConstantDecl(const EnumConstantDecl &D);
279 void EmitBlockVarDecl(const BlockVarDecl &D);
280 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
281 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
282
283 //===--------------------------------------------------------------------===//
284 // Statement Emission
285 //===--------------------------------------------------------------------===//
286
287 void EmitStmt(const Stmt *S);
288 void EmitCompoundStmt(const CompoundStmt &S);
289 void EmitLabelStmt(const LabelStmt &S);
290 void EmitGotoStmt(const GotoStmt &S);
291 void EmitIfStmt(const IfStmt &S);
292 void EmitWhileStmt(const WhileStmt &S);
293 void EmitDoStmt(const DoStmt &S);
294 void EmitForStmt(const ForStmt &S);
295 void EmitReturnStmt(const ReturnStmt &S);
296 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000297 void EmitBreakStmt();
298 void EmitContinueStmt();
299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 //===--------------------------------------------------------------------===//
301 // LValue Expression Emission
302 //===--------------------------------------------------------------------===//
303
304 /// EmitLValue - Emit code to compute a designator that specifies the location
305 /// of the expression.
306 ///
307 /// This can return one of two things: a simple address or a bitfield
308 /// reference. In either case, the LLVM Value* in the LValue structure is
309 /// guaranteed to be an LLVM pointer type.
310 ///
311 /// If this returns a bitfield reference, nothing about the pointee type of
312 /// the LLVM value is known: For example, it may not be a pointer to an
313 /// integer.
314 ///
315 /// If this returns a normal address, and if the lvalue's C type is fixed
316 /// size, this method guarantees that the returned pointer type will point to
317 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
318 /// variable length type, this is not possible.
319 ///
320 LValue EmitLValue(const Expr *E);
321
322 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
323 /// this method emits the address of the lvalue, then loads the result as an
324 /// rvalue, returning the rvalue.
325 RValue EmitLoadOfLValue(const Expr *E);
326 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattner6481a572007-08-03 17:31:20 +0000327 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
Chris Lattner34cdc862007-08-03 16:18:34 +0000329
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
331 /// lvalue, where both are guaranteed to the have the same type, and that type
332 /// is 'Ty'.
333 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000334 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000335
336 LValue EmitDeclRefLValue(const DeclRefExpr *E);
337 LValue EmitStringLiteralLValue(const StringLiteral *E);
Anders Carlsson22742662007-07-21 05:21:51 +0000338 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 LValue EmitUnaryOpLValue(const UnaryOperator *E);
340 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattner6481a572007-08-03 17:31:20 +0000341 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342
343 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000344 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 //===--------------------------------------------------------------------===//
346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
348 LValue &LHSLV, RValue &LHS, RValue &RHS);
349 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
350 LValue LHSLV, RValue ResV);
351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 RValue EmitExpr(const Expr *E);
353 RValue EmitIntegerLiteral(const IntegerLiteral *E);
354 RValue EmitFloatingLiteral(const FloatingLiteral *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000355 RValue EmitCharacterLiteral(const CharacterLiteral *E);
Chris Lattner30bf3ae2007-08-03 17:51:03 +0000356 RValue EmitTypesCompatibleExpr(const TypesCompatibleExpr *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000357
Chris Lattner7016a702007-08-20 22:37:10 +0000358 RValue EmitImplicitCastExpr(const ImplicitCastExpr *Op);
Chris Lattnerd07eb3b2007-07-13 20:25:53 +0000359 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 RValue EmitCallExpr(const CallExpr *E);
Anders Carlsson022012e2007-08-20 18:05:56 +0000361 RValue EmitBuiltinExpr(unsigned builtinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
363
364 // Unary Operators.
365 RValue EmitUnaryOperator(const UnaryOperator *E);
Chris Lattner57274792007-07-11 23:43:46 +0000366 RValue EmitUnaryIncDec (const UnaryOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 RValue EmitUnaryAddrOf (const UnaryOperator *E);
368 RValue EmitUnaryPlus (const UnaryOperator *E);
369 RValue EmitUnaryMinus (const UnaryOperator *E);
370 RValue EmitUnaryNot (const UnaryOperator *E);
371 RValue EmitUnaryLNot (const UnaryOperator *E);
Chris Lattner5e3fbe52007-07-18 18:12:07 +0000372 RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // FIXME: real/imag
374
375 // Binary Operators.
376 RValue EmitBinaryOperator(const BinaryOperator *E);
377 RValue EmitBinaryMul(const BinaryOperator *E);
378 RValue EmitBinaryDiv(const BinaryOperator *E);
379 RValue EmitBinaryRem(const BinaryOperator *E);
380 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
381 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
382 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
383 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000384 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
385 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
Chris Lattner8b9023b2007-07-13 03:05:23 +0000387 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
388 RValue RHS, QualType RHSTy, QualType EltTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
390 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
391 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
392 unsigned SICmpOpc, unsigned FCmpOpc);
393 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
394 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
395 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
396 RValue EmitBinaryLAnd(const BinaryOperator *E);
397 RValue EmitBinaryLOr(const BinaryOperator *E);
398
399 RValue EmitBinaryAssign(const BinaryOperator *E);
400 RValue EmitBinaryComma(const BinaryOperator *E);
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000401
402 // Conditional Operator.
403 RValue EmitConditionalOperator(const ConditionalOperator *E);
Chris Lattner94f05e32007-08-04 00:20:15 +0000404 RValue EmitChooseExpr(const ChooseExpr *E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000405
406 //===--------------------------------------------------------------------===//
407 // Aggregate Expression Emission
408 //===--------------------------------------------------------------------===//
409
410 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
411 QualType EltTy);
412
413 /// EmitAggExpr - Emit the computation of the specified expression of
414 /// aggregate type. The result is computed into DestPtr. Note that if
415 /// DestPtr is null, the value of the aggregate expression is not needed.
416 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000417
418 /// EmitComplexExpr - Emit the computation of the specified expression of
419 /// complex type, ignoring the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000420 ComplexPairTy EmitComplexExpr(const Expr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421};
422} // end namespace CodeGen
423} // end namespace clang
424
425#endif