27#define SNOBOL_DYNAMIC_PATTERN 1
33#ifdef SNOBOL_DYNAMIC_PATTERN
86static inline int utf8_peek_next(
const char *s,
size_t len,
size_t pos,
87 uint32_t *out_cp,
int *out_bytes) {
91 unsigned char c = (
unsigned char)s[pos];
97 if ((c & 0xE0) == 0xC0) {
101 *out_cp = ((c & 0x1F) << 6) | ((
unsigned char)s[pos + 1] & 0x3F);
105 if ((c & 0xF0) == 0xE0) {
106 if (pos + 2 >= len) {
109 *out_cp = ((c & 0x0F) << 12) | (((
unsigned char)s[pos + 1] & 0x3F) << 6) |
110 ((
unsigned char)s[pos + 2] & 0x3F);
114 if ((c & 0xF8) == 0xF0) {
115 if (pos + 3 >= len) {
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);
129static inline uint32_t read_u32(
const uint8_t *bc,
size_t bc_len,
size_t *ip) {
130 if (*ip + 4 > bc_len) {
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];
141static inline uint16_t read_u16(
const uint8_t *bc,
size_t bc_len,
size_t *ip) {
142 if (*ip + 2 > bc_len) {
146 uint16_t v = ((uint16_t)bc[*ip] << 8) | ((uint16_t)bc[*ip + 1]);
152static inline uint8_t read_u8(
const uint8_t *bc,
size_t bc_len,
size_t *ip) {
153 if (*ip + 1 > bc_len) {
157 uint8_t v = bc[(*ip)++];
326typedef void (*
emit_cb)(
const char *data,
size_t len,
void *udata);
562 bool (*
eval_fn)(
int fn_id,
const char *
s,
size_t start,
size_t end,
575#ifdef SNOBOL_DYNAMIC_PATTERN
594 uint64_t dispatch_count;
659 uint16_t *out_count, uint16_t *out_case);
715static inline bool bitmap_test(
const uint64_t map[2], uint8_t c) {
720 return (map[0] & (1ULL << c)) != 0;
722 return (map[1] & (1ULL << (c - 64))) != 0;
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;
757#ifdef SNOBOL_DYNAMIC_PATTERN
828 size_t prior_last_pos);
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.
struct ChoiceArenaPage * prev
struct ChoiceArenaPage * next
A single trail undo record.
VM state: all mutable state for a single pattern execution.
uint32_t counters[MAX_LOOPS]
WriteLogEntry * write_log
size_t choice_peak_memory
uint64_t write_log_bitmap
struct snobol_auto_trie_t * trie_cache
struct snobol_pattern * pattern
Optional owning pattern (NULL for stateless searches). Used as the attachment point for the cached Ti...
const snobol_range_meta_t * range_meta
Cached charclass range metadata (set_id -> range data). Built at compile time; index is set_id - 1....
size_t var_start[MAX_VARS]
size_t dyn_pending_bc_len
size_t loop_last_pos[MAX_LOOPS]
size_t write_log_compressed_count
ChoiceArena * choices_arena
size_t cap_start[MAX_CAPS]
bool loop_span_greedy[MAX_LOOPS]
struct simd_nfa * simd_nfa
bool(* eval_fn)(int fn_id, const char *s, size_t start, size_t end, void *udata)
char * dyn_pending_source
size_t dyn_pending_source_len
uint32_t loop_min[MAX_LOOPS]
uint32_t loop_max[MAX_LOOPS]
dynamic_pattern_cache_t * dyn_cache
Write-log entry recording a single capture register modification.
Legacy full-snapshot choice stack entry.
uint32_t counters_snapshot[MAX_LOOPS]
size_t cap_end_snapshot[MAX_CAPS]
size_t var_count_snapshot
uint8_t max_counter_used_snapshot
size_t loop_last_pos_snapshot[MAX_LOOPS]
uint8_t max_cap_used_snapshot
size_t cap_start_snapshot[MAX_CAPS]
Unicode codepoint range [start, end] (both inclusive).
Lightweight VM state for search-mode execution (Tier 1-7).
uint32_t loop_min[MAX_LOOPS]
uint32_t loop_max[MAX_LOOPS]
uint32_t counters[MAX_LOOPS]
size_t cap_start[MAX_CAPS]
size_t var_start[MAX_VARS]
size_t loop_last_pos[MAX_LOOPS]
const snobol_range_meta_t * range_meta
Generic growable byte buffer used for VM output accumulation.
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.
size_t vm_compact_choice_record_size(const CompactChoiceHeader *hdr)
Return the total size in bytes of a compact choice record.
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.
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.
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).
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.
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.
void(* emit_cb)(const char *data, size_t len, void *udata)
Callback invoked by OP_EMIT_* instructions to stream output.
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.
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.
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)
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,...
UndoKind
Undo-record op kinds for the per-thread backtracking trail.
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).