Пример #1
0
        private static string ToPTX(this ControlFlowInstruction cfi, BasicBlock following)
        {
            JumpInstruction   jump   = cfi as JumpInstruction;
            JumpIfInstruction jumpif = cfi as JumpIfInstruction;

            switch (cfi.OpCode)
            {
            case IROpCodes.JMP:
                return(jump.Target == following ? "" : "bra.uni " + jump.Target.Label + ";\n");

            case IROpCodes.JT:
                return("setp.ne." + jumpif.Flag.DataType.ToPTX() + " %p, 0, " + jumpif.Flag + ";\n" +
                       "@%p bra.uni " + jump.Target.Label + ";\n" +
                       (jumpif.Next == following ? "" : "bra.uni " + jumpif.Next.Label + ";\n"));

            case IROpCodes.JF:
                return("setp.eq." + jumpif.Flag.DataType.ToPTX() + " %p, 0, " + jumpif.Flag + ";\n" +
                       "@%p bra.uni " + jump.Target.Label + ";\n" +
                       (jumpif.Next == following ? "" : "bra.uni " + jumpif.Next.Label + ";\n"));

            case IROpCodes.RET:
                return("ret;\n");

            default:
                throw new NotSupportedException(cfi.OpCode.ToString());
            }
        }
Пример #2
0
        public void ConstructorTest()
        {
            JumpInstruction target = new JumpInstruction();

            // TODO: Implement code to verify target
            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Пример #3
0
        private void ProcessJumpOperation(JumpInstruction instruction,
                                          ref long instructionPointer,
                                          long[] memory)
        {
            var value = GetParameterValue(instruction.Value,
                                          memory);

            bool setInstructionPointer;

            switch (instruction.OpCode)
            {
            case OpCode.JumpIfTrue:
                setInstructionPointer = value != 0;
                break;

            case OpCode.JumpIfFalse:
                setInstructionPointer = value == 0;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(instruction.OpCode));
            }

            if (setInstructionPointer)
            {
                instructionPointer = GetParameterValue(instruction.InstructionPointer,
                                                       memory);
            }
            else
            {
                instructionPointer += 3;
            }
        }
        private void GetTernaryExpression()
        {
            GetUnaryExpression();

            if (_lexer.LookAheadToken().Type == Token.EType.QUESTIONMARK)
            {
                var trueBlock  = new InstructionBlock();
                var falseBlock = new InstructionBlock();
                var exitBlock  = new InstructionBlock();
                var exitJump   = new JumpInstruction(exitBlock);

                var conditionalJump = new ConditionalJumpInstruction(trueBlock, falseBlock);
                _compilerEnvironment.AddInstruction(conditionalJump);

                _lexer.Expect(Token.EType.QUESTIONMARK);

                _compilerEnvironment.SetCurrentBlock(trueBlock);
                GetUnaryExpression();
                _compilerEnvironment.AddInstruction(exitJump);


                _lexer.Expect(Token.EType.COLON);

                _compilerEnvironment.SetCurrentBlock(falseBlock);
                GetUnaryExpression();
                _compilerEnvironment.AddInstruction(exitJump);


                _compilerEnvironment.SetCurrentBlock(exitBlock);
            }
        }
        public void Execute_Quits()
        {
            var eof = new JumpInstruction();

            eof.Execute(ExecutionEnvironment);

            Assert.That(ExecutionEnvironment.Terminated, Is.True);
        }
Пример #6
0
 private bool ExecuteInstruction(JumpInstruction instruction)
 {
     if (instruction.CompareValue > 0)
     {
         index += instruction.JumpValue - 1;
     }
     return(true);
 }
