aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-11-08 12:24:00 +0000
committergingerBill <bill@gingerbill.org>2022-11-08 12:24:00 +0000
commita74093784cea3637b445657541cb7fff2f374f50 (patch)
tree191dc88360ed5e138de7d163ef13172eecce10e1
parented58374964889db91b38fe95db409111819790ca (diff)
Add `intrinsics.map_cell_info` and `intrinsics.map_info`
-rw-r--r--core/intrinsics/intrinsics.odin3
-rw-r--r--core/runtime/dynamic_map_internal.odin23
-rw-r--r--src/check_builtin.cpp14
-rw-r--r--src/checker_builtin_procs.hpp8
-rw-r--r--src/llvm_backend.cpp10
-rw-r--r--src/llvm_backend.hpp1
-rw-r--r--src/llvm_backend_proc.cpp4
7 files changed, 40 insertions, 23 deletions
diff --git a/core/intrinsics/intrinsics.odin b/core/intrinsics/intrinsics.odin
index 69f77cea2..38542d2fc 100644
--- a/core/intrinsics/intrinsics.odin
+++ b/core/intrinsics/intrinsics.odin
@@ -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 ---
diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin
index 79e1a4f30..983bfde35 100644
--- a/core/runtime/dynamic_map_internal.odin
+++ b/core/runtime/dynamic_map_internal.odin
@@ -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
diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp
index 457100d6e..890f7a39b 100644
--- a/src/check_builtin.cpp
+++ b/src/check_builtin.cpp
@@ -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:
{
diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp
index 72639e071..e03e94ab4 100644
--- a/src/checker_builtin_procs.hpp
+++ b/src/checker_builtin_procs.hpp
@@ -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},
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index 960ef84f6..bce1fa1d1 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -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;
diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp
index 065bf4943..6c7c2e392 100644
--- a/src/llvm_backend.hpp
+++ b/src/llvm_backend.hpp
@@ -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);
diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp
index 3881790e0..6d7d7eecb 100644
--- a/src/llvm_backend_proc.cpp
+++ b/src/llvm_backend_proc.cpp
@@ -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: