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
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
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
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
148typedef struct VM VM;
149
154void vm_init(VM *vm);
155
160void vm_clear_output(VM *vm);
165void vm_print_output(VM *vm);
171void vm_free(VM *vm);
172
178void vm_reset(VM *vm);
179
184void vm_dump_globals(VM *vm);
191void vm_run(VM *vm, Bytecode *entry);
192
201void vm_raise_error(VM *vm, const char *msg);
202
203/* --- Debugger API --- */
205void vm_debug_reset(VM *vm);
207int vm_debug_add_breakpoint(VM *vm, const char *file, int line);
209int vm_debug_delete_breakpoint(VM *vm, int id);
222
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) --- */
234int64_t vm_pop_i64(VM *vm);
236void vm_push_i64(VM *vm, int64_t v);
237
239int fun_op_radd(VM *vm);
240
242const char *fun_rust_get_string(void);
244int fun_rust_print_string(const char *msg);
246char *fun_rust_echo_string(const char *input);
248void fun_rust_string_free(char *ptr);
249
251int fun_op_cpp_add(struct VM *vm);
252
253/* --- Extended C ABI for Rust to access VM internals (unsafe) --- */
255size_t vm_sizeof(void);
257size_t vm_value_sizeof(void);
258
263void *vm_as_mut_ptr(VM *vm);
264
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
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
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
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