-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
77 lines (58 loc) · 1.65 KB
/
codegen.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "codegen.h"
#include "alloc.h"
#include "util.h"
#include "vector.h"
struct instr *alloc_instr(struct codegen_function *func) {
struct instr *instr = arena_alloc(func->unit->arena, sizeof(*instr));
if (!func->first) {
func->first = instr;
}
instr->id = func->instr_count++;
instr->pred = func->last;
instr->succ = NULL;
instr->reloc = NULL;
instr->op = NULL;
instr->p = arena_alloc(func->unit->arena, func->unit->instr_size);
if (func->last) {
func->last->succ = instr;
}
func->last = instr;
return instr;
}
const char *mangle_str_cnst_name(struct arena_allocator *arena,
const char *func_name, size_t id) {
// TODO: this should all really be handled by the mach-o file
func_name = "str";
size_t func_name_len = strlen(func_name);
size_t len = 0;
len += func_name_len;
len += 2; // strlen("l_"), required for local symbols
len += 1; // surround function name with `.` so it cannot conflict with real
// names
if (id) {
len += 1; // extra "." before id
}
size_t id_len = id ? num_digits(id) : 0;
len += id_len;
len += 1; // null char
char *buff = arena_alloc(arena, len);
size_t head = 0;
strcpy(&buff[head], "l_");
head += strlen("l_");
buff[head++] = '.';
strcpy(&buff[head], func_name);
head += func_name_len;
if (id) {
buff[head++] = '.';
size_t tail = head + id_len - 1;
while (tail >= head) {
buff[tail--] = (id % 10) + '0';
id /= 10;
}
}
head += id_len;
buff[head++] = 0;
DEBUG_ASSERT(head == len, "head (%zu) != len (%zu) in mangle_str_cnst_name",
head, len);
return buff;
}