libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
snobol_internal.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
8/* C23 keyword compatibility for pre-C23 compilers (MSVC, GCC/Clang < C23).
9 * __STDC_VERSION__ == 202311L for C23; anything lower (or undefined) means the
10 * compiler does not recognise 'nullptr' or 'constexpr' as keywords in C mode.
11 */
12#include "snobol/c23_compat.h"
13
14#include <stdint.h>
15#include <stddef.h>
16
17/* C11 _Alignof: MSVC in default mode (without /std:c11+) does not recognise
18 * _Alignof; use the MSVC-specific __alignof extension instead. */
19#if defined(_MSC_VER) && !defined(_Alignof)
20#define _Alignof __alignof
21#endif
22
23/* Default to standalone build unless PHP_BUILD is explicitly defined */
24#ifndef PHP_BUILD
25#define STANDALONE_BUILD 1
26#endif
27
28#ifdef STANDALONE_BUILD
29#include <stdlib.h>
30#include <string.h>
31#else
32#include "php.h"
33#endif
34
35/* Memory management macros to abstract away PHP's allocator */
36#ifdef STANDALONE_BUILD
37#define snobol_malloc malloc
38#define snobol_free free
39#define snobol_realloc realloc
40#define snobol_calloc calloc
41#else
42#define snobol_malloc emalloc
43#define snobol_free efree
44#define snobol_realloc erealloc
45#define snobol_calloc ecalloc
46#endif
47
59#ifdef STANDALONE_BUILD
60#define snobol_check_alloc(ptr) ((ptr) != NULL)
61#else
62#define snobol_check_alloc(ptr) ((void)(ptr), true)
63#endif
64
65/* Debug logging */
66/* #define SNOBOL_TRACE 1 */
67#ifdef SNOBOL_TRACE
68#include <stdarg.h>
69#include <stdio.h>
70#include <time.h>
71static inline void snobol_log_impl(const char *file, int line, const char *fmt,
72 ...) {
73 FILE *f = fopen("/tmp/snobol_debug.log", "a");
74 if (f) {
75 time_t now = time(NULL);
76 struct tm *t = localtime(&now);
77 char ts[32];
78 strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", t);
79 fprintf(f, "[%s] [%s:%d] ", ts, file, line);
80 va_list args;
81 va_start(args, fmt);
82 vfprintf(f, fmt, args);
83 va_end(args);
84 fprintf(f, "\n");
85 fflush(f);
86 fclose(f);
87 }
88}
89#define SNOBOL_LOG(fmt, ...) \
90 snobol_log_impl(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
91#else
92#define SNOBOL_LOG(fmt, ...) ((void)0)
93#endif
94
95/* ---------------------------------------------------------------------------
96 * Register-state liveness classes (D3)
97 *
98 * A conservative bytecode walk reports which register classes a pattern can
99 * touch. The search-VM restart loop uses this to zero only the classes the
100 * bytecode provably uses — caller-supplied snobol_search_meta_t descriptors
101 * are NOT trusted for this (raw-bytecode callers and tests build metas that
102 * only set eligibility bits; an uninitialized meta field would leave stack
103 * registers unzeroed and valgrind would flag uninitialised reads).
104 * ------------------------------------------------------------------------- */
105enum {
107 SNBL_REG_CAPTURE = 1 << 0, /* cap_start/cap_end + max_cap_used */
108 SNBL_REG_VARS = 1 << 1, /* var_start/var_end + var_count */
109 SNBL_REG_COUNTERS = 1 << 2 /* counters + loop_last_pos + max_counter_used */
110};
111
112/* Report which register classes the bytecode can touch. Conservative: an op
113 * the walker cannot classify marks EVERY class used. Never under-classifies,
114 * so callers may zero exactly the returned classes and no more. */
115uint8_t snobol_bc_register_classes(const uint8_t *bc, size_t bc_len);
116
117/* ---------------------------------------------------------------------------
118 * Bytecode body boundary
119 *
120 * Compiler-produced bytecode appends the charclass section and the label
121 * table after the instruction stream:
122 * [body][class blobs][class offsets u32 x N][class_count u32]
123 * [label offsets u32 x lc][label_count u32][MAGIC u32]
124 * The earliest class blob starts exactly where the body ends. Walkers need
125 * this bound: range data can contain bytes that mimic opcodes, so a linear
126 * scan that runs past the body may mistake metadata for instructions.
127 *
128 * Returns the body length, or 0 when the trailer does not validate (hand-
129 * built bytecode without one) — callers then fall back to their own bound.
130 * ------------------------------------------------------------------------- */
131size_t snobol_bc_body_len(const uint8_t *bc, size_t bc_len);
132
133#ifdef __cplusplus
134}
135#endif
uint8_t snobol_bc_register_classes(const uint8_t *bc, size_t bc_len)
size_t snobol_bc_body_len(const uint8_t *bc, size_t bc_len)
@ SNBL_REG_CAPTURE
@ SNBL_REG_NONE
@ SNBL_REG_COUNTERS
@ SNBL_REG_VARS