/
control.pir
394 lines (313 loc) Β· 8.19 KB
/
control.pir
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
=head1 NAME
src/builtins/control.pir - control flow related functions
=head1 Functions
=over 4
=cut
.include 'except_types.pasm'
.include 'except_severity.pasm'
=item die
=cut
.sub '&die'
.param pmc list :slurpy
.local string message
.local pmc p6ex
.local pmc ex
.annotate 'invizible_frame', 1
message = join '', list
if message > '' goto have_message
message = "Died\n"
have_message:
p6ex = new ['Perl6Exception']
ex = root_new ['parrot';'Exception']
ex = message
ex['severity'] = .EXCEPT_FATAL
ex['type'] = .CONTROL_ERROR
setattribute p6ex, '$!exception', ex
set_global '$!', p6ex
throw ex
.return ()
.annotate 'invizible_frame', 0
.end
=item exit
=cut
.sub '&exit'
.param int status :optional
.param int has_status :opt_flag
if has_status goto x
status = 0
x:
exit status
.end
=item return
=cut
.sub '&return'
.param pmc retvals :slurpy
.local pmc ex, retval
ex = root_new ['parrot';'Exception']
ex['type'] = .CONTROL_RETURN
$I0 = elements retvals
if $I0 == 0 goto nil
if $I0 > 1 goto many
retval = retvals[0]
goto done
nil:
retval = '&Nil'()
goto done
many:
retval = '&infix:<,>'(retvals :flat)
done:
setattribute ex, 'payload', retval
throw ex
.end
=item warn
=cut
.sub '&warn'
.param pmc list :slurpy
.local string message
.local pmc p6ex
.local pmc ex
.annotate 'invizible_frame', 1
message = join '', list
if message > '' goto have_message
message = "Warning"
have_message:
p6ex = new ['Perl6Exception']
ex = root_new ['parrot';'Exception']
ex = message
ex['severity'] = .EXCEPT_WARNING
ex['type'] = .CONTROL_ERROR
setattribute p6ex, '$!exception', ex
set_global '$!', p6ex
throw ex
.return ()
.annotate 'invizible_frame', 0
.end
=item fail
=cut
# XXX Need to throw a Failure, not a Mu
# XXX Need to check for 'use fatal'
.sub '&fail'
.param pmc value :optional
.param int has_value :opt_flag
.local pmc ex, p6ex, failure, fatal
ex = root_new ['parrot';'Exception']
unless has_value goto no_value
ex['payload'] = value
$S0 = value
ex['message'] = value
no_value:
fatal = "!find_contextual"("$*FATAL")
unless fatal goto no_fatal
throw ex
no_fatal:
$P0 = get_hll_global 'Exception'
p6ex = $P0.'new'(ex)
$P0 = get_hll_global 'Failure'
failure = $P0.'new'(p6ex)
ex = root_new ['parrot';'Exception']
ex['payload'] = failure
ex['severity'] = .EXCEPT_ERROR
ex['type'] = .CONTROL_RETURN
throw ex
.end
=item proceed
=cut
.sub '&proceed'
.local pmc ex, p6ex
ex = root_new ['parrot';'Exception']
ex['severity'] = .EXCEPT_NORMAL
ex['type'] = .CONTROL_CONTINUE
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', ex
set_global '$!', p6ex
throw ex
.end
=item succeed
=cut
.sub '&succeed'
.param pmc arg :optional
.param int has_arg :opt_flag
.local pmc e, p6ex
e = root_new ['parrot';'Exception']
e['severity'] = .EXCEPT_NORMAL
e['type'] = .CONTROL_BREAK
unless has_arg, no_arg
e['payload'] = arg
no_arg:
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', e
set_global '$!', p6ex
throw e
.end
=item next
=cut
.sub '&next'
.local pmc e, p6ex
e = root_new ['parrot';'Exception']
e['severity'] = .EXCEPT_NORMAL
e['type'] = .CONTROL_LOOP_NEXT
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', e
set_global '$!', p6ex
throw e
.end
=item redo
=cut
.sub '&redo'
.local pmc e, p6ex
e = root_new ['parrot';'Exception']
e['severity'] = .EXCEPT_NORMAL
e['type'] = .CONTROL_LOOP_REDO
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', e
set_global '$!', p6ex
throw e
.end
=item last
=cut
.sub '&last'
.local pmc e, p6ex
e = root_new ['parrot';'Exception']
e['severity'] = .EXCEPT_NORMAL
e['type'] = .CONTROL_LOOP_LAST
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', e
set_global '$!', p6ex
throw e
.end
=item take
=cut
.sub '&take'
.param pmc values :slurpy
.local pmc ex, p6ex
ex = root_new ['parrot';'Exception']
ex['type'] = .CONTROL_TAKE
ex['severity'] = .EXCEPT_NORMAL
ex['message'] = 'take without gather'
$I0 = elements values
if $I0 == 0 goto nil
if $I0 > 1 goto many
values = values[0]
goto done
nil:
values = '&Nil'()
goto done
many:
values = '&infix:<,>'(values :flat)
done:
setattribute ex, 'payload', values
p6ex = new ['Perl6Exception']
setattribute p6ex, '$!exception', ex
set_global '$!', p6ex
throw ex
.return (values)
.end
=item callwith
=cut
.sub '&callwith'
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# For callwith, it's easy - just want to get the next candidate, call
# it and hand back it's return values. A tailcall does fine.
.local pmc clist, lexpad, self, next
get_next_candidate_info clist, $P0, lexpad
next = clone clist
next.'set_failure_mode'()
$P0 = deref next
push_eh not_method
self = lexpad['self']
pop_eh
.tailcall next(self, pos_args :flat, named_args :flat :named)
not_method:
pop_eh
.tailcall next(pos_args :flat, named_args :flat :named)
.end
=item nextwith
=cut
.sub '&nextwith'
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
# Find next candiate, invoke it and get its return value, then use
# return to return it as if it was from our original call.
.local pmc clist, lexpad, self, next, result
get_next_candidate_info clist, $P0, lexpad
next = clone clist
next.'set_failure_mode'()
$P0 = deref next
push_eh not_method
self = lexpad['self']
pop_eh
(result) = next(self, pos_args :flat, named_args :flat :named)
goto process_result
not_method:
pop_eh
(result) = next(pos_args :flat, named_args :flat :named)
process_result:
$I0 = isa result, ['Failure']
unless $I0 goto did_defer
$P0 = result.'exception'()
if null $P0 goto did_defer
$S0 = $P0.'Str'()
if $S0 != 'No method to defer to' goto did_defer
.return (result)
did_defer:
$P0 = root_new ['parrot';'Exception']
$P0['type'] = .CONTROL_RETURN
setattribute $P0, 'payload', result
throw $P0
.end
=item callsame
=cut
.sub '&callsame'
# Find next candidate as well as caller and lexpad.
.local pmc clist, routine, lexpad, next
get_next_candidate_info clist, routine, lexpad
next = clone clist
# Build arguments based upon what the caller was originall invoked with,
# and tailcall the next candidate.
.local pmc pos_args, named_args
$P1 = lexpad['call_sig']
(pos_args, named_args) = '!deconstruct_call_sig'($P1)
next.'set_failure_mode'()
.tailcall next(pos_args :flat, named_args :flat :named)
.end
=item nextsame
=cut
.sub '&nextsame'
# Find next candidate as well as caller and lexpad.
.local pmc clist, routine, lexpad, next
get_next_candidate_info clist, routine, lexpad
next = clone clist
# Build arguments based upon what the caller was originall invoked with,
# get the result of the next candidate and use return to retrun from
# the caller, provided the defer did not fail.
.local pmc pos_args, named_args, result
$P1 = lexpad['call_sig']
(pos_args, named_args) = '!deconstruct_call_sig'($P1)
next.'set_failure_mode'()
(result) = next(pos_args :flat, named_args :flat :named)
$I0 = isa result, ['Failure']
unless $I0 goto did_defer
$P0 = result.'exception'()
if null $P0 goto did_defer
$S0 = $P0.'Str'()
if $S0 != 'No method to defer to' goto did_defer
.return (result)
did_defer:
$P0 = root_new ['parrot';'Exception']
$P0['type'] = .CONTROL_RETURN
setattribute $P0, 'payload', result
throw $P0
.end
=item lastcall
Trims the candidate list so that nextsame/nextwith/callsame/callwith will
find nothing more to call.
=cut
.sub '&lastcall'
# Find candidate list and trim it.
.local pmc clist
get_next_candidate_info clist, $P0, $P1
clist.'trim_candidate_list'()
.end
=back
=cut