Add support for C libraries

  • make facility to declare external functions
  • possibly parse header files and wrap them with C modules
Submitted by Daco Harkes on 2 May 2013 at 12:25

On 23 May 2013 at 14:36 Daco Harkes commented:

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:

  1. Create a module containing printf definition (where to place this module?)
  2. Import that module automatically by desugaring
  3. Normalize the module automatically away when generating c, h and makefile
  4. For printf, add specific typechecking allowing multiple types.

On 23 May 2013 at 14:37 Daco Harkes tagged @daco

On 23 May 2013 at 15:11 Daco Harkes commented:

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, ...);
}

On 29 May 2013 at 22:40 Daco Harkes commented:

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


On 29 May 2013 at 22:49 Daco Harkes commented:

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();
}

On 30 May 2013 at 12:18 Daco Harkes commented:

Done:
3. referenced actual c headers

Note: the files have still to be in the same folder, see #35.

Closing as functionality is implemented, and pending issues are in other issues.


On 30 May 2013 at 12:18 Daco Harkes closed this issue.

Log in to post comments