r/programming • • Jul 05 '24

C Is Not a Low-level Language - Your computer is not a fast PDP-11 (David Chisnall, 2018)

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

186 comments sorted by

783

u/7h4tguy Jul 05 '24

This article isn't even self-consistent. It outlines a spectrum for low-level to high-level of assembly to Star Trek Alexa.

And then goes on to argue that the reason C is not low level is because you don't deal directly with speculative execution/pipelining, physical (non-virtual) memory addressing, or control of multitasking.

But assembly does none of those either - the hardware deals with branch prediction and speculative execution and the OS deals with virtualizing memory and preemptive multitasking.

The fact is C maps pretty closely to what you'd write in assembly. Therefore it's low level. What a nonsense article.

179

u/bluetomcat Jul 05 '24

It's another way of saying that hardware has evolved in complexity and "assembly programming" is not low-level. There are countless abstractions at the hardware level which are transparent and are not exposed through the ISA. Even the names of architectural registers are translated. Should real men program with physical registers?

Apart from that, the abstract C machine is still the most relevant mapping we have. Pointers, offsets and unrestrained memory manipulation is what sets it apart from the competitors.

109

u/HelloThisIsVictor Jul 05 '24

My program comes in physical form with wires you need to solder to the cpu pins

87

u/Overunderrated Jul 05 '24

And I thought c++ compile times were bad

71

u/TheBrawlersOfficial Jul 05 '24

Real programmers start with a block of metal and a pile of sand. Doing anything else relies too much on magical abstractions that can produce bugs.

45

u/PancakeInvaders Jul 05 '24

blocks of metal ? what kind of poser doesn't shovel and smelt his copper ore, electrolize his alumina, and hammer his iron bloom

→ More replies (10)

5

u/[deleted] Jul 05 '24

This must be why whenever I open a JIRA ticket, I nervously start fingering my soldering iron

→ More replies (1)

1

u/Alexander_Selkirk Jul 05 '24

Interestingly, the first Apple computers had the interesting concept that periphery devices like floppy disk drives came with complete drivers included in their firmware. I still find it a genius idea. Steve Wozniak was a genius.

4

u/well-litdoorstep112 Jul 05 '24

It's another way of saying that hardware has evolved in complexity and "assembly programming" is not low-level. There are countless abstractions at the hardware level which are transparent and are not exposed through the ISA. Even the names of architectural registers are translated.

It's true though and it's a good thing.

4

u/astrange Jul 05 '24

C doesn't have unrestrained memory manipulation because of memory aliasing and type rules. You're just running it in an unsafe implementation that doesn't check for violations.

2

u/fryerandice Jul 05 '24

yes, real men should program with physical registers, and toggle switches.

24

u/UjudGablE Jul 05 '24

C (ie high level assembly) is basically how close a programmer will ever get to the actual hardware.

It is nonsensical to talk about lower hardware levels from the software's perspective because it is simply impossible to see and interact with it.

5

u/Alexander_Selkirk Jul 05 '24 edited Jul 05 '24

The thing is not that there aren't many "higher-level" languages. These exist for sure.

The point is that being "low-level" does not necessarily give programmers the control they think they have, and - similar with assembly today - being able to control very low-level aspects does not give any more an advantage in speed. The speed advantage of assembly compared to C / Turbo Pascal disappeared at the end of the nineties. The speed advantage of C is disappearing now.

(This is not to say that very direct control is not sometimes needed, such as manipulating interrupt registers in an OS kernel, but this is not needed for most C code.)

Point in case: C allows pointer aliasing to function arguments. FORTRAN does not. As a result, a FORTRAN compiler can produce faster vector and matrix code, because it knows that output arguments will not overwrite input arrays.

Yes, you can use pointer aliasing in C to implement memmove(), but you pay with less performance. Rust, interestingly, enforces something similar as FORTRAN, since objects are either writable, or shared - never both. The compiler can turn this knowledge into faster machine code.

Another example: You can program C or Assembly in a way that everything is stored in global variables, like in mid-1980ies BASIC code. Does this make your program faster? Probably not. Does this make your program harder to debug and evolve? Very likely.

8

u/tjf314 Jul 05 '24

well yeah, that's because hardware has had to remain compatible with C as it has been evolving. One could easily imagine having huge performance gains in software if you could actually manually control what data is stored in which cache, for example. But since that isn't part of C's abstract machine, if you as a chip manufacturer want C code to run on your CPU, you can't expose that stuff to the programmer, and it literally forces your assembly code to become further from the actual hardware.

19

u/Rezrex91 Jul 05 '24 edited Jul 05 '24

No no, the article got that very wrong IMO. The reason why those things aren't exposed even in assembly on the x86 architecture isn't to remain compatible with C's abstract machine. Other ISAs expose some of the features the article mentions yet they can run C code. And even on x86 vector instructions for example are present in the various extensions; also, you could probably write a C compiler for running C code on a GPU if you wanted...

