[cfe-commits] r40009 - in /cfe/trunk/CodeGen: CGExpr.cpp CodeGenFunction.h
Chris Lattner
sabre at nondot.org
Wed Jul 18 11:12:07 PDT 2007
Author: lattner
Date: Wed Jul 18 13:12:07 2007
New Revision: 40009
URL: http://llvm.org/viewvc/llvm-project?rev=40009&view=rev
Log:
implement codegen support for sizeof/alignof
Modified:
cfe/trunk/CodeGen/CGExpr.cpp
cfe/trunk/CodeGen/CodeGenFunction.h
Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=40009&r1=40008&r2=40009&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 18 13:12:07 2007
@@ -473,6 +473,10 @@
return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
case Expr::UnaryOperatorClass:
return EmitUnaryOperator(cast<UnaryOperator>(E));
+ case Expr::SizeOfAlignOfTypeExprClass:
+ return EmitSizeAlignOf(cast<SizeOfAlignOfTypeExpr>(E)->getArgumentType(),
+ E->getType(),
+ cast<SizeOfAlignOfTypeExpr>(E)->isSizeOf());
case Expr::ImplicitCastExprClass:
return EmitCastExpr(cast<ImplicitCastExpr>(E)->getSubExpr(), E->getType());
case Expr::CastExprClass:
@@ -655,7 +659,10 @@
case UnaryOperator::Minus : return EmitUnaryMinus(E);
case UnaryOperator::Not : return EmitUnaryNot(E);
case UnaryOperator::LNot : return EmitUnaryLNot(E);
- // FIXME: SIZEOF/ALIGNOF(expr).
+ case UnaryOperator::SizeOf :
+ return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
+ case UnaryOperator::AlignOf :
+ return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
// FIXME: real/imag
case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
}
@@ -751,6 +758,23 @@
return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
}
+/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
+/// an integer (RetType).
+RValue CodeGenFunction::EmitSizeAlignOf(QualType TypeToSize,
+ QualType RetType, bool isSizeOf) {
+ /// FIXME: This doesn't handle VLAs yet!
+ std::pair<uint64_t, unsigned> Info =
+ getContext().getTypeInfo(TypeToSize, SourceLocation());
+
+ uint64_t Val = isSizeOf ? Info.first : Info.second;
+ Val /= 8; // Return size in bytes, not bits.
+
+ assert(RetType->isIntegerType() && "Result type must be an integer!");
+
+ unsigned ResultWidth = getContext().getTypeSize(RetType, SourceLocation());
+ return RValue::get(llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)));
+}
+
//===--------------------------------------------------------------------===//
// Binary Operator Emission
Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=40009&r1=40008&r2=40009&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 18 13:12:07 2007
@@ -340,7 +340,7 @@
RValue EmitUnaryMinus (const UnaryOperator *E);
RValue EmitUnaryNot (const UnaryOperator *E);
RValue EmitUnaryLNot (const UnaryOperator *E);
- // FIXME: SIZEOF/ALIGNOF(expr).
+ RValue EmitSizeAlignOf (QualType TypeToSize, QualType RetType,bool isSizeOf);
// FIXME: real/imag
// Binary Operators.
More information about the cfe-commits
mailing list