view test/stackframe.c @ 725:3f1f6c0610c1

goto with enviornment syntax changed.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 20 Aug 2009 16:39:02 +0900
parents 5e71527f9fd6
children c2c709727221
line wrap: on
line source

extern int printf(const char *,...);
// extern void *alloca(int);

#define A0 0xa0a0a0a0
#define A1 0xa1a1a1a1
#define A2 0xa2a2a2a2
#define A3 0xa3a3a3a3
#define A4 0xa4a4a4a4

#define L0 0xb0b0b0b0
#define L1 0xb1b1b1b1
#define L2 0xb2b2b2b2
#define L3 0xb3b3b3b3
#define L4 0xb4b4b4b4

void *ret;

int dum;

void *float_frame(int a0);

static inline void frame_pointer(char *s) __attribute__((always_inline));

static inline void frame_pointer(char *s)
{
#ifdef i386
    void *fr,*st;
    __asm__("\tmovl %%ebp,%0\n": "=r" (fr));
    __asm__("\tmovl %%esp,%0\n": "=r" (st));
    printf("fr=%08x st=%08x at %s\n",fr,st,s);
#else
#ifdef ppc
    void *fr,*st;
    __asm__("\tmr %0,31\n": "=r" (fr));
    __asm__("\tmr %0,1\n": "=r" (st));
    printf("fr=%08x st=%08x at %s\n",fr,st,s);
#endif
#endif
}


int
int_frame(int a0)
{
    int i0;
    int f0;
    int *p;
    int i1;
    frame_pointer("int frame");

    i0 = f0 = 0.3;
    printf("int  0 offset: %08x\n",&a0-&i0);
    printf("int  1 offset: %08x\n",&a0-&i1);
    printf("int  1 offset: %08x\n",&a0-(int*)float_frame(A1));
    printf("%d\n",f0);
    return i0;
}

void *
float_frame(int a0)
{
    int i0;
    double f0;
    int *p;
    int i1;
    frame_pointer("float frame");

    f0 = 0.3;
    printf("local0 offset: %08x\n",&a0-&i0);
    printf("local1 offset: %08x\n",&a0-&i1);
    printf("%g\n",f0);
    return &i0;
}

void
called(int a0,int a1,int a2)
{
    frame_pointer("called");
    dum = a0+a1+a2;
}

void
frame(int a0,int a1,int a2)
{
    int l0 = L0;
    int *top;
    int *p;
    int i = 0;
    int l1 = L1;
    frame_pointer("frame");
    if (a1==1) {
	top = (int *)__builtin_alloca(16);
	printf("alloca %08x\n",top);
    } else {
	top = &l0;
    }
    printf("local  %08x\n",&l0);

    *top = L2;
    called(A3,A1,A4);
#define OFFSET 16
    i = -OFFSET;
    for(p = top-OFFSET;p<top+0x100; ) {
	if (*p==(int)ret) {
	    printf("%08x: %08x ret address\n",p,i);
	} else if (*p==A0) {
	    printf("%08x: %08x caller arg 1\n",p,i);
	} else if (*p==A2) {
	    printf("%08x: %08x caller arg last\n",p,i);
	} else if (*p==A3) {
	    printf("%08x: %08x callee arg 1\n",p,i);
	} else if (*p==A4) {
	    printf("%08x: %08x callee arg last\n",p,i);
	} else if (*p==L0) {
	    printf("%08x: %08x local var top\n",p,i);
	} else if (*p==L1) {
	    printf("%08x: %08x local var end\n",p,i);
	} else if (*p==L2) {
	    printf("%08x: %08x stack top (alloca)\n",p,i);
	} else {
	    // printf(" %08x",*p);
	}
	i+= sizeof(int);
	p++;
    }
}


int
main()
{
    frame_pointer("main");
    ret = &&label;
    frame(A0,A1,A2);
    frame(A0,1,A2);
    printf("\n");
    float_frame(A0);
    int_frame(A0);
label:
    return 0;
}