Exemple #1
0
 private void updateLineNumber(ITextBuffer buffer, ITagAggregator <AsmTokenTag> aggregator, int lineNumber, uint id)
 {
     AsmDudeToolsStatic.Output(string.Format("INFO: LabelGraph:updateLineNumber: line {0}", lineNumber));
     this.addLineNumber(buffer, aggregator, lineNumber, id);
     this.removeLineNumber(lineNumber, id);
 }
Exemple #2
0
        private void Doc_FileActionOccurred(Object sender, TextDocumentFileActionEventArgs e)
        {
            ITextDocument doc = sender as ITextDocument;

            AsmDudeToolsStatic.Output("INFO: LabelGraph:Doc_FileActionOccurred: " + doc.FilePath + ":" + e.FileActionType);
        }
Exemple #3
0
        /// <summary>Guess whether the provided buffer has assembly in Masm syntax (return true) or Gas syntax (return false)</summary>
        public static bool Guess_Masm_Syntax(ITextBuffer buffer, int nLinesMax = 30)
        {
            //AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Masm_Syntax. file=\"{1}\"", "AsmDudeToolsStatic", AsmDudeToolsStatic.GetFilename(buffer)));
            ITextSnapshot snapshot      = buffer.CurrentSnapshot;
            int           evidence_masm = 0;

            for (int i = 0; i < Math.Min(snapshot.LineCount, nLinesMax); ++i)
            {
                string line_capitals = snapshot.GetLineFromLineNumber(i).GetText().ToUpper();
                //AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Masm_Syntax {1}:\"{2}\"", "AsmDudeToolsStatic", i, line_capitals));

                List <string> keywords = AsmSourceTools.SplitIntoKeywordsList(line_capitals);

                foreach (string word in keywords)
                {
                    switch (word)
                    {
                    case "PTR":
                    case "@B":
                    case "@F":
                        evidence_masm++;
                        break;

                    case ".INTEL_SYNTAX":
                    case ".ATT_SYNTAX":
                        return(false);    // we know for sure
                    }
                }
            }
            bool result = (evidence_masm > 0);

            AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Masm_Syntax; result {1}; file=\"{2}\"; evidence_masm {3}", "AsmDudeToolsStatic", result, AsmDudeToolsStatic.GetFilename(buffer), evidence_masm));
            return(result);
        }
