Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Foreign Function Interface (FFI) #8

Closed
wants to merge 5 commits into from

Conversation

alastairreid
Copy link
Owner

This new interface makes the types of imported/exported
functions independent of the particular representation
choices made by the runtime system.

Instead, values of arguments and results are converted
between the type used for ASL code and a standard
representation used for C code.

The representation of types is as follows

- if the ASL type is boolean then "bool" (from stdbool.h)
- if the ASL type is string then "const char*"
- if the ASL type is integer then "int"
- if the ASL type is bits(N) then either
  - uint8_t, uint16_t, uint32_t, uint64_t if N IN {8, 16, 32, 64}
  - array uint64_t _[N'] where N' is ceiling(N / 64)
- if the ASL type is an enumeration, then that enumeration
  provided that the enumeration type has been explicitly
  exported
- if a function returns a tuple of N values, then the C function
  expects N extra arguments which should be pointers for
  the C function to write results to.

Other types cannot be used in imported/exported functions.

In addition, exported functions must not raise exceptions
and, for extra safety, we insert a runtime check that
they do not raise an exception

In addition, for each configuration variable 'v : ty', we generate
a pair of getter/setter functions

    ASL_set_config_{v}({cty} value);
    {cty} ASL_get_config_{v}();

Note that (based on the ASL semantics of configuration variables)
all changes to the configuration variable should be made
before executing any ASL code.

All the interesting stuff is in libASL/backend_c_new.ml - the rest is just changes to the runtime API to support the new FFI or to support testing.

Signed-off-by: Alastair Reid <[email protected]>
This provides a way to test whether a type was generated
by the :xform_tuples transformation - which is needed
to support tuple-return in the foreign function interface

Signed-off-by: Alastair Reid <[email protected]>
This interface makes the types of imported/exported
functions independent of the particular representation
choices made by the runtime system.

Instead, values of arguments and results are converted
between the type used for ASL code and a standard
representation used for C code.

The representation of types is as follows

- if the ASL type is boolean then "bool" (from stdbool.h)
- if the ASL type is string then "const char*"
- if the ASL type is integer then "int"
- if the ASL type is bits(N) then either
  - uint8_t, uint16_t, uint32_t, uint64_t if N IN {8, 16, 32, 64}
  - array uint64_t _[N'] where N' is ceiling(N / 64)
- if the ASL type is an enumeration, then that enumeration
  provided that the enumeration type has been explicitly
  exported

Other types cannot be used in imported/exported functions.

In addition, exported functions must not raise exceptions
and, for extra safety, we insert a runtime check that
they do not raise an exception

In addition, for each configuration variable 'v : ty', we generate
a pair of getter/setter functions

    ASL_set_config_{v}({cty} value);
    {cty} ASL_get_config_{v}();

Note that (based on the ASL semantics of configuration variables)
all changes to the configuration variable should be made
before executing any ASL code.

Signed-off-by: Alastair Reid <[email protected]>
- Add --new-ffi flag

  This causes the new FFI to be used instead of the old FFI
  where the type of imported/exported functions was determined
  by the data representations of a particular runtime.

- Add --configuration=<json file>

  Specify a configuration control file typically containing
  import and export lists to control the FFI

- Add --import=<C function name>
  and --export=<C function name>

  These specify a C function to be imported/exported.
  The ASL function name needs to be the same as the C function name.

  This is a shortcut to creating a configuration file
  that is mostly intended to make it easier to write tests.

- Add --extra-c=<C file> flag

  This specifies a C file to be compiled and linked against
  the ASL code when using --build or --run.

  Mostly intended to make it easier to write tests.

Signed-off-by: Alastair Reid <[email protected]>
Signed-off-by: Alastair Reid <[email protected]>
@alastairreid alastairreid deleted the areid-new-ffi2 branch February 18, 2025 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant