public static bool CanApplyMovMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; if (instructionOne.MNEMONIC == "MOV") { if (InstructionUtil.IsLoadOrStoreInstruction(instructionTwo.FULL)) { // TODO: FIGURE SHIT OUT if (instructionTwo.MNEMONIC == "ST" && instructionTwo.SOURCE1 == instructionOne.DESTINATION) { return(true); } return(false); } // TODO: take into account Source2 is null // if (instructionOne.DESTINATION == instructionTwo.SOURCE1) // { // return true; // } // if(instructionOne.D) return(new[] { instructionTwo.SOURCE1, instructionTwo.SOURCE2 }.Contains(instructionOne.DESTINATION)); } return(false); }
public static bool CanApplyImmediateMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; if (instructionOne.SOURCE2 == null || instructionTwo.SOURCE2 == null || !instructionOne.SOURCE2.StartsWith("#") || !instructionTwo.SOURCE2.StartsWith("#")) { return(false); } if (InstructionUtil.IsLoadOrStoreInstruction(instructionTwo.FULL) || InstructionUtil.IsLoadOrStoreInstruction(instructionOne.FULL)) { return(false); } var combination = GenerateMnemonicCombination(instructionOne, instructionTwo); if (AllowedImmediateMergingCombinations.Contains(combination)) { return(false); } return(instructionOne.DESTINATION == instructionTwo.SOURCE1); }
public static bool CanApplyMovReabsorption(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; if (instructionTwo.MNEMONIC == "MOV") { return(instructionOne.DESTINATION == instructionTwo.SOURCE1); } return(false); }
private void ApplyMovReabsorption(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; var instructionLine = megaInstructionTwo.Line; var indexOfLine = instructionLine - 1; AssemblyLines.RemoveRange(indexOfLine, 1); var mergedInstruction = GenerateNewMovReabsorptionInstruction(instructionTwo, instructionOne); AssemblyLines.Insert(indexOfLine, mergedInstruction); }
private static bool AreDestinationAddressesEqual(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var traceOne = megaInstructionOne.Trace; var traceTwo = megaInstructionTwo.Trace; if (traceOne != null && traceTwo != null) { if (traceOne.DESTINATION_ADDRESS != null && traceTwo.DESTINATION_ADDRESS != null) { return(traceOne.DESTINATION_ADDRESS == traceTwo.DESTINATION_ADDRESS); } } return(false); }
private void ApplyImmediateMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; var mnemonicInstructionOne = instructionOne.MNEMONIC; var mnemonicInstructionTwo = instructionTwo.MNEMONIC; if ((mnemonicInstructionOne == "DIV" && mnemonicInstructionTwo == "MULT") || (mnemonicInstructionOne == "MULT" && mnemonicInstructionTwo == "DIV")) { if (instructionOne.SOURCE2 == instructionTwo.SOURCE2) { var instructionLine = megaInstructionOne.Line; var indexOfLine = instructionLine - 1; AssemblyLines.RemoveRange(indexOfLine, 2); var newInstruction = $"MOV {instructionTwo.SOURCE1}, {instructionOne.SOURCE1}"; AssemblyLines.Insert(indexOfLine, newInstruction); AssemblyLines.Insert(indexOfLine + 1, ""); } } var additionMnemonics = new string[] { "ADD", "SUB" }; if (additionMnemonics.Contains(mnemonicInstructionOne) && additionMnemonics.Contains(mnemonicInstructionTwo)) { var source2InstructionOne = Convert.ToInt32(instructionOne.SOURCE2.Remove(0, 1)); var source2InstructionTwo = Convert.ToInt32(instructionOne.SOURCE2.Remove(0, 1)); source2InstructionOne *= mnemonicInstructionOne == "SUB" ? -1 : 1; source2InstructionTwo *= mnemonicInstructionTwo == "SUB" ? -1 : 1; var newValue = source2InstructionOne + source2InstructionTwo; var newInstruction = $"{instructionTwo.MNEMONIC} {instructionTwo.DESTINATION}, {instructionOne.SOURCE1}, #{newValue}"; var instructionLine = megaInstructionTwo.Line; var indexOfLine = instructionLine - 1; AssemblyLines.RemoveRange(indexOfLine, 1); AssemblyLines.Insert(indexOfLine, newInstruction); } }
public static bool CanApplyMemoryAntiAlias(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; var areDestinationAddressesEqual = AreDestinationAddressesEqual(megaInstructionOne, megaInstructionTwo); if (instructionOne.MNEMONIC == "ST" && instructionTwo.MNEMONIC == "LD") { return(areDestinationAddressesEqual || instructionOne.DESTINATION == instructionTwo.SOURCE1); } else if (instructionOne.MNEMONIC == "LD" && instructionTwo.MNEMONIC == "ST") { // TODO ADD CODE TO HANDLE THIS return(areDestinationAddressesEqual || false); } return(false); }
private void ApplyMovMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo) { var instructionOne = megaInstructionOne.Instruction; var instructionTwo = megaInstructionTwo.Instruction; /**TODO: * - instructiune relationala * - instructiune gardata **/ var mergedInstruction = instructionTwo.MNEMONIC switch { "ADD" => GenerateNewMovMergingInstruction(instructionTwo, instructionOne), "ST" => GenerateNewStMovMergingInstruction(instructionOne, instructionTwo), _ => "" }; if (mergedInstruction != "") { var instructionLine = megaInstructionTwo.Line; var indexOfLine = instructionLine - 1; AssemblyLines.RemoveRange(indexOfLine, 1); AssemblyLines.Insert(indexOfLine, mergedInstruction); } }
public List <MegaInstruction> Build(List <string> assemblyLines, List <Trace> traceLines) { if (assemblyLines.Count == 0) { throw new ArgumentException("Empty assemblyLines"); } if (traceLines.Count == 0) { throw new ArgumentException("Empty traceLines"); } var list = new List <MegaInstruction>(); // TODO: Remove unused variable? var tracesHandled = 0; traceLines.ForEach( trace => { tracesHandled++; var goToNextTrace = false; while (!goToNextTrace) { var instruction = assemblyLines[index]; // TODO: Remove this debugging "hack" if (instruction.Contains("trap")) { var x = 1 == 1; } if (InstructionUtil.IsLabelCommentOrEmptyString(instruction)) { index++; continue; } if (programCounter.HasValue && !programCounterDictionary.ContainsKey(programCounter.Value) || (programCounter.HasValue && !programCounterDictionary[programCounter.Value].Equals(instruction))) { programCounterDictionary.Add((int)programCounter, instruction); } var instructionType = InstructionUtil.TypeOf(instruction); if (instructionType == InstructionType.Arithmetic) { var arithmeticTrace = new Trace($"A {programCounter} XXXX"); var megaInstruction = new MegaInstruction(index + 1, instruction, arithmeticTrace); list.Add(megaInstruction); index++; } else { goToNextTrace = true; if (instructionType == InstructionType.Load || instructionType == InstructionType.Store) { var megaInstruction = new MegaInstruction(index + 1, instruction, trace); list.Add(megaInstruction); index++; } if (instructionType == InstructionType.Branch) { var desiredTrace = trace; var shouldSkip = false; if ( // !trace.IsOfType(TraceType.Branch)|| trace.CURRENT_ADDRESS != programCounter) { desiredTrace = new Trace($"A {programCounter} XXXX"); shouldSkip = true; } var megaInstruction = new MegaInstruction(index + 1, instruction, desiredTrace); list.Add(megaInstruction); if (shouldSkip) { programCounter++; index++; goToNextTrace = false; continue; } index = DetermineIndex(assemblyLines, instruction); } } programCounter = instructionType != InstructionType.Branch ? programCounter + 1 : trace.DESTINATION_ADDRESS; } }); return(list); }