blob: fe570feb939b876b9519c1d9969f721a10dc2120 [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"
18#include "llvm/Support/LLVMBuilder.h"
19#include <vector>
20
21namespace llvm {
22 class Module;
23}
24
25namespace clang {
26 class ASTContext;
27 class Decl;
28 class FunctionDecl;
29 class TargetInfo;
30 class QualType;
31 class FunctionTypeProto;
32
33 class Stmt;
34 class CompoundStmt;
35 class LabelStmt;
36 class GotoStmt;
37 class IfStmt;
38 class WhileStmt;
39 class DoStmt;
40 class ForStmt;
41 class ReturnStmt;
42 class DeclStmt;
43
44 class Expr;
45 class DeclRefExpr;
46 class StringLiteral;
47 class IntegerLiteral;
48 class FloatingLiteral;
49 class CastExpr;
50 class CallExpr;
51 class UnaryOperator;
52 class BinaryOperator;
53 class CompoundAssignOperator;
54 class ArraySubscriptExpr;
55
56 class BlockVarDecl;
57 class EnumConstantDecl;
58 class ParmVarDecl;
59namespace CodeGen {
60 class CodeGenModule;
61
62
63/// RValue - This trivial value class is used to represent the result of an
64/// expression that is evaluated. It can be one of two things: either a simple
65/// LLVM SSA value, or the address of an aggregate value in memory. These two
66/// possibilities are discriminated by isAggregate/isScalar.
67class RValue {
68 llvm::Value *V;
69 // TODO: Encode this into the low bit of pointer for more efficient
70 // return-by-value.
71 bool IsAggregate;
72
73 // FIXME: Aggregate rvalues need to retain information about whether they are
74 // volatile or not.
75public:
76
77 bool isAggregate() const { return IsAggregate; }
78 bool isScalar() const { return !IsAggregate; }
79
80 /// getVal() - Return the Value* of this scalar value.
81 llvm::Value *getVal() const {
82 assert(!isAggregate() && "Not a scalar!");
83 return V;
84 }
85
86 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
87 llvm::Value *getAggregateAddr() const {
88 assert(isAggregate() && "Not an aggregate!");
89 return V;
90 }
91
92 static RValue get(llvm::Value *V) {
93 RValue ER;
94 ER.V = V;
95 ER.IsAggregate = false;
96 return ER;
97 }
98 static RValue getAggregate(llvm::Value *V) {
99 RValue ER;
100 ER.V = V;
101 ER.IsAggregate = true;
102 return ER;
103 }
104};
105
106
107/// LValue - This represents an lvalue references. Because C/C++ allow
108/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
109/// bitrange.
110class LValue {
111 // FIXME: Volatility. Restrict?
112 // alignment?
113
114 enum {
115 Simple, // This is a normal l-value, use getAddress().
116 VectorElt, // This is a vector element l-value (V[i]), use getVector*
117 BitField // This is a bitfield l-value, use getBitfield*.
118 } LVType;
119
120 llvm::Value *V;
121
122 union {
123 llvm::Value *VectorIdx;
124 };
125public:
126 bool isSimple() const { return LVType == Simple; }
127 bool isVectorElt() const { return LVType == VectorElt; }
128 bool isBitfield() const { return LVType == BitField; }
129
130 // simple lvalue
131 llvm::Value *getAddress() const { assert(isSimple()); return V; }
132 // vector elt lvalue
133 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
134 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
135
136 static LValue MakeAddr(llvm::Value *V) {
137 LValue R;
138 R.LVType = Simple;
139 R.V = V;
140 return R;
141 }
142
143 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
144 LValue R;
145 R.LVType = VectorElt;
146 R.V = Vec;
147 R.VectorIdx = Idx;
148 return R;
149 }
150
151};
152
153/// CodeGenFunction - This class organizes the per-function state that is used
154/// while generating LLVM code.
155class CodeGenFunction {
156 CodeGenModule &CGM; // Per-module state.
157 TargetInfo &Target;
158 llvm::LLVMBuilder Builder;
159
160 const FunctionDecl *CurFuncDecl;
161 llvm::Function *CurFn;
162
163 /// AllocaInsertPoint - This is an instruction in the entry block before which
164 /// we prefer to insert allocas.
165 llvm::Instruction *AllocaInsertPt;
166
167 const llvm::Type *LLVMIntTy;
168 unsigned LLVMPointerWidth;
169
170 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
171 /// decls.
172 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
173
174 /// LabelMap - This keeps track of the LLVM basic block for each C label.
175 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
176public:
177 CodeGenFunction(CodeGenModule &cgm);
178
179 ASTContext &getContext() const;
180
181 void GenerateCode(const FunctionDecl *FD);
182
183 const llvm::Type *ConvertType(QualType T);
184
185 /// hasAggregateLLVMType - Return true if the specified AST type will map into
186 /// an aggregate LLVM type or is void.
187 static bool hasAggregateLLVMType(QualType T);
188
189 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
190 /// label maps to.
191 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
192
193
194 void EmitBlock(llvm::BasicBlock *BB);
195
196 //===--------------------------------------------------------------------===//
197 // Helpers
198 //===--------------------------------------------------------------------===//
199
200 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
201 /// block.
202 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
203 const char *Name = "tmp");
204
205 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
206 /// expression and compare the result against zero, returning an Int1Ty value.
207 llvm::Value *EvaluateExprAsBool(const Expr *E);
208
209
210 /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
211 /// load the real and imaginary pieces, returning them as Real/Imag.
212 void EmitLoadOfComplex(RValue V, llvm::Value *&Real, llvm::Value *&Imag);
213
214 /// EmitStoreOfComplex - Store the specified real/imag parts into the
215 /// specified value pointer.
216 void EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
217 llvm::Value *ResPtr);
218
219 //===--------------------------------------------------------------------===//
220 // Conversions
221 //===--------------------------------------------------------------------===//
222
223 /// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
224 /// the type specified by DstTy, following the rules of C99 6.3.
225 RValue EmitConversion(RValue Val, QualType ValTy, QualType DstTy);
226
227 /// ConvertScalarValueToBool - Convert the specified expression value to a
228 /// boolean (i1) truth value. This is equivalent to "Val == 0".
229 llvm::Value *ConvertScalarValueToBool(RValue Val, QualType Ty);
230
231 //===--------------------------------------------------------------------===//
232 // Declaration Emission
233 //===--------------------------------------------------------------------===//
234
235 void EmitDecl(const Decl &D);
236 void EmitEnumConstantDecl(const EnumConstantDecl &D);
237 void EmitBlockVarDecl(const BlockVarDecl &D);
238 void EmitLocalBlockVarDecl(const BlockVarDecl &D);
239 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
240
241 //===--------------------------------------------------------------------===//
242 // Statement Emission
243 //===--------------------------------------------------------------------===//
244
245 void EmitStmt(const Stmt *S);
246 void EmitCompoundStmt(const CompoundStmt &S);
247 void EmitLabelStmt(const LabelStmt &S);
248 void EmitGotoStmt(const GotoStmt &S);
249 void EmitIfStmt(const IfStmt &S);
250 void EmitWhileStmt(const WhileStmt &S);
251 void EmitDoStmt(const DoStmt &S);
252 void EmitForStmt(const ForStmt &S);
253 void EmitReturnStmt(const ReturnStmt &S);
254 void EmitDeclStmt(const DeclStmt &S);
255
256 //===--------------------------------------------------------------------===//
257 // LValue Expression Emission
258 //===--------------------------------------------------------------------===//
259
260 /// EmitLValue - Emit code to compute a designator that specifies the location
261 /// of the expression.
262 ///
263 /// This can return one of two things: a simple address or a bitfield
264 /// reference. In either case, the LLVM Value* in the LValue structure is
265 /// guaranteed to be an LLVM pointer type.
266 ///
267 /// If this returns a bitfield reference, nothing about the pointee type of
268 /// the LLVM value is known: For example, it may not be a pointer to an
269 /// integer.
270 ///
271 /// If this returns a normal address, and if the lvalue's C type is fixed
272 /// size, this method guarantees that the returned pointer type will point to
273 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
274 /// variable length type, this is not possible.
275 ///
276 LValue EmitLValue(const Expr *E);
277
278 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
279 /// this method emits the address of the lvalue, then loads the result as an
280 /// rvalue, returning the rvalue.
281 RValue EmitLoadOfLValue(const Expr *E);
282 RValue EmitLoadOfLValue(LValue V, QualType LVType);
283
284 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
285 /// lvalue, where both are guaranteed to the have the same type, and that type
286 /// is 'Ty'.
287 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
288
289 LValue EmitDeclRefLValue(const DeclRefExpr *E);
290 LValue EmitStringLiteralLValue(const StringLiteral *E);
291 LValue EmitUnaryOpLValue(const UnaryOperator *E);
292 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
293
294 //===--------------------------------------------------------------------===//
295 // Expression Emission
296 //===--------------------------------------------------------------------===//
297
298 RValue EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
299 QualType EmitUsualArithmeticConversions(const BinaryOperator *E,
300 RValue &LHS, RValue &RHS);
301 void EmitShiftOperands(const BinaryOperator *E, RValue &LHS, RValue &RHS);
302
303 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
304 LValue &LHSLV, RValue &LHS, RValue &RHS);
305 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
306 LValue LHSLV, RValue ResV);
307
308
309 RValue EmitExpr(const Expr *E);
310 RValue EmitIntegerLiteral(const IntegerLiteral *E);
311 RValue EmitFloatingLiteral(const FloatingLiteral *E);
312
313 RValue EmitCastExpr(const CastExpr *E);
314 RValue EmitCallExpr(const CallExpr *E);
315 RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
316
317 // Unary Operators.
318 RValue EmitUnaryOperator(const UnaryOperator *E);
Chris Lattner57274792007-07-11 23:43:46 +0000319 RValue EmitUnaryIncDec (const UnaryOperator *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 RValue EmitUnaryAddrOf (const UnaryOperator *E);
321 RValue EmitUnaryPlus (const UnaryOperator *E);
322 RValue EmitUnaryMinus (const UnaryOperator *E);
323 RValue EmitUnaryNot (const UnaryOperator *E);
324 RValue EmitUnaryLNot (const UnaryOperator *E);
325 // FIXME: SIZEOF/ALIGNOF(expr).
326 // FIXME: real/imag
327
328 // Binary Operators.
329 RValue EmitBinaryOperator(const BinaryOperator *E);
330 RValue EmitBinaryMul(const BinaryOperator *E);
331 RValue EmitBinaryDiv(const BinaryOperator *E);
332 RValue EmitBinaryRem(const BinaryOperator *E);
333 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
334 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
335 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
336 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
337 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
338 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
339 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
340 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
341 unsigned SICmpOpc, unsigned FCmpOpc);
342 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
343 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
344 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
345 RValue EmitBinaryLAnd(const BinaryOperator *E);
346 RValue EmitBinaryLOr(const BinaryOperator *E);
347
348 RValue EmitBinaryAssign(const BinaryOperator *E);
349 RValue EmitBinaryComma(const BinaryOperator *E);
350};
351} // end namespace CodeGen
352} // end namespace clang
353
354#endif