mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 04:50:29 +00:00
Add intrinsics.type_hasher_proc; Make map work with generic hasher procedure
This commit is contained in:
@@ -154,4 +154,5 @@ type_polymorphic_record_parameter_value :: proc($T: typeid, index: int) -> $V --
|
||||
|
||||
type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---
|
||||
|
||||
type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, rawptr) -> bool) ---
|
||||
type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, rawptr) -> bool) ---
|
||||
type_hasher_proc :: proc($T: typeid) -> (hasher: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr) ---
|
||||
|
||||
@@ -42,7 +42,8 @@ Platform_Endianness :: enum u8 {
|
||||
Big = 2,
|
||||
}
|
||||
|
||||
Equal_Proc :: distinct proc "contextless" (rawptr, rawptr) -> bool;
|
||||
Equal_Proc :: distinct proc "contextless" (rawptr, rawptr) -> bool;
|
||||
Hasher_Proc :: distinct proc "contextless" (data: rawptr, seed: uintptr) -> uintptr;
|
||||
|
||||
Type_Info_Struct_Soa_Kind :: enum u8 {
|
||||
None = 0,
|
||||
@@ -125,6 +126,8 @@ Type_Info_Map :: struct {
|
||||
key: ^Type_Info,
|
||||
value: ^Type_Info,
|
||||
generated_struct: ^Type_Info,
|
||||
key_equal: Equal_Proc,
|
||||
key_hasher: Hasher_Proc,
|
||||
};
|
||||
Type_Info_Bit_Field :: struct {
|
||||
names: []string,
|
||||
|
||||
@@ -11,31 +11,11 @@ Map_Hash :: struct {
|
||||
key_ptr: rawptr, // address of Map_Entry_Header.key
|
||||
}
|
||||
|
||||
__get_map_hash :: proc "contextless" (k: ^$K) -> Map_Hash {
|
||||
key := k;
|
||||
map_hash: Map_Hash;
|
||||
|
||||
T :: intrinsics.type_core_type(K);
|
||||
|
||||
__get_map_hash :: proc "contextless" (k: ^$K) -> (map_hash: Map_Hash) {
|
||||
hasher := intrinsics.type_hasher_proc(K);
|
||||
map_hash.key_ptr = k;
|
||||
|
||||
when intrinsics.type_is_integer(T) {
|
||||
map_hash.hash = default_hash_ptr(key, size_of(T));
|
||||
} else when intrinsics.type_is_rune(T) {
|
||||
map_hash.hash = default_hash_ptr(key, size_of(T));
|
||||
} else when intrinsics.type_is_pointer(T) {
|
||||
map_hash.hash = default_hash_ptr(key, size_of(T));
|
||||
} else when intrinsics.type_is_float(T) {
|
||||
map_hash.hash = default_hash_ptr(key, size_of(T));
|
||||
} else when intrinsics.type_is_string(T) {
|
||||
#assert(T == string);
|
||||
str := (^string)(key)^;
|
||||
map_hash.hash = default_hash_string(str);
|
||||
} else {
|
||||
#panic("Unhandled map key type");
|
||||
}
|
||||
|
||||
return map_hash;
|
||||
map_hash.hash = hasher(k, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
__get_map_hash_from_entry :: proc "contextless" (h: Map_Header, entry: ^Map_Entry_Header) -> (hash: Map_Hash) {
|
||||
@@ -96,6 +76,42 @@ default_hash_ptr :: inline proc "contextless" (data: rawptr, size: int) -> uintp
|
||||
return default_hash(transmute([]byte)(s));
|
||||
}
|
||||
|
||||
_default_hasher_const :: inline proc "contextless" (data: rawptr, seed: uintptr, $N: uint) -> uintptr {
|
||||
h := u64(seed) + 0xcbf29ce484222325;
|
||||
p := uintptr(data);
|
||||
inline for _ in 0..<N {
|
||||
b := u64((^byte)(p)^);
|
||||
h = (h ~ b) * 0x100000001b3;
|
||||
p += 1;
|
||||
}
|
||||
return uintptr(h);
|
||||
}
|
||||
_default_hasher_n :: inline proc "contextless" (data: rawptr, seed: uintptr, N: int) -> uintptr {
|
||||
h := u64(seed) + 0xcbf29ce484222325;
|
||||
p := uintptr(data);
|
||||
for _ in 0..<N {
|
||||
b := u64((^byte)(p)^);
|
||||
h = (h ~ b) * 0x100000001b3;
|
||||
p += 1;
|
||||
}
|
||||
return uintptr(h);
|
||||
}
|
||||
|
||||
default_hasher1 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 1); }
|
||||
default_hasher2 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 2); }
|
||||
default_hasher4 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 4); }
|
||||
default_hasher8 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 8); }
|
||||
default_hasher16 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 16); }
|
||||
default_hasher_string :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr {
|
||||
h := u64(seed) + 0xcbf29ce484222325;
|
||||
str := (^[]byte)(data)^;
|
||||
for b in str {
|
||||
h = (h ~ u64(b)) * 0x100000001b3;
|
||||
}
|
||||
return uintptr(h);
|
||||
}
|
||||
|
||||
|
||||
|
||||
source_code_location_hash :: proc(s: Source_Code_Location) -> uintptr {
|
||||
hash := _fnv64a(transmute([]byte)s.file_path);
|
||||
|
||||
@@ -91,7 +91,7 @@ Type *type_to_abi_compat_result_type(gbAllocator a, Type *original_type, ProcCal
|
||||
bool abi_compat_return_by_pointer(gbAllocator a, ProcCallingConvention cc, Type *abi_return_type);
|
||||
void set_procedure_abi_types(Type *type);
|
||||
void check_assignment_error_suggestion(CheckerContext *c, Operand *o, Type *type);
|
||||
|
||||
void add_map_key_type_dependencies(CheckerContext *ctx, Type *key);
|
||||
|
||||
Type *make_soa_struct_slice(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
|
||||
Type *make_soa_struct_dynamic_array(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem);
|
||||
@@ -6081,6 +6081,29 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
|
||||
operand->type = t_equal_proc;
|
||||
break;
|
||||
}
|
||||
|
||||
case BuiltinProc_type_hasher_proc:
|
||||
{
|
||||
Operand op = {};
|
||||
Type *bt = check_type(c, ce->args[0]);
|
||||
Type *type = base_type(bt);
|
||||
if (type == nullptr || type == t_invalid) {
|
||||
error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name));
|
||||
return false;
|
||||
}
|
||||
if (!is_type_valid_for_keys(type)) {
|
||||
gbString t = type_to_string(type);
|
||||
error(ce->args[0], "Expected a valid type for map keys for '%.*s', got %s", LIT(builtin_name), t);
|
||||
gb_string_free(t);
|
||||
return false;
|
||||
}
|
||||
|
||||
add_map_key_type_dependencies(c, type);
|
||||
|
||||
operand->mode = Addressing_Value;
|
||||
operand->type = t_hasher_proc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -2848,6 +2848,26 @@ void init_map_internal_types(Type *type) {
|
||||
type->Map.lookup_result_type = make_optional_ok_type(value);
|
||||
}
|
||||
|
||||
void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
|
||||
if (is_type_string(key)) {
|
||||
add_package_dependency(ctx, "runtime", "default_hash_string");
|
||||
add_package_dependency(ctx, "runtime", "default_hasher_string");
|
||||
} else if (!is_type_polymorphic(key)) {
|
||||
add_package_dependency(ctx, "runtime", "default_hash_ptr");
|
||||
GB_ASSERT_MSG(is_type_simple_compare(key), "%s", type_to_string(key));
|
||||
|
||||
i64 sz = type_size_of(key);
|
||||
switch (sz) {
|
||||
case 1: add_package_dependency(ctx, "runtime", "default_hasher1"); break;
|
||||
case 2: add_package_dependency(ctx, "runtime", "default_hasher2"); break;
|
||||
case 4: add_package_dependency(ctx, "runtime", "default_hasher4"); break;
|
||||
case 8: add_package_dependency(ctx, "runtime", "default_hasher8"); break;
|
||||
case 16: add_package_dependency(ctx, "runtime", "default_hasher16"); break;
|
||||
default: GB_PANIC("unhandled hasher for key type: %s", type_to_string(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
|
||||
GB_ASSERT(type->kind == Type_Map);
|
||||
ast_node(mt, MapType, node);
|
||||
@@ -2864,16 +2884,16 @@ void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
|
||||
gb_string_free(str);
|
||||
}
|
||||
}
|
||||
if (type_size_of(key) == 0) {
|
||||
gbString str = type_to_string(key);
|
||||
error(node, "Invalid type of a key for a map of size 0, got '%s'", str);
|
||||
gb_string_free(str);
|
||||
}
|
||||
|
||||
type->Map.key = key;
|
||||
type->Map.value = value;
|
||||
|
||||
if (is_type_string(key)) {
|
||||
add_package_dependency(ctx, "runtime", "default_hash_string");
|
||||
} else {
|
||||
add_package_dependency(ctx, "runtime", "default_hash_ptr");
|
||||
}
|
||||
|
||||
add_map_key_type_dependencies(ctx, key);
|
||||
|
||||
init_core_map_type(ctx->checker);
|
||||
init_map_internal_types(type);
|
||||
|
||||
@@ -729,9 +729,13 @@ void init_universal(void) {
|
||||
{
|
||||
void set_procedure_abi_types(Type *type);
|
||||
|
||||
Type *args[2] = {t_rawptr, t_rawptr};
|
||||
t_equal_proc = alloc_type_proc_from_types(args, 2, t_bool, false, ProcCC_Contextless);
|
||||
Type *equal_args[2] = {t_rawptr, t_rawptr};
|
||||
t_equal_proc = alloc_type_proc_from_types(equal_args, 2, t_bool, false, ProcCC_Contextless);
|
||||
set_procedure_abi_types(t_equal_proc);
|
||||
|
||||
Type *hasher_args[2] = {t_rawptr, t_uintptr};
|
||||
t_hasher_proc = alloc_type_proc_from_types(hasher_args, 2, t_uintptr, false, ProcCC_Contextless);
|
||||
set_procedure_abi_types(t_hasher_proc);
|
||||
}
|
||||
|
||||
// Constants
|
||||
|
||||
@@ -184,6 +184,7 @@ BuiltinProc__type_simple_boolean_end,
|
||||
BuiltinProc_type_field_index_of,
|
||||
|
||||
BuiltinProc_type_equal_proc,
|
||||
BuiltinProc_type_hasher_proc,
|
||||
|
||||
BuiltinProc__type_end,
|
||||
|
||||
@@ -369,7 +370,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
||||
|
||||
{STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
{STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||
|
||||
{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
|
||||
};
|
||||
|
||||
96
src/ir.cpp
96
src/ir.cpp
@@ -24,7 +24,9 @@ struct irModule {
|
||||
Map<String> entity_names; // Key: Entity * of the typename
|
||||
Map<irDebugInfo *> debug_info; // Key: Unique pointer
|
||||
Map<irValue *> anonymous_proc_lits; // Key: Ast *
|
||||
Map<irValue *> compare_procs; // Key: Type *
|
||||
|
||||
Map<irValue *> equal_procs; // Key: Type *
|
||||
Map<irValue *> hasher_procs; // Key: Type *
|
||||
|
||||
irDebugInfo * debug_compile_unit;
|
||||
Array<irDebugInfo *> debug_location_stack;
|
||||
@@ -4875,7 +4877,7 @@ irValue *ir_get_equal_proc_for_type(irModule *m, Type *type) {
|
||||
Type *pt = alloc_type_pointer(type);
|
||||
|
||||
auto key = hash_type(type);
|
||||
irValue **found = map_get(&m->compare_procs, key);
|
||||
irValue **found = map_get(&m->equal_procs, key);
|
||||
if (found) {
|
||||
return *found;
|
||||
}
|
||||
@@ -4883,7 +4885,7 @@ irValue *ir_get_equal_proc_for_type(irModule *m, Type *type) {
|
||||
static u32 proc_index = 0;
|
||||
|
||||
char buf[16] = {};
|
||||
isize n = gb_snprintf(buf, 16, "__$cmp%u", ++proc_index);
|
||||
isize n = gb_snprintf(buf, 16, "__$equal%u", ++proc_index);
|
||||
char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
|
||||
String proc_name = make_string_c(str);
|
||||
|
||||
@@ -4894,6 +4896,7 @@ irValue *ir_get_equal_proc_for_type(irModule *m, Type *type) {
|
||||
irValue *p = ir_value_procedure(m, e, t_equal_proc, nullptr, body, proc_name);
|
||||
map_set(&m->values, hash_entity(e), p);
|
||||
string_map_set(&m->members, proc_name, p);
|
||||
map_set(&m->equal_procs, key, p);
|
||||
|
||||
irProcedure *proc = &p->Proc;
|
||||
proc->is_startup = true;
|
||||
@@ -4957,7 +4960,83 @@ irValue *ir_get_equal_proc_for_type(irModule *m, Type *type) {
|
||||
|
||||
ir_end_procedure_body(proc);
|
||||
|
||||
map_set(&m->compare_procs, key, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
irValue *ir_get_hasher_proc_for_type(irModule *m, Type *type) {
|
||||
Type *original_type = type;
|
||||
type = base_type(type);
|
||||
Type *pt = alloc_type_pointer(type);
|
||||
|
||||
GB_ASSERT(is_type_valid_for_keys(type));
|
||||
|
||||
auto key = hash_type(type);
|
||||
irValue **found = map_get(&m->hasher_procs, key);
|
||||
if (found) {
|
||||
return *found;
|
||||
}
|
||||
|
||||
static u32 proc_index = 0;
|
||||
|
||||
char buf[16] = {};
|
||||
isize n = gb_snprintf(buf, 16, "__$hasher%u", ++proc_index);
|
||||
char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
|
||||
String proc_name = make_string_c(str);
|
||||
|
||||
|
||||
Ast *body = alloc_ast_node(nullptr, Ast_Invalid);
|
||||
Entity *e = alloc_entity_procedure(nullptr, make_token_ident(proc_name), t_hasher_proc, 0);
|
||||
e->Procedure.link_name = proc_name;
|
||||
irValue *p = ir_value_procedure(m, e, t_hasher_proc, nullptr, body, proc_name);
|
||||
map_set(&m->values, hash_entity(e), p);
|
||||
string_map_set(&m->members, proc_name, p);
|
||||
map_set(&m->hasher_procs, key, p);
|
||||
|
||||
irProcedure *proc = &p->Proc;
|
||||
proc->is_startup = true;
|
||||
proc->ignore_dead_instr = true;
|
||||
ir_begin_procedure_body(proc);
|
||||
// ir_start_block(proc, proc->decl_block);
|
||||
GB_ASSERT(proc->curr_block != nullptr);
|
||||
|
||||
irValue *data = proc->params[0];
|
||||
irValue *seed = proc->params[1];
|
||||
|
||||
if (type->kind == Type_Struct) {
|
||||
type_set_offsets(type);
|
||||
|
||||
GB_PANIC("Type_Struct");
|
||||
} else if (is_type_string(type)) {
|
||||
auto args = array_make<irValue *>(permanent_allocator(), 2);
|
||||
args[0] = data;
|
||||
args[1] = seed;
|
||||
irValue *res = ir_emit_runtime_call(proc, "default_hasher_string", args);
|
||||
ir_emit(proc, ir_instr_return(proc, res));
|
||||
} else {
|
||||
GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
|
||||
|
||||
i64 sz = type_size_of(type);
|
||||
char const *name = nullptr;
|
||||
switch (sz) {
|
||||
case 1: name = "default_hasher1"; break;
|
||||
case 2: name = "default_hasher2"; break;
|
||||
case 4: name = "default_hasher4"; break;
|
||||
case 8: name = "default_hasher8"; break;
|
||||
case 16: name = "default_hasher16"; break;
|
||||
default: GB_PANIC("unhandled hasher for key type: %s", type_to_string(type));
|
||||
}
|
||||
GB_ASSERT(name != nullptr);
|
||||
|
||||
auto args = array_make<irValue *>(permanent_allocator(), 2);
|
||||
args[0] = data;
|
||||
args[1] = seed;
|
||||
irValue *res = ir_emit_runtime_call(proc, name, args);
|
||||
ir_emit(proc, ir_instr_return(proc, res));
|
||||
}
|
||||
|
||||
|
||||
ir_end_procedure_body(proc);
|
||||
|
||||
return p;
|
||||
}
|
||||
@@ -7589,6 +7668,8 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu
|
||||
case BuiltinProc_type_equal_proc:
|
||||
return ir_get_equal_proc_for_type(proc->module, ce->args[0]->tav.type);
|
||||
|
||||
case BuiltinProc_type_hasher_proc:
|
||||
return ir_get_hasher_proc_for_type(proc->module, ce->args[0]->tav.type);
|
||||
}
|
||||
|
||||
GB_PANIC("Unhandled built-in procedure");
|
||||
@@ -11705,7 +11786,8 @@ void ir_init_module(irModule *m, Checker *c) {
|
||||
map_init(&m->debug_info, heap_allocator());
|
||||
map_init(&m->entity_names, heap_allocator());
|
||||
map_init(&m->anonymous_proc_lits, heap_allocator());
|
||||
map_init(&m->compare_procs, heap_allocator());
|
||||
map_init(&m->equal_procs, heap_allocator());
|
||||
map_init(&m->hasher_procs, heap_allocator());
|
||||
array_init(&m->procs, heap_allocator());
|
||||
array_init(&m->procs_to_generate, heap_allocator());
|
||||
array_init(&m->foreign_library_paths, heap_allocator());
|
||||
@@ -12447,10 +12529,14 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info
|
||||
irValue *key = ir_emit_struct_ep(proc, tag, 0);
|
||||
irValue *value = ir_emit_struct_ep(proc, tag, 1);
|
||||
irValue *generated_struct = ir_emit_struct_ep(proc, tag, 2);
|
||||
irValue *key_equal = ir_emit_struct_ep(proc, tag, 3);
|
||||
irValue *key_hasher = ir_emit_struct_ep(proc, tag, 4);
|
||||
|
||||
ir_emit_store(proc, key, ir_get_type_info_ptr(proc, t->Map.key));
|
||||
ir_emit_store(proc, value, ir_get_type_info_ptr(proc, t->Map.value));
|
||||
ir_emit_store(proc, generated_struct, ir_get_type_info_ptr(proc, t->Map.generated_struct_type));
|
||||
ir_emit_store(proc, key_equal, ir_get_equal_proc_for_type(proc->module, t->Map.key));
|
||||
ir_emit_store(proc, key_hasher, ir_get_hasher_proc_for_type(proc->module, t->Map.key));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -8559,6 +8559,9 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
|
||||
|
||||
case BuiltinProc_type_equal_proc:
|
||||
return lb_get_equal_proc_for_type(p->module, ce->args[0]->tav.type);
|
||||
|
||||
case BuiltinProc_type_hasher_proc:
|
||||
return lb_get_hasher_proc_for_type(p->module, ce->args[0]->tav.type);
|
||||
}
|
||||
|
||||
GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));
|
||||
@@ -9169,84 +9172,156 @@ lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type) {
|
||||
LLVMTypeRef ptr_type = lb_type(m, pt);
|
||||
|
||||
auto key = hash_type(type);
|
||||
lbProcedure **found = map_get(&m->compare_procs, key);
|
||||
lbProcedure **found = map_get(&m->equal_procs, key);
|
||||
lbProcedure *compare_proc = nullptr;
|
||||
if (found) {
|
||||
compare_proc = *found;
|
||||
} else {
|
||||
static u32 proc_index = 0;
|
||||
GB_ASSERT(compare_proc != nullptr);
|
||||
return {compare_proc->value, compare_proc->type};
|
||||
}
|
||||
|
||||
char buf[16] = {};
|
||||
isize n = gb_snprintf(buf, 16, "__$cmp%u", ++proc_index);
|
||||
char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
|
||||
String proc_name = make_string_c(str);
|
||||
static u32 proc_index = 0;
|
||||
|
||||
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
|
||||
lb_begin_procedure_body(p);
|
||||
char buf[16] = {};
|
||||
isize n = gb_snprintf(buf, 16, "__$equal%u", ++proc_index);
|
||||
char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
|
||||
String proc_name = make_string_c(str);
|
||||
|
||||
LLVMValueRef x = LLVMGetParam(p->value, 0);
|
||||
LLVMValueRef y = LLVMGetParam(p->value, 1);
|
||||
x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
|
||||
y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
|
||||
lbValue lhs = {x, pt};
|
||||
lbValue rhs = {y, pt};
|
||||
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
|
||||
map_set(&m->equal_procs, key, p);
|
||||
lb_begin_procedure_body(p);
|
||||
|
||||
LLVMValueRef x = LLVMGetParam(p->value, 0);
|
||||
LLVMValueRef y = LLVMGetParam(p->value, 1);
|
||||
x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
|
||||
y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
|
||||
lbValue lhs = {x, pt};
|
||||
lbValue rhs = {y, pt};
|
||||
|
||||
|
||||
lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
|
||||
lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
|
||||
lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
|
||||
lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
|
||||
|
||||
lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
|
||||
lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
|
||||
lb_start_block(p, block_same_ptr);
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
|
||||
lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
|
||||
lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
|
||||
lb_start_block(p, block_same_ptr);
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
|
||||
|
||||
lb_start_block(p, block_diff_ptr);
|
||||
lb_start_block(p, block_diff_ptr);
|
||||
|
||||
if (type->kind == Type_Struct) {
|
||||
type_set_offsets(type);
|
||||
if (type->kind == Type_Struct) {
|
||||
type_set_offsets(type);
|
||||
|
||||
lbBlock *block_false = lb_create_block(p, "bfalse");
|
||||
lbValue res = lb_const_bool(m, t_bool, true);
|
||||
lbBlock *block_false = lb_create_block(p, "bfalse");
|
||||
lbValue res = lb_const_bool(m, t_bool, true);
|
||||
|
||||
for_array(i, type->Struct.fields) {
|
||||
lbBlock *next_block = lb_create_block(p, "btrue");
|
||||
for_array(i, type->Struct.fields) {
|
||||
lbBlock *next_block = lb_create_block(p, "btrue");
|
||||
|
||||
lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
|
||||
lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
|
||||
lbValue left = lb_emit_load(p, pleft);
|
||||
lbValue right = lb_emit_load(p, pright);
|
||||
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
|
||||
|
||||
lb_emit_if(p, ok, next_block, block_false);
|
||||
|
||||
lb_emit_jump(p, next_block);
|
||||
lb_start_block(p, next_block);
|
||||
}
|
||||
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
|
||||
|
||||
lb_start_block(p, block_false);
|
||||
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
|
||||
} else {
|
||||
lbValue left = lb_emit_load(p, lhs);
|
||||
lbValue right = lb_emit_load(p, rhs);
|
||||
lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
|
||||
lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
|
||||
lbValue left = lb_emit_load(p, pleft);
|
||||
lbValue right = lb_emit_load(p, pright);
|
||||
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
|
||||
ok = lb_emit_conv(p, ok, t_bool);
|
||||
LLVMBuildRet(p->builder, ok.value);
|
||||
|
||||
lb_emit_if(p, ok, next_block, block_false);
|
||||
|
||||
lb_emit_jump(p, next_block);
|
||||
lb_start_block(p, next_block);
|
||||
}
|
||||
|
||||
lb_end_procedure_body(p);
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
|
||||
|
||||
map_set(&m->compare_procs, key, p);
|
||||
lb_start_block(p, block_false);
|
||||
|
||||
compare_proc = p;
|
||||
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
|
||||
} else {
|
||||
lbValue left = lb_emit_load(p, lhs);
|
||||
lbValue right = lb_emit_load(p, rhs);
|
||||
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
|
||||
ok = lb_emit_conv(p, ok, t_bool);
|
||||
LLVMBuildRet(p->builder, ok.value);
|
||||
}
|
||||
GB_ASSERT(compare_proc != nullptr);
|
||||
|
||||
lb_end_procedure_body(p);
|
||||
|
||||
compare_proc = p;
|
||||
return {compare_proc->value, compare_proc->type};
|
||||
}
|
||||
|
||||
lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
|
||||
Type *original_type = type;
|
||||
type = base_type(type);
|
||||
GB_ASSERT(is_type_valid_for_keys(type));
|
||||
|
||||
Type *pt = alloc_type_pointer(type);
|
||||
LLVMTypeRef ptr_type = lb_type(m, pt);
|
||||
|
||||
auto key = hash_type(type);
|
||||
lbProcedure **found = map_get(&m->hasher_procs, key);
|
||||
lbProcedure *hasher_proc = nullptr;
|
||||
if (found) {
|
||||
hasher_proc = *found;
|
||||
GB_ASSERT(hasher_proc != nullptr);
|
||||
return {hasher_proc->value, hasher_proc->type};
|
||||
}
|
||||
|
||||
static u32 proc_index = 0;
|
||||
|
||||
char buf[16] = {};
|
||||
isize n = gb_snprintf(buf, 16, "__$hasher%u", ++proc_index);
|
||||
char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
|
||||
String proc_name = make_string_c(str);
|
||||
|
||||
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
|
||||
map_set(&m->hasher_procs, key, p);
|
||||
lb_begin_procedure_body(p);
|
||||
|
||||
LLVMValueRef x = LLVMGetParam(p->value, 0);
|
||||
LLVMValueRef y = LLVMGetParam(p->value, 1);
|
||||
lbValue data = {x, t_rawptr};
|
||||
lbValue seed = {y, t_uintptr};
|
||||
|
||||
if (type->kind == Type_Struct) {
|
||||
type_set_offsets(type);
|
||||
|
||||
GB_PANIC("Type_Struct");
|
||||
} else if (is_type_string(type)) {
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 2);
|
||||
args[0] = data;
|
||||
args[1] = seed;
|
||||
lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
|
||||
LLVMBuildRet(p->builder, res.value);
|
||||
} else {
|
||||
GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
|
||||
|
||||
i64 sz = type_size_of(type);
|
||||
char const *name = nullptr;
|
||||
switch (sz) {
|
||||
case 1: name = "default_hasher1"; break;
|
||||
case 2: name = "default_hasher2"; break;
|
||||
case 4: name = "default_hasher4"; break;
|
||||
case 8: name = "default_hasher8"; break;
|
||||
case 16: name = "default_hasher16"; break;
|
||||
default: GB_PANIC("unhandled hasher for key type: %s", type_to_string(type));
|
||||
}
|
||||
GB_ASSERT(name != nullptr);
|
||||
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 2);
|
||||
args[0] = data;
|
||||
args[1] = seed;
|
||||
lbValue res = lb_emit_runtime_call(p, name, args);
|
||||
LLVMBuildRet(p->builder, res.value);
|
||||
}
|
||||
|
||||
lb_end_procedure_body(p);
|
||||
|
||||
hasher_proc = p;
|
||||
return {hasher_proc->value, hasher_proc->type};
|
||||
}
|
||||
|
||||
|
||||
|
||||
lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) {
|
||||
Type *a = core_type(left.type);
|
||||
Type *b = core_type(right.type);
|
||||
@@ -11571,7 +11646,8 @@ void lb_init_module(lbModule *m, Checker *c) {
|
||||
string_map_init(&m->const_strings, a);
|
||||
map_init(&m->anonymous_proc_lits, a);
|
||||
map_init(&m->function_type_map, a);
|
||||
map_init(&m->compare_procs, a);
|
||||
map_init(&m->equal_procs, a);
|
||||
map_init(&m->hasher_procs, a);
|
||||
array_init(&m->procedures_to_generate, a);
|
||||
array_init(&m->foreign_library_paths, a);
|
||||
|
||||
@@ -12307,10 +12383,12 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
|
||||
tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr);
|
||||
init_map_internal_types(t);
|
||||
|
||||
LLVMValueRef vals[3] = {
|
||||
LLVMValueRef vals[5] = {
|
||||
lb_get_type_info_ptr(m, t->Map.key).value,
|
||||
lb_get_type_info_ptr(m, t->Map.value).value,
|
||||
lb_get_type_info_ptr(m, t->Map.generated_struct_type).value,
|
||||
lb_get_equal_proc_for_type(m, t->Map.key).value,
|
||||
lb_get_hasher_proc_for_type(m, t->Map.key).value
|
||||
};
|
||||
|
||||
lbValue res = {};
|
||||
|
||||
@@ -106,7 +106,8 @@ struct lbModule {
|
||||
Map<lbProcedure *> anonymous_proc_lits; // Key: Ast *
|
||||
Map<struct lbFunctionType *> function_type_map; // Key: Type *
|
||||
|
||||
Map<lbProcedure *> compare_procs; // Key: Type *
|
||||
Map<lbProcedure *> equal_procs; // Key: Type *
|
||||
Map<lbProcedure *> hasher_procs; // Key: Type *
|
||||
|
||||
u32 global_array_index;
|
||||
u32 global_generated_index;
|
||||
@@ -380,6 +381,7 @@ lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, To
|
||||
lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos);
|
||||
|
||||
lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type);
|
||||
lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type);
|
||||
lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
|
||||
|
||||
#define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
|
||||
|
||||
@@ -690,7 +690,8 @@ gb_global Type *t_map_header = nullptr;
|
||||
gb_global Type *t_vector_x86_mmx = nullptr;
|
||||
|
||||
|
||||
gb_global Type *t_equal_proc = nullptr;
|
||||
gb_global Type *t_equal_proc = nullptr;
|
||||
gb_global Type *t_hasher_proc = nullptr;
|
||||
|
||||
|
||||
i64 type_size_of (Type *t);
|
||||
@@ -1949,6 +1950,9 @@ bool is_type_simple_compare(Type *t) {
|
||||
if (t->Basic.flags & BasicFlag_SimpleCompare) {
|
||||
return true;
|
||||
}
|
||||
if (t->Basic.kind == Basic_typeid) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case Type_Pointer:
|
||||
|
||||
Reference in New Issue
Block a user