private Instruction[] SwitchFirstJmpNop(Instruction[] instructions, int replaceFrom, out int replaced) { var result = new Instruction[instructions.Length]; replaced = -1; for (int i = 0; i < instructions.Length; i++) { if (replaced < 0 && i >= replaceFrom && (instructions[i].GetCode() == "jmp" || instructions[i].GetCode() == "nop")) { Instruction instructionToReplace = null; if (instructions[i] is JmpInstruction ins) { instructionToReplace = new NopInstruction(ins.Jump); } else if (instructions[i] is NopInstruction ins2) { instructionToReplace = new JmpInstruction(ins2.Value); } result[i] = instructionToReplace; replaced = i; } else { result[i] = instructions[i]; } } return(result); }
private IReadOnlyList <CodeBlock> InstructionSequence(ILabel cfg) { var(orderedTrees, labelToIndexMapping) = this.cfgLinearizer.Linearize(cfg); var indexToLabelsMapping = labelToIndexMapping .GroupBy(kvp => kvp.Value, kvp => kvp.Key) .ToDictionary(x => x.Key, x => new HashSet <ILabel>(x)); return(orderedTrees.SelectMany((tree, index) => { var labelsWithNops = indexToLabelsMapping[index] .Select(label => { var nopInstruction = new NopInstruction(); var nopInstructionBlock = new List <Instruction> { nopInstruction } as IReadOnlyList <Instruction>; label.Tree = new Tree(null, new UnconditionalJump(null)); return new CodeBlock(label, nopInstructionBlock); }); var auxiliaryLabel = this.labelFactory.GetLabel(tree); var block = this.instructionSelector.GetInstructions(tree).ToList() as IReadOnlyList <Instruction>; var labelBlockTuple = new CodeBlock(auxiliaryLabel, block); return labelsWithNops.Append(labelBlockTuple); }).ToList()); }
public static long Part2() { var instructions = Input.ReadLines(day: 8).Select(Parse).ToArray(); var init = new MachineState(0, 0); for (int i = 0; i < instructions.Length; i++) { var instruction = instructions[i]; if (instruction is JmpInstruction) { instructions[i] = new NopInstruction(0); var console = new Console(instructions); var states = console.Run(init).Take(instructions.Length + 1).ToList(); if (states.Count <= instructions.Length) { return(states.Last().Acc); } instructions[i] = instruction; } } throw new NotSupportedException("☠☠☠ Kernel panic. ☠☠☠"); }
private async Task <long> Part2() { var program = await ParseInput(); for (var i = 0; i < program.Count; i++) { var newProgram = program.ToList(); if (newProgram[i] is JmpInstruction jmpInstruction) { newProgram[i] = new NopInstruction(jmpInstruction.Value); } else if (newProgram[i] is NopInstruction nopInstruction) { newProgram[i] = new JmpInstruction(nopInstruction.Value); } else { continue; } var computer = new Computer(0, 0); var returnCode = computer.Run(newProgram); if (returnCode == ReturnCode.Completed) { return(computer.Accumulator); } } throw new Exception("Correct program not found."); }
public void NopTest() { State initialState = new State(); Instruction nopInstruction = new NopInstruction(0); var newState = nopInstruction.Execute(initialState); newState.Acc.Should().Be(0); newState.Pc.Should().Be(1); }
private void addNopInstructions() { NopInstruction nop0 = new NopInstruction(mundo, 0); addInstruction(INSTR_NOP + "_0", nop0); NopInstruction nop1 = new NopInstruction(mundo, 1); addInstruction(INSTR_NOP + "_1", nop1); ALifeConsts.NOP0 = nop0.getId(); ALifeConsts.NOP1 = nop1.getId(); }
public void Initialize() { ArrayList program = new ArrayList(); NopInstruction inst = new NopInstruction(); program.Add(inst); AMProgram p = new AMProgram(); p.Initialize(program); Assert.AreSame(p.Program.Instruction, inst); }
public virtual void build(IWorld m) { this.mundo = m; // primeiro sempre NopInstruction nop0 = new NopInstruction(mundo, 0); addInstruction(INSTR_NOP, nop0); addIOInstructions(); addGoToInstructions(); addAdrInstructions(); addMovInstructions(); addArithmetic(); addNopInstructions(); }
public AMInstructionSet() { _instructions["allocate"] = new AllocateInstruction(); _instructions["bcall"] = new BCallInstruction(); _instructions["call"] = new CallInstruction(); _instructions["cut"] = new CutInstruction(); _instructions["deallocate"] = new DeallocateInstruction(); _instructions["execute"] = new ExecuteInstruction(); _instructions["fcall"] = new FCallInstruction(); _instructions["fail"] = new FailInstruction(); _instructions["get_constant"] = new GetConstantInstruction(); _instructions["get_list"] = new GetListInstruction(); _instructions["get_structure"] = new GetStructureInstruction(); _instructions["get_value"] = new GetValueInstruction(); _instructions["get_variable"] = new GetVariableInstruction(); _instructions["halt"] = new HaltInstruction(); _instructions["nop"] = new NopInstruction(); _instructions["proceed"] = new ProceedInstruction(); _instructions["put_constant"] = new PutConstantInstruction(); _instructions["put_list"] = new PutListInstruction(); _instructions["put_structure"] = new PutStructureInstruction(); _instructions["put_unsafe_value"] = new PutUnsafeValueInstruction(); _instructions["put_variable"] = new PutVariableInstruction(); _instructions["put_value"] = new PutValueInstruction(); _instructions["retry_me_else"] = new RetryMeElseInstruction(); _instructions["set_constant"] = new SetConstantInstruction(); _instructions["set_local_value"] = new SetLocalValueInstruction(); _instructions["set_value"] = new SetValueInstruction(); _instructions["set_variable"] = new SetVariableInstruction(); _instructions["set_void"] = new SetVoidInstruction(); _instructions["trust_me"] = new TrustMeInstruction(); _instructions["try_me_else"] = new TryMeElseInstruction(); _instructions["unify_constant"] = new UnifyConstantInstruction(); _instructions["unify_local_value"] = new UnifyLocalValueInstruction(); _instructions["unify_variable"] = new UnifyVariableInstruction(); _instructions["unify_value"] = new UnifyValueInstruction(); _instructions["unify_void"] = new UnifyVoidInstruction(); _instructions["procedure"] = new ProcedureInstruction(); }
public virtual void Visit(NopInstruction instruction) { Default(instruction); }
public void Transform(MethodBody body) { if (this.result == null) { throw new InvalidOperationException("Analysis result not available."); } // TODO: Not sure if we need to process the cfg nodes in backward order // since there is no global information used, the order shouldn't matter. //var sorted_nodes = cfg.BackwardOrder; // //foreach (var node in sorted_nodes) foreach (var node in this.cfg.Nodes) { var node_result = this.result[node.Id]; var copies = new Dictionary <IVariable, IVariable>(); if (node_result.Output != null) { copies.AddRange(node_result.Output); } for (var i = node.Instructions.Count - 1; i >= 0; --i) { var instruction = node.Instructions[i]; foreach (var variable in instruction.ModifiedVariables) { // Only replace temporal variables if (variable.IsTemporal() && copies.ContainsKey(variable)) { var operand = copies[variable]; instruction.Replace(variable, operand); } } var isTemporalCopy = this.Flow(instruction, copies); foreach (var variable in instruction.UsedVariables) { // Only replace temporal variables if (variable.IsTemporal() && copies.ContainsKey(variable)) { var operand = copies[variable]; instruction.Replace(variable, operand); } } // Only replace temporal variables if (isTemporalCopy) { // Remove the copy instruction if (i == 0) { // The copy is the first instruction of the basic block // Replace the copy instruction with a nop to preserve branch targets var nop = new NopInstruction(instruction.Offset); var index = body.Instructions.IndexOf(instruction); body.Instructions[index] = nop; node.Instructions[i] = nop; } else { // The copy is not the first instruction of the basic block body.Instructions.Remove(instruction); node.Instructions.RemoveAt(i); } } } } body.UpdateVariables(); }
public ProgramClause(string name, int arity) { _name = name; _arity = arity; _instruction = new NopInstruction(); }
public void Transform(MethodBody body) { if (this.result == null) { throw new InvalidOperationException("Analysis result not available."); } var copiesToRemove = new Dictionary <IVariable, InstructionLocation>(); var sorted_nodes = cfg.ForwardOrder; foreach (var node in sorted_nodes) { var node_result = this.result[node.Id]; var copies = new Dictionary <IVariable, IVariable>(); if (node_result.Input != null) { copies.AddRange(node_result.Input); } for (var i = 0; i < node.Instructions.Count; ++i) { var instruction = node.Instructions[i]; foreach (var variable in instruction.UsedVariables) { // If the variable definition is marked to be removed // but it is not a copy reaching this instruction, // then we cannot remove the definition because the // variable cannot be replaced with a copy. if (copiesToRemove.ContainsKey(variable) && !copies.ContainsKey(variable)) { copiesToRemove.Remove(variable); } // Only replace temporal variables else if (variable.IsTemporal() && copies.ContainsKey(variable)) { var operand = copies[variable]; instruction.Replace(variable, operand); } } var isTemporalCopy = this.Flow(instruction, copies); // Only replace temporal variables if (isTemporalCopy) { var definition = instruction as DefinitionInstruction; // Mark the copy instruction to be removed later var location = new InstructionLocation() { CFGNode = node, Instruction = instruction }; // TODO: Check this, not sure if this is the right fix. if (!copiesToRemove.ContainsKey(definition.Result)) { copiesToRemove.Add(definition.Result, location); } } } } // Remove unnecessary copy instructions foreach (var location in copiesToRemove.Values) { var bodyIndex = body.Instructions.IndexOf(location.Instruction); var nodeIndex = location.CFGNode.Instructions.IndexOf(location.Instruction); if (nodeIndex == 0) { // The copy is the first instruction of the basic block // Replace the copy instruction with a nop to preserve branch targets var nop = new NopInstruction(location.Instruction.Offset); body.Instructions[bodyIndex] = nop; location.CFGNode.Instructions[nodeIndex] = nop; } else { // The copy is not the first instruction of the basic block body.Instructions.RemoveAt(bodyIndex); location.CFGNode.Instructions.RemoveAt(nodeIndex); } } body.UpdateVariables(); }
public void VisitNop(NopInstruction instruction) => IncrementCallCount(nameof(VisitNop));
public void Transform(MethodBody body) { if (this.result == null) { throw new InvalidOperationException("Analysis result not available."); } foreach (var node in this.cfg.Nodes) { var node_result = this.result[node.Id]; var copies = new Dictionary <IVariable, IVariable>(); if (node_result.Input != null) { copies.AddRange(node_result.Input); } for (var i = 0; i < node.Instructions.Count; ++i) { var instruction = node.Instructions[i]; foreach (var variable in instruction.UsedVariables) { // Only replace temporal variables if (variable.IsTemporal() && copies.ContainsKey(variable)) { var operand = copies[variable]; instruction.Replace(variable, operand); } } var isTemporalCopy = this.Flow(instruction, copies); // Only replace temporal variables if (isTemporalCopy) { // Remove the copy instruction if (i == 0) { // The copy is the first instruction of the basic block // Replace the copy instruction with a nop to preserve branch targets var nop = new NopInstruction(instruction.Offset); var index = body.Instructions.IndexOf(instruction); body.Instructions[index] = nop; node.Instructions[i] = nop; } else { // The copy is not the first instruction of the basic block body.Instructions.Remove(instruction); node.Instructions.RemoveAt(i); --i; } } } } body.UpdateVariables(); }
public virtual void Visit(NopInstruction instruction) { }
public void Read(ref BlobReader reader) { this.ReadStart(); while (reader.RemainingBytes > 0) { int offset = reader.Offset; ILOpCode opCode = reader.DecodeILOpCode(); switch (opCode) { case ILOpCode.Nop: this.Read(NopInstruction.Nop(), offset); break; case ILOpCode.Ldarg_0: this.Read(LoadArgumentInstruction.FromIndex(0), offset); break; case ILOpCode.Ldarg_1: this.Read(LoadArgumentInstruction.FromIndex(1), offset); break; case ILOpCode.Ldloc_0: this.Read(LoadLocalInstruction.FromIndex(0), offset); break; case ILOpCode.Ldloc_1: this.Read(LoadLocalInstruction.FromIndex(1), offset); break; case ILOpCode.Ldloca_s: this.Read(LoadLocalAddressInstruction.FromIndex(reader.ReadSByte()), offset); break; case ILOpCode.Stloc_0: this.Read(StoreLocalInstruction.ToIndex(0), offset); break; case ILOpCode.Stloc_1: this.Read(StoreLocalInstruction.ToIndex(1), offset); break; case ILOpCode.Ldc_i4_0: this.Read(PushInt32Instruction.Constant(0), offset); break; case ILOpCode.Ldc_i4_1: this.Read(PushInt32Instruction.Constant(1), offset); break; case ILOpCode.Ldc_i4_2: this.Read(PushInt32Instruction.Constant(2), offset); break; case ILOpCode.Ldc_i4_3: this.Read(PushInt32Instruction.Constant(3), offset); break; case ILOpCode.Ldc_i4_s: this.Read(PushInt32Instruction.Constant(reader.ReadSByte()), offset); break; case ILOpCode.Ldc_i4: this.Read(PushInt32Instruction.Constant(reader.ReadInt32()), offset); break; case ILOpCode.Ldc_i8: this.Read(PushInt64Instruction.Constant(reader.ReadInt64()), offset); break; case ILOpCode.Br_s: this.Read(BranchInstruction.ILIndex(reader.ReadSByte() + reader.Offset), offset); break; case ILOpCode.Bgt_s: this.Read(BranchGreaterInstruction.ILIndex(reader.ReadSByte() + reader.Offset), offset); break; case ILOpCode.Brfalse_s: this.Read(BranchFalseInstruction.ILIndex(reader.ReadSByte() + reader.Offset), offset); break; case ILOpCode.Call: this.Read(CallInstruction.Handle(reader.ReadInt32()), offset); break; case ILOpCode.Pop: this.Read(PopInstruction.Pop(), offset); break; case ILOpCode.Ret: this.Read(ReturnInstruction.Return(), offset); break; case ILOpCode.Add: this.Read(AddInstruction.Add(), offset); break; case ILOpCode.Sub: this.Read(SubtractInstruction.Subtract(), offset); break; case ILOpCode.Initobj: this.Read(InitObjectInstruction.Handle(reader.ReadInt32()), offset); break; case ILOpCode.Ldfld: this.Read(LoadFieldValueInstruction.Handle(reader.ReadInt32()), offset); break; case ILOpCode.Stfld: this.Read(SaveFieldValueInstruction.Handle(reader.ReadInt32()), offset); break; default: throw new NotSupportedException("Opcode: " + opCode); } } this.ReadEnd(); }
static NopInstruction() { Singleton = new NopInstruction(); }