Пример #1
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);
        }
Пример #2
0
        public static bool Guess_Intel_Syntax(ITextBuffer buffer, int nLinesMax = 30)
        {
            Contract.Requires(buffer != null);

            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(AsmDudeToolsStatic.CultureUI, "{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_upcase = snapshot.GetLineFromLineNumber(i).GetText().ToUpperInvariant();
                Output_INFO(string.Format(CultureUI, "{0}:Guess_Intel_Syntax {1}:\"{2}\"", "AsmDudeToolsStatic", i, line_upcase));

                List <string> keywords_upcase = AsmSourceTools.SplitIntoKeywordsList(line_upcase);

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

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

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

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

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

                if (contains_mnemonic_intel(keywords_upcase))
                {
                    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);

            Output_INFO(string.Format(CultureUI, "{0}:Guess_Intel_Syntax; result {1}; file=\"{2}\"; registers {3}; constants {4}; mnemonics {5}", "AsmDudeToolsStatic", result, GetFilename(buffer), registers_i, constants_i, mnemonics_i));
            return(result);
        }