Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

737 lines (496 sloc) 18.268 kB
/*
Copyright (C) 2001-2015, Parrot Foundation.
=head1 NAME
src/pmc/parrotinterpreter.pmc - ParrotInterpreter PMC
=head1 DESCRIPTION
These are the vtable functions for the ParrotInterpreter base class
getinterp P0
set P1, P0[.IGLOBALS_*] # access interpreter globals
set I0, P0[x] # interpinfo I0, x
set I0, P0[-1] # get interpreter flags
set P0[-1], x # set flags on interpreter
# NOTE: this doesn't restart
=head2 Functions
=over 4
=cut
*/
#include "parrot/dynext.h"
#include "parrot/io.h"
#include "parrot/runcore_api.h"
#include "parrot/thread.h"
#include "pmc/pmc_class.h"
#include "pmc/pmc_sub.h"
#include "pmc/pmc_proxy.h"
#include "pmc/pmc_task.h"
#define PMC_interp(x) ((Parrot_ParrotInterpreter_attributes *)PMC_data(x))->interp
#define PMC_args(x) ((Parrot_ParrotInterpreter_attributes *)PMC_data(x))->args
#define PMC_sub(x) ((Parrot_ParrotInterpreter_attributes *)PMC_data(x))->sub
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
static void create_interp(
ARGIN(PMC *self),
ARGIN_NULLOK(Parrot_Interp parent))
__attribute__nonnull__(1);
#define ASSERT_ARGS_create_interp __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(self))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<static void create_interp(PMC *self, Parrot_Interp parent)>
Creates a new child interpreter of C<parent>.
=cut
*/
static void
create_interp(ARGIN(PMC *self), ARGIN_NULLOK(Parrot_Interp parent))
{
ASSERT_ARGS(create_interp)
Interp_flags flag = PARROT_NO_FLAGS;
Parrot_Interp new_interp;
new_interp = Parrot_interp_make_interpreter(parent, (INTVAL)flag);
PMC_interp(self) = new_interp;
VTABLE_set_pmc_keyed_int(new_interp, new_interp->iglobals,
(INTVAL) IGLOBALS_INTERPRETER, self);
}
pmclass ParrotInterpreter no_ro manual_attrs provides invokable {
ATTR struct parrot_interp_t *interp; /* this PMC's interpreter */
ATTR INTVAL tid; /* thread id. Unused, see get_integer() */
ATTR PMC *args; /* args passed to this thread */
ATTR PMC *sub; /* this thread's sub */
/*
=back
=head2 Methods
=over 4
=item C<yield>
Yield the current thread
=item C<recursion_limit(INTVAL l :optional, INTVAL has_l :opt_flag )>
Gets the recursion limit of the interpreter, optionally setting it to something
new.
=cut
*/
METHOD recursion_limit(INTVAL l :optional, INTVAL has_l :opt_flag) :no_wb {
const INTVAL ret = INTERP->recursion_limit;
UNUSED(SELF)
if (has_l)
INTERP->recursion_limit = l;
RETURN(INTVAL ret);
}
/*
=item C<void init()>
Initializes the interpreter.
=cut
*/
VTABLE void init() {
if (!PMC_data(SELF)) {
Parrot_ParrotInterpreter_attributes * const attrs =
mem_gc_allocate_zeroed_typed(INTERP, Parrot_ParrotInterpreter_attributes);
PMC_data(SELF) = attrs;
}
if (!PMC_interp(SELF))
create_interp(SELF, INTERP);
PObj_custom_destroy_SET(SELF);
}
/*
=item C<void init_pmc(PMC *parent)>
Initializes a child interpreter with C<*parent> if C<parent> is
a ParrotInterpreter instance. Otherwise takes the thread ID from
C<parent> and uses that thread.
=cut
*/
VTABLE void init_pmc(PMC *parent) {
if (!PMC_data(SELF)) {
Parrot_ParrotInterpreter_attributes * const attrs =
mem_gc_allocate_zeroed_typed(INTERP, Parrot_ParrotInterpreter_attributes);
PMC_data(SELF) = attrs;
}
if (!PMC_interp(SELF))
create_interp(SELF, PMC_interp(parent));
PObj_custom_destroy_SET(SELF);
}
/*
=item C<void destroy()>
Destroys the PMC.
=cut
*/
VTABLE void destroy() :no_wb {
if (PMC_data(SELF)) {
mem_gc_free(INTERP, PMC_data(SELF));
PMC_data(SELF) = NULL;
}
}
/*
=item C<void set_pointer(void *value)>
Sets C<struct_val> to C<*value>.
=cut
*/
VTABLE void set_pointer(void *value) {
/* XXX: init_world in src/global_setup.c needs to create a
* ParrotInterpreter through Parrot_pmc_new_noinit. If this PMC hasn't been
* initialized, cheat by initializing instead. */
if (!PMC_data(SELF)) {
Parrot_ParrotInterpreter_attributes * const attrs =
mem_gc_allocate_zeroed_typed(INTERP, Parrot_ParrotInterpreter_attributes);
PMC_data(SELF) = attrs;
PObj_custom_destroy_SET(SELF);
}
PMC_interp(SELF) = (struct parrot_interp_t *)value;
}
/*
=item C<void *get_pointer()>
Returns C<struct_val>.
=cut
*/
VTABLE void *get_pointer() :no_wb {
UNUSED(INTERP)
return PMC_interp(SELF);
}
/*
=item C<INTVAL get_integer()>
Returns the thread id of the interpreter.
=cut
*/
VTABLE INTVAL get_integer() :no_wb {
UNUSED(INTERP)
const Parrot_Interp i = PMC_interp(SELF);
if (i->thread_data)
return (INTVAL)i->thread_data->tid;
return 0;
}
/*
=item C<opcode_t *invoke(void *next)>
Runs the interpreter's byte code.
=cut
*/
VTABLE opcode_t *invoke(void *next) :no_wb {
Interp * const new_interp = PMC_interp(SELF);
/* TODO pass arguments from parent (interp) to child (new_interp) by
* possibly clone of share the arguments r/o args can be passed as is */
/* calculate offset and run */
runops(new_interp, (size_t)((opcode_t *)PMC_sub(SELF) -
(opcode_t *)INTERP->code->base.data));
return (opcode_t *)next;
}
/*
=item C<PMC *get_pmc()>
Return this Thread's args.
=cut
*/
VTABLE PMC *get_pmc() :no_wb {
UNUSED(INTERP)
return PMC_args(SELF);
}
/*
=item C<void set_pmc(PMC *args)>
Set this Thread's args.
=cut
*/
VTABLE void set_pmc(PMC *args) {
PMC_args(SELF) = args;
}
/*
=item C<PMC *get_pmc_keyed_int(INTVAL key)>
Returns the PMC global value for C<key>.
=cut
*/
VTABLE PMC *get_pmc_keyed_int(INTVAL key) :no_wb {
Interp * const new_interp = PMC_interp(SELF);
UNUSED(INTERP)
if (key >= 0 && key < IGLOBALS_SIZE) {
PMC * const val = VTABLE_get_pmc_keyed_int(new_interp,
new_interp->iglobals, key);
return val == NULL ? PMCNULL : val;
}
/* quick hack to get the global stash */
if (key == -1)
return new_interp->root_namespace;
return PMCNULL;
}
/*
=item C<PMC *get_pmc_keyed(PMC *key)>
Introspection interface. C<key> can be:
"context" ... return Context PMC
"sub" ... return Sub object of this subroutine
"continuation" ... return Continuation PMC
"lexpad" ... return lexpad PMC for this sub
"namespace" ... return namespace PMC for this sub
"outer" ... return outer sub of this closure
"<item>"; level ... same for caller <level>
"annotations"; level > 0 ... annotations at point of call <level>s down
"outer"; "<item>" ... same for outer level 1
"outer"; "<item>"; level ... same for outer <level>
"globals" ... return global stash
=cut
*/
VTABLE PMC *get_pmc_keyed(PMC *key) :no_wb {
PMC *nextkey;
STRING *outer = NULL;
STRING *item = Parrot_key_string(INTERP, key);
int level = 0;
PMC *ctx;
UNUSED(SELF)
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "globals")))
return INTERP->root_namespace;
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "packfile")))
return Parrot_pf_get_current_packfile(INTERP);
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "outer"))) {
outer = item;
nextkey = Parrot_key_next(INTERP, key);
if (nextkey && (PObj_get_FLAGS(nextkey) & KEY_string_FLAG)) {
key = nextkey;
item = VTABLE_get_string(INTERP, key);
}
}
nextkey = Parrot_key_next(INTERP, key);
if (nextkey)
level = VTABLE_get_integer(INTERP, nextkey);
else if (outer)
level = 1;
if (level < 0)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"No such caller depth");
ctx = CURRENT_CONTEXT(INTERP);
if (outer) {
for (; level; --level) {
ctx = Parrot_pcc_get_outer_ctx(INTERP, ctx);
if (PMC_IS_NULL(ctx))
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION, "No such outer depth");
}
}
else {
for (; level; --level) {
PMC * const cont = Parrot_pcc_get_continuation(INTERP, ctx);
if (PMC_IS_NULL(cont) || !PARROT_CONTINUATION(cont)->seg)
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION, "No such caller depth");
ctx = PARROT_CONTINUATION(cont)->to_ctx;
if (PMC_IS_NULL(Parrot_pcc_get_sub(INTERP, ctx)))
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION, "No such caller depth");
}
}
if (item == outer)
return Parrot_pcc_get_sub(INTERP, ctx);
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "context")))
return ctx;
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "sub")))
return Parrot_pcc_get_sub(INTERP, ctx);
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "lexpad")))
return Parrot_pcc_get_lex_pad(INTERP, ctx);
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "namespace")))
return Parrot_pcc_get_namespace(INTERP, ctx);
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "continuation")))
return VTABLE_clone(INTERP, Parrot_pcc_get_continuation(INTERP, ctx));
if (STRING_equal(INTERP, item, CONST_STRING(INTERP, "annotations"))) {
PMC * const sub_pmc = Parrot_pcc_get_sub(INTERP, ctx);
if (ctx == CURRENT_CONTEXT(INTERP)) {
/* We can't know the current program counter for the currently
* executing sub, so can't return annotations for that. */
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"Cannot get annotations at depth 0; use annotations op instead.");
}
if (!PMC_IS_NULL(sub_pmc)
&& sub_pmc->vtable->base_type == enum_class_Sub) {
Parrot_Sub_attributes *sub;
PackFile_ByteCode *seg;
opcode_t * const pc = Parrot_pcc_get_pc(INTERP, ctx);
PMC_get_sub(INTERP, sub_pmc, sub);
seg = sub->seg;
if (sub->seg->annotations)
return Parrot_pf_annotations_lookup(INTERP, seg->annotations,
pc - seg->base.data, NULL);
}
return Parrot_pmc_new(INTERP, enum_class_Hash);
}
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
"No such item %Ss", item);
}
/*
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
Returns the interpreter info for C<key>.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL key) :no_wb {
UNUSED(INTERP)
Interp * const new_interp = PMC_interp(SELF);
if (key == -1)
return (INTVAL)new_interp->flags;
return Parrot_interp_info(new_interp, key);
}
/*
=item C<void set_integer_keyed_int(INTVAL key, INTVAL val)>
Sets the interpreter info for C<key> to C<val>.
=cut
*/
VTABLE void set_integer_keyed_int(INTVAL key, INTVAL val) {
Interp * const new_interp = PMC_interp(SELF);
/* set interpreter flags */
if (key == -1) {
const INTVAL allowed = PARROT_BOUNDS_FLAG | PARROT_PROFILE_FLAG |
PARROT_GC_DEBUG_FLAG;
Parrot_interp_clear_flag(new_interp, allowed);
Parrot_interp_set_flag(new_interp, val & allowed);
}
}
/*
=item C<PMC *clone()>
First attempt to make things running, and to see, where problems may
arise. Only minimal items are done yet.
XXX this should of course call C<Parrot_clone()> and use freeze/thaw.
=cut
*/
VTABLE PMC *clone() :no_wb {
PMC * const dest = Parrot_interp_clone(PMC_interp(SELF), PARROT_CLONE_DEFAULT);
UNUSED(INTERP)
return dest;
}
/*
=item C<INTVAL is_equal(PMC *val)>
Returns whether the interpreter is equal to C<*val>.
Two interpreters (threads) are equal if both are non-threaded or they
have the same thread id.
=cut
*/
MULTI INTVAL is_equal(ParrotInterpreter val) :no_wb {
UNUSED(INTERP)
Parrot_Interp self = PMC_interp(SELF);
Parrot_Interp other = PMC_interp(val);
return self == other;
}
MULTI INTVAL is_equal(DEFAULT value) :no_wb {
UNUSED(SELF)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INTERNAL_NOT_IMPLEMENTED,
"ParrotInterpreter: no multiple dispatch variant 'is_equal' for %Ss",
VTABLE_name(INTERP, value));
}
/*
=item C<void visit(PMC *info)>
This is used by freeze/thaw to visit the contents of the interpreter.
C<*info> is the visit info, (see F<include/parrot/pmc_freeze.h>).
=item C<void freeze(PMC *info)>
Used to archive the interpreter. Actually not the whole interpreter is
frozen but the state of the interpreter, which includes everything that
has changes since creating an empty interpreter.
=item C<void thaw(PMC *info)>
Used to unarchive the interpreter. This merges the changes into this
interpreter instance.
=item C<void thawfinish(PMC *info)>
Finish thawing.
=cut
*/
VTABLE void freeze(PMC *info) :no_wb {
UNUSED(SELF)
UNUSED(info)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"Attempt to freeze interpreter");
}
METHOD run_gc() {
Parrot_gc_mark_and_sweep(PMC_interp(SELF), 0);
}
/*
=item METHOD hll_map(PMC core_type,PMC hll_type)
Map core_type to hll_type.
=cut
*/
METHOD hll_map(PMC *core_type, PMC *hll_type) :no_wb {
const INTVAL core_type_id = VTABLE_type(INTERP, core_type);
const INTVAL hll_type_id = VTABLE_type(INTERP, hll_type);
const INTVAL hll_id = Parrot_pcc_get_HLL(INTERP, CURRENT_CONTEXT(INTERP));
UNUSED(SELF)
Parrot_hll_register_HLL_type(INTERP, hll_id, core_type_id, hll_type_id);
}
/*
=item METHOD stdin_handle(PMC *newhandle :optional)
If a PMC object is provided, the standard input handle for this interpreter
is set to that PMC, and the new PMC handle is returned.
If no PMC object is provided, the current standard input handle is returned.
=cut
*/
METHOD stdin_handle(PMC *newhandle :optional) :no_wb {
PMC * const handle = Parrot_io_stdhandle(INTERP, PIO_STDIN_FILENO, newhandle);
UNUSED(SELF)
RETURN(PMC *handle);
}
/*
=item METHOD stdout_handle(PMC *newhandle :optional)
If a PMC object is provided, the standard output handle for this interpreter
is set to that PMC, and the new PMC handle is returned.
If no PMC object is provided, the current standard output handle is returned.
=cut
*/
METHOD stdout_handle(PMC *newhandle :optional) :no_wb {
PMC * const handle = Parrot_io_stdhandle(INTERP, PIO_STDOUT_FILENO, newhandle);
RETURN(PMC *handle);
}
/*
=item METHOD stderr_handle(PMC *newhandle :optional)
If a PMC object is provided, the standard error handle for this interpreter
is set to that PMC, and the new PMC handle is returned.
If no PMC object is provided, the current standard error handle is returned.
=cut
*/
METHOD stderr_handle(PMC *newhandle :optional) :no_wb {
PMC * const handle = Parrot_io_stdhandle(INTERP, PIO_STDERR_FILENO, newhandle);
RETURN(PMC *handle);
}
/*
=item METHOD getpid()
Returns the pid of the current process, 0 in platforms that doesn't
support it.
=cut
*/
METHOD getpid() :no_wb {
const INTVAL id = Parrot_getpid();
RETURN(INTVAL id);
}
/*
=item METHOD current_task()
Returns the task currently executed by this interpreter.
=cut
*/
METHOD current_task() :no_wb {
Interp * const this_interp = PMC_interp(SELF);
PMC * const current_task = this_interp->cur_task;
RETURN(PMC *current_task);
}
/*
=item METHOD schedule(PMC *task)
Schedules the given task on this interpreter.
=cut
*/
METHOD schedule(PMC *task) :no_wb {
Interp * const this_interp = PMC_interp(SELF);
Parrot_cx_schedule_immediate(this_interp, task);
}
/*
=item METHOD schedule_proxied(PMC *task, PMC *proxy)
Schedules the given task on the proxy's interpreter.
=cut
*/
METHOD schedule_proxied(PMC *task, PMC *proxy) :no_wb {
Parrot_Proxy_attributes * const core_struct = PARROT_PROXY(proxy);
Interp * const proxied_interp = core_struct->interp;
UNUSED(SELF)
#ifdef PARROT_HAS_THREADS
/* don't run GC from the wrong thread since GC involves stack walking and we
* don't want the foreign GC to find our objects */
Parrot_block_GC_mark_locked(proxied_interp);
Parrot_cx_schedule_immediate(proxied_interp,
Parrot_thread_create_local_task(INTERP, proxied_interp, task));
Parrot_unblock_GC_mark_locked(proxied_interp);
#else
Parrot_cx_schedule_immediate(interp, task);
#endif
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Jump to Line
Something went wrong with that request. Please try again.