NO

Author Topic: how to avoid optimization in the middle of a function  (Read 3079 times)

whatsup

  • Guest
how to avoid optimization in the middle of a function
« on: April 25, 2010, 11:16:02 PM »
I'm trying to put a blcok of code in a function
this code can be called from some places in the function with asm call
the problem start when i'm using optimization compliation

i have to put a goto instruction before this block of code

example:

Code: [Select]
 goto somewhere;  //this line has to be here.

BlockOfCode:
DoSomthing();
_asm ret

now when there is no optimization this code works fine no problem
but when optimization is on
the compiler see the block of code as unreachable,
and so ignore it
and i get error that the label "BlockOfCode" is not recognized.

BTW i think this is kind a PellesC bug, because if there is a label preceding the code
the code is surely accessable.

anyway, i tried to turn off the optimization:

Code: [Select]
#pragma optimize(none)
  goto somewhere;

BlockOfCode:
DoSomthing();
_asm ret
#pragma optimize()

but i still get the error, only if the general optimization is off, i don't get any error

is there something to do about this ?
i mean i still want optimization, but i don't want to get error on this code.

thanks ahead.

whatsup

  • Guest
Re: how to avoid optimization in the middle of a function
« Reply #1 on: April 26, 2010, 06:52:29 AM »
Quote
i found a workaround for this issue
i'm using asm jmp instead of goto
but i still would like to know if there is other solution.
my mistake this is not working,
i found what's going on,
when optimize is on,
and the compiler reach a branch instruction: jmp, return , etc..
he skip all the code below, doesn't compile t at all,
so if there was a jump to this code from somewhere else,
there will be an error

example:
Code: [Select]

dosomething();
_asm call my_label
return;

my_label:

// anything i write here, will be ignored, when optimize is on,
//this is a bug, because if there is a label, this code can be called from somewhere else
//and it shouldn't be skipped.

exit0000:;

anyway at the meantime i found a workaround,
instead of direct jmp, i'm using indirect:

Code: [Select]
  // instead of return, i'm usign jmp to exit0000 like this:
  {_asm mov eax, exit0000 _asm jmp eax}
« Last Edit: April 27, 2010, 08:25:38 AM by whatsup »

whatsup

  • Guest
Re: how to avoid optimization in the middle of a function
« Reply #2 on: April 26, 2010, 08:58:18 PM »
No one know the answer ?  ??? :'(

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: how to avoid optimization in the middle of a function
« Reply #3 on: April 27, 2010, 07:54:20 PM »
If you have an unconditional return in a function, all code behind is unreachable, because return leaves the function.

Perhaps a possible workaround would be:
Code: [Select]
volatile int hide=1;        // volatile to hide the value to the compiler

int main()
{


  if (hide) return 0;     // the compiler does not see, that hide is always true
my_label:

// so this code should not be eliminated

exit0000:;
// there is possible a return necessary
}

But this style of programming C is not very fine.
« Last Edit: April 27, 2010, 07:55:54 PM by AlexN »
best regards
 Alex ;)

Offline Vortex

  • Member
  • *
  • Posts: 867
    • http://www.vortex.masmcode.com
Re: how to avoid optimization in the middle of a function
« Reply #4 on: April 27, 2010, 10:19:28 PM »
Hi whatsup,

Concerning mixing C with assembly, the best solution is to move the assembly code to a separate .asm module. Inline assembly interfers with the compiler's optimization engine.
Code it... That's all...