ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
program.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_expressions of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
9#define HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
10
11#ifndef HPP_ALIB_EXPRESSIONS_DETAIL_VIRTUAL_MACHINE
13#endif
14
15#ifndef HPP_ALIB_EXPRESSIONS_COMPILER
17#endif
18
19#if !defined (HPP_ALIB_MONOMEM_LIST)
20# include "alib/monomem/list.hpp"
21#endif
22
23#if !defined (HPP_ALIB_MONOMEM_STDCONTAINERMA)
25#endif
26
27#if !defined (HPP_ALIB_MONOMEM_MASTRING)
29#endif
30
31namespace alib { namespace expressions {
32
33/**
34 * This is the detail namespace of #alib::expressions implementing the abstract syntax tree,
35 * the expression parser, the expression program and the virtual machine to execute the program
36 * with expression evaluation.
37 */
38namespace detail {
39
40
41/** ************************************************************************************************
42 * This class represents a program that is "run on" the \alib{expressions;detail::VirtualMachine}
43 * to evaluate an expression.
44 *
45 * ## Friends ##
46 * class \alib{expressions;detail::VirtualMachine}
47 **************************************************************************************************/
49{
50 // #############################################################################################
51 // Public fields
52 // #############################################################################################
53 public:
54 /** The compiler that created this object. */
56
57 /** The expression that this program evaluates. */
59
60
61 // #############################################################################################
62 // Internal fields
63 // #############################################################################################
64 protected:
65 /// Shortcut.
67
68 /** The array of commands. */
70
71 /** The number of commands. */
73
74 /**
75 * List of compile-time identified nested expressions. Using the shared pointers, it is
76 * ensured that the expressions do not get deleted until this program is.
77 */
78 std::vector<SPExpression,
80
81 /** Counter of the number of optimization made during program assembly. */
83
84 /**
85 * Data needed during compilation. An instance of this object is allocated
86 * in the temporary compile-time monotonic allocator.
87 */
89 {
90 /** The intermediate program listing. Commands are collected here during
91 * compilation. Only when finalized, the result is copied into the vector that
92 * the outer class program inherits from, which uses the non-temporary monotonic
93 * allocator. */
94 std::vector<VirtualMachine::Command*, StdContMA<VirtualMachine::Command*>>
96
97 /** Used with compilation. Stores the positions of current result types while adding new
98 * commands. */
99 std::vector<VM::PC,StdContMA<VM::PC>>
101
102 /**
103 * Compile-time information on conditional operator jump positions.
104 */
106 {
107 #if !defined(ALIB_DOX)
108 ConditionalInfo( VM::PC q, VM::PC t, int f)
109 : QJumpPos (q)
110 , TJumpPos (t)
111 , ConstFlags(f)
112 {}
113 #endif
114
115 VM::PC QJumpPos; ///< The position of the lhs result.
116 VM::PC TJumpPos; ///< The position of the jump command between T and F.
117 int ConstFlags; ///< Two bits: Bit 1 determines whether Q was constant and
118 ///< bit 0 stores the value of Q (if constant)
119 };
120
121 /**
122 * Stores the positions of current results while adding new commands.
123 * The third value is used for optimizing constant conditionals out.
124 */
125 std::vector<ConditionalInfo, StdContMA<ConditionalInfo>>
127
128 /**
129 * Needed during compilation. Collects information from plug-ins to create meaningful
130 * messages.
131 */
133
134 /**
135 * Constructor.<br>
136 * The given allocator is used exclusively during compilation.
137 * Its memory is cleared (respectively reset to a previous state) after the
138 * compilation completed. The program is created in this allocator. Only when
139 * compilation is finished (and after all optimizations have been performed)
140 * it is copied to the compile-time scope's allocator.<br>
141 * (Refers to object \alib{expressions;Compiler::allocator}.)
142 *
143 * @param compileTimeAllocator The allocator to use temporarily during compilation.
144 */
145 CompileStorage( MonoAllocator& compileTimeAllocator )
146 : Assembly ( compileTimeAllocator)
147 , ResultStack ( compileTimeAllocator)
148 , ConditionalStack ( compileTimeAllocator)
149 , FunctionsWithNonMatchingArguments(&compileTimeAllocator)
150 {
151 Assembly .reserve(30);
152 ResultStack .reserve(20);
153 ConditionalStack.reserve(5);
154 }
155
156 };
157
158 /**
159 * Set of data needed during compilation and deleted afterwards. Also, this field indicates
160 * that compilation is "suppressed", which is used when normalized optimized expression
161 * strings are generated from de-compiled programs.
162 */
164
165
166 // #############################################################################################
167 // Constructor/destructor
168 // ##################################################P###########################################
169 public:
170 /** ****************************************************************************************
171 * Constructor.
172 *
173 * Prepares the assembly if \p{pCTScope} is given. If it is \c nullptr, then no program is
174 * assembled. This option is used for creating normalized expression strings from
175 * de-compiled, optimized programs.
176 *
177 * @param pCompiler Stored in field #compiler.
178 * @param pExpression Stored in field #expression.
179 * @param compileTimeAllocator A temporary allocator valid until assembly of the program
180 * finished.<br>
181 * If \c nullptr, no assembly is performed in subsequent
182 * invocations of assemble methods. This option is used for
183 * the generation of normalized, optimized expression strings.
184 * from reversely created ASTs.
185 ******************************************************************************************/
187 Program( Compiler& pCompiler , Expression& pExpression,
188 MonoAllocator* compileTimeAllocator );
189
190
191 /** ****************************************************************************************
192 * Destructor.
193 ******************************************************************************************/
195
196 // #############################################################################################
197 // Overrides
198 // #############################################################################################
199 /** ****************************************************************************************
200 * Returns the result type of the program.
201 * @return The program's result type.
202 ******************************************************************************************/
204 const Box& ResultType() const;
205
206 /** ****************************************************************************************
207 * Returns the number of \alib{expressions::detail;VirtualMachine::Command} that the
208 * program encompasses.
209 * @return The program's length.
210 ******************************************************************************************/
212 {
213 return commandsCount;
214 }
215
216 /** ****************************************************************************************
217 * Returns the command at the given program counter \p{pc}.
218 * @param pc The program counter of the command to get.
219 * @return A reference to the requested command.
220 ******************************************************************************************/
227
228 /** ****************************************************************************************
229 * Implementation of abstract interface method.
230 *
231 * @return The number of optimizations or \c -1 if optimizations were not activated during
232 * program assembly.
233 ******************************************************************************************/
235 {
236 return qtyOptimizations;
237 }
238
239 /** ****************************************************************************************
240 * Runs the program using the virtual machine.
241 *
242 * @param scope The evaluation scope.
243 * @return The result value.
244 ******************************************************************************************/
245 Box Run(Scope& scope)
246 {
247 return VirtualMachine::Run(*this, scope);
248 }
249
250
251 // #############################################################################################
252 // Assemble methods
253 // #############################################################################################
254
255 /** ****************************************************************************************
256 * Has to be invoked to finalizes the program after.
257 * No further invocations of assemble methods must be invoked after a call to this methods.
258 ******************************************************************************************/
260 void AssembleFinalize();
261
262
263 /** ****************************************************************************************
264 * Add a command that produces a constant value. Used with literals.
265 * @param value The value to produce.
266 * @param idxInOriginal The index of the operator in the expression string.
267 * @param idxInNormalized The index of the operator in the normalized expression
268 * string.
269 ******************************************************************************************/
271 void AssembleConstant( Box& value,
272 integer idxInOriginal, integer idxInNormalized );
273
274 /** ****************************************************************************************
275 * Add a command that invokes a native function.
276 * @param functionName The name of the function.
277 * Used to query the corresponding native C++ callback
278 * function from the compiler plug-ins.
279 * @param idxInOriginal The index of the operator in the expression string.
280 * @param idxInNormalized The index of the operator in the normalized expression
281 * string.
282 * @param qtyArgsOrNoParentheses The number of function args. If negative, this indicates
283 * that the function name was given as an 'identifier', what
284 * means that no brackets '()' had been added.
285 ******************************************************************************************/
287 void AssembleFunction( AString& functionName, integer qtyArgsOrNoParentheses,
288 integer idxInOriginal, integer idxInNormalized );
289
290 /** ****************************************************************************************
291 * Add a command that invokes a native function that implements an unary operator.
292 * @param op The operator to add a command for.
293 * @param idxInOriginal The index of the operator in the expression string.
294 * @param idxInNormalized The index of the operator in the normalized expression string.
295 ******************************************************************************************/
297 void AssembleUnaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
298
299 /** ****************************************************************************************
300 * Add a command that invokes a native function that implements a binary operator.
301 * @param op The operator to add a command for.
302 * @param idxInOriginal The index of the operator in the expression string.
303 * @param idxInNormalized The index of the operator in the normalized expression string.
304 ******************************************************************************************/
306 void AssembleBinaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
307
308 /** ****************************************************************************************
309 * To be called after the AST for \c Q was assembled. Adds an "jump on false" statement,
310 * unless it is detected that \c Q is constant. In this case, only one of the subsequent
311 * sub-expressions is assembled.
312 *
313 * Has to be followed by assembly of \c T, followed by
314 * #AssembleCondFinalize_T and furthermore assembly of \c F, followed by
315 * #AssembleCondFinalize_F.
316 *
317 * @param idxInOriginal The index of the operator in the expression string.
318 * @param idxInNormalized The index of the operator in the normalized expression string.
319 ******************************************************************************************/
321 void AssembleCondFinalize_Q( integer idxInOriginal, integer idxInNormalized );
322
323 /** ****************************************************************************************
324 * End of ternary \c T expression. Jumps to end of \c f.
325 * @param idxInOriginal The index of the operator in the expression string.
326 * @param idxInNormalized The index of the operator in the normalized expression string.
327 ******************************************************************************************/
329 void AssembleCondFinalize_T( integer idxInOriginal, integer idxInNormalized );
330
331 /** ****************************************************************************************
332 * Finalizes a previously started conditional expression.
333 * @param idxInOriginal The index of the operator in the expression string.
334 * @param idxInNormalized The index of the operator in the normalized expression string.
335 ******************************************************************************************/
337 void AssembleCondFinalize_F( integer idxInOriginal, integer idxInNormalized );
338
339
340
341 // #############################################################################################
342 // Internals used during compilation
343 // #############################################################################################
344 protected:
345 /**
346 * Collects \p{qty} types from the result stack and stores them, respectively the constant
347 * values in field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;af1f402e9cac81ef3ad0a982590344472;Scope::Stack;expressions::Scope::Stack}
348 * stack of field \alib{expressions;Expression::ctScope}.
349 * @param qty The number of types to collect.
350 * @return \c true if all arguments collected were constants.
351 */
353 bool collectArgs( integer qty );
354
355};
356
357}}} // namespace [alib::expressions::detail]
358
359
360#endif // HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
ALIB_API void AssembleCondFinalize_T(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:898
ALIB_API const Box & ResultType() const
Definition program.cpp:55
ALIB_API bool collectArgs(integer qty)
Definition program.cpp:247
ALIB_API Program(Compiler &pCompiler, Expression &pExpression, MonoAllocator *compileTimeAllocator)
Definition program.cpp:39
ALIB_API void AssembleCondFinalize_F(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:920
ALIB_API void AssembleUnaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:476
ALIB_API void AssembleFunction(AString &functionName, integer qtyArgsOrNoParentheses, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:284
ALIB_API void AssembleConstant(Box &value, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:270
ALIB_API void AssembleCondFinalize_Q(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:867
std::vector< SPExpression, StdContMA< SPExpression > > ctNestedExpressions
Definition program.hpp:79
VM::Command & At(VM::PC pc)
Definition program.hpp:221
ALIB_API void AssembleBinaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:660
integer PC
Type definition for a program counter.
static ALIB_API alib::Box Run(Program &program, Scope &scope)
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_API
Definition alib.hpp:538
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
std::shared_ptr< Expression > SPExpression
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
VM::PC TJumpPos
The position of the jump command between T and F.
Definition program.hpp:116
std::vector< ConditionalInfo, StdContMA< ConditionalInfo > > ConditionalStack
Definition program.hpp:126
CompileStorage(MonoAllocator &compileTimeAllocator)
Definition program.hpp:145
std::vector< VirtualMachine::Command *, StdContMA< VirtualMachine::Command * > > Assembly
Definition program.hpp:95
std::vector< VM::PC, StdContMA< VM::PC > > ResultStack
Definition program.hpp:100