DevTrends开发者趋势
主题色

takashiidobe /

slate

Hey HN, For the past half year I've been working on Slate, my C to Rust transpiler. Slate parses C code as gcc/clang/msvc would, and then translates code to rust. The best supported "flavor" as I call them…

DT研究与学习DT内容 / 资源DTSkill / 插件DTLinuxDTmacOSDTWindows

关于

Hey HN, For the past half year I've been working on Slate, my C to Rust transpiler. Slate parses C code as gcc/clang/msvc would, and then translates code to rust. The best supported "flavor" as I call them is clang, with clang flavor I've been able to translate the Linux kernel (built for x86_64 defconfig) and booted the built vmlinux to a busybox shell. Code is here: https://github.com/takashiidobe/slate-linux . Same for projects like Cpython: https://github.com/takashiidobe/slate-python , and SQLite: https://github.com/takashiidobe/slate-sqlite . For an example of a multi-target build, I translated zlib to all targets slate supports with 9 features: https://github.com/takashiidobe/slate-zlib . Since the entire stack is written in Rust, I compiled to wasm and have a fully client side compiler-explorer esque demo as well: https://slate.takashiidobe.com/ . I wanted slate to be easy to use, and also emit code that a dev would want to maintain. It's one binary (cargo +nightly install slate-c2rust --locked), and comes with a subcommand to set up sysroots (for cross-target builds). Instead of supporting only the clang ecosystem (as many existing transpilers do), I wrote my own C parser stack so I can emulate the three major C compilers for multiple targets, and do things like keep comments, do rewrites based on where functions/macros/definitions come from with header tracking, and also handle flags differently based on compiler. There are currently a couple dozen rewrites, like removing muts, lifting pointers to higher level types, solving gotos, improving inline asm, etc, Slate aims to support all of modern C, including compiler extensions, like all builtins, intrinsics, inline asm for the three major compilers, and flags. Target support (for now) for x86_64, aarch64, i686, arm32, on linux, windows, mac. Of course this is a gargantuan task but I'm chipping away little by little. Regressions and testing are a big deal for me trying to scale up the project. I have thousands of tests that run per commit, and do oracle and snapshot testing (with filecheck), so every rust binary translated has to have the same exit code as the oracle (C translated by clang, gcc, msvc w/ wine). That gives me confidence that I'm neither worsening codegen nor breaking any past code as I add new functionality. At this point I have over a million lines of tests, and I'm trying to catch more cases day by day. Hope you try it out!

收录记录

  1. Show HN

    2026年11月26日 · 1 次收录

讨论这个产品