A bunch of array fixes and some buffer stuff that doesn't quite work yet

This commit is contained in:
2025-09-24 14:04:50 +02:00
parent 50a404984d
commit 940b58331d
18 changed files with 261 additions and 137 deletions

View File

@@ -22,7 +22,7 @@ Codegen_State :: struct {
builder : String_Builder;
result : *Compiler_Context;
ctx : *Compiler_Context;
}
Reserved_HLSL_Words :: string.[
@@ -44,7 +44,7 @@ Reserved_GLSL_Words :: string.[
""
];
init_codegen_state :: (state : *Codegen_State, result : *Compiler_Context, output_language : Output_Language) {
init_codegen_state :: (state : *Codegen_State, ctx : *Compiler_Context, output_language : Output_Language) {
state.current_scope = cast(Scope_Handle)1;
state.output_language = output_language;
init_string_builder(*state.builder);
@@ -54,7 +54,11 @@ indent :: (state : *Codegen_State, indentation : int) {
for 1..indentation append(*state.builder, " ");
}
hlsl_type_to_string :: (type_variable : Type_Variable) -> string {
hlsl_type_to_string :: (variables : []Type_Variable, type_handle : Type_Variable_Handle) -> string {
return hlsl_type_to_string(variables, from_handle(variables, type_handle));
}
hlsl_type_to_string :: (variables : []Type_Variable, type_variable : Type_Variable) -> string {
if type_variable.type == {
case .Invalid;
return "{{invalid}}";
@@ -83,20 +87,20 @@ hlsl_type_to_string :: (type_variable : Type_Variable) -> string {
return type_variable.typename;
}
case .Array;
return "array";
return hlsl_type_to_string(variables, type_variable.element_type);
}
return "";
}
emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
find_result := find_symbol(state.result.scope_stack, node.name, state.current_scope);
find_result := find_symbol(state.ctx.scope_stack, node.name, state.current_scope);
field := from_handle(state.result.type_variables, find_result.type_variable);
field := from_handle(state.ctx.type_variables, find_result.type_variable);
indent(state, indentation);
print_to_builder(*state.builder, "% ", hlsl_type_to_string(field));
print_to_builder(*state.builder, "% ", hlsl_type_to_string(state.ctx.type_variables, field));
print_to_builder(*state.builder, "%", node.name);
@@ -111,8 +115,14 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
if node.children.count == 1 {
child := node.children[0];
print_to_builder(*state.builder, " = ");
emit_node(state, child, 0);
if field.type == .Array {
append(*state.builder, "[");
emit_node(state, child, 0);
append(*state.builder, "]");
} else {
print_to_builder(*state.builder, " = ");
emit_node(state, child, 0);
}
}
if node.parent.kind == .Block {
@@ -120,7 +130,7 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
}
for i :0..field.children.count - 1 {
child := from_handle(state.result.type_variables, field.children[i]);
child := from_handle(state.ctx.type_variables, field.children[i]);
emit_node(state, child.source_node, 0);
}
@@ -206,7 +216,7 @@ emit_call :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, emit_body := true) {
name := get_actual_function_name(node);
find_result := find_symbol(state.result.scope_stack, name, state.current_scope);
find_result := find_symbol(state.ctx.scope_stack, name, state.current_scope);
assert(find_result != null, "Attempting to generate undeclared function. This should never happen at this stage.");
if !find_result {
@@ -214,17 +224,17 @@ emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, e
message.message_kind = .Internal_Error;
message.path = state.path;
message.message = "Attempting to generate undeclared function. This should never happen at this stage.";
array_add(*state.result.messages, message);
array_add(*state.ctx.messages, message);
}
for func : find_result.functions {
function_variable := from_handle(state.result.type_variables, func.type_variable);
function_variable := from_handle(state.ctx.type_variables, func.type_variable);
indent(state, indentation);
if function_variable.return_type_variable {
return_variable := from_handle(state.result.type_variables, function_variable.return_type_variable);
print_to_builder(*state.builder, "% ", hlsl_type_to_string(return_variable));
return_variable := from_handle(state.ctx.type_variables, function_variable.return_type_variable);
print_to_builder(*state.builder, "% ", hlsl_type_to_string(state.ctx.type_variables, return_variable));
} else {
append(*state.builder, "void ");
}
@@ -360,7 +370,7 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
case .Variable; {
indent(*state.builder, indentation);
type_var := from_handle(state.result.type_variables, node.type_variable);
type_var := from_handle(state.ctx.type_variables, node.type_variable);
print_to_builder(*state.builder, "%", node.name);
@@ -372,19 +382,29 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
case .Binary; {
indent(*state.builder, indentation);
if node.token.kind != .TOKEN_ASSIGN {
if node.token.kind != .TOKEN_ASSIGN && node.token.kind != .TOKEN_LEFTBRACKET {
append(*state.builder, "(");
}
lhs := node.children[0];
rhs := node.children[1];
emit_node(state, lhs, 0);
append(*state.builder, " ");
emit_operator(state, node.token.kind);
append(*state.builder, " ");
emit_node(state, rhs, 0);
if node.token.kind != .TOKEN_ASSIGN {
if node.token.kind == .TOKEN_LEFTBRACKET {
emit_node(state, lhs, 0);
append(*state.builder, "[");
emit_node(state, rhs, 0);
append(*state.builder, "]");
} else {
emit_node(state, lhs, 0);
append(*state.builder, " ");
emit_operator(state, node.token.kind);
append(*state.builder, " ");
emit_node(state, rhs, 0);
}
if node.token.kind != .TOKEN_ASSIGN && node.token.kind != .TOKEN_LEFTBRACKET {
append(*state.builder, ")");
}
}
@@ -486,7 +506,7 @@ emit_struct :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
print_to_builder(*state.builder, "struct %", node.name);
current_scope := state.current_scope;
state.current_scope = from_handle(state.result.type_variables, node.type_variable).scope;
state.current_scope = from_handle(state.ctx.type_variables, node.type_variable).scope;
field_list := node.children[0];
@@ -503,11 +523,11 @@ emit_struct :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
}
emit_cbuffer :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
variable := from_handle(state.result.type_variables, node.type_variable);
variable := from_handle(state.ctx.type_variables, node.type_variable);
print_to_builder(*state.builder, "cbuffer % : register(b%)", variable.name, variable.resource_index);
current_scope := state.current_scope;
state.current_scope = from_handle(state.result.type_variables, node.type_variable).scope;
state.current_scope = from_handle(state.ctx.type_variables, node.type_variable).scope;
field_list := node.children[0];
@@ -553,7 +573,7 @@ codegen :: (result : *Compiler_Context, output_language : Output_Language, alloc
defer clear_context_allocators();
state : Codegen_State;
state.result = result;
state.ctx = result;
state.current_scope = cast(Scope_Handle)1;
state.output_language = output_language;
init_string_builder(*state.builder);
@@ -567,7 +587,7 @@ codegen :: (state : *Codegen_State) {
found_function : bool = false;
// found_struct : bool = false;
// for variable : state.result.type_variables {
// for variable : state.ctx.type_variables {
// if variable.type == .Struct && variable.kind == .Declaration && !variable.builtin {
// if variable.source_node.kind == .Properties continue;
// if variable.source_node.kind == .Meta continue;
@@ -580,7 +600,7 @@ codegen :: (state : *Codegen_State) {
// append(*state.builder, "\n");
// }
for variable : state.result.type_variables {
for variable : state.ctx.type_variables {
if variable.type == .Function && !variable.builtin
&& !variable.source_node.vertex_entry_point && !variable.source_node.pixel_entry_point {
emit_function(state, variable.source_node, 0, false);
@@ -591,14 +611,14 @@ codegen :: (state : *Codegen_State) {
append(*state.builder, "\n");
}
for declaration : state.result.root.children {
for declaration : state.ctx.root.children {
if declaration.foreign_declaration {
continue;
}
emit_declaration(state, declaration);
}
state.result.codegen_result_text = builder_to_string(*state.builder);
state.ctx.codegen_result_text = builder_to_string(*state.builder);
}
#scope_module