News for hackers. Stuff that matters.
Re:No sane compiler would optimize atomics (2015) by overgard on Sunday July 11, 2021 @10:52PM (#27806426)
> You use a compiler to abstract away the CPU. Because of that, there is no “run as written”.

Um, no. I use a compiler to generate code that would otherwise be very tedious to write. I do not expect it in any way to abstract away the CPU. I do not want it to abstract away the CPU. If I did I would write Java or python. I'm using C++. I am very aware of the CPU. What I'm not aware of is whatever silly transformations the optimizer thinks it can do to my carefully crafted program because I didn't read page 783 of the spec.

Re:No sane compiler would optimize atomics (2015) by gizmo686 on Sunday July 11, 2021 @11:06PM (#27806514)
You want a macro assembler. C is not a macro assembler. C was never supposed to be a macro assembler. It was designed as a cross platform language, which fundamentally means you are programming to an abstract machine, which necessarily means that you cannot have an macro assembler [0].

I can, however, see an argument that people want a C like language which is a macro assembler. I am lost again when people start asking for a C++ like macro assembler. The features that C++ brings over C are not concepts that exist on any mainstream CPU.

[0] Unless you decide to build a concrete machine (either in bare metal, or as a VM).

Re:No sane compiler would optimize atomics (2015) by overgard on Sunday July 11, 2021 @11:18PM (#27806587)
C was originally a macro assembler! This new mutation of C is just that, new. I don't mind optimizations as long as it doesn't remove intent, which is usually clearly indicated in the source, standards be damned.
Re:No sane compiler would optimize atomics (2015) by gizmo686 on Sunday July 11, 2021 @11:56PM (#27806873)
How far back to you want to go?

There is no definitive start point for C, as it grew fairly organically out of B and began as a living language. However, a reasonable approximation for C 1.0 is The C Programming Language (Kernighan and Ritchie, 1978) [0], commonly known as K&R C.

The language they describe is much simpler than modern C. Notably for this discussion, it contains no synchronization mechanisms.

There is nothing in that book that suggests C was a macro assembler. On the contrary, its description of C precludes it from being a macro assembler for the reasons I talked about in a parent comment:

> Although C matches the capabilities of many computers, it is independent of any particular machine architecture, and so with a little care it is easy to write "portable" programs, that is, programs which can be run without change on a variety of hardware. . It is now routine in our environment that software developed on UNIX is transported to the local Honeywell, IBM and Interdata systems. In fact, the C compilers and runtime support on these four machines are much more compatible than than the supposedly ANSI standard versions of Fortran.

> Because the data types and control structures provided by C are supported directly by most existing computers, the runtime library required to implement self-contained programs is tiny. On the PDP-I1, for example, it contains only the routines to do 32-bit multiplication and division and to perform the subroutine entry and exit sequences.

Notice the way this paragraph frames the issue. Since the features of C are supported by most machines, it can be implemented with a small runtime library. It does not say that C must use those features directly. In fact, it explicitly anticipates that C could provide some of its primitives in ways other then directly leveraging a corresponding capability of the hardware. The feature here is efficiency and a small runtime.

[0] https://1lib.us/book/2660358/b1a4cf

Re:No sane compiler would optimize atomics (2015) by stormbrew on Sunday July 11, 2021 @11:58PM (#27806889)
I'm not sure where you got that idea. C was never ever a macro assembler, and its antecedent language (B[1]) is actually even more abstracted away. C was a slight lowering, not a raising, of hardware abstraction.

If C were a macro assembler the following things would be true of it, most likely:

  - types would have fixed bit sizes (ie. uint32_t would be the norm)
  - you'd be able to do various 'normal' things for assembly like rotate left and right.
  - inline assembly would probably be.. you know.. standard instead of something that compiler vendors only started putting in in like the 80s.
You might be thinking of the fact that C++ was originally sort of a Macro C (aka Cfront)?

[1] Ever wondered why `int` is so fuzzily defined in C? It's because in B there was only one type and it was always word sized. That's also why various things are typed `int` by default if you don't specify.

Re:No sane compiler would optimize atomics (2015) by Someone on Monday July 12, 2021 @03:48AM (#27808046)
Another extremely normal thing assembly allows you to do, but C doesn’t: access carry and overflow flags.
Re:No sane compiler would optimize atomics (2015) by spc476 on Monday July 12, 2021 @04:35AM (#27808268)
Not all CPUs have carry and overflow flags. For example, MIPS, or SPARC, or the new RISC-V.
Re:No sane compiler would optimize atomics (2015) by stormbrew on Monday July 12, 2021 @02:21PM (#27813365)
really relevant to this discussion, though, the pdp11 did.
Re:No sane compiler would optimize atomics (2015) by umanwizard on Monday July 12, 2021 @01:00PM (#27812426)
Another (modern) example: SIMD instructions.
Re:No sane compiler would optimize atomics (2015) by overgard on Monday July 12, 2021 @10:05AM (#27810397)
> I'm not sure where you got that idea

History: C was essentially PDP11 macro assembler. There are so many assumptions in C that basically came from the PDP11. C was originally a very utilitarian language to make writing unix easier. That it became massively popular was more of a happy accident.

Re:No sane compiler would optimize atomics (2015) by stormbrew on Monday July 12, 2021 @02:20PM (#27813357)
I'm sorry, but this is just a gross mischaracterization of what a "macro assembler" is. I agree it was utilitarian and made to make unix easier and that many of its core assumptions were based on the platform of the pdp11, but it never did in any way resemble a macro assembler as far as I know.

And in fact, as gizmo86 pointed out, one of the explicit goals of C in terms of "making unix easier to write" was, afaik, that it would allow the code to be more portable to other architectures, because as the 11 in the name implies, the history that led to the creation of unix was not short on new architectures, and one of the key weaknesses of Multics was, afaik, the fact that it was rigidly written specifically for one specific architecture.

By this logic nearly every programming language is just a macro assembler at a certain level of (non-)complexity. All compilers do is translate syntax (macros?) into machine code (assembly?). So, is pascal also a macro assembler?

Anyways here is a list of instructions on the pdp11 that you can't directly represent even in the earliest versions of C to my knowledge:

  ROR
  ROL
  SWAB
  SXT
  ADC
  SBC
  BVC
  BVS
  BCS
  MARK
  HALT
  WAIT
  RESET
  MTPD
  MTPI
  MFPD
  MFPI
  MTPS
  MFPS
  MFPT
  CLC
  CLV
  CLZ
  CLN
  CCC
  SEC
  SEV
  SEZ
  SEN
  SCC
That's the distinguishing factor of a "compiler" vs. a "macro assembler". A macro assembler is a layer on top of an assembler that still allows you full access to the instruction set and allows control flow that most compilers would consider unsafe. C was never that. It wasn't derived from a language that was that and it didn't, to my knowledge, allow that even on pdp-11 unix.

I'd be really happy to see some code from one of the code archives of unix' history that proves me wrong there, though, if you have it.

Re:No sane compiler would optimize atomics (2015) by pjmlp on Tuesday July 13, 2021 @04:04AM (#27818597)
UNIX was originally written in Assembly, C was used to rewrite the 4th version.
Re:No sane compiler would optimize atomics (2015) by benibela on Monday July 12, 2021 @02:06PM (#27813217)
Unfortunately, classes teaching C often start with telling people that with C they learn how the real machine and the real CPU works on the lowest levels.
Re:No sane compiler would optimize atomics (2015) by weeblewobble on Monday July 12, 2021 @05:18PM (#27815256)
I'd be surprised if this was true. It certainly doesn't match my experience, and it also doesn't really make sense. The difference between machine code, assembly, and programming languages like C is first-week CS 101 stuff. What are you basing this on?
Re:No sane compiler would optimize atomics (2015) by co_pilates on Sunday July 11, 2021 @11:14PM (#27806562)
Have any good sources of this topic of macro assemblers? I'm interested in reading about this type of thing. I'll search Google, but you can't always find good sources there today.
Re:No sane compiler would optimize atomics (2015) by pjmlp on Monday July 12, 2021 @02:46AM (#27807757)
Yeah, plenty of books at archive.org.

"Assembly language for the PC"

https://archive.org/details/assemblylanguage00soch

"TASM Reference"

https://archive.org/details/bitsavers_borlandturemblerVersio...

"Structured Assembler Language For IBM Microcomputers"

https://archive.org/details/structured-assembler-language-fo...

Or just go to MASM documentation, you will find plenty of directives that look like an high level language

https://docs.microsoft.com/en-us/cpp/assembler/masm/directiv...

Re:No sane compiler would optimize atomics (2015) by gizmo686 on Monday July 12, 2021 @12:52AM (#27807148)
Unfourtuantly not. The only one I have worked with was really just a normal assembler with a big library of in house macros.
Re:No sane compiler would optimize atomics (2015) by lokedhs on Monday July 12, 2021 @02:03AM (#27807536)
I searched a bit, and found a large set of pages with information on the topic in the context of the 360 architecture. I'd say this architecture is unique in that it is still to this day used to write high level software.

Put "360 assembler macros" in your search engine and you'll find lots of information.

This is a good place to start: https://www.ibm.com/docs/en/zos/2.1.0?topic=language-macro-i...

Re:No sane compiler would optimize atomics (2015) by brundolf on Monday July 12, 2021 @01:28AM (#27807336)
Re:No sane compiler would optimize atomics (2015) by aaronmdjones on Monday July 12, 2021 @05:22AM (#27808524)
Even x86 assembly is not a low-level language: https://blog.erratasec.com/2015/03/x86-is-high-level-languag...
Re:No sane compiler would optimize atomics (2015) by pjmlp on Tuesday July 13, 2021 @04:07AM (#27818603)
Not only x86 assembly, microcoded CPUs has always been quite common on CISC designs, hence why 1960's papers tend to refer to Assembly as bytecode.
Re:No sane compiler would optimize atomics (2015) by overgard on Monday July 12, 2021 @09:38AM (#27810113)
I've read that and it would take a book to explain whats wrong with it.
Re:No sane compiler would optimize atomics (2015) by pajko on Monday July 12, 2021 @01:48AM (#27807445)
> I do not expect it in any way to abstract away the CPU

C was especially designed to do that! https://stackoverflow.com/questions/53100198/what-is-the-pre...

Re:No sane compiler would optimize atomics (2015) by kaba0 on Sunday July 11, 2021 @11:13PM (#27806558)
Feel free to examine the resulting machine code. Godbolt is a really valuable tool.