Efficient way to reuse variables

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 498
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Efficient way to reuse variables

Post by Jarhmander » Thu Jun 28, 2018 6:06 am

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.
((λ (x) (x x)) (λ (x) (x x)))

User avatar
pubby
Posts: 550
Joined: Thu Mar 31, 2016 11:15 am

Re: Efficient way to reuse variables

Post by pubby » Thu Jun 28, 2018 10:41 am

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.
You too read this article yesterday? :P
https://wozniak.ca/blog/2018/06/25/Mass ... index.html

Oziphantom
Posts: 827
Joined: Tue Feb 07, 2017 2:03 am

Re: Efficient way to reuse variables

Post by Oziphantom » Sat Jun 30, 2018 1:03 am

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?

User avatar
slembcke
Posts: 171
Joined: Fri Nov 24, 2017 2:40 pm
Location: Minnesota

Re: Efficient way to reuse variables

Post by slembcke » Fri Jul 13, 2018 9:59 am

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.

Post Reply