r/ProgrammingLanguages • u/P-39_Airacobra • Jul 27 '26
Discussion A hole in systems programming language design
Given the recent discourse about systems programming I wanted to throw my 2 cents in. This may be a controversial take in a subreddit that's all about innovation and improvement, but I think a lot of budding systems languages are trying too hard to "fix C" and this is exactly why C has not been replaced yet.
Like it or not, C is successful. It does what it means to do very well. Yes, it bites you constantly, but developers have made some form of peace with this because they appreciate the essence of the language. Systems programming is a very pragmatic field, and what works, works.
A lot of language designers want to improve on C, when by nature to "improve on" C is to depart from it, because C is less about what it includes and more about what it omits and what it lets you do that other languages don't.
If you want to replace C, you need to just make C but without the pain points. Less undefined behavior, more standard compiler behavior, easier function pointer syntax, safer macro system, etc. The things that C can't do because of backwards compat.
On the other hand, bolting on features, revamping C's core nature, aren't going to give you a language that will replace C at the low-level or among hobbyist programmers. Most developers aren't as concerned about what C lets them accomplish, as they are concerned about all the painful tedious tendencies of the language.
View all comments
Show parent comment
1
u/EggplantExtra4946 Aug 02 '26 edited Aug 02 '26
Sort of, my idea is that it would be relatively easy for a native compiler of a statically typed language to produce a struct definition of the stack layout of the frame once register allocation is done. I know that one strategy for natively compiled garbage collected languages to track down which stack slot can contain pointers to heap objects is for the compiler to generate so called "stack maps" so this is not unheard of. It could be possible to analyze the generated code at posteriori using the executable but it would indeed be harder since you would need to reconstruct the control flow graph.
The return address and the RBP register will usually occupy a fixed slot of their own. Now for the local variables, they might individually not be saved to the same stack slot and conversely a stack slot might be occupied by different local variables. In that case, part of the struct definition of the frame would be a union type like in C and the implicit tag would depend on the current state of the function, which would be the result of the analysis you propose in 2).
This stack layout data could be saved in the binary similarly to static data and reflection data, then looked up at runtime. A given function would easily inspect its own stack frame. Now the harder part is looking up the stack frame of the callers. I had admitedly not thought about it that much but you indeed need to know who the caller is. A tag inside every frame would indeed work, but you can also look at the return address. The executable contains a finite number of call instruction, each with a unique address and each belonging to a unique function (assuming a regular compilation scheme with no special basic block sharing "trick"), so you could have a hashtable mapping possibly ocurring RIP addresses to a unique function definition. It would also work for functions called with a function pointer, as indirect call instructions are occuring a finite number of times and belong to a unique function body.
The exact RIP addresses are not know at compile time due to Position Independent Code and ASLR but they are known at link time, it's just a matter of adding a bases address to the relative return address offsets.