# C code generation - We use the 'lus2c' compiler ``` lus2c -help ``` * note the usage: lus2c * do not focus on options for now, except -v - generate c code ``` lus2c samples.lus stopwatch -v ``` * take a look at the generated files stopwatch.c and stopwatch.h - note that 'stopwatch.c' can be (separately) compiled by a c compiler, e.g. gcc: gcc -c stopwatch.c - but no executed, since a main program is missing: ``` gcc stopwatch.c ``` * note that are missing: the 'main' and the output procedures # Step-by-step generic loop - (re) compile the lustre code with the -loop option ``` lus2c samples.lus stopwatch -v -loop ``` * it generates an extra c file 'stopwatch_loop.c' * it contains the missing procedures * it implements a generic interactive loop - compile and link the c codes to get a functional executable ``` gcc stopwatch.c stopwatch_loop.c -o stopwatch ``` - and run it: * it starts a step-by-step interactive excution * Crtl-D to terminate ``` ./stopwatch ``` # C code execution using luciole - step-by-step console execution is far from convenient - to test the C code, it is possible to use luciole, via the script lus2dro: ``` lus2dro samples.lus stopwatch ``` * it generates the c code as usual, plus some ad-hoc glue code * compile it with gcc into a dynamic link library (normal extension is .so) * this library (.dro) is in a format that can be loaded and executed by luciole ``` luciole ./stopwatch.dro ``` * WARNING: the './' is necessary, since '.' is normally NOT in system DLL path # lus2c advanced options - take again a look at the lus2c options ``` lus2c -help ``` - some options will be be useful for this course: * -exttypes: allows to define the actual type (and) size of basic data * -ctx-static: strongly simplifies the interface of the C code (not general, sufficient for us) ``` lus2c samples.lus stopwatch -o general_api lus2c samples.lus stopwatch -ctx-static -exttypes -o simplified_api ``` * compare the generated codes * note that the simplified code: - does not use the heap (no 'new/malloc') - limits the use of the stack (functions have no or few arguments) - very interesting for low-resource hardware