blob: 374cdf6cd7beb003a2cae8a4ce539786f86a6c79 [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-function state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
15#define CLANG_CODEGEN_CODEGENFUNCTION_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Chris Lattner391d77a2008-03-30 23:03:07 +000017#include "clang/AST/Type.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/DenseMap.h"
Chris Lattnerda138702007-07-16 21:28:45 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner50b36742008-04-13 07:32:11 +000020#include "llvm/Support/IRBuilder.h"
Ted Kremenek55499762008-06-17 02:43:46 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprObjC.h"
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include <vector>
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000025#include <map>
Reid Spencer5f016e22007-07-11 17:01:13 +000026
27namespace llvm {
28 class Module;
29}
30
31namespace clang {
32 class ASTContext;
33 class Decl;
34 class FunctionDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 class TargetInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 class FunctionTypeProto;
Devang Patelb84a06e2007-10-23 02:10:49 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039namespace CodeGen {
40 class CodeGenModule;
Devang Patelb84a06e2007-10-23 02:10:49 +000041 class CodeGenTypes;
Devang Patel88a981b2007-11-01 19:11:01 +000042 class CGRecordLayout;
Reid Spencer5f016e22007-07-11 17:01:13 +000043
44/// RValue - This trivial value class is used to represent the result of an
Chris Lattner9b655512007-08-31 22:49:20 +000045/// expression that is evaluated. It can be one of three things: either a
46/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
47/// address of an aggregate value in memory.
Reid Spencer5f016e22007-07-11 17:01:13 +000048class RValue {
Chris Lattner9b655512007-08-31 22:49:20 +000049 llvm::Value *V1, *V2;
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // TODO: Encode this into the low bit of pointer for more efficient
51 // return-by-value.
Chris Lattner9b655512007-08-31 22:49:20 +000052 enum { Scalar, Complex, Aggregate } Flavor;
Reid Spencer5f016e22007-07-11 17:01:13 +000053
54 // FIXME: Aggregate rvalues need to retain information about whether they are
55 // volatile or not.
56public:
57
Chris Lattner9b655512007-08-31 22:49:20 +000058 bool isScalar() const { return Flavor == Scalar; }
59 bool isComplex() const { return Flavor == Complex; }
60 bool isAggregate() const { return Flavor == Aggregate; }
Reid Spencer5f016e22007-07-11 17:01:13 +000061
Chris Lattner9b655512007-08-31 22:49:20 +000062 /// getScalar() - Return the Value* of this scalar value.
63 llvm::Value *getScalarVal() const {
64 assert(isScalar() && "Not a scalar!");
65 return V1;
Reid Spencer5f016e22007-07-11 17:01:13 +000066 }
67
Chris Lattner9b655512007-08-31 22:49:20 +000068 /// getComplexVal - Return the real/imag components of this complex value.
69 ///
70 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
71 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
72 }
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
75 llvm::Value *getAggregateAddr() const {
76 assert(isAggregate() && "Not an aggregate!");
Chris Lattner9b655512007-08-31 22:49:20 +000077 return V1;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 }
79
80 static RValue get(llvm::Value *V) {
81 RValue ER;
Chris Lattner9b655512007-08-31 22:49:20 +000082 ER.V1 = V;
83 ER.Flavor = Scalar;
84 return ER;
85 }
86 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
87 RValue ER;
88 ER.V1 = V1;
89 ER.V2 = V2;
90 ER.Flavor = Complex;
91 return ER;
92 }
93 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
94 RValue ER;
95 ER.V1 = C.first;
96 ER.V2 = C.second;
97 ER.Flavor = Complex;
Reid Spencer5f016e22007-07-11 17:01:13 +000098 return ER;
99 }
100 static RValue getAggregate(llvm::Value *V) {
101 RValue ER;
Chris Lattner9b655512007-08-31 22:49:20 +0000102 ER.V1 = V;
103 ER.Flavor = Aggregate;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 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 {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000113 // FIXME: alignment?
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
115 enum {
Chris Lattner349aaec2007-08-02 23:37:31 +0000116 Simple, // This is a normal l-value, use getAddress().
117 VectorElt, // This is a vector element l-value (V[i]), use getVector*
118 BitField, // This is a bitfield l-value, use getBitfield*.
Nate Begeman213541a2008-04-18 23:10:10 +0000119 ExtVectorElt // This is an extended vector subset, use getExtVectorComp
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 } LVType;
121
122 llvm::Value *V;
123
124 union {
Nate Begeman8a997642008-05-09 06:41:27 +0000125 // Index into a vector subscript: V[i]
126 llvm::Value *VectorIdx;
127
128 // ExtVector element subset: V.xyx
129 llvm::Constant *VectorElts;
130
Nate Begeman69ce1df2008-07-25 20:15:41 +0000131 // BitField start bit and size
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000132 struct {
133 unsigned short StartBit;
134 unsigned short Size;
135 bool IsSigned;
Nate Begeman69ce1df2008-07-25 20:15:41 +0000136 } BitfieldData;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 };
Eli Friedman1e692ac2008-06-13 23:01:12 +0000138
139 bool Volatile:1;
140 // FIXME: set but never used, what effect should it have?
141 bool Restrict:1;
142
143private:
144 static void SetQualifiers(unsigned Qualifiers, LValue& R) {
145 R.Volatile = (Qualifiers&QualType::Volatile)!=0;
146 R.Restrict = (Qualifiers&QualType::Restrict)!=0;
147 }
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149public:
150 bool isSimple() const { return LVType == Simple; }
151 bool isVectorElt() const { return LVType == VectorElt; }
152 bool isBitfield() const { return LVType == BitField; }
Nate Begeman213541a2008-04-18 23:10:10 +0000153 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000154
Eli Friedman1e692ac2008-06-13 23:01:12 +0000155 bool isVolatileQualified() const { return Volatile; }
156 bool isRestrictQualified() const { return Restrict; }
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // simple lvalue
159 llvm::Value *getAddress() const { assert(isSimple()); return V; }
160 // vector elt lvalue
161 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
162 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Nate Begeman213541a2008-04-18 23:10:10 +0000163 // extended vector elements.
164 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
Nate Begeman8a997642008-05-09 06:41:27 +0000165 llvm::Constant *getExtVectorElts() const {
Nate Begeman213541a2008-04-18 23:10:10 +0000166 assert(isExtVectorElt());
Chris Lattner6481a572007-08-03 17:31:20 +0000167 return VectorElts;
Chris Lattner349aaec2007-08-02 23:37:31 +0000168 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000169 // bitfield lvalue
170 llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
171 unsigned short getBitfieldStartBit() const {
172 assert(isBitfield());
173 return BitfieldData.StartBit;
174 }
175 unsigned short getBitfieldSize() const {
176 assert(isBitfield());
177 return BitfieldData.Size;
178 }
179 bool isBitfieldSigned() const {
180 assert(isBitfield());
181 return BitfieldData.IsSigned;
182 }
183
Eli Friedman1e692ac2008-06-13 23:01:12 +0000184 static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 LValue R;
186 R.LVType = Simple;
187 R.V = V;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000188 SetQualifiers(Qualifiers,R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 return R;
190 }
191
Eli Friedman1e692ac2008-06-13 23:01:12 +0000192 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
193 unsigned Qualifiers) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 LValue R;
195 R.LVType = VectorElt;
196 R.V = Vec;
197 R.VectorIdx = Idx;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000198 SetQualifiers(Qualifiers,R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 return R;
200 }
201
Eli Friedman1e692ac2008-06-13 23:01:12 +0000202 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
203 unsigned Qualifiers) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000204 LValue R;
Nate Begeman213541a2008-04-18 23:10:10 +0000205 R.LVType = ExtVectorElt;
Chris Lattner349aaec2007-08-02 23:37:31 +0000206 R.V = Vec;
Nate Begeman8a997642008-05-09 06:41:27 +0000207 R.VectorElts = Elts;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000208 SetQualifiers(Qualifiers,R);
Chris Lattner349aaec2007-08-02 23:37:31 +0000209 return R;
210 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000211
212 static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000213 unsigned short Size, bool IsSigned,
214 unsigned Qualifiers) {
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000215 LValue R;
216 R.LVType = BitField;
217 R.V = V;
218 R.BitfieldData.StartBit = StartBit;
219 R.BitfieldData.Size = Size;
220 R.BitfieldData.IsSigned = IsSigned;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000221 SetQualifiers(Qualifiers,R);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000222 return R;
223 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000224};
225
226/// CodeGenFunction - This class organizes the per-function state that is used
227/// while generating LLVM code.
228class CodeGenFunction {
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000229public:
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 CodeGenModule &CGM; // Per-module state.
231 TargetInfo &Target;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000232
Chris Lattner58dee102007-08-21 16:57:55 +0000233 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Chris Lattner50b36742008-04-13 07:32:11 +0000234 llvm::IRBuilder Builder;
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000236 // Holds the Decl for the current function or method
Chris Lattner41110242008-06-17 18:05:57 +0000237 const Decl *CurFuncDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +0000238 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 llvm::Function *CurFn;
240
241 /// AllocaInsertPoint - This is an instruction in the entry block before which
242 /// we prefer to insert allocas.
243 llvm::Instruction *AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 const llvm::Type *LLVMIntTy;
Hartmut Kaiser7b660002007-10-17 15:00:17 +0000246 uint32_t LLVMPointerWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +0000247
Chris Lattner7f02f722007-08-24 05:35:26 +0000248private:
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000249 /// LabelIDs - Track arbitrary ids assigned to labels for use in
250 /// implementing the GCC address-of-label extension and indirect
251 /// goto. IDs are assigned to labels inside getIDForAddrOfLabel().
252 std::map<const LabelStmt*, unsigned> LabelIDs;
253
254 /// IndirectSwitches - Record the list of switches for indirect
255 /// gotos. Emission of the actual switching code needs to be delayed
256 /// until all AddrLabelExprs have been seen.
257 std::vector<llvm::SwitchInst*> IndirectSwitches;
258
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
260 /// decls.
261 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
262
263 /// LabelMap - This keeps track of the LLVM basic block for each C label.
264 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000265
266 // BreakContinueStack - This keeps track of where break and continue
267 // statements should jump to.
268 struct BreakContinue {
269 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
270 : BreakBlock(bb), ContinueBlock(cb) {}
271
272 llvm::BasicBlock *BreakBlock;
273 llvm::BasicBlock *ContinueBlock;
274 };
275 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
276
Devang Patel80fd5f92007-10-09 17:08:50 +0000277 /// SwitchInsn - This is nearest current switch instruction. It is null if
278 /// if current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +0000279 llvm::SwitchInst *SwitchInsn;
280
Devang Patel80fd5f92007-10-09 17:08:50 +0000281 /// CaseRangeBlock - This block holds if condition check for last case
282 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +0000283 llvm::BasicBlock *CaseRangeBlock;
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285public:
286 CodeGenFunction(CodeGenModule &cgm);
287
288 ASTContext &getContext() const;
289
Chris Lattner391d77a2008-03-30 23:03:07 +0000290 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000291 void GenerateCode(const FunctionDecl *FD,
292 llvm::Function *Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000293 void GenerateFunction(const Stmt *Body);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
295 const llvm::Type *ConvertType(QualType T);
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000296
297 llvm::Value *LoadObjCSelf();
Chris Lattner41110242008-06-17 18:05:57 +0000298
299 /// isObjCPointerType - Return true if the specificed AST type will map onto
300 /// some Objective-C pointer type.
301 static bool isObjCPointerType(QualType T);
302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 /// hasAggregateLLVMType - Return true if the specified AST type will map into
304 /// an aggregate LLVM type or is void.
305 static bool hasAggregateLLVMType(QualType T);
306
307 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
308 /// label maps to.
309 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
310
311
312 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000313
314 /// WarnUnsupported - Print out a warning that codegen doesn't support the
315 /// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000316 void WarnUnsupported(const Stmt *S, const char *Type);
Reid Spencer5f016e22007-07-11 17:01:13 +0000317
318 //===--------------------------------------------------------------------===//
319 // Helpers
320 //===--------------------------------------------------------------------===//
321
322 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
323 /// block.
324 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
325 const char *Name = "tmp");
326
327 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
328 /// expression and compare the result against zero, returning an Int1Ty value.
329 llvm::Value *EvaluateExprAsBool(const Expr *E);
330
Chris Lattner9b655512007-08-31 22:49:20 +0000331 /// EmitAnyExpr - Emit code to compute the specified expression which can have
332 /// any type. The result is returned as an RValue struct. If this is an
333 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
334 /// the result should be returned.
335 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
336 bool isAggLocVolatile = false);
Devang Pateld9363c32007-09-28 21:49:18 +0000337
338 /// isDummyBlock - Return true if BB is an empty basic block
339 /// with no predecessors.
340 static bool isDummyBlock(const llvm::BasicBlock *BB);
341
Devang Patel51b09f22007-10-04 23:45:31 +0000342 /// StartBlock - Start new block named N. If insert block is a dummy block
343 /// then reuse it.
344 void StartBlock(const char *N);
345
Devang Patel88a981b2007-11-01 19:11:01 +0000346 /// getCGRecordLayout - Return record layout info.
347 const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000348
349 /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
Steve Naroff248a7532008-04-15 22:42:06 +0000350 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000351
352 /// getAccessedFieldNo - Given an encoded value and a result number, return
353 /// the input field number being accessed.
354 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
355
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000356 unsigned GetIDForAddrOfLabel(const LabelStmt *L);
357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // Declaration Emission
360 //===--------------------------------------------------------------------===//
361
362 void EmitDecl(const Decl &D);
363 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Steve Naroff248a7532008-04-15 22:42:06 +0000364 void EmitBlockVarDecl(const VarDecl &D);
365 void EmitLocalBlockVarDecl(const VarDecl &D);
366 void EmitStaticBlockVarDecl(const VarDecl &D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg);
368
369 //===--------------------------------------------------------------------===//
370 // Statement Emission
371 //===--------------------------------------------------------------------===//
372
373 void EmitStmt(const Stmt *S);
Chris Lattner9b655512007-08-31 22:49:20 +0000374 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
375 llvm::Value *AggLoc = 0, bool isAggVol = false);
Chris Lattner91d723d2008-07-26 20:23:23 +0000376 void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 void EmitLabelStmt(const LabelStmt &S);
378 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000379 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 void EmitIfStmt(const IfStmt &S);
381 void EmitWhileStmt(const WhileStmt &S);
382 void EmitDoStmt(const DoStmt &S);
383 void EmitForStmt(const ForStmt &S);
384 void EmitReturnStmt(const ReturnStmt &S);
385 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000386 void EmitBreakStmt();
387 void EmitContinueStmt();
Devang Patel51b09f22007-10-04 23:45:31 +0000388 void EmitSwitchStmt(const SwitchStmt &S);
389 void EmitDefaultStmt(const DefaultStmt &S);
390 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +0000391 void EmitCaseStmtRange(const CaseStmt &S);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000392 void EmitAsmStmt(const AsmStmt &S);
393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 //===--------------------------------------------------------------------===//
395 // LValue Expression Emission
396 //===--------------------------------------------------------------------===//
397
398 /// EmitLValue - Emit code to compute a designator that specifies the location
399 /// of the expression.
400 ///
401 /// This can return one of two things: a simple address or a bitfield
402 /// reference. In either case, the LLVM Value* in the LValue structure is
403 /// guaranteed to be an LLVM pointer type.
404 ///
405 /// If this returns a bitfield reference, nothing about the pointee type of
406 /// the LLVM value is known: For example, it may not be a pointer to an
407 /// integer.
408 ///
409 /// If this returns a normal address, and if the lvalue's C type is fixed
410 /// size, this method guarantees that the returned pointer type will point to
411 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
412 /// variable length type, this is not possible.
413 ///
414 LValue EmitLValue(const Expr *E);
415
416 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
417 /// this method emits the address of the lvalue, then loads the result as an
418 /// rvalue, returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Nate Begeman213541a2008-04-18 23:10:10 +0000420 RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000421 RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422
Chris Lattner34cdc862007-08-03 16:18:34 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
425 /// lvalue, where both are guaranteed to the have the same type, and that type
426 /// is 'Ty'.
427 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Nate Begeman213541a2008-04-18 23:10:10 +0000428 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
429 QualType Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000430 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty);
Christopher Lamb22c940e2007-12-29 05:02:41 +0000431
432 // Note: only availabe for agg return types
433 LValue EmitCallExprLValue(const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000434
435 LValue EmitDeclRefLValue(const DeclRefExpr *E);
436 LValue EmitStringLiteralLValue(const StringLiteral *E);
Anders Carlsson22742662007-07-21 05:21:51 +0000437 LValue EmitPreDefinedLValue(const PreDefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 LValue EmitUnaryOpLValue(const UnaryOperator *E);
439 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Nate Begeman213541a2008-04-18 23:10:10 +0000440 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +0000441 LValue EmitMemberExpr(const MemberExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +0000442 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
Eli Friedman472778e2008-02-09 08:50:58 +0000443
444 LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000445 bool isUnion, unsigned CVRQualifiers);
Chris Lattner391d77a2008-03-30 23:03:07 +0000446
447 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000449 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 //===--------------------------------------------------------------------===//
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 RValue EmitCallExpr(const CallExpr *E);
Ted Kremenek55499762008-06-17 02:43:46 +0000453
454 RValue EmitCallExpr(Expr *FnExpr, CallExpr::const_arg_iterator ArgBeg,
455 CallExpr::const_arg_iterator ArgEnd);
456
Eli Friedman5193b8a2008-01-30 01:32:06 +0000457 RValue EmitCallExpr(llvm::Value *Callee, QualType FnType,
Ted Kremenek55499762008-06-17 02:43:46 +0000458 CallExpr::const_arg_iterator ArgBeg,
459 CallExpr::const_arg_iterator ArgEnd);
460
Chris Lattner1e4d21e2007-08-26 22:58:05 +0000461 RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000462
Anders Carlsson564f1de2007-12-09 23:17:02 +0000463 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
464 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
465
Anders Carlssoncc23aca2007-12-10 19:35:18 +0000466 llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
Nate Begeman4119d1a2007-12-30 02:59:45 +0000467 llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
468 bool isSplat = false);
Anders Carlsson6086bbd2007-12-15 21:23:30 +0000469
Chris Lattner7f02f722007-08-24 05:35:26 +0000470 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000471 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
472 llvm::Value *EmitObjCMessageExpr(const ObjCMessageExpr *E);
473
474
Anders Carlsson55085182007-08-21 17:43:55 +0000475
Chris Lattner883f6a72007-08-11 00:04:45 +0000476 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000477 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +0000478 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000479
480 // Expressions are broken into three classes: scalar, complex, aggregate.
Chris Lattner883f6a72007-08-11 00:04:45 +0000481
Chris Lattner7f02f722007-08-24 05:35:26 +0000482 /// EmitScalarExpr - Emit the computation of the specified expression of
483 /// LLVM scalar type, returning the result.
484 llvm::Value *EmitScalarExpr(const Expr *E);
485
Chris Lattner3707b252007-08-26 06:48:56 +0000486 /// EmitScalarConversion - Emit a conversion from the specified type to the
487 /// specified destination type, both of which are LLVM scalar types.
488 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
489 QualType DstTy);
490
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000491 /// EmitComplexToScalarConversion - Emit a conversion from the specified
492 /// complex type to the specified destination type, where the destination
493 /// type is an LLVM scalar type.
494 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
495 QualType DstTy);
496
Chris Lattner3707b252007-08-26 06:48:56 +0000497
Chris Lattner883f6a72007-08-11 00:04:45 +0000498 /// EmitAggExpr - Emit the computation of the specified expression of
499 /// aggregate type. The result is computed into DestPtr. Note that if
500 /// DestPtr is null, the value of the aggregate expression is not needed.
501 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000502
503 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000504 /// complex type, returning the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000505 ComplexPairTy EmitComplexExpr(const Expr *E);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000506
507 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
508 /// of complex type, storing into the specified Value*.
Chris Lattner190dbe22007-08-26 16:22:13 +0000509 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
510 bool DestIsVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +0000511 /// LoadComplexFromAddr - Load a complex number from the specified address.
512 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
Chris Lattner2621fd12008-05-08 05:58:21 +0000513
514 /// GenerateStaticBlockVarDecl - return the the static
515 /// declaration of local variable.
516 llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D,
517 bool NoInit,
518 const char *Separator);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000519
520 //===--------------------------------------------------------------------===//
521 // Internal Helpers
522 //===--------------------------------------------------------------------===//
523
524private:
525 /// EmitIndirectSwitches - Emit code for all of the switch
526 /// instructions in IndirectSwitches.
527 void EmitIndirectSwitches();
Reid Spencer5f016e22007-07-11 17:01:13 +0000528};
529} // end namespace CodeGen
530} // end namespace clang
531
532#endif