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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# Location enum
int VLOC_STCK = 2
int VLOC_LITL = 1
int VLOC_DATA = 0
int PTYPE_NONE = 2
int PTYPE_PTR = 1
int PTYPE_REF = 0
int PTYPE_ARR = 1
int PRIM_NON = 0
int PRIM_BOO = 1
int PRIM_INT = 2
int PRIM_FLT = 3
~uint8 PRIM_CSV_BOO = "bool\0"
~uint8 PRIM_CSV_INT = "int,int8,int16,int32,int64,uint,uint8,uint16,uint32,uint64\0"
~uint8 PRIM_CSV_FLT = "float,float32,float64\0"
# Should dispose of this constructed string
# 1-8 are ax, bx, cx, dx, si, di, sp, bp
# 9-16 are r8, r9, r10, r11, r12, r13, r14, r15
# 17-32 are xmm0, xmm1, xmm2, ..., xmm15
/; reg_string (int r, int size) [~uint8]
utils.Vector out
out.init(1)
uint8 add
/; if (r < 9)
/; if (size == 4)
add = 'e'
out.push(~add)
;; else if (size == 8)
add = 'r'
out.push(~add)
;/
add = 'a'
/; if (r < 5)
add = add + r - 1
;; else if (r == 5 || r == 7)
add = 's'
;; else if (r == 6)
add = 'd'
;; else if (r == 8)
add = 'b'
;/
out.push(~add)
/; if (r == 5 || r == 6)
add = 'i'
out.push(~add)
;; else if (r == 7 || r == 8)
add = 'p'
out.push(~add)
;; else if (size !== 1)
add = 'x'
out.push(~add)
;/
/; if (size == 1)
add = 'l'
out.push(~add)
;/
;; else if (r < 17)
add = 'r'
out.push(~add)
~uint8 num = utils.int_to_str(r - 1)
out.push_cstr(num)
_delete(num)
/; if (size == 1)
add = 'b'
out.push(~add)
;; else if (size == 2)
add = 'w'
out.push(~add)
;; else if (size == 4)
add = 'd'
out.push(~add)
;/
;; else if (r < 33)
out.push_cstr("xmm\0")
~uint8 num = utils.int_to_str(r - 17)
out.push_cstr(num)
_delete(num)
;/
return out.as_cstr()
;/
struct Var {
~uint8 name,
~Struct _type,
utils.Vector ptrc,
int loc
}
/; method Var
/; init (~uint8 name)
self.name = name
self.ptrc.init(4)
;/
/; ptr [int32]
~int32 i
i = self.ptrc.get(self.ptrc.count - 1)
return i`
;/
/; ptr_push (int32 p)
self.ptrc.push(~p)
;/
/; ptr_pop
self.ptrc.pop()
;/
/; is_primitive [int]
~uint8 tn = self`._type`.name
/; if (parse._in_csv(PRIM_CSV_BOO, tn) == true)
return PRIM_BOO
;; else if (parse._in_csv(PRIM_CSV_INT, tn) == true)
return PRIM_INT
;; else if (parse._in_csv(PRIM_CSV_FLT, tn) == true)
return PRIM_FLT
;/
return PRIM_NON
;/
/; end
_delete(self.name)
self.ptrc.end()
;/
###################################
# Variable manipulation functions #
###################################
# Generate a string which represents where the variable is in memory,
# this string may be used to set the value of the variable with operations like "mov"
# if "maybe_mem" is true, this might be an address like "[rsi]"
/; gen_to (bool maybe_mem) [~uint8]
utils.Vector out
out.init(1)
return out.as_cstr()
;/
# Generate a string which represents where the variable is in memory,
# this string may be used to read the value of the variable with operations like "mov"
# if "maybe_mem" is true, this might be an address like "[rsi]"
/; gen_from (bool maybe_mem) [~uint8]
utils.Vector out
out.init(1)
return out.as_cstr()
;/
# Returns true if the variable is stored in memory
/; in_mem [bool]
/; if (self.loc !> 0)
return true
;/
return false
;/
# Set this variable to the value of a literal
/; set_literal (~CompBuf buf, ~Var other)
;/
# Set this Variable to the value of other
/; set (~CompBuf buf, ~Var other)
;/
/; standard_op (~CompBuf buf, ~Var other, ~uint8 op_str)
~uint8 from_str
~uint8 to_str = self.gen_to(true)
/; if (self.in_mem())
from_str = other`.gen_from(false)
;; else
from_str = other`.gen_from(true)
;/
buf`.add_c(op_str)
buf`.add_c(" \0")
buf`.add_c(to_str)
buf`.add_c(", \0")
buf`.add_c(from_str)
buf`.add_c("\n\0")
_delete(from_str)
_delete(to_str)
;/
/; product_op (~CompBuf buf, ~Var other, ~uint8 op_str, int read_reg)
;/
/; add (~CompBuf buf, ~Var other)
/; if (self.loc = VLOC_LITL)
;/
self.standard_op("add")
;/
/; sub (~CompBuf buf, ~Var other)
self.standard_op("sub")
;/
/; mul (~CompBuf buf, ~Var other)
self.product_op(buf, other, "imul", 1)
;/
/; div (~CompBuf buf, ~Var other)
/; if ("signed")
self.product_op(buf, other, "idiv", 1)
;; else
self.product_op(buf, other, "div", 1)
;/
;/
/; mod (~CompBuf buf, ~Var other)
/; if ("signed")
self.product_op(buf, other, "idiv", 4)
;; else
self.product_op(buf, other, "div", 4)
;/
;/
/; and (~CompBuf buf, ~Var other)
self.standard_op("and")
;/
/; or (~CompBuf buf, ~Var other)
self.standard_op("or")
;/
/; xor (~CompBuf buf, ~Var other)
self.standard_op("xor")
;/
/; not (~CompBuf buf, ~Var other)
self.standard_op("xor")
;/
/; member (~CompBuf buf, ~uint8 name) [Var]
Var out
return out
;/
;/
|