forked from OSchip/llvm-project
Tweaks to EmitLValue in CGExprConstant. Patch by Eli Friedman.
llvm-svn: 46389
This commit is contained in:
parent
a413911373
commit
8a88c3815a
|
@ -22,7 +22,8 @@ using namespace clang;
|
|||
using namespace CodeGen;
|
||||
|
||||
namespace {
|
||||
class VISIBILITY_HIDDEN ConstExprEmitter : public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
||||
class VISIBILITY_HIDDEN ConstExprEmitter :
|
||||
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
||||
CodeGenModule &CGM;
|
||||
public:
|
||||
ConstExprEmitter(CodeGenModule &cgm)
|
||||
|
@ -285,7 +286,7 @@ public:
|
|||
// The source value may be an integer, or a pointer.
|
||||
if (isa<llvm::PointerType>(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<CompoundLiteralExpr>(E);
|
||||
llvm::Constant* C = CGM.EmitGlobalInit(CLE->getInitializer());
|
||||
C =new llvm::GlobalVariable(C->getType(), E->getType().isConstQualified(),
|
||||
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(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<DeclRefExpr>(E)->getDecl();
|
||||
ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
||||
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
||||
return CGM.GetAddrOfFunctionDecl(FD, false);
|
||||
if (const FileVarDecl* FVD = dyn_cast<FileVarDecl>(Decl))
|
||||
|
@ -386,9 +387,13 @@ public:
|
|||
return 0;
|
||||
}
|
||||
case Expr::MemberExprClass: {
|
||||
const MemberExpr* ME = cast<MemberExpr>(E);
|
||||
MemberExpr* ME = cast<MemberExpr>(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<ArraySubscriptExpr>(E);
|
||||
llvm::Constant *Base = EmitLValue(ASExpr->getBase());
|
||||
llvm::Constant *Index = EmitLValue(ASExpr->getIdx());
|
||||
ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(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<StringLiteral>(E);
|
||||
StringLiteral *String = cast<StringLiteral>(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<UnaryOperator>(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,
|
||||
return CGM.GetAddrOfConstantString(std::string(StrData, StrData + Len));
|
||||
}
|
||||
case Expr::UnaryOperatorClass: {
|
||||
UnaryOperator *Exp = cast<UnaryOperator>(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());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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 = "<group>"; };
|
||||
1A30A9E80B93A4C800201A91 /* ExprCXX.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ExprCXX.h; path = clang/AST/ExprCXX.h; sourceTree = "<group>"; };
|
||||
1A376A2C0D4AED9B002A1C52 /* CGExprConstant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CGExprConstant.cpp; path = CodeGen/CGExprConstant.cpp; sourceTree = "<group>"; };
|
||||
1A68BC110D0CADDD001A28C8 /* PPCBuiltins.def */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = PPCBuiltins.def; path = clang/AST/PPCBuiltins.def; sourceTree = "<group>"; };
|
||||
1A68BC120D0CADDD001A28C8 /* TargetBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TargetBuiltins.h; path = clang/AST/TargetBuiltins.h; sourceTree = "<group>"; };
|
||||
1A68BC130D0CADDD001A28C8 /* X86Builtins.def */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = X86Builtins.def; path = clang/AST/X86Builtins.def; sourceTree = "<group>"; };
|
||||
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue