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