AMD64 alien-indirect support; document alien-indirect

slava 2006-09-09 05:19:43 +00:00
parent aa94868cea
commit c72a919526
7 changed files with 25 additions and 4 deletions

View File

@ -30,6 +30,8 @@ ARTICLE: "alien-invoke" "Calling C from Factor"
$terpri $terpri
"The above parsing words create word definitions which call a lower-level word; you can use it directly, too:" "The above parsing words create word definitions which call a lower-level word; you can use it directly, too:"
{ $subsection alien-invoke } { $subsection alien-invoke }
"Sometimes it is necessary to invoke a C function pointer, rather than a named C function:"
{ $subsection alien-indirect }
"There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-types" } "." ; "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-types" } "." ;
ARTICLE: "alien-callback" "Calling Factor from C" ARTICLE: "alien-callback" "Calling Factor from C"

View File

@ -21,4 +21,4 @@ HELP: alien-callback
} }
} }
{ $errors "Throws an " { $link alien-callback-error } " if the word calling " { $link alien-callback } " is not compiled." } { $errors "Throws an " { $link alien-callback-error } " if the word calling " { $link alien-callback } " is not compiled." }
{ $see-also alien-invoke } ; { $see-also alien-invoke alien-indirect } ;

View File

@ -0,0 +1,13 @@
IN: alien
USING: errors help ;
HELP: alien-indirect-error
{ $error-description "Thrown when " { $link alien-indirect } " is called in the interpreter. Words using " { $link alien-indirect } " must be compiled first, and all three inputs to " { $link alien-indirect } " must be literals." } ;
HELP: alien-indirect
{ $values { "funcptr" "a C function pointer" } { "return" "a C return type" } { "parameters" "a sequence of C parameter types" } { "abi" "one of " { $snippet "\"cdecl\"" } " or " { $snippet "\"stdcall\"" } } }
{ $description
"Invokes a C function pointer passed on the data stack. Input parameters are taken from the data stack following the function pointer, and the return value is pushed on the data stack after the function returns. A return type of " { $snippet "\"void\"" } " indicates that no value is to be expected."
}
{ $errors "Throws an " { $link alien-indirect-error } " if the word calling " { $link alien-indirect } " is not compiled." }
{ $see-also alien-invoke alien-callback } ;

View File

@ -8,7 +8,7 @@ HELP: alien-invoke
{ $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } } { $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } }
{ $description "Calls a C library function with the given name. Input parameters are taken from the data stack, and the return value is pushed on the data stack after the function returns. A return type of " { $snippet "\"void\"" } " indicates that no value is to be expected." } { $description "Calls a C library function with the given name. Input parameters are taken from the data stack, and the return value is pushed on the data stack after the function returns. A return type of " { $snippet "\"void\"" } " indicates that no value is to be expected." }
{ $errors "Throws an " { $link alien-invoke-error } " if the word calling " { $link alien-invoke } " is not compiled." } { $errors "Throws an " { $link alien-invoke-error } " if the word calling " { $link alien-invoke } " is not compiled." }
{ $see-also alien-callback } ; { $see-also alien-indirect alien-callback } ;
HELP: define-c-word HELP: define-c-word
{ $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } } { $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } }

View File

@ -48,8 +48,12 @@ M: stack-params %freg>stack
: %alien-invoke ( symbol dll -- ) : %alien-invoke ( symbol dll -- )
reset-sse compile-c-call ; reset-sse compile-c-call ;
: alien-temp ( quot -- )
0 R11 MOV "alien_temp" f rel-absolute-cell rel-dlsym
R11 swap call ; inline
: %alien-indirect ( -- ) : %alien-indirect ( -- )
"unbox_alien" f %alien-invoke RAX CALL ; reset-sse [ CALL ] alien-temp ;
: %alien-callback ( quot -- ) : %alien-callback ( quot -- )
RDI load-indirect "run_callback" f compile-c-call ; RDI load-indirect "run_callback" f compile-c-call ;

View File

@ -33,6 +33,7 @@ PROVIDE: library/compiler {
"alien/syntax.factor" "alien/syntax.factor"
"alien/alien-callback.facts" "alien/alien-callback.facts"
"alien/alien-indirect.facts"
"alien/alien-invoke.facts" "alien/alien-invoke.facts"
"alien/aliens.facts" "alien/aliens.facts"
"alien/c-types.facts" "alien/c-types.facts"

View File

@ -30,10 +30,11 @@ M: cs-loc v>operand cs-loc-n cs-reg reg-stack ;
: alien-temp ( quot -- ) : alien-temp ( quot -- )
0 [] swap call "alien_temp" f rel-absolute rel-dlsym ; 0 [] swap call "alien_temp" f rel-absolute rel-dlsym ;
inline
: %prepare-alien-indirect ( -- ) : %prepare-alien-indirect ( -- )
"unbox_alien" f %alien-invoke "unbox_alien" f %alien-invoke
[ EAX MOV ] alien-temp ; [ T{ int-regs } return-reg MOV ] alien-temp ;
: %alien-indirect ( -- ) : %alien-indirect ( -- )
[ CALL ] alien-temp ; [ CALL ] alien-temp ;