Пример #1
0
 public bool TryGetFuncLabel(int current, out FunctionLabel label)
 {
     lock (symbolListLock)
     {
         var s = from item in _funcList
                 where item.Offset == current
                 select item;
         var success = s.Count() != 0;
         label = success ? s.First() : new FunctionLabel(current);
         return(success);
     }
 }
Пример #2
0
 public void RemoveFuncLabel(FunctionLabel toBeRemoved)
 {
     lock (symbolListLock)
     {
         _funcList.Remove(toBeRemoved);
         var sym = new Symbol()
         {
             Name = toBeRemoved.Name
         };
         symbolTable.RemoveSymbol(sym);
     }
 }
Пример #3
0
        public void AddFuncLabel(FunctionLabel toBeAdded)
        {
            lock (symbolListLock)
            {
                var sym = new Symbol()
                {
                    Name  = toBeAdded.Name,
                    Value = toBeAdded.Offset
                };
                if (!symbolTable.ContainsSymbol(sym))
                {
                    _funcList.Add(toBeAdded);

                    symbolTable.AddSymbol(sym);
                }
            }
        }
Пример #4
0
 public AddFunctionLabelForm(LabelContainer lblContainer, LabelEditMode editMode, FunctionLabel newPriorLabel = null)
 {
     InitializeComponent();
     labelContainer = lblContainer;
     editingMode = editMode;
     editedLabel = newPriorLabel;
     if (editingMode == LabelEditMode.Edit)
     {
         Text = "Edit Label";
         if (editedLabel != null)
         {
             nameBox.Text = editedLabel.Name;
             offsetBox.Text = editedLabel.Offset.ToString("X");
             if (!String.IsNullOrEmpty(editedLabel.Comment))
             {
                 commentBox.Text = editedLabel.Comment;
             }
         }
     }
 }
Пример #5
0
 public FunctionLabel(FunctionLabel prev) : this(prev.Value, prev.Name, prev.Comment)
 {
 }
Пример #6
0
 public void RemoveFuncLabel(FunctionLabel toBeRemoved)
 {
     lock (symbolListLock)
     {
         _funcList.Remove(toBeRemoved);
         var sym = new Symbol()
         {
             Name = toBeRemoved.Name
         };
         symbolTable.RemoveSymbol(sym);
     }
 }
Пример #7
0
 public bool TryGetFuncLabel(int current, out FunctionLabel label)
 {
     lock (symbolListLock)
     {
         var s = from item in _funcList
                 where item.Offset == current
                 select item;
         var success = s.Count() != 0;
         label = success ? s.First() : new FunctionLabel(current);
         return success;
     }
 }
Пример #8
0
        public void AddFuncLabel(FunctionLabel toBeAdded)
        {
            lock (symbolListLock)
            {
                var sym = new Symbol()
                {
                    Name = toBeAdded.Name,
                    Value = toBeAdded.Offset
                };
                if (!symbolTable.ContainsSymbol(sym))
                {
                    _funcList.Add(toBeAdded);

                    symbolTable.AddSymbol(sym);
                }
            }
        }
