GOOC - Game Oriented Object C

GOOC - Game Oriented Object C

Next: Subroutines as Expressions, Previous: Subroutine Parameters, Up: Subroutines
[Contents]

9.3. Parameter Storage

9.3. Parameter Storage

It is important to understand how the GOOL system stores parameters in memory and how they are passed.

The GOOL system passes parameters by pushing them onto the callee's stack (for subroutines, the callee and the caller are the same). That is, the following subroutine call: sub MySub (a, b, c) { x = a y = b z = c } MySub(10, 25, 50) Will first push these three parameters to the stack like so: ..., 10, 25, 50 And afterwards, fill it in with a stack frame which stores information about where the caller left off, once the called subroutine MySub returns: ..., 10, 25, 50, stack frame

GOOL accesses parameters (either for an object state or a subroutine) by going backwards starting from the stack frame. This means that the "first" parameter is described as being in fp-(1*4): ..., 10, 25, 50, stack frame ^ ^ ^ | | | 3rd 2nd 1st

GOOC deliberately has no protection against passing in the "wrong" amount of parameters, since that is completely acceptable and expected behavior. Now watch what happens when too few parameters are passed: MySub(10, 25) ..., 10, 25, stack frame Since only two parameters were passed, only the last two parameters of the MySub subroutine have proper defined values.

On the other hand, passing in too many parameters to the left will have this effect: MySub(100, 200, 300, 10, 25, 50) ..., 100, 200, 300, 10, 25, 50, stack frame Now, the last three paramaters have defined values, while the extra parameters we passed are inoffensively just taking up stack space. However, since their memory location is based on a predictable pattern, you can actually use pointers to access these parameters. Here is an example of how the MySub subroutine can be changed to make use of this (this following code is specifically in Crash 2 fornat): sub MySub (count, a, b, c) { x = a y = b z = c var va_start = &count - count*4 if (count > 0) { do (var i = 0) { x += va_start[i] } while ({i += 1.0}; i < count) } } ... MySub(20.0, 30.0, 40.0, 3, 10.0, 25.0, 50.0) ... The variable va_start is set to the memory location of the first extra parameter to the left, assuming count is correct, then its elements are iterated through.