Merge pull request #2063 from odin-lang/map-header-changes

Map header changes
This commit is contained in:
gingerBill
2022-09-21 16:07:36 +01:00
committed by GitHub
10 changed files with 216 additions and 210 deletions

View File

@@ -405,7 +405,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
raw_map.entries.allocator = p.allocator
}
header := runtime.__get_map_header_runtime(raw_map, t)
header := runtime.__get_map_header_table_runtime(t)
elem_backing := bytes_make(t.value.size, t.value.align, p.allocator) or_return
defer delete(elem_backing, p.allocator)
@@ -432,7 +432,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
key_ptr = &key_cstr
}
set_ptr := runtime.__dynamic_map_set(header, key_hash, key_ptr, map_backing_value.data)
set_ptr := runtime.__dynamic_map_set(raw_map, header, key_hash, key_ptr, map_backing_value.data)
if set_ptr == nil {
delete(key, p.allocator)
}

View File

@@ -296,7 +296,8 @@ clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
@builtin
reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
if m != nil {
__dynamic_map_reserve(__get_map_header(m), uint(capacity), loc)
h := __get_map_header_table(T)
__dynamic_map_reserve(m, h, uint(capacity), loc)
}
}

View File

@@ -16,7 +16,7 @@ __get_map_key_hash :: #force_inline proc "contextless" (k: ^$K) -> uintptr {
return hasher(k, 0)
}
__get_map_entry_key_ptr :: #force_inline proc "contextless" (h: Map_Header, entry: ^Map_Entry_Header) -> rawptr {
__get_map_entry_key_ptr :: #force_inline proc "contextless" (h: Map_Header_Table, entry: ^Map_Entry_Header) -> rawptr {
return rawptr(uintptr(entry) + h.key_offset)
}
@@ -38,8 +38,7 @@ Map_Entry_Header :: struct {
*/
}
Map_Header :: struct {
m: ^Raw_Map,
Map_Header_Table :: struct {
equal: Equal_Proc,
entry_size: int,
@@ -52,6 +51,102 @@ Map_Header :: struct {
value_size: int,
}
Map_Header :: struct {
m: ^Raw_Map,
using table: Map_Header_Table,
}
// USED INTERNALLY BY THE COMPILER
__dynamic_map_get :: proc "contextless" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr) -> rawptr {
if m != nil {
h := Map_Header{(^Raw_Map)(m), table}
index := __dynamic_map_find(h, key_hash, key_ptr).entry_index
if index != MAP_SENTINEL {
data := uintptr(__dynamic_map_get_entry(h, index))
return rawptr(data + h.value_offset)
}
}
return nil
}
// USED INTERNALLY BY THE COMPILER
__dynamic_map_set :: proc "odin" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check {
add_entry :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, loc := #caller_location) -> Map_Index {
prev := Map_Index(h.m.entries.len)
c := Map_Index(__dynamic_array_append_nothing(&h.m.entries, h.entry_size, h.entry_align, loc))
if c != prev {
end := __dynamic_map_get_entry(h, c-1)
end.hash = key_hash
mem_copy(rawptr(uintptr(end) + h.key_offset), key_ptr, h.key_size)
end.next = MAP_SENTINEL
}
return prev
}
h := Map_Header{(^Raw_Map)(m), table}
index := MAP_SENTINEL
if len(h.m.hashes) == 0 {
__dynamic_map_reserve(m, table, INITIAL_MAP_CAP, loc)
__dynamic_map_grow(h, loc)
}
fr := __dynamic_map_find(h, key_hash, key_ptr)
if fr.entry_index != MAP_SENTINEL {
index = fr.entry_index
} else {
index = add_entry(h, key_hash, key_ptr, loc)
if fr.entry_prev != MAP_SENTINEL {
entry := __dynamic_map_get_entry(h, fr.entry_prev)
entry.next = index
} else if fr.hash_index != MAP_SENTINEL {
h.m.hashes[fr.hash_index] = index
} else {
return nil
}
}
e := __dynamic_map_get_entry(h, index)
e.hash = key_hash
key := rawptr(uintptr(e) + h.key_offset)
val := rawptr(uintptr(e) + h.value_offset)
mem_copy(key, key_ptr, h.key_size)
mem_copy(val, value, h.value_size)
if __dynamic_map_full(h) {
__dynamic_map_grow(h, loc)
}
return __dynamic_map_get_entry(h, index)
}
// USED INTERNALLY BY THE COMPILER
__dynamic_map_reserve :: proc "odin" (m: rawptr, table: Map_Header_Table, cap: uint, loc := #caller_location) {
h := Map_Header{(^Raw_Map)(m), table}
c := context
if h.m.entries.allocator.procedure != nil {
c.allocator = h.m.entries.allocator
}
context = c
cap := cap
cap = ceil_to_pow2(cap)
__dynamic_array_reserve(&h.m.entries, h.entry_size, h.entry_align, int(cap), loc)
if h.m.entries.len*2 < len(h.m.hashes) {
return
}
if __slice_resize(&h.m.hashes, int(cap*2), h.m.entries.allocator, loc) {
__dynamic_map_reset_entries(h, loc)
}
}
INITIAL_HASH_SEED :: 0xcbf29ce484222325
_fnv64a :: proc "contextless" (data: []byte, seed: u64 = INITIAL_HASH_SEED) -> u64 {
@@ -135,8 +230,19 @@ default_hasher_cstring :: proc "contextless" (data: rawptr, seed: uintptr) -> ui
}
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header := Map_Header{m = (^Raw_Map)(m)}
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> (header: Map_Header) {
header.m = (^Raw_Map)(m)
header.table = #force_inline __get_map_header_table(T)
return
}
__get_map_header_runtime :: proc "contextless" (m: ^Raw_Map, ti: Type_Info_Map) -> (header: Map_Header) {
header.m = m
header.table = #force_inline __get_map_header_table_runtime(ti)
return
}
__get_map_header_table :: proc "contextless" ($T: typeid/map[$K]$V) -> (header: Map_Header_Table) {
Entry :: struct {
hash: uintptr,
next: Map_Index,
@@ -155,18 +261,16 @@ __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header.value_offset = offset_of(Entry, value)
header.value_size = size_of(V)
return header
return
}
__get_map_header_runtime :: proc "contextless" (m: ^Raw_Map, ti: Type_Info_Map) -> Map_Header {
header := Map_Header{m = m}
__get_map_header_table_runtime :: proc "contextless" (ti: Type_Info_Map) -> (header: Map_Header) {
header.equal = ti.key_equal
entries := ti.generated_struct.variant.(Type_Info_Struct).types[1]
entry := entries.variant.(Type_Info_Dynamic_Array).elem
e := entry.variant.(Type_Info_Struct)
header.entry_size = entry.size
header.entry_align = entry.align
@@ -176,10 +280,11 @@ __get_map_header_runtime :: proc "contextless" (m: ^Raw_Map, ti: Type_Info_Map)
header.value_offset = e.offsets[3]
header.value_size = e.types[3].size
return header
return
}
__slice_resize :: proc "odin" (array_: ^$T/[]$E, new_count: int, allocator: Allocator, loc := #caller_location) -> bool {
array := (^Raw_Slice)(array_)
@@ -221,26 +326,6 @@ __dynamic_map_reset_entries :: proc "contextless" (h: Map_Header, loc := #caller
}
}
__dynamic_map_reserve :: proc "odin" (h: Map_Header, cap: uint, loc := #caller_location) {
c := context
if h.m.entries.allocator.procedure != nil {
c.allocator = h.m.entries.allocator
}
context = c
cap := cap
cap = ceil_to_pow2(cap)
__dynamic_array_reserve(&h.m.entries, h.entry_size, h.entry_align, int(cap), loc)
if h.m.entries.len*2 < len(h.m.hashes) {
return
}
if __slice_resize(&h.m.hashes, int(cap*2), h.m.entries.allocator, loc) {
__dynamic_map_reset_entries(h, loc)
}
}
__dynamic_map_shrink :: proc "odin" (h: Map_Header, cap: int, loc := #caller_location) -> (did_shrink: bool) {
c := context
if h.m.entries.allocator.procedure != nil {
@@ -251,68 +336,6 @@ __dynamic_map_shrink :: proc "odin" (h: Map_Header, cap: int, loc := #caller_loc
return __dynamic_array_shrink(&h.m.entries, h.entry_size, h.entry_align, cap, loc)
}
// USED INTERNALLY BY THE COMPILER
__dynamic_map_get :: proc "contextless" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr) -> rawptr {
index := __dynamic_map_find(h, key_hash, key_ptr).entry_index
if index != MAP_SENTINEL {
data := uintptr(__dynamic_map_get_entry(h, index))
return rawptr(data + h.value_offset)
}
return nil
}
// USED INTERNALLY BY THE COMPILER
__dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check {
add_entry :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, loc := #caller_location) -> Map_Index {
prev := Map_Index(h.m.entries.len)
c := Map_Index(__dynamic_array_append_nothing(&h.m.entries, h.entry_size, h.entry_align, loc))
if c != prev {
end := __dynamic_map_get_entry(h, c-1)
end.hash = key_hash
mem_copy(rawptr(uintptr(end) + h.key_offset), key_ptr, h.key_size)
end.next = MAP_SENTINEL
}
return prev
}
index := MAP_SENTINEL
if len(h.m.hashes) == 0 {
__dynamic_map_reserve(h, INITIAL_MAP_CAP, loc)
__dynamic_map_grow(h, loc)
}
fr := __dynamic_map_find(h, key_hash, key_ptr)
if fr.entry_index != MAP_SENTINEL {
index = fr.entry_index
} else {
index = add_entry(h, key_hash, key_ptr, loc)
if fr.entry_prev != MAP_SENTINEL {
entry := __dynamic_map_get_entry(h, fr.entry_prev)
entry.next = index
} else if fr.hash_index != MAP_SENTINEL {
h.m.hashes[fr.hash_index] = index
} else {
return nil
}
}
e := __dynamic_map_get_entry(h, index)
e.hash = key_hash
key := rawptr(uintptr(e) + h.key_offset)
val := rawptr(uintptr(e) + h.value_offset)
mem_copy(key, key_ptr, h.key_size)
mem_copy(val, value, h.value_size)
if __dynamic_map_full(h) {
__dynamic_map_grow(h, loc)
}
return __dynamic_map_get_entry(h, index)
}
@(private="file")
ceil_to_pow2 :: proc "contextless" (n: uint) -> uint {
@@ -336,7 +359,7 @@ ceil_to_pow2 :: proc "contextless" (n: uint) -> uint {
__dynamic_map_grow :: proc "odin" (h: Map_Header, loc := #caller_location) {
new_count := max(uint(h.m.entries.cap) * 2, INITIAL_MAP_CAP)
// Rehash through Reserve
__dynamic_map_reserve(h, new_count, loc)
__dynamic_map_reserve(h.m, h.table, new_count, loc)
}
__dynamic_map_full :: #force_inline proc "contextless" (h: Map_Header) -> bool {

View File

@@ -2831,23 +2831,12 @@ void init_core_source_code_location(Checker *c) {
}
void init_core_map_type(Checker *c) {
if (t_map_hash == nullptr) {
Entity *e = find_core_entity(c, str_lit("Map_Hash"));
if (e->state == EntityState_Unresolved) {
check_entity_decl(&c->builtin_ctx, e, nullptr, nullptr);
}
t_map_hash = e->type;
GB_ASSERT(t_map_hash != nullptr);
}
if (t_map_header == nullptr) {
Entity *e = find_core_entity(c, str_lit("Map_Header"));
if (e->state == EntityState_Unresolved) {
check_entity_decl(&c->builtin_ctx, e, nullptr, nullptr);
}
t_map_header = e->type;
GB_ASSERT(t_map_header != nullptr);
if (t_map_hash != nullptr) {
return;
}
t_map_hash = find_core_type(c, str_lit("Map_Hash"));
t_map_header = find_core_type(c, str_lit("Map_Header"));
t_map_header_table = find_core_type(c, str_lit("Map_Header_Table"));
}
void init_preload(Checker *c) {

View File

@@ -500,20 +500,18 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
return value;
}
lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
lbValue lb_gen_map_header_table_internal(lbProcedure *p, Type *map_type) {
lbModule *m = p->module;
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
lbAddr h = lb_add_local_generated(p, t_map_header, true); // all the values will be initialzed later
Type *key_type = map_type->Map.key;
Type *val_type = map_type->Map.value;
gb_unused(val_type);
lbAddr *found = map_get(&m->map_header_table_map, map_type);
if (found) {
return lb_addr_load(p, *found);
}
GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
map_type->Map.entry_type->cached_size = -1;
map_type->Map.entry_type->Struct.are_offsets_set = false;
i64 entry_size = type_size_of (map_type->Map.entry_type);
i64 entry_align = type_align_of (map_type->Map.entry_type);
@@ -524,52 +522,34 @@ lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map
i64 value_size = type_size_of (map_type->Map.value);
Type *map_header_base = base_type(t_map_header);
GB_ASSERT(map_header_base->Struct.fields.count == 8);
Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
LLVMValueRef const_values[8] = {};
const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
const_values[1] = lb_get_equal_proc_for_type(p->module, key_type) .value;
const_values[2] = lb_const_int(p->module, t_int, entry_size) .value;
const_values[3] = lb_const_int(p->module, t_int, entry_align) .value;
const_values[4] = lb_const_int(p->module, t_uintptr, key_offset) .value;
const_values[5] = lb_const_int(p->module, t_int, key_size) .value;
const_values[6] = lb_const_int(p->module, t_uintptr, value_offset).value;
const_values[7] = lb_const_int(p->module, t_int, value_size) .value;
Type *key_type = map_type->Map.key;
Type *val_type = map_type->Map.value;
gb_unused(val_type);
LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
LLVMBuildStore(p->builder, const_value, h.addr.value);
Type *st = base_type(t_map_header_table);
GB_ASSERT(st->Struct.fields.count == 7);
// NOTE(bill): Removes unnecessary allocation if split gep
lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
lb_emit_store(p, gep0, m);
return h;
}
LLVMValueRef const_values[7] = {};
const_values[0] = lb_get_equal_proc_for_type(m, key_type) .value;
const_values[1] = lb_const_int(m, t_int, entry_size) .value;
const_values[2] = lb_const_int(m, t_int, entry_align) .value;
const_values[3] = lb_const_int(m, t_uintptr, key_offset) .value;
const_values[4] = lb_const_int(m, t_int, key_size) .value;
const_values[5] = lb_const_int(m, t_uintptr, value_offset).value;
const_values[6] = lb_const_int(m, t_int, value_size) .value;
LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_header_table, const_values, gb_count_of(const_values));
lbValue res = {llvm_res, t_map_header_table};
lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
GB_ASSERT(is_type_map(map_type));
lbAddr addr = lb_add_global_generated(m, t_map_header_table, res, nullptr);
LLVMValueRef global_data = addr.addr.value;
LLVMSetLinkage(global_data, LLVMPrivateLinkage);
LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
LLVMSetGlobalConstant(global_data, true);
// TODO(bill): this is a temporary fix since this caching is not working other platforms
bool allow_caching = build_context.metrics.os == TargetOs_windows || is_arch_wasm();
lbAddr h = {};
if (!allow_caching) {
h = lb_gen_map_header_internal(p, map_val_ptr, map_type);
} else {
lbAddr *found = map_get(&p->map_header_cache, map_val_ptr.value);
if (found != nullptr) {
h = *found;
} else {
h = lb_gen_map_header_internal(p, map_val_ptr, map_type);
map_set(&p->map_header_cache, map_val_ptr.value, h);
}
}
return lb_addr_load(p, h);
map_set(&m->map_header_table_map, map_type, addr);
return lb_addr_load(p, addr);
}
lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
@@ -638,12 +618,26 @@ lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue
return hashed_key;
}
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type,
lbValue map_key, lbValue map_value, Ast *node) {
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
Type *map_type = base_type(type_deref(map_ptr.type));
lbValue key_ptr = {};
auto args = array_make<lbValue>(permanent_allocator(), 4);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = lb_gen_map_header_table_internal(p, map_type);
args[2] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr);
args[3] = key_ptr;
lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value));
}
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type,
lbValue const &map_key, lbValue const &map_value, Ast *node) {
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
lbValue h = lb_gen_map_header(p, addr.addr, map_type);
lbValue key_ptr = {};
lbValue key_hash = lb_gen_map_key_hash(p, map_key, map_type->Map.key, &key_ptr);
lbValue v = lb_emit_conv(p, map_value, map_type->Map.value);
@@ -651,15 +645,32 @@ void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_
lbAddr value_addr = lb_add_local_generated(p, v.type, false);
lb_addr_store(p, value_addr, v);
auto args = array_make<lbValue>(permanent_allocator(), 5);
args[0] = h;
args[1] = key_hash;
args[2] = key_ptr;
args[3] = lb_emit_conv(p, value_addr.addr, t_rawptr);
args[4] = lb_emit_source_code_location(p, node);
auto args = array_make<lbValue>(permanent_allocator(), 6);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = lb_gen_map_header_table_internal(p, map_type);
args[2] = key_hash;
args[3] = key_ptr;
args[4] = lb_emit_conv(p, value_addr.addr, t_rawptr);
args[5] = lb_emit_source_code_location(p, node);
lb_emit_runtime_call(p, "__dynamic_map_set", args);
}
void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) {
GB_ASSERT(!build_context.no_dynamic_literals);
String proc_name = {};
if (p->entity) {
proc_name = p->entity->token.string;
}
auto args = array_make<lbValue>(permanent_allocator(), 4);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = lb_gen_map_header_table_internal(p, type_deref(map_ptr.type));
args[2] = lb_const_int(p->module, t_int, capacity);
args[3] = lb_emit_source_code_location(p, proc_name, pos);
lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
}
struct lbGlobalVariable {
lbValue var;

View File

@@ -159,6 +159,8 @@ struct lbModule {
StringMap<lbAddr> objc_classes;
StringMap<lbAddr> objc_selectors;
PtrMap<Type *, lbAddr> map_header_table_map;
};
struct lbGenerator {
@@ -308,7 +310,6 @@ struct lbProcedure {
PtrMap<Ast *, lbValue> selector_values;
PtrMap<Ast *, lbAddr> selector_addr;
PtrMap<LLVMValueRef, lbAddr> map_header_cache;
};
@@ -444,9 +445,11 @@ 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_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type);
lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_);
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, lbValue map_key, lbValue map_value, Ast *node);
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key);
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node);
void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos);
lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e);
lbValue lb_find_value_from_entity(lbModule *m, Entity *e);

