[HN Gopher] Aro - Zig's new C compiler
       ___________________________________________________________________
        
       Aro - Zig's new C compiler
        
       Author : whatever3
       Score  : 97 points
       Date   : 2024-07-20 11:03 UTC (12 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | Y_Y wrote:
       | It would be interesting to see how well it optimizes compared to
       | the established players. The fact that it handles C23 and common
       | GNU extensions is already impressive.
        
         | MaxBarraclough wrote:
         | I agree it does seem very impressive. My first thought was that
         | they must be using LLVM for the backend, but it looks like no,
         | it's fully 'self-contained'.
        
           | samatman wrote:
           | Eliminating the dependency on LLVM is a major focus for the
           | language team, and a big part of why Aro exists.
           | 
           | Note that it's the _dependency_ which is being eliminated: it
           | will remain possible to use LLVM as a back end, and to
           | compile Zig and C together without using Aro to translate the
           | C into Zig. If I 'm understanding the roadmap correctly, the
           | C _headers_ needed to link a C library with Zig code will be
           | translated into Zig, but doing so with the actual .c files
           | will be optional.
        
       | sirwhinesalot wrote:
       | Very nice, honestly the tooling around Zig is even more
       | impressive than the language itself.
        
         | Validark wrote:
         | The language itself is the reason the tooling exists though,
         | because it attracts a certain kind of developer that probably
         | would not have done the same kind of work for other languages.
        
           | fuzztester wrote:
           | what kind of developer is that? interested to know.
           | 
           | I have a lot of prior C usage background, including for
           | nontrivial stuff, including a product, although it has been a
           | while since I used it.
           | 
           | and I am checking out zig as one of my next languages to
           | learn in the C-like / systems programming area.
        
       | lambdaone wrote:
       | Zig is amazing. What started as a one-man project is becoming a
       | mature software ecosystem capable of standing on its own as a
       | peer with the major languages.
        
         | squarefoot wrote:
         | It appears to me it could become the low level language of
         | choice for small embedded systems in contexts where Lua or
         | MicroPython couldn't fit the bill performance-wise and one
         | wants something higher level than C. Ditto for Nim and Crystal.
        
           | Drygord wrote:
           | It's constraints on memory management basically will never
           | allow that to happen.
           | 
           | It lures people in with "use any allocator you want"! Which
           | only appears as freedom of choice when reality it's locking
           | the user into the same paradigm of memory management that has
           | given C a bad name to begin with.
        
             | throwawaymaths wrote:
             | 1/3 of the problem with c memory management is null
             | unsafely, not a problem in zig. 1/3 more is conflating
             | arrays with single item pointers. Also not a problem with
             | zig. The only memory management "issue" with zig is lack of
             | temporal memory safety.
        
         | akkad33 wrote:
         | Why is zig amazing? Could you elaborate a bit
        
         | samatman wrote:
         | I'm all-in on Zig, I think it's the bee's knees.
         | 
         | I needed a regex library for another project, and dashed off an
         | acceptable one in a week flat https://github.com/mnemnion/mvzr
         | 
         | The fun part about this is that it can compile, and _match_ ,
         | during both comptime and runtime. Will it replace PCRE? No. But
         | nor is it intended to.
        
           | ozgrakkurt wrote:
           | Really want to get into it when reading these posts. Feels
           | like rust killed my casual programming taste in last years.
        
       | janice1999 wrote:
       | There's doesn't appear to a whole lot of comments or
       | documentation in the code. Is this normal for Zig projects? Does
       | it have an equivalent of Sphinx to build docs from code?
        
         | hellcow wrote:
         | Zig has a documentation generator that uses specially formatted
         | comments, similar to godoc or JSDoc.
        
         | hu3 wrote:
         | https://zig.guide/build-system/generating-documentation/
        
       | slowmovintarget wrote:
       | I'm not all that familiar with the Zig tool chain.
       | 
       | Am I understanding correctly that this is a regular C compiler
       | used as a component of the Zig compiler chain allowing
       | compilation of C files alongside Zig? Or does Zig generate C and
       | therefore need a C compiler under the hood?
       | 
       | Looking at the documentation suggests it is the former, not the
       | latter.
        
         | anonymoushn wrote:
         | It says it's a C frontend for `zig translate-c` which
         | translates C code to Zig code. `zig translate-c` is not
         | required for most Zig projects or even for most Zig projects
         | that interop with C.
        
           | slowmovintarget wrote:
           | Thank you.
           | 
           | I also started reading through the Zig overview [1] and near
           | the end of that document it says this: "Not only can Zig
           | compile C code, but there is a very good reason to use Zig as
           | a C compiler: Zig ships with libc."
           | 
           | From the doc, IIUC, compilation of header files and other C
           | code allows Zig to do direct interop with no intermediaries.
           | Seems useful.
           | 
           | [1] https://ziglang.org/learn/overview/
        
             | 3836293648 wrote:
             | That's just wrapping clang. They're working on replacing it
        
           | Laremere wrote:
           | translate-c is not required for compiling pure Zig code.
           | However, the plan is to remove the "@cImport" built in. So if
           | your project is importing a c header, in the future you'll
           | add build step translating it from c to Zig, and then you
           | import it into your Zig code as a module.
        
             | fuzztester wrote:
             | what is the reason for this change? zig less than newbie
             | here. I've only just recently started reading the zig
             | overview, although I have been reading hn posts about zig
             | with interest for some time.
        
               | Laremere wrote:
               | Primary reason is that it removes a dependency on
               | libclang, and as such is required to remove the LLVM
               | dependency.
        
             | 6keZbCECT2uB wrote:
             | How does this work with things which are expressible only
             | in C? For example, Pascal strings with flexible array
             | members?
             | 
             | I guess since you said header, you keep everything opaque
             | and create a header for that which gets translated to Zig.
        
       | WalterBright wrote:
       | The dlang D compiler includes a full C compiler, so you can mix
       | and match C and D code. You can import C code from D, and D code
       | from C.                   dmd hello.c         ./hello
       | hello world
       | 
       | This helps enormously in binding to existing C libraries.
        
         | brabel wrote:
         | Is it possible to do what Zig does and cross compile to any
         | OS/arch, from any OS/arch?
         | 
         | Last I tried, I found some compiler options and commands to do
         | it, but they were badly explained, I tried for hours and it
         | just didn't work at all, so I gave up.
        
           | WalterBright wrote:
           | The dmd D compiler can cross-compile to any of the targets
           | that it supports. I use it all the time to develop on other
           | platforms.
           | 
           | I am currently adding AArch64 support to DMD's code
           | generator. dmd doesn't run on my Raspberry Pi (yet!), so I
           | use sshfs to access the Pi's filesystem from Ubuntu, compile
           | programs on Ubuntu, transfer the .o file to the Pi, link it
           | on the Pi, and run it on the Pi.
           | 
           | I'm sorry you're having trouble with this, so I recommend
           | posting on the D forums, and people are there to help!
           | 
           | https://forum.dlang.org/
        
             | wyldfire wrote:
             | The grandparent post refers to the fact that Zig ships with
             | C, C++ library source that it lazily cross-builds (at least
             | for linux, I don't recall how it solves this problem for
             | macOS or windows). And possibly it uses lld too? Since Zig
             | ships with these, it makes the whole cross-build experience
             | much smoother and less dependent on whether your host OS
             | distribution includes packages for your desired target
             | OS/architecture.
        
               | brabel wrote:
               | Exactly. Zig tooling includes a linker, and that's also
               | cross-platform. Zig can generate actual runnable
               | executables that run anywhere from a single machine.
               | 
               | As far as I can tell, D's DMD can cross compile but not
               | link. I probably missed that before (actually, I think I
               | was trying with LDC, I found the page where they document
               | the process while searching for it just now:
               | https://wiki.dlang.org/Cross-compiling_with_LDC if you
               | try those options, nothing works, at least from MacOS).
               | 
               | I just tried and this only works with the -c option
               | (compile, but not link):                   dmd -os=linux
               | -c source/app.d
               | 
               | Without `-c`:
               | 
               | ``` Error: linker exited with status 1 cc app.o -o app
               | -m64 -Xlinker -no_compact_unwind
               | -L/Users/me/dlang/dmd-2.109.1/osx/bin/../lib -Xlinker
               | -Bstatic -lphobos2 -Xlinker -Bdynamic -lpthread -lm
               | (dmd-2.109.1) ```
               | 
               | I am sure Walter knows how to compile on one machine,
               | then go to another machine and do the linking or whatever
               | is needed. I know nothing about those, but with Zig I
               | need not know anything other than which flag to use to
               | cross compile. It's a huge difference. D should do that,
               | otherwise it's just not competitive for people like me (I
               | am just an average joe programmer who knows how to write
               | code but has very little interest in learning how linkers
               | work).
        
               | wyldfire wrote:
               | > I am sure Walter knows how to compile on one machine,
               | then go to another machine and do the linking or whatever
               | is needed.
               | 
               | Historically, you would or at least could get your
               | compiler and linker from different vendors -- assembler,
               | disassembler and other tools too. So it's normal/common
               | for C compilers like gcc, clang, dmd to not be
               | distributed like Zig is.
               | 
               | But I would dare say that the way Zig is doing it is a
               | great model for others.
               | 
               | > go to another machine and do the linking
               | 
               | BTW - if you use something like debian/ubuntu for
               | development, they have compilers+linkers+binutils for
               | several different architectures available to make cross-
               | building without Zig at least a little bit easier.
        
           | o11c wrote:
           | Generally, the hard part isn't the cross _compiler_ (nor,
           | these days, the cross binutils), but the cross _libraries_.
           | There are at least 3 ways ( /usr/lib/$ARCH, /usr/$ARCH/lib,
           | /$CHROOT/usr/lib/) to set up the libraries, all incompatible
           | with each other.
           | 
           | Zig is notable for putting a lot of work into hard-coding
           | basic C libraries, though of course you still need a
           | scattering of other C libraries for most real-world programs.
           | 
           | (for a sufficiently advanced language ecosystem, the
           | language's own libraries should be easy enough)
        
         | Daunk wrote:
         | D is so underrated. Keep up the good work Walter.
        
         | nequo wrote:
         | How exactly does this relate to the post, Aro, other than this
         | also being a C compiler? Could you draw a comparison for those
         | who came here for Zig and not D?
         | 
         | What conclusion should we draw about Aro based on what you're
         | saying?
        
           | WJW wrote:
           | It doesn't relate to Zig at all, but Walter Bright is the
           | creator of D and tremendously proud of it.
        
             | nequo wrote:
             | I know that he is but this is incredibly off-topic. Aro
             | deserves the attention it is getting, without the
             | discussion getting hijacked by another project.
             | 
             | D would deserve attention too, in a separate post, or here
             | if it was explained how it relates to Aro.
        
               | jraph wrote:
               | Walter's comment could be read more charitably and could
               | show his interest in Zig and similar choices that are
               | made.
               | 
               | The last sentence, or, indeed, the whole comment, can be
               | read like "We did this for D, and it works well. [go for
               | it]".
               | 
               | I don't think D is casting any shadow on Zig. Both
               | languages play in the same field, D is older but I feel
               | like we hear about Zig way more these days.
               | 
               | His comment is likely to interest people interested in
               | Zig or PLs in general.
               | 
               | The comment as is might lack some finesse but I don't
               | think it deserves this level of aggressiveness.
        
               | bithavoc wrote:
               | I have profound respect for Walter and all his work, same
               | for Andrei; but his comment was off-topic.
        
         | binary132 wrote:
         | I would 100% be a D user if it had full C++ typesystem fluency.
        
       | livrem wrote:
       | Is this part of the long-term plan for zig to get rid of the
       | dependency on clang/llvm?
       | 
       | https://github.com/ziglang/zig/issues/16270
        
         | Laremere wrote:
         | Yes.
        
           | brabel wrote:
           | Does Zig already compile without LLVM, even if not by
           | default?
           | 
           | Also, do you know when the changes Andrew mentioned in his
           | "Data Oriented Design" are going to be released in the zig
           | compiler?
           | 
           | I would expect that when both of the above are done,
           | compiling Zig will be really, really fast compared to
           | anything we currently have.
        
             | samatman wrote:
             | It's the major area of focus right now. You can track the
             | issues with this tag/link if you'd like:
             | 
             | https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aope
             | n...
        
             | Laremere wrote:
             | The Zig compiler has used Data Oriented Design for a long
             | time, so any recent version has that, yes. The most
             | substantial part of running the compiler right now is LLVM,
             | hence the focus to provide an alternative. The other big
             | focus right now is incremental compilation, which will make
             | recompilations very fast.
        
               | brabel wrote:
               | The talk I am referring to is from 2024 and shows some
               | metrics improving by 30% or more. And Andrew said that's
               | not yet shipped:
               | 
               | https://www.youtube.com/watch?v=IroPQ150F6c
               | 
               | So, are you sure about that??
               | 
               | EDIT: ok, the talk was uploaded just last week, but
               | someone in the comments clarified that:
               | 
               | > The original talk is from 2021.. Thank you for
               | reuploading.
               | 
               | I feel stupid now :/
        
               | szundi wrote:
               | Don't
        
         | AndyKelley wrote:
         | No. Or rather, only the preprocessor.
        
       | AndyKelley wrote:
       | I don't think it's accurate to call it "Zig's". This is an
       | independent project started by Veikka Tuominen, a Zig core team
       | member, but it's not owned or managed by ZSF in any way. It's
       | Veikka's project. Don't take that away from him!
        
         | fuzztester wrote:
         | thanks for clarifying.
         | 
         | I was wondering why it was under a different github site.
        
       | dist1ll wrote:
       | It seems like I can't include standard header files, so it can't
       | compile a common hello world. I guess that'd be useful to mention
       | on the README, or maybe I'm doing something wrong?
       | main.c:1:10: fatal error: 'stdio.h' not found         #include
       | <stdio.h>
       | 
       | It'd be nice if header inclusion, and -l options work. This would
       | allow benchmarking Aro against some other compilers on a wide
       | range of C files. Usually sqlite3.c is my first go-to for stress-
       | testing C compilers.
        
         | defen wrote:
         | System header search paths are only implemented on Linux
         | currently. You should be able to parse sqlite3.c on Linux with
         | arocc -fsyntax-only -Wno-unknown-attributes sqlite3.c
        
       | fefe23 wrote:
       | Wait, so you still need zig, and zig still needs LLVM. If you
       | have LLVM, you have clang.
       | 
       | What is the value proposition of this compiler, then?
        
         | defen wrote:
         | The Zig self-hosted x86 backend is nearly complete (and is
         | already capable of building the Zig compiler itself). So it's
         | part of the path toward not needing LLVM or clang at all.
        
       | tiffanyh wrote:
       | The ReadMe is quite lacking ...
       | 
       | - why create a competing front end to Zig?
       | 
       | - what problems is it solving?
       | 
       | - what's different than the official Zig frontend?
       | 
       | Etc
        
       ___________________________________________________________________
       (page generated 2024-07-20 23:06 UTC)