Implement subroutines for yul functions.

This commit is contained in:
chriseth
2020-07-14 18:29:37 +01:00
committed by Alex Beregszaszi
parent dca85a286d
commit cc04085b9e
22 changed files with 105 additions and 51 deletions
+20 -9
View File
@@ -110,7 +110,11 @@ public:
))
{
flush();
m_out << m_prefix << (_item.type() == Tag ? "" : " ") << _item.toAssemblyText() << endl;
m_out <<
m_prefix <<
((_item.type() == Tag || _item.type() == Subtag) ? "" : " ") <<
_item.toAssemblyText() <<
endl;
return;
}
string expression = _item.toAssemblyText();
@@ -173,7 +177,7 @@ void Assembly::assemblyStream(ostream& _out, string const& _prefix, StringMap co
if (!m_data.empty() || !m_subs.empty())
{
_out << _prefix << "stop" << endl;
_out << _prefix << "beginsub" << endl;
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
_out << _prefix << "data_" << toHex(u256(i.first)) << " " << toHex(i.second) << endl;
@@ -302,10 +306,13 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
));
break;
case Tag:
case Subtag:
collection.append(
createJsonValue("tag", sourceIndex, i.location().start, i.location().end, toString(i.data())));
createJsonValue("tag", sourceIndex, i.location().start, i.location().end, toString(i.data()))
);
collection.append(
createJsonValue("JUMPDEST", sourceIndex, i.location().start, i.location().end));
createJsonValue(i.type() == Tag ? "JUMPDEST" : "BEGINSUB", sourceIndex, i.location().start, i.location().end)
);
break;
case PushData:
collection.append(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
@@ -682,12 +689,16 @@ LinkerObject const& Assembly::assemble() const
ret.bytecode.resize(ret.bytecode.size() + 20);
break;
case Tag:
case Subtag:
assertThrow(i.data() != 0, AssemblyException, "Invalid tag position.");
assertThrow(i.splitForeignPushTag().first == numeric_limits<size_t>::max(), AssemblyException, "Foreign tag.");
assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large.");
assertThrow(m_tagPositionsInBytecode[static_cast<size_t>(i.data())] == numeric_limits<size_t>::max(), AssemblyException, "Duplicate tag position.");
m_tagPositionsInBytecode[static_cast<size_t>(i.data())] = ret.bytecode.size();
ret.bytecode.push_back((uint8_t)Instruction::JUMPDEST);
if (i.type() == Tag)
ret.bytecode.push_back((uint8_t)Instruction::JUMPDEST);
else
ret.bytecode.push_back((uint8_t)Instruction::BEGINSUB);
break;
default:
assertThrow(false, InvalidOpcode, "Unexpected opcode while assembling.");
@@ -702,8 +713,8 @@ LinkerObject const& Assembly::assemble() const
if (!m_subs.empty() || !m_data.empty() || !m_auxiliaryData.empty())
// Append an INVALID here to help tests find miscompilation.
ret.bytecode.push_back(uint8_t(Instruction::INVALID));
// Append a BEGINSUB here to help tests find miscompilation.
ret.bytecode.push_back(uint8_t(Instruction::BEGINSUB));
for (size_t i = 0; i < m_subs.size(); ++i)
{
@@ -729,8 +740,8 @@ LinkerObject const& Assembly::assemble() const
m_subs[subId]->m_tagPositionsInBytecode;
assertThrow(tagId < tagPositions.size(), AssemblyException, "Reference to non-existing tag.");
size_t pos = tagPositions[tagId];
assertThrow(pos != numeric_limits<size_t>::max(), AssemblyException, "Reference to tag without position.");
assertThrow(util::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
//assertThrow(pos != numeric_limits<size_t>::max(), AssemblyException, "Reference to tag without position.");
//assertThrow(util::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
bytesRef r(ret.bytecode.data() + i.first, bytesPerTag);
toBigEndian(pos, r);
}
+14 -4
View File
@@ -33,7 +33,7 @@ static_assert(sizeof(size_t) <= 8, "size_t must be at most 64-bits wide");
AssemblyItem AssemblyItem::toSubAssemblyTag(size_t _subId) const
{
assertThrow(data() < (u256(1) << 64), util::Exception, "Tag already has subassembly set.");
assertThrow(m_type == PushTag || m_type == Tag, util::Exception, "");
assertThrow(m_type == PushTag || m_type == Tag || m_type == Subtag, util::Exception, "");
auto tag = static_cast<size_t>(u256(data()) & 0xffffffffffffffffULL);
AssemblyItem r = *this;
r.m_type = PushTag;
@@ -43,7 +43,7 @@ AssemblyItem AssemblyItem::toSubAssemblyTag(size_t _subId) const
pair<size_t, size_t> AssemblyItem::splitForeignPushTag() const
{
assertThrow(m_type == PushTag || m_type == Tag, util::Exception, "");
assertThrow(m_type == PushTag || m_type == Tag || m_type == Subtag, util::Exception, "");
u256 combined = u256(data());
size_t subId = static_cast<size_t>((combined >> 64) - 1);
size_t tag = static_cast<size_t>(combined & 0xffffffffffffffffULL);
@@ -52,7 +52,7 @@ pair<size_t, size_t> AssemblyItem::splitForeignPushTag() const
void AssemblyItem::setPushTagSubIdAndTag(size_t _subId, size_t _tag)
{
assertThrow(m_type == PushTag || m_type == Tag, util::Exception, "");
assertThrow(m_type == PushTag || m_type == Tag || m_type == Subtag, util::Exception, "");
u256 data = _tag;
if (_subId != numeric_limits<size_t>::max())
data |= (u256(_subId) + 1) << 64;
@@ -64,7 +64,8 @@ size_t AssemblyItem::bytesRequired(size_t _addressLength) const
switch (m_type)
{
case Operation:
case Tag: // 1 byte for the JUMPDEST
case Subtag:
case Tag: // 1 byte for the JUMPDEST / BEGINSUB
return 1;
case PushString:
return 1 + 32;
@@ -121,6 +122,7 @@ size_t AssemblyItem::returnValues() const
case PushDeployTimeAddress:
return 1;
case Tag:
case Subtag:
return 0;
default:
break;
@@ -148,6 +150,7 @@ bool AssemblyItem::canBeFunctional() const
case PushImmutable:
return true;
case Tag:
case Subtag:
return false;
default:
break;
@@ -203,6 +206,10 @@ string AssemblyItem::toAssemblyText() const
assertThrow(data() < 0x10000, AssemblyException, "Declaration of sub-assembly tag.");
text = string("tag_") + to_string(static_cast<size_t>(data())) + ":";
break;
case Subtag:
assertThrow(data() < 0x10000, AssemblyException, "Declaration of sub-assembly tag.");
text = string("beginsubtag_") + to_string(size_t(data())) + ":";
break;
case PushData:
text = string("data_") + util::toHex(data());
break;
@@ -271,6 +278,9 @@ ostream& solidity::evmasm::operator<<(ostream& _out, AssemblyItem const& _item)
case Tag:
_out << " Tag " << _item.data();
break;
case Subtag:
_out << " Beginsub " << _item.data();
break;
case PushData:
_out << " PushData " << hex << static_cast<unsigned>(_item.data()) << dec;
break;
+1
View File
@@ -42,6 +42,7 @@ enum AssemblyItemType {
PushSubSize,
PushProgramSize,
Tag,
Subtag,
PushData,
PushLibraryAddress, ///< Push a currently unknown address of another (library) contract.
PushDeployTimeAddress, ///< Push an address to be filled at deploy time. Should not be touched by the optimizer.
+1
View File
@@ -104,6 +104,7 @@ bool BlockDeduplicator::applyTagReplacement(
size_t _subId
)
{
// TODO this might be problematic for jumpsub
bool changed = false;
for (AssemblyItem& item: _items)
if (item.type() == PushTag)
+1
View File
@@ -55,6 +55,7 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item, bool _
gas = runGas(Instruction::PUSH1);
break;
case Tag:
// TODO Subtag
gas = runGas(Instruction::JUMPDEST);
break;
case Operation:
+6
View File
@@ -165,6 +165,9 @@ std::map<std::string, Instruction> const solidity::evmasm::c_instructions =
{ "LOG2", Instruction::LOG2 },
{ "LOG3", Instruction::LOG3 },
{ "LOG4", Instruction::LOG4 },
{ "JUMPSUB", Instruction::JUMPSUB },
{ "BEGINSUB", Instruction::BEGINSUB },
{ "RETURNSUB", Instruction::RETURNSUB },
{ "CREATE", Instruction::CREATE },
{ "CALL", Instruction::CALL },
{ "CALLCODE", Instruction::CALLCODE },
@@ -311,6 +314,9 @@ static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::LOG2, { "LOG2", 0, 4, 0, true, Tier::Special } },
{ Instruction::LOG3, { "LOG3", 0, 5, 0, true, Tier::Special } },
{ Instruction::LOG4, { "LOG4", 0, 6, 0, true, Tier::Special } },
{ Instruction::JUMPSUB, { "JUMPSUB", 0, 1, 0, true, Tier::Special } },
{ Instruction::BEGINSUB, { "BEGINSUB", 0, 0, 0, true, Tier::Special } },
{ Instruction::RETURNSUB, { "RETURNSUB", 0, 0, 0, true, Tier::Special } },
{ Instruction::CREATE, { "CREATE", 0, 3, 1, true, Tier::Special } },
{ Instruction::CALL, { "CALL", 0, 7, 1, true, Tier::Special } },
{ Instruction::CALLCODE, { "CALLCODE", 0, 7, 1, true, Tier::Special } },
+4
View File
@@ -185,6 +185,10 @@ enum class Instruction: uint8_t
EIP615_PUTLOCAL, ///< pop top of stack to local variable -- not part of Instructions.cpp
EIP615_GETLOCAL, ///< push local variable to top of stack -- not part of Instructions.cpp
BEGINSUB = 0x5c, ///< set a potential jumpsub destination
RETURNSUB = 0x5d, ///< return to subroutine jumped from
JUMPSUB = 0x5e, ///< alter the program counter to a beginsub
CREATE = 0xf0, ///< create a new account with associated code
CALL, ///< message-call into an account
CALLCODE, ///< message-call with another account's code only
+1 -1
View File
@@ -40,7 +40,7 @@ bool JumpdestRemover::optimise(set<size_t> const& _tagsReferencedFromOutside)
m_items.end(),
[&](AssemblyItem const& _item)
{
if (_item.type() != Tag)
if (_item.type() != Tag && _item.type() != Subtag)
return false;
auto asmIdAndTag = _item.splitForeignPushTag();
assertThrow(asmIdAndTag.first == numeric_limits<size_t>::max(), OptimizerException, "Sub-assembly tag used as label.");
+1 -1
View File
@@ -87,7 +87,7 @@ ostream& KnownState::stream(ostream& _out) const
KnownState::StoreOperation KnownState::feedItem(AssemblyItem const& _item, bool _copyItem)
{
StoreOperation op;
if (_item.type() == Tag)
if (_item.type() == Tag || _item.type() == Subtag)
{
// can be ignored
}
+9 -1
View File
@@ -35,6 +35,7 @@ bool SemanticInformation::breaksCSEAnalysisBlock(AssemblyItem const& _item, bool
default:
case UndefinedItem:
case Tag:
case Subtag:
case PushDeployTimeAddress:
case AssignImmutable:
return true;
@@ -110,7 +111,12 @@ bool SemanticInformation::isSwapInstruction(AssemblyItem const& _item)
bool SemanticInformation::isJumpInstruction(AssemblyItem const& _item)
{
return _item == Instruction::JUMP || _item == Instruction::JUMPI;
// TODO check the usages of this function
return
_item == Instruction::JUMP ||
_item == Instruction::JUMPI ||
_item == Instruction::JUMPSUB ||
_item == Instruction::RETURNSUB;
}
bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)
@@ -124,6 +130,8 @@ bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)
case Instruction::JUMP:
case Instruction::JUMPI:
case Instruction::RETURN:
case Instruction::JUMPSUB:
case Instruction::RETURNSUB:
case Instruction::SELFDESTRUCT:
case Instruction::STOP:
case Instruction::INVALID: