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