Move compile result stuff out of specific stages.
This commit is contained in:
88
Codegen.jai
88
Codegen.jai
@@ -16,26 +16,26 @@ Output_Language :: enum {
|
||||
Codegen_State :: struct {
|
||||
path : string;
|
||||
|
||||
scope_stack : Scope_Stack;
|
||||
// scope_stack : Scope_Stack;
|
||||
current_scope : Scope_Handle;
|
||||
|
||||
type_variables : []Type_Variable;
|
||||
root : *AST_Node;
|
||||
// type_variables : []Type_Variable;
|
||||
// root : *AST_Node;
|
||||
|
||||
output_language : Output_Language;
|
||||
|
||||
builder : String_Builder;
|
||||
|
||||
result : Codegen_Result;
|
||||
result : *Compile_Result;
|
||||
}
|
||||
|
||||
Codegen_Result :: struct {
|
||||
messages : [..]Compiler_Message;
|
||||
// Codegen_Result :: struct {
|
||||
// messages : [..]Compiler_Message;
|
||||
|
||||
had_error : bool;
|
||||
// had_error : bool;
|
||||
|
||||
result_text : string; // @Incomplete(nb): Result for now, should likely be far more sophisticated.
|
||||
}
|
||||
// result_text : string; // @Incomplete(nb): Result for now, should likely be far more sophisticated.
|
||||
// }
|
||||
|
||||
Reserved_HLSL_Words :: string.[
|
||||
"texture",
|
||||
@@ -56,10 +56,7 @@ Reserved_GLSL_Words :: string.[
|
||||
""
|
||||
];
|
||||
|
||||
init_codegen_state :: (state : *Codegen_State, file : *Compiled_File, output_language : Output_Language) {
|
||||
state.root = file.ast_root;
|
||||
state.scope_stack = file.scope_stack;
|
||||
state.type_variables = file.type_variables;
|
||||
init_codegen_state :: (state : *Codegen_State, result : *Compile_Result, output_language : Output_Language) {
|
||||
state.current_scope = cast(Scope_Handle)1;
|
||||
state.output_language = output_language;
|
||||
init_string_builder(*state.builder);
|
||||
@@ -105,16 +102,16 @@ hlsl_type_to_string :: (type_variable : Type_Variable) -> string {
|
||||
}
|
||||
|
||||
emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
find_result := find_symbol(state.scope_stack, node.name, state.current_scope);
|
||||
find_result := find_symbol(state.result.scope_stack, node.name, state.current_scope);
|
||||
|
||||
field := from_handle(state.type_variables, find_result.type_variable);
|
||||
field := from_handle(state.result.type_variables, find_result.type_variable);
|
||||
|
||||
indent(state, indentation);
|
||||
|
||||
print_to_builder(*state.builder, "% ", hlsl_type_to_string(field));
|
||||
|
||||
if field.struct_field_parent {
|
||||
parent_tv := from_handle(state.type_variables, field.struct_field_parent.type_variable);
|
||||
parent_tv := from_handle(state.result.type_variables, field.struct_field_parent.type_variable);
|
||||
|
||||
if parent_tv.typename == "properties" {
|
||||
append(*state.builder, "__PROPERTIES__");
|
||||
@@ -144,7 +141,7 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
|
||||
|
||||
for i :0..field.children.count - 1 {
|
||||
child := from_handle(state.type_variables, field.children[i]);
|
||||
child := from_handle(state.result.type_variables, field.children[i]);
|
||||
emit_node(state, child.source_node, 0);
|
||||
}
|
||||
|
||||
@@ -230,7 +227,7 @@ emit_call :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
}
|
||||
|
||||
emit_properties :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
find_result := find_symbol(state.scope_stack, ifx node.name.count > 0 then node.name else "properties", state.current_scope);
|
||||
find_result := find_symbol(state.result.scope_stack, ifx node.name.count > 0 then node.name else "properties", state.current_scope);
|
||||
|
||||
if !find_result {
|
||||
message : Compiler_Message;
|
||||
@@ -241,7 +238,7 @@ emit_properties :: (state : *Codegen_State, node : *AST_Node, indentation : int)
|
||||
}
|
||||
assert(find_result != null, "Attempting to generate undeclared properties buffer. This should never happen at this stage.");
|
||||
|
||||
variable := from_handle(state.type_variables, find_result.type_variable);
|
||||
variable := from_handle(state.result.type_variables, find_result.type_variable);
|
||||
|
||||
print_to_builder(*state.builder, "cbuffer __PROPERTIES : register(b%) \n{\n", variable.resource_index);
|
||||
|
||||
@@ -253,7 +250,7 @@ emit_properties :: (state : *Codegen_State, node : *AST_Node, indentation : int)
|
||||
for child : node.children {
|
||||
if child.kind == .FieldList {
|
||||
for field : child.children {
|
||||
tv := from_handle(state.type_variables, field.type_variable);
|
||||
tv := from_handle(state.result.type_variables, field.type_variable);
|
||||
if tv.type == .Sampler || tv.type == .Texture2D {
|
||||
array_add(*resources, field);
|
||||
continue;
|
||||
@@ -281,7 +278,7 @@ emit_properties :: (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.scope_stack, name, state.current_scope);
|
||||
find_result := find_symbol(state.result.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 {
|
||||
@@ -293,12 +290,12 @@ emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, e
|
||||
}
|
||||
|
||||
for func : find_result.functions {
|
||||
function_variable := from_handle(state.type_variables, func.type_variable);
|
||||
function_variable := from_handle(state.result.type_variables, func.type_variable);
|
||||
|
||||
indent(state, indentation);
|
||||
|
||||
if function_variable.return_type_variable {
|
||||
return_variable := from_handle(state.type_variables, 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));
|
||||
} else {
|
||||
append(*state.builder, "void ");
|
||||
@@ -438,12 +435,12 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
case .Variable; {
|
||||
indent(*state.builder, indentation);
|
||||
|
||||
type_var := from_handle(state.type_variables, node.type_variable);
|
||||
type_var := from_handle(state.result.type_variables, node.type_variable);
|
||||
is_properties := type_var.typename == "properties";
|
||||
|
||||
if !is_properties {
|
||||
if type_var.struct_field_parent {
|
||||
parent_tv := from_handle(state.type_variables, type_var.struct_field_parent.type_variable);
|
||||
parent_tv := from_handle(state.result.type_variables, type_var.struct_field_parent.type_variable);
|
||||
|
||||
if parent_tv.typename == "properties" {
|
||||
append(*state.builder, "__PROPERTIES__");
|
||||
@@ -556,7 +553,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.type_variables, node.type_variable).scope;
|
||||
state.current_scope = from_handle(state.result.type_variables, node.type_variable).scope;
|
||||
|
||||
field_list := node.children[0];
|
||||
|
||||
@@ -573,11 +570,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.type_variables, node.type_variable);
|
||||
variable := from_handle(state.result.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.type_variables, node.type_variable).scope;
|
||||
state.current_scope = from_handle(state.result.type_variables, node.type_variable).scope;
|
||||
|
||||
field_list := node.children[0];
|
||||
|
||||
@@ -615,21 +612,20 @@ codegen :: (result : *Compile_Result) {
|
||||
return;
|
||||
}
|
||||
|
||||
for *file : result.files {
|
||||
state : Codegen_State;
|
||||
init_codegen_state(*state, file, .HLSL);
|
||||
state : Codegen_State;
|
||||
state.result = result;
|
||||
state.current_scope = cast(Scope_Handle)1;
|
||||
state.output_language = .HLSL;
|
||||
init_string_builder(*state.builder);
|
||||
|
||||
codegen_result := codegen(*state);
|
||||
|
||||
file.codegen_result_text = copy_string(codegen_result.result_text);
|
||||
}
|
||||
codegen(*state);
|
||||
}
|
||||
|
||||
codegen :: (state : *Codegen_State) -> Codegen_Result {
|
||||
codegen :: (state : *Codegen_State) {
|
||||
found_function : bool = false;
|
||||
// found_struct : bool = false;
|
||||
|
||||
// for variable : state.type_variables {
|
||||
// for variable : state.result.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;
|
||||
@@ -642,7 +638,7 @@ codegen :: (state : *Codegen_State) -> Codegen_Result {
|
||||
// append(*state.builder, "\n");
|
||||
// }
|
||||
|
||||
for variable : state.type_variables {
|
||||
for variable : state.result.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);
|
||||
@@ -653,22 +649,24 @@ codegen :: (state : *Codegen_State) -> Codegen_Result {
|
||||
append(*state.builder, "\n");
|
||||
}
|
||||
|
||||
for declaration : state.root.children {
|
||||
for declaration : state.result.root.children {
|
||||
if declaration.foreign_declaration {
|
||||
continue;
|
||||
}
|
||||
emit_declaration(state, declaration);
|
||||
}
|
||||
|
||||
state.result.result_text = builder_to_string(*state.builder);
|
||||
|
||||
return state.result;
|
||||
state.result.codegen_result_text = builder_to_string(*state.builder);
|
||||
}
|
||||
|
||||
codegen :: (file : *Compiled_File, output_language : Output_Language) -> Codegen_Result {
|
||||
codegen :: (result : *Compile_Result, output_language : Output_Language) {
|
||||
codegen_state : Codegen_State;
|
||||
init_codegen_state(*codegen_state, file, output_language);
|
||||
return codegen(*codegen_state);
|
||||
codegen_state.result = result;
|
||||
codegen_state.current_scope = cast(Scope_Handle)1;
|
||||
codegen_state.output_language = output_language;
|
||||
init_string_builder(*codegen_state.builder);
|
||||
|
||||
codegen(*codegen_state);
|
||||
}
|
||||
|
||||
#scope_module
|
||||
|
||||
Reference in New Issue
Block a user