GCC optimizing my code away

Programming, for all ages and all languages.
Post Reply
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

GCC optimizing my code away

Post by FlashBurn »

I compile all my source files with the -O2 flag.

I want to switch on the A20 gate and have the following code:

Code: Select all

uint32t enableA20Gate() {
	struct x86Regs_t regs= { 0x2401, 0, 0, 0, 0, 0, 0, 0 };
	uint32t flags, tmp;
	uint16t *test1= (uint16t *)0x500, *test2= (uint16t *)0x100500;
	
	*test1= 0;
	*test2= 1;	

	if(*test1 != *test2)
		return 1;
	
	flags= callBIOS(0x15,&regs);
	
	if(flags & FLAG_CF) {
		tmp= inb(0x92);
		tmp|= 2;
		outb(0x92,tmp);
	}
	
	*test2= 1;
	
	if(*test1 != *test2)
		return 1;

	waitKbd();
	outb(0x64,0xad);
	
	waitKbd();
	outb(0x64,0xd0);
	
	waitKbdData();
	tmp= inb(0x60);
	
	waitKbd();
	outb(0x64,0xd1);
	
	waitKbd();
	outb(0x60,tmp | 2);
	
	waitKbd();
	outb(0x64,0xae);
	
	waitKbd();
	
	*test2= 1;
	
	if(*test1 != *test2)
		return 1;
	
	return 0;
}
I use gcc 4.3.1 for compiling and if I compile this code gcc only makes assembly code so that I write 0 at 0x500 and 1 at 0x100500 and because for it the if statement is always true it doesn´t compile the rest of the function. How can I say to gcc (w/o creating an extra entry in my makefile for this source file) that it has not to optimize this code away?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: GCC optimizing my code away

Post by Combuster »

have you told GCC that you need the operation when you do inline assembly? (hint: try __asm__ __volatile__)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Re: GCC optimizing my code away

Post by FlashBurn »

Yes, I use it and I also tried the "#pragma optimize("",off)", but gcc ignores it. I think the best and easiest way to solve it, is to write the code in assembly or is here anyone who has another solution I can try?
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: GCC optimizing my code away

Post by ru2aqare »

Try declaring the variables as volatile, that hints the compiler that for example their value may change anytime.
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Re: GCC optimizing my code away

Post by FlashBurn »

Thanks, that did it. Now it compiles to the code I need!
Post Reply