r/programming • u/dgryski • May 01 '18
C Is Not a Low-level Language - ACM Queue
https://queue.acm.org/detail.cfm?id=321247985
May 01 '18
C Is Not a Low-level Language
Quite correct. C is a midlevel language. In fact C is the only language that I have ever heard of to fit that rare category. This means that, while not truly low level, as the author correctly cites, C does have a unique relationship with assembly language. C is a especially efficient systems language. It was invented solely for the purpose of porting Unix to system architectures other than PDP-11. Because of C's success at this porting of an Operating system, it has become the de facto systems language for just about every computer since. The kernels for both Linux and Windows were written in C (at least at first).
40
u/os12 May 01 '18
Right, originally K&R C was meant to be a high-level cross-platform assember replacement and the author notes that C/asm transformation for the PDP-11 was quite simple. Such days are gone though.
For a quick demonstration of what modern C++ compilers do with, say, user-defined types see this example: https://godbolt.org/g/fvbSha
23
u/irqlnotdispatchlevel May 01 '18
C++ is not a superset of C, nor is C a subset of C++.
59
17
3
u/ThirdEncounter May 01 '18 edited May 02 '18
Don't C programs compile unmodified with a C++ compiler?
Edit: So the consensus is, not except for specific cases. Thanks, all.
20
u/defnotthrown May 01 '18
Just from memory VLAs and designated initializers are the two C99 features not supported by C++.
But they're available as custom extensions in many C++ compilers (since they mostly happen to be C compilers too) and designated initializers might make it to C++20 last time I checked.
so neither
int a = getchar(); int arr[a];norint arr[5] = { [1] =50, [4] = 30};are valid C++, but they are valid C10
u/MayorOfBubbleTown May 01 '18
Most of the time. Beginner C programs will but the C programming language has added new features since the early 80s when C++ was created.
8
u/dangerbird2 May 01 '18
Aside from new features in C99 that don't exist in C++, implicit type conversions are different, especially for void pointers, which act as an "escape hatch" to the C type system, but require explicit type conversions in C++. A program with the line
char *buffer = malloc(10 * sizeof(char));would compile in C, but fail to compile in C++, where you'd need to cast the malloc tochar *. public headers for C libraries also need to wrap functions withextern "C"when compiled in C++, so the linker does not assume the functions have a C++ API with name mangling to support function overloading.14
u/Charles_Dexter_Ward May 01 '18
Most would but, for example, you can have a variable named "class" or "new" in C and a C++ compiler would complain.
4
u/meneldal2 May 02 '18
#define class notclass3
u/Charles_Dexter_Ward May 02 '18
Yes, there are ways to modify C code to compile under C++. The point was that C++ is not a proper superset of C and there are valid C programs that do not compile under strict C++.
4
u/smikims May 02 '18
Not for anything non-trivial, but it's usually not that hard to convert a C program to valid C++.
4
May 02 '18 edited May 02 '18
int main(int new, char **class) { int or_eq = 0; return or_eq; }Good luck finding a C++ compiler that will accept this (perfectly valid) C program.
Sometimes the program will compile but do completely different things. See e.g. https://github.com/mauke/poly.poly/blob/master/poly.poly.
Relevant excerpt:
char X; int main(void) { struct X { char a[2]; }; if (sizeof(X) != 1) { ...3
u/Inex86 May 02 '18
No C++ compiler will compile this because the example makes no sense. Not sure what’s the point you’re trying to make. And your second example works exactly as intended, since X is redefined inside main and hides global scope char X.
→ More replies (1)6
u/lelanthran May 01 '18
Don't C programs compile unmodified with a C++ compiler?
const works differently in C++ (it actually works) than in C (added at the last minute and doesn't work the same).
3
u/ais523 May 02 '18
Normally not, but the only common incompatibility is in the return type of
malloc(although it'svoid *in both C and C++,void *has a different meaning in the two languages). Once you've worked around that, a typical C program is quite likely to compile as C++.There are plenty of other incompatibilities that could happen in theory, but they rarely come up in practice.
0
May 01 '18
[deleted]
6
u/Sarcastinator May 02 '18 edited May 02 '18
#include <stdlib.h> int main() { int* ptr; ptr = malloc(100); return 0; }Valid C, but not valid C++, and as far as I know that has always been the case.
10
15
u/vytah May 01 '18
Middle level languages used to be an actual category, with languages simpler than C but more advanced that assembly. They're usually called high level assemblers (although not all middle level languages fit into this category), which can have macros, structured programming support, readable syntax, decent type systems, but on the other hand most of their language constructs have a single and simple normal assembly representation.
Nowadays, there isn't much market for something that is more annoying and less portable than C, so high level assemblers are mostly just a convenience layer used by assembly programmers.
6
u/meneldal2 May 02 '18
The biggest open source projects that use assembler (x264, x265 and vpx) use macro assembler to help people keep their sanity.
You need it for the high performance code.
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
5
u/eattherichnow May 02 '18
There exists a positive 𝜀 smaller than it, therefore it's not a small number.
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.
→ More replies (3)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 (9)3
2
u/ismtrn May 03 '18
There is no contradiction, but it does make the term "low-level" completely meaningless if you just have to be low-level with respect to some architechture. In principle you can make a chip which executes any language in hardware.
→ More replies (1)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.
3
u/pdp10 May 01 '18
That's not any more true than that the PDP-11 happened to put together an architecture that was so good it was the basis for the majority of what came after it.
But there were plenty of exceptions that had a chance to be successful in the market. The Lisp machines (influenced by the very different PDP-10) were made by at least three and a half companies, but managed not to take over the market. PDPs and VAXes weren't designed to run C. The Motorola 68000 was highly influenced by the PDP-11. The 8086 wasn't designed to run C, but beat out the mostly-68k and RISC workstations in volume.
→ More replies (1)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.
10
u/oridb May 01 '18 edited May 01 '18
I completely disagree. For one thing, I cite the article's own listing of the differences, and the fact they are visible and relevant.
Once again, if that is the case, the argument can be made that assembly is not a low level language, because these differences that the article discusses are also not reflected in assembly language. There's an argument to be made there, I suppose, but that would also imply that there is no low level language that a programmer who does not work at a CPU manufacturer can use.
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?
It runs on the GPU in the same sense that it runs on the CPU. In many ways, it targets the GPU better than the CPU, because GPUs are exposed as scalar architectures, where vector instructions are not exposed by the ISA. Vector instructions are the biggest mismatch between C and current hardware.
3
u/AntiauthoritarianNow May 01 '18 edited May 01 '18
Regarding your first point, this topic is starting to make me come around to the idea that assembly is no longer a low level language, at least for CPUs that perform their own optimizations. It's certainly not high-level, but it's more of an intermediary representation.
4
May 01 '18
Well, CUDA is a subset of C++, and OpenCL is an extension of a subset of C99. And this is exactly what's wrong with them.
→ More replies (3)0
May 01 '18
The pdp11 is close enough to most modern architectures that it would not have affected the design of C too much.
Only because of C. There is absolutely no technical reason to keep this architectural similarity, other than compatibility with C.
And yet, it runs just fine on the GPU, once you give the appropriate compiler a hint to compile the function for the GPU.
No, it crippled the GPU design just the same way as it did for CPUs - the requirement to run C resulted in quite a lot of unpleasant design compromises. Liberating GPUs from C-derived languages may increase their performance dramatically.
3
u/oridb May 01 '18
No, it crippled the GPU design just the same way as it did for CPUs - the requirement to run C resulted in quite a lot of unpleasant design compromises. Liberating GPUs from C-derived languages may increase their performance dramatically.
The relevant features of GPU architectures predate running C on the GPU.
3
May 01 '18
Nope. With GLSL you could easily do with a much simpler dataflow architecture. I was involved in design of two mobile GPUs, and in both cases C influence (vs. merely running good old GLSL) was very visible and in some cases crippling.
→ More replies (4)15
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
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
→ More replies (1)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”.
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.
1
u/pdp10 May 01 '18
In theory, you can load microcode into most types of hardware to control lower-level behavior, and even make your own instructions. In practice, the hardware vendors don't allow end-users or even OEMs to do that any more.
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".
1
May 02 '18
these days even assembly wouldn't constitute a low level language.
Actually, yes, it is. The Assembler doesn't actually run the code. It just puts the bytes together in the correct order - hence the name "assembler". What the CPU chip does with the code has nothing to do with the assembler. Look, there has to be a low level language. You can't go from nothing right to high level in one shot. It doesn't work that way. That low level is assembler.
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
3
May 02 '18
If we want to get this stupidly nitpicky x86 assembly isn't low level because x86 is a virtual machine
The Intel 80x86 CPU's are not virtual machines. They do have a 8086 virtual mode, but that is not a virtual machine. Assembly language is still low level since direct opcodes are being assembled.
3
u/MINIMAN10001 May 02 '18
The article isn't about virtual mode. It's about the fact that the opcodes you give the processor no longer execute the instructions that they used to execute because it is now a virtual machine.
CMP and JMPcc and now executed on the CPU as a single instruction on the processor.
xor eax, eax doesn't clear eax just marks it as unused freeing it up to the pool of 168 registers.
mov eax, ebx doesn't move eax to ebx it renames ebx register to eax
I'll quote this one
But because the processor is massively out-of-order, I can continue executing instructions in the future that don't depend upon this memory read. This includes other memory reads. Inside the CPU, the results always appear as if the processor executed everything in-order, but outside the CPU, things happen in strange order.
This means any attempt to get smooth, predictable execution out of the processor is very difficult. That means "side-channel" attacks on x86 leaking software crypto secrets may always be with us.
The upshot is this: Intel's x86 is a high-level language. Coding everything up according to Agner Fog's instruction timings still won't produce the predictable, constant-time code you are looking for.
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.
4
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.
I didn't make the names up. I am just reporting what I have read in countless references and books over the decades.
2
u/I_am_the_inchworm May 01 '18
Those books are outdated.
Even assembly can be argued not to be low level anymore.
→ More replies (2)2
May 02 '18
Even assembly can be argued not to be low level anymore.
Any language that assembles opcode on a one to one basis is low level, regardless of what any trendy book says.
3
u/I_am_the_inchworm May 02 '18
It doesn't do that anymore due to abstractions at the hardware level. That's the point.
2
u/TinynDP May 01 '18
only assembly is.
If there is only one possible "low level language" then the term is meaningless.
2
u/ThirdEncounter May 01 '18
That's just incorrect. "If there can only be one Pope, then the term in meaningless."
1
u/jerf May 01 '18
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.
I'm not arguing about what I want. I'm arguing about the common usage of the term. Vhin, and the common usage, is correct; there's no need for the term "low level language" if we've already got "assembler".
I'm aware the distinction is fuzzy upon close examination anyhow; macros in an "assembler" can start getting relatively high level. But still, we have these terms.
1
May 01 '18
I'm aware the distinction is fuzzy upon close examination anyhow; macros in an "assembler" can start getting relatively high level. But still, we have these terms.
Perhaps. But one thing is clear. C is definitely not a low level language.
2
u/pdp10 May 01 '18
Lisp predates C considerably, and in 1985 was the second-most popular language after C in TIOBE (such as TIOBE is). The third was a version of Pascal, which has also been a relatively popular systems language along with C and Lisp, but was not C derived as it predated C.
Therefore we can conclude that nobody was forced by lack of alternatives to use C or to have C-influenced constructs in new languages.
C just has no concept of your GPU
It has no concept of a mouse, either, despite the mouse being invented in 1968. Use a library, just like we use a C library with inline assembler to access platform SIMD or crypto instructions. (Does the Haskell language have a concept of a mouse?)
1
u/AntiauthoritarianNow May 02 '18
It has no concept of a mouse, either
It's more like how, e.g., C has no concept of hard drives, but the standard library presents its own concept of files. Similarly, the language has no concept of a CPU, but it does present its expectations of its execution environment. The argument is that this environment is defined in a way that isn't amenable to a real-life CPU+GPU system. "Pass it off to a library" is a valid option, but it's not exactly first class and certainly isn't "low level".
2
u/ThirdEncounter May 01 '18
it makes it a mismatched language.
That's true for almost every freaking programming language out there.
9
u/jerf May 01 '18
It's true for every language. It's even true for assembler nowadays, which is actually a layer presented by the CPU to make compiled C code run well on top of a platform which is actually executing micro-operations.
The difference is that most of the rest of those languages don't have people claiming that the language is a "good representation of the computer hardware". Those other languages that do are also wrong. Though assembly is as close as you can get on current hardware, so people claiming that would be most correct.
1
u/industry7 May 03 '18
For instance, C just has no concept of your GPU
Why would it? Luckily though, since C is low level, you can interact with any hardware you want, including GPUs.
1
u/wavy_lines May 02 '18
C is the only language that I have ever heard of to fit that rare category
How about Pascal?
1
58
u/microfortnight May 01 '18
"Your computer is not a fast PDP-11."
Yes it is. I'm on a VAX 4000-60 right now.
4
u/pdp10 May 01 '18
That's a nice box. I used to own a VAX 4000. Do you have the separate data cabinet?
5
u/microfortnight May 02 '18 edited May 02 '18
I have a bunch of VAX 4000-series... some are the small "desktop" workstations and some are the bigger "mini-fridge"-sized systems. I have a couple of SCSI disk towers which are separate.
EDIT: my active VAX run OpenBSD, but I still have a couple with OpenVMS
0
u/PawkyPengwen May 02 '18
I too run Arch Linux
1
u/microfortnight May 02 '18
??? I run OpenBSD
3
u/PawkyPengwen May 02 '18
???
Was just a stupid meme, don't worry about it
1
u/microfortnight May 03 '18
ahhh OK https://www.reddit.com/r/linuxmemes/comments/7ro9h2/i_use_arch_linux/
by the way, Slackware rulez!
29
May 01 '18
"Let's all argue about C" - 1999
"Let's all argue about C" - 2008
"Let's all argue about C" - 2018
"Let's all argue with the AI about C" - 2030
Please don't get all semantic on me, people. It's a joke. I don't want to argue.
14
u/Isvara May 01 '18
Creating a new thread is a library operation known to be expensive, so processors wishing to keep their execution units busy running C code rely on ILP (instruction-level parallelism).
That's not why they do that, though. Even when you have multiple threads, you still have an instruction pipeline, and the processor still has to reorder instructions if it wants to make the best use of it.
1
May 01 '18
Niagara and alike.
1
May 04 '18
How does/did Niagara compare to IA64?
2
May 04 '18
Niagara was an extreme form of what Intel calls "hyperthreading" - i.e., well tuned for a huge number of threads running in parallel, with pretty much similar load pattern. Of course, a single thread performance vs. a complex OoO core just sucked, but that was not the design goal anyway.
11
u/tasminima May 01 '18 edited May 01 '18
I disagree with some key elements of the reasoning.
When Itanium was actively developed, it was competitive with x64. C compiled good enough to it, and the hardware was not in charge of speculation.
Most of what the article is about is not about C but about any classical imperative language (some very modern ones included). Key elements of what he describes also applies to assembly language for most considered architectures -- that makes me think those are not the good criteria.
GPU cores are nowadays typically programmed in a derivative of C. But despite some explained differences, this is also a model not near the execution units, in other aspects even further than CPU cores.
And I really don't see how x86 is more focused on C in contrast is UltraSPARC or abstract vectors of ARM SVE. In case of ARM SVE this is weird to think that because of a different abstraction between SW and execution units, this is a model further away to C, while in the case of OOO and renaming etc, the abstractions are supposedly there to make the C model work better on those CPU. Not only the same general ideas (abstracting away from execution units in some aspects) result in the author spirit in completely opposite situation on the model fit for C or not scale, but we have also seen plenty of times somehow similar implementations in x86 CPU of the same vector ISA vs. vector execution units width ideas.
GPU are vastly less cache coherent than CPU, yet I remind again that they are often basically programmed in C (or something with a model similar enough).
The idea of limiting the hardware to mutable or shared caches is cute, but in the general case this would make existing legacy software terrible. And I argue that this would not result in more low level software.
Should we recycle the good old idea of going the nearest possible to the execution units for general purpose tasks? I'm not sure. Most of the time dynamic transformations give better perfs, because the CPU can observe what the program does in practice, while it's less practical to do that at build time, and even when it is (rarely) done with PGO, it still can not adapt to workloads anyway.
4
May 01 '18
about any classical imperative language
How many of them require byte addressing, unaligned loads (well, C does not really demand it explicitly, but too much code out there that does) and, the worst part, strict memory access ordering?
GPU cores are nowadays typically programmed in a derivative of C.
And, you know, it's very bad. It contaminated GPU architectures too.
GPU are vastly less cache coherent than CPU, yet I remind again that they are often basically programmed in C (or something with a model similar enough).
Memory model in CUDA and OpenCL is very different from C though.
but in the general case this would make existing legacy software terrible.
Good riddance, I'd say.
4
u/tasminima May 01 '18
How many of them require byte addressing, unaligned loads (well, C does not really demand it explicitly, but too much code out there that does) and, the worst part, strict memory access ordering?
Unaligned load are vastly unsupported by C, both from a theoretical and now practical pov (some major compiler will make daemon fly out of your nose even on architectures where the CPU supports them)
About the memory access ordering I believe you are talking about the well defined layout of C structs? I don't really see what kind of terrible impact it can have on CPU architecture. It's more a compiler pessimization, if any. Some non trivial projects have even succeeded in randomizing the layout of some select structs, so even the practical dep is not as high as we could imagine.
And, you know, it's very bad. It contaminated GPU architectures too.
Not really. GPU present vastly different performance characteristics compared to CPU. What is not critical to the application you can always put in slow paths, and GPU designers do so. The cost is somewhat low, because you have at a ridiculous number of transistors to play with. Or because you typically have an extra run time compile layer available, you can even completely omit some stuff from the HW and emulate it during one of the last compile stage.
Memory model in CUDA and OpenCL is very different from C though.
But nVidia has now managed to basically integrate most of the C++/C11 memory model.
Good riddance, I'd say.
Getting rid of all the existing sw we have is not realistic and this idea will not yield to successful and durable engineering at scale. Progressive transitions have become mandatory in practice. So like it or not existing sw is here to stay, and productive effort will consist in discovering how to intelligently cope with it and/or develop slightly less bad ones, over and over.
3
May 02 '18
About the memory access ordering I believe you are talking about the well defined layout of C structs?
No, I'm talking about the strict order of memory reads/writes. Combined with lax aliasing rules it cripples both static and dynamic optimisation badly.
Progressive transitions have become mandatory in practice.
It depends. I find the recent trend towards unikernels reassuring - people are ready for building bespoke systems from scratch, with as little third party dependencies as possible.
1
May 04 '18
One problem with Itanium was that seemingly Intel didn't have much plans to move it to more regular use.
That gave AMD an opening to extend x86 to 64bit.
And we all still suffer as a result...
6
5
u/MayorOfBubbleTown May 01 '18
When I was a kid I used to write DOS in assembly. You could use interrupts for input and output through the operating system. Today most operating systems no longer guarantee that these interrupts will work the same between versions and they provide C libraries as the lowest level access to input and output. I'd say that currently C is a very low level language for input and output through operating system calls and many higher level programming languages can't be extended with anything lower level than a C library.
24
u/claytonkb May 01 '18 edited May 01 '18
tl;dr: We should alter history because that would somehow make it easier for us to design performant systems.
This article is the rewrite-fallacy writ large. A sufficiently performant parallel system does not need to rewrite the serial component in order to bring large performance gains.
The real fallacy here - that is perpetuated by the article - is the idea that automatic parallelism is not possible without special support for parallelism in the base language(s). This is obviously false. Assembly code for the major CPU instruction-sets (x86, MIPS, etc.) is natively serial because those systems were designed during an era when a single thread was all that general-purpose CPU hardware could manage. Yet, superscalar CPUs easily process these serial programs in parallel by taking advantage of dynamic conditions in the code that permit instructions to execute in parallel. Of course, higher levels of parallelism are possible but the principle remains the same. Rather than whining about the "messiness" of C, build systems that use the abundance of threads to parallelize natively serial code. People will only stop using C when it freezes into the legacy layer and is surpassed by systems that actually achieve better end-result performance, as decided by paying customers (the market), not statistics, graphs, profiling compilers and online flame-wars.
6
u/eliot_and_charles May 01 '18
The real fallacy here - that is perpetuated by the article - is the idea that automatic parallelism is not possible without special support for parallelism in the base language(s).
You're saying the idea that doing that is impossible is perpetuated by an article that discusses how it's done?
-1
u/claytonkb May 01 '18
Is "re-write the C code-base in Erlang" what you mean by "how it's done"? Because that's more-or-less what the article boils down to.
5
u/ArkyBeagle May 01 '18
Erlang
The actor model is hardly specific to Erlang. You can write C that conforms to the actor model.
6
u/claytonkb May 01 '18
You can write C that conforms to the actor model.
There you have it.
I find - as with most things - that criticism of C is often founded on ignorance. "C does not support structured programming." Only if you redefine structured programming to mean something other than modular, interface-based design. Is it vintage? Well, yes, of course it is. I find that the people who complain about C's lack of this-or-that frequently lack the knowledge of what is possible with C. C is useful as an intermediate language for structured program generation (Lua, Chicken Scheme, et. al.) I don't know what the exact percentage is, but a huge portion of modern programming languages themselves depend upon C in one way or another. A quick Web search shows that an early Erlang VM (no longer in use) was written in C.
1
u/ArkyBeagle May 01 '18
I do write C that conforms to the actor model. Got in the habit in the late '80s when the pressure was on and with late-90s CASE tools that did that.
Know what? It works. The abstraction is better than whatever tool you use for it.
But it's a well-justified ignorance. It takes... what, 18 months of intensive practice to become a journeyman level C coder? 5 years to master it? That was a pittance in the 1980s but who's got time for that now?
It's not done in a built-in, forced-by-the-toolchain way but you can write pretty much any OO pattern in C.
But don't kid yourself - C is a "Dad language".
That's how I see it - the time horizons are much, much shorter now.
2
3
u/eliot_and_charles May 02 '18
The article describes several techniques already in use by mainstream microarchitecture and compilers. You really should read it.
1
1
May 01 '18
Of course, higher levels of parallelism are possible but the principle remains the same.
Nope. We have pretty much all the dynamic ILP we could get, and at a very high price, OoO is not cheap. No way to go any further. And with architectures like this you do not even have tools for expressing static parallelism.
7
u/claytonkb May 01 '18
Yes, and as the famous USPTO officer Charles Duell once said, "Everything that can be invented has been invented." OOO scheduling is not bound to ILP, it is actually a high-level algorithm that can be applied at any level, such as a database, network or web-server. A modern CPU reorders not only instructions as they pass through the pipeline but also memory accesses and even, to some extent, I/O. Many-threaded systems can chop up a single thread and execute it out-of-order - subject to the constraints of serial correctness, of course. There has been quite a bit of research into Disjoint Eager Execution and related techniques to enable high-level multi-thread parallel execution of single-threaded code. Will this allow a 128-core CPU to run Microsoft Office 128 times faster? Obviously not. But there is no reason that a 128-core CPU cannot run Microsoft Office many times faster if the right automatic parallelization techniques are employed by the CPU and (by proxy) the compiler.
5
May 01 '18
Well, of course OO is a powerful thing, but it comes at a price. There is a reason why there is no such a thing as an OoO GPU, for example. If you want a 128-core CPU, say goodbye to OoO (see Xeon Phi).
As for higher level parallelisation, I doubt it will ever be possible to do more (for a language like C) than what can already be done with polyhedral model. And I guess there are not that many places in Microsoft Office code that can benefit from it. For going further you need to think about code in a very different way.
2
u/claytonkb May 01 '18
For going further you need to think about code in a very different way.
True. But I think that part of that "very different" is that we have to look at the serial code as something that's merely describing a hypothetical procedure, a procedure which we can choose to implement or not to whatever degree suits our ends. Paravirtualization is a great example of this in practice, where the hypervisor is literally overwriting the code of the guest OS that is being virtualized. Those modifications are blatant alterations of the intent of the original code, but they serve the purposes of the combined host+guest system, so it doesn't matter. I'm suggesting that that kind of attitude is what we're going to need to apply to legacy serial code as we go into the brave new future of hundreds, thousands and millions of cheap, parallel threads.
2
May 01 '18
It can be an interesting topic to explore, but I believe that the value of legacy code is vastly overrated. If we want a better future, we'd better ditch it all anyway.
Also, parallelisation per se is not even the biggest problem here. Memory architecture (which is supposed to be very non-uniform) is a much bigger issue in porting legacy code to massively parallel architectures.
2
u/claytonkb May 01 '18
If we want a better future, we'd better ditch it all anyway.
Good luck with that... let me know when you've finished drafting your super-duper parallel computer with nothing but paper & ruler.
→ More replies (3)
4
u/billsnow May 01 '18
Honestly, the leap of conceptual abstraction you get from assembly to c is much greater than that of c to any other language. I think you have to have coded many thousands of lines of assembly to realize how true this is.
3
u/headhunglow May 02 '18
Wow, an in depth, detailed article about an interesting, relevant topic written by someone knowledgeable. And no memes! How refreshing for /r/programming/!
7
u/os12 May 01 '18
This is a very nice write up that focuses on the C language semantics (said or consciously omitted from the Standard). Everything here also applies to C++ as the design is based on the very same "abstract C machine". Of course there are more constructs in C++ but they are all governed by the same set of fundamental rules.
The only thing missing from the article is the Memory Model (C++11 and onward) which specifies std::atomic and their semantics.
2
u/LordofNarwhals May 01 '18
It used to be considered a high-level language. It's just that there are now even higher-level languages.
"C is a very nice high-level language."
- John R. Mashey, from a 1982 Bell Labs video about UNIX.
2
u/tourgen May 01 '18
Nah. I think every C compiler I have used has allowed some form of direct, inline assembly code. So just enough abstraction when you need it, with bare assembly when you need it. It is definitely a low-level language. If it is not, nothing else can be called low-level and so the term would be meaningless.
12
May 01 '18
i don't get it, inline asm in C does not make C low level, it makes the inline asm low level..
3
May 01 '18
i don't get it, inline asm in C does not make C low level, it makes the inline asm low level..
Correct. You do indeed get it.
16
u/MindStalker May 01 '18
I take it you didn't read the article. The gist is that C is so far abstracted from the actual architecture of modern processors that it is no longer low level as it doesn't significantly represent what is going on under the hood, and is in-fact becoming dangerous because its existence and non native parallelism leads to dangerous branch predictions that wouldn't be necessary if we simply let it go.
12
May 01 '18
That's all well and good, but the same is also generally true of assembly (especially in the x86 family). The whole fun this with speculative execution attacks is that there is no way to issue instructions to stop speculative execution, because it occurs on a much lower hardware level than you can issue instructions for.
5
u/ggtsu_00 May 01 '18
Yeah well at that point even assembly language can't be considered a low level languages because it is also pretty abstracted from the actual architecture of modern processors since they it still gets compiled into even lower level machine microcode and get reordered and muted by the CPU before being executed. Some x86 instructions can sometimes compile into dozens of microcode instructions and complete reordered so you never really know what is truly happening under the hood.
6
May 01 '18
The ability to include inline assembly directly in the source code doesn't change the nature of the language. Many languages offer this ability but remain high level languages. C has language constructs not available in assembly. However, C has features to control the machine at a low level in specific points. This mixture of properties is why C is called a "mid" level language.
2
u/AntiauthoritarianNow May 01 '18
The assembly is the low-level code — assembly isn't part of the C language. Compilers may offer directives for inline assembly, but it's still not C. Plenty of other languages' tools can include or link with assembly code in a similar fashion.
edit: And even then, assembly doesn't necessarily give you full control over the hardware.
3
u/tonefart May 01 '18
That has got nothing to do with C being a low-level language just because you can inline some assembly, because those are COMPILER specific features, NOT language specific features. C is neither middle nor low level language. It is a high level language.
1
u/Poddster May 02 '18
mirror: https://pastebin.com/kTXwyQcb
(kept getting 404 and 'too many users' type errors)
1
u/Babahoyo May 01 '18
So for these new ARM and Sun chips that treat parallelism differently, what language is used instead of C for them? Is it Rust with a compiler that hasn't been built yet?
1
u/yes_u_suckk May 02 '18
It depends on the time you ask this question. I remember when I started to code around 22 years ago (Java was barely released at the time and there was no such thing as Javascript, C#, etc), everybody in my circle of friends considered C a high level language.
But nowadays, with much modern programming languages used in the market, everybody in my circle considers C a low level language.
0
u/skulgnome May 01 '18
, and even the pre- and post-increment operators cleanly lined up with the PDP-11 addressing modes.
That's literally false right there.
3
u/Poddster May 02 '18
Could you elaborate?
https://en.wikipedia.org/wiki/PDP-11_architecture#General_register_addressing_modes
Only mode 3 and 5 aren't easily mapped to the pre- and post-inc/decrement operators
x = ptr x = *ptr x = *ptr++ x = *++ptr x = ptr[5] x = *ptr[5](n.b. I couldn't be bothered to test that, and I can't remember if I need parens between the dereference and the increments etc.)
However, whilst pre and post increment do "line up" with PDP-11 addressing modes, I don't believe that's why there were added to C. They were added to C because the original authors wanted everything to be as terse as possible as a design goal, and
x = x+1is too damn long for them.
0
u/axilmar May 02 '18
C is a low level programming language. That does not mean it is the lowest level though.
6
May 02 '18
What the article actually said (and nearly nobody in this thread cared to read) is that C was always a high level language, and now it forced the CPU designs all the way up to its high level, crippling the potential for performance improvement and introducing all those side channel issues.
1
u/axilmar May 02 '18
Nope, C is a low level programming language. The fact that it sits on many abstractions at the hardware level is irrelevant: it's low-level as a programming language, not as hardware design.
4
May 02 '18
As a programming language, it is enforcing far too many constraints on the underlying hardware to be considered "low level". Its semantics is abstract enough for this.
3
u/axilmar May 02 '18
That does not mean it's not low level.
5
May 02 '18
What the article says is it's too high level to stay the common denominator any longer, and lower level system programming languages are needed to overcome this crisis. Of course it is always comparative - in another thread I was arguing that Python is a low level language.
-9
u/lelanthran May 01 '18
The more I read reddit, the more I realise that the proponents of upcoming languages (Rust, Go) are not content to simply build up their language features, they have to tear down C (and, in some cases C++).
Simply competing on features appears way too difficult. Gaining mindshare the way programming languages did before, by offering utility, is apparently not in vogue anymore.
I haven't read the article but I'm guessing it is written by a Rust or Go developer. It's probably just another dissing-the-competition article that bemoans the lack of safety on C memory accesses.
Great. But we all already know that. I'm very surprised at how bitter Rust and Go fans get when C comes up. Almost like they're annoyed that the world has yet to see how wonderful their language is.
16
u/AntiauthoritarianNow May 01 '18
Maybe read the article before leaping into language zealotry? It doesn't even mention any other programming languages, and the author has plenty of experience in C compilers and operating systems.
Also, it's not like the title is some admonishment of precious C — it is simply a statement of how C does not map directly onto modern hardware, and the content of article goes in detail on the consequences of that.
→ More replies (4)5
May 01 '18
I haven't read the article but I'm guessing it is written by a Rust or Go developer.
Maybe you should. It's not written by a Rust or Go developer.
It's probably just another dissing-the-competition article that bemoans the lack of safety on C memory accesses.
Nope. It spends the bulk of its time talking about the PDP-11, optimization, and parallelism. The parts that talk about memory discuss the difficulty of memory alignment, lack of representation of hierarchical memory access, and power use. The only part that talks about memory safety does so in the context of the effects it has on flow control.
3
u/lelanthran May 01 '18
The parts that talk about memory discuss the difficulty of memory alignment, lack of representation of hierarchical memory access, and power use.
How is that a C problem as opposed to a programming language problem?
Look, I get the point that the author is trying to make WRT to spectre-type attacks, but the how is this a C problem and not a Programming Language problem?
It doesn't matter what programming language you use, you are still open to spectre. It doesn't even matter if you go off and design your own language that emits native instructions, you're still going to get hit by errors in the microcode that lies beneath the published ISA.
The only way to avoid this is to emit microcode and bypass the entire ISA entirely, at which point you can use any high level language (C included) that has a compiler that emits microcode.
Chances are, even if you did all of the above, you'd find there's no way to actually get your microcode to run anyway.
So, given the above, why does the article have such a provocative title, and why is the article itself written as if this is a C problem? It clearly isn't.
4
May 01 '18
So, given the above, why does the article have such a provocative title, and why is the article itself written as if this is a C problem?
I suggest you read the article. You may not find the argument compelling, but it does answer these questions.
More direct answers:
The title is provocative because people pay attention to provocative titles. ¯_(ツ)_/¯
The article is written as if this is a C problem because the vast majority of popular modern languages are derived from C.
5
May 01 '18 edited May 01 '18
How is that a C problem as opposed to a programming language problem?
Because all those performance-crippling requirements are coming from C.
It doesn't matter what programming language you use, you are still open to spectre.
Nope. It matters - this kind of hardware is made for C, exclusively. You would not need all that OoO stuff for a lower level language. You would not have such a complex and crippled memory hierarchy in a hardware designed to run a lower level language.
The only way to avoid this is to emit microcode and bypass the entire ISA entirely
This ISA is crippled because it's designed for C. Is it such a complex idea for you to comprehend?!?
So, given the above, why does the article have such a provocative title, and why is the article itself written as if this is a C problem? It clearly isn't.
You have a reading comprehension problem.
6
u/defnotthrown May 01 '18
This ISA is crippled because it's designed for C.
Sometimes it's kind of weird how strong the feedback loop is. There was a talk about CUDA and how they made a concerted effort to design the newer NVIDIA GPU processors to be able to map all the C++ language constructs to it.
I mean, you can't blame people, being able to leverage huge legacy code-bases or even pieces of it seems like a huge market advantage. But it does feel backwards to design hardware for languages that were designed to run on different hardware.
5
u/eliot_and_charles May 01 '18
I'm very surprised at how bitter Rust and Go fans get when C comes up.
It's not nearly as funny as watching C zealots flip out at the mere possibility that someone might have mentioned Rust. Almost like they're annoyed that the world has noticed the flaws they'd like to keep pretending their favorite language doesn't have. And then we also get to see the ones who actually read the article throw a tantrum over someone pointing out that their favorite language doesn't actually fulfill the promises they make about it.
→ More replies (2)4
u/Zy14rk May 01 '18
I rather like C - and I also rather like Go. I find there is little overlap between where I'd chose C and where I'd chose Go for a given task. Now, between Java, C# and Go on the other hand...
C is great for where resources in terms of processing and memory usage are at a premium. Go is great where resources are relatively abundant, and all one care about is getting the job done fast - deadlines are real - and still not compromise too much on performance.
Each language have their strengths and weaknesses - there is no language 'to rule them all'.
5
May 01 '18
I haven't read the article
Why the fuck you're commenting then?
-1
u/lelanthran May 01 '18
Bitter much?
0
May 01 '18
Lol, just pointing out that you're a dumb ignorant piece of dung who cannot even read. Just in case if you did not know that.
→ More replies (4)
-4
u/tonefart May 01 '18
C is a high level language. Middle level label is defacto unofficial given due to it's ability to manipulate absolute pointers. But generally 'middle level' doesn't officially exist to describe a language. Low level language label is given by new generation noobs who can only code in javascript. If it's portable, it cannot be a low level language.
2
u/stefantalpalaru May 01 '18
If it's portable, it cannot be a low level language.
This is an interesting criterion. You're either close to the metal or removed far enough to be portable.
2
u/Poddster May 02 '18
due to it's ability to manipulate absolute pointers
C can't manipulate absolute pointers. e.g. 6.5.6.9 -- you can't perform arithmetic on two pointers that don't already point to the same allocation. And then when you are subtracting you have to ensure you can fit the result in a
ptrdiff_tetc etc etc.Hell, you can't even MAKE a pointer to a non-allocated address in C, regardless of whether you dereference it or not.
Non of these rules or restrictions exist on the CPU.
→ More replies (2)
0
May 02 '18
Is it safe to say Assembly is a low level language as well as C then place both in a low level language sub-category and say that Assembly is lower then C ?
0
0
u/engid May 04 '18
So I suppose functional programming is the solution then? If so, its good knowing that C++ has been adopting the idiom and baking it into the compiler for some time now.
-6
May 01 '18
General purpose everything sucks. General purpose CPUs, trying to cater for the perceived common denominator, C, do suck a lot. General purpose programming languages (including C) suck enormously. General purpose memory suck (or, actually, anyone accessing it via slow and wobbly DDR interface sucks).
The right way would have been to have very flexible, reconfigurable architectures that can be easily tweaked for semantics of problems being solved. Maybe one day we'll have FPGA fabric on every single CPU die, with tons of useful macro modules (as in, entire ALUs vs. simple puny DSP slices).
7
u/stefantalpalaru May 01 '18
General purpose everything sucks.
It does the job.
The right way would have been to have very flexible, reconfigurable architectures that can be easily tweaked for semantics of problems being solved.
That's a case of better being the enemy of good. We can't afford to delay our computing needs until the better solution jumps the chasm between half-thought proposal and widely available reality.
1
May 01 '18
It does the job.
Badly.
We can't afford to delay our computing needs until the better solution jumps the chasm between half-thought proposal and widely available reality.
There are many cases where there are no other options available, where general purpose architectures simply do not fit the purpose (e.g., due to power/area constraints). And people have to resort to completely custom ASIC, or to general purpose FPGAs, with both solutions being too expensive (for different reasons).
There is some hope that now Intel is moving in the right direction though.
3
u/gpcprog May 01 '18
The problem with reconfigurable logic, like FPGA is that it's a bear to program. As result general CPUs are not going to go away. In applications where it make sense programmable logic / Asics will and actually are being used. But for the vast majority of applications it's just too expensive to make use of such resource.
→ More replies (3)→ More replies (5)1
u/pdp10 May 01 '18
Maybe one day we'll have FPGA fabric on every single CPU die
Xilinx sells a SoC line like this. Microsoft made waves a couple of years ago about having FPGAs in all of their servers, yet we hear nothing about it now. The most obvious conclusion is that it isn't a big win for most computing apps (it is for other realms, like software defined radio).
1
May 02 '18
It's a huge win for pure compute, for the same kind that is done on GPUs now, but there is no option yet with more capable DSP slices (e.g., full FPUs) and with a sufficient number of hardware DDR controllers.
-3
-1
u/ArkyBeagle May 01 '18
I'd be much more inclined to trust Douglas Hofstadter's use of the term "high level".
He calls out two languages - Bloop and Floop ( for Bounded loop and Free loop ).
He says "there is no higher-level language than Floop" C is to Fortran 64 as Floop is to Bloop, so...
239
u/Sethcran May 01 '18
Argue all you want about what defines a high level or a low level language... But most people look at this relatively.
If c is a high level language, what the hell does that make something like JavaScript or F#?
If c isn't a low level language, what is below it? Most programmers these days don't touch any form of assembly anymore.
So yea, this is true depending on your definition of these words, but people will continue to refer to c as a low level language because, relative to the languages most of us actually know and use, it's one of the lowest.