Compile C to LLVM with Python.
首先安装必备的 python3 库:
pip install -r requirements.txt
生成 parser:
make parser
生成 preprocessor:
make preprocessor
运行库安装后,使用 python antlrbook 项目下的 bin/antlr4env.bat
脚本设置 dos 宏环境:
call <root_dir>/bin/antlr4env.bat
之后生成本项目的 antlr 框架:
antlr4py3 -visitor compiler/parser/C.g4 -o compiler/parser/
antlr4py3 -visitor preprocessor/parser/CPreprocessor.g4 -o preprocessor/parser/
编译到 LLVM IR
Usage: python3 main.py [-h] [-o output_name] [-t target] [-I include_dirs] [-D<macro<=value>>] filename
-h, --help: 语法帮助
-o, --output=: 输出的文件名
-t, --target=: LLVM IR 目标平台架构,默认为 x86_64-pc-linux-gnu
-I, --include=: 头文件搜寻目录,允许多个
-D<macro<=value>>: 定义宏 macro,值设为 value,允许定义多个宏
示例
python3 main.py -o snake.ll -Itest/libc/include -Itest/windows/include -t x86_64-pc-windows-gnu -D_WIN64 test/snake.c
LLVM IR 编译到二进制文件 (示例: clang)
clang snake.ll -o snake.exe