Open esaulenka opened 6 days ago
I think the issue here is mainly the OV
flag macro.
macro set_OV_neg(var1, var2)
{
local A:4 = var1;
local B:4 = var2;
local R = A - B;
local A1 = A[31,1];
local B1 = B[31,1];
local R1 = R[31,1];
$(OV) = (A1 != B1) && (B1 == R1);
#OV = 1 if:
#pos - neg = neg
#neg - pos = pos
}
the local A1 = A[31,1];
leads to some messy decompilation. What it's really doing is testing for sign, so a better format is probably:
macro set_OV_neg(var1, var2)
{
local A:4 = var1;
local B:4 = var2;
local R = A - B;
local A1 = A s< 0;
local B1 = B s< 0;
local R1 = R s< 0;
$(OV) = (A1 != B1) && (B1 == R1);
#OV = 1 if:
#pos - neg = neg
#neg - pos = pos
}
Thanks, that's a good point.
Decompiled code become a bit more clear
... while (iVar5 + -8 < 0 != (iVar5 < 0 && -1 < iVar5 + -8));
but still dirty.
PS if you try to compile it now, this loop runs exactly 8 times, as expected.
The condition (iVar5 + -8 < 0 != (iVar5 < 0 && -1 < iVar5 + -8))
is equivalent to (iVar5 < 8)
, as can be shown using the following derivation:
(iVar5 + -8 < 0 != (iVar5 < 0 && -1 < iVar5 + -8))
= { add 8 to both sides of equations involving 'iVar5 + -8' }
(iVar5 + -8 + 8 < 8 != (iVar5 < 0 && -1 + 8 < iVar5 + -8 + 8))
= { RuleCollapseConstants: -8 + 8 => 0, -1 + 8 == 7 }
(iVar5 + 0 < 8 != (iVar5 < 0 && 7 < iVar5))
= { RuleRangeMeld: (7 < x && x < 0) leaves no values for x, so this is always false }
(iVar5 < 8 != false)
= { RuleIdentityEl: (x + 0) -> x }
(iVar5 < 8 != (iVar5 < 0 && 7 < iVar5))
= { RuleBooleanNegate: bool != false => bool }
(iVar5 < 8)
It would be nice if RuleRangeMeld
could 'see through' the constant arithmetic, and also work for cases like (V s< 0) && (-1 s< V + -8)
. If that was the case, I think this expression would simplify nicely.
Describe the bug
Decompiler produces a weird code for sequence
SXB CMP BLT
(sign extend byte, compare, branch if less than). Perhaps it was a simple loop with aint8_t
counter.To Reproduce
Expected behavior
The inner loop is just a
do { .... } while (iVar6 < 8);
if I'm reading assembler correctly.Assembler:
Actual decompiled code:
I tried to find the bug, but it looks like both
CMP
andBLT
instructions written as described in the spec.Also I have checked other combinations, and sometimes it works a bit better:
ADDI loopLength, loopCounter, 0; BLT
makesdo { ... } while (iVar1 < 0 != SCARRY(iVar3, loopLength)
(see the second .hex sample)Environment (please complete the following information):
Additional context
original code, save it as .hex file
And this one decompiled bit better