Compare commits

9 Commits

16 changed files with 126 additions and 76 deletions

View File

@@ -283,7 +283,6 @@ pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String
print_to_builder(builder, "%", node.name); print_to_builder(builder, "%", node.name);
for child : node.children { for child : node.children {
print("%\n", child.kind);
if child.kind == .Variable { if child.kind == .Variable {
append(builder, "."); append(builder, ".");
pretty_print_variable(child, indentation, builder); pretty_print_variable(child, indentation, builder);

View File

@@ -1,3 +1,12 @@
/////////////////////////////////////
//~ nbr:
//
/////////////////////////////////////
//~ nbr: Codegen TODOs
//
// [ ] Prefix output of property values with __PROPERTIES so we don't get name clashes
Output_Language :: enum { Output_Language :: enum {
HLSL; HLSL;
GLSL; // @Incomplete GLSL; // @Incomplete
@@ -82,7 +91,16 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
field := h2tv(state.type_variables, find_result.type_variable); field := h2tv(state.type_variables, find_result.type_variable);
indent(state, indentation); indent(state, indentation);
print_to_builder(*state.builder, "% ", dx11_type_to_string(field)); print_to_builder(*state.builder, "% ", dx11_type_to_string(field));
if field.struct_field_parent {
parent_tv := h2tv(state.type_variables, field.struct_field_parent.type_variable);
if parent_tv.typename == "properties" {
append(*state.builder, "__PROPERTIES__");
}
}
print_to_builder(*state.builder, "%", node.name); print_to_builder(*state.builder, "%", node.name);
if field.type == .Sampler { if field.type == .Sampler {
@@ -100,7 +118,7 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_node(state, child, 0); emit_node(state, child, 0);
} }
for i :0..field.child_count - 1 { for i :0..field.children.count - 1 {
child := h2tv(state.type_variables, field.children[i]); child := h2tv(state.type_variables, field.children[i]);
emit_node(state, child.source_node, 0); emit_node(state, child.source_node, 0);
} }
@@ -111,6 +129,8 @@ emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
append(*state.builder, " : POSITION"); append(*state.builder, " : POSITION");
} else if hint.ident_value == "uv" { } else if hint.ident_value == "uv" {
append(*state.builder, " : TEXCOORD0"); append(*state.builder, " : TEXCOORD0");
} else if hint.ident_value == "outposition" {
append(*state.builder, " : SV_POSITION");
} }
} }
} }
@@ -363,6 +383,13 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
is_properties := type_var.typename == "properties"; is_properties := type_var.typename == "properties";
if !is_properties { if !is_properties {
if type_var.struct_field_parent {
parent_tv := h2tv(state.type_variables, type_var.struct_field_parent.type_variable);
if parent_tv.typename == "properties" {
append(*state.builder, "__PROPERTIES__");
}
}
print_to_builder(*state.builder, "%", node.name); print_to_builder(*state.builder, "%", node.name);
} }
@@ -375,6 +402,11 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
} }
case .Binary; { case .Binary; {
indent(*state.builder, indentation); indent(*state.builder, indentation);
if node.token.kind != .TOKEN_ASSIGN {
append(*state.builder, "(");
}
lhs := node.children[0]; lhs := node.children[0];
rhs := node.children[1]; rhs := node.children[1];
emit_node(state, lhs, 0); emit_node(state, lhs, 0);
@@ -383,6 +415,9 @@ emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
emit_operator(state, node.token.kind); emit_operator(state, node.token.kind);
append(*state.builder, " "); append(*state.builder, " ");
emit_node(state, rhs, 0); emit_node(state, rhs, 0);
if node.token.kind != .TOKEN_ASSIGN {
append(*state.builder, ")");
}
} }
case .Unary; { case .Unary; {
assert(false, "Not implemented yet: unary"); assert(false, "Not implemented yet: unary");

View File

@@ -5,7 +5,8 @@
///////////////////////////////////// /////////////////////////////////////
//~ nbr: Error reporting TODOs //~ nbr: Error reporting TODOs
// //
// [x] Improve error reporting on mismatched overloads when types don't match, but arity does // [ ] Add and error for using keywords as names, or rename the dx11 keywords in the resulting hlsl shader.
// [x] Improve error reporting on mismatched overloads when types don't match, but arity does.
// [x] Improve error reporting for type mismatches in general. It seems like the expect node is not always correct. // [x] Improve error reporting for type mismatches in general. It seems like the expect node is not always correct.
VERTEX_MAIN_FUNCTION_PREFIX :: "vertex"; VERTEX_MAIN_FUNCTION_PREFIX :: "vertex";
@@ -70,9 +71,8 @@ Type_Variable :: struct {
typename : string; typename : string;
is_array : bool; is_array : bool;
MAX_TYPE_VARIABLE_CHILDREN :: 16; MAX_TYPE_VARIABLE_CHILDREN :: 32;
children : [MAX_TYPE_VARIABLE_CHILDREN]Type_Variable_Handle; children : Static_Array(Type_Variable_Handle, MAX_TYPE_VARIABLE_CHILDREN);
child_count : int;
//@Note(niels): For constant buffers //@Note(niels): For constant buffers
resource_index : u32; resource_index : u32;
@@ -361,10 +361,9 @@ no_matching_overload_found :: (checker : *Semantic_Checker, call : *AST_Node, ov
func_var := h2tv(checker, func.type_variable); func_var := h2tv(checker, func.type_variable);
if arg_list.children.count != func_var.child_count { if arg_list.children.count != func_var.children.count {
print_to_builder(*builder, "Not enough arguments: Wanted %, got %.\n\n", func_var.child_count, arg_list.children.count); print_to_builder(*builder, "Not enough arguments: Wanted %, got %.\n\n", func_var.children.count, arg_list.children.count);
} }
} }
} }
@@ -543,7 +542,7 @@ type_mismatch :: (checker : *Semantic_Checker, usage_site : *AST_Node, expect_no
if got_var.builtin { if got_var.builtin {
print_to_builder(*builder, "% :: (", got_var.name); print_to_builder(*builder, "% :: (", got_var.name);
for i: 0..got_var.child_count - 1{ for i: 0..got_var.children.count - 1{
child_handle := got_var.children[i]; child_handle := got_var.children[i];
child := h2tv(checker, child_handle); child := h2tv(checker, child_handle);
@@ -716,16 +715,16 @@ new_type_variable :: (checker : *Semantic_Checker) -> *Type_Variable, Type_Varia
} }
add_child :: (variable : *Type_Variable, child : Type_Variable_Handle) { add_child :: (variable : *Type_Variable, child : Type_Variable_Handle) {
assert(variable.child_count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN); assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
variable.children[variable.child_count] = child; array_add(*variable.children, child);
variable.child_count += 1; // variable.children[variable.children.count] = child;
// variable.children.count += 1;
} }
add_child :: (checker : *Semantic_Checker, handle : Type_Variable_Handle, child : Type_Variable_Handle) { add_child :: (checker : *Semantic_Checker, handle : Type_Variable_Handle, child : Type_Variable_Handle) {
variable := h2tv(checker, handle); variable := h2tv(checker, handle);
assert(variable.child_count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN); assert(variable.children.count < Type_Variable.MAX_TYPE_VARIABLE_CHILDREN);
variable.children[variable.child_count] = child; array_add(*variable.children, child);
variable.child_count += 1;
} }
init_semantic_checker :: (checker : *Semantic_Checker, root : *AST_Node, path : string) { init_semantic_checker :: (checker : *Semantic_Checker, root : *AST_Node, path : string) {
@@ -1028,7 +1027,7 @@ declare_function :: (checker : *Semantic_Checker, node : *AST_Node, builtin : bo
} }
all_same : bool = true; all_same : bool = true;
for i : 0..func_var.child_count - 1 { for i : 0..func_var.children.count - 1 {
arg := func_var.children[i]; arg := func_var.children[i];
node_child := field_list.children[i]; node_child := field_list.children[i];
@@ -1128,7 +1127,7 @@ create_function_constraint :: (checker : *Semantic_Checker, node : *AST_Node) {
constraint.kind = .Function_Decl; constraint.kind = .Function_Decl;
constraint.function.symbol_variable = function.type_variable; constraint.function.symbol_variable = function.type_variable;
for i : 0..variable.child_count - 1 { for i : 0..variable.children.count - 1 {
arg_var := variable.children[i]; arg_var := variable.children[i];
if arg_var > 0 { if arg_var > 0 {
@@ -1378,11 +1377,11 @@ create_call_constraint :: (checker : *Semantic_Checker, node : *AST_Node, type_v
function := h2tv(checker, func.type_variable); function := h2tv(checker, func.type_variable);
if arg_count != function.child_count { if arg_count != function.children.count {
continue; continue;
} }
if node.children.count == 0 && function.child_count == 0 { if node.children.count == 0 && function.children.count == 0 {
overload_found = true; overload_found = true;
break; break;
} }
@@ -1392,7 +1391,7 @@ create_call_constraint :: (checker : *Semantic_Checker, node : *AST_Node, type_v
for arg : arg_vars { for arg : arg_vars {
function_param := function.children[it_index]; function_param := function.children[it_index];
if !types_compatible(checker, arg.var, function_param) { if !types_compatible(checker, arg.var, function_param, true) {
if all_args_match { if all_args_match {
arg_node = arg.node; arg_node = arg.node;
} }
@@ -1572,7 +1571,7 @@ Unification_Result :: enum {
Unification_Failure; Unification_Failure;
} }
types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rhs : Type_Variable_Handle) -> bool { types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rhs : Type_Variable_Handle, param_matching : bool = false) -> bool {
lhs_var := h2tv(checker, lhs); lhs_var := h2tv(checker, lhs);
rhs_var := h2tv(checker, rhs); rhs_var := h2tv(checker, rhs);
@@ -1581,6 +1580,7 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
case .Half; #through; case .Half; #through;
case .Float; #through; case .Float; #through;
case .Double; { case .Double; {
if !param_matching {
if rhs_var.type == .Struct { if rhs_var.type == .Struct {
if rhs_var.typename == { if rhs_var.typename == {
case "float2"; #through; case "float2"; #through;
@@ -1590,6 +1590,7 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
} }
} }
} }
}
return rhs_var.type == .Int || rhs_var.type == .Half || return rhs_var.type == .Int || rhs_var.type == .Half ||
rhs_var.type == .Float || rhs_var.type == .Double; rhs_var.type == .Float || rhs_var.type == .Double;
} }
@@ -1629,11 +1630,11 @@ types_compatible :: (checker : *Semantic_Checker, lhs : Type_Variable_Handle, rh
lhs_struct_var := h2tv(checker, lhs_struct.type_variable); lhs_struct_var := h2tv(checker, lhs_struct.type_variable);
rhs_struct_var := h2tv(checker, rhs_struct.type_variable); rhs_struct_var := h2tv(checker, rhs_struct.type_variable);
if lhs_struct_var.child_count != rhs_struct_var.child_count { if lhs_struct_var.children.count != rhs_struct_var.children.count {
return false; return false;
} }
for i : 0..lhs_struct_var.child_count - 1 { for i : 0..lhs_struct_var.children.count - 1 {
lhs_child := lhs_struct_var.children[i]; lhs_child := lhs_struct_var.children[i];
rhs_child := rhs_struct_var.children[i]; rhs_child := rhs_struct_var.children[i];
if !types_compatible(checker, lhs_child, rhs_child) { if !types_compatible(checker, lhs_child, rhs_child) {
@@ -1915,14 +1916,14 @@ pretty_print_struct :: (checker : *Semantic_Checker, builder : *String_Builder,
print_key(checker, builder, name); print_key(checker, builder, name);
append(builder, "{"); append(builder, "{");
for 0..struct_type.child_count - 1 { for 0..struct_type.children.count - 1 {
child_handle := struct_type.children[it]; child_handle := struct_type.children[it];
child := h2tv(checker, child_handle); child := h2tv(checker, child_handle);
print_to_builder(builder, child.name); print_to_builder(builder, child.name);
append(builder, " : "); append(builder, " : ");
print_to_builder(builder, type_to_string(child)); print_to_builder(builder, type_to_string(child));
if it < struct_type.child_count - 1 { if it < struct_type.children.count - 1 {
append(builder, ", "); append(builder, ", ");
} }
} }
@@ -2186,14 +2187,14 @@ pretty_print_type_variable :: (checker : *Semantic_Checker, type_variable : *Typ
if type_variable.kind == .Declaration { if type_variable.kind == .Declaration {
append(builder, "{"); append(builder, "{");
for 0..type_variable.child_count - 1 { for 0..type_variable.children.count - 1 {
child_handle := type_variable.children[it]; child_handle := type_variable.children[it];
child := h2tv(checker, child_handle); child := h2tv(checker, child_handle);
print_to_builder(builder, child.name); print_to_builder(builder, child.name);
append(builder, " : "); append(builder, " : ");
print_to_builder(builder, type_to_string(child)); print_to_builder(builder, type_to_string(child));
if it < type_variable.child_count - 1 { if it < type_variable.children.count - 1 {
append(builder, ", "); append(builder, ", ");
} }
} }

View File

@@ -310,8 +310,8 @@ run_semantic_analysis_test :: (file_path : string, root : *AST_Node, output_type
result_text = pretty_print_symbol_table(*checker, temp); result_text = pretty_print_symbol_table(*checker, temp);
constraints := pretty_print_type_constraints(*checker, temp); constraints := pretty_print_type_constraints(*checker, temp);
type_vars := pretty_print_type_variables(*checker, temp); type_vars := pretty_print_type_variables(*checker, temp);
print("Constraints\n%\n", constraints); // print("Constraints\n%\n", constraints);
print("Solution\n%\n", type_vars); // print("Solution\n%\n", type_vars);
} }
if output_type & .StdOut { if output_type & .StdOut {
@@ -477,7 +477,23 @@ run_test :: (file_path : string, stage_flags : Stage_Flags, results : *[..]Resul
} }
run_test :: (test_case : Test_Case, results : *[..]Result, output_type : Output_Type = 0) { run_test :: (test_case : Test_Case, results : *[..]Result, output_type : Output_Type = 0) {
print("%Running test: %\n", cyan(), test_case.path); print("%Running test: %......", cyan(), test_case.path);
// path 30
// len 35
// == 5
// path 20
// len = 35
// == 15
len := 50;
rest := len - test_case.path.count;
for i: 0..rest {
print(" ");
}
run_test(test_case.path, test_case.stage_flags, results, output_type); run_test(test_case.path, test_case.stage_flags, results, output_type);
} }
@@ -525,7 +541,7 @@ run_test_suite :: (using suite : *Test_Suite, output_type : Output_Type = 0) {
} }
} }
print("\n"); // print("\n");
} }
print("\n"); print("\n");
@@ -621,25 +637,25 @@ evaluate_result :: (result : Result) {
if #complete result.type == { if #complete result.type == {
case .File_Read_Failed; { case .File_Read_Failed; {
print(" %", red()); print(" %", red());
print("% failed with File_Read_Failed\n", result.path); print("failed with File_Read_Failed\n");
} }
case .Golden_File_Not_Found; { case .Golden_File_Not_Found; {
print(" %", red()); print(" %", red());
print("% failed with Golden File Not Found for stage %\n", result.path, stage); print("failed with Golden File Not Found for stage %\n", stage);
} }
case .StdOut; { case .StdOut; {
} }
case .Golden_Output; { case .Golden_Output; {
print(" %", yellow()); print(" %", yellow());
print("% output new golden file at %\n", result.path, result.golden_path); print("output new golden file at %\n", result.golden_path);
} }
case .Passed; { case .Passed; {
print(" %", green()); print(" %", green());
print("% passed %\n", result.path, stage); print("passed %\n", stage);
} }
case .Failed; { case .Failed; {
print(" %", red()); print(" %", red());
print("% failed %\n", result.path, stage); print("failed %\n", stage);
} }
} }

View File

@@ -56,6 +56,7 @@ Hint_Kind :: enum {
None; None;
Position; Position;
UV;
Target; Target;
Custom; Custom;
@@ -319,7 +320,7 @@ type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variabl
type_var := h2tv(checker, find_result.type_variable); type_var := h2tv(checker, find_result.type_variable);
for i : 0..type_var.child_count - 1 { for i : 0..type_var.children.count - 1 {
child := type_var.children[i]; child := type_var.children[i];
child_field := type_variable_to_field(checker, h2tv(checker, child)); child_field := type_variable_to_field(checker, h2tv(checker, child));
array_add(*type.children, child_field); array_add(*type.children, child_field);
@@ -335,6 +336,8 @@ type_variable_to_field :: (checker : *Semantic_Checker, variable : *Type_Variabl
if hint.ident_value == "position" { if hint.ident_value == "position" {
// @Incomplete(nb): Should be a lookup table somewhere // @Incomplete(nb): Should be a lookup table somewhere
field_hint.kind = .Position; field_hint.kind = .Position;
} else if hint.ident_value == "uv" {
field_hint.kind = .UV;
} else if starts_with(hint.ident_value, "target") { } else if starts_with(hint.ident_value, "target") {
// @Incomplete(nb): Should be a lookup table somewhere // @Incomplete(nb): Should be a lookup table somewhere
index_str : string; index_str : string;
@@ -453,7 +456,7 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
cb := array_add(*result.collection.cbuffers); cb := array_add(*result.collection.cbuffers);
for i : 0..variable.child_count - 1 { for i : 0..variable.children.count - 1 {
child := variable.children[i]; child := variable.children[i];
field : Property_Field; field : Property_Field;
field.base_field = type_variable_to_field(*checker, h2tv(*checker, child));; field.base_field = type_variable_to_field(*checker, h2tv(*checker, child));;
@@ -467,7 +470,7 @@ compile_file :: (compiler : *Shader_Compiler, path : string) -> Compilation_Resu
if find_result { if find_result {
property_variable := h2tv(check_result.type_variables, find_result.type_variable); property_variable := h2tv(check_result.type_variables, find_result.type_variable);
for i : 0..property_variable.child_count - 1 { for i : 0..property_variable.children.count - 1 {
child := property_variable.children[i]; child := property_variable.children[i];
field := type_variable_to_field(*checker, h2tv(*checker, child)); field := type_variable_to_field(*checker, h2tv(*checker, child));
prop_field : Property_Field; prop_field : Property_Field;

View File

@@ -1,5 +1,5 @@
void vs_main() void vs_main()
{ {
float x = 2.0f + 5.0f; float x = (2.0f + 5.0f);
} }

View File

@@ -1,6 +1,6 @@
cbuffer __PROPERTIES : register(b0) cbuffer __PROPERTIES : register(b0)
{ {
float4 color; float4 __PROPERTIES__color;
} }
@@ -11,6 +11,6 @@ float3 vs_main(float3 pos : POSITION) : SV_POSITION
float4 ps_main() : SV_TARGET float4 ps_main() : SV_TARGET
{ {
return color; return __PROPERTIES__color;
} }

View File

@@ -2,6 +2,6 @@ void vs_main()
{ {
float x = 5.0f; float x = 5.0f;
float y = 3000.0f; float y = 3000.0f;
float z = y * y + x; float z = ((y * y) + x);
} }

View File

@@ -1,4 +1,2 @@
struct Foo; struct Foo {};
struct Foo {}

View File

@@ -1,16 +1,16 @@
cbuffer __PROPERTIES : register(b0) cbuffer __PROPERTIES : register(b0)
{ {
float4 color; float4 __PROPERTIES__color;
} }
float3 vs_main(float3 pos : POSITION, float2 uv) : SV_POSITION float3 vs_main(float3 pos : POSITION, float2 uv : TEXCOORD0) : SV_POSITION
{ {
return pos; return pos;
} }
float4 ps_main() : SV_TARGET float4 ps_main() : SV_TARGET
{ {
return color; return __PROPERTIES__color;
} }

View File

@@ -8,7 +8,7 @@ int foo()
float bar() float bar()
{ {
return 1235.0f * 500; return (1235.0f * 500);
} }
void vs_main() void vs_main()

View File

@@ -1,15 +1,13 @@
struct Foo;
float foo(Foo f); float foo(Foo f);
struct Foo struct Foo
{ {
float some_data; float some_data;
} };
float foo(Foo f) float foo(Foo f)
{ {
return f.some_data * 2.0f; return (f.some_data * 2.0f);
} }
void vs_main() void vs_main()

View File

@@ -1,9 +1,7 @@
struct Data;
struct Data struct Data
{ {
float4 color; float4 color;
} };
void vs_main() void vs_main()
{ {

View File

@@ -1,15 +1,12 @@
struct Foo;
struct Bar;
struct Foo struct Foo
{ {
float4 color; float4 color;
} };
struct Bar struct Bar
{ {
Foo t; Foo t;
} };
void vs_main() void vs_main()
{ {

7
test/double_access.shd Normal file
View File

@@ -0,0 +1,7 @@
p :: properties {
v : float2;
}
vertex main ::() {
x : float = p.v.x / p.v.y;
}

View File

@@ -6,8 +6,6 @@ test/empty_struct.shd semant
test/empty_vertex_main.shd semant test/empty_vertex_main.shd semant
test/empty_vertex_main_with_position_parameter.shd semant test/empty_vertex_main_with_position_parameter.shd semant
test/field_assignment.shd semant test/field_assignment.shd semant
test/field_without_type_specifier.shd semant
test/float_suffix.shd semant
test/function_call.shd semant test/function_call.shd semant
test/function_call_out_of_order_declaration.shd semant test/function_call_out_of_order_declaration.shd semant
test/function_call_return.shd semant test/function_call_return.shd semant