1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
#ifndef AWKWARD_FORTHMACHINE_H_
#define AWKWARD_FORTHMACHINE_H_
#include <set>
#include <map>
#include <stack>
#include "awkward/common.h"
#include "awkward/util.h"
#include "awkward/forth/ForthInputBuffer.h"
#include "awkward/forth/ForthOutputBuffer.h"
namespace awkward {
/// @class ForthMachine
///
/// @brief HERE
///
/// THERE
template <typename T, typename I>
class EXPORT_SYMBOL ForthMachineOf {
template <typename TYPE> using IndexTypeOf = typename std::vector<TYPE>::size_type;
public:
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);
~ForthMachineOf();
/// @brief HERE
int64_t
abi_version() const noexcept;
/// @brief HERE
const std::string
source() const noexcept;
/// @brief HERE
const std::vector<I>
bytecodes() const;
/// @brief HERE
const std::vector<int64_t>
bytecodes_offsets() const;
/// @brief HERE
const std::string
decompiled() const;
/// @brief HERE
const std::string
decompiled_segment(int64_t segment_position, const std::string& indent="",
bool endline = true) const;
/// @brief HERE
const std::string
decompiled_at(int64_t bytecode_position, const std::string& indent="") const;
/// @brief HERE
const std::vector<std::string>
dictionary() const;
/// @brief HERE
int64_t
stack_max_depth() const noexcept;
/// @brief HERE
int64_t
recursion_max_depth() const noexcept;
/// @brief HERE
int64_t
string_buffer_size() const noexcept;
/// @brief HERE
int64_t
output_initial_size() const noexcept;
/// @brief HERE
double
output_resize_factor() const noexcept;
/// @brief HERE
const std::vector<T>
stack() const;
/// @brief HERE
T
stack_at(int64_t from_top) const noexcept;
/// @brief HERE
int64_t
stack_depth() const noexcept;
/// @brief HERE
inline bool
stack_can_push() const noexcept {
return stack_depth_ < stack_max_depth_;
}
/// @brief HERE
inline bool
stack_can_pop() const noexcept {
return stack_depth_ > 0;
}
/// @brief HERE
inline void
stack_push(T value) noexcept {
stack_buffer_[stack_depth_] = value;
stack_depth_++;
}
/// @brief HERE
inline T
stack_pop() noexcept {
stack_depth_--;
return stack_buffer_[stack_depth_];
}
/// @brief HERE
void
stack_clear() noexcept;
/// @brief HERE
const std::map<std::string, T>
variables() const;
/// @brief HERE
const std::vector<std::string>
variable_index() const;
/// @brief HERE
T
variable_at(const std::string& name) const;
/// @brief HERE
T
variable_at(int64_t index) const noexcept;
/// @brief HERE
bool
input_must_be_writable(const std::string& name) const;
/// @brief HERE
int64_t
input_position_at(const std::string& name) const;
/// @brief HERE
int64_t
input_position_at(int64_t index) const noexcept;
/// @brief HERE
const std::map<std::string, std::shared_ptr<ForthOutputBuffer>>
outputs() const;
/// @brief HERE
const std::vector<std::string>
output_index() const noexcept;
/// @brief HERE
const std::shared_ptr<ForthOutputBuffer>
output_at(const std::string& name) const;
/// @brief HERE
const std::shared_ptr<ForthOutputBuffer>
output_at(int64_t index) const noexcept;
/// @brief Returns a string at 'index'.
/// The strings are defined with an 's"' core word.
const std::string
string_at(int64_t index) const noexcept;
/// @brief HERE
void
reset();
/// @brief HERE
void
begin(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs);
/// @brief HERE
void
begin();
///@brief HERE
util::ForthError
begin_again(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs, bool reset_instruction);
/// @brief HERE
util::ForthError
step();
/// @brief HERE
util::ForthError
run(const std::map<std::string, std::shared_ptr<ForthInputBuffer>>& inputs);
/// @brief HERE
util::ForthError
run();
/// @brief HERE
util::ForthError
resume();
/// @brief HERE
util::ForthError
call(const std::string& name);
/// @brief HERE
util::ForthError
call(int64_t index);
/// @brief HERE
void
maybe_throw(util::ForthError err, const std::set<util::ForthError>& ignore) const;
/// @brief HERE
int64_t
current_bytecode_position() const noexcept;
/// @brief HERE
int64_t
current_recursion_depth() const noexcept;
/// @brief HERE
const std::string
current_instruction() const;
/// @brief HERE
void
count_reset() noexcept;
/// @brief HERE
int64_t
count_instructions() const noexcept;
/// @brief HERE
int64_t
count_reads() const noexcept;
/// @brief HERE
int64_t
count_writes() const noexcept;
/// @brief HERE
int64_t
count_nanoseconds() const noexcept;
/// @brief HERE
bool
is_integer(const std::string& word, int64_t& value) const;
/// @brief HERE
bool
is_variable(const std::string& word) const;
/// @brief HERE
bool
is_input(const std::string& word) const;
/// @brief HERE
bool
is_output(const std::string& word) const;
/// @brief HERE
bool
is_nbit(const std::string& word, I& value) const;
/// @brief HERE
bool
is_reserved(const std::string& word) const;
/// @brief HERE
bool
is_defined(const std::string& word) const;
/// @brief HERE
inline bool
is_ready() const noexcept {
return is_ready_;
}
/// @brief HERE
inline bool
is_done() const noexcept {
return recursion_target_depth_.empty();
}
/// @brief HERE
inline bool
is_segment_done() const noexcept {
return !(bytecodes_pointer_where() < (
bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which() + 1] -
bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()]
));
}
private:
/// @brief HERE
bool
segment_nonempty(int64_t segment_position) const;
/// @brief HERE
int64_t
bytecodes_per_instruction(int64_t bytecode_position) const;
/// @brief HERE
const std::string
err_linecol(const std::vector<std::pair<int64_t, int64_t>>& linecol,
int64_t startpos,
int64_t stoppos,
const std::string& message) const;
/// @brief HERE
void
tokenize(std::vector<std::string>& tokenized,
std::vector<std::pair<int64_t, int64_t>>& linecol);
/// @brief HERE
void
compile(const std::vector<std::string>& tokenized,
const std::vector<std::pair<int64_t, int64_t>>& linecol);
/// @brief HERE
void
parse(const std::string& defn,
const std::vector<std::string>& tokenized,
const std::vector<std::pair<int64_t, int64_t>>& linecol,
int64_t start,
int64_t stop,
std::vector<I>& bytecodes,
std::vector<std::vector<I>>& dictionary,
int64_t exitdepth,
int64_t dodepth);
/// @brief HERE
void
internal_run(bool single_step, int64_t recursion_target_depth_top); // noexcept
/// @brief HERE
void
write_from_stack(int64_t num, T* top) noexcept;
/// @brief HERE
void
write_add_from_stack(int64_t num, T* top) noexcept;
/// @brief HERE
void
print_number(T num) noexcept;
/// @brief HERE
inline bool
stack_cannot_push() const noexcept {
return stack_depth_ == stack_max_depth_;
}
/// @brief HERE
inline bool
stack_cannot_pop() const noexcept {
return stack_depth_ == 0;
}
/// @brief HERE
inline bool
stack_cannot_pop2() const noexcept {
return stack_depth_ < 2;
}
/// @brief HERE
inline bool
stack_cannot_pop3() const noexcept {
return stack_depth_ < 3;
}
/// @brief HERE
inline T*
stack_pop2() noexcept {
stack_depth_ -= 2;
return &stack_buffer_[stack_depth_];
}
/// @brief HERE
inline T*
stack_pop2_before_pushing1() noexcept {
stack_depth_--;
return &stack_buffer_[stack_depth_ - 1];
}
/// @brief HERE
inline T*
stack_peek() const noexcept {
return &stack_buffer_[stack_depth_ - 1];
}
/// @brief HERE
inline I
bytecode_get() const noexcept {
int64_t start = bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()];
return bytecodes_[(IndexTypeOf<I>)(start + bytecodes_pointer_where())];
}
/// @brief HERE
inline void
bytecodes_pointer_push(int64_t which) noexcept {
current_which_[recursion_current_depth_] = which;
current_where_[recursion_current_depth_] = 0;
recursion_current_depth_++;
}
/// @brief HERE
inline void
bytecodes_pointer_pop() noexcept {
recursion_current_depth_--;
}
/// @brief HERE
inline int64_t&
bytecodes_pointer_which() const noexcept {
return current_which_[recursion_current_depth_ - 1];
}
/// @brief HERE
inline int64_t&
bytecodes_pointer_where() const noexcept {
return current_where_[recursion_current_depth_ - 1];
}
/// @brief HERE
inline void
do_loop_push(int64_t start, int64_t stop) noexcept {
do_recursion_depth_[do_current_depth_] = recursion_current_depth_;
do_stop_[do_current_depth_] = stop;
do_i_[do_current_depth_] = start;
do_current_depth_++;
}
/// @brief HERE
inline void
do_steploop_push(int64_t start, int64_t stop) noexcept {
do_recursion_depth_[do_current_depth_] = ~recursion_current_depth_;
do_stop_[do_current_depth_] = stop;
do_i_[do_current_depth_] = start;
do_current_depth_++;
}
/// @brief HERE
inline int64_t&
do_recursion_depth() const noexcept {
return do_recursion_depth_[do_current_depth_ - 1];
}
/// @brief HERE
inline int64_t
do_abs_recursion_depth() const noexcept {
int64_t out = do_recursion_depth_[do_current_depth_ - 1];
if (out < 0) {
return ~out;
}
else {
return out;
}
}
/// @brief HERE
inline bool
do_loop_is_step() const noexcept {
return do_recursion_depth_[do_current_depth_ - 1] < 0;
}
/// @brief HERE
inline int64_t&
do_stop() const noexcept {
return do_stop_[do_current_depth_ - 1];
}
/// @brief HERE
inline int64_t&
do_i() const noexcept {
return do_i_[do_current_depth_ - 1];
}
/// @brief HERE
inline int64_t&
do_j() const noexcept {
return do_i_[do_current_depth_ - 2];
}
/// @brief HERE
inline int64_t&
do_k() const noexcept {
return do_i_[do_current_depth_ - 3];
}
std::string source_;
int64_t output_initial_size_;
double output_resize_factor_;
T* stack_buffer_;
int64_t stack_depth_;
int64_t stack_max_depth_;
std::vector<std::string> variable_names_;
std::vector<T> variables_;
std::vector<std::string> input_names_;
std::vector<bool> input_must_be_writable_;
std::vector<std::string> output_names_;
std::vector<util::dtype> output_dtypes_;
std::vector<std::string> strings_;
std::vector<std::string> dictionary_names_;
std::vector<I> dictionary_bytecodes_;
std::vector<int64_t> bytecodes_offsets_;
std::vector<I> bytecodes_;
char* string_buffer_;
int64_t string_buffer_size_;
std::vector<std::shared_ptr<ForthInputBuffer>> current_inputs_;
std::vector<std::shared_ptr<ForthOutputBuffer>> current_outputs_;
bool is_ready_;
int64_t* current_which_;
int64_t* current_where_;
int64_t recursion_current_depth_;
std::stack<int64_t> recursion_target_depth_;
int64_t recursion_max_depth_;
int64_t* do_recursion_depth_;
int64_t* do_stop_;
int64_t* do_i_;
int64_t do_current_depth_;
util::ForthError current_error_;
int64_t count_instructions_;
int64_t count_reads_;
int64_t count_writes_;
int64_t count_nanoseconds_;
};
using ForthMachine32 = ForthMachineOf<int32_t, int32_t>;
using ForthMachine64 = ForthMachineOf<int64_t, int32_t>;
}
#endif // AWKWARD_FORTHMACHINE_H_
|