#include "ast.h" #include "collection.h" #include "ir.h" #include "isel.h" #include "parse.h" #include "preproc.h" #include "sema.h" #include #include #include #include #include int main(int argc, char* argv[]) { if (argc < 2) { fprintf(stderr, "error: no filename\n"); return EXIT_FAILURE; } char* filename = argv[1]; char* out_filename = nullptr; for (int i = 2; i < argc; ++i) { if (strcmp(argv[i], "-o") == 0) { i += 1; assert(i < argc); out_filename = argv[i]; } } char* text = read_textfile(filename); if (!text) { return EXIT_FAILURE; } { Arena tok_arena; arena_init(&tok_arena, 4096); Preproc* preproc = preproc_new(strdup(filename), text, &tok_arena); TokStream tokstream; tokstream_init(&tokstream, preproc, &tok_arena); Sema* sema = sema_new(); Arena ast_arena; arena_init(&ast_arena, 4096); printf("--- ast ---\n"); Parser parser; parser_init(&parser, &tokstream, sema, &ast_arena); AstLowerer astlow; astlowerer_init(&astlow, sema); AstStmt* stmt; while (!parser_done(&parser)) { stmt = parser_parse_stmt(&parser); // ast_stmt_print(stdout, stmt, 0); // printf("\n"); astlowerer_lower_top_stmt(&astlow, stmt); } IrCx* ircx = &astlow.cx; printf("--- ir ---\n"); for (size_t i = 0; i < ircx->fns_size; ++i) { // irfn_dump(ircx->fns[i], stdout); // printf("\n"); } X86Cx x86cx; x86cx_init(&x86cx); X86IrLowerer irlow; x86irlowerer_init(&irlow, &x86cx); printf("--- instruction selection ---\n"); x86irlowerer_lower_ir(&irlow, ircx); // x86cx_dump(&x86cx, stdout); // printf("\n"); x86irlowerer_deinit(&irlow); printf("--- register allocation ---\n"); X86RegAllocator reg_allocator; x86regallocator_init(®_allocator, &x86cx); x86regallocator_allocate_registers(®_allocator); // x86cx_dump(&x86cx, stdout); // printf("\n"); if (out_filename) { FILE* outfile = fopen(out_filename, "w"); assert(outfile); x86cx_dump(&x86cx, outfile); fprintf(outfile, "\n"); fclose(outfile); } else { x86cx_dump(&x86cx, stdout); printf("\n"); } x86regallocator_deinit(®_allocator); x86cx_deinit(&x86cx); parser_deinit(&parser); preproc_free(preproc); arena_deinit(&tok_arena); arena_deinit(&ast_arena); astlowerer_deinit(&astlow); sema_free(sema); } }