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