-
Notifications
You must be signed in to change notification settings - Fork 9
/
temp.c
112 lines (94 loc) · 1.91 KB
/
temp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "temp.h"
struct temp_s
{
int num;
};
string_t tmp_name(tmp_label_t label)
{
return sym_name(label);
}
static int _labels = 0;
tmp_label_t tmp_label(void)
{
char buf[16];
snprintf(buf, sizeof(buf), ".L%d", _labels++);
return tmp_named_label(string(buf));
}
tmp_label_t tmp_named_label(string_t str)
{
return symbol(str);
}
static int _temps = 100;
temp_t temp(void)
{
temp_t p = checked_malloc(sizeof(*p));
p->num = _temps++;
{
char buf[16];
snprintf(buf, sizeof(buf), "%d", p->num);
tmp_enter(tmp_map(), p, string(buf));
}
return p;
}
struct tmp_map_s
{
table_t table;
tmp_map_t under;
};
tmp_map_t tmp_map(void)
{
static tmp_map_t map = NULL;
if (!map)
map = tmp_empty();
return map;
}
static tmp_map_t new_map(table_t tab, tmp_map_t under)
{
tmp_map_t p = checked_malloc(sizeof(*p));
p->table = tab;
p->under = under;
return p;
}
tmp_map_t tmp_empty(void)
{
return new_map(tab_empty(), NULL);
}
tmp_map_t tmp_layer_map(tmp_map_t over, tmp_map_t under)
{
if (over == NULL)
return under;
else
return new_map(over->table, tmp_layer_map(over->under, under));
}
void tmp_enter(tmp_map_t map, temp_t tmp, string_t str)
{
assert(map && map->table);
tab_enter(map->table, tmp, str);
}
string_t tmp_lookup(tmp_map_t map, temp_t tmp)
{
string_t str;
assert(map && map->table);
str = tab_lookup(map->table, tmp);
if (str)
return str;
else if (map->under)
return tmp_lookup(map->under, tmp);
else
return NULL;
}
static FILE *_fp;
static void show(temp_t tmp, string_t str)
{
fprintf(_fp, "t%d ->%s\n", tmp->num, str);
}
void tmp_dump_map(FILE *fp, tmp_map_t map)
{
_fp = fp;
tab_dump(map->table, (tab_dump_func_t) show);
if (map->under)
{
fprintf(fp, "-------\n");
tmp_dump_map(fp, map->under);
}
}