1 #include "alpha-codegen.h"
11 /* A typical Alpha stack frame looks like this */
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.
18 stq ra, 0(sp) // save the return address.
20 stq s0, 8(sp) // callee-saved registers.
23 // Move the arguments to the argument registers...
25 mov addr, pv // Load the callee address
26 jsr ra, (pv) // call the method.
27 ldgp gp, 0(ra) // restore gp
29 // return value is in v0
31 ldq ra, 0(sp) // free stack frame
32 ldq s0, 8(sp) // restore callee-saved registers.
34 ldq sp, 32(sp) // restore stack pointer
36 ret zero, (ra), 1 // return.
42 // Simple function which returns 10.
44 static int testfunc(void)
49 // Write it using the known asm bytecodes.
50 static unsigned int * write_testfunc_1(unsigned int * p )
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
81 memcpy( p , _func_code, 4 * 11 );
82 return p + ( 4 * 11 );
85 // The same function encoded with alpha-codegen.h
86 unsigned int * write_testfunc_2( unsigned int * p )
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 );
101 alpha_ret( p, alpha_ra, 1 );
107 void output( char * p, int len )
109 char * maxp = p + len;
112 printf (".text\n.align 4\n.globl main\n.type main,@function\nmain:\n");
113 for ( ; cp < maxp; cp++ )
115 printf (".byte 0x%0.2x\n", (*cp&0x00ff) );
118 int fd = open( "bad.out", O_CREAT | O_TRUNC );
123 unsigned int code [16000/4];
125 int main( int argc, char ** argv ) {
126 // unsigned int code [16000/4];
127 unsigned int *p = code;
134 // so, `test blah` gets you the byte-encoded function.
135 // and `test` gets you the alpha-codegen.h encoded function.
139 p = write_testfunc_1( p );
143 p = write_testfunc_2( p );
146 // output( code, p-code );
148 // call the procedure.