This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
x86.stas
441 lines (398 loc) · 6.6 KB
/
x86.stas
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const Reg.rbx { 0 }
const Reg.rsi { 1 }
const Reg.rdi { 2 }
const Reg.r8 { 3 }
const Reg.r9 { 4 }
const Reg.r10 { 5 }
const Reg.r11 { 6 }
const Reg.r12 { 7 }
const Reg.r13 { 8 }
const Reg.r14 { 9 }
const Reg.r15 { 10 }
const Reg.rcx { 11 }
const Reg.rdx { 12 }
const Reg.rax { 13 }
const sizeof(Reg) { 14 }
; (Reg -- str len)
fn Reg.64.to_str 1 2 {
dup Reg.rbx = if {
"rbx"
} elif dup Reg.rcx = {
"rcx"
} elif dup Reg.rsi = {
"rsi"
} elif dup Reg.rdi = {
"rdi"
} elif dup Reg.r8 = {
"r8"
} elif dup Reg.r9 = {
"r9"
} elif dup Reg.r10 = {
"r10"
} elif dup Reg.r11 = {
"r11"
} elif dup Reg.r12 = {
"r12"
} elif dup Reg.r13 = {
"r13"
} elif dup Reg.r14 = {
"r14"
} elif dup Reg.r15 = {
"r15"
} elif dup Reg.rdx = {
"rdx"
} elif dup Reg.rax = {
"rax"
} else {
0 0 0 assert -> 'unreachable'
}
rot rot drop
}
; (Reg -- str len)
fn Reg.32.to_str 1 2 {
dup Reg.rbx = if {
"ebx"
} elif dup Reg.rcx = {
"ecx"
} elif dup Reg.rsi = {
"esi"
} elif dup Reg.rdi = {
"edi"
} elif dup Reg.r8 = {
"r8d"
} elif dup Reg.r9 = {
"r9d"
} elif dup Reg.r10 = {
"r10d"
} elif dup Reg.r11 = {
"r11d"
} elif dup Reg.r12 = {
"r12d"
} elif dup Reg.r13 = {
"r13d"
} elif dup Reg.r14 = {
"r14d"
} elif dup Reg.r15 = {
"r15d"
} elif dup Reg.rdx = {
"edx"
} elif dup Reg.rax = {
"eax"
} else {
0 0 0 assert -> 'unreachable'
}
rot rot drop
}
; (Reg -- str len)
fn Reg.16.to_str 1 2 {
dup Reg.rbx = if {
"bx"
} elif dup Reg.rcx = {
"cx"
} elif dup Reg.rsi = {
"si"
} elif dup Reg.rdi = {
"di"
} elif dup Reg.r8 = {
"r8w"
} elif dup Reg.r9 = {
"r9w"
} elif dup Reg.r10 = {
"r10w"
} elif dup Reg.r11 = {
"r11w"
} elif dup Reg.r12 = {
"r12w"
} elif dup Reg.r13 = {
"r13w"
} elif dup Reg.r14 = {
"r14w"
} elif dup Reg.r15 = {
"r15w"
} elif dup Reg.rdx = {
"dx"
} elif dup Reg.rax = {
"ax"
} else {
0 0 0 assert -> 'unreachable'
}
rot rot drop
}
; (Reg -- str len)
fn Reg.8.to_str 1 2 {
dup Reg.rbx = if {
"bl"
} elif dup Reg.rcx = {
"cl"
} elif dup Reg.rsi = {
"sil"
} elif dup Reg.rdi = {
"dil"
} elif dup Reg.r8 = {
"r8b"
} elif dup Reg.r9 = {
"r9b"
} elif dup Reg.r10 = {
"r10b"
} elif dup Reg.r11 = {
"r11b"
} elif dup Reg.r12 = {
"r12b"
} elif dup Reg.r13 = {
"r13b"
} elif dup Reg.r14 = {
"r14b"
} elif dup Reg.r15 = {
"r15b"
} elif dup Reg.rdx = {
"dl"
} elif dup Reg.rax = {
"al"
} else {
0 0 0 assert -> 'unreachable'
}
rot rot drop
}
; ( str len -- Reg bool )
fn Reg.64.from_str? 2 2 {
auto str 2 pop str
str "rbx" streq if {
Reg.rbx
} elif str "rcx" streq {
Reg.rcx
} elif str "rsi" streq {
Reg.rsi
} elif str "rdi" streq {
Reg.rdi
} elif str "r8" streq {
Reg.r8
} elif str "r9" streq {
Reg.r9
} elif str "r10" streq {
Reg.r10
} elif str "r11" streq {
Reg.r11
} elif str "r12" streq {
Reg.r12
} elif str "r13" streq {
Reg.r13
} elif str "r14" streq {
Reg.r14
} elif str "r15" streq {
Reg.r15
} elif str "rdx" streq {
Reg.rdx
} elif str "rax" streq {
Reg.rax
} else {
-1 false ret
}
true
}
reserve rallocator_mask sizeof(Reg)
const rallocator_stack.cap { 1024 2 * }
auto rallocator_stack.len 1
reserve rallocator_stack { rallocator_stack.cap sizeof(u8) * }
; (Reg)
fn rallocator_stack<< 1 0 {
dup sizeof(Reg) < assert
rallocator_stack.len rallocator_stack.cap < assert
rallocator_stack rallocator_stack.len + swap w8
rallocator_stack.len ++ pop rallocator_stack.len
}
; ( -- Reg)
fn rallocator_stack.top 0 1 {
rallocator_stack.len 0 > assert
rallocator_stack.len 1 - rallocator_stack + r8
}
; ( -- Reg)
fn rallocator_stack.pop 0 1 {
rallocator_stack.top
rallocator_stack.len -- pop rallocator_stack.len
}
fn rallocator_mask.dump 0 0 {
auto f 1
false pop f
`[` putc
0
while dup sizeof(Reg) < {
dup rallocator_mask + r8 if {
f if {
", " puts
}
dup Reg.64.to_str puts
true pop f
} else {
false pop f
}
++
}
drop
`]` putc `\n` putc
}
; (Reg)
fn r_b_clear 1 0 {
dup sizeof(Reg) < assert
rallocator_mask + false w8
}
; (Reg)
fn r_b_set 1 0 {
dup sizeof(Reg) < assert
rallocator_mask + true w8
}
; (Reg -- bool)
fn r_is_used 1 1 {
dup sizeof(Reg) < assert
rallocator_mask + r8
}
; ( -- Reg)
fn r_ffs 0 1 {
0
while dup sizeof(Reg) < {
dup rallocator_mask + r8 ! if {
ret
}
++
}
drop
UINT64_MAX
}
; (Reg -- idx)
fn r_index 1 1 {
auto rg 1 pop rg
0
while dup rallocator_stack.len < {
dup rallocator_stack + r8 rg = if {
ret
}
++
}
drop
UINT64_MAX
}
fn r_stack_deep_flush {
0
while dup rallocator_stack.len < {
dup rallocator_stack + r8
dup r_b_clear
" push " fwrite Reg.64.to_str fwriteln
++
}
drop
0 pop rallocator_stack.len
}
fn r_flush {
0
while dup rallocator_stack.len < {
dup rallocator_stack + r8
" push " fwrite Reg.64.to_str fwriteln
++
}
drop
0 pop rallocator_stack.len
rallocator_mask 0 sizeof(Reg) memset
}
; (Reg)
fn r_alloc 0 1 {
r_ffs dup
UINT64_MAX = if {
drop
rallocator_stack.len 0 != assert
r_stack_deep_flush
r_alloc
}
}
; (Reg)
fn r_release 1 0 {
auto rg 1 pop rg
rg r_is_used if {
auto rg_a 1
r_alloc pop rg_a
rg_a r_b_set
rg r_b_clear
" mov " fwrite rg_a Reg.64.to_str fwrite ", " fwrite rg Reg.64.to_str fwriteln
auto idx 1
rg r_index pop idx
idx UINT64_MAX != if {
rallocator_stack idx + rg_a w8
}
}
}
; (Reg)
fn r_free 1 0 {
r_b_clear
}
; ( -- Reg)
fn r_new 0 1 {
r_alloc
dup r_b_set
}
; (Reg)
fn r_new_r 1 0 {
dup r_release
r_b_set
}
; (Reg)
fn r_push 1 0 {
dup rallocator_stack<<
r_b_set
}
; ( -- Reg)
fn r_pop 0 1 {
rallocator_stack.len 0 > if {
rallocator_stack.pop
} else {
r_alloc
" pop " fwrite
dup Reg.64.to_str fwriteln
}
dup r_b_set ; chances are it is already set
}
; (Reg)
fn r_pop_r 1 0 {
auto rg 1 pop rg
rg r_is_used if {
rg r_release
}
rallocator_stack.len 0 > if {
auto rg_a 1
rallocator_stack.pop
pop rg_a
rg_a rg = if {
ret
}
rg_a r_b_clear
" mov " fwrite rg Reg.64.to_str fwrite ", " fwrite rg_a Reg.64.to_str fwriteln
} else {
rg r_is_used ! assert
" pop " fwrite rg Reg.64.to_str fwriteln
}
rg r_b_set
}
; ( -- Reg)
fn r_top 0 1 {
rallocator_stack.len 0 > if {
rallocator_stack.top
} else {
r_alloc dup r_push
" pop " fwrite
dup Reg.64.to_str fwriteln
}
}
; (Reg)
fn r_dup 1 0 {
r_alloc
dup r_push
" mov " fwrite Reg.64.to_str fwrite ", " fwrite Reg.64.to_str fwriteln
}
; (u64)
fn r_push_const 1 0 {
r_alloc
dup r_push
" mov " fwrite Reg.64.to_str fwrite ", " fwrite fwriteuln
}
fn r_prepare_push_const_word {
r_alloc
dup r_push
" mov " fwrite Reg.64.to_str fwrite ", " fwrite
}