Git Product home page Git Product logo

dart-ast-json's Introduction

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

dart-ast-json's People

Contributors

buggaboo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.