r/cpp • u/hrco159753 • Mar 29 '24
Why we stopped using macro assemblers?
Here me out, I don't think I've wrongly posted this on r/cpp.
I'm using C++ on daily basis, not the latest standards thought, but still I'm using it really regularly and I'm into MTP, heavy constexpr usage,... I'm also a big expectant of standardized static reflection, so you could say that I'm basically a giant proponent of doing the most at compile time and passing just what you need to runtime. Now, because we still don't have reflection in the standard, we are quite limited with what is possible to do at compile time(another important component, that eventually needs to land in the standard, is code injection but for the sake of this post lets say that code injection is implied when I say "reflection"), so as probably many of you did before, I also tried to "invent" some kind of compile time reflection. For example, I imagine that many of you have tied the same, I've tried to create a compiler plugin that would pull information from the AST and generate code based on it which is essentially what we will eventually get with the standard proposal but with a nicer API. Parallelly with my attempts to compensate the lack of static reflection facilities, I'm looking at what other people are trying to do at compile time, e.g. a full constexpr JSON parser has been created by Jason Turner but as far as I know it has limitations primarily because there is no way to pass the dynamically allocated memory to runtime from constexpr context(another compile time feature that's not available in the language similar to reflection). This is just an example of what people want to do at compile time, and if you ask me, to me it seems that people will find a use case for anything to be done at compile time, or in other words, people want an ability to do whatever they want at compile time. This is something that some languages today allow to a high degree, e.g. zig, circle(c++ compiler). Now here is where I finally start to connect to my title, I am asking myself, if we allow ourself to do whatever at compile time why don't we create a compiler at compile time? I know that this is quite a jump and an extreme, but really, what is stopping us to eventually do that? Even today people are creating full constexpr enabled virtual machines, what is stopping us to eventually start writing compilers at compiler time?(maybe sanity) And with that thought I somehow landed on a thought "why we stopped using macro assemblers?", maybe I'm wrong but to me it seems that pushing this "do everything at compile time"-train leads to some kind of a "modern macro assembler". We are more and more writing a compile time program that generates a program that will be propagated to runtime and eventually executed. And to be honest, I might not even be against it at all. Maybe, just maybe, there is value in trying to go back and try again with macro assemblers, but with modern macros, i.e. no glorified copy-pasting, maybe try with something closer to a particular oxidized language's macros. I'd be interested in seeing a new spin of an old idea, maybe instead of calling them "macro assemblers" maybe they can be called "compile time assemblers" idk.
I tried to aggregate my train of thoughts here and I hope that it is to some extent understandable what I'm trying to say. I've tried searching for articles of something similar that I'm trying to say here but with no success. Maybe there is already something like a "modern macro assembler" out there that I'm not aware of and I'd be grateful if you would point me at it. In the end I want to say that I'm not really searching for answers here, I'm really just trying to see whether anybody has been thinking about anything similar and whether this is even something that makes any sense to anyone but me. Thank you for reading my wall of text and thank you for any kind of input.
8
u/johannes1971 Mar 30 '24
I can't speak for everybody else, but personally I stopped using them because x86 has such horrible assembly language that I never managed to properly learn it. Considering that I'm fluent in both z80 and 68000 assembly (I've written a z80 emulator in 68000 assembly) it has always been a source of frustration for me that I somehow cannot wrap my head around x86.
Here's what happens when I look at an x86 instruction on godbolt:
movsrcpxglmtsnp32x aexrgmp, pc64...so something is being moved... it's probably conditional... 32 bits I guess... ....something something 'pointer'? Memory dereference? 'A' register, extended to... what? Wait, is this the dialect where the source comes first or the other one? Should I just invent my own dialect and make it make sense?
I mean, I sometimes complain about people counting instructions on godbolt and pretend that that's a performance metric, but at the same time: it's hard to blame them when you are faced with this kind of nonsense...
6
u/dustyhome Mar 30 '24
What do you mean? VGF2P8AFFINEINVQB just rolls of the tongue.
4
u/johannes1971 Mar 31 '24
That... That's real... 😭
I think a large part of the problem is the insistence on using ever-more cryptic mnemonics. It was fine back in the old days when the number of distinct instructions was small, but at this point x86 assembly should really have switched to something that looks a bit more like normal English words instead of cat-like typing.
1
u/hrco159753 Mar 30 '24
I feel like a lot of people have stopped for the same reason as you, the modern instruction sets have become so complicated that you couldn't reason about them anymore. What I think is plausible is that we have jumped to early into higher level or rather that we didn't land on the right abstraction mechanisms with, for example, C. Maybe there is something in between C and a macro assembler. Is that something that make sense?
2
u/johannes1971 Mar 30 '24
I think C is pretty much the lowest level you can be at, without starting to rely heavily on details of the architecture you are targetting (like specific register names). Do you think there's a level between C and assembly that isn't architecture-dependent? What would that look like?
And if you don't mind being architecture-dependent, people don't write entire applications in assembler anymore. The only usage nowadays is short snippets that speed up one specific thing, and you don't need a super-powerful higher-level macro assembler for that.
4
u/ResearcherNo6820 Mar 30 '24
Yep, right there with you. Loved 6502/68000/MIPS/SuperH, etc. but x86 and its variants are so ugly my brain shuts down looking at it.
1
u/MrPopoGod Apr 01 '24
x86 was ok when it was 32 bit. The 64 bit extension really made a mess of the iSA.
9
u/asenz Mar 29 '24
how about having a robust build system executed at compile time?
6
u/llothar68 Mar 29 '24
I guess that will be one of things in JAI this vaporware language Jonathan Blow is writing for game development.
And i agree, i love that in MSVC you can add #pragma lib. and then let the linker find the libraries based on each compilation unit included. Thats easier then some mechanism CMake offers.
We would just need a #pragma for the library detection and transitive dependency declarations.
1
u/hrco159753 Mar 29 '24
Yeah I guess that would be equivalent as doing everything in `constexpr` context in C++. But then the build system would actually be generating the code directly and not invoking the tools that generate the code. I might not be catching what you mean by "robust build system"? Could you please elaborate?
9
u/Brisngr368 Mar 29 '24
If you need something more complicated than macros just write a bash / python script to generate your code for you?
2
u/hrco159753 Mar 29 '24
I agree, code generation is the most powerful tool that we always can use and it's effectively equivalent to the eventual future code injection feature, but why generate language X when you can generate native assembly at compile time? You can generate native assembly also with python I guess but what if we have a tool directly designed for generating assembly, i.e. "modern macro assembler"? Does that make sense?
2
u/Brisngr368 Mar 29 '24
I'm not sure I quite understand, but aren't you generating native assembly anyways? A simple preprocessor script is just going to generate C++ code that gets compiled to assembly?
67
1
u/llothar68 Mar 29 '24
I wish the standard C and C++ macros were better cut and paste like m4.
I almost never use C++ templates but a lot of C macros on repeating code snippets
1
u/hrco159753 Mar 29 '24
I'm not really aware what m4 gives that's better then c preprocessor but I don't think preprocessors are not going to cut it in this day and age.
2
u/Potatoswatter Mar 30 '24
Macro assembler was my gateway drug to template metaprogramming.
No further layer of abstraction is needed. We just keep refining templates and constexpr.
2
u/justinhj Mar 30 '24
This kind of yearning for more power at compile time is what drives people to common lisp. Perhaps make that leap and use it to generate your C++.
1
u/Conscious_Support176 Mar 30 '24
Seems like pulling in the opposite direction to templates. Template parameter packs seem like a great tool. Maybe parameter packs with named parameters would help close the loop?
template Tuple<MemberType member name…> { MemberType membername; … }
1
u/XTBZ Mar 30 '24
The description reminds me of the Meta Object Compiler (MOC), one of the big examples is Qt MOC. Its capabilities, of course, prevent it from writing complex automatic code. You can always write your own :)
But I like the system that translates js/qml into C++.
10
u/AssemblerGuy Mar 29 '24
Portability?