blob: 8642c1b861fd2348f631b9795f68d029adff6cd9 [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 {
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000028 class BasicBlock;
Reid Spencer5f016e22007-07-11 17:01:13 +000029 class Module;
30}
31
32namespace clang {
33 class ASTContext;
34 class Decl;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000035 class EnumConstantDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 class FunctionDecl;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000037 class FunctionTypeProto;
38 class LabelStmt;
Chris Lattner391d77a2008-03-30 23:03:07 +000039 class ObjCMethodDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 class TargetInfo;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000041 class VarDecl;
Devang Patelb84a06e2007-10-23 02:10:49 +000042
Reid Spencer5f016e22007-07-11 17:01:13 +000043namespace CodeGen {
44 class CodeGenModule;
Devang Patelb84a06e2007-10-23 02:10:49 +000045 class CodeGenTypes;
Devang Patel88a981b2007-11-01 19:11:01 +000046 class CGRecordLayout;
Reid Spencer5f016e22007-07-11 17:01:13 +000047
48/// RValue - This trivial value class is used to represent the result of an
Chris Lattner9b655512007-08-31 22:49:20 +000049/// expression that is evaluated. It can be one of three things: either a
50/// simple LLVM SSA value, a pair of SSA values for complex numbers, or the
51/// address of an aggregate value in memory.
Reid Spencer5f016e22007-07-11 17:01:13 +000052class RValue {
Chris Lattner9b655512007-08-31 22:49:20 +000053 llvm::Value *V1, *V2;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 // TODO: Encode this into the low bit of pointer for more efficient
55 // return-by-value.
Chris Lattner9b655512007-08-31 22:49:20 +000056 enum { Scalar, Complex, Aggregate } Flavor;
Reid Spencer5f016e22007-07-11 17:01:13 +000057
58 // FIXME: Aggregate rvalues need to retain information about whether they are
59 // volatile or not.
60public:
61
Chris Lattner9b655512007-08-31 22:49:20 +000062 bool isScalar() const { return Flavor == Scalar; }
63 bool isComplex() const { return Flavor == Complex; }
64 bool isAggregate() const { return Flavor == Aggregate; }
Reid Spencer5f016e22007-07-11 17:01:13 +000065
Chris Lattner9b655512007-08-31 22:49:20 +000066 /// getScalar() - Return the Value* of this scalar value.
67 llvm::Value *getScalarVal() const {
68 assert(isScalar() && "Not a scalar!");
69 return V1;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71
Chris Lattner9b655512007-08-31 22:49:20 +000072 /// getComplexVal - Return the real/imag components of this complex value.
73 ///
74 std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
75 return std::pair<llvm::Value *, llvm::Value *>(V1, V2);
76 }
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078 /// getAggregateAddr() - Return the Value* of the address of the aggregate.
79 llvm::Value *getAggregateAddr() const {
80 assert(isAggregate() && "Not an aggregate!");
Chris Lattner9b655512007-08-31 22:49:20 +000081 return V1;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 }
83
84 static RValue get(llvm::Value *V) {
85 RValue ER;
Chris Lattner9b655512007-08-31 22:49:20 +000086 ER.V1 = V;
87 ER.Flavor = Scalar;
88 return ER;
89 }
90 static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
91 RValue ER;
92 ER.V1 = V1;
93 ER.V2 = V2;
94 ER.Flavor = Complex;
95 return ER;
96 }
97 static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
98 RValue ER;
99 ER.V1 = C.first;
100 ER.V2 = C.second;
101 ER.Flavor = Complex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 return ER;
103 }
104 static RValue getAggregate(llvm::Value *V) {
105 RValue ER;
Chris Lattner9b655512007-08-31 22:49:20 +0000106 ER.V1 = V;
107 ER.Flavor = Aggregate;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 return ER;
109 }
110};
111
112
113/// LValue - This represents an lvalue references. Because C/C++ allow
114/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
115/// bitrange.
116class LValue {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000117 // FIXME: alignment?
Reid Spencer5f016e22007-07-11 17:01:13 +0000118
119 enum {
Chris Lattner349aaec2007-08-02 23:37:31 +0000120 Simple, // This is a normal l-value, use getAddress().
121 VectorElt, // This is a vector element l-value (V[i]), use getVector*
122 BitField, // This is a bitfield l-value, use getBitfield*.
Nate Begeman213541a2008-04-18 23:10:10 +0000123 ExtVectorElt // This is an extended vector subset, use getExtVectorComp
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 } LVType;
125
126 llvm::Value *V;
127
128 union {
Nate Begeman8a997642008-05-09 06:41:27 +0000129 // Index into a vector subscript: V[i]
130 llvm::Value *VectorIdx;
131
132 // ExtVector element subset: V.xyx
133 llvm::Constant *VectorElts;
134
Nate Begeman69ce1df2008-07-25 20:15:41 +0000135 // BitField start bit and size
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000136 struct {
137 unsigned short StartBit;
138 unsigned short Size;
139 bool IsSigned;
Nate Begeman69ce1df2008-07-25 20:15:41 +0000140 } BitfieldData;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 };
Eli Friedman1e692ac2008-06-13 23:01:12 +0000142
143 bool Volatile:1;
144 // FIXME: set but never used, what effect should it have?
145 bool Restrict:1;
146
147private:
148 static void SetQualifiers(unsigned Qualifiers, LValue& R) {
149 R.Volatile = (Qualifiers&QualType::Volatile)!=0;
150 R.Restrict = (Qualifiers&QualType::Restrict)!=0;
151 }
152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153public:
154 bool isSimple() const { return LVType == Simple; }
155 bool isVectorElt() const { return LVType == VectorElt; }
156 bool isBitfield() const { return LVType == BitField; }
Nate Begeman213541a2008-04-18 23:10:10 +0000157 bool isExtVectorElt() const { return LVType == ExtVectorElt; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000158
Eli Friedman1e692ac2008-06-13 23:01:12 +0000159 bool isVolatileQualified() const { return Volatile; }
160 bool isRestrictQualified() const { return Restrict; }
161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // simple lvalue
163 llvm::Value *getAddress() const { assert(isSimple()); return V; }
164 // vector elt lvalue
165 llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; }
166 llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; }
Nate Begeman213541a2008-04-18 23:10:10 +0000167 // extended vector elements.
168 llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; }
Nate Begeman8a997642008-05-09 06:41:27 +0000169 llvm::Constant *getExtVectorElts() const {
Nate Begeman213541a2008-04-18 23:10:10 +0000170 assert(isExtVectorElt());
Chris Lattner6481a572007-08-03 17:31:20 +0000171 return VectorElts;
Chris Lattner349aaec2007-08-02 23:37:31 +0000172 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000173 // bitfield lvalue
174 llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; }
175 unsigned short getBitfieldStartBit() const {
176 assert(isBitfield());
177 return BitfieldData.StartBit;
178 }
179 unsigned short getBitfieldSize() const {
180 assert(isBitfield());
181 return BitfieldData.Size;
182 }
183 bool isBitfieldSigned() const {
184 assert(isBitfield());
185 return BitfieldData.IsSigned;
186 }
187
Eli Friedman1e692ac2008-06-13 23:01:12 +0000188 static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 LValue R;
190 R.LVType = Simple;
191 R.V = V;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000192 SetQualifiers(Qualifiers,R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 return R;
194 }
195
Eli Friedman1e692ac2008-06-13 23:01:12 +0000196 static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx,
197 unsigned Qualifiers) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 LValue R;
199 R.LVType = VectorElt;
200 R.V = Vec;
201 R.VectorIdx = Idx;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000202 SetQualifiers(Qualifiers,R);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 return R;
204 }
205
Eli Friedman1e692ac2008-06-13 23:01:12 +0000206 static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts,
207 unsigned Qualifiers) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000208 LValue R;
Nate Begeman213541a2008-04-18 23:10:10 +0000209 R.LVType = ExtVectorElt;
Chris Lattner349aaec2007-08-02 23:37:31 +0000210 R.V = Vec;
Nate Begeman8a997642008-05-09 06:41:27 +0000211 R.VectorElts = Elts;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000212 SetQualifiers(Qualifiers,R);
Chris Lattner349aaec2007-08-02 23:37:31 +0000213 return R;
214 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000215
216 static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000217 unsigned short Size, bool IsSigned,
218 unsigned Qualifiers) {
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000219 LValue R;
220 R.LVType = BitField;
221 R.V = V;
222 R.BitfieldData.StartBit = StartBit;
223 R.BitfieldData.Size = Size;
224 R.BitfieldData.IsSigned = IsSigned;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000225 SetQualifiers(Qualifiers,R);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000226 return R;
227 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000228};
229
230/// CodeGenFunction - This class organizes the per-function state that is used
231/// while generating LLVM code.
232class CodeGenFunction {
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000233public:
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 CodeGenModule &CGM; // Per-module state.
235 TargetInfo &Target;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000236
Chris Lattner58dee102007-08-21 16:57:55 +0000237 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Chris Lattner85e35682008-08-08 19:57:58 +0000238 llvm::IRBuilder<> Builder;
Reid Spencer5f016e22007-07-11 17:01:13 +0000239
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000240 // Holds the Decl for the current function or method
Chris Lattner41110242008-06-17 18:05:57 +0000241 const Decl *CurFuncDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +0000242 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 llvm::Function *CurFn;
244
245 /// AllocaInsertPoint - This is an instruction in the entry block before which
246 /// we prefer to insert allocas.
247 llvm::Instruction *AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 const llvm::Type *LLVMIntTy;
Hartmut Kaiser7b660002007-10-17 15:00:17 +0000250 uint32_t LLVMPointerWidth;
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
Chris Lattner7f02f722007-08-24 05:35:26 +0000252private:
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000253 /// LabelIDs - Track arbitrary ids assigned to labels for use in
254 /// implementing the GCC address-of-label extension and indirect
255 /// goto. IDs are assigned to labels inside getIDForAddrOfLabel().
256 std::map<const LabelStmt*, unsigned> LabelIDs;
257
258 /// IndirectSwitches - Record the list of switches for indirect
259 /// gotos. Emission of the actual switching code needs to be delayed
260 /// until all AddrLabelExprs have been seen.
261 std::vector<llvm::SwitchInst*> IndirectSwitches;
262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
264 /// decls.
265 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
266
267 /// LabelMap - This keeps track of the LLVM basic block for each C label.
268 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
Chris Lattnerda138702007-07-16 21:28:45 +0000269
270 // BreakContinueStack - This keeps track of where break and continue
271 // statements should jump to.
272 struct BreakContinue {
273 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
274 : BreakBlock(bb), ContinueBlock(cb) {}
275
276 llvm::BasicBlock *BreakBlock;
277 llvm::BasicBlock *ContinueBlock;
278 };
279 llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
280
Devang Patel80fd5f92007-10-09 17:08:50 +0000281 /// SwitchInsn - This is nearest current switch instruction. It is null if
282 /// if current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +0000283 llvm::SwitchInst *SwitchInsn;
284
Devang Patel80fd5f92007-10-09 17:08:50 +0000285 /// CaseRangeBlock - This block holds if condition check for last case
286 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +0000287 llvm::BasicBlock *CaseRangeBlock;
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289public:
290 CodeGenFunction(CodeGenModule &cgm);
291
292 ASTContext &getContext() const;
293
Chris Lattner391d77a2008-03-30 23:03:07 +0000294 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000295 void GenerateCode(const FunctionDecl *FD,
296 llvm::Function *Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000297 void GenerateFunction(const Stmt *Body);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
299 const llvm::Type *ConvertType(QualType T);
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000300
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000301 /// LoadObjCSelf - Load the value of self. This function is only
302 /// valid while generating code for an Objective-C method.
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000303 llvm::Value *LoadObjCSelf();
Chris Lattner41110242008-06-17 18:05:57 +0000304
305 /// isObjCPointerType - Return true if the specificed AST type will map onto
306 /// some Objective-C pointer type.
307 static bool isObjCPointerType(QualType T);
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 /// hasAggregateLLVMType - Return true if the specified AST type will map into
310 /// an aggregate LLVM type or is void.
311 static bool hasAggregateLLVMType(QualType T);
312
313 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
314 /// label maps to.
315 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
316
317
318 void EmitBlock(llvm::BasicBlock *BB);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000319
Daniel Dunbar488e9932008-08-16 00:56:44 +0000320 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000321 /// specified stmt yet.
Daniel Dunbar488e9932008-08-16 00:56:44 +0000322 void ErrorUnsupported(const Stmt *S, const char *Type);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323
324 //===--------------------------------------------------------------------===//
325 // Helpers
326 //===--------------------------------------------------------------------===//
327
328 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
329 /// block.
330 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
331 const char *Name = "tmp");
332
333 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
334 /// expression and compare the result against zero, returning an Int1Ty value.
335 llvm::Value *EvaluateExprAsBool(const Expr *E);
336
Chris Lattner9b655512007-08-31 22:49:20 +0000337 /// EmitAnyExpr - Emit code to compute the specified expression which can have
338 /// any type. The result is returned as an RValue struct. If this is an
339 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
340 /// the result should be returned.
341 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
342 bool isAggLocVolatile = false);
Devang Pateld9363c32007-09-28 21:49:18 +0000343
344 /// isDummyBlock - Return true if BB is an empty basic block
345 /// with no predecessors.
346 static bool isDummyBlock(const llvm::BasicBlock *BB);
347
Devang Patel51b09f22007-10-04 23:45:31 +0000348 /// StartBlock - Start new block named N. If insert block is a dummy block
349 /// then reuse it.
350 void StartBlock(const char *N);
351
Devang Patel88a981b2007-11-01 19:11:01 +0000352 /// getCGRecordLayout - Return record layout info.
353 const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000354
355 /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
Steve Naroff248a7532008-04-15 22:42:06 +0000356 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000357
358 /// getAccessedFieldNo - Given an encoded value and a result number, return
359 /// the input field number being accessed.
360 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
361
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000362 unsigned GetIDForAddrOfLabel(const LabelStmt *L);
363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // Declaration Emission
366 //===--------------------------------------------------------------------===//
367
368 void EmitDecl(const Decl &D);
369 void EmitEnumConstantDecl(const EnumConstantDecl &D);
Steve Naroff248a7532008-04-15 22:42:06 +0000370 void EmitBlockVarDecl(const VarDecl &D);
371 void EmitLocalBlockVarDecl(const VarDecl &D);
372 void EmitStaticBlockVarDecl(const VarDecl &D);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000373
374 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
375 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg);
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
377 //===--------------------------------------------------------------------===//
378 // Statement Emission
379 //===--------------------------------------------------------------------===//
380
381 void EmitStmt(const Stmt *S);
Chris Lattner9b655512007-08-31 22:49:20 +0000382 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
383 llvm::Value *AggLoc = 0, bool isAggVol = false);
Chris Lattner91d723d2008-07-26 20:23:23 +0000384 void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 void EmitLabelStmt(const LabelStmt &S);
386 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000387 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 void EmitIfStmt(const IfStmt &S);
389 void EmitWhileStmt(const WhileStmt &S);
390 void EmitDoStmt(const DoStmt &S);
391 void EmitForStmt(const ForStmt &S);
392 void EmitReturnStmt(const ReturnStmt &S);
393 void EmitDeclStmt(const DeclStmt &S);
Chris Lattnerda138702007-07-16 21:28:45 +0000394 void EmitBreakStmt();
395 void EmitContinueStmt();
Devang Patel51b09f22007-10-04 23:45:31 +0000396 void EmitSwitchStmt(const SwitchStmt &S);
397 void EmitDefaultStmt(const DefaultStmt &S);
398 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +0000399 void EmitCaseStmtRange(const CaseStmt &S);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +0000400 void EmitAsmStmt(const AsmStmt &S);
401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 //===--------------------------------------------------------------------===//
403 // LValue Expression Emission
404 //===--------------------------------------------------------------------===//
405
406 /// EmitLValue - Emit code to compute a designator that specifies the location
407 /// of the expression.
408 ///
409 /// This can return one of two things: a simple address or a bitfield
410 /// reference. In either case, the LLVM Value* in the LValue structure is
411 /// guaranteed to be an LLVM pointer type.
412 ///
413 /// If this returns a bitfield reference, nothing about the pointee type of
414 /// the LLVM value is known: For example, it may not be a pointer to an
415 /// integer.
416 ///
417 /// If this returns a normal address, and if the lvalue's C type is fixed
418 /// size, this method guarantees that the returned pointer type will point to
419 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
420 /// variable length type, this is not possible.
421 ///
422 LValue EmitLValue(const Expr *E);
423
424 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
425 /// this method emits the address of the lvalue, then loads the result as an
426 /// rvalue, returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 RValue EmitLoadOfLValue(LValue V, QualType LVType);
Nate Begeman213541a2008-04-18 23:10:10 +0000428 RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000429 RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430
Chris Lattner34cdc862007-08-03 16:18:34 +0000431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
433 /// lvalue, where both are guaranteed to the have the same type, and that type
434 /// is 'Ty'.
435 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
Nate Begeman213541a2008-04-18 23:10:10 +0000436 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
437 QualType Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000438 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty);
Christopher Lamb22c940e2007-12-29 05:02:41 +0000439
440 // Note: only availabe for agg return types
441 LValue EmitCallExprLValue(const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
443 LValue EmitDeclRefLValue(const DeclRefExpr *E);
444 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000445 LValue EmitPredefinedLValue(const PredefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 LValue EmitUnaryOpLValue(const UnaryOperator *E);
447 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Nate Begeman213541a2008-04-18 23:10:10 +0000448 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +0000449 LValue EmitMemberExpr(const MemberExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +0000450 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
Eli Friedman472778e2008-02-09 08:50:58 +0000451
452 LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000453 bool isUnion, unsigned CVRQualifiers);
Chris Lattner391d77a2008-03-30 23:03:07 +0000454
455 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +0000457 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 //===--------------------------------------------------------------------===//
459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 RValue EmitCallExpr(const CallExpr *E);
Ted Kremenek55499762008-06-17 02:43:46 +0000461
462 RValue EmitCallExpr(Expr *FnExpr, CallExpr::const_arg_iterator ArgBeg,
463 CallExpr::const_arg_iterator ArgEnd);
464
Eli Friedman5193b8a2008-01-30 01:32:06 +0000465 RValue EmitCallExpr(llvm::Value *Callee, QualType FnType,
Ted Kremenek55499762008-06-17 02:43:46 +0000466 CallExpr::const_arg_iterator ArgBeg,
467 CallExpr::const_arg_iterator ArgEnd);
468
Chris Lattner1e4d21e2007-08-26 22:58:05 +0000469 RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000470
Anders Carlsson564f1de2007-12-09 23:17:02 +0000471 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
472 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
473
Anders Carlssoncc23aca2007-12-10 19:35:18 +0000474 llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
Nate Begeman4119d1a2007-12-30 02:59:45 +0000475 llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
476 bool isSplat = false);
Anders Carlsson6086bbd2007-12-15 21:23:30 +0000477
Daniel Dunbared7c6182008-08-20 00:28:19 +0000478 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +0000479 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000480 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
481 llvm::Value *EmitObjCMessageExpr(const ObjCMessageExpr *E);
482
483
Chris Lattner883f6a72007-08-11 00:04:45 +0000484 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000485 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +0000486 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000487
488 // Expressions are broken into three classes: scalar, complex, aggregate.
Chris Lattner883f6a72007-08-11 00:04:45 +0000489
Chris Lattner7f02f722007-08-24 05:35:26 +0000490 /// EmitScalarExpr - Emit the computation of the specified expression of
491 /// LLVM scalar type, returning the result.
492 llvm::Value *EmitScalarExpr(const Expr *E);
493
Chris Lattner3707b252007-08-26 06:48:56 +0000494 /// EmitScalarConversion - Emit a conversion from the specified type to the
495 /// specified destination type, both of which are LLVM scalar types.
496 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
497 QualType DstTy);
498
Chris Lattner4f1a7b32007-08-26 16:34:22 +0000499 /// EmitComplexToScalarConversion - Emit a conversion from the specified
500 /// complex type to the specified destination type, where the destination
501 /// type is an LLVM scalar type.
502 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
503 QualType DstTy);
504
Chris Lattner3707b252007-08-26 06:48:56 +0000505
Chris Lattner883f6a72007-08-11 00:04:45 +0000506 /// EmitAggExpr - Emit the computation of the specified expression of
507 /// aggregate type. The result is computed into DestPtr. Note that if
508 /// DestPtr is null, the value of the aggregate expression is not needed.
509 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000510
511 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000512 /// complex type, returning the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000513 ComplexPairTy EmitComplexExpr(const Expr *E);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000514
515 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
516 /// of complex type, storing into the specified Value*.
Chris Lattner190dbe22007-08-26 16:22:13 +0000517 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
518 bool DestIsVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +0000519 /// LoadComplexFromAddr - Load a complex number from the specified address.
520 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
Chris Lattner2621fd12008-05-08 05:58:21 +0000521
522 /// GenerateStaticBlockVarDecl - return the the static
523 /// declaration of local variable.
524 llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D,
525 bool NoInit,
526 const char *Separator);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000527
528 //===--------------------------------------------------------------------===//
529 // Internal Helpers
530 //===--------------------------------------------------------------------===//
531
532private:
533 /// EmitIndirectSwitches - Emit code for all of the switch
534 /// instructions in IndirectSwitches.
535 void EmitIndirectSwitches();
Reid Spencer5f016e22007-07-11 17:01:13 +0000536};
537} // end namespace CodeGen
538} // end namespace clang
539
540#endif