1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
gb_internal cgValue cg_const_nil(cgProcedure *p, Type *type) {
Type *original_type = type;
type = core_type(type);
i64 size = type_size_of(type);
i64 align = type_align_of(type);
TB_DataType dt = cg_data_type(type);
if (TB_IS_VOID_TYPE(dt)) {
TB_Module *m = p->module->mod;
char name[32] = {};
gb_snprintf(name, 31, "cnil$%u", 1+p->module->const_nil_guid.fetch_add(1));
TB_Global *global = tb_global_create(m, -1, name, nullptr, TB_LINKAGE_PRIVATE);
tb_global_set_storage(m, tb_module_get_rdata(m), global, size, align, 0);
TB_Symbol *symbol = cast(TB_Symbol *)global;
TB_Node *node = tb_inst_get_symbol_address(p->func, symbol);
return cg_lvalue_addr(node, type);
}
if (is_type_internally_pointer_like(type)) {
return cg_value(tb_inst_uint(p->func, dt, 0), type);
} else if (is_type_integer(type) || is_type_boolean(type) || is_type_bit_set(type)) {
return cg_value(tb_inst_uint(p->func, dt, 0), type);
} else if (is_type_float(type)) {
switch (size) {
case 2:
return cg_value(tb_inst_uint(p->func, dt, 0), type);
case 4:
return cg_value(tb_inst_float32(p->func, 0), type);
case 8:
return cg_value(tb_inst_float64(p->func, 0), type);
}
}
GB_PANIC("TODO(bill): cg_const_nil %s", type_to_string(original_type));
return {};
}
gb_internal cgValue cg_const_value(cgModule *m, cgProcedure *p, Type *type, ExactValue const &value) {
TB_Node *node = nullptr;
if (value.kind == ExactValue_Invalid) {
return cg_const_nil(p, type);
}
if (value.kind == ExactValue_Procedure) {
Ast *expr = unparen_expr(value.value_procedure);
Entity *e = entity_of_node(expr);
if (e != nullptr) {
cgValue found = cg_find_procedure_value_from_entity(m, e);
GB_ASSERT(are_types_identical(type, found.type));
return found;
}
}
GB_ASSERT(node != nullptr);
return cg_value(node, type);
}
gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const &value) {
GB_ASSERT(p != nullptr);
return cg_const_value(p->module, p, type, value);
}
|