From: Anders Carlsson Date: Sat, 26 Jan 2008 04:30:23 +0000 (+0000) Subject: Tweaks to EmitLValue in CGExprConstant. Patch by Eli Friedman. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84005b4b6b92e3a716fa4a792bea7d41bbb0102e;p=clang Tweaks to EmitLValue in CGExprConstant. Patch by Eli Friedman. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46389 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CodeGen/CGExprConstant.cpp b/CodeGen/CGExprConstant.cpp index 4b3b3cb8be..5808da4212 100644 --- a/CodeGen/CGExprConstant.cpp +++ b/CodeGen/CGExprConstant.cpp @@ -22,7 +22,8 @@ using namespace clang; using namespace CodeGen; namespace { -class VISIBILITY_HIDDEN ConstExprEmitter : public StmtVisitor { +class VISIBILITY_HIDDEN ConstExprEmitter : + public StmtVisitor { CodeGenModule &CGM; public: ConstExprEmitter(CodeGenModule &cgm) @@ -285,7 +286,7 @@ public: // The source value may be an integer, or a pointer. if (isa(Src->getType())) return llvm::ConstantExpr::getBitCast(Src, DstTy); - assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); + assert(SrcType->isIntegerType() &&"Not ptr->ptr or int->ptr conversion?"); return llvm::ConstantExpr::getIntToPtr(Src, DstTy); } @@ -353,8 +354,8 @@ public: CGM.getContext().getTypeSize(RetType, SourceLocation())); return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); } - - llvm::Constant *EmitLValue(const Expr *E) { + + llvm::Constant *EmitLValue(Expr *E) { switch (E->getStmtClass()) { default: { CGM.WarnUnsupported(E, "constant l-value expression"); @@ -367,15 +368,15 @@ public: case Expr::CompoundLiteralExprClass: { // Note that due to the nature of compound literals, this is guaranteed // to be the only use of the variable, so we just generate it here. - const CompoundLiteralExpr *CLE = cast(E); - llvm::Constant* C = CGM.EmitGlobalInit(CLE->getInitializer()); - C =new llvm::GlobalVariable(C->getType(), E->getType().isConstQualified(), + CompoundLiteralExpr *CLE = cast(E); + llvm::Constant* C = Visit(CLE->getInitializer()); + C = new llvm::GlobalVariable(C->getType(), E->getType().isConstQualified(), llvm::GlobalValue::InternalLinkage, C, ".compoundliteral", &CGM.getModule()); return C; - } + } case Expr::DeclRefExprClass: { - const ValueDecl *Decl = cast(E)->getDecl(); + ValueDecl *Decl = cast(E)->getDecl(); if (const FunctionDecl *FD = dyn_cast(Decl)) return CGM.GetAddrOfFunctionDecl(FD, false); if (const FileVarDecl* FVD = dyn_cast(Decl)) @@ -386,9 +387,13 @@ public: return 0; } case Expr::MemberExprClass: { - const MemberExpr* ME = cast(E); + MemberExpr* ME = cast(E); unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(ME->getMemberDecl()); - llvm::Constant *Base = EmitLValue(ME->getBase()); + llvm::Constant *Base; + if (ME->isArrow()) + Base = Visit(ME->getBase()); + else + Base = EmitLValue(ME->getBase()); llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, FieldNumber); @@ -396,46 +401,46 @@ public: return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2); } case Expr::ArraySubscriptExprClass: { - const ArraySubscriptExpr* ASExpr = cast(E); - llvm::Constant *Base = EmitLValue(ASExpr->getBase()); - llvm::Constant *Index = EmitLValue(ASExpr->getIdx()); + ArraySubscriptExpr* ASExpr = cast(E); + llvm::Constant *Base = Visit(ASExpr->getBase()); + llvm::Constant *Index = Visit(ASExpr->getIdx()); assert(!ASExpr->getBase()->getType()->isVectorType() && "Taking the address of a vector component is illegal!"); return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1); } case Expr::StringLiteralClass: { - const StringLiteral *String = cast(E); + StringLiteral *String = cast(E); assert(!String->isWide() && "Cannot codegen wide strings yet"); - const char *StrData = String->getStrData(); - unsigned Len = String->getByteLength(); + const char *StrData = String->getStrData(); + unsigned Len = String->getByteLength(); - return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); - } - case Expr::UnaryOperatorClass: { - const UnaryOperator *Exp = cast(E); - switch (Exp->getOpcode()) { - default: assert(0 && "Unsupported unary operator."); - case UnaryOperator::Extension: - // Extension is just a wrapper for expressions - return EmitLValue(Exp->getSubExpr()); - case UnaryOperator::Real: - case UnaryOperator::Imag: { - // The address of __real or __imag is just a GEP off the address - // of the internal expression - llvm::Constant* C = EmitLValue(Exp->getSubExpr()); - llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); - llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, - Exp->getOpcode() == UnaryOperator::Imag); - llvm::Value *Ops[] = {Zero, Idx}; - return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); + return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len)); } - case UnaryOperator::Deref: - // The address of a deref is just the value of the expression - return Visit(Exp->getSubExpr()); + case Expr::UnaryOperatorClass: { + UnaryOperator *Exp = cast(E); + switch (Exp->getOpcode()) { + default: assert(0 && "Unsupported unary operator."); + case UnaryOperator::Extension: + // Extension is just a wrapper for expressions + return EmitLValue(Exp->getSubExpr()); + case UnaryOperator::Real: + case UnaryOperator::Imag: { + // The address of __real or __imag is just a GEP off the address + // of the internal expression + llvm::Constant* C = EmitLValue(Exp->getSubExpr()); + llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); + llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, + Exp->getOpcode() == UnaryOperator::Imag); + llvm::Value *Ops[] = {Zero, Idx}; + return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2); + } + case UnaryOperator::Deref: + // The address of a deref is just the value of the expression + return Visit(Exp->getSubExpr()); + } } + } } - } -} }; diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index e32e85113c..f8b31b64bc 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 03F50AC60D416EAA00B9CF60 /* Targets.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F50AC50D416EAA00B9CF60 /* Targets.cpp */; }; 1A30A9E90B93A4C800201A91 /* ExprCXX.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1A30A9E80B93A4C800201A91 /* ExprCXX.h */; }; + 1A376A2D0D4AED9B002A1C52 /* CGExprConstant.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A376A2C0D4AED9B002A1C52 /* CGExprConstant.cpp */; }; 1A7342480C7B57D500122F56 /* CGObjC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A7342470C7B57D500122F56 /* CGObjC.cpp */; }; 1A869A700BA2164C008DA07A /* LiteralSupport.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1A869A6E0BA2164C008DA07A /* LiteralSupport.h */; }; 1A869AA80BA21ABA008DA07A /* LiteralSupport.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A869AA70BA21ABA008DA07A /* LiteralSupport.cpp */; }; @@ -221,6 +222,7 @@ /* Begin PBXFileReference section */ 03F50AC50D416EAA00B9CF60 /* Targets.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Targets.cpp; sourceTree = ""; }; 1A30A9E80B93A4C800201A91 /* ExprCXX.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ExprCXX.h; path = clang/AST/ExprCXX.h; sourceTree = ""; }; + 1A376A2C0D4AED9B002A1C52 /* CGExprConstant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CGExprConstant.cpp; path = CodeGen/CGExprConstant.cpp; sourceTree = ""; }; 1A68BC110D0CADDD001A28C8 /* PPCBuiltins.def */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = PPCBuiltins.def; path = clang/AST/PPCBuiltins.def; sourceTree = ""; }; 1A68BC120D0CADDD001A28C8 /* TargetBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TargetBuiltins.h; path = clang/AST/TargetBuiltins.h; sourceTree = ""; }; 1A68BC130D0CADDD001A28C8 /* X86Builtins.def */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = X86Builtins.def; path = clang/AST/X86Builtins.def; sourceTree = ""; }; @@ -580,6 +582,7 @@ DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */, DEF2EFF20C6CDD74000C4259 /* CGExprAgg.cpp */, DE224FF70C7AA98800D370A5 /* CGExprComplex.cpp */, + 1A376A2C0D4AED9B002A1C52 /* CGExprConstant.cpp */, DE22526F0C7E82D000D370A5 /* CGExprScalar.cpp */, 1A7342470C7B57D500122F56 /* CGObjC.cpp */, DE4772F90C10EAE5002239E8 /* CGStmt.cpp */, @@ -901,6 +904,7 @@ 35BB2D7F0D19954000944DB5 /* ASTConsumer.cpp in Sources */, DE47999C0D2EBE1A00706D2D /* SemaExprObjC.cpp in Sources */, 03F50AC60D416EAA00B9CF60 /* Targets.cpp in Sources */, + 1A376A2D0D4AED9B002A1C52 /* CGExprConstant.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };