Alright, today I had the strangest coding situation ever.
Have a look at this:
We all agree that this condition should never be true?
Alright, but here it is, right in the top left corner of my screen:
Screenshot.png [ 3.18 KiB | Viewed 8798 times ]
This is what cc65 created out of it:
Would you say this is correct code? I mean, where is the number 7 here? Where is the actual comparison?
If I change the code to MyVariable < 100, it's still the same.
O.k., maybe the compiler already optimizes the comparison since the variable always has the same value. But then, how is it possible that the HeroEnergy variable is ever set by this function?
Also, please note that the function does and doesn't produce the strange behavior, dependent on where I call it. I haven't really found out the reason, but if I have code where I do several things and then I call this function here, the question whether the hero's energy is changed is dependent on where I call the function. Sometimes it changes the energy, sometimes it doesn't.
Also, when I assign a value less than 256 to the variable, the compiler immediately optimizes the condition away:
But for values from 256 and larger, it doesn't do the complete optimization, even though the condition result should be the same.
Does anybody have any idea what's going on here?
Have a look at this:
Code:
unsigned int MyVariable;
void VeryStrangeFunction(void)
{
MyVariable = 259;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}
void VeryStrangeFunction(void)
{
MyVariable = 259;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}
We all agree that this condition should never be true?
Alright, but here it is, right in the top left corner of my screen:
Attachment:
Screenshot.png [ 3.18 KiB | Viewed 8798 times ]
This is what cc65 created out of it:
Code:
.segment "CODE"
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$01
lda #$03
sta _MyVariable
stx _MyVariable+1
bcs L0681
lda #$D9
sta _Saveable+13
stx _Saveable+13+1
L0681: rts
.endproc
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$01
lda #$03
sta _MyVariable
stx _MyVariable+1
bcs L0681
lda #$D9
sta _Saveable+13
stx _Saveable+13+1
L0681: rts
.endproc
Would you say this is correct code? I mean, where is the number 7 here? Where is the actual comparison?
If I change the code to MyVariable < 100, it's still the same.
O.k., maybe the compiler already optimizes the comparison since the variable always has the same value. But then, how is it possible that the HeroEnergy variable is ever set by this function?
Also, please note that the function does and doesn't produce the strange behavior, dependent on where I call it. I haven't really found out the reason, but if I have code where I do several things and then I call this function here, the question whether the hero's energy is changed is dependent on where I call the function. Sometimes it changes the energy, sometimes it doesn't.
Also, when I assign a value less than 256 to the variable, the compiler immediately optimizes the condition away:
Code:
void VeryStrangeFunction(void)
{
MyVariable = 250;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}
{
MyVariable = 250;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}
Code:
.segment "CODE"
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$00
lda #$FA
sta _MyVariable
stx _MyVariable+1
rts
.endproc
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$00
lda #$FA
sta _MyVariable
stx _MyVariable+1
rts
.endproc
But for values from 256 and larger, it doesn't do the complete optimization, even though the condition result should be the same.
Does anybody have any idea what's going on here?