| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | use Mildew::SSA; | ||
| 4 | use Mildew::Types; | ||
| 5 | class Mildew::Backend::Perlesque with Mildew::Backend { | ||
| 6 | use File::Temp qw(tempfile tmpnam); | ||
| 7 | method perlesque_source($ast) { | ||
| 8 | my $ssa_ast = Mildew::SSA::to_ssa($ast->simplified,{ | ||
| 9 | '$scope' => Mildew::Type::Scope->new(outer=> $Mildew::LexicalPreludeType) | ||
| 10 | }); | ||
| 11 | |||
| 12 | 'my $main = '.$self->emit_block($ssa_ast)."\n" | ||
| 13 | . 'my P6LexicalScope $scope = P6LexicalScope.new();'."\n" | ||
| 14 | . '$main($scope);'."\n"; | ||
| 15 | } | ||
| 16 | method emit_block($block) { | ||
| 17 | my %regs; | ||
| 18 | my $value = sub { | ||
| 19 | if ($_[0]->isa('Mildew::AST::Reg')) { | ||
| 20 | $regs{$_[0]->real_name}++; | ||
| 21 | $_[0]->real_name; | ||
| 22 | } elsif ($_[0]->isa('Mildew::AST::IntegerConstant')) { | ||
| 23 | 'P6int.new(' . $_[0]->value . ')'; | ||
| 24 | } elsif ($_[0]->isa('Mildew::AST::StringConstant')) { | ||
| 25 | my $str = $_[0]->value; | ||
| 26 | # TODO properly quote characters | ||
| 27 | $str =~ s/(["\\])/\\$1/g; | ||
| 28 | $str =~ s/\n/\\n/g; | ||
| 29 | 'P6Str.new("' . $str . '")'; | ||
| 30 | } else { | ||
| 31 | ref($_[0]); | ||
| 32 | } | ||
| 33 | }; | ||
| 34 | # if ($_[0]->isa('Mildew::AST::Reg')) { | ||
| 35 | # if ($_[0]->name =~ /^¢/) { | ||
| 36 | # my $n = $_[0]->name; | ||
| 37 | # $n =~ s/^¢//; | ||
| 38 | # return $n; | ||
| 39 | # } | ||
| 40 | # unless (defined $regs{$_[0]->real_name}) { | ||
| 41 | # $regs{$_[0]->real_name} = $reg_id++; | ||
| 42 | # } | ||
| 43 | # "frame->reg[" . $regs{$_[0]->real_name} . "]"; | ||
| 44 | # } elsif ($_[0]->isa('Mildew::AST::Block::SSA')) { | ||
| 45 | # my ($func,$expr,$init) = $_[0]->emit_c; | ||
| 46 | # $funcs .= $func; | ||
| 47 | # $call_init_funcs .= $init; | ||
| 48 | # $constant->($expr); | ||
| 49 | # } else { | ||
| 50 | # die "don't know how to emit: ",ref($_[0]); | ||
| 51 | # } | ||
| 52 | # #$constant->(ref($_[0]).'???'); | ||
| 53 | # } | ||
| 54 | my $code; | ||
| 55 | for my $sub_block (@{$block->stmts}) { | ||
| 56 | for my $stmt (@{$sub_block->stmts}) { | ||
| 57 | #$code .= "\n/*".$stmt->pretty."*/"; | ||
| 58 | if ($stmt->isa('Mildew::AST::Goto')) { | ||
| 59 | $code .= "#goto\n" | ||
| 60 | #$code .= "frame->pc = " . $labels{$stmt->block->id} . ";" . "break;\n" | ||
| 61 | } elsif ($stmt->isa('Mildew::AST::Branch')) { | ||
| 62 | $code .= "#branch\n"; | ||
| 63 | # $code .= "frame->pc = " | ||
| 64 | # . $value->($stmt->cond) | ||
| 65 | # . " == SMOP__NATIVE__bool_false ? " | ||
| 66 | # . $labels{$stmt->else->id} | ||
| 67 | # . " : " | ||
| 68 | # . $labels{$stmt->then->id} | ||
| 69 | # . ";break;\n"; | ||
| 70 | } elsif ($stmt->isa('Mildew::AST::Reg')) { | ||
| 71 | # make it a noop | ||
| 72 | } elsif ($stmt->isa('Mildew::AST::Assign')) { | ||
| 73 | if ($stmt->rvalue->isa('Mildew::AST::Call')) { | ||
| 74 | my $type = $stmt->rvalue->capture->invocant->type_info->type; | ||
| 75 | $code .= $type->emit_perlesque_call($stmt,$value) . "\n"; | ||
| 76 | |||
| 77 | } elsif ($stmt->rvalue->isa('Mildew::AST::Phi')) { | ||
| 78 | # TODO make it a noop | ||
| 79 | } elsif ($stmt->rvalue->isa('Mildew::AST::InferredTypeTest')) { | ||
| 80 | $code .= "#inferred type test\n" | ||
| 81 | #my $type = $stmt->rvalue->value->type_info->type; | ||
| 82 | #$code .= Emit::Yeast::assign($value->($stmt->lvalue),$value->(Mildew::AST::IntegerConstant->new(value=>eval($stmt->rvalue->test) ? 1 : 0))); | ||
| 83 | #die if $@; | ||
| 84 | } else { | ||
| 85 | $code .= "# assignment\n"; | ||
| 86 | #$code .= Emit::Yeast::assign($value->($stmt->lvalue),$value->($stmt->rvalue)); | ||
| 87 | } | ||
| 88 | } else { | ||
| 89 | #die "don't know how to emit: ",ref($stmt); | ||
| 90 | $code .= "#".ref($stmt)."\n"; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | for (@{$block->regs}) { | ||
| 96 | delete $regs{'$'.$_}; | ||
| 97 | } | ||
| 98 | "sub (P6object \$scope --> int) {\n" . (join '',(map {"my P6object $_;\n"} keys %regs)) . "${code}return 1;\n}"; | ||
| 99 | } | ||
| 100 | method compile($ast,$output) { | ||
| 101 | $self->output($self->perlesque_source($ast)."\n",$output); | ||
| 102 | } | ||
| 103 | method run($ast) { | ||
| 104 | my $tmp_file = tmpnam; | ||
| 105 | $self->compile($ast,$tmp_file); | ||
| 106 | die 'the SPRIXEL enviroinment variable needs to be set for -Bperlesque to work' unless $ENV{SPRIXEL}; | ||
| 107 | exec('mono',$ENV{SPRIXEL},$tmp_file); | ||
| 108 | } | ||
| 109 | } |