Files
gw-basic-2026/include/gwrt.h
Eremey Valetov 20ecdae938 Add --warn and --safe memory safety flags to the compiler
Three progressive levels for gwbasic-compile:

--warn: static analysis warnings (uninitialized variables, GOTO to
nonexistent line, unreachable code detection). Zero runtime cost.

--safe (implies --warn): runtime checked integer arithmetic via
gw_int_add/sub/mul/neg matching real GW-BASIC overflow semantics,
enhanced array bounds diagnostics with variable names and line numbers,
GOSUB stack overflow diagnostics with source line reporting.

--safe=sanitize (implies --safe): passes -fsanitize=address,undefined
to gcc for full memory error detection.

Also: fix pre-existing missing closing paren in array LET-to-integer
codegen, add strpool_pin/unpin infrastructure, add compiler optimization
flags and memory safety sections to roadmap.

72/72 interpreter tests pass. 64/64 eligible compiler tests pass in
--safe mode.
2026-04-09 13:14:26 -04:00

68 lines
2.1 KiB
C

#ifndef GWRT_H
#define GWRT_H
/*
* Runtime library for compiled GW-BASIC programs.
*
* Provides initialization, DATA/READ support, GOSUB return-label stack,
* and convenience wrappers. Compiled programs link against libgwrt.a
* which contains all the existing interpreter modules except the
* execution loop.
*/
#include "gwbasic.h"
#include "strpool.h"
#include "portio.h"
#include "sound.h"
#include "graphics.h"
#include "virmem.h"
#include <setjmp.h>
/* Initialization / shutdown */
void gwrt_init(void);
void gwrt_shutdown(void);
/* DATA / READ / RESTORE */
void gwrt_data_set(const char **pool, const int *line_map, int line_count);
void gwrt_data_restore(int index);
const char *gwrt_data_read(void); /* returns next datum or errors ERR_OD */
/* GOSUB return-label stack */
#define GWRT_GOSUB_MAX 24
void gwrt_gosub_push(int label);
void gwrt_gosub_push_safe(int label, uint16_t from_line);
int gwrt_gosub_pop(void);
/* FOR/NEXT stack (for NEXT without variable matching) */
#define GWRT_FOR_MAX 16
/* Event checking (ON TIMER, ON KEY, Ctrl+Break) + string pool GC */
void gwrt_check_line(uint16_t line_num);
/* Error handling */
extern jmp_buf gwrt_error_jmp;
extern int gwrt_error_target; /* ON ERROR GOTO label, 0 = none */
extern int gwrt_resume_label; /* label for RESUME NEXT */
/* Array element access (wraps gw_array_element) */
gw_value_t *gwrt_array_elem(const char *name, int type, int ndims, int *subs);
/* Safe variant: enhanced diagnostics on bounds error */
gw_value_t *gwrt_array_elem_safe(const char *name, int type, int ndims,
int *subs, uint16_t line_num);
/* DIM statement (wraps gw_stmt_dim via text_ptr) */
void gwrt_dim(const char *name, int type, int ndims, int *dims);
/* Print helpers (wrappers around existing print.c) */
void gwrt_print_sng(float v);
void gwrt_print_dbl(double v);
void gwrt_print_int(int16_t v);
void gwrt_print_str(gw_string_t s);
void gwrt_print_cstr(const char *s);
void gwrt_print_newline(void);
void gwrt_print_tab(void); /* comma zone */
void gwrt_print_spc(int n);
#endif