Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Provide a way to let a HLL specify that returned values should always…
… be boxed. Outsourcing this to QAST has various benefits, including keeping this out of the QAST tree itself. Then inlining doesn't have to worry over this detail.
  • Loading branch information
jnthn committed Aug 1, 2012
1 parent 6485fa4 commit df6015a
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/QAST/Compiler.nqp
Expand Up @@ -187,6 +187,11 @@ class QAST::Compiler is HLL::Compiler {
sub type_to_lookup_name($type) {
@prim_to_lookup_name[pir::repr_get_primitive_type_spec__IP($type)]
}

my %hll_force_return_boxing;
method force_return_boxing_for_hll($hll) {
%hll_force_return_boxing{$hll} := 1;
}

method unique($prefix = '') { $prefix ~ $serno++ }
method escape($str) {
Expand Down Expand Up @@ -405,19 +410,31 @@ class QAST::Compiler is HLL::Compiler {
$decls.push_pirop('.local ' ~ $block.local_type_long($_.name) ~ ' ' ~ $_.name);
}

# Wrap all up in a POST::Sub.
# Create a PIRT::Sub and apply HLL if any.
$sub := PIRT::Sub.new();
$sub.push($decls);
$sub.push($stmts);
$sub.push_pirop(".return (" ~ $stmts.result ~ ")");

# Apply HLL if any.
my $hll := '';
try $hll := $*HLL;
if $hll {
$sub.hll($hll);
}

# Emit declarations, statements and and emit return instruction.
$sub.push($decls);
{
my $*BLOCK := $block;
my $ret_type := 'P';
try $ret_type := self.infer_type($stmts.result);
if $ret_type ne 'P' && %hll_force_return_boxing{$hll} {
my $boxed := self.coerce($stmts, 'P');
$sub.push($boxed);
$sub.push_pirop(".return (" ~ $boxed.result ~ ")");
}
else {
$sub.push($stmts);
$sub.push_pirop(".return (" ~ $stmts.result ~ ")");
}
}

# Set compilation unit ID, name and, if applicable, outer.
$sub.subid($node.cuid);
if nqp::istype($block.outer, BlockInfo) {
Expand Down

0 comments on commit df6015a

Please sign in to comment.