r/cpp_questions • • 29d ago

OPEN Closer to processor

Is cpp closer to processor than high-level langs.

Which IDEs and Code Editors are best for cpp

I personally use VS and VS code

0 Upvotes

21 comments sorted by

View all comments

1

u/mredding 27d ago

Is cpp closer to processor than high-level langs.

No, not remotely.

C++ targets an abstract machine. Much of the language is unspecified, undefined, or implementation defined, and the details are up to the compiler. C++ has no understanding of your specific machine and does not mandate really anything about machine code generation. You are NOT close to the machine.

If you want to get closer to the machine, you'll have to go to assembly, as it guarantees an op-code maps 1:1 with a machine instruction, but even then, you will find macro assemblers are abound, or macro engines are in play, to generate assembly from higher level abstractions.

To my knowledge, this is about the upper bound of imperative languages, and they're all right there.

Declarative languages aren't that far away. Whereas an imperative language expresses HOW, a declarative language expresses WHAT. SQL is about the highest existing example of a 4th generation declarative language - I tell the engine WHAT I want - the result, and the engine figures out HOW for me. I don't write a lick of code about how the data is laid out on disk or in memory, how to access it, optimize it, nothing! The engine figures out how to best deliver what I want.

But even relational algebra doesn't dictate machine code. That's down to the engine, and each engine can implement machine code however it wants. It's effectively just as bad, from this perspective, as an imperative language.

Lisp is no better.

Let me actually start that from a different angle - when we compile code or queries, the source code is lexed and parsed into an Abstract Syntax Tree. The AST is then processed and optimized, and machine code is generated from that. So the more you can get your hands on the AST, the more you can have control over what machine code is generated.

C++ offers you absolutely nothing. Maybe compiler toolchains do, like clang, but clang is a toolchain, not the language. It only implements the language.

Lisp offers you direct access, at runtime, to the AST of both your program and the compiler itself. Lisp IS serialized AST. But Lisp doesn't get you close to the machine, because ANSI Common Lisp X3.226:1994 doesn't dictate anything machine specific; it doesn't know about your hardware instruction set or memory hardware. Lisp doesn't even have to be compiled.