| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | use Mildew::SSA; | ||
| 4 | use Mildew::Types; | ||
| 5 | use utf8; | ||
| 6 | class Mildew::Backend::OptC with Mildew::Backend::C { | ||
| 7 | use File::Temp qw(tempfile tmpnam); | ||
| 8 | use String::Escape qw(backslash quote); | ||
| 9 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 10 | use Encode qw(encode_utf8); | ||
| 11 | has options=>(is=>'ro',default=>sub {{}}); | ||
| 12 | has trace=>(is=>'rw'); | ||
| 13 | has dump=>(is=>'rw'); | ||
| 14 | method BUILD { | ||
| 15 | my ($trace,$dump,$cflags,$ld_library_path,$no_setting,$valgrind,$gdb,$no_wrap_in_block); | ||
| 16 | GetOptionsFromArray( | ||
| 17 | ($self->options->{BACKEND} // []), | ||
| 18 | 'trace' => \$trace, | ||
| 19 | 'dump=s' => \$dump, | ||
| 20 | 'cflags=s' => \$cflags, | ||
| 21 | 'no-setting' => \$no_setting, | ||
| 22 | 'ld-library-path=s' => \$ld_library_path, | ||
| 23 | 'valgrind' => \$valgrind, | ||
| 24 | 'gdb' => \$gdb, | ||
| 25 | 'no-wrap-in-block' => \$no_wrap_in_block, | ||
| 26 | ) || die 'incorrent options passed to Mildew::Backend::OptC'; | ||
| 27 | use YAML::XS; | ||
| 28 | $self->trace($trace); | ||
| 29 | $self->dump($dump); | ||
| 30 | $self->cflags([split(':',$cflags)]) if $cflags; | ||
| 31 | $self->load_setting(!$no_setting) if $no_setting; | ||
| 32 | $self->ld_library_path([split(':',$ld_library_path)]) if $ld_library_path; | ||
| 33 | $self->valgrind($valgrind); | ||
| 34 | $self->gdb($gdb); | ||
| 35 | $self->wrap_in_block(!$no_wrap_in_block); | ||
| 36 | } | ||
| 37 | method c_source($ast) { | ||
| 38 | my $ssa_ast = Mildew::SSA::to_ssa($ast->simplified,{ | ||
| 39 | '$scope' => Mildew::Type::Scope->new(outer=> $Mildew::LexicalPreludeType) | ||
| 40 | }); | ||
| 41 | my ($funcs,$expr,$call_init_funcs) = $self->emit_block($ssa_ast); | ||
| 42 | my $boilerplate = $self->get_boilerplate; | ||
| 43 | my $body = | ||
| 44 | $call_init_funcs | ||
| 45 | . "SMOP__Object* yeast = " . $expr . ";\n" | ||
| 46 | . "SMOP__Object* frame = SMOP__Yeast__Frame_create(interpreter,yeast);\n" | ||
| 47 | . "yeast_reg_set(interpreter,frame,0,SMOP_REFERENCE(interpreter,interpreter));\n" | ||
| 48 | . "yeast_reg_set(interpreter,frame,1,SMOP_REFERENCE(interpreter,SMOP__S1P__LexicalPrelude));\n"; | ||
| 49 | $boilerplate =~ s/%%BODY%%/$body/; | ||
| 50 | $boilerplate =~ s/%%FUNCS%%/$funcs/; | ||
| 51 | $boilerplate; | ||
| 52 | } | ||
| 53 | |||
| 54 | method yeast($ast) { | ||
| 55 | my $ssa_ast = Mildew::SSA::to_ssa($ast->simplified,{ | ||
| 56 | '$scope' => Mildew::Type::Scope->new(outer=> $Mildew::LexicalPreludeType) | ||
| 57 | }); | ||
| 58 | $self->emit_block($ssa_ast); | ||
| 59 | } | ||
| 60 | method emit_block($block) { | ||
| 61 | state $unique_func_id = 0; | ||
| 62 | |||
| 63 | my $func_name = 'smop_yeast_' . $unique_func_id++; | ||
| 64 | my $funcs = ''; | ||
| 65 | my $call_init_funcs = ''; | ||
| 66 | my $i = 0; | ||
| 67 | my $code; | ||
| 68 | my %labels; | ||
| 69 | |||
| 70 | my %regs; | ||
| 71 | my $reg_id = 0; | ||
| 72 | |||
| 73 | my $constant_decls; | ||
| 74 | my $init_constants = "static void ${func_name}_init(interpreter) {"; | ||
| 75 | |||
| 76 | my $constants_id = 0; | ||
| 77 | my $constant = sub { | ||
| 78 | my $c = $func_name . "_constant_" . $constants_id++; | ||
| 79 | $constant_decls .= "static SMOP__Object* $c;\n"; | ||
| 80 | $init_constants .= "$c = $_[0];\n"; | ||
| 81 | $c; | ||
| 82 | }; | ||
| 83 | |||
| 84 | for (@{$block->regs}) { | ||
| 85 | $regs{'$'.$_} = $reg_id++; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $value = sub { | ||
| 89 | if ($_[0]->isa('Mildew::AST::Reg')) { | ||
| 90 | if ($_[0]->name =~ /^¢|^\?/) { | ||
| 91 | my $n = $_[0]->name; | ||
| 92 | $n =~ s/^¢|^\?//; | ||
| 93 | return $n; | ||
| 94 | } | ||
| 95 | unless (defined $regs{$_[0]->real_name}) { | ||
| 96 | $regs{$_[0]->real_name} = $reg_id++; | ||
| 97 | } | ||
| 98 | "frame->reg[" . $regs{$_[0]->real_name} . "]"; | ||
| 99 | } elsif ($_[0]->isa('Mildew::AST::StringConstant')) { | ||
| 100 | my $str = $_[0]->value; | ||
| 101 | # TODO properly quote characters | ||
| 102 | $str =~ s/(["\\])/\\$1/g; | ||
| 103 | $str =~ s/\n/\\n/g; | ||
| 104 | $constant->('SMOP__NATIVE__idconst_createn("' . $str . '",' . length(encode_utf8($_[0]->value)) . ')'); | ||
| 105 | } elsif ($_[0]->isa('Mildew::AST::IntegerConstant')) { | ||
| 106 | $constant->('SMOP__NATIVE__int_create(' . $_[0]->value . ')'); | ||
| 107 | } elsif ($_[0]->isa('Mildew::AST::Block::SSA')) { | ||
| 108 | my ($func,$expr,$init) = $self->emit_block($_[0]); | ||
| 109 | $funcs .= $func; | ||
| 110 | $call_init_funcs .= $init; | ||
| 111 | $constant->($expr); | ||
| 112 | } else { | ||
| 113 | die "don't know how to emit: ",ref($_[0]); | ||
| 114 | #$constant->(ref($_[0]).'???'); | ||
| 115 | } | ||
| 116 | }; | ||
| 117 | |||
| 118 | for my $subblock (@{$block->stmts}) { | ||
| 119 | if ($subblock->id) { | ||
| 120 | $labels{$subblock->id} = $i; | ||
| 121 | } | ||
| 122 | for my $stmt (@{$subblock->stmts}) { | ||
| 123 | if ($stmt->isa('Mildew::AST::Assign')) { | ||
| 124 | # TODO - handle profile_info more cleanly | ||
| 125 | if ($stmt->rvalue->isa('Mildew::AST::Call') && defined $Mildew::profile_info) { | ||
| 126 | $i++; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | $i++; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | $i = 0; | ||
| 133 | for my $subblock (@{$block->stmts}) { | ||
| 134 | for my $stmt (@{$subblock->stmts}) { | ||
| 135 | #$code .= "\n/*".$stmt->pretty."*/\n"; | ||
| 136 | $code .= "case $i:"; | ||
| 137 | if ($self->trace) { | ||
| 138 | $code .= "\nprintf(".quote(backslash($stmt->pretty . "\n")).");\n"; | ||
| 139 | } | ||
| 140 | if ($self->dump) { | ||
| 141 | my $file = quote(backslash($self->dump)); | ||
| 142 | $code .= "\nsmop_dump_print(interpreter,(SMOP__Object*)frame,$file);\n"; | ||
| 143 | } | ||
| 144 | |||
| 145 | if ($stmt->isa('Mildew::AST::Goto')) { | ||
| 146 | $code .= "frame->pc = " . $labels{$stmt->block->id} . ";" . "break;\n" | ||
| 147 | } elsif ($stmt->isa('Mildew::AST::Branch')) { | ||
| 148 | $code .= "frame->pc = " | ||
| 149 | . $value->($stmt->cond) | ||
| 150 | . " == SMOP__NATIVE__bool_false ? " | ||
| 151 | . $labels{$stmt->else->id} | ||
| 152 | . " : " | ||
| 153 | . $labels{$stmt->then->id} | ||
| 154 | . ";break;\n"; | ||
| 155 | } elsif ($stmt->isa('Mildew::AST::Reg')) { | ||
| 156 | # make it a noop | ||
| 157 | $code .= ';'; | ||
| 158 | } elsif ($stmt->isa('Mildew::AST::Assign')) { | ||
| 159 | if ($stmt->rvalue->isa('Mildew::AST::Call')) { | ||
| 160 | my $type = $stmt->rvalue->capture->invocant->type_info->type; | ||
| 161 | my ($c,$trailing) = $type->emit_call($i,$stmt,$value); | ||
| 162 | $code .= $c; | ||
| 163 | if ($trailing) { | ||
| 164 | $i++; | ||
| 165 | $code .= "case $i: frame->pc = " . ($i+1) . ';' . $trailing; | ||
| 166 | } | ||
| 167 | } elsif ($stmt->rvalue->isa('Mildew::AST::Phi')) { | ||
| 168 | # TODO make it a noop | ||
| 169 | $code .= ';'; | ||
| 170 | } elsif ($stmt->rvalue->isa('Mildew::AST::InferredTypeTest')) { | ||
| 171 | my $type = $stmt->rvalue->value->type_info->type; | ||
| 172 | $code .= Mildew::Emit::Yeast::assign($value->($stmt->lvalue),"SMOP_REFERENCE(interpreter,".$value->(Mildew::AST::IntegerConstant->new(value=>eval($stmt->rvalue->test) ? 1 : 0).")")); | ||
| 173 | die if $@; | ||
| 174 | } else { | ||
| 175 | $code .= Mildew::Emit::Yeast::assign($value->($stmt->lvalue),"SMOP_REFERENCE(interpreter,".$value->($stmt->rvalue).")"); | ||
| 176 | } | ||
| 177 | } else { | ||
| 178 | $code .= "/*".ref($stmt)."*/\n"; | ||
| 179 | } | ||
| 180 | $i++; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | ($funcs . $constant_decls . $init_constants . "}\n" . "static void " . $func_name . "(SMOP__Object* interpreter,SMOP__Yeast__Frame* frame) {" | ||
| 184 | . " switch (frame->pc) {" | ||
| 185 | . $code | ||
| 186 | . "case $i : frame->pc = -1;\n" | ||
| 187 | . " }}\n","SMOP__Yeast_create(" . (scalar keys %regs) | ||
| 188 | . ",(SMOP__Object*[]) {NULL}" | ||
| 189 | . ",$func_name)","${call_init_funcs}${func_name}_init(interpreter);"); | ||
| 190 | } | ||
| 191 | } |