using System; using System.Collections.Generic; using System.Text; using Cosmos.Assembler; using x86 = Cosmos.Assembler.x86; namespace Orvid { public static class Optimizer { public static Assembler Optimize(Assembler asmb) { return asmb; Assembler asmblr = asmb; List instr = asmb.Instructions; //List dmbrs = asmb.DataMembers; SortedDictionary labels = new SortedDictionary(); List comments = new List(); List usedLabels = new List(); usedLabels.Add("KernelStart"); foreach (Instruction ins in instr) { if (ins is Label) { if (((Label)ins).IsGlobal) { usedLabels.Add(((Label)ins).QualifiedName); } labels.Add(((Label)ins).QualifiedName, ins); } else if (ins is x86.JumpToSegment) { if (((x86.JumpToSegment)ins).DestinationRef != null) { usedLabels.Add(((x86.JumpToSegment)ins).DestinationRef.Name); } else { usedLabels.Add(((x86.JumpToSegment)ins).DestinationLabel); } } else if (ins is x86.JumpBase) { usedLabels.Add(((x86.JumpBase)ins).DestinationLabel); } else if (ins is x86.Call) { usedLabels.Add(((x86.Call)ins).DestinationLabel); } else if (ins is x86.Push) { if (((x86.Push)ins).DestinationRef != null) { usedLabels.Add(((x86.Push)ins).DestinationRef.Name); } } else if (ins is x86.Mov) { if (((x86.Mov)ins).SourceRef != null) { usedLabels.Add(((x86.Mov)ins).SourceRef.Name); } } } foreach (string s in usedLabels) { labels.Remove(s); } usedLabels = null; instr.RemoveAll( delegate(Instruction inst) { if (inst is Comment) return true; else if (inst is Label) { if (labels.ContainsKey(((Label)inst).QualifiedName)) return true; return false; } return false; } ); labels = null; comments = null; asmblr.Instructions = instr; //asmblr.DataMembers = dmbrs; return asmblr; } } }