diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index e43a61d37..8fe76e24b 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -1605,7 +1605,7 @@ void Main::on_data_textChanged() shared_ptr scanner = make_shared(); try { - m_data = dev::solidity::CompilerStack::compile(src, scanner); + m_data = dev::solidity::CompilerStack::compile(src, scanner, m_enableOptimizer); } catch (dev::Exception const& exception) { diff --git a/libevmcore/Assembly.cpp b/libevmcore/Assembly.cpp index d4f6c0a73..4725c8c1a 100644 --- a/libevmcore/Assembly.cpp +++ b/libevmcore/Assembly.cpp @@ -70,7 +70,12 @@ unsigned Assembly::bytesRequired() const case PushData: case PushSub: ret += 1 + br; - default:; + break; + case NoOptimizeBegin: + case NoOptimizeEnd: + break; + default: + BOOST_THROW_EXCEPTION(InvalidOpcode()); } if (dev::bytesRequired(ret) <= br) return ret; @@ -140,9 +145,17 @@ ostream& dev::eth::operator<<(ostream& _out, AssemblyItemsConstRef _i) case PushSubSize: _out << " PUSHss[" << hex << h256(i.data()).abridged() << "]"; break; + case NoOptimizeBegin: + _out << " DoNotOptimze{{"; + break; + case NoOptimizeEnd: + _out << " DoNotOptimze}}"; + break; case UndefinedItem: _out << " ???"; - default:; + break; + default: + BOOST_THROW_EXCEPTION(InvalidOpcode()); } return _out; } @@ -177,7 +190,14 @@ ostream& Assembly::streamRLP(ostream& _out, string const& _prefix) const case PushData: _out << _prefix << " PUSH [" << hex << (unsigned)i.m_data << "]" << endl; break; - default:; + case NoOptimizeBegin: + _out << _prefix << "DoNotOptimze{{" << endl; + break; + case NoOptimizeEnd: + _out << _prefix << "DoNotOptimze}}" << endl; + break; + default: + BOOST_THROW_EXCEPTION(InvalidOpcode()); } if (m_data.size() || m_subs.size()) @@ -217,6 +237,12 @@ inline bool matches(AssemblyItemsConstRef _a, AssemblyItemsConstRef _b) return true; } +inline bool popCountIncreased(AssemblyItemsConstRef _pre, AssemblyItems const& _post) +{ + auto isPop = [](AssemblyItem const& _item) -> bool { return _item.match(AssemblyItem(Instruction::POP)); }; + return count_if(begin(_post), end(_post), isPop) > count_if(begin(_pre), end(_pre), isPop); +} + struct OptimiserChannel: public LogChannel { static const char* name() { return "OPT"; } static const int verbosity = 12; }; #define copt dev::LogOutputStream() @@ -224,6 +250,14 @@ Assembly& Assembly::optimise(bool _enable) { if (!_enable) return *this; + auto signextend = [](u256 a, u256 b) -> u256 + { + if (a >= 31) + return b; + unsigned testBit = unsigned(a) * 8 + 7; + u256 mask = (u256(1) << testBit) - 1; + return boost::multiprecision::bit_test(b, testBit) ? b | ~mask : b & mask; + }; map> c_simple = { { Instruction::SUB, [](u256 a, u256 b)->u256{return a - b;} }, @@ -232,6 +266,7 @@ Assembly& Assembly::optimise(bool _enable) { Instruction::MOD, [](u256 a, u256 b)->u256{return a % b;} }, { Instruction::SMOD, [](u256 a, u256 b)->u256{return s2u(u2s(a) % u2s(b));} }, { Instruction::EXP, [](u256 a, u256 b)->u256{return (u256)boost::multiprecision::powm((bigint)a, (bigint)b, bigint(2) << 256);} }, + { Instruction::SIGNEXTEND, signextend }, { Instruction::LT, [](u256 a, u256 b)->u256{return a < b ? 1 : 0;} }, { Instruction::GT, [](u256 a, u256 b)->u256{return a > b ? 1 : 0;} }, { Instruction::SLT, [](u256 a, u256 b)->u256{return u2s(a) < u2s(b) ? 1 : 0;} }, @@ -242,6 +277,9 @@ Assembly& Assembly::optimise(bool _enable) { { Instruction::ADD, [](u256 a, u256 b)->u256{return a + b;} }, { Instruction::MUL, [](u256 a, u256 b)->u256{return a * b;} }, + { Instruction::AND, [](u256 a, u256 b)->u256{return a & b;} }, + { Instruction::OR, [](u256 a, u256 b)->u256{return a | b;} }, + { Instruction::XOR, [](u256 a, u256 b)->u256{return a ^ b;} }, }; std::vector>> rules = { @@ -260,8 +298,23 @@ Assembly& Assembly::optimise(bool _enable) { rules.push_back({ { Push, Push, i.first }, [&](AssemblyItemsConstRef m) -> AssemblyItems { return { i.second(m[1].data(), m[0].data()) }; } }); rules.push_back({ { Push, i.first, Push, i.first }, [&](AssemblyItemsConstRef m) -> AssemblyItems { return { i.second(m[2].data(), m[0].data()), i.first }; } }); - rules.push_back({ { PushTag, Instruction::JUMP, Tag }, [&](AssemblyItemsConstRef m) -> AssemblyItems { if (m[0].m_data == m[2].m_data) return {}; else return m.toVector(); }}); } + // jump to next instruction + rules.push_back({ { PushTag, Instruction::JUMP, Tag }, [&](AssemblyItemsConstRef m) -> AssemblyItems { if (m[0].m_data == m[2].m_data) return {m[2]}; else return m.toVector(); }}); + + // pop optimization, do not compute values that are popped again anyway + rules.push_back({ { AssemblyItem(UndefinedItem), Instruction::POP }, [](AssemblyItemsConstRef m) -> AssemblyItems + { + if (m[0].type() != Operation) + return m.toVector(); + Instruction instr = Instruction(byte(m[0].data())); + if (Instruction::DUP1 <= instr && instr <= Instruction::DUP16) + return {}; + InstructionInfo info = instructionInfo(instr); + if (info.sideEffects || info.additional != 0 || info.ret != 1) + return m.toVector(); + return AssemblyItems(info.args, Instruction::POP); + } }); copt << *this; @@ -269,16 +322,21 @@ Assembly& Assembly::optimise(bool _enable) for (unsigned count = 1; count > 0; total += count) { count = 0; - map tags; for (unsigned i = 0; i < m_items.size(); ++i) { + if (m_items[i].type() == NoOptimizeBegin) + { + while (i < m_items.size() && m_items[i].type() != NoOptimizeEnd) + ++i; + continue; + } for (auto const& r: rules) { auto vr = AssemblyItemsConstRef(&m_items).cropped(i, r.first.size()); - if (matches(&r.first, vr)) + if (matches(vr, &r.first)) { auto rw = r.second(vr); - if (rw.size() < vr.size()) + if (rw.size() < vr.size() || (rw.size() == vr.size() && popCountIncreased(vr, rw))) { copt << vr << "matches" << AssemblyItemsConstRef(&r.first) << "becomes..."; for (unsigned j = 0; j < vr.size(); ++j) @@ -297,6 +355,8 @@ Assembly& Assembly::optimise(bool _enable) bool o = false; while (m_items.size() > i + 1 && m_items[i + 1].type() != Tag) { + if (m_items[i + 1].type() == NoOptimizeBegin) + break; m_items.erase(m_items.begin() + i + 1); o = true; } @@ -308,6 +368,7 @@ Assembly& Assembly::optimise(bool _enable) } } + map tags; for (unsigned i = 0; i < m_items.size(); ++i) if (m_items[i].type() == Tag) tags.insert(make_pair(m_items[i].data(), i)); @@ -416,7 +477,11 @@ bytes Assembly::assemble() const tagPos[(unsigned)i.m_data] = ret.size(); ret.push_back((byte)Instruction::JUMPDEST); break; - default:; + case NoOptimizeBegin: + case NoOptimizeEnd: + break; + default: + BOOST_THROW_EXCEPTION(InvalidOpcode()); } for (auto const& i: tagRef) diff --git a/libevmcore/Assembly.h b/libevmcore/Assembly.h index ef5294a54..b8e59a474 100644 --- a/libevmcore/Assembly.h +++ b/libevmcore/Assembly.h @@ -32,7 +32,7 @@ namespace dev namespace eth { -enum AssemblyItemType { UndefinedItem, Operation, Push, PushString, PushTag, PushSub, PushSubSize, Tag, PushData }; +enum AssemblyItemType { UndefinedItem, Operation, Push, PushString, PushTag, PushSub, PushSubSize, Tag, PushData, NoOptimizeBegin, NoOptimizeEnd }; class Assembly; diff --git a/libevmcore/Instruction.cpp b/libevmcore/Instruction.cpp index ad27b28ef..2df77a3c5 100644 --- a/libevmcore/Instruction.cpp +++ b/libevmcore/Instruction.cpp @@ -162,136 +162,136 @@ const std::map dev::eth::c_instructions = }; static const std::map c_instructionInfo = -{ // Add, Args, Ret - { Instruction::STOP, { "STOP", 0, 0, 0 } }, - { Instruction::ADD, { "ADD", 0, 2, 1 } }, - { Instruction::SUB, { "SUB", 0, 2, 1 } }, - { Instruction::MUL, { "MUL", 0, 2, 1 } }, - { Instruction::DIV, { "DIV", 0, 2, 1 } }, - { Instruction::SDIV, { "SDIV", 0, 2, 1 } }, - { Instruction::MOD, { "MOD", 0, 2, 1 } }, - { Instruction::SMOD, { "SMOD", 0, 2, 1 } }, - { Instruction::EXP, { "EXP", 0, 2, 1 } }, - { Instruction::NOT, { "NOT", 0, 1, 1 } }, - { Instruction::LT, { "LT", 0, 2, 1 } }, - { Instruction::GT, { "GT", 0, 2, 1 } }, - { Instruction::SLT, { "SLT", 0, 2, 1 } }, - { Instruction::SGT, { "SGT", 0, 2, 1 } }, - { Instruction::EQ, { "EQ", 0, 2, 1 } }, - { Instruction::ISZERO, { "ISZERO", 0, 1, 1 } }, - { Instruction::AND, { "AND", 0, 2, 1 } }, - { Instruction::OR, { "OR", 0, 2, 1 } }, - { Instruction::XOR, { "XOR", 0, 2, 1 } }, - { Instruction::BYTE, { "BYTE", 0, 2, 1 } }, - { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1 } }, - { Instruction::MULMOD, { "MULMOD", 0, 3, 1 } }, - { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1 } }, - { Instruction::SHA3, { "SHA3", 0, 2, 1 } }, - { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1 } }, - { Instruction::BALANCE, { "BALANCE", 0, 1, 1 } }, - { Instruction::ORIGIN, { "ORIGIN", 0, 0, 1 } }, - { Instruction::CALLER, { "CALLER", 0, 0, 1 } }, - { Instruction::CALLVALUE, { "CALLVALUE", 0, 0, 1 } }, - { Instruction::CALLDATALOAD,{ "CALLDATALOAD", 0, 1, 1 } }, - { Instruction::CALLDATASIZE,{ "CALLDATASIZE", 0, 0, 1 } }, - { Instruction::CALLDATACOPY,{ "CALLDATACOPY", 0, 3, 0 } }, - { Instruction::CODESIZE, { "CODESIZE", 0, 0, 1 } }, - { Instruction::CODECOPY, { "CODECOPY", 0, 3, 0 } }, - { Instruction::GASPRICE, { "GASPRICE", 0, 0, 1 } }, - { Instruction::EXTCODESIZE, { "EXTCODESIZE", 0, 1, 1 } }, - { Instruction::EXTCODECOPY, { "EXTCODECOPY", 0, 4, 0 } }, - { Instruction::PREVHASH, { "PREVHASH", 0, 0, 1 } }, - { Instruction::COINBASE, { "COINBASE", 0, 0, 1 } }, - { Instruction::TIMESTAMP, { "TIMESTAMP", 0, 0, 1 } }, - { Instruction::NUMBER, { "NUMBER", 0, 0, 1 } }, - { Instruction::DIFFICULTY, { "DIFFICULTY", 0, 0, 1 } }, - { Instruction::GASLIMIT, { "GASLIMIT", 0, 0, 1 } }, - { Instruction::POP, { "POP", 0, 1, 0 } }, - { Instruction::MLOAD, { "MLOAD", 0, 1, 1 } }, - { Instruction::MSTORE, { "MSTORE", 0, 2, 0 } }, - { Instruction::MSTORE8, { "MSTORE8", 0, 2, 0 } }, - { Instruction::SLOAD, { "SLOAD", 0, 1, 1 } }, - { Instruction::SSTORE, { "SSTORE", 0, 2, 0 } }, - { Instruction::JUMP, { "JUMP", 0, 1, 0 } }, - { Instruction::JUMPI, { "JUMPI", 0, 2, 0 } }, - { Instruction::PC, { "PC", 0, 0, 1 } }, - { Instruction::MSIZE, { "MSIZE", 0, 0, 1 } }, - { Instruction::GAS, { "GAS", 0, 0, 1 } }, - { Instruction::JUMPDEST, { "JUMPDEST", 0, 1, 0 } }, - { Instruction::PUSH1, { "PUSH1", 1, 0, 1 } }, - { Instruction::PUSH2, { "PUSH2", 2, 0, 1 } }, - { Instruction::PUSH3, { "PUSH3", 3, 0, 1 } }, - { Instruction::PUSH4, { "PUSH4", 4, 0, 1 } }, - { Instruction::PUSH5, { "PUSH5", 5, 0, 1 } }, - { Instruction::PUSH6, { "PUSH6", 6, 0, 1 } }, - { Instruction::PUSH7, { "PUSH7", 7, 0, 1 } }, - { Instruction::PUSH8, { "PUSH8", 8, 0, 1 } }, - { Instruction::PUSH9, { "PUSH9", 9, 0, 1 } }, - { Instruction::PUSH10, { "PUSH10", 10, 0, 1 } }, - { Instruction::PUSH11, { "PUSH11", 11, 0, 1 } }, - { Instruction::PUSH12, { "PUSH12", 12, 0, 1 } }, - { Instruction::PUSH13, { "PUSH13", 13, 0, 1 } }, - { Instruction::PUSH14, { "PUSH14", 14, 0, 1 } }, - { Instruction::PUSH15, { "PUSH15", 15, 0, 1 } }, - { Instruction::PUSH16, { "PUSH16", 16, 0, 1 } }, - { Instruction::PUSH17, { "PUSH17", 17, 0, 1 } }, - { Instruction::PUSH18, { "PUSH18", 18, 0, 1 } }, - { Instruction::PUSH19, { "PUSH19", 19, 0, 1 } }, - { Instruction::PUSH20, { "PUSH20", 20, 0, 1 } }, - { Instruction::PUSH21, { "PUSH21", 21, 0, 1 } }, - { Instruction::PUSH22, { "PUSH22", 22, 0, 1 } }, - { Instruction::PUSH23, { "PUSH23", 23, 0, 1 } }, - { Instruction::PUSH24, { "PUSH24", 24, 0, 1 } }, - { Instruction::PUSH25, { "PUSH25", 25, 0, 1 } }, - { Instruction::PUSH26, { "PUSH26", 26, 0, 1 } }, - { Instruction::PUSH27, { "PUSH27", 27, 0, 1 } }, - { Instruction::PUSH28, { "PUSH28", 28, 0, 1 } }, - { Instruction::PUSH29, { "PUSH29", 29, 0, 1 } }, - { Instruction::PUSH30, { "PUSH30", 30, 0, 1 } }, - { Instruction::PUSH31, { "PUSH31", 31, 0, 1 } }, - { Instruction::PUSH32, { "PUSH32", 32, 0, 1 } }, - { Instruction::DUP1, { "DUP1", 0, 1, 2 } }, - { Instruction::DUP2, { "DUP2", 0, 2, 3 } }, - { Instruction::DUP3, { "DUP3", 0, 3, 4 } }, - { Instruction::DUP4, { "DUP4", 0, 4, 5 } }, - { Instruction::DUP5, { "DUP5", 0, 5, 6 } }, - { Instruction::DUP6, { "DUP6", 0, 6, 7 } }, - { Instruction::DUP7, { "DUP7", 0, 7, 8 } }, - { Instruction::DUP8, { "DUP8", 0, 8, 9 } }, - { Instruction::DUP9, { "DUP9", 0, 9, 10 } }, - { Instruction::DUP10, { "DUP10", 0, 10, 11 } }, - { Instruction::DUP11, { "DUP11", 0, 11, 12 } }, - { Instruction::DUP12, { "DUP12", 0, 12, 13 } }, - { Instruction::DUP13, { "DUP13", 0, 13, 14 } }, - { Instruction::DUP14, { "DUP14", 0, 14, 15 } }, - { Instruction::DUP15, { "DUP15", 0, 15, 16 } }, - { Instruction::DUP16, { "DUP16", 0, 16, 17 } }, - { Instruction::SWAP1, { "SWAP1", 0, 2, 2 } }, - { Instruction::SWAP2, { "SWAP2", 0, 3, 3 } }, - { Instruction::SWAP3, { "SWAP3", 0, 4, 4 } }, - { Instruction::SWAP4, { "SWAP4", 0, 5, 5 } }, - { Instruction::SWAP5, { "SWAP5", 0, 6, 6 } }, - { Instruction::SWAP6, { "SWAP6", 0, 7, 7 } }, - { Instruction::SWAP7, { "SWAP7", 0, 8, 8 } }, - { Instruction::SWAP8, { "SWAP8", 0, 9, 9 } }, - { Instruction::SWAP9, { "SWAP9", 0, 10, 10 } }, - { Instruction::SWAP10, { "SWAP10", 0, 11, 11 } }, - { Instruction::SWAP11, { "SWAP11", 0, 12, 12 } }, - { Instruction::SWAP12, { "SWAP12", 0, 13, 13 } }, - { Instruction::SWAP13, { "SWAP13", 0, 14, 14 } }, - { Instruction::SWAP14, { "SWAP14", 0, 15, 15 } }, - { Instruction::SWAP15, { "SWAP15", 0, 16, 16 } }, - { Instruction::SWAP16, { "SWAP16", 0, 17, 17 } }, - { Instruction::LOG0, { "LOG0", 0, 1, 0 } }, - { Instruction::LOG1, { "LOG1", 0, 2, 0 } }, - { Instruction::LOG2, { "LOG2", 0, 3, 0 } }, - { Instruction::LOG3, { "LOG3", 0, 4, 0 } }, - { Instruction::LOG4, { "LOG4", 0, 5, 0 } }, - { Instruction::CREATE, { "CREATE", 0, 3, 1 } }, - { Instruction::CALL, { "CALL", 0, 7, 1 } }, - { Instruction::CALLCODE, { "CALLCODE", 0, 7, 1 } }, - { Instruction::RETURN, { "RETURN", 0, 2, 0 } }, - { Instruction::SUICIDE, { "SUICIDE", 0, 1, 0} } +{ // Add, Args, Ret, SideEffects + { Instruction::STOP, { "STOP", 0, 0, 0, true } }, + { Instruction::ADD, { "ADD", 0, 2, 1, false } }, + { Instruction::SUB, { "SUB", 0, 2, 1, false } }, + { Instruction::MUL, { "MUL", 0, 2, 1, false } }, + { Instruction::DIV, { "DIV", 0, 2, 1, false } }, + { Instruction::SDIV, { "SDIV", 0, 2, 1, false } }, + { Instruction::MOD, { "MOD", 0, 2, 1, false } }, + { Instruction::SMOD, { "SMOD", 0, 2, 1, false } }, + { Instruction::EXP, { "EXP", 0, 2, 1, false } }, + { Instruction::NOT, { "NOT", 0, 1, 1, false } }, + { Instruction::LT, { "LT", 0, 2, 1, false } }, + { Instruction::GT, { "GT", 0, 2, 1, false } }, + { Instruction::SLT, { "SLT", 0, 2, 1, false } }, + { Instruction::SGT, { "SGT", 0, 2, 1, false } }, + { Instruction::EQ, { "EQ", 0, 2, 1, false } }, + { Instruction::ISZERO, { "ISZERO", 0, 1, 1, false } }, + { Instruction::AND, { "AND", 0, 2, 1, false } }, + { Instruction::OR, { "OR", 0, 2, 1, false } }, + { Instruction::XOR, { "XOR", 0, 2, 1, false } }, + { Instruction::BYTE, { "BYTE", 0, 2, 1, false } }, + { Instruction::ADDMOD, { "ADDMOD", 0, 3, 1, false } }, + { Instruction::MULMOD, { "MULMOD", 0, 3, 1, false } }, + { Instruction::SIGNEXTEND, { "SIGNEXTEND", 0, 2, 1, false } }, + { Instruction::SHA3, { "SHA3", 0, 2, 1, false } }, + { Instruction::ADDRESS, { "ADDRESS", 0, 0, 1, false } }, + { Instruction::BALANCE, { "BALANCE", 0, 1, 1, false } }, + { Instruction::ORIGIN, { "ORIGIN", 0, 0, 1, false } }, + { Instruction::CALLER, { "CALLER", 0, 0, 1, false } }, + { Instruction::CALLVALUE, { "CALLVALUE", 0, 0, 1, false } }, + { Instruction::CALLDATALOAD,{ "CALLDATALOAD", 0, 1, 1, false } }, + { Instruction::CALLDATASIZE,{ "CALLDATASIZE", 0, 0, 1, false } }, + { Instruction::CALLDATACOPY,{ "CALLDATACOPY", 0, 3, 0, true } }, + { Instruction::CODESIZE, { "CODESIZE", 0, 0, 1, false } }, + { Instruction::CODECOPY, { "CODECOPY", 0, 3, 0, true } }, + { Instruction::GASPRICE, { "GASPRICE", 0, 0, 1, false } }, + { Instruction::EXTCODESIZE, { "EXTCODESIZE", 0, 1, 1, false } }, + { Instruction::EXTCODECOPY, { "EXTCODECOPY", 0, 4, 0, true } }, + { Instruction::PREVHASH, { "PREVHASH", 0, 0, 1, false } }, + { Instruction::COINBASE, { "COINBASE", 0, 0, 1, false } }, + { Instruction::TIMESTAMP, { "TIMESTAMP", 0, 0, 1, false } }, + { Instruction::NUMBER, { "NUMBER", 0, 0, 1, false } }, + { Instruction::DIFFICULTY, { "DIFFICULTY", 0, 0, 1, false } }, + { Instruction::GASLIMIT, { "GASLIMIT", 0, 0, 1, false } }, + { Instruction::POP, { "POP", 0, 1, 0, false } }, + { Instruction::MLOAD, { "MLOAD", 0, 1, 1, false } }, + { Instruction::MSTORE, { "MSTORE", 0, 2, 0, true } }, + { Instruction::MSTORE8, { "MSTORE8", 0, 2, 0, true } }, + { Instruction::SLOAD, { "SLOAD", 0, 1, 1, false } }, + { Instruction::SSTORE, { "SSTORE", 0, 2, 0, true } }, + { Instruction::JUMP, { "JUMP", 0, 1, 0, true } }, + { Instruction::JUMPI, { "JUMPI", 0, 2, 0, true } }, + { Instruction::PC, { "PC", 0, 0, 1, false } }, + { Instruction::MSIZE, { "MSIZE", 0, 0, 1, false } }, + { Instruction::GAS, { "GAS", 0, 0, 1, false } }, + { Instruction::JUMPDEST, { "JUMPDEST", 0, 1, 0, true } }, + { Instruction::PUSH1, { "PUSH1", 1, 0, 1, false } }, + { Instruction::PUSH2, { "PUSH2", 2, 0, 1, false } }, + { Instruction::PUSH3, { "PUSH3", 3, 0, 1, false } }, + { Instruction::PUSH4, { "PUSH4", 4, 0, 1, false } }, + { Instruction::PUSH5, { "PUSH5", 5, 0, 1, false } }, + { Instruction::PUSH6, { "PUSH6", 6, 0, 1, false } }, + { Instruction::PUSH7, { "PUSH7", 7, 0, 1, false } }, + { Instruction::PUSH8, { "PUSH8", 8, 0, 1, false } }, + { Instruction::PUSH9, { "PUSH9", 9, 0, 1, false } }, + { Instruction::PUSH10, { "PUSH10", 10, 0, 1, false } }, + { Instruction::PUSH11, { "PUSH11", 11, 0, 1, false } }, + { Instruction::PUSH12, { "PUSH12", 12, 0, 1, false } }, + { Instruction::PUSH13, { "PUSH13", 13, 0, 1, false } }, + { Instruction::PUSH14, { "PUSH14", 14, 0, 1, false } }, + { Instruction::PUSH15, { "PUSH15", 15, 0, 1, false } }, + { Instruction::PUSH16, { "PUSH16", 16, 0, 1, false } }, + { Instruction::PUSH17, { "PUSH17", 17, 0, 1, false } }, + { Instruction::PUSH18, { "PUSH18", 18, 0, 1, false } }, + { Instruction::PUSH19, { "PUSH19", 19, 0, 1, false } }, + { Instruction::PUSH20, { "PUSH20", 20, 0, 1, false } }, + { Instruction::PUSH21, { "PUSH21", 21, 0, 1, false } }, + { Instruction::PUSH22, { "PUSH22", 22, 0, 1, false } }, + { Instruction::PUSH23, { "PUSH23", 23, 0, 1, false } }, + { Instruction::PUSH24, { "PUSH24", 24, 0, 1, false } }, + { Instruction::PUSH25, { "PUSH25", 25, 0, 1, false } }, + { Instruction::PUSH26, { "PUSH26", 26, 0, 1, false } }, + { Instruction::PUSH27, { "PUSH27", 27, 0, 1, false } }, + { Instruction::PUSH28, { "PUSH28", 28, 0, 1, false } }, + { Instruction::PUSH29, { "PUSH29", 29, 0, 1, false } }, + { Instruction::PUSH30, { "PUSH30", 30, 0, 1, false } }, + { Instruction::PUSH31, { "PUSH31", 31, 0, 1, false } }, + { Instruction::PUSH32, { "PUSH32", 32, 0, 1, false } }, + { Instruction::DUP1, { "DUP1", 0, 1, 2, false } }, + { Instruction::DUP2, { "DUP2", 0, 2, 3, false } }, + { Instruction::DUP3, { "DUP3", 0, 3, 4, false } }, + { Instruction::DUP4, { "DUP4", 0, 4, 5, false } }, + { Instruction::DUP5, { "DUP5", 0, 5, 6, false } }, + { Instruction::DUP6, { "DUP6", 0, 6, 7, false } }, + { Instruction::DUP7, { "DUP7", 0, 7, 8, false } }, + { Instruction::DUP8, { "DUP8", 0, 8, 9, false } }, + { Instruction::DUP9, { "DUP9", 0, 9, 10, false } }, + { Instruction::DUP10, { "DUP10", 0, 10, 11, false } }, + { Instruction::DUP11, { "DUP11", 0, 11, 12, false } }, + { Instruction::DUP12, { "DUP12", 0, 12, 13, false } }, + { Instruction::DUP13, { "DUP13", 0, 13, 14, false } }, + { Instruction::DUP14, { "DUP14", 0, 14, 15, false } }, + { Instruction::DUP15, { "DUP15", 0, 15, 16, false } }, + { Instruction::DUP16, { "DUP16", 0, 16, 17, false } }, + { Instruction::SWAP1, { "SWAP1", 0, 2, 2, false } }, + { Instruction::SWAP2, { "SWAP2", 0, 3, 3, false } }, + { Instruction::SWAP3, { "SWAP3", 0, 4, 4, false } }, + { Instruction::SWAP4, { "SWAP4", 0, 5, 5, false } }, + { Instruction::SWAP5, { "SWAP5", 0, 6, 6, false } }, + { Instruction::SWAP6, { "SWAP6", 0, 7, 7, false } }, + { Instruction::SWAP7, { "SWAP7", 0, 8, 8, false } }, + { Instruction::SWAP8, { "SWAP8", 0, 9, 9, false } }, + { Instruction::SWAP9, { "SWAP9", 0, 10, 10, false } }, + { Instruction::SWAP10, { "SWAP10", 0, 11, 11, false } }, + { Instruction::SWAP11, { "SWAP11", 0, 12, 12, false } }, + { Instruction::SWAP12, { "SWAP12", 0, 13, 13, false } }, + { Instruction::SWAP13, { "SWAP13", 0, 14, 14, false } }, + { Instruction::SWAP14, { "SWAP14", 0, 15, 15, false } }, + { Instruction::SWAP15, { "SWAP15", 0, 16, 16, false } }, + { Instruction::SWAP16, { "SWAP16", 0, 17, 17, false } }, + { Instruction::LOG0, { "LOG0", 0, 1, 0, true } }, + { Instruction::LOG1, { "LOG1", 0, 2, 0, true } }, + { Instruction::LOG2, { "LOG2", 0, 3, 0, true } }, + { Instruction::LOG3, { "LOG3", 0, 4, 0, true } }, + { Instruction::LOG4, { "LOG4", 0, 5, 0, true } }, + { Instruction::CREATE, { "CREATE", 0, 3, 1, true } }, + { Instruction::CALL, { "CALL", 0, 7, 1, true } }, + { Instruction::CALLCODE, { "CALLCODE", 0, 7, 1, true } }, + { Instruction::RETURN, { "RETURN", 0, 2, 0, true } }, + { Instruction::SUICIDE, { "SUICIDE", 0, 1, 0, true } } }; string dev::eth::disassemble(bytes const& _mem) diff --git a/libevmcore/Instruction.h b/libevmcore/Instruction.h index 0892c50dc..eb85c0610 100644 --- a/libevmcore/Instruction.h +++ b/libevmcore/Instruction.h @@ -204,6 +204,7 @@ struct InstructionInfo int additional; ///< Additional items required in memory for this instructions (only for PUSH). int args; ///< Number of items required on the stack for this instruction (and, for the purposes of ret, the number taken from the stack). int ret; ///< Number of items placed (back) on the stack by this instruction, assuming args items were removed. + bool sideEffects; ///< false if the only effect on the execution environment (apart from gas usage) is a change to a topmost segment of the stack }; /// Information on all the instructions. diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index ce87f7bb0..ed2b1f45f 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -32,11 +32,11 @@ using namespace std; namespace dev { namespace solidity { -bytes Compiler::compile(ContractDefinition& _contract) +bytes Compiler::compile(ContractDefinition& _contract, bool _optimize) { Compiler compiler; compiler.compileContract(_contract); - return compiler.m_context.getAssembledBytecode(); + return compiler.m_context.getAssembledBytecode(_optimize); } void Compiler::compileContract(ContractDefinition& _contract) @@ -93,10 +93,11 @@ void Compiler::appendFunctionSelector(vector> con eth::AssemblyItem jumpTableStart = m_context.pushNewTag(); m_context << eth::Instruction::ADD << eth::Instruction::JUMP; - // jump table @todo it could be that the optimizer destroys this - m_context << jumpTableStart; + // jump table, tell the optimizer not to remove the JUMPDESTs + m_context << eth::AssemblyItem(eth::NoOptimizeBegin) << jumpTableStart; for (pair> const& f: publicFunctions) m_context.appendJumpTo(f.second.second) << eth::Instruction::JUMPDEST; + m_context << eth::AssemblyItem(eth::NoOptimizeEnd); m_context << returnTag << eth::Instruction::STOP; diff --git a/libsolidity/Compiler.h b/libsolidity/Compiler.h index 4e4d90d45..d931f5359 100644 --- a/libsolidity/Compiler.h +++ b/libsolidity/Compiler.h @@ -33,11 +33,11 @@ public: Compiler(): m_returnTag(m_context.newTag()) {} void compileContract(ContractDefinition& _contract); - bytes getAssembledBytecode() { return m_context.getAssembledBytecode(); } + bytes getAssembledBytecode(bool _optimize = false) { return m_context.getAssembledBytecode(_optimize); } void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); } /// Compile the given contract and return the EVM bytecode. - static bytes compile(ContractDefinition& _contract); + static bytes compile(ContractDefinition& _contract, bool _optimize); private: /// Creates a new compiler context / assembly and packs the current code into the data part. diff --git a/libsolidity/CompilerContext.h b/libsolidity/CompilerContext.h index 9f8658c3d..562c29321 100644 --- a/libsolidity/CompilerContext.h +++ b/libsolidity/CompilerContext.h @@ -84,7 +84,7 @@ public: eth::Assembly const& getAssembly() const { return m_asm; } void streamAssembly(std::ostream& _stream) const { _stream << m_asm; } - bytes getAssembledBytecode() const { return m_asm.assemble(); } + bytes getAssembledBytecode(bool _optimize = false) { return m_asm.optimise(_optimize).assemble(); } private: eth::Assembly m_asm; diff --git a/libsolidity/CompilerStack.cpp b/libsolidity/CompilerStack.cpp index bbd693ae5..c991171a5 100644 --- a/libsolidity/CompilerStack.cpp +++ b/libsolidity/CompilerStack.cpp @@ -34,7 +34,8 @@ namespace dev namespace solidity { -bytes CompilerStack::compile(std::string const& _sourceCode, shared_ptr _scanner) +bytes CompilerStack::compile(std::string const& _sourceCode, shared_ptr _scanner, + bool _optimize) { if (!_scanner) _scanner = make_shared(); @@ -42,7 +43,7 @@ bytes CompilerStack::compile(std::string const& _sourceCode, shared_ptr ASTPointer contract = Parser().parse(_scanner); NameAndTypeResolver().resolveNamesAndTypes(*contract); - return Compiler::compile(*contract); + return Compiler::compile(*contract, _optimize); } } diff --git a/libsolidity/CompilerStack.h b/libsolidity/CompilerStack.h index 9f3f81c04..b003745d2 100644 --- a/libsolidity/CompilerStack.h +++ b/libsolidity/CompilerStack.h @@ -36,7 +36,7 @@ class CompilerStack public: /// Compile the given @a _sourceCode to bytecode. If a scanner is provided, it is used for /// scanning the source code - this is useful for printing exception information. - static bytes compile(std::string const& _sourceCode, std::shared_ptr _scanner = std::shared_ptr()); + static bytes compile(std::string const& _sourceCode, std::shared_ptr _scanner = std::shared_ptr(), bool _optimize = false); }; } diff --git a/solc/main.cpp b/solc/main.cpp index 04fee2905..04fdc0ee1 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -42,7 +42,8 @@ void help() { cout << "Usage solc [OPTIONS] " << endl << "Options:" << endl - << " -h,--help Show this help message and exit." << endl + << " -o,--optimize Optimize the bytecode for size." << endl + << " -h,--help Show this help message and exit." << endl << " -V,--version Show the version and exit." << endl; exit(0); } @@ -58,10 +59,13 @@ void version() int main(int argc, char** argv) { string infile; + bool optimize = false; for (int i = 1; i < argc; ++i) { string arg = argv[i]; - if (arg == "-h" || arg == "--help") + if (arg == "-o" || arg == "--optimize") + optimize = true; + else if (arg == "-h" || arg == "--help") help(); else if (arg == "-V" || arg == "--version") version(); @@ -98,7 +102,7 @@ int main(int argc, char** argv) printer.print(cout); compiler.compileContract(*ast); - instructions = compiler.getAssembledBytecode(); + instructions = compiler.getAssembledBytecode(optimize); } catch (ParserError const& exception) {