-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocedures.mx
More file actions
267 lines (249 loc) · 6.52 KB
/
procedures.mx
File metadata and controls
267 lines (249 loc) · 6.52 KB
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
%include "memory"
%using stack
%using MEMORY_LAYOUT
;; basic procedures -----------------------------------
;; handle only return address
; proc definition: after body expects retval in reg %where_retaddr
%macro _proc_impl(name, where_retaddr, body) {
jmp proc_(%name)_body_end ; skip over proc body
:proc_(%name) ; entry
%body
jmp(%where_retaddr)
:proc_(%name)_body_end
}
; proc definition - calling&returning, no args/locals/retval
; preserves r, expects retaddr @top
%macro proc(name, body) {
%_proc_impl(%name, m,
%body
%stack:drop() ; mov to retaddr
)
}
; call proc / func
%macro call(name) {
%push(2) ; ret addr offset
strap
jmp proc_(%name)
%stack:top() ; load retval for ctime
}
; call proc / func by ptr in r
%macro call_ptr() {
%push(2) ; ret addr offset
strap
jmpr
%stack:top()
}
; call func by ptr in seg[idx]
; if ptr is null do default_instrs instead
%macro call_ptr_with_default(ptr_seg, ptr_idx, default_instrs) {
%seg:move(%ptr_seg, %ptr_idx)
%if_else { lmeq 0,
%default_instrs
,
ldm
%call_ptr()
}
}
;; functions ---------------------------------
;; allow argument passing, local variables and returning
;; safe even with misaligned stack after function body
;; funcs properly preserve memory segments (args, locals) even when nested
; saves ARGS, LOCALS segment pointers of caller by pushing in function prologue (above retaddr)
%macro _push_segment_ptrs() {
; TODO optimization - save only in non-leaf funcs
mov %MEMORY_LAYOUT:G_ARGS_PTR
%stack:pushm()
mov %MEMORY_LAYOUT:G_LOCALS_PTR
%stack:pushm()
; TODO save G_this_ptr
}
; reset SP to expected length, restore caller's segments, pop retaddr -> r
%macro _rstSP_pop_segment_ptrs() {
%load(%MEMORY_LAYOUT:G_LOCALS_PTR)
%storer(%MEMORY_LAYOUT:G_STACK_PTR)
strs 1 ; SP = locals-1
%stack:pop()
%storer(%MEMORY_LAYOUT:G_LOCALS_PTR)
%stack:pop()
%storer(%MEMORY_LAYOUT:G_ARGS_PTR)
%stack:pop() ; retaddr
}
; TODO auto reserve space for retval when args = 0
; safe when stack grows in func body
; volatile r on entry/exit
%macro _func_impl(name, args, drop_args, locals, body) {
; function frame: stack top of prev func-| A1..An retaddr seg_args seg_locals L1..Ln
%_proc_impl(%name, r,
%_push_segment_ptrs()
ldh ; r = &retaddr + 2 (segments)
%storer(%G_LOCALS_PTR) ; LOCALS = top() + 1
stra 1
%stack:reserve(%locals) ; reserve space for locals (above saved segments)
lds !op(a, %args, 2)
%storer(%G_ARGS_PTR) ; ARGS = &retaddr - #args
%body
:return_(%name) ; function epilogue - jump here to exit func (doesn't save retval!)
%_rstSP_pop_segment_ptrs()
%stack:dropN(%drop_args) ; drop #drop_args
; retaddr in r
)
}
; args on stack
; args, locals - number of vars to reserve
; volatile r
%macro void_func(name, args, locals, body) {
%_func_impl(%name, %args, %args, %locals,
%body
)
}
; caller: push args on stack - auto removed after, retval appears "pushed" instead
; callee: returns value in r on function end (see function returning)
; args, locals - number of vars to reserve
; !! args must be >= 1 (space for retval)
%macro func(name, args, locals, body) {
%_func_impl(%name, %args, !op(s, %args, 1), %locals,
%body
%save_retval() ; return r
)
}
; same as %func + saves first arg to G_THIS_PTR
; first argument must be ptr to instance
%macro method(name, args, locals, body) {
%func(%name, %args, %locals,
%seg:arg(0, %storem(%this))
%body
)
}
%macro void_method(name, args, locals, body) {
%void_func(%name, %args, %locals,
%seg:arg(0, %storem(%this))
%body
)
}
;; function returning ------------------------
; saves retval from r
%macro save_retval() {
%seg:arg(0, strr)
}
; ends function execution without affecting retval
%macro returnFunc(funcName) {
jmp return_(%funcName)
}
; return value in r and exit function
%macro returnr(funcName) {
%save_retval()
%returnFunc(%funcName)
}
%macro return_imm(funcName, imm) {
%seg:arg(0, str %imm)
%returnFunc(%funcName)
}
;; data segments ---------------------------------------------------
;; namespace seg is not meant for including
;; arg - current function arguments
;; local - current function local variables
;; this - current instance in methods
;; src - object pointed to by G_SRC
;; dest - object pointed to by G_DEST
;; temp - segment for temporary values - volatile!
%define arg (%MEMORY_LAYOUT:G_ARGS_PTR)
%define local (%MEMORY_LAYOUT:G_LOCALS_PTR)
%define this (%MEMORY_LAYOUT:G_THIS_PTR)
%define src (%MEMORY_LAYOUT:G_SRC)
%define dest (%MEMORY_LAYOUT:G_DEST)
%define temp_addr (%MEMORY_LAYOUT:SEG_TEMP) ; don't use as @name!
%namespace seg {
;; use ^ for name arg
; load segment address
%macro load_addr(name, idx) {
mov %name
ldma %idx
}
; push segment address
%macro push_seg(name) {
%load_addr(%name, 0)
%stack:pushr()
}
; push address of segment:idx
%macro push_addr(name, idx) {
%load_addr(%name, %idx)
%stack:pushr()
}
; set dest_segment to idx_seg:idx
%macro point(dest_seq, idx_seg, idx) {
%load_addr(%idx_seg, %idx)
%storer(%dest_seq)
}
; set dest_segment to pointer from idx_seg:idx
%macro pointptr(dest_seq, idx_seg, idx) {
%load(%idx_seg, %idx)
%storer(%dest_seq)
}
; move to segment
%macro move(name, idx) {
mov %name
movma %idx
}
; move to segment
%macro movptr(name, idx) {
%move(%name, %idx)
movm
}
; load segment value
%macro load(name, idx) {
%move(%name, %idx)
ldm
}
; store r in segment
%macro store(name, idx) {
%move(%name, %idx)
strr
}
; set value of segment to imm
%macro set(name, idx, imm) {
%move(%name, %idx)
str %imm
}
; push segment value
%macro push(name, idx) {
%move(%name, %idx)
%stack:pushm()
}
; pop stack to segment
%macro pop(name, idx) {
%stack:pop()
%store(%name, %idx)
}
%macro arg(idx, instrs) { %_func_seg(%MEMORY_LAYOUT:G_ARGS_PTR, %idx, %instrs) }
%macro local(idx, instrs) { %_func_seg(%MEMORY_LAYOUT:G_LOCALS_PTR, %idx, %instrs) }
%macro this(idx, instrs) { %_func_seg(%MEMORY_LAYOUT:G_THIS_PTR, %idx, %instrs) }
%macro src(idx, instrs) { %_func_seg(%MEMORY_LAYOUT:G_SRC, %idx, %instrs) }
%macro dest(idx, instrs) { %_func_seg(%MEMORY_LAYOUT:G_DEST, %idx, %instrs) }
%macro temp(idx, instrs) { mov !op(a, %MEMORY_LAYOUT:SEG_TEMP, %idx)
%instrs
}
;; impl
%macro _func_seg(ptr_addr, idx, after) {
mov %ptr_addr
movma %idx
%after
}
}
;; calling functions with constant arguments
%macro callWith1Arg(name, argument) {
%push(%argument)
%call(%name)
}
%macro callWith2Arg(name, a, b) {
%push(%a)
%push(%b)
%call(%name)
}
%macro callWith3Arg(name, a, b, c) {
%push(%a)
%push(%b)
%push(%c)
%call(%name)
}
%include "control"
%include "math"