Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 1 | //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// |
| 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 contains code to emit Constant Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/GlobalVariable.h" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | namespace { |
| 25 | class VISIBILITY_HIDDEN ConstExprEmitter : public StmtVisitor<ConstExprEmitter, llvm::Constant*> { |
| 26 | CodeGenModule &CGM; |
| 27 | public: |
| 28 | ConstExprEmitter(CodeGenModule &cgm) |
| 29 | : CGM(cgm) { |
| 30 | } |
| 31 | |
| 32 | //===--------------------------------------------------------------------===// |
| 33 | // Visitor Methods |
| 34 | //===--------------------------------------------------------------------===// |
| 35 | |
| 36 | llvm::Constant *VisitStmt(Stmt *S) { |
| 37 | CGM.WarnUnsupported(S, "constant expression"); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | llvm::Constant *VisitParenExpr(ParenExpr *PE) { |
| 42 | return Visit(PE->getSubExpr()); |
| 43 | } |
| 44 | |
| 45 | // Leaves |
| 46 | llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 47 | return llvm::ConstantInt::get(E->getValue()); |
| 48 | } |
| 49 | llvm::Constant *VisitFloatingLiteral(const FloatingLiteral *E) { |
| 50 | return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue()); |
| 51 | } |
| 52 | llvm::Constant *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 53 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 54 | } |
| 55 | llvm::Constant *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 56 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 57 | } |
| 58 | |
| 59 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 60 | return Visit(E->getInitializer()); |
| 61 | } |
| 62 | |
| 63 | llvm::Constant *VisitCastExpr(const CastExpr* E) { |
| 64 | llvm::Constant *C = Visit(E->getSubExpr()); |
| 65 | |
| 66 | return EmitConversion(C, E->getSubExpr()->getType(), E->getType()); |
| 67 | } |
| 68 | |
| 69 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { |
| 70 | if (ILE->getType()->isVoidType()) { |
| 71 | // FIXME: Remove this when sema of initializers is finished (and the code |
| 72 | // below). |
| 73 | CGM.WarnUnsupported(ILE, "initializer"); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | assert((ILE->getType()->isArrayType() || ILE->getType()->isStructureType() || |
| 78 | ILE->getType()->isVectorType()) && |
| 79 | "Bad type for init list!"); |
| 80 | CodeGenTypes& Types = CGM.getTypes(); |
| 81 | |
| 82 | unsigned NumInitElements = ILE->getNumInits(); |
| 83 | unsigned NumInitableElts = NumInitElements; |
| 84 | |
| 85 | const llvm::CompositeType *CType = |
| 86 | cast<llvm::CompositeType>(Types.ConvertType(ILE->getType())); |
| 87 | assert(CType); |
| 88 | std::vector<llvm::Constant*> Elts; |
| 89 | |
| 90 | // Initialising an array requires us to automatically initialise any |
| 91 | // elements that have not been initialised explicitly |
| 92 | const llvm::ArrayType *AType = 0; |
| 93 | const llvm::Type *AElemTy = 0; |
| 94 | unsigned NumArrayElements = 0; |
| 95 | |
| 96 | // If this is an array, we may have to truncate the initializer |
| 97 | if ((AType = dyn_cast<llvm::ArrayType>(CType))) { |
| 98 | NumArrayElements = AType->getNumElements(); |
| 99 | AElemTy = AType->getElementType(); |
| 100 | NumInitableElts = std::min(NumInitableElts, NumArrayElements); |
| 101 | } |
| 102 | |
| 103 | // Copy initializer elements. |
| 104 | unsigned i = 0; |
| 105 | for (i = 0; i < NumInitableElts; ++i) { |
| 106 | llvm::Constant *C = Visit(ILE->getInit(i)); |
| 107 | // FIXME: Remove this when sema of initializers is finished (and the code |
| 108 | // above). |
| 109 | if (C == 0 && ILE->getInit(i)->getType()->isVoidType()) { |
| 110 | if (ILE->getType()->isVoidType()) return 0; |
| 111 | return llvm::UndefValue::get(CType); |
| 112 | } |
| 113 | assert (C && "Failed to create initializer expression"); |
| 114 | Elts.push_back(C); |
| 115 | } |
| 116 | |
| 117 | if (ILE->getType()->isStructureType()) |
| 118 | return llvm::ConstantStruct::get(cast<llvm::StructType>(CType), Elts); |
| 119 | |
| 120 | if (ILE->getType()->isVectorType()) |
| 121 | return llvm::ConstantVector::get(cast<llvm::VectorType>(CType), Elts); |
| 122 | |
| 123 | // Make sure we have an array at this point |
| 124 | assert(AType); |
| 125 | |
| 126 | // Initialize remaining array elements. |
| 127 | for (; i < NumArrayElements; ++i) |
| 128 | Elts.push_back(llvm::Constant::getNullValue(AElemTy)); |
| 129 | |
| 130 | return llvm::ConstantArray::get(AType, Elts); |
| 131 | } |
| 132 | |
| 133 | llvm::Constant *VisitImplicitCastExpr(ImplicitCastExpr *ICExpr) { |
| 134 | // If this is due to array->pointer conversion, emit the array expression as |
| 135 | // an l-value. |
| 136 | if (ICExpr->getSubExpr()->getType()->isArrayType()) { |
| 137 | // Note that VLAs can't exist for global variables. |
| 138 | // The only thing that can have array type like this is a |
| 139 | // DeclRefExpr(FileVarDecl)? |
| 140 | const DeclRefExpr *DRE = cast<DeclRefExpr>(ICExpr->getSubExpr()); |
| 141 | const VarDecl *VD = cast<VarDecl>(DRE->getDecl()); |
| 142 | llvm::Constant *C = CGM.GetAddrOfGlobalVar(VD, false); |
| 143 | assert(isa<llvm::PointerType>(C->getType()) && |
| 144 | isa<llvm::ArrayType>(cast<llvm::PointerType>(C->getType()) |
| 145 | ->getElementType())); |
| 146 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 147 | |
| 148 | llvm::Constant *Ops[] = {Idx0, Idx0}; |
| 149 | C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
| 150 | |
| 151 | // The resultant pointer type can be implicitly cast to other pointer |
| 152 | // types as well, for example void*. |
| 153 | const llvm::Type *DestPTy = ConvertType(ICExpr->getType()); |
| 154 | assert(isa<llvm::PointerType>(DestPTy) && |
| 155 | "Only expect implicit cast to pointer"); |
| 156 | return llvm::ConstantExpr::getBitCast(C, DestPTy); |
| 157 | } |
| 158 | |
| 159 | llvm::Constant *C = Visit(ICExpr->getSubExpr()); |
| 160 | |
| 161 | return EmitConversion(C, ICExpr->getSubExpr()->getType(),ICExpr->getType()); |
| 162 | } |
| 163 | |
| 164 | llvm::Constant *VisitStringLiteral(StringLiteral *E) { |
| 165 | const char *StrData = E->getStrData(); |
| 166 | unsigned Len = E->getByteLength(); |
| 167 | |
| 168 | // If the string has a pointer type, emit it as a global and use the pointer |
| 169 | // to the global as its value. |
| 170 | if (E->getType()->isPointerType()) |
| 171 | return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); |
| 172 | |
| 173 | // Otherwise this must be a string initializing an array in a static |
| 174 | // initializer. Don't emit it as the address of the string, emit the string |
| 175 | // data itself as an inline array. |
| 176 | const ConstantArrayType *CAT = E->getType()->getAsConstantArrayType(); |
| 177 | assert(CAT && "String isn't pointer or array!"); |
| 178 | |
| 179 | std::string Str(StrData, StrData + Len); |
| 180 | // Null terminate the string before potentially truncating it. |
| 181 | // FIXME: What about wchar_t strings? |
| 182 | Str.push_back(0); |
| 183 | |
| 184 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 185 | // String or grow the initializer to the required size. |
| 186 | if (RealLen != Str.size()) |
| 187 | Str.resize(RealLen); |
| 188 | |
| 189 | return llvm::ConstantArray::get(Str, false); |
| 190 | } |
| 191 | |
| 192 | llvm::Constant *VisitDeclRefExpr(DeclRefExpr *E) { |
| 193 | const ValueDecl *Decl = E->getDecl(); |
| 194 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
| 195 | return CGM.GetAddrOfFunctionDecl(FD, false); |
| 196 | assert(0 && "Unsupported decl ref type!"); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | llvm::Constant *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 201 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 202 | } |
| 203 | |
| 204 | // Unary operators |
| 205 | llvm::Constant *VisitUnaryPlus(const UnaryOperator *E) { |
| 206 | return Visit(E->getSubExpr()); |
| 207 | } |
| 208 | llvm::Constant *VisitUnaryMinus(const UnaryOperator *E) { |
| 209 | return llvm::ConstantExpr::getNeg(Visit(E->getSubExpr())); |
| 210 | } |
| 211 | llvm::Constant *VisitUnaryNot(const UnaryOperator *E) { |
| 212 | return llvm::ConstantExpr::getNot(Visit(E->getSubExpr())); |
| 213 | } |
| 214 | llvm::Constant *VisitUnaryLNot(const UnaryOperator *E) { |
| 215 | llvm::Constant *SubExpr = Visit(E->getSubExpr()); |
| 216 | |
| 217 | if (E->getSubExpr()->getType()->isRealFloatingType()) { |
| 218 | // Compare against 0.0 for fp scalars. |
| 219 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 220 | SubExpr = llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UEQ, SubExpr, |
| 221 | Zero); |
| 222 | } else { |
| 223 | assert((E->getSubExpr()->getType()->isIntegerType() || |
| 224 | E->getSubExpr()->getType()->isPointerType()) && |
| 225 | "Unknown scalar type to convert"); |
| 226 | // Compare against an integer or pointer null. |
| 227 | llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType()); |
| 228 | SubExpr = llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, SubExpr, |
| 229 | Zero); |
| 230 | } |
| 231 | |
| 232 | return llvm::ConstantExpr::getZExt(SubExpr, ConvertType(E->getType())); |
| 233 | } |
| 234 | llvm::Constant *VisitUnarySizeOf(const UnaryOperator *E) { |
| 235 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 236 | } |
| 237 | llvm::Constant *VisitUnaryAlignOf(const UnaryOperator *E) { |
| 238 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 239 | } |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame^] | 240 | llvm::Constant *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 241 | return EmitLValue(E->getSubExpr()); |
| 242 | } |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 243 | |
| 244 | // Utility methods |
| 245 | const llvm::Type *ConvertType(QualType T) { |
| 246 | return CGM.getTypes().ConvertType(T); |
| 247 | } |
| 248 | |
| 249 | llvm::Constant *EmitConversionToBool(llvm::Constant *Src, QualType SrcType) { |
| 250 | assert(SrcType->isCanonical() && "EmitConversion strips typedefs"); |
| 251 | |
| 252 | if (SrcType->isRealFloatingType()) { |
| 253 | // Compare against 0.0 for fp scalars. |
| 254 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 255 | return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Src, Zero); |
| 256 | } |
| 257 | |
| 258 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 259 | "Unknown scalar type to convert"); |
| 260 | |
| 261 | // Compare against an integer or pointer null. |
| 262 | llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 263 | return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Src, Zero); |
| 264 | } |
| 265 | |
| 266 | llvm::Constant *EmitConversion(llvm::Constant *Src, QualType SrcType, |
| 267 | QualType DstType) { |
| 268 | SrcType = SrcType.getCanonicalType(); |
| 269 | DstType = DstType.getCanonicalType(); |
| 270 | if (SrcType == DstType) return Src; |
| 271 | |
| 272 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 273 | if (DstType->isBooleanType()) |
| 274 | return EmitConversionToBool(Src, SrcType); |
| 275 | |
| 276 | const llvm::Type *DstTy = ConvertType(DstType); |
| 277 | |
| 278 | // Ignore conversions like int -> uint. |
| 279 | if (Src->getType() == DstTy) |
| 280 | return Src; |
| 281 | |
| 282 | // Handle pointer conversions next: pointers can only be converted to/from |
| 283 | // other pointers and integers. |
| 284 | if (isa<PointerType>(DstType)) { |
| 285 | // The source value may be an integer, or a pointer. |
| 286 | if (isa<llvm::PointerType>(Src->getType())) |
| 287 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
| 288 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 289 | return llvm::ConstantExpr::getIntToPtr(Src, DstTy); |
| 290 | } |
| 291 | |
| 292 | if (isa<PointerType>(SrcType)) { |
| 293 | // Must be an ptr to int cast. |
| 294 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
| 295 | return llvm::ConstantExpr::getPtrToInt(Src, DstTy); |
| 296 | } |
| 297 | |
| 298 | // A scalar source can be splatted to a vector of the same element type |
| 299 | if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) { |
| 300 | const llvm::VectorType *VT = cast<llvm::VectorType>(DstTy); |
| 301 | assert((VT->getElementType() == Src->getType()) && |
| 302 | "Vector element type must match scalar type to splat."); |
| 303 | unsigned NumElements = DstType->getAsVectorType()->getNumElements(); |
| 304 | llvm::SmallVector<llvm::Constant*, 16> Elements; |
| 305 | for (unsigned i = 0; i < NumElements; i++) |
| 306 | Elements.push_back(Src); |
| 307 | |
| 308 | return llvm::ConstantVector::get(&Elements[0], NumElements); |
| 309 | } |
| 310 | |
| 311 | if (isa<llvm::VectorType>(Src->getType()) || |
| 312 | isa<llvm::VectorType>(DstTy)) { |
| 313 | return llvm::ConstantExpr::getBitCast(Src, DstTy); |
| 314 | } |
| 315 | |
| 316 | // Finally, we have the arithmetic types: real int/float. |
| 317 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 318 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 319 | if (isa<llvm::IntegerType>(DstTy)) |
| 320 | return llvm::ConstantExpr::getIntegerCast(Src, DstTy, InputSigned); |
| 321 | else if (InputSigned) |
| 322 | return llvm::ConstantExpr::getSIToFP(Src, DstTy); |
| 323 | else |
| 324 | return llvm::ConstantExpr::getUIToFP(Src, DstTy); |
| 325 | } |
| 326 | |
| 327 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 328 | if (isa<llvm::IntegerType>(DstTy)) { |
| 329 | if (DstType->isSignedIntegerType()) |
| 330 | return llvm::ConstantExpr::getFPToSI(Src, DstTy); |
| 331 | else |
| 332 | return llvm::ConstantExpr::getFPToUI(Src, DstTy); |
| 333 | } |
| 334 | |
| 335 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
| 336 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 337 | return llvm::ConstantExpr::getFPTrunc(Src, DstTy); |
| 338 | else |
| 339 | return llvm::ConstantExpr::getFPExtend(Src, DstTy); |
| 340 | } |
| 341 | |
| 342 | llvm::Constant *EmitSizeAlignOf(QualType TypeToSize, |
| 343 | QualType RetType, bool isSizeOf) { |
| 344 | std::pair<uint64_t, unsigned> Info = |
| 345 | CGM.getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 346 | |
| 347 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 348 | Val /= 8; // Return size in bytes, not bits. |
| 349 | |
| 350 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 351 | |
| 352 | uint32_t ResultWidth = static_cast<uint32_t>( |
| 353 | CGM.getContext().getTypeSize(RetType, SourceLocation())); |
| 354 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 355 | } |
| 356 | |
Anders Carlsson | e3d3b7d | 2008-01-26 02:08:50 +0000 | [diff] [blame^] | 357 | llvm::Constant *EmitLValue(const Expr *E) { |
| 358 | switch (E->getStmtClass()) { |
| 359 | default: { |
| 360 | CGM.WarnUnsupported(E, "constant l-value expression"); |
| 361 | llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); |
| 362 | return llvm::UndefValue::get(Ty); |
| 363 | } |
| 364 | case Expr::ParenExprClass: |
| 365 | // Elide parenthesis |
| 366 | return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); |
| 367 | case Expr::CompoundLiteralExprClass: { |
| 368 | // Note that due to the nature of compound literals, this is guaranteed |
| 369 | // to be the only use of the variable, so we just generate it here. |
| 370 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 371 | llvm::Constant* C = CGM.EmitGlobalInit(CLE->getInitializer()); |
| 372 | C =new llvm::GlobalVariable(C->getType(), E->getType().isConstQualified(), |
| 373 | llvm::GlobalValue::InternalLinkage, |
| 374 | C, ".compoundliteral", &CGM.getModule()); |
| 375 | return C; |
| 376 | } |
| 377 | case Expr::DeclRefExprClass: { |
| 378 | const ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl(); |
| 379 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) |
| 380 | return CGM.GetAddrOfFunctionDecl(FD, false); |
| 381 | if (const FileVarDecl* FVD = dyn_cast<FileVarDecl>(Decl)) |
| 382 | return CGM.GetAddrOfGlobalVar(FVD, false); |
| 383 | // We can end up here with static block-scope variables (and others?) |
| 384 | // FIXME: How do we implement block-scope variables?! |
| 385 | assert(0 && "Unimplemented Decl type"); |
| 386 | return 0; |
| 387 | } |
| 388 | case Expr::MemberExprClass: { |
| 389 | const MemberExpr* ME = cast<MemberExpr>(E); |
| 390 | unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(ME->getMemberDecl()); |
| 391 | llvm::Constant *Base = EmitLValue(ME->getBase()); |
| 392 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 393 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 394 | FieldNumber); |
| 395 | llvm::Value *Ops[] = {Zero, Idx}; |
| 396 | return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2); |
| 397 | } |
| 398 | case Expr::ArraySubscriptExprClass: { |
| 399 | const ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(E); |
| 400 | llvm::Constant *Base = EmitLValue(ASExpr->getBase()); |
| 401 | llvm::Constant *Index = EmitLValue(ASExpr->getIdx()); |
| 402 | assert(!ASExpr->getBase()->getType()->isVectorType() && |
| 403 | "Taking the address of a vector component is illegal!"); |
| 404 | return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1); |
| 405 | } |
| 406 | case Expr::StringLiteralClass: { |
| 407 | const StringLiteral *String = cast<StringLiteral>(E); |
| 408 | assert(!String->isWide() && "Cannot codegen wide strings yet"); |
| 409 | const char *StrData = String->getStrData(); |
| 410 | unsigned Len = String->getByteLength(); |
| 411 | |
| 412 | return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); |
| 413 | } |
| 414 | case Expr::UnaryOperatorClass: { |
| 415 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 416 | switch (Exp->getOpcode()) { |
| 417 | default: assert(0 && "Unsupported unary operator."); |
| 418 | case UnaryOperator::Extension: |
| 419 | // Extension is just a wrapper for expressions |
| 420 | return EmitLValue(Exp->getSubExpr()); |
| 421 | case UnaryOperator::Real: |
| 422 | case UnaryOperator::Imag: { |
| 423 | // The address of __real or __imag is just a GEP off the address |
| 424 | // of the internal expression |
| 425 | llvm::Constant* C = EmitLValue(Exp->getSubExpr()); |
| 426 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 427 | llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 428 | Exp->getOpcode() == UnaryOperator::Imag); |
| 429 | llvm::Value *Ops[] = {Zero, Idx}; |
| 430 | return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); |
| 431 | } |
| 432 | case UnaryOperator::Deref: |
| 433 | // The address of a deref is just the value of the expression |
| 434 | return Visit(Exp->getSubExpr()); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | }; |
| 441 | |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 442 | } // end anonymous namespace. |
| 443 | |
| 444 | |
| 445 | llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E) |
| 446 | { |
| 447 | QualType type = E->getType().getCanonicalType(); |
| 448 | |
| 449 | if (type->isIntegerType()) { |
| 450 | llvm::APSInt |
| 451 | Value(static_cast<uint32_t>(Context.getTypeSize(type, SourceLocation()))); |
| 452 | if (E->isIntegerConstantExpr(Value, Context)) { |
| 453 | return llvm::ConstantInt::get(Value); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return ConstExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 458 | } |