libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
16
17#include <stdbool.h>
18#include <stddef.h>
19#include <stdint.h>
20
21struct snobol_pattern; /* forward decl — only used as an opaque pointer below */
22struct simd_nfa; /* forward decl — cached NFA for Tier 9 */
23struct
24 snobol_auto_trie_t; /* forward decl — pre-built alt-lit trie for Tier 5 */
25
27#define SNOBOL_DYNAMIC_PATTERN 1
28
29/* Forward declare table and array types */
30typedef struct snobol_table snobol_table_t;
31typedef struct snobol_array snobol_array_t;
32
33#ifdef SNOBOL_DYNAMIC_PATTERN
34/* Include dynamic pattern types */
36#endif
37
39enum { MAX_CAPS = 64 };
41enum { MAX_VARS = 64 };
42
48typedef struct cp_range {
49 uint32_t start;
50 uint32_t end;
52
60typedef struct {
61 const uint8_t *ranges_ptr;
62 uint16_t count;
72 uint64_t ascii_map[2];
75
86static inline int utf8_peek_next(const char *s, size_t len, size_t pos,
87 uint32_t *out_cp, int *out_bytes) {
88 if (pos >= len) {
89 return 0;
90 }
91 unsigned char c = (unsigned char)s[pos];
92 if (c < 0x80) {
93 *out_cp = c;
94 *out_bytes = 1;
95 return 1;
96 }
97 if ((c & 0xE0) == 0xC0) {
98 if (pos + 1 >= len) {
99 return 0;
100 }
101 *out_cp = ((c & 0x1F) << 6) | ((unsigned char)s[pos + 1] & 0x3F);
102 *out_bytes = 2;
103 return 1;
104 }
105 if ((c & 0xF0) == 0xE0) {
106 if (pos + 2 >= len) {
107 return 0;
108 }
109 *out_cp = ((c & 0x0F) << 12) | (((unsigned char)s[pos + 1] & 0x3F) << 6) |
110 ((unsigned char)s[pos + 2] & 0x3F);
111 *out_bytes = 3;
112 return 1;
113 }
114 if ((c & 0xF8) == 0xF0) {
115 if (pos + 3 >= len) {
116 return 0;
117 }
118 *out_cp = ((c & 0x07) << 18) | (((unsigned char)s[pos + 1] & 0x3F) << 12) |
119 (((unsigned char)s[pos + 2] & 0x3F) << 6) |
120 ((unsigned char)s[pos + 3] & 0x3F);
121 *out_bytes = 4;
122 return 1;
123 }
124 return 0;
125}
126
129static inline uint32_t read_u32(const uint8_t *bc, size_t bc_len, size_t *ip) {
130 if (*ip + 4 > bc_len) {
131 *ip = bc_len;
132 return 0;
133 }
134 uint32_t v = ((uint32_t)bc[*ip] << 24) | ((uint32_t)bc[*ip + 1] << 16) |
135 ((uint32_t)bc[*ip + 2] << 8) | (uint32_t)bc[*ip + 3];
136 *ip += 4;
137 return v;
138}
141static inline uint16_t read_u16(const uint8_t *bc, size_t bc_len, size_t *ip) {
142 if (*ip + 2 > bc_len) {
143 *ip = bc_len;
144 return 0;
145 }
146 uint16_t v = ((uint16_t)bc[*ip] << 8) | ((uint16_t)bc[*ip + 1]);
147 *ip += 2;
148 return v;
149}
152static inline uint8_t read_u8(const uint8_t *bc, size_t bc_len, size_t *ip) {
153 if (*ip + 1 > bc_len) {
154 *ip = bc_len;
155 return 0;
156 }
157 uint8_t v = bc[(*ip)++];
158 return v;
159}
160
239
240/* --------------------------------------------------------------------------
241 * OP_EMIT_FORMAT format_type discriminants (SNBL_FMT_*)
242 *
243 * Used by compile_template_to_bytecode() and the VM dispatch.
244 * SNBL_FMT_LPAD and SNBL_FMT_RPAD have two extra operands:
245 * width:u16 (big-endian, capped at 1024) and fill_char:u8.
246 * -------------------------------------------------------------------------- */
247enum {
249};
250enum {
252};
253enum {
255};
256enum { SNBL_FMT_LPAD = 4 };
257enum { SNBL_FMT_RPAD = 5 };
258
262enum { SNBL_TABLE_ID_UNBOUND = 0xFFFFU };
263
264/* --------------------------------------------------------------------------
265 * Built-in function dispatch enumeration
266 *
267 * These IDs are used with OP_EVAL to dispatch directly to C built-in
268 * functions in the VM, bypassing the host eval_fn callback for lower latency.
269 * IDs start at 1 (0 = SNOBOL_FN_NONE = host callback).
270 * -------------------------------------------------------------------------- */
277typedef enum {
279 /* String transformation functions */
293 /* Comparison / type-check functions */
302 /* Numeric comparison functions (multi-arg, routed to host eval_fn) */
310 28,
312
314enum { MAX_LOOPS = 16 };
315
319typedef struct {
320 char *data;
321 size_t len;
322 size_t cap;
323} snobol_buf;
324
326typedef void (*emit_cb)(const char *data, size_t len, void *udata);
327
333typedef struct {
334 uint8_t cap_index;
335 size_t old_start;
336 size_t old_end;
338
353
361typedef struct {
362 uint8_t kind;
363 uint8_t index;
364 uint8_t sub;
365 uint32_t prior_u;
366 size_t prior_a;
367 size_t prior_b;
368 size_t prior_lp;
369} UndoRecord;
370
378typedef struct {
379 uint32_t total_size;
380 size_t ip;
381 size_t pos;
382 size_t var_count;
383 uint8_t max_cap_used;
385 uint32_t trail_base;
386 uint8_t pad;
388
406
407/* ── Choice-stack arena (W2c) ──────────────────────────────────────────────
408 * The full-VM choice stack is stored in a page-linked arena rather than a
409 * single realloc'd contiguous buffer. Each page holds a run of fixed/compact
410 * records; when a record does not fit in the current page's spare space a new
411 * page is chained. Popping walks back within the current page (records carry a
412 * trailing size word), freeing empty pages past the head. This bounds peak
413 * allocation to the live working set and avoids large contiguous reallocs. */
414
416
417typedef struct ChoiceArenaPage {
418 uint8_t *data;
419 size_t cap;
420 size_t used;
424
432
436/* Reserve @p n contiguous bytes for a new record; returns the write pointer
437 * (already advanced in the arena). A trailing size word is NOT written here. */
438uint8_t *vm_arena_alloc(ChoiceArena *arena, size_t n);
439/* Remove the most recently allocated record from the arena. */
441
450typedef struct {
451 const uint8_t *bc;
452 size_t bc_len;
453
458 struct snobol_pattern *pattern;
459
465
466 const char *s;
467 size_t len; // bytes of s
468
469 size_t ip; // instruction pointer
470 size_t pos; // input byte index
471
472 // captures (byte offsets)
475
476 // named vars (filled by ASSIGN): start/end pairs
479 size_t var_count;
480
481 // loop counters
486 bool loop_span_greedy[MAX_LOOPS]; /* body is pure OP_SPAN — use max-greedy */
487
488 // optimization: track which captures/counters are actually used
489 uint8_t max_cap_used; // highest capture index used + 1 (0 = none used)
490 uint8_t max_counter_used; // highest counter index used + 1 (0 = none used)
491
492 // output buffer
494
495 /* Pike thread buffers: heap-allocated once, reused across calls.
496 * Sized to PIKE_THREAD_BUF and PIKE_DEFER_BUF respectively.
497 * Allocated by pike_scan on first use; freed in state-destroy. */
500
501 /* Cold-slab pool for the pike_scan hot/cold thread split (D1/D2). Owns
502 * up to PIKE_THREAD_BUF capture/variable/counter slabs, materialized
503 * lazily on first register op. Allocated by pike_scan on first use;
504 * freed in state-destroy. */
506
507 /* Cached SIMD NFA for Tier 9. Set by the search state before dispatch;
508 * tier_simd_nfa checks/updates it. Owned by the search state's destroy
509 * path, not by the VM. Declared as void* to avoid header dependency on
510 * simd_nfa_t (defined in search_simd.c). */
512
513 /* Pre-built alt-literals trie for Tier 5. Set by the PHP binding (or other
514 * host) before calling snobol_search_exec(). When non-NULL,
515 * search_alt_literals_try() uses this instead of reading from vm->pattern
516 * (which may point to a struct with different layout offsets). Owned by
517 * the PHP pattern object; never freed by VM cleanup. */
519
520 // emit callback
523
524 // choice stack for backtracking (page-linked arena, W2c)
526 size_t choices_top; /* == arena->total_used, for stats */
528 /* Scratch byte-buffer shared with the lightweight search-VM (Tier 6): it
529 * stores search_choice_t records here. Not used by the full-VM backtracking
530 * stack (that lives in choices_arena). Retained so search_vm_init_from_vm /
531 * search_vm_writeback_to_vm keep working unchanged. */
532 void *choices;
534 size_t choice_allocated; /* Total bytes allocated for choice records (for
535 stats) */
536 size_t
537 choice_push_count; /* Total number of choice records pushed (for stats) */
538 size_t choice_peak_depth; /* Peak number of simultaneous choice points */
539 size_t choice_peak_memory; /* Peak bytes used by the choice stack
540 simultaneously */
541 size_t choice_live_depth; /* Current number of live (not yet popped) choice
542 points */
543
544 /* Undo trail for trail-based choice save (W2a): records only the mutations a
545 * thread makes since the last choice point. Mirrors the choice stack: depth
546 * invariant trail_top == (number of live choice points). Restored by
547 * replaying the abandoned thread's entries in reverse. */
548 UndoRecord *trail; /* Growable array of undo records */
549 size_t trail_cap; /* Allocated capacity (records) */
550 size_t trail_top; /* Number of live trail records */
551
552 /* Write-log for compact choice stack: tracks capture modifications */
553 WriteLogEntry *write_log; /* Circular buffer of modification entries */
554 size_t write_log_cap; /* Allocated capacity (>= MAX_CAPS) */
555 size_t write_log_next; /* Next slot to use (circular) */
556 uint64_t write_log_bitmap; /* Bitmap: bit i set => entry i has valid data */
557 size_t write_log_compressed_count; /* Count of entries when compressed at
558 choice point */
559 bool write_log_dirty; /* True if write-log has un-compressed entries */
560
561 // callback for EVAL: returns true if ok, false to cause fail
562 bool (*eval_fn)(int fn_id, const char *s, size_t start, size_t end,
563 void *udata);
565
566 /* Control flow state for labelled patterns and goto-like transfers */
567 uint16_t *label_offsets; /* label_id -> bytecode offset */
568 size_t label_count; /* Number of defined labels */
569 size_t label_capacity; /* Allocated capacity */
570 uint16_t current_label; /* Current label being processed */
571 bool in_goto_fail; /* True if in GOTO_F failure handling */
572 bool abort_flag; /* True if ABORT opcode was executed */
573 bool keep_choices; /* If true, vm_run preserves choice stack across calls */
574
575#ifdef SNOBOL_DYNAMIC_PATTERN
576 /* Dynamic pattern support */
577 dynamic_pattern_cache_t *dyn_cache; /* Dynamic pattern cache */
578 snobol_table_t **tables; /* Table registry */
579 size_t table_count; /* Number of registered tables */
580 size_t table_capacity; /* Table registry capacity */
581 snobol_array_t **arrays; /* Array registry */
582 size_t array_count; /* Number of registered arrays */
583 size_t array_capacity; /* Array registry capacity */
584 char *dyn_pending_source; /* Pending dynamic pattern source text from
585 OP_DYNAMIC_DEF */
586 size_t dyn_pending_source_len; /* Length of pending source text */
587 uint8_t *
588 dyn_pending_bc; /* Pending dynamic pattern bytecode from OP_DYNAMIC_DEF */
589 size_t dyn_pending_bc_len; /* Length of pending bytecode */
590#endif
591
592#ifdef SNOBOL_PROFILE
593 struct {
594 uint64_t dispatch_count;
595 uint64_t push_count;
596 uint64_t pop_count;
597 size_t max_depth;
598 } profile;
599#endif
600} VM;
601
612typedef struct {
613 const uint8_t *bc;
614 size_t bc_len;
615
616 const char *s;
617 size_t len;
618
619 size_t ip;
620 size_t pos;
621
625
626 /* Choice stack for backtracking (Tier 6 search-VM). */
627 void *choices;
631
632 /* Loop counters for REPEAT_INIT/REPEAT_STEP (Tier 6 search-VM). */
638
639 /* Capture registers for CAP_START/CAP_END (Tier 6 capture-aware search-VM). */
643
644 /* Variable registers for ASSIGN (Tier 6 capture-aware search-VM). */
647 size_t var_count;
649
658const uint8_t *get_ranges_ptr(const VM *vm, uint16_t set_id,
659 uint16_t *out_count, uint16_t *out_case);
660
674void snobol_build_range_meta(const uint8_t *bc, size_t bc_len,
675 snobol_range_meta_t **out_table,
676 size_t *out_count);
677
686bool ranges_to_ascii_bitmap(const uint8_t *ranges_ptr, size_t count,
687 uint64_t map[2]);
688
701bool ranges_to_full_bitmap(const uint8_t *ranges_ptr, size_t count,
702 uint64_t map[4]);
703
711bool range_contains(const uint8_t *ranges_ptr, size_t count, uint32_t cp);
712
715static inline bool bitmap_test(const uint64_t map[2], uint8_t c) {
716 if (c > 127) {
717 return false;
718 }
719 if (c < 64) {
720 return (map[0] & (1ULL << c)) != 0;
721 }
722 return (map[1] & (1ULL << (c - 64))) != 0;
723}
724
726static inline bool bitmap_test_256(const uint64_t map[4], uint8_t c) {
727 unsigned idx = (unsigned)c >> 6;
728 return (map[idx] & (1ULL << (c & 63))) != 0;
729}
730
731/* VM entry */
737bool vm_exec(VM *vm);
738
744bool vm_run(VM *vm);
745
746/* Control flow management */
752bool vm_register_label(VM *vm, uint16_t label_id, uint32_t offset);
755uint32_t vm_get_label_offset(VM *vm, uint16_t label_id);
756
757#ifdef SNOBOL_DYNAMIC_PATTERN
758/* Table registry */
764bool vm_register_table(VM *vm, snobol_table_t *table, uint16_t *out_id);
766snobol_table_t *vm_get_table(VM *vm, uint16_t table_id);
772bool vm_register_array(VM *vm, snobol_array_t *array, uint16_t *out_id);
774snobol_array_t *vm_get_array(VM *vm, uint16_t array_id);
775#endif
776
777/* Buffer management */
781void snobol_buf_append(snobol_buf *b, const char *data, size_t len);
786
787/* helpers */
789void vm_push_choice(VM *vm, size_t ip, size_t pos);
792
797
798/* Write-log management for compact choice stack */
806void vm_write_log_track_cap_start(VM *vm, uint8_t cap, size_t old_start);
808void vm_write_log_track_cap_end(VM *vm, uint8_t cap, size_t old_end);
813 size_t dst_cap);
816
817/* Undo trail management for trail-based choice save (W2a) */
827void vm_trail_counter_inc(VM *vm, uint8_t loop_id, uint32_t prior_count,
828 size_t prior_last_pos);
832void vm_trail_cap_write(VM *vm, uint8_t cap, uint8_t sub, size_t prior_start,
833 size_t prior_end);
835void vm_trail_var_write(VM *vm, uint8_t var, size_t prior_start,
836 size_t prior_end);
838void vm_trail_replay(VM *vm, size_t base);
840size_t vm_trail_depth(const VM *vm);
841
842/* Choice stack statistics */
850
851#ifdef __cplusplus
852}
853#endif
struct snobol_array snobol_array_t
Sparse array state.
Dynamic pattern objects and canonical cache keys.
struct snobol_table snobol_table_t
Associative table (opaque handle).
struct snobol_auto_trie_t snobol_auto_trie_t
Opaque pre-allocated trie used by the Tier-5 alternation matcher.
Definition search.h:164
uint8_t * data
Definition vm.h:418
size_t cap
Definition vm.h:419
struct ChoiceArenaPage * prev
Definition vm.h:421
size_t used
Definition vm.h:420
struct ChoiceArenaPage * next
Definition vm.h:422
size_t peak_used
Definition vm.h:429
size_t last_rec_size
Definition vm.h:430
ChoiceArenaPage * head
Definition vm.h:426
size_t total_used
Definition vm.h:428
ChoiceArenaPage * cur
Definition vm.h:427
Header for a compact choice stack record (trail-based).
Definition vm.h:378
uint8_t max_counter_used
Definition vm.h:384
size_t var_count
Definition vm.h:382
uint8_t pad
Definition vm.h:386
uint8_t max_cap_used
Definition vm.h:383
uint32_t trail_base
Definition vm.h:385
uint32_t total_size
Definition vm.h:379
A single trail undo record.
Definition vm.h:361
size_t prior_a
Definition vm.h:366
size_t prior_b
Definition vm.h:367
uint32_t prior_u
Definition vm.h:365
uint8_t index
Definition vm.h:363
uint8_t sub
Definition vm.h:364
uint8_t kind
Definition vm.h:362
size_t prior_lp
Definition vm.h:368
VM state: all mutable state for a single pattern execution.
Definition vm.h:450
bool abort_flag
Definition vm.h:572
uint32_t counters[MAX_LOOPS]
Definition vm.h:482
size_t trail_top
Definition vm.h:550
size_t trail_cap
Definition vm.h:549
snobol_array_t ** arrays
Definition vm.h:581
WriteLogEntry * write_log
Definition vm.h:553
size_t choice_peak_memory
Definition vm.h:539
size_t ip
Definition vm.h:469
uint64_t write_log_bitmap
Definition vm.h:556
struct snobol_auto_trie_t * trie_cache
Definition vm.h:518
struct snobol_pattern * pattern
Optional owning pattern (NULL for stateless searches). Used as the attachment point for the cached Ti...
Definition vm.h:458
const snobol_range_meta_t * range_meta
Cached charclass range metadata (set_id -> range data). Built at compile time; index is set_id - 1....
Definition vm.h:463
bool keep_choices
Definition vm.h:573
size_t var_start[MAX_VARS]
Definition vm.h:477
size_t write_log_next
Definition vm.h:555
UndoRecord * trail
Definition vm.h:548
size_t table_count
Definition vm.h:579
size_t choice_allocated
Definition vm.h:534
bool in_goto_fail
Definition vm.h:571
const uint8_t * bc
Definition vm.h:451
size_t dyn_pending_bc_len
Definition vm.h:589
size_t array_count
Definition vm.h:582
uint8_t max_cap_used
Definition vm.h:489
size_t loop_last_pos[MAX_LOOPS]
Definition vm.h:485
emit_cb emit_fn
Definition vm.h:521
const char * s
Definition vm.h:466
size_t choice_peak_depth
Definition vm.h:538
uint8_t * dyn_pending_bc
Definition vm.h:588
uint16_t current_label
Definition vm.h:570
size_t var_count
Definition vm.h:479
size_t bc_len
Definition vm.h:452
size_t write_log_cap
Definition vm.h:554
void * pike_cold_pool
Definition vm.h:505
size_t range_meta_count
Definition vm.h:464
size_t table_capacity
Definition vm.h:580
size_t var_end[MAX_VARS]
Definition vm.h:478
void * choices
Definition vm.h:532
size_t write_log_compressed_count
Definition vm.h:557
size_t array_capacity
Definition vm.h:583
void * pike_thread_buf
Definition vm.h:498
snobol_buf * out
Definition vm.h:493
bool write_log_dirty
Definition vm.h:559
ChoiceArena * choices_arena
Definition vm.h:525
size_t cap_start[MAX_CAPS]
Definition vm.h:473
uint16_t * label_offsets
Definition vm.h:567
bool loop_span_greedy[MAX_LOOPS]
Definition vm.h:486
struct simd_nfa * simd_nfa
Definition vm.h:511
size_t pos
Definition vm.h:470
void * eval_udata
Definition vm.h:564
void * pike_defer_buf
Definition vm.h:499
size_t cap_end[MAX_CAPS]
Definition vm.h:474
size_t choices_top
Definition vm.h:526
size_t choice_live_depth
Definition vm.h:541
void * emit_udata
Definition vm.h:522
bool(* eval_fn)(int fn_id, const char *s, size_t start, size_t end, void *udata)
Definition vm.h:562
bool use_compact_choice
Definition vm.h:527
char * dyn_pending_source
Definition vm.h:584
size_t dyn_pending_source_len
Definition vm.h:586
size_t len
Definition vm.h:467
uint32_t loop_min[MAX_LOOPS]
Definition vm.h:483
uint32_t loop_max[MAX_LOOPS]
Definition vm.h:484
size_t label_count
Definition vm.h:568
dynamic_pattern_cache_t * dyn_cache
Definition vm.h:577
uint8_t max_counter_used
Definition vm.h:490
size_t choice_push_count
Definition vm.h:537
snobol_table_t ** tables
Definition vm.h:578
size_t label_capacity
Definition vm.h:569
size_t choices_cap
Definition vm.h:533
Write-log entry recording a single capture register modification.
Definition vm.h:333
size_t old_end
Definition vm.h:336
size_t old_start
Definition vm.h:335
uint8_t cap_index
Definition vm.h:334
Legacy full-snapshot choice stack entry.
Definition vm.h:395
uint32_t counters_snapshot[MAX_LOOPS]
Definition vm.h:401
size_t cap_end_snapshot[MAX_CAPS]
Definition vm.h:399
size_t var_count_snapshot
Definition vm.h:400
uint8_t max_counter_used_snapshot
Definition vm.h:404
size_t pos
Definition vm.h:397
size_t ip
Definition vm.h:396
size_t loop_last_pos_snapshot[MAX_LOOPS]
Definition vm.h:402
uint8_t max_cap_used_snapshot
Definition vm.h:403
size_t cap_start_snapshot[MAX_CAPS]
Definition vm.h:398
Unicode codepoint range [start, end] (both inclusive).
Definition vm.h:48
uint32_t start
Definition vm.h:49
uint32_t end
Definition vm.h:50
Lightweight VM state for search-mode execution (Tier 1-7).
Definition vm.h:612
size_t ip
Definition vm.h:619
uint8_t max_counter_used
Definition vm.h:637
void * choices
Definition vm.h:627
bool use_compact_choice
Definition vm.h:630
size_t range_meta_count
Definition vm.h:624
size_t bc_len
Definition vm.h:614
const uint8_t * bc
Definition vm.h:613
const char * s
Definition vm.h:616
uint32_t loop_min[MAX_LOOPS]
Definition vm.h:634
uint32_t loop_max[MAX_LOOPS]
Definition vm.h:635
size_t choices_cap
Definition vm.h:628
size_t cap_end[MAX_CAPS]
Definition vm.h:641
uint32_t counters[MAX_LOOPS]
Definition vm.h:633
size_t cap_start[MAX_CAPS]
Definition vm.h:640
size_t pos
Definition vm.h:620
size_t var_start[MAX_VARS]
Definition vm.h:645
size_t choices_top
Definition vm.h:629
size_t len
Definition vm.h:617
size_t loop_last_pos[MAX_LOOPS]
Definition vm.h:636
size_t var_count
Definition vm.h:647
const snobol_range_meta_t * range_meta
Definition vm.h:623
size_t var_end[MAX_VARS]
Definition vm.h:646
uint8_t max_cap_used
Definition vm.h:642
Sparse array state.
Definition array.h:46
Generic growable byte buffer used for VM output accumulation.
Definition vm.h:319
char * data
Definition vm.h:320
size_t cap
Definition vm.h:322
size_t len
Definition vm.h:321
Cached range metadata for a single charclass set.
Definition vm.h:60
bool has_ascii_map
Definition vm.h:73
uint16_t count
Definition vm.h:62
uint64_t ascii_map[2]
Definition vm.h:72
uint16_t case_insensitive
Definition vm.h:63
const uint8_t * ranges_ptr
Definition vm.h:61
void vm_trail_init(VM *vm)
Allocate the undo trail buffer.
void snobol_buf_free(snobol_buf *b)
Free buffer memory and reset to empty state.
void vm_trail_clear(VM *vm)
Reset trail depth to zero without freeing memory.
void vm_trail_free(VM *vm)
Free the undo trail buffer.
void snobol_buf_clear(snobol_buf *b)
Reset buffer length to 0 (keeps allocation).
bool ranges_to_ascii_bitmap(const uint8_t *ranges_ptr, size_t count, uint64_t map[2])
Build a 128-bit ASCII bitmap from range data; returns false if any range exceeds 127.
void vm_trail_counter_inc(VM *vm, uint8_t loop_id, uint32_t prior_count, size_t prior_last_pos)
Record a counter increment (OP_REPEAT_STEP) for undo on backtrack.
void vm_write_log_copy_entries(const VM *vm, WriteLogEntry *dst, size_t dst_cap)
Copy write-log entries into a caller-provided buffer.
void vm_trail_replay(VM *vm, size_t base)
Replay trail entries [base, top) in reverse, restoring prior state.
void snobol_build_range_meta(const uint8_t *bc, size_t bc_len, snobol_range_meta_t **out_table, size_t *out_count)
Build a range metadata table from bytecode for O(1) set_id lookup.
@ MAX_LOOPS
Definition vm.h:314
size_t vm_compact_choice_record_size(const CompactChoiceHeader *hdr)
Return the total size in bytes of a compact choice record.
@ MAX_CAPS
Definition vm.h:39
uint32_t vm_get_label_offset(VM *vm, uint16_t label_id)
Retrieve the bytecode offset for a label; returns UINT32_MAX if not found.
void vm_write_log_track_cap_end(VM *vm, uint8_t cap, size_t old_end)
Track an old cap_end value before overwriting register cap.
snobol_array_t * vm_get_array(VM *vm, uint16_t array_id)
Look up an array by runtime ID; returns NULL if not found.
@ SNBL_FMT_LOWER
Definition vm.h:251
void snobol_buf_append(snobol_buf *b, const char *data, size_t len)
Append len bytes from data to the buffer, growing as needed.
void vm_write_log_init(VM *vm)
Allocate write-log circular buffer.
@ SNBL_FMT_RPAD
Definition vm.h:257
bool vm_exec(VM *vm)
Execute a single pass of VM bytecode (used internally by vm_run).
size_t vm_choice_record_average_size(VM *vm)
Return the average choice record size in bytes (computed from accumulated stats).
@ SNBL_FMT_UPPER
Definition vm.h:248
void vm_free_arrays(VM *vm)
Free the VM's array registry.
ChoiceArena * vm_arena_create(void)
bool vm_pop_choice(VM *vm)
Pop the most recent choice point; return false if stack is empty.
void vm_write_log_track_cap_start(VM *vm, uint8_t cap, size_t old_start)
Track an old cap_start value before overwriting register cap.
void vm_write_log_free(VM *vm)
Free write-log buffer.
void vm_arena_destroy(ChoiceArena *arena)
snobol_builtin_fn_t
Built-in function dispatch IDs for direct C dispatch via OP_EVAL.
Definition vm.h:277
@ SNOBOL_FN_CHAR
Definition vm.h:289
@ SNOBOL_FN_LEXLT
Definition vm.h:297
@ SNOBOL_FN_REPLACE_CHAR
Definition vm.h:286
@ SNOBOL_FN_MAX
Definition vm.h:309
@ SNOBOL_FN_REPLACE
Definition vm.h:285
@ SNOBOL_FN_TRIM
Definition vm.h:281
@ SNOBOL_FN_LEXGT
Definition vm.h:298
@ SNOBOL_FN_SIZE
Definition vm.h:280
@ SNOBOL_FN_ORD
Definition vm.h:290
@ SNOBOL_FN_GE
Definition vm.h:308
@ SNOBOL_FN_RPAD
Definition vm.h:288
@ SNOBOL_FN_GT
Definition vm.h:306
@ SNOBOL_FN_EQ
Definition vm.h:303
@ SNOBOL_FN_LPAD
Definition vm.h:287
@ SNOBOL_FN_DIFFER
Definition vm.h:295
@ SNOBOL_FN_NUMERIC
Definition vm.h:301
@ SNOBOL_FN_IDENT
Definition vm.h:294
@ SNOBOL_FN_INTEGER
Definition vm.h:299
@ SNOBOL_FN_SUBSTR
Definition vm.h:284
@ SNOBOL_FN_LOWER
Definition vm.h:292
@ SNOBOL_FN_LEXEQ
Definition vm.h:296
@ SNOBOL_FN_NONE
Definition vm.h:278
@ SNOBOL_FN_NE
Definition vm.h:304
@ SNOBOL_FN_REAL
Definition vm.h:300
@ SNOBOL_FN_REVERSE
Definition vm.h:283
@ SNOBOL_FN_LE
Definition vm.h:307
@ SNOBOL_FN_UPPER
Definition vm.h:291
@ SNOBOL_FN_DUPL
Definition vm.h:282
@ SNOBOL_FN_LT
Definition vm.h:305
void vm_trail_cap_write(VM *vm, uint8_t cap, uint8_t sub, size_t prior_start, size_t prior_end)
Record a capture-register write (OP_CAP_START/OP_CAP_END) for undo. sub is 0 to restore only prior_st...
void vm_arena_pop_last(ChoiceArena *arena)
void vm_free_labels(VM *vm)
Free label offset table.
size_t vm_write_log_count_entries(const VM *vm)
Return the current number of valid write-log entries.
bool vm_register_table(VM *vm, snobol_table_t *table, uint16_t *out_id)
Register a table and return its runtime ID via out_id.
void vm_write_log_clear(VM *vm)
Clear all write-log entries without freeing memory.
@ MAX_VARS
Definition vm.h:41
void(* emit_cb)(const char *data, size_t len, void *udata)
Callback invoked by OP_EMIT_* instructions to stream output.
Definition vm.h:326
void snobol_buf_init(snobol_buf *b)
Initialise a snobol_buf to empty state (no allocation).
void vm_free_tables(VM *vm)
Free the VM's table registry.
size_t vm_trail_depth(const VM *vm)
Return the current number of live trail records.
@ SNBL_TABLE_ID_UNBOUND
Definition vm.h:262
bool range_contains(const uint8_t *ranges_ptr, size_t count, uint32_t cp)
Test whether a codepoint is contained in a packed range array.
void vm_push_choice(VM *vm, size_t ip, size_t pos)
Push a backtracking choice point onto the VM's choice stack.
OpCode
Bytecode opcodes for the SNOBOL4 VM.
Definition vm.h:167
@ OP_ASSIGN
Definition vm.h:179
@ OP_CAP_END
Definition vm.h:178
@ OP_DYNAMIC_DEF
Definition vm.h:220
@ OP_SPLIT
Definition vm.h:171
@ OP_SUCCEED
Definition vm.h:234
@ OP_REPEAT_STEP
Definition vm.h:185
@ OP_ABORT
Definition vm.h:233
@ OP_POS
Definition vm.h:231
@ OP_FENCE
Definition vm.h:227
@ OP_NOP
Definition vm.h:237
@ OP_TAB
Definition vm.h:232
@ OP_LEN
Definition vm.h:180
@ OP_REM
Definition vm.h:228
@ OP_REPEAT_INIT
Definition vm.h:183
@ OP_BREAK
Definition vm.h:176
@ OP_ARRAY_GET
Definition vm.h:214
@ OP_EMIT_LITERAL
Definition vm.h:186
@ OP_BAL
Definition vm.h:226
@ OP_LIT
Definition vm.h:172
@ OP_EMIT_EXPR
Definition vm.h:188
@ OP_SPAN
Definition vm.h:175
@ OP_TABLE_SET
Definition vm.h:208
@ OP_EMIT_CAPTURE
Definition vm.h:187
@ OP_CAP_START
Definition vm.h:177
@ OP_RTAB
Definition vm.h:230
@ OP_ANY
Definition vm.h:173
@ OP_GOTO_F
Definition vm.h:199
@ OP_RPOS
Definition vm.h:229
@ OP_ANCHOR
Definition vm.h:182
@ OP_FAIL
Definition vm.h:169
@ OP_TABLE_GET
Definition vm.h:200
@ OP_DYNAMIC
Definition vm.h:218
@ OP_NOTANY
Definition vm.h:174
@ OP_BREAKX
Definition vm.h:224
@ OP_ARRAY_SET
Definition vm.h:216
@ OP_GOTO
Definition vm.h:198
@ OP_EVAL
Definition vm.h:181
@ OP_LABEL
Definition vm.h:197
@ OP_EMIT_TABLE
Definition vm.h:192
@ OP_ACCEPT
Definition vm.h:168
@ OP_JMP
Definition vm.h:170
@ OP_EMIT_FORMAT
Definition vm.h:193
uint8_t * vm_arena_alloc(ChoiceArena *arena, size_t n)
snobol_table_t * vm_get_table(VM *vm, uint16_t table_id)
Look up a table by runtime ID; returns NULL if not found.
bool vm_run(VM *vm)
Run the VM, re-executing on backtrack until accept or exhausted.
size_t vm_choice_stack_depth(VM *vm)
Return the current number of live choice points on the stack.
void vm_init_arrays(VM *vm)
Initialise the VM's array registry.
bool vm_register_array(VM *vm, snobol_array_t *array, uint16_t *out_id)
Register an array and return its runtime ID via out_id.
bool vm_register_label(VM *vm, uint16_t label_id, uint32_t offset)
Register a label at a given bytecode offset.
bool ranges_to_full_bitmap(const uint8_t *ranges_ptr, size_t count, uint64_t map[4])
Build a 256-bit byte-level bitmap from range data; supports the full 0-255 byte range (UTF-8 safe).
void vm_arena_reset(ChoiceArena *arena)
@ SNBL_FMT_LENGTH
Definition vm.h:254
void vm_trail_push(VM *vm, UndoRecord rec)
Push a record onto the trail, growing the buffer as needed.
void snobol_vm_reset(VM *vm)
Reset VM state between match attempts while keeping choice/allocation. Clears captures,...
@ SNBL_FMT_LPAD
Definition vm.h:256
UndoKind
Undo-record op kinds for the per-thread backtracking trail.
Definition vm.h:347
@ UNDO_CAP_WRITE
Definition vm.h:349
@ UNDO_COUNTER_DEC
Definition vm.h:348
@ UNDO_WL_POP
Definition vm.h:350
@ UNDO_VAR_WRITE
Definition vm.h:351
@ CHOICE_ARENA_PAGE_SIZE
Definition vm.h:415
void vm_init_tables(VM *vm)
Initialise the VM's table registry.
const uint8_t * get_ranges_ptr(const VM *vm, uint16_t set_id, uint16_t *out_count, uint16_t *out_case)
Retrieve charclass range data for a set ID.
void vm_trail_var_write(VM *vm, uint8_t var, size_t prior_start, size_t prior_end)
Record a named-variable write (OP_ASSIGN) for undo on backtrack.
size_t vm_choice_stack_memory_usage(VM *vm)
Return the current bytes used by the choice stack.
void vm_init_labels(VM *vm)
Allocate label offset table; call before vm_run().
struct cp_range CpRange
Unicode codepoint range [start, end] (both inclusive).