nesdev.com
http://forums.nesdev.com/

Efficient way to reuse variables
http://forums.nesdev.com/viewtopic.php?f=2&t=17405
Page 4 of 4

Author:  Jarhmander [ Thu Jun 28, 2018 6:06 am ]
Post subject:  Re: Efficient way to reuse variables

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.

Author:  pubby [ Thu Jun 28, 2018 10:41 am ]
Post subject:  Re: Efficient way to reuse variables

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

Author:  Oziphantom [ Sat Jun 30, 2018 1:03 am ]
Post subject:  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?

Author:  slembcke [ Fri Jul 13, 2018 9:59 am ]
Post subject:  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.

Page 4 of 4 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/