-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional-y86.s
229 lines (187 loc) · 5.14 KB
/
functional-y86.s
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
# FIRST CLASS FUNCTIONS IN y86
# (a solution to http://www.rosettacode.org/wiki/First-class_functions)
# using self-modifying code.
# (run in sequential: ANY pipelining ruins this. Instructions overwrite each
# other so we have a data hazard in FETCH)
irmovq stack, %rsp
irmovq fnstack, %rbp
jmp main
# Example functional program
.pos 0x100
main:
irmovq addeight, %rdi
irmovq endaddeight, %rsi
call fnalloc
rrmovq %rax, %r12 # %r12 = pointer to static (x -> (+8) x) function
irmovq addten, %rdi
irmovq endaddten, %rsi
call fnalloc # %rax = pointer to static (x -> (+10) x) function
rrmovq %rax, %rdi
rrmovq %r12, %rsi
call fncompose # %rax = pointer to static (x -> ((+10) . (+8)) x) function
rrmovq %rax, %rdi
irmovq 13, %rsi
call fnexecute # evalute. %rax = ((+10) . (+8)) 13 = 31
halt
################################################
## FUNCTIONAL OPERATORS
# fnexecute
# Excecutes the funciton pointed to by %rdi with single argument %rsi and
# returns the result
.pos 0x500
fnexecute:
irmovq fnjmp, %rdx
rmmovq %rdi, 0x1(%rdx)
rrmovq %rsi, %rdi
fnjmp:
call invalidfn
ret
# fnalloc
# Takes a valid (single ret terminated), allocates space on the function stack,
# and returns address of the now static function. Copies in 8-byte chunks.
# arguments:
# %rdi = start of the function to be copied
# %rsi = end of function to be copied
# effects:
# - function is copied to the top of the fnstack
# - %rax = start of function to be copied
# - %rbp is updated to point to the new stack top
fnalloc:
rrmovq %rbp, %rax
irmovq 0x8, %r8
jmp loopcheck
looptop:
mrmovq 0(%rdi), %r11
rmmovq %r11, 0(%rbp)
addq %r8, %rbp
addq %r8, %rdi
loopcheck:
# if (rdi < rsi) keep copying <=> if (rdi - rsi < 0) goto top
# otherwise, we have copied everything.
rrmovq %rdi, %r11
subq %rsi, %r11
jl looptop
# Allocate 8 extra bytes for and overwrite garbage with HALT: if a function
# doesn't return we want to terminate. Not strictly nessecary.
irmovq 0, %r10
rmmovq %r10, 0(%rbp)
rrmovq %rbp, %r9
subq %r11, %r9
rmmovq %r10, 0(%r9)
addq %r8, %rbp
ret
# fncompose
# Allocates a new function on the static function stack which is the composition of the
# two functions %rdi and %rsi, and returns the address.
# %rdi = address of outer function
# %rsi = address of inner function
fncompose:
irmovq TCompose, %rdx
rmmovq %rsi, 2(%rdx)
rmmovq %rdi, 23(%rdx)
irmovq TCompose, %rdi
irmovq TComposeEnd, %rsi
call fnalloc
ret
# The following template will be overwritten and allocated as a new static fn
TCompose:
irmovq 0xDEADBEEF, %rdi
call fnexecute
rrmovq %rax, %rsi
irmovq 0xDEADBEEF, %rdi
call fnexecute
ret
TComposeEnd:
################################################
## STACK
.pos 0x2000
stack:
################################################
## PRIMITIVE FUNCTIONS
.pos 0x3000
invalidfn:
halt
addeight:
irmovq 0x8, %r8
rrmovq %rdi, %rax
addq %r8, %rax
ret
endaddeight:
addten:
irmovq 10, %r8
rrmovq %rdi, %rax
addq %r8, %rax
ret
endaddten:
################################################
## STATICALLY ALLOCATED FUNCTION STACK
.pos 0x2000
stack:
# Static function stack
# Grows upwards (%rbp being the pointer to the top).
.pos 0x10000
fnstack:
# I have this initialized to invalid instructions just so we can see it
# in the simulator
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0C0C0C0C0
.quad 0xC0C0C0C0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