| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | role Mildew::Backend::C { | ||
| 4 | use Mildew::AST; | ||
| 5 | use Mildew::AST::Helpers; | ||
| 6 | use File::Temp qw(tempfile tmpnam); | ||
| 7 | |||
| 8 | has cflags=>(lazy_build=>1,is=>'rw'); | ||
| 9 | has ld_library_path=>(lazy_build=>1,is=>'rw'); | ||
| 10 | has load_setting=>(default=>1,is=>'rw'); | ||
| 11 | has valgrind=>(default=>0,is=>'rw'); | ||
| 12 | has gdb=>(default=>0,is=>'rw'); | ||
| 13 | has wrap_in_block=>(default=>1,is=>'rw'); | ||
| 14 | |||
| 15 | method _build_cflags { | ||
| 16 | require SMOP; | ||
| 17 | [SMOP::lib_flags(),SMOP::include_flags()]; | ||
| 18 | } | ||
| 19 | method _build_ld_library_path { | ||
| 20 | require SMOP; | ||
| 21 | require Mildew::Setting::SMOP; | ||
| 22 | [Mildew::Setting::SMOP::ld_library_path(),SMOP::ld_library_path()]; | ||
| 23 | } | ||
| 24 | |||
| 25 | requires 'c_source'; | ||
| 26 | |||
| 27 | # false doesn't support setr | ||
| 28 | sub wrap_in_block_without_setr { | ||
| 29 | my ($ast,$scope) = @_; | ||
| 30 | Mildew::AST::Block->new(regs=>['interpreter','scope'],stmts=>[fcall(call(new => FETCH(lookup('Code')),[],[string 'outer'=>($scope // reg '$scope'),string 'signature'=>empty_sig(),string 'mold' => $ast]))]); | ||
| 31 | } | ||
| 32 | |||
| 33 | method compile($ast,$output) { | ||
| 34 | die "-o is required when compiling to an executable\n" unless $output; | ||
| 35 | my ($c_fh,$c_file) = tempfile(); | ||
| 36 | binmode($c_fh,":utf8"); | ||
| 37 | my $wrapped_ast = $self->wrap_in_block ? wrap_in_block_without_setr($ast,$self->enclosing_scope) : $ast; | ||
| 38 | print $c_fh $self->c_source($wrapped_ast); | ||
| 39 | |||
| 40 | |||
| 41 | # compile the c source to the executable | ||
| 42 | system("gcc","-g","-xc",@{$self->cflags},$c_file,"-o",$output); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | method get_boilerplate { | ||
| 47 | require SMOP::Boilerplate; | ||
| 48 | return $SMOP::Boilerplate::BOILERPLATE; | ||
| 49 | } | ||
| 50 | |||
| 51 | method run($ast) { | ||
| 52 | my $tmp_executable = tmpnam; | ||
| 53 | $self->compile($ast,$tmp_executable); | ||
| 54 | |||
| 55 | local $ENV{LD_LIBRARY_PATH} = join(':',@{$self->ld_library_path}); | ||
| 56 | |||
| 57 | #'../mildew-old/CORE:../smop/build/lib'; | ||
| 58 | # local $ENV{PERL5LIB} = "../smop/SMOP/blib/lib/:../smop/SMOP/blib/arch:" . ($ENV{PERL5LIB} || ''); | ||
| 59 | |||
| 60 | my @debug_aids; | ||
| 61 | @debug_aids = 'valgrind' if $self->valgrind; | ||
| 62 | @debug_aids = 'gdb' if $self->gdb; | ||
| 63 | exec(@debug_aids,$tmp_executable); | ||
| 64 | } | ||
| 65 | |||
| 66 | method path_to_setting { | ||
| 67 | 'MildewCORE.setting.so'; | ||
| 68 | } | ||
| 69 | |||
| 70 | # load the setting | ||
| 71 | method enclosing_scope { | ||
| 72 | |||
| 73 | $self->load_setting ? call(load => call(new => FETCH(lookup 'MildewSOLoader')), | ||
| 74 | [string $self->path_to_setting,FETCH(lookup('$LexicalPrelude'))]) | ||
| 75 | : reg '$scope'; | ||
| 76 | } | ||
| 77 | } |