Compile DLL

Started by sunmountain, November 14, 2011, 03:24:08 PM

Previous topic - Next topic

sunmountain

#15
Is there any public cvs/git/svn/fossil/bazzar/mercurial repo ?



Or, do you prefere patches or complete tar files ?

Lutz

#16
You can mail me a  .tgz of the files changed.



I just posted the 10.3.6 development release in:



http://www.newlisp.org/downloads/development/">http://www.newlisp.org/downloads/development/



you can take this as the starting point. It already has a #define FFI in newlisp.h and some other changes not previously in inprogress/ .

sunmountain

#17
Hi Lutz,

your work has laid the foundaition,  so I could relatively easlily code a first demo (until now static variable pushing), but now this one here:

(import "msvcrt" "printf" "cdecl")
(println (fcall printf a b c))
(exit 0)


gives this here:
$ newlisp fcall.lsp
Hello FFI - here's newLisp (and the answer is 42)
printf


So I'm looking forward on getting it going further :-)



I just extended FFIMPORT (for now)


#ifdef FFI
typedef struct
    {
    /* original import name of function */
    char * name;
    /* 0 for traditional import calls */
    int type;
    /* holds information for calling */
    ffi_cif cif;
    /* the ABI to use, mostly FFI_DEFAULT_ABI */
    ffi_abi abi;
    /* structure for use with fcall, newLisp -> C */
    struct fcall
        {
        /* number of arguments for foreign function */
        unsigned int nargs;
        /* returned type */
        ffi_type *rtype;
        /* array of argument types */
        ffi_type *atypes[2];
        /* attay of arguments */
        void *avalues[2];
        } fcall;
    /* structure for use with fclos (FFI callback), C -> newLisp */
    struct fclos
        {
        /* holds information for closure */
        ffi_closure *clos;
        /* the newList cell to be eval'd if called */
        CELL *expr;
        } fclos;
    } FFIMPORT;
#endif


and wrote some code (p_fcall):

ffi = (FFIMPORT *)pCell->aux;

/* setup FFIMPORT structure */

char *fmt = "Hello FFI - here's newLisp (and the answer is %i)n";
int i = 42;
void *aval[2];
aval[0] = &fmt;
aval[1] = &i;
ffi_type *argt[2];
argt[0] = &ffi_type_pointer;
argt[1] = &ffi_type_uint;
ffi_cif cif;

status = ffi_prep_cif(&cif,FFI_DEFAULT_ABI,2,&ffi_type_uint,argt);
if(status != FFI_OK)
    {
    return stuffString("ffi_prep_cif failed");
    }
else
    {
    ffi_call(&cif,FFI_FN(printf),&result,aval);
    }

return stuffString(ffi->name);


For the fcall stuff, the rest now is correct argument parsing and setup.

It should of course return an executable cell in the end, though, to keep the facility newLisp

has with import.



BTW: What's the C coding style named, you use ? Is this K&R ?

Lutz

#18
Not sure, what that style is called. In the beginning 90's I was reading some of Charles Petzold's books about Windows programming. He used to write the same style (not any more today). Many others back then would use this kind of style too. Today another style is in fashion, not indenting the braces but only the code. I like the other one more.



Use whatever you feel comfortable with.