28 comments

  • lioeters14 hours ago
    &gt; For the past 3 years, I have been working on compiling Rust to C. .. This is, by my count, the 14th attempt: cilly<p>Gotta respect the dedication to a niche interest.<p>&gt; The primary goal of this is support for old&#x2F;obscure hardware with no LLVM&#x2F;GCC support.<p>I remember reading about the bootstrapping question, how it typically requires a Rust compiler to build the Rust compiler from source. <a href="https:&#x2F;&#x2F;bootstrapping.miraheze.org&#x2F;wiki&#x2F;Bootstrapping_Specific_Languages" rel="nofollow">https:&#x2F;&#x2F;bootstrapping.miraheze.org&#x2F;wiki&#x2F;Bootstrapping_Specif...</a><p>Oh, but I see there&#x27;s a C++ implementation of the Rust compiler. <a href="https:&#x2F;&#x2F;github.com&#x2F;thepowersgang&#x2F;mrustc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;thepowersgang&#x2F;mrustc</a><p>Anyway, this part sounds useful too, that crustc can compile across network and devices.<p>&gt; You build a small C server on your Blorbo OS, run rustc on some normal platform like Linux, and let cilly talk over the wire.
    • radarsat12 hours ago
      &gt; The primary goal of this is support for old&#x2F;obscure hardware with no LLVM&#x2F;GCC support.<p>If you&#x27;re going to go to all this effort for an old target though, wouldn&#x27;t the effort be better spent on making it an LLVM target? Then you&#x27;d get Rust and a bunch of other languages for free.<p>But maybe there are required parts of the LLVM IR that make this undesirable for certain targets, maybe requiring specific hardware features, I don&#x27;t know. I guess also WASM-as-IR is a possible way to go. (Is that a thing?)<p>Edit: sorry I see that this point was already raised in this thread by ivanjermakov. Ignore.
    • lispwitch13 hours ago
      Guix uses mrustc for bootstrapping Rust, as required for compiler packages; it&#x27;s a really impressive project and has worked well in that role for some time. This new project is interesting for other reasons though, because mrustc is targeted at the de fact &quot;subset&quot; of Rust in use by rustc at any given time. This looks like it could have broader applications, like compiling Rust programs for platforms not supported by LLVM. If it really targets ANSI C (C89 or so), that&#x27;s potentially many more platforms than are possible with ordinary rustc<p>More on the Rust bootstrapping process (2018): <a href="https:&#x2F;&#x2F;guix.gnu.org&#x2F;blog&#x2F;2018&#x2F;bootstrapping-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;guix.gnu.org&#x2F;blog&#x2F;2018&#x2F;bootstrapping-rust&#x2F;</a>
    • zadikian14 hours ago
      So the author made a Rust to C transpiler and immediately used it to transpile... the Rust compiler. I love it.
      • Animats8 hours ago
        It makes sense as a porting tool, if you need to port the Rust compiler to some target that has a C compiler.<p>But it doesn&#x27;t mean rustc generates code for that target, only that you can run it there. You&#x27;d still have to teach LLVM about the target. Although that might already have been done.<p>It&#x27;s not that useful for retro computers because the Rust compiler needs too much memory for most machines of the 32-bit era.
  • taris214 hours ago
    Have you tried Diverse Double-Compiling (DDC) to test if the official rust compiler has a backdoor?<p>Use crustc to compile the rust source code, producing a new compiler. Then use this new compiler and the official rustc binary, both with deterministic flags, to compile the rust source code again. The two outputs should match bit for bit.
    • steveklabnik13 hours ago
      This was done with mrustc, which produced byte identical output.
    • rcxdude12 hours ago
      Better than that, you can get a bootstrapped rust from the Guix project, which has bootstrapped its entire system from source code from only a tiny verifiable binary.
    • wmanley5 hours ago
      It&#x27;s not diverse in that case - it&#x27;s the same compiler source compiled to binaries twice - it&#x27;s just that with one compiler you&#x27;ve gone via a C intermediate representation. For the purposes of diversity it&#x27;s the same as compiling rustc with the cranelift&#x2F;gcc backend.
      • moltonel43 minutes ago
        From a &quot;trusting trust&quot; point of view, compiling rustc-translated-to-C with a C compiler (and comparing the result to normally-compiled rustc) is a valid demonstration, because we&#x27;re again starting from code and can eliminate the possibility of a binary-resident compromise.<p>The actual issue here is that the translation was done using a rustc backend, and therefore an existing rustc binary which could be compromised and inject a &quot;if (user==&quot;wmanley&quot;) {...}&quot; that isn&#x27;t present in the original Rust code. If cilly was completely standalone (like mrustc), or if you had a rustc+cilly build you trusted, there would be no issue.
    • swalberg13 hours ago
      This reminds me of <a href="https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~rdriley&#x2F;487&#x2F;papers&#x2F;Thompson_1984_ReflectionsonTrustingTrust.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~rdriley&#x2F;487&#x2F;papers&#x2F;Thompson_1984_Ref...</a>
      • gmzamz8 minutes ago
        I&#x27;m also a fan of <a href="https:&#x2F;&#x2F;www.teamten.com&#x2F;lawrence&#x2F;writings&#x2F;coding-machines&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.teamten.com&#x2F;lawrence&#x2F;writings&#x2F;coding-machines&#x2F;</a>
      • laughinghan10 hours ago
        The sole and entire purpose of Diverse Double-Compiling is addressing Thompson&#x27;s Trusting Trust: <a href="https:&#x2F;&#x2F;dwheeler.com&#x2F;trusting-trust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dwheeler.com&#x2F;trusting-trust&#x2F;</a>
        • FergusArgyll2 hours ago
          I didn&#x27;t know there&#x27;s a solution, thanks!
      • lioeters12 hours ago
        Reflections on Trusting TRust.
    • amir734jj13 hours ago
      Awesome idea!
  • levkk14 hours ago
    Very cool. At first, I thought it was yet another LLM-generated demo, but no: original work of art. Super cool. Transpiling into C does seem easier than LLVM IR, and letting GCC optimize seems like this might actually work.<p>Excited to see the compiler implementation when it&#x27;s out -- a lot to learn from.
  • ronsor15 hours ago
    &gt; I put my left hand in a blender. The blender won. (Still have all my fingers, just some stitches). I will not elaborate further.<p>What a shame. I would&#x27;ve read an article about this.
    • RustyRussell5 hours ago
      Nobody who would code this up would be entirely sane. But I can&#x27;t help thinking &quot;these are my people&quot; when I read this...
    • luke-stanley2 hours ago
      But did it Blender open the source?
    • cozzyd13 hours ago
      I mean I get confused every time I use blender too but not to the point of losing fingers...
    • iwassayinbourns12 hours ago
      I desperately want to know more.
  • ahartmetz14 hours ago
    Wait, I thought LLVM had a C backend which could be used for the purpose of transpiling Rust to C? Turns out not for a long time, but now maybe again: <a href="https:&#x2F;&#x2F;discourse.llvm.org&#x2F;t&#x2F;re-ann-llvm-c-backend-still-able-to-generate-c-code-from-c&#x2F;90055" rel="nofollow">https:&#x2F;&#x2F;discourse.llvm.org&#x2F;t&#x2F;re-ann-llvm-c-backend-still-abl...</a>
    • simonask7 hours ago
      This approach is harder than you might imagine. LLVM can do a lot of things that don&#x27;t map to C language constructs. You cannot generally roundtrip arbitrary LLVM IR through some C representation. You can emulate most things, but you won&#x27;t necessarily get the same LLVM IR in the other end.
      • afdbcreid6 hours ago
        FWIW, Rust can <i>also</i> do a lot of things that do not easily map to C (at least standard C) constructs. For example:<p>- You can compare any two pointers, while in C they must point to the same allocation. This is possible to solve by converting to integers first.<p>- Signed integer overflow is UB in C, defined to wrap&#x2F;panic in Rust.<p>- Type-based alias analysis is a big one, does not exist in Rust.
        • tialaramex39 minutes ago
          &gt; You can compare any two pointers, while in C they must point to the same allocation. This is possible to solve by converting to integers first.<p>Indeed specifically Rust defines that pointer comparisons are done by address, whereas C doesn&#x27;t specify what the rules are exactly. This gets sticky if the pointer was invalidated (e.g. you free&#x27;d the memory it was pointing at). Rust says - as you might expect - that you can still compare this invalid pointer to another pointer (which may or may not still be valid and indeed might be a <i>valid</i> pointer to the same address!) because we&#x27;re only comparing the address - but C++ says you mustn&#x27;t do that and I believe C has the same rule (I feel like Martin Uecker will correct me here if I&#x27;m wrong, but other people feel free to jump in).<p>&gt; Signed integer overflow is UB in C, defined to wrap&#x2F;panic in Rust.<p>This doesn&#x27;t feel like an interesting difference. C made a weird choice, which was maybe convenient for the usual Worse Is Better reasons 50+ years ago. LLVM doesn&#x27;t care about that choice, LLVM can cheerfully simulate this behaviour in C if you want that.<p>&gt; Type-based alias analysis is a big one, does not exist in Rust.<p>This also doesn&#x27;t feel relevant but I guess I&#x27;m interested in why you think it would make &quot;a big&quot; difference?
  • Cadwhisker14 hours ago
    &gt; The primary goal of this is support for old&#x2F;obscure hardware with no LLVM&#x2F;GCC support. There are still some systems out there that don&#x27;t support Rust but support C.<p>The landing page mentions Plan 9 as one of the systems.
  • layer814 hours ago
    Finally we can rewrite all the Rust in C. ;)
    • avadodin4 hours ago
      As long as we rewrite all the C++ in Rust first.
  • npalli13 hours ago
    Rewrite in C is the new Rewrite in Rust.
    • keyle6 hours ago
      GTA VI out in Rust before C++?
      • roflcopter694 hours ago
        Wake me up when consoles officially support Rust. Until then there&#x27;s no real way around C&#x2F;C++ if you want to publish a real game to real consoles. But there&#x27;s <a href="https:&#x2F;&#x2F;akaganite.com" rel="nofollow">https:&#x2F;&#x2F;akaganite.com</a> so it might become possible soon™
  • ivanjermakov6 hours ago
    &gt; The primary goal of this is support for old&#x2F;obscure hardware with no LLVM&#x2F;GCC support<p>Wouldn&#x27;t it be easier to add old hardware support to LLVM&#x2F;GCC instead? I adore the project scale and determination, but for this goal extending existing projects seems more logical than building a language translator.
    • Perz1val5 hours ago
      &gt; Wouldn&#x27;t it be easier to add old hardware support to LLVM&#x2F;GCC instead?<p>No, in fact it&#x27;s much, much harded. You have no idea of the scope. I have no idea of the scope. Nobody does. There are obscure machines we&#x27;ve never heard about and there are C compilers for them. Targeting and supporting them from modern toolchains is a fool&#x27;s errand.
    • jitl1 hour ago
      rust to c supports infinite platforms that already have a c compiler by implementing a single rust to c program<p>on the other hand, porting llvm to an infinite number of platforms requires an infinite amount of work<p>so, it is less work this way
    • claudex5 hours ago
      Some architecture lacks documentation, if you have a working C compiler, it&#x27;s easier to use it than working on a compiler to target it.
      • Liquid_Fire2 hours ago
        Presumably you still need a C++ compiler for LLVM itself though. Or... a C++ to C translator.<p>Edit: On second thought, that&#x27;s only needed if you want to run rustc itself on the old hardware, which is probably not super useful given the main reason you would need to do this is if LLVM can&#x27;t target that hardware.<p>For building code written in Rust for such old hardware, this would be sufficient.
  • groos14 hours ago
    As an ex C++ compiler developer, I heartily approve of this project. Kudos.
  • SpecialistK14 hours ago
    I wonder if this could be used in PPC Mac OS X, where LLVM isn&#x27;t supported and most graphical applications need to use GCC 4 with Apple&#x27;s SDK.
  • westurner28 minutes ago
    There are discussions of round-trip between Rust and C;<p>Would this be useful for this too?<p>From <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46265855">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46265855</a> :<p>&gt; <i>To better port C to Rust: 3C (Checked C), c2rust, Crown ownership analysis, RustMap, c2saferrust (LLM), Laertes</i><p>C -&gt; Checked C -&gt; Rust<p>Because Checked C will annotate the raw and other C pointers first.
  • adithyassekhar7 hours ago
    I&#x27;ll wait for rustcrustc
  • db48x3 hours ago
    We should port it to Rust.
  • Tiberium15 hours ago
    I wonder how the performance looks like, because this can be interesting even for non-porting reasons ;)
    • adastra2214 hours ago
      It is very unlikely that it would be faster.
      • gerdesj14 hours ago
        Faster than what? Please finish your sentence.
        • lpribis14 hours ago
          Faster than rustc (the main rust compiler written in rust). Obvious from the context.
        • keepupnow14 hours ago
          Sonic
  • Imustaskforhelp14 hours ago
    This could be used within <a href="https:&#x2F;&#x2F;bootstrappable.org&#x2F;projects.html" rel="nofollow">https:&#x2F;&#x2F;bootstrappable.org&#x2F;projects.html</a> to make bootstrappability of rust incredibly much easier other than the previous route of OCaml and other things.<p>I know some folks within the bootstrappable OS projects community are on Hackernews and I hope that they could take a look at this. I feel as if this project could drastically shrink down the efforts needed to get a working rust compiler in a bootstrappable manner.
    • lispwitch13 hours ago
      mrustc (a handwritten Rust compiler in C++) is already used for that in Guix and likely other distros: <a href="https:&#x2F;&#x2F;guix.gnu.org&#x2F;blog&#x2F;2018&#x2F;bootstrapping-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;guix.gnu.org&#x2F;blog&#x2F;2018&#x2F;bootstrapping-rust&#x2F;</a> This would have other benefits though, as it&#x27;s both a second bootstrapping path and could potentially compile Rust programs for platforms not supported by LLVM
    • hkalbasi14 hours ago
      Not really. This C code is more like a binary and compiler artifact than a source code. So it won&#x27;t match the standards of bootstrap.
      • afdbcreid6 hours ago
        Depending on what your goal is. If it is eliminating trusting trust attacks, yes this is no enough. But more commonly you only want to compile rustc for a platform it was never compiled on, and for that this project is definitely enough.
  • micheleeet5 hours ago
    Rust is finally memory safe enough to become C
    • Sharlin5 hours ago
      &quot;You have become the very thing you swore to destroy&quot;
  • nxtfari14 hours ago
    this is really cool but it seems very unlikely that someone targeting an exotic system not supported by rust (mostly embedded and ancient mainframe targets) would be willing to trust a beta transpiler to not inject any bugs or leaks in the process of turning rust to c. nevertheless, very cool.
  • moezd9 hours ago
    Let Rust vs C battle commence with renewed haste!
  • fithisux8 hours ago
    Kudos.
  • linzhangrun13 hours ago
    Quite an interesting project, lol. I like the name `crustc` :)
  • casey210 hours ago
    Not to be confused with crust [1] the dialect of rust B [2][3] is written in.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;tsoding&#x2F;crust" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tsoding&#x2F;crust</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;bext-lang&#x2F;b" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bext-lang&#x2F;b</a> [3] <a href="https:&#x2F;&#x2F;www.nokia.com&#x2F;bell-labs&#x2F;about&#x2F;dennis-m-ritchie&#x2F;kbman.html" rel="nofollow">https:&#x2F;&#x2F;www.nokia.com&#x2F;bell-labs&#x2F;about&#x2F;dennis-m-ritchie&#x2F;kbman...</a>
  • valentynkit7 hours ago
    [dead]
  • sylware1 hour ago
    [dead]
  • CurbStomper13 hours ago
    [dead]
  • unjuno10 hours ago
    The most interesting part of this to me is not “Rust to C” by itself, but the fact that it widens the pool of people who can help debug portability problems.<p>There are relatively few people who understand Rust’s compiler internals, LLVM backends, and obscure target support deeply. But there are many engineers who understand C compilers, ABIs, linkers, makefiles, cross-compilation, old operating systems, and weird platform-specific compiler behavior.<p>If Rust can be lowered into target-specific C, then some problems stop being exclusively “Rust compiler problems” and also become C toolchain problems. That means more people can inspect the generated C, build failures, linker errors, ABI mismatches, and compiler-specific behavior.<p>C is obviously not a magic portability layer. ABI details, integer widths, alignment, TLS, aliasing, and undefined behavior still matter. But as an ecosystem boundary, C gives many more engineers a way to participate in debugging and porting work.<p>I think that social&#x2F;maintenance aspect may be more important than the language translation itself.
    • jolmg8 hours ago
      Guideline:<p>&gt; Don&#x27;t post generated text or AI-edited text. HN is for conversation between humans.
      • jdw647 hours ago
        I saw the reply, and it&#x27;s not GenAI text. It&#x27;s just that in the process of translation, people usually use machine translation or LLM translation. The problem is the vocabulary we East Asians use. I experience this issue too.<p>Probably because in East Asia, we tend to emphasize things with things like &quot;xx&quot; a lot.
        • jolmg6 hours ago
          I thought that may be the case. I shared the guideline so they&#x27;d know the likely reason all their comments die immediately.<p>However, their comments are consistently long, so it may be GenAI after all. Their last comment in particular...<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=48771339">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=48771339</a>
          • jdw646 hours ago
            You may be right. It&#x27;s a really difficult problem.
    • jdw647 hours ago
      I think you&#x27;re probably using DeepL or some other AI translation. When you use DeepL, most sentences become flat and end up being judged as GenAI. I also used DeepL to communicate on Hacker News in the early days.I had a similar problem
  • icase19 minutes ago
    [flagged]
  • dangoodmanUT14 hours ago
    i believe the author is confused<p>this is the wrong direction<p>(jk i read the readme)