blob: 0cc5f0ddbe040eeff4813e85fd8be8298aa1ba64 [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;
44
45 class Expr;
46 class DeclRefExpr;
47 class StringLiteral;
48 class IntegerLiteral;
49 class FloatingLiteral;
50 class CharacterLiteral;
Chris Lattner4ca7e752007-08-03 17:51:03 +000051 class TypesCompatibleExpr;
52
Chris Lattner4b009652007-07-25 00:24:17 +000053 class CastExpr;
54 class CallExpr;
55 class UnaryOperator;
56 class BinaryOperator;
57 class CompoundAssignOperator;
58 class ArraySubscriptExpr;
Chris Lattnera0d03a72007-08-03 17:31:20 +000059 class OCUVectorElementExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000060 class ConditionalOperator;
Chris Lattner44fcf4f2007-08-04 00:20:15 +000061 class ChooseExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000062 class PreDefinedExpr;
63
64 class BlockVarDecl;
65 class EnumConstantDecl;
66 class ParmVarDecl;
67namespace CodeGen {
68 class CodeGenModule;
69
70
71/// RValue - This trivial value class is used to represent the result of an
72/// expression that is evaluated. It can be one of two things: either a simple
73/// LLVM SSA value, or the address of an aggregate value in memory. These two
74/// possibilities are discriminated by isAggregate/isScalar.
75class RValue {
76 llvm::Value *V;
77 // TODO: Encode this into the low bit of pointer for more efficient
78 // return-by-value.
79 bool IsAggregate;
80
81 // FIXME: Aggregate rvalues need to retain information about whether they are
82 // volatile or not.
83public:
84
85 bool isAggregate() const { return IsAggregate; }
86 bool isScalar() const { return !IsAggregate; }
87
88 /// getVal() - Return the Value* of this scalar value.
89 llvm::Value *getVal() const {
90 assert(!isAggregate() && "Not a scalar!");
91 return V;
92 }
93
94 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
95 llvm::Value *getAggregateAddr() const {
96 assert(isAggregate() && "Not an aggregate!");
97 return V;
98 }
99
100 static RValue get(llvm::Value *V) {
101 RValue ER;
102 ER.V = V;
103 ER.IsAggregate = false;
104 return ER;
105 }
106 static RValue getAggregate(llvm::Value *V) {
107 RValue ER;
108 ER.V = V;
109 ER.IsAggregate = true;
110 return ER;
111 }
112};
113
114
115/// LValue - This represents an lvalue references. Because C/C++ allow
116/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
117/// bitrange.
118class LValue {
119 // FIXME: Volatility. Restrict?
120 // alignment?
121
122 enum {
Chris Lattner65520192007-08-02 23:37:31 +0000123 Simple, // This is a normal l-value, use getAddress().
124 VectorElt, // This is a vector element l-value (V[i]), use getVector*
125 BitField, // This is a bitfield l-value, use getBitfield*.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000126 OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
Chris Lattner4b009652007-07-25 00:24:17 +0000127 } LVType;
128
129 llvm::Value *V;
130
131 union {
Chris Lattner65520192007-08-02 23:37:31 +0000132 llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
Chris Lattnera0d03a72007-08-03 17:31:20 +0000133 unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
Chris Lattner4b009652007-07-25 00:24:17 +0000134 };
135public:
136 bool isSimple() const { return LVType == Simple; }
137 bool isVectorElt() const { return LVType == VectorElt; }
138 bool isBitfield() const { return LVType == BitField; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000139 bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
Chris Lattner4b009652007-07-25 00:24:17 +0000140
141 // simple lvalue
142 llvm::Value *getAddress() const { assert(isSimple()); return V; }
143 // vector elt lvalue
144 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
145 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000146 // ocu vector elements.
147 llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
148 unsigned getOCUVectorElts() const {
149 assert(isOCUVectorElt());
150 return VectorElts;
Chris Lattner65520192007-08-02 23:37:31 +0000151 }
152
Chris Lattner4b009652007-07-25 00:24:17 +0000153
154 static LValue MakeAddr(llvm::Value *V) {
155 LValue R;
156 R.LVType = Simple;
157 R.V = V;
158 return R;
159 }
160
161 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
162 LValue R;
163 R.LVType = VectorElt;
164 R.V = Vec;
165 R.VectorIdx = Idx;
166 return R;
167 }
168
Chris Lattnera0d03a72007-08-03 17:31:20 +0000169 static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
Chris Lattner65520192007-08-02 23:37:31 +0000170 LValue R;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000171 R.LVType = OCUVectorElt;
Chris Lattner65520192007-08-02 23:37:31 +0000172 R.V = Vec;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000173 R.VectorElts = Elements;
Chris Lattner65520192007-08-02 23:37:31 +0000174 return R;
175 }
Chris Lattner4b009652007-07-25 00:24:17 +0000176};
177
178/// CodeGenFunction - This class organizes the per-function state that is used
179/// while generating LLVM code.
180class CodeGenFunction {
181 CodeGenModule &CGM; // Per-module state.
182 TargetInfo &Target;
183 llvm::LLVMBuilder Builder;
184
185 const FunctionDecl *CurFuncDecl;
186 llvm::Function *CurFn;
187
188 /// AllocaInsertPoint - This is an instruction in the entry block before which
189 /// we prefer to insert allocas.
190 llvm::Instruction *AllocaInsertPt;
191
192 const llvm::Type *LLVMIntTy;
193 unsigned LLVMPointerWidth;
194
195 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
196 /// decls.
197 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
198
199 /// LabelMap - This keeps track of the LLVM basic block for each C label.
200 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
201
202 // BreakContinueStack - This keeps track of where break and continue
203 // statements should jump to.
204 struct BreakContinue {
205 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
206 : BreakBlock(bb), ContinueBlock(cb) {}
207
208 llvm::BasicBlock *BreakBlock;
209 llvm::BasicBlock *ContinueBlock;
210 };
211 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
212
213public:
214 CodeGenFunction(CodeGenModule &cgm);
215
216 ASTContext &getContext() const;
217
218 void GenerateCode(const FunctionDecl *FD);
219
220 const llvm::Type *ConvertType(QualType T);
221
222 /// hasAggregateLLVMType - Return true if the specified AST type will map into
223 /// an aggregate LLVM type or is void.
224 static bool hasAggregateLLVMType(QualType T);
225
226 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
227 /// label maps to.
228 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
229
230
231 void EmitBlock(llvm::BasicBlock *BB);
232
233 //===--------------------------------------------------------------------===//
234 // Helpers
235 //===--------------------------------------------------------------------===//
236
237 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
238 /// block.
239 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
240 const char *Name = "tmp");
241
242 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
243 /// expression and compare the result against zero, returning an Int1Ty value.
244 llvm::Value *EvaluateExprAsBool(const Expr *E);
245
246
247 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
248 /// load the real and imaginary pieces, returning them as Real/Imag.
249 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
250
251 /// EmitStoreOfComplex - Store the specified real/imag parts into the
252 /// specified value pointer.
253 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
254 llvm::Value *ResPtr);
255
256 //===--------------------------------------------------------------------===//
257 // Conversions
258 //===--------------------------------------------------------------------===//
259
260 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
261 /// the type specified by DstTy, following the rules of C99 6.3.
262 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
263
264 /// ConvertScalarValueToBool - Convert the specified expression value to a
265 /// boolean (i1) truth value. This is equivalent to "Val == 0".
266 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
267
268 //===--------------------------------------------------------------------===//
269 // Declaration Emission
270 //===--------------------------------------------------------------------===//
271
272 void EmitDecl(const Decl &D);
273 void EmitEnumConstantDecl(const EnumConstantDecl &D);
274 void EmitBlockVarDecl(const BlockVarDecl &D);
275 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
276 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
277
278 //===--------------------------------------------------------------------===//
279 // Statement Emission
280 //===--------------------------------------------------------------------===//
281
282 void EmitStmt(const Stmt *S);
283 void EmitCompoundStmt(const CompoundStmt &S);
284 void EmitLabelStmt(const LabelStmt &S);
285 void EmitGotoStmt(const GotoStmt &S);
286 void EmitIfStmt(const IfStmt &S);
287 void EmitWhileStmt(const WhileStmt &S);
288 void EmitDoStmt(const DoStmt &S);
289 void EmitForStmt(const ForStmt &S);
290 void EmitReturnStmt(const ReturnStmt &S);
291 void EmitDeclStmt(const DeclStmt &S);
292 void EmitBreakStmt();
293 void EmitContinueStmt();
294
295 //===--------------------------------------------------------------------===//
296 // LValue Expression Emission
297 //===--------------------------------------------------------------------===//
298
299 /// EmitLValue - Emit code to compute a designator that specifies the location
300 /// of the expression.
301 ///
302 /// This can return one of two things: a simple address or a bitfield
303 /// reference. In either case, the LLVM Value* in the LValue structure is
304 /// guaranteed to be an LLVM pointer type.
305 ///
306 /// If this returns a bitfield reference, nothing about the pointee type of
307 /// the LLVM value is known: For example, it may not be a pointer to an
308 /// integer.
309 ///
310 /// If this returns a normal address, and if the lvalue's C type is fixed
311 /// size, this method guarantees that the returned pointer type will point to
312 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
313 /// variable length type, this is not possible.
314 ///
315 LValue EmitLValue(const Expr *E);
316
317 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
318 /// this method emits the address of the lvalue, then loads the result as an
319 /// rvalue, returning the rvalue.
320 RValue EmitLoadOfLValue(const Expr *E);
321 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000322 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Chris Lattner4b009652007-07-25 00:24:17 +0000323
Chris Lattner944f7962007-08-03 16:18:34 +0000324
Chris Lattner4b009652007-07-25 00:24:17 +0000325 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
326 /// lvalue, where both are guaranteed to the have the same type, and that type
327 /// is 'Ty'.
328 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000329 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 LValue EmitDeclRefLValue(const DeclRefExpr *E);
332 LValue EmitStringLiteralLValue(const StringLiteral *E);
333 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
334 LValue EmitUnaryOpLValue(const UnaryOperator *E);
335 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000336 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000337
338 //===--------------------------------------------------------------------===//
339 // Expression Emission
340 //===--------------------------------------------------------------------===//
341
342 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
343 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
344 RValue &LHS, RValue &RHS);
345 void EmitShiftOperands(const BinaryOperator *E, RValue &LHS, RValue &RHS);
346
347 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
348 LValue &LHSLV, RValue &LHS, RValue &RHS);
349 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
350 LValue LHSLV, RValue ResV);
351
352
353 RValue EmitExpr(const Expr *E);
354 RValue EmitIntegerLiteral(const IntegerLiteral *E);
355 RValue EmitFloatingLiteral(const FloatingLiteral *E);
356 RValue EmitCharacterLiteral(const CharacterLiteral *E);
Chris Lattner4ca7e752007-08-03 17:51:03 +0000357 RValue EmitTypesCompatibleExpr(const TypesCompatibleExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000358
359 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
360 RValue EmitCallExpr(const CallExpr *E);
361 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
362
363 // Unary Operators.
364 RValue EmitUnaryOperator(const UnaryOperator *E);
365 RValue EmitUnaryIncDec (const UnaryOperator *E);
366 RValue EmitUnaryAddrOf (const UnaryOperator *E);
367 RValue EmitUnaryPlus (const UnaryOperator *E);
368 RValue EmitUnaryMinus (const UnaryOperator *E);
369 RValue EmitUnaryNot (const UnaryOperator *E);
370 RValue EmitUnaryLNot (const UnaryOperator *E);
371 RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
372 // FIXME: real/imag
373
374 // Binary Operators.
375 RValue EmitBinaryOperator(const BinaryOperator *E);
376 RValue EmitBinaryMul(const BinaryOperator *E);
377 RValue EmitBinaryDiv(const BinaryOperator *E);
378 RValue EmitBinaryRem(const BinaryOperator *E);
379 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
380 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
381 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
382 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
383 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
384 RValue RHS, QualType RHSTy, QualType EltTy);
385 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
386 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
387 RValue RHS, QualType RHSTy, QualType EltTy);
388 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
389 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
390 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
391 unsigned SICmpOpc, unsigned FCmpOpc);
392 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
393 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
394 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
395 RValue EmitBinaryLAnd(const BinaryOperator *E);
396 RValue EmitBinaryLOr(const BinaryOperator *E);
397
398 RValue EmitBinaryAssign(const BinaryOperator *E);
399 RValue EmitBinaryComma(const BinaryOperator *E);
400
401 // Conditional Operator.
402 RValue EmitConditionalOperator(const ConditionalOperator *E);
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000403 RValue EmitChooseExpr(const ChooseExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000404};
405} // end namespace CodeGen
406} // end namespace clang
407
408#endif