Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The wat2wasm tool already supports wasmGC, such as struct.new  #2363

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions include/wabt/expr-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ class ExprVisitor::Delegate {
virtual Result EndIfExpr(IfExpr*) = 0;
virtual Result OnLoadExpr(LoadExpr*) = 0;
virtual Result OnLocalGetExpr(LocalGetExpr*) = 0;
virtual Result OnStructNewExpr(StructNewExpr*) = 0;
virtual Result OnStructNewDefaultExpr(StructNewDefaultExpr*) = 0;
virtual Result OnStructGetExpr(StructGetExpr*) = 0;
virtual Result OnStructGetUExpr(StructGetUExpr*) = 0;
virtual Result OnStructGetSExpr(StructGetSExpr*) = 0;
virtual Result OnStructSetExpr(StructSetExpr*) = 0;
virtual Result OnArrayNewExpr(ArrayNewExpr*) = 0;
virtual Result OnArrayNewDefaultExpr(ArrayNewDefaultExpr*) = 0;
virtual Result OnArrayNewFixedExpr(ArrayNewFixedExpr*) = 0;
virtual Result OnArrayNewDataExpr(ArrayNewDataExpr*) = 0;
virtual Result OnArrayNewElemExpr(ArrayNewElemExpr*) = 0;
virtual Result OnArrayGetExpr(ArrayGetExpr*) = 0;
virtual Result OnArrayGetSExpr(ArrayGetSExpr*) = 0;
virtual Result OnArrayGetUExpr(ArrayGetUExpr*) = 0;
virtual Result OnArraySetExpr(ArraySetExpr*) = 0;
virtual Result OnArrayLenExpr(ArrayLenExpr*) = 0;
virtual Result OnArrayFillExpr(ArrayFillExpr*) = 0;
virtual Result OnArrayCopyExpr(ArrayCopyExpr*) = 0;
virtual Result OnArrayInitDataExpr(ArrayInitDataExpr*) = 0;
virtual Result OnArrayInitElemExpr(ArrayInitElemExpr*) = 0;

virtual Result OnRefTestExpr(RefTestExpr*) = 0;
virtual Result OnRefCastExpr(RefCastExpr*) = 0;
virtual Result OnBrOnCastExpr(BrOnCastExpr*) = 0;
virtual Result OnBrOnCastFailExpr(BrOnCastFailExpr*) = 0;
virtual Result OnAnyConvertExternExpr(AnyConvertExternExpr*) = 0;
virtual Result OnExternConvertAnyExpr(ExternConvertAnyExpr*) = 0;
virtual Result OnRefI31Expr(RefI31Expr*) = 0;
virtual Result OnI31GetSExpr(I31GetSExpr*) = 0;
virtual Result OnI31GetUExpr(I31GetUExpr*) = 0;
virtual Result OnLocalSetExpr(LocalSetExpr*) = 0;
virtual Result OnLocalTeeExpr(LocalTeeExpr*) = 0;
virtual Result BeginLoopExpr(LoopExpr*) = 0;
Expand All @@ -106,6 +136,7 @@ class ExprVisitor::Delegate {
virtual Result OnTableGrowExpr(TableGrowExpr*) = 0;
virtual Result OnTableSizeExpr(TableSizeExpr*) = 0;
virtual Result OnTableFillExpr(TableFillExpr*) = 0;
virtual Result OnRefEqExpr(RefEqExpr*) = 0;
virtual Result OnRefFuncExpr(RefFuncExpr*) = 0;
virtual Result OnRefNullExpr(RefNullExpr*) = 0;
virtual Result OnRefIsNullExpr(RefIsNullExpr*) = 0;
Expand Down Expand Up @@ -162,6 +193,36 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
Result EndIfExpr(IfExpr*) override { return Result::Ok; }
Result OnLoadExpr(LoadExpr*) override { return Result::Ok; }
Result OnLocalGetExpr(LocalGetExpr*) override { return Result::Ok; }
Result OnStructNewExpr(StructNewExpr*) override { return Result::Ok; }
Result OnStructNewDefaultExpr(StructNewDefaultExpr*) override { return Result::Ok;}
Result OnStructGetExpr(StructGetExpr*) override { return Result::Ok; }
Result OnStructGetUExpr(StructGetUExpr*) override { return Result::Ok; }
Result OnStructGetSExpr(StructGetSExpr*) override { return Result::Ok; }
Result OnStructSetExpr(StructSetExpr*) override { return Result::Ok; }
Result OnArrayNewExpr(ArrayNewExpr*) override { return Result::Ok; }
Result OnArrayNewDefaultExpr(ArrayNewDefaultExpr*) override { return Result::Ok; }
Result OnArrayNewFixedExpr(ArrayNewFixedExpr*) override { return Result::Ok; }
Result OnArrayNewDataExpr(ArrayNewDataExpr*) override { return Result::Ok; }
Result OnArrayNewElemExpr(ArrayNewElemExpr*) override { return Result::Ok; }
Result OnArrayGetExpr(ArrayGetExpr*) override { return Result::Ok; }
Result OnArrayGetSExpr(ArrayGetSExpr*) override { return Result::Ok; }
Result OnArrayGetUExpr(ArrayGetUExpr*) override { return Result::Ok; }
Result OnArraySetExpr(ArraySetExpr*) override { return Result::Ok; }
Result OnArrayLenExpr(ArrayLenExpr*) override { return Result::Ok; }
Result OnArrayFillExpr(ArrayFillExpr*) override { return Result::Ok; }
Result OnArrayCopyExpr(ArrayCopyExpr*) override { return Result::Ok; }
Result OnArrayInitDataExpr(ArrayInitDataExpr*) override { return Result::Ok; }
Result OnArrayInitElemExpr(ArrayInitElemExpr*) override { return Result::Ok; }

Result OnRefTestExpr(RefTestExpr*) override { return Result::Ok; }
Result OnRefCastExpr(RefCastExpr*) override { return Result::Ok; }
Result OnBrOnCastExpr(BrOnCastExpr*) override { return Result::Ok; }
Result OnBrOnCastFailExpr(BrOnCastFailExpr*) override { return Result::Ok; }
Result OnAnyConvertExternExpr(AnyConvertExternExpr*) override { return Result::Ok; }
Result OnExternConvertAnyExpr(ExternConvertAnyExpr*) override { return Result::Ok; }
Result OnRefI31Expr(RefI31Expr*) override { return Result::Ok; }
Result OnI31GetSExpr(I31GetSExpr*) override { return Result::Ok; }
Result OnI31GetUExpr(I31GetUExpr*) override { return Result::Ok; }
Result OnLocalSetExpr(LocalSetExpr*) override { return Result::Ok; }
Result OnLocalTeeExpr(LocalTeeExpr*) override { return Result::Ok; }
Result BeginLoopExpr(LoopExpr*) override { return Result::Ok; }
Expand All @@ -180,6 +241,7 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
Result OnTableGrowExpr(TableGrowExpr*) override { return Result::Ok; }
Result OnTableSizeExpr(TableSizeExpr*) override { return Result::Ok; }
Result OnTableFillExpr(TableFillExpr*) override { return Result::Ok; }
Result OnRefEqExpr(RefEqExpr*) override { return Result::Ok; }
Result OnRefFuncExpr(RefFuncExpr*) override { return Result::Ok; }
Result OnRefNullExpr(RefNullExpr*) override { return Result::Ok; }
Result OnRefIsNullExpr(RefIsNullExpr*) override { return Result::Ok; }
Expand Down
146 changes: 133 additions & 13 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,12 @@ struct FuncSignature {
bool operator==(const FuncSignature&) const;
};

enum class TypeEntryKind {
Func,
Struct,
Array,
enum class TypeEntryKind { Func, Struct, Array, Sub, Rec };

struct Field {
std::string name;
Type type = Type::Void;
bool mutable_ = false;
};

class TypeEntry {
Expand Down Expand Up @@ -319,20 +321,14 @@ class FuncType : public TypeEntry {
} features_used;
};

struct Field {
std::string name;
Type type = Type::Void;
bool mutable_ = false;
};

class StructType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Struct;
}

explicit StructType(std::string_view name = std::string_view())
: TypeEntry(TypeEntryKind::Struct) {}
: TypeEntry(TypeEntryKind::Struct, name) {}

std::vector<Field> fields;
};
Expand All @@ -344,11 +340,32 @@ class ArrayType : public TypeEntry {
}

explicit ArrayType(std::string_view name = std::string_view())
: TypeEntry(TypeEntryKind::Array) {}

: TypeEntry(TypeEntryKind::Array, name) {}
Field field;
};

class SubType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Sub;
}

explicit SubType(std::string name = std::string())
: TypeEntry(TypeEntryKind::Sub, name) {}
std::unique_ptr<TypeEntry> typeEntry;
};

class RecType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Rec;
}

explicit RecType(std::string name = std::string())
: TypeEntry(TypeEntryKind::Rec, name) {}
std::vector<std::unique_ptr<TypeEntry>> fields;
};

struct FuncDeclaration {
Index GetNumParams() const { return sig.GetNumParams(); }
Index GetNumResults() const { return sig.GetNumResults(); }
Expand Down Expand Up @@ -386,6 +403,36 @@ enum class ExprType {
If,
Load,
LocalGet,
StructNew,
StructNewDefault,
StructGet,
StructGetU,
StructGetS,
StructSet,
ArrayNew,
ArrayNewDefault,
ArrayNewFixed,
ArrayNewData,
ArrayNewElem,
ArrayGet,
ArrayGetS,
ArrayGetU,
ArraySet,
ArrayLen,
ArrayFill,
ArrayCopy,
ArrayInitData,
ArrayInitElem,

RefTest,
RefCast,
BrOnCast,
BrOnCastFail,
AnyConvertExtern,
ExternConvertAny,
RefI31,
I31GetS,
I31GetU,
LocalSet,
LocalTee,
Loop,
Expand All @@ -397,6 +444,7 @@ enum class ExprType {
MemorySize,
Nop,
RefIsNull,
RefEq,
RefFunc,
RefNull,
Rethrow,
Expand Down Expand Up @@ -616,6 +664,42 @@ class VarExpr : public ExprMixin<TypeEnum> {
Var var;
};

template <ExprType TypeEnum>
class StructGetVarExpr : public ExprMixin<TypeEnum> {
public:
StructGetVarExpr(const Var& var1,
const Var& var2,
const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), var1(var1), var2(var2) {}

Var var1;
Var var2;
};

template <ExprType TypeEnum>
class Var2Expr : public ExprMixin<TypeEnum> {
public:
Var2Expr(const Var& var1, const Var& var2, const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), var1(var1), var2(var2) {}

Var var1;
Var var2;
};

template <ExprType TypeEnum>
class Var3Expr : public ExprMixin<TypeEnum> {
public:
Var3Expr(const Var& var1,
const Var& var2,
const Var& var3,
const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), var1(var1), var2(var2), var3(var3) {}

Var var1;
Var var2;
Var var3;
};

template <ExprType TypeEnum>
class MemoryVarExpr : public MemoryExpr<TypeEnum> {
public:
Expand All @@ -628,10 +712,42 @@ class MemoryVarExpr : public MemoryExpr<TypeEnum> {
using BrExpr = VarExpr<ExprType::Br>;
using BrIfExpr = VarExpr<ExprType::BrIf>;
using CallExpr = VarExpr<ExprType::Call>;
using RefEqExpr = ExprMixin<ExprType::RefEq>;
using RefFuncExpr = VarExpr<ExprType::RefFunc>;
using GlobalGetExpr = VarExpr<ExprType::GlobalGet>;
using GlobalSetExpr = VarExpr<ExprType::GlobalSet>;
using LocalGetExpr = VarExpr<ExprType::LocalGet>;
using StructNewExpr = VarExpr<ExprType::StructNew>;
using StructNewDefaultExpr = VarExpr<ExprType::StructNewDefault>;
using StructGetExpr = StructGetVarExpr<ExprType::StructGet>;
using StructGetUExpr = StructGetVarExpr<ExprType::StructGetU>;
using StructGetSExpr = StructGetVarExpr<ExprType::StructGetS>;
using StructSetExpr = StructGetVarExpr<ExprType::StructSet>;
using ArrayNewExpr = VarExpr<ExprType::ArrayNew>;
using ArrayNewDefaultExpr = VarExpr<ExprType::ArrayNewDefault>;
using ArrayNewFixedExpr = Var2Expr<ExprType::ArrayNewFixed>;
using ArrayNewDataExpr = Var2Expr<ExprType::ArrayNewData>;
using ArrayNewElemExpr = Var2Expr<ExprType::ArrayNewElem>;
using ArrayGetExpr = VarExpr<ExprType::ArrayGet>;
using ArrayGetUExpr = VarExpr<ExprType::ArrayGetU>;
using ArrayGetSExpr = VarExpr<ExprType::ArrayGetS>;
using ArraySetExpr = VarExpr<ExprType::ArraySet>;
using ArrayLenExpr = ExprMixin<ExprType::ArrayLen>;
using ArrayFillExpr = VarExpr<ExprType::ArrayFill>;
using ArrayCopyExpr = Var2Expr<ExprType::ArrayCopy>;
using ArrayInitDataExpr = Var2Expr<ExprType::ArrayInitData>;
using ArrayInitElemExpr = Var2Expr<ExprType::ArrayInitElem>;

using RefTestExpr = VarExpr<ExprType::RefTest>;
using RefCastExpr = VarExpr<ExprType::RefCast>;
using BrOnCastExpr = Var3Expr<ExprType::BrOnCast>;
using BrOnCastFailExpr = Var3Expr<ExprType::BrOnCastFail>;
using AnyConvertExternExpr = ExprMixin<ExprType::AnyConvertExtern>;
using ExternConvertAnyExpr = ExprMixin<ExprType::ExternConvertAny>;
using RefI31Expr = ExprMixin<ExprType::RefI31>;
using I31GetSExpr = ExprMixin<ExprType::I31GetS>;
using I31GetUExpr = ExprMixin<ExprType::I31GetU>;

using LocalSetExpr = VarExpr<ExprType::LocalSet>;
using LocalTeeExpr = VarExpr<ExprType::LocalTee>;
using ReturnCallExpr = VarExpr<ExprType::ReturnCall>;
Expand Down Expand Up @@ -1229,6 +1345,7 @@ struct Module {
void AppendField(std::unique_ptr<ExportModuleField>);
void AppendField(std::unique_ptr<FuncModuleField>);
void AppendField(std::unique_ptr<TypeModuleField>);
void AppendFieldRec(std::unique_ptr<TypeModuleField>);
void AppendField(std::unique_ptr<GlobalModuleField>);
void AppendField(std::unique_ptr<ImportModuleField>);
void AppendField(std::unique_ptr<MemoryModuleField>);
Expand Down Expand Up @@ -1262,6 +1379,9 @@ struct Module {
std::vector<Var*> starts;
std::vector<Custom> customs;

// ´¦ÀíÀàÐÍÉùÃ÷
std::unordered_map<std::string, int> moduletypesname;

BindingHash tag_bindings;
BindingHash func_bindings;
BindingHash global_bindings;
Expand Down
41 changes: 38 additions & 3 deletions include/wabt/opcode.def
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ WABT_OPCODE(I64, I64, ___, ___, 0, 0, 0xC2, I64Extend8S, "i64.extend8_s"
WABT_OPCODE(I64, I64, ___, ___, 0, 0, 0xC3, I64Extend16S, "i64.extend16_s", "")
WABT_OPCODE(I64, I64, ___, ___, 0, 0, 0xC4, I64Extend32S, "i64.extend32_s", "")

WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xd0, RefNull, "ref.null", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0, 0xd1, RefIsNull, "ref.is_null", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xd2, RefFunc, "ref.func", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0, 0xd3, RefEq, "ref.eq", "")

/* Interpreter-only opcodes */
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xe0, InterpAlloca, "alloca", "")
WABT_OPCODE(___, I32, ___, ___, 0, 0, 0xe1, InterpBrUnless, "br_unless", "")
Expand All @@ -234,6 +239,38 @@ WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xe4, InterpDropKeep, "drop_keep",
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xe5, InterpCatchDrop, "catch_drop", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xe6, InterpAdjustFrameForReturnCall, "adjust_frame_for_return_call", "")

WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x00, StructNew, "struct.new", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x01, StructNewDefault, "struct.new_default", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x02, StructGet, "struct.get", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x03, StructGetS, "struct.get_s", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x04, StructGetU, "struct.get_u", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x05, StructSet, "struct.set", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x06, ArrayNew, "array.new", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x07, ArrayNewDefault, "array.new_default", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x08, ArrayNewFixed, "array.new_fixed", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x09, ArrayNewData, "array.new_data", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x0a, ArrayNewElem, "array.new_elem", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x0b, ArrayGet, "array.get", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x0c, ArrayGetS, "array.get_s", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x0d, ArrayGetU, "array.get_u", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x0e, ArraySet, "array.set", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x0f, ArrayLen, "array.len", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x10, ArrayFill, "array.fill", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x11, ArrayCopy, "array.copy", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x12, ArrayInitData, "array.init_data", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x13, ArrayInitElem, "array.init_elem", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x14, RefTestRef, "ref.test", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x15, RefTestRefNull, "ref.test", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x16, RefCastRef, "ref.cast", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x17, RefCastRefNull, "ref.cast", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x18, BrOnCast, "br_on_cast", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x19, BrOnCastFail, "br_on_cast_fail", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x1a, AnyConvertExtern, "ant.convert_extern", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfb, 0x1b, ExternConvertAny, "extern.convert_any", "")
WABT_OPCODE(___, I32, ___, ___, 0, 0xfb, 0x1c, RefI31, "ref.i31", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x1d, I31GetS, "i31.get_s", "")
WABT_OPCODE(I32, ___, ___, ___, 0, 0xfb, 0x1e, I31GetU, "i31.get_u", "")

/* Saturating float-to-int opcodes (--enable-saturating-float-to-int) */
WABT_OPCODE(I32, F32, ___, ___, 0, 0xfc, 0x00, I32TruncSatF32S, "i32.trunc_sat_f32_s", "")
WABT_OPCODE(I32, F32, ___, ___, 0, 0xfc, 0x01, I32TruncSatF32U, "i32.trunc_sat_f32_u", "")
Expand All @@ -259,9 +296,7 @@ WABT_OPCODE(___, I32, ___, ___, 0, 0, 0x26, TableSet, "table.set", "")
WABT_OPCODE(___, ___, I32, ___, 0, 0xfc, 0x0f, TableGrow, "table.grow", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0xfc, 0x10, TableSize, "table.size", "")
WABT_OPCODE(___, I32, ___, I32, 0, 0xfc, 0x11, TableFill, "table.fill", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xd0, RefNull, "ref.null", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xd1, RefIsNull, "ref.is_null", "")
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0xd2, RefFunc, "ref.func", "")


/* Simd opcodes */
WABT_OPCODE(V128, I32, ___, ___, 16, 0xfd, 0x00, V128Load, "v128.load", "")
Expand Down
Loading
Loading