Stack thoughts
CannibalRobotics
Posts: 535
Calculating Stack space has long been a messy and tedious process. Seems like most of the threads end up in something like "sorry, it's a SWAG at best" For a large program it's even messier.
So, I was wondering about a different angle of analysis - I may be retracing steps here because I have not really dug into the details of the Obex tools.
But, what if a very fast PASM cog that just watched the stack pointer of another cog and captured only min and max values over a period of time?
Would the pointer be an accurate assessment of usage? (ie, is a point to an address the usage of one long or possibly the base of x more.)
Would a 'stack overflow' warning be possible by looking at what has been allocated then comparing it to what the pointer monitor is seeing?
Would cog timing be a huge factor in the sampling?
Thoughts?
So, I was wondering about a different angle of analysis - I may be retracing steps here because I have not really dug into the details of the Obex tools.
But, what if a very fast PASM cog that just watched the stack pointer of another cog and captured only min and max values over a period of time?
Would the pointer be an accurate assessment of usage? (ie, is a point to an address the usage of one long or possibly the base of x more.)
Would a 'stack overflow' warning be possible by looking at what has been allocated then comparing it to what the pointer monitor is seeing?
Would cog timing be a huge factor in the sampling?
Thoughts?
Comments
Try OBEX tools, stack length. Measures stack requirements of methods under development.
RS_Jim
What could also be done is watch the stacks. You could run one COG where you register all stacks and that COG always keeps an eye on the last entry of each stack registered. Of course each stack then should have one additional LONG that should not change.
Here's a routine that will return the number of unused longs on the stack.
I think I'll think of it as "StackOffset"?
Is the pointer value available within the COG SPIN environment? It's not in the SPR or readily apparent elsewhere.
"It" is a program that will calculate stack length by static analysis of code, giving an upper bound to the actual requirement. By contrast, all the methods I've seen here use dynamic (run-time) analysis, and therefore can only give a lower bound that depends on how thoroughly you can exercise the code.
Enemies to this kind of analysis include recursion (a killer), self-modifying code (which the Prop uses, but which would be surmountable in all the code I've looked at) and computed jumps. So even if I get this thing finished, it won't necessarily cope with all programs. Still, I'm hopeful that it would cope with a fair number.
I'm aware that a few people have said this can't be done. However, I've done it in the past with a couple of other (simpler) micros and so far I haven't seen any insurmountable obstacles.
If anyone else would be interested, it'd help to spur me on. Or if anyone else is working on the same idea, maybe we could cooperate.
There is no possibility in the existing SPIN interpreter to access any COG-RAM except the SFRs and the stack pointer is no SFR.
So, if you need this feature you have to modify the SPIN interpreter and maybe the SPIN compiler.
Thanks for that helpful comment!
I'm in the early days of this project, currently studying the interpreter source code to determine exact stack implications of each byte code. Knowing that SPIN doesn't use self-modifying code or computed jumps is a big step forward.