2012/5/7 David Banks <
amo...@gmail.com>:
> Hey, I had the need to do this again today and I was wondering if
> there has been any progress on this?
> I noticed that some of the FFI stuff for nmosh has been tweaked a bit
> over the past year, is it now possible to wrap a varargs function from
> the FFI?
If we don't have any floating point arguments to the FFI function, we
can call vararg functions even on amd64 like this:
(import (rnrs) (mosh ffi)
(nmosh pffi interface)
(srfi :42))
(define lib (open-shared-library "libc.so"))
(define printf7 (c-function lib int printf
;; ARGS (Up to 7, for example)
void* void* void*
void* void* void* void*))
(define (arg->pointer obj)
(cond
((bytevector? obj)
;; bytevector-pointer is from (nmosh pffi interface)
(bytevector-pointer obj))
((string? obj)
(arg->pointer (string->utf8 obj)))
((pointer? obj) obj)
((fixnum? obj) (integer->pointer obj))
(else (assertion-violation 'arg->int-pointer "invalid argument" obj))))
(define (printf . args)
(define arglen (length args))
(define call-args (list-ec (: i 7) ;; Arg count for printf7
(if (< i arglen)
(arg->pointer (list-ref args i))
(integer->pointer 0) ;; Dummy arg
)))
(write call-args)(newline)
(apply printf7 call-args))
(printf "hoge\n")
(printf "%s, %s\n" "Hello" "World")
(printf "%s %d %s\n" "Hello" 123 "World")
Points are:
- We cannot pass any double or float arguments for vararg functions on amd64
- use void* as arguments for variable portion of the function
- nmosh has "bytevector-pointer" (Create a pointer object which
references a bytevector content) in (nmosh pffi interface) library
- Fill dummy arguments with some pointers
Currently, we don't care about floating point arguments for vararg functions.
Amd64 ABI uses SSE registers to pass floating point arguments and we
don't pass its size to the function.
Hope this helps,
-- oku
> [quote below for context]
>
> On 7 March 2011 13:04, okuoku <
oku...@gmail.com> wrote:
>> 2011/3/7 David Banks <
amo...@gmail.com>:
>>> Hi,
>>>
>>> Is it possible to call a varargs function using the FFI?
> ...
>> Anyway, implementing vararg call is somewhat hard work. I'm now
>> considering 2 way to implement it:
>>
>> 1) using LLVM.
>> LLVM knows about how to call vararg functions. So we can use it to
>> generate calling stub.
>>
>> 2) Implementing some "tiny-compiler."
>> Now I'm implementing amd64 run-time assembler like industria library already has
>> (
https://code.launchpad.net/~weinholt/scheme-libraries/industria ).
>> We may use it to generate calling stub.
>>
>> But both are not coming anytime soon..
>
>
> --
> David Banks <
amo...@gmail.com>
>
> --
> You received this message because you are subscribed to the Google Groups "Mosh Developer Disscus" group.
> To post to this group, send email to
mosh-develo...@googlegroups.com.
> To unsubscribe from this group, send email to
mosh-developer-di...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/mosh-developer-discuss?hl=en.
>