Pages: 1 [2]   Go Down
Author Topic: Is there a way to determine the size of a function runtime? (solved?)  (Read 1382 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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.)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 smiley-wink
Logged

Rob Tillaart

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yes, it will.  It will make the function one word larger but no slower nor faster.   smiley-wink
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what I meant was it will affect its behaviour
Logged

Rob Tillaart

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


How?  The eye-catcher is after the return.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

aaagh, stupid me smiley
Logged

Rob Tillaart

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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).
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Argh.  It does not work.  This...

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

...produces this...

Code:
00000faa <_ZL10Do_Nothingv>:
     faa: 08 95        ret


This...

Code:
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:
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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8439
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: December 27, 2013, 08:29:17 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Argh.  It does not work.  This...

Code:
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.
« Last Edit: December 28, 2013, 07:06:04 am by robtillaart » Logged

Rob Tillaart

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

SF Bay Area (USA)
Online Online
Tesla Member
***
Karma: 106
Posts: 6372
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

Pages: 1 [2]   Go Up
Jump to: