Live data from Hacker News

Aro – Zig's new C compiler

github.com

81–90 of 130 posts

Re: Aro – Zig's new C compiler

#81
post #73

One of the features missing in bun:ffi is inferring types for symbols from header files. This would let you import C libraries in JavaScript/TypeScript directly, without having to configure bindings. We embed TinyCC for FFI, but TinyCC doesn't expose a way to read the types for exported symbols. Currently, it looks like this: import { dlopen, FFIType, suffix } from "bun:ffi"; // `suffix` is either "dylib", "so", or "…

Perhaps a middle-of-the-road approach would be to use inline C like LuaJIT's FFI does? For instance:

  new_clib("/system/lib64/liblog.so", "log")
  ffi.cdef[[int __android_log_print(int priority, const char *tag, const char *msg);]]
  ffi.log.__android_log_print(5, "TEST_TAG", "Hello from FFI")
I can't say I'm familiar with Bun, but I've used a similar syntax to the example you provided of the current Bun syntax when working with Frida (https://frida.re). I agree it leaves something to be desired and indeed it would be cool if Aro could do this in the future.

Re: Aro – Zig's new C compiler

#82
post #57
post #18

Earlier quoted context omitted.

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 progr…

The nixpkgs have a rather elegant way to deal with that. Let's presume you need a libpng for an armv7 platform, such as an old RaspberryPi. Then Nix allows you to - get a suitable cross compiler toolchain - pick whether you want dynamic or static linking (most likely static when the target system does not come with /nix/store) - compile all prerequisites for the library such as transitive dependencies - compile the l…

You can combine zig and nix with https://github.com/Cloudef/zig2nix> (shameless plug)

Re: Aro – Zig's new C compiler

#83
post #73

One of the features missing in bun:ffi is inferring types for symbols from header files. This would let you import C libraries in JavaScript/TypeScript directly, without having to configure bindings. We embed TinyCC for FFI, but TinyCC doesn't expose a way to read the types for exported symbols. Currently, it looks like this: import { dlopen, FFIType, suffix } from "bun:ffi"; // `suffix` is either "dylib", "so", or "…

With D it is:

    import sqlite3;
This causes sqlite3.h to be read, lexed, parsed, and semantically analyzed to convert (where possible) C symbols to D symbols, and makes them available to the importer.

I've been sometimes pressured to instead use various more complicated syntaxes, but have held the line :-)

Re: Aro – Zig's new C compiler

#85
post #16

Earlier quoted context omitted.

D is so underrated. Keep up the good work Walter.

I am pretty sure that most of the language design is done by Andrei Alexandrescu now.

Not at all. Walter is the "boss", Andrei left as a leader in May 2019: https://www.youtube.com/watch?v=cpTAtiboIDs&t=3049s

Re: Aro – Zig's new C compiler

#86
post #30

Earlier quoted context omitted.

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_LD…

A cross-linker would be required for that to work. If you use a cross-linker with D, it should work.

Given Zig has one, it might actually be possible to delegate to that then?

It would be awesome to have a post explaining how to do that :)

Re: Aro – Zig's new C compiler

#87
post #30

Earlier quoted context omitted.

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_LD…

> 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 grea…

FYI, Zig is in the process of dropping dependence on lld on Linux and Windows; macOS uses Zig's own linker since roughly 2021 as far as I recall. Tracking issue for reference: https://github.com/ziglang/zig/issues/8726

Re: Aro – Zig's new C compiler

#88

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.

...I mean Zig has those features too since the beginning with `zig cc`, translate-c and @cImport (currently via an embedded Clang), but the post is about an indepdent C compiler written in Zig (but which will most likely replace Clang as the embedded C frontend once Clang/LLVM isn't compiled into the Zig compiler anymore).

Re: Aro – Zig's new C compiler

#89
post #84

I hope Zig adds complex numbers before it is too late. It's a pity that they did not steal array syntax and slices from Fortran/Numpy.

I'm curious, what use cases do you have for complex numbers that couldn't be provided in a 3rd party library or maybe even the stdlib?

Re: Aro – Zig's new C compiler

#90
post #26

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.

Why is zig amazing? Could you elaborate a bit

This is a pretty good high level overview:

https://ziglang.org/learn/overview/

Post reply on HN