Mercurial > hg > CbC > old > device
annotate test/stackframe.c @ 885:1a027275743d
struct returinng function have to be indirect.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 05 Apr 2014 21:12:43 +0900 |
parents | 5313ed059cee |
children |
rev | line source |
---|---|
667 | 1 extern int printf(const char *,...); |
2 // extern void *alloca(int); | |
3 | |
4 #define A0 0xa0a0a0a0 | |
5 #define A1 0xa1a1a1a1 | |
6 #define A2 0xa2a2a2a2 | |
7 #define A3 0xa3a3a3a3 | |
8 #define A4 0xa4a4a4a4 | |
9 | |
10 #define L0 0xb0b0b0b0 | |
11 #define L1 0xb1b1b1b1 | |
12 #define L2 0xb2b2b2b2 | |
13 #define L3 0xb3b3b3b3 | |
14 #define L4 0xb4b4b4b4 | |
15 | |
16 void *ret; | |
17 | |
669 | 18 int dum; |
19 | |
673 | 20 void *float_frame(int a0); |
21 | |
22 static inline void frame_pointer(char *s) __attribute__((always_inline)); | |
23 | |
24 static inline void frame_pointer(char *s) | |
25 { | |
676 | 26 #ifdef i386 |
27 void *fr,*st; | |
28 __asm__("\tmovl %%ebp,%0\n": "=r" (fr)); | |
29 __asm__("\tmovl %%esp,%0\n": "=r" (st)); | |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
30 printf("#0029:fr=%08x st=%08x at %s\n",fr,st,s); |
676 | 31 #else |
32 #ifdef ppc | |
673 | 33 void *fr,*st; |
34 __asm__("\tmr %0,31\n": "=r" (fr)); | |
35 __asm__("\tmr %0,1\n": "=r" (st)); | |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
36 printf("#0035:fr=%08x st=%08x at %s\n",fr,st,s); |
676 | 37 #endif |
38 #endif | |
673 | 39 } |
40 | |
41 | |
42 int | |
43 int_frame(int a0) | |
44 { | |
45 int i0; | |
46 int f0; | |
47 int *p; | |
48 int i1; | |
49 frame_pointer("int frame"); | |
50 | |
51 i0 = f0 = 0.3; | |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
52 printf("#0051:int 0 offset: %08x\n",&a0-&i0); |
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
53 printf("#0052:int 1 offset: %08x\n",&a0-&i1); |
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
54 printf("#0053:int 1 offset: %08x\n",&a0-(int*)float_frame(A1)); |
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
55 printf("#0054:%d\n",f0); |
673 | 56 return i0; |
57 } | |
58 | |
59 void * | |
60 float_frame(int a0) | |
61 { | |
62 int i0; | |
63 double f0; | |
64 int *p; | |
65 int i1; | |
66 frame_pointer("float frame"); | |
67 | |
68 f0 = 0.3; | |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
69 printf("#0068:local0 offset: %08x\n",&a0-&i0); |
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
70 printf("#0069:local1 offset: %08x\n",&a0-&i1); |
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
71 printf("#0070:%g\n",f0); |
673 | 72 return &i0; |
73 } | |
74 | |
669 | 75 void |
76 called(int a0,int a1,int a2) | |
77 { | |
673 | 78 frame_pointer("called"); |
669 | 79 dum = a0+a1+a2; |
80 } | |
81 | |
667 | 82 void |
83 frame(int a0,int a1,int a2) | |
84 { | |
85 int l0 = L0; | |
676 | 86 int *top; |
667 | 87 int *p; |
88 int i = 0; | |
89 int l1 = L1; | |
673 | 90 frame_pointer("frame"); |
676 | 91 if (a1==1) { |
880 | 92 top = (int *)__builtin_alloca(16); |
93 printf("#0092:alloca %08x\n",top); | |
676 | 94 } else { |
880 | 95 top = &l0; |
676 | 96 } |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
97 printf("#0096:local %08x\n",&l0); |
667 | 98 |
99 *top = L2; | |
669 | 100 called(A3,A1,A4); |
101 #define OFFSET 16 | |
102 i = -OFFSET; | |
103 for(p = top-OFFSET;p<top+0x100; ) { | |
880 | 104 if (*p==(int)ret) { |
105 printf("#0104:%08x: %08x ret address\n",p,i); | |
106 } else if (*p==A0) { | |
107 printf("#0106:%08x: %08x caller arg 1\n",p,i); | |
108 } else if (*p==A2) { | |
109 printf("#0108:%08x: %08x caller arg last\n",p,i); | |
110 } else if (*p==A3) { | |
111 printf("#0110:%08x: %08x callee arg 1\n",p,i); | |
112 } else if (*p==A4) { | |
113 printf("#0112:%08x: %08x callee arg last\n",p,i); | |
114 } else if (*p==L0) { | |
115 printf("#0114:%08x: %08x local var top\n",p,i); | |
116 } else if (*p==L1) { | |
117 printf("#0116:%08x: %08x local var end\n",p,i); | |
118 } else if (*p==L2) { | |
119 printf("#0118:%08x: %08x stack top (alloca)\n",p,i); | |
120 } else { | |
121 // printf("#0120: %08x",*p); | |
122 } | |
123 i+= sizeof(int); | |
124 p++; | |
667 | 125 } |
126 } | |
127 | |
128 | |
129 int | |
130 main() | |
131 { | |
673 | 132 frame_pointer("main"); |
667 | 133 ret = &&label; |
134 frame(A0,A1,A2); | |
676 | 135 frame(A0,1,A2); |
748
c2c709727221
i64 continue... basic.s assembled.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
676
diff
changeset
|
136 printf("#0135:\n"); |
673 | 137 float_frame(A0); |
138 int_frame(A0); | |
667 | 139 label: |
140 return 0; | |
141 } |