Chibil:一个针对 .NET IL 的 C 语言编译器
Chibil: A C compiler targeting .NET IL

原始链接: https://github.com/MichalStrehovsky/chibil

Chibil 是一款用 C# 编写、以 .NET 中间语言 (IL) 为目标的 C 语言编译器。它基于 *chibicc* 开发,具备足够的鲁棒性,能够编译并运行 *PureDOOM*。该编译器生成的 COFF 目标文件与 MSVC 的 `/clr` 模式二进制兼容,因此可以使用标准 Visual Studio 工具 (`link.exe`) 进行链接,甚至可以集成到 C++/CLI 项目中。 其主要功能包括对标准 C 语言调试的支持,允许用户在 .NET 调试器中对 C 代码进行单步调试,以及一个用于处理入口点的精简 C 运行时库。虽然 Chibil 目前依赖外部链接器,但未来计划开发原生链接器以移除对 Windows 的依赖。 与 .NET 的互操作性目前处于早期阶段;编译后的代码位于全局命名空间中,需要通过反射才能实现外部访问。该项目还包含 `asm2obj` 和 COFF 对象转储器等自定义工具,相比 `dumpbin` 或 `ILDASM` 等标准工具,这些工具能提供更清晰的 .NET 元数据可视化效果。总而言之,Chibil 为在 .NET 生态系统中运行 C 代码提供了一座独特的桥梁。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Chibil:一个针对 .NET IL 的 C 语言编译器 (github.com/michalstrehovsky) 8 分,由 algorithmsRcool 发布于 46 分钟前 | 隐藏 | 过往 | 收藏 | 讨论 | 帮助 准则 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Chibil is a C compiler based on chibicc rewritten in C# and updated to target .NET IL (MSIL).

It is complete enough to run DOOM (PureDOOM).

Chibil takes C source files and generates COFF OBJ files. These OBJ files are binary-compatible with OBJ files produced by the MSVC compiler in /clr mode. link.exe from Visual Studio is used to link the object files together and produce final executables. One can actually mix and match C++/CLI and chibil-produced object files.

Chibil will probably have its own linker later, if for no other reason, just so we don't need Windows.

Line numbers and locals work as expected. You can step through the C code in a .NET debugger.

There isn't one.

There is a minimal stub of a C runtime library in the crt directory. This provides a runnable main that takes string[] of arguments and dispatches to the C-standard main with argc and argv. The produced assembly is converted to a COFF object file using the asm2obj utility and can be linked together with the C code to form something with a runnable main.

Consuming C code from .NET code

This is not complete yet. The code is generated into global namespace so if you want to consume the compiled code from elsewhere (i.e. don't intend to just run the EXE), you'll need to use reflection such as Module.GetMethod to find the methods and reflection-invoke them.

The tools/coffobjdumper.cs file contains a COFF OBJ dumper that dumps .NET OBJ files. It's a good complement for ILDASM (the desktop CLR ILDASM!) that can dump the .NET metadata from COFF OBJ files, but doesn't show method bodies, and dumpbin.exe that can dump various things from COFF objects, but not much in terms of .NET metadata.

联系我们 contact @ memedata.com