view test/stackframe.c @ 927:b491db049d74

Added tag current-release for changeset 4e73a22327a8
author kono
date Sun, 13 Apr 2014 10:20:02 +0900
parents 5313ed059cee
children
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("#0029: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("#0035: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("#0051:int  0 offset: %08x\n",&a0-&i0);
    printf("#0052:int  1 offset: %08x\n",&a0-&i1);
    printf("#0053:int  1 offset: %08x\n",&a0-(int*)float_frame(A1));
    printf("#0054:%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("#0068:local0 offset: %08x\n",&a0-&i0);
    printf("#0069:local1 offset: %08x\n",&a0-&i1);
    printf("#0070:%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("#0092:alloca %08x\n",top);
    } else {
        top = &l0;
    }
    printf("#0096: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("#0104:%08x: %08x ret address\n",p,i);
        } else if (*p==A0) {
            printf("#0106:%08x: %08x caller arg 1\n",p,i);
        } else if (*p==A2) {
            printf("#0108:%08x: %08x caller arg last\n",p,i);
        } else if (*p==A3) {
            printf("#0110:%08x: %08x callee arg 1\n",p,i);
        } else if (*p==A4) {
            printf("#0112:%08x: %08x callee arg last\n",p,i);
        } else if (*p==L0) {
            printf("#0114:%08x: %08x local var top\n",p,i);
        } else if (*p==L1) {
            printf("#0116:%08x: %08x local var end\n",p,i);
        } else if (*p==L2) {
            printf("#0118:%08x: %08x stack top (alloca)\n",p,i);
        } else {
            // printf("#0120: %08x",*p);
        }
        i+= sizeof(int);
        p++;
    }
}


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