parrotcode: Multimethod dispatch for binary opcode functions | |
Contents | Documentation |
docs/mmd.pod - Multimethod dispatch for binary opcode functions
XXX - Part or all of this document is outdated. Especially the "the MMD system doesn't handle inheritance" bit. Please refer to PDD03 at this moment while we rewrite or merge this document. We apologize for the inconvenience.
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 behavior.
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.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.left
,
right
,
and dest
are all PMC pointers,
while func_num
is the MMD table that should be used to do the dispatching.left
and right
and call it,
passing in left
,
right
,
and dest
like any other binary vtable function.left
and right
are PMC pointers,
while func_num
is the MMD table that should be used to do the dispatching.
The function is responsible for creating the returned string.mmd_dispatch_string
,
only it returns an INTVAL.mmd_dispatch_string
,
only it returns a FLOATVAL.func_num
is the number of the new function,
while default_func
is the function to be called when the system doesn't know which function it should call.
(Because,
for example,
there hasn't been a function installed that matches the left and right types for a call)The following constants are defined to identify function tables:
By default,
functions are installed for all the functions that have constants associated with them.
They are all functions suitable for calling with mmd_dispatch_pmc
.
The math functions (add, subtract, multiply, and divide) all work on the float values of the left and right sides.
The cmod function does a plan C-style mod (the C %
operator) on the integer value of the left and right sides.
The mod function does an fmod on the float values of the two sides.
The bitwise functions (and, or, xor, left shift, right shift) work on the integer values of the two sides.
The concat function concatenates the string values of the left and right sides.
The logical functions (and,
or,
xor) use the boolean values of the left and right sides to see whether they should set_pmc the destination to the left or right sides.
The and
and or
functions short-circuit,
xor
does not.
The repeat function gets the string value of the left side and the integer value of the right.
The numeric equal and numeric comparison functions work on the float values of both sides.
The string equal and comparison functions work on the string values of both sides.
The string bitwise ops (and, or, xor) take the string values of both sides and do bitwise operations on the resulting bitstring.
|