Plasma Engine  2.0
Loading...
Searching...
No Matches
ExpressionByteCode.h
1#pragma once
2
3#include <Foundation/CodeUtils/Expression/ExpressionDeclarations.h>
4#include <Foundation/Containers/Blob.h>
5
8
9class PL_FOUNDATION_DLL plExpressionByteCode
10{
11public:
12 struct OpCode
13 {
14 enum Enum
15 {
16 Nop,
17
18 FirstUnary,
19
20 AbsF_R,
21 AbsI_R,
22 SqrtF_R,
23
24 ExpF_R,
25 LnF_R,
26 Log2F_R,
27 Log2I_R,
28 Log10F_R,
29 Pow2F_R,
30
31 SinF_R,
32 CosF_R,
33 TanF_R,
34
35 ASinF_R,
36 ACosF_R,
37 ATanF_R,
38
39 RoundF_R,
40 FloorF_R,
41 CeilF_R,
42 TruncF_R,
43
44 NotI_R,
45 NotB_R,
46
47 IToF_R,
48 FToI_R,
49
50 LastUnary,
51
52 FirstBinary,
53
54 AddF_RR,
55 AddI_RR,
56
57 SubF_RR,
58 SubI_RR,
59
60 MulF_RR,
61 MulI_RR,
62
63 DivF_RR,
64 DivI_RR,
65
66 MinF_RR,
67 MinI_RR,
68
69 MaxF_RR,
70 MaxI_RR,
71
72 ShlI_RR,
73 ShrI_RR,
74 AndI_RR,
75 XorI_RR,
76 OrI_RR,
77
78 EqF_RR,
79 EqI_RR,
80 EqB_RR,
81
82 NEqF_RR,
83 NEqI_RR,
84 NEqB_RR,
85
86 LtF_RR,
87 LtI_RR,
88
89 LEqF_RR,
90 LEqI_RR,
91
92 GtF_RR,
93 GtI_RR,
94
95 GEqF_RR,
96 GEqI_RR,
97
98 AndB_RR,
99 OrB_RR,
100
101 LastBinary,
102
103 FirstBinaryWithConstant,
104
105 AddF_RC,
106 AddI_RC,
107
108 SubF_RC,
109 SubI_RC,
110
111 MulF_RC,
112 MulI_RC,
113
114 DivF_RC,
115 DivI_RC,
116
117 MinF_RC,
118 MinI_RC,
119
120 MaxF_RC,
121 MaxI_RC,
122
123 ShlI_RC,
124 ShrI_RC,
125 AndI_RC,
126 XorI_RC,
127 OrI_RC,
128
129 EqF_RC,
130 EqI_RC,
131 EqB_RC,
132
133 NEqF_RC,
134 NEqI_RC,
135 NEqB_RC,
136
137 LtF_RC,
138 LtI_RC,
139
140 LEqF_RC,
141 LEqI_RC,
142
143 GtF_RC,
144 GtI_RC,
145
146 GEqF_RC,
147 GEqI_RC,
148
149 AndB_RC,
150 OrB_RC,
151
152 LastBinaryWithConstant,
153
154 FirstTernary,
155
156 SelF_RRR,
157 SelI_RRR,
158 SelB_RRR,
159
160 LastTernary,
161
162 FirstSpecial,
163
164 MovX_R,
165 MovX_C,
166 LoadF,
167 LoadI,
168 StoreF,
169 StoreI,
170
171 Call,
172
173 LastSpecial,
174
175 Count
176 };
177
178 static const char* GetName(Enum code);
179 };
180
181 using StorageType = plUInt32;
182
186
187 void operator=(const plExpressionByteCode& other);
188
189 bool operator==(const plExpressionByteCode& other) const;
190 bool operator!=(const plExpressionByteCode& other) const { return !(*this == other); }
191
192 void Clear();
193 bool IsEmpty() const { return m_uiByteCodeCount == 0; }
194
195 const StorageType* GetByteCodeStart() const;
196 const StorageType* GetByteCodeEnd() const;
197 plArrayPtr<const StorageType> GetByteCode() const;
198
199 plUInt32 GetNumInstructions() const;
200 plUInt32 GetNumTempRegisters() const;
204
205 static OpCode::Enum GetOpCode(const StorageType*& ref_pByteCode);
206 static plUInt32 GetRegisterIndex(const StorageType*& ref_pByteCode);
207 static plExpression::Register GetConstant(const StorageType*& ref_pByteCode);
208 static plUInt32 GetFunctionIndex(const StorageType*& ref_pByteCode);
209 static plUInt32 GetFunctionArgCount(const StorageType*& ref_pByteCode);
210
211 void Disassemble(plStringBuilder& out_sDisassembly) const;
212
213 plResult Save(plStreamWriter& inout_stream) const;
214 plResult Load(plStreamReader& inout_stream, plByteArrayPtr externalMemory = plByteArrayPtr());
215
216 plConstByteBlobPtr GetDataBlob() const { return m_Data.GetByteBlobPtr(); }
217
218private:
219 friend class plExpressionCompiler;
220
221 void Init(plArrayPtr<const StorageType> byteCode, plArrayPtr<const plExpression::StreamDesc> inputs, plArrayPtr<const plExpression::StreamDesc> outputs, plArrayPtr<const plExpression::FunctionDesc> functions, plUInt32 uiNumTempRegisters, plUInt32 uiNumInstructions);
222
223 plBlob m_Data;
224
225 plExpression::StreamDesc* m_pInputs = nullptr;
226 plExpression::StreamDesc* m_pOutputs = nullptr;
227 plExpression::FunctionDesc* m_pFunctions = nullptr;
228 StorageType* m_pByteCode = nullptr;
229
230 plUInt32 m_uiByteCodeCount = 0;
231 plUInt16 m_uiNumInputs = 0;
232 plUInt16 m_uiNumOutputs = 0;
233 plUInt16 m_uiNumFunctions = 0;
234
235 plUInt16 m_uiNumTempRegisters = 0;
236 plUInt32 m_uiNumInstructions = 0;
237};
238
239#if PL_ENABLED(PL_PLATFORM_64BIT)
240static_assert(sizeof(plExpressionByteCode) == 64);
241#endif
242
243PL_DECLARE_REFLECTABLE_TYPE(PL_FOUNDATION_DLL, plExpressionByteCode);
244PL_DECLARE_CUSTOM_VARIANT_TYPE(plExpressionByteCode);
245
246#include <Foundation/CodeUtils/Expression/Implementation/ExpressionByteCode_inl.h>
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
plBlob allows to store simple binary data larger than 4GB. This storage class is used by plImage to a...
Definition Blob.h:319
This class encapsulates a blob's storage and it's size. It is recommended to use this class instead o...
Definition Blob.h:13
Definition ExpressionByteCode.h:10
Definition ExpressionCompiler.h:9
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
Describes an expression function and its signature, e.g. how many input parameter it has and their ty...
Definition ExpressionDeclarations.h:72
Definition ExpressionDeclarations.h:17
Describes an input or output stream for a expression VM.
Definition ExpressionDeclarations.h:57
Definition ExpressionByteCode.h:13
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54