implement codegen support for sizeof/alignof
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40009 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp
index 40c25ae..dd13763 100644
--- a/CodeGen/CGExpr.cpp
+++ b/CodeGen/CGExpr.cpp
@@ -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
diff --git a/CodeGen/CodeGenFunction.h b/CodeGen/CodeGenFunction.h
index 67cefc2..db92ee2 100644
--- a/CodeGen/CodeGenFunction.h
+++ b/CodeGen/CodeGenFunction.h
@@ -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.