Files
zig/stage0/zig0.c
Motiejus Jakštys 5700b742d2 stage0: add --verbose-intern-pool flag and IP dumper
Add verbose_intern_pool.c/h for dumping the C sema's InternPool
entries. Integrate as --verbose-intern-pool flag in zig0, mirroring
the Zig compiler's flag.

Fix InternPool.zig dump crash on locals with zero-capacity items
(skip empty locals in dumpStatsFallible and dumpAllFallible).

Update CLAUDE.md with IP debugging tools documentation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-25 20:37:34 +00:00

105 lines
2.7 KiB
C

#include "ast.h"
#include "astgen.h"
#include "intern_pool.h"
#include "sema.h"
#include "verbose_air.h"
#include "verbose_intern_pool.h"
#include "zir.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// API:
// - code = 0: program successfully terminated.
// - code = 1: panicked, panic message in msg. Caller should free msg.
// - code = 2: interpreter error, error in msg. Caller should free msg.
static int zig0Run(const char* program, bool verbose_air,
bool verbose_intern_pool, char** msg) {
uint32_t len = (uint32_t)strlen(program);
Ast ast = astParse(program, len);
if (ast.has_error) {
*msg = ast.err_msg;
ast.err_msg = NULL;
astDeinit(&ast);
return 2;
}
fprintf(stderr, "tokens: %u, nodes: %u, first token: %s\n", ast.tokens.len,
ast.nodes.len, tokenizerGetTagString(ast.tokens.tags[0]));
Zir zir = astGen(&ast);
astDeinit(&ast);
if (zir.has_compile_errors) {
const char err[] = "astgen failed";
*msg = malloc(sizeof(err));
memcpy(*msg, err, sizeof(err));
zirDeinit(&zir);
return 2;
}
fprintf(stderr, "zir: %u instructions, %u extra, %u string bytes\n",
zir.inst_len, zir.extra_len, zir.string_bytes_len);
InternPool ip = ipInit();
Sema sema;
semaInit(&sema, &ip, zir);
SemaFuncAirList func_airs = semaAnalyze(&sema);
if (verbose_intern_pool)
verboseIpPrint(stderr, &ip);
if (verbose_air)
verboseAirPrint(stderr, &func_airs, &ip);
semaDeinit(&sema);
semaFuncAirListDeinit(&func_airs);
ipDeinit(&ip);
zirDeinit(&zir);
return 0;
}
// API: run and:
// code = 3: abnormal error, expect something in stderr.
int zig0RunFile(const char* fname, bool verbose_air, bool verbose_intern_pool,
char** msg) {
FILE* f = fopen(fname, "r");
if (f == NULL) {
perror("fopen");
return 3;
}
fseek(f, 0, SEEK_END);
long fsizel = ftell(f);
if (fsizel == -1) {
perror("ftell");
fclose(f);
return 3;
}
unsigned long fsize = (unsigned long)fsizel;
fseek(f, 0, SEEK_SET);
char* program = malloc(fsize + 1);
if (program == NULL) {
perror("malloc");
fclose(f);
return 3;
}
size_t bytes_read = fread(program, 1, fsize, f);
if (bytes_read < fsize) {
if (ferror(f)) {
perror("fread");
} else {
fprintf(stderr, "Unexpected end of file\n");
}
free(program);
fclose(f);
return 3;
}
fclose(f);
program[fsize] = 0;
int code = zig0Run(program, verbose_air, verbose_intern_pool, msg);
free(program);
return code;
}