The reason ILP is needed, and why even assembly started to heavily deviate from the underlying hardware is because x86 and x86_64 needed to remain binary compatible with the original 80x86 processors, yet they needed to evolve from scalar processors into pipelined parallel processors to meet the demand for ever increasing performance. This was a decision first by Intel then AMD and Intel, driven by the market demand at the time (the market didn't want another new architecture after the teething issues and the incredibly slow x86 compatibility layer of the failed Itanium...)

EDIT: corrected a IPC to ILP

→ More replies (6)

6

u/Smallpaul Jul 05 '24

How would exposing such a thing to a new language break C?

→ More replies (3)

44

u/loup-vaillant Jul 05 '24

The fact is C maps pretty closely to what you'd write in assembly.

Not as closely as one might think, and the difference often have significant implications, most notably security implications.

  • What happens when you overflow an integer in x86? It wraps around. What happens when you overflow a signed integer in C? It’s undefined, and has led to the compiler removing security checks, and subsequent vulnerabilities.

  • What happens when you dereference a null pointer in x86? It traps. In C? It’s undefined, and if the compiler notices this at compile times the results can be really really strange.

  • What happens when you interpret data in different ways in x86 (type punning)? Nothing special. In C? It’s undefined again, and the nasal demons are still out to get you.

16

u/[deleted] Jul 05 '24

I think GCC has a flag that stops it from removing integer wrapping. 

10

u/cdb_11 Jul 05 '24

-fwrapv for signed integer wrapping

-fsanitize-undefined-trap-on-error -fsanitize=null to trap on null pointer dereference in software, or -fno-delete-null-pointer-checks to disable optimizations around it

memcpy for type punning

→ More replies (4)
→ More replies (1)

7

u/jkrejcha3 Jul 05 '24 edited Jul 05 '24

What happens when you interpret data in different ways in x86 (type punning)? Nothing special. In C? It’s undefined again, and the nasal demons are still out to get you.

Wrt strict aliasing, strict aliasing rules are pretty widely seen as a unforced error on part of the standard. It's unfortunate that some compilers (gcc and clang) perform type-based alias analysis (TBAA) and has caused so many issues with C code, such that basically any non-trivial project either

  1. Does something like -fno-strict-aliasing

  2. Uses a compiler which doesn't do this

In fact, even LLVM, when compiled under gcc, disables strict aliasing. But in general, for real world code, it might as well not be a rule.

Do note: that it is legal in C to type pun via unions even with strict aliasing enabled.

→ More replies (8)

4

u/mpyne Jul 05 '24

The fact is C maps pretty closely to what you'd write in assembly.

Not as closely as one might think, and the difference often have significant implications, most notably security implications.

What happens when you overflow an integer in x86? It wraps around. What happens when you overflow a signed integer in C? It’s undefined, and has led to the compiler removing security checks, and subsequent vulnerabilities.

That's an odd objection though, as having signed integer overflow be undefined is precisely what allows C to consistently map "int b = int_val + 1" cleanly to INC ax without additional unnecessary checks across multiple potential hardware definitions of what should happen.

Some hardware might saturate, some might wraparound, but you'll get the instruction you'd have written in assembly.

If you want to enforce a view of the universe on the hardware you can (and should!) but that's not what C was trying to do. Maybe they should have been, but I don't think even Ken Thompson was tracking things like timing side channels back then.

→ More replies (1)
→ More replies (1)

10

u/[deleted] Jul 05 '24

It's like, words come pre equipped with specialised and inflexible meaning, and using them makes us believe that the world is neatly compartmentalized and orderly where A isn't even a little bit B and B and B are actually clones.

High level. Low level. True level. Computers do fundamentally 6 things (jump, compare, add, load, store, halt). It's all the same.

Where a language sits on the high low spectrum is more easily and consistently defined by the complexity of the compiler. Python has no compiler, and very simple rules. I'd say that makes it high.

→ More replies (1)

3

u/Slime0 Jul 06 '24

I can't imagine reading this article and having this takeaway. Maybe the headline is bad, but forget the words "low level" for a minute. The article makes a lot of good points about how the connection between a language and a processor could be a lot stronger than the connection is between C and the ideal fast modern-day processor, to the point where processor design is very much not ideal (because it's largely designed around making C fast) and C becomes a minefield of undefined behavior that's necessary for compilers to optimize for the processors. Both ends of this processor-to-language connection are hurting because C doesn't map well to what processors could be like if we prioritized potential speed over C compatibility.

The fact is C maps pretty closely to what you'd write in assembly

The article specifically addresses how this is not the case because of vectorization and other compiler optimizations, and also points out that the instruction set itself is designed the way it is because of C.

11

u/Uristqwerty Jul 05 '24 edited Jul 05 '24

If C were low-level, it'd have an if overflow(a += b) construct, exposing the flags the CPU is already setting as a side effect, rather than requiring the programmer to very carefully re-phrase their bounds tests to avoid UB, so that the optimizer doesn't delete safety-critical logic.

If C were low-level, it would expose the fact that the underlying multiplication instructions themselves tend to produce an output with twice the bit width of its inputs, including native 128-bit results split between an upper and lower register. Once more, a trivial way to detect overflow, turned into a footgun by the optimizer, and the cause of numerous vulnerabilities.

