public bool Optimize(ref List <Instruction> src) { int pushidx; if (CurrentIndex == -1) { return(false); } Pop OldPop = (Pop)src[CurrentIndex]; pushidx = -1; Push OldPush = OptimizeUtils.GetLastPush(src, CurrentIndex - 1, ref pushidx); if (OldPush == null) { return(false); } // check for same operands if (OptimizeUtils.SameOperands(OldPop, OldPush)) { src[CurrentIndex].Emit = false; src[pushidx].Emit = false; SamePushPop++; Optimizer.OptimizationsSize += 6; Optimizer.Optimizations++; return(true); } if (OldPop.DestinationIsIndirect && OldPush.DestinationIsIndirect) { return(false); } // Transfer to mov dst,src if (OptimizeUtils.CanTransfer(OldPush, OldPop)) { src[CurrentIndex].Emit = false; src[pushidx].Emit = false; InstructionWithDestinationAndSourceAndSize mv = new Mov(); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)OldPush, ref mv, true); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)OldPop, ref mv, false); src[CurrentIndex] = mv; src[pushidx] = null; PushPopCount++; Optimizer.OptimizationsSize += 3; Optimizer.Optimizations++; } return(true); }
public bool Optimize(ref List <Instruction> src) { if (CurrentIndex == -1) { return(false); } LeftPopIdx = CurrentIndex; LeftPop = src[CurrentIndex] as Pop; if (LeftPop != null) { RightPop = OptimizeUtils.GetLastPop(src, CurrentIndex - 1, ref RightPopIdx); if (RightPop != null) { Right = OptimizeUtils.GetLastPush(src, RightPopIdx - 1, ref RightIdx); if (Right != null) { Left = OptimizeUtils.GetLastPush(src, RightIdx - 1, ref LeftIdx); if (Left != null) { if ((RightPop.DestinationIsIndirect && Right.DestinationIsIndirect) || (LeftPop.DestinationIsIndirect && Left.DestinationIsIndirect)) { return(false); } if (OptimizeUtils.CrossUsage(Left, RightPop) || OptimizeUtils.CrossUsage(Right, LeftPop)) { return(false); } if (OptimizeUtils.IsSegment(Left) || OptimizeUtils.IsSegment(LeftPop) || OptimizeUtils.IsSegment(Right) || OptimizeUtils.IsSegment(RightPop)) { return(false); } // optimize src[LeftIdx].Emit = false; src[RightIdx].Emit = false; InstructionWithDestinationAndSourceAndSize mv = new Mov(); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)Right, ref mv, true); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)RightPop, ref mv, false); src[RightPopIdx] = mv; mv = new Mov(); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)Left, ref mv, true); OptimizeUtils.CopyDestination((InstructionWithDestinationAndSize)LeftPop, ref mv, false); src[LeftPopIdx] = mv; Optimizer.Optimizations += 2; Optimizations++; Optimizer.OptimizationsSize += 6; } else { return(false); } } else { return(false); } } else { return(false); } } else { return(false); } return(true); }