Пример #7
0
        public void ConstructorTest1()
        {
            int target_target1 = 0; // TODO: Initialize to an appropriate value

            JumpInstruction target2 = new JumpInstruction(target_target1);

            // TODO: Implement code to verify target
            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Пример #8
0
        public void AcceptTest()
        {
            JumpInstruction target = new JumpInstruction();

            IVisitor visitor = null; // TODO: Initialize to an appropriate value

            target.Accept(visitor);

            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
        public void ResultShouldBe(string input, string target, string source, JumpInstruction.Operations operation, JumpInstruction.Conditions condition, int operationValue, int conditionValue)
        {
            var result = JumpInstruction.Parse(input);

            Assert.AreEqual(target, result.TargetRegister);
            Assert.AreEqual(source, result.ConditionRegister);
            Assert.AreEqual(operation, result.Operation);
            Assert.AreEqual(condition, result.Condition);
            Assert.AreEqual(operationValue, result.OperationValue);
            Assert.AreEqual(conditionValue, result.ConditionValue);
        }
Пример #10
0
        public void TargetTest()
        {
            JumpInstruction target = new JumpInstruction();

            int val = 0; // TODO: Assign to an appropriate value for the property

            target.Target = val;


            Assert.AreEqual(val, target.Target, "Composestar.StarLight.Entities.WeaveSpec.Instructions.Jump.Target was not set cor" +
                            "rectly.");
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Пример #11
0
        public void move_by_jump_amount_when_integer()
        {
            var nonZeroNumber = Any.NonZeroNumber();
            var jumpDistance  = Any.JumpDistance();
            var computer      = new MockComputerBuilder()
                                .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(1, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition + jumpDistance - 1, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Пример #12
0
        public void JumpTest(int programCounterInitial, int address)
        {
            var instruction = new JumpInstruction();

            var instructionModel = new InstructionModel()
            {
                Type = instruction.Type,
                Data = address,
            };

            var context = new InstructionExecutionContext(new bool[0], programCounterInitial, instructionModel);

            var result = instruction.Execute(context);

            Assert.Equal(address, result.ProgramCounter);
        }
Пример #13
0
        public void stay_for_zero_value_registry()
        {
            var register     = Any.Register();
            var jumpDistance = Any.JumpDistance();

            AdventOfCodeLibrary.Instructions.Computer computer = new MockComputerBuilder()
                                                                 .WithDefaultRegisterValue(register, 0)
                                                                 .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(register, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Пример #14
0
        public void move_by_jump_amount_when_register_non_zero()
        {
            var register      = Any.Register();
            var registerValue = Any.RegisterValue() + 1;
            var jumpDistance  = Any.JumpDistance();

            AdventOfCodeLibrary.Instructions.Computer computer = new MockComputerBuilder()
                                                                 .WithDefaultRegisterValue(register, registerValue)
                                                                 .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(register, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition + jumpDistance - 1, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Пример #15
0
        public void execute_all_with_jump()
        {
            var instructions = new IInstruction[6];

            instructions[0] = new CopyValueToRegisterInstruction('a', 41);
            instructions[1] = new IncrementInstruction('a');
            instructions[2] = new IncrementInstruction('a');
            instructions[3] = new DecrementInstruction('a');
            instructions[4] = new JumpInstruction('a', 2);
            instructions[5] = new DecrementInstruction('a');

            var computer = new MockComputerBuilder()
                           .WithInstructions(instructions)
                           .Build();

            computer.ExecuteAll();

            Assert.AreEqual(42, computer['a']);
        }
Пример #16
0
        public override Address GenerateCode(List <Instruction> instructions)
        {
            Condition.GenerateCode(instructions);

            Label trueLabel = new Label();

            instructions.Add(new LabelInstruction(trueLabel));
            IfBody.GenerateCode(instructions);

            JumpInstruction doneJump = new JumpInstruction(null);

            NextInstructionsToBackpatch.Add(doneJump);
            instructions.Add(doneJump);

            Label falseLabel = new Label();

            instructions.Add(new LabelInstruction(falseLabel));
            ElseBody.GenerateCode(instructions);
            ElseBody.NextInstructionsToBackpatch.AddRange(NextInstructionsToBackpatch);

            Condition.Backpatch(trueLabel, falseLabel);

            return(base.GenerateCode(instructions));
        }
Пример #17
0
        public void JumpInstruction__Largest(string[] input, int expectedResult)
        {
            JumpInstruction jumpInstruction = new JumpInstruction(input);

            Assert.Equal(expectedResult, jumpInstruction.CalcLargest());
        }
Пример #18
0
 public void SetupBase()
 {
     Result = JumpInstruction.Parse(Instruction);
 }
Пример #19
0
 public virtual void VisitJumpInstruction(JumpInstruction instruction)
 {
     DefaultVisitInstruction(instruction);
 }