summaryrefslogtreecommitdiff
path: root/gen/docs/main.c
blob: 760253ee7534348996adbf007063d9eee28b3daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "parser.h"
#include "generator.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void print_usage(const char *prog) {
    fprintf(stderr, "Usage: %s [options] <isa_defs_dir>\n\n", prog);
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  -o <file>     Output Markdown file (default: doc/spec/isa_reference.md)\n");
    fprintf(stderr, "  --html        Also generate HTML output\n");
    fprintf(stderr, "  --help        Show this help\n\n");
    fprintf(stderr, "Generates professional ISA reference documentation from .isa definition files.\n");
}

int main(int argc, char **argv) {
    const char *defs_dir = NULL;
    const char *output = "doc/spec/isa_reference.md";
    int gen_html_flag = 0;

    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--help") == 0) {
            print_usage(argv[0]);
            return 0;
        } else if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) {
            output = argv[++i];
        } else if (strcmp(argv[i], "--html") == 0) {
            gen_html_flag = 1;
        } else if (argv[i][0] != '-') {
            defs_dir = argv[i];
        } else {
            fprintf(stderr, "Unknown option: %s\n", argv[i]);
            print_usage(argv[0]);
            return 1;
        }
    }

    if (!defs_dir) {
        fprintf(stderr, "Error: no ISA definitions directory specified.\n\n");
        print_usage(argv[0]);
        return 1;
    }

    IsaDb db;
    memset(&db, 0, sizeof(db));

    printf("ISA Documentation Generator\n");
    printf("Reading definitions from: %s\n", defs_dir);

    if (isa_parse_dir(defs_dir, &db) != 0) {
        fprintf(stderr, "Error: failed to parse ISA definitions.\n");
        return 1;
    }

    printf("\nParsed:\n");
    printf("  %d format(s)\n", db.num_formats);
    printf("  %d register(s)\n", db.num_registers);
    printf("  %d instruction(s)\n", db.num_instructions);
    printf("  %d CSR(s)\n", db.num_csrs);

    if (db.num_instructions == 0) {
        fprintf(stderr, "Warning: no instructions loaded. Output will be sparse.\n");
    }

    if (gen_markdown(&db, output) != 0) {
        fprintf(stderr, "Error: failed to generate Markdown.\n");
        return 1;
    }

    if (gen_html_flag) {
        char html_path[1024];
        snprintf(html_path, sizeof(html_path), "%s.html", output);
        const char *ext = strrchr(output, '.');
        if (ext) {
            size_t len = ext - output;
            snprintf(html_path, sizeof(html_path), "%.*s.html", (int)len, output);
        }
        gen_html(&db, html_path);
    }

    printf("\nDone. Output: %s\n", output);
    return 0;
}