View File

@@ -3670,16 +3670,14 @@ lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) {
if (is_type_map(t)) {
lbAddr map_addr = lb_build_addr(p, ie->expr);
lbValue map_val = lb_addr_load(p, map_addr);
if (deref) {
map_val = lb_emit_load(p, map_val);
}
lbValue key = lb_build_expr(p, ie->index);
key = lb_emit_conv(p, key, t->Map.key);
Type *result_type = type_of_expr(expr);
lbValue map_ptr = lb_address_from_load_or_generate_local(p, map_val);
lbValue map_ptr = lb_addr_get_ptr(p, map_addr);
if (is_type_pointer(type_deref(map_ptr.type))) {
map_ptr = lb_emit_load(p, map_ptr);
}
return lb_addr_map(map_ptr, key, t, result_type);
}
@@ -4130,20 +4128,16 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
break;
}
GB_ASSERT(!build_context.no_dynamic_literals);
{
auto args = array_make<lbValue>(permanent_allocator(), 3);
args[0] = lb_gen_map_header(p, v.addr, type);
args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count);
args[2] = lb_emit_source_code_location(p, proc_name, pos);
lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
}
lb_dynamic_map_reserve(p, v.addr, 2*cl->elems.count, pos);
for_array(field_index, cl->elems) {
Ast *elem = cl->elems[field_index];
ast_node(fv, FieldValue, elem);
lbValue key = lb_build_expr(p, fv->field);
lbValue value = lb_build_expr(p, fv->value);
lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem);
lb_insert_dynamic_map_key_and_value(p, v.addr, type, key, value, elem);
}
break;
}

