2019-04-18 12:39:48 +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-18 12:39:48 +00:00
|
|
|
/**
|
|
|
|
* Simplified in-memory representation of a Wasm AST.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-30 18:03:27 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2019-04-18 12:39:48 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-10-31 16:27:52 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2020-05-12 15:17:23 +00:00
|
|
|
#include <optional>
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul::wasm
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
|
2020-06-10 22:54:12 +00:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
i32,
|
|
|
|
i64,
|
|
|
|
};
|
|
|
|
|
2020-05-28 19:20:19 +00:00
|
|
|
struct TypedName { std::string name; Type type; };
|
|
|
|
using TypedNameList = std::vector<TypedName>;
|
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
struct Literal;
|
2019-06-24 14:51:59 +00:00
|
|
|
struct StringLiteral;
|
2019-04-23 08:45:16 +00:00
|
|
|
struct LocalVariable;
|
|
|
|
struct GlobalVariable;
|
2019-04-18 12:39:48 +00:00
|
|
|
struct FunctionCall;
|
|
|
|
struct BuiltinCall;
|
|
|
|
struct LocalAssignment;
|
2019-04-23 08:45:16 +00:00
|
|
|
struct GlobalAssignment;
|
2019-04-18 12:39:48 +00:00
|
|
|
struct Block;
|
|
|
|
struct If;
|
|
|
|
struct Loop;
|
2020-05-20 22:59:03 +00:00
|
|
|
struct Branch;
|
|
|
|
struct BranchIf;
|
2019-10-28 14:25:02 +00:00
|
|
|
struct Return;
|
2019-11-19 15:42:49 +00:00
|
|
|
using Expression = std::variant<
|
2019-10-17 11:14:19 +00:00
|
|
|
Literal, StringLiteral, LocalVariable, GlobalVariable,
|
2019-04-23 08:45:16 +00:00
|
|
|
FunctionCall, BuiltinCall, LocalAssignment, GlobalAssignment,
|
2020-05-20 22:59:03 +00:00
|
|
|
Block, If, Loop, Branch, BranchIf, Return
|
2019-04-23 08:45:16 +00:00
|
|
|
>;
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2020-05-28 19:20:19 +00:00
|
|
|
struct Literal { std::variant<uint32_t, uint64_t> value; };
|
2020-11-20 21:16:56 +00:00
|
|
|
// This is a special AST element used for certain builtins. It is not mapped to actual WebAssembly.
|
2019-06-24 14:51:59 +00:00
|
|
|
struct StringLiteral { std::string value; };
|
2019-04-23 08:45:16 +00:00
|
|
|
struct LocalVariable { std::string name; };
|
|
|
|
struct GlobalVariable { std::string name; };
|
2019-04-18 12:39:48 +00:00
|
|
|
struct Label { std::string name; };
|
|
|
|
struct FunctionCall { std::string functionName; std::vector<Expression> arguments; };
|
|
|
|
struct BuiltinCall { std::string functionName; std::vector<Expression> arguments; };
|
|
|
|
struct LocalAssignment { std::string variableName; std::unique_ptr<Expression> value; };
|
2019-04-23 08:45:16 +00:00
|
|
|
struct GlobalAssignment { std::string variableName; std::unique_ptr<Expression> value; };
|
2019-04-18 12:39:48 +00:00
|
|
|
struct Block { std::string labelName; std::vector<Expression> statements; };
|
2019-04-23 19:32:58 +00:00
|
|
|
struct If {
|
|
|
|
std::unique_ptr<Expression> condition;
|
|
|
|
std::vector<Expression> statements;
|
|
|
|
std::unique_ptr<std::vector<Expression>> elseStatements;
|
|
|
|
};
|
2019-04-18 12:39:48 +00:00
|
|
|
struct Loop { std::string labelName; std::vector<Expression> statements; };
|
2020-05-20 22:59:03 +00:00
|
|
|
struct Branch { Label label; };
|
2019-10-28 14:25:02 +00:00
|
|
|
struct Return {};
|
2020-05-20 22:59:03 +00:00
|
|
|
struct BranchIf { Label label; std::unique_ptr<Expression> condition; };
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2020-05-28 19:20:19 +00:00
|
|
|
struct VariableDeclaration { std::string variableName; Type type; };
|
|
|
|
struct GlobalVariableDeclaration { std::string variableName; Type type; };
|
2019-07-09 13:19:50 +00:00
|
|
|
struct FunctionImport {
|
|
|
|
std::string module;
|
|
|
|
std::string externalName;
|
|
|
|
std::string internalName;
|
2020-06-10 22:54:12 +00:00
|
|
|
std::vector<Type> paramTypes;
|
|
|
|
std::optional<Type> returnType;
|
2019-07-09 13:19:50 +00:00
|
|
|
};
|
2019-04-23 08:45:16 +00:00
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
struct FunctionDefinition
|
|
|
|
{
|
|
|
|
std::string name;
|
2020-05-28 19:20:19 +00:00
|
|
|
std::vector<TypedName> parameters;
|
|
|
|
std::optional<Type> returnType;
|
2019-04-18 12:39:48 +00:00
|
|
|
std::vector<VariableDeclaration> locals;
|
|
|
|
std::vector<Expression> body;
|
|
|
|
};
|
|
|
|
|
2019-10-31 16:27:52 +00:00
|
|
|
/**
|
|
|
|
* Abstract representation of a wasm module.
|
|
|
|
*/
|
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
std::vector<GlobalVariableDeclaration> globals;
|
|
|
|
std::vector<FunctionImport> imports;
|
|
|
|
std::vector<FunctionDefinition> functions;
|
|
|
|
std::map<std::string, Module> subModules;
|
2020-10-30 18:03:27 +00:00
|
|
|
std::map<std::string, bytes> customSections;
|
2019-10-31 16:27:52 +00:00
|
|
|
};
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
}
|