Skip to content

Commit

Permalink
Streamline return value logic
Browse files Browse the repository at this point in the history
- make Binder.get_return_type return nqp::null on Mu
- make p6typecheckrv op to just emit the value if type is nqp::null
  effectively bypassing all typechecks if --> Mu
- streamline the raku-rv-typecheck dispatcher
  - more comments
  - fast-path handling of Nil / Failure being returned
  - fewer checks on paths that need runtime / definite checks / coercion
  • Loading branch information
lizmat committed Feb 8, 2024
1 parent 02bc10c commit ccbe370
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 163 deletions.
7 changes: 6 additions & 1 deletion src/Perl6/bootstrap.c/BOOTSTRAP.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,12 @@ my class Binder {
}

method get_return_type($code) {
nqp::getattr(nqp::getattr($code, Code, '$!signature'), Signature, '$!returns')
my $type := nqp::getattr(
nqp::getattr($code, Code, '$!signature'), Signature, '$!returns'
);
nqp::eqaddr($type,Mu) || nqp::eqaddr($type,NQPMu)
?? nqp::null
!! $type
}
#?endif

Expand Down
43 changes: 19 additions & 24 deletions src/vm/moar/Perl6/Ops.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -408,33 +408,28 @@ $ops.add_hll_op('Raku', 'p6box', -> $qastcomp, $op {
});
$ops.add_hll_op('Raku', 'p6typecheckrv', -> $qastcomp, $op {
if nqp::istype($op[1], QAST::WVal) {
my $type := &get_binder().get_return_type($op[1].value);
if nqp::isnull($type) || nqp::objprimspec(nqp::decont($type)) {
$qastcomp.as_mast($op[0])
my $value := $op[0];
my $type := &get_binder().get_return_type($op[1].value);
if nqp::isnull($type) {
$qastcomp.as_mast(QAST::Op.new(:op<p6box>, $value));
}
elsif nqp::objprimspec(nqp::decont($type)) {
$qastcomp.as_mast($value)
}
else {
my $is_generic := $type.HOW.archetypes($type).generic;
my $type_ast;
if $is_generic {
$type_ast :=
QAST::Op.new(
:op<callmethod>,
:name<instantiate_generic>,
QAST::Op.new(:op<how>, QAST::WVal.new(:value($type))),
QAST::WVal.new(:value($type)),
QAST::Op.new(:op<curlexpad>)
);
}
else {
$type_ast := QAST::WVal.new( :value($type) );
}
$qastcomp.as_mast(QAST::Op.new(
:op('dispatch'),
QAST::SVal.new( :value('raku-rv-typecheck') ),
QAST::Op.new( :op('p6box'), $op[0] ),
$type_ast,
QAST::IVal.new(:value($is_generic))
))
$qastcomp.as_mast(QAST::Op.new(:op<dispatch>,
QAST::SVal.new( :value('raku-rv-typecheck') ),
QAST::Op.new( :op('p6box'), $value ),
$is_generic
?? QAST::Op.new(:op<callmethod>, :name<instantiate_generic>,
QAST::Op.new(:op<how>, QAST::WVal.new(:value($type))),
QAST::WVal.new(:value($type)),
QAST::Op.new(:op<curlexpad>)
)
!! QAST::WVal.new(:value($type)),
QAST::IVal.new(:value($is_generic))
));
}
}
else {
Expand Down

0 comments on commit ccbe370

Please sign in to comment.