 M: ppc %alien-invoke ( symbol dll -- )
     [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
 
-M: ppc %prepare-alien-indirect ( -- )
-    3 ds-reg 0 LWZ
-    ds-reg ds-reg 4 SUBI
-    4 %load-vm-addr
-    "pinned_alien_offset" f %alien-invoke
-    16 3 MR ;
-
-M: ppc %alien-indirect ( -- )
-    16 MTLR BLRL ;
+M: ppc %alien-indirect ( src -- )
+    [ 11 ] dip load-param 11 MTLR BLRL ;
 
 M: ppc immediate-arithmetic? ( n -- ? ) -32768 32767 between? ;
 

    3 1 f struct-return@ ADDI
    3 1 0 local@ STW ;

M: ppc %box-large-struct ( n c-type -- )
    ! If n = f, then we're boxing a returned struct
    ! Compute destination address and load struct size
    [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
    5 %load-vm-addr
    ! Call the function
    "from_value_struct" f %alien-invoke ;

M:: ppc %restore-context ( temp1 temp2 -- )
    temp1 %context

M: ppc return-struct-in-registers? ( c-type -- ? )
    c-type return-in-registers?>> ;

M:: ppc %box-small-struct ( dst c-type -- )
    #! Box a <= 16-byte struct returned in r3:r4:r5:r6
    c-type heap-size 7 LI
    8 %load-vm-addr
    "from_medium_struct" f %alien-invoke
    3 dst store-param ;

: %unbox-struct-1 ( -- )
    ! Alien must be in r3.
    3 3 0 LWZ ;

: %unbox-struct-2 ( -- )
    ! Alien must be in r3.
    4 3 4 LWZ
    3 3 0 LWZ ;

: %unbox-struct-4 ( -- )
    ! Alien must be in r3.
    6 3 12 LWZ
    5 3 8 LWZ
    4 3 4 LWZ
    3 3 0 LWZ ;

M:: ppc %unbox-small-struct ( src c-type -- )
    src 3 load-param
    c-type heap-size {
        { [ dup 4 <= ] [ drop %unbox-struct-1 ] }
        { [ dup 8 <= ] [ drop %unbox-struct-2 ] }
        { [ dup 16 <= ] [ drop %unbox-struct-4 ] }
    } cond ;

M: ppc %begin-callback ( -- )
    3 %load-vm-addr
    "begin_callback" f %alien-invoke ;

M: ppc %alien-callback ( quot -- )
    3 swap %load-reference
    4 3 quot-entry-point-offset LWZ
    4 MTLR
    BLRL ;

M: ppc %end-callback ( -- )
    3 %load-vm-addr
    "end_callback" f %alien-invoke ;

enable-float-functions

