Look at msp430-builtins.c in the implementation for delay cycles for
examples of generating local labels, comparison, and jump
instructions.  You can probably use something like gen_rtx_MEM (Pmode,
gen_rtx_SYMBOL_REF (HImode, "__stack_base")) and use that to compare
against stack_pointer_rtx rather than having to push and pop r15.

The ICEs are likely if you don't set RTX_FRAME_RELATED_P on the prolog
insns that access the stack pointer.

If you get it working solidly, please file a tracker ticket on the
mspgcc project and submit a patch for review and incorporation into
the development branches.  The final version should set a new frame
flag in msp430_set_current_function, and implement it in
expand_prologue, probably right before MSP430_FF_prologue_eint.

You might also want to look at what -fstack-check really does and
whether what you describe is compatible with its use on other
platforms.  A quick check suggests it's mostly implemented in the
middle-end and may result in code generated that depends on some
runtime support for stack probes.  -mstack-check may be more
appropriate for the approach you describe.

Peter

On Wed, Aug 24, 2011 at 3:27 PM, Radu Rendec <radu.ren...@cloudbit.ro> wrote:
> Hello,
>
> I'm trying to implement a simple stack checking mechanism in an MSP430
> multi-task environment (task scheduling/switching is subject to a custom
> "OS").
>
> Each task has its own stack space, but since there's a single address
> space and the memory is small, tasks can easily overflow their stack and
> corrupt "foreign" data.
>
> The best idea I came up with for checking the overflow is adding some
> explicit checking code at the beginning of each function - actually at
> the end of the preamble, when SP is already lowered to reserve space for
> local variables.
>
> It would be very nice though if the compiler could generate that code
> itself, for instance when the "-fstack-check" is specified on the
> command line.
>
> I tracked down the preamble generation to the expand_prologue() function
> (file msp430-function.c). The global variable "flag_stack_check" is
> visible and automatically set to 1 by (generic) gcc when "-fstack-check"
> is specified on the command line.
>
> The best I could do was identify the code that generates "push"
> instructions for all clobbered registers and the code that explicitly
> lowers SP by the required amount. My code goes next to this.
>
> However, I'm new to gcc internals and most of my attempts to produce the
> check code ended up with gcc reporting an internal bug and bailing out.
>
> I would really appreciate any suggestions or directions for implementing
> this. The resulting code should be as simple as comparing R1 to a
> variable (contents of a fixed memory location) that holds the stack base
> address and conditionally jump to a handling routine. To my (limited)
> MSP430 asm knowledge, it shoud look like this:
>        push    r15
>        mov     #__stack_base, r15
>        cmp     r1, 0(r15)
>        jge     .Lsomething
>        br      #_handling_routine
> .Lsomething
>        pop     r15
>
> Best regards,
>
> Radu Rendec
>
>
>
> ------------------------------------------------------------------------------
> EMC VNX: the world's simplest storage, starting under $10K
> The only unified storage solution that offers unified management
> Up to 160% more powerful than alternatives and 25% more efficient.
> Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
> _______________________________________________
> Mspgcc-users mailing list
> Mspgcc-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>

------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management 
Up to 160% more powerful than alternatives and 25% more efficient. 
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to