News for hackers. Stuff that matters.
Re:Chris Lattner left Swift core team by skohan on Monday February 21, 2022 @01:49PM (#30419248)
That's exactly why Zig is such an interesting language to me. The premise of trying to write a modern C replacement - essentially what C would be if it were designed with the learnings of the past 40 years in mind - is super exciting.

I am a fan of Rust, but I am not 100% sold on it. The safety features and ADT's are nice, but I find it quite clunky in practice, and it is just such a huge language full of so many features. It almost feels more like a test language to try the concept of static memory management than a properly designed language in some ways.

I feel it's missing the quality from C/C++ that they are a very thin abstraction over assembly.

Re:Chris Lattner left Swift core team by nindalf on Monday February 21, 2022 @02:50PM (#30419810)
> a very thin abstraction over assembly.

I think this statement implies that it’s clear and obvious what assembly will be generated from a snippet of C/C++ code. But few people can understand everything that optimising compilers and modern processors do to normal looking code. An example of this lack of knowledge is seeing people disagree on if a snippet contains UB or not.

If C is so simple, why do people struggle to write it?

Re:Chris Lattner left Swift core team by skohan on Monday February 21, 2022 @03:15PM (#30420047)
> But few people can understand everything that optimising compilers and modern processors do to normal looking code.

Well when I'm talking about an abstraction over assembly I'm not talking about optimizing compilers. That's another topic entirely. What I mean is, if you look at a block of C code, it's very easy to understand what the machine is doing.

> If C is so simple, why do people struggle to write it?

Do they? I think people struggle to write correct, bug-free code in C, but that's because it doesn't save you from yourself, and it's happy to let you do whatever the hardware will do. That's a much larger programmable space than the set of all safe correct programs.

But I don't think people in general have difficulty looking at a snippet of C code and understanding it.

Re:Chris Lattner left Swift core team by nindalf on Tuesday February 22, 2022 @02:32AM (#30425004)
> What I mean is, if you look at a block of C code, it's very easy to understand what the machine is doing.

So they simultaneously understand what the machine is doing but don’t know what assembly will be generated? Both can’t be true.

If everyone understood what the machine was doing, everyone would be able to look at a snippet and agree - “that’s UB, let’s not do that”. But they can’t agree. Because few people understand what the compiler and the processor will do.

The “thin layer of assembly” was true for the first generation of C compilers. But it hasn’t been true for a long time. It’s a complete black box now. Anyone who thinks that it’s straightforward isn’t being upfront with themselves.

Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @03:58AM (#30425550)
> So they simultaneously understand what the machine is doing but don’t know what assembly will be generated? Both can’t be true.

I mean I can understand a reasonable mapping to what the un-optimized assembly would be. Compiler optimization is very complex, and is going to obscure the results in every language.

> If everyone understood what the machine was doing, everyone would be able to look at a snippet and agree - “that’s UB, let’s not do that”. But they can’t agree. Because few people understand what the compiler and the processor will do.

What? I think everyone can agree that UB is much harder to detect in assembly than in higher level languages than assembly, and assembly gives the most clear view of what the machine is doing. It's a very complex topic to create a system which detects and disallows UB automatically in the compiler - this requires a lot more complexity than a simple mapping of high level instructions to machine instructions.

> The “thin layer of assembly” was true for the first generation of C compilers. But it hasn’t been true for a long time. It’s a complete black box now. Anyone who thinks that it’s straightforward isn’t being upfront with themselves.

I don't know, seems pretty straightforward to me: https://godbolt.org

Re:Chris Lattner left Swift core team by nindalf on Tuesday February 22, 2022 @09:35AM (#30428367)
Let me rephrase. You say C is simple. I say Rust (for example) is simple. I can look at a Rust code base and say confidently - this code base has no UB in it, it has no memory safety issues in it.

Can you look at a non trivial C code base and make such an assertion? You can’t. Even simple looking C code could be translated into problematic assembly because such a transformation is technically valid. And it’s beyond the ability of anyone but an expert to guard against that.

C is a very useful language. Very important. Very fast. A great tool in the right hands. The world wouldn’t run without it. And it will remain useful and important and fast for decades to come, certainly. But it’s not simple and hasn’t been for a long time. Let’s acknowledge that.

Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @05:32PM (#30434818)
I'm sorry I don't understand your argument at all. What does lack of UB and memory safety have to do with simplicity? Those are very complex features of Rust which require a very complex compiler in order to achieve. Also Rust is notorious for having a steep learning curve, and it takes time for even very experienced programmers to become accustomed to it.
Re:Chris Lattner left Swift core team by nindalf on Wednesday February 23, 2022 @02:39AM (#30438666)
> a thin abstraction over assembly

Do you agree that this statement implies 2 things

1. The compiler isn’t doing anything unusual or unexpected. It applies only basic, easily understandable transformations from C to assembly

2. An intermediate C programmer would be able to guess correctly most of the time what the generated assembly would look like. And thanks to this, such a programmer would be able to avoid most footguns.

But the compiler does unusual/unexpected things, and it’s hard to guess what assembly will be generated or what that assembly does, it’s not a “thin abstraction”. Would you agree?

Re:Chris Lattner left Swift core team by skohan on Wednesday February 23, 2022 @04:51AM (#30439462)
I don't think this is the best way to understand this. C has been around for 50 years at this point, and there has been an enormous amount of investment and advancement in the realm of C compilers in that time, which has naturally resulted in complexity and esotericism in terms of how actual mainstream C compilers work. But that's not a metric of language complexity, it's an artifact of a half century of work on the topic.

I think a better metric is: an average CS grad with a little bit of background in compilers and assembly could reasonably be expected to be able to write a naive C compiler which covers say 80% of the footprint of the core language on their own in a matter of weeks.

What do you think is the size of the cohort of people who could write a naive Rust compiler, with borrow checking, ADT's, traits and non-lexical lifetimes? Even without some of the fancy bits like async you're already talking about grad level CS topics at the very least.

Re:Chris Lattner left Swift core team by pcwalton on Monday February 21, 2022 @03:47PM (#30420398)
C and C++ are not a very thin abstraction over assembly language. The C virtual machine is very far from the actual hardware. And Rust is the same distance from the hardware as C and C++ are. Rust's core operations are, by and large, the same simple mapping to LLVM instructions.

I don't find the idea of a new non-memory-safe C replacement in 2022 very exciting. We should be moving away as an industry from non-memory-safe languages, for the obvious security and productivity reasons.

Re:Chris Lattner left Swift core team by inkyoto on Monday February 21, 2022 @08:26PM (#30423016)
> C and C++ are not a very thin abstraction over assembly language

We can argue the thinkness (or the thinness) of the abstraction until cows come home, but

  while (*dst++ = *src++) ;
is a direct abstraction over

  L1:
    movb @(r0)+, @(r1)+
    tstb (r0)
    bne L1
(assuming «src» and «dst» are both «char *» and addresses are loaded into «r1» and «r0» registers, consequently) in the PDP-11 architecture that C was designed on and for. The instruction sequence is exactly 4x 16 bit words long. As well as

  *ptr &= 1;
becoming

  and #1, (r0)
and being 2x 16 bit word instruction (assuming «ptr» is an «int *» and is loaded into «r0»). Pointer arithmetic and array design in C as we know them today was highly influenced by the addressing modes existing in the PDP-11 ISA, with many C abstractions having to a direct correspondence to specific sequences of PDP-11 instructions.

C++ is much less of a hardware abstraction, specifically when it comes to the higher level language features.

Re:Chris Lattner left Swift core team by pjmlp on Tuesday February 22, 2022 @03:43AM (#30425462)
Except that breaks outside non PDP-11 CPU, where many languages offer the same "low level" capabilities of C.

Or 8 bit CPUs like 6502 and Z80 that aren't even able to fully support C.

Re:Chris Lattner left Swift core team by inkyoto on Tuesday February 22, 2022 @09:57AM (#30428667)
6502 and Z80 had not existed yet when C was begot for the PDP-11 architecture.

Yes – historically – C has never been a perfect fit for 8-bit architectures as it had been conceived for a 16-bit architecture. So what? There are C compilers for 68HC08 68HC11 MCU's as well.

Yet, C has outlived «many languages offering the same "low level" capabilities of C».

Re:Chris Lattner left Swift core team by pjmlp on Tuesday February 22, 2022 @11:11AM (#30429620)
The reasons for its survival cross many axis, it wasn't just for the language alone on its own.
Re:Chris Lattner left Swift core team by rswail on Tuesday February 22, 2022 @09:09AM (#30428063)
It really was an elegant instruction set.
Re:Chris Lattner left Swift core team by skohan on Monday February 21, 2022 @04:09PM (#30420659)
I don't see how you could argue that C is comparable in terms of level of abstraction to Rust and C++. For instance with Rust memory management is totally abstracted away from you as the programmer, which is a core part of what a computer is doing.

And if you index into an array in C, that's basically like saying `root memory location + stride * index`. In Rust it's calling a trait function which could be doing arbitrary work.

Rust and C++ are on similar levels of abstraction, but C is much, much simpler.

Re:Chris Lattner left Swift core team by pcwalton on Monday February 21, 2022 @04:48PM (#30421075)
How is Rust memory management totally abstracted away from you? You have to opt in to every heap allocation, just as you have to call malloc() in C.

And it's certainly true that Rust has overloading and C doesn't, but that wasn't what I was getting at. The point is that C is defined in terms of the C virtual machine, not the underlying hardware. The C virtual machine is quite far from the actual CPU instructions.

Re:Chris Lattner left Swift core team by skohan on Monday February 21, 2022 @06:33PM (#30422162)
In Rust you opt into every heap allocation with an abstraction like Rc, Arc, Box etc. With C you would have to implement each of those behaviors with primitives, because C is at a lower level of abstraction than Rust.
Re:Chris Lattner left Swift core team by littlestymaar on Tuesday February 22, 2022 @05:38AM (#30426226)
How is that different from C++? Since you've explicitly put C and C++ in the same basket, this argument doesn't hold any water…
Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @05:33PM (#30434831)
It sees like you're trying to catch me with some kind of gotcha instead of dealing with the argument in good faith.
Re:Chris Lattner left Swift core team by djur on Tuesday February 22, 2022 @01:03AM (#30424507)
malloc is also an abstraction.
Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @02:05AM (#30424829)
I never said C is without abstraction, only that it is a relatively thin abstraction. Are you seriously arguing that Rust is not at a higher level of abstraction than C?
Re:Chris Lattner left Swift core team by littlestymaar on Tuesday February 22, 2022 @05:39AM (#30426238)
> Are you seriously arguing that Rust is not at a higher level of abstraction than C?

Rust is clearly not at a higher level of abstraction than C++, yet in your own argument you've put C and C++ on the same ground…

Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @08:24AM (#30427593)
No I haven't. I said Rust and C++ are on similar levels of abstraction.
Re:Chris Lattner left Swift core team by littlestymaar on Tuesday February 22, 2022 @09:19AM (#30428175)
Yet a few comments above in this thread[1]:

> I feel it's missing the quality from C/C++ that they are a very thin abstraction over assembly.

[1]: https://news.ycombinator.com/item?id=30419248

Re:Chris Lattner left Swift core team by skohan on Tuesday February 22, 2022 @05:34PM (#30434836)
I also said this:

> Rust and C++ are on similar levels of abstraction, but C is much, much simpler.

Re:Chris Lattner left Swift core team by eropple on Monday February 21, 2022 @01:52PM (#30419283)
I haven't looked at Zig much because I mostly noodle with lower-level languages with an eye towards game prototypes and the like, so I don't know much about it. Have anything you'd recommend reading about the language?
Re:Chris Lattner left Swift core team by cturtle on Monday February 21, 2022 @03:25PM (#30420157)
I like Zig because it feels like a refinement of C with all the knowledge of the years since C was created. Some of my favorite features are no global allocator, well-defined pointers (with optionals required for nullability), generics and partial evaluation at compile-time, and builtin features/functions for all sorts of operations that are only extensions in C/C++ like SIMD and packed structs.

Read [0] for a comparison among Zig, D, Rust, and C++, and read [1] for a deeper look at the language's goals. I personally really love Zig's type system [2], with a system for generics that is very simple and consistent.

[0]: https://ziglang.org/learn/why_zig_rust_d_cpp/

[1]: https://ziglang.org/learn/overview/

[2]: https://nathancraddock.com/blog/consistency-in-zigs-type-sys...

Re:Chris Lattner left Swift core team by eropple on Monday February 21, 2022 @08:09PM (#30422920)
I'm going to give those a read. Thanks!
Re:Chris Lattner left Swift core team by Aloha on Monday February 21, 2022 @02:15PM (#30419490)
I've always understood that C was designed to fit a hardware budget.

We knew about many of these features then, when C was created - but it wasn't until the 2000's really where computing horsepower caught up to be able to use them in a universal way.

Re:Chris Lattner left Swift core team by skohan on Monday February 21, 2022 @02:50PM (#30419801)
Hmm I've always thought of C in terms of "portable assembly". Not so much that features were omitted because they weren't performant enough, but because they strayed too far away from machine-primitive operations.
Re:Chris Lattner left Swift core team by eropple on Monday February 21, 2022 @03:12PM (#30420013)
C itself is pretty far from the machine, too, except by accident.
Re:Chris Lattner left Swift core team by nicoburns on Monday February 21, 2022 @06:32PM (#30422156)
Some of them would have been great even on 70s hardware. Proper array, slice and string types come to mind.
Re:Chris Lattner left Swift core team by pjmlp on Tuesday February 22, 2022 @03:37AM (#30425420)
You mean the learnings of Modula-2 in 1978, but with a C like syntax?