c2fj is an compiler from C code to FlipJump code (by compiling it to RiscV first). Any program in C can become just a bunch of flips in memory. This project is a compiler, and is a python package too.
c2fj
c2fj is a powerful tool designed to compile C code into the innovative FlipJump programming language through an intermediate RISC-V representation. It takes a C source file and transforms it into a series of format conversions, eventually outputting a .fjm file ready to be executed.
Features
- Multi-stage Compilation: This tool efficiently compiles C into RISC-V ELF, then converts that into FlipJump (.fj) files, culminating in a .fjm execution file.
- Example Usage: A sample program that calculates prime numbers demonstrates the capabilities of c2fj:
int main() {
printf("Calculate primes up to: ");
int max_number;
scanf("%d", &max_number);
for (int p = 3; p <= max_number; p += 2) {
if (non_prime[p] == false) {
for (int i = p*p; i <= max_number; i += p) {
non_prime[i] = true;
}
printf("%d\n", p);
}
}
return 0;
}
After compilation, you can see traces of execution like:
Calculate primes up to: 20
2
3
5
7
11
13
17
19
Program exited with exit code 0x0.
Functionality
To use c2fj, simply run:
python3 c2fj.py file.c
This will compile the specified C file into ELF, then into FlipJump files, and finally produce the executable .fjm file.
Command-Line Options
c2fj offers various flags for customization:
--breakpoints
: Set breakpoints at specified RISC-V addresses.--single-step
: Enable single-step execution mode.--unify_fj
: Consolidate multiple generated .fj files into one.--finish-after
: Halt compilation at a specific step.--build-dir
: Define a directory for storing builds.
Support for Makefile
Projects involving multiple C files can leverage c2fj by specifying a Makefile instead of a single C file. The Makefile should define specific constants that c2fj will fill during the build process, enabling efficient management of source files and dependencies.
Compilation Mechanics
- C to RISC-V: Using picolibc, C code is compiled into RISC-V ELF format. Custom implementations are provided to support system calls.
- RISC-V to FlipJump: The compiled ELF is analyzed to map each processor opcode to its FlipJump equivalent, optimizing space and ensuring scalability with large codebases.
- Execution: The final FlipJump .fjm file is executed using the FlipJump language interpreter, integrating debugging capabilities for an efficient development workflow.
Memory and Jump Tables
During compilation, c2fj generates additional files to manage memory and facilitate control flow through jump tables, supporting a robust execution environment.
Testing
c2fj is thoroughly tested on Linux with Python 3.13. To execute tests, simply use:
pytest
Related Projects
- bf2fj: A compiler converting Brainfuck code to FlipJump.
- FlipJump: The essential FlipJump macro assembler, standard library, and interpreter.
- fji-cpp: A C++ interpreter optimized for FlipJump.
Explore c2fj for seamless C programming to FlipJump compilation and leverage the benefits of reverse engineering with this modern tool.
No comments yet.
Sign in to be the first to comment.