Go Down

Topic: Is there a way to determine the size of a function runtime? (solved?) (Read 2563 times) previous topic - next topic

Coding Badly


Some inline assembly and an illegal opcode (or rarely used opcode) may do the trick...

Code: [Select]
int f()
{
  return 25;
  asm volatile(".dw 0xA5A5\n\t"::);
}

int g()
{
  return f() * f() * 3;
  asm volatile(".dw 0xA5A5\n\t"::);
}


(I believe ".dw" is used to output a word sized value.  0xA5A5 is just an example; I have no idea if it is or is not an illegal opcode.)

robtillaart

@Graynomad
That idea had crossed my mind too, could be just a mask & compare:   POP = 1001 000d dddd 1111
would improve the quality IF a POP is present. A small function like
int f()
{
 return 42;
}
does not push / pop,

so the algo should count the PUSHes at the begin and match them with POPs at the end,
optionally take into account the reverse order of registers.

maybe the runtime disassembler idea is not a bad one after all. Needs: MEGA + graduation project + student...

@Coding Badly
injecting code/tags this way is fun, however it might affect the optimized code under test ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Coding Badly

@Coding Badly
injecting code/tags this way is fun, however it might affect the optimized code under test ;)


Yes, it will.  It will make the function one word larger but no slower nor faster.   ;)

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Coding Badly


robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Coding Badly


My money is on "preoccupied" rather than "stupid".  You are juggling a conversation here and on the Developers List (plus whatever else you're doing today).

Coding Badly


Argh.  It does not work.  This...

Code: [Select]
static void Do_Nothing( void )
{
  return;
  asm volatile(".word  0xA5A5\n\t");
}


...produces this...

Code: [Select]
00000faa <_ZL10Do_Nothingv>:
     faa: 08 95        ret



This...

Code: [Select]
static void Do_Nothing( void )
{
  asm volatile(".word  0xA5A5\n\t");
  return;
}


...produces this (demonstrating that the asm directive does something; just not what we need)...

Code: [Select]
00000faa <_ZL10Do_Nothingv>:
     faa: a5 a5        ldd r26, Z+45 ; 0x2d
     fac: 08 95        ret



Apparently, "volatile" is taken as a suggestion rather than a command.

Graynomad

#23
Dec 28, 2013, 02:26 am Last Edit: Dec 28, 2013, 02:29 am by Graynomad Reason: 1
Interesting, so regardless of the volatile it decided that the "code" is outside a function and not used. 

So much for volatile.

What does an AVR do on encountering an invalid opcode? If it does nothing then you could still put one inside the return and adjust the printed result by -2.

EDIT: In the first example did it move the A5A5 to variable space? After all it's assuming that something may change it.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Coding Badly

Interesting, so regardless of the volatile it decided that the "code" is outside a function and not used. 


Exactly.  Originally, I had a syntax error in the asm.  The code compiled without error indicating the compiler completely stripped it away before sending it to the assembly.

Quote
What does an AVR do on encountering an invalid opcode?


Ugh.  I can't find a reference but I believe invalid opcodes are executed as NOPs.

Quote
If it does nothing then you could still put one inside the return and adjust the printed result by -2.


True.  Actually, the compiler never outputs NOPs so putting one just before the return would work.

Quote
EDIT: In the first example did it move the A5A5 to variable space?


Naw.  It's just gone.

robtillaart

#25
Dec 28, 2013, 01:04 pm Last Edit: Dec 28, 2013, 01:06 pm by robtillaart Reason: 1


Argh.  It does not work.  This...

Code: [Select]
static void Do_Nothing( void )
{
 return;
 asm volatile(".word  0xA5A5\n\t");
}

...
Apparently, "volatile" is taken as a suggestion rather than a command.

It is unreachable code that way... so can be optimized.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

westfw

Quote
I believe invalid opcodes are executed as NOPs

Nope.  Invalid opcodes have "undefined" results.  Even some "valid" opcodes have undefined results.
Clever programmers have been known to figure out what the results are and use those opcodes (very dangerous!) or use the results to detect type of CPU (xx opcode does X on cpu Y, but does X2 on cpu Z)
ARM-based Arduinos may actually trap "invalid opcodes" as errors, and then ... do something.
My favorite CPU used to trap "UnUsed Operations", and used about half of them for implementing operating system calls, and about half were available for the users to write their own instructions.  (on this particular CPU, the address calculations were extremely regular and happened before opcode decode, so your user-implemented instructions ended up supporting all the fancy addressing modes of all the normal instructions.)

Go Up