| 1 | use v5.10; | ||
| 2 | use MooseX::Declare; | ||
| 3 | use SMOP; | ||
| 4 | |||
| 5 | # TODO seperate the shared library creation from the type of the C backend | ||
| 6 | class Mildew::Backend::C::So extends Mildew::Backend::OptC { | ||
| 7 | use Mildew::AST; | ||
| 8 | use Mildew::AST::Helpers; | ||
| 9 | use File::Temp qw(tempfile tmpnam); | ||
| 10 | |||
| 11 | method compile($ast,$output) { | ||
| 12 | die "-o is required when compiling to an executable\n" unless $output; | ||
| 13 | my ($c_fh,$c_file) = tempfile(); | ||
| 14 | binmode($c_fh,":utf8"); | ||
| 15 | # TODO handle_YOU_ARE_HERE only on settings | ||
| 16 | |||
| 17 | print $c_fh $self->c_source(wrap_in_block( | ||
| 18 | $self->handle_YOU_ARE_HERE($ast), | ||
| 19 | $self->enclosing_scope | ||
| 20 | )); | ||
| 21 | |||
| 22 | # compile the c source to the executable | ||
| 23 | $ENV{LD_RUN_PATH} = SMOP::ld_library_path; | ||
| 24 | system("gcc","-fPIC","-g","-xc",@{$self->cflags},"-shared",$c_file,"-o",$output); | ||
| 25 | } | ||
| 26 | method handle_YOU_ARE_HERE($ast) { | ||
| 27 | |||
| 28 | Mildew::AST::Block->new( | ||
| 29 | regs=>[@{$ast->regs},'YOU_ARE_HERE'], | ||
| 30 | stmts=>trailing_return([ | ||
| 31 | Mildew::AST::Assign->new(lvalue=>reg '$YOU_ARE_HERE',rvalue=>reg '$scope'), | ||
| 32 | @{$ast->stmts}, | ||
| 33 | reg '$YOU_ARE_HERE']) | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | method get_boilerplate { | ||
| 39 | return <<'BOILERPLATE'; | ||
| 40 | #include <stdio.h> | ||
| 41 | #include <smop/base.h> | ||
| 42 | #include <smop/mold.h> | ||
| 43 | #include <smop/capture.h> | ||
| 44 | #include <smop/s0native.h> | ||
| 45 | #include <smop/native.h> | ||
| 46 | #include <smop/s1p.h> | ||
| 47 | #include <smop/yeast.h> | ||
| 48 | #include <smop/dump.h> | ||
| 49 | %%FUNCS%% | ||
| 50 | void init(SMOP__Object* interpreter,SMOP__Object* scope,SMOP__Object* continuation) { | ||
| 51 | %%BODY%% | ||
| 52 | smop_reg_set(interpreter,frame,0,SMOP_REFERENCE(interpreter,interpreter)); | ||
| 53 | smop_reg_set(interpreter,frame,1,scope); | ||
| 54 | smop_back_set(interpreter,frame,continuation); | ||
| 55 | |||
| 56 | SMOP_DISPATCH(interpreter, SMOP_RI(interpreter), | ||
| 57 | SMOP__NATIVE__idconst_create("goto"), | ||
| 58 | SMOP__NATIVE__capture_create(interpreter, | ||
| 59 | (SMOP__Object*[]) {SMOP_REFERENCE(interpreter,interpreter),frame,NULL}, | ||
| 60 | (SMOP__Object*[]) {NULL}) | ||
| 61 | ); | ||
| 62 | |||
| 63 | } | ||
| 64 | BOILERPLATE | ||
| 65 | } | ||
| 66 | |||
| 67 | method run($ast) { | ||
| 68 | die "You can't run a shared library.\n"; | ||
| 69 | } | ||
| 70 | } |