Пример #9
0
        // TODO: Make sure everything saves and loads properly.
        public void LoadLabelFile(string fileName)
        {
            using (TextReader labelFile = new StreamReader(fileName))
            {
                using (TextWriter errFile = new StreamWriter("err.txt"))
                {
                    if (labelFile.ReadLine() != "gbr")
                    {
                        return;
                    }
                    else
                    {
                        var items = new List<Dictionary<string, string>>();
                        int curItem = -1;
                        string currentLine;
                        while ((currentLine = labelFile.ReadLine()) != null)
                        {
                            switch (currentLine)
                            {
                                case ".label":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "label" } });
                                        curItem++;
                                    }
                                    break;

                                case ".data":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "data" } });
                                        curItem++;
                                    }
                                    break;

                                case ".var":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "var" } });
                                        curItem++;
                                    }
                                    break;

                                case ".comment":
                                    {
                                        items.Add(new Dictionary<string, string>() { { "tag", "comment" } });
                                        curItem++;
                                    }
                                    break;

                                default:
                                    if (curItem == -1)
                                    {
                                        break;
                                    }
                                    string[] opt = currentLine.Split(new[] { ':' }, 2);
                                    if (opt.Length == 1)
                                    {
                                        if (items[curItem].ContainsKey("_c"))
                                        {
                                            items[curItem]["_c"] += "\n" + currentLine;
                                        }
                                        else
                                        {
                                            items[curItem].Add("_c", currentLine);
                                        }
                                    }
                                    else
                                    {
                                        if (items[curItem].ContainsKey(opt[0]))
                                        {
                                            items[curItem][opt[0]] = opt[1];
                                        }
                                        else
                                        {
                                            items[curItem].Add(opt[0], opt[1]);
                                        }
                                    }
                                    break;
                            }
                        }
                        foreach (var currentItem in items)
                        {
                            switch (currentItem["tag"])
                            {
                                case "label":
                                    {
                                        int offset = 0;
                                        string name = "";
                                        string comment = "";
                                        bool offsetGood = false;
                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }
                                        if (offsetGood)
                                        {
                                            FunctionLabel fl = new FunctionLabel(offset, name, comment);
                                            AddFuncLabel(fl);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "data":
                                    {
                                        int offset = -1;
                                        int length = -1;
                                        string printTemp = "";
                                        string name = "";
                                        string comment = "";
                                        DataSectionType dst = DataSectionType.Data;
                                        GBPalette gbp = new GBPalette();
                                        bool offsetGood = false;
                                        bool lengthGood = false;
                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_l":
                                                    {
                                                        lengthGood = Utility.OffsetStringToInt(kvp.Value, out length);
                                                    }
                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_d":
                                                    {
                                                        printTemp = kvp.Value;
                                                    }
                                                    break;

                                                case "_p1":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_1);
                                                    }

                                                    break;

                                                case "_p2":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_2);
                                                    }

                                                    break;

                                                case "_p3":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_3);
                                                    }

                                                    break;

                                                case "_p4":
                                                    {
                                                        dst = DataSectionType.Image;
                                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_4);
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }

                                        if (offsetGood && lengthGood)
                                        {
                                            DataLabel ds = new DataLabel(offset, length, name, printTemp, comment, dst, gbp);
                                            AddDataLabel(ds);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "var":
                                    {
                                        int variable = -1;
                                        string name = "";
                                        string comment = "";
                                        bool variableGood = false;

                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_v":
                                                    {
                                                        variableGood = Utility.OffsetStringToInt(kvp.Value, out variable);
                                                    }

                                                    break;

                                                case "_n":
                                                    {
                                                        if (Utility.IsWord(kvp.Value))
                                                        {
                                                            name = kvp.Value;
                                                        }
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }

                                        if (variableGood)
                                        {
                                            VarLabel vl = new VarLabel(variable, name, comment);
                                            AddVarLabel(vl);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }

                                    break;

                                case "comment":
                                    {
                                        int offset = 0;
                                        string name = String.Empty;
                                        string comment = "";
                                        bool offsetGood = false;

                                        foreach (var kvp in currentItem)
                                        {
                                            switch (kvp.Key)
                                            {
                                                case "_o":
                                                    {
                                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                                    }

                                                    break;

                                                case "_c":
                                                    {
                                                        comment = kvp.Value;
                                                    }

                                                    break;
                                            }
                                        }
                                        if (offsetGood)
                                        {
                                            AddComment(offset, comment);
                                        }
                                        else
                                        {
                                            errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                        }
                                    }
                                    break;

                                default:

                                    break;
                            }
                        }
                    }
                }
            }
        }
