/// <summary> /// This function will return the name of the variable, argument, etc /// that we want to store our value into. /// </summary> /// <param name="method"></param> /// <param name="instruction"></param> /// <returns></returns> public static string GetStoreLocation(MethodDefinition method, Instruction instruction) { var code = instruction.OpCode.Code; if (instruction.Operand != null && !InstructionHelper.IsStoreN(code)) { return(instruction.Operand.ToString()); } if (InstructionHelper.IsStoreLocalVariable(code)) { if (InstructionHelper.IsStoreN(code)) { /* Not yet implemented. */ } else { var index = InstructionHelper.GetCodeIndex(code); return(GetVariableName(method.Body.Variables[index])); } } if (InstructionHelper.IsStoreArgument(code)) { if (InstructionHelper.IsStoreN(code)) { return(instruction.Operand.ToString()); } else { var index = InstructionHelper.GetCodeIndex(code); return(method.Parameters[index].Name); } } return(""); }
/// <summary> /// This function will return a string, representing the values grabbed from the instruction /// </summary> /// <param name="instruction"></param> /// <returns></returns> public static string GetValueOf(MethodDefinition method, Instruction instruction) { var code = instruction.OpCode.Code; // If this is a conditional instruction. lets return the value of that. if (InstructionHelper.IsConditional(instruction.OpCode.Code)) { return(GetConditionalStatement(method, instruction)); } // Check if we are trying to load a value // Can be, Load a field, load a constant value, argument // variable and more. if (InstructionHelper.IsLoad(code)) { // Check if we want to return a null value if (InstructionHelper.IsLoadNull(instruction.OpCode.Code)) { return("null"); } // For now, we will settle with just handling integers. // We will have to add more if checks later to support // strings, variables, etc. if (InstructionHelper.IsLoadInteger(code)) { // In case the integer is bigger than 4 // We will need to get the value from the // operand instead of the "index". if (InstructionHelper.IsLoadN(code)) { return(instruction.Operand.ToString()); } else { // The GetCodeIndex will return the value used from // the constant load. For instance ldc.i4.4 will return // the value 4, ldc.i4.3 will return value 3. Etc. return(InstructionHelper.GetCodeIndex(code).ToString()); } } // Check if we want to load a string // if so, then return the value from the operand. if (InstructionHelper.IsLoadString(code)) { return("\"" + instruction.Operand + "\""); } // If we want to load a local variable. // So be it! if (InstructionHelper.IsLoadLocalVariable(code)) { if (InstructionHelper.IsLoadN(code)) { var targetVariable = instruction.Operand as VariableDefinition; return(GetVariableName(targetVariable)); } else { var index = InstructionHelper.GetCodeIndex(code); return(GetVariableName(method.Body.Variables[index])); } } // If we want to load the value of a argument // Then this is necessary. if (InstructionHelper.IsLoadArgs(code)) { if (InstructionHelper.IsLoadN(code)) { return(instruction.Operand.ToString()); } else { var index = InstructionHelper.GetCodeIndex(code); if (method.IsConstructor) { index--; } return(method.Parameters[index].Name); } } } // If our instructions are to call a method if (InstructionHelper.IsCallMethod(code)) { // We will need to grab the information of that specific method // By casting the Operand into a MethodDefinition. var callingMethod = instruction.Operand as MethodDefinition; // Note: We are not managing any parameters yet. return(callingMethod.Name + "()"); } // Return a empty value if we can't determine what we want to return. return(""); }