X
  • Work
    November 19, 2014

Writing inline templates

Guest Author

Writing some inline templates today... I've written about doing this kind of stuff in the past here and, in more detail, here.

I happen to need to pass a bundle of parameters on to the routine. The best way of checking how the parameters will be passed is to get the compiler to provide some initial template. Here's an example routine:

int parameters (int p0, int * p1, int * p2, int* p3, int * p4, int * p5, int * p6, int p7)
{
return p0 + *p1 + *p2 + *p3 + *p4 + ((*p5)<<2) + ((*p6)<<3) + p7*p7;
}

In the routine I've tried to handle some of the parameters differently. I know that the first parameters get passed in registers, and then the later ones get passed on the stack. By handling them differently I can work out which loads from the stack correspond to which variables. The disassembly looks like:

-bash-4.1$ cc -g -O parameters.c -c
-bash-4.1$ dis -F parameters parameters.o
disassembly for parameters.o
parameters()
parameters: ca 02 60 00 ld [%o1], %g5
parameters+0x4: c4 02 e0 00 ld [%o3], %g2
parameters+0x8: c2 02 a0 00 ld [%o2], %g1
parameters+0xc: c6 03 a0 60 ld [%sp + 0x60], %g3 // load of p7
parameters+0x10: 88 02 00 05 add %o0, %g5, %g4
parameters+0x14: d0 03 60 00 ld [%o5], %o0
parameters+0x18: ca 03 20 00 ld [%o4], %g5
parameters+0x1c: 92 00 80 01 add %g2, %g1, %o1
parameters+0x20: 87 38 e0 00 sra %g3, 0x0, %g3
parameters+0x24: 82 01 00 09 add %g4, %o1, %g1
parameters+0x28: d2 03 a0 5c ld [%sp + 0x5c], %o1 // load of p6
parameters+0x2c: 88 48 c0 03 mulx %g3, %g3, %g4 // %g4 = %g3*%g3
parameters+0x30: 97 2a 20 02 sll %o0, 0x2, %o3
parameters+0x34: 94 00 40 05 add %g1, %g5, %o2
parameters+0x38: da 02 60 00 ld [%o1], %o5
parameters+0x3c: 84 02 c0 0a add %o3, %o2, %g2
parameters+0x40: 99 2b 60 03 sll %o5, 0x3, %o4 // %o4 = %o5<<3
parameters+0x44: 90 00 80 0c add %g2, %o4, %o0
parameters+0x48: 81 c3 e0 08 retl
parameters+0x4c: 90 02 00 04 add %o0, %g4, %o0

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha