27db190efdb7b7973d0fddccdc967601dcad9da7
[mono.git] / mono / arch / alpha / test.c
1 #include "alpha-codegen.h"
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 /* A typical Alpha stack frame looks like this */
10 /*
11 fun:                         // called from outside the module.
12         ldgp gp,0(pv)        // load the global pointer
13 fun..ng:                     // called from inside the module.
14         lda sp, -SIZE( sp )  // grow the stack downwards.
15
16         stq ra, 0(sp)        // save the return address.
17
18         stq s0, 8(sp)        // callee-saved registers.
19         stq s1, 16(sp)       // ...
20
21         // Move the arguments to the argument registers...
22         
23         mov addr, pv         // Load the callee address
24         jsr  ra, (pv)        // call the method.
25         ldgp gp, 0(ra)       // restore gp
26
27         // return value is in v0
28         
29         ldq ra, 0(sp)        // free stack frame
30         ldq s0, 8(sp)        // restore callee-saved registers.
31         ldq s1, 16(sp)       
32         ldq sp, 32(sp)       // restore stack pointer
33
34         ret zero, (ra), 1    // return.
35 */
36
37
38
39 //
40 // Simple function which returns 10.
41 //
42 int testfunc()
43 {
44     return 10;
45 }
46
47 // Write it using the known asm bytecodes.
48 char * write_testfunc_1( char * p )
49 {
50 //
51 //                               ldah     gp, 0(pv)
52 //                               lda      gp, 0(gp)
53 //00000001200004d0 <testfunc>:
54 //   1200004d0:   f0 ff de 23     lda     sp,-16(sp)
55 //   1200004d4:   00 00 5e b7     stq     ra,0(sp)
56 //   1200004d8:   08 00 fe b5     stq     fp,8(sp)
57 //   1200004dc:   0f 04 fe 47     mov     sp,fp
58 //   1200004e0:   0a 00 3f 20     lda     t0,10
59 //   1200004e4:   00 04 e1 47     mov     t0,v0
60 //   1200004e8:   1e 04 ef 47     mov     fp,sp
61 //   1200004ec:   00 00 5e a7     ldq     ra,0(sp)
62 //   1200004f0:   08 00 fe a5     ldq     fp,8(sp)
63 //   1200004f4:   10 00 de 23     lda     sp,16(sp)
64 //   1200004f8:   01 80 fa 6b     ret
65
66 int _func_code[] = {
67     0x23defff0,
68     0xb75e0000,
69     0xb5fe0008,
70     0x47fe040f,
71     0x203f000a,
72     0x47e10400,    
73     0x47ef041e,
74     0xa75e0000,
75     0xa5fe0008,
76     0x23de0010,
77     0x6bfa8001 };
78
79     memcpy( p , _func_code, 4 * 11 );
80     return p + ( 4 * 11 );
81 }
82
83 // The same function encoded with alpha-codegen.h
84 char * write_testfunc_2( char * p )
85 {    
86     alpha_ldah( p, alpha_gp, alpha_pv, 0 );  // start the gp load
87     alpha_lda( p, alpha_sp, alpha_sp, -16 ); // allocate the stack
88     alpha_lda( p, alpha_gp, alpha_gp, 0 );   // finish the gp load
89     alpha_stq( p, alpha_ra, alpha_sp, 0 );   // start param save.
90     alpha_stq( p, alpha_fp, alpha_sp, 8 );
91     alpha_mov1( p, alpha_sp, alpha_fp );
92     alpha_lda( p, alpha_t0, alpha_zero, 10 );
93     alpha_mov1( p, alpha_t0, alpha_v0 );
94     alpha_mov1( p, alpha_fp, alpha_sp );
95     alpha_ldq( p, alpha_ra, alpha_sp, 0 );
96     alpha_ldq( p, alpha_fp, alpha_sp, 8 );
97     alpha_lda( p, alpha_sp, alpha_sp, 16 );
98
99     alpha_ret( p, alpha_ra, 1 );
100
101     return p;
102 }
103
104
105 void output( char * p, int len )
106 {
107         char * maxp = p + len;
108         char * cp = p;
109
110         printf (".text\n.align 4\n.globl main\n.type main,@function\nmain:\n");
111         for ( ; cp < maxp; cp++ ) 
112         {
113                 printf (".byte 0x%0.2x\n", (*cp&0x00ff) );
114         }
115
116         int fd = open( "bad.out", O_CREAT | O_TRUNC );
117         write( fd, p, len );
118         close( fd );
119 }
120
121 int main( int argc, char ** argv ) {
122         char code [16000];
123         char *p = code;
124         char * cp;
125
126         int (*x)() = 0;
127         int y = 0;
128         int z = 10;
129
130         // so, `test blah` gets you the byte-encoded function.
131         // and  `test` gets you the alpha-codegen.h encoded function.
132
133         if( argc > 1 )
134         {
135             p = write_testfunc_1( p );
136         }
137         else
138         {
139             p = write_testfunc_2( p );
140         }
141
142         // output( code, p-code );
143
144         // call the procedure.
145         x = (int(*)())code;
146
147         while( z-- > 0 )
148             y = x();
149
150         return 0;
151 }
152