Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1 | //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the internal per-function state used for llvm translation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | ef52a2f | 2008-02-29 17:10:38 +0000 | [diff] [blame] | 14 | #ifndef CLANG_CODEGEN_CODEGENFUNCTION_H |
| 15 | #define CLANG_CODEGEN_CODEGENFUNCTION_H |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 17 | #include "clang/AST/Type.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 50b3674 | 2008-04-13 07:32:11 +0000 | [diff] [blame] | 20 | #include "llvm/Support/IRBuilder.h" |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/ExprObjC.h" |
| 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include <vector> |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 25 | #include <map> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | class Module; |
| 29 | } |
| 30 | |
| 31 | namespace clang { |
| 32 | class ASTContext; |
| 33 | class Decl; |
| 34 | class FunctionDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 35 | class ObjCMethodDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | class TargetInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | class FunctionTypeProto; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 38 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | namespace CodeGen { |
| 40 | class CodeGenModule; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 41 | class CodeGenTypes; |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 42 | class CGRecordLayout; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
| 44 | /// RValue - This trivial value class is used to represent the result of an |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 45 | /// 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | class RValue { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 49 | llvm::Value *V1, *V2; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | // TODO: Encode this into the low bit of pointer for more efficient |
| 51 | // return-by-value. |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 52 | enum { Scalar, Complex, Aggregate } Flavor; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | |
| 54 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 55 | // volatile or not. |
| 56 | public: |
| 57 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 58 | bool isScalar() const { return Flavor == Scalar; } |
| 59 | bool isComplex() const { return Flavor == Complex; } |
| 60 | bool isAggregate() const { return Flavor == Aggregate; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 62 | /// getScalar() - Return the Value* of this scalar value. |
| 63 | llvm::Value *getScalarVal() const { |
| 64 | assert(isScalar() && "Not a scalar!"); |
| 65 | return V1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 68 | /// 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 75 | llvm::Value *getAggregateAddr() const { |
| 76 | assert(isAggregate() && "Not an aggregate!"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 77 | return V1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static RValue get(llvm::Value *V) { |
| 81 | RValue ER; |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 82 | 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | return ER; |
| 99 | } |
| 100 | static RValue getAggregate(llvm::Value *V) { |
| 101 | RValue ER; |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 102 | ER.V1 = V; |
| 103 | ER.Flavor = Aggregate; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | 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. |
| 112 | class LValue { |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 113 | // FIXME: alignment? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | |
| 115 | enum { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 116 | 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 Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 119 | ExtVectorElt // This is an extended vector subset, use getExtVectorComp |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } LVType; |
| 121 | |
| 122 | llvm::Value *V; |
| 123 | |
| 124 | union { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 125 | // Index into a vector subscript: V[i] |
| 126 | llvm::Value *VectorIdx; |
| 127 | |
| 128 | // ExtVector element subset: V.xyx |
| 129 | llvm::Constant *VectorElts; |
| 130 | |
Nate Begeman | 69ce1df | 2008-07-25 20:15:41 +0000 | [diff] [blame] | 131 | // BitField start bit and size |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 132 | struct { |
| 133 | unsigned short StartBit; |
| 134 | unsigned short Size; |
| 135 | bool IsSigned; |
Nate Begeman | 69ce1df | 2008-07-25 20:15:41 +0000 | [diff] [blame] | 136 | } BitfieldData; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | }; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 138 | |
| 139 | bool Volatile:1; |
| 140 | // FIXME: set but never used, what effect should it have? |
| 141 | bool Restrict:1; |
| 142 | |
| 143 | private: |
| 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | public: |
| 150 | bool isSimple() const { return LVType == Simple; } |
| 151 | bool isVectorElt() const { return LVType == VectorElt; } |
| 152 | bool isBitfield() const { return LVType == BitField; } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 153 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 155 | bool isVolatileQualified() const { return Volatile; } |
| 156 | bool isRestrictQualified() const { return Restrict; } |
| 157 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | // 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 Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 163 | // extended vector elements. |
| 164 | llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 165 | llvm::Constant *getExtVectorElts() const { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 166 | assert(isExtVectorElt()); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 167 | return VectorElts; |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 168 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 169 | // 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 Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 184 | static LValue MakeAddr(llvm::Value *V, unsigned Qualifiers) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | LValue R; |
| 186 | R.LVType = Simple; |
| 187 | R.V = V; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 188 | SetQualifiers(Qualifiers,R); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | return R; |
| 190 | } |
| 191 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 192 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx, |
| 193 | unsigned Qualifiers) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | LValue R; |
| 195 | R.LVType = VectorElt; |
| 196 | R.V = Vec; |
| 197 | R.VectorIdx = Idx; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 198 | SetQualifiers(Qualifiers,R); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | return R; |
| 200 | } |
| 201 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 202 | static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts, |
| 203 | unsigned Qualifiers) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 204 | LValue R; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 205 | R.LVType = ExtVectorElt; |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 206 | R.V = Vec; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 207 | R.VectorElts = Elts; |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 208 | SetQualifiers(Qualifiers,R); |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 209 | return R; |
| 210 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 211 | |
| 212 | static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit, |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 213 | unsigned short Size, bool IsSigned, |
| 214 | unsigned Qualifiers) { |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 215 | 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 Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 221 | SetQualifiers(Qualifiers,R); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 222 | return R; |
| 223 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 227 | /// while generating LLVM code. |
| 228 | class CodeGenFunction { |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 229 | public: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | CodeGenModule &CGM; // Per-module state. |
| 231 | TargetInfo &Target; |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 233 | typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; |
Chris Lattner | 50b3674 | 2008-04-13 07:32:11 +0000 | [diff] [blame] | 234 | llvm::IRBuilder Builder; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 236 | // Holds the Decl for the current function or method |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 237 | const Decl *CurFuncDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 238 | QualType FnRetTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | 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 Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 244 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | const llvm::Type *LLVMIntTy; |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 246 | uint32_t LLVMPointerWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 248 | private: |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 249 | /// 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | /// 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 265 | |
| 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 Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 277 | /// SwitchInsn - This is nearest current switch instruction. It is null if |
| 278 | /// if current context is not in a switch. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 279 | llvm::SwitchInst *SwitchInsn; |
| 280 | |
Devang Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 281 | /// CaseRangeBlock - This block holds if condition check for last case |
| 282 | /// statement range in current switch instruction. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 283 | llvm::BasicBlock *CaseRangeBlock; |
| 284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | public: |
| 286 | CodeGenFunction(CodeGenModule &cgm); |
| 287 | |
| 288 | ASTContext &getContext() const; |
| 289 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 290 | void GenerateObjCMethod(const ObjCMethodDecl *OMD); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 291 | void GenerateCode(const FunctionDecl *FD, |
| 292 | llvm::Function *Fn); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 293 | void GenerateFunction(const Stmt *Body); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | |
| 295 | const llvm::Type *ConvertType(QualType T); |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 296 | |
| 297 | llvm::Value *LoadObjCSelf(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 298 | |
| 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | /// 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 Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 313 | |
| 314 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 315 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 316 | void WarnUnsupported(const Stmt *S, const char *Type); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | |
| 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 Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 331 | /// 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 Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 337 | |
| 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 Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 342 | /// 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 Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 346 | /// getCGRecordLayout - Return record layout info. |
| 347 | const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy); |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 348 | |
| 349 | /// GetAddrOfStaticLocalVar - Return the address of a static local variable. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 350 | llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 356 | unsigned GetIDForAddrOfLabel(const LabelStmt *L); |
| 357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | //===--------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | // Declaration Emission |
| 360 | //===--------------------------------------------------------------------===// |
| 361 | |
| 362 | void EmitDecl(const Decl &D); |
| 363 | void EmitEnumConstantDecl(const EnumConstantDecl &D); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 364 | void EmitBlockVarDecl(const VarDecl &D); |
| 365 | void EmitLocalBlockVarDecl(const VarDecl &D); |
| 366 | void EmitStaticBlockVarDecl(const VarDecl &D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg); |
| 368 | |
| 369 | //===--------------------------------------------------------------------===// |
| 370 | // Statement Emission |
| 371 | //===--------------------------------------------------------------------===// |
| 372 | |
| 373 | void EmitStmt(const Stmt *S); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 374 | RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, |
| 375 | llvm::Value *AggLoc = 0, bool isAggVol = false); |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 376 | void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | void EmitLabelStmt(const LabelStmt &S); |
| 378 | void EmitGotoStmt(const GotoStmt &S); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 379 | void EmitIndirectGotoStmt(const IndirectGotoStmt &S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 386 | void EmitBreakStmt(); |
| 387 | void EmitContinueStmt(); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 388 | void EmitSwitchStmt(const SwitchStmt &S); |
| 389 | void EmitDefaultStmt(const DefaultStmt &S); |
| 390 | void EmitCaseStmt(const CaseStmt &S); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 391 | void EmitCaseStmtRange(const CaseStmt &S); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 392 | void EmitAsmStmt(const AsmStmt &S); |
| 393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | //===--------------------------------------------------------------------===// |
| 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | RValue EmitLoadOfLValue(LValue V, QualType LVType); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 420 | RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 421 | RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 423 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | /// 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 Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 428 | void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst, |
| 429 | QualType Ty); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 430 | void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty); |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 431 | |
| 432 | // Note: only availabe for agg return types |
| 433 | LValue EmitCallExprLValue(const CallExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | |
| 435 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
| 436 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 437 | LValue EmitPreDefinedLValue(const PreDefinedExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
| 439 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 440 | LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 441 | LValue EmitMemberExpr(const MemberExpr *E); |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 442 | LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 443 | |
| 444 | LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field, |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 445 | bool isUnion, unsigned CVRQualifiers); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 446 | |
| 447 | LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | //===--------------------------------------------------------------------===// |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 449 | // Scalar Expression Emission |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 450 | //===--------------------------------------------------------------------===// |
| 451 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | RValue EmitCallExpr(const CallExpr *E); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 453 | |
| 454 | RValue EmitCallExpr(Expr *FnExpr, CallExpr::const_arg_iterator ArgBeg, |
| 455 | CallExpr::const_arg_iterator ArgEnd); |
| 456 | |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 457 | RValue EmitCallExpr(llvm::Value *Callee, QualType FnType, |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 458 | CallExpr::const_arg_iterator ArgBeg, |
| 459 | CallExpr::const_arg_iterator ArgEnd); |
| 460 | |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 461 | RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 462 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 463 | llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 464 | llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 465 | |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 466 | llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 467 | llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals, |
| 468 | bool isSplat = false); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 470 | llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 471 | llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); |
| 472 | llvm::Value *EmitObjCMessageExpr(const ObjCMessageExpr *E); |
| 473 | |
| 474 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 476 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 477 | // Expression Emission |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 478 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 479 | |
| 480 | // Expressions are broken into three classes: scalar, complex, aggregate. |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 482 | /// 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 Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 486 | /// 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 Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 491 | /// 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 Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 498 | /// 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 Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 502 | |
| 503 | /// EmitComplexExpr - Emit the computation of the specified expression of |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 504 | /// complex type, returning the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 505 | ComplexPairTy EmitComplexExpr(const Expr *E); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 506 | |
| 507 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 508 | /// of complex type, storing into the specified Value*. |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 509 | void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, |
| 510 | bool DestIsVolatile); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 511 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
| 512 | ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 513 | |
| 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 Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 519 | |
| 520 | //===--------------------------------------------------------------------===// |
| 521 | // Internal Helpers |
| 522 | //===--------------------------------------------------------------------===// |
| 523 | |
| 524 | private: |
| 525 | /// EmitIndirectSwitches - Emit code for all of the switch |
| 526 | /// instructions in IndirectSwitches. |
| 527 | void EmitIndirectSwitches(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | }; |
| 529 | } // end namespace CodeGen |
| 530 | } // end namespace clang |
| 531 | |
| 532 | #endif |