If C were low-level, you'd have a way to write (a + b)/2 using the carry flag as the n+1th bit. X86 can do it in two opcodes for unsigned types, using the rotate-through-carry instruction. Three, if you want it to round towards one of the inputs rather than 0, since you can compare then add-with-carry as a conditional increment merged into the add you'd be doing regardless. Slightly more complex for signed types, though. But the code you need to write instead, in order to stick within behaviour that C defines, is going to be substantially less readable than (a + b)/2. Ironically, since unsigned wrapping is defined behaviour, I don't think a compiler can even recognize your intent and give you the more-correct version that doesn't overflow.

Edit to conclude: You're not writing low-level code; low-level code straightforwardly represents its underlying logic. You're writing against the high-level abstract machine used by the optimizer, where if you do things too low-level, it gets flagged UB, and potentially gets the whole branch, even the whole function, even its callsites in turn marked as unreachable and so optimized out. The C you need to write in practice starts at a mid-level then dashes off in a non-euclidean direction full of madness.

2

u/dvmason May 02 '26

One of the reasons I like Zig is that there *is* an addWithOverflow operation: https://ziglang.org/documentation/master/#addWithOverflow (along with more explicit semantics than C: meaning better optimization).

→ More replies (1)
→ More replies (2)

5

u/nanotree Jul 05 '24

The level we are talking about in "low-level" is the level of abstraction away from machine code. There is no hard measurements for abstraction. And low-level is relative now to other languages as well. C's language spec is also super short compared to modern languages. And there probably was a time where C could be used to reference physical memory and not just virtual.

On another point, way back when, there was no such thing as pipelining or multitasking. There was no virtual memory. People programmed with punch cards. Is this guy suggesting that they weren't programming at a low level?

This guy just sounds like gate-keeping braggart.

21

u/StoicWeasle Jul 05 '24

Your take, and OP's take (or at least the article's take) are both lame.

  1. C is low-level compared to the HUGE NUMBER of high-level languages today, that look nothing like hardware, whether it's Javascript, or PHP, or bash, or SQL.

  2. C is not a low-level language compared with assembly.

There. All done. The article, and all the arguing being done here, is all pedantic nonsense. I can embed ASM inside of C. I cannot embed ASM inside of Javascript. In that context, C is low-level. To claim it's not is to claim that the Sun isn't large. True, compared with VY Canis Majoris, the Sun is but a tiny blip. But we don't live on a planet orbiting VY Canis Majoris, (whether or not we could is a while different ball of wax), so that's not the context that nearly all of us inhabit.

Most of us are not operating system people, let alone kernel-level, ring-0 people. There are maybe a few dozen to a few hundred of those people in the world. An air as rareified as, say, an NBA player. To NBA players, a dunk is "not hard"; in fact, it's "stupidly easy". To everyone else, a dunk is pretty damn hard.

In the exact same way that C is "pretty damn low-level".

22

u/jaskij Jul 05 '24

Most of us are not operating system people, let alone kernel-level, ring-0 people. There are maybe a few dozen to a few hundred of those people in the world

You forgot the embedded developers. Sure, few of us work on an RTOS, but there's still a shitload of firmware written that does not use one. And even with an RTOS, some of the concerns are the same.

I do agree that C is a low level language though.

3

u/barrows_arctic Jul 05 '24

Yeah and a decent amount of embedded work is also in assembly, or a combination of C and assembly. Bootloader work for instance.

5

u/jaskij Jul 05 '24

My current project is a bootloader for an STM33H743. All my code is C++, the libraries I use are a mix of C and C++, and by the end of it the whole thing will have maybe ten lines of inline assembly.

Assembly isn't really used any more, unless you absolutely have to, like in high volume applications where every cent of BOM matters.

→ More replies (10)
→ More replies (1)

15

u/[deleted] Jul 05 '24 edited Jul 05 '24

I write VHDL & System Verilog for SoCs and develop/maintain preemptive kernels for space bound systems. I also design avionics software that runs on top. There’s quite a few of us in industry that do this all over the world.

Embedded Systems Engineering or embedded focused CS curriculums do exist. To say “most of us are not operating systems people” seems a bit hyperbolic. I would agree that it may receive less attention than web and/or cloud development.

6

u/StoicWeasle Jul 05 '24

Hyperbolic?

What is the percentage of programmers, in your estimation, which work on embedded or OS stuff?

Does it approach 75%, or whatever you think “most” means? I’d say maybe 1 out of every 1000 programmers works on or close to the metal.

→ More replies (5)

10

u/case-o-nuts Jul 05 '24

C is not a low-level language compared with assembly.

You missed the point: C is low level compared to assembly

Assembly is high level compared to CPU internals.

You don't get to write low level code.

2

u/istarian Jul 06 '24

By that reasoning almost no one has written any low level code since the days of 8-bit computers.

→ More replies (1)
→ More replies (1)

2

u/vaporeng Jul 05 '24

Also, a lot of C is not run on speculative processors (embedded for example).

2

u/AxeLond Jul 05 '24

Also when the article goes on to say that modern hardware is held back by C language support. If C was a higher, abstract language then the hardware wouldn't have to so bound to the way C expects things to work.

2

u/TheRealUnrealDan Jul 05 '24

