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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
;struct Value {
bool
on_stack,
temp,
literal,
int
loc,
val,
VType
_type
}
;Value NV = {false, false, false, 0, 0, 0, {0, 0, "null", {}, {}}}
/; setup_stack_offset (int offset) [{}uint8]
;{}uint8 out = "$"
;{}uint8 tmp = string_from_int(offset)
;add_strings(~out, ~tmp)
;return mov_asm(out, "%rax")
;/
;{}charp general_stack_offset = "(%rsp, %rax, 1)"
# The temp registers in order
/; reg_by_num(int r) [{}uint8]
/; if (r == 0)
;return "ax"
;; if (r == 1)
;return "bx"
;; if (r == 2)
;return "cx"
;; if (r == 3)
;return "dx"
;; if (r == 4)
;return "si"
;; if (r == 5)
;return "di"
;; if (r == 6)
;return "bp"
;/
;return string_from_int(r)
;/
/; method Value
/; get_norm_loc [{}uint8]
/; if (self.on_stack)
;return general_stack_offset
;/
/; if (self.temp)
;return get_reg(self._type._size, reg_by_num(self.loc))
;; else
;return get_reg(self._type._size, reg_by_num(self.loc + 8))
;/
;return ""
;/
/; add_literal (int i) [{}uint8]
/; if (self.literal)
;self.val = self.val + i
;return ""
;; else if (!self.on_stack)
;{}uint8 reg = self.get_norm_loc()
;{}uint8 out = "\tadd $"
;{}uint8 tmp = string_from_int(i)
;add_strings(~out, ~tmp)
;out.append(',')
;out.append(' ')
;add_strings(~out, ~reg)
;return out
;/
;/
/; add_value (Value v) [{}uint8]
/; if (self.literal)
;self.val = self.val + v.val
;return ""
;; else if (!self.on_stack)
;{}uint8 tmp = self.get_norm_loc()
;{}uint8 out = "\tadd "
;add_strings(~out, ~tmp)
;out.append(',')
;out.append(' ')
;tmp = v.get_norm_loc()
;add_strings(~out, ~tmp)
;return out
;/
;/
/; sub_value (Value v) [{}uint8]
/; if (self.literal)
;self.val = self.val - v.val
;return ""
;/
;/
/; mul_literal (int i) [{}uint8]
/; if (self.literal)
;return ""
;/
;/
/; div_literal (int i) [{}uint8]
;/
/; mul_value (Value v) [{}uint8]
;/
/; div_value (int i) [{}uint8]
;/
/; deref_value [Value]
/; if (self._type.ptr == 0)
;/
;/
/; arr_value (int i) [Value]
/; if (self._type.ptr == 0)
;/
;/
/; copy_from_value (Value v)
;/
/; get_member_value ({}uint8 name) [Value]
;Value out = self
;/
/; update_loc(int offset)
/; if (self.on_stack)
;self.loc = self.loc + offset
;/
;/
;/
|