1 # Copyright (C) 2001-2004, The Perl Foundation.
6 docs/mmd.pod - Multimethod dispatch for binary opcode functions
10 XXX - Part or all of this document is outdated. Especially the "the MMD system
11 doesn't handle inheritance" bit. Please refer to PDD03 at this moment while we
12 rewrite or merge this document. We apologize for the inconvenience.
16 This system is set up to handle type-based dispatching for binary (i.e.
17 two-arg) functions. This includes, though isn't necessarily limited to, binary
18 operators such as addition or subtraction.
22 The MMD system is straightforward, and currently must be explicitly invoked,
23 for example by a vtable function. (We may reserve the right to use MMD in all
24 circumstances, but currently do not)
28 For the purposes of the API, each MMD-able function is assigned a unique number
29 which is used to find the correct function table. This is the C<func_num>
30 parameter in the following functions. While Parrot isn't restricted to a
31 predefined set of functions, it I<does> set things up so that all the binary
32 vtable functions have a MMD table preinstalled for them, with default behavior.
36 =item mmd_add_by_class(interp, func_num, left_class, right_class, funcptr)
38 Adds a new MMD function C<funcptr> to the C<func_num> function table that will
39 be invoked when the left parameter is of class C<left_class> and the right
40 parameter is of class C<right_class>. Both classes are C<STRING *>s that hold
41 the PMC class names for the left and right sides. If either class isn't yet
42 loaded, Parrot will cache the information such that the function will be
43 installed if at some point in the future both classes are available.
45 Currently this is done by just assigning class numbers to the classes, which
46 the classes will pick up and use if they're later loaded, but we may later put
47 the functions into a deferred table that we scan when PMC classes are loaded.
48 Either way, the function will be guaranteed to be installed when it's needed.
50 The function table must exist, but if it is too small, it will automatically be
53 =item mmd_register(interp, func_num, left_type, right_type, funcptr)
55 Register a function C<funcptr> for MMD function table C<func_num> for classes
56 C<left_type> and C<right_type>. The left and right types are C<INTVAL>s that
57 represent the class ID numbers.
59 The function table must exist, but if it is too small, it will automatically be
62 Currently the MMD system doesn't handle inheritance and best match searching,
63 as it assumes that all PMC types have no parent type. This can be considered a
64 bug, and will be resolved at some point in the future.
66 =item mmd_dispatch_pmc(interp, left, right, dest, func_num)
68 Dispatch to a multimethod that "returns" a PMC. C<left>, C<right>, and C<dest>
69 are all PMC pointers, while C<func_num> is the MMD table that should be used to
72 The MMD system will figure out which function should be called based on the
73 types of C<left> and C<right> and call it, passing in C<left>, C<right>, and
74 C<dest> like any other binary vtable function.
76 This function has a void return type, like all the "take two PMCs, return a
77 PMC" vtable functions do.
79 =item STRING *mmd_dispatch_string(interp, left, right, func_num)
81 Dispatch to a multimethod that returns a string. C<left> and C<right> are PMC
82 pointers, while C<func_num> is the MMD table that should be used to do the
83 dispatching. The function is responsible for creating the returned string.
85 =item INTVAL mmd_dispatch_intval(interp, left, right, func_num)
87 Like C<mmd_dispatch_string>, only it returns an INTVAL.
89 =item FLOATVAL mmd_dispatch_floatval(interp, left, right, func_num)
91 Like C<mmd_dispatch_string>, only it returns a FLOATVAL.
93 =item mmd_add_function(interp, func_num, default_func)
95 Add a new function table to the list of functions the MMD system knows of.
96 C<func_num> is the number of the new function, while C<default_func> is the
97 function to be called when the system doesn't know which function it should
98 call. (Because, for example, there hasn't been a function installed that
99 matches the left and right types for a call)
105 The following constants are defined to identify function tables:
159 Short-circuiting logical and
163 Short-circuiting logical or
167 Logical xor (not short-circuiting)
191 Bitwise or of the string value
195 Bitwise and of the string value
199 Bitwise xor of the string value
205 By default, functions are installed for all the functions that have constants
206 associated with them. They are all functions suitable for calling with
209 The math functions (add, subtract, multiply, and divide) all work on the float
210 values of the left and right sides.
212 The cmod function does a plan C-style mod (the C C<%> operator) on the integer
213 value of the left and right sides.
215 The mod function does an fmod on the float values of the two sides.
217 The bitwise functions (and, or, xor, left shift, right shift) work on the
218 integer values of the two sides.
220 The concat function concatenates the string values of the left and right sides.
222 The logical functions (and, or, xor) use the boolean values of the left and
223 right sides to see whether they should set_pmc the destination to the left or
224 right sides. The C<and> and C<or> functions short-circuit, C<xor> does not.
226 The repeat function gets the string value of the left side and the integer
229 The numeric equal and numeric comparison functions work on the float values of
232 The string equal and comparison functions work on the string values of both
235 The string bitwise ops (and, or, xor) take the string values of both sides and
236 do bitwise operations on the resulting bitstring.