View File

@@ -74,6 +74,9 @@ void lb_init_module(lbModule *m, Checker *c) {
string_map_init(&m->objc_classes, a);
string_map_init(&m->objc_selectors, a);
map_init(&m->map_header_table_map, a, 0);
}
bool lb_init_generator(lbGenerator *gen, Checker *c) {
@@ -383,21 +386,6 @@ Type *lb_addr_type(lbAddr const &addr) {
return type_deref(addr.addr.type);
}
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
Type *map_type = base_type(type_deref(map_ptr.type));
lbValue h = lb_gen_map_header(p, map_ptr, map_type);
lbValue key_ptr = {};
auto args = array_make<lbValue>(permanent_allocator(), 3);
args[0] = h;
args[1] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr);
args[2] = key_ptr;
lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value));
}
lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) {
if (addr.addr.value == nullptr) {
GB_PANIC("Illegal addr -> nullptr");
@@ -715,7 +703,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
return;
} else if (addr.kind == lbAddr_Map) {
lb_insert_dynamic_map_key_and_value(p, addr, addr.map.type, addr.map.key, value, p->curr_stmt);
lb_insert_dynamic_map_key_and_value(p, addr.addr, addr.map.type, addr.map.key, value, p->curr_stmt);
return;
} else if (addr.kind == lbAddr_Context) {
lbAddr old_addr = lb_find_or_generate_context_ptr(p);

View File

@@ -123,7 +123,6 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body)
p->scope_stack.allocator = a;
map_init(&p->selector_values, a, 0);
map_init(&p->selector_addr, a, 0);
map_init(&p->map_header_cache, a, 0);
if (p->is_foreign) {
lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library);
@@ -380,9 +379,6 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type
lb_add_proc_attribute_at_index(p, offset+parameter_index, "nonnull");
lb_add_proc_attribute_at_index(p, offset+parameter_index, "nocapture");
}
map_init(&p->map_header_cache, heap_allocator(), 0);
return p;
}
@@ -889,7 +885,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
GB_ASSERT(param_count-1 <= args.count);
param_count -= 1;
} else {
GB_ASSERT_MSG(param_count == args.count, "%td == %td", param_count, args.count);
GB_ASSERT_MSG(param_count == args.count, "%td == %td (%s)", param_count, args.count, LLVMPrintValueToString(value.value));
}
lbValue result = {};

View File

@@ -688,6 +688,7 @@ gb_global Type *t_source_code_location_ptr = nullptr;
gb_global Type *t_map_hash = nullptr;
gb_global Type *t_map_header = nullptr;
gb_global Type *t_map_header_table = nullptr;
gb_global Type *t_equal_proc = nullptr;