parrotcode: Multimethod dispatch for binary opcode functions | |
Contents | C |
src/mmd.c - Multimethod dispatch for binary opcode functions
This system is set up to handle type-based dispatching for binary (i.e. two-arg) functions. This includes, though isn't necessarily limited to, binary operators such as addition or subtraction.
The MMD system is straightforward, and currently must be explicitly invoked, for example by a vtable function. (We may reserve the right to use MMD in all circumstances, but currently do not).
For the purposes of the API,
each MMD-able function is assigned a unique number which is used to find the correct function table.
This is the func_num
parameter in the following functions.
While Parrot isn't restricted to a predefined set of functions,
it does set things up so that all the binary vtable functions have a MMD table preinstalled for them,
with default behaviour.
binop_mmd_funcs->x
and ->y
are table sizes not highest type in table.
static PMC *mmd_deref(Interp *interp, INTVAL function, PMC *value)
value
is a reference-like PMC,
dereference it so we can make an MMD call on the 'real' value.static void mmd_ensure_writable(Interp *, INTVAL function, PMC *pmc)
pmc
is writable enough for function
.PMC *mmd_dispatch_p_ppp(Interp *, PMC *left, PMC *right, PMC *dest, INTVAL function)
left
,
right
,
and dest
are all PMC pointers,
while func_num
is the MMD table that should be used to do the dispatching.
If the dest
pointer is NULL,
it dispatches two a two-argument function that returns a new dest
always.left
and right
and call it,
passing in left
,
right
,
and possibly dest
like any other binary vtable function.PMC *mmd_dispatch_p_pip(Interp *, PMC *left, INTVAL right, PMC *dest, INTVAL function)
PMC *mmd_dispatch_p_pnp(Interp *, PMC *left, FLOATVAL right, PMC *dest, INTVAL function)
PMC *mmd_dispatch_p_psp(Interp *, PMC *left, STRING *right, PMC *dest, INTVAL function)
void mmd_dispatch_v_pp(Interp *, PMC *left, PMC *right, INTVAL function)
void mmd_dispatch_v_pi(Interp *, PMC *left, INTVAL right, INTVAL function)
void mmd_dispatch_v_pn(Interp *, PMC *left, FLOATVAL right, INTVAL function)
void mmd_dispatch_v_ps(Interp *, PMC *left, STRING *right, INTVAL function)
left <op=> right
. left = mmd_deref(interp, func_nr, left);
right = mmd_deref(interp, func_nr, right);
real_function = (mmd_f_p_ppp)get_mmd_dispatcher(interp,
left, right, func_nr, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (dest)
return Parrot_runops_fromc_args(interp, sub, "PPPP",
left, right, dest);
else
return Parrot_runops_fromc_args(interp, sub, "PPP",
left, right);
}
else {
return (*real_function)(interp, left, right, dest);
}
}
UINTVAL left_type;
mmd_f_p_pip real_function;
left = mmd_deref(interp, func_nr, left);
left_type = left->vtable->base_type;
real_function =
(mmd_f_p_pip)get_mmd_dispatch_type(interp, func_nr,
left_type, enum_type_INTVAL,
&is_pmc);
if (is_pmc) {
PMC * const sub = (PMC*)real_function;
if (dest)
return Parrot_runops_fromc_args(interp, sub, "PPIP",
left, right, dest);
else
return Parrot_runops_fromc_args(interp, sub, "PPI",
left, right);
}
else {
return (*real_function)(interp, left, right, dest);
}
}
left = mmd_deref(interp, func_nr, left);
left_type = left->vtable->base_type;
real_function = (mmd_f_p_pnp)get_mmd_dispatch_type(interp,
func_nr, left_type, enum_type_FLOATVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (dest)
return Parrot_runops_fromc_args(interp, sub, "PPNP",
left, right, dest);
else
return Parrot_runops_fromc_args(interp, sub, "PPN",
left, right);
}
else {
return (*real_function)(interp, left, right, dest);
}
}
left_type = left->vtable->base_type;
real_function = (mmd_f_p_psp)get_mmd_dispatch_type(interp,
func_nr, left_type, enum_type_STRING, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
if (dest)
return Parrot_runops_fromc_args(interp, sub, "PPSP",
left, right, dest);
else
return Parrot_runops_fromc_args(interp, sub, "PPS",
left, right);
}
else {
return (*real_function)(interp, left, right, dest);
}
}
left = mmd_deref(interp, func_nr, left);
right = mmd_deref(interp, func_nr, right);
mmd_ensure_writable(interp, func_nr, left);
real_function = (mmd_f_v_pp)get_mmd_dispatcher(interp,
left, right, func_nr, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
Parrot_runops_fromc_args(interp, sub, "vPP", left, right);
}
else {
(*real_function)(interp, left, right);
}
}
left = mmd_deref(interp, func_nr, left);
mmd_ensure_writable(interp, func_nr, left);
left_type = left->vtable->base_type;
real_function = (mmd_f_v_pi)get_mmd_dispatch_type(interp,
func_nr, left_type, enum_type_INTVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
Parrot_runops_fromc_args(interp, sub, "vPI", left, right);
}
else {
(*real_function)(interp, left, right);
}
}
left = mmd_deref(interp, func_nr, left);
mmd_ensure_writable(interp, func_nr, left);
left_type = left->vtable->base_type;
real_function = (mmd_f_v_pn)get_mmd_dispatch_type(interp,
func_nr, left_type, enum_type_FLOATVAL, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
Parrot_runops_fromc_args(interp, sub, "vPN", left, right);
}
else {
(*real_function)(interp, left, right);
}
}
left = mmd_deref(interp, func_nr, left);
mmd_ensure_writable(interp, func_nr, left);
left_type = VTABLE_type(interp, left);
real_function = (mmd_f_v_ps)get_mmd_dispatch_type(interp,
func_nr, left_type, enum_type_STRING, &is_pmc);
if (is_pmc) {
sub = (PMC*)real_function;
Parrot_runops_fromc_args(interp, sub, "vPS", left, right);
}
else {
(*real_function)(interp, left, right);
}
}
INTVAL mmd_dispatch_i_pp(Interp *interp, PMC *left, PMC *right, INTVAL func_nr)
mmd_dispatch_p_ppp()
, only it returns an INTVAL
. This is used by MMD compare functions.void mmd_add_function(Interp *interp, INTVAL funcnum, funcptr_t function)
func_num
is the number of the new function. function
is ignored.static void mmd_expand_x(Interp *interp, INTVAL func_nr, INTVAL new_x)
new_x
.static void mmd_expand_y(Interp *interp, INTVAL func_nr, INTVAL new_y)
void mmd_add_by_class(Interp *interp, INTVAL functype, STRING *left_class, STRING *right_class, funcptr_t funcptr)
funcptr
to the func_num
function table that will be invoked when the left parameter is of class left_class
and the right parameter is of class right_class
. Both classes are STRING *
s that hold the PMC class names for the left and right sides. If either class isn't yet loaded, Parrot will cache the information such that the function will be installed if at some point in the future both classes are available.void mmd_register(Interp *interp, INTVAL func_num, INTVAL left_type, INTVAL right_type, funcptr_t funcptr)
funcptr
for MMD function table func_num
for classes left_type
and right_type
. The left and right types are INTVAL
s that represent the class ID numbers.void mmd_destroy(Parrot_Interp interp)
PMC *mmd_vtfind(Parrot_Interp interp, INTVAL type, INTVAL left, INTVAL right)
PMC *Parrot_MMD_search_default_inline(Interp *, STRING *meth, STRING *signature, ...)
PMC *Parrot_MMD_search_default_func(Interp *, STRING *meth)
PMC *Parrot_MMD_dispatch_func(Interp *, PMC *multi)
static PMC *mmd_arg_tuple_inline(Interp *, STRING *signature, va_list args)
static PMC *mmd_arg_tuple_func(Interp *)
static PMC *mmd_search_default(Interp *, STRING *meth, PMC *arg_tuple)
static void mmd_search_classes(Interp *, STRING *meth, PMC *arg_tuple, PMC *cl, INTVAL start_at_parent)
cl
and return a list of all candidates. start_at_parent
is 0 to start at the class itself or 1 to search from the first parent class.static UINTVAL mmd_distance(Interp *, PMC *pmc, PMC *arg_tuple)
pmc
against given argument types. 0xffff is the maximum distancestatic void mmd_sort_candidates(Interp *, PMC *arg_tuple, PMC *cl)
cl
by Manhattan Distancestatic PMC *mmd_search_scopes(Interp *, STRING *meth, PMC *arg_tuple)
arg_tuple
.static int mmd_is_hidden(Interp *, PMC *multi, PMC *cl)
cl
.static int mmd_maybe_candidate(Interp *, PMC *pmc, PMC *arg_tuple, PMC *cl)
pmc
is a Sub PMC, push it on the candidate list and return TRUE to stop further search.static int mmd_search_lexical(Interp *, STRING *meth, PMC *arg_tuple, PMC *cl)
static int mmd_search_package(Interp *, STRING *meth, PMC *arg_tuple, PMC *cl)
static int mmd_search_global(Interp *, STRING *meth, PMC *arg_tuple, PMC *cl)
static void mmd_search_builtin(Interp *, STRING *meth, PMC *arg_tuple, PMC *cl)
void Parrot_mmd_register_table(Interp*, INTVAL type, MMD_init *, INTVAL)
void Parrot_mmd_rebuild_table(Interp*, INTVAL type, INTVAL func_nr)
type
is negative all classes are rebuilt. If func_nr
is negative all MMD functions are rebuilt.include/parrot/mmd.h, http://svn.perl.org/perl6/doc/trunk/design/apo/A12.pod, http://svn.perl.org/perl6/doc/trunk/design/syn/S12.pod
|