-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathptrlib.fj
102 lines (81 loc) · 2.89 KB
/
ptrlib.fj
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
// ---------- Init
ns stl {
// NOTE: must be placed just after the startup, so that the read_ptr_byte_table will be in address 256.
// Complexity: 2.5w+263
def ptr_init {
hex.pointers.ptr_init
bit.pointers.ptr_init
}
// Space Complexity: n+w/4
// Initializes a stack of size n (maximal capacity of n hexes / return-addresses).
// n is the size of the stack.
def stack_init n {
hex.pointers.stack_init n
}
}
// ---------- Functions
// @requires the stack_init.
ns stl {
// Time Complexity: ~2.5w@ (exact: w(2.25@+10) + 24@+51)
// Space Complexity: <3w@ (exact: w(2.625@+49) + 20@+231)
// Saves the return address to the stack and jumps to the given "address".
// When returned, it removes the return-address from the stack.
//
// note: the pop_ret_address is for the future return (counts as space, but not time, complexity).
// @requires the stack_init.
def call address @ return_label {
hex.push_ret_address return_label
;address
pad 2
return_label:
hex.pop_ret_address return_label
}
// Time Complexity: <3w@ (exact: w(2.25@+10) + 37@+57)
// Space Complexity: <3w@ (exact: w(2.625@+49) + 43@+251)
// Saves the return address to the stack and jumps to the given "address".
// When returned, it removes the return-address from the stack, and pops "params_stack_length" cells from the stack.
//
// note: the pop_ret_address is for the future return (counts as space, but not time, complexity).
// @requires the stack_init.
def call address, params_stack_length @ return_label {
hex.push_ret_address return_label
;address
pad 2
return_label:
hex.pop_ret_address return_label
hex.sp_sub params_stack_length
}
// Time Complexity: w(2@+7) + 9@+23
// Space Complexity: w(2.375@+34) + 5@+67
// Returns to the calling function (gets the return-address from the top of the stack).
//
// note: jumps to the last-call's pop_ret_address (which this macro counts as time, but not space, complexity).
// @requires the stack_init.
def return < hex.pointers.sp {
hex.ptr_jump hex.pointers.sp
}
// Time Complexity: n(2@)
// Space Complexity: n(2@+24)
// (Unsafe if dst overlaps with hex.pointers.sp).
// dst[:w/4] = sp
// @requires the stack_init.
def get_sp dst < hex.pointers.sp {
hex.mov w/4, dst, hex.pointers.sp
}
}
// ---------- Fast Call
ns stl {
// Complexity: @-1
// Jumps to label, and saves the return address in the given "ret_reg" variable.
def fcall label, ret_reg @ ret {
wflip ret_reg+w, ret, label
pad 2
ret:
wflip ret_reg+w, ret
}
// Complexity: 1
// Return into the address written in the "ret_reg" variable.
def fret ret_reg {
;ret_reg
}
}