Skip to content
← Back

src/ir/temp.ghul

1
namespace IR is
2
use IO.Std;
3
4
use Semantic.Types.Type;
5
use Values.Value;
6
7
class TEMP is
8
_block: IR.Values.BLOCK;
9
_next_id: int static;
10
_name: string =>
11
if suffix > 0 then
12
"'.{prefix}.{id}.{suffix}'"
13
else
14
"'.{prefix}.{id}'"
15
fi;
16
17
id: int;
18
prefix: string;
19
suffix: int;
20
type: Type;
21
22
init(block: IR.Values.BLOCK, prefix: string, suffix: int, type: Type) is
23
_block = block;
24
self.prefix = prefix;
25
self.suffix = suffix;
26
27
self.id = get_next_id();
28
self.type = type;
29
30
declare();
31
si
32
33
init(block: IR.Values.BLOCK, prefix: string, value: Value) is
34
init(block, prefix, 0, value);
35
si
36
37
init(block: IR.Values.BLOCK, prefix: string, suffix: int, value: Value) is
38
init(block, prefix, suffix, value.type!);
39
40
store(value);
41
si
42
43
init(block: IR.Values.BLOCK, prefix: string, type: Type) is
44
init(block, prefix, 0, type);
45
si
46
47
reset_id() static is
48
_next_id = 0;
49
si
50
51
get_next_id() -> int static is
52
let result = _next_id;
53
_next_id = _next_id + 1;
54
return result;
55
si
56
57
declare() is
58
let buffer = System.Text.StringBuilder();
59
60
buffer
61
.append(".locals init (");
62
63
type.gen_type(buffer);
64
65
buffer
66
.append(' ')
67
.append(_name)
68
.append(')');
69
70
_block.add(RAW(type, buffer.to_string()));
71
si
72
73
il_name: string => _name;
74
75
load() -> Values.Load.TEMP => Values.Load.TEMP(_name, type);
76
77
store(value: Value) is
78
_block.add(value);
79
80
_block.add(RAW(type, "stloc {_name}"));
81
si
82
si
83
si