Getting Started with M68K Coral 66: Compilation System for the Motorola M68000 Family | ||
---|---|---|
Prev | Chapter 2. Advanced Techniques | Next |
The compiler requires a stack to support procedure calls. Each stack frame contains the link address, a pointer to the previous frame, and any local data that has not been allocated a register and is not static.
Usually Coral 66 programs require very little stack space, and the default size of 64K (as set in the linker script file) is more than enough. However, programs that include recursive procedures may require large numbers of stack frames, and each frame will contain any non-preset arrays and tables declared locally.
Use the compile-time option -fstack-check
to get the
compiler to generate additional code each time a stack allocation is made.
The stack limit, which is the lowest address in the stack, is held in a
global variable. Here is an example that overflows the 64K byte main
program stack:
Example 2-2. Program that Requires a Large Stack
$ more biggy.cor 'begin' 'recursive' Biggy; 'begin' 'byte''array' S [1 : 100000]; 'end' Biggy; Biggy; 'end'
The generated code for the recursive procedure Biggy
is
as follows. Note that if Biggy
were a procedure and
not recursive then the large array would be allocated statically and not on the stack.
In this case stack overflow would not be relevant.
Example 2-3. Stack Overflow Check
$ m68k-coff-gcc -Wa,-a -c biggy.cor ... 6 Biggy: 7 0000 4E56 0000 link.w %a6,#0 8 0004 207C FFFE move.l #-100000,%a0 8 7960 9 000a D1CF add.l %sp,%a0 10 000c B1F9 0000 cmpa.l _stack_limit,%a0 10 0000 11 0012 6D00 0002 blt .+4 12 0016 4E49 trap #9 13 0018 4E5E unlk %a6 14 001a 4E75 rts ...
The program will fail at run time having raised trap 9. You may attach a handler to trap 9 and take appropriate action.