it's almost like backwards compatibility has been the king, and just ditching the 'old way' to create a 'new way' just makes a bigger mess -- something something and now there's two standards

1

u/wc3betterthansc2 5d ago

also C does deal with physical memory addressing if you're compiling an OS

1

u/ziplock9000 Jul 05 '24

None of that means it's not low level. Which it VERY much is.

-62

u/Alexander_Selkirk Jul 05 '24

The fact is C maps pretty closely to what you'd write in assembly.

Really?

https://godbolt.org/z/bha4o46ov

47

u/[deleted] Jul 05 '24

lol, mate you shared a link that had -O3 and thought it was making a point, come on

5

u/tjf314 Jul 05 '24

Compiler optimizations by definition don't change the semantics of your code. (And if they do, it's unsound and a compiler bug.)

-32

u/Alexander_Selkirk Jul 05 '24

So, you publish speed-critical software that does not use optimizations?

25

u/bananaboy319 Jul 05 '24

Well, you choose, either you optimize or the compiler does, u can't complain that the output is different if you ask the compiler to do it for you.

→ More replies (1)

15

u/[deleted] Jul 05 '24

Now you're changing the topic

→ More replies (2)

4

u/Konaber Jul 05 '24

Sure, if it's also safety critical.

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

6

u/tu_tu_tu Jul 05 '24 edited Jul 05 '24

Totally. Compiler made exactly the same code that I'd expect to get from him. That's one of the main points of using C today: it produces predictable code.

→ More replies (1)

16

u/SN0WFAKER Jul 05 '24

Yes really. It's a different language, but you are basically doing the same thing.

→ More replies (2)

8

u/not-my-walrus Jul 05 '24

I feel like a lot of commenters didn't understand the point of the article. It's not just that there are differences between the C abstract machine and modern hardware --- it's that the requirement for hardware to emulate the C abstract machine caused issues and restrictions that may not have existed otherwise.

