Skip to content

Commit

Permalink
[clang][Interp][NFCI] Cleanup emitConst()
Browse files Browse the repository at this point in the history
Before, when emitting a regular integer constant, we went:

Int -> APInt -> int -> emit

Fix this by using regular integer constants in emitConst() and instead
converting APInt to those once.
  • Loading branch information
tbaederr committed Nov 7, 2022
1 parent c40ef64 commit f4707af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
51 changes: 26 additions & 25 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Expand Up @@ -160,9 +160,7 @@ bool ByteCodeExprGen<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
if (DiscardResult)
return true;

if (Optional<PrimType> T = classify(LE->getType()))
return emitConst(*T, LE->getValue(), LE);
return this->bail(LE);
return this->emitConst(LE->getValue(), LE);
}

template <class Emitter>
Expand Down Expand Up @@ -380,7 +378,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
Size = Ctx.getASTContext().getTypeSizeInChars(ArgType);
}

return this->emitConst(E, Size.getQuantity());
return this->emitConst(Size.getQuantity(), E);
}

return false;
Expand Down Expand Up @@ -420,9 +418,7 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitIndexExpr(
// stand-alone, e.g. via EvaluateAsInt().
if (!ArrayIndex)
return false;
QualType IndexType = E->getType();
APInt Value(getIntWidth(IndexType), *ArrayIndex);
return this->emitConst(classifyPrim(IndexType), Value, E);
return this->emitConst(*ArrayIndex, E);
}

template <class Emitter>
Expand Down Expand Up @@ -470,7 +466,7 @@ bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitCharacterLiteral(
const CharacterLiteral *E) {
return this->emitConst(E, E->getValue());
return this->emitConst(E->getValue(), E);
}

template <class Emitter>
Expand Down Expand Up @@ -715,34 +711,41 @@ bool ByteCodeExprGen<Emitter>::dereferenceVar(
}

template <class Emitter>
bool ByteCodeExprGen<Emitter>::emitConst(PrimType T, const APInt &Value,
const Expr *E) {
switch (T) {
template <typename T>
bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
switch (classifyPrim(E->getType())) {
case PT_Sint8:
return this->emitConstSint8(Value.getSExtValue(), E);
return this->emitConstSint8(Value, E);
case PT_Uint8:
return this->emitConstUint8(Value.getZExtValue(), E);
return this->emitConstUint8(Value, E);
case PT_Sint16:
return this->emitConstSint16(Value.getSExtValue(), E);
return this->emitConstSint16(Value, E);
case PT_Uint16:
return this->emitConstUint16(Value.getZExtValue(), E);
return this->emitConstUint16(Value, E);
case PT_Sint32:
return this->emitConstSint32(Value.getSExtValue(), E);
return this->emitConstSint32(Value, E);
case PT_Uint32:
return this->emitConstUint32(Value.getZExtValue(), E);
return this->emitConstUint32(Value, E);
case PT_Sint64:
return this->emitConstSint64(Value.getSExtValue(), E);
return this->emitConstSint64(Value, E);
case PT_Uint64:
return this->emitConstUint64(Value.getZExtValue(), E);
return this->emitConstUint64(Value, E);
case PT_Bool:
return this->emitConstBool(Value.getBoolValue(), E);
return this->emitConstBool(Value, E);
case PT_Ptr:
llvm_unreachable("Invalid integral type");
break;
}
llvm_unreachable("unknown primitive type");
}

template <class Emitter>
bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
if (Value.isSigned())
return this->emitConst(Value.getSExtValue(), E);
return this->emitConst(Value.getZExtValue(), E);
}

template <class Emitter>
unsigned ByteCodeExprGen<Emitter>::allocateLocalPrimitive(DeclTy &&Src,
PrimType Ty,
Expand Down Expand Up @@ -1198,7 +1201,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
return this->emitIncPop(*T, E);

this->emitLoad(*T, E);
this->emitConst(E, 1);
this->emitConst(1, E);
this->emitAdd(*T, E);
return this->emitStore(*T, E);
}
Expand All @@ -1211,7 +1214,7 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
return this->emitDecPop(*T, E);

this->emitLoad(*T, E);
this->emitConst(E, 1);
this->emitConst(1, E);
this->emitSub(*T, E);
return this->emitStore(*T, E);
}
Expand Down Expand Up @@ -1284,9 +1287,7 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
return this->emitGetPtrParam(It->second, E);
}
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(Decl)) {
PrimType T = *classify(ECD->getType());

return this->emitConst(T, ECD->getInitVal(), E);
return this->emitConst(ECD->getInitVal(), E);
}

return false;
Expand Down
14 changes: 6 additions & 8 deletions clang/lib/AST/Interp/ByteCodeExprGen.h
Expand Up @@ -229,16 +229,14 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
DerefKind AK, llvm::function_ref<bool(PrimType)> Direct,
llvm::function_ref<bool(PrimType)> Indirect);

/// Emits an APInt constant.
bool emitConst(PrimType T, const llvm::APInt &Value, const Expr *E);
/// Emits an APSInt constant.
bool emitConst(const APSInt &Value, const Expr *E);
bool emitConst(const APInt &Value, const Expr *E) {
return emitConst(static_cast<APSInt>(Value), E);
}

/// Emits an integer constant.
template <typename T> bool emitConst(const Expr *E, T Value) {
QualType Ty = E->getType();
APInt WrappedValue(getIntWidth(Ty), static_cast<uint64_t>(Value),
std::is_signed<T>::value);
return emitConst(*Ctx.classify(Ty), WrappedValue, E);
}
template <typename T> bool emitConst(T Value, const Expr *E);

/// Emits the initialized pointer.
bool emitInitFn() {
Expand Down

0 comments on commit f4707af

Please sign in to comment.