Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file vm.h
12 * @brief Core virtual machine data structures and public VM API.
13 *
14 * Declares the execution stack/frame layout, global state of the Fun VM,
15 * human-readable opcode names for diagnostics, and the public functions for
16 * initializing, running, resetting, and debugging the VM. FFI helper
17 * declarations for Rust/C++ experiments are also exposed here.
18 */
19#ifndef FUN_VM_H
20#define FUN_VM_H
21
22#include "bytecode.h"
23#include <stddef.h>
24
25#ifndef MAX_FRAMES
26#define MAX_FRAMES 128
27#endif
28
29#ifndef MAX_FRAME_LOCALS
30#define MAX_FRAME_LOCALS 64
31#endif
32
33#ifndef MAX_GLOBALS
34#define MAX_GLOBALS 128
35#endif
36
37#ifndef OUTPUT_SIZE
38#define OUTPUT_SIZE 1024
39#endif
40
41#ifndef STACK_SIZE
42#define STACK_SIZE 1024
43#endif
44
45static const char *opcode_names[] = {
46 "NOP", "LOAD_CONST", "LOAD_LOCAL", "STORE_LOCAL",
47 "LOAD_GLOBAL", "STORE_GLOBAL", "ADD", "SUB", "MUL", "DIV",
48 "LT", "LTE", "GT", "GTE", "EQ", "NEQ", "POP", "JUMP",
49 "JUMP_IF_FALSE", "CALL", "RETURN", "PRINT", "ECHO", "HALT",
50 "LINE",
51 "MOD", "AND", "OR", "NOT", "DUP", "SWAP",
52 "MAKE_ARRAY", "INDEX_GET", "INDEX_SET",
53 "LEN", "PUSH", "APOP", "SET", "INSERT", "REMOVE", "SLICE",
54 "TO_NUMBER", "TO_STRING", "CAST", "TYPEOF",
55 "SPLIT", "JOIN", "SUBSTR", "FIND",
56 "REGEX_MATCH", "REGEX_SEARCH", "REGEX_REPLACE",
57 "CONTAINS", "INDEX_OF", "CLEAR",
58 "ENUMERATE", "ZIP",
59 "MIN", "MAX", "CLAMP", "ABS", "POW", "RANDOM_SEED", "RANDOM_INT",
60 "MAKE_MAP", "KEYS", "VALUES", "HAS_KEY",
61 "READ_FILE", "WRITE_FILE", "ENV", "INPUT_LINE", "PROC_RUN", "PROC_SYSTEM",
62 "TIME_NOW_MS", "CLOCK_MONO_MS", "DATE_FORMAT",
63 "THREAD_SPAWN", "THREAD_JOIN", "SLEEP_MS",
64 "RANDOM_NUMBER",
65 "BAND", "BOR", "BXOR", "BNOT", "SHL", "SHR", "ROTL", "ROTR",
66 "JSON_PARSE", "JSON_STRINGIFY", "JSON_FROM_FILE", "JSON_TO_FILE",
67 "CURL_GET", "CURL_POST", "CURL_DOWNLOAD",
68 "SQLITE_OPEN", "SQLITE_CLOSE", "SQLITE_EXEC", "SQLITE_QUERY",
69 "LIBSQL_OPEN", "LIBSQL_CLOSE", "LIBSQL_EXEC", "LIBSQL_QUERY",
70 "PCSC_ESTABLISH", "PCSC_RELEASE", "PCSC_LIST_READERS", "PCSC_CONNECT", "PCSC_DISCONNECT", "PCSC_TRANSMIT",
71 "PCRE2_TEST", "PCRE2_MATCH", "PCRE2_FINDALL",
72 "INI_LOAD", "INI_FREE", "INI_GET_STRING", "INI_GET_INT", "INI_GET_DOUBLE", "INI_GET_BOOL", "INI_SET", "INI_UNSET", "INI_SAVE",
73 "XML_PARSE", "XML_ROOT", "XML_NAME", "XML_TEXT",
74 "SOCK_TCP_LISTEN", "SOCK_TCP_ACCEPT", "SOCK_TCP_CONNECT", "SOCK_SEND", "SOCK_RECV", "SOCK_CLOSE", "SOCK_UNIX_LISTEN", "SOCK_UNIX_CONNECT",
75 "FD_SET_NONBLOCK", "FD_POLL_READ", "FD_POLL_WRITE",
76 "EXIT",
77 "OS_LIST_DIR",
78 "SERIAL_OPEN", "SERIAL_CONFIG", "SERIAL_SEND", "SERIAL_RECV", "SERIAL_CLOSE",
79 "TRY_PUSH", "TRY_POP", "THROW",
80 "FMIN", "FMAX",
81 /* Rust FFI demo */
82 "RUST_HELLO", "RUST_HELLO_ARGS", "RUST_HELLO_ARGS_RETURN", "RUST_GET_SP", "RUST_SET_EXIT",
83 /* C++ demo */
84 "CPP_ADD"};
85
86/**
87 * @brief Call frame representing one active function invocation.
88 *
89 * Each frame keeps a pointer to its function bytecode, the current
90 * instruction pointer within that bytecode, a fixed-size array of local
91 * variables, and a small try/catch stack for exception handling.
92 */
93typedef struct {
95 int ip;
97 /* exception handling (per-frame) */
98 int try_stack[16];
99 int try_sp; /* -1 when empty */
100} Frame;
101
102/**
103 * @brief The Fun virtual machine state.
104 *
105 * Holds the operand stack, call frames, globals, standard output capture,
106 * runtime counters, and debugger state. Functions in this header operate on
107 * this structure; callers must ensure proper initialization with vm_init()
108 * before use and call vm_free()/vm_reset() as appropriate.
109 */
110struct VM {
112 int sp;
113
115 int fp; // frame pointer, -1 when no frame
116
118
119 Value output[OUTPUT_SIZE]; // store printed values
121 int output_is_partial[OUTPUT_SIZE]; // 1 when the corresponding output entry should not end with newline (echo)
122
123 long long instr_count; // executed instructions in the last vm_run
124
125 int current_line; // last executed source line (debug)
126
127 int exit_code; // process exit code set by OP_EXIT
128
129 int trace_enabled; // when non-zero, print executed ops and stack
130 int repl_on_error; // when non-zero, enter REPL on runtime error (preserve stack)
131 int (*on_error_repl)(struct VM *vm); // optional hook to run REPL on error
132
133 /* --- Debugger state --- */
134 int debug_step_mode; // 0 none, 1 step, 2 next, 3 finish
135 int debug_step_target_fp; // target frame pointer for next/finish
136 long long debug_step_start_ic; // instruction count snapshot when step/next requested
137 int debug_stop_requested; // force a pause at loop top
138
139 struct {
140 char *file; // strdup'ed file path
141 int line; // 1-based line
142 int active; // 1 if active
144 int break_count; // number of active breakpoints
145};
146
147/** @brief Opaque VM alias for external users. */
148typedef struct VM VM;
149
150/**
151 * @brief Initialize a VM instance to zero/initial state.
152 * @param vm Non-NULL pointer to VM storage to initialize.
153 */
154void vm_init(VM *vm);
155
156/**
157 * @brief Clear the buffered output captured by the VM.
158 * @param vm VM instance.
159 */
160void vm_clear_output(VM *vm);
161/**
162 * @brief Print buffered output entries to stdout (debug aid).
163 * @param vm VM instance.
164 */
165void vm_print_output(VM *vm);
166/**
167 * @brief Free all resources owned by the VM (globals, frames, output buffers).
168 * The VM object itself is not freed when allocated on the stack.
169 * @param vm VM instance to dispose.
170 */
171void vm_free(VM *vm);
172
173/**
174 * @brief Reset VM to initial state, freeing globals/locals/output.
175 * The VM object remains valid for reuse after this call.
176 * @param vm VM instance to reset.
177 */
178void vm_reset(VM *vm);
179
180/**
181 * @brief Print non-nil globals (index and value) to stdout.
182 * @param vm VM instance.
183 */
184void vm_dump_globals(VM *vm);
185/**
186 * @brief Execute the provided entry bytecode in the VM.
187 * Pushes an initial frame and runs until HALT or an unrecoverable error.
188 * @param vm VM instance.
189 * @param entry Entry bytecode to execute; must outlive the call.
190 */
191void vm_run(VM *vm, Bytecode *entry);
192
193/**
194 * @brief Raise a runtime error honoring active try/catch/finally handlers.
195 * If a try handler is active in the current frame, control jumps to it with
196 * an error string pushed on the stack. Otherwise, prints the error (with
197 * location) and terminates execution.
198 * @param vm VM instance.
199 * @param msg Null-terminated error message.
200 */
201void vm_raise_error(VM *vm, const char *msg);
202
203/* --- Debugger API --- */
204/** Reset debugger state (clear step mode and breakpoints). */
205void vm_debug_reset(VM *vm);
206/** Add a breakpoint at file:line; returns non-negative id on success or -1. */
207int vm_debug_add_breakpoint(VM *vm, const char *file, int line);
208/** Delete a breakpoint by id; returns 1 on success, 0 on failure. */
209int vm_debug_delete_breakpoint(VM *vm, int id);
210/** Remove all breakpoints. */
212/** Print the current list of breakpoints to stdout. */
214/** Request single-step execution mode. */
215void vm_debug_request_step(VM *vm);
216/** Step over (next) within the current frame. */
217void vm_debug_request_next(VM *vm);
218/** Run until the current frame returns (finish). */
220/** Continue execution until next breakpoint/stop. */
222
223/**
224 * @brief Check whether an integer value corresponds to a defined opcode.
225 * @param op Numeric opcode to validate.
226 * @return 1 if valid, 0 otherwise.
227 */
228static inline int opcode_is_valid(int op) {
229 return op >= OP_NOP && op <= OP_CPP_ADD; // all current opcodes
230}
231
232/* --- Minimal C ABI helpers for FFI (Rust opcode experiments) --- */
233/** Pop an int64 from the VM stack; accepts int/float; returns truncated value. */
234int64_t vm_pop_i64(VM *vm);
235/** Push an int64 onto the VM stack. */
236void vm_push_i64(VM *vm, int64_t v);
237
238/** Example Rust-implemented opcode (adds top two ints on stack). */
240
241/** Return a demo null-terminated C string owned by Rust. */
242const char *fun_rust_get_string(void);
243/** Print a C string via Rust; returns 0 on success. */
244int fun_rust_print_string(const char *msg);
245/** Return a newly allocated duplicate of the input C string (caller frees). */
246char *fun_rust_echo_string(const char *input);
247/** Free a C string previously returned by fun_rust_echo_string(). */
248void fun_rust_string_free(char *ptr);
249
250/** C++ demo opcode entry point (C ABI). */
251int fun_op_cpp_add(struct VM *vm);
252
253/* --- Extended C ABI for Rust to access VM internals (unsafe) --- */
254/** Size of struct VM in bytes. */
255size_t vm_sizeof(void);
256/** Size of struct Value in bytes. */
257size_t vm_value_sizeof(void);
258
259/**
260 * @brief Get a mutable byte pointer to the VM object.
261 * Extremely unsafe; for low-level FFI use only.
262 */
263void *vm_as_mut_ptr(VM *vm);
264
265/** Offsets of commonly accessed VM fields (for Rust FFI). */
266size_t vm_offset_of_exit_code(void);
267size_t vm_offset_of_sp(void);
268size_t vm_offset_of_stack(void);
269size_t vm_offset_of_globals(void);
270
271/* Demo Rust ops using the extended ABI */
274
275#endif
Definitions for the Fun VM bytecode: opcodes, instruction format, and bytecode container API.
@ OP_CPP_ADD
Definition bytecode.h:285
@ OP_NOP
Definition bytecode.h:36
Value v
Definition cast.c:22
char * msg
Definition hello_args.c:35
Call frame representing one active function invocation.
Definition vm.h:93
int try_sp
Definition vm.h:99
Bytecode * fn
Definition vm.h:94
Value locals[MAX_FRAME_LOCALS]
Definition vm.h:96
int try_stack[16]
Definition vm.h:98
int ip
Definition vm.h:95
The Fun virtual machine state.
Definition vm.h:110
int sp
Definition vm.h:112
long long instr_count
Definition vm.h:123
int debug_step_target_fp
Definition vm.h:135
Value output[OUTPUT_SIZE]
Definition vm.h:119
int line
Definition vm.h:141
int current_line
Definition vm.h:125
int repl_on_error
Definition vm.h:130
int fp
Definition vm.h:115
int output_is_partial[OUTPUT_SIZE]
Definition vm.h:121
int(* on_error_repl)(struct VM *vm)
Definition vm.h:131
int debug_stop_requested
Definition vm.h:137
int break_count
Definition vm.h:144
int active
Definition vm.h:142
int trace_enabled
Definition vm.h:129
int exit_code
Definition vm.h:127
int output_count
Definition vm.h:120
struct VM::@204221333366357065317066305241116055104274166224 breakpoints[64]
char * file
Definition vm.h:140
Value globals[MAX_GLOBALS]
Definition vm.h:117
int debug_step_mode
Definition vm.h:134
long long debug_step_start_ic
Definition vm.h:136
Frame frames[MAX_FRAMES]
Definition vm.h:114
Value stack[STACK_SIZE]
Definition vm.h:111
Tagged union representing a Fun value.
Definition value.h:68
size_t vm_offset_of_exit_code(void)
Obtain offsetof(VM, exit_code) for FFI struct field access.
Definition vm.c:659
void fun_rust_string_free(char *ptr)
#define MAX_GLOBALS
Definition vm.h:34
size_t vm_offset_of_globals(void)
Obtain offsetof(VM, globals) for FFI struct field access.
Definition vm.c:686
size_t vm_value_sizeof(void)
Return sizeof(Value) for external FFI consumers.
Definition vm.c:640
void vm_raise_error(VM *vm, const char *msg)
Raise a runtime error honoring active try/catch/finally handlers. If a try handler is active in the c...
Definition vm.c:248
char * fun_rust_echo_string(const char *input)
void vm_debug_request_finish(VM *vm)
Request finish (run until the current frame returns).
Definition vm.c:540
void vm_debug_clear_breakpoints(VM *vm)
Remove all breakpoints from the VM.
Definition vm.c:492
void vm_debug_request_continue(VM *vm)
Resume normal execution (clear stepping state and stop flag).
Definition vm.c:551
void vm_debug_request_step(VM *vm)
Request single-step execution (stop after next instruction).
Definition vm.c:517
int vm_debug_delete_breakpoint(VM *vm, int id)
Delete a breakpoint by id.
Definition vm.c:472
void vm_push_i64(VM *vm, int64_t v)
Push a 64-bit integer as a VM int Value (C ABI helper).
Definition vm.c:621
int fun_op_cpp_add(struct VM *vm)
Add two 64-bit integers from the VM stack and push the sum.
Definition add.cpp:47
#define MAX_FRAMES
Definition vm.h:26
#define STACK_SIZE
Definition vm.h:42
void vm_init(VM *vm)
Initialize a VM instance to zero/initial state.
Definition vm.c:714
const char * fun_rust_get_string(void)
int fun_rust_print_string(const char *msg)
void vm_debug_reset(VM *vm)
Reset debugger state: breakpoints and stepping controls.
Definition vm.c:429
#define OUTPUT_SIZE
Definition vm.h:38
int fun_op_rget_sp(VM *vm)
void vm_run(VM *vm, Bytecode *entry)
Execute the provided entry bytecode in the VM. Pushes an initial frame and runs until HALT or an unre...
void vm_debug_request_next(VM *vm)
Request step-over (stop after next instruction in current frame).
Definition vm.c:528
static const char * opcode_names[]
Definition vm.h:45
size_t vm_offset_of_stack(void)
Obtain offsetof(VM, stack) for FFI struct field access.
Definition vm.c:677
size_t vm_offset_of_sp(void)
Obtain offsetof(VM, sp) for FFI struct field access.
Definition vm.c:668
int fun_op_radd(VM *vm)
void vm_free(VM *vm)
Free all resources owned by the VM (globals, frames, output buffers). The VM object itself is not fre...
Definition vm.c:367
size_t vm_sizeof(void)
Return sizeof(VM) for external FFI consumers.
Definition vm.c:631
int vm_debug_add_breakpoint(VM *vm, const char *file, int line)
Add a source breakpoint.
Definition vm.c:453
void vm_clear_output(VM *vm)
Clear the buffered output captured by the VM.
Definition vm.c:349
void vm_reset(VM *vm)
Reset VM to initial state, freeing globals/locals/output. The VM object remains valid for reuse after...
Definition vm.c:382
void * vm_as_mut_ptr(VM *vm)
Get a mutable byte pointer to the VM object. Extremely unsafe; for low-level FFI use only.
Definition vm.c:650
void vm_debug_list_breakpoints(VM *vm)
Print active breakpoints to stdout.
Definition vm.c:501
int fun_op_rset_exit(VM *vm)
void vm_print_output(VM *vm)
Print buffered output entries to stdout (debug aid).
Definition vm.c:797
int64_t vm_pop_i64(VM *vm)
Pop a numeric Value and convert it to a 64-bit integer (C ABI helper).
Definition vm.c:598
static int opcode_is_valid(int op)
Check whether an integer value corresponds to a defined opcode.
Definition vm.h:228
void vm_dump_globals(VM *vm)
Print non-nil globals (index and value) to stdout.
Definition vm.c:408
#define MAX_FRAME_LOCALS
Definition vm.h:30