From 647958b72d7cdf665273d1054e55c09074a5b323 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 1 Jan 2023 14:17:37 +0100 Subject: [PATCH] Getting rid of globals --- core_codegen_c_language.cpp | 36 ++++++++++++++++++------------------ core_compiler.cpp | 13 +++++++++---- core_compiler.h | 19 +++++++++++++++++-- core_globals.cpp | 10 ---------- core_lexing.cpp | 6 +++--- core_main.cpp | 20 ++++++++++---------- core_parsing.cpp | 4 ++-- core_typechecking.cpp | 4 ++-- core_types.cpp | 2 +- 9 files changed, 62 insertions(+), 52 deletions(-) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index f71474d..ff89cfc 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -16,7 +16,7 @@ global Intern_String last_filename; global int last_line; CORE_Static void gen_line(Ast *node){ - if(emit_line_directives){ + if(pctx->emit_line_directives){ last_line = node->pos->line+1; genln("#line %d", last_line); if(node->pos->file != last_filename){ @@ -28,7 +28,7 @@ gen_line(Ast *node){ CORE_Static void gen_last_line(){ - if(emit_line_directives){ + if(pctx->emit_line_directives){ genln("#line %d", last_line); } } @@ -223,7 +223,7 @@ gen_value(Token *pos, Value a){ }break; case TYPE_STRING:{ int length = 0; - gen("(%QString){(uint8_t *)\"", symbol_prefix); + gen("(%QString){(uint8_t *)\"", pctx->symbol_prefix); for(int i = 0; i < a.intern_val.len; i++){ if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");} else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");} @@ -518,8 +518,8 @@ gen_ast(Ast *ast){ switch(ast->kind){ CASE(RUNTIME_ASSERT, Builtin){ - if(node->assert_message.len == 0) gen("%QAssert", symbol_prefix); - else gen("%QAssertMessage", symbol_prefix); + if(node->assert_message.len == 0) gen("%QAssert", pctx->symbol_prefix); + else gen("%QAssertMessage", pctx->symbol_prefix); gen("("); gen_expr(node->expr); if(node->assert_message.len){ @@ -540,7 +540,7 @@ gen_ast(Ast *ast){ int i = 0; For(node->expr){ - genln("%QMemoryCopy(&%Q.m%d, ", symbol_prefix, var_name, i); + genln("%QMemoryCopy(&%Q.m%d, ", pctx->symbol_prefix, var_name, i); if(!is_array(it->resolved_type)) gen("&"); gen("("); gen_expr(it); @@ -619,7 +619,7 @@ gen_ast(Ast *ast){ if(node->is_array_traversal){ gen("for(int64_t _i%d = 0; _i%d < ", node->pos->line, node->pos->line); if(is_array(node->cond->resolved_type)){ - gen("%QBufferSize(", symbol_prefix); + gen("%QBufferSize(", pctx->symbol_prefix); gen_expr(node->cond); gen(")"); } else{ @@ -753,7 +753,7 @@ gen_ast(Ast *ast){ int i = 0; For(node->vars){ - gen("%QMemoryCopy((void *)&%Q, (void *)&%Q.m%d, sizeof(%Q));", symbol_prefix, it->name, var_name, i++, it->name); + gen("%QMemoryCopy((void *)&%Q, (void *)&%Q.m%d, sizeof(%Q));", pctx->symbol_prefix, it->name, var_name, i++, it->name); } BREAK(); } @@ -773,8 +773,8 @@ CORE_Static String compile_to_c_code(){ pctx->generating_time_begin = os_time(); - prefixed_string_type = string_fmt(pctx->perm, "%QString", symbol_prefix); - if(single_header_library_mode){ + prefixed_string_type = string_fmt(pctx->perm, "%QString", pctx->symbol_prefix); + if(pctx->single_header_library_mode){ gen(R"( /* Do this: @@ -792,10 +792,10 @@ compile_to_c_code(){ You can #define %QAssertMessage(x) to get more comprehensive error info You can #define %QMemoryCopy(x) to avoid using default memory copy */ - )", single_header_library_name, single_header_library_name, single_header_library_name, - symbol_prefix, symbol_prefix, symbol_prefix); - genln("#ifndef %Q_LIBRARY_HEADER ", single_header_library_name); - genln("#define %Q_LIBRARY_HEADER ", single_header_library_name); + )", pctx->single_header_library_name, pctx->single_header_library_name, pctx->single_header_library_name, + pctx->symbol_prefix, pctx->symbol_prefix, pctx->symbol_prefix); + genln("#ifndef %Q_LIBRARY_HEADER ", pctx->single_header_library_name); + genln("#define %Q_LIBRARY_HEADER ", pctx->single_header_library_name); } gen(R"( @@ -906,7 +906,7 @@ typedef struct String{ } - if(emit_type_info){ + if(pctx->emit_type_info){ // Generate language.core for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){ Ast_Decl *it = get(&pctx->ordered_decls, i); @@ -959,9 +959,9 @@ typedef struct String{ } - if(single_header_library_mode){ + if(pctx->single_header_library_mode){ genln("#endif"); - genln("#ifdef %Q_IMPLEMENTATION ", single_header_library_name); + genln("#ifdef %Q_IMPLEMENTATION ", pctx->single_header_library_name); } // Generate actual code @@ -973,7 +973,7 @@ typedef struct String{ } - if(single_header_library_mode){ + if(pctx->single_header_library_mode){ genln("#endif"); } diff --git a/core_compiler.cpp b/core_compiler.cpp index 6d120f5..1007e55 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -1,6 +1,11 @@ CORE_Static void core_init_compiler(Core_Ctx *ctx, Allocator *allocator) { + ctx->emit_type_info = true; + ctx->emit_line_directives = true; + ctx->color_codes_enabled = true; + ctx->same_scope_token = { SAME_SCOPE }; + ctx->init_ctx_time_begin = os_time(); pctx = ctx; ctx->perm_push_only = make_push_arena(allocator); @@ -240,21 +245,21 @@ compile_file_to_string(Allocator *allocator, String filename) { } { - Ast_Scope *scope = ast_decl_scope(&null_token, pctx->perm, get(&module->all_loaded_files, 0)); - Ast_Decl * decl = ast_namespace(&null_token, scope, pctx->intern("Const"_s)); + Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0)); + Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s)); decl->state = DECL_RESOLVED; Value v1 = {}; v1.type = untyped_string; v1.intern_val = pctx->intern(OS_NAME); - Ast_Decl *const_os1 = ast_const(&null_token, pctx->intern("OSName"_s), v1); + Ast_Decl *const_os1 = ast_const(&pctx->null_token, pctx->intern("OSName"_s), v1); const_os1->state = DECL_RESOLVED; insert_into_scope(scope, const_os1); Value v2 = {}; v1.type = untyped_string; v1.intern_val = pctx->intern(OS_NAME_LOWER); - Ast_Decl *const_os2 = ast_const(&null_token, pctx->intern("OSNameLower"_s), v2); + Ast_Decl *const_os2 = ast_const(&pctx->null_token, pctx->intern("OSNameLower"_s), v2); const_os2->state = DECL_RESOLVED; insert_into_scope(scope, const_os2); diff --git a/core_compiler.h b/core_compiler.h index 35f3be0..57b6618 100644 --- a/core_compiler.h +++ b/core_compiler.h @@ -1,5 +1,6 @@ /* + @! Separate out the codegen stage cause that can change @! Change type of Stage allocator @! Look into stage allocator and perhaps use it more often to reduce scenarios @@ -68,8 +69,6 @@ struct Core_Ctx{ String working_folder; List files_to_link; - S64 indent; - String_Builder gen; String_Builder helper_builder; F64 generating_time_begin; @@ -82,6 +81,22 @@ struct Core_Ctx{ F64 parsing_time_begin; F64 parsing_time_end; + bool color_codes_enabled; + + // Codegen stage mostly + S64 indent; + String_Builder gen; + + // Codegen stage configurables + bool emit_line_directives; + bool emit_type_info; + String symbol_prefix; + bool single_header_library_mode; + String single_header_library_name; + + Token same_scope_token; + Token null_token; + Intern_String intern(String string){ assert(string.len > 0); return intern_string(&interns, string); diff --git a/core_globals.cpp b/core_globals.cpp index df25a16..f02b285 100644 --- a/core_globals.cpp +++ b/core_globals.cpp @@ -1,18 +1,8 @@ -global B32 emit_line_directives = true; -global B32 emit_type_info = true; -global String symbol_prefix = ""_s; -global B32 single_header_library_mode = false; -global String single_header_library_name = ""_s; -global bool color_codes_enabled; - thread_local Core_Ctx *pctx; Allocator *bigint_allocator; global S64 bigint_allocation_count; -global Token token_null = {SAME_SCOPE}; -global Token null_token; // @todo: memes, why the above is called null? - /*#import meta for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};') for i in meta.interns: print(f'Intern_String intern_{i.lower()};') diff --git a/core_lexing.cpp b/core_lexing.cpp index e4c769f..b890559 100644 --- a/core_lexing.cpp +++ b/core_lexing.cpp @@ -192,10 +192,10 @@ token_make(Core_Ctx *lexer){ CORE_Static Token * lex_last_indent_token(Lex_Stream *s){ - if(s->indent_stack.len > 0){ + if (s->indent_stack.len > 0) { return *s->indent_stack.last(); } - return &token_null; + return &pctx->same_scope_token; } CORE_Static B32 @@ -594,7 +594,7 @@ lex_restream(Core_Ctx *lexer, String istream, String file){ Scratch_Scope _scope(lexer->scratch); lexer->stream.indent_stack.allocator = lexer->scratch; - lexer->stream.indent_stack.add(&token_null); + lexer->stream.indent_stack.add(&lexer->same_scope_token); lex__stream(lexer); } diff --git a/core_main.cpp b/core_main.cpp index fcf53a6..5c4d36a 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -307,8 +307,9 @@ int main(int argument_count, char **arguments){ return 0; } + bool enable_color_codes = false; #if OS_UNIX - color_codes_enabled = true; + enable_color_codes = true; #endif #if OS_WINDOWS // Set output mode to handle virtual terminal sequences @@ -318,28 +319,27 @@ int main(int argument_count, char **arguments){ if (GetConsoleMode(hOut, &dwMode)) { dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if (SetConsoleMode(hOut, dwMode)) { - color_codes_enabled = true; + enable_color_codes = true; } else{ - log_info("Failed to enable colored terminal output C"); + printf("Failed to enable colored terminal output C\n"); } } else{ - log_info("Failed to enable colored terminal output B"); + printf("Failed to enable colored terminal output B\n"); } } else { - log_info("Failed to enable colored terminal output A"); + printf("Failed to enable colored terminal output A\n"); } test_os_memory(); #endif - // emit_line_directives = false; - // emit_type_info = false; + (void)enable_color_codes; For(args){ - - if(it == "-testing"_s){ // @copy_paste + + if(it == "-testing"_s){ Scratch_Scope _scope(scratch_arena); Array examples = os_list_dir(scratch_arena, scratch_arena, "examples"_s); Array tests = os_list_dir(scratch_arena, scratch_arena, "tests"_s); @@ -359,6 +359,6 @@ int main(int argument_count, char **arguments){ } } - log_info("End of program"); + printf("End of program\n"); return 0; } diff --git a/core_parsing.cpp b/core_parsing.cpp index fac010d..3d5e923 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -33,7 +33,7 @@ print_token_line(Token *token){ log_info_no_nl("%.*s", i1, token->line_begin); // Print token part - if(color_codes_enabled){ + if(pctx->color_codes_enabled){ log_info_no_nl( PRINTF_RED "%.*s" PRINTF_RESET, (int)token->len, token->str); } else { log_info_no_nl("%.*s", (int)token->len, token->str); @@ -105,7 +105,7 @@ CORE_Static Token * token_get(S64 i = 0){ i += pctx->token_iter; if(i >= pctx->tokens.len){ - return &null_token; + return &pctx->null_token; } Token *result = &pctx->tokens[i]; return result; diff --git a/core_typechecking.cpp b/core_typechecking.cpp index e1c5c28..5253bda 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1647,11 +1647,11 @@ resolve_decl(Ast_Decl *ast){ node->unique_name = node->name; if(!is_flag_set(node->expr->flags, AST_FOREIGN)){ - node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", symbol_prefix, node->name, pctx->lambda_ids++)); + node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", pctx->symbol_prefix, node->name, pctx->lambda_ids++)); } if(is_flag_set(node->flags, AST_OPERATOR_OVERLOAD)){ - node->unique_name = pctx->intern(string_fmt(scratch, "%QOPERATOR_%Q%d", symbol_prefix, node->overload_op_info->name, pctx->lambda_ids++)); + node->unique_name = pctx->intern(string_fmt(scratch, "%QOPERATOR_%Q%d", pctx->symbol_prefix, node->overload_op_info->name, pctx->lambda_ids++)); } BREAK(); diff --git a/core_types.cpp b/core_types.cpp index 13a99a6..8952463 100644 --- a/core_types.cpp +++ b/core_types.cpp @@ -252,7 +252,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){ type->padding = type->size - members_size; type->agg.members = members.tight_copy(pctx->perm); type->kind = TYPE_STRUCT; - node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q", symbol_prefix, node->name)); + node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q", pctx->symbol_prefix, node->name)); } CORE_Static void