Skip to content

Commit

Permalink
#601 : optimize: if (nil == name) {} else {}
Browse files Browse the repository at this point in the history
  • Loading branch information
arakov committed May 6, 2024
1 parent 92a2124 commit 1312e08
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
1 change: 1 addition & 0 deletions dat/og/bt_rules60.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local_address, saving_stack, local_address, conversion_op, local_address, copyin
create_struct =4, assigning, local_address, saving_stack =0, local, copying_to_acc => 6;
intcondop, assigning, local, branchop => 7;
realcondop, assigning, local, branchop => 7;
nilcondop, assigning, local, branchop => 7;
int_real_op, local_address, copying => 10;
real_int_op, local_address, copying => 10;
direct_call_op, inplacemark, copying => 12;
Expand Down
1 change: 0 additions & 1 deletion doc/todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ In development:
- pi under 6 sec
- #601 : optimize: if (a == b) {} else {}
--------------------------------------
- #601 : optimize: if (nil == name) {} else {}

=== Iteration 23 ===
--------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion elenasrc3/engine/bcwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ void intLongOp(CommandTape& tape, BuildNode& node, TapeScope&)
default:
throw InternalError(errFatalError);
}

}

void realIntOp(CommandTape& tape, BuildNode& node, TapeScope&)
Expand Down Expand Up @@ -2449,6 +2448,9 @@ inline bool nativeBranchingOp(BuildNode lastNode)
case BuildKey::RealCondOp:
branchNode.setKey(BuildKey::RealBranchOp);
break;
case BuildKey::NilCondOp:
branchNode.setKey(BuildKey::NilRefBranchOp);
break;
default:
break;
}
Expand Down Expand Up @@ -2881,6 +2883,11 @@ void ByteCodeWriter :: saveNativeBranching(CommandTape& tape, BuildNode node, Ta
tape.write(ByteCode::CmpN, valueNode.arg.value);
break;
}
case BuildKey::NilRefBranchOp:
// NOTE : sp[0] - loperand
tape.write(ByteCode::PeekSI, 0);
tape.write(ByteCode::CmpR);
break;
default:
assert(false);
break;
Expand Down Expand Up @@ -3414,6 +3421,7 @@ void ByteCodeWriter :: saveTape(CommandTape& tape, BuildNode node, TapeScope& ta
case BuildKey::IntBranchOp:
case BuildKey::IntConstBranchOp:
case BuildKey::RealBranchOp:
case BuildKey::NilRefBranchOp:
saveNativeBranching(tape, current, tapeScope, paths, tapeOptMode, loopMode);
weakLoop = false;
break;
Expand Down
4 changes: 3 additions & 1 deletion elenasrc3/engine/buildtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ namespace elena_lang
IntLongOp = 0x0082,
DistributedTypeList = 0x0083,
UnboxAndCallMessage = 0x0084,
NilRefBranchOp = 0x0085,

MaxOperationalKey = 0x0084,
MaxOperationalKey = 0x0085,

Import = 0x0090,
DictionaryOp = 0x0091,
Expand Down Expand Up @@ -335,6 +336,7 @@ namespace elena_lang
map.add("local", BuildKey::Local);
map.add("intcondop", BuildKey::IntCondOp);
map.add("realcondop", BuildKey::RealCondOp);
map.add("nilcondop", BuildKey::NilCondOp);
map.add("branchop", BuildKey::BranchOp);
map.add("intbranchop", BuildKey::IntBranchOp);
map.add("conversion_op", BuildKey::ConversionOp);
Expand Down
29 changes: 6 additions & 23 deletions tests60/sandbox/sandbox.l
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import extensions;

class Object
{
dispatch() => nil;
}

public interface Func
{
abstract function();
}

singleton E
{
callFromLambda()
{
var f := { callMe(); };

f();
}

private callMe(){}
}

public program()
{
E.callFromLambda()
var name := nil;

if (nil == name) {
console.writeLine("name is nil")
}
else console.printLine("name=",name)
}

0 comments on commit 1312e08

Please sign in to comment.