blob: 0975bd975cd13cce98ff7105ffa0c8534a7f4643 [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;
Anders Carlssona66cad42007-08-21 17:43:55 +000064 class ObjCStringLiteral;
Chris Lattner4b009652007-07-25 00:24:17 +000065
66 class BlockVarDecl;
67 class EnumConstantDecl;
68 class ParmVarDecl;
69namespace CodeGen {
70 class CodeGenModule;
71
72
73/// RValue - This trivial value class is used to represent the result of an
74/// expression that is evaluated. It can be one of two things: either a simple
75/// LLVM SSA value, or the address of an aggregate value in memory. These two
76/// possibilities are discriminated by isAggregate/isScalar.
77class RValue {
78 llvm::Value *V;
79 // TODO: Encode this into the low bit of pointer for more efficient
80 // return-by-value.
81 bool IsAggregate;
82
83 // FIXME: Aggregate rvalues need to retain information about whether they are
84 // volatile or not.
85public:
86
87 bool isAggregate() const { return IsAggregate; }
88 bool isScalar() const { return !IsAggregate; }
89
90 /// getVal() - Return the Value* of this scalar value.
91 llvm::Value *getVal() const {
92 assert(!isAggregate() && "Not a scalar!");
93 return V;
94 }
95
96 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
97 llvm::Value *getAggregateAddr() const {
98 assert(isAggregate() && "Not an aggregate!");
99 return V;
100 }
101
102 static RValue get(llvm::Value *V) {
103 RValue ER;
104 ER.V = V;
105 ER.IsAggregate = false;
106 return ER;
107 }
108 static RValue getAggregate(llvm::Value *V) {
109 RValue ER;
110 ER.V = V;
111 ER.IsAggregate = true;
112 return ER;
113 }
114};
115
116
117/// LValue - This represents an lvalue references. Because C/C++ allow
118/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
119/// bitrange.
120class LValue {
121 // FIXME: Volatility. Restrict?
122 // alignment?
123
124 enum {
Chris Lattner65520192007-08-02 23:37:31 +0000125 Simple, // This is a normal l-value, use getAddress().
126 VectorElt, // This is a vector element l-value (V[i]), use getVector*
127 BitField, // This is a bitfield l-value, use getBitfield*.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000128 OCUVectorElt // This is an ocu vector subset, use getOCUVectorComp
Chris Lattner4b009652007-07-25 00:24:17 +0000129 } LVType;
130
131 llvm::Value *V;
132
133 union {
Chris Lattner65520192007-08-02 23:37:31 +0000134 llvm::Value *VectorIdx; // Index into a vector subscript: V[i]
Chris Lattnera0d03a72007-08-03 17:31:20 +0000135 unsigned VectorElts; // Encoded OCUVector element subset: V.xyx
Chris Lattner4b009652007-07-25 00:24:17 +0000136 };
137public:
138 bool isSimple() const { return LVType == Simple; }
139 bool isVectorElt() const { return LVType == VectorElt; }
140 bool isBitfield() const { return LVType == BitField; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000141 bool isOCUVectorElt() const { return LVType == OCUVectorElt; }
Chris Lattner4b009652007-07-25 00:24:17 +0000142
143 // simple lvalue
144 llvm::Value *getAddress() const { assert(isSimple()); return V; }
145 // vector elt lvalue
146 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
147 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Chris Lattnera0d03a72007-08-03 17:31:20 +0000148 // ocu vector elements.
149 llvm::Value *getOCUVectorAddr() const { assert(isOCUVectorElt()); return V; }
150 unsigned getOCUVectorElts() const {
151 assert(isOCUVectorElt());
152 return VectorElts;
Chris Lattner65520192007-08-02 23:37:31 +0000153 }
154
Chris Lattner4b009652007-07-25 00:24:17 +0000155
156 static LValue MakeAddr(llvm::Value *V) {
157 LValue R;
158 R.LVType = Simple;
159 R.V = V;
160 return R;
161 }
162
163 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) {
164 LValue R;
165 R.LVType = VectorElt;
166 R.V = Vec;
167 R.VectorIdx = Idx;
168 return R;
169 }
170
Chris Lattnera0d03a72007-08-03 17:31:20 +0000171 static LValue MakeOCUVectorElt(llvm::Value *Vec, unsigned Elements) {
Chris Lattner65520192007-08-02 23:37:31 +0000172 LValue R;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000173 R.LVType = OCUVectorElt;
Chris Lattner65520192007-08-02 23:37:31 +0000174 R.V = Vec;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000175 R.VectorElts = Elements;
Chris Lattner65520192007-08-02 23:37:31 +0000176 return R;
177 }
Chris Lattner4b009652007-07-25 00:24:17 +0000178};
179
180/// CodeGenFunction - This class organizes the per-function state that is used
181/// while generating LLVM code.
182class CodeGenFunction {
183 CodeGenModule &CGM; // Per-module state.
184 TargetInfo &Target;
Chris Lattnerb50e3902007-08-21 04:25:47 +0000185public:
Chris Lattner5280c5f2007-08-21 16:57:55 +0000186 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000187 llvm::LLVMBuilder Builder;
188
189 const FunctionDecl *CurFuncDecl;
190 llvm::Function *CurFn;
191
192 /// AllocaInsertPoint - This is an instruction in the entry block before which
193 /// we prefer to insert allocas.
194 llvm::Instruction *AllocaInsertPt;
195
196 const llvm::Type *LLVMIntTy;
197 unsigned LLVMPointerWidth;
198
Chris Lattner9fba49a2007-08-24 05:35:26 +0000199private:
Chris Lattner4b009652007-07-25 00:24:17 +0000200 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
201 /// decls.
202 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
203
204 /// LabelMap - This keeps track of the LLVM basic block for each C label.
205 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
206
207 // BreakContinueStack - This keeps track of where break and continue
208 // statements should jump to.
209 struct BreakContinue {
210 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
211 : BreakBlock(bb), ContinueBlock(cb) {}
212
213 llvm::BasicBlock *BreakBlock;
214 llvm::BasicBlock *ContinueBlock;
215 };
216 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
217
218public:
219 CodeGenFunction(CodeGenModule &cgm);
220
221 ASTContext &getContext() const;
222
223 void GenerateCode(const FunctionDecl *FD);
224
225 const llvm::Type *ConvertType(QualType T);
226
227 /// hasAggregateLLVMType - Return true if the specified AST type will map into
228 /// an aggregate LLVM type or is void.
229 static bool hasAggregateLLVMType(QualType T);
230
231 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
232 /// label maps to.
233 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
234
235
236 void EmitBlock(llvm::BasicBlock *BB);
237
238 //===--------------------------------------------------------------------===//
239 // Helpers
240 //===--------------------------------------------------------------------===//
241
242 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
243 /// block.
244 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
245 const char *Name = "tmp");
246
247 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
248 /// expression and compare the result against zero, returning an Int1Ty value.
249 llvm::Value *EvaluateExprAsBool(const Expr *E);
250
251
Chris Lattner4b009652007-07-25 00:24:17 +0000252 //===--------------------------------------------------------------------===//
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);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000318 RValue EmitLoadOfOCUElementLValue(LValue V, QualType LVType);
Chris Lattner4b009652007-07-25 00:24:17 +0000319
Chris Lattner944f7962007-08-03 16:18:34 +0000320
Chris Lattner4b009652007-07-25 00:24:17 +0000321 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
322 /// lvalue, where both are guaranteed to the have the same type, and that type
323 /// is 'Ty'.
324 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner5bfdd232007-08-03 16:28:33 +0000325 void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000326
327 LValue EmitDeclRefLValue(const DeclRefExpr *E);
328 LValue EmitStringLiteralLValue(const StringLiteral *E);
329 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
330 LValue EmitUnaryOpLValue(const UnaryOperator *E);
331 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Chris Lattnera0d03a72007-08-03 17:31:20 +0000332 LValue EmitOCUVectorElementExpr(const OCUVectorElementExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000333
334 //===--------------------------------------------------------------------===//
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000335 // Scalar Expression Emission
Chris Lattner4b009652007-07-25 00:24:17 +0000336 //===--------------------------------------------------------------------===//
337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 void EmitCompoundAssignmentOperands(const CompoundAssignOperator *CAO,
339 LValue &LHSLV, RValue &LHS, RValue &RHS);
340 RValue EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
341 LValue LHSLV, RValue ResV);
342
Chris Lattner348c8a22007-08-23 23:43:33 +0000343 /// EmitAnyExpr - Emit an expression of any type: scalar, complex, aggregate,
344 /// returning an rvalue corresponding to it. If NeedResult is false, the
345 /// result of the expression doesn't need to be generated into memory.
346 RValue EmitAnyExpr(const Expr *E, bool NeedResult = true);
347
Chris Lattner4b009652007-07-25 00:24:17 +0000348 RValue EmitCallExpr(const CallExpr *E);
Anders Carlsson49865302007-08-20 18:05:56 +0000349 RValue EmitBuiltinExpr(unsigned builtinID, const CallExpr *E);
Chris Lattner4b009652007-07-25 00:24:17 +0000350
Chris Lattner9fba49a2007-08-24 05:35:26 +0000351#if 0
352 RValue EmitExpr(const Expr *E);
353
Chris Lattner4b009652007-07-25 00:24:17 +0000354 // Binary Operators.
355 RValue EmitBinaryOperator(const BinaryOperator *E);
356 RValue EmitBinaryMul(const BinaryOperator *E);
357 RValue EmitBinaryDiv(const BinaryOperator *E);
358 RValue EmitBinaryRem(const BinaryOperator *E);
359 RValue EmitMul(RValue LHS, RValue RHS, QualType EltTy);
360 RValue EmitDiv(RValue LHS, RValue RHS, QualType EltTy);
361 RValue EmitRem(RValue LHS, RValue RHS, QualType EltTy);
362 RValue EmitAdd(RValue LHS, RValue RHS, QualType EltTy);
363 RValue EmitPointerAdd(RValue LHS, QualType LHSTy,
364 RValue RHS, QualType RHSTy, QualType EltTy);
365 RValue EmitSub(RValue LHS, RValue RHS, QualType EltTy);
366 RValue EmitPointerSub(RValue LHS, QualType LHSTy,
367 RValue RHS, QualType RHSTy, QualType EltTy);
368 RValue EmitShl(RValue LHS, RValue RHS, QualType ResTy);
369 RValue EmitShr(RValue LHS, RValue RHS, QualType ResTy);
370 RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
371 unsigned SICmpOpc, unsigned FCmpOpc);
372 RValue EmitAnd(RValue LHS, RValue RHS, QualType EltTy);
373 RValue EmitOr (RValue LHS, RValue RHS, QualType EltTy);
374 RValue EmitXor(RValue LHS, RValue RHS, QualType EltTy);
375 RValue EmitBinaryLAnd(const BinaryOperator *E);
376 RValue EmitBinaryLOr(const BinaryOperator *E);
377
378 RValue EmitBinaryAssign(const BinaryOperator *E);
379 RValue EmitBinaryComma(const BinaryOperator *E);
380
381 // Conditional Operator.
382 RValue EmitConditionalOperator(const ConditionalOperator *E);
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000383 RValue EmitChooseExpr(const ChooseExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000384#endif
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000385
Chris Lattner9fba49a2007-08-24 05:35:26 +0000386 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Anders Carlssona66cad42007-08-21 17:43:55 +0000387
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000388 //===--------------------------------------------------------------------===//
389 // Aggregate Expression Emission
390 //===--------------------------------------------------------------------===//
391
392 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
393 QualType EltTy);
394
Chris Lattner9fba49a2007-08-24 05:35:26 +0000395 /// EmitScalarExpr - Emit the computation of the specified expression of
396 /// LLVM scalar type, returning the result.
397 llvm::Value *EmitScalarExpr(const Expr *E);
398
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000399 /// EmitAggExpr - Emit the computation of the specified expression of
400 /// aggregate type. The result is computed into DestPtr. Note that if
401 /// DestPtr is null, the value of the aggregate expression is not needed.
402 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattner8d0cc2f2007-08-21 05:54:00 +0000403
404 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner348c8a22007-08-23 23:43:33 +0000405 /// complex type, returning the result.
Chris Lattner5280c5f2007-08-21 16:57:55 +0000406 ComplexPairTy EmitComplexExpr(const Expr *E);
Chris Lattner348c8a22007-08-23 23:43:33 +0000407
408 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
409 /// of complex type, storing into the specified Value*.
410 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr);
Chris Lattner4b009652007-07-25 00:24:17 +0000411};
412} // end namespace CodeGen
413} // end namespace clang
414
415#endif