aboutsummaryrefslogtreecommitdiff
path: root/src/tilde_const.cpp
blob: 7a8be70c7ffbe5b04e790fc1dab7a758ebdbb797 (plain)
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
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, 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);
	}

	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);
}