Introduce helpers for VariableOffset.

This commit is contained in:
chriseth 2023-02-09 11:21:37 +01:00
parent 3ac6edec5b
commit 29e4becd73
2 changed files with 18 additions and 9 deletions

View File

@ -71,11 +71,7 @@ bool KnowledgeBase::knownToBeZero(YulString _a)
optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a) optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
{ {
VariableOffset offset = explore(_a); return explore(_a).absoluteValue();
if (offset.reference.empty())
return offset.offset;
else
return nullopt;
} }
optional<u256> KnowledgeBase::valueIfKnownConstant(Expression const& _expression) optional<u256> KnowledgeBase::valueIfKnownConstant(Expression const& _expression)
@ -129,10 +125,10 @@ optional<KnowledgeBase::VariableOffset> KnowledgeBase::explore(Expression const&
if (optional<VariableOffset> b = explore(f->arguments[1])) if (optional<VariableOffset> b = explore(f->arguments[1]))
{ {
u256 offset = a->offset + b->offset; u256 offset = a->offset + b->offset;
if (a->reference.empty()) if (a->isAbsolute())
// a is constant // a is constant
return VariableOffset{b->reference, offset}; return VariableOffset{b->reference, offset};
else if (b->reference.empty()) else if (b->isAbsolute())
// b is constant // b is constant
return VariableOffset{a->reference, offset}; return VariableOffset{a->reference, offset};
} }
@ -144,7 +140,7 @@ optional<KnowledgeBase::VariableOffset> KnowledgeBase::explore(Expression const&
u256 offset = a->offset - b->offset; u256 offset = a->offset - b->offset;
if (a->reference == b->reference) if (a->reference == b->reference)
return VariableOffset{YulString{}, offset}; return VariableOffset{YulString{}, offset};
else if (b->reference.empty()) else if (b->isAbsolute())
// b is constant // b is constant
return VariableOffset{a->reference, offset}; return VariableOffset{a->reference, offset};
} }
@ -175,7 +171,7 @@ void KnowledgeBase::reset(YulString _var)
if (VariableOffset const* offset = util::valueOrNullptr(m_offsets, _var)) if (VariableOffset const* offset = util::valueOrNullptr(m_offsets, _var))
{ {
// Remove var from its group // Remove var from its group
if (!offset->reference.empty()) if (!offset->isAbsolute())
m_groupMembers[offset->reference].erase(_var); m_groupMembers[offset->reference].erase(_var);
m_offsets.erase(_var); m_offsets.erase(_var);
} }

View File

@ -84,6 +84,19 @@ private:
{ {
YulString reference; YulString reference;
u256 offset; u256 offset;
bool isAbsolute() const
{
return reference.empty();
}
std::optional<u256> absoluteValue() const
{
if (isAbsolute())
return offset;
else
return std::nullopt;
}
}; };
VariableOffset explore(YulString _var); VariableOffset explore(YulString _var);