Modifying the class hierarchy and removing globals, allocators, simplifying
This commit is contained in:
@@ -1,90 +1,87 @@
|
||||
|
||||
CORE_Static void
|
||||
lex_init(Arena *token_string_arena, Arena *map_allocator, Lexer *l) {
|
||||
l->arena = token_string_arena;
|
||||
l->tokens = array_make<Token>(token_string_arena, 4096 * 4);
|
||||
l->interns = intern_table_make(token_string_arena, map_allocator, 2048);
|
||||
|
||||
/*#import meta
|
||||
for i in meta.keywords:
|
||||
print(f'keyword_{i.lower()} = l->intern("{i}"_s);')
|
||||
print(f'l->interns.first_keyword = keyword_{meta.keywords[0].lower()}.str;')
|
||||
print(f'l->interns.last_keyword = keyword_{meta.keywords[-1].lower()}.str;')
|
||||
|
||||
for i in meta.interns:
|
||||
print(f'intern_{i.lower()} = l->intern("{i}"_s);')
|
||||
|
||||
index = 0
|
||||
for i in meta.token_simple_expr:
|
||||
if i[1] != "SPECIAL":
|
||||
print(f'op_info_table[{index}].op = l->intern("{i[1]}"_s);')
|
||||
index += 1
|
||||
|
||||
*/
|
||||
keyword_struct = l->intern("struct"_s);
|
||||
keyword_union = l->intern("union"_s);
|
||||
keyword_true = l->intern("true"_s);
|
||||
keyword_default = l->intern("default"_s);
|
||||
keyword_break = l->intern("break"_s);
|
||||
keyword_false = l->intern("false"_s);
|
||||
keyword_return = l->intern("return"_s);
|
||||
keyword_switch = l->intern("switch"_s);
|
||||
keyword_assert = l->intern("Assert"_s);
|
||||
keyword_if = l->intern("if"_s);
|
||||
keyword_elif = l->intern("elif"_s);
|
||||
keyword_pass = l->intern("pass"_s);
|
||||
keyword_else = l->intern("else"_s);
|
||||
keyword_for = l->intern("for"_s);
|
||||
keyword_enum = l->intern("enum"_s);
|
||||
l->interns.first_keyword = keyword_struct.str;
|
||||
l->interns.last_keyword = keyword_enum.str;
|
||||
intern_typeof = l->intern("TypeOf"_s);
|
||||
intern_sizeof = l->intern("SizeOf"_s);
|
||||
intern_len = l->intern("Len"_s);
|
||||
intern_alignof = l->intern("AlignOf"_s);
|
||||
intern_foreign = l->intern("foreign"_s);
|
||||
intern_strict = l->intern("strict"_s);
|
||||
intern_void = l->intern("void"_s);
|
||||
intern_flag = l->intern("flag"_s);
|
||||
intern_it = l->intern("it"_s);
|
||||
intern_load = l->intern("load"_s);
|
||||
intern_import = l->intern("import"_s);
|
||||
intern_link = l->intern("link"_s);
|
||||
op_info_table[0].op = l->intern("*"_s);
|
||||
op_info_table[1].op = l->intern("/"_s);
|
||||
op_info_table[2].op = l->intern("%"_s);
|
||||
op_info_table[3].op = l->intern("<<"_s);
|
||||
op_info_table[4].op = l->intern(">>"_s);
|
||||
op_info_table[5].op = l->intern("+"_s);
|
||||
op_info_table[6].op = l->intern("-"_s);
|
||||
op_info_table[7].op = l->intern("=="_s);
|
||||
op_info_table[8].op = l->intern("<="_s);
|
||||
op_info_table[9].op = l->intern(">="_s);
|
||||
op_info_table[10].op = l->intern("<"_s);
|
||||
op_info_table[11].op = l->intern(">"_s);
|
||||
op_info_table[12].op = l->intern("!="_s);
|
||||
op_info_table[13].op = l->intern("&"_s);
|
||||
op_info_table[14].op = l->intern("|"_s);
|
||||
op_info_table[15].op = l->intern("^"_s);
|
||||
op_info_table[16].op = l->intern("&&"_s);
|
||||
op_info_table[17].op = l->intern("||"_s);
|
||||
op_info_table[18].op = l->intern("~"_s);
|
||||
op_info_table[19].op = l->intern("!"_s);
|
||||
/*END*/
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
parse_init(Parse_Ctx *ctx, Arena *perm_allocator) {
|
||||
core_init_compiler(Parse_Ctx *ctx, Allocator *allocator) {
|
||||
ctx->init_ctx_time_begin = os_time();
|
||||
pctx = ctx;
|
||||
ctx->perm = perm_allocator;
|
||||
ctx->type_map = map_make(ctx->perm, 2048);
|
||||
Allocator *heap_allocator = allocator;
|
||||
ctx->perm = (Arena *)allocator;
|
||||
ctx->type_map = map_make(heap_allocator, 2048);
|
||||
ctx->gen = {ctx->perm};
|
||||
ctx->helper_builder = {ctx->perm};
|
||||
ctx->scope_ids = 1;
|
||||
bigint_allocator = ctx->perm;
|
||||
arena_init(&ctx->stage_arena, "Compiler stage arena"_s);
|
||||
|
||||
lex_init(ctx->perm, ctx->perm, ctx);
|
||||
ctx->tokens = array_make<Token>(heap_allocator, 4096 * 4);
|
||||
ctx->interns = intern_table_make(ctx->perm, heap_allocator, 2048);
|
||||
|
||||
/*#import meta
|
||||
for i in meta.keywords:
|
||||
print(f'keyword_{i.lower()} = ctx->intern("{i}"_s);')
|
||||
print(f'ctx->interns.first_keyword = keyword_{meta.keywords[0].lower()}.str;')
|
||||
print(f'ctx->interns.last_keyword = keyword_{meta.keywords[-1].lower()}.str;')
|
||||
|
||||
for i in meta.interns:
|
||||
print(f'intern_{i.lower()} = ctx->intern("{i}"_s);')
|
||||
|
||||
index = 0
|
||||
for i in meta.token_simple_expr:
|
||||
if i[1] != "SPECIAL":
|
||||
print(f'op_info_table[{index}].op = ctx->intern("{i[1]}"_s);')
|
||||
index += 1
|
||||
|
||||
*/
|
||||
keyword_struct = ctx->intern("struct"_s);
|
||||
keyword_union = ctx->intern("union"_s);
|
||||
keyword_true = ctx->intern("true"_s);
|
||||
keyword_default = ctx->intern("default"_s);
|
||||
keyword_break = ctx->intern("break"_s);
|
||||
keyword_false = ctx->intern("false"_s);
|
||||
keyword_return = ctx->intern("return"_s);
|
||||
keyword_switch = ctx->intern("switch"_s);
|
||||
keyword_assert = ctx->intern("Assert"_s);
|
||||
keyword_if = ctx->intern("if"_s);
|
||||
keyword_elif = ctx->intern("elif"_s);
|
||||
keyword_pass = ctx->intern("pass"_s);
|
||||
keyword_else = ctx->intern("else"_s);
|
||||
keyword_for = ctx->intern("for"_s);
|
||||
keyword_enum = ctx->intern("enum"_s);
|
||||
ctx->interns.first_keyword = keyword_struct.str;
|
||||
ctx->interns.last_keyword = keyword_enum.str;
|
||||
intern_typeof = ctx->intern("TypeOf"_s);
|
||||
intern_sizeof = ctx->intern("SizeOf"_s);
|
||||
intern_len = ctx->intern("Len"_s);
|
||||
intern_alignof = ctx->intern("AlignOf"_s);
|
||||
intern_foreign = ctx->intern("foreign"_s);
|
||||
intern_strict = ctx->intern("strict"_s);
|
||||
intern_void = ctx->intern("void"_s);
|
||||
intern_flag = ctx->intern("flag"_s);
|
||||
intern_it = ctx->intern("it"_s);
|
||||
intern_load = ctx->intern("load"_s);
|
||||
intern_import = ctx->intern("import"_s);
|
||||
intern_link = ctx->intern("link"_s);
|
||||
op_info_table[0].op = ctx->intern("*"_s);
|
||||
op_info_table[1].op = ctx->intern("/"_s);
|
||||
op_info_table[2].op = ctx->intern("%"_s);
|
||||
op_info_table[3].op = ctx->intern("<<"_s);
|
||||
op_info_table[4].op = ctx->intern(">>"_s);
|
||||
op_info_table[5].op = ctx->intern("+"_s);
|
||||
op_info_table[6].op = ctx->intern("-"_s);
|
||||
op_info_table[7].op = ctx->intern("=="_s);
|
||||
op_info_table[8].op = ctx->intern("<="_s);
|
||||
op_info_table[9].op = ctx->intern(">="_s);
|
||||
op_info_table[10].op = ctx->intern("<"_s);
|
||||
op_info_table[11].op = ctx->intern(">"_s);
|
||||
op_info_table[12].op = ctx->intern("!="_s);
|
||||
op_info_table[13].op = ctx->intern("&"_s);
|
||||
op_info_table[14].op = ctx->intern("|"_s);
|
||||
op_info_table[15].op = ctx->intern("^"_s);
|
||||
op_info_table[16].op = ctx->intern("&&"_s);
|
||||
op_info_table[17].op = ctx->intern("||"_s);
|
||||
op_info_table[18].op = ctx->intern("~"_s);
|
||||
op_info_table[19].op = ctx->intern("!"_s);
|
||||
/*END*/
|
||||
|
||||
init_type();
|
||||
|
||||
// Init paths
|
||||
@@ -93,20 +90,13 @@ parse_init(Parse_Ctx *ctx, Arena *perm_allocator) {
|
||||
|
||||
String main_module = string_fmt(ctx->perm, "%Q/modules", ctx->exe_folder);
|
||||
add(ctx->perm, &ctx->module_folders, main_module);
|
||||
ctx->init_ctx_time_end = os_time();
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
begin_compilation(Arena *allocator) {
|
||||
init_ctx_time_begin = os_time();
|
||||
Parse_Ctx *ctx = allocate_struct(allocator, Parse_Ctx);
|
||||
parse_init(ctx, allocator);
|
||||
init_ctx_time_end = os_time();
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
destroy_compiler() {
|
||||
arena_clear(pctx->perm);
|
||||
arena_clear(&pctx->stage_arena);
|
||||
core_bootstrap_compiler(Allocator *allocator) {
|
||||
Parse_Ctx *ctx = allocate_struct(allocator, Parse_Ctx);
|
||||
core_init_compiler(ctx, allocator);
|
||||
}
|
||||
|
||||
CORE_Static void
|
||||
@@ -120,11 +110,9 @@ insert_builtin_type_into_scope(Ast_Scope *p, String name, Ast_Type *type) {
|
||||
add(pctx->perm, &pctx->all_types, type);
|
||||
}
|
||||
|
||||
global F64 parsing_time_begin;
|
||||
global F64 parsing_time_end;
|
||||
CORE_Static void
|
||||
parse_all_modules() {
|
||||
parsing_time_begin = os_time();
|
||||
pctx->parsing_time_begin = os_time();
|
||||
|
||||
Iter_Named(&pctx->modules, mod_it) {
|
||||
Ast_Module *module = mod_it.item[0];
|
||||
@@ -142,7 +130,7 @@ parse_all_modules() {
|
||||
|
||||
module->state = MODULE_PARSED;
|
||||
}
|
||||
parsing_time_end = os_time();
|
||||
pctx->parsing_time_end = os_time();
|
||||
}
|
||||
|
||||
CORE_Static Ast_Module *
|
||||
@@ -207,7 +195,7 @@ CORE_Static void
|
||||
resolve_everything_in_module(Ast_Module *module) {
|
||||
if (module->state == MODULE_RESOLVED)
|
||||
return;
|
||||
resolving_time_begin = os_time();
|
||||
pctx->resolving_time_begin = os_time();
|
||||
Iter_Named(&module->all_loaded_files, file) {
|
||||
Iter(&file.item[0]->decls) {
|
||||
Ast_Decl *decl = it.item[0];
|
||||
@@ -219,13 +207,13 @@ resolve_everything_in_module(Ast_Module *module) {
|
||||
}
|
||||
}
|
||||
module->state = MODULE_RESOLVED;
|
||||
resolving_time_end = os_time();
|
||||
pctx->resolving_time_end = os_time();
|
||||
}
|
||||
|
||||
CORE_Static String
|
||||
compile_file_to_string(Arena *arena, String filename) {
|
||||
total_time = os_time();
|
||||
begin_compilation(arena);
|
||||
pctx->total_time = os_time();
|
||||
core_bootstrap_compiler(arena);
|
||||
{
|
||||
Ast_Module *module = add_module(0, pctx->intern("Language.core"_s));
|
||||
{
|
||||
@@ -321,11 +309,11 @@ compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) {
|
||||
F64 end = os_time();
|
||||
|
||||
if (is_flag_set(compile_flags, COMPILE_PRINT_STATS)) {
|
||||
log_info("total = %f", os_time() - total_time);
|
||||
log_info("total = %f", os_time() - pctx->total_time);
|
||||
log_info("clang = %f", end - begin);
|
||||
log_info("parsing = %f", parsing_time_end - parsing_time_begin);
|
||||
log_info("resolving = %f", resolving_time_end - resolving_time_begin);
|
||||
log_info("generatin = %f", generating_time_end - generating_time_begin);
|
||||
log_info("parsing = %f", pctx->parsing_time_end - pctx->parsing_time_begin);
|
||||
log_info("resolving = %f", pctx->resolving_time_end - pctx->resolving_time_begin);
|
||||
log_info("generatin = %f", pctx->generating_time_end - pctx->generating_time_begin);
|
||||
}
|
||||
|
||||
if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) {
|
||||
@@ -355,5 +343,4 @@ compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) {
|
||||
log_info("");
|
||||
}
|
||||
|
||||
destroy_compiler();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user