Skip to content

Commit

Permalink
Improve regex prelude when we can know cursor type
Browse files Browse the repository at this point in the history
Means we can emit some more things as hinted lookups.
  • Loading branch information
jnthn committed Jan 25, 2014
1 parent 50185fc commit 3cccf18
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/NQP/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,8 @@ class NQP::Actions is HLL::Actions {
$block.symbol('', :scope<lexical>);
$block.symbol('$/', :scope<lexical>);
my $code := %*RX<code>;
my $regex := %*LANG<Regex-actions>.qbuildsub($<p6regex>.ast, $block, code_obj => $code);
my $regex := %*LANG<Regex-actions>.qbuildsub($<p6regex>.ast, $block,
code_obj => $code, cursor_type => $*W.find_sym(['NQPCursor']));
$regex.name($name);

if $*PKGDECL && nqp::can($*PACKAGE.HOW, 'add_method') {
Expand Down
15 changes: 15 additions & 0 deletions src/QAST/Regex.nqp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
role QAST::RegexCursorType {
has $!cursor_type;
method has_cursor_type() { 1 }
method cursor_type($value = NO_VALUE) {
$!cursor_type := $value unless $value =:= NO_VALUE;
$!cursor_type
}
}

class QAST::Regex is QAST::Node {
has $!name;
has str $!rxtype;
Expand Down Expand Up @@ -27,5 +36,11 @@ class QAST::Regex is QAST::Node {
method dump_extra_node_info() {
":rxtype($!rxtype)" ~ (!nqp::isnull_s($!subtype) ?? " :subtype($!subtype)" !! "") ~ ($!negate ?? ' (negated)' !! '') ~ (nqp::defined($!name) ?? " :name($!name)" !! '')
}

method has_cursor_type() { 0 }
method cursor_type($type) {
self.HOW.mixin(self, QAST::RegexCursorType);
self.cursor_type($type);
}
}

4 changes: 4 additions & 0 deletions src/QRegex/P6Regex/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,10 @@ class QRegex::P6Regex::Actions is HLL::Actions {
$qast[2].backtrack('r');
}
$block.push($qast);

if nqp::existskey(%rest, 'cursor_type') {
$qast.cursor_type(%rest<cursor_type>);
}

$block;
}
Expand Down
46 changes: 36 additions & 10 deletions src/vm/moar/QAST/QASTRegexCompilerMAST.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use QASTNode;
use NQPHLL;
use MASTNodes;
use MASTOps;
use QRegex;

my $MVM_reg_void := 0; # not really a register; just a result/return kind marker
my $MVM_reg_int8 := 1;
Expand Down Expand Up @@ -110,24 +111,49 @@ class QAST::MASTRegexCompiler {
op('const_i64', $cclass_word, ival(nqp::const::CCLASS_WORD)),
op('const_i64', $cclass_newline, ival(nqp::const::CCLASS_NEWLINE)),
op('findmeth', $method, $self, sval('!cursor_start')),
call($method, [ $Arg::obj ], :result($cur), $self ),
op('findmeth', $shared, $self, sval('!shared')),
call($shared, [ $Arg::obj ], :result($shared), $self ),
op('getwhat', $sharedclass, $shared),
op('getattr_o', $curclass, $shared, $sharedclass, sval('$!CUR_CLASS'), ival(-1)),
op('getattr_s', $tgt, $shared, $sharedclass, sval('$!target'), ival(-1)),
call($method, [ $Arg::obj ], :result($cur), $self )
];

my int $has_cursor_type := $node.has_cursor_type();
my $cursor_type;
if $has_cursor_type {
$cursor_type := $node.cursor_type();
my $wval := $*QASTCOMPILER.as_mast(QAST::WVal.new( :value($cursor_type) ));
merge_ins(@ins, $wval.instructions);
merge_ins(@ins, [
op('set', $curclass, $wval.result_reg),
op('getattr_o', $shared, $self, $curclass, sval('$!shared'),
ival(nqp::hintfor($cursor_type, '$!shared')))
]);
release($wval.result_reg, $MVM_reg_obj);
}
else {
merge_ins(@ins, [
op('findmeth', $shared, $self, sval('!shared')),
call($shared, [ $Arg::obj ], :result($shared), $self ),
op('getwhat', $sharedclass, $shared),
op('getattr_o', $curclass, $shared, $sharedclass, sval('$!CUR_CLASS'), ival(-1))
]);
}

merge_ins(@ins, [
op('getattr_s', $tgt, $shared, $sharedclass, sval('$!target'),
ival(nqp::hintfor(ParseShared, '$!target'))),
op('flattenropes', $tgt),
op('getattr_i', $pos, $cur, $curclass, sval('$!from'), ival(-1)),
op('getattr_o', $bstack, $cur, $curclass, sval('$!bstack'), ival(-1)),
op('getattr_o', $tmp, $self, $curclass, sval('$!restart'), ival(-1)),
op('getattr_i', $pos, $cur, $curclass, sval('$!from'),
ival(nqp::hintfor($cursor_type, '$!from'))),
op('getattr_o', $bstack, $cur, $curclass, sval('$!bstack'),
ival(nqp::hintfor($cursor_type, '$!bstack'))),
op('getattr_o', $tmp, $self, $curclass, sval('$!restart'),
ival(nqp::hintfor($cursor_type, '$!restart'))),
op('isconcrete', $i19, $tmp),
op('bindlex', $*BLOCK.resolve_lexical(''), $cur),
op('graphs_s', $eos, $tgt),
op('eq_i', $i0, $one, $i19),
op('if_i', $i0, $restartlabel),
op('gt_i', $i0, $pos, $eos),
op('if_i', $i0, $faillabel)
];
]);
release($i0, $MVM_reg_int64);
release($i19, $MVM_reg_int64);

Expand Down

0 comments on commit 3cccf18

Please sign in to comment.