/// <summary> /// Протянуть if с метками /// </summary> /// <param name="findInstruction">Инструкция, которую мы ищем</param> /// <param name="instructions">Набор наших инструкций</param> /// <returns> /// Вернёт изменённые инструкции, если меток if не более двух /// </returns> private static List <Instruction> PropagateIfWithLabel(Instruction findInstruction, List <Instruction> instructions) { var findIndexIf = instructions.IndexOf(findInstruction); if (findIndexIf == -1 || instructions.Where(x => instructions[findIndexIf].Label == x.Argument1 && x.Operation == "goto" && x.ToString() != instructions[findIndexIf].ToString()).Count() > 1) { return(instructions); } var findItem = instructions.Where(x => instructions[findIndexIf].Label == x.Argument1 && x.Operation == "goto").Count() > 0 ? instructions.Where(x => instructions[findIndexIf].Label == x.Argument1 && x.Operation == "goto").ElementAt(0) : new Instruction("", "", "", "", ""); var findIndexGoto = instructions.IndexOf(findItem); if (findIndexGoto == -1) { return(instructions); } wasChanged = true; instructions[findIndexGoto] = new Instruction("", instructions[findIndexIf].Operation, instructions[findIndexIf].Argument1, instructions[findIndexIf].Argument2, instructions[findIndexIf].Result); instructions.RemoveAt(findIndexIf); if (instructions[findIndexIf].Label == "") { instructions[findIndexIf].Label = ThreeAddressCodeTmp.GenTmpLabel(); } instructions.Insert(findIndexGoto + 1, new Instruction("", "goto", instructions[findIndexIf].Label, "", "")); return(instructions); }
/// <summary> /// Протянуть if с метками /// </summary> /// <param name="findInstruction">Инструкция которую мы ищем</param> /// <param name="instructions">Набор наших инструкций</param> /// <returns> /// Вернет измененные инструкции если меткок if не более двух /// </returns> private static List <Instruction> StretchIFWithLabel(Instruction findInstruction, List <Instruction> instructions) { int findIndexIf = instructions.IndexOf(findInstruction); if (findIndexIf == -1 || instructions.Where(x => instructions[findIndexIf].Label == x.Argument1 && x.Operation == "goto" && x.ToString() != instructions[findIndexIf].ToString()).Count() > 1) { return(instructions); } int findIndexGoto = instructions.IndexOf(instructions.Where(x => instructions[findIndexIf].Label == x.Argument1 && x.Operation == "goto").ElementAt(0)); wasChanged = true; if (instructions[findIndexIf + 1].Label == "") { instructions[findIndexGoto] = new Instruction("", instructions[findIndexIf].Operation, instructions[findIndexIf].Argument1, instructions[findIndexIf].Argument2, instructions[findIndexIf].Result); var tmp = ThreeAddressCodeTmp.GenTmpLabel(); instructions[findIndexIf] = new Instruction(tmp, "noop", "", "", ""); instructions.Insert(findIndexGoto + 1, new Instruction("", "goto", tmp, "", "")); } else { instructions[findIndexGoto] = new Instruction("", instructions[findIndexIf].Operation, instructions[findIndexIf].Argument1, instructions[findIndexIf].Argument2, instructions[findIndexIf].Result); var tmp = instructions[findIndexIf + 1].Label; instructions[findIndexIf] = new Instruction("", "noop", "", "", ""); instructions.Insert(findIndexGoto + 1, new Instruction("", "goto", tmp, "", "")); } return(instructions); }