Exemple #4
0
        private void BufferChanged(object sender, TextContentChangedEventArgs e)
        {
            //AsmDudeToolsStatic.Output(string.Format("INFO: LabelGraph:OnTextBufferChanged: number of changes={0}; first change: old={1}; new={2}", e.Changes.Count, e.Changes[0].OldText, e.Changes[0].NewText));
            if (!_enabled)
            {
                return;
            }

            if (true)
            {
                this.reset_Delayed();
            }
            else
            {
                lock (_updateLock) {
                    // experimental faster method, but it still has subtle bugs
                    switch (e.Changes.Count)
                    {
                    case 0: return;

                    case 1:
                        ITextChange textChange = e.Changes[0];
                        ITextBuffer buffer     = this._buffer;
                        ITagAggregator <AsmTokenTag> aggregator = null;


                        switch (textChange.LineCountDelta)
                        {
                        case 0: {
                            int lineNumber = e.Before.GetLineNumberFromPosition(textChange.OldPosition);
                            this.updateLineNumber(buffer, aggregator, lineNumber, (uint)lineNumber);
                        }
                        break;

                        case 1: {
                            int lineNumber = e.Before.GetLineNumberFromPosition(textChange.OldPosition);
                            //AsmDudeToolsStatic.Output(string.Format("INFO: LabelGraph:OnTextBufferChanged: old={0}; new={1}; LineNumber={2}", textChange.OldText, textChange.NewText, lineNumber));
                            this.shiftLineNumber(lineNumber + 1, 1);
                            this.updateLineNumber(buffer, aggregator, lineNumber, (uint)lineNumber);
                        }
                        break;

                        case -1: {
                            int lineNumber = e.Before.GetLineNumberFromPosition(textChange.OldPosition);
                            AsmDudeToolsStatic.Output(string.Format("INFO: LabelGraph:OnTextBufferChanged: old={0}; new={1}; LineNumber={2}", textChange.OldText, textChange.NewText, lineNumber));
                            this.shiftLineNumber(lineNumber + 1, -1);
                            this.updateLineNumber(buffer, aggregator, lineNumber, (uint)lineNumber);
                            this.updateLineNumber(buffer, aggregator, lineNumber - 1, (uint)lineNumber);
                        }
                        break;

                        default:
                            AsmDudeToolsStatic.Output(string.Format("INFO: LabelGraph:OnTextBufferChanged: lineDelta={0}", textChange.LineCountDelta));
                            this.reset_Delayed();
                            break;
                        }
                        break;

                    default:
                        this.reset_Delayed();
                        break;
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>Guess whether the provided buffer has assembly in Intel syntax (return true) or AT&T syntax (return false)</summary>
        public static bool Guess_Intel_Syntax(ITextBuffer buffer, int nLinesMax = 30)
        {
            bool contains_register_att(List <string> line)
            {
                foreach (string asmToken in line)
                {
                    if (asmToken[0].Equals('%'))
                    {
                        string asmToken2 = asmToken.Substring(1);
                        if (RegisterTools.IsRn(asmToken2, true))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }

            bool contains_register_intel(List <string> line)
            {
                foreach (string asmToken in line)
                {
                    if (RegisterTools.IsRn(asmToken, true))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            bool contains_constant_att(List <string> line)
            {
                foreach (string asmToken in line)
                {
                    if (asmToken[0].Equals('$'))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            bool contains_constant_intel(List <string> line)
            {
                return(false);
            }

            bool contains_mnemonic_att(List <string> line)
            {
                foreach (string word in line)
                {
                    if (!AsmSourceTools.IsMnemonic(word, true))
                    {
                        if (AsmSourceTools.IsMnemonic_Att(word, true))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }

            bool contains_mnemonic_intel(List <string> line)
            {
                return(false);
            }

            //AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Intel_Syntax. file=\"{1}\"", "AsmDudeToolsStatic", AsmDudeToolsStatic.GetFilename(buffer)));
            ITextSnapshot snapshot    = buffer.CurrentSnapshot;
            int           registers_i = 0;
            int           constants_i = 0;
            int           mnemonics_i = 0;

            for (int i = 0; i < Math.Min(snapshot.LineCount, nLinesMax); ++i)
            {
                string line_capitals = snapshot.GetLineFromLineNumber(i).GetText().ToUpper();
                AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Intel_Syntax {1}:\"{2}\"", "AsmDudeToolsStatic", i, line_capitals));

                List <string> keywords = AsmSourceTools.SplitIntoKeywordsList(line_capitals);

                if (contains_register_att(keywords))
                {
                    registers_i++;
                }

                if (contains_register_intel(keywords))
                {
                    registers_i--;
                }

                if (contains_constant_att(keywords))
                {
                    constants_i++;
                }

                if (contains_constant_intel(keywords))
                {
                    constants_i--;
                }

                if (contains_mnemonic_att(keywords))
                {
                    mnemonics_i++;
                }

                if (contains_mnemonic_intel(keywords))
                {
                    mnemonics_i--;
                }
            }
            int total =
                Math.Max(Math.Min(1, registers_i), -1) +
                Math.Max(Math.Min(1, constants_i), -1) +
                Math.Max(Math.Min(1, mnemonics_i), -1);

            bool result = (total <= 0);

            AsmDudeToolsStatic.Output_INFO(string.Format("{0}:Guess_Intel_Syntax; result {1}; file=\"{2}\"; registers {3}; constants {4}; mnemonics {5}", "AsmDudeToolsStatic", result, AsmDudeToolsStatic.GetFilename(buffer), registers_i, constants_i, mnemonics_i));
            return(result);
        }
        private void loadHandcraftedData(string filename)
        {
            //AsmDudeToolsStatic.Output("INFO: MnemonicStore:load_data_intel: filename=" + filename);
            try {
                System.IO.StreamReader file = new System.IO.StreamReader(filename);
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if ((line.Length > 0) && (!line.StartsWith(";")))
                    {
                        string[] columns = line.Split('\t');
                        if (columns.Length == 4)   // general description
                        {
                            #region
                            Mnemonic mnemonic = AsmSourceTools.parseMnemonic(columns[1]);
                            if (mnemonic == Mnemonic.UNKNOWN)
                            {
                                AsmDudeToolsStatic.Output("WARNING: MnemonicStore:loadHandcraftedData: unknown mnemonic in line" + line);
                            }
                            else
                            {
                                if (this._description.ContainsKey(mnemonic))
                                {
                                    this._description.Remove(mnemonic);
                                }
                                this._description.Add(mnemonic, columns[2]);

                                if (this._htmlRef.ContainsKey(mnemonic))
                                {
                                    this._htmlRef.Remove(mnemonic);
                                }
                                this._htmlRef.Add(mnemonic, columns[3]);
                            }
                            #endregion
                        }
                        else if ((columns.Length == 5) || (columns.Length == 6))     // signature description, ignore an old sixth column
                        {
                            #region
                            Mnemonic mnemonic = AsmSourceTools.parseMnemonic(columns[0]);
                            if (mnemonic == Mnemonic.UNKNOWN)
                            {
                                AsmDudeToolsStatic.Output("WARNING: MnemonicStore:loadHandcraftedData: unknown mnemonic in line" + line);
                            }
                            else
                            {
                                AsmSignatureElement se = new AsmSignatureElement(mnemonic, columns[1], columns[2], columns[3], columns[4]);
                                this.add(se);
                            }
                            #endregion
                        }
                        else
                        {
                            AsmDudeToolsStatic.Output("WARNING: MnemonicStore:loadHandcraftedData: s.Length=" + columns.Length + "; funky line" + line);
                        }
                    }
                }
                file.Close();

                #region Fill Arch
                foreach (KeyValuePair <Mnemonic, IList <AsmSignatureElement> > pair in this._data)
                {
                    ISet <Arch> archs = new HashSet <Arch>();
                    foreach (AsmSignatureElement signatureElement in pair.Value)
                    {
                        foreach (Arch arch in signatureElement.arch)
                        {
                            archs.Add(arch);
                        }
                    }
                    IList <Arch> list = new List <Arch>();
                    foreach (Arch a in archs)
                    {
                        list.Add(a);
                    }
                    this._arch[pair.Key] = list;
                }
                #endregion
            } catch (FileNotFoundException) {
                MessageBox.Show("ERROR: AsmTokenTagger: could not find file \"" + filename + "\".");
            } catch (Exception e) {
                MessageBox.Show("ERROR: AsmTokenTagger: error while reading file \"" + filename + "\"." + e);
            }
        }
Exemple #7
0
        private void AddData(MicroArch microArch, string filename, IDictionary <string, IList <Mnemonic> > translations)
        {
            //AsmDudeToolsStatic.Output_INFO("PerformanceStore:AddData_New: microArch=" + microArch + "; filename=" + filename);
            try
            {
                StreamReader file = new StreamReader(filename);
                string       line;
                int          lineNumber = 0;

                while ((line = file.ReadLine()) != null)
                {
                    if ((line.Trim().Length > 0) && (!line.StartsWith(";")))
                    {
                        string[] columns = line.Split('\t');
                        if (columns.Length == 8)
                        {
                            { // handle instruction
                                string mnemonicKey = columns[0].Trim();
                                if (!translations.TryGetValue(mnemonicKey, out IList <Mnemonic> mnemonics))
                                {
                                    mnemonics = new List <Mnemonic>();
                                    foreach (string mnemonicStr in mnemonicKey.Split(' '))
                                    {
                                        Mnemonic mnemonic = AsmSourceTools.ParseMnemonic(mnemonicStr);
                                        if (mnemonic == Mnemonic.NONE)
                                        {   // check if the mnemonicStr can be translated to a list of mnemonics
                                            if (translations.TryGetValue(mnemonicStr, out IList <Mnemonic> mnemonics2))
                                            {
                                                foreach (Mnemonic m in mnemonics2)
                                                {
                                                    mnemonics.Add(m);
                                                }
                                            }
                                            else
                                            {
                                                AsmDudeToolsStatic.Output_WARNING("PerformanceStore:AddData: microArch=" + microArch + ": unknown mnemonic " + mnemonicStr + " in line " + lineNumber + " with content \"" + line + "\".");
                                            }
                                        }
                                        else
                                        {
                                            mnemonics.Add(mnemonic);
                                        }
                                    }
                                }
                                foreach (Mnemonic m in mnemonics)
                                {
                                    this._data.Add(new PerformanceItem()
                                    {
                                        _microArch     = microArch,
                                        _instr         = m,
                                        _args          = columns[1],
                                        _mu_Ops_Fused  = columns[2],
                                        _mu_Ops_Merged = columns[3],
                                        _mu_Ops_Port   = columns[4],
                                        _latency       = columns[5],
                                        _throughput    = columns[6],
                                        _remark        = columns[7]
                                    });
                                }
                            }
                        }
                        else
                        {
                            AsmDudeToolsStatic.Output_WARNING("PerformanceStore:AddData: found " + columns.Length + " columns; funky line" + line);
                        }
                    }
                    lineNumber++;
                }
                file.Close();
            }
            catch (FileNotFoundException)
            {
                AsmDudeToolsStatic.Output_ERROR("PerformanceStore:LoadData: could not find file \"" + filename + "\".");
            }
            catch (Exception e)
            {
                AsmDudeToolsStatic.Output_ERROR("PerformanceStore:LoadData: error while reading file \"" + filename + "\"." + e);
            }
        }
Exemple #8
0
        private void LoadRegularData(string filename)
        {
            //AsmDudeToolsStatic.Output_INFO("MnemonicStore:loadRegularData: filename=" + filename);
            try
            {
                System.IO.StreamReader file = new System.IO.StreamReader(filename);
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    if ((line.Length > 0) && (!line.StartsWith(";")))
                    {
                        string[] columns = line.Split('\t');
                        if (columns.Length == 4)
                        { // general description
                            #region
                            Mnemonic mnemonic = AsmSourceTools.ParseMnemonic(columns[1]);
                            if (mnemonic == Mnemonic.NONE)
                            {
                                // ignore the unknown mnemonic
                                //AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: unknown mnemonic in line: " + line);
                            }
                            else
                            {
                                if (!this._description.ContainsKey(mnemonic))
                                {
                                    this._description.Add(mnemonic, columns[2]);
                                }
                                else
                                {
                                    // this happens when the mnemonic is defined in multiple files, using the data from the first file
                                    //AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: mnemonic " + mnemonic + " already has a description");
                                }
                                if (!this._htmlRef.ContainsKey(mnemonic))
                                {
                                    this._htmlRef.Add(mnemonic, columns[3]);
                                }
                                else
                                {
                                    // this happens when the mnemonic is defined in multiple files, using the data from the first file
                                    //AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: mnemonic " + mnemonic + " already has a html ref");
                                }
                            }
                            #endregion
                        }
                        else if ((columns.Length == 5) || (columns.Length == 6))
                        { // signature description, ignore an old sixth column
                            #region
                            Mnemonic mnemonic = AsmSourceTools.ParseMnemonic(columns[0]);
                            if (mnemonic == Mnemonic.NONE)
                            {
                                AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: unknown mnemonic in line: " + line);
                            }
                            else
                            {
                                AsmSignatureElement se = new AsmSignatureElement(mnemonic, columns[1], columns[2], columns[3], columns[4]);
                                if (Add(se))
                                {
                                    AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: signature already exists" + se.ToString());
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadRegularData: s.Length=" + columns.Length + "; funky line" + line);
                        }
                    }
                }
                file.Close();

                #region Fill Arch
                foreach (KeyValuePair <Mnemonic, IList <AsmSignatureElement> > pair in this._data)
                {
                    ISet <Arch> archs = new HashSet <Arch>();
                    foreach (AsmSignatureElement signatureElement in pair.Value)
                    {
                        foreach (Arch arch in signatureElement.Arch)
                        {
                            archs.Add(arch);
                        }
                    }
                    IList <Arch> list = new List <Arch>();
                    foreach (Arch a in archs)
                    {
                        list.Add(a);
                    }
                    this._arch[pair.Key] = list;
                }
                #endregion
            }
            catch (FileNotFoundException)
            {
                AsmDudeToolsStatic.Output_ERROR("MnemonicStore:loadRegularData: could not find file \"" + filename + "\".");
            }
            catch (Exception e)
            {
                AsmDudeToolsStatic.Output_ERROR("MnemonicStore:loadRegularData: error while reading file \"" + filename + "\"." + e);
            }
        }
Exemple #9
0
        private void LoadHandcraftedData(string filename)
        {
            //AsmDudeToolsStatic.Output_INFO("MnemonicStore:load_data_intel: filename=" + filename);
            try
            {
                StreamReader file = new StreamReader(filename);
                string       line;
                while ((line = file.ReadLine()) != null)
                {
                    if ((line.Length > 0) && (!line.StartsWith(";", StringComparison.Ordinal)))
                    {
                        string[] columns = line.Split('\t');
                        if (columns.Length == 4)
                        { // general description
                            #region
                            Mnemonic mnemonic = AsmSourceTools.ParseMnemonic(columns[1], false);
                            if (mnemonic == Mnemonic.NONE)
                            {
                                AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadHandcraftedData: unknown mnemonic in line" + line);
                            }
                            else
                            {
                                if (this.description_.ContainsKey(mnemonic))
                                {
                                    this.description_.Remove(mnemonic);
                                }
                                this.description_.Add(mnemonic, columns[2]);

                                if (this.htmlRef_.ContainsKey(mnemonic))
                                {
                                    this.htmlRef_.Remove(mnemonic);
                                }
                                this.htmlRef_.Add(mnemonic, columns[3]);
                            }
                            #endregion
                        }
                        else if ((columns.Length == 5) || (columns.Length == 6))
                        { // signature description, ignore an old sixth column
                            #region
                            Mnemonic mnemonic = AsmSourceTools.ParseMnemonic(columns[0], false);
                            if (mnemonic == Mnemonic.NONE)
                            {
                                AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadHandcraftedData: unknown mnemonic in line" + line);
                            }
                            else
                            {
                                AsmSignatureElement se = new AsmSignatureElement(mnemonic, columns[1], columns[2], columns[3], columns[4]);
                                this.Add(se);
                            }
                            #endregion
                        }
                        else
                        {
                            AsmDudeToolsStatic.Output_WARNING("MnemonicStore:loadHandcraftedData: s.Length=" + columns.Length + "; funky line" + line);
                        }
                    }
                }
                file.Close();

                #region Fill Arch
                foreach (KeyValuePair <Mnemonic, IList <AsmSignatureElement> > pair in this.data_)
                {
                    ISet <Arch> archs = new HashSet <Arch>();
                    foreach (AsmSignatureElement signatureElement in pair.Value)
                    {
                        foreach (Arch arch in signatureElement.Arch)
                        {
                            archs.Add(arch);
                        }
                    }
                    IList <Arch> list = new List <Arch>();
                    foreach (Arch a in archs)
                    {
                        list.Add(a);
                    }
                    this.arch_[pair.Key] = list;
                }
                #endregion
            }
            catch (FileNotFoundException)
            {
                AsmDudeToolsStatic.Output_ERROR("MnemonicStore:LoadHandcraftedData: could not find file \"" + filename + "\".");
            }
            catch (Exception e)
            {
                AsmDudeToolsStatic.Output_ERROR("MnemonicStore:LoadHandcraftedData: error while reading file \"" + filename + "\"." + e);
            }
        }