Merge remote-tracking branch 'origin/develop' into breaking

This commit is contained in:
chriseth
2020-06-15 17:11:41 +02:00
132 changed files with 1083 additions and 417 deletions
+2 -2
View File
@@ -137,7 +137,7 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
{
bool insideFunction = m_currentScope->insideFunction();
size_t stackSize = m_resolver(_identifier, yul::IdentifierContext::RValue, insideFunction);
if (stackSize != size_t(-1))
if (stackSize != numeric_limits<size_t>::max())
{
found = true;
yulAssert(stackSize == 1, "Invalid stack size of external reference.");
@@ -479,7 +479,7 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueT
{
bool insideFunction = m_currentScope->insideFunction();
size_t variableSize = m_resolver(_variable, yul::IdentifierContext::LValue, insideFunction);
if (variableSize != size_t(-1))
if (variableSize != numeric_limits<size_t>::max())
{
found = true;
variableType = &m_dialect.defaultType;
+1 -1
View File
@@ -45,7 +45,7 @@ string solidity::yul::reindent(string const& _code)
auto const static countBraces = [](string const& _s) noexcept -> int
{
auto const i = _s.find("//");
auto const e = i == _s.npos ? end(_s) : next(begin(_s), i);
auto const e = i == _s.npos ? end(_s) : next(begin(_s), static_cast<ptrdiff_t>(i));
auto const opening = count_if(begin(_s), e, [](auto ch) { return ch == '{' || ch == '('; });
auto const closing = count_if(begin(_s), e, [](auto ch) { return ch == '}' || ch == ')'; });
return opening - closing;
+2 -2
View File
@@ -71,10 +71,10 @@ public:
{
// FNV hash - can be replaced by a better one, e.g. xxhash64
std::uint64_t hash = emptyHash();
for (auto c: v)
for (char c: v)
{
hash *= 1099511628211u;
hash ^= c;
hash ^= static_cast<uint64_t>(c);
}
return hash;
+3 -3
View File
@@ -143,14 +143,14 @@ pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EthAssemblyAdapter::
{
shared_ptr<evmasm::Assembly> assembly{make_shared<evmasm::Assembly>()};
auto sub = m_assembly.newSub(assembly);
return {make_shared<EthAssemblyAdapter>(*assembly), size_t(sub.data())};
return {make_shared<EthAssemblyAdapter>(*assembly), static_cast<size_t>(sub.data())};
}
void EthAssemblyAdapter::appendDataOffset(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineOffset(size_t(_sub));
m_assembly.pushSubroutineOffset(_sub);
else
m_assembly << evmasm::AssemblyItem(evmasm::PushData, it->second);
}
@@ -159,7 +159,7 @@ void EthAssemblyAdapter::appendDataSize(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineSize(size_t(_sub));
m_assembly.pushSubroutineSize(static_cast<size_t>(_sub));
else
m_assembly << u256(m_assembly.data(h256(it->second)).size());
}
+3 -3
View File
@@ -74,7 +74,7 @@ void EVMAssembly::appendLabelReference(LabelID _labelId)
EVMAssembly::LabelID EVMAssembly::newLabelId()
{
m_labelPositions[m_nextLabelId] = size_t(-1);
m_labelPositions[m_nextLabelId] = numeric_limits<size_t>::max();
return m_nextLabelId++;
}
@@ -165,7 +165,7 @@ evmasm::LinkerObject EVMAssembly::finalize()
size_t referencePos = ref.first;
yulAssert(m_labelPositions.count(ref.second), "");
size_t labelPos = m_labelPositions.at(ref.second);
yulAssert(labelPos != size_t(-1), "Undefined but allocated label used.");
yulAssert(labelPos != numeric_limits<size_t>::max(), "Undefined but allocated label used.");
updateReference(referencePos, labelReferenceSize, u256(labelPos));
}
@@ -177,7 +177,7 @@ evmasm::LinkerObject EVMAssembly::finalize()
void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId)
{
yulAssert(m_labelPositions.count(_labelId), "Label not found.");
yulAssert(m_labelPositions[_labelId] == size_t(-1), "Label already set.");
yulAssert(m_labelPositions[_labelId] == numeric_limits<size_t>::max(), "Label already set.");
m_labelPositions[_labelId] = m_bytecode.size();
}
+38 -31
View File
@@ -175,28 +175,29 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl)
{
yulAssert(m_scope, "");
int const numVariables = _varDecl.variables.size();
int heightAtStart = m_assembly.stackHeight();
size_t const numVariables = _varDecl.variables.size();
auto heightAtStart = static_cast<size_t>(m_assembly.stackHeight());
if (_varDecl.value)
{
std::visit(*this, *_varDecl.value);
expectDeposit(numVariables, heightAtStart);
expectDeposit(static_cast<int>(numVariables), static_cast<int>(heightAtStart));
}
else
{
m_assembly.setSourceLocation(_varDecl.location);
int variablesLeft = numVariables;
size_t variablesLeft = numVariables;
while (variablesLeft--)
m_assembly.appendConstant(u256(0));
}
m_assembly.setSourceLocation(_varDecl.location);
bool atTopOfStack = true;
for (int varIndex = numVariables - 1; varIndex >= 0; --varIndex)
for (size_t varIndex = 0; varIndex < numVariables; ++varIndex)
{
YulString varName = _varDecl.variables[varIndex].name;
size_t varIndexReverse = numVariables - 1 - varIndex;
YulString varName = _varDecl.variables[varIndexReverse].name;
auto& var = std::get<Scope::Variable>(m_scope->identifiers.at(varName));
m_context->variableStackHeights[&var] = heightAtStart + varIndex;
m_context->variableStackHeights[&var] = heightAtStart + varIndexReverse;
if (!m_allowStackOpt)
continue;
@@ -214,10 +215,10 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl)
atTopOfStack = false;
else
{
int slot = *m_unusedStackSlots.begin();
auto slot = static_cast<size_t>(*m_unusedStackSlots.begin());
m_unusedStackSlots.erase(m_unusedStackSlots.begin());
m_context->variableStackHeights[&var] = slot;
if (int heightDiff = variableHeightDiff(var, varName, true))
if (size_t heightDiff = variableHeightDiff(var, varName, true))
m_assembly.appendInstruction(evmasm::swapInstruction(heightDiff - 1));
m_assembly.appendInstruction(evmasm::Instruction::POP);
}
@@ -240,7 +241,7 @@ void CodeTransform::operator()(Assignment const& _assignment)
{
int height = m_assembly.stackHeight();
std::visit(*this, *_assignment.value);
expectDeposit(_assignment.variableNames.size(), height);
expectDeposit(static_cast<int>(_assignment.variableNames.size()), height);
m_assembly.setSourceLocation(_assignment.location);
generateMultiAssignment(_assignment.variableNames);
@@ -263,7 +264,7 @@ void CodeTransform::operator()(FunctionCall const& _call)
else
{
m_assembly.setSourceLocation(_call.location);
EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
EVMAssembly::LabelID returnLabel(numeric_limits<EVMAssembly::LabelID>::max()); // only used for evm 1.0
if (!m_evm15)
{
returnLabel = m_assembly.newLabelId();
@@ -281,10 +282,17 @@ void CodeTransform::operator()(FunctionCall const& _call)
visitExpression(arg);
m_assembly.setSourceLocation(_call.location);
if (m_evm15)
m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size());
m_assembly.appendJumpsub(
functionEntryID(_call.functionName.name, *function),
static_cast<int>(function->arguments.size()),
static_cast<int>(function->returns.size())
);
else
{
m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1);
m_assembly.appendJumpTo(
functionEntryID(_call.functionName.name, *function),
static_cast<int>(function->returns.size() - function->arguments.size()) - 1
);
m_assembly.appendLabel(returnLabel);
}
}
@@ -300,7 +308,7 @@ void CodeTransform::operator()(Identifier const& _identifier)
{
// TODO: opportunity for optimization: Do not DUP if this is the last reference
// to the top most element of the stack
if (int heightDiff = variableHeightDiff(_var, _identifier.name, false))
if (size_t heightDiff = variableHeightDiff(_var, _identifier.name, false))
m_assembly.appendInstruction(evmasm::dupInstruction(heightDiff));
else
// Store something to balance the stack
@@ -407,7 +415,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
int const stackHeightBefore = m_assembly.stackHeight();
if (m_evm15)
m_assembly.appendBeginsub(functionEntryID(_function.name, function), _function.parameters.size());
m_assembly.appendBeginsub(functionEntryID(_function.name, function), static_cast<int>(_function.parameters.size()));
else
m_assembly.appendLabel(functionEntryID(_function.name, function));
@@ -465,15 +473,15 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
// modified parallel to the actual stack.
vector<int> stackLayout;
if (!m_evm15)
stackLayout.push_back(_function.returnVariables.size()); // Move return label to the top
stackLayout.push_back(static_cast<int>(_function.returnVariables.size())); // Move return label to the top
stackLayout += vector<int>(_function.parameters.size(), -1); // discard all arguments
for (size_t i = 0; i < _function.returnVariables.size(); ++i)
stackLayout.push_back(i); // Move return values down, but keep order.
stackLayout.push_back(static_cast<int>(i)); // Move return values down, but keep order.
if (stackLayout.size() > 17)
{
StackTooDeepError error(_function.name, YulString{}, stackLayout.size() - 17);
StackTooDeepError error(_function.name, YulString{}, static_cast<int>(stackLayout.size()) - 17);
error << errinfo_comment(
"The function " +
_function.name.str() +
@@ -481,11 +489,11 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
to_string(stackLayout.size() - 17) +
" parameters or return variables too many to fit the stack size."
);
stackError(std::move(error), m_assembly.stackHeight() - _function.parameters.size());
stackError(std::move(error), m_assembly.stackHeight() - static_cast<int>(_function.parameters.size()));
}
else
{
while (!stackLayout.empty() && stackLayout.back() != int(stackLayout.size() - 1))
while (!stackLayout.empty() && stackLayout.back() != static_cast<int>(stackLayout.size() - 1))
if (stackLayout.back() < 0)
{
m_assembly.appendInstruction(evmasm::Instruction::POP);
@@ -493,17 +501,17 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
}
else
{
m_assembly.appendInstruction(evmasm::swapInstruction(stackLayout.size() - stackLayout.back() - 1));
swap(stackLayout[stackLayout.back()], stackLayout.back());
m_assembly.appendInstruction(evmasm::swapInstruction(stackLayout.size() - static_cast<size_t>(stackLayout.back()) - 1));
swap(stackLayout[static_cast<size_t>(stackLayout.back())], stackLayout.back());
}
for (int i = 0; size_t(i) < stackLayout.size(); ++i)
yulAssert(i == stackLayout[i], "Error reshuffling stack.");
for (size_t i = 0; i < stackLayout.size(); ++i)
yulAssert(i == static_cast<size_t>(stackLayout[i]), "Error reshuffling stack.");
}
}
if (m_evm15)
m_assembly.appendReturnsub(_function.returnVariables.size(), stackHeightBefore);
m_assembly.appendReturnsub(static_cast<int>(_function.returnVariables.size()), stackHeightBefore);
else
m_assembly.appendJump(stackHeightBefore - _function.returnVariables.size());
m_assembly.appendJump(stackHeightBefore - static_cast<int>(_function.returnVariables.size()));
m_assembly.setStackHeight(stackHeightBefore);
}
@@ -683,7 +691,7 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
if (auto var = m_scope->lookup(_variableName.name))
{
Scope::Variable const& _var = std::get<Scope::Variable>(*var);
if (int heightDiff = variableHeightDiff(_var, _variableName.name, true))
if (size_t heightDiff = variableHeightDiff(_var, _variableName.name, true))
m_assembly.appendInstruction(evmasm::swapInstruction(heightDiff - 1));
m_assembly.appendInstruction(evmasm::Instruction::POP);
decreaseReference(_variableName.name, _var);
@@ -698,12 +706,12 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
}
}
int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
size_t CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
{
yulAssert(m_context->variableStackHeights.count(&_var), "");
int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var];
size_t heightDiff = static_cast<size_t>(m_assembly.stackHeight()) - m_context->variableStackHeights[&_var];
yulAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
int limit = _forSwap ? 17 : 16;
size_t limit = _forSwap ? 17 : 16;
if (heightDiff > limit)
{
m_stackErrors.emplace_back(_varName, heightDiff - limit);
@@ -723,4 +731,3 @@ void CodeTransform::expectDeposit(int _deposit, int _oldHeight) const
{
yulAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit.");
}
+2 -2
View File
@@ -54,7 +54,7 @@ struct StackTooDeepError: virtual YulException
struct CodeTransformContext
{
std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs;
std::map<Scope::Variable const*, int> variableStackHeights;
std::map<Scope::Variable const*, size_t> variableStackHeights;
std::map<Scope::Variable const*, unsigned> variableReferences;
struct JumpInfo
@@ -200,7 +200,7 @@ private:
/// Determines the stack height difference to the given variables. Throws
/// if it is not yet in scope or the height difference is too large. Returns
/// the (positive) stack height difference otherwise.
int variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap);
size_t variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap);
void expectDeposit(int _deposit, int _oldHeight) const;
+2 -2
View File
@@ -63,8 +63,8 @@ pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
evmasm::InstructionInfo info = evmasm::instructionInfo(_instruction);
BuiltinFunctionForEVM f;
f.name = YulString{_name};
f.parameters.resize(info.args);
f.returns.resize(info.ret);
f.parameters.resize(static_cast<size_t>(info.args));
f.returns.resize(static_cast<size_t>(info.ret));
f.sideEffects = EVMDialect::sideEffectsOfInstruction(_instruction);
f.controlFlowSideEffects.terminates = evmasm::SemanticInformation::terminatesControlFlow(_instruction);
f.controlFlowSideEffects.reverts = evmasm::SemanticInformation::reverts(_instruction);
+5 -1
View File
@@ -91,7 +91,11 @@ void GasMeterVisitor::operator()(Literal const& _lit)
m_runGas += evmasm::GasMeter::runGas(evmasm::Instruction::PUSH1);
m_dataGas +=
singleByteDataGas() +
size_t(evmasm::GasMeter::dataGas(toCompactBigEndian(valueOfLiteral(_lit), 1), m_isCreation, m_dialect.evmVersion()));
static_cast<size_t>(evmasm::GasMeter::dataGas(
toCompactBigEndian(valueOfLiteral(_lit), 1),
m_isCreation,
m_dialect.evmVersion()
));
}
void GasMeterVisitor::operator()(Identifier const&)
+84 -30
View File
@@ -22,9 +22,11 @@
#include <libyul/Exceptions.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/Visitor.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/adaptor/transformed.hpp>
using namespace std;
using namespace solidity;
@@ -82,6 +84,16 @@ bytes toBytes(ValueType _vt)
return toBytes(uint8_t(_vt));
}
ValueType toValueType(wasm::Type _type)
{
if (_type == wasm::Type::i32)
return ValueType::I32;
else if (_type == wasm::Type::i64)
return ValueType::I64;
else
yulAssert(false, "Invalid wasm variable type");
}
enum class Export: uint8_t
{
Function = 0x0,
@@ -131,6 +143,16 @@ bytes toBytes(Opcode _o)
return toBytes(uint8_t(_o));
}
Opcode constOpcodeFor(ValueType _type)
{
if (_type == ValueType::I32)
return Opcode::I32Const;
else if (_type == ValueType::I64)
return Opcode::I64Const;
else
yulAssert(false, "Values of this type cannot be used with const opcode");
}
static map<string, uint8_t> const builtins = {
{"i32.load", 0x28},
{"i64.load", 0x29},
@@ -249,6 +271,34 @@ bytes makeSection(Section _section, bytes _data)
return toBytes(_section) + prefixSize(move(_data));
}
/// This is a kind of run-length-encoding of local types.
vector<pair<size_t, ValueType>> groupLocalVariables(vector<VariableDeclaration> _localVariables)
{
vector<pair<size_t, ValueType>> localEntries;
size_t entrySize = 0;
ValueType entryType = ValueType::I32; // Any type would work here
for (VariableDeclaration const& localVariable: _localVariables)
{
ValueType variableType = toValueType(localVariable.type);
if (variableType != entryType)
{
if (entrySize > 0)
localEntries.emplace_back(entrySize, entryType);
entryType = variableType;
entrySize = 0;
}
++entrySize;
}
if (entrySize > 0)
localEntries.emplace_back(entrySize, entryType);
return localEntries;
}
}
bytes BinaryTransform::run(Module const& _module)
@@ -297,7 +347,10 @@ bytes BinaryTransform::run(Module const& _module)
bytes BinaryTransform::operator()(Literal const& _literal)
{
return toBytes(Opcode::I64Const) + lebEncodeSigned(_literal.value);
return std::visit(GenericVisitor{
[&](uint32_t _value) -> bytes { return toBytes(Opcode::I32Const) + lebEncodeSigned(static_cast<int32_t>(_value)); },
[&](uint64_t _value) -> bytes { return toBytes(Opcode::I64Const) + lebEncodeSigned(static_cast<int64_t>(_value)); },
}, _literal.value);
}
bytes BinaryTransform::operator()(StringLiteral const&)
@@ -323,12 +376,12 @@ bytes BinaryTransform::operator()(BuiltinCall const& _call)
if (_call.functionName == "dataoffset")
{
string name = get<StringLiteral>(_call.arguments.at(0)).value;
return toBytes(Opcode::I64Const) + lebEncodeSigned(m_subModulePosAndSize.at(name).first);
return toBytes(Opcode::I64Const) + lebEncodeSigned(static_cast<int64_t>(m_subModulePosAndSize.at(name).first));
}
else if (_call.functionName == "datasize")
{
string name = get<StringLiteral>(_call.arguments.at(0)).value;
return toBytes(Opcode::I64Const) + lebEncodeSigned(m_subModulePosAndSize.at(name).second);
return toBytes(Opcode::I64Const) + lebEncodeSigned(static_cast<int64_t>(m_subModulePosAndSize.at(name).second));
}
bytes args = visit(_call.arguments);
@@ -443,21 +496,18 @@ bytes BinaryTransform::operator()(FunctionDefinition const& _function)
{
bytes ret;
// This is a kind of run-length-encoding of local types. Has to be adapted once
// we have locals of different types.
if (_function.locals.size() == 0)
ret += lebEncode(0); // number of locals groups
else
vector<pair<size_t, ValueType>> localEntries = groupLocalVariables(_function.locals);
ret += lebEncode(localEntries.size());
for (pair<size_t, ValueType> const& entry: localEntries)
{
ret += lebEncode(1); // number of locals groups
ret += lebEncode(_function.locals.size());
ret += toBytes(ValueType::I64);
ret += lebEncode(entry.first);
ret += toBytes(entry.second);
}
m_locals.clear();
size_t varIdx = 0;
for (size_t i = 0; i < _function.parameterNames.size(); ++i)
m_locals[_function.parameterNames[i]] = varIdx++;
for (size_t i = 0; i < _function.parameters.size(); ++i)
m_locals[_function.parameters[i].name] = varIdx++;
for (size_t i = 0; i < _function.locals.size(); ++i)
m_locals[_function.locals[i].variableName] = varIdx++;
@@ -475,38 +525,39 @@ BinaryTransform::Type BinaryTransform::typeOf(FunctionImport const& _import)
{
return {
encodeTypes(_import.paramTypes),
encodeTypes(_import.returnType ? vector<string>(1, *_import.returnType) : vector<string>())
encodeTypes(_import.returnType ? vector<wasm::Type>(1, *_import.returnType) : vector<wasm::Type>())
};
}
BinaryTransform::Type BinaryTransform::typeOf(FunctionDefinition const& _funDef)
{
return {
encodeTypes(vector<string>(_funDef.parameterNames.size(), "i64")),
encodeTypes(vector<string>(_funDef.returns ? 1 : 0, "i64"))
encodeTypes(_funDef.parameters),
encodeTypes(_funDef.returnType ? vector<wasm::Type>(1, *_funDef.returnType) : vector<wasm::Type>())
};
}
uint8_t BinaryTransform::encodeType(string const& _typeName)
uint8_t BinaryTransform::encodeType(wasm::Type _type)
{
if (_typeName == "i32")
return uint8_t(ValueType::I32);
else if (_typeName == "i64")
return uint8_t(ValueType::I64);
else
yulAssert(false, "");
return 0;
return uint8_t(toValueType(_type));
}
vector<uint8_t> BinaryTransform::encodeTypes(vector<string> const& _typeNames)
vector<uint8_t> BinaryTransform::encodeTypes(vector<wasm::Type> const& _types)
{
vector<uint8_t> result;
for (auto const& t: _typeNames)
for (wasm::Type t: _types)
result.emplace_back(encodeType(t));
return result;
}
vector<uint8_t> BinaryTransform::encodeTypes(wasm::TypedNameList const& _typedNameList)
{
vector<uint8_t> result;
for (TypedName const& typedName: _typedNameList)
result.emplace_back(encodeType(typedName.type));
return result;
}
map<BinaryTransform::Type, vector<string>> BinaryTransform::typeToFunctionMap(
vector<wasm::FunctionImport> const& _imports,
vector<wasm::FunctionDefinition> const& _functions
@@ -612,13 +663,16 @@ bytes BinaryTransform::memorySection()
bytes BinaryTransform::globalSection(vector<wasm::GlobalVariableDeclaration> const& _globals)
{
bytes result = lebEncode(_globals.size());
for (size_t i = 0; i < _globals.size(); ++i)
for (wasm::GlobalVariableDeclaration const& global: _globals)
{
ValueType globalType = toValueType(global.type);
result +=
toBytes(ValueType::I64) +
toBytes(globalType) +
lebEncode(static_cast<uint8_t>(Mutability::Var)) +
toBytes(Opcode::I64Const) +
toBytes(constOpcodeFor(globalType)) +
lebEncodeSigned(0) +
toBytes(Opcode::End);
}
return makeSection(Section::GLOBAL, move(result));
}
+3 -2
View File
@@ -71,8 +71,9 @@ private:
static Type typeOf(wasm::FunctionImport const& _import);
static Type typeOf(wasm::FunctionDefinition const& _funDef);
static uint8_t encodeType(std::string const& _typeName);
static std::vector<uint8_t> encodeTypes(std::vector<std::string> const& _typeNames);
static uint8_t encodeType(wasm::Type _type);
static std::vector<uint8_t> encodeTypes(std::vector<wasm::Type> const& _types);
static std::vector<uint8_t> encodeTypes(wasm::TypedNameList const& _typedNameList);
static std::map<Type, std::vector<std::string>> typeToFunctionMap(
std::vector<wasm::FunctionImport> const& _imports,
+25 -9
View File
@@ -20,7 +20,10 @@
#include <libyul/backends/wasm/TextTransform.h>
#include <libyul/Exceptions.h>
#include <libsolutil/StringUtils.h>
#include <libsolutil/Visitor.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
@@ -44,9 +47,9 @@ string TextTransform::run(wasm::Module const& _module)
{
ret += " (import \"" + imp.module + "\" \"" + imp.externalName + "\" (func $" + imp.internalName;
if (!imp.paramTypes.empty())
ret += " (param" + joinHumanReadablePrefixed(imp.paramTypes, " ", " ") + ")";
ret += " (param" + joinHumanReadablePrefixed(imp.paramTypes | boost::adaptors::transformed(encodeType), " ", " ") + ")";
if (imp.returnType)
ret += " (result " + *imp.returnType + ")";
ret += " (result " + encodeType(*imp.returnType) + ")";
ret += "))\n";
}
@@ -56,7 +59,7 @@ string TextTransform::run(wasm::Module const& _module)
ret += " (export \"main\" (func $main))\n";
for (auto const& g: _module.globals)
ret += " (global $" + g.variableName + " (mut i64) (i64.const 0))\n";
ret += " (global $" + g.variableName + " (mut " + encodeType(g.type) + ") (" + encodeType(g.type) + ".const 0))\n";
ret += "\n";
for (auto const& f: _module.functions)
ret += transform(f) + "\n";
@@ -65,7 +68,10 @@ string TextTransform::run(wasm::Module const& _module)
string TextTransform::operator()(wasm::Literal const& _literal)
{
return "(i64.const " + to_string(_literal.value) + ")";
return std::visit(GenericVisitor{
[&](uint32_t _value) -> string { return "(i32.const " + to_string(_value) + ")"; },
[&](uint64_t _value) -> string { return "(i64.const " + to_string(_value) + ")"; },
}, _literal.value);
}
string TextTransform::operator()(wasm::StringLiteral const& _literal)
@@ -162,12 +168,12 @@ string TextTransform::indented(string const& _in)
string TextTransform::transform(wasm::FunctionDefinition const& _function)
{
string ret = "(func $" + _function.name + "\n";
for (auto const& param: _function.parameterNames)
ret += " (param $" + param + " i64)\n";
if (_function.returns)
ret += " (result i64)\n";
for (auto const& param: _function.parameters)
ret += " (param $" + param.name + " " + encodeType(param.type) + ")\n";
if (_function.returnType.has_value())
ret += " (result " + encodeType(_function.returnType.value()) + ")\n";
for (auto const& local: _function.locals)
ret += " (local $" + local.variableName + " i64)\n";
ret += " (local $" + local.variableName + " " + encodeType(local.type) + ")\n";
ret += indented(joinTransformed(_function.body, '\n'));
if (ret.back() != '\n')
ret += '\n';
@@ -193,3 +199,13 @@ string TextTransform::joinTransformed(vector<wasm::Expression> const& _expressio
}
return ret;
}
string TextTransform::encodeType(wasm::Type _type)
{
if (_type == wasm::Type::i32)
return "i32";
else if (_type == wasm::Type::i64)
return "i64";
else
yulAssert(false, "Invalid wasm type");
}
+2
View File
@@ -63,6 +63,8 @@ private:
std::vector<wasm::Expression> const& _expressions,
char _separator = ' '
);
static std::string encodeType(wasm::Type _type);
};
}
+16 -7
View File
@@ -30,6 +30,15 @@
namespace solidity::yul::wasm
{
enum class Type
{
i32,
i64,
};
struct TypedName { std::string name; Type type; };
using TypedNameList = std::vector<TypedName>;
struct Literal;
struct StringLiteral;
struct LocalVariable;
@@ -50,7 +59,7 @@ using Expression = std::variant<
Block, If, Loop, Branch, BranchIf, Return
>;
struct Literal { uint64_t value; };
struct Literal { std::variant<uint32_t, uint64_t> value; };
struct StringLiteral { std::string value; };
struct LocalVariable { std::string name; };
struct GlobalVariable { std::string name; };
@@ -70,21 +79,21 @@ struct Branch { Label label; };
struct Return {};
struct BranchIf { Label label; std::unique_ptr<Expression> condition; };
struct VariableDeclaration { std::string variableName; };
struct GlobalVariableDeclaration { std::string variableName; };
struct VariableDeclaration { std::string variableName; Type type; };
struct GlobalVariableDeclaration { std::string variableName; Type type; };
struct FunctionImport {
std::string module;
std::string externalName;
std::string internalName;
std::vector<std::string> paramTypes;
std::optional<std::string> returnType;
std::vector<Type> paramTypes;
std::optional<Type> returnType;
};
struct FunctionDefinition
{
std::string name;
std::vector<std::string> parameterNames;
bool returns;
std::vector<TypedName> parameters;
std::optional<Type> returnType;
std::vector<VariableDeclaration> locals;
std::vector<Expression> body;
};
+26 -14
View File
@@ -88,7 +88,7 @@ wasm::Expression WasmCodeTransform::operator()(VariableDeclaration const& _varDe
for (auto const& var: _varDecl.variables)
{
variableNames.emplace_back(var.name.str());
m_localVariables.emplace_back(wasm::VariableDeclaration{variableNames.back()});
m_localVariables.emplace_back(wasm::VariableDeclaration{variableNames.back(), wasm::Type::i64});
}
if (_varDecl.value)
@@ -127,10 +127,10 @@ wasm::Expression WasmCodeTransform::operator()(FunctionCall const& _call)
builtin->name.str().substr(4),
builtin->name.str(),
{},
builtin->returns.empty() ? nullopt : make_optional<string>(builtin->returns.front().str())
builtin->returns.empty() ? nullopt : make_optional<wasm::Type>(translatedType(builtin->returns.front()))
};
for (auto const& param: builtin->parameters)
imp.paramTypes.emplace_back(param.str());
imp.paramTypes.emplace_back(translatedType(param));
m_functionsToImport[builtin->name] = std::move(imp);
}
typeConversionNeeded = true;
@@ -184,7 +184,7 @@ wasm::Expression WasmCodeTransform::operator()(Literal const& _literal)
{
u256 value = valueOfLiteral(_literal);
yulAssert(value <= numeric_limits<uint64_t>::max(), "Literal too large: " + value.str());
return wasm::Literal{uint64_t(value)};
return wasm::Literal{static_cast<uint64_t>(value)};
}
wasm::Expression WasmCodeTransform::operator()(If const& _if)
@@ -193,7 +193,7 @@ wasm::Expression WasmCodeTransform::operator()(If const& _if)
vector<wasm::Expression> args;
args.emplace_back(visitReturnByValue(*_if.condition));
args.emplace_back(wasm::Literal{0});
args.emplace_back(wasm::Literal{static_cast<uint64_t>(0)});
return wasm::If{
make_unique<wasm::Expression>(wasm::BuiltinCall{"i64.ne", std::move(args)}),
visit(_if.body.statements),
@@ -205,7 +205,7 @@ wasm::Expression WasmCodeTransform::operator()(Switch const& _switch)
{
wasm::Block block;
string condition = m_nameDispenser.newName("condition"_yulstring).str();
m_localVariables.emplace_back(wasm::VariableDeclaration{condition});
m_localVariables.emplace_back(wasm::VariableDeclaration{condition, wasm::Type::i64});
block.statements.emplace_back(wasm::LocalAssignment{condition, visit(*_switch.expression)});
vector<wasm::Expression>* currentBlock = &block.statements;
@@ -325,10 +325,11 @@ wasm::FunctionDefinition WasmCodeTransform::translateFunction(yul::FunctionDefin
wasm::FunctionDefinition fun;
fun.name = _fun.name.str();
for (auto const& param: _fun.parameters)
fun.parameterNames.emplace_back(param.name.str());
fun.parameters.push_back({param.name.str(), wasm::Type::i64});
for (auto const& retParam: _fun.returnVariables)
fun.locals.emplace_back(wasm::VariableDeclaration{retParam.name.str()});
fun.returns = !_fun.returnVariables.empty();
fun.locals.emplace_back(wasm::VariableDeclaration{retParam.name.str(), wasm::Type::i64});
if (!_fun.returnVariables.empty())
fun.returnType = wasm::Type::i64;
yulAssert(m_localVariables.empty(), "");
yulAssert(m_functionBodyLabel.empty(), "");
@@ -361,14 +362,14 @@ wasm::Expression WasmCodeTransform::injectTypeConversionIfNeeded(wasm::FunctionC
{
wasm::FunctionImport const& import = m_functionsToImport.at(YulString{_call.functionName});
for (size_t i = 0; i < _call.arguments.size(); ++i)
if (import.paramTypes.at(i) == "i32")
if (import.paramTypes.at(i) == wasm::Type::i32)
_call.arguments[i] = wasm::BuiltinCall{"i32.wrap_i64", make_vector<wasm::Expression>(std::move(_call.arguments[i]))};
else
yulAssert(import.paramTypes.at(i) == "i64", "Unknown type " + import.paramTypes.at(i));
yulAssert(import.paramTypes.at(i) == wasm::Type::i64, "Invalid Wasm type");
if (import.returnType && *import.returnType != "i64")
if (import.returnType && *import.returnType != wasm::Type::i64)
{
yulAssert(*import.returnType == "i32", "Invalid type " + *import.returnType);
yulAssert(*import.returnType == wasm::Type::i32, "Invalid Wasm type");
return wasm::BuiltinCall{"i64.extend_i32_u", make_vector<wasm::Expression>(std::move(_call))};
}
return {std::move(_call)};
@@ -400,6 +401,17 @@ void WasmCodeTransform::allocateGlobals(size_t _amount)
{
while (m_globalVariables.size() < _amount)
m_globalVariables.emplace_back(wasm::GlobalVariableDeclaration{
m_nameDispenser.newName("global_"_yulstring).str()
m_nameDispenser.newName("global_"_yulstring).str(),
wasm::Type::i64
});
}
wasm::Type WasmCodeTransform::translatedType(yul::Type _yulType)
{
if (_yulType == "i32"_yulstring)
return wasm::Type::i32;
else if (_yulType == "i64"_yulstring)
return wasm::Type::i64;
else
yulAssert(false, "This Yul type does not have a corresponding type in Wasm.");
}
+2
View File
@@ -89,6 +89,8 @@ private:
/// Makes sure that there are at least @a _amount global variables.
void allocateGlobals(size_t _amount);
static wasm::Type translatedType(yul::Type _yulType);
Dialect const& m_dialect;
NameDispenser m_nameDispenser;
+11 -11
View File
@@ -112,7 +112,7 @@ void WordSizeTransform::operator()(Block& _block)
yulAssert(varDecl.variables.size() == 1, "");
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
vector<Statement> ret;
for (int i = 0; i < 3; i++)
for (size_t i = 0; i < 3; i++)
ret.emplace_back(VariableDeclaration{
varDecl.location,
{TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
@@ -143,7 +143,7 @@ void WordSizeTransform::operator()(Block& _block)
auto newRhs = expandValue(*varDecl.value);
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
vector<Statement> ret;
for (int i = 0; i < 4; i++)
for (size_t i = 0; i < 4; i++)
ret.emplace_back(VariableDeclaration{
varDecl.location,
{TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
@@ -172,7 +172,7 @@ void WordSizeTransform::operator()(Block& _block)
yulAssert(assignment.variableNames.size() == 1, "");
auto newLhs = generateU64IdentifierNames(assignment.variableNames[0].name);
vector<Statement> ret;
for (int i = 0; i < 3; i++)
for (size_t i = 0; i < 3; i++)
ret.emplace_back(Assignment{
assignment.location,
{Identifier{assignment.location, newLhs[i]}},
@@ -203,7 +203,7 @@ void WordSizeTransform::operator()(Block& _block)
auto newRhs = expandValue(*assignment.value);
YulString lhsName = assignment.variableNames[0].name;
vector<Statement> ret;
for (int i = 0; i < 4; i++)
for (size_t i = 0; i < 4; i++)
ret.emplace_back(Assignment{
assignment.location,
{Identifier{assignment.location, m_variableMapping.at(lhsName)[i]}},
@@ -382,7 +382,7 @@ std::vector<Statement> WordSizeTransform::handleSwitch(Switch& _switch)
array<YulString, 4> WordSizeTransform::generateU64IdentifierNames(YulString const& _s)
{
yulAssert(m_variableMapping.find(_s) == m_variableMapping.end(), "");
for (int i = 0; i < 4; i++)
for (size_t i = 0; i < 4; i++)
m_variableMapping[_s][i] = m_nameDispenser.newName(YulString{_s.str() + "_" + to_string(i)});
return m_variableMapping[_s];
}
@@ -392,19 +392,20 @@ array<unique_ptr<Expression>, 4> WordSizeTransform::expandValue(Expression const
array<unique_ptr<Expression>, 4> ret;
if (holds_alternative<Identifier>(_e))
{
Identifier const& id = std::get<Identifier>(_e);
for (int i = 0; i < 4; i++)
auto const& id = std::get<Identifier>(_e);
for (size_t i = 0; i < 4; i++)
ret[i] = make_unique<Expression>(Identifier{id.location, m_variableMapping.at(id.name)[i]});
}
else if (holds_alternative<Literal>(_e))
{
Literal const& lit = std::get<Literal>(_e);
auto const& lit = std::get<Literal>(_e);
u256 val = valueOfLiteral(lit);
for (int i = 3; i >= 0; i--)
for (size_t exprIndex = 0; exprIndex < 4; ++exprIndex)
{
size_t exprIndexReverse = 3 - exprIndex;
u256 currentVal = val & std::numeric_limits<uint64_t>::max();
val >>= 64;
ret[i] = make_unique<Expression>(
ret[exprIndexReverse] = make_unique<Expression>(
Literal{
lit.location,
LiteralKind::Number,
@@ -426,4 +427,3 @@ vector<Expression> WordSizeTransform::expandValueToVector(Expression const& _e)
ret.emplace_back(std::move(*val));
return ret;
}
+2 -3
View File
@@ -51,10 +51,10 @@ void DeadCodeEliminator::operator()(Block& _block)
tie(controlFlowChange, index) = TerminationFinder{m_dialect}.firstUnconditionalControlFlowChange(_block.statements);
// Erase everything after the terminating statement that is not a function definition.
if (controlFlowChange != TerminationFinder::ControlFlow::FlowOut && index != size_t(-1))
if (controlFlowChange != TerminationFinder::ControlFlow::FlowOut && index != std::numeric_limits<size_t>::max())
_block.statements.erase(
remove_if(
_block.statements.begin() + index + 1,
_block.statements.begin() + static_cast<ptrdiff_t>(index) + 1,
_block.statements.end(),
[] (Statement const& _s) { return !holds_alternative<yul::FunctionDefinition>(_s); }
),
@@ -63,4 +63,3 @@ void DeadCodeEliminator::operator()(Block& _block)
ASTModifier::operator()(_block);
}
+1 -1
View File
@@ -119,7 +119,7 @@ void ExpressionJoiner::decrementLatestStatementPointer()
void ExpressionJoiner::resetLatestStatementPointer()
{
m_currentBlock = nullptr;
m_latestStatementInBlock = size_t(-1);
m_latestStatementInBlock = numeric_limits<size_t>::max();
}
Statement* ExpressionJoiner::latestStatement()
+1 -1
View File
@@ -180,7 +180,7 @@ pair<TerminationFinder::ControlFlow, size_t> TerminationFinder::firstUncondition
if (controlFlow != ControlFlow::FlowOut)
return {controlFlow, i};
}
return {ControlFlow::FlowOut, size_t(-1)};
return {ControlFlow::FlowOut, numeric_limits<size_t>::max()};
}
TerminationFinder::ControlFlow TerminationFinder::controlFlowKind(Statement const& _statement)
+3 -3
View File
@@ -178,14 +178,14 @@ bool StackCompressor::run(
eliminateVariables(
_dialect,
std::get<Block>(_object.code->statements.at(0)),
stackSurplus.at({}),
static_cast<size_t>(stackSurplus.at({})),
allowMSizeOptimzation
);
}
for (size_t i = 1; i < _object.code->statements.size(); ++i)
{
FunctionDefinition& fun = std::get<FunctionDefinition>(_object.code->statements[i]);
auto& fun = std::get<FunctionDefinition>(_object.code->statements[i]);
if (!stackSurplus.count(fun.name))
continue;
@@ -193,7 +193,7 @@ bool StackCompressor::run(
eliminateVariables(
_dialect,
fun,
stackSurplus.at(fun.name),
static_cast<size_t>(stackSurplus.at(fun.name)),
allowMSizeOptimzation
);
}