public static bool OptimizeSegment(ref CodeSegment segment) { #if !fulltest if (segment.Lines[0].address != "0015FC6C") { return(false); } Console.WriteLine(LineNo); #endif if (segment.ContainsFpu == false) { return(false); } //if(segment.Lines.Length<10) return false; //Fpu to sse stuff FpuSegment FpuStream = segment.GetFpuStream(); CurrentSeg = segment; CurrentFpu = FpuStream; thingy = true; fpu.Reset(); string s; for (byte i = 0; i < FpuStream.Lines.Length; i++) { fpu.PerformOp(FpuStream.Lines[i], i); } fpu.Process(); vectorizer.Reset(); for (int i = 0; i < fpu.Result.Length; i++) { vectorizer.AddLine(fpu.Result[i], fpu.ResultLines[i]); } VectorMatch[] PatchedCode = vectorizer.Process(); if (PatchedCode == null || PatchedCode.Length == 0) { throw new OptimizationException("Main: fpu code not vectorizable"); } CodeGenerator.GenerateCode(new ArrayList(PatchedCode)); Patcher.ApplyPatch(FpuStream); if (CreateLog) { s = string.Join("\r\n", fpu.Result); LogFile.Write(segment.Lines[0].address + ":"); LogFile.WriteLine(CodeGenerator.ToHex(HexParse(FpuStream.Lines[0].address) + 4198400).Remove(0, 3).PadLeft(8, '0')); LogFile.WriteLine(s); LogFile.WriteLine("\r\nPatched:"); LogFile.WriteLine(segment.FpuString); LogFile.WriteLine("\r\nwith:"); LogFile.Write(FpuStream.PreString() + ReadFile("out.txt").Replace("bits 32\r\n", "") + FpuStream.PostString()); if (Benchmark) { LogFile.WriteLine("\r\nBenchmark:"); LogFile.WriteLine("FPU: " + Patcher.FpuTime.ToString()); LogFile.WriteLine("SSE: " + Patcher.SseTime.ToString()); LogFile.WriteLine("Ratio: " + ((double)Patcher.SseTime.Ticks / (double)Patcher.FpuTime.Ticks).ToString()); } LogFile.WriteLine("------------------------------------------------"); } return(true); }
public static void ReadCode() { #if clean File.Delete("Morrowind.exe"); File.Delete("code"); File.Copy("clean\\Morrowind.exe","Morrowind.exe"); File.Copy("clean\\code","code"); #endif Console.WriteLine("Loading code segments"); StreamReader sr=new StreamReader("dcode.txt"); ArrayList segLines=new ArrayList(); int count=0; bool DropSegment=false; bool StartedSegment=true; LineInfo li; #if !fulltest||partialtest int count2=0; #endif count3=0; while(sr.Peek()!=-1) { string s=sr.ReadLine(); #if !fulltest||partialtest count2++; LineNo=count2; if(count2<500000||count2>510000) continue; #endif try { li=new LineInfo(s); } catch(Exception) { DropSegment=true; li=new LineInfo("00000000 00 int3"); } if(Array.IndexOf(JumpInstructions,li.instruction)==-1) { if(StartedSegment) { segLines.Add(li); } else if(Array.IndexOf(FpuInstructions,li.instruction)!=-1) { StartedSegment=true; segLines.Add(li); } } else if(StartedSegment) { if(!DropSegment) { segLines.Add(li); CodeSegment cs=new CodeSegment(segLines); try { if(OptimizeSegment(ref cs)) count3++; #if !fulltest } catch(OptimizationException ex) { if(thingy) { try { LogFile.WriteLine(CurrentSeg.Lines[0].address); LogFile.WriteLine("\nPatched:"); LogFile.Write(CurrentSeg.ToString()); LogFile.WriteLine("\nwith:"); s=string.Join("\n",fpu.Result); LogFile.WriteLine(s); //LogFile.Write(tempFpu.PreString()+File.ReadAll("out.txt")+tempFpu.PostString()); LogFile.WriteLine("------------------------------------------------"); } catch { } string ss=ex.ToString(); Console.WriteLine(ss); thingy=false; } #else } catch(OptimizationException) { #endif } count++; #if useconsole if(count%100==0) { Console.WriteLine("Processed "+count.ToString()+" segments and found "+count3+" patches."); } #else Console.WriteLine("Processed "+count.ToString()+" segments and found "+count3+" patches."); #endif } segLines.Clear(); DropSegment=false; StartedSegment=false; } else DropSegment=false; } sr.Close(); }
public static bool OptimizeSegment(ref CodeSegment segment) { #if !fulltest if(segment.Lines[0].address!="0015FC6C") return false; Console.WriteLine(LineNo); #endif if(segment.ContainsFpu==false) return false; //if(segment.Lines.Length<10) return false; //Fpu to sse stuff FpuSegment FpuStream=segment.GetFpuStream(); CurrentSeg=segment; CurrentFpu=FpuStream; thingy=true; fpu.Reset(); string s; for(byte i=0;i<FpuStream.Lines.Length;i++) { fpu.PerformOp(FpuStream.Lines[i],i); } fpu.Process(); vectorizer.Reset(); for(int i=0;i<fpu.Result.Length;i++) { vectorizer.AddLine(fpu.Result[i],fpu.ResultLines[i]); } VectorMatch[] PatchedCode=vectorizer.Process(); if(PatchedCode==null||PatchedCode.Length==0) throw new OptimizationException("Main: fpu code not vectorizable"); CodeGenerator.GenerateCode(new ArrayList(PatchedCode)); Patcher.ApplyPatch(FpuStream); if(CreateLog) { s=string.Join("\r\n",fpu.Result); LogFile.Write(segment.Lines[0].address+":"); LogFile.WriteLine(CodeGenerator.ToHex(HexParse(FpuStream.Lines[0].address)+4198400).Remove(0,3).PadLeft(8,'0')); LogFile.WriteLine(s); LogFile.WriteLine("\r\nPatched:"); LogFile.WriteLine(segment.FpuString); LogFile.WriteLine("\r\nwith:"); LogFile.Write(FpuStream.PreString()+ReadFile("out.txt").Replace("bits 32\r\n","")+FpuStream.PostString()); if(Benchmark) { LogFile.WriteLine("\r\nBenchmark:"); LogFile.WriteLine("FPU: "+Patcher.FpuTime.ToString()); LogFile.WriteLine("SSE: "+Patcher.SseTime.ToString()); LogFile.WriteLine("Ratio: "+((double)Patcher.SseTime.Ticks/(double)Patcher.FpuTime.Ticks).ToString()); } LogFile.WriteLine("------------------------------------------------"); } return true; }
public static void ReadCode() { #if clean File.Delete("Morrowind.exe"); File.Delete("code"); File.Copy("clean\\Morrowind.exe", "Morrowind.exe"); File.Copy("clean\\code", "code"); #endif Console.WriteLine("Loading code segments"); StreamReader sr = new StreamReader("dcode.txt"); ArrayList segLines = new ArrayList(); int count = 0; bool DropSegment = false; bool StartedSegment = true; LineInfo li; #if !fulltest || partialtest int count2 = 0; #endif count3 = 0; while (sr.Peek() != -1) { string s = sr.ReadLine(); #if !fulltest || partialtest count2++; LineNo = count2; if (count2 < 500000 || count2 > 510000) { continue; } #endif try { li = new LineInfo(s); } catch (Exception) { DropSegment = true; li = new LineInfo("00000000 00 int3"); } if (Array.IndexOf(JumpInstructions, li.instruction) == -1) { if (StartedSegment) { segLines.Add(li); } else if (Array.IndexOf(FpuInstructions, li.instruction) != -1) { StartedSegment = true; segLines.Add(li); } } else if (StartedSegment) { if (!DropSegment) { segLines.Add(li); CodeSegment cs = new CodeSegment(segLines); try { if (OptimizeSegment(ref cs)) { count3++; } #if !fulltest } catch (OptimizationException ex) { if (thingy) { try { LogFile.WriteLine(CurrentSeg.Lines[0].address); LogFile.WriteLine("\nPatched:"); LogFile.Write(CurrentSeg.ToString()); LogFile.WriteLine("\nwith:"); s = string.Join("\n", fpu.Result); LogFile.WriteLine(s); //LogFile.Write(tempFpu.PreString()+File.ReadAll("out.txt")+tempFpu.PostString()); LogFile.WriteLine("------------------------------------------------"); } catch { } string ss = ex.ToString(); Console.WriteLine(ss); thingy = false; } #else } catch (OptimizationException) { #endif } count++; #if useconsole if (count % 100 == 0) { Console.WriteLine("Processed " + count.ToString() + " segments and found " + count3 + " patches."); } #else Console.WriteLine("Processed " + count.ToString() + " segments and found " + count3 + " patches."); #endif } segLines.Clear(); DropSegment = false; StartedSegment = false; } else { DropSegment = false; } } sr.Close(); }