Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should convert if with a head controlled loop to a tail controlled loop #682

Open
rfalke opened this issue Sep 12, 2018 · 2 comments
Open

Comments

@rfalke
Copy link

rfalke commented Sep 12, 2018

Exe: https://github.com/rfalke/decompiler-subjects/blob/master/from_holdec/dmi/cfg/ia32_elf/subject.exe
Reko version: 46f203b

Input

int intermediate_11_loop_with_two_breaks(int arg)
{
	while (arg > 0) {
		puts("head");
		if (time(0) == arg) {
			break;
		}
		puts("tail");
		arg--;
	}
	return 0;
}

Output

// 080487E0: void intermediate_11_loop_with_two_breaks(Stack Eq_19 dwArg04)
void intermediate_11_loop_with_two_breaks(Eq_19 dwArg04)
{
	Eq_19 ebx_22 = dwArg04;
	if (dwArg04 > 0x00)
	{
		do
		{
			puts("head");
			if (time(null) == ebx_22)
				return;
			puts("tail");
			ebx_22 = ebx_22 - 0x01;
		} while (ebx_22 != 0x00);
	}
}

The output is more complex with the if. The decompiler has enough information to convert this to a head controlled while loop.

@uxmal uxmal closed this as completed in 4fec9a4 Sep 18, 2018
@uxmal
Copy link
Owner

uxmal commented Sep 18, 2018

Gated do-loops are now handled by Reko, if the condition in the if statement matches the condition in the do-loop. Sadly, Reko is failing to understand that dwArg04 and ebx_22 are the same variable, so this particular case remains broken.

@rfalke
Copy link
Author

rfalke commented Aug 26, 2023

Output of 0.11.4.0-931ca7d:

void intermediate_11_loop_with_two_breaks(Eq_10 dwArg04)
{
	Eq_10 ebx_22 = dwArg04;
	if (dwArg04 > 0x00)
	{
		do
		{
			puts("head");
			if (time(null) == ebx_22)
				return;
			puts("tail");
			--ebx_22;
		} while (ebx_22 != 0x00);
	}
}

The problem still exists.

I'm not sure if it is the variable aliasing which is the problem or that the fact that the conditions (x > 0x00 vs y != 0x00)) are not the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants