Mock out more global value stuff

This commit is contained in:
gingerBill
2023-07-20 01:54:36 +01:00
parent bcdcad5847
commit b54f3d4ee9
2 changed files with 212 additions and 129 deletions
+1 -1
View File
@@ -567,7 +567,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo
} }
} else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) { } else if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
if (is_type_rune_array(type) && value.kind == ExactValue_String) { if (is_type_rune_array(type)) {
i64 count = type->Array.count; i64 count = type->Array.count;
Type *elem = type->Array.elem; Type *elem = type->Array.elem;
LLVMTypeRef et = lb_type(m, elem); LLVMTypeRef et = lb_type(m, elem);
+105 -22
View File
@@ -110,7 +110,7 @@ gb_internal TB_Global *cg_global_const_string(cgModule *m, String const &str, Ty
char name[32] = {}; char name[32] = {};
gb_snprintf(name, 31, "csb$%u", 1+m->const_nil_guid.fetch_add(1)); gb_snprintf(name, 31, "csb$%u", 1+m->const_nil_guid.fetch_add(1));
TB_Global *str_global = tb_global_create(m->mod, -1, name, cg_debug_type(m, t_cstring), TB_LINKAGE_PRIVATE); TB_Global *str_global = tb_global_create(m->mod, -1, name, nullptr, TB_LINKAGE_PRIVATE);
i64 size = str.len+1; i64 size = str.len+1;
tb_global_set_storage(m->mod, tb_module_get_rdata(m->mod), str_global, size, 1, 1); tb_global_set_storage(m->mod, tb_module_get_rdata(m->mod), str_global, size, 1, 1);
u8 *data = cast(u8 *)tb_global_add_region(m->mod, str_global, 0, size); u8 *data = cast(u8 *)tb_global_add_region(m->mod, str_global, 0, size);
@@ -127,7 +127,6 @@ gb_internal TB_Global *cg_global_const_string(cgModule *m, String const &str, Ty
if (global == nullptr) { if (global == nullptr) {
global = tb_global_create(m->mod, -1, name, cg_debug_type(m, type), TB_LINKAGE_PRIVATE); global = tb_global_create(m->mod, -1, name, cg_debug_type(m, type), TB_LINKAGE_PRIVATE);
tb_global_set_storage(m->mod, tb_module_get_rdata(m->mod), str_global, type_size_of(type), type_align_of(type), 2); tb_global_set_storage(m->mod, tb_module_get_rdata(m->mod), str_global, type_size_of(type), type_align_of(type), 2);
} }
tb_global_add_symbol_reloc(m->mod, global, offset+0, cast(TB_Symbol *)str_global); tb_global_add_symbol_reloc(m->mod, global, offset+0, cast(TB_Symbol *)str_global);
@@ -222,10 +221,34 @@ gb_internal i64 cg_global_const_calculate_region_count_from_basic_type(Type *typ
return -1; return -1;
} }
gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value, Type *type) { gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value, Type *type) {
Type *bt = base_type(type);
if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
if (is_type_rune_array(type)) {
return 1;
}
Type *et = base_array_type(type);
i64 base_count = 2;
if (is_type_cstring(et)) {
base_count = 1;
}
return base_count * bt->Array.count;
} else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
return 1;
} else if (is_type_array(type) &&
value.kind != ExactValue_Invalid &&
value.kind != ExactValue_String &&
value.kind != ExactValue_Compound) {
Type *elem = type->Array.elem;
i64 base_count = cg_global_const_calculate_region_count(value, elem);
return base_count * type->Array.count;
}
isize count = 0; isize count = 0;
switch (value.kind) { switch (value.kind) {
case ExactValue_Invalid: case ExactValue_Invalid:
break; return 0;
case ExactValue_Bool: case ExactValue_Bool:
case ExactValue_Integer: case ExactValue_Integer:
case ExactValue_Float: case ExactValue_Float:
@@ -233,22 +256,18 @@ gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value
case ExactValue_Typeid: case ExactValue_Typeid:
case ExactValue_Complex: case ExactValue_Complex:
case ExactValue_Quaternion: case ExactValue_Quaternion:
count += 1; return 1;
break;
case ExactValue_Procedure: case ExactValue_Procedure:
count += 1; return 1;
break;
case ExactValue_String: case ExactValue_String:
if (is_type_string(type)) { if (is_type_string(type)) {
count += 2; return 2;
} else if (is_type_cstring(type) || is_type_array_like(type)) { } else if (is_type_cstring(type) || is_type_array_like(type)) {
count += 1; return 1;
} else {
count += 2;
} }
break; return 2;
case ExactValue_Compound: { case ExactValue_Compound: {
ast_node(cl, CompoundLit, value.value_compound); ast_node(cl, CompoundLit, value.value_compound);
@@ -354,15 +373,12 @@ gb_internal isize cg_global_const_calculate_region_count(ExactValue const &value
} break; } break;
case Type_BitSet: case Type_BitSet:
count += 1; return 1;
break;
case Type_Matrix: case Type_Matrix:
count += 1; return 1;
break;
case Type_Slice: case Type_Slice:
count += 2; return 2;
break;
default: default:
GB_PANIC("TODO(bill): %s", type_to_string(type)); GB_PANIC("TODO(bill): %s", type_to_string(type));
@@ -379,8 +395,72 @@ gb_internal bool cg_global_const_add_region(cgModule *m, ExactValue const &value
GB_ASSERT(is_type_endian_little(type)); GB_ASSERT(is_type_endian_little(type));
GB_ASSERT(!is_type_different_to_arch_endianness(type)); GB_ASSERT(!is_type_different_to_arch_endianness(type));
GB_ASSERT(global != nullptr);
i64 size = type_size_of(type); i64 size = type_size_of(type);
if (value.kind != ExactValue_Invalid) { if (value.kind == ExactValue_Invalid) {
return false;
}
if (is_type_array(type) && value.kind == ExactValue_String && !is_type_u8(core_array_type(type))) {
if (is_type_rune_array(type)) {
i64 count = type->Array.count;
Rune rune;
isize rune_offset = 0;
isize width = 1;
String s = value.value_string;
Rune *runes = cast(Rune *)tb_global_add_region(m->mod, global, offset, count*4);
for (i64 i = 0; i < count && rune_offset < s.len; i++) {
width = utf8_decode(s.text+rune_offset, s.len-rune_offset, &rune);
runes[i] = rune;
rune_offset += width;
}
GB_ASSERT(offset == s.len);
return true;
}
Type *bt = base_type(type);
Type *et = bt->Array.elem;
i64 elem_size = type_size_of(et);
for (i64 i = 0; i < bt->Array.count; i++) {
cg_global_const_add_region(m, value, et, global, offset+(i * elem_size));
}
return true;
} else if (is_type_u8_array(type) && value.kind == ExactValue_String) {
u8 *dst = cast(u8 *)tb_global_add_region(m->mod, global, offset, size);
gb_memcopy(dst, value.value_string.text, gb_min(value.value_string.len, size));
return true;
} else if (is_type_array(type) &&
value.kind != ExactValue_Invalid &&
value.kind != ExactValue_String &&
value.kind != ExactValue_Compound) {
Type *bt = base_type(type);
Type *et = bt->Array.elem;
i64 elem_size = type_size_of(et);
for (i64 i = 0; i < bt->Array.count; i++) {
cg_global_const_add_region(m, value, et, global, offset+(i * elem_size));
}
return true;
} else if (is_type_matrix(type) &&
value.kind != ExactValue_Invalid &&
value.kind != ExactValue_Compound) {
i64 row = type->Matrix.row_count;
i64 column = type->Matrix.column_count;
GB_ASSERT(row == column);
GB_PANIC("TODO(bill): constant matrix from scalar");
} else if (is_type_simd_vector(type) &&
value.kind != ExactValue_Invalid &&
value.kind != ExactValue_Compound) {
GB_PANIC("TODO(bill): constant vector from scalar");
}
switch (value.kind) { switch (value.kind) {
case ExactValue_Bool: case ExactValue_Bool:
{ {
@@ -416,7 +496,13 @@ gb_internal bool cg_global_const_add_region(cgModule *m, ExactValue const &value
break; break;
case ExactValue_String: case ExactValue_String:
if (is_type_array_like(type)) {
GB_ASSERT(global != nullptr);
void *data = tb_global_add_region(m->mod, global, offset, size);
gb_memcopy(data, value.value_string.text, gb_min(value.value_string.len, size));
} else {
cg_global_const_string(m, value.value_string, type, global, offset); cg_global_const_string(m, value.value_string, type, global, offset);
}
break; break;
case ExactValue_Typeid: case ExactValue_Typeid:
@@ -495,8 +581,6 @@ gb_internal bool cg_global_const_add_region(cgModule *m, ExactValue const &value
break; break;
} }
return true; return true;
}
return false;
} }
@@ -559,7 +643,6 @@ gb_internal TB_Global *cg_global_const_comp_literal(cgModule *m, Type *original_
continue; continue;
} }
i64 offset = type_offset_of_from_selection(bt, sel); i64 offset = type_offset_of_from_selection(bt, sel);
cg_global_const_add_region(m, value, sel.entity->type, global, base_offset+offset); cg_global_const_add_region(m, value, sel.entity->type, global, base_offset+offset);
} }