Пример #10
0
        // TODO: Make sure everything saves and loads properly.

        public void LoadLabelFile(string fileName)
        {
            using (TextReader labelFile = new StreamReader(fileName))
            {
                using (TextWriter errFile = new StreamWriter("err.txt"))
                {
                    if (labelFile.ReadLine() != "gbr")
                    {
                        return;
                    }
                    else
                    {
                        var    items   = new List <Dictionary <string, string> >();
                        int    curItem = -1;
                        string currentLine;
                        while ((currentLine = labelFile.ReadLine()) != null)
                        {
                            switch (currentLine)
                            {
                            case ".label":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "label" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".data":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "data" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".var":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "var" }
                                    });
                                curItem++;
                            }
                            break;

                            case ".comment":
                            {
                                items.Add(new Dictionary <string, string>()
                                    {
                                        { "tag", "comment" }
                                    });
                                curItem++;
                            }
                            break;

                            default:
                                if (curItem == -1)
                                {
                                    break;
                                }
                                string[] opt = currentLine.Split(new[] { ':' }, 2);
                                if (opt.Length == 1)
                                {
                                    if (items[curItem].ContainsKey("_c"))
                                    {
                                        items[curItem]["_c"] += "\n" + currentLine;
                                    }
                                    else
                                    {
                                        items[curItem].Add("_c", currentLine);
                                    }
                                }
                                else
                                {
                                    if (items[curItem].ContainsKey(opt[0]))
                                    {
                                        items[curItem][opt[0]] = opt[1];
                                    }
                                    else
                                    {
                                        items[curItem].Add(opt[0], opt[1]);
                                    }
                                }
                                break;
                            }
                        }
                        foreach (var currentItem in items)
                        {
                            switch (currentItem["tag"])
                            {
                            case "label":
                            {
                                int    offset     = 0;
                                string name       = "";
                                string comment    = "";
                                bool   offsetGood = false;
                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }
                                if (offsetGood)
                                {
                                    FunctionLabel fl = new FunctionLabel(offset, name, comment);
                                    AddFuncLabel(fl);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "data":
                            {
                                int             offset     = -1;
                                int             length     = -1;
                                string          printTemp  = "";
                                string          name       = "";
                                string          comment    = "";
                                DataSectionType dst        = DataSectionType.Data;
                                GBPalette       gbp        = new GBPalette();
                                bool            offsetGood = false;
                                bool            lengthGood = false;
                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_l":
                                    {
                                        lengthGood = Utility.OffsetStringToInt(kvp.Value, out length);
                                    }
                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_d":
                                    {
                                        printTemp = kvp.Value;
                                    }
                                    break;

                                    case "_p1":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_1);
                                    }

                                    break;

                                    case "_p2":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_2);
                                    }

                                    break;

                                    case "_p3":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_3);
                                    }

                                    break;

                                    case "_p4":
                                    {
                                        dst = DataSectionType.Image;
                                        Utility.OffsetStringToInt(kvp.Value, out gbp.Col_4);
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }

                                if (offsetGood && lengthGood)
                                {
                                    DataLabel ds = new DataLabel(offset, length, name, printTemp, comment, dst, gbp);
                                    AddDataLabel(ds);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "var":
                            {
                                int    variable     = -1;
                                string name         = "";
                                string comment      = "";
                                bool   variableGood = false;

                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_v":
                                    {
                                        variableGood = Utility.OffsetStringToInt(kvp.Value, out variable);
                                    }

                                    break;

                                    case "_n":
                                    {
                                        if (Utility.IsWord(kvp.Value))
                                        {
                                            name = kvp.Value;
                                        }
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }

                                if (variableGood)
                                {
                                    VarLabel vl = new VarLabel(variable, name, comment);
                                    AddVarLabel(vl);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }

                            break;

                            case "comment":
                            {
                                int    offset     = 0;
                                string name       = String.Empty;
                                string comment    = "";
                                bool   offsetGood = false;

                                foreach (var kvp in currentItem)
                                {
                                    switch (kvp.Key)
                                    {
                                    case "_o":
                                    {
                                        offsetGood = Utility.OffsetStringToInt(kvp.Value, out offset);
                                    }

                                    break;

                                    case "_c":
                                    {
                                        comment = kvp.Value;
                                    }

                                    break;
                                    }
                                }
                                if (offsetGood)
                                {
                                    AddComment(offset, comment);
                                }
                                else
                                {
                                    errFile.WriteLine("Label #" + items.IndexOf(currentItem) + " was unrecognized.");
                                }
                            }
                            break;

                            default:

                                break;
                            }
                        }
                    }
                }
            }
        }
