Application built from scratch that compiles a C file to an EXE of the same code.
Ctron translates a simplified C-like language into a Windows executable (.exe).
It uses Flex & Bison to generate LLVM IR, compiles it to assembly with llc.exe, and finally produces an executable using gcc.
Ctron currently supports a restricted subset of C. Follow these rules when writing code:
"\d"is interpreted as"\n".- The keyword
globalmust appear before declaring any global variables. - Use
//to start a single-line comment. - Only the
inttype is fully functional (andvoidfor functions).
void functions must end with:
return;Fully functional:
==,||,&&,<,>,<=,>=
Not functional yet:
!,!=+=,-=,++,--,*=,/=
Use the expanded form instead, for example:
x = x + 1; // instead of x++
x = x - 1; // instead of x--
x = x * 2; // instead of x *= 2
x = x / 2; // instead of x /= 2The following files must be located in the same folder as Ctron.exe (or Main.cpp during development):
Flex_BisonV6.exellc.exe
Additionally, the computer must have gcc installed (MinGW recommended).
- If
gccis missing, Ctron should produce an initial error indicating that the environment is not set up correctly.
Open cmd in the folder that contains Ctron.exe (or the built binary).
Type:
Ctron -helpor:
Ctron -hExample output:
Usage:
Ctron [options]
Ctron [source_file] [command]
Options:
-h Display this information.
Commands:
-o <file> Place the output into <file>.
-o Place the output into [source_file.exe].
To compile test.c and output an EXE with the same base name (test.exe):
Ctron test.c -oTo compile test.c and output an EXE with a custom file name:
Ctron test.c -o custom.exeThese steps show the internal pipeline when you want to run Flex/Bison and LLVM tools directly.
-
Generate
out.ll(LLVM IR) using Flex + Bison:Flex_BisonV6.exe test.c
This generates:
out.ll -
Convert
out.llto x86 assembly usingllc.exe:llc.exe -march=x86 --x86-asm-syntax=intel out.ll
This generates:
out.s -
Compile the assembly file to an executable using
gcc:gcc out.s -o test.exe
-
Run the resulting executable:
test.exe