Awkward Array: include/awkward/forth/ForthMachine.h Source File

Loading...
Searching...
No Matches
ForthMachine.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2
3#ifndef AWKWARD_FORTHMACHINE_H_
4#define AWKWARD_FORTHMACHINE_H_
5
6#include <set>
7#include <map>
8#include <stack>
9
10#include "awkward/common.h"
11#include "awkward/util.h"
12#include "awkward/Content.h"
13#include "awkward/array/NumpyArray.h"
14#include "awkward/array/ListOffsetArray.h"
15#include "awkward/forth/ForthInputBuffer.h"
16#include "awkward/forth/ForthOutputBuffer.h"
17
18namespace awkward {
24 template <typename T, typename I>
25 class LIBAWKWARD_EXPORT_SYMBOL ForthMachineOf {
26
27 template <typename TYPE> using IndexTypeOf = typename std::vector<TYPE>::size_type;
28
29 public:
30 ForthMachineOf(const std::string& source,
31 int64_t stack_max_depth=1024,
32 int64_t recursion_max_depth=1024,
33 int64_t string_buffer_size=1024,
34 int64_t output_initial_size=1024,
35 double output_resize_factor=1.5);
36
37 ~ForthMachineOf();
38
40 const std::string
41 source() const noexcept;
42
44 const ContentPtr
45 bytecodes() const;
46
48 const std::string
49 decompiled() const;
50
52 const std::string
53 decompiled_segment(int64_t segment_position, const std::string& indent="",
54 bool endline = true) const;
55
57 const std::string
58 decompiled_at(int64_t bytecode_position, const std::string& indent="") const;
59
61 const std::vector<std::string>
62 dictionary() const;
63
65 int64_t
66 stack_max_depth() const noexcept;
67
69 int64_t
70 recursion_max_depth() const noexcept;
71
73 int64_t
74 string_buffer_size() const noexcept;
75
77 int64_t
78 output_initial_size() const noexcept;
79
81 double
82 output_resize_factor() const noexcept;
83
85 const std::vector<T>
86 stack() const;
87
89 T
90 stack_at(int64_t from_top) const noexcept;
91
93 int64_t
94 stack_depth() const noexcept;
95
97 inline bool
98 stack_can_push() const noexcept {
99 return stack_depth_ < stack_max_depth_;
100 }
101
103 inline bool
104 stack_can_pop() const noexcept {
105 return stack_depth_ > 0;
106 }
107
109 inline void
110 stack_push(T value) noexcept {
111 stack_buffer_[stack_depth_] = value;
112 stack_depth_++;
113 }
114
116 inline T
117 stack_pop() noexcept {
118 stack_depth_--;
119 return stack_buffer_[stack_depth_];
120 }
121
123 void
124 stack_clear() noexcept;
125
127 const std::map<std::string, T>
128 variables() const;
129
131 const std::vector<std::string>
132 variable_index() const;
133
135 T
136 variable_at(const std::string& name) const;
137
139 T
140 variable_at(int64_t index) const noexcept;
141
143 bool
144 input_must_be_writable(const std::string& name) const;
145
147 int64_t
148 input_position_at(const std::string& name) const;
149
151 int64_t
152 input_position_at(int64_t index) const noexcept;
153
155 const std::map<std::string, std::shared_ptr<ForthOutputBuffer>>
156 outputs() const;
157
159 const std::vector<std::string>
160 output_index() const noexcept;
161
163 const std::shared_ptr<ForthOutputBuffer>
164 output_at(const std::string& name) const;
165
167 const std::shared_ptr<ForthOutputBuffer>
168 output_at(int64_t index) const noexcept;
169
171 const ContentPtr
172 output_NumpyArray_at(const std::string& name) const;
173
175 const ContentPtr
176 output_NumpyArray_at(int64_t index) const;
177
179 const Index8
180 output_Index8_at(const std::string& name) const;
181
183 const Index8
184 output_Index8_at(int64_t index) const;
185
187 const IndexU8
188 output_IndexU8_at(const std::string& name) const;
189
191 const IndexU8
192 output_IndexU8_at(int64_t index) const;
193
195 const Index32
196 output_Index32_at(const std::string& name) const;
197
199 const Index32
200 output_Index32_at(int64_t index) const;
201
203 const IndexU32
204 output_IndexU32_at(const std::string& name) const;
205
207 const IndexU32
208 output_IndexU32_at(int64_t index) const;
209
211 const Index64
212 output_Index64_at(const std::string& name) const;
213
215 const Index64
216 output_Index64_at(int64_t index) const;
217
220 const std::string
221 string_at(int64_t index) const noexcept;
222
224 void
225 reset();
226
228 void
229 begin(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs);
230
232 void
233 begin();
234
236 util::ForthError
237 begin_again(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs, bool reset_instruction);
238
240 util::ForthError
241 step();
242
244 util::ForthError
245 run(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs);
246
248 util::ForthError
249 run();
250
252 util::ForthError
253 resume();
254
256 util::ForthError
257 call(const std::string& name);
258
260 util::ForthError
261 call(int64_t index);
262
264 void
265 maybe_throw(util::ForthError err, const std::set<util::ForthError>& ignore) const;
266
268 int64_t
269 current_bytecode_position() const noexcept;
270
272 int64_t
273 current_recursion_depth() const noexcept;
274
276 const std::string
277 current_instruction() const;
278
280 void
281 count_reset() noexcept;
282
284 int64_t
285 count_instructions() const noexcept;
286
288 int64_t
289 count_reads() const noexcept;
290
292 int64_t
293 count_writes() const noexcept;
294
296 int64_t
297 count_nanoseconds() const noexcept;
298
300 bool
301 is_integer(const std::string& word, int64_t& value) const;
302
304 bool
305 is_variable(const std::string& word) const;
306
308 bool
309 is_input(const std::string& word) const;
310
312 bool
313 is_output(const std::string& word) const;
314
316 bool
317 is_nbit(const std::string& word, I& value) const;
318
320 bool
321 is_reserved(const std::string& word) const;
322
324 bool
325 is_defined(const std::string& word) const;
326
328 inline bool
329 is_ready() const noexcept {
330 return is_ready_;
331 }
332
334 inline bool
335 is_done() const noexcept {
336 return recursion_target_depth_.empty();
337 }
338
340 inline bool
341 is_segment_done() const noexcept {
342 return !(bytecodes_pointer_where() < (
343 bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which() + 1] -
344 bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()]
345 ));
346 }
347
348 private:
350 bool
351 segment_nonempty(int64_t segment_position) const;
352
354 int64_t
355 bytecodes_per_instruction(int64_t bytecode_position) const;
356
358 const std::string
359 err_linecol(const std::vector<std::pair<int64_t, int64_t>>& linecol,
360 int64_t startpos,
361 int64_t stoppos,
362 const std::string& message) const;
363
365 void
366 tokenize(std::vector<std::string>& tokenized,
367 std::vector<std::pair<int64_t, int64_t>>& linecol);
368
370 void
371 compile(const std::vector<std::string>& tokenized,
372 const std::vector<std::pair<int64_t, int64_t>>& linecol);
373
375 void
376 parse(const std::string& defn,
377 const std::vector<std::string>& tokenized,
378 const std::vector<std::pair<int64_t, int64_t>>& linecol,
379 int64_t start,
380 int64_t stop,
381 std::vector<I>& bytecodes,
382 std::vector<std::vector<I>>& dictionary,
383 int64_t exitdepth,
384 int64_t dodepth);
385
387 void
388 internal_run(bool single_step, int64_t recursion_target_depth_top); // noexcept
389
391 void
392 write_from_stack(int64_t num, T* top) noexcept;
393
395 void
396 write_add_from_stack(int64_t num, T* top) noexcept;
397
399 void
400 print_number(T num) noexcept;
401
403 inline bool
404 stack_cannot_push() const noexcept {
405 return stack_depth_ == stack_max_depth_;
406 }
407
409 inline bool
410 stack_cannot_pop() const noexcept {
411 return stack_depth_ == 0;
412 }
413
415 inline bool
416 stack_cannot_pop2() const noexcept {
417 return stack_depth_ < 2;
418 }
419
421 inline bool
422 stack_cannot_pop3() const noexcept {
423 return stack_depth_ < 3;
424 }
425
427 inline T*
428 stack_pop2() noexcept {
429 stack_depth_ -= 2;
430 return &stack_buffer_[stack_depth_];
431 }
432
434 inline T*
435 stack_pop2_before_pushing1() noexcept {
436 stack_depth_--;
437 return &stack_buffer_[stack_depth_ - 1];
438 }
439
441 inline T*
442 stack_peek() const noexcept {
443 return &stack_buffer_[stack_depth_ - 1];
444 }
445
447 inline I
448 bytecode_get() const noexcept {
449 int64_t start = bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()];
450 return bytecodes_[(IndexTypeOf<I>)(start + bytecodes_pointer_where())];
451 }
452
454 inline void
455 bytecodes_pointer_push(int64_t which) noexcept {
456 current_which_[recursion_current_depth_] = which;
457 current_where_[recursion_current_depth_] = 0;
458 recursion_current_depth_++;
459 }
460
462 inline void
463 bytecodes_pointer_pop() noexcept {
464 recursion_current_depth_--;
465 }
466
468 inline int64_t&
469 bytecodes_pointer_which() const noexcept {
470 return current_which_[recursion_current_depth_ - 1];
471 }
472
474 inline int64_t&
475 bytecodes_pointer_where() const noexcept {
476 return current_where_[recursion_current_depth_ - 1];
477 }
478
480 inline void
481 do_loop_push(int64_t start, int64_t stop) noexcept {
482 do_recursion_depth_[do_current_depth_] = recursion_current_depth_;
483 do_stop_[do_current_depth_] = stop;
484 do_i_[do_current_depth_] = start;
485 do_current_depth_++;
486 }
487
489 inline void
490 do_steploop_push(int64_t start, int64_t stop) noexcept {
491 do_recursion_depth_[do_current_depth_] = ~recursion_current_depth_;
492 do_stop_[do_current_depth_] = stop;
493 do_i_[do_current_depth_] = start;
494 do_current_depth_++;
495 }
496
498 inline int64_t&
499 do_recursion_depth() const noexcept {
500 return do_recursion_depth_[do_current_depth_ - 1];
501 }
502
504 inline int64_t
505 do_abs_recursion_depth() const noexcept {
506 int64_t out = do_recursion_depth_[do_current_depth_ - 1];
507 if (out < 0) {
508 return ~out;
509 }
510 else {
511 return out;
512 }
513 }
514
516 inline bool
517 do_loop_is_step() const noexcept {
518 return do_recursion_depth_[do_current_depth_ - 1] < 0;
519 }
520
522 inline int64_t&
523 do_stop() const noexcept {
524 return do_stop_[do_current_depth_ - 1];
525 }
526
528 inline int64_t&
529 do_i() const noexcept {
530 return do_i_[do_current_depth_ - 1];
531 }
532
534 inline int64_t&
535 do_j() const noexcept {
536 return do_i_[do_current_depth_ - 2];
537 }
538
540 inline int64_t&
541 do_k() const noexcept {
542 return do_i_[do_current_depth_ - 3];
543 }
544
545 std::string source_;
546 int64_t output_initial_size_;
547 double output_resize_factor_;
548
549 T* stack_buffer_;
550 int64_t stack_depth_;
551 int64_t stack_max_depth_;
552
553 std::vector<std::string> variable_names_;
554 std::vector<T> variables_;
555
556 std::vector<std::string> input_names_;
557 std::vector<bool> input_must_be_writable_;
558 std::vector<std::string> output_names_;
559 std::vector<util::dtype> output_dtypes_;
560
561 std::vector<std::string> strings_;
562 std::vector<std::string> dictionary_names_;
563 std::vector<I> dictionary_bytecodes_;
564 std::vector<int64_t> bytecodes_offsets_;
565 std::vector<I> bytecodes_;
566
567 char* string_buffer_;
568 int64_t string_buffer_size_;
569
570 std::vector<std::shared_ptr<ForthInputBuffer>> current_inputs_;
571 std::vector<std::shared_ptr<ForthOutputBuffer>> current_outputs_;
572 bool is_ready_;
573
574 int64_t* current_which_;
575 int64_t* current_where_;
576 int64_t recursion_current_depth_;
577 std::stack<int64_t> recursion_target_depth_;
578 int64_t recursion_max_depth_;
579
580 int64_t* do_recursion_depth_;
581 int64_t* do_stop_;
582 int64_t* do_i_;
583 int64_t do_current_depth_;
584
585 util::ForthError current_error_;
586
587 int64_t count_instructions_;
588 int64_t count_reads_;
589 int64_t count_writes_;
590 int64_t count_nanoseconds_;
591 };
592
593 using ForthMachine32 = ForthMachineOf<int32_t, int32_t>;
594 using ForthMachine64 = ForthMachineOf<int64_t, int32_t>;
595
596}
597
598#endif // AWKWARD_FORTHMACHINE_H_
Content.h
ForthInputBuffer.h
ForthOutputBuffer.h
ListOffsetArray.h
NumpyArray.h
awkward::ForthInputBuffer
HERE.
Definition: ForthInputBuffer.h:17
awkward::ForthMachineOf
Definition: ForthMachine.h:25
awkward::ForthMachineOf::stack_clear
void stack_clear() noexcept
HERE.
awkward::ForthMachineOf::stack_push
void stack_push(T value) noexcept
HERE.
Definition: ForthMachine.h:110
awkward::ForthMachineOf::is_segment_done
bool is_segment_done() const noexcept
HERE.
Definition: ForthMachine.h:341
awkward::ForthMachineOf::~ForthMachineOf
~ForthMachineOf()
awkward::ForthMachineOf::stack_pop
T stack_pop() noexcept
HERE.
Definition: ForthMachine.h:117
awkward::ForthMachineOf::stack_can_pop
bool stack_can_pop() const noexcept
HERE.
Definition: ForthMachine.h:104
awkward::ForthMachineOf::is_done
bool is_done() const noexcept
HERE.
Definition: ForthMachine.h:335
awkward::ForthMachineOf::source
const std::string source() const noexcept
HERE.
awkward::ForthMachineOf::ForthMachineOf
ForthMachineOf(const std::string &source, int64_t stack_max_depth=1024, int64_t recursion_max_depth=1024, int64_t string_buffer_size=1024, int64_t output_initial_size=1024, double output_resize_factor=1.5)
awkward::ForthOutputBuffer
HERE.
Definition: ForthOutputBuffer.h:36
awkward::IndexOf
A contiguous, one-dimensional array of integers used to represent data structures,...
Definition: Index.h:82
common.h
LIBAWKWARD_EXPORT_SYMBOL
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
awkward
Definition: BitMaskedArray.h:15
awkward::ContentPtr
std::shared_ptr< Content > ContentPtr
Definition: Content.h:15
util.h