Пример #11
0
 public FunctionLabel(FunctionLabel prev) : this(prev.Value, prev.Name, prev.Comment)
 {
 }
Пример #12
0
 private void okButton_Click(object sender, EventArgs e)
 {
     bool checkNameCollision = editingMode == LabelEditMode.Add ?
         true :
         !nameBox.Text.Equals(editedLabel.Name, StringComparison.Ordinal);
     int off = -1;
     Symbol sym = new Symbol()
     {
         Name = nameBox.Text
     };
     if (!Utility.IsWord(nameBox.Text))
     {
         Error.ShowErrorMessage(ErrorMessage.Label_InvalidName);
     }
     else if (checkNameCollision && labelContainer.IsSymbolDefined(sym))
     {
         Error.ShowErrorMessage(ErrorMessage.Label_NameAlreadyDefined);
     }
     else if (!Utility.OffsetStringToInt(offsetBox.Text, out off))
     {
         Error.ShowErrorMessage(ErrorMessage.Label_InvalidOffset);
     }
     else
     {
         if (editingMode == LabelEditMode.Edit)
         {
             labelContainer.RemoveFuncLabel(editedLabel);
         }
         editedLabel = new FunctionLabel(off, nameBox.Text, commentBox.Text);
         labelContainer.AddFuncLabel(editedLabel);
         this.DialogResult = System.Windows.Forms.DialogResult.OK;
     }
 }
Пример #13
0
 public string ShowFuncLabel(FunctionLabel funcLabel)
 {
     return PrintASM(funcLabel.Offset, GuessLabelPrintLength(funcLabel.Offset));
 }
Пример #14
0
 public Task<string> ShowFuncLabelAsync(FunctionLabel funcLabel)
 {
     var task = new Task<string>(() => ShowFuncLabel(funcLabel));
     task.Start();
     return task;
 }
Пример #15
0
 private void SearchFileRangeForFunctionCall(Dictionary<string, FuncRefType> results, int startingOffset, int length, FunctionLabel searchLabel)
 {
     if (startingOffset < 0x4000 && searchLabel.Offset > 0x3FFF)
     {
         return;
     }
     int currentOffset = startingOffset;
     while (currentOffset < startingOffset + length)
     {
         if (lc.isAddressMarkedAsData(currentOffset))
         {
             currentOffset = lc.GetNextNonDataAddress(currentOffset);
             continue;
         }
         GBInstruction isu = new GBInstruction();
         if (!GBASM.GetInstruction(CoreFile.MainFile, 0, currentOffset, ref isu))
             break;
         if (isu.InstType == InstructionType.call || isu.InstType == InstructionType.jp || isu.InstType == InstructionType.jr)
         {
             ushort calledAddress = isu.ArgCount == 1 ? isu.Arg1.NumArg : isu.Arg2.NumArg;
             if (!(calledAddress < 0x4000 && currentOffset > 0x3FFF))
             {
                 int calledNum = Utility.GetRealAddress(isu.Bank, calledAddress);
                 if (searchLabel.Offset == calledNum)
                 {
                     if (isu.InstType == InstructionType.call)
                         results.Add(currentOffset.ToString("X"), FuncRefType.Call);
                     else
                         results.Add(currentOffset.ToString("X"), FuncRefType.Call);
                 }
             }
         }
         currentOffset += isu.InstSize;
     }
 }
