Coverage Report

Created: 2025-08-25 06:58

/src/WasmEdge/include/ast/component/canonical.h
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
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/expression.h"
17
#include "ast/type.h"
18
19
#include <vector>
20
21
namespace WasmEdge {
22
namespace AST {
23
namespace Component {
24
25
// canonopt ::= 0x00                  => string-encoding=utf8
26
//            | 0x01                  => string-encoding=utf16
27
//            | 0x02                  => string-encoding=latin1+utf16
28
//            | 0x03 m:<core:memidx>  => (memory m)
29
//            | 0x04 f:<core:funcidx> => (realloc f)
30
//            | 0x05 f:<core:funcidx> => (post-return f)
31
//            | 0x06                  => async ๐Ÿ”€
32
//            | 0x07 f:<core:funcidx> => (callback f) ๐Ÿ”€
33
//            | 0x08                  => always-task-return ๐Ÿ”€
34
35
/// AST Component::CanonOpt definition.
36
class CanonOpt {
37
public:
38
  enum class OptCode : Byte {
39
    Encode_UTF8 = 0x00,
40
    Encode_UTF16 = 0x01,
41
    Encode_Latin1 = 0x02,
42
    Memory = 0x03,
43
    Realloc = 0x04,
44
    PostReturn = 0x05,
45
    Async = 0x06,
46
    Callback = 0x07,
47
  };
48
49
0
  CanonOpt() noexcept : Code(OptCode::Encode_UTF8), Idx(0) {}
50
51
0
  OptCode getCode() const noexcept { return Code; }
52
0
  void setCode(const OptCode C) noexcept { Code = C; }
53
54
0
  uint32_t getIndex() const noexcept { return Idx; }
55
0
  void setIndex(const uint32_t I) noexcept { Idx = I; }
56
57
private:
58
  OptCode Code;
59
  uint32_t Idx;
60
};
61
62
// canon ::= 0x00 0x00 f:<core:funcidx> opts:<opts> ft:<typeidx>
63
//           => (canon lift f opts type-index-space[ft])
64
//         | 0x01 0x00 f:<funcidx> opts:<opts>
65
//           => (canon lower f opts (core func))
66
//         | 0x02 rt:<typeidx>    => (canon resource.new rt (core func))
67
//         | 0x03 rt:<typeidx>    => (canon resource.drop rt (core func))
68
//         | 0x07 rt:<typeidx>
69
//           => (canon resource.drop rt async (core func)) ๐Ÿ”€
70
//         | 0x04 rt:<typeidx>    => (canon resource.rep rt (core func))
71
//         | 0x08                 => (canon backpressure.set (core func)) ๐Ÿ”€
72
//         | 0x09 rs:<resultlist> opts:<opts>
73
//           => (canon task.return rs opts (core func)) ๐Ÿ”€
74
//         | 0x05                 => (canon task.cancel (core func)) ๐Ÿ”€
75
//         | 0x0a 0x7f i:<u32>    => (canon context.get i32 i (core func)) ๐Ÿ”€
76
//         | 0x0b 0x7f i:<u32>    => (canon context.set i32 i (core func)) ๐Ÿ”€
77
//         | 0x0c async?:<async?> => (canon yield async? (core func)) ๐Ÿ”€
78
//         | 0x06 async?:<async?>
79
//           => (canon subtask.cancel async? (core func)) ๐Ÿ”€
80
//         | 0x0d                 => (canon subtask.drop (core func)) ๐Ÿ”€
81
//         | 0x0e t:<typeidx>     => (canon stream.new t (core func)) ๐Ÿ”€
82
//         | 0x0f t:<typeidx> opts:<opts>
83
//           => (canon stream.read t opts (core func)) ๐Ÿ”€
84
//         | 0x10 t:<typeidx> opts:<opts>
85
//           => (canon stream.write t opts (core func)) ๐Ÿ”€
86
//         | 0x11 t:<typeidx> async?:<async?>
87
//           => (canon stream.cancel-read async? (core func)) ๐Ÿ”€
88
//         | 0x12 t:<typeidx> async?:<async?>
89
//           => (canon stream.cancel-write async? (core func)) ๐Ÿ”€
90
//         | 0x13 t:<typeidx> => (canon stream.close-readable t (core func)) ๐Ÿ”€
91
//         | 0x14 t:<typeidx> => (canon stream.close-writable t (core func)) ๐Ÿ”€
92
//         | 0x15 t:<typeidx> => (canon future.new t (core func)) ๐Ÿ”€
93
//         | 0x16 t:<typeidx> opts:<opts>
94
//           => (canon future.read t opts (core func)) ๐Ÿ”€
95
//         | 0x17 t:<typeidx> opts:<opts>
96
//           => (canon future.write t opts (core func)) ๐Ÿ”€
97
//         | 0x18 t:<typeidx> async?:<async?>
98
//           => (canon future.cancel-read async? (core func)) ๐Ÿ”€
99
//         | 0x19 t:<typeidx> async?:<async?>
100
//           => (canon future.cancel-write async? (core func)) ๐Ÿ”€
101
//         | 0x1a t:<typeidx> => (canon future.close-readable t (core func)) ๐Ÿ”€
102
//         | 0x1b t:<typeidx> => (canon future.close-writable t (core func)) ๐Ÿ”€
103
//         | 0x1c opts:<opts> => (canon error-context.new opts (core func)) ๐Ÿ“
104
//         | 0x1d opts:<opts>
105
//           => (canon error-context.debug-message opts (core func)) ๐Ÿ“
106
//         | 0x1e                 => (canon error-context.drop (core func)) ๐Ÿ“
107
//         | 0x1f                 => (canon waitable-set.new (core func)) ๐Ÿ”€
108
//         | 0x20 async?:<async>? m:<core:memidx>
109
//           => (canon waitable-set.wait async? (memory m) (core func)) ๐Ÿ”€
110
//         | 0x21 async?:<async>? m:<core:memidx>
111
//           => (canon waitable-set.poll async? (memory m) (core func)) ๐Ÿ”€
112
//         | 0x22                 => (canon waitable-set.drop (core func)) ๐Ÿ”€
113
//         | 0x23                 => (canon waitable.join (core func)) ๐Ÿ”€
114
//         | 0x40 ft:<typeidx>    => (canon thread.spawn_ref ft (core func)) ๐Ÿงต
115
//         | 0x41 ft:<typeidx> tbl:<core:tableidx>
116
//           => (canon thread.spawn_indirect ft tbl (core func)) ๐Ÿงต
117
//         | 0x42 => (canon thread.available_parallelism (core func)) ๐Ÿงต
118
// opts   ::= opt*:vec(<canonopt>) => opt*
119
// async? ::= 0x00 => ฯต
120
//          | 0x01 => async
121
122
// Currently implementing:
123
//   0x00 0x00 (canon lift f opts type-index-space[ft])
124
//   0x01 0x00 (canon lower f opts (core func))
125
//   0x02      (canon resource.new rt (core func))
126
//   0x03      (canon resource.drop rt (core func))
127
//   0x04      (canon resource.rep rt (core func))
128
129
class Canonical {
130
public:
131
  // TODO: COMPONENT - move to enum.inc.
132
  enum class OpCode : Byte {
133
    Lift = 0x00,
134
    Lower = 0x01,
135
    Resource__new = 0x02,
136
    Resource__drop = 0x03,
137
    Resource__drop_async = 0x07,
138
    Resource__rep = 0x04,
139
    Backpressure__set = 0x08,
140
    Task__return = 0x09,
141
    Task__cancel = 0x05,
142
    Context__get = 0x0A,
143
    Context__set = 0x0B,
144
    Yield = 0x0C,
145
    Subtask__cancel = 0x06,
146
    Subtask__drop = 0x0D,
147
    Stream__new = 0x0E,
148
    Stream__read = 0x0F,
149
    Stream__write = 0x10,
150
    Stream__cancel_read = 0x11,
151
    Stream__cancel_write = 0x12,
152
    Stream__close_readable = 0x13,
153
    Stream__close_writable = 0x14,
154
    Future__new = 0x15,
155
    Future__read = 0x16,
156
    Future__write = 0x17,
157
    Future__cancel_read = 0x18,
158
    Future__cancel_write = 0x19,
159
    Future__close_readable = 0x1A,
160
    Future__close_writable = 0x1B,
161
    Error_context__new = 0x1C,
162
    Error_context__debug_message = 0x1D,
163
    Error_context__drop = 0x1E,
164
    Waitable_set__new = 0x1F,
165
    Waitable_set__wait = 0x20,
166
    Waitable_set__poll = 0x21,
167
    Waitable_set__drop = 0x22,
168
    Waitable__join = 0x23,
169
    Thread__spawn_ref = 0x40,
170
    Thread__spawn_indirect = 0x41,
171
    Thread__available_parallelism = 0x42,
172
  };
173
174
0
  Canonical() noexcept = default;
175
176
0
  OpCode getOpCode() const noexcept { return Code; }
177
0
  void setOpCode(const OpCode C) noexcept { Code = C; }
178
179
0
  bool isAsync() const noexcept { return IsAsync; }
180
0
  void setAsync(const bool A) noexcept { IsAsync = A; }
181
182
0
  uint32_t getIndex() const noexcept { return Idx; }
183
0
  void setIndex(const uint32_t I) noexcept { Idx = I; }
184
185
0
  uint32_t getTargetIndex() const noexcept { return TargetIdx; }
186
0
  void setTargetIndex(const uint32_t I) noexcept { TargetIdx = I; }
187
188
0
  uint32_t getConstVal() const noexcept { return I32; }
189
0
  void setConstVal(const uint32_t V) noexcept { I32 = V; }
190
191
0
  Span<const CanonOpt> getOptions() const noexcept { return Opts; }
192
0
  void setOptions(std::vector<CanonOpt> &&List) noexcept {
193
0
    Opts = std::move(List);
194
0
  }
195
196
private:
197
  OpCode Code;
198
  bool IsAsync;
199
  uint32_t Idx, TargetIdx;
200
  uint32_t I32;
201
  std::vector<CanonOpt> Opts;
202
};
203
204
} // namespace Component
205
} // namespace AST
206
} // namespace WasmEdge