There are a lot of interesting ideas that can't really be / are a lot more difficult to express in hardware because they don't exist in C. Some examples:

  • message passing instead of shared memory (could simplify a lot of cache control)

  • pointer metadata

  • exposed parallel execution (not SIMD, a way for the ISA to specify "execute these two separate instructions simultaneously")

  • mutability at the CPU level (could be used to implement something similar to Rust's borrow checker in hardware)

This isn't to say that we should burn our hardware specifications to the ground every decade just because we can --- hardware interface standardization is undeniably useful, but it is also limiting.

2

u/Alexander_Selkirk Jul 05 '24 edited Jul 05 '24

Good points.

One thing is also that hardware evolution is incredibly path dependent from a software perspective.

  • Software: SNOBOL was not a good idea? We use different languages today, who cares. SNOBOL was introduced in 1962, and only software archeologists remember the name. It has long been replaced by awk, Tcl, and Lua.

  • Hardware: You want to connect to your brand new embedded smart meter via USB? Uh, you need to use a chip that wraps RS232 and tunnels it over USB, since we can't redesign the hardware and protocols. Oh, and don't forget to set the right baud rate. RS-232 was introduced in 1960, so it is older than SNOBOL.

2

u/cdb_11 Jul 05 '24

message passing instead of shared memory (could simplify a lot of cache control)

I'd like to remind that concurrency support in C (stdatomic.h, threads.h, pthread.h) is optional. And that even with the current C memory model data races (ie. violating the rule that data has to be either immutable or modified by one thread) is undefined behavior. You could do message passing and still be fully compliant with the standard. And in fact this is how C programs used to be written, by forking and talking over pipes. If you are okay with breaking or making slower existing multithreaded C, C++, Rust and Java programs, you can do what the article is proposing and still be fully compliant with the C standard. So this is hardly C's fault, people will just use what the hardware does best.

pointer metadata

The author of the article is involved in CHERI that does just that, makes pointers 128-bit and stores metadata inside them. And if I remember correctly, according to him they can run most C programs just fine, or with very minimal changes.

exposed parallel execution (not SIMD, a way for the ISA to specify "execute these two separate instructions simultaneously")

You can already do that, by not having data dependency chains. If two instructions don't depend on each others results, they will be executed in parallel. But regardless, you can do what you're saying without breaking anything. I believe this exact thing was tried in Itanium with each instruction doing three things at once, and it didn't really work out.

mutability at the CPU level (could be used to implement something similar to Rust's borrow checker in hardware)

Again, CHERI does something like that on C and C++ programs. As far as I know it's not enforcing that there has to be always a single mutable reference or multiple immutable ones, but it is tracking the bounds and lifetime.

37

u/ResidentAppointment5 Jul 05 '24 edited Jul 05 '24

There's a lot of good, valuable discussion in this thread about the fact that all we need do is look around the landscape to see hundreds of languages that, along many dimensions we can name, are vastly "higher-level" than C, and this seems to support an argument that C is a low-level language.

I'm old enough, though, to remember when the definition of "low-level language" was much more straightforward: it essentially meant that, given any piece of source code in the language, you, a human programmer, who knew the CPU you were targeting, could predict the assembly language the compiler would generate, and that the language and standard library provided direct access to the underlying hardware.

C was noteworthy at the time for satisfying these criteria when essentially no other language did. Even the closest competitor, Pascal, didn't satisfy the second criterion—for example, whatever I/O facilities weren't provided by the libraries for your particular implementation effectively didn't exist. This is why C has always been considered a "systems programming language" and Pascal hasn't.

At the time, this was effectively the end of the story. Over time, more sophisticated compilers started adding "peephole optimizations," or optimizations that only applied to a contiguous block of code at a time and were still processor-independent, like constant-subexpression elimination or loop-unrolling. Then things started getting weird, like processors introducing instruction caches and suddenly that loop-unrolling "optimization" became "pessimization" because the unrolled loop no longer fit in the instruction cache.

It just gets massively worse from there. The idea anyone can read any non-trivial C snippet and predict what assembly instructions it will generate, for any set of compiler flags and any processor designed in the last couple of decades, is a sick joke.

What to take from the observation, though, is far from clear. Does this mean we shouldn't care about C, C++, Rust, Go, Zig, Crystal, Odin, Virgil...? That seems obviously ridiculous. It seems much more reasonable to think we need a more modern, more expansive definition of "low-level" that probably encompasses all of these languages, and some I'm forgetting.

On the other hand, though, I can see an argument that there are no low-level languages in the original sense. It's fairly well-understood at this point, for example, that human code optimization by hand-writing assembly language is a losing game: you will literally never do a better job than a modern compiler will. The combinatorial explosion of things you have to understand, and understand in combination, prevents human beings from doing as good a job as a computer can.

So I suppose my takeaway is: yeah, C is a "low-level language" in a very important sense, but the idea that it's a particularly good model of "how computers work" (there's one instruction pointer marching linearly through memory, the CPU dispatches work based on the instruction at that pointer, "memory" is one big linear block of stuff immediately accessible by address, etc.) is dramatically false, and has been for decades.

7

u/Ty-McFly Jul 05 '24

This is well put. Given the current landscape, we may as well just not have the "high level/low level" scale at all if it's effectively reduced to "assembly/not assembly". Why shouldn't the scale describe the gradient of languages out there? Because we have to appease some draconian gods that demand we adhere to the definition as it applied to the programming landscape at the time? I don't think so.

7

u/blackrossy Jul 05 '24

You use assembly as lowest level reference, the author uses the actual code execution as reference

The point is not that there are abstractions between assembly and C, but C and hardware execution.

5

u/ResidentAppointment5 Jul 05 '24

Fair point! So maybe one way to interpret “there are no low-level languages” is “there is no 1:1 mapping from CPU instruction to cost model or even memory access model,” which is true, but bumps into the “what should we make of this?” question.

2

u/blackrossy Jul 06 '24

Its very hard to say. Also its fair to mention that its the platform that holds the abstractions. There are enough architectures to which the assembly would map 1 to 1 to execution.

I do FPGA design for a living and have always wondered(but not bothered to research) the complexity of code execution on modern processors. The authors claim regarding the 180 instructions concurrently in flight really made me raise my eyebrows lol.

My guy feeling says that C is a low level programming language, anything below assembly is just not programming anymore.

92

u/Ok-Craft-9865 Jul 05 '24

Assembly isn't low level either. Binary is too damn high as well!.. Real coders go straight to electrical signals.. even better if you can go into manipulating the atoms in resistors.

49

u/Rockola_HEL Jul 05 '24

I was hit with this epiphany when writing C code for a SIMD machine. Wasn't a good fit.

27

u/aanzeijar Jul 05 '24

I first encountered it for overflow checks. Most CPUs have had a flag register that gives you overflow information for 30 years, but overflow checks in C are a minefield of undefined behaviour and ckd_add/sub/mul were only just added in C23.

11

u/Enip0 Jul 05 '24

I've never written simd code, do we have any languages that are better suited for it, or is the current situation that everything is just as bad?

4

u/krapht Jul 05 '24

Ispc is good.

41

u/cediddi Jul 05 '24

Maybe we shouldn't deal in absolutes. C is a higher level language than assembly, lower level language than visual basic.

51

u/bighi Jul 05 '24

Saying that C isn’t low level because it isn’t the lowest possible level a language could be is the equivalent of saying the second poorest person in the country isn’t poor because they aren’t at the lowest possible level.

Which is nonsense in any kind of discussion.

5

u/matjoeman Jul 05 '24

This is just semantic quibbling. The point of the article is that C and even assembly don't give the programmer or compiler control of a lot of what the CPU is doing, and the author discusses some other ways CPUs could be designed to allow that.

3

u/istarian Jul 06 '24

At the same time, many higher level languages don't exactly expose you to anything about what's going on.

Java and the JVM hide away all kinds of stuff of stuff. You are programming for the JVM and nothing else.

C could still be considered lower level because it exposes it's abstract model to the programmer.

So even though C's model of the computer no longer really represents the true hardware, it also doesn't add a bunch of additional layers.

1

u/bighi Jul 06 '24

That may be the point of the article, but I’m criticizing the title.

If the title was “C doesn’t give you total control over the CPU” I wouldn’t be criticizing it.

4

u/grandpa5000 Jul 05 '24

had to do pdp-11 assembly in college fun times

22

u/RiftHunter4 Jul 05 '24

C is low-level because it's strictly typed, has no garbage collection, and needs semi-colons. /s

19

u/jaskij Jul 05 '24

C being strictly typed is arguable. Statically typed, yes, absolutely. But it's not very strict about those types.

1

u/dontyougetsoupedyet Jul 05 '24 edited Jul 05 '24

Arguable by people who don't understand anything about type theories, yes, absolutely.

C is in fact "very strict about those types." Your variables don't change type in C, ever. You can use an existing variable to produce a new value of a different type using a cast. There are explicitly defined conditions under which that process is implicitly undertaken by the compiler on your behalf. That doesn't make the language not be strictly typed.

The same people making that nonsense assertion also tend to assert crap like "C does not have a type system," while in fact the type calculus used by C both has types and even implicitly requires subtypes.

/u/Noxitu is running into those values being converted on your behalf because you're supposed to actually know the C programming language when using the C programming language, that isn't some "gotcha." If you don't want that behavior, use -Wconversion to have your compiler treat you like an infant that doesn't know the programming language. You not knowing the semantics of the language is not synonymous with the language not having strict typing. You can hit casts under explicitly defined conditions, you not knowing those conditions is not the same as the language not caring about types!

/u/IAMARedPanda's drivel is the dumbest type of argument. C is not lacking strict typing because you make use of a zero sized type that is explicitly defined to allow you to opt out of strict typing rules. The crap about ints is the same crap Noxitu claims, which runs directly into warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion] when you tell the compiler you don't know what you are doing. IAMARedPanda not knowing the semantics of the C programming language is not synonymous with the C programming language lacking strict typing. Now we're just waiting on the trifecta of some know nothing neckbeard making the claim that C lacks a type system and we'll be able to scream "bingo."

4

u/Noxitu Jul 05 '24

You seem to be understanding "strict typing" as "static typing" (because what else could "your variables don't change type" mean), but there is no common agreement that those mean the same thing. Nor agreement what "strictly", or even "strongly" means in context of types.

And the fact is that C type rules are not very strict. Not that C isn't strict about its type rules - the rules themself aren't strict. You have a function taking an int, but you have a float? Sure thing. Your function takes a pointer to type X, but you provide it pointer to not related type Y? Sure, it is a pointer after all.

2

u/IAMARedPanda Jul 05 '24

C is not strict about types. For example void ponters being implicitly cast.

    int a = 10;
    void *ptr = &a;
    int *intPtr = ptr;  // Implicit conversion from void* to int*
    printf("%d\n", *intPtr);

Notably you can assign different numeric types without issue i.e. this would not compile in C++.

double a = 32.5678;
int b = a;

20

u/loup-vaillant Jul 05 '24

it's strictly typed

Did you mean statically typed? Because in practice its typing discipline isn’t very strict.

We could say failing to strictly following its typing rules can lead to critical vulnerabilities and lives lost, though.

7

u/Godd2 Jul 05 '24

No, it's a brand new dimension of typing!

strong <--> weak

static <--> dynamic

strict <--> lenient

3

u/Kjufka Jul 05 '24

Everything is strictly char*

1

u/Infrared-77 Jul 05 '24

“..and needs semicolons” 😂 Python programmers about to start seething when they find out what language powers Python

8

u/noodle-face Jul 05 '24

In write UEFI BIOS and id argue aside from assembly it's the lowest you can go. Sometimes we even write assembly within C.

I feel like this dude wrote this article to sound cool

3

u/matjoeman Jul 05 '24

Did you read the article? The author is talking about how much of what the CPU does is no longer exposed in the ISA, and that C is a closer mapping to older CPU architectures than modern ones.

2

u/elsharkawym Jul 06 '24

Excuse me, but what knowledge or topics of CS should I study to grasp and fully understand the concepts that this article covers?

I am a self-taught computer science student and currently, I am studying DS and Algorithms.

Thank you so much in advance

4

u/[deleted] Jul 05 '24

This is a lame article and such an inaccurate read.

2

u/matjoeman Jul 05 '24

What is inaccurate?

3

u/dontyougetsoupedyet Jul 05 '24

It's not a very good article. A lot of people want to blame the C programming language for crap completely out of its control, like what choices are made by processor manufacturers. Everything popular pretends the environment is like a PDP 11 because that's what userspace users want. Every single time alternatives are manufactured, like Cell processors as an example, programmers absolutely lose their shit and complain constantly: so manufacturers tend not to. It's not mysterious, and it damn sure isn't on the shoulders of C to hold the weight or the blame. It's not "c programmers," it's literally almost everyone. Almost no one wants to learn to program for new architectures, in any language: when basic changes in high level code lead to new types of grievances, like new types of pipeline stalls or bus stalls, people get pissed off. Then they get vocal, then the new hardware becomes a meme, then they become a hard sell, then manufacturers stop trying to sell those new things.

2

u/LeCrushinator Jul 05 '24 edited Jul 05 '24

So by his standards is Assembly the only low-level language? Which leaves basically everything else to be high-level? I disagree.

I am curious what “levels” most people think are out there though. Maybe there’s something higher than assembly but lower than most others that C could fall into.

11

u/cdb_11 Jul 05 '24

By his standard assembly is not a low-level language either. I think that's the point of the article.

2

u/JaggedMetalOs Jul 05 '24

The root cause of the Spectre and Meltdown vulnerabilities was that processor architects were trying to build not just fast processors, but fast processors that expose the same abstract machine as a PDP-11. 

What?? I'm pretty sure the root cause of the Spectre and Meltdown vulnerabilities was that processors just can't wait for memory any more so have to execute based on speculative values.

5

u/genericallyloud Jul 05 '24

Yes, the speculation being caused by processors wanting to go faster while still supporting an abstract machine like a PDP-11. The presumption of the author is that if it wasn’t trying to support C better and just be fast, it would be doing more parallel work instead of more speculation.

1

u/matjoeman Jul 05 '24

Yes that is what the author is saying. The author is saying that if the ISA gave control over cache storage or was otherwise designed for parallelism differently then these vulnerabilities wouldn't have happened.

3

u/JaggedMetalOs Jul 05 '24

Is there anything beyond just suggesting everything to be SMT multithreaded and have some kind of software control over cache?

Pretty sure you can't just click your fingers and make every kind of task be suitable for multithreading just by writing things in something other than C. If it was possible surely people would already be doing that given how many cores modern CPUs have.

(Also conveniently ignoring all the SMT related vulnerabilities ;)

I also can't see why there couldn't be explicit cache control added to C either. Beyond there just not being a good way to allow control of cache in any language when there is so much variation of cache setup across even all the different chip SKUs of a single manufacturer let alone across different manufacturers and architectures.

-1

u/Alexander_Selkirk Jul 05 '24

This is an article which explains why C is not really "close to the machine" as is the common argument why C is better for performance.

This was posted some years ago. I re-posted this as an companion link to a suite of microbenchmarks comparing Rust and C, where Rust wins (and I think exactly for the reasons described in the OP article by David Chisnall).

15

u/cdb_11 Jul 05 '24

Rust doesn't let you get any lower than C, because assembly itself isn't lower than C by that much if you trust what the article says. And those benchmarks show that C solution is faster (C++ really, but it's not relying on any of the C++ features). Contrary to what your title says, the fastest C solution doesn't have any inline assembly whatsoever, it just uses SIMD intrinsics. And the Rust solution basically rolls out its own SIMD type and lets the auto-vectorizer transform it to appropriate instructions, which really isn't that much different from using intrinsics. And auto-vectorization works in C too obviously, so you could do the same thing there. But none of it matters, because C, C++ and Rust (and x86, ARM assembly) are all sufficiently low level to let you to exploit how the modern hardware works, instead of forcing you into some inefficient high level abstractions. And even if you stumble on a problem where the compiler isn't generating the code you wanted, you can likely work around it, for example by using restrict in C or unsafe in Rust.

-6

u/Alexander_Selkirk Jul 05 '24

Well, the argument was not that Rust is low-level.

9

u/cdb_11 Jul 05 '24 edited Jul 05 '24

It's my argument. All fastest Rust solutions are not what you'd say is an idiomatic, obvious, high-level code. All of them consciously rely on the low-level knowledge of the target platform, and define vector types that can be mapped directly into ymm/zmm registers. Just like when even in assembly you can use the knowledge on how branch prediction, pipelining and caches on your hardware works, despite having little to no control over it. And in some other higher-level languages you simply can't do that, because you don't even have structs or static typing. Or you have to wait for JIT compiler, which isn't free either.

You can't simply learn Rust and then write code like those benchmarks. You need to also understand your compiler, generated assembly and your processor. And this is how you write optimal programs. Not by "picking Rust over C, because I've seen some benchmarks where it's faster". The choice of language is less relevant on this level, if the question is "how fast can it go" and you ignore things beyond pure performance, like ergonomics etc.

1

u/vinciblechunk Jul 05 '24

Counterpoint: Pretty much every modern processor is a faster, wider PDP-11 because that's what the market wanted and C's unhealthy obsession with UB is why everyone hates it now

1

u/aqjo Jul 05 '24

Unless you’re configuring FPGAs, you’re just a high level poser.

1

u/ChrisOz Jul 05 '24

My soldering iron and transistors laugh at your high level FPGA.

1

u/ub3rh4x0rz Jul 06 '24 edited Jul 06 '24

The rhetoric of this seems to be boiled down to, "C weenies are wrong to think they're working in a low level language, or they're wrong that low level means close to the metal, so everyone should be writing checks notes erlang, because don't you know parallelism is so easy, a bunch of dumb kids can do it! Oh and c's problems definitely have nothing to do with the baggage that comes with being the most widely used PL in history, used consistently for several decades. No, its procedural code! Nevermind that I'm advocating for sophisticated compilers, C's sophisticated compilers arent allowed because those weenies think they're simple."

Maybe the qualities people associate with low level languages aren't about being close to the actual metal it runs on, but about being close to something that was close to the actual metal in a simpler time. C is written for a comprehensible concrete turing machine, not something you have to be a hardware nerd to build a mental model for, nor something that is abstracted away from the concept of manipulating a turing machine, like the haskell compiler.

Idk this sounds like a rant uppity junior me would have made if I had a few more degrees at the time, and it would have been just as wrong.

1

u/VisibleSmell3327 Jul 05 '24

The "level" line moves all the time. I've seen C argued as high and low.

The actual logic is voltage levels, so let's agree that bytecode up is high level and be done.

0

u/lt_Matthew Jul 05 '24

C is high level to people that only know Basic and Assembly

2

u/istarian Jul 06 '24

BASIC is simple from the programmer's perspective, but that doesn't necessarily make it low-level.

Depending on the computer hardware and your BASIC implementation it could be very high level or fairly low level.

0

u/Alexander_Selkirk Jul 05 '24 edited Jul 05 '24

At the time that I was at "high school", BASIC was standard (few other stuff would run on microcomputers with 16 or 32 KiB of memory) and Pascal and C were new. From today's perspective, they were relatively comparable; both had quite fast compilers, which mattered at that time. C was better for bit-fiddling, and Pascal was, like later Oberon, better for correctness. And we see that correctness becomes more important.

C mostly won because it could self-host Unix on 16-bit machines, like the PDP-11. But before GNU and Linux, you would not get a standard, free, not prohibitively expensive system for hobby users. And you needed a kind of MMU for Unix, which was not available until the Intel 80386 CPU.

C was the right fit for systems which were incredibly limited by todays standards. Like the saying goes, a modern smart tootbrush hss more computing power than the Apollo 9 board computer had.

0

u/madogson Jul 05 '24

"It's not low level unless you are physically manipulating the transistors directly"

-This guy probably

0

u/[deleted] Jul 05 '24

[deleted]

1

u/Alexander_Selkirk Jul 05 '24 edited Jul 05 '24

PC bioses can run Minix or Forth. You never see them, but via Intel System Management Mode or "Ring -2", they can interrupt your CPU at any time.

Smartphones run like five or more interconnected comouters. The part that runs Android or whatever is just one if them. And it has several unequal CPU cores plus a DSP core, like TMS320C40, which runs a signal processor - with an own "runtime".

1

u/_SloppyJose_ Jul 05 '24

PC bioses can run Minix or Forth. You never see them, but via Intel System Management Mode or "Ring -2", they can interrupt your CPU at any time.

Forth? The only Forth I know is the programming language, are you talking about something different?

Anyway, yeah, maybe a decade ago Reddit/Slashdot/others discovered that modern hardware has this additional ghost layer and got upset that the NSA would use it to spy on everyone. But then everyone seemed to forget about it.

0

u/Alexander_Selkirk Jul 05 '24 edited Jul 05 '24

Yes, the language FORTH.

The capabilities of our three-letter agencies do not matter that much as long as these are controlled by strongly democratic goverments. Iff that is no longer a given, they matter a lot.

1

u/_SloppyJose_ Jul 06 '24

Yes, the language FORTH.

Do you have a link? Google searches aren't turning up anything related.

1

u/istarian Jul 06 '24

I don't see how it's more a VM than any other machine which uses microcode.

The moment the assembly language instructions cease to select a unique, dedicated hardware path there is inevitably some abstraction going on.

By comparison to speculative execution, caching is trivial and represents little more than a bit more work getting data from point A to point B.

18

u/[deleted] Jul 05 '24

[removed] — view removed comment

11

u/IDatedSuccubi Jul 05 '24

We were taught that anything that is made for a specific machine architecture (so assembly, vendor-specific shader languages etc) is low level, because it is always in the context of target hardware, and anything portable (like C) is high level

3

u/LagT_T Jul 05 '24

I've been programming since early 00's and it was considered a low level language back then. We had python 2, C#, java, and php ruled the web. 15 years ago was 2010 dude.

1

u/thesuperbob Jul 05 '24

By some definitions I've encountered, assembly is not considered low-level either. IIRC it's because it also abstracts some aspects of what's actually being done during compilation, even if compared to C it's a less confusing transformation.

I mean on a scale of low-level to no-code, Assembly in nearly as low level as you can get, since AFAIK there isn't a lot of options to pick from when translating a particular instructon to a machine opcode, and C wouldn't be that far from assembly on that scale. C++ is still in that neighbourhood, since you can opt-out from some of more abstract features, and "higher-level" stuff like v-tables isn't even close to levels magic you see in interpretted languages.

Ultimately even writing machine opcodes isn't guarenteed to evoke a particular behavior from a modern CPU, since there's just too much magic going on in there these days, especially if you're running in user space on a modern multi tasking OS with virtualization. It's been a while since code was directly tracable to what hardware actually does. Today best we can do is run benchmarks and profilers, experiment until the program does what we want it to do.

0

u/[deleted] Jul 05 '24

[deleted]

3

u/Alexander_Selkirk Jul 05 '24

These days, you can run C programs in real-time scheduling on Linux as root, and your latencies might be hugely influenced by system management interrupts which are processed in the BIOS. Whether you run C or something else can be far less important than that.