forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.mpl
executable file
·202 lines (166 loc) · 4.8 KB
/
Function.mpl
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
"Function" module
"control" includeModule
"objectTools" includeModule
addContextToSignature: [
signature:;
args: 0 static @signature @;
options: 2 static @signature @;
(Natx) 0 static "context" @args move insertField
1 static @signature unwrapField
options codeRef
];
CALL_FUNC: [0nx];
DIE_FUNC: [1nx];
ASSIGN_FUNC: [2nx];
INIT_FUNC: [3nx];
Function: [{
CONTEXT_SIZE: [32 static];
schema CALL_FUNC_SCHEMA: addContextToSignature;
schema DIE_FUNC_SCHEMA: {this: Natx;} {} {} codeRef;
schema ASSIGN_FUNC_SCHEMA: {this: Natx; other: Natx;} {} {} codeRef;
schema INIT_FUNC_SCHEMA: {this: Natx;} {} {} codeRef;
contextData: Nat8 CONTEXT_SIZE array;
vtable: {functionIndex: Natx;} Natx {} codeRef;
assign: [
context0IsMoved: isMoved;
context0IsCodeRef: isCodeRef;
context0:;
release
context0IsCodeRef [
context: @contextData storageAddress Natx addressToReference;
@context0 storageAddress @context set
] [
context0IsMoved @context0 isCopyable or ~ [
0 .ERROR_CONTEXT_NEITHER_MOVED_NOR_COPYABLE
] when
@context0 storageSize CONTEXT_SIZE Natx cast > [
0 .ERROR_CONTEXT_SIZE_LARGER_THAN_FUNCTION_CONTEXT_SIZE
] when
@context0 storageSize 0nx static > [
context: contextData storageAddress @context0 addressToReference;
@context manuallyInitVariable
@context0 context0IsMoved moveIf @context set
] when
] if
schema contextType: @context0;
updateVtable
];
hasContext: [
CALL_FUNC vtable 0nx = ~
];
release: [
@contextData storageAddress DIE_FUNC vtable @DIE_FUNC_SCHEMA addressToReference call
makeEmptyVtable
];
updateVtable: [
schema CALL_FUNC_SCHEMA: @CALL_FUNC_SCHEMA;
schema DIE_FUNC_SCHEMA: @DIE_FUNC_SCHEMA;
schema ASSIGN_FUNC_SCHEMA: @ASSIGN_FUNC_SCHEMA;
schema INIT_FUNC_SCHEMA: @INIT_FUNC_SCHEMA;
[
functionIndex:;
@contextType isCodeRef contextIsCodeRef:; drop
functionIndex (
CALL_FUNC [
f: @CALL_FUNC_SCHEMA Ref;
contextIsCodeRef [@contextType storageSize 0nx static >] || [
[@contextType addressToReference call] !f
] [
[drop @contextType call] !f
] if
@f storageAddress
]
DIE_FUNC [
f: @DIE_FUNC_SCHEMA Ref;
contextIsCodeRef ~ [@contextType storageSize 0nx static >] && [
[@contextType addressToReference manuallyDestroyVariable] !f
] [
[drop] !f
] if
@f storageAddress
]
ASSIGN_FUNC [
f: @ASSIGN_FUNC_SCHEMA Ref;
@contextType isCopyable [
@contextType storageSize 0nx static > [
[
this: @contextType addressToReference;
other: @contextType addressToReference;
@other @this set
] !f
] [
[drop drop] !f
] if
] [
contextIsCodeRef [
[
this: Natx addressToReference;
other: Natx addressToReference;
@other @this set
] !f
] when
] if
@f storageAddress
]
INIT_FUNC [
f: @INIT_FUNC_SCHEMA Ref;
contextIsCodeRef ~ [@contextType storageSize 0nx static >] && [
[@contextType addressToReference manuallyInitVariable] !f
] [
[drop] !f
] if
@f storageAddress
]
[0nx]
) case
] !vtable
];
makeEmptyVtable: [
schema CALL_FUNC_SCHEMA: @CALL_FUNC_SCHEMA;
schema DIE_FUNC_SCHEMA: @DIE_FUNC_SCHEMA;
schema ASSIGN_FUNC_SCHEMA: @ASSIGN_FUNC_SCHEMA;
schema INIT_FUNC_SCHEMA: @INIT_FUNC_SCHEMA;
[
(
CALL_FUNC [0nx]
DIE_FUNC [
f: DIE_FUNC_SCHEMA Ref;
[drop] !f
@f storageAddress
]
ASSIGN_FUNC [
f: ASSIGN_FUNC_SCHEMA Ref;
[drop drop] !f
@f storageAddress
]
INIT_FUNC [
f: INIT_FUNC_SCHEMA Ref;
[drop] !f
@f storageAddress
]
[0nx]
) case
] !vtable
];
CALL: [
@contextData storageAddress CALL_FUNC vtable @CALL_FUNC_SCHEMA addressToReference call
];
INIT: [makeEmptyVtable];
ASSIGN: [
other:;
release
@other.@vtable @closure.!vtable
@closure.@contextData storageAddress INIT_FUNC vtable @INIT_FUNC_SCHEMA addressToReference call
@other.@contextData storageAddress @closure.@contextData storageAddress ASSIGN_FUNC vtable @ASSIGN_FUNC_SCHEMA addressToReference call
];
DIE: [release];
makeEmptyVtable
}];
makeFunction: [
signature:;
contextIsMoved: isMoved;
context:;
function: @signature Function;
@context contextIsMoved moveIf @function.assign
@function
];