Live data from Hacker News

Writing a C Compiler, in Zig (2025)

ar-ms.me

51–60 of 74 posts

Re: Writing a C Compiler, in Zig (2025)

#51
post #14
post #6

Earlier quoted context omitted.

Zig actually bundles LLVM's Clang, which it uses to compile C with the `zig cc` command. But the long term goal seems to not be so tightly coupled to LLVM, so I'm expecting that to move elsewhere. They still do some clever stuff around compiler-rt, allowing it to be better at cross-compilation than raw Clang, but the bulk of it is mostly just Clang. There is also another C compiler written in Zig, Aro[1], which seems…

They're not planning on dropping Clang.

I expressly said "not be so tightly coupled to LLVM" because I know they're not planning on dropping it altogether. But it is the plan for LLVM and Clang not to be compiled into the Zig binary anymore, because that has proven to be very burdensome. Instead, the plan seems to be to "side-car" it somehow.

Re: Writing a C Compiler, in Zig (2025)

#52
post #31

I'm not sure why people seem to be under the impression that writing a compiler means that the language the compiler is implemented in should have "low level" features. A compiler is just a text -> text translation tool if you can leverage other tools such as an assembler and never needs to access machine level instructions. E.g., Pascal compilers have traditionally been written in Pascal, hardly a language which con…

> I'm not sure why people seem to be under the impression that writing a compiler means that the language the compiler is implemented in should have "low level" features. Performance. You definitely can write a compiler in a high-level language and given the choice I certainly prefer to on my hobby projects. Having a garbage collector makes so many compiler algorithms and data structures easier. But I also accept tha…

For context, this is the author of https://craftinginterpreters.com/ , and https://gameprogrammingpatterns.com/ , and https://journal.stuffwithstuff.com/2015/09/08/the-hardest-pr... .

I promise that he knows a thing or two about compilers and performance!

For what it's worth, I agree with him. A recent example is the porting of the TypeScript compiler to Go: it hasn't been fully released yet, but people are already going wild for its performance improvement over the original in-TS compiler.

Of course, it took them over a decade to reach the point where a port was necessary - so it's up to you to decide when that decision makes sense for your language.

Re: Writing a C Compiler, in Zig (2025)

#53
post #16
post #8

Earlier quoted context omitted.

I think Rust is "higher level" than C or Zig in the sense that there are most abstractions than C or Zig. Its not Javascript, but it is possible to program Rust without worrying too much about low level concerns.

Except if you need to expose or consume a C API, or you need to use some obscure performance improvement.

Fuck it, by that logic C and Python are the same language.

Re: Writing a C Compiler, in Zig (2025)

#54
post #17

Earlier quoted context omitted.

The author was fed up with not having data structures already provided, and needing to roll his own

Not really understanding what this would be though, zig has all the basic stuff you would expect in its stdlib (hashmap, queues, lists etc) just like Rust

This is from last year, the stdlib was much more bare back then.

Re: Writing a C Compiler, in Zig (2025)

#56
post #46

Earlier quoted context omitted.

They're just removing the obligate dependency. I'm pretty sure they will keep it around as a first-class supported backend target for compilation.

No, the whole point is to eliminate dependencies that they have to maintain. "not obligate" really doesn't mean anything if it's available as a backend--the obligation is on the Zig developers to keep it working, and they want to eliminate that obligation. And the original question was "how will they reivent the wheel on the man-years of optimization work went into LLVM to their own compiler infrastructure?" -- the a…

> the answer is that Andrew naively believes that they can recreate comparable optimization.

That's exactly wrong.

> There are a whole lot of misstatements about Zig and other matters in the comments here by people who don't have much knowledge about what they are talking about.

Well spoken. You should look in the mirror.

Re: Writing a C Compiler, in Zig (2025)

#57

Earlier quoted context omitted.

Feels like maybe something lost in translation with their explanation - they say they were fed up of data structures etc. but they returned to Rust? I’m assuming there’s something a bit more nuanced about what they got tired of with Zig

Rust is a world away from Zig as far as being low-level. Rust does not have manual memory management and revolves around RAII which hides a great deal of complexity from you. Moreover it is not unusual for a Rust project to have 300+ dependencies that deal with data structures, synchronization, threading etc. Zig has a rich std lib, but is otherwise very bare and expects you to implement the things you actually want.

[dead]

Re: Writing a C Compiler, in Zig (2025)

#58
post #38
post #31

I'm not sure why people seem to be under the impression that writing a compiler means that the language the compiler is implemented in should have "low level" features. A compiler is just a text -> text translation tool if you can leverage other tools such as an assembler and never needs to access machine level instructions. E.g., Pascal compilers have traditionally been written in Pascal, hardly a language which con…

This comment started out strong, but then: > Pascal compilers have traditionally been written in Pascal, hardly a language which conjures up a "low level" image. It may be the case that it doesn't conjure up such an image, but Pascal is approximately on the same rung as Zig or D—lower level than Go, higher level than assembly. If folks have a different impression, the problem is just that: their impression.

Pascal, as defined by Wirth, had no "low level" features. E.g., no control over memory allocation other than the language provided new/dispose, no bit operators, clunky strings of fixed size, no access to system calls, no access to assembly, not even any hex or octal constants, all features which a language allowing "low level" access is expected to have (e.g. Ada, Modula-2/3, Oberon, all Pascal-derived languages). Things like conformant array parameters showed up much later in the ISO version but were not widely adopted. No modules either but this is not a low level feature. Turbo Pascal attempted to fix all this on the PC later on and it was deservedly well loved. Still, Wirth successfully wrote Pascal compilers in Pascal without --- obviously -- having a Pascal compiler available. [Link](https://en.wikipedia.org/wiki/Pascal_(programming_language)#...)

Re: Writing a C Compiler, in Zig (2025)

#59
post #31

I'm not sure why people seem to be under the impression that writing a compiler means that the language the compiler is implemented in should have "low level" features. A compiler is just a text -> text translation tool if you can leverage other tools such as an assembler and never needs to access machine level instructions. E.g., Pascal compilers have traditionally been written in Pascal, hardly a language which con…

It's because a compiler is supposed to be high-level to low-level; you already have a lower-level language to write in it, and not a higher-level one. Writing a C compiler in a higher-level language than C is going backwards. E.g., Pascal compilers have traditionally been written in Pascal, hardly a language which conjures up a "low level" image. How could the first Pascal compiler be compiled if it was written in Pa…

> How could the first Pascal compiler be compiled if it was written in Pascal, but a Pascal compiler didn't yet exist?

Therein lies the magic of bootstrapping a language compiler written in the same language. Look it up.

Re: Writing a C Compiler, in Zig (2025)

#60
post #59

Earlier quoted context omitted.

It's because a compiler is supposed to be high-level to low-level; you already have a lower-level language to write in it, and not a higher-level one. Writing a C compiler in a higher-level language than C is going backwards. E.g., Pascal compilers have traditionally been written in Pascal, hardly a language which conjures up a "low level" image. How could the first Pascal compiler be compiled if it was written in Pa…

> How could the first Pascal compiler be compiled if it was written in Pascal, but a Pascal compiler didn't yet exist? Therein lies the magic of bootstrapping a language compiler written in the same language. Look it up.

You need a different compiler to compile the first one. You are the one who needs to "look it up".
Post reply on HN