-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.c
132 lines (104 loc) · 2.59 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* temp.c - functions to create and manipulate temporary variables which are
* used in the IR tree representation before it has been determined
* which variables are to go into registers.
*
*/
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "symbol.h"
#include "temp.h"
#include "table.h"
struct Temp_temp_ {
int num;
};
string Temp_labelstring(Temp_label s) {
return S_name(s);
}
static int labels = 0;
Temp_label Temp_newlabel(void) {
char buf[100];
sprintf(buf, "L%d", labels++);
return Temp_namedlabel(String(buf));
}
/* The label will be created only if it is not found. */
Temp_label Temp_namedlabel(string s) {
return S_Symbol(s);
}
static int temps = 100;
Temp_temp Temp_newtemp(void) {
Temp_temp p = (Temp_temp)checked_malloc(sizeof (*p));
p->num = temps++;
{
char r[16];
sprintf(r, "t%d", p->num);
Temp_enter(Temp_name(), p, String(r));
}
return p;
}
struct Temp_map_ {
TAB_table tab;
Temp_map under;
};
/* A global `Temp_map<Temp_temp, string>` */
Temp_map Temp_name(void) {
static Temp_map m = NULL;
if (!m)
m = Temp_empty();
return m;
}
Temp_map newMap(TAB_table tab, Temp_map under) {
Temp_map m = checked_malloc(sizeof(*m));
m->tab = tab;
m->under = under;
return m;
}
Temp_map Temp_empty(void) {
return newMap(TAB_empty(), NULL);
}
Temp_map Temp_layerMap(Temp_map over, Temp_map under) {
if (over == NULL)
return under;
else
return newMap(over->tab, Temp_layerMap(over->under, under));
}
void Temp_enter(Temp_map m, Temp_temp t, string s) {
assert(m && m->tab);
TAB_enter(m->tab, t, s);
}
string Temp_look(Temp_map m, Temp_temp t) {
string s;
assert(m && m->tab);
s = TAB_look(m->tab, t);
if (s)
return s;
else if (m->under)
return Temp_look(m->under, t);
else
return NULL;
}
Temp_tempList Temp_TempList(Temp_temp h, Temp_tempList t) {
Temp_tempList p = (Temp_tempList)checked_malloc(sizeof(*p));
p->head = h;
p->tail = t;
return p;
}
Temp_labelList Temp_LabelList(Temp_label h, Temp_labelList t) {
Temp_labelList p = (Temp_labelList)checked_malloc(sizeof(*p));
p->head = h;
p->tail = t;
return p;
}
static FILE *outfile;
void showit(Temp_temp t, string r) {
fprintf(outfile, "t%d -> %s\n", t->num, r);
}
void Temp_dumpMap(FILE *out, Temp_map m) {
outfile = out;
TAB_dump(m->tab,(void (*)(void *, void*))showit);
if (m->under) {
fprintf(out,"---------\n");
Temp_dumpMap(out,m->under);
}
}