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 void dump_mmd(PARROT_INTERP, INTVAL function)
PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL funcptr_t get_mmd_dispatch_type(PARROT_INTERP, INTVAL func_nr, INTVAL left_type, INTVAL right_type, NOTNULL(int *is_pmc))
PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL static funcptr_t get_mmd_dispatcher(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(PMC *right), INTVAL function, NOTNULL(int *is_pmc))
PARROT_CANNOT_RETURN_NULL static PMC *mmd_deref(PARROT_INTERP, NOTNULL(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(PARROT_INTERP, INTVAL function, NULLOK(PMC *pmc))
pmc
is writable enough for function
.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *mmd_dispatch_p_ppp(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(PMC *right), NULLOK(PMC *dest), INTVAL func_nr)
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.PARROT_API PARROT_CAN_RETURN_NULL PMC *mmd_dispatch_p_pip(PARROT_INTERP, NOTNULL(PMC *left), INTVAL right, NULLOK(PMC *dest), INTVAL func_nr)
mmd_dispatch_p_ppp
,
right argument is a native INTVAL.PARROT_API PARROT_CAN_RETURN_NULL PMC *mmd_dispatch_p_pnp(PARROT_INTERP, NOTNULL(PMC *left), FLOATVAL right, NULLOK(PMC *dest), INTVAL func_nr)
mmd_dispatch_p_ppp
,
right argument is a native FLOATVAL.PARROT_API PARROT_CAN_RETURN_NULL PMC *mmd_dispatch_p_psp(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(STRING *right), NULLOK(PMC *dest), INTVAL func_nr)
mmd_dispatch_p_ppp
,
right argument is a native STRING *.PARROT_API void mmd_dispatch_v_pp(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(PMC *right), INTVAL func_nr)
left <op=> right
.PARROT_API void mmd_dispatch_v_pi(PARROT_INTERP, NOTNULL(PMC *left), INTVAL right, INTVAL func_nr)
left <op=> right
.PARROT_API void mmd_dispatch_v_pn(PARROT_INTERP, NOTNULL(PMC *left), FLOATVAL right, INTVAL func_nr)
left <op=> right
.PARROT_API void mmd_dispatch_v_ps(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(STRING *right), INTVAL func_nr)
left <op=> right
.PARROT_API INTVAL mmd_dispatch_i_pp(PARROT_INTERP, NOTNULL(PMC *left), NOTNULL(PMC *right), INTVAL func_nr)
mmd_dispatch_p_ppp()
,
only it returns an INTVAL
.
This is used by MMD compare functions.PARROT_API void mmd_add_function(PARROT_INTERP, INTVAL func_nr, SHIM(funcptr_t function))
func_num
is the number of the new function.
function
is ignored.static void mmd_expand_x(PARROT_INTERP, INTVAL func_nr, INTVAL new_x)
new_x
.static void mmd_expand_y(PARROT_INTERP, INTVAL func_nr, INTVAL new_y)
PARROT_API void mmd_add_by_class(PARROT_INTERP, INTVAL functype, NOTNULL(STRING *left_class), NOTNULL(STRING *right_class), NULLOK(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.PARROT_API void mmd_register(PARROT_INTERP, INTVAL func_nr, INTVAL left_type, INTVAL right_type, NULLOK(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.PARROT_API void mmd_register_sub(PARROT_INTERP, INTVAL func_nr, INTVAL left_type, INTVAL right_type, NOTNULL(PMC *sub))
PARROT_API void mmd_destroy(PARROT_INTERP)
PARROT_API PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT PMC *mmd_vtfind(PARROT_INTERP, INTVAL func_nr, INTVAL left, INTVAL right)
PARROT_API PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT PMC *Parrot_MMD_search_default_infix(PARROT_INTERP, NOTNULL(STRING *meth), INTVAL left_type, INTVAL right_type)
PARROT_API PARROT_CAN_RETURN_NULL PARROT_WARN_UNUSED_RESULT PMC *Parrot_mmd_sort_candidate_list(PARROT_INTERP, NOTNULL(PMC *candidates))
PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_arg_tuple_inline(PARROT_INTERP, NOTNULL(STRING *signature), va_list args)
PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL static PMC *mmd_arg_tuple_func(PARROT_INTERP)
PARROT_CAN_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_search_default(PARROT_INTERP, NOTNULL(STRING *meth), NOTNULL(PMC *arg_tuple))
static void mmd_search_classes(PARROT_INTERP, NOTNULL(STRING *meth), NOTNULL(PMC *arg_tuple), NOTNULL(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 INTVAL distance_cmp(SHIM_INTERP, INTVAL a, INTVAL b)
PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_cvt_to_types(PARROT_INTERP, NOTNULL(PMC *multi_sig))
static UINTVAL mmd_distance(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(PMC *arg_tuple))
pmc
against given argument types.
0xffff is the maximum distancestatic void mmd_sort_candidates(PARROT_INTERP, NOTNULL(PMC *arg_tuple), NOTNULL(PMC *cl))
cl
by Manhattan DistancePARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_search_scopes(PARROT_INTERP, NOTNULL(STRING *meth))
arg_tuple
.PARROT_WARN_UNUSED_RESULT static int mmd_is_hidden(PARROT_INTERP, NOTNULL(PMC *multi), NOTNULL(PMC *cl))
cl
.static int mmd_maybe_candidate(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(PMC *cl))
pmc
is a Sub PMC,
push it on the candidate list and return TRUE to stop further search.static int mmd_search_cur_namespace(PARROT_INTERP, NOTNULL(STRING *meth), NOTNULL(PMC *cl))
PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_get_ns(PARROT_INTERP)
PARROT_CANNOT_RETURN_NULL PARROT_WARN_UNUSED_RESULT static PMC *mmd_make_ns(PARROT_INTERP)
static void mmd_search_builtin(PARROT_INTERP, NOTNULL(STRING *meth), NOTNULL(PMC *cl))
void mmd_create_builtin_multi_stub(PARROT_INTERP, NOTNULL(PMC *ns), INTVAL func_nr)
static void mmd_create_builtin_multi_meth_2(PARROT_INTERP, NOTNULL(PMC *ns), INTVAL func_nr, INTVAL type, INTVAL right, funcptr_t func_ptr)
static void mmd_create_builtin_multi_meth(PARROT_INTERP, NOTNULL(PMC *ns), INTVAL type, NOTNULL(const MMD_init *entry))
PARROT_API void Parrot_mmd_register_table(PARROT_INTERP, INTVAL type, NOTNULL(const MMD_init *mmd_table), INTVAL n)
PARROT_API void Parrot_mmd_rebuild_table(PARROT_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
|