Propeller Manual

Table Of Contents
COGNEW – Spin Language Reference
The Need for Stack Space
A cog executing Spin code, unlike one executing Propeller Assembly code, needs some
temporary workspace, called “stack space,” to hold operational data such as call stacks,
parameters and intermediate expression results. Without this, sophisticated features such as
method calls, result values, and complex expressions would not be possible without severe
limitations.
The Spin compiler automatically provides stack space for the Propeller Application’s initial
code, the top-level Spin code of the application. The “free space” following the application’s
memory image is used for this purpose. However, the compiler is not able to provide distinct
blocks of stack space for Spin code that the application may launch on its own, therefore, the
application must provide that stack space itself.
Typically, this stack space is provided in the form of a declared global variable meant only
for that use, such as the
SqStack variable in the example above. Unfortunately, it’s difficult
to determine just how much stack space should be provided, so when developing an object, it
is suggested to initially provide a large amount of longs of memory (like 128 longs or more)
and once the object is deemed complete, use an object like the Stack Length object in the
Propeller Library to determine the optimal length. See the Stack Length object for further
explanation.
Spin Code Can Only be Launched by its Containing Object
In the Spin language, by design, objects must intelligently manage their own data, the
methods that operate on that data, the cogs that execute those methods, and the interface that
other objects use to affect it. These are all aspects that serve to maintain the integrity of the
object and increase its useful and reliable nature.
For these reasons, the object and its designer are notably the best equipped to provide the
proper stack space that is required for Spin code being launched into another cog.
To enforce this principle, the
COGNEW and COGINIT commands cannot launch Spin code outside
of its containing object. This means that a statement like the following will not work as
expected.
cognew(SomeObject.Method, @StackSpace)
Instead of launching SomeObject.Method into another cog, the Propeller will instead execute
SomeObject.Method within the current cog and if that method returns a value, the Propeller
will take that value and use it as the address of code to launch with the cognew command.
This will not result in the code writer’s intended effect.
Page 80 · Propeller Manual v1.1