Browse Source

Begin minimizing `Ast` size

gingerBill 3 years ago
parent
commit
3f038428a7

+ 3 - 3
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)) {
 			if (check_is_assignable_to(c, &z, first_type)) {
 				// NOTE(bill): AST GENERATION HACK!
 				// NOTE(bill): AST GENERATION HACK!
 				Token op = {Token_Pointer};
 				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) {
 			} else if (y.mode == Addressing_Variable) {
 				Operand w = y;
 				Operand w = y;
 				w.type = alloc_type_pointer(y.type);
 				w.type = alloc_type_pointer(y.type);
 				if (check_is_assignable_to(c, &w, first_type)) {
 				if (check_is_assignable_to(c, &w, first_type)) {
 					// NOTE(bill): AST GENERATION HACK!
 					// NOTE(bill): AST GENERATION HACK!
 					Token op = {Token_And};
 					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) {
 			if (!fail && first_is_field_value) {
 				Token op = {Token_Eq};
 				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);
 				first_arg = ast_field_value(f, ast_ident(f, make_token_ident(first_arg_name)), first_arg, op);
 			}
 			}
 		}
 		}

+ 1 - 1
src/check_stmt.cpp

@@ -1616,7 +1616,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
 			}
 			}
 			Operand lhs = {Addressing_Invalid};
 			Operand lhs = {Addressing_Invalid};
 			Operand rhs = {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);
 			ast_node(be, BinaryExpr, binary_expr);
 			be->op = op;
 			be->op = op;
 			be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));
 			be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));

+ 3 - 2
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);
 			error(proc_type_node, "A procedure type with the #optional_second tag requires 2 return values, got %td", result_count);
 		} else {
 		} else {
 			bool ok = false;
 			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) {
 			if (!ok) {

+ 1 - 1
src/checker.cpp

@@ -3712,7 +3712,7 @@ String path_to_entity_name(String name, String fullpath, bool strip_extension=tr
 #if 1
 #if 1
 
 
 void add_import_dependency_node(Checker *c, Ast *decl, PtrMap<AstPackage *, ImportGraphNode *> *M) {
 void add_import_dependency_node(Checker *c, Ast *decl, PtrMap<AstPackage *, ImportGraphNode *> *M) {
-	AstPackage *parent_pkg = decl->file->pkg;
+	AstPackage *parent_pkg = decl->file()->pkg;
 
 
 	switch (decl->kind) {
 	switch (decl->kind) {
 	case_ast_node(id, ImportDecl, decl);
 	case_ast_node(id, ImportDecl, decl);

+ 3 - 3
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.kind = Token_Ident;
 	token.string = name;
 	token.string = name;
 	Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags);
 	Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags);
-	e->file = expr->file;
+	e->file = expr->file();
 	e->decl_info = pl->decl;
 	e->decl_info = pl->decl;
 	e->code_gen_module = m;
 	e->code_gen_module = m;
 	e->flags |= EntityFlag_ProcBodyChecked;
 	e->flags |= EntityFlag_ProcBodyChecked;
@@ -1278,8 +1278,8 @@ void lb_generate_code(lbGenerator *gen) {
 
 
 			if (Entity *entry_point = m->info->entry_point) {
 			if (Entity *entry_point = m->info->entry_point) {
 				if (Ast *ident = entry_point->identifier.load()) {
 				if (Ast *ident = entry_point->identifier.load()) {
-					if (ident->file) {
-						init_file = ident->file;
+					if (ident->file_id) {
+						init_file = ident->file();
 					}
 					}
 				}
 				}
 			}
 			}

+ 6 - 6
src/llvm_backend_debug.cpp

@@ -18,7 +18,7 @@ LLVMMetadataRef lb_get_llvm_file_metadata_from_node(lbModule *m, Ast *node) {
 	if (node == nullptr) {
 	if (node == nullptr) {
 		return 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) {
 LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) {
@@ -660,7 +660,7 @@ void lb_debug_complete_types(lbModule *m) {
 			case Type_Struct:
 			case Type_Struct:
 				if (file == nullptr) {
 				if (file == nullptr) {
 					if (bt->Struct.node) {
 					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;
 						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) {
 					if (file == nullptr) {
 						GB_ASSERT(bt->Union.node != 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;
 						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) {
 					if (file == nullptr) {
 						GB_ASSERT(bt->BitSet.node != 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;
 						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_scope = lb_get_current_debug_scope(p);
 	LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, file);
 	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 = {};
 	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.line = LLVMDILocationGetLine(loc);
 	pos.column = LLVMDILocationGetColumn(loc);
 	pos.column = LLVMDILocationGetColumn(loc);
 
 

+ 2 - 2
src/llvm_backend_proc.cpp

@@ -258,8 +258,8 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body)
 		if (entity->file != nullptr) {
 		if (entity->file != nullptr) {
 			file = lb_get_llvm_metadata(m, entity->file);
 			file = lb_get_llvm_metadata(m, entity->file);
 			scope = 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;
 			scope = file;
 		} else if (entity->scope != nullptr) {
 		} else if (entity->scope != nullptr) {
 			file = lb_get_llvm_metadata(m, entity->scope->file);
 			file = lb_get_llvm_metadata(m, entity->scope->file);

+ 3 - 2
src/llvm_backend_stmt.cpp

@@ -212,8 +212,9 @@ void lb_open_scope(lbProcedure *p, Scope *s) {
 			unsigned column = cast(unsigned)token.pos.column;
 			unsigned column = cast(unsigned)token.pos.column;
 
 
 			LLVMMetadataRef file = nullptr;
 			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;
 			LLVMMetadataRef scope = nullptr;
 			if (p->scope_stack.count > 0) {
 			if (p->scope_stack.count > 0) {

+ 12 - 8
src/parser.cpp

@@ -65,7 +65,7 @@ Ast *alloc_ast_node(AstFile *f, AstKind kind) {
 
 
 	Ast *node = cast(Ast *)gb_alloc(a, size);
 	Ast *node = cast(Ast *)gb_alloc(a, size);
 	node->kind = kind;
 	node->kind = kind;
-	node->file = f;
+	node->file_id = f ? f->id : 0;
 	return node;
 	return node;
 }
 }
 
 
@@ -95,7 +95,8 @@ Ast *clone_ast(Ast *node) {
 	if (node == nullptr) {
 	if (node == nullptr) {
 		return 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));
 	gb_memmove(n, node, ast_node_size(node->kind));
 
 
 	switch (n->kind) {
 	switch (n->kind) {
@@ -399,8 +400,9 @@ void error(Ast *node, char const *fmt, ...) {
 	va_start(va, fmt);
 	va_start(va, fmt);
 	error_va(token.pos, end_pos, fmt, va);
 	error_va(token.pos, end_pos, fmt, va);
 	va_end(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);
 	va_start(va, fmt);
 	error_no_newline_va(token.pos, fmt, va);
 	error_no_newline_va(token.pos, fmt, va);
 	va_end(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);
 	va_start(va, fmt);
 	syntax_error_va(token.pos, end_pos, fmt, va);
 	syntax_error_va(token.pos, end_pos, fmt, va);
 	va_end(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;
 	}
 	}
 }
 }
 
 

+ 17 - 10
src/parser.hpp

@@ -255,14 +255,14 @@ ProcCallingConvention default_calling_convention(void) {
 	return ProcCC_Odin;
 	return ProcCC_Odin;
 }
 }
 
 
-enum StateFlag : u16 {
+enum StateFlag : u8 {
 	StateFlag_bounds_check    = 1<<0,
 	StateFlag_bounds_check    = 1<<0,
 	StateFlag_no_bounds_check = 1<<1,
 	StateFlag_no_bounds_check = 1<<1,
 
 
-	StateFlag_BeenHandled = 1<<15,
+	StateFlag_BeenHandled = 1<<7,
 };
 };
 
 
-enum ViralStateFlag : u16 {
+enum ViralStateFlag : u8 {
 	ViralStateFlag_ContainsDeferredProcedure = 1<<0,
 	ViralStateFlag_ContainsDeferredProcedure = 1<<0,
 };
 };
 
 
@@ -666,7 +666,7 @@ AST_KIND(_TypeBegin, "", bool) \
 	}) \
 	}) \
 AST_KIND(_TypeEnd,  "", bool)
 AST_KIND(_TypeEnd,  "", bool)
 
 
-enum AstKind {
+enum AstKind : u16 {
 	Ast_Invalid,
 	Ast_Invalid,
 #define AST_KIND(_kind_name_, ...) GB_JOIN2(Ast_, _kind_name_),
 #define AST_KIND(_kind_name_, ...) GB_JOIN2(Ast_, _kind_name_),
 	AST_KINDS
 	AST_KINDS
@@ -696,18 +696,18 @@ isize const ast_variant_sizes[] = {
 
 
 struct AstCommonStuff {
 struct AstCommonStuff {
 	AstKind      kind;
 	AstKind      kind;
-	u16          state_flags;
-	u16          viral_state_flags;
-	AstFile *    file;
+	u8           state_flags;
+	u8           viral_state_flags;
+	i32          file_id;
 	Scope *      scope;
 	Scope *      scope;
 	TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
 	TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
 };
 };
 
 
 struct Ast {
 struct Ast {
 	AstKind      kind;
 	AstKind      kind;
-	u16          state_flags;
-	u16          viral_state_flags;
-	AstFile *    file;
+	u8           state_flags;
+	u8           viral_state_flags;
+	i32          file_id;
 	Scope *      scope;
 	Scope *      scope;
 	TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
 	TypeAndValue tav; // TODO(bill): Make this a pointer to minimize pointer size
 
 
@@ -717,6 +717,13 @@ struct Ast {
 	AST_KINDS
 	AST_KINDS
 #undef AST_KIND
 #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);
+	}
 };
 };