From 1572ed57b669ed06d4ad7513bc51f70ba0952c16 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:40:36 +0100 Subject: [PATCH 1/7] Add `intrinsics.concatenate` --- src/check_builtin.cpp | 86 +++++++++++++++++++++++++++++++++++ src/checker_builtin_procs.hpp | 4 ++ 2 files changed, 90 insertions(+) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index a19fb15ec..13a1c4e69 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4885,6 +4885,92 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As break; } + case BuiltinProc_concatenate: { + Operand lhs = {}; + Operand rhs = {}; + + check_expr(c, &lhs, ce->args[0]); + if (lhs.mode == Addressing_Invalid) { + return false; + } + check_expr(c, &rhs, ce->args[1]); + if (rhs.mode == Addressing_Invalid) { + return false; + } + if (lhs.mode != Addressing_Constant) { + error(lhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + if (rhs.mode != Addressing_Constant) { + error(rhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + + if (!are_types_identical(lhs.type, rhs.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(rhs.type); + error(rhs.expr, "'%*.s' expects a two constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + + if (!is_type_slice(lhs.type) && !is_type_array(lhs.type)) { + gbString a = type_to_string(lhs.type); + error(lhs.expr, "'%*.s' expects a constant array or slice, got %s", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + if (lhs.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(lhs.value); + error(lhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + if (rhs.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(rhs.value); + error(rhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + ast_node(lhs_cl, CompoundLit, lhs.value.value_compound); + ast_node(rhs_cl, CompoundLit, rhs.value.value_compound); + + for (Ast *elem : lhs_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + for (Ast *elem : rhs_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + + Ast *type_ast = lhs_cl->type; + if (type_ast == nullptr) { + type_ast = rhs_cl->type; + } + + + Array new_elems = {}; + array_init(&new_elems, heap_allocator()); + + array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); + array_add_elems(&new_elems, rhs_cl->elems.data, rhs_cl->elems.count); + + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(rhs.expr)); + + operand->mode = Addressing_Constant; + operand->value = exact_value_compound(new_compound_lit); + operand->type = lhs.type; + break; + } + case BuiltinProc_alloca: { Operand sz = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index addaeaf23..8de6a4f5e 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -56,6 +56,8 @@ enum BuiltinProcId { BuiltinProc_soa_struct, + BuiltinProc_concatenate, + BuiltinProc_alloca, BuiltinProc_cpu_relax, BuiltinProc_trap, @@ -427,6 +429,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("soa_struct"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type + {STR_LIT("concatenate"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, From 061b14e1649ea5829e2612dc905cdc9af863a071 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:48:18 +0100 Subject: [PATCH 2/7] Allow `intrinsics.concatenate` to be variadic --- src/check_builtin.cpp | 87 +++++++++++++++++++---------------- src/checker_builtin_procs.hpp | 2 +- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 13a1c4e69..c337802c9 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4887,56 +4887,32 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_concatenate: { Operand lhs = {}; - Operand rhs = {}; check_expr(c, &lhs, ce->args[0]); if (lhs.mode == Addressing_Invalid) { return false; } - check_expr(c, &rhs, ce->args[1]); - if (rhs.mode == Addressing_Invalid) { - return false; - } if (lhs.mode != Addressing_Constant) { - error(lhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); - return false; - } - if (rhs.mode != Addressing_Constant) { - error(rhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); - return false; - } - - if (!are_types_identical(lhs.type, rhs.type)) { - gbString a = type_to_string(lhs.type); - gbString b = type_to_string(rhs.type); - error(rhs.expr, "'%*.s' expects a two constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); - gb_string_free(b); - gb_string_free(a); + error(lhs.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); return false; } + operand->type = lhs.type; + operand->mode = Addressing_Value; if (!is_type_slice(lhs.type) && !is_type_array(lhs.type)) { gbString a = type_to_string(lhs.type); - error(lhs.expr, "'%*.s' expects a constant array or slice, got %s", LIT(builtin_name), a); + error(lhs.expr, "'%.*s' expects a constant array or slice, got %s", LIT(builtin_name), a); gb_string_free(a); return false; } - if (lhs.value.kind != ExactValue_Compound) { gbString a = exact_value_to_string(lhs.value); error(lhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); gb_string_free(a); return false; } - if (rhs.value.kind != ExactValue_Compound) { - gbString a = exact_value_to_string(rhs.value); - error(rhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); - gb_string_free(a); - return false; - } ast_node(lhs_cl, CompoundLit, lhs.value.value_compound); - ast_node(rhs_cl, CompoundLit, rhs.value.value_compound); for (Ast *elem : lhs_cl->elems) { if (elem->kind == Ast_FieldValue) { @@ -4944,26 +4920,57 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As return false; } } - for (Ast *elem : rhs_cl->elems) { - if (elem->kind == Ast_FieldValue) { - error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); - return false; - } - } Ast *type_ast = lhs_cl->type; - if (type_ast == nullptr) { - type_ast = rhs_cl->type; - } - Array new_elems = {}; array_init(&new_elems, heap_allocator()); array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); - array_add_elems(&new_elems, rhs_cl->elems.data, rhs_cl->elems.count); - Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(rhs.expr)); + for (isize i = 1; i < ce->args.count; i++) { + Operand extra = {}; + check_expr(c, &extra, ce->args[i]); + if (extra.mode == Addressing_Invalid) { + return false; + } + if (extra.mode != Addressing_Constant) { + error(extra.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + if (!are_types_identical(lhs.type, extra.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + + if (extra.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(extra.value); + error(extra.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + ast_node(extra_cl, CompoundLit, extra.value.value_compound); + + + for (Ast *elem : extra_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + + if (type_ast == nullptr) { + type_ast = extra_cl->type; + } + array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); + } + + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 8de6a4f5e..01502128a 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -429,7 +429,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("soa_struct"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type - {STR_LIT("concatenate"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("concatenate"), 2, true, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, From 5e12532ebad58215cc6039dc77211652961dc1a9 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:49:09 +0100 Subject: [PATCH 3/7] Add basic type inference to the arguments --- src/check_builtin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index c337802c9..a4109601e 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4888,7 +4888,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_concatenate: { Operand lhs = {}; - check_expr(c, &lhs, ce->args[0]); + check_expr_with_type_hint(c, &lhs, ce->args[0], type_hint); if (lhs.mode == Addressing_Invalid) { return false; } @@ -4930,7 +4930,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; - check_expr(c, &extra, ce->args[i]); + check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); if (extra.mode == Addressing_Invalid) { return false; } From 24bc044d78c5d02e174b15490a0f8f5289b0cb03 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:00:44 +0100 Subject: [PATCH 4/7] Support fixed-length arrays for `intrinsics.concatenate` --- src/check_builtin.cpp | 50 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index a4109601e..fb7b29edd 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4921,6 +4921,8 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } } + Type *elem_type = base_any_array_type(lhs.type); + Ast *type_ast = lhs_cl->type; Array new_elems = {}; @@ -4930,7 +4932,11 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; - check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); + if (is_type_slice(lhs.type)) { + check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); + } else { + check_expr(c, &extra, ce->args[i]); + } if (extra.mode == Addressing_Invalid) { return false; } @@ -4938,13 +4944,34 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As error(extra.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); return false; } - if (!are_types_identical(lhs.type, extra.type)) { - gbString a = type_to_string(lhs.type); - gbString b = type_to_string(extra.type); - error(extra.expr, "'%.*s' expects constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); - gb_string_free(b); - gb_string_free(a); - return false; + + if (is_type_slice(lhs.type)) { + if (!are_types_identical(lhs.type, extra.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects constant values of the same slice type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + } else if (is_type_array(lhs.type)) { + if (!is_type_array(extra.type)) { + gbString a = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects a constant array or slice, got %s", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + Type *extra_elem_type = base_array_type(extra.type); + if (!are_types_identical(elem_type, extra_elem_type)) { + gbString a = type_to_string(elem_type); + gbString b = type_to_string(extra_elem_type); + error(extra.expr, "'%.*s' expects constant values of the same element-type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + } else { + GB_PANIC("Unhandled type: %s", type_to_string(lhs.type)); } if (extra.value.kind != ExactValue_Compound) { @@ -4974,7 +5001,12 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); - operand->type = lhs.type; + + if (is_type_slice(lhs.type)) { + operand->type = lhs.type; + } else { + operand->type = alloc_type_array(elem_type, new_elems.count); + } break; } From 1387c3d311db07edb36d8db5d0a2befb8417b771 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:04:22 +0100 Subject: [PATCH 5/7] Remove unneeded type expression --- src/check_builtin.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index fb7b29edd..fcd2200dc 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4923,8 +4923,6 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As Type *elem_type = base_any_array_type(lhs.type); - Ast *type_ast = lhs_cl->type; - Array new_elems = {}; array_init(&new_elems, heap_allocator()); @@ -4991,13 +4989,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } } - if (type_ast == nullptr) { - type_ast = extra_cl->type; - } array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); } - Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), nullptr, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); From 26b3a4d182788a08ea1d9eb8d7c3b49235065cb6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:09:41 +0100 Subject: [PATCH 6/7] Handle concatenation at the end --- src/check_builtin.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index fcd2200dc..530efb3c8 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4923,11 +4923,6 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As Type *elem_type = base_any_array_type(lhs.type); - Array new_elems = {}; - array_init(&new_elems, heap_allocator()); - - array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); - for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; if (is_type_slice(lhs.type)) { @@ -4988,8 +4983,25 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As return false; } } + } - array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); + isize count_needed = 0; + + for (Ast *arg : ce->args) { + ExactValue value = arg->tav.value; + GB_ASSERT(value.kind == ExactValue_Compound); + ast_node(cl, CompoundLit, value.value_compound); + count_needed += cl->elems.count; + } + + Array new_elems = {}; + array_init(&new_elems, permanent_allocator(), 0, count_needed); + + for (Ast *arg : ce->args) { + ExactValue value = arg->tav.value; + GB_ASSERT(value.kind == ExactValue_Compound); + ast_node(cl, CompoundLit, value.value_compound); + array_add_elems(&new_elems, cl->elems.data, cl->elems.count); } Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), nullptr, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); From 98dac324e92d950ab9374de9c56169f7396246a4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:13:49 +0100 Subject: [PATCH 7/7] Add to `intrinsics.odin` --- base/intrinsics/intrinsics.odin | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/intrinsics/intrinsics.odin b/base/intrinsics/intrinsics.odin index 805d78594..952f927bd 100644 --- a/base/intrinsics/intrinsics.odin +++ b/base/intrinsics/intrinsics.odin @@ -348,6 +348,9 @@ simd_lanes_rotate_right :: proc(a: #simd[N]T, $offset: int) -> #simd[N]T --- has_target_feature :: proc($test: $T) -> bool where type_is_string(T) || type_is_proc(T) --- +// Utility Calls +concatentate :: proc(x, y: $T, z: ..T) -> T where type_is_array(T) || type_is_slice(T) --- + // Returns the value of the procedure where `x` must be a call expression procedure_of :: proc(x: $T) -> T where type_is_proc(T) ---