mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8777 from ethereum/sol_yul_simplefunctions
[Sol - Yul] Add some built-in functions.
This commit is contained in:
commit
1aef9c7d20
@ -585,7 +585,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
|||||||
case FunctionType::Kind::Internal:
|
case FunctionType::Kind::Internal:
|
||||||
{
|
{
|
||||||
vector<string> args;
|
vector<string> args;
|
||||||
for (unsigned i = 0; i < arguments.size(); ++i)
|
for (size_t i = 0; i < arguments.size(); ++i)
|
||||||
if (functionType->takesArbitraryParameters())
|
if (functionType->takesArbitraryParameters())
|
||||||
args.emplace_back(IRVariable(*arguments[i]).commaSeparatedList());
|
args.emplace_back(IRVariable(*arguments[i]).commaSeparatedList());
|
||||||
else
|
else
|
||||||
@ -829,15 +829,43 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FunctionType::Kind::GasLeft:
|
case FunctionType::Kind::AddMod:
|
||||||
|
case FunctionType::Kind::MulMod:
|
||||||
{
|
{
|
||||||
define(_functionCall) << "gas()\n";
|
static map<FunctionType::Kind, string> functions = {
|
||||||
|
{FunctionType::Kind::AddMod, "addmod"},
|
||||||
|
{FunctionType::Kind::MulMod, "mulmod"},
|
||||||
|
};
|
||||||
|
solAssert(functions.find(functionType->kind()) != functions.end(), "");
|
||||||
|
solAssert(arguments.size() == 3 && parameterTypes.size() == 3, "");
|
||||||
|
|
||||||
|
IRVariable modulus(m_context.newYulVariable(), *(parameterTypes[2]));
|
||||||
|
define(modulus, *arguments[2]);
|
||||||
|
Whiskers templ("if iszero(<modulus>) { invalid() }\n");
|
||||||
|
m_code << templ("modulus", modulus.name()).render();
|
||||||
|
|
||||||
|
string args;
|
||||||
|
for (size_t i = 0; i < 2; ++i)
|
||||||
|
args += expressionAsType(*arguments[i], *(parameterTypes[i])) + ", ";
|
||||||
|
args += modulus.name();
|
||||||
|
define(_functionCall) << functions[functionType->kind()] << "(" << args << ")\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case FunctionType::Kind::GasLeft:
|
||||||
case FunctionType::Kind::Selfdestruct:
|
case FunctionType::Kind::Selfdestruct:
|
||||||
|
case FunctionType::Kind::BlockHash:
|
||||||
{
|
{
|
||||||
solAssert(arguments.size() == 1, "");
|
static map<FunctionType::Kind, string> functions = {
|
||||||
define(_functionCall) << "selfdestruct(" << expressionAsType(*arguments.front(), *parameterTypes.front()) << ")\n";
|
{FunctionType::Kind::GasLeft, "gas"},
|
||||||
|
{FunctionType::Kind::Selfdestruct, "selfdestruct"},
|
||||||
|
{FunctionType::Kind::BlockHash, "blockhash"},
|
||||||
|
};
|
||||||
|
solAssert(functions.find(functionType->kind()) != functions.end(), "");
|
||||||
|
|
||||||
|
string args;
|
||||||
|
for (size_t i = 0; i < arguments.size(); ++i)
|
||||||
|
args += (args.empty() ? "" : ", ") + expressionAsType(*arguments[i], *(parameterTypes[i]));
|
||||||
|
define(_functionCall) << functions[functionType->kind()] << "(" << args << ")\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FunctionType::Kind::Log0:
|
case FunctionType::Kind::Log0:
|
||||||
|
@ -18,6 +18,8 @@ contract C {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
// ----
|
// ----
|
||||||
// f(uint256): 0 -> FAILURE
|
// f(uint256): 0 -> FAILURE
|
||||||
// g(uint256): 0 -> FAILURE
|
// g(uint256): 0 -> FAILURE
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
contract C {
|
||||||
|
function f() public returns(bytes32) {
|
||||||
|
return blockhash(1);
|
||||||
|
}
|
||||||
|
function g() public returns(bytes32) {
|
||||||
|
return blockhash(2);
|
||||||
|
}
|
||||||
|
function h() public returns(bytes32) {
|
||||||
|
return blockhash(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> 0x3737373737373737373737373737373737373737373737373737373737373738
|
||||||
|
// g() -> 0x3737373737373737373737373737373737373737373737373737373737373739
|
||||||
|
// h() -> 0x373737373737373737373737373737373737373737373737373737373737373a
|
Loading…
Reference in New Issue
Block a user