diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 8588832fe..99ad4ea84 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -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 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::max(), AssemblyException, "Foreign tag."); assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large."); assertThrow(m_tagPositionsInBytecode[static_cast(i.data())] == numeric_limits::max(), AssemblyException, "Duplicate tag position."); m_tagPositionsInBytecode[static_cast(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::max(), AssemblyException, "Reference to tag without position."); - assertThrow(util::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space."); + //assertThrow(pos != numeric_limits::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); } diff --git a/libevmasm/AssemblyItem.cpp b/libevmasm/AssemblyItem.cpp index b78c0e60c..be34b5c8f 100644 --- a/libevmasm/AssemblyItem.cpp +++ b/libevmasm/AssemblyItem.cpp @@ -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(u256(data()) & 0xffffffffffffffffULL); AssemblyItem r = *this; r.m_type = PushTag; @@ -43,7 +43,7 @@ AssemblyItem AssemblyItem::toSubAssemblyTag(size_t _subId) const pair 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((combined >> 64) - 1); size_t tag = static_cast(combined & 0xffffffffffffffffULL); @@ -52,7 +52,7 @@ pair 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::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(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(_item.data()) << dec; break; diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index 4b0b7ca72..d2245536e 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -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. diff --git a/libevmasm/BlockDeduplicator.cpp b/libevmasm/BlockDeduplicator.cpp index 95aba2a01..989b32c22 100644 --- a/libevmasm/BlockDeduplicator.cpp +++ b/libevmasm/BlockDeduplicator.cpp @@ -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) diff --git a/libevmasm/GasMeter.cpp b/libevmasm/GasMeter.cpp index 6a07e9905..97c5fdd2f 100644 --- a/libevmasm/GasMeter.cpp +++ b/libevmasm/GasMeter.cpp @@ -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: diff --git a/libevmasm/Instruction.cpp b/libevmasm/Instruction.cpp index 0a61fbea0..237546f92 100644 --- a/libevmasm/Instruction.cpp +++ b/libevmasm/Instruction.cpp @@ -165,6 +165,9 @@ std::map 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 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 } }, diff --git a/libevmasm/Instruction.h b/libevmasm/Instruction.h index 67a77f0c9..8173fde7c 100644 --- a/libevmasm/Instruction.h +++ b/libevmasm/Instruction.h @@ -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 diff --git a/libevmasm/JumpdestRemover.cpp b/libevmasm/JumpdestRemover.cpp index cbb498c01..1df3be023 100644 --- a/libevmasm/JumpdestRemover.cpp +++ b/libevmasm/JumpdestRemover.cpp @@ -40,7 +40,7 @@ bool JumpdestRemover::optimise(set 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::max(), OptimizerException, "Sub-assembly tag used as label."); diff --git a/libevmasm/KnownState.cpp b/libevmasm/KnownState.cpp index 10e4a4f10..50b441e1a 100644 --- a/libevmasm/KnownState.cpp +++ b/libevmasm/KnownState.cpp @@ -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 } diff --git a/libevmasm/SemanticInformation.cpp b/libevmasm/SemanticInformation.cpp index 76eeb5956..24d93968f 100644 --- a/libevmasm/SemanticInformation.cpp +++ b/libevmasm/SemanticInformation.cpp @@ -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: diff --git a/liblangutil/EVMVersion.cpp b/liblangutil/EVMVersion.cpp index fa100c091..e83527c41 100644 --- a/liblangutil/EVMVersion.cpp +++ b/liblangutil/EVMVersion.cpp @@ -45,6 +45,10 @@ bool EVMVersion::hasOpcode(Instruction _opcode) const return hasChainID(); case Instruction::SELFBALANCE: return hasSelfBalance(); + case Instruction::JUMPSUB: + case Instruction::BEGINSUB: + case Instruction::RETURNSUB: + return supportsSubroutines(); default: return true; } diff --git a/liblangutil/EVMVersion.h b/liblangutil/EVMVersion.h index 2c9e87ef3..f4545c5c7 100644 --- a/liblangutil/EVMVersion.h +++ b/liblangutil/EVMVersion.h @@ -86,6 +86,7 @@ public: bool hasExtCodeHash() const { return *this >= constantinople(); } bool hasChainID() const { return *this >= istanbul(); } bool hasSelfBalance() const { return *this >= istanbul(); } + bool supportsSubroutines() const { return *this >= berlin(); } bool hasOpcode(evmasm::Instruction _opcode) const; diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index 2f3659010..cad842337 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -132,6 +132,7 @@ void CompilerContext::callLowLevelFunction( *this << lowLevelFunctionTag(_name, _inArgs, _outArgs, _generator); + // TODO could use jumpsub? appendJump(evmasm::AssemblyItem::JumpType::IntoFunction); adjustStackOffset(static_cast(_outArgs) - 1 - static_cast(_inArgs)); *this << retTag.tag(); diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index ad9c97474..cf6798004 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -505,6 +505,7 @@ void ContractCompiler::appendFunctionSelector(ContractDefinition const& _contrac m_context << Instruction::DUP1 << Instruction::CALLDATASIZE << Instruction::SUB; CompilerUtils(m_context).abiDecode(functionType->parameterTypes()); } + // TODO could use JUMPSUB? m_context.appendJumpTo( m_context.functionEntryLabel(functionType->declaration()), evmasm::AssemblyItem::JumpType::IntoFunction diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 694ea8fa6..95b9a5bf2 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -614,6 +614,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) // Extract the runtime part. m_context << ((u256(1) << 32) - 1) << Instruction::AND; + // TODO could use jumpsub m_context.appendJump(evmasm::AssemblyItem::JumpType::IntoFunction); m_context << returnLabel; diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 164938c60..46b376b96 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -1060,25 +1060,25 @@ void CompilerStack::compileContract( solAssert(false, "Optimizer exception during compilation"); } - try - { +// try +// { // Assemble deployment (incl. runtime) object. compiledContract.object = compiler->assembledObject(); - } - catch(evmasm::AssemblyException const&) - { - solAssert(false, "Assembly exception for bytecode"); - } +// } +// catch(evmasm::AssemblyException const&) +// { +// solAssert(false, "Assembly exception for bytecode"); +// } - try - { +// try +// { // Assemble runtime object. compiledContract.runtimeObject = compiler->runtimeObject(); - } - catch(evmasm::AssemblyException const&) - { - solAssert(false, "Assembly exception for deployed bytecode"); - } +// } +// catch(evmasm::AssemblyException const&) +// { +// solAssert(false, "Assembly exception for deployed bytecode"); +// } // Throw a warning if EIP-170 limits are exceeded: // If contract creation initialization returns data with length of more than 0x6000 (214 + 213) bytes, diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 315699153..4feada615 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -573,7 +573,10 @@ bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocatio yulAssert( _instr != evmasm::Instruction::JUMP && _instr != evmasm::Instruction::JUMPI && - _instr != evmasm::Instruction::JUMPDEST, + _instr != evmasm::Instruction::JUMPDEST && + _instr != evmasm::Instruction::JUMPSUB && + _instr != evmasm::Instruction::RETURNSUB && + _instr != evmasm::Instruction::BEGINSUB ""); auto errorForVM = [&](ErrorId _errorId, string const& vmKindMessage) { diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index 8dcb9a832..e659075a6 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -70,6 +70,9 @@ std::map const& Parser::instructions() { if ( instruction.second == evmasm::Instruction::JUMPDEST || + instruction.second == evmasm::Instruction::BEGINSUB || + instruction.second == evmasm::Instruction::JUMPSUB || + instruction.second == evmasm::Instruction::RETURNSUB || evmasm::isPushInstruction(instruction.second) ) continue; diff --git a/libyul/backends/evm/AsmCodeGen.cpp b/libyul/backends/evm/AsmCodeGen.cpp index 327ce8895..4115f1f83 100644 --- a/libyul/backends/evm/AsmCodeGen.cpp +++ b/libyul/backends/evm/AsmCodeGen.cpp @@ -116,22 +116,22 @@ void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId, JumpType _jumpType) appendJumpInstruction(evmasm::Instruction::JUMPI, _jumpType); } -void EthAssemblyAdapter::appendBeginsub(LabelID, int) +void EthAssemblyAdapter::appendBeginsub(LabelID _labelId, int) { - // TODO we could emulate that, though - yulAssert(false, "BEGINSUB not implemented for EVM 1.0"); + m_assembly.append(evmasm::AssemblyItem(evmasm::Subtag, _labelId)); } -void EthAssemblyAdapter::appendJumpsub(LabelID, int, int) +void EthAssemblyAdapter::appendJumpsub(LabelID _labelId, int _args, int _returns) { - // TODO we could emulate that, though - yulAssert(false, "JUMPSUB not implemented for EVM 1.0"); + appendLabelReference(_labelId); + appendInstruction(evmasm::Instruction::JUMPSUB); + m_assembly.adjustDeposit(-_args + _returns); } -void EthAssemblyAdapter::appendReturnsub(int, int) +void EthAssemblyAdapter::appendReturnsub(int _returns, int _stackDiffAfter) { - // TODO we could emulate that, though - yulAssert(false, "RETURNSUB not implemented for EVM 1.0"); + appendInstruction(evmasm::Instruction::RETURNSUB); + m_assembly.adjustDeposit(_stackDiffAfter - _returns); } void EthAssemblyAdapter::appendAssemblySize() diff --git a/libyul/backends/evm/AsmCodeGen.h b/libyul/backends/evm/AsmCodeGen.h index 6ff02009c..7a3ba5a8e 100644 --- a/libyul/backends/evm/AsmCodeGen.h +++ b/libyul/backends/evm/AsmCodeGen.h @@ -53,8 +53,8 @@ public: void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override; void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override; void appendBeginsub(LabelID, int) override; - void appendJumpsub(LabelID, int, int) override; - void appendReturnsub(int, int) override; + void appendJumpsub(LabelID, int _args, int _returns) override; + void appendReturnsub(int _returns, int _stackDiffAfter) override; void appendAssemblySize() override; std::pair, SubID> createSubAssembly() override; void appendDataOffset(SubID _sub) override; diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index 041e00380..c603c41da 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -267,7 +267,7 @@ void CodeTransform::operator()(FunctionCall const& _call) { m_assembly.setSourceLocation(_call.location); EVMAssembly::LabelID returnLabel(numeric_limits::max()); // only used for evm 1.0 - if (!m_evm15) + if (!m_evm15 && !m_dialect.evmVersion().supportsSubroutines()) { returnLabel = m_assembly.newLabelId(); m_assembly.appendLabelReference(returnLabel); @@ -283,7 +283,7 @@ void CodeTransform::operator()(FunctionCall const& _call) for (auto const& arg: _call.arguments | boost::adaptors::reversed) visitExpression(arg); m_assembly.setSourceLocation(_call.location); - if (m_evm15) + if (m_evm15 || m_dialect.evmVersion().supportsSubroutines()) m_assembly.appendJumpsub( functionEntryID(_call.functionName.name, *function), static_cast(function->arguments.size()), @@ -417,7 +417,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) m_assembly.setSourceLocation(_function.location); int const stackHeightBefore = m_assembly.stackHeight(); - if (m_evm15) + if (m_evm15 || m_dialect.evmVersion().supportsSubroutines()) m_assembly.appendBeginsub(functionEntryID(_function.name, function), static_cast(_function.parameters.size())); else m_assembly.appendLabel(functionEntryID(_function.name, function)); @@ -475,7 +475,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) // This vector holds the desired target positions of all stack slots and is // modified parallel to the actual stack. vector stackLayout; - if (!m_evm15) + if (!m_evm15 && !m_dialect.evmVersion().supportsSubroutines()) stackLayout.push_back(static_cast(_function.returnVariables.size())); // Move return label to the top stackLayout += vector(_function.parameters.size(), -1); // discard all arguments @@ -511,7 +511,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) yulAssert(i == static_cast(stackLayout[i]), "Error reshuffling stack."); } } - if (m_evm15) + if (m_evm15 || m_dialect.evmVersion().supportsSubroutines()) m_assembly.appendReturnsub(static_cast(_function.returnVariables.size()), stackHeightBefore); else m_assembly.appendJump( @@ -519,6 +519,8 @@ void CodeTransform::operator()(FunctionDefinition const& _function) AbstractAssembly::JumpType::OutOfFunction ); m_assembly.setStackHeight(stackHeightBefore); + // TODO add BEGINSUB? + // We need to make sure that we did function hoister and move the "main code" to the beginning. } void CodeTransform::operator()(ForLoop const& _forLoop) diff --git a/libyul/backends/evm/NoOutputAssembly.cpp b/libyul/backends/evm/NoOutputAssembly.cpp index ee280338d..f610e26ff 100644 --- a/libyul/backends/evm/NoOutputAssembly.cpp +++ b/libyul/backends/evm/NoOutputAssembly.cpp @@ -101,21 +101,17 @@ void NoOutputAssembly::appendJumpToIf(LabelID _labelId, JumpType) void NoOutputAssembly::appendBeginsub(LabelID, int _arguments) { - yulAssert(m_evm15, "BEGINSUB used for EVM 1.0"); yulAssert(_arguments >= 0, ""); - m_stackHeight += _arguments; } void NoOutputAssembly::appendJumpsub(LabelID, int _arguments, int _returns) { - yulAssert(m_evm15, "JUMPSUB used for EVM 1.0"); yulAssert(_arguments >= 0 && _returns >= 0, ""); m_stackHeight += _returns - _arguments; } void NoOutputAssembly::appendReturnsub(int _returns, int _stackDiffAfter) { - yulAssert(m_evm15, "RETURNSUB used for EVM 1.0"); yulAssert(_returns >= 0, ""); m_stackHeight += _stackDiffAfter - _returns; }