1 #include "alpha-codegen.h"
9 /* A typical Alpha stack frame looks like this */
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.
16 stq ra, 0(sp) // save the return address.
18 stq s0, 8(sp) // callee-saved registers.
21 // Move the arguments to the argument registers...
23 mov addr, pv // Load the callee address
24 jsr ra, (pv) // call the method.
25 ldgp gp, 0(ra) // restore gp
27 // return value is in v0
29 ldq ra, 0(sp) // free stack frame
30 ldq s0, 8(sp) // restore callee-saved registers.
32 ldq sp, 32(sp) // restore stack pointer
34 ret zero, (ra), 1 // return.
40 // Simple function which returns 10.
47 // Write it using the known asm bytecodes.
48 char * write_testfunc_1( char * p )
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
79 memcpy( p , _func_code, 4 * 11 );
80 return p + ( 4 * 11 );
83 // The same function encoded with alpha-codegen.h
84 char * write_testfunc_2( char * p )
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 );
99 alpha_ret( p, alpha_ra, 1 );
105 void output( char * p, int len )
107 char * maxp = p + len;
110 printf (".text\n.align 4\n.globl main\n.type main,@function\nmain:\n");
111 for ( ; cp < maxp; cp++ )
113 printf (".byte 0x%0.2x\n", (*cp&0x00ff) );
116 int fd = open( "bad.out", O_CREAT | O_TRUNC );
121 int main( int argc, char ** argv ) {
130 // so, `test blah` gets you the byte-encoded function.
131 // and `test` gets you the alpha-codegen.h encoded function.
135 p = write_testfunc_1( p );
139 p = write_testfunc_2( p );
142 // output( code, p-code );
144 // call the procedure.