Пример #16
0
 private Dictionary<string, FuncRefType> SearchForFunctionCall(FunctionLabel searchLabel, SearchOptions options)
 {
     var results = new Dictionary<string, FuncRefType>();
     var searched = new HashSet<int>();
     if (options == SearchOptions.InFile)
     {
         SearchFileRangeForFunctionCall(results, 0x0, CoreFile.Length, searchLabel);
         return results;
     }
     else
     {
         foreach (FunctionLabel c in lc.FuncList)
         {
             FuncRefType refType = FuncRefType.None;
             int currentOffset = c.Offset;
             if (searched.Contains(currentOffset))
             {
                 continue;
             }
             else
             {
                 searched.Add(currentOffset);
             }
             int curLen = GuessLabelPrintLength(c.Offset);
             while (currentOffset < c.Offset + curLen)
             {
                 if (currentOffset < 0x4000 && searchLabel.Offset > 0x3FFF)
                 {
                     break;
                 }
                 if (lc.isAddressMarkedAsData(currentOffset))
                 {
                     currentOffset = lc.GetNextNonDataAddress(currentOffset);
                     continue;
                 }
                 GBInstruction isu = new GBInstruction();
                 if (!GBASM.GetInstruction(CoreFile.MainFile, 0, currentOffset, ref isu))
                     break;
                 if (isu.InstType == InstructionType.call || isu.InstType == InstructionType.jp || isu.InstType == InstructionType.jr)
                 {
                     ushort calledAddress = isu.ArgCount == 1 ? isu.Arg1.NumArg : isu.Arg2.NumArg;
                     if (!(currentOffset < 0x4000 && calledAddress > 0x3FFF))
                     {
                         int calledNum = Utility.GetRealAddress(isu.Bank, calledAddress);
                         if (searchLabel.Offset == calledNum)
                         {
                             if (isu.InstType == InstructionType.call)
                                 refType |= FuncRefType.Call;
                             else
                                 refType |= FuncRefType.Jump;
                         }
                     }
                 }
                 currentOffset += isu.InstSize;
             }
             if (refType != FuncRefType.None)
                 results.Add(c.Name, refType);
         }
         return results;
     }
 }
Пример #17
0
        public string SearchForFunctionCall(FunctionLabel labelName)
        {
            StringBuilder returned = new StringBuilder();
            var ifResult = SearchForFunctionCall(labelName, SearchOptions.InFunctions);
            var ofResult = SearchForFunctionCall(labelName, SearchOptions.InFile);

            string functionUsedMessage = "{0} was referred to near these labels:{1}";
            string offsetUsedMessage = "{0} was referred to at these offsets:{1}";

            if (ifResult.Count == 0)
            {
                returned.AppendLine(labelName.Name + " was not referred to near any of these labels.");
            }
            else
            {
                returned.AppendFormat(functionUsedMessage, labelName.Name, Environment.NewLine);
                foreach (var kvp in ifResult)
                {
                    returned.AppendFormat("{0} ({1}){2}", kvp.Key, kvp.Value.ToString(), Environment.NewLine);
                }
            }

            if (ofResult.Count == 0)
            {
                returned.AppendLine(labelName.Name + " was not referred to at any offset in disassembly.");
            }
            else
            {
                returned.AppendFormat(offsetUsedMessage, labelName.Name, Environment.NewLine);
                foreach (var kvp in ofResult)
                {
                    returned.AppendFormat("{0} ({1}){2}", kvp.Key, kvp.Value.ToString(), Environment.NewLine);
                }
            }
            return returned.ToString();
        }
Пример #18
0
 public async Task<string> SearchForFunctionCallAsync(FunctionLabel labelName)
 {
     var task = new Task<string>(() => SearchForFunctionCall(labelName));
     task.Start();
     return await task;
 }