2019-01-17 10:19:54 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/protoToYul.h>
|
2019-03-18 12:12:19 +00:00
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace yul::test::yul_fuzzer;
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
string ProtoConverter::createHex(string const& _hexBytes) const
|
2019-03-18 12:12:19 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
string tmp{_hexBytes};
|
2019-03-18 12:12:19 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
{
|
|
|
|
boost::range::remove_erase_if(tmp, [=](char c) -> bool {
|
|
|
|
return !std::isxdigit(c);
|
|
|
|
});
|
|
|
|
tmp = tmp.substr(0, 64);
|
|
|
|
}
|
2019-04-17 09:41:28 +00:00
|
|
|
// We need this awkward if case because hex literals cannot be empty.
|
2019-03-18 12:12:19 +00:00
|
|
|
if (tmp.empty())
|
|
|
|
tmp = "1";
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
string ProtoConverter::createAlphaNum(string const& _strBytes) const
|
2019-03-18 12:12:19 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
string tmp{_strBytes};
|
2019-03-18 12:12:19 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
{
|
|
|
|
boost::range::remove_erase_if(tmp, [=](char c) -> bool {
|
|
|
|
return !(std::isalpha(c) || std::isdigit(c));
|
|
|
|
});
|
|
|
|
tmp = tmp.substr(0, 32);
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2019-04-17 09:41:28 +00:00
|
|
|
bool ProtoConverter::isCaseLiteralUnique(Literal const& _x)
|
|
|
|
{
|
|
|
|
std::string tmp;
|
|
|
|
bool isUnique = false;
|
|
|
|
bool isEmptyString = false;
|
|
|
|
switch (_x.literal_oneof_case())
|
|
|
|
{
|
|
|
|
case Literal::kIntval:
|
|
|
|
tmp = std::to_string(_x.intval());
|
|
|
|
break;
|
|
|
|
case Literal::kHexval:
|
|
|
|
tmp = "0x" + createHex(_x.hexval());
|
|
|
|
break;
|
|
|
|
case Literal::kStrval:
|
|
|
|
tmp = createAlphaNum(_x.strval());
|
|
|
|
if (tmp.empty())
|
|
|
|
{
|
|
|
|
isEmptyString = true;
|
|
|
|
tmp = std::to_string(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp = "\"" + tmp + "\"";
|
|
|
|
break;
|
|
|
|
case Literal::LITERAL_ONEOF_NOT_SET:
|
|
|
|
tmp = std::to_string(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!_x.has_strval() || isEmptyString)
|
|
|
|
isUnique = m_switchLiteralSetPerScope.top().insert(dev::u256(tmp)).second;
|
|
|
|
else
|
|
|
|
isUnique = m_switchLiteralSetPerScope.top().insert(
|
|
|
|
dev::u256(dev::h256(tmp, dev::h256::FromBinary, dev::h256::AlignLeft))).second;
|
|
|
|
return isUnique;
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(Literal const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.literal_oneof_case())
|
|
|
|
{
|
|
|
|
case Literal::kIntval:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << _x.intval();
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
2019-03-18 12:12:19 +00:00
|
|
|
case Literal::kHexval:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "0x" << createHex(_x.hexval());
|
2019-03-18 12:12:19 +00:00
|
|
|
break;
|
|
|
|
case Literal::kStrval:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "\"" << createAlphaNum(_x.strval()) << "\"";
|
2019-03-18 12:12:19 +00:00
|
|
|
break;
|
2019-03-14 14:40:54 +00:00
|
|
|
case Literal::LITERAL_ONEOF_NOT_SET:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "1";
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
// Reference any index in [0, m_numLiveVars-1] or [0, m_numLiveVars)
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(VarRef const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "x_" << (static_cast<uint32_t>(_x.varnum()) % m_numLiveVars);
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(Expression const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.expr_oneof_case())
|
|
|
|
{
|
|
|
|
case Expression::kVarref:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.varref());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Expression::kCons:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.cons());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Expression::kBinop:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.binop());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Expression::kUnop:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.unop());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case Expression::kTop:
|
|
|
|
visit(_x.top());
|
|
|
|
break;
|
|
|
|
case Expression::kNop:
|
|
|
|
visit(_x.nop());
|
|
|
|
break;
|
2019-03-14 14:40:54 +00:00
|
|
|
case Expression::EXPR_ONEOF_NOT_SET:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "1";
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(BinaryOp const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case BinaryOp::ADD:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "add";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SUB:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sub";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::MUL:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "mul";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::DIV:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "div";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::MOD:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "mod";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::XOR:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "xor";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::AND:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "and";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::OR:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "or";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::EQ:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "eq";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::LT:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "lt";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::GT:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "gt";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
2019-03-14 21:26:25 +00:00
|
|
|
case BinaryOp::SHR:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "shr";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SHL:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "shl";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SAR:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sar";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SDIV:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sdiv";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SMOD:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "smod";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::EXP:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "exp";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SLT:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "slt";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SGT:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sgt";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::BYTE:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "byte";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SI:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "signextend";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::KECCAK:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "keccak256";
|
2019-03-14 21:26:25 +00:00
|
|
|
break;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "(";
|
|
|
|
visit(_x.left());
|
|
|
|
m_output << ",";
|
|
|
|
visit(_x.right());
|
|
|
|
m_output << ")";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
// New var numbering starts from x_10
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(VarDecl const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "let x_" << m_numLiveVars << " := ";
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.expr());
|
2019-03-20 15:21:38 +00:00
|
|
|
m_numVarsPerScope.top()++;
|
|
|
|
m_numLiveVars++;
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(TypedVarDecl const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "let x_" << m_numLiveVars;
|
2019-01-17 10:19:54 +00:00
|
|
|
switch (_x.type())
|
|
|
|
{
|
|
|
|
case TypedVarDecl::BOOL:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": bool := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : bool\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::S8:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": s8 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : s8\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::S32:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": s32 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : s32\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::S64:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": s64 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : s64\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::S128:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": s128 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : s128\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::S256:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": s256 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : s256\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::U8:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": u8 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : u8\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::U32:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": u32 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : u32\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::U64:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": u64 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : u64\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::U128:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": u128 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : u128\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case TypedVarDecl::U256:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << ": u256 := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << " : u256\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
m_numVarsPerScope.top()++;
|
|
|
|
m_numLiveVars++;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(UnaryOp const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case UnaryOp::NOT:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "not";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::MLOAD:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "mload";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::SLOAD:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sload";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::ISZERO:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "iszero";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case UnaryOp::CALLDATALOAD:
|
|
|
|
m_output << "calldataload";
|
|
|
|
break;
|
|
|
|
case UnaryOp::EXTCODESIZE:
|
|
|
|
m_output << "extcodesize";
|
|
|
|
break;
|
|
|
|
case UnaryOp::EXTCODEHASH:
|
|
|
|
m_output << "extcodehash";
|
|
|
|
break;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "(";
|
|
|
|
visit(_x.operand());
|
|
|
|
m_output << ")";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 09:52:30 +00:00
|
|
|
void ProtoConverter::visit(TernaryOp const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case TernaryOp::ADDM:
|
|
|
|
m_output << "addmod";
|
|
|
|
break;
|
|
|
|
case TernaryOp::MULM:
|
|
|
|
m_output << "mulmod";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.arg1());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.arg2());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.arg3());
|
|
|
|
m_output << ")";
|
|
|
|
}
|
|
|
|
|
2019-03-26 09:52:30 +00:00
|
|
|
void ProtoConverter::visit(NullaryOp const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case NullaryOp::PC:
|
|
|
|
m_output << "pc()";
|
|
|
|
break;
|
|
|
|
case NullaryOp::MSIZE:
|
|
|
|
m_output << "msize()";
|
|
|
|
break;
|
|
|
|
case NullaryOp::GAS:
|
|
|
|
m_output << "gas()";
|
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case NullaryOp::CALLDATASIZE:
|
|
|
|
m_output << "calldatasize()";
|
|
|
|
break;
|
|
|
|
case NullaryOp::CODESIZE:
|
|
|
|
m_output << "codesize()";
|
|
|
|
break;
|
|
|
|
case NullaryOp::RETURNDATASIZE:
|
|
|
|
m_output << "returndatasize()";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConverter::visit(CopyFunc const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.ct())
|
|
|
|
{
|
|
|
|
case CopyFunc::CALLDATA:
|
|
|
|
m_output << "calldatacopy";
|
|
|
|
break;
|
|
|
|
case CopyFunc::CODE:
|
|
|
|
m_output << "codecopy";
|
|
|
|
break;
|
|
|
|
case CopyFunc::RETURNDATA:
|
|
|
|
m_output << "returndatacopy";
|
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
}
|
2019-03-26 09:52:30 +00:00
|
|
|
m_output << "(";
|
|
|
|
visit(_x.target());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.source());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConverter::visit(ExtCodeCopy const& _x)
|
|
|
|
{
|
|
|
|
m_output << "extcodecopy";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.addr());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.target());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.source());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ")\n";
|
2019-03-26 09:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProtoConverter::visit(LogFunc const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.num_topics())
|
|
|
|
{
|
|
|
|
case LogFunc::ZERO:
|
|
|
|
m_output << "log0";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.pos());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ")\n";
|
|
|
|
break;
|
|
|
|
case LogFunc::ONE:
|
|
|
|
m_output << "log1";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.pos());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t1());
|
|
|
|
m_output << ")\n";
|
|
|
|
break;
|
|
|
|
case LogFunc::TWO:
|
|
|
|
m_output << "log2";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.pos());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t1());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t2());
|
|
|
|
m_output << ")\n";
|
|
|
|
break;
|
|
|
|
case LogFunc::THREE:
|
|
|
|
m_output << "log3";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.pos());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t1());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t2());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t3());
|
|
|
|
m_output << ")\n";
|
|
|
|
break;
|
|
|
|
case LogFunc::FOUR:
|
|
|
|
m_output << "log4";
|
|
|
|
m_output << "(";
|
|
|
|
visit(_x.pos());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.size());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t1());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t2());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t3());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.t4());
|
|
|
|
m_output << ")\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(AssignmentStatement const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.ref_id());
|
|
|
|
m_output << " := ";
|
|
|
|
visit(_x.expr());
|
|
|
|
m_output << "\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(IfStmt const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "if ";
|
|
|
|
visit(_x.cond());
|
|
|
|
m_output << " ";
|
|
|
|
visit(_x.if_body());
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(StoreFunc const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
|
|
|
switch (_x.st())
|
|
|
|
{
|
|
|
|
case StoreFunc::MSTORE:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "mstore(";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case StoreFunc::SSTORE:
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "sstore(";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case StoreFunc::MSTORE8:
|
|
|
|
m_output << "mstore8(";
|
|
|
|
break;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.loc());
|
|
|
|
m_output << ", ";
|
|
|
|
visit(_x.val());
|
|
|
|
m_output << ")\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(ForStmt const& _x)
|
2019-03-18 16:12:03 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
std::string loopVarName("i_" + std::to_string(m_numNestedForLoops++));
|
|
|
|
m_output << "for { let " << loopVarName << " := 0 } "
|
|
|
|
<< "lt(" << loopVarName << ", 0x60) "
|
|
|
|
<< "{ " << loopVarName << " := add(" << loopVarName << ", 0x20) } ";
|
2019-03-26 09:52:30 +00:00
|
|
|
m_inForScope.push(true);
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.for_body());
|
2019-03-26 09:52:30 +00:00
|
|
|
m_inForScope.pop();
|
2019-03-20 15:21:38 +00:00
|
|
|
--m_numNestedForLoops;
|
2019-03-18 16:12:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(CaseStmt const& _x)
|
2019-03-18 16:12:03 +00:00
|
|
|
{
|
2019-04-17 09:41:28 +00:00
|
|
|
// Silently ignore duplicate case literals
|
|
|
|
if (isCaseLiteralUnique(_x.case_lit()))
|
|
|
|
{
|
|
|
|
m_output << "case ";
|
|
|
|
visit(_x.case_lit());
|
|
|
|
m_output << " ";
|
|
|
|
visit(_x.case_block());
|
|
|
|
}
|
2019-03-18 16:12:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(SwitchStmt const& _x)
|
2019-03-18 16:12:03 +00:00
|
|
|
{
|
|
|
|
if (_x.case_stmt_size() > 0 || _x.has_default_block())
|
|
|
|
{
|
2019-04-17 09:41:28 +00:00
|
|
|
std::set<dev::u256> s;
|
|
|
|
m_switchLiteralSetPerScope.push(s);
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "switch ";
|
|
|
|
visit(_x.switch_expr());
|
|
|
|
m_output << "\n";
|
2019-04-17 09:41:28 +00:00
|
|
|
|
2019-03-18 16:12:03 +00:00
|
|
|
for (auto const& caseStmt: _x.case_stmt())
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(caseStmt);
|
2019-04-17 09:41:28 +00:00
|
|
|
|
|
|
|
m_switchLiteralSetPerScope.pop();
|
|
|
|
|
2019-03-18 16:12:03 +00:00
|
|
|
if (_x.has_default_block())
|
2019-03-20 15:21:38 +00:00
|
|
|
{
|
|
|
|
m_output << "default ";
|
|
|
|
visit(_x.default_block());
|
|
|
|
}
|
2019-03-18 16:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(Statement const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.stmt_oneof_case())
|
|
|
|
{
|
|
|
|
case Statement::kDecl:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.decl());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Statement::kAssignment:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.assignment());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Statement::kIfstmt:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.ifstmt());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Statement::kStorageFunc:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.storage_func());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
|
|
|
case Statement::kBlockstmt:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.blockstmt());
|
2019-03-14 14:40:54 +00:00
|
|
|
break;
|
2019-03-18 16:12:03 +00:00
|
|
|
case Statement::kForstmt:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.forstmt());
|
2019-03-18 16:12:03 +00:00
|
|
|
break;
|
|
|
|
case Statement::kSwitchstmt:
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_x.switchstmt());
|
2019-03-18 16:12:03 +00:00
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case Statement::kBreakstmt:
|
|
|
|
if (m_inForScope.top())
|
|
|
|
m_output << "break\n";
|
|
|
|
break;
|
|
|
|
case Statement::kContstmt:
|
|
|
|
if (m_inForScope.top())
|
|
|
|
m_output << "continue\n";
|
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case Statement::kLogFunc:
|
|
|
|
visit(_x.log_func());
|
|
|
|
break;
|
2019-03-26 09:52:30 +00:00
|
|
|
case Statement::kCopyFunc:
|
|
|
|
visit(_x.copy_func());
|
|
|
|
break;
|
|
|
|
case Statement::kExtcodeCopy:
|
|
|
|
visit(_x.extcode_copy());
|
|
|
|
break;
|
2019-03-14 14:40:54 +00:00
|
|
|
case Statement::STMT_ONEOF_NOT_SET:
|
|
|
|
break;
|
|
|
|
}
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(Block const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
|
|
|
if (_x.statements_size() > 0)
|
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_numVarsPerScope.push(0);
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "{\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
for (auto const& st: _x.statements())
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(st);
|
|
|
|
m_output << "}\n";
|
2019-03-20 15:21:38 +00:00
|
|
|
m_numLiveVars -= m_numVarsPerScope.top();
|
|
|
|
m_numVarsPerScope.pop();
|
2019-03-14 14:40:54 +00:00
|
|
|
}
|
|
|
|
else
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "{}\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
void ProtoConverter::visit(Function const& _x)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
m_output << "{\n"
|
|
|
|
<< "let a,b := foo(calldataload(0),calldataload(32),calldataload(64),calldataload(96),calldataload(128),"
|
|
|
|
<< "calldataload(160),calldataload(192),calldataload(224))\n"
|
|
|
|
<< "sstore(0, a)\n"
|
|
|
|
<< "sstore(32, b)\n"
|
|
|
|
<< "function foo(x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7) -> x_8, x_9\n";
|
|
|
|
visit(_x.statements());
|
|
|
|
m_output << "}\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
string ProtoConverter::functionToString(Function const& _input)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
visit(_input);
|
|
|
|
return m_output.str();
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
string ProtoConverter::protoToYul(const uint8_t* _data, size_t _size)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
|
|
|
Function message;
|
|
|
|
if (!message.ParsePartialFromArray(_data, _size))
|
|
|
|
return "#error invalid proto\n";
|
|
|
|
return functionToString(message);
|
2019-03-20 15:21:38 +00:00
|
|
|
}
|