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