CodeGenFunction.h revision 88b5396b0897f28d22ae3debf4a0d97b33b6c362
1//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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 CLANG_CODEGEN_CODEGENFUNCTION_H
15#define CLANG_CODEGEN_CODEGENFUNCTION_H
16
17#include "clang/AST/Type.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
20#include "clang/Basic/TargetInfo.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
23#include "clang/AST/ExprObjC.h"
24
25#include <vector>
26#include <map>
27
28#include "CGBuilder.h"
29#include "CGCall.h"
30#include "CGValue.h"
31
32namespace llvm {
33  class BasicBlock;
34  class Module;
35  class SwitchInst;
36  class Value;
37}
38
39namespace clang {
40  class ASTContext;
41  class Decl;
42  class EnumConstantDecl;
43  class FunctionDecl;
44  class FunctionTypeProto;
45  class LabelStmt;
46  class ObjCContainerDecl;
47  class ObjCInterfaceDecl;
48  class ObjCIvarDecl;
49  class ObjCMethodDecl;
50  class ObjCImplementationDecl;
51  class ObjCPropertyImplDecl;
52  class TargetInfo;
53  class VarDecl;
54
55namespace CodeGen {
56  class CodeGenModule;
57  class CodeGenTypes;
58  class CGFunctionInfo;
59  class CGRecordLayout;
60
61/// CodeGenFunction - This class organizes the per-function state that is used
62/// while generating LLVM code.
63class CodeGenFunction {
64public:
65  CodeGenModule &CGM;  // Per-module state.
66  TargetInfo &Target;
67
68  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
69  CGBuilderTy Builder;
70
71  // Holds the Decl for the current function or method
72  const Decl *CurFuncDecl;
73  const CGFunctionInfo *CurFnInfo;
74  QualType FnRetTy;
75  llvm::Function *CurFn;
76
77  /// ReturnBlock - Unified return block.
78  llvm::BasicBlock *ReturnBlock;
79  /// ReturnValue - The temporary alloca to hold the return value. This
80  /// is null iff the function has no return value.
81  llvm::Instruction *ReturnValue;
82
83  /// AllocaInsertPoint - This is an instruction in the entry block before which
84  /// we prefer to insert allocas.
85  llvm::Instruction *AllocaInsertPt;
86
87  const llvm::Type *LLVMIntTy;
88  uint32_t LLVMPointerWidth;
89
90public:
91  // FIXME: The following should be private once EH code is moved out
92  // of NeXT runtime.
93
94  // ObjCEHStack - This keeps track of which object to rethrow from
95  // inside @catch blocks and which @finally block exits from an EH
96  // scope should be chained through.
97  struct ObjCEHEntry {
98    ObjCEHEntry(llvm::BasicBlock *fb, llvm::BasicBlock *fne,
99                llvm::SwitchInst *fs, llvm::Value *dc)
100      : FinallyBlock(fb), FinallyNoExit(fne), FinallySwitch(fs),
101        DestCode(dc), Exception(0) {}
102
103    /// Entry point to the finally block.
104    llvm::BasicBlock *FinallyBlock;
105
106    /// Entry point to the finally block which skips execution of the
107    /// try_exit runtime function.
108    llvm::BasicBlock *FinallyNoExit;
109
110    /// Switch instruction which runs at the end of the finally block
111    /// to forward jumps through the finally block.
112    llvm::SwitchInst *FinallySwitch;
113
114    /// Variable holding the code for the destination of a jump
115    /// through the @finally block.
116    llvm::Value *DestCode;
117
118    /// The exception object being handled, during IR generation for a
119    /// @catch block.
120    llvm::Value *Exception;
121  };
122
123  typedef llvm::SmallVector<ObjCEHEntry*, 8> ObjCEHStackType;
124  ObjCEHStackType ObjCEHStack;
125
126  /// EmitJumpThroughFinally - Emit a branch from the current insert
127  /// point through the finally handling code for \arg Entry and then
128  /// on to \arg Dest. It is legal to call this function even if there
129  /// is no current insertion point.
130  ///
131  /// \param ExecuteTryExit - When true, the try_exit runtime function
132  /// should be called prior to executing the finally code.
133  void EmitJumpThroughFinally(ObjCEHEntry *Entry, llvm::BasicBlock *Dest,
134                              bool ExecuteTryExit=true);
135
136private:
137  /// LabelIDs - Track arbitrary ids assigned to labels for use in
138  /// implementing the GCC address-of-label extension and indirect
139  /// goto. IDs are assigned to labels inside getIDForAddrOfLabel().
140  std::map<const LabelStmt*, unsigned> LabelIDs;
141
142  /// IndirectSwitches - Record the list of switches for indirect
143  /// gotos. Emission of the actual switching code needs to be delayed
144  /// until all AddrLabelExprs have been seen.
145  std::vector<llvm::SwitchInst*> IndirectSwitches;
146
147  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
148  /// decls.
149  llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
150
151  /// LabelMap - This keeps track of the LLVM basic block for each C label.
152  llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
153
154  // BreakContinueStack - This keeps track of where break and continue
155  // statements should jump to, as well as the size of the eh stack.
156  struct BreakContinue {
157    BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb, size_t ehss)
158      : BreakBlock(bb), ContinueBlock(cb), EHStackSize(ehss) {}
159
160    llvm::BasicBlock *BreakBlock;
161    llvm::BasicBlock *ContinueBlock;
162    size_t EHStackSize;
163  };
164  llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
165
166  /// SwitchInsn - This is nearest current switch instruction. It is null if
167  /// if current context is not in a switch.
168  llvm::SwitchInst *SwitchInsn;
169
170  /// CaseRangeBlock - This block holds if condition check for last case
171  /// statement range in current switch instruction.
172  llvm::BasicBlock *CaseRangeBlock;
173
174  // VLASizeMap - This keeps track of the associated size for each VLA type
175  // FIXME: Maybe this could be a stack of maps that is pushed/popped as
176  // we enter/leave scopes.
177  llvm::DenseMap<const VariableArrayType*, llvm::Value*> VLASizeMap;
178
179  /// StackSaveValues - A stack(!) of stack save values. When a new scope is
180  /// entered, a null is pushed on this stack. If a VLA is emitted, then
181  /// the return value of llvm.stacksave() is stored at the top of this stack.
182  llvm::SmallVector<llvm::Value*, 8> StackSaveValues;
183
184public:
185  CodeGenFunction(CodeGenModule &cgm);
186
187  ASTContext &getContext() const;
188
189  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
190
191  void StartObjCMethod(const ObjCMethodDecl *MD,
192                       const ObjCContainerDecl *CD);
193
194  /// GenerateObjCGetter - Synthesize an Objective-C property getter
195  /// function.
196  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
197                          const ObjCPropertyImplDecl *PID);
198
199  /// GenerateObjCSetter - Synthesize an Objective-C property setter
200  /// function for the given property.
201  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
202                          const ObjCPropertyImplDecl *PID);
203
204  void GenerateCode(const FunctionDecl *FD,
205                    llvm::Function *Fn);
206  void StartFunction(const Decl *D, QualType RetTy,
207                     llvm::Function *Fn,
208                     const FunctionArgList &Args,
209                     SourceLocation StartLoc);
210
211  /// EmitReturnBlock - Emit the unified return block, trying to avoid
212  /// its emission when possible.
213  void EmitReturnBlock();
214
215  /// FinishFunction - Complete IR generation of the current
216  /// function. It is legal to call this function even if there is no
217  /// current insertion point.
218  void FinishFunction(SourceLocation EndLoc=SourceLocation());
219
220  /// EmitFunctionProlog - Emit the target specific LLVM code to load
221  /// the arguments for the given function. This is also responsible
222  /// for naming the LLVM function arguments.
223  void EmitFunctionProlog(const CGFunctionInfo &FI,
224                          llvm::Function *Fn,
225                          const FunctionArgList &Args);
226
227  /// EmitFunctionEpilog - Emit the target specific LLVM code to
228  /// return the given temporary.
229  void EmitFunctionEpilog(const CGFunctionInfo &FI, llvm::Value *ReturnValue);
230
231  const llvm::Type *ConvertType(QualType T);
232
233  /// LoadObjCSelf - Load the value of self. This function is only
234  /// valid while generating code for an Objective-C method.
235  llvm::Value *LoadObjCSelf();
236
237  /// isObjCPointerType - Return true if the specificed AST type will map onto
238  /// some Objective-C pointer type.
239  static bool isObjCPointerType(QualType T);
240
241  /// hasAggregateLLVMType - Return true if the specified AST type will map into
242  /// an aggregate LLVM type or is void.
243  static bool hasAggregateLLVMType(QualType T);
244
245  /// createBasicBlock - Create an LLVM basic block.
246  llvm::BasicBlock *createBasicBlock(const char *Name="",
247                                     llvm::Function *Parent=0,
248                                     llvm::BasicBlock *InsertBefore=0) {
249#ifdef NDEBUG
250    return llvm::BasicBlock::Create("", Parent, InsertBefore);
251#else
252    return llvm::BasicBlock::Create(Name, Parent, InsertBefore);
253#endif
254  }
255
256  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
257  /// label maps to.
258  llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
259
260  /// EmitBlock - Emit the given block \arg BB and set it as the
261  /// insert point, adding a fall-through branch from the current
262  /// insert block if necessary. It is legal to call this function
263  /// even if there is no current insertion point.
264  ///
265  /// IsFinished - If true, indicates that the caller has finished
266  /// emitting branches to the given block and does not expect to emit
267  /// code into it. This means the block can be ignored if it is
268  /// unreachable.
269  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
270
271  /// EmitBranch - Emit a branch to the specified basic block from the
272  /// current insert block, taking care to avoid creation of branches
273  /// from dummy blocks. It is legal to call this function even if
274  /// there is no current insertion point.
275  ///
276  /// This function clears the current insertion point. The caller
277  /// should follow calls to this function with calls to Emit*Block
278  /// prior to generation new code.
279  void EmitBranch(llvm::BasicBlock *Block);
280
281  /// HaveInsertPoint - True if an insertion point is defined. If not,
282  /// this indicates that the current code being emitted is
283  /// unreachable.
284  bool HaveInsertPoint() const {
285    return Builder.GetInsertBlock() != 0;
286  }
287
288  /// EnsureInsertPoint - Ensure that an insertion point is defined so
289  /// that emitted IR has a place to go. Note that by definition, if
290  /// this function creates a block then that block is unreachable;
291  /// callers may do better to detect when no insertion point is
292  /// defined and simply skip IR generation.
293  void EnsureInsertPoint() {
294    if (!HaveInsertPoint())
295      EmitBlock(createBasicBlock());
296  }
297
298  /// ErrorUnsupported - Print out an error that codegen doesn't support the
299  /// specified stmt yet.
300  void ErrorUnsupported(const Stmt *S, const char *Type,
301                        bool OmitOnError=false);
302
303  //===--------------------------------------------------------------------===//
304  //                                  Helpers
305  //===--------------------------------------------------------------------===//
306
307  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
308  /// block.
309  llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
310                                     const char *Name = "tmp");
311
312  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
313  /// expression and compare the result against zero, returning an Int1Ty value.
314  llvm::Value *EvaluateExprAsBool(const Expr *E);
315
316  /// EmitAnyExpr - Emit code to compute the specified expression which can have
317  /// any type.  The result is returned as an RValue struct.  If this is an
318  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
319  /// the result should be returned.
320  RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
321                     bool isAggLocVolatile = false);
322
323  // EmitVAListRef - Emit a "reference" to a va_list; this is either the
324  // address or the value of the expression, depending on how va_list is
325  // defined.
326  llvm::Value *EmitVAListRef(const Expr *E);
327
328  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
329  /// will always be accessible even if no aggregate location is
330  /// provided.
331  RValue EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc = 0,
332                           bool isAggLocVolatile = false);
333
334  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
335                         QualType EltTy);
336
337  void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
338
339  /// StartBlock - Start new block named N. If insert block is a dummy block
340  /// then reuse it.
341  void StartBlock(const char *N);
342
343  /// getCGRecordLayout - Return record layout info.
344  const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy);
345
346  /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
347  llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
348
349  /// GetAddrOfLocalVar - Return the address of a local variable.
350  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD);
351
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
356  unsigned GetIDForAddrOfLabel(const LabelStmt *L);
357
358  /// EmitMemSetToZero - Generate code to memset a value of the given type to 0;
359  void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty);
360
361  // EmitVAArg - Generate code to get an argument from the passed in pointer
362  // and update it accordingly. The return value is a pointer to the argument.
363  // FIXME: We should be able to get rid of this method and use the va_arg
364  // instruction in LLVM instead once it works well enough.
365  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
366
367  // EmitVLASize - Generate code for any VLA size expressions that might occur
368  // in a variably modified type. If Ty is a VLA, will return the value that
369  // corresponds to the size in bytes of the VLA type. Will return 0 otherwise.
370  llvm::Value *EmitVLASize(QualType Ty);
371
372  // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
373  // of a variable length array type.
374  llvm::Value *GetVLASize(const VariableArrayType *);
375
376  //===--------------------------------------------------------------------===//
377  //                            Declaration Emission
378  //===--------------------------------------------------------------------===//
379
380  void EmitDecl(const Decl &D);
381  void EmitBlockVarDecl(const VarDecl &D);
382  void EmitLocalBlockVarDecl(const VarDecl &D);
383  void EmitStaticBlockVarDecl(const VarDecl &D);
384
385  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
386  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg);
387
388  //===--------------------------------------------------------------------===//
389  //                             Statement Emission
390  //===--------------------------------------------------------------------===//
391
392  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug
393  /// info.
394  void EmitStopPoint(const Stmt *S);
395
396  /// EmitStmt - Emit the code for the statement \arg S. It is legal
397  /// to call this function even if there is no current insertion
398  /// point.
399  ///
400  /// This function may clear the current insertion point; callers
401  /// should use EnsureInsertPoint if they wish to subsequently
402  /// generate code without first calling EmitBlock, EmitBranch, or
403  /// EmitStmt.
404  void EmitStmt(const Stmt *S);
405
406  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
407  /// necessarily require an insertion point or debug information;
408  /// typically because the statement amounts to a jump or a container
409  /// of other statements.
410  ///
411  /// \return True if the statement was handled.
412  bool EmitSimpleStmt(const Stmt *S);
413
414  RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
415                          llvm::Value *AggLoc = 0, bool isAggVol = false);
416
417  /// EmitLabel - Emit the block for the given label. It is legal
418  /// to call this function even if there is no current insertion
419  /// point.
420  void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt.
421
422  void EmitLabelStmt(const LabelStmt &S);
423  void EmitGotoStmt(const GotoStmt &S);
424  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
425  void EmitIfStmt(const IfStmt &S);
426  void EmitWhileStmt(const WhileStmt &S);
427  void EmitDoStmt(const DoStmt &S);
428  void EmitForStmt(const ForStmt &S);
429  void EmitReturnStmt(const ReturnStmt &S);
430  void EmitDeclStmt(const DeclStmt &S);
431  void EmitBreakStmt(const BreakStmt &S);
432  void EmitContinueStmt(const ContinueStmt &S);
433  void EmitSwitchStmt(const SwitchStmt &S);
434  void EmitDefaultStmt(const DefaultStmt &S);
435  void EmitCaseStmt(const CaseStmt &S);
436  void EmitCaseStmtRange(const CaseStmt &S);
437  void EmitAsmStmt(const AsmStmt &S);
438
439  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
440  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
441  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
442  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
443
444  //===--------------------------------------------------------------------===//
445  //                         LValue Expression Emission
446  //===--------------------------------------------------------------------===//
447
448  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
449  /// and issue an ErrorUnsupported style diagnostic (using the
450  /// provided Name).
451  RValue EmitUnsupportedRValue(const Expr *E,
452                               const char *Name);
453
454  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E
455  /// and issue an ErrorUnsupported style diagnostic (using the
456  /// provided Name).
457  LValue EmitUnsupportedLValue(const Expr *E,
458                               const char *Name);
459
460  /// EmitLValue - Emit code to compute a designator that specifies the location
461  /// of the expression.
462  ///
463  /// This can return one of two things: a simple address or a bitfield
464  /// reference.  In either case, the LLVM Value* in the LValue structure is
465  /// guaranteed to be an LLVM pointer type.
466  ///
467  /// If this returns a bitfield reference, nothing about the pointee type of
468  /// the LLVM value is known: For example, it may not be a pointer to an
469  /// integer.
470  ///
471  /// If this returns a normal address, and if the lvalue's C type is fixed
472  /// size, this method guarantees that the returned pointer type will point to
473  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
474  /// variable length type, this is not possible.
475  ///
476  LValue EmitLValue(const Expr *E);
477
478  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
479  /// this method emits the address of the lvalue, then loads the result as an
480  /// rvalue, returning the rvalue.
481  RValue EmitLoadOfLValue(LValue V, QualType LVType);
482  RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
483  RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
484  RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType);
485  RValue EmitLoadOfKVCRefLValue(LValue LV, QualType ExprType);
486
487
488  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
489  /// lvalue, where both are guaranteed to the have the same type, and that type
490  /// is 'Ty'.
491  void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
492  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
493                                                QualType Ty);
494  void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty);
495  void EmitStoreThroughKVCRefLValue(RValue Src, LValue Dst, QualType Ty);
496
497  /// EmitStoreThroughLValue - Store Src into Dst with same
498  /// constraints as EmitStoreThroughLValue.
499  ///
500  /// \param Result [out] - If non-null, this will be set to a Value*
501  /// for the bit-field contents after the store, appropriate for use
502  /// as the result of an assignment to the bit-field.
503  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty,
504                                      llvm::Value **Result=0);
505
506  // Note: only availabe for agg return types
507  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
508  // Note: only availabe for agg return types
509  LValue EmitCallExprLValue(const CallExpr *E);
510  LValue EmitDeclRefLValue(const DeclRefExpr *E);
511  LValue EmitStringLiteralLValue(const StringLiteral *E);
512  LValue EmitPredefinedFunctionName(unsigned Type);
513  LValue EmitPredefinedLValue(const PredefinedExpr *E);
514  LValue EmitUnaryOpLValue(const UnaryOperator *E);
515  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
516  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
517  LValue EmitMemberExpr(const MemberExpr *E);
518  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
519
520  llvm::Value *EmitIvarOffset(ObjCInterfaceDecl *Interface,
521                              const ObjCIvarDecl *Ivar);
522  LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
523                            bool isUnion, unsigned CVRQualifiers);
524  LValue EmitLValueForIvar(llvm::Value* Base, const ObjCIvarDecl *Ivar,
525                           const FieldDecl *Field,
526                           unsigned CVRQualifiers);
527
528  LValue EmitLValueForBitfield(llvm::Value* Base, FieldDecl* Field,
529                                unsigned CVRQualifiers, unsigned idx);
530
531  LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E);
532
533  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
534  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
535  LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
536  LValue EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E);
537  LValue EmitObjCSuperExpr(const ObjCSuperExpr *E);
538
539  //===--------------------------------------------------------------------===//
540  //                         Scalar Expression Emission
541  //===--------------------------------------------------------------------===//
542
543  /// EmitCall - Generate a call of the given function, expecting the
544  /// given result type, and using the given argument list which
545  /// specifies both the LLVM arguments and the types they were
546  /// derived from.
547  RValue EmitCall(const CGFunctionInfo &FnInfo,
548                  llvm::Value *Callee,
549                  const CallArgList &Args);
550
551  RValue EmitCallExpr(const CallExpr *E);
552
553  RValue EmitCallExpr(Expr *FnExpr, CallExpr::const_arg_iterator ArgBeg,
554                      CallExpr::const_arg_iterator ArgEnd);
555
556  RValue EmitCallExpr(llvm::Value *Callee, QualType FnType,
557                      CallExpr::const_arg_iterator ArgBeg,
558                      CallExpr::const_arg_iterator ArgEnd);
559
560  RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
561
562  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0
563  /// if the call is unhandled by the current target.
564  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
565
566  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
567  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
568
569  llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
570  llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
571                          bool isSplat = false);
572
573  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
574  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
575  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
576  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E);
577  RValue EmitObjCPropertyGet(const Expr *E);
578  void EmitObjCPropertySet(const Expr *E, RValue Src);
579
580
581  //===--------------------------------------------------------------------===//
582  //                           Expression Emission
583  //===--------------------------------------------------------------------===//
584
585  // Expressions are broken into three classes: scalar, complex, aggregate.
586
587  /// EmitScalarExpr - Emit the computation of the specified expression of
588  /// LLVM scalar type, returning the result.
589  llvm::Value *EmitScalarExpr(const Expr *E);
590
591  /// EmitScalarConversion - Emit a conversion from the specified type to the
592  /// specified destination type, both of which are LLVM scalar types.
593  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
594                                    QualType DstTy);
595
596  /// EmitComplexToScalarConversion - Emit a conversion from the specified
597  /// complex type to the specified destination type, where the destination
598  /// type is an LLVM scalar type.
599  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
600                                             QualType DstTy);
601
602
603  /// EmitAggExpr - Emit the computation of the specified expression of
604  /// aggregate type.  The result is computed into DestPtr.  Note that if
605  /// DestPtr is null, the value of the aggregate expression is not needed.
606  void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest);
607
608  /// EmitComplexExpr - Emit the computation of the specified expression of
609  /// complex type, returning the result.
610  ComplexPairTy EmitComplexExpr(const Expr *E);
611
612  /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
613  /// of complex type, storing into the specified Value*.
614  void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
615                               bool DestIsVolatile);
616
617  /// StoreComplexToAddr - Store a complex number into the specified address.
618  void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
619                          bool DestIsVolatile);
620  /// LoadComplexFromAddr - Load a complex number from the specified address.
621  ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
622
623  /// GenerateStaticBlockVarDecl - return the the static
624  /// declaration of local variable.
625  llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D,
626                                                bool NoInit,
627                                                const char *Separator);
628
629  // GenerateStaticBlockVarDecl - return the static declaration of
630  // a local variable. Performs initialization of the variable if necessary.
631  llvm::GlobalValue *GenerateStaticCXXBlockVarDecl(const VarDecl &D);
632
633  //===--------------------------------------------------------------------===//
634  //                             Internal Helpers
635  //===--------------------------------------------------------------------===//
636
637  /// ContainsLabel - Return true if the statement contains a label in it.  If
638  /// this statement is not executed normally, it not containing a label means
639  /// that we can just remove the code.
640  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
641
642  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
643  /// to a constant, or if it does but contains a label, return 0.  If it
644  /// constant folds to 'true' and does not contain a label, return 1, if it
645  /// constant folds to 'false' and does not contain a label, return -1.
646  int ConstantFoldsToSimpleInteger(const Expr *Cond);
647
648  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
649  /// if statement) to the specified blocks.  Based on the condition, this might
650  /// try to simplify the codegen of the conditional based on the branch.
651  ///
652  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
653                            llvm::BasicBlock *FalseBlock);
654private:
655
656  /// EmitIndirectSwitches - Emit code for all of the switch
657  /// instructions in IndirectSwitches.
658  void EmitIndirectSwitches();
659
660  void EmitReturnOfRValue(RValue RV, QualType Ty);
661
662  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
663  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
664  ///
665  /// \param AI - The first function argument of the expansion.
666  /// \return The argument following the last expanded function
667  /// argument.
668  llvm::Function::arg_iterator
669  ExpandTypeFromArgs(QualType Ty, LValue Dst,
670                     llvm::Function::arg_iterator AI);
671
672  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type
673  /// for \arg Ty, into individual arguments on the provided vector
674  /// \arg Args. See ABIArgInfo::Expand.
675  void ExpandTypeToArgs(QualType Ty, RValue Src,
676                        llvm::SmallVector<llvm::Value*, 16> &Args);
677
678  llvm::Value* EmitAsmInput(const AsmStmt &S, TargetInfo::ConstraintInfo Info,
679                            const Expr *InputExpr, std::string &ConstraintStr);
680
681};
682}  // end namespace CodeGen
683}  // end namespace clang
684
685#endif
686