A bunch of new allocation related stuff.

This commit is contained in:
2025-09-10 06:59:29 +02:00
parent f4a9592f26
commit ceafd197f5
18 changed files with 852 additions and 170 deletions

View File

@@ -90,8 +90,6 @@ Type_Variable_Handle :: #type, distinct u32;
Scope_Stack :: struct {
allocator : Allocator;
arena : Arena;
stack : [..]Scope;
}
@@ -126,6 +124,8 @@ Scope :: struct {
builtin : bool;
kind : Scope_Kind;
allocator : Allocator;
}
Scope_Handle :: #type, distinct u32;
@@ -621,6 +621,8 @@ push_scope :: (checker : *Semantic_Checker, name := "", kind : Scope_Kind = .Glo
count := checker.result.scope_stack.stack.count;
scope := *checker.result.scope_stack.stack[count - 1];
scope.allocator = make_arena(Kilobytes(512));
scope.table.allocator = scope.allocator;
scope.parent = checker.current_scope;
scope.name = name;
scope.kind = kind;
@@ -628,7 +630,7 @@ push_scope :: (checker : *Semantic_Checker, name := "", kind : Scope_Kind = .Glo
scope.builtin = true;
}
scope.children.allocator = checker.result.scope_stack.allocator;
scope.children.allocator = checker.result.scope_stack.stack.allocator;
if checker.current_scope {
scope := get_current_scope(checker);
@@ -721,18 +723,19 @@ Arg :: struct {
new_builtin_struct :: (checker : *Semantic_Checker, name : string, members : []Arg) -> *Type_Variable, Type_Variable_Handle {
tv, handle := new_builtin_type_variable(checker, .Struct, .Declaration, name, name);
// @Incomplete: Skip for now. This is solely for error reporting?
// At least let's not make a big deal out of it for now.
// We could report builtin nodes in a special way instead of with an actual source location.
builtin_node := new_builtin_struct_node(checker.result, name, members, checker.result.allocator);
symbol : Defined_Symbol;
symbol.name = name;
// symbol.source_node = builtin_node;
symbol.source_node = builtin_node;
symbol.builtin = true;
symbol.type_variable = handle;
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, name, symbol);
tv.source_node = builtin_node;
field_list := get_field_list(builtin_node);
scope, scope_handle := push_scope(checker, name, .Struct);
tv.scope = scope_handle;
@@ -746,7 +749,10 @@ new_builtin_struct :: (checker : *Semantic_Checker, name : string, members : []A
member_symbol : Defined_Symbol;
member_symbol.name = member.name;
member_symbol.type_variable = member_handle;
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, member.name, symbol);
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, member.name, member_symbol);
field_list.children[it_index].type_variable = member_handle;
member_var.source_node = field_list.children[it_index];
add_child(checker, handle, member_handle);
}
@@ -756,36 +762,60 @@ new_builtin_struct :: (checker : *Semantic_Checker, name : string, members : []A
return from_handle(checker, handle), handle;
}
// new_builtin_function :: (checker : *Semantic_Checker, name : string, args : []Arg) -> *Type_Variable, Type_Variable_Handle {
// tv, handle := new_builtin_type_variable(checker, .Function, .Declaration, name);
new_builtin_function :: (checker : *Semantic_Checker, name : string, args : []Arg, return_arg : Arg) -> *Type_Variable, Type_Variable_Handle {
tv, handle := new_builtin_type_variable(checker, .Function, .Declaration, name);
// // @Incomplete: Skip for now. This is solely for error reporting?
// // At least let's not make a big deal out of it for now.
// // We could report builtin nodes in a special way instead of with an actual source location.
// // builtin_node := new_builtin_struct_node(checker.result.nodes, members, checker.result.allocator;
// symbol : Defined_Symbol;
// symbol.name = name;
// // symbol.source_node = builtin_node;
// symbol.builtin = true;
// symbol.type_variable = handle;
// add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, name, symbol);
builtin_node := new_builtin_function_node(checker.result, name, args, return_arg, checker.result.allocator);
// scope, scope_handle := push_scope(checker, name, .Struct);
// tv.scope = scope_handle;
function : Defined_Symbol;
function.name = name;
function.source_node = builtin_node;
function.type_variable = handle;
// for arg : args {
// typename : string;
// kind := get_type_from_identifier(checker, checker.current_scope, arg, *typename);
// arg_var, arg_handle := new_builtin_type_variable(checker, kind, .Expression, typename);
// add_child(checker, handle, arg_handle);
// }
find_result := find_symbol(checker, name, checker.current_scope);
if !find_result {
symbol : Defined_Symbol;
symbol.name = name;
symbol.source_node = builtin_node;
symbol.builtin = true;
symbol.type_variable = 0;
symbol.functions.allocator = get_current_scope(checker).allocator;
array_add(*symbol.functions, function);
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, name, symbol);
} else {
array_add(*find_result.functions, function);
}
// pop_scope(checker);
tv.source_node = builtin_node;
// return from_handle(checker, handle), handle;
// }
field_list := get_field_list(builtin_node);
scope, scope_handle := push_scope(checker, name, .Struct);
tv.scope = scope_handle;
for arg : args {
typename : string;
kind := lookup_type(checker, checker.current_scope, arg.typename, *typename);
arg_var, arg_handle := new_builtin_type_variable(checker, kind, .Expression, arg.name);
arg_var.scope = tv.scope;
arg_symbol : Defined_Symbol;
arg_symbol.name = arg.name;
arg_symbol.type_variable = arg_handle;
add_symbol_to_scope(checker.state, *checker.result.scope_stack, checker.current_scope, arg.name, arg_symbol);
field_list.children[it_index].type_variable = arg_handle;
arg_var.source_node = field_list.children[it_index];
add_child(checker, handle, arg_handle);
}
pop_scope(checker);
return from_handle(checker, handle), handle;
}
add_child :: (variable : *Type_Variable, child : Type_Variable_Handle) {
assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
@@ -808,11 +838,11 @@ init_semantic_checker :: (checker : *Semantic_Checker, root : *AST_Node, path :
checker.program_root = root;
checker.path = path;
// @Incomplete(niels): Use other allocator and/or add static array with convenience functions
checker.result.type_variables.allocator = checker.result.allocator;
array_reserve(*checker.result.type_variables, 2048);
checker.result.scope_stack.allocator = make_arena(Megabytes(8));
checker.result.scope_stack.stack.allocator = checker.result.scope_stack.allocator;
array_reserve(*checker.result.scope_stack.stack, 256);
global_scope, global_handle := push_scope(checker, kind = .Global);
@@ -1040,7 +1070,6 @@ declare_cbuffer :: (checker : *Semantic_Checker, node : *AST_Node) -> Type_Varia
get_actual_function_name :: (node : *AST_Node) -> string {
name_to_check := node.name;
if node.vertex_entry_point {
name_to_check = sprint("%__%", VERTEX_MAIN_FUNCTION_PREFIX, node.name);
} else if node.pixel_entry_point {
name_to_check = sprint("%__%", PIXEL_MAIN_FUNCTION_PREFIX, node.name);
@@ -1087,6 +1116,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
symbol.name = name_to_check;
symbol.source_node = node;
symbol.type_variable = 0;
symbol.functions.allocator = get_current_scope(checker).allocator;
array_reserve(*symbol.functions, 32);
array_add(*symbol.functions, function);
@@ -1768,7 +1798,6 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
return true;
}
}
return false;
}
@@ -1776,9 +1805,54 @@ add_builtins_new :: (checker : *Semantic_Checker) {
checker.state = .Adding_Builtins;
float_name := Typenames[Type_Kind.Float];
new_builtin_struct(checker, "float2", .[.{"x", float_name}, .{"y", float_name}]);
new_builtin_struct(checker, "float3", .[.{"x", float_name}, .{"y", float_name}, .{"z", float_name}]);
new_builtin_struct(checker, "float4", .[.{"x", float_name}, .{"y", float_name}, .{"z", float_name}, .{"w", float_name}]);
int_name := Typenames[Type_Kind.Int];
arg :: (name : string, kind : Type_Kind) -> Arg {
return .{ name, Typenames[kind] };
}
farg :: (name : string) -> Arg {
return arg(name, .Float);
}
iarg :: (name : string) -> Arg {
return arg(name, .Int);
}
float2_tv, f2h := new_builtin_struct(checker, "float2", .[farg("x"), farg("y")]);
float3_tv, f3h := new_builtin_struct(checker, "float3", .[farg("x"), farg("y"), farg("z")]);
float4_tv, f4h := new_builtin_struct(checker, "float4", .[farg("x"), farg("y"), farg("z"), farg("w")]);
float4x4_members : [16]Arg;
i := 0;
for x : 0..3 {
for y : 0..3 {
float4x4_members[i] = farg(tprint("m%%", x + 1, y + 1));
i += 1;
}
}
float4x4_tv, f4x4h := new_builtin_struct(checker, "float4x4", float4x4_members);
int2_tv, i2h := new_builtin_struct(checker, "int2", .[iarg("x"), iarg("y")]);
int3_tv, i3h := new_builtin_struct(checker, "int3", .[iarg("x"), iarg("y"), iarg("z")]);
int4_tv, i4h := new_builtin_struct(checker, "int4", .[iarg("x"), iarg("y"), iarg("z"), iarg("w")]);
int4x4_members : [16]Arg;
i = 0;
for x : 0..3 {
for y : 0..3 {
int4x4_members[i].name = tprint("m%%", x + 1, y + 1);
int4x4_members[i].typename = int_name;
i += 1;
}
}
int4x4_tv, i4x4h := new_builtin_struct(checker, "int4x4", int4x4_members);
new_builtin_function(checker, "float2", .[farg("x"), farg("y")], .{ "res", "float2" });
new_builtin_function(checker, "float2", .[.{"v", "float2"}], .{ "res", "float2" });
new_builtin_function(checker, "float3", .[farg("x"), farg("y"), farg("z")], .{ "res", "float3" });
checker.state = .Type_Checking;
}
@@ -1819,7 +1893,7 @@ add_builtins :: (checker : *Semantic_Checker) {
checker.result.root = null;
tokens : [..]Token;
scratch := get_scratch(checker.result);
scratch := get_scratch();
defer scratch_end(scratch);
tokens.allocator = scratch.allocator;
array_reserve(*tokens, 1024 * 1024);
@@ -1846,26 +1920,35 @@ type_check :: (checker : *Semantic_Checker, root : *AST_Node) {
traverse(checker, root);
}
check :: (result : *Compile_Result) {
check :: (result : *Compile_Result, allocator : Allocator = temp) {
if result.had_error {
return;
}
checker : Semantic_Checker;
new_context := context;
new_context.allocator = allocator;
push_context new_context {
init_context_allocators();
defer clear_context_allocators();
checker.current_buffer_index = 0;
checker.current_sampler_index = 0;
checker.current_texture_index = 0;
checker.result = result;
checker : Semantic_Checker;
checker.current_buffer_index = 0;
checker.current_sampler_index = 0;
checker.current_texture_index = 0;
checker.result = result;
init_semantic_checker(*checker, result.root, result.file.path);
add_builtins_new(*checker);
// add_builtins(*checker);
type_check(*checker, result.root);
result.had_error |= checker.had_error;
}
init_semantic_checker(*checker, result.root, result.file.path);
// add_builtins_new(*checker);
add_builtins(*checker);
type_check(*checker, result.root);
result.had_error |= checker.had_error;
}
// ===========================================================
@@ -2101,7 +2184,7 @@ print_type_variable :: (builder : *String_Builder, variables : []Type_Variable,
source_location.end = right_most.source_location.main_token;
source_location.main_token = node.source_location.main_token;
print_from_source_location(builder, source_location);
print("%\n", print_from_source_location(builder, source_location,, temp));
}
case .Call; {
if variable.return_type_variable{
@@ -2128,7 +2211,7 @@ print_type_variable :: (builder : *String_Builder, variables : []Type_Variable,
append(builder, ")");
}
case; {
print_from_source_location(builder, node.source_location);
print("%\n", print_from_source_location(builder, node.source_location,, temp));
}
}
}