Skip to content

Commit

Permalink
Improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 12, 2024
1 parent b4ce8d5 commit 0f2e011
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 33 deletions.
5 changes: 5 additions & 0 deletions Sources/backends/cstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %f;\n", type_string(o->op_load_float_constant.to.type.type), o->op_load_float_constant.to.index,
o->op_load_float_constant.number);
break;
case OPCODE_LOAD_INT_CONSTANT:
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %i;\n", type_string(o->op_load_int_constant.to.type.type), o->op_load_int_constant.to.index,
o->op_load_int_constant.number);
break;
case OPCODE_LOAD_BOOL_CONSTANT:
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %s;\n", type_string(o->op_load_bool_constant.to.type.type), o->op_load_bool_constant.to.index,
Expand Down
2 changes: 1 addition & 1 deletion Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ static void write_functions(char *hlsl, size_t *offset, shader_stage stage, func
break;
}
case OPCODE_MULTIPLY: {
if (o->op_binary.left.type.type == float4x4_id) {
if (o->op_binary.left.type.type == float4x4_id || o->op_binary.left.type.type == float3x3_id) {
indent(hlsl, offset, indentation);
*offset += sprintf(&hlsl[*offset], "%s _%" PRIu64 " = mul(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.right.index, o->op_binary.left.index);
Expand Down
4 changes: 2 additions & 2 deletions Sources/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) {
case OPERATOR_MULTIPLY: {
variable right_var = emit_expression(code, parent, right);
variable left_var = emit_expression(code, parent, left);
variable result_var = allocate_variable(right_var.type, VARIABLE_LOCAL);
variable result_var = allocate_variable(e->type, VARIABLE_LOCAL);

opcode o;
switch (e->binary.op) {
Expand Down Expand Up @@ -443,7 +443,7 @@ variable emit_expression(opcodes *code, block *parent, expression *e) {
case EXPRESSION_INT: {
type_ref t;
init_type_ref(&t, NO_NAME);
t.type = float_id;
t.type = int_id;
variable v = allocate_variable(t, VARIABLE_LOCAL);

opcode o;
Expand Down
192 changes: 165 additions & 27 deletions Sources/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ static function *functions = NULL;
static function_id functions_size = 1024;
static function_id next_function_index = 0;

function_id sample_id;
function_id float2_constructor_id;
function_id float3_constructor_id;
function_id float4_constructor_id;

static void add_func_int(char *name) {
function_id func = add_function(add_name(name));
function *f = get_function(func);
Expand Down Expand Up @@ -70,6 +65,18 @@ static void add_func_float_float(char *name) {
f->block = NULL;
}

static void add_func_float_float2(char *name) {
function_id func = add_function(add_name(name));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("a");
init_type_ref(&f->parameter_types[0], add_name("float2"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);
f->parameters_size = 1;
f->block = NULL;
}

static void add_func_float3_float3(char *name) {
function_id func = add_function(add_name(name));
function *f = get_function(func);
Expand Down Expand Up @@ -108,8 +115,8 @@ void functions_init(void) {
next_function_index = 0;

{
sample_id = add_function(add_name("sample"));
function *f = get_function(sample_id);
function_id func = add_function(add_name("sample"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float4"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("tex_coord");
Expand All @@ -120,8 +127,8 @@ void functions_init(void) {
}

{
sample_id = add_function(add_name("sample_lod"));
function *f = get_function(sample_id);
function_id func = add_function(add_name("sample_lod"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float4"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("tex_coord");
Expand All @@ -132,8 +139,22 @@ void functions_init(void) {
}

{
float2_constructor_id = add_function(add_name("float2"));
function *f = get_function(float2_constructor_id);
function_id func = add_function(add_name("float"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("float"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameters_size = 1;

f->block = NULL;
}

{
function_id func = add_function(add_name("float2"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float2"));
f->return_type.type = find_type_by_ref(&f->return_type);
f->parameter_names[0] = add_name("x");
Expand All @@ -150,8 +171,8 @@ void functions_init(void) {
}

{
float3_constructor_id = add_function(add_name("float3"));
function *f = get_function(float3_constructor_id);
function_id func = add_function(add_name("float3"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float3"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -173,8 +194,8 @@ void functions_init(void) {
}

{
float4_constructor_id = add_function(add_name("float4"));
function *f = get_function(float4_constructor_id);
function_id func = add_function(add_name("float4"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("float4"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -200,8 +221,23 @@ void functions_init(void) {
}

{
float2_constructor_id = add_function(add_name("int2"));
function *f = get_function(float2_constructor_id);
function_id func = add_function(add_name("int"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("int"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("int"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameters_size = 1;

f->block = NULL;
}

{
function_id func = add_function(add_name("int2"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("int2"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -219,8 +255,8 @@ void functions_init(void) {
}

{
float3_constructor_id = add_function(add_name("int3"));
function *f = get_function(float3_constructor_id);
function_id func = add_function(add_name("int3"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("int3"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -242,8 +278,8 @@ void functions_init(void) {
}

{
float4_constructor_id = add_function(add_name("int4"));
function *f = get_function(float4_constructor_id);
function_id func = add_function(add_name("int4"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("int4"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -269,8 +305,23 @@ void functions_init(void) {
}

{
float2_constructor_id = add_function(add_name("uint2"));
function *f = get_function(float2_constructor_id);
function_id func = add_function(add_name("uint"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("uint"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("uint"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameters_size = 1;

f->block = NULL;
}

{
function_id func = add_function(add_name("uint2"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("uint2"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -288,8 +339,8 @@ void functions_init(void) {
}

{
float3_constructor_id = add_function(add_name("uint3"));
function *f = get_function(float3_constructor_id);
function_id func = add_function(add_name("uint3"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("uint3"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -311,8 +362,8 @@ void functions_init(void) {
}

{
float4_constructor_id = add_function(add_name("uint4"));
function *f = get_function(float4_constructor_id);
function_id func = add_function(add_name("uint4"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("uint4"));
f->return_type.type = find_type_by_ref(&f->return_type);

Expand All @@ -337,6 +388,90 @@ void functions_init(void) {
f->block = NULL;
}

{
function_id func = add_function(add_name("bool"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("bool"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("bool"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameters_size = 1;

f->block = NULL;
}

{
function_id func = add_function(add_name("bool2"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("bool2"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("bool"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameter_names[1] = add_name("y");
init_type_ref(&f->parameter_types[1], add_name("bool"));
f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]);

f->parameters_size = 2;

f->block = NULL;
}

{
function_id func = add_function(add_name("bool3"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("bool3"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("bool"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameter_names[1] = add_name("y");
init_type_ref(&f->parameter_types[1], add_name("bool"));
f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]);

f->parameter_names[2] = add_name("z");
init_type_ref(&f->parameter_types[2], add_name("bool"));
f->parameter_types[2].type = find_type_by_ref(&f->parameter_types[2]);

f->parameters_size = 3;

f->block = NULL;
}

{
function_id func = add_function(add_name("bool4"));
function *f = get_function(func);
init_type_ref(&f->return_type, add_name("bool4"));
f->return_type.type = find_type_by_ref(&f->return_type);

f->parameter_names[0] = add_name("x");
init_type_ref(&f->parameter_types[0], add_name("bool"));
f->parameter_types[0].type = find_type_by_ref(&f->parameter_types[0]);

f->parameter_names[1] = add_name("y");
init_type_ref(&f->parameter_types[1], add_name("bool"));
f->parameter_types[1].type = find_type_by_ref(&f->parameter_types[1]);

f->parameter_names[2] = add_name("z");
init_type_ref(&f->parameter_types[2], add_name("bool"));
f->parameter_types[2].type = find_type_by_ref(&f->parameter_types[2]);

f->parameter_names[3] = add_name("w");
init_type_ref(&f->parameter_types[3], add_name("bool"));
f->parameter_types[3].type = find_type_by_ref(&f->parameter_types[3]);

f->parameters_size = 4;

f->block = NULL;
}

{
function_id func = add_function(add_name("trace_ray"));
function *f = get_function(func);
Expand Down Expand Up @@ -401,6 +536,9 @@ void functions_init(void) {
add_func_float3("world_ray_direction");
add_func_float3_float3("normalize");
add_func_float_float("saturate");
add_func_float_float("sin");
add_func_float_float("cos");
add_func_float_float2("length");
add_func_uint3("ray_index");
add_func_float3("ray_dimensions");

Expand Down
2 changes: 0 additions & 2 deletions Sources/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ function_id add_function(name_id name);
function_id find_function(name_id name);

function *get_function(function_id function);

extern function_id sample_id;
5 changes: 4 additions & 1 deletion Sources/integrations/kope.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ static uint32_t base_type_size(type_id type) {
if (type == float4x4_id) {
return 4 * 4 * 4;
}
if (type == float3x3_id) {
return 3 * 4 * 4;
}

debug_context context = {0};
error(context, "Unknown type for structure");
error(context, "Unknown type %s for structure", get_name(get_type(type)->name));
return 1;
}

Expand Down
1 change: 1 addition & 0 deletions Sources/kong.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ void resolve_types_in_expression(statement *parent, expression *e) {

if (e->type.type == NO_TYPE) {
debug_context context = {0};
const char *n = get_name(e->call.func_name);
error(context, "Could not resolve type");
}
}
Expand Down

0 comments on commit 0f2e011

Please sign in to comment.