blob: 37614e95f060217d38644a60f3051508831c233f [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.
144 unsigned getOCUVectorComp() const {
145 assert(isOCUVectorComp());
146 return VectorComp;
147 }
148
Chris Lattner4b009652007-07-25 00:24:17 +0000149
150 static LValue MakeAddr(llvm::Value *V) {
151 LValue R;
152 R.LVType = Simple;
153 R.V = V;
154 return R;
155 }
156
157 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
158 LValue R;
159 R.LVType = VectorElt;
160 R.V = Vec;
161 R.VectorIdx = Idx;
162 return R;
163 }
164
Chris Lattner65520192007-08-02 23:37:31 +0000165 static LValue MakeOCUVectorComp(llvm::Value *Vec, unsigned Components) {
166 LValue R;
167 R.LVType = VectorElt;
168 R.V = Vec;
169 R.VectorComp = Components;
170 return R;
171 }
Chris Lattner4b009652007-07-25 00:24:17 +0000172};
173
174/// CodeGenFunction - This class organizes the per-function state that is used
175/// while generating LLVM code.
176class CodeGenFunction {
177 CodeGenModule &CGM; // Per-module state.
178 TargetInfo &Target;
179 llvm::LLVMBuilder Builder;
180
181 const FunctionDecl *CurFuncDecl;
182 llvm::Function *CurFn;
183
184 /// AllocaInsertPoint - This is an instruction in the entry block before which
185 /// we prefer to insert allocas.
186 llvm::Instruction *AllocaInsertPt;
187
188 const llvm::Type *LLVMIntTy;
189 unsigned LLVMPointerWidth;
190
191 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
192 /// decls.
193 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
194
195 /// LabelMap - This keeps track of the LLVM basic block for each C label.
196 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
197
198 // BreakContinueStack - This keeps track of where break and continue
199 // statements should jump to.
200 struct BreakContinue {
201 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
202 : BreakBlock(bb), ContinueBlock(cb) {}
203
204 llvm::BasicBlock *BreakBlock;
205 llvm::BasicBlock *ContinueBlock;
206 };
207 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
208
209public:
210 CodeGenFunction(CodeGenModule &cgm);
211
212 ASTContext &getContext() const;
213
214 void GenerateCode(const FunctionDecl *FD);
215
216 const llvm::Type *ConvertType(QualType T);
217
218 /// hasAggregateLLVMType - Return true if the specified AST type will map into
219 /// an aggregate LLVM type or is void.
220 static bool hasAggregateLLVMType(QualType T);
221
222 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
223 /// label maps to.
224 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
225
226
227 void EmitBlock(llvm::BasicBlock *BB);
228
229 //===--------------------------------------------------------------------===//
230 // Helpers
231 //===--------------------------------------------------------------------===//
232
233 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
234 /// block.
235 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
236 const char *Name = "tmp");
237
238 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
239 /// expression and compare the result against zero, returning an Int1Ty value.
240 llvm::Value *EvaluateExprAsBool(const Expr *E);
241
242
243 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
244 /// load the real and imaginary pieces, returning them as Real/Imag.
245 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
246
247 /// EmitStoreOfComplex - Store the specified real/imag parts into the
248 /// specified value pointer.
249 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
250 llvm::Value *ResPtr);
251
252 //===--------------------------------------------------------------------===//
253 // Conversions
254 //===--------------------------------------------------------------------===//
255
256 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
257 /// the type specified by DstTy, following the rules of C99 6.3.
258 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
259
260 /// ConvertScalarValueToBool - Convert the specified expression value to a
261 /// boolean (i1) truth value. This is equivalent to "Val == 0".
262 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
263
264 //===--------------------------------------------------------------------===//
265 // Declaration Emission
266 //===--------------------------------------------------------------------===//
267
268 void EmitDecl(const Decl &D);
269 void EmitEnumConstantDecl(const EnumConstantDecl &D);
270 void EmitBlockVarDecl(const BlockVarDecl &D);
271 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
272 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
273
274 //===--------------------------------------------------------------------===//
275 // Statement Emission
276 //===--------------------------------------------------------------------===//
277
278 void EmitStmt(const Stmt *S);
279 void EmitCompoundStmt(const CompoundStmt &S);
280 void EmitLabelStmt(const LabelStmt &S);
281 void EmitGotoStmt(const GotoStmt &S);
282 void EmitIfStmt(const IfStmt &S);
283 void EmitWhileStmt(const WhileStmt &S);
284 void EmitDoStmt(const DoStmt &S);
285 void EmitForStmt(const ForStmt &S);
286 void EmitReturnStmt(const ReturnStmt &S);
287 void EmitDeclStmt(const DeclStmt &S);
288 void EmitBreakStmt();
289 void EmitContinueStmt();
290
291 //===--------------------------------------------------------------------===//
292 // LValue Expression Emission
293 //===--------------------------------------------------------------------===//
294
295 /// EmitLValue - Emit code to compute a designator that specifies the location
296 /// of the expression.
297 ///
298 /// This can return one of two things: a simple address or a bitfield
299 /// reference. In either case, the LLVM Value* in the LValue structure is
300 /// guaranteed to be an LLVM pointer type.
301 ///
302 /// If this returns a bitfield reference, nothing about the pointee type of
303 /// the LLVM value is known: For example, it may not be a pointer to an
304 /// integer.
305 ///
306 /// If this returns a normal address, and if the lvalue's C type is fixed
307 /// size, this method guarantees that the returned pointer type will point to
308 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
309 /// variable length type, this is not possible.
310 ///
311 LValue EmitLValue(const Expr *E);
312
313 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
314 /// this method emits the address of the lvalue, then loads the result as an
315 /// rvalue, returning the rvalue.
316 RValue EmitLoadOfLValue(const Expr *E);
317 RValue EmitLoadOfLValue(LValue V, QualType LVType);
318
319 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
320 /// lvalue, where both are guaranteed to the have the same type, and that type
321 /// is 'Ty'.
322 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
323
324 LValue EmitDeclRefLValue(const DeclRefExpr *E);
325 LValue EmitStringLiteralLValue(const StringLiteral *E);
326 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
327 LValue EmitUnaryOpLValue(const UnaryOperator *E);
328 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattner65520192007-08-02 23:37:31 +0000329 LValue EmitOCUVectorComponentExpr(const OCUVectorComponent *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 //===--------------------------------------------------------------------===//
332 // Expression Emission
333 //===--------------------------------------------------------------------===//
334
335 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
336 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
337 RValue &LHS, RValue &RHS);
338 void EmitShiftOperands(const BinaryOperator *E, RValue &LHS, RValue &RHS);
339
340 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
341 LValue &LHSLV, RValue &LHS, RValue &RHS);
342 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
343 LValue LHSLV, RValue ResV);
344
345
346 RValue EmitExpr(const Expr *E);
347 RValue EmitIntegerLiteral(const IntegerLiteral *E);
348 RValue EmitFloatingLiteral(const FloatingLiteral *E);
349 RValue EmitCharacterLiteral(const CharacterLiteral *E);
350
351 RValue EmitCastExpr(const Expr *Op, QualType DestTy);
352 RValue EmitCallExpr(const CallExpr *E);
353 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
354
355 // Unary Operators.
356 RValue EmitUnaryOperator(const UnaryOperator *E);
357 RValue EmitUnaryIncDec (const UnaryOperator *E);
358 RValue EmitUnaryAddrOf (const UnaryOperator *E);
359 RValue EmitUnaryPlus (const UnaryOperator *E);
360 RValue EmitUnaryMinus (const UnaryOperator *E);
361 RValue EmitUnaryNot (const UnaryOperator *E);
362 RValue EmitUnaryLNot (const UnaryOperator *E);
363 RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
364 // FIXME: real/imag
365
366 // Binary Operators.
367 RValue EmitBinaryOperator(const BinaryOperator *E);
368 RValue EmitBinaryMul(const BinaryOperator *E);
369 RValue EmitBinaryDiv(const BinaryOperator *E);
370 RValue EmitBinaryRem(const BinaryOperator *E);
371 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
372 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
373 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
374 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
375 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
376 RValue RHS, QualType RHSTy, QualType EltTy);
377 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
378 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
379 RValue RHS, QualType RHSTy, QualType EltTy);
380 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
381 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
382 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
383 unsigned SICmpOpc, unsigned FCmpOpc);
384 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
385 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
386 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
387 RValue EmitBinaryLAnd(const BinaryOperator *E);
388 RValue EmitBinaryLOr(const BinaryOperator *E);
389
390 RValue EmitBinaryAssign(const BinaryOperator *E);
391 RValue EmitBinaryComma(const BinaryOperator *E);
392
393 // Conditional Operator.
394 RValue EmitConditionalOperator(const ConditionalOperator *E);
395};
396} // end namespace CodeGen
397} // end namespace clang
398
399#endif