You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. # c2logic
  2. Compiles C code to Mindustry logic. Still in beta, so compiled output may not be fully optimized. Requirements are the `pycparser` package and the C preprocessor (`cpp`).
  3. # Installation
  4. `pip install git+https://github.com/SuperStormer/c2logic`
  5. # Documentation
  6. Run the command line tool using:
  7. `c2logic filename -O optimization_level`
  8. where `filename` is a string and `optimization_level` is an optional integer.
  9. Optimization Level:
  10. 0. completely unoptimized.
  11. 1. the default
  12. - modify variables without using a temporary
  13. 2. more optimizations
  14. - remove uncalled functions
  15. 3. turns on some potentially unsafe optimizations
  16. - augmented assignment and pre/postincrement/decrement don't modify `__rax`
  17. - returning from main becomes equivalent to `end`
  18. Locals are rewritten as `_<varname>_<func_name>`. Globals are unchanged.
  19. Special Variables:
  20. - `__rax`: similar to x86 rax
  21. - `__rbx`: stores left hand side of binary ops to avoid clobbering by the right side
  22. - `__retaddr__<func_name>`: stores return address of func call
  23. When writing your code, you must include `c2logic/builtins.h`, which is located in the python include directory (location depends on system, mine is at `~/.local/include/python3.8/`).
  24. A quick way to find this is `python3 -c "from c2logic.compiler import get_include_path; print(get_include_path())"` (use `python` if you are using windows).
  25. See [include/builtins.h](./include/builtins.h) for API definitions and [examples](./examples) for API sample usage.
  26. # Supported Features
  27. - all Mindustry instructions as of BE 9420
  28. - all C control flow structures except switch
  29. - functions
  30. - local/global variables
  31. # Unsupported Features
  32. - defining global variables outside of functions - define it in main
  33. - recursive calls - use iteration
  34. - structs - split it into multiple variables
  35. - enums - use an int plus macros
  36. - block scoped variables - just use locals
  37. - typedefs - use macros
  38. - pointers - don't use them
  39. - switch - use if-else chains