void writer_BeforeWritingString(object sender, InstructionInfoEventArgs e)
            {
                var instructionInfo       = e.InstructionInfo;
                var instruction           = instructionInfo.instruction;
                int indexOfStringArgument = instruction.IndexOfStringArgument();

                if (instruction.IndexOfStringArgument() >= 0)
                {
                    string originalString    = ainFile.GetString(instructionInfo.words[indexOfStringArgument]);
                    string replacementString = null;
                    if (stringDictionary != null)
                    {
                        replacementString = stringDictionary.GetOrNull(stringNumber);
                    }
                    if (replacementString != null && originalString != replacementString)
                    {
                        e.Text = replacementString;
                    }
                    stringNumber++;
                }
            }
Esempio n. 2
0
 public HistogramEntry <string>[] FindArgumentValuesString(Variable argument)
 {
     return(FindArgumentValues(argument, Instruction.S_PUSH, expression => ainFile.GetString(expression.Value)));
 }
        private List <string> GetTextFromFunction(Function function)
        {
            int numberOfNonCommentedLines = 0;
            int numberOfStrings           = 0;
            int numberOfMessages          = 0;

            //Expression expression = null;
            //int expressionLastAddress = function.Address;

            useStringsToMatch = StringsToMatch != null && StringsToMatch.Count > 0;
            //List<string> strings = new List<string>();
            //List<string> messages = new List<string>();
            List <string> functionLines      = new List <string>();
            string        functionLineString = "FUNCTION " + /*function.Index.ToString() + " " + */ AssemblerProjectWriter.SanitizeVariableName(function.Name);

            functionLines.Add(functionLineString);
            functionLines.Add("#x strings, x messages");  //this line gets changed later (it's index 1)
            int    address  = function.Address;
            string lastName = null;

            while (address < ainFile.Code.Length)
            {
                var instructionInfo = ainFile.Peek(address);
                if (instructionInfo.instruction == Instruction.ENDFUNC || instructionInfo.instruction == Instruction.FUNC)
                {
                    break;
                }
                if (this.AnnotateEnumerationType != null && instructionInfo.instruction == Instruction.CALLFUNC)
                {
                    var func = ainFile.GetFunction(instructionInfo.word1);
                    if (VariablesUsingEnumerationType.Contains(func))
                    {
                        var parameters = GetParametersThatUsesEnumerationType(func);
                        if (parameters.FirstOrDefault() != null)
                        {
                            foreach (var parameter in parameters)
                            {
                                int i    = parameter.Index;
                                int addr = instructionInfo.CurrentAddress - (func.ParameterCount) * 6 + i * 6;
                                var ins2 = ainFile.Peek(addr);
                                if (ins2.instruction == Instruction.PUSH)
                                {
                                    string enumerationValue = this.AnnotateEnumerationType.GetOrDefault(ins2.word1, "");
                                    if (!String.IsNullOrEmpty(enumerationValue))
                                    {
                                        functionLines.Add("");
                                        functionLines.Add("#" + enumerationValue);
                                    }
                                }
                                else if (ins2.instruction == Instruction.S_PUSH)
                                {
                                    string str = ainFile.GetString(ins2.word1);
                                    if (!String.IsNullOrEmpty(str))
                                    {
                                        if (this.replacementStringsForAnnotations != null && this.replacementStringsForAnnotations.ContainsKey(str))
                                        {
                                            string nextStr = this.replacementStringsForAnnotations[str];
                                            if (!nextStr.StartsWith("*"))
                                            {
                                                str = nextStr;
                                            }
                                            else
                                            {
                                                str = lastName;
                                            }
                                        }
                                        else
                                        {
                                        }
                                        if (lastName != str)
                                        {
                                            lastName = str;
                                            functionLines.Add("");
                                            functionLines.Add("#" + str);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (instructionInfo.instruction == Instruction.MSG)
                {
                    //if (this.AnnotateEnumerationType != null)
                    //{
                    //    if (expression == null)
                    //    {
                    //        expression = ainFile.DecompiledCodeCache.GetDecompiledCode(function);
                    //    }
                    //    CatchUpToAddress(ref expression, functionLines, address);
                    //}
                    int    messageNumber = instructionInfo.word1;
                    string message       = ainFile.GetMessage(messageNumber);
                    if (message != null)
                    {
                        if (useStringsToMatch == false || StringsToMatch.Contains(message))
                        {
                            string messageLine = "m " + numberOfMessages.ToString("000") + " " + StringExportImport.EscapeText(message);
                            functionLines.Add(messageLine);
                        }
                        numberOfMessages++;
                        numberOfNonCommentedLines++;
                    }
                }
                else if (instructionInfo.instruction == Instruction.STRSWITCH)
                {
                    int switchBlockNumber = instructionInfo.word1;
                    var switchBlock       = ainFile.Switches.GetOrNull(switchBlockNumber);
                    if (switchBlock != null)
                    {
                        foreach (var switchCase in switchBlock.SwitchCases)
                        {
                            int    stringNumber = switchCase.Value;
                            string str          = ainFile.GetString(stringNumber);
                            if (str != null)
                            {
                                AddString(ref numberOfNonCommentedLines, ref numberOfStrings, functionLines, stringNumber, str);
                            }
                        }
                    }
                }
                else
                {
                    int indexOfStringArgument = instructionInfo.instruction.IndexOfStringArgument();
                    if (indexOfStringArgument != -1)
                    {
                        int    stringNumber = instructionInfo.words[indexOfStringArgument];
                        string str          = ainFile.GetString(stringNumber);
                        if (str != null)
                        {
                            AddString(ref numberOfNonCommentedLines, ref numberOfStrings, functionLines, stringNumber, str);
                        }
                    }
                }
                address = instructionInfo.nextAddress;
            }
            functionLines[1] = "#" + numberOfStrings.ToString() + " strings, " + numberOfMessages.ToString() + " messages";
            if (numberOfNonCommentedLines == 0)
            {
                functionLines.Clear();
            }
            return(functionLines);
        }
        private bool FindFunctionTypeForVariable(IVariable variable, Expression code, bool MakeNewStuffUp)
        {
            bool variableIsFunction = variable as Function != null;
            bool variableIsFuncType = variable.DataType.IsFuncType();
            bool variableIsDelegate = variable.DataType.IsDelegate();

            foreach (var e in code.GetChildExpressions())
            {
                Expression otherExpression = null;
                IVariable  expVariable     = null;
                if (e.ExpressionType == Instruction.CALLFUNC2 && variableIsFuncType)
                {
                    bool isVoid = e.IsVoidContext();
                    if (!isVoid)
                    {
                        otherExpression = e.GetOtherSideOfBinaryExpression();
                    }
                    IVariable otherExpressionVariable = null;
                    if (otherExpression != null)
                    {
                        otherExpressionVariable = otherExpression.Variable;
                    }

                    DataType otherExpressionDataType   = DataType.Void;
                    int      otherExpressionStructType = -1;
                    if (otherExpressionVariable != null)
                    {
                        otherExpressionDataType   = otherExpressionVariable.DataType;
                        otherExpressionStructType = otherExpressionVariable.StructType;
                    }
                    else
                    {
                        if (isVoid)
                        {
                            otherExpressionDataType = DataType.Void;
                        }
                        else
                        {
                            otherExpressionDataType = DataType.AnyNonVoidType;
                        }
                    }

                    var argExpressions    = (e.Arg3 ?? Expression.Empty).FlattenContainerExpression(Instruction.Comma).ToArray();
                    var variables         = GetVariablesFromExpressions(argExpressions);
                    var matchingFuncTypes = ainFile.MatchingFunctionTypes(otherExpressionDataType, otherExpressionStructType, variables, variables.Length).Distinct().ToArray();

                    var firstMatchingFuncType = matchingFuncTypes.FirstOrDefault();
                    if (firstMatchingFuncType != null)
                    {
                        if (MakeNewStuffUp || matchingFuncTypes.Skip(1).FirstOrDefault() == null)
                        {
                            variable.StructType = firstMatchingFuncType.Index;
                        }
                    }
                }
                if (variableIsFunction && e.ExpressionType == Instruction.RETURN)
                {
                    otherExpression = e.Arg1;
                    expVariable     = variable;
                }
                else
                {
                    expVariable = e.Variable;
                    if (variable == expVariable)
                    {
                        otherExpression = e.GetOtherSideOfBinaryExpression();
                    }
                }

                if (otherExpression != null)
                {
                    Function otherFunction = null;
                    if (otherExpression.ExpressionType == Instruction.PUSH)
                    {
                        otherFunction = ainFile.GetFunction(otherExpression.Value);
                    }
                    else if (otherExpression.ExpressionType == Instruction.S_PUSH)
                    {
                        otherFunction = ainFile.GetFunction(ainFile.GetString(otherExpression.Value));
                    }
                    else
                    {
                        var otherVariable = otherExpression.Variable;
                        if (otherVariable != null && !variableIsDelegate && otherVariable.DataType.IsFuncType() && otherVariable.StructType != -1)
                        {
                            variable.StructType = otherVariable.StructType;
                            return(true);
                        }
                        if (otherVariable != null && variableIsDelegate && otherVariable.DataType.IsDelegate() && otherVariable.StructType != -1)
                        {
                            variable.StructType = otherVariable.StructType;
                            return(true);
                        }
                    }
                    if (otherFunction != null)
                    {
                        FunctionType matchingFuncType = null;
                        if (!variableIsDelegate)
                        {
                            if (!MakeNewStuffUp)
                            {
                                matchingFuncType = ainFile.GetFuncTypeUnique(otherFunction);
                            }
                            else
                            {
                                matchingFuncType = ainFile.GetFuncType(otherFunction);
                            }
                            if (matchingFuncType != null)
                            {
                                variable.StructType = matchingFuncType.Index;
                                return(true);
                            }
                        }
                        else
                        {
                            if (!MakeNewStuffUp)
                            {
                                matchingFuncType = ainFile.GetDelegateUnique(otherFunction);
                            }
                            else
                            {
                                matchingFuncType = ainFile.GetDelegateType(otherFunction);
                            }
                            if (matchingFuncType != null)
                            {
                                variable.StructType = matchingFuncType.Index;
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    var functionParameter = e.GetFunctionCallParameter();
                    if (functionParameter != null)
                    {
                        if (functionParameter != null && !variableIsDelegate && functionParameter.DataType.IsFuncType() && functionParameter.StructType != -1)
                        {
                            variable.StructType = functionParameter.StructType;
                            return(true);
                        }
                        if (functionParameter != null && variableIsDelegate && functionParameter.DataType.IsDelegate() && functionParameter.StructType != -1)
                        {
                            variable.StructType = functionParameter.StructType;
                            return(true);
                        }
                    }
                    else if (variable.DataType.IsArray())
                    {
                        var parent = e.Parent;
                        if (parent != null && (parent.ExpressionType == Instruction.A_PUSHBACK || parent.ExpressionType == Instruction.A_INSERT))
                        {
                            var       arg2 = parent.Arg2;
                            IVariable var2 = null;
                            if (arg2 != null)
                            {
                                var2 = arg2.Variable;
                            }
                            if (var2 != null)
                            {
                                if (!variableIsDelegate && var2.DataType.IsFuncType() && var2.StructType != -1)
                                {
                                    variable.StructType = var2.StructType;
                                    return(true);
                                }
                                if (variableIsDelegate && var2.DataType.IsDelegate() && var2.StructType != -1)
                                {
                                    variable.StructType = var2.StructType;
                                    return(true);
                                }
                            }
                        }
                    }
                    else if (e.ExpressionType == Instruction.CALLFUNC2)
                    {
                    }
                }
            }
            return(false);
        }