Skip to content

dart build_runner ffi binding generator for any LLVM Clang JSON ast-dump

License

Notifications You must be signed in to change notification settings

Buggaboo/dart-ast-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dart_ast_json

Dart FFI binding generator for any LLVM Clang JSON AST dump

Dependencies

  • build_runner
  • code_gen
  • LLVM Clang >10.* to produce the AST-dump in JSON

Installing clang on MacOS

brew install llvm

Other great MacOS, iOS related guides

Create the dumps

# dump json
clang-10 -Isomewhere/include -c main.c -o build/target.o -Xclang -ast-dump=json -fsyntax-only > project_os_arch.json

# dump record memory layout
clang-10 -Isomewhere/include -c main.c -o build/target.o -Xclang -fdump-record-layouts > project_os_arch.layout

# check for padding optimization
clang-10 -Isomewhere/include -c main.c -o build/target.o -Wpadded

Also useful: -fsave-optimization-record

Current status WIP

  • Working on examples, tests and workarounds
  • Waiting for the dart team to provide a offset mechanism to solve alignment/padding issues.

Caveats

The generated JSON will be different per ARCH and OS

Grouping commonalities between generated ARCH and OSs is still a manual task.

Dart FFI Limitations

  1. Nested structs/unions/etc.
  2. Pass by value e.g. Struct/Union/Enum
  3. extern C required for C++ code due to name mangling
  4. Packed structs
  5. No bitfields, DIY bitmasking
  6. No union
  7. No support for non scalar struct fields, e.g. int meh[2];
  8. And more...

Workarounds

  1. Each struct depends on the largest member (e.g. another struct), to determine its alignment and padding, use -Wpadded in clang
  2. Just don't, especially for big padded structs, don't overflow the stack.
  3. Just do it.
  4. Clang has a -Wpadded that could provide clues on how to spatially optimize (instead of packing)
  5. Don't. Instead use a machine word sized integer, and provide masking where necessary; explicit is better.
  6. Use Struct from dart's FFI, but use a single memory location, e.g. a Pointer<Void> as the only field.
  7. The workaround is to repeatedly state the field @Int32 int meh_0, meh_1; // etc. to determine the proper alignment and padding. Yuck.

Run program

  1. Add a build.yaml with the necessary configuration, see the examples
  2. Compile your C/C++ code for your ARCH and OS, with clang, and enable -dump-ast=json
  3. Capture the JSON output in a file, <project>_<os>_arch.json, e.g. miniaudio_android_arm64.py. Place the file generated by adding -ast-dump=json file in your lib/src. Then run:
pub run build_runner build

Struct alignment & padding

Handy commands

Sort out types in the AST JSON:

grep desugaredQualType web/miniaudio.json | grep -v __attr | sed -e 's/^[ \t]*//' | sort | uniq | less

Links

About

dart build_runner ffi binding generator for any LLVM Clang JSON ast-dump

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages