Skip to content

Commit

Permalink
Factor parametric role creation PIR out into a macro, which neatens a…
Browse files Browse the repository at this point in the history
…ctions.pm a little and means we can use it to create parametric roles from PIR too.
  • Loading branch information
jnthn committed Apr 3, 2009
1 parent 1bf637c commit 0832809
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build/Makefile.in
Expand Up @@ -189,7 +189,7 @@ perlcritic:

## supporting targets for perl6
# the Perl 6 compiler
perl6_s1.pbc: $(PARROT) $(SOURCES) $(BUILTINS_PIR)
perl6_s1.pbc: $(PARROT) $(SOURCES) $(BUILTINS_PIR) src/pr_macros.pir
$(PERL) -e "" > src/gen_setting.pir
$(PARROT) $(PARROT_ARGS) -o perl6_s1.pbc perl6.pir

Expand Down
23 changes: 5 additions & 18 deletions src/parser/actions.pm
Expand Up @@ -20,8 +20,9 @@ method TOP($/) {
declare_implicit_routine_vars($past);
$past.lexical(0);

# Make sure we have the interpinfo constants.
# Make sure we have the interpinfo constants and parametric role macros.
$past.unshift( PAST::Op.new( :inline('.include "interpinfo.pasm"') ) );
$past.unshift( PAST::Op.new( :inline('.include "src/pr_macros.pir"') ) );

# Set package for unit mainline
$past.unshift(set_package_magical());
Expand Down Expand Up @@ -1768,24 +1769,10 @@ method package_def($/, $key) {
# For a role, we now need to produce a new one which clones the original,
# but without the methods. Then we need to add back the methods. We emit
# PIR here to do it rather than doing a call, since we need to call
# new_closure from the correct scope.
# new_closure from the correct scope. (Note: create_parametric_role is a
# PIR macro).
$block[0].push(PAST::Op.new(:inline(
' "!meta_compose"(%0)',
' .local pmc orig_role, meths, meth_iter',
' orig_role = getprop "$!orig_role", %0',
' meths = orig_role."methods"()',
' meth_iter = iter meths',
' it_loop:',
' unless meth_iter goto it_loop_end',
' $S0 = shift meth_iter',
' $P0 = meths[$S0]',
' $P1 = getprop "$!signature", $P0',
' $P0 = newclosure $P0',
' setprop $P0, "$!signature", $P1',
' %0."add_method"($S0, $P0)',
' goto it_loop',
' it_loop_end:',
' .return (%0)'
' .create_parametric_role(%0)',
),
$?METACLASS
));
Expand Down
21 changes: 21 additions & 0 deletions src/pr_macros.pir
@@ -0,0 +1,21 @@
# This is a macro that helps in creating parametric roles. It exists to
# keep actions.pm a little shorter, but also to enable us to more easily
# construct parametric roles in PIR.
.macro create_parametric_role(mr)
"!meta_compose"(.mr)
.local pmc orig_role, meths, meth_iter
orig_role = getprop "$!orig_role", .mr
meths = orig_role."methods"()
meth_iter = iter meths
it_loop:
unless meth_iter goto it_loop_end
$S0 = shift meth_iter
$P0 = meths[$S0]
$P1 = getprop "$!signature", $P0
$P0 = newclosure $P0
setprop $P0, "$!signature", $P1
.mr."add_method"($S0, $P0)
goto it_loop
it_loop_end:
.return (.mr)
.endm

0 comments on commit 0832809

Please sign in to comment.