Add intrinsics.map_cell_info and intrinsics.map_info

This commit is contained in:
gingerBill
2022-11-08 12:24:00 +00:00
parent ed58374964
commit a74093784c
7 changed files with 40 additions and 23 deletions

View File

@@ -188,6 +188,9 @@ type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---
type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, rawptr) -> bool) where type_is_comparable(T) ---
type_hasher_proc :: proc($T: typeid) -> (hasher: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr) where type_is_comparable(T) ---
type_map_info :: proc($T: typeid/map[$K]$V) -> ^runtime.Map_Info ---
type_map_cell_info :: proc($T: typeid) -> ^runtime.Map_Cell_Info ---
type_convert_variants_to_pointers :: proc($T: typeid) -> typeid where type_is_union(T) ---
constant_utf16_cstring :: proc($literal: string) -> [^]u16 ---

View File

@@ -103,6 +103,9 @@ Map_Cell_Info :: struct {
elements_per_cell: uintptr, // 8-bytes on 64-bit, 4-bytes on 32-bits
}
// map_cell_info :: proc "contextless" ($T: typeid) -> ^Map_Cell_Info {...}
map_cell_info :: intrinsics.type_map_cell_info
// Same as the above procedure but at runtime with the cell Map_Cell_Info value.
map_cell_index_dynamic :: #force_inline proc "contextless" (base: uintptr, info: ^Map_Cell_Info, index: uintptr) -> uintptr {
// Micro-optimize the common cases to save on integer division.
@@ -229,18 +232,13 @@ Map_Info :: struct {
map_info :: intrinsics.type_map_info
map_kvh_data_dynamic :: proc "contextless" (m: Raw_Map, #no_alias info: ^Map_Info) -> (ks: uintptr, vs: uintptr, hs: [^]Map_Hash, sk: uintptr, sv: uintptr) {
@static INFO_HS := Map_Cell_Info {
size_of(Map_Hash),
align_of(Map_Hash),
size_of(Map_Cell(Map_Hash)),
len(Map_Cell(Map_Hash){}.data),
}
INFO_HS := intrinsics.type_map_cell_info(Map_Hash)
capacity := uintptr(1) << map_log2_cap(m)
ks = map_data(m)
vs = map_cell_index_dynamic(ks, info.ks, capacity) // Skip past ks to get start of vs
hs_ := map_cell_index_dynamic(vs, info.vs, capacity) // Skip past vs to get start of hs
sk = map_cell_index_dynamic(hs_, &INFO_HS, capacity) // Skip past hs to get start of sk
sk = map_cell_index_dynamic(hs_, INFO_HS, capacity) // Skip past hs to get start of sk
// Need to skip past two elements in the scratch key space to get to the start
// of the scratch value space, of which there's only two elements as well.
sv = map_cell_index_dynamic_const(sk, info.ks, 2)
@@ -270,21 +268,16 @@ map_alloc_dynamic :: proc(info: ^Map_Info, log2_capacity: uintptr, allocator :=
capacity := uintptr(1) << max(log2_capacity, MAP_MIN_LOG2_CAPACITY)
@static INFO_HS := Map_Cell_Info {
size_of(Map_Hash),
align_of(Map_Hash),
size_of(Map_Cell(Map_Hash)),
len(Map_Cell(Map_Hash){}.data),
}
round :: #force_inline proc "contextless" (value: uintptr) -> uintptr {
return (value + MAP_CACHE_LINE_SIZE - 1) &~ uintptr(MAP_CACHE_LINE_SIZE - 1)
}
INFO_HS := intrinsics.type_map_cell_info(Map_Hash)
size := uintptr(0)
size = round(map_cell_index_dynamic(size, info.ks, capacity))
size = round(map_cell_index_dynamic(size, info.vs, capacity))
size = round(map_cell_index_dynamic(size, &INFO_HS, capacity))
size = round(map_cell_index_dynamic(size, INFO_HS, capacity))
size = round(map_cell_index_dynamic(size, info.ks, 2)) // Two additional ks for scratch storage
size = round(map_cell_index_dynamic(size, info.vs, 2)) // Two additional vs for scratch storage

View File

@@ -5385,6 +5385,20 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
operand->type = t_map_info_ptr;
break;
}
case BuiltinProc_type_map_cell_info:
{
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;
}
operand->mode = Addressing_Value;
operand->type = t_map_cell_info_ptr;
break;
}
case BuiltinProc_constant_utf16_cstring:
{

View File

@@ -278,6 +278,7 @@ BuiltinProc__type_simple_boolean_end,
BuiltinProc_type_equal_proc,
BuiltinProc_type_hasher_proc,
BuiltinProc_type_map_info,
BuiltinProc_type_map_cell_info,
BuiltinProc__type_end,
@@ -571,9 +572,10 @@ 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_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("type_map_info"), 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("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("type_map_cell_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},

View File

@@ -501,10 +501,10 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
}
LLVMValueRef lb_gen_map_cell_info(lbModule *m, Type *type) {
lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) {
lbAddr *found = map_get(&m->map_cell_info_map, type);
if (found) {
return found->addr.value;
return found->addr;
}
i64 size = 0, len = 0;
@@ -523,7 +523,7 @@ LLVMValueRef lb_gen_map_cell_info(lbModule *m, Type *type) {
map_set(&m->map_cell_info_map, type, addr);
return addr.addr.value;
return addr.addr;
}
lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) {
map_type = base_type(map_type);
@@ -537,8 +537,8 @@ lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) {
GB_ASSERT(t_map_info != nullptr);
GB_ASSERT(t_map_cell_info != nullptr);
LLVMValueRef key_cell_info = lb_gen_map_cell_info(m, map_type->Map.key);
LLVMValueRef value_cell_info = lb_gen_map_cell_info(m, map_type->Map.value);
LLVMValueRef key_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.key).value;
LLVMValueRef value_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.value).value;
LLVMValueRef const_values[4] = {};
const_values[0] = key_cell_info;

View File

@@ -446,6 +446,7 @@ String lb_get_const_string(lbModule *m, lbValue value);
lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true);
lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id);
lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_);
lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type);
lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type);
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key);

View File

@@ -2327,6 +2327,10 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
case BuiltinProc_type_map_info:
return lb_gen_map_info_ptr(p->module, ce->args[0]->tav.type);
case BuiltinProc_type_map_cell_info:
return lb_gen_map_cell_info_ptr(p->module, ce->args[0]->tav.type);
case BuiltinProc_fixed_point_mul:
case BuiltinProc_fixed_point_div:
case BuiltinProc_fixed_point_mul_sat: