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