/src/WasmEdge/include/ast/component/canonical.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/ast/component/canonical.h - Canon class definitions ------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the Canon node related classes. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/valtype.h" |
17 | | #include "common/enum_ast.hpp" |
18 | | #include "common/span.h" |
19 | | |
20 | | #include <cstdint> |
21 | | #include <vector> |
22 | | |
23 | | namespace WasmEdge { |
24 | | namespace AST { |
25 | | namespace Component { |
26 | | |
27 | | // canonopt ::= 0x00 => string-encoding=utf8 |
28 | | // | 0x01 => string-encoding=utf16 |
29 | | // | 0x02 => string-encoding=latin1+utf16 |
30 | | // | 0x03 m:<core:memidx> => (memory m) |
31 | | // | 0x04 f:<core:funcidx> => (realloc f) |
32 | | // | 0x05 f:<core:funcidx> => (post-return f) |
33 | | // | 0x06 => async ๐ |
34 | | // | 0x07 f:<core:funcidx> => (callback f) ๐ |
35 | | // | 0x08 => always-task-return ๐ |
36 | | |
37 | | /// AST Component::CanonOpt definition. |
38 | | class CanonOpt { |
39 | | public: |
40 | 0 | CanonOpt() noexcept : Code(ComponentCanonOptCode::Encode_UTF8), Idx(0) {} |
41 | | |
42 | 0 | ComponentCanonOptCode getCode() const noexcept { return Code; } |
43 | 0 | void setCode(const ComponentCanonOptCode C) noexcept { Code = C; } |
44 | | |
45 | 0 | uint32_t getIndex() const noexcept { return Idx; } |
46 | 0 | void setIndex(const uint32_t I) noexcept { Idx = I; } |
47 | | |
48 | | private: |
49 | | ComponentCanonOptCode Code; |
50 | | uint32_t Idx; |
51 | | }; |
52 | | |
53 | | // canon ::= 0x00 0x00 f:<core:funcidx> opts:<opts> ft:<typeidx> |
54 | | // => (canon lift f opts type-index-space[ft]) |
55 | | // | 0x01 0x00 f:<funcidx> opts:<opts> |
56 | | // => (canon lower f opts (core func)) |
57 | | // | 0x02 rt:<typeidx> => (canon resource.new rt (core func)) |
58 | | // | 0x03 rt:<typeidx> => (canon resource.drop rt (core func)) |
59 | | // | 0x07 rt:<typeidx> |
60 | | // => (canon resource.drop rt async (core func)) ๐ |
61 | | // | 0x04 rt:<typeidx> => (canon resource.rep rt (core func)) |
62 | | // | 0x08 => (canon backpressure.set (core func)) ๐ |
63 | | // | 0x09 rs:<resultlist> opts:<opts> |
64 | | // => (canon task.return rs opts (core func)) ๐ |
65 | | // | 0x05 => (canon task.cancel (core func)) ๐ |
66 | | // | 0x0a 0x7f i:<u32> => (canon context.get i32 i (core func)) ๐ |
67 | | // | 0x0b 0x7f i:<u32> => (canon context.set i32 i (core func)) ๐ |
68 | | // | 0x0c async?:<async?> => (canon yield async? (core func)) ๐ |
69 | | // | 0x06 async?:<async?> |
70 | | // => (canon subtask.cancel async? (core func)) ๐ |
71 | | // | 0x0d => (canon subtask.drop (core func)) ๐ |
72 | | // | 0x0e t:<typeidx> => (canon stream.new t (core func)) ๐ |
73 | | // | 0x0f t:<typeidx> opts:<opts> |
74 | | // => (canon stream.read t opts (core func)) ๐ |
75 | | // | 0x10 t:<typeidx> opts:<opts> |
76 | | // => (canon stream.write t opts (core func)) ๐ |
77 | | // | 0x11 t:<typeidx> async?:<async?> |
78 | | // => (canon stream.cancel-read async? (core func)) ๐ |
79 | | // | 0x12 t:<typeidx> async?:<async?> |
80 | | // => (canon stream.cancel-write async? (core func)) ๐ |
81 | | // | 0x13 t:<typeidx> => (canon stream.close-readable t (core func)) ๐ |
82 | | // | 0x14 t:<typeidx> => (canon stream.close-writable t (core func)) ๐ |
83 | | // | 0x15 t:<typeidx> => (canon future.new t (core func)) ๐ |
84 | | // | 0x16 t:<typeidx> opts:<opts> |
85 | | // => (canon future.read t opts (core func)) ๐ |
86 | | // | 0x17 t:<typeidx> opts:<opts> |
87 | | // => (canon future.write t opts (core func)) ๐ |
88 | | // | 0x18 t:<typeidx> async?:<async?> |
89 | | // => (canon future.cancel-read async? (core func)) ๐ |
90 | | // | 0x19 t:<typeidx> async?:<async?> |
91 | | // => (canon future.cancel-write async? (core func)) ๐ |
92 | | // | 0x1a t:<typeidx> => (canon future.close-readable t (core func)) ๐ |
93 | | // | 0x1b t:<typeidx> => (canon future.close-writable t (core func)) ๐ |
94 | | // | 0x1c opts:<opts> => (canon error-context.new opts (core func)) ๐ |
95 | | // | 0x1d opts:<opts> |
96 | | // => (canon error-context.debug-message opts (core func)) ๐ |
97 | | // | 0x1e => (canon error-context.drop (core func)) ๐ |
98 | | // | 0x1f => (canon waitable-set.new (core func)) ๐ |
99 | | // | 0x20 async?:<async>? m:<core:memidx> |
100 | | // => (canon waitable-set.wait async? (memory m) (core func)) ๐ |
101 | | // | 0x21 async?:<async>? m:<core:memidx> |
102 | | // => (canon waitable-set.poll async? (memory m) (core func)) ๐ |
103 | | // | 0x22 => (canon waitable-set.drop (core func)) ๐ |
104 | | // | 0x23 => (canon waitable.join (core func)) ๐ |
105 | | // | 0x40 ft:<typeidx> => (canon thread.spawn_ref ft (core func)) ๐งต |
106 | | // | 0x41 ft:<typeidx> tbl:<core:tableidx> |
107 | | // => (canon thread.spawn_indirect ft tbl (core func)) ๐งต |
108 | | // | 0x42 => (canon thread.available_parallelism (core func)) ๐งต |
109 | | // opts ::= opt*:vec(<canonopt>) => opt* |
110 | | // async? ::= 0x00 => ฯต |
111 | | // | 0x01 => async |
112 | | |
113 | | // Currently implementing: |
114 | | // 0x00 0x00 (canon lift f opts type-index-space[ft]) |
115 | | // 0x01 0x00 (canon lower f opts (core func)) |
116 | | // 0x02 (canon resource.new rt (core func)) |
117 | | // 0x03 (canon resource.drop rt (core func)) |
118 | | // 0x04 (canon resource.rep rt (core func)) |
119 | | |
120 | | class Canonical { |
121 | | public: |
122 | 0 | Canonical() noexcept = default; |
123 | | |
124 | 0 | ComponentCanonOpCode getOpCode() const noexcept { return Code; } |
125 | 0 | void setOpCode(const ComponentCanonOpCode C) noexcept { Code = C; } |
126 | | |
127 | 0 | bool isAsync() const noexcept { return IsAsync; } |
128 | 0 | void setAsync(const bool A) noexcept { IsAsync = A; } |
129 | | |
130 | 0 | uint32_t getIndex() const noexcept { return Idx; } |
131 | 0 | void setIndex(const uint32_t I) noexcept { Idx = I; } |
132 | | |
133 | 0 | uint32_t getTargetIndex() const noexcept { return TargetIdx; } |
134 | 0 | void setTargetIndex(const uint32_t I) noexcept { TargetIdx = I; } |
135 | | |
136 | 0 | uint32_t getConstVal() const noexcept { return I32; } |
137 | 0 | void setConstVal(const uint32_t V) noexcept { I32 = V; } |
138 | | |
139 | 0 | Span<const CanonOpt> getOptions() const noexcept { return Opts; } |
140 | 0 | void setOptions(std::vector<CanonOpt> &&List) noexcept { |
141 | 0 | Opts = std::move(List); |
142 | 0 | } |
143 | | |
144 | 0 | Span<const LabelValType> getResultList() const noexcept { return ResultList; } |
145 | 0 | void setResultList(std::vector<LabelValType> &&R) noexcept { |
146 | 0 | ResultList = std::move(R); |
147 | 0 | } |
148 | 0 | void setResultList(const ComponentValType &VT) noexcept { |
149 | 0 | ResultList.clear(); |
150 | 0 | ResultList.emplace_back(VT); |
151 | 0 | } |
152 | | |
153 | | private: |
154 | | ComponentCanonOpCode Code; |
155 | | bool IsAsync; |
156 | | uint32_t Idx, TargetIdx; |
157 | | uint32_t I32; |
158 | | std::vector<CanonOpt> Opts; |
159 | | std::vector<LabelValType> ResultList; |
160 | | }; |
161 | | |
162 | | } // namespace Component |
163 | | } // namespace AST |
164 | | } // namespace WasmEdge |