2019-04-26 05:39:40 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-04-26 05:39:40 +00:00
|
|
|
/**
|
|
|
|
* Replace every u256 variable with four u64 variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
|
|
|
#include <libyul/optimiser/NameDispenser.h>
|
|
|
|
|
2019-06-25 09:27:58 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A stage that replace every u256 variable with four u64 variables.
|
|
|
|
* This transformation stage is required because values in EVM are 256 bits,
|
|
|
|
* but wasm only supports values up to 64 bits, so we use four u64 values to simulate
|
|
|
|
* one u256 value.
|
|
|
|
*
|
2019-11-06 16:25:03 +00:00
|
|
|
* For FunctionCall that accepts or returns u256 values, they accepts or returns
|
2019-04-26 05:39:40 +00:00
|
|
|
* four times the number of values after this transformation, with the order of significance,
|
|
|
|
* from the most significant to the least significant.
|
|
|
|
*
|
2019-11-06 16:25:03 +00:00
|
|
|
* For example, the FunctionCall MUL supplied by code generator
|
2019-04-26 05:39:40 +00:00
|
|
|
* should take 8 arguments and return 4 values (instead of 2 and 1) after this transformation.
|
|
|
|
*
|
|
|
|
* mul(a1, a2, a3, a4, b1, b2, b3, b4) -> c1, c2, c3, c4
|
|
|
|
*
|
|
|
|
* the value of c4 should be
|
|
|
|
* ((a1*(2^192) + a2*(2^128) + a3(2^64) + a4) * (b1*(2^192) + b2*(2^128) + b3(2^64) + b4)) & ((1<<64)-1)
|
|
|
|
*
|
2019-05-09 14:47:51 +00:00
|
|
|
* The resulting code still uses the EVM builtin functions but assumes that they
|
|
|
|
* take four times the parameters and each of type u64.
|
|
|
|
* In addition, it uses a single other builtin function called `or_bool` that
|
|
|
|
* takes four u64 parameters and is supposed to return the logical disjunction
|
2020-02-19 15:56:53 +00:00
|
|
|
* of them as a i32 value. If this name is already used somewhere, it is renamed.
|
2019-05-09 14:47:51 +00:00
|
|
|
*
|
2019-11-19 23:00:34 +00:00
|
|
|
* Prerequisite: Disambiguator, ForLoopConditionIntoBody, ExpressionSplitter
|
2019-04-26 05:39:40 +00:00
|
|
|
*/
|
|
|
|
class WordSizeTransform: public ASTModifier
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void operator()(FunctionDefinition&) override;
|
|
|
|
void operator()(FunctionCall&) override;
|
|
|
|
void operator()(If&) override;
|
|
|
|
void operator()(Switch&) override;
|
2019-06-18 16:14:49 +00:00
|
|
|
void operator()(ForLoop&) override;
|
2019-04-26 05:39:40 +00:00
|
|
|
void operator()(Block& _block) override;
|
|
|
|
|
2020-01-30 18:40:22 +00:00
|
|
|
static void run(
|
|
|
|
Dialect const& _inputDialect,
|
2020-02-20 15:17:09 +00:00
|
|
|
Dialect const& _targetDialect,
|
2020-01-30 18:40:22 +00:00
|
|
|
Block& _ast,
|
|
|
|
NameDispenser& _nameDispenser
|
|
|
|
);
|
2019-04-26 05:39:40 +00:00
|
|
|
|
|
|
|
private:
|
2019-12-19 16:58:20 +00:00
|
|
|
explicit WordSizeTransform(
|
|
|
|
Dialect const& _inputDialect,
|
2020-02-20 15:17:09 +00:00
|
|
|
Dialect const& _targetDialect,
|
|
|
|
NameDispenser& _nameDispenser
|
|
|
|
);
|
2019-04-26 05:39:40 +00:00
|
|
|
|
|
|
|
void rewriteVarDeclList(std::vector<TypedName>&);
|
|
|
|
void rewriteIdentifierList(std::vector<Identifier>&);
|
|
|
|
|
2019-06-25 09:27:58 +00:00
|
|
|
std::vector<Statement> handleSwitch(Switch& _switch);
|
|
|
|
std::vector<Statement> handleSwitchInternal(
|
2021-04-27 14:53:04 +00:00
|
|
|
std::shared_ptr<DebugData const> const& _debugData,
|
2019-06-25 09:27:58 +00:00
|
|
|
std::vector<YulString> const& _splitExpressions,
|
|
|
|
std::vector<Case> _cases,
|
|
|
|
YulString _runDefaultFlag,
|
|
|
|
size_t _depth
|
|
|
|
);
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
std::array<YulString, 4> generateU64IdentifierNames(YulString const& _s);
|
|
|
|
std::array<std::unique_ptr<Expression>, 4> expandValue(Expression const& _e);
|
2019-05-09 14:47:51 +00:00
|
|
|
std::vector<Expression> expandValueToVector(Expression const& _e);
|
2019-04-26 05:39:40 +00:00
|
|
|
|
2019-06-18 16:14:49 +00:00
|
|
|
Dialect const& m_inputDialect;
|
2020-02-20 15:17:09 +00:00
|
|
|
Dialect const& m_targetDialect;
|
2019-04-26 05:39:40 +00:00
|
|
|
NameDispenser& m_nameDispenser;
|
|
|
|
/// maps original u256 variable's name to corresponding u64 variables' names
|
|
|
|
std::map<YulString, std::array<YulString, 4>> m_variableMapping;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|