I want to jump from some arbitrary places to my exception handling code, to
deal with fatal error conditions. Is it possible to do this with mspgcc?
Any standards compliant implementation of C, including mspgcc, supports this feature, with
the routines setjmp() and longjmp(). The following example illustrates their use, with two
destinations to jump to. You may define any number of destinations. You may jumps from
anywhere, even from an interrupt service routine, and the stack will be properly cleaned up:
#include <setjmp.h>
jmp_buf __jmp_a; /* make this global */
jmp_buf __jmp_b; /* make this global */
void first_place_to_jump_from(void)
{
...
if (something1)
longjmp(__jmp_a, 1);
if (something2)
longjmp(__jmp_b, 123);
...
}
type second_place_to_jump_from(...)
{
...
if (something3)
longjmp(__jmp_a, 2);
...
}
void the_place_to_jump_to(void)
{
int res;
...
res = setjmp(__jmp_a);
switch (res)
{
case 0:
/* Not a jump. Just normal program flow. */
res = setjmp(__jmp_b);
switch (res)
{
case 0:
/* Not a jump. Just normal program flow. */
break;
case 123:
/* We jumped here from the first subroutine */
break;
}
break;
case 1:
/* We jumped here from the first subroutine */
break;
case 2:
/* We jumped here from second routine */
break;
}
for (;;)
{
first_place_to_jump_from();
second_place_to_jump_from();
}
}
|