r/programming • • May 01 '18

C Is Not a Low-level Language - ACM Queue

https://queue.acm.org/detail.cfm?id=3212479
152 Upvotes

303 comments sorted by

View all comments

Show parent comments

12

u/jerf May 01 '18

I would have to disagree. C is a low-level language, for a computer that isn't the one you're programming on, so you're really working with a simulation of that computer rather than the hardware you're really on. That doesn't make C a mid-level language... it makes it a mismatched language. For instance, C just has no concept of your GPU, which on many computers is in some sense the majority of raw computational power available from the hardware! And C just has nothing for that. It's never heard of GPUs.

(Neither do most other languages. I submit that part of that is the way that C has controlled our very conception of what a programming language is for so long that people creating new languages tend to create languages that are often "C, but with this new feature", rather than "Let's think about how to create a language that takes advantage of the CPU and the GPU together." Or "let's build a language that works with cloud abstractions and has a fundamental concept of 'a function that may not return' or something". Or any number of other interesting avenues left unexamined because A Computer Language Is Basically C With More Features.)

41

u/Isvara May 01 '18

This is like arguing over whether 3 is a small number.

16

u/JessieArr May 01 '18

It's obviously a large number. It's 300 times as big as 0.01!

5

u/eattherichnow May 02 '18

There exists a positive šœ€ smaller than it, therefore it's not a small number.

→ More replies (1)

25

u/monocasa May 01 '18

C is a low-level language, for a computer that isn't the one you're programming on, so you're really working with a simulation of that computer rather than the hardware you're really on.

That's true of every high level language though.

21

u/jerf May 01 '18

Not every high-level language claims to be an accurate representation of the hardware the way C proponents claim it to be. In fact, pretty much nothing else does. So there's no contradiction there.

18

u/monocasa May 01 '18

I'm a C proponent, and I'm saying that the C abstract machine isn't a 1 to 1 mapping to any hardware that exists. So what you're saying is empirically not true.

27

u/jerf May 01 '18 edited May 01 '18

That's a motte & bailey argument. C has been promoted for decades as "the language that's close to the bare metal", as "high-level assembler", and everyone knows it.

And it's not like it was a lie. It was those things. It's just that step by step, year by year, CPU by CPU, it gradually ceased to be. And so there's no sharp line where it ceased to be true. I can't say "Oh, it was the Pentium that did it in, yeah." But it went from true, to an approximation of true, to a useful lie to tell students (which I consider not necessarily a bad thing), to where we are today, where it is an active impediment to both understanding modern CPUs and programming them well.

9

u/monocasa May 01 '18

I mean, it's still one of the closest languages to assembly.

Assembly doesn't get you all that closer to how these machines work as compared to C. That's not saying that C gets you near there, but more that these machines don't expose any public interface that isn't abstracted to a sequential instruction machine that's a close analog of C, including assembly.

4

u/sabas123 May 01 '18

Why wouldn't assembly bring you any closer (assuming were just talking about x86-64). While its true that assembly does not provide a 1-1 conversion to machine code, I don't see how it could illustrate a stack based architecture any better.

6

u/monocasa May 01 '18

I mean, these machines are stack machines. There's more gates in the stack engines than the ALUs. And if you treat RSP (and to a lesser degree RBP) as anything other than a stack pointer with stack-like accesses you'll kill performance as you'll fall off into microcode manually synchronizing between the ALUs and the stack engine.

You can see this in AArch64 too where they demoted SP from being a full architectural register vs AArch32 so they don't even have to attempt to synchronize the disjoint hardware.

→ More replies (3)
→ More replies (10)
→ More replies (2)

21

u/oridb May 01 '18 edited May 01 '18

C is a low-level language, for a computer that isn't the one you're programming on

This is a meme. This meme is false. The pdp11 is close enough to most modern architectures that it would not have affected the design of C too much.

There are differences, but these differences aren't exposed by the assembly language either.

For instance, C just has no concept of your GPU, which on many computers is in some sense the majority of raw computational power available from the hardware!

And yet, it runs just fine on the GPU, once you give the appropriate compiler a hint to compile the function for the GPU.

10

u/monocasa May 01 '18

This is a meme. This meme is false. The pdp11 is close enough to most modern architectures that it would not have affected the design of C too much.

To be fair, modern architectures were designed to run C well. That was the whole point of RISC, take a look at what a compiler actually emits and make a core that runs just that.

→ More replies (2)

11

u/jerf May 01 '18 edited May 01 '18

This is a meme. This meme is false. The pdp11 is close enough to most modern architectures that it would not have affected the design of C too much.

I completely disagree. For one thing, I cite the article's own listing of the differences, and the fact they are visible and relevant. Considering among those things are "multicore", we're talking some pretty fundamental differences. (C11 sort of has it, but it is clearly bolted on literally decades after the fact, not something C has any concept of.) The article isn't even complete, and this is including as monocasa points out that modern hardware is designed to be a C machine, which means that the differences aren't just "things that happened", but changes that were made despite the fact it would take the hardware farther from what C supports.

Among the other things that C doesn't account for is the importance of the memory hierarchy. It may superficially seem like C has good support for cache locality by letting you arrange structs and pack things tightly, but that's just because things like dynamic scripting languages have such poor cache locality that even C looks good. A language that was truly good with cache locality would be something like that language Jonathon Blow was prototyping that would let you use columnar arrays of data, where arrays slice horizontally on structs instead of just plopping structs down in a row, with a simple specification on the array rather than the manual implementation you'd have to do in C.

(This is another one of those things where C limits the very thoughts of language designers. This is an obvious consequence of the way modern caches work, but few languages have fiddled with this, even though it isn't even that hard, once you're writing your own language.)

And yet, it runs just fine on the GPU, once you give the appropriate compiler a hint to compile the function for the GPU.

Does it run on the GPU, or does it run on a simulation of the C standard machine running on the GPU, resulting in access to only a fraction of the GPU's capabilities?

Honest question. Last I knew you needed CUDA or something to get anything like real access to the GPU's capabilities. Since the entire point here is precisely that merely running C doesn't mean you're getting a good abstraction of your computer's capabilities, a GPU merely running C could very well be merely a more obvious version of the point I'm making about the CPU as well.

→ More replies (5)
→ More replies (10)

15

u/[deleted] May 01 '18

For any system architecture in history, the only low level language has been, and is, assembly language. Assembly language is merely the one-to-one direct association to machine language for any CPU. If you want to call C a "mixed" level language, fine. That is merely semantics. But C is not a low level language - only assembly is.

24

u/[deleted] May 01 '18

Assembly is still a high level language. The only real language is writing the op codes by hand with toggle switches. /s

13

u/monocasa May 01 '18

There was a program to do that job, an ā€œoptimizing assemblerā€, but Mel refused to use it.

ā€œYou never know where it's going to put thingsā€, he explained, ā€œso you'd have to use separate constantsā€.

http://www.catb.org/jargon/html/story-of-mel.html

→ More replies (1)

17

u/Chippiewall May 01 '18

Ehh.. these days even assembly wouldn't constitute a low level language. The machine code that's generated still doesn't run precisely as described. Modern CPUs perform so many optimisations at runtime in terms of instruction re-ordering, register remapping, skipping no-op instructions that even assembly doesn't fit as an accurate description of what the hardware will do.

5

u/ThirdEncounter May 01 '18

The thing is, as a programmer you have no control over how those CPU tasks are done - except, of course, if there is a bug that leads to Spectre and others. So, what does it matter if assembler does not account for such tasks?

That's like saying that you're not a true expert car driver because you can't access the steering hydraulic pump with your bare hands to control it.

7

u/Garestinian May 01 '18

But I think that's precisely the point of the article - modern "fast" processors are so complex that even plain old assembly isn't low level in the truest sense. So a question is - could we do with simpler (dumber) processors, and assembly that is closer to how processors work today (multithreading, vectorisation, cache), but is not supportive of C?

3

u/MINIMAN10001 May 01 '18

Well the reason why we've reached this point is because it irritatingly went down the rabbit hole of saying "Well X isn't low level because Y"

Truth is the answer is no, C as a language is close enough to the code ran on the hardware and does not try to abstract away a large amount it does qualify as low level.

→ More replies (1)

4

u/FenrirW0lf May 01 '18

Which is ultimately the point of the article tbh. Things have been written in C for decades, so processors expose an interface similar to the kind that C compilers have historically targeted despite operating completely differently under the hood these days.

Maybe that point would have been better made if the author had titled it as "Assembly Is Not a Low-Level Language".

→ More replies (1)

6

u/MINIMAN10001 May 01 '18

If we want to get this stupidly nitpicky x86 assembly isn't low level because x86 is a virtual machine

→ More replies (3)

11

u/Vhin May 01 '18

This makes the term "low level languages" useless. We already have a perfectly satisfactory term to refer to assembly languages. Namely, assembly languages.

→ More replies (6)
→ More replies (6)
→ More replies (5)