From 3f038428a7f282468011415db76da4bf08ddb67c Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 14 Nov 2021 15:12:37 +0000 Subject: [PATCH] Begin minimizing `Ast` size --- src/check_expr.cpp | 6 +++--- src/check_stmt.cpp | 2 +- src/check_type.cpp | 5 +++-- src/checker.cpp | 2 +- src/llvm_backend.cpp | 6 +++--- src/llvm_backend_debug.cpp | 12 ++++++------ src/llvm_backend_proc.cpp | 4 ++-- src/llvm_backend_stmt.cpp | 5 +++-- src/parser.cpp | 20 ++++++++++++-------- src/parser.hpp | 27 +++++++++++++++++---------- 10 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 7a0273805..96ca2d308 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -8413,14 +8413,14 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type if (check_is_assignable_to(c, &z, first_type)) { // NOTE(bill): AST GENERATION HACK! Token op = {Token_Pointer}; - first_arg = ast_deref_expr(first_arg->file, first_arg, op); + first_arg = ast_deref_expr(first_arg->file(), first_arg, op); } else if (y.mode == Addressing_Variable) { Operand w = y; w.type = alloc_type_pointer(y.type); if (check_is_assignable_to(c, &w, first_type)) { // NOTE(bill): AST GENERATION HACK! Token op = {Token_And}; - first_arg = ast_unary_expr(first_arg->file, op, first_arg); + first_arg = ast_unary_expr(first_arg->file(), op, first_arg); } } } @@ -8443,7 +8443,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type } if (!fail && first_is_field_value) { Token op = {Token_Eq}; - AstFile *f = first_arg->file; + AstFile *f = first_arg->file(); first_arg = ast_field_value(f, ast_ident(f, make_token_ident(first_arg_name)), first_arg, op); } } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 24ad0eec1..b5e3d8c88 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1616,7 +1616,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { } Operand lhs = {Addressing_Invalid}; Operand rhs = {Addressing_Invalid}; - Ast *binary_expr = alloc_ast_node(node->file, Ast_BinaryExpr); + Ast *binary_expr = alloc_ast_node(node->file(), Ast_BinaryExpr); ast_node(be, BinaryExpr, binary_expr); be->op = op; be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add)); diff --git a/src/check_type.cpp b/src/check_type.cpp index 398967af8..b7feb562d 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -1939,8 +1939,9 @@ bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, error(proc_type_node, "A procedure type with the #optional_second tag requires 2 return values, got %td", result_count); } else { bool ok = false; - if (proc_type_node->file && proc_type_node->file->pkg) { - ok = proc_type_node->file->pkg->scope == ctx->info->runtime_package->scope; + AstFile *file = proc_type_node->file(); + if (file && file->pkg) { + ok = file->pkg->scope == ctx->info->runtime_package->scope; } if (!ok) { diff --git a/src/checker.cpp b/src/checker.cpp index cffaad348..4ae8fd456 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -3712,7 +3712,7 @@ String path_to_entity_name(String name, String fullpath, bool strip_extension=tr #if 1 void add_import_dependency_node(Checker *c, Ast *decl, PtrMap *M) { - AstPackage *parent_pkg = decl->file->pkg; + AstPackage *parent_pkg = decl->file()->pkg; switch (decl->kind) { case_ast_node(id, ImportDecl, decl); diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index e468f3032..5acd2a80f 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -454,7 +454,7 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A token.kind = Token_Ident; token.string = name; Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags); - e->file = expr->file; + e->file = expr->file(); e->decl_info = pl->decl; e->code_gen_module = m; e->flags |= EntityFlag_ProcBodyChecked; @@ -1278,8 +1278,8 @@ void lb_generate_code(lbGenerator *gen) { if (Entity *entry_point = m->info->entry_point) { if (Ast *ident = entry_point->identifier.load()) { - if (ident->file) { - init_file = ident->file; + if (ident->file_id) { + init_file = ident->file(); } } } diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index a890c56ef..7a2b00fe9 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -18,7 +18,7 @@ LLVMMetadataRef lb_get_llvm_file_metadata_from_node(lbModule *m, Ast *node) { if (node == nullptr) { return nullptr; } - return lb_get_llvm_metadata(m, node->file); + return lb_get_llvm_metadata(m, node->file()); } LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) { @@ -660,7 +660,7 @@ void lb_debug_complete_types(lbModule *m) { case Type_Struct: if (file == nullptr) { if (bt->Struct.node) { - file = lb_get_llvm_metadata(m, bt->Struct.node->file); + file = lb_get_llvm_metadata(m, bt->Struct.node->file()); line_number = cast(unsigned)ast_token(bt->Struct.node).pos.line; } } @@ -741,7 +741,7 @@ void lb_debug_complete_types(lbModule *m) { { if (file == nullptr) { GB_ASSERT(bt->Union.node != nullptr); - file = lb_get_llvm_metadata(m, bt->Union.node->file); + file = lb_get_llvm_metadata(m, bt->Union.node->file()); line_number = cast(unsigned)ast_token(bt->Union.node).pos.line; } @@ -801,7 +801,7 @@ void lb_debug_complete_types(lbModule *m) { { if (file == nullptr) { GB_ASSERT(bt->BitSet.node != nullptr); - file = lb_get_llvm_metadata(m, bt->BitSet.node->file); + file = lb_get_llvm_metadata(m, bt->BitSet.node->file()); line_number = cast(unsigned)ast_token(bt->BitSet.node).pos.line; } @@ -929,7 +929,7 @@ void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, T } - AstFile *file = p->body->file; + AstFile *file = p->body->file(); LLVMMetadataRef llvm_scope = lb_get_current_debug_scope(p); LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, file); @@ -975,7 +975,7 @@ void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx) { } TokenPos pos = {}; - pos.file_id = p->body->file ? p->body->file->id : 0; + pos.file_id = p->body->file_id; pos.line = LLVMDILocationGetLine(loc); pos.column = LLVMDILocationGetColumn(loc); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 8b8559cae..25b27ee47 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -258,8 +258,8 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) if (entity->file != nullptr) { file = lb_get_llvm_metadata(m, entity->file); scope = file; - } else if (ident != nullptr && ident->file != nullptr) { - file = lb_get_llvm_metadata(m, ident->file); + } else if (ident != nullptr && ident->file_id != 0) { + file = lb_get_llvm_metadata(m, ident->file()); scope = file; } else if (entity->scope != nullptr) { file = lb_get_llvm_metadata(m, entity->scope->file); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 9d3d2c949..ec8ded7fa 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -212,8 +212,9 @@ void lb_open_scope(lbProcedure *p, Scope *s) { unsigned column = cast(unsigned)token.pos.column; LLVMMetadataRef file = nullptr; - if (s->node->file != nullptr) { - file = lb_get_llvm_metadata(m, s->node->file); + AstFile *ast_file = s->node->file(); + if (ast_file != nullptr) { + file = lb_get_llvm_metadata(m, ast_file); } LLVMMetadataRef scope = nullptr; if (p->scope_stack.count > 0) { diff --git a/src/parser.cpp b/src/parser.cpp index 12888a52d..ba5df1f27 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -65,7 +65,7 @@ Ast *alloc_ast_node(AstFile *f, AstKind kind) { Ast *node = cast(Ast *)gb_alloc(a, size); node->kind = kind; - node->file = f; + node->file_id = f ? f->id : 0; return node; } @@ -95,7 +95,8 @@ Ast *clone_ast(Ast *node) { if (node == nullptr) { return nullptr; } - Ast *n = alloc_ast_node(node->file, node->kind); + AstFile *f = get_ast_file_from_id(node->file_id); + Ast *n = alloc_ast_node(f, node->kind); gb_memmove(n, node, ast_node_size(node->kind)); switch (n->kind) { @@ -399,8 +400,9 @@ void error(Ast *node, char const *fmt, ...) { va_start(va, fmt); error_va(token.pos, end_pos, fmt, va); va_end(va); - if (node != nullptr && node->file != nullptr) { - node->file->error_count += 1; + if (node != nullptr && node->file_id != 0) { + AstFile *f = get_ast_file_from_id(node->file_id); + f->error_count += 1; } } @@ -413,8 +415,9 @@ void error_no_newline(Ast *node, char const *fmt, ...) { va_start(va, fmt); error_no_newline_va(token.pos, fmt, va); va_end(va); - if (node != nullptr && node->file != nullptr) { - node->file->error_count += 1; + if (node != nullptr && node->file_id != 0) { + AstFile *f = get_ast_file_from_id(node->file_id); + f->error_count += 1; } } @@ -442,8 +445,9 @@ void syntax_error(Ast *node, char const *fmt, ...) { va_start(va, fmt); syntax_error_va(token.pos, end_pos, fmt, va); va_end(va); - if (node != nullptr && node->file != nullptr) { - node->file->error_count += 1; + if (node != nullptr && node->file_id != 0) { + AstFile *f = get_ast_file_from_id(node->file_id); + f->error_count += 1; } } diff --git a/src/parser.hpp b/src/parser.hpp index 418d035c4..76ae33b21 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -255,14 +255,14 @@ ProcCallingConvention default_calling_convention(void) { return ProcCC_Odin; } -enum StateFlag : u16 { +enum StateFlag : u8 { StateFlag_bounds_check = 1<<0, StateFlag_no_bounds_check = 1<<1, - StateFlag_BeenHandled = 1<<15, + StateFlag_BeenHandled = 1<<7, }; -enum ViralStateFlag : u16 { +enum ViralStateFlag : u8 { ViralStateFlag_ContainsDeferredProcedure = 1<<0, }; @@ -666,7 +666,7 @@ AST_KIND(_TypeBegin, "", bool) \ }) \ AST_KIND(_TypeEnd, "", bool) -enum AstKind { +enum AstKind : u16 { Ast_Invalid, #define AST_KIND(_kind_name_, ...) GB_JOIN2(Ast_, _kind_name_), AST_KINDS @@ -696,18 +696,18 @@ isize const ast_variant_sizes[] = { struct AstCommonStuff { AstKind kind; - u16 state_flags; - u16 viral_state_flags; - AstFile * file; + u8 state_flags; + u8 viral_state_flags; + i32 file_id; Scope * scope; TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size }; struct Ast { AstKind kind; - u16 state_flags; - u16 viral_state_flags; - AstFile * file; + u8 state_flags; + u8 viral_state_flags; + i32 file_id; Scope * scope; TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size @@ -717,6 +717,13 @@ struct Ast { AST_KINDS #undef AST_KIND }; + + + // NOTE(bill): I know I dislike methods but this is hopefully a temporary thing + // for refactoring purposes + AstFile *file() const { + return get_ast_file_from_id(this->file_id); + } };