In addition, Microchip's XC8 C compiler does that too. In fact, depending on the chip model, it can use both a "compiled stack" (which is just fixed locations for variables) and a software stack; by default, it only use the later for functions that are called from multiple contexts (ex: from main and an ISR) or if it detects recursion.
If you're really curious about this, here's the manual, and the relevant info is at the section 5.5.2.2, "Auto variable allocation and access". Among other things, it explains the process of how variables are allocated in the "compiled stack" by analysing the call graph and observing the lifetime of individual variables in it, so variables that are not active at the same time can overlap in memory. Nothing we didn't know before, but might be interesting nevertheless.
Efficient way to reuse variables
Moderator: Moderators
- Jarhmander
- Formerly ~J-@D!~
- Posts: 569
- Joined: Sun Mar 12, 2006 12:36 am
- Location: Rive nord de Montréal
Re: Efficient way to reuse variables
((λ (x) (x x)) (λ (x) (x x)))
Re: Efficient way to reuse variables
You too read this article yesterday?tepples wrote:Today I learned some production C compilers actually do this. From BL51 Users Guide: Data Overlaying:
The 8051 hardware stack is limited to a maximum of 256 bytes. As such, using stack frames on the 8051 is very wasteful of the limited memory available.
The Keil C51 C Compiler works with the LX51 Linker to store function arguments and local variables in fixed memory locations using well-defined names (so that function arguments are easily passed and accessed). The linker analyzes the structure of the program and creates a call tree which it uses to overlay the data segments containing local variables and function arguments.
https://wozniak.ca/blog/2018/06/25/Mass ... index.html
-
- Posts: 1565
- Joined: Tue Feb 07, 2017 2:03 am
Re: Efficient way to reuse variables
You have all seen this right? https://www.youtube.com/watch?v=zBkNBP00wJE Where is shows off how insane modern c++ compilers are at optimising things?
Re: Efficient way to reuse variables
Oof. That video is kind of hard to watch.
Seems like it follows the trap of "Isn't it neat that you can use zero-cost abstractions to write more code than necessary?" I've had too many cases where I found myself writing a lot of code in order to use RAII because it was the "correct" thing to do when all I really needed was malloc() / free() or alloca(). Bah!
I think that got a little rant-y because I've been enjoying NES dev so much lately because not only is C++ not really an option, but I don't feel like I even need it. Sort of a weight off the shoulders sort of thing. Heh.
Seems like it follows the trap of "Isn't it neat that you can use zero-cost abstractions to write more code than necessary?" I've had too many cases where I found myself writing a lot of code in order to use RAII because it was the "correct" thing to do when all I really needed was malloc() / free() or alloca(). Bah!
I think that got a little rant-y because I've been enjoying NES dev so much lately because not only is C++ not really an option, but I don't feel like I even need it. Sort of a weight off the shoulders sort of thing. Heh.