public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
bc2eabf867bbbb779c46c411bb561e1cdde36d1f blob 6738 bytes (raw)

  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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
 
/*
 * Copyright (C) 2024 olang maintainers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "arena.h"
#include "checker.h"
#include "cli.h"
#include "codegen_linux_aarch64.h"
#include "codegen_linux_x86_64.h"
#include "lexer.h"
#include "parser.h"
#include "pretty_print_ast.h"
#include "string_view.h"

// TODO: find a better solution to define the arena capacity
#define ARENA_CAPACITY (1024 * 1024)

void
handle_dump_tokens(cli_opts_t *opts);

void
handle_dump_ast(cli_opts_t *opts);

void
handle_codegen_linux(cli_opts_t *opts);

static void
print_token(token_t *token);

source_code_t
read_entire_file(char *filepath, arena_t *arena);

int
main(int argc, char **argv) {
    cli_opts_t opts = cli_parse_args(argc, argv);

    if (opts.options & CLI_OPT_DUMP_TOKENS) {
        handle_dump_tokens(&opts);
        return EXIT_SUCCESS;
    }

    if (opts.options & CLI_OPT_DUMP_AST) {
        handle_dump_ast(&opts);
        return EXIT_SUCCESS;
    }

    if (opts.options & CLI_OPT_OUTPUT) {
        handle_codegen_linux(&opts);
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}

void
handle_dump_tokens(cli_opts_t *opts) {
    if (opts->filepath == NULL) {
        cli_print_usage(stderr, opts->compiler_path);
        exit(EXIT_FAILURE);
    }

    arena_t arena = arena_new(ARENA_CAPACITY);
    source_code_t src = read_entire_file(opts->filepath, &arena);

    lexer_t lexer = { 0 };
    lexer_init(&lexer, src);

    token_t token = { 0 };
    lexer_next_token(&lexer, &token);
    while (token.kind != TOKEN_EOF) {
        print_token(&token);
        lexer_next_token(&lexer, &token);
    }
    print_token(&token);

    arena_free(&arena);
}

void
handle_dump_ast(cli_opts_t *opts) {
    if (opts->filepath == NULL) {
        cli_print_usage(stderr, opts->compiler_path);
        exit(EXIT_FAILURE);
    }

    arena_t arena = arena_new(ARENA_CAPACITY);
    lexer_t lexer = { 0 };
    parser_t parser = { 0 };

    source_code_t src = read_entire_file(opts->filepath, &arena);

    lexer_init(&lexer, src);
    parser_init(&parser, &lexer, &arena);

    ast_node_t *ast = parser_parse_translation_unit(&parser);

    pretty_print_ast(ast);
}

void
handle_codegen_linux(cli_opts_t *opts) {
    if (opts->filepath == NULL) {
        cli_print_usage(stderr, opts->compiler_path);
        exit(EXIT_FAILURE);
    }

    arena_t arena = arena_new(ARENA_CAPACITY);
    lexer_t lexer = { 0 };
    parser_t parser = { 0 };

    source_code_t src = read_entire_file(opts->filepath, &arena);
    lexer_init(&lexer, src);
    parser_init(&parser, &lexer, &arena);

    ast_node_t *ast = parser_parse_translation_unit(&parser);

    checker_t *checker = checker_new(&arena);
    checker_check(checker, ast);

    char asm_file[opts->output_bin.size + 3];
    sprintf(asm_file, "" SV_FMT ".s", SV_ARG(opts->output_bin));

    FILE *out = fopen(asm_file, "w");
    assert(out);

    if (!(opts->options & CLI_OPT_ARCH)) {
        codegen_x86_64_t codegen = { 0 };
        codegen_linux_x86_64_init(&codegen, &arena, out);
        codegen_linux_x86_64_emit_translation_unit(&codegen, ast);
    } else {
        if (strcmp(opts->arch, "x86_64") == 0) {
            codegen_x86_64_t codegen = { 0 };
            codegen_linux_x86_64_init(&codegen, &arena, out);
            codegen_linux_x86_64_emit_translation_unit(&codegen, ast);
        } else if (strcmp(opts->arch, "aarch64") == 0) {
            codegen_linux_aarch64_emit_translation_unit(out, ast);
        } else {
            fprintf(
                stderr, "error: architecture '%s' not supported\n", opts->arch
            );
            cli_print_usage(stderr, opts->compiler_path);
            exit(EXIT_FAILURE);
        }
    }

    fclose(out);

    if (!(opts->options & CLI_OPT_SYSROOT)) {
        opts->sysroot = "";
    }

    char command[512];
    sprintf(
        command,
        "%s/bin/as %s -o " SV_FMT ".o",
        opts->sysroot,
        asm_file,
        SV_ARG(opts->output_bin)
    );

    int exit_code = system(command);

    if (exit_code != 0) {
        exit(exit_code);
    }

    sprintf(
        command,
        "%s/bin/ld " SV_FMT ".o -o " SV_FMT "",
        opts->sysroot,
        SV_ARG(opts->output_bin),
        SV_ARG(opts->output_bin)
    );

    exit_code = system(command);

    if (exit_code != 0) {
        exit(exit_code);
    }

    if (!(opts->options & CLI_OPT_SAVE_TEMPS)) {
        char output_file[256];

        sprintf(output_file, "" SV_FMT ".s", SV_ARG(opts->output_bin));
        remove(output_file);

        sprintf(output_file, "" SV_FMT ".o", SV_ARG(opts->output_bin));
        remove(output_file);
    }

    arena_free(&arena);
}

source_code_t
read_entire_file(char *filepath, arena_t *arena) {
    FILE *stream = fopen(filepath, "rb");

    if (stream == NULL) {
        fprintf(
            stderr,
            "error: could not open file %s: %s\n",
            filepath,
            strerror(errno)
        );
        exit(EXIT_FAILURE);
    }

    string_view_t code = { 0 };

    fseek(stream, 0, SEEK_END);
    code.size = ftell(stream);
    fseek(stream, 0, SEEK_SET);

    assert(code.size * 2 < ARENA_CAPACITY);

    code.chars = (char *)arena_alloc(arena, (size_t)code.size);

    if (code.chars == NULL) {
        fprintf(
            stderr,
            "error: could not read file %s: %s\n",
            filepath,
            strerror(errno)
        );
        exit(EXIT_FAILURE);
    }

    size_t read_bytes = fread(code.chars, 1, code.size, stream);

    if (read_bytes != code.size) {
        fprintf(stderr, "error: failed to read all file bytes %s\n", filepath);
        exit(EXIT_FAILURE);
    }

    fclose(stream);

    return (source_code_t){
        .filepath = filepath,
        .code = code,
    };
}

static void
print_token(token_t *token) {
    printf(
        "%s:%lu:%lu: <%s>\n",
        token->loc.src.filepath,
        token_loc_to_lineno(token->loc),
        token_loc_to_colno(token->loc),
        token_kind_to_cstr(token->kind)
    );
}
debug log:

solving bc2eabf ...
found bc2eabf in http://lists.johnnyrichard.com/olang/20241010013318.222905-7-carlos@maniero.me/
found 0e174a1 in http://lists.johnnyrichard.com/olang/20241010013318.222905-6-carlos@maniero.me/
found bb0e451 in http://lists.johnnyrichard.com/olang/20241010013318.222905-3-carlos@maniero.me/
found d7738bd in http://lists.johnnyrichard.com/olang/20241010013318.222905-2-carlos@maniero.me/
found d1c76e3 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 d1c76e375d8c530e1da964f94c17220399a6ee66	src/main.c

applying [1/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-2-carlos@maniero.me/
diff --git a/src/main.c b/src/main.c
index d1c76e3..d7738bd 100644


applying [2/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-3-carlos@maniero.me/
diff --git a/src/main.c b/src/main.c
index d7738bd..bb0e451 100644


applying [3/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-6-carlos@maniero.me/
diff --git a/src/main.c b/src/main.c
index bb0e451..0e174a1 100644


applying [4/4] http://lists.johnnyrichard.com/olang/20241010013318.222905-7-carlos@maniero.me/
diff --git a/src/main.c b/src/main.c
index 0e174a1..bc2eabf 100644

Checking patch src/main.c...
Applied patch src/main.c cleanly.
Checking patch src/main.c...
Applied patch src/main.c cleanly.
Checking patch src/main.c...
Applied patch src/main.c cleanly.
Checking patch src/main.c...
Applied patch src/main.c cleanly.

index at:
100644 bc2eabf867bbbb779c46c411bb561e1cdde36d1f	src/main.c

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox