Skip to content

Commit

Permalink
optimize HandleMathClamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Sep 5, 2024
1 parent 6039fc5 commit 7c226a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
54 changes: 23 additions & 31 deletions src/Neo.Compiler.CSharp/MethodConvert/System/SystemCall.Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,41 +306,33 @@ private static void HandleMathClamp(MethodConvert methodConvert, SemanticModel m
if (arguments is not null)
methodConvert.PrepareArgumentsForMethod(model, symbol, arguments, CallingConvention.StdCall);

var endTarget = new JumpTarget();
var exceptionTarget = new JumpTarget();
var minTarget = new JumpTarget();
var maxTarget = new JumpTarget();
methodConvert.AddInstruction(OpCode.DUP);// 5 0 10 10
methodConvert.AddInstruction(OpCode.ROT);// 5 10 10 0
methodConvert.AddInstruction(OpCode.DUP);// 5 10 10 0 0
methodConvert.AddInstruction(OpCode.ROT);// 5 10 0 0 10
methodConvert.Jump(OpCode.JMPLT, exceptionTarget);// 5 10 0
methodConvert.AddInstruction(OpCode.THROW);
// Evaluation stack: value=5 min=0 max=10 <- top
methodConvert.AddInstruction(OpCode.OVER); // 5 0 10 0
methodConvert.AddInstruction(OpCode.OVER); // 5 0 10 0 10 <- top
methodConvert.Jump(OpCode.JMPLE, exceptionTarget); // 5 0 10 // if 0 <= 10, continue execution
methodConvert.Push("min>max");
methodConvert.AddInstruction(OpCode.ABORTMSG);
exceptionTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.ROT);// 10 0 5
methodConvert.AddInstruction(OpCode.DUP);// 10 0 5 5
methodConvert.AddInstruction(OpCode.ROT);// 10 5 5 0
methodConvert.AddInstruction(OpCode.DUP);// 10 5 5 0 0
methodConvert.AddInstruction(OpCode.ROT);// 10 5 0 0 5
methodConvert.Jump(OpCode.JMPGT, minTarget);// 10 5 0
methodConvert.AddInstruction(OpCode.DROP);// 10 5
methodConvert.AddInstruction(OpCode.DUP);// 10 5 5
methodConvert.AddInstruction(OpCode.ROT);// 5 5 10
methodConvert.AddInstruction(OpCode.DUP);// 5 5 10 10
methodConvert.AddInstruction(OpCode.ROT);// 5 10 10 5
methodConvert.Jump(OpCode.JMPLT, maxTarget);// 5 10
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
minTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.REVERSE3);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
maxTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.SWAP);
methodConvert.AddInstruction(OpCode.DROP);
methodConvert.Jump(OpCode.JMP, endTarget);
endTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP);
methodConvert.AddInstruction(OpCode.REVERSE3); // 10 0 5
methodConvert.AddInstruction(OpCode.OVER); // 10 0 5 0
methodConvert.AddInstruction(OpCode.OVER); // 10 0 5 0 5
methodConvert.Jump(OpCode.JMPGE, minTarget); // 10 0 5; should return 0 if JMPed
methodConvert.AddInstruction(OpCode.NIP); // 10 5
methodConvert.AddInstruction(OpCode.OVER); // 10 5 10
methodConvert.AddInstruction(OpCode.OVER); // 10 5 10 5
methodConvert.Jump(OpCode.JMPLE, maxTarget); // 10 5; should return 10 if JMPed
methodConvert.AddInstruction(OpCode.NIP); // 5; should return 5
methodConvert.AddInstruction(OpCode.RET);
minTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP); // 10 0 5; should return 0
methodConvert.AddInstruction(OpCode.DROP); // 10 0; should return 0
methodConvert.AddInstruction(OpCode.NIP); // 0; should return 0
methodConvert.AddInstruction(OpCode.RET);
maxTarget.Instruction = methodConvert.AddInstruction(OpCode.NOP); // 10 5; should return 10
methodConvert.AddInstruction(OpCode.DROP); // 10; should return 10
methodConvert.AddInstruction(OpCode.RET);
}

private static void HandleMathBigMul(MethodConvert methodConvert, SemanticModel model, IMethodSymbol symbol, ExpressionSyntax? instanceExpression, IReadOnlyList<SyntaxNode>? arguments)
Expand Down
Loading

0 comments on commit 7c226a0

Please sign in to comment.