|
| 1 | +#define PERL_NO_GET_CONTEXT |
| 2 | +#include "EXTERN.h" |
| 3 | +#include "perl.h" |
| 4 | +#include "XSUB.h" |
| 5 | + |
| 6 | +#include "ppport.h" |
| 7 | + |
| 8 | +#include <moar.h> |
| 9 | + |
| 10 | +SV *(*call_method_callback)(IV, char *); |
| 11 | +MVMInstance *instance; |
| 12 | +MVMCompUnit *cu; |
| 13 | +const char *filename = "/home/nine/install/rakudo/install/languages/perl6/runtime/perl6.moarvm"; |
| 14 | + |
| 15 | +static void toplevel_initial_invoke(MVMThreadContext *tc, void *data) { |
| 16 | + /* Create initial frame, which sets up all of the interpreter state also. */ |
| 17 | + MVM_frame_invoke(tc, (MVMStaticFrame *)data, MVM_callsite_get_common(tc, MVM_CALLSITE_ID_NULL_ARGS), NULL, NULL, NULL, -1); |
| 18 | +} |
| 19 | + |
| 20 | +void p6_run_code(char *code) { |
| 21 | + const char *raw_clargs[1]; |
| 22 | + instance->num_clargs = 1; |
| 23 | + raw_clargs[0] = code; |
| 24 | + instance->raw_clargs = raw_clargs; |
| 25 | + instance->clargs = NULL; /* clear cache */ |
| 26 | + |
| 27 | + MVMThreadContext *tc = instance->main_thread; |
| 28 | + MVMStaticFrame *start_frame; |
| 29 | + |
| 30 | + start_frame = cu->body.main_frame ? cu->body.main_frame : cu->body.frames[0]; |
| 31 | + MVM_interp_run(tc, &toplevel_initial_invoke, start_frame); |
| 32 | +} |
| 33 | + |
| 34 | +void init_call_method(SV *(*call_p6_method)(IV, char *)) { |
| 35 | + call_method_callback = call_p6_method; |
| 36 | +} |
| 37 | + |
| 38 | +MODULE = Inline::Perl6 PACKAGE = Inline::Perl6 |
| 39 | + |
| 40 | +void |
| 41 | +p6_initialize() |
| 42 | + CODE: |
| 43 | + const char *executable_name = NULL; |
| 44 | + const char *lib_path[8]; |
| 45 | + |
| 46 | + int dump = 0; |
| 47 | + int full_cleanup = 0; |
| 48 | + int argi = 1; |
| 49 | + int lib_path_i = 0; |
| 50 | + |
| 51 | + MVM_crash_on_error(); |
| 52 | + |
| 53 | + instance = MVM_vm_create_instance(); |
| 54 | + lib_path[lib_path_i++] = "/home/nine/install/rakudo/install/languages/nqp/lib"; |
| 55 | + lib_path[lib_path_i++] = "/home/nine/install/rakudo/install/languages/perl6/lib"; |
| 56 | + lib_path[lib_path_i++] = "/home/nine/install/rakudo/install/languages/perl6/runtime"; |
| 57 | + lib_path[lib_path_i++] = NULL; |
| 58 | + |
| 59 | + for( argi = 0; argi < lib_path_i; argi++) |
| 60 | + instance->lib_path[argi] = lib_path[argi]; |
| 61 | + |
| 62 | + /* stash the rest of the raw command line args in the instance */ |
| 63 | + instance->num_clargs = 0; |
| 64 | + instance->prog_name = "/home/nine/install/rakudo/install/languages/perl6/runtime/perl6.moarvm"; |
| 65 | + instance->exec_name = "perl6"; |
| 66 | + instance->raw_clargs = NULL; |
| 67 | + |
| 68 | + /* Map the compilation unit into memory and dissect it. */ |
| 69 | + MVMThreadContext *tc = instance->main_thread; |
| 70 | + cu = MVM_cu_map_from_file(tc, filename); |
| 71 | + |
| 72 | + call_method_callback = NULL; |
| 73 | + |
| 74 | + MVMROOT(tc, cu, { |
| 75 | + /* The call to MVM_string_utf8_decode() may allocate, invalidating the |
| 76 | + location cu->body.filename */ |
| 77 | + MVMString *const str = MVM_string_utf8_decode(tc, instance->VMString, filename, strlen(filename)); |
| 78 | + cu->body.filename = str; |
| 79 | + |
| 80 | + /* Run deserialization frame, if there is one. */ |
| 81 | + if (cu->body.deserialize_frame) { |
| 82 | + MVM_interp_run(tc, &toplevel_initial_invoke, cu->body.deserialize_frame); |
| 83 | + } |
| 84 | + }); |
| 85 | + p6_run_code("/home/nine/Inline-Perl6/inline.pl6"); |
| 86 | + |
| 87 | + /* Points to the current opcode. */ |
| 88 | + MVMuint8 *cur_op = NULL; |
| 89 | + |
| 90 | + /* The current frame's bytecode start. */ |
| 91 | + MVMuint8 *bytecode_start = NULL; |
| 92 | + |
| 93 | + /* Points to the base of the current register set for the frame we |
| 94 | + * are presently in. */ |
| 95 | + MVMRegister *reg_base = NULL; |
| 96 | + |
| 97 | + /* The current call site we're constructing. */ |
| 98 | + MVMCallsite *cur_callsite = NULL; |
| 99 | + |
| 100 | + /* Stash addresses of current op, register base and SC deref base |
| 101 | + * in the TC; this will be used by anything that needs to switch |
| 102 | + * the current place we're interpreting. */ |
| 103 | + tc->interp_cur_op = &cur_op; |
| 104 | + tc->interp_bytecode_start = &bytecode_start; |
| 105 | + tc->interp_reg_base = ®_base; |
| 106 | + tc->interp_cu = &cu; |
| 107 | + toplevel_initial_invoke(tc, cu->body.main_frame); |
| 108 | + |
| 109 | +void |
| 110 | +p6_destroy() |
| 111 | + CODE: |
| 112 | + MVM_vm_exit(instance); |
| 113 | + |
| 114 | +void |
| 115 | +p6_call_method(name) |
| 116 | + char *name |
| 117 | + CODE: |
| 118 | + MVMThreadContext *tc = instance->main_thread; |
| 119 | + call_method_callback(0, name); |
| 120 | + |
0 commit comments