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

Redundancy in compiled form of UNTIL #3094

Open
NCGThompson opened this issue Sep 10, 2023 · 0 comments
Open

Redundancy in compiled form of UNTIL #3094

NCGThompson opened this issue Sep 10, 2023 · 0 comments

Comments

@NCGThompson
Copy link

There is no problem, but by visual inspection of the compiler code, I can see that it outputs a not operation in front of a branch-if-false. This is a double negative.

AddOpcode(new OpcodeLogicNot());
Opcode branch = AddOpcode(new OpcodeBranchIfFalse());

It would be logically equivalent to omit the not and use a branch-if-true. This section of code was written before branch-if-true was added.

There is a difference. not has a try catch that the others do not. For reference, branch-if-false:

public override void Execute(ICpu cpu)
{
bool condition = Convert.ToBoolean(cpu.PopValueArgument());
DeltaInstructionPointer = !condition ? Distance : 1;
}

branch-if-true:
public override void Execute(ICpu cpu)
{
bool condition = Convert.ToBoolean(cpu.PopValueArgument());
DeltaInstructionPointer = condition ? Distance : 1;
}

and not:
public override void Execute(ICpu cpu)
{
object value = cpu.PopValueArgument();
object result;
// Convert to bool instead of cast in case the identifier is stored
// as an encapsulated BooleanValue, preventing an unboxing collision.
// Wrapped in a try/catch since the Convert framework doesn't have a
// way to determine if a type can be converted.
try
{
result = !Convert.ToBoolean(value);
}
catch
{
throw new KOSCastException(value.GetType(), typeof(BooleanValue));
}
cpu.PushArgumentStack(Structure.FromPrimitive(result));
}

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

1 participant