Files
gw-basic-2026/include/analysis.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

63 lines
1.6 KiB
C

#ifndef ANALYSIS_H
#define ANALYSIS_H
#include "types.h"
#include <stdbool.h>
#include <stdint.h>
#define MAX_LINES 4096
#define MAX_VARS 256
#define MAX_GOTOS 256
#define MAX_DATA 1024
#define MAX_GOSUB_RET 256
typedef struct {
uint16_t line_num;
bool is_target; /* referenced by GOTO/GOSUB/etc. */
bool has_data; /* contains DATA statement */
int data_start; /* index into data pool */
} line_info_t;
typedef struct {
char name[2];
gw_valtype_t type;
uint16_t first_assign_line; /* 0 = never assigned */
uint16_t first_use_line; /* 0 = never used */
} var_info_t;
typedef struct {
line_info_t lines[MAX_LINES];
int line_count;
var_info_t vars[MAX_VARS];
int var_count;
uint16_t goto_targets[MAX_GOTOS];
int goto_count;
char *data_pool[MAX_DATA]; /* collected DATA literals */
int data_count;
int data_line_map[MAX_LINES][2]; /* [line_num, data_start_index] */
int data_line_count;
gw_valtype_t def_type[26]; /* from DEFINT/DEFSNG/DEFDBL/DEFSTR */
} analysis_t;
/* Run analysis pass over the loaded program */
void analysis_run(analysis_t *a);
/* Find a variable in the census, return index or -1 */
int analysis_find_var(analysis_t *a, const char name[2], gw_valtype_t type);
/* Add a variable to the census if not already present */
int analysis_add_var(analysis_t *a, const char name[2], gw_valtype_t type);
/* Check if a line number is a jump target */
bool analysis_is_target(analysis_t *a, uint16_t line_num);
/* Emit static analysis warnings to stderr */
void analysis_warnings(analysis_t *a);
#endif