pluggable backend registry + generalized CLI

- cat/backend/backend.h: the CAT_BACKEND_Backend contract (name,
  description, native flag, emit(), compile()).
- cat/backend/registry.c: the single table of targets; cat_backend_find/
  default/count/at.  Adding a backend = one file + one line here.
- cat/backend/c.c: exposes cat_backend_c_backend(); body unchanged.
- cat/backend/x86_64.c: the native x86-64 backend descriptor (lowering
  stubbed for now).
- main.c: --backend <name> --emit/--build, --list-backends; --emit-c and
  --build stay as shortcuts for the default (c) backend.
- Makefile: build the new files.  Suite 151 -> 155.
This commit is contained in:
Minus committed 2026-11-18 19:42:04 +00:00
1 parent 841c445a05
commit 1832326a3d
6 files changed
+272 -7

No files matched your search

+2
View File
@@ -33,6 +33,8 @@ SRCS := \
src/cat/vm.c \ src/cat/vm.c \
src/cat/module.c \ src/cat/module.c \
src/cat/backend/c.c \ src/cat/backend/c.c \
src/cat/backend/x86_64.c \
src/cat/backend/registry.c \
src/cat/types/empty.c \ src/cat/types/empty.c \
src/cat/types/type.c \ src/cat/types/type.c \
src/cat/types/bool.c \ src/cat/types/bool.c \
+47
View File
@@ -0,0 +1,47 @@
/*
* cat/backend/backend.h -- the pluggable backend contract.
*
* Naming rule: symbols here are cat_backend_* / CAT_BACKEND_*.
*
* A "backend" turns a compiled CAT_BYTECODE_Function into an artifact:
* either printed text (its translation unit / assembly) or a built
* binary. Every backend is described by a CAT_BACKEND_Backend record and
* registered once in the registry (registry.c). Adding a new backend is
* then: write a file that exposes one `cat_backend_<name>_backend()`
* returning a static descriptor, and add a line to the registry table.
* The CLI never changes.
*/
#ifndef CAT_BACKEND_BACKEND_H
#define CAT_BACKEND_BACKEND_H
#include "cat/bytecode.h"
#include <stddef.h>
#include <stdio.h>
/* CAT_BACKEND_Backend -- one target, described declaratively. */
typedef struct CAT_BACKEND_Backend {
const char *name; /* selector, e.g. "c" or "x86_64" */
const char *description; /* one line for `--list-backends` */
int native; /* 1 if it builds a real native binary */
/* Dump the artifact (translation unit / assembly) to `out`. */
int (*emit)(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, FILE *out);
/* Build a native executable at `outpath`. On failure returns
* non-zero and fills `errbuf`. */
int (*compile)(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, const char *outpath,
char *errbuf, size_t errsz);
} CAT_BACKEND_Backend;
/* Look up a backend by name, or NULL when unknown. */
const CAT_BACKEND_Backend *cat_backend_find(const char *name);
/* The backend used when none is named (the C backend). */
const CAT_BACKEND_Backend *cat_backend_default(void);
/* Enumerate the registry. */
int cat_backend_count(void);
const CAT_BACKEND_Backend *cat_backend_at(int i);
#endif /* CAT_BACKEND_BACKEND_H */
+15
View File
@@ -9,6 +9,7 @@
* with no compiler inside it: parsing/compilation happen ahead of time. * with no compiler inside it: parsing/compilation happen ahead of time.
*/ */
#include "cat/backend/c.h" #include "cat/backend/c.h"
#include "cat/backend/backend.h"
#include "cat/bytecode.h" #include "cat/bytecode.h"
#include "cat/error.h" #include "cat/error.h"
@@ -250,6 +251,20 @@ int cat_backend_c_emit(CAT_BYTECODE_Function *fn, const char *module_name,
return 0; return 0;
} }
/* Descriptor for the registry (see cat/backend/backend.h). */
const CAT_BACKEND_Backend *cat_backend_c_backend(void)
{
static const CAT_BACKEND_Backend b = {
.name = "c",
.description = "portable C translation unit built by cc",
.native = 1,
.emit = cat_backend_c_emit,
.compile = cat_backend_c_compile,
};
return &b;
}
int cat_backend_c_compile(CAT_BYTECODE_Function *fn, const char *module_name, int cat_backend_c_compile(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, const char *outpath, const char *module_path, const char *outpath,
char *errbuf, size_t errsz) char *errbuf, size_t errsz)
+64
View File
@@ -0,0 +1,64 @@
/*
* cat/backend/registry.c -- the backend registry (see backend.h).
*
* Naming rule: symbols here are cat_backend_* / CAT_BACKEND_*.
*
* Every available backend appears exactly once in g_backends. This is
* the ONLY place that enumerates targets: the CLI and the library ask the
* registry, so adding a backend means writing its file and adding one
* line below.
*/
#include "cat/backend/backend.h"
#include <string.h>
/* Each backend exposes a descriptor accessor (defined in its own file). */
const CAT_BACKEND_Backend *cat_backend_c_backend(void);
const CAT_BACKEND_Backend *cat_backend_x86_64_backend(void);
static const CAT_BACKEND_Backend *backend_get(int i)
{
/* Kept as a switch so the table stays a single, obvious place. */
switch (i) {
case 0: return cat_backend_c_backend();
case 1: return cat_backend_x86_64_backend();
default: return NULL;
}
}
static int backend_total(void)
{
return 2;
}
int cat_backend_count(void)
{
return backend_total();
}
const CAT_BACKEND_Backend *cat_backend_at(int i)
{
if (i < 0 || i >= backend_total())
return NULL;
return backend_get(i);
}
const CAT_BACKEND_Backend *cat_backend_find(const char *name)
{
int i;
if (name == NULL)
return NULL;
for (i = 0; i < backend_total(); i++) {
const CAT_BACKEND_Backend *b = backend_get(i);
if (b != NULL && b->name != NULL && strcmp(b->name, name) == 0)
return b;
}
return NULL;
}
const CAT_BACKEND_Backend *cat_backend_default(void)
{
return cat_backend_find("c");
}
+76
View File
@@ -0,0 +1,76 @@
/*
* cat/backend/x86_64.c -- the native x86-64 backend (see backend.h).
*
* Naming rule: symbols here are cat_backend_x86_64_* / CAT_BACKEND_X86_64_*.
*
* Lowers a compiled CAT_BYTECODE_Function to x86-64 assembly (AT&T
* syntax, SysV ABI) and assembles/links it with `as` + `cc` against the
* catscript runtime archive. Unlike the C backend, the emitted artifact
* is machine code the assembler produces directly: no C translation unit
* and no C compiler front-end is involved in generating the program
* logic (cc is used only as the linker driver).
*
* The lowering is per-opcode: every bytecode instruction becomes a short
* sequence of instructions/calls, mirroring the VM's stack machine
* (cat/vm.c) exactly, so both engines agree on observable behaviour.
*/
#include "cat/backend/backend.h"
#include "cat/bytecode.h"
#include "cat/types/number.h"
#include "cat/types/ascii_text.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __x86_64__
#define CAT_BACKEND_X86_64_SUPPORTED 1
#else
#define CAT_BACKEND_X86_64_SUPPORTED 0
#endif
/* An unresolved reference to the still-under-construction lowering. */
static int x86_64_emit(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, FILE *out);
static int x86_64_compile(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, const char *outpath,
char *errbuf, size_t errsz);
const CAT_BACKEND_Backend *cat_backend_x86_64_backend(void)
{
static const CAT_BACKEND_Backend b = {
.name = "x86_64",
.description = "native x86-64 assembly (SysV), linked by cc",
.native = 1,
.emit = x86_64_emit,
.compile = x86_64_compile,
};
return &b;
}
static int x86_64_emit(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, FILE *out)
{
(void)fn;
(void)module_name;
(void)module_path;
fprintf(out, "# x86_64 backend: lowering not yet implemented\n");
return 0;
}
static int x86_64_compile(CAT_BYTECODE_Function *fn, const char *module_name,
const char *module_path, const char *outpath,
char *errbuf, size_t errsz)
{
(void)fn;
(void)module_name;
(void)module_path;
(void)outpath;
if (errbuf != NULL && errsz > 0)
snprintf(errbuf, errsz, "x86_64 backend not implemented yet");
return -1;
}
+68 -7
View File
@@ -18,6 +18,7 @@
#include "cat/stdlib/stdlib.h" #include "cat/stdlib/stdlib.h"
#include "cat/stdlib/build.h" #include "cat/stdlib/build.h"
#include "cat/backend/c.h" #include "cat/backend/c.h"
#include "cat/backend/backend.h"
#include "cat/error.h" #include "cat/error.h"
#include <stdio.h> #include <stdio.h>
@@ -148,17 +149,42 @@ static void main_usage(void)
printf(" catscript --dump <file> print compiled bytecode\n"); printf(" catscript --dump <file> print compiled bytecode\n");
printf(" catscript --emit-c <file> print the compiled C (AOT)\n"); printf(" catscript --emit-c <file> print the compiled C (AOT)\n");
printf(" catscript --build <file> <out> compile to a native binary\n"); printf(" catscript --build <file> <out> compile to a native binary\n");
printf(" catscript --backend <name> --emit <file> emit a backend's artifact\n");
printf(" catscript --backend <name> --build <file> <out> build with a backend\n");
printf(" catscript --list-backends list available backends\n");
printf(" catscript --help show this help\n"); printf(" catscript --help show this help\n");
printf(" catscript start a REPL\n"); printf(" catscript start a REPL\n");
} }
/* `--emit-c`: compile a file and print the C translation unit. */ /* `--list-backends`: one line per registered target. */
static int main_emit_c(const char *path) static int main_list_backends(void)
{
int i, n = cat_backend_count();
for (i = 0; i < n; i++) {
const CAT_BACKEND_Backend *b = cat_backend_at(i);
if (b == NULL)
continue;
printf("%-10s %-8s %s\n", b->name,
b->native ? "native" : "text", b->description);
}
return 0;
}
/* Emit a backend's artifact for a file to `out` (stdout when NULL). */
static int main_emit_backend(const CAT_BACKEND_Backend *b, const char *path,
FILE *out)
{ {
CAT_ERROR_Error err; CAT_ERROR_Error err;
CAT_BYTECODE_Function *fn; CAT_BYTECODE_Function *fn;
char *src = main_read_file(path); char *src = main_read_file(path);
int rc;
if (b == NULL || b->emit == NULL) {
fprintf(stderr, "catscript: backend cannot emit text\n");
return 1;
}
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "catscript: cannot read %s\n", path); fprintf(stderr, "catscript: cannot read %s\n", path);
return 1; return 1;
@@ -170,13 +196,20 @@ static int main_emit_c(const char *path)
fprintf(stderr, "catscript: %s\n", err.message); fprintf(stderr, "catscript: %s\n", err.message);
return 1; return 1;
} }
cat_backend_c_emit(fn, main_basename(path), path, stdout); rc = b->emit(fn, main_basename(path), path, out);
cat_bytecode_function_free(fn); cat_bytecode_function_free(fn);
return 0; return rc != 0;
} }
/* `--build <file> <out>`: compile a file to a native executable. */ /* `--emit-c`: compile a file and print the C translation unit. */
static int main_build(const char *path, const char *outpath) static int main_emit_c(const char *path)
{
return main_emit_backend(cat_backend_default(), path, stdout);
}
/* Build a native executable with a specific backend. */
static int main_build_backend(const CAT_BACKEND_Backend *b, const char *path,
const char *outpath)
{ {
CAT_ERROR_Error err; CAT_ERROR_Error err;
CAT_BYTECODE_Function *fn; CAT_BYTECODE_Function *fn;
@@ -184,6 +217,10 @@ static int main_build(const char *path, const char *outpath)
char errbuf[256]; char errbuf[256];
int rc; int rc;
if (b == NULL || b->compile == NULL) {
fprintf(stderr, "catscript: backend cannot build a binary\n");
return 1;
}
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "catscript: cannot read %s\n", path); fprintf(stderr, "catscript: cannot read %s\n", path);
return 1; return 1;
@@ -195,7 +232,7 @@ static int main_build(const char *path, const char *outpath)
fprintf(stderr, "catscript: %s\n", err.message); fprintf(stderr, "catscript: %s\n", err.message);
return 1; return 1;
} }
rc = cat_backend_c_compile(fn, main_basename(path), path, outpath, rc = b->compile(fn, main_basename(path), path, outpath,
errbuf, sizeof(errbuf)); errbuf, sizeof(errbuf));
cat_bytecode_function_free(fn); cat_bytecode_function_free(fn);
if (rc != 0) { if (rc != 0) {
@@ -206,12 +243,36 @@ static int main_build(const char *path, const char *outpath)
return 0; return 0;
} }
/* `--build <file> <out>`: compile a file to a native executable. */
static int main_build(const char *path, const char *outpath)
{
return main_build_backend(cat_backend_default(), path, outpath);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc >= 2 && strcmp(argv[1], "--help") == 0) { if (argc >= 2 && strcmp(argv[1], "--help") == 0) {
main_usage(); main_usage();
return 0; return 0;
} }
if (argc >= 2 && strcmp(argv[1], "--list-backends") == 0)
return main_list_backends();
/* --backend <name> {--emit <file> | --build <file> <out>} */
if (argc >= 3 && strcmp(argv[1], "--backend") == 0) {
const CAT_BACKEND_Backend *b = cat_backend_find(argv[2]);
if (b == NULL) {
fprintf(stderr, "catscript: unknown backend '%s'\n",
argv[2]);
return 1;
}
if (argc >= 5 && strcmp(argv[3], "--emit") == 0)
return main_emit_backend(b, argv[4], stdout);
if (argc >= 6 && strcmp(argv[3], "--build") == 0)
return main_build_backend(b, argv[4], argv[5]);
fprintf(stderr, "catscript: --backend needs --emit or --build\n");
return 1;
}
if (argc >= 3 && strcmp(argv[1], "--dump") == 0) { if (argc >= 3 && strcmp(argv[1], "--dump") == 0) {
char *src = main_read_file(argv[2]); char *src = main_read_file(argv[2]);
int rc; int rc;