/src/WasmEdge/include/ast/segment.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/segment.h - segment class definitions ----------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the Segment node class and subsegment |
12 | | /// node classes: GlobalSegment, ElementSegment, CodeSegment, and DataSegment. |
13 | | /// |
14 | | //===----------------------------------------------------------------------===// |
15 | | #pragma once |
16 | | |
17 | | #include "ast/expression.h" |
18 | | #include "ast/type.h" |
19 | | |
20 | | #include <vector> |
21 | | |
22 | | namespace WasmEdge { |
23 | | namespace AST { |
24 | | |
25 | | /// Segment's base class. |
26 | | class Segment { |
27 | | public: |
28 | | /// Getter of expression. |
29 | 28.0k | const Expression &getExpr() const noexcept { return Expr; } |
30 | 31.1k | Expression &getExpr() noexcept { return Expr; } |
31 | | |
32 | | protected: |
33 | | /// Expression node in this segment. |
34 | | Expression Expr; |
35 | | }; |
36 | | |
37 | | /// AST GlobalSegment node. |
38 | | class GlobalSegment : public Segment { |
39 | | public: |
40 | | /// Getter of global type. |
41 | 733 | const GlobalType &getGlobalType() const noexcept { return Global; } |
42 | 926 | GlobalType &getGlobalType() noexcept { return Global; } |
43 | | |
44 | | private: |
45 | | /// \name Data of GlobalSegment node. |
46 | | /// @{ |
47 | | GlobalType Global; |
48 | | /// @} |
49 | | }; |
50 | | |
51 | | /// AST ElementSegment node. |
52 | | class ElementSegment : public Segment { |
53 | | public: |
54 | | /// Element mode enumeration. |
55 | | enum class ElemMode : uint8_t { Passive, Active, Declarative }; |
56 | | |
57 | | /// Getter and setter of element mode. |
58 | 576 | ElemMode getMode() const noexcept { return Mode; } |
59 | 6.21k | void setMode(ElemMode EMode) noexcept { Mode = EMode; } |
60 | | |
61 | | /// Getter of reference type. |
62 | 2.66k | const ValType &getRefType() const noexcept { return Type; } |
63 | 5.52k | void setRefType(const ValType &RType) noexcept { |
64 | 5.52k | assuming(RType.isRefType()); |
65 | 5.52k | Type = RType; |
66 | 5.52k | } |
67 | | |
68 | | /// Getter of table index. |
69 | 946 | uint32_t getIdx() const noexcept { return TableIdx; } |
70 | 7.30k | void setIdx(uint32_t Idx) noexcept { TableIdx = Idx; } |
71 | | |
72 | | /// Getter of initialization expressions. |
73 | 602 | Span<const Expression> getInitExprs() const noexcept { return InitExprs; } |
74 | 33.1k | std::vector<Expression> &getInitExprs() noexcept { return InitExprs; } |
75 | | |
76 | | private: |
77 | | /// \name Data of ElementSegment node. |
78 | | /// @{ |
79 | | ElemMode Mode = ElemMode::Active; |
80 | | ValType Type = TypeCode::FuncRef; |
81 | | uint32_t TableIdx = 0; |
82 | | std::vector<Expression> InitExprs; |
83 | | /// @} |
84 | | }; |
85 | | |
86 | | /// AST TableSegment node. |
87 | | class TableSegment : public Segment { |
88 | | public: |
89 | | /// Getter of table type. |
90 | 907 | const TableType &getTableType() const noexcept { return TType; } |
91 | 571 | TableType &getTableType() noexcept { return TType; } |
92 | | |
93 | | private: |
94 | | /// \name Data of TableSegment node. |
95 | | /// @{ |
96 | | TableType TType; |
97 | | /// @} |
98 | | }; |
99 | | |
100 | | /// AST CodeSegment node. |
101 | | class CodeSegment : public Segment { |
102 | | public: |
103 | | /// Getter and setter of segment size. |
104 | 22.4k | uint32_t getSegSize() const noexcept { return SegSize; } |
105 | 22.4k | void setSegSize(uint32_t Size) noexcept { SegSize = Size; } |
106 | | |
107 | | /// Getter of locals vector. |
108 | 26.9k | Span<const std::pair<uint32_t, ValType>> getLocals() const noexcept { |
109 | 26.9k | return Locals; |
110 | 26.9k | } |
111 | 48.1k | std::vector<std::pair<uint32_t, ValType>> &getLocals() noexcept { |
112 | 48.1k | return Locals; |
113 | 48.1k | } |
114 | | |
115 | | /// Getter and setter of compiled symbol. |
116 | 0 | const auto &getSymbol() const noexcept { return FuncSymbol; } |
117 | 0 | void setSymbol(Symbol<void> S) noexcept { FuncSymbol = std::move(S); } |
118 | | |
119 | | private: |
120 | | /// \name Data of CodeSegment node. |
121 | | /// @{ |
122 | | uint32_t SegSize = 0; |
123 | | std::vector<std::pair<uint32_t, ValType>> Locals; |
124 | | Symbol<void> FuncSymbol; |
125 | | /// @} |
126 | | }; |
127 | | |
128 | | /// AST DataSegment node. |
129 | | class DataSegment : public Segment { |
130 | | public: |
131 | | /// Data mode enumeration. |
132 | | enum class DataMode : uint8_t { Passive, Active }; |
133 | | |
134 | | /// Getter and setter of data mode. |
135 | 367 | DataMode getMode() const noexcept { return Mode; } |
136 | 6.91k | void setMode(DataMode DMode) noexcept { Mode = DMode; } |
137 | | |
138 | | /// Getter and setter of memory index. |
139 | 228 | uint32_t getIdx() const noexcept { return MemoryIdx; } |
140 | 5.94k | void setIdx(uint32_t Idx) noexcept { MemoryIdx = Idx; } |
141 | | |
142 | | /// Getter of data. |
143 | 0 | Span<const Byte> getData() const noexcept { return Data; } |
144 | 3.75k | std::vector<Byte> &getData() noexcept { return Data; } |
145 | | |
146 | | private: |
147 | | /// \name Data of DataSegment node. |
148 | | /// @{ |
149 | | DataMode Mode = DataMode::Active; |
150 | | uint32_t MemoryIdx = 0; |
151 | | std::vector<Byte> Data; |
152 | | /// @} |
153 | | }; |
154 | | |
155 | | } // namespace AST |
156 | | } // namespace WasmEdge |