/
Compiler.nqp
executable file
·593 lines (543 loc) · 24.1 KB
/
Compiler.nqp
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
class QAST::Compiler is HLL::Compiler {
# Holds information about the current register usage, for when
# we're allocating tempories.
my class RegAlloc {
has int $!cur_p;
has int $!cur_s;
has int $!cur_i;
has int $!cur_n;
method new($cur?) {
my $obj := nqp::create(self);
$cur ??
$obj.BUILD($cur.cur_p, $cur.cur_s, $cur.cur_i, $cur.cur_n) !!
$obj.BUILD(10, 10, 10, 10);
$obj
}
method BUILD($p, $s, $i, $n) {
$!cur_p := $p;
$!cur_s := $s;
$!cur_i := $i;
$!cur_n := $n;
}
method fresh_p() {
$!cur_p := $!cur_p + 1;
'$P' ~ $!cur_p
}
method fresh_s() {
$!cur_s := $!cur_s + 1;
'$S' ~ $!cur_s
}
method fresh_i() {
$!cur_i := $!cur_i + 1;
'$I' ~ $!cur_i
}
method fresh_n() {
$!cur_n := $!cur_n + 1;
'$N' ~ $!cur_n
}
method cur_p() { $!cur_p }
method cur_s() { $!cur_s }
method cur_i() { $!cur_i }
method cur_n() { $!cur_n }
}
our $serno;
INIT {
$serno := 10;
Q:PIR {
$P0 = find_lex '$?CLASS'
set_root_global ['parrot';'QAST'], 'Compiler', $P0
};
}
method unique($prefix = '') { $prefix ~ $serno++ }
method escape($str) { 'ucs4:"' ~ pir::escape__Ss($str) ~ '"' }
proto method as_post(*@args, *%_) { * }
multi method as_post(QAST::Block $node) {
# Fresh registers.
my $*REGALLOC := RegAlloc.new();
# DO ALL THE STUFFS
}
multi method as_post(QAST::Stmt $node) {
my $orig_reg := $*REGALLOC;
{
my $*REGALLOC := RegAlloc.new($orig_reg);
# COMPILE ALL THE NODES
}
}
multi method as_post(QAST::IVal $node) {
~$node.value
}
multi method as_post(QAST::NVal $node) {
~$node.value
}
multi method as_post(QAST::SVal $node) {
self.escape($node.value)
}
multi method as_post(QAST::BVal $node) {
my $cuid := self.escape($node.block.cuid);
my $reg := $*REGALLOC.fresh_p();
my $ops := self.post_new('Ops', :result($reg));
$ops.push_pirop(".const 'Sub' $reg = '$cuid'");
$ops;
}
multi method as_post(QAST::WVal $node) {
my $val := $node.value;
my $sc := pir::nqp_get_sc_for_object__PP($val);
my $handle := $sc.handle;
my $idx := $sc.slot_index_for($val);
my $reg := $*REGALLOC.fresh_p();
my $ops := self.post_new('Ops', :result($reg));
$ops.push_pirop('nqp_get_sc_object', $reg, self.escape($handle), ~$idx);
$ops;
}
multi method as_post(QAST::Regex $node) {
my $ops := self.post_new('Ops');
my $prefix := self.unique('rx') ~ '_';
my %*REG;
# build the list of (unique) registers we need
my $reglist := nqp::split(' ', 'tgt string pos int off int eos int rep int cur pmc curclass pmc bstack pmc cstack pmc');
while $reglist {
my $reg := nqp::shift($reglist);
my $name := %*REG{$reg} := $prefix ~ $reg;
$ops.push_pirop('.local ' ~ nqp::shift($reglist), $name);
}
# create our labels
my $startlabel := self.post_new('Label', :result($prefix ~ 'start'));
my $donelabel := self.post_new('Label', :result($prefix ~ 'done'));
my $restartlabel := self.post_new('Label', :result($prefix ~ 'restart'));
my $faillabel := self.post_new('Label', :result($prefix ~ 'fail'));
my $jumplabel := self.post_new('Label', :result($prefix ~ 'jump'));
my $cutlabel := self.post_new('Label', :result($prefix ~ 'cut'));
my $cstacklabel := self.post_new('Label', :result($prefix ~ 'cstack_done'));
%*REG<fail> := $faillabel;
# common prologue
my $startreg := '(' ~ nqp::join(', ', [%*REG<cur>, %*REG<tgt>, %*REG<pos>, %*REG<curclass>, %*REG<bstack>, '$I19']) ~ ')';
$ops.push_pirop('callmethod', '"!cursor_start"', 'self', :result($startreg));
$ops.push_pirop('store_lex', 'unicode:"$\x{a2}"', %*REG<cur>);
$ops.push_pirop('length', %*REG<eos>, %*REG<tgt>);
$ops.push_pirop('eq', '$I19', 1, $restartlabel);
$ops.push(self.regex_post($node));
$ops.push($restartlabel);
$ops.push_pirop('repr_get_attr_obj', %*REG<cstack>, %*REG<cur>, %*REG<curclass>, '"$!cstack"');
$ops.push($faillabel);
$ops.push_pirop('unless', %*REG<bstack>, $donelabel);
$ops.push_pirop('pop', '$I19', %*REG<bstack>);
$ops.push_pirop('if_null', %*REG<cstack>, $cstacklabel);
$ops.push_pirop('unless', %*REG<cstack>, $cstacklabel);
$ops.push_pirop('dec', '$I19');
$ops.push_pirop('set', '$P11', %*REG<cstack> ~ '[$I19]');
$ops.push($cstacklabel);
$ops.push_pirop('pop', %*REG<rep>, %*REG<bstack>);
$ops.push_pirop('pop', %*REG<pos>, %*REG<bstack>);
$ops.push_pirop('pop', '$I19', %*REG<bstack>);
$ops.push_pirop('lt', %*REG<pos>, -1, $donelabel);
$ops.push_pirop('lt', %*REG<pos>, 0, $faillabel);
$ops.push_pirop('eq', '$I19', 0, $faillabel);
# backtrack the cursor stack
$ops.push_pirop('if_null', %*REG<cstack>, $jumplabel);
$ops.push_pirop('elements', '$I18', %*REG<bstack>);
$ops.push_pirop('le', '$I18', 0, $cutlabel);
$ops.push_pirop('dec', '$I18');
$ops.push_pirop('set', '$I18', %*REG<bstack>~'[$I18]');
$ops.push($cutlabel);
$ops.push_pirop('assign', %*REG<cstack>, '$I18');
$ops.push($jumplabel);
$ops.push_pirop('jump', '$I19');
$ops.push($donelabel);
$ops.push_pirop('callmethod', '"!cursor_fail"', %*REG<cur>);
$ops.result(%*REG<cur>);
$ops;
}
method post_children($node) {
Q:PIR {
$P0 = find_dynamic_lex '$*PASTCOMPILER'
$P1 = find_lex '$node'
(%r :slurpy) = $P0.'post_children'($P1)
}
}
method regex_post($node) {
return $*PASTCOMPILER.as_post($node) if $node ~~ PAST::Node;
my $rxtype := $node.rxtype() // 'concat';
self."$rxtype"($node);
}
method post_new($type, *@args, *%options) {
Q:PIR {
$P0 = find_lex '$type'
$S0 = $P0
$P0 = get_root_global ['parrot';'POST'], $S0
$P1 = find_lex '@args'
$P2 = find_lex '%options'
%r = $P0.'new'($P1 :flat, $P2 :flat :named)
}
}
method alt($node) { self.altseq($node) }
method altseq($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $prefix := self.unique('alt') ~ '_';
my $altcount := 0;
my $iter := nqp::iterator($node.list);
my $endlabel := self.post_new('Label', :result($prefix ~ 'end'));
my $altlabel := self.post_new('Label', :result($prefix ~ $altcount));
my $apost := self.regex_post(nqp::shift($iter));
while $iter {
$ops.push($altlabel);
$altcount++;
$altlabel := self.post_new('Label', :result($prefix ~ $altcount));
self.regex_mark($ops, $altlabel, %*REG<pos>, 0);
$ops.push($apost);
$ops.push_pirop('goto', $endlabel);
$apost := self.regex_post(nqp::shift($iter));
}
$ops.push($altlabel);
$ops.push($apost);
$ops.push($endlabel);
$ops;
}
method anchor($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $subtype := $node.subtype;
my $donelabel := self.post_new('Label', :result(self.unique('rxanchor') ~ '_done'));
if $subtype eq 'bos' {
$ops.push_pirop('ne', %*REG<pos>, 0, %*REG<fail>);
}
elsif $subtype eq 'eos' {
$ops.push_pirop('lt', %*REG<pos>, %*REG<eos>, %*REG<fail>);
}
elsif $subtype eq 'lwb' {
$ops.push_pirop('ge', %*REG<pos>, %*REG<eos>, %*REG<fail>);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_WORD', %*REG<tgt>, %*REG<pos>);
$ops.push_pirop('unless', '$I11', %*REG<fail>);
$ops.push_pirop('sub', '$I11', %*REG<pos>, 1);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_WORD', %*REG<tgt>, '$I11');
$ops.push_pirop('if', '$I11', %*REG<fail>);
}
elsif $subtype eq 'rwb' {
$ops.push_pirop('le', %*REG<pos>, 0, %*REG<fail>);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_WORD', %*REG<tgt>, %*REG<pos>);
$ops.push_pirop('if', '$I11', %*REG<fail>);
$ops.push_pirop('sub', '$I11', %*REG<pos>, 1);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_WORD', %*REG<tgt>, '$I11');
$ops.push_pirop('unless', '$I11', %*REG<fail>);
}
elsif $subtype eq 'bol' {
$ops.push_pirop('eq', %*REG<pos>, 0, $donelabel);
$ops.push_pirop('ge', %*REG<pos>, %*REG<eos>, %*REG<fail>);
$ops.push_pirop('sub', '$I11', %*REG<pos>, 1);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_NEWLINE', %*REG<tgt>, '$I11');
$ops.push_pirop('unless', '$I11', %*REG<fail>);
$ops.push($donelabel);
}
elsif $subtype eq 'eol' {
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_NEWLINE', %*REG<tgt>, %*REG<pos>);
$ops.push_pirop('if', '$I11', $donelabel);
$ops.push_pirop('ne', %*REG<pos>, %*REG<eos>, %*REG<fail>);
$ops.push_pirop('eq', %*REG<pos>, 0, $donelabel);
$ops.push_pirop('sub', '$I11', %*REG<pos>, 1);
$ops.push_pirop('is_cclass', '$I11', '.CCLASS_NEWLINE', %*REG<tgt>, '$I11');
$ops.push_pirop('if', '$I11', %*REG<fail>);
$ops.push($donelabel);
}
elsif $subtype eq 'fail' {
$ops.push_pirop('goto', %*REG<fail>);
}
$ops;
}
our %cclass_code;
INIT {
%cclass_code<.> := '.CCLASS_ANY';
%cclass_code<d> := '.CCLASS_NUMERIC';
%cclass_code<s> := '.CCLASS_WHITESPACE';
%cclass_code<w> := '.CCLASS_WORD';
%cclass_code<n> := '.CCLASS_NEWLINE';
%cclass_code<nl> := '.CCLASS_NEWLINE';
}
method cclass($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $subtype := $node.subtype;
$ops.push_pirop('ge', %*REG<pos>, %*REG<eos>, %*REG<fail>);
my $cclass := %cclass_code{nqp::lc($subtype)};
self.panic("Unrecognized subtype '$subtype' in QAST::Regex cclass")
unless $cclass;
if $cclass ne '.CCLASS_ANY' {
my $testop := $node.negate ?? 'if' !! 'unless';
$ops.push_pirop('is_cclass', '$I11', $cclass, %*REG<tgt>, %*REG<pos>);
$ops.push_pirop($testop, '$I11', %*REG<fail>);
if $subtype eq 'nl' {
$ops.push_pirop('substr', '$S10', %*REG<tgt>, %*REG<pos>, 2);
$ops.push_pirop('iseq', '$I11', '$S10', '"\r\n"');
$ops.push_pirop('add', %*REG<pos>, '$I11');
}
}
$ops.push_pirop('add', %*REG<pos>, 1);
$ops;
}
method concat($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
for $node.list { $ops.push(self.regex_post($_)); }
$ops;
}
method conj($node) { self.conjseq($node) }
method conjseq($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $prefix := self.unique('rxconj') ~ '_';
my $conjlabel := self.post_new('Label', :result($prefix ~ 'fail'));
my $firstlabel := self.post_new('Label', :result($prefix ~ 'first'));
my $iter := nqp::iterator($node.list);
# make a mark that holds our starting position in the pos slot
self.regex_mark($ops, $conjlabel, %*REG<pos>, 0);
$ops.push_pirop('goto', $firstlabel);
$ops.push($conjlabel);
$ops.push_pirop('goto', %*REG<fail>);
# call the first child
$ops.push($firstlabel);
$ops.push(self.regex_post(nqp::shift($iter)));
# use previous mark to make one with pos=start, rep=end
self.regex_peek($ops, $conjlabel, '$I11');
self.regex_mark($ops, $conjlabel, '$I11', %*REG<pos>);
while $iter {
$ops.push_pirop('set', %*REG<pos>, '$I11');
$ops.push(self.regex_post(nqp::shift($iter)));
self.regex_peek($ops, $conjlabel, '$I11', '$I12');
$ops.push_pirop('ne', %*REG<pos>, '$I12', %*REG<fail>);
}
$ops.push_pirop('set', %*REG<pos>, '$I11') if $node.subtype eq 'zerowidth';
$ops;
}
method enumcharlist($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $charlist := self.escape($node[0]);
my $testop := $node.negate ?? 'ge' !! 'lt';
$ops.push_pirop('ge', %*REG<pos>, %*REG<eos>, %*REG<fail>);
$ops.push_pirop('substr', '$S11', %*REG<tgt>, %*REG<pos>, 1);
$ops.push_pirop('index', '$I11', $charlist, '$S11');
$ops.push_pirop($testop, '$I11', 0, %*REG<fail>);
$ops.push_pirop('inc', %*REG<pos>) unless $node.subtype eq 'zerowidth';
$ops;
}
method literal($node) {
my $ops := self.post_new('Ops');
my $litconst := $node[0];
$litconst := nqp::lc($litconst)
if $node.subtype eq 'ignorecase';
my $litlen := nqp::chars($litconst);
my $litpost := self.escape($litconst);
my $cmpop := $node.negate ?? 'eq' !! 'ne';
$ops.push_pirop('add', '$I11', %*REG<pos>, $litlen);
$ops.push_pirop('gt', '$I11', %*REG<eos>, %*REG<fail>);
$ops.push_pirop('substr', '$S10', %*REG<tgt>, %*REG<pos>, $litlen);
$ops.push_pirop('downcase', '$S10', '$S10')
if $node.subtype eq 'ignorecase';
$ops.push_pirop($cmpop, '$S10', $litpost, %*REG<fail>);
$ops.push_pirop('add', %*REG<pos>, $litlen);
$ops;
}
method pass($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my @backtrack := ["'backtrack'=>1"]
if $node.backtrack ne 'r';
if $node.name() {
my $name := $*PASTCOMPILER.as_post($node.name(), :rtype<~>);
$ops.push_pirop('callmethod', '"!cursor_pass"', %*REG<cur>, %*REG<pos>, $name, |@backtrack);
}
else {
$ops.push_pirop('callmethod', '"!cursor_pass"', %*REG<cur>, %*REG<pos>, |@backtrack);
}
$ops.push_pirop('return', %*REG<cur>);
$ops;
}
method pastnode($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
$ops.push_pirop('repr_bind_attr_int', %*REG<cur>, %*REG<curclass>, '"$!pos"', %*REG<pos>);
$ops.push_pirop('store_lex', 'unicode:"$\x{a2}"', %*REG<cur>);
my $cpost := $*PASTCOMPILER.as_post($node[0], :rtype<P>);
$ops.push($cpost);
if $node.subtype eq 'zerowidth' {
$ops.push_pirop($node.negate ?? 'if' !! 'unless', $cpost, %*REG<fail>);
}
$ops;
}
method quant($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $backtrack := $node.backtrack || 'g';
my $sep := $node[1];
my $prefix := self.unique('rxquant' ~ $backtrack);
my $looplabel := self.post_new('Label', :result($prefix ~ '_loop'));
my $donelabel := self.post_new('Label', :result($prefix ~ '_done'));
my $rcslabel := self.post_new('Label', :result($prefix ~ '_ratchet_cstack'));
my $min := $node.min || 0;
my $max := $node.max // -1;
my $needrep := $min > 1 || $max > 1;
my $needmark := $needrep || $backtrack eq 'r';
$ops.push_pirop('inline', :inline(' # rx %0 ** %1..%2'), $prefix, $min, $max);
if $backtrack eq 'f' {
my $seplabel := self.post_new('Label', :result($prefix ~ '_sep'));
my $ireg := '$I12';
$ops.push_pirop('set', %*REG<rep>, 0);
if $min < 1 {
self.regex_mark($ops, $looplabel, %*REG<pos>, %*REG<rep>);
$ops.push_pirop('goto', $donelabel);
}
$ops.push_pirop('goto', $seplabel) if $sep;
$ops.push($looplabel);
$ops.push_pirop('set', $ireg, %*REG<rep>);
$ops.push(self.regex_post($sep)) if $sep;
$ops.push($seplabel) if $sep;
$ops.push(self.regex_post($node[0]));
$ops.push_pirop('set', %*REG<rep>, $ireg);
$ops.push_pirop('inc', %*REG<rep>);
$ops.push_pirop('lt', %*REG<rep>, $min, $looplabel) if $min > 1;
$ops.push_pirop('ge', %*REG<rep>, $max, $donelabel) if $max > 1;
self.regex_mark($ops, $looplabel, %*REG<pos>, %*REG<rep>) if $max != 1;
$ops.push($donelabel);
}
else {
self.regex_mark($ops, $rcslabel, -1, 0) # to track cstack as we ratchet
if $backtrack eq 'r' && $max != 1;
if $min == 0 { self.regex_mark($ops, $donelabel, %*REG<pos>, 0); }
elsif $needmark { self.regex_mark($ops, $donelabel, -1, 0); }
$ops.push($looplabel);
$ops.push(self.regex_post($node[0]));
if $needmark {
self.regex_peek($ops, $donelabel, '*', %*REG<rep>);
self.regex_commit($ops, $donelabel) if $backtrack eq 'r';
$ops.push_pirop('inc', %*REG<rep>);
$ops.push_pirop('ge', %*REG<rep>, $node.max, $donelabel)
if $max > 1;
}
unless $max == 1 {
self.regex_mark($ops, $donelabel, %*REG<pos>, %*REG<rep>);
$ops.push_pirop('nqp_rxsetcaps', %*REG<bstack>, %*REG<cstack>)
if $backtrack eq 'r';
$ops.push(self.regex_post($sep)) if $sep;
$ops.push_pirop('goto', $looplabel);
}
$ops.push($donelabel);
if $backtrack eq 'r' && $max != 1 {
self.regex_commit($ops, $rcslabel); # pop cstack/ratchet tracking mark
$ops.push($rcslabel);
}
$ops.push_pirop('lt', %*REG<rep>, +$node.min, %*REG<fail>)
if $min > 1;
}
$ops;
}
method scan($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $prefix := self.unique('rxscan');
my $looplabel := self.post_new('Label', :result($prefix ~ '_loop'));
my $scanlabel := self.post_new('Label', :result($prefix ~ '_scan'));
my $donelabel := self.post_new('Label', :result($prefix ~ '_done'));
$ops.push_pirop('repr_get_attr_int', '$I11', 'self', %*REG<curclass>, '"$!from"');
$ops.push_pirop('ne', '$I11', -1, $donelabel);
$ops.push_pirop('goto', $scanlabel);
$ops.push($looplabel);
$ops.push_pirop('inc', %*REG<pos>);
$ops.push_pirop('gt', %*REG<pos>, %*REG<eos>, %*REG<fail>);
$ops.push_pirop('repr_bind_attr_int', %*REG<cur>, %*REG<curclass>, '"$!from"', %*REG<pos>);
$ops.push($scanlabel);
self.regex_mark($ops, $looplabel, %*REG<pos>, 0);
$ops.push($donelabel);
$ops;
}
method subcapture($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $prefix := self.unique('rxcap');
my $donelabel := self.post_new('Label', :result($prefix ~ '_done'));
my $faillabel := self.post_new('Label', :result($prefix ~ '_fail'));
my $name := $*PASTCOMPILER.as_post($node.name, :rtype<*>);
self.regex_mark($ops, $faillabel, %*REG<pos>, 0);
$ops.push(self.regex_post($node[0]));
self.regex_peek($ops, $faillabel, '$I11');
$ops.push($name);
$ops.push_pirop('repr_bind_attr_int', %*REG<cur>, %*REG<curclass>, '"$!pos"', '$I11');
$ops.push_pirop('callmethod', '"!cursor_start"', %*REG<cur>, :result<$P11>);
$ops.push_pirop('callmethod', '"!cursor_pass"', '$P11', %*REG<pos>);
$ops.push_pirop('callmethod', '"!cursor_capture"', %*REG<cur>,
'$P11', $name, :result(%*REG<cstack>));
$ops.push_pirop('goto', $donelabel);
$ops.push($faillabel);
$ops.push_pirop('goto', %*REG<fail>);
$ops.push($donelabel);
$ops;
}
method subrule($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $name := $*PASTCOMPILER.as_post($node.name, :rtype<*>);
my $subtype := $node.subtype;
my $cpn := self.post_children($node[0]);
my @pargs := $cpn[1] // [];
my @nargs := $cpn[2] // [];
my $subpost := nqp::shift(@pargs);
my $testop := $node.negate ?? 'ge' !! 'lt';
my $captured := 0;
$ops.push($cpn[0]);
$ops.push_pirop('repr_bind_attr_int', %*REG<cur>, %*REG<curclass>, '"$!pos"', %*REG<pos>);
$ops.push_pirop('callmethod', $subpost, %*REG<cur>, |@pargs, |@nargs, :result<$P11>);
$ops.push_pirop('repr_get_attr_int', '$I11', '$P11', %*REG<curclass>, '"$!pos"');
$ops.push_pirop($testop, '$I11', '0', %*REG<fail>);
if $subtype ne 'zerowidth' {
my $rxname := self.unique('rxsubrule');
my $passlabel := self.post_new('Label', :result($rxname ~ '_pass'));
if $node.backtrack eq 'r' {
unless $subtype eq 'method' {
self.regex_mark($ops, $passlabel, -1, 0);
$ops.push($passlabel);
}
}
else {
my $backlabel := self.post_new('Label', :result($rxname ~ '_back'));
$ops.push_pirop('goto', $passlabel);
$ops.push($backlabel);
$ops.push_pirop('callmethod', '"!cursor_next"', '$P11', :result('$P11'));
$ops.push_pirop('repr_get_attr_int', '$I11', '$P11', %*REG<curclass>, '"$!pos"');
$ops.push_pirop($testop, '$I11', '0', %*REG<fail>);
$ops.push($passlabel);
if $subtype eq 'capture' {
$ops.push_pirop('callmethod', '"!cursor_capture"', %*REG<cur>,
'$P11', $name, :result(%*REG<cstack>));
$captured := 1;
}
else {
$ops.push_pirop('callmethod', '"!cursor_push_cstack"', %*REG<cur>,
'$P11', :result(%*REG<cstack>));
}
$ops.push_pirop('set_addr', '$I11', $backlabel);
$ops.push_pirop('push', %*REG<bstack>, '$I11');
$ops.push_pirop('push', %*REG<bstack>, 0);
$ops.push_pirop('push', %*REG<bstack>, %*REG<pos>);
$ops.push_pirop('elements', '$I11', %*REG<cstack>);
$ops.push_pirop('push', %*REG<bstack>, '$I11');
}
}
$ops.push_pirop('callmethod', '"!cursor_capture"', %*REG<cur>,
'$P11', $name, :result(%*REG<cstack>))
if !$captured && $subtype eq 'capture';
$ops.push_pirop('repr_get_attr_int', %*REG<pos>, '$P11', %*REG<curclass>, '"$!pos"')
unless $subtype eq 'zerowidth';
$ops;
}
method uniprop($node) {
my $ops := self.post_new('Ops', :result(%*REG<cur>));
my $cmpop := $node.negate ?? 'ne' !! 'eq';
$ops.push_pirop('assign', '$S10', '"' ~ $node[0] ~ '"');
$ops.push_pirop('is_uprop', '$I11', '$S10', %*REG<tgt>, %*REG<pos>);
$ops.push_pirop($cmpop, '$I11', 0, %*REG<fail>);
$ops.push_pirop('inc', %*REG<pos>) unless $node.subtype eq 'zerowidth';
$ops;
}
# a :rxtype<ws> node is a normal subrule call
method ws($node) { self.subrule($node) }
method regex_mark($ops, $mark, $pos, $rep) {
$ops.push_pirop('nqp_rxmark', %*REG<bstack>, $mark, $pos, $rep);
}
method regex_peek($ops, $mark, *@regs) {
$ops.push_pirop('nqp_rxpeek', '$I19', %*REG<bstack>, $mark);
for @regs {
$ops.push_pirop('inc', '$I19');
$ops.push_pirop('set', $_, %*REG<bstack>~'[$I19]') if $_ ne '*';
}
}
method regex_commit($ops, $mark) {
$ops.push_pirop('nqp_rxpeek', '$I11', %*REG<bstack>, $mark);
$ops.push_pirop('assign', %*REG<bstack>, '$I11');
}
}