/
NQPCORE.setting
618 lines (541 loc) · 19.5 KB
/
NQPCORE.setting
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# This file automatically generated by tools\build\gen-cat.pl
# From src\core\NativeTypes.pm
use nqp-mo;
# We'll re-export all of the HOWs that we import from the meta-objects
# library. (Questionable whether we should need to do this - maybe the
# set of mappings the setting has should just be taken as the default
# for any program using it. OTOH, there's not a good way to factor
# this just yet.)
my module EXPORTHOW {
($?PACKAGE.WHO)<module> := NQPModuleHOW;
($?PACKAGE.WHO)<class> := NQPClassHOW;
($?PACKAGE.WHO)<class-attr> := NQPAttribute;
($?PACKAGE.WHO)<grammar> := NQPClassHOW;
($?PACKAGE.WHO)<grammar-attr> := NQPAttribute;
($?PACKAGE.WHO)<role> := NQPParametricRoleHOW;
($?PACKAGE.WHO)<role-attr> := NQPAttribute;
($?PACKAGE.WHO)<native> := NQPNativeHOW;
}
my native int is repr('P6int') { }
my native num is repr('P6num') { }
my native str is repr('P6str') { }
# From src\core\NQPRoutine.pm
my knowhow NQPRoutine {
has $!do;
has $!signature;
has $!dispatchees;
has $!dispatch_cache;
has $!dispatch_order;
# Adds a multi-dispatch candidate.
method add_dispatchee($code) {
#pir::nqp_disable_sc_write_barrier__v();
$!dispatch_cache := nqp::null();
$!dispatch_order := nqp::null();
nqp::push($!dispatchees, $code);
#pir::nqp_enable_sc_write_barrier__v();
}
# Checks if this code object is a dispatcher.
method is_dispatcher() {
nqp::defined($!dispatchees)
}
method derive_dispatcher() {
# Clone the underlying VM code ref.
my $do := nqp::clone($!do);
# Clone and attach the code object.
my $der := nqp::clone(self);
nqp::bindattr($der, NQPRoutine, '$!do', $do);
nqp::bindattr($der, NQPRoutine, '$!dispatchees', nqp::clone($!dispatchees));
nqp::setcodeobj($do, $der);
# If needed, arrange for a fixup of the cloned code-ref.
# my $clone_callback := pir::getprop__PPs($!do, 'CLONE_CALLBACK');
# if nqp::defined($clone_callback) {
# $clone_callback($!do, $do, $der);
# }
$der
}
# Checks if one type is narrower than the other.
sub is_narrower_type($a, $b) {
# If one of the types is null, then we know that's automatically
# wider than anything.
if nqp::isnull($b) && !nqp::isnull($a) { 1 }
elsif nqp::isnull($a) || nqp::isnull($b) { 0 }
else { nqp::istype($a, $b) }
}
# Sorts the dispatchees. Puts nulls between groups that are of equal weight.
# The most specific group comes first.
my $SLURPY_ARITY := nqp::bitshiftl_i(1, 30);
my $EDGE_REMOVAL_TODO := -1;
my $EDGE_REMOVED := -2;
my $DEFINED_ONLY := 1;
my $UNDEFINED_ONLY := 2;
method sort_dispatchees() {
# Takes two candidates and determines if the first one is narrower than the
# second. Returns a true value if they are.
sub is_narrower(%a, %b) {
# Work out how many parameters to compare, factoring in slurpiness
# and optionals.
my $types_to_check;
if %a<num_types> == %b<num_types> {
$types_to_check := %a<num_types>;
}
elsif %a<min_arity> == %b<min_arity> {
$types_to_check := %a<num_types> > %b<num_types> ?? %b<num_types> !! %a<num_types>;
}
else {
return 0;
}
# Analyse each parameter in the two candidates.
my $i := 0;
my $narrower := 0;
my $tied := 0;
while $i < $types_to_check {
my $type_obj_a := %a<types>[$i];
my $type_obj_b := %b<types>[$i];
if nqp::eqaddr($type_obj_a, $type_obj_b) {
$tied++;
}
elsif is_narrower_type($type_obj_a, $type_obj_b) {
$narrower++;
}
elsif !is_narrower_type($type_obj_b, $type_obj_a) {
$tied++;
}
$i++;
}
# If one is narrower than the other from current analysis, we're done.
if $narrower >= 1 && $narrower + $tied == $types_to_check {
return 1;
}
# If they aren't tied, we're also done.
elsif $tied != $types_to_check {
return 0;
}
# Otherwise, we see if one has a slurpy and the other not. A lack of
# slurpiness makes the candidate narrower. Otherwise, they're tied.
return %a<max_arity> != $SLURPY_ARITY && %b<max_arity> == $SLURPY_ARITY;
}
# Create a node for each candidate in the graph.
my @graph;
my $num_candidates := nqp::elems($!dispatchees);
my $i := 0;
while $i < $num_candidates {
# Get hold of signature, types and definednesses.
my $candidate := $!dispatchees[$i];
my $multi_sig := $candidate.signature;
my @types_list := $multi_sig.types;
my @definedness_list := $multi_sig.definednesses;
my $sig_elems := nqp::elems(@types_list);
# Type information.
my %info := nqp::hash(
'sub', $candidate,
'types', [],
'definednesses', [],
'min_arity', 0,
'max_arity', 0,
'num_types', 0
);
my %significant_param := 0;
my $j := 0;
while $j < $sig_elems {
# XXX TODO: Worry about optional and slurpy later.
%info<max_arity>++;
%info<min_arity>++;
# Record type info for this parameter. */
nqp::push(%info<types>, @types_list[$j]);
nqp::push(%info<definednesses>, @definedness_list[$j]);
%info<num_types>++;
$j++;
}
# Add it to graph node, and initialize list of edges.
nqp::push(@graph, nqp::hash(
'info', %info,
'edges', [],
'edges_in', 0,
'edges_out', 0
));
$i++;
}
# Now analyze type narrowness of the candidates relative to each other
# and create the edges.
$i := 0;
while $i < $num_candidates {
my $j := 0;
while $j < $num_candidates {
if ($i != $j) {
if is_narrower(@graph[$i]<info>, @graph[$j]<info>) {
@graph[$i]<edges>[@graph[$i]<edges_out>] := @graph[$j];
@graph[$i]<edges_out>++;
@graph[$j]<edges_in>++;
}
}
$j++;
}
$i++;
}
# Perform the topological sort.
my @result;
my $candidates_to_sort := $num_candidates;
while $candidates_to_sort > 0 {
my $rem_results := nqp::elems(@result);
# Find any nodes that have no incoming edges and add them to
# results.
my $i := 0;
while $i < $num_candidates {
if @graph[$i]<edges_in> == 0 {
# Add to results.
nqp::push(@result, @graph[$i]<info>);
$candidates_to_sort--;
@graph[$i]<edges_in> := $EDGE_REMOVAL_TODO;
}
$i++;
}
if $rem_results == nqp::elems(@result) {
nqp::die("Circularity detected in multi sub types");
}
# Now we need to decrement edges in counts for things that had
# edges from candidates we added here.
$i := 0;
while $i < $num_candidates {
if @graph[$i]<edges_in> == $EDGE_REMOVAL_TODO {
my $j := 0;
while $j < @graph[$i]<edges_out> {
@graph[$i]<edges>[$j]<edges_in>--;
$j++;
}
@graph[$i]<edges_in> := $EDGE_REMOVED;
}
$i++;
}
# Add gap between groups.
nqp::push(@result, nqp::null());
}
# Add final null sentinel.
nqp::push(@result, nqp::null());
return @result;
}
method dispatch($capture) {
# Count arguments.
my $num_args := nqp::captureposelems($capture);
# Get list and number of candidates, triggering a sort if there are none.
my @candidates := $!dispatch_order;
if nqp::isnull(@candidates) {
#pir::nqp_disable_sc_write_barrier__v();
@candidates := $!dispatch_order := self.sort_dispatchees();
#pir::nqp_enable_sc_write_barrier__v();
1;
}
my $num_candidates := nqp::elems(@candidates);
# Initialize dispatcher state.
my @possibles;
# Go thorugh candidates.
my $type_mismatch;
my $type_check_count;
my $i;
my $cur_idx := 0;
my $cur_candidate;
while 1 {
$cur_candidate := @candidates[$cur_idx];
if nqp::isnull($cur_candidate) {
# If we have some possible candidate(s), we're done in this loop.
if nqp::elems(@possibles) {
last;
}
# Otherwise, we keep looping and looking, unless we really hit the end.
$cur_idx++;
if nqp::isnull(@candidates[$cur_idx]) {
last;
}
else {
next;
}
}
# Check if it's admissable by arity.
if $num_args < $cur_candidate<min_arity> || $num_args > $cur_candidate<max_arity> {
$cur_idx++;
next;
}
# Check if it's admissable by type.
$type_check_count := $cur_candidate<num_types> > $num_args
?? $num_args
!! $cur_candidate<num_types>;
$type_mismatch := 0;
$i := 0;
while $i < $type_check_count {
my $param := nqp::captureposarg($capture, $i);
my $param_type := $param.WHAT;
my $type_obj := $cur_candidate<types>[$i];
my $definedness := $cur_candidate<definednesses>[$i];
unless nqp::eqaddr($param_type, $type_obj) || is_narrower_type($param_type, $type_obj) {
$type_mismatch := 1;
last;
}
if $definedness {
# Have a constraint on the definedness.
my $defined := nqp::isnull($param_type) ?? nqp::defined($param) !! nqp::isconcrete($param);
if (!$defined && $definedness == $DEFINED_ONLY) || ($defined && $definedness == $UNDEFINED_ONLY) {
$type_mismatch := 1;
last;
}
}
$i++;
}
if $type_mismatch {
$cur_idx++;
next;
}
# If we get here, it's an admissable candidate; add to list. */
nqp::push(@possibles, $cur_candidate);
$cur_idx++;
}
# Cache the result if there's a single chosen one and return it.
if nqp::elems(@possibles) == 1 {
my $result := @possibles[0]<sub>;
#pir::nqp_disable_sc_write_barrier__v();
$!dispatch_cache := nqp::multicacheadd($!dispatch_cache, $capture, $result);
#pir::nqp_enable_sc_write_barrier__v();
$result
}
elsif nqp::elems(@possibles) == 0 {
# XXX Include possible candidate signatures.
nqp::die("No applicable candidates found to dispatch to for '" ~ self.name ~ "'.")
}
else {
# XXX Include ambiguous candidate signatures.
nqp::die("Ambiguous dispatch to multi '" ~ self.name ~ "'.")
}
}
method clone() {
# Clone the underlying VM code ref.
my $do := nqp::clone($!do);
# Clone and attach the code object.
my $der := nqp::clone(self);
nqp::bindattr($der, NQPRoutine, '$!do', $do);
nqp::setcodeobj($do, $der);
# If needed, arrange for a fixup of the cloned code-ref.
# my $clone_callback := pir::getprop__PPs($!do, 'CLONE_CALLBACK');
# if nqp::defined($clone_callback) {
# $clone_callback($!do, $do, $der);
# }
$der
}
method !set_name($name) {
nqp::setcodename($!do, $name);
}
method name() {
nqp::getcodename($!do)
}
method signature() { $!signature }
}
nqp::setinvokespec(NQPRoutine, NQPRoutine, '$!do', nqp::null);
nqp::setboolspec(NQPRoutine, 5, nqp::null());
#pir::stable_publish_vtable_handler_mapping__vPP(NQPRoutine,
# nqp::hash('get_string', nqp::list(NQPRoutine, '$!do')));
my knowhow NQPSignature {
has $!types;
has $!definednesses;
method types() { $!types }
method definednesses() { $!definednesses }
}
my knowhow NQPRegex {
has $!do;
has $!caps;
has $!nfa;
has %!alt_nfas;
has $!generic_nfa;
has @!nested_codes;
method SET_CAPS($caps) {
$!caps := $caps;
}
method SET_NFA($nfa) {
$!nfa := $nfa;
}
method SET_ALT_NFA($name, $nfa) {
nqp::ifnull(%!alt_nfas, %!alt_nfas := {});
%!alt_nfas{$name} := $nfa;
}
method SET_GENERIC_NFA($nfa) {
$!generic_nfa := $nfa.save();
}
method ADD_NESTED_CODE($code) {
nqp::ifnull(@!nested_codes, @!nested_codes := nqp::list());
nqp::push(@!nested_codes, $code);
}
method CAPS() {
$!caps
}
method NFA() {
$!nfa
}
method ALT_NFAS() {
nqp::isnull(%!alt_nfas) ?? nqp::hash() !! %!alt_nfas
}
method ALT_NFA(str $name) {
nqp::isnull(%!alt_nfas) ?? nqp::null() !! %!alt_nfas{$name}
}
method NESTED_CODES() {
nqp::isnull(@!nested_codes) ?? nqp::list() !! @!nested_codes
}
method clone() {
# Clone the underlying VM code ref.
my $do := nqp::clone($!do);
# Clone and attach the code object.
my $der := nqp::clone(self);
nqp::bindattr($der, NQPRegex, '$!do', $do);
nqp::setcodeobj($do, $der);
# If needed, arrange for a fixup of the cloned code-ref.
# my $clone_callback := pir::getprop__PPs($!do, 'CLONE_CALLBACK');
# if nqp::defined($clone_callback) {
# $clone_callback($!do, $do, $der);
# }
$der
}
my $nfa_type;
method SET_NFA_TYPE($type) {
$nfa_type := $type;
}
method instantiate_generic($env) {
if nqp::isnull($!generic_nfa) {
self
}
else {
my $ins := self.clone();
nqp::bindattr($ins, NQPRegex, '$!nfa',
$nfa_type.from_saved($!generic_nfa).instantiate_generic($env).save());
nqp::bindattr($ins, NQPRegex, '$!generic_nfa', nqp::null());
$ins
}
}
method name() {
nqp::getcodename($!do)
}
method !set_name($name) {
nqp::setcodename($!do, $name);
}
}
nqp::setinvokespec(NQPRegex, NQPRegex, '$!do', nqp::null);
nqp::setboolspec(NQPRegex, 5, nqp::null());
#pir::stable_publish_vtable_handler_mapping__vPP(NQPRegex,
# nqp::hash('get_string', nqp::list(NQPRegex, '$!do')));
# From src\core\NQPMu.pm
my class NQPMu {
method CREATE() {
nqp::create(self)
}
method bless(NQPMu:U $self: *%attributes) {
my $instance := self.CREATE();
$instance.BUILDALL(|%attributes);
$instance
}
method BUILDALL(NQPMu:D $self: *%attrinit) {
# Get the build plan.
my $build_plan := self.HOW.BUILDALLPLAN(self);
my $count := nqp::elems($build_plan);
my $i := 0;
while $i < $count {
my $task := nqp::atpos($build_plan, $i);
my $code := nqp::atpos($task, 0);
$i := $i + 1;
if nqp::iseq_i($code, 0) {
# Custom BUILD call.
nqp::atpos($task, 1)(self, |%attrinit);
}
elsif nqp::iseq_i($code, 1) {
# See if we have a value to initialize this attr with.
my $key_name := nqp::atpos($task, 2);
if nqp::existskey(%attrinit, $key_name) {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 3), %attrinit{$key_name});
}
}
elsif nqp::iseq_i($code, 2) {
# See if we have a value to initialize this attr with;
# if not, set it to an empty array.
my $key_name := nqp::atpos($task, 2);
if nqp::existskey(%attrinit, $key_name) {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 3), %attrinit{$key_name});
}
else {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 3), nqp::list());
}
}
elsif nqp::iseq_i($code, 3) {
# See if we have a value to initialize this attr with;
# if not, set it to an empty array.
my $key_name := nqp::atpos($task, 2);
if nqp::existskey(%attrinit, $key_name) {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 3), %attrinit{$key_name});
}
else {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 3), nqp::hash());
}
}
elsif nqp::iseq_i($code, 4) {
unless nqp::attrinited(self, nqp::atpos($task, 1), nqp::atpos($task, 2)) {
nqp::bindattr(self, nqp::atpos($task, 1), nqp::atpos($task, 2),
nqp::atpos($task, 3)(self,
nqp::getattr(self, nqp::atpos($task, 1), nqp::atpos($task, 2))));
}
}
else {
nqp::die("Invalid BUILDALLPLAN");
}
}
self
}
method new(*%attributes) {
self.bless(|%attributes);
}
}
sub print(*@args) {
for @args {
nqp::print($_);
}
1;
}
sub say(*@args) {
print(|@args, "\n");
}
sub join($delim, @things) {
my @strs;
for @things { nqp::push(@strs, ~$_) }
nqp::join($delim, @strs)
}
# vim: ft=perl6
# From src\core\Hash.pm
sub hash(*%new) {
%new
}
# From src\core\testing.pm
my $test_counter := 0;
my $todo_upto_test_num := 0;
my $todo_reason := '';
sub plan($quantity) {
say("1..$quantity");
}
sub ok($condition, $desc?) {
$test_counter := $test_counter + 1;
unless $condition {
print("not ");
}
print("ok $test_counter");
if $desc {
print(" - $desc");
}
if $test_counter <= $todo_upto_test_num {
print($todo_reason);
}
print("\n");
$condition ?? 1 !! 0
}
sub todo($reason, $count) {
$todo_upto_test_num := $test_counter + $count;
$todo_reason := "# TODO $reason";
}
sub skip($desc) {
$test_counter := $test_counter + 1;
say("ok $test_counter # SKIP $desc\n");
}
# vim: ft=perl6
# From src\core\YOUAREHERE.pm
1;
{YOU_ARE_HERE}
# vim: set ft=perl6 nomodifiable :