C libraries support
Add support for C libraries
Submitted by Daco Harkes on 2 May 2013 at 12:25
- make facility to declare external functions
- possibly parse header files and wrap them with C modules
Issue Log
There is multiple ways to implement this
- fix the unresolved messages by adding Spoofax code which circumvents error messages on functions with c-function names. –> this is nasty, non expandable and does not provide type checking
- Put a c function definitions in a MetaC module, import that module with name analysis and type checking and ignore that module when generating c code. –> requires translating c to basec function definitions (which is not so neat). + provides support for other external functions
- Parse c files and add functions to index. –> This requires working with two languages in Spoofax, which is quite a challenge.
We decided to go for the second option.
Plan:
- Create a module containing printf definition (where to place this module?)
- Import that module automatically by desugaring
- Normalize the module automatically away when generating c, h and makefile
- For printf, add specific typechecking allowing multiple types.
The mbeddr way of importing manual is the following:
// external module contents are exported by default external module stdio_stub imports nothing resources header <stdio.h> { void printf(const char* format, ...); }
Done:
0. Syntax for external modules and function prototypes
1. stdio wrapper with printf function created
3. compilation, but does not do anything with the c header files inside the resources yet.
4. basic namebinding, but function calls do not resolve yet, same problem as #26.Skip:
2. no import automatically, mbeddr also doesn’t do that.Todo:
3. compilation, actually reference the correct c headers
4. fix #26
mbeddr does not actually generate anything from the external modules, it directly adds the referenced resources in the import statements of the the modules importing the external module.
Importing math.h and stdio.h in an external module results in this main module source code:
#ifndef BASE_H #define BASE_H #include <stdint.h> #include <stddef.h> #include <math.h, stdio.h> int32_t Base_main(int32_t argc,int8_t* argv[]); int8_t Base_testCase1(void); #endif #include "Base.h"
and
#include <math.h, stdio.h> #include <stdio.h> #include <stdlib.h> int32_t main(int32_t argc, int8_t* argv[]) { printf(43); return Base_blockexpr_a1a0(); }
Done:
3. referenced actual c headersNote: the files have still to be in the same folder, see #35.
Closing as functionality is implemented, and pending issues are in other issues.
Log in to post comments