Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.h - Per-Function state for LLVM CodeGen ----------===// |
| 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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | namespace llvm { |
| 24 | class Module; |
| 25 | } |
| 26 | |
| 27 | namespace clang { |
| 28 | class ASTContext; |
| 29 | class Decl; |
| 30 | class FunctionDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 31 | class ObjCMethodDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | class TargetInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | class FunctionTypeProto; |
| 34 | |
| 35 | class Stmt; |
| 36 | class CompoundStmt; |
| 37 | class LabelStmt; |
| 38 | class GotoStmt; |
| 39 | class IfStmt; |
| 40 | class WhileStmt; |
| 41 | class DoStmt; |
| 42 | class ForStmt; |
| 43 | class ReturnStmt; |
| 44 | class DeclStmt; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 45 | class CaseStmt; |
| 46 | class DefaultStmt; |
| 47 | class SwitchStmt; |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 48 | class AsmStmt; |
| 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | class Expr; |
| 51 | class DeclRefExpr; |
| 52 | class StringLiteral; |
| 53 | class IntegerLiteral; |
| 54 | class FloatingLiteral; |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 55 | class CharacterLiteral; |
Chris Lattner | 30bf3ae | 2007-08-03 17:51:03 +0000 | [diff] [blame] | 56 | class TypesCompatibleExpr; |
| 57 | |
Chris Lattner | 7016a70 | 2007-08-20 22:37:10 +0000 | [diff] [blame] | 58 | class ImplicitCastExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | class CastExpr; |
| 60 | class CallExpr; |
| 61 | class UnaryOperator; |
| 62 | class BinaryOperator; |
| 63 | class CompoundAssignOperator; |
| 64 | class ArraySubscriptExpr; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 65 | class ExtVectorElementExpr; |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 66 | class ConditionalOperator; |
Chris Lattner | 94f05e3 | 2007-08-04 00:20:15 +0000 | [diff] [blame] | 67 | class ChooseExpr; |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 68 | class PreDefinedExpr; |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 69 | class ObjCStringLiteral; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 70 | class ObjCIvarRefExpr; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 71 | class MemberExpr; |
| 72 | |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 73 | class VarDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | class EnumConstantDecl; |
| 75 | class ParmVarDecl; |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 76 | class FieldDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | namespace CodeGen { |
| 78 | class CodeGenModule; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 79 | class CodeGenTypes; |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 80 | class CGRecordLayout; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | |
| 82 | /// 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] | 83 | /// expression that is evaluated. It can be one of three things: either a |
| 84 | /// simple LLVM SSA value, a pair of SSA values for complex numbers, or the |
| 85 | /// address of an aggregate value in memory. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | class RValue { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 87 | llvm::Value *V1, *V2; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | // TODO: Encode this into the low bit of pointer for more efficient |
| 89 | // return-by-value. |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 90 | enum { Scalar, Complex, Aggregate } Flavor; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | |
| 92 | // FIXME: Aggregate rvalues need to retain information about whether they are |
| 93 | // volatile or not. |
| 94 | public: |
| 95 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 96 | bool isScalar() const { return Flavor == Scalar; } |
| 97 | bool isComplex() const { return Flavor == Complex; } |
| 98 | bool isAggregate() const { return Flavor == Aggregate; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 100 | /// getScalar() - Return the Value* of this scalar value. |
| 101 | llvm::Value *getScalarVal() const { |
| 102 | assert(isScalar() && "Not a scalar!"); |
| 103 | return V1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 106 | /// getComplexVal - Return the real/imag components of this complex value. |
| 107 | /// |
| 108 | std::pair<llvm::Value *, llvm::Value *> getComplexVal() const { |
| 109 | return std::pair<llvm::Value *, llvm::Value *>(V1, V2); |
| 110 | } |
| 111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | /// getAggregateAddr() - Return the Value* of the address of the aggregate. |
| 113 | llvm::Value *getAggregateAddr() const { |
| 114 | assert(isAggregate() && "Not an aggregate!"); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 115 | return V1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static RValue get(llvm::Value *V) { |
| 119 | RValue ER; |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 120 | ER.V1 = V; |
| 121 | ER.Flavor = Scalar; |
| 122 | return ER; |
| 123 | } |
| 124 | static RValue getComplex(llvm::Value *V1, llvm::Value *V2) { |
| 125 | RValue ER; |
| 126 | ER.V1 = V1; |
| 127 | ER.V2 = V2; |
| 128 | ER.Flavor = Complex; |
| 129 | return ER; |
| 130 | } |
| 131 | static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) { |
| 132 | RValue ER; |
| 133 | ER.V1 = C.first; |
| 134 | ER.V2 = C.second; |
| 135 | ER.Flavor = Complex; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | return ER; |
| 137 | } |
| 138 | static RValue getAggregate(llvm::Value *V) { |
| 139 | RValue ER; |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 140 | ER.V1 = V; |
| 141 | ER.Flavor = Aggregate; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | return ER; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | /// LValue - This represents an lvalue references. Because C/C++ allow |
| 148 | /// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a |
| 149 | /// bitrange. |
| 150 | class LValue { |
| 151 | // FIXME: Volatility. Restrict? |
| 152 | // alignment? |
| 153 | |
| 154 | enum { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 155 | Simple, // This is a normal l-value, use getAddress(). |
| 156 | VectorElt, // This is a vector element l-value (V[i]), use getVector* |
| 157 | BitField, // This is a bitfield l-value, use getBitfield*. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 158 | ExtVectorElt // This is an extended vector subset, use getExtVectorComp |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } LVType; |
| 160 | |
| 161 | llvm::Value *V; |
| 162 | |
| 163 | union { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 164 | // Index into a vector subscript: V[i] |
| 165 | llvm::Value *VectorIdx; |
| 166 | |
| 167 | // ExtVector element subset: V.xyx |
| 168 | llvm::Constant *VectorElts; |
| 169 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 170 | struct { |
| 171 | unsigned short StartBit; |
| 172 | unsigned short Size; |
| 173 | bool IsSigned; |
| 174 | } BitfieldData; // BitField start bit and size |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | }; |
| 176 | public: |
| 177 | bool isSimple() const { return LVType == Simple; } |
| 178 | bool isVectorElt() const { return LVType == VectorElt; } |
| 179 | bool isBitfield() const { return LVType == BitField; } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 180 | bool isExtVectorElt() const { return LVType == ExtVectorElt; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | |
| 182 | // simple lvalue |
| 183 | llvm::Value *getAddress() const { assert(isSimple()); return V; } |
| 184 | // vector elt lvalue |
| 185 | llvm::Value *getVectorAddr() const { assert(isVectorElt()); return V; } |
| 186 | llvm::Value *getVectorIdx() const { assert(isVectorElt()); return VectorIdx; } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 187 | // extended vector elements. |
| 188 | llvm::Value *getExtVectorAddr() const { assert(isExtVectorElt()); return V; } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 189 | llvm::Constant *getExtVectorElts() const { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 190 | assert(isExtVectorElt()); |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 191 | return VectorElts; |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 192 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 193 | // bitfield lvalue |
| 194 | llvm::Value *getBitfieldAddr() const { assert(isBitfield()); return V; } |
| 195 | unsigned short getBitfieldStartBit() const { |
| 196 | assert(isBitfield()); |
| 197 | return BitfieldData.StartBit; |
| 198 | } |
| 199 | unsigned short getBitfieldSize() const { |
| 200 | assert(isBitfield()); |
| 201 | return BitfieldData.Size; |
| 202 | } |
| 203 | bool isBitfieldSigned() const { |
| 204 | assert(isBitfield()); |
| 205 | return BitfieldData.IsSigned; |
| 206 | } |
| 207 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | static LValue MakeAddr(llvm::Value *V) { |
| 209 | LValue R; |
| 210 | R.LVType = Simple; |
| 211 | R.V = V; |
| 212 | return R; |
| 213 | } |
| 214 | |
| 215 | static LValue MakeVectorElt(llvm::Value *Vec, llvm::Value *Idx) { |
| 216 | LValue R; |
| 217 | R.LVType = VectorElt; |
| 218 | R.V = Vec; |
| 219 | R.VectorIdx = Idx; |
| 220 | return R; |
| 221 | } |
| 222 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 223 | static LValue MakeExtVectorElt(llvm::Value *Vec, llvm::Constant *Elts) { |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 224 | LValue R; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 225 | R.LVType = ExtVectorElt; |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 226 | R.V = Vec; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 227 | R.VectorElts = Elts; |
Chris Lattner | 349aaec | 2007-08-02 23:37:31 +0000 | [diff] [blame] | 228 | return R; |
| 229 | } |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 230 | |
| 231 | static LValue MakeBitfield(llvm::Value *V, unsigned short StartBit, |
| 232 | unsigned short Size, bool IsSigned) { |
| 233 | LValue R; |
| 234 | R.LVType = BitField; |
| 235 | R.V = V; |
| 236 | R.BitfieldData.StartBit = StartBit; |
| 237 | R.BitfieldData.Size = Size; |
| 238 | R.BitfieldData.IsSigned = IsSigned; |
| 239 | return R; |
| 240 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | /// CodeGenFunction - This class organizes the per-function state that is used |
| 244 | /// while generating LLVM code. |
| 245 | class CodeGenFunction { |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 246 | public: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | CodeGenModule &CGM; // Per-module state. |
| 248 | TargetInfo &Target; |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 250 | typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; |
Chris Lattner | 50b3674 | 2008-04-13 07:32:11 +0000 | [diff] [blame] | 251 | llvm::IRBuilder Builder; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 253 | // Holds the Decl for the current function or method |
| 254 | const Decl *CurFuncDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 255 | QualType FnRetTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | llvm::Function *CurFn; |
| 257 | |
| 258 | /// AllocaInsertPoint - This is an instruction in the entry block before which |
| 259 | /// we prefer to insert allocas. |
| 260 | llvm::Instruction *AllocaInsertPt; |
| 261 | |
| 262 | const llvm::Type *LLVMIntTy; |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 263 | uint32_t LLVMPointerWidth; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 265 | private: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C |
| 267 | /// decls. |
| 268 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
| 269 | |
| 270 | /// LabelMap - This keeps track of the LLVM basic block for each C label. |
| 271 | llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 272 | |
| 273 | // BreakContinueStack - This keeps track of where break and continue |
| 274 | // statements should jump to. |
| 275 | struct BreakContinue { |
| 276 | BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb) |
| 277 | : BreakBlock(bb), ContinueBlock(cb) {} |
| 278 | |
| 279 | llvm::BasicBlock *BreakBlock; |
| 280 | llvm::BasicBlock *ContinueBlock; |
| 281 | }; |
| 282 | llvm::SmallVector<BreakContinue, 8> BreakContinueStack; |
| 283 | |
Devang Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 284 | /// SwitchInsn - This is nearest current switch instruction. It is null if |
| 285 | /// if current context is not in a switch. |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 286 | llvm::SwitchInst *SwitchInsn; |
| 287 | |
Devang Patel | 80fd5f9 | 2007-10-09 17:08:50 +0000 | [diff] [blame] | 288 | /// CaseRangeBlock - This block holds if condition check for last case |
| 289 | /// statement range in current switch instruction. |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 290 | llvm::BasicBlock *CaseRangeBlock; |
| 291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | public: |
| 293 | CodeGenFunction(CodeGenModule &cgm); |
| 294 | |
| 295 | ASTContext &getContext() const; |
| 296 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 297 | void GenerateObjCMethod(const ObjCMethodDecl *OMD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | void GenerateCode(const FunctionDecl *FD); |
| 299 | |
| 300 | const llvm::Type *ConvertType(QualType T); |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 301 | |
| 302 | llvm::Value *LoadObjCSelf(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | |
| 304 | /// hasAggregateLLVMType - Return true if the specified AST type will map into |
| 305 | /// an aggregate LLVM type or is void. |
| 306 | static bool hasAggregateLLVMType(QualType T); |
| 307 | |
| 308 | /// getBasicBlockForLabel - Return the LLVM basicblock that the specified |
| 309 | /// label maps to. |
| 310 | llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); |
| 311 | |
| 312 | |
| 313 | void EmitBlock(llvm::BasicBlock *BB); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 314 | |
| 315 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 316 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 317 | void WarnUnsupported(const Stmt *S, const char *Type); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | |
| 319 | //===--------------------------------------------------------------------===// |
| 320 | // Helpers |
| 321 | //===--------------------------------------------------------------------===// |
| 322 | |
| 323 | /// CreateTempAlloca - This creates a alloca and inserts it into the entry |
| 324 | /// block. |
| 325 | llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, |
| 326 | const char *Name = "tmp"); |
| 327 | |
| 328 | /// EvaluateExprAsBool - Perform the usual unary conversions on the specified |
| 329 | /// expression and compare the result against zero, returning an Int1Ty value. |
| 330 | llvm::Value *EvaluateExprAsBool(const Expr *E); |
| 331 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 332 | /// EmitAnyExpr - Emit code to compute the specified expression which can have |
| 333 | /// any type. The result is returned as an RValue struct. If this is an |
| 334 | /// aggregate expression, the aggloc/agglocvolatile arguments indicate where |
| 335 | /// the result should be returned. |
| 336 | RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0, |
| 337 | bool isAggLocVolatile = false); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 338 | |
| 339 | /// isDummyBlock - Return true if BB is an empty basic block |
| 340 | /// with no predecessors. |
| 341 | static bool isDummyBlock(const llvm::BasicBlock *BB); |
| 342 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 343 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 344 | /// then reuse it. |
| 345 | void StartBlock(const char *N); |
| 346 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 347 | /// getCGRecordLayout - Return record layout info. |
| 348 | const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy); |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 349 | |
| 350 | /// GetAddrOfStaticLocalVar - Return the address of a static local variable. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 351 | llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | //===--------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | // Declaration Emission |
| 354 | //===--------------------------------------------------------------------===// |
| 355 | |
| 356 | void EmitDecl(const Decl &D); |
| 357 | void EmitEnumConstantDecl(const EnumConstantDecl &D); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 358 | void EmitBlockVarDecl(const VarDecl &D); |
| 359 | void EmitLocalBlockVarDecl(const VarDecl &D); |
| 360 | void EmitStaticBlockVarDecl(const VarDecl &D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | void EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg); |
| 362 | |
| 363 | //===--------------------------------------------------------------------===// |
| 364 | // Statement Emission |
| 365 | //===--------------------------------------------------------------------===// |
| 366 | |
| 367 | void EmitStmt(const Stmt *S); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 368 | RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, |
| 369 | llvm::Value *AggLoc = 0, bool isAggVol = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | void EmitLabelStmt(const LabelStmt &S); |
| 371 | void EmitGotoStmt(const GotoStmt &S); |
| 372 | void EmitIfStmt(const IfStmt &S); |
| 373 | void EmitWhileStmt(const WhileStmt &S); |
| 374 | void EmitDoStmt(const DoStmt &S); |
| 375 | void EmitForStmt(const ForStmt &S); |
| 376 | void EmitReturnStmt(const ReturnStmt &S); |
| 377 | void EmitDeclStmt(const DeclStmt &S); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 378 | void EmitBreakStmt(); |
| 379 | void EmitContinueStmt(); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 380 | void EmitSwitchStmt(const SwitchStmt &S); |
| 381 | void EmitDefaultStmt(const DefaultStmt &S); |
| 382 | void EmitCaseStmt(const CaseStmt &S); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 383 | void EmitCaseStmtRange(const CaseStmt &S); |
Anders Carlsson | fb1aeb8 | 2008-02-05 16:35:33 +0000 | [diff] [blame] | 384 | void EmitAsmStmt(const AsmStmt &S); |
| 385 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | //===--------------------------------------------------------------------===// |
| 387 | // LValue Expression Emission |
| 388 | //===--------------------------------------------------------------------===// |
| 389 | |
| 390 | /// EmitLValue - Emit code to compute a designator that specifies the location |
| 391 | /// of the expression. |
| 392 | /// |
| 393 | /// This can return one of two things: a simple address or a bitfield |
| 394 | /// reference. In either case, the LLVM Value* in the LValue structure is |
| 395 | /// guaranteed to be an LLVM pointer type. |
| 396 | /// |
| 397 | /// If this returns a bitfield reference, nothing about the pointee type of |
| 398 | /// the LLVM value is known: For example, it may not be a pointer to an |
| 399 | /// integer. |
| 400 | /// |
| 401 | /// If this returns a normal address, and if the lvalue's C type is fixed |
| 402 | /// size, this method guarantees that the returned pointer type will point to |
| 403 | /// an LLVM type of the same size of the lvalue's type. If the lvalue has a |
| 404 | /// variable length type, this is not possible. |
| 405 | /// |
| 406 | LValue EmitLValue(const Expr *E); |
| 407 | |
| 408 | /// EmitLoadOfLValue - Given an expression that represents a value lvalue, |
| 409 | /// this method emits the address of the lvalue, then loads the result as an |
| 410 | /// rvalue, returning the rvalue. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | RValue EmitLoadOfLValue(LValue V, QualType LVType); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 412 | RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType); |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 413 | RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 34cdc86 | 2007-08-03 16:18:34 +0000 | [diff] [blame] | 415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | /// EmitStoreThroughLValue - Store the specified rvalue into the specified |
| 417 | /// lvalue, where both are guaranteed to the have the same type, and that type |
| 418 | /// is 'Ty'. |
| 419 | void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 420 | void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst, |
| 421 | QualType Ty); |
Lauro Ramos Venancio | a0c5d0e | 2008-01-22 22:36:45 +0000 | [diff] [blame] | 422 | void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty); |
Christopher Lamb | 22c940e | 2007-12-29 05:02:41 +0000 | [diff] [blame] | 423 | |
| 424 | // Note: only availabe for agg return types |
| 425 | LValue EmitCallExprLValue(const CallExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | |
| 427 | LValue EmitDeclRefLValue(const DeclRefExpr *E); |
| 428 | LValue EmitStringLiteralLValue(const StringLiteral *E); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 429 | LValue EmitPreDefinedLValue(const PreDefinedExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | LValue EmitUnaryOpLValue(const UnaryOperator *E); |
| 431 | LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 432 | LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 433 | LValue EmitMemberExpr(const MemberExpr *E); |
Eli Friedman | 472778e | 2008-02-09 08:50:58 +0000 | [diff] [blame] | 434 | |
| 435 | LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field, |
| 436 | bool isUnion); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 437 | |
| 438 | LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | //===--------------------------------------------------------------------===// |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 440 | // Scalar Expression Emission |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 441 | //===--------------------------------------------------------------------===// |
| 442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | RValue EmitCallExpr(const CallExpr *E); |
Eli Friedman | 5193b8a | 2008-01-30 01:32:06 +0000 | [diff] [blame] | 444 | RValue EmitCallExpr(Expr *FnExpr, Expr *const *Args, unsigned NumArgs); |
| 445 | RValue EmitCallExpr(llvm::Value *Callee, QualType FnType, |
| 446 | Expr *const *Args, unsigned NumArgs); |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 447 | RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | |
Anders Carlsson | 564f1de | 2007-12-09 23:17:02 +0000 | [diff] [blame] | 449 | llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 450 | llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); |
| 451 | |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 452 | llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 453 | llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals, |
| 454 | bool isSplat = false); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 456 | llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 458 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 459 | // Expression Emission |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 460 | //===--------------------------------------------------------------------===// |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 461 | |
| 462 | // Expressions are broken into three classes: scalar, complex, aggregate. |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 464 | /// EmitScalarExpr - Emit the computation of the specified expression of |
| 465 | /// LLVM scalar type, returning the result. |
| 466 | llvm::Value *EmitScalarExpr(const Expr *E); |
| 467 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 468 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 469 | /// specified destination type, both of which are LLVM scalar types. |
| 470 | llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, |
| 471 | QualType DstTy); |
| 472 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 473 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 474 | /// complex type to the specified destination type, where the destination |
| 475 | /// type is an LLVM scalar type. |
| 476 | llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, |
| 477 | QualType DstTy); |
| 478 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 480 | /// EmitAggExpr - Emit the computation of the specified expression of |
| 481 | /// aggregate type. The result is computed into DestPtr. Note that if |
| 482 | /// DestPtr is null, the value of the aggregate expression is not needed. |
| 483 | void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 484 | |
| 485 | /// EmitComplexExpr - Emit the computation of the specified expression of |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 486 | /// complex type, returning the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 487 | ComplexPairTy EmitComplexExpr(const Expr *E); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 488 | |
| 489 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 490 | /// of complex type, storing into the specified Value*. |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 491 | void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, |
| 492 | bool DestIsVolatile); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 493 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
| 494 | ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 495 | |
| 496 | /// GenerateStaticBlockVarDecl - return the the static |
| 497 | /// declaration of local variable. |
| 498 | llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D, |
| 499 | bool NoInit, |
| 500 | const char *Separator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | }; |
| 502 | } // end namespace CodeGen |
| 503 | } // end namespace clang |
| 504 | |
| 505 | #endif |