Embed C or ASM in newLISP

Started by Lutz, January 01, 2012, 07:07:11 PM

Previous topic - Next topic

Lutz

Interesting project:



http://sourceforge.net/projects/defineext/">http://sourceforge.net/projects/defineext/

unixtechie

#1
ah, 'tcc', a brilliant piece of software (by the author of QEMU emulator), which allows on-the-fly compilation and execution.



There exists a whole web server based on tcc, which is number one in speed comparisons (pushing several hundred thousand responses per second), called G-WAN.

The authors distribute it as a compiled bundle, do not allow to look inside nor change anything, but at the same time give it away for free and prohibit charging money for it.



That server does static pages of course, but relies mostly on tcc on-the-fly compilation for the 'application server' part of it. This whole speed champion server is scripted in C, which scriplet handlers or filters are included as a source code and rely in the built-in tcc to run.



Actually, taking one of the sample C scriplets and slightly modifying it one could turn Newlisp into a persistent server language for G-WAN.

They could communicate over a pair of pipes, as one example, with web headers and (RESTful) requests sent over in the simplest format possible.



One could go the other way, i.e. compile and include newlisp as a shared library into the G-WAN file hierarchy, so that C scriplets would be able to use embedded newlisp from C - but somehow that seems to me a more cumbersome approach.



http://gwan.ch/">http://gwan.ch/

.

rowanthorpe

#2
...although TCC is awesome and deserves all the credit and attention it gets, and more (as do Fabrice Ballard's other creations like FFmpeg, etc) one of the most important - but not immediately apparent - features of the "define-ext" module is that it has been written in a modular fashion to allow easy extension for arbitrary compilable languages (not just C and ASM). The "plugins" for ASM and C were just provided bundled-in (using imported functions from tinycc) as an example, and should provide easy templates for adapting to embed any language for which there is a dynamic lib compiler, or even to implement ASM or C using other libraries if you prefer... Also, it simplifies embedding (pre-compiled?) object code too, making it as easy as writing a (define...).

VadimTukaev

#3
It seems to be "killer-feature" to me. I have some C-code and I want to link it by high-level language.



Unfortunately, I can't understand, how to use it to embed obj files. I read USAGE.txt already! Example:


#define MAX_SIZ 100

static double per[MAX_SIZ];

double* c_percents(double* arr, int siz)
{
    if( siz > MAX_SIZ) siz = MAX_SIZ;
    double sum = 0.0;
    int i;
    for( i = 0; i < siz; ++i ) {
        sum += arr[i];
    }
    sum = 100.0 / sum;
    for( i = 0; i < siz; ++i ) {
        per[i] = arr[i] * sum;
    }
    return per;
}


I compiled it to object file c_percents.o. So, how to write newLisp code for utilizing that function? It must convert any of the lists (1 2 3 4), (300 600 900 1200) or (0.15 0.3 0.45 0.6) to (10 20 30 40). Plizzz! :)



P.S. Sorry for my ugly english, I'm russian.

rrq

#4
You need to make it into "shared libraries", then run newlisp with LD_LIBRARY_PATH set up, then declare the imported functions with (import ...), then Bob's your uncle.

It's amazingly simple (until you come to the data sharing)

Maybe this is a help

http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html">//http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

VadimTukaev

#5
Apparently you have not read the previous posts. It was about the framework "define-ext" - https://github.com/rowanthorpe/define-ext/blob/master/USAGE.txt">https://github.com/rowanthorpe/define-e ... /USAGE.txt">https://github.com/rowanthorpe/define-ext/blob/master/USAGE.txt

rrq

#6
Right. My apologies.

abaddon1234

#7
how to write newLisp code for utilizing that function?

https://www.gclub88.com/register/">สมัครบาคาร่าออนไลน์