public void SetUp()
        {
            Instruction instr1 = new AddInstruction(2, 3, 4);
            Instruction instr2 = new LuiInstruction(5, 0xFFFF);

            Instruction[] instructions = { instr1, instr2 };
            target = new InstructionMemory(instructions);
        }
        public void Execute_PlusThreeToZero_ReturnsThree()
        {
            AddInstruction instruction = new AddInstruction(3);

            var expectedOutput = 3;

            Assert.AreEqual(expectedOutput, instruction.Execute(0));
        }
        public void SetInstruction_ValidIndex()
        {
            target = new InstructionMemory(8);
            Instruction instr = new AddInstruction(1, 1, 2);

            target[4] = 0x00220820;

            //Assert.AreEqual(instr, target.GetInstruction(4));
        }
Ejemplo n.º 4
0
        public static Machine getSampleMachine()
        {
            Machine machine = new Machine(6);

            Instruction i0 = new AddInstruction();
            List <int>  p  = new List <int>(); p.Add(2); p.Add(3); p.Add(0); // M2 = 0 + M0

            i0.setParameters(p);
            machine.addInstruction(i0);

            Instruction i1 = new AssignValueInstruction();

            p.Clear(); p.Add(4); p.Add(1); //M4 = 1
            i1.setParameters(p);
            machine.addInstruction(i1);

            Instruction i2 = new GotoIfInstruction();

            p.Clear(); p.Add(5); p.Add(2); // goto 5 if M2 > 0
            i2.setParameters(p);
            machine.addInstruction(i2);

            Instruction i3 = new AddInstruction();

            p.Clear(); p.Add(0); p.Add(3); p.Add(5); // M0 = M3 + 0
            i3.setParameters(p);
            machine.addInstruction(i3);

            Instruction i4 = new HaltInstruction();

            machine.addInstruction(i4); //halt

            Instruction i5 = new AddInstruction();

            p.Clear(); p.Add(3); p.Add(3); p.Add(1); // M3 = M3 + M1
            i5.setParameters(p);
            machine.addInstruction(i5);

            Instruction i6 = new SubstractInstruction();

            p.Clear(); p.Add(2); p.Add(2); p.Add(4); // M2 = M2 - 1
            i6.setParameters(p);
            machine.addInstruction(i6);

            Instruction i7 = new GotoIfInstruction();

            p.Clear(); p.Add(2); p.Add(4); // goto 2
            i7.setParameters(p);
            machine.addInstruction(i7);

            int[] input = { 5, 6 };
            machine.insertInput(input);

            // when machine halts result will be in M0
            return(machine);
        }
Ejemplo n.º 5
0
        public void Execute_Overflow()
        {
            reg[9]  = 0xFFFFFFFF;
            reg[10] = 0x1;
            target  = new AddInstruction(8, 9, 10);

            target.Execute(ref pc, mem, reg);

            Assert.AreEqual(0x0, reg[8]);
            Assert.AreEqual(0x4, pc);
        }
Ejemplo n.º 6
0
        public void Execute_NegativeNumber()
        {
            reg[9]  = 0x3;
            reg[10] = 0xFFFFFFFF;
            target  = new AddInstruction(8, 9, 10);

            target.Execute(ref pc, mem, reg);

            Assert.AreEqual(0x2, reg[8]);
            Assert.AreEqual(0x4, pc);
        }
Ejemplo n.º 7
0
        public void Execute_PositiveNumbers()
        {
            reg[9]  = 0x1;
            reg[10] = 0x3;
            target  = new AddInstruction(8, 9, 10);

            target.Execute(ref pc, mem, reg);

            Assert.AreEqual(0x4, reg[8]);
            Assert.AreEqual(0x4, pc);
        }
Ejemplo n.º 8
0
 public void EmitAdd(Type type, bool @checked)
 {
     if (@checked)
     {
         Emit(AddOvfInstruction.Create(type));
     }
     else
     {
         Emit(AddInstruction.Create(type));
     }
 }
Ejemplo n.º 9
0
        protected override void VisitAddInstruction(AddInstruction instruction)
        {
            ControlState.EvaluationStack.Pop(out var stackVal1, out var stackVal2);
            var resultStackVal = ComputeBinaryNumericOperation(
                stackVal1,
                stackVal2,
                (a, b) => new CilStackValueInt32(a.Value + b.Value),
                (a, b) => new CilStackValueInt64(a.Value + b.Value),
                (a, b) => new CilStackValueFloat(a.Value + b.Value)
                );

            ControlState.EvaluationStack.Push(resultStackVal);

            ControlState.MoveToNextInstruction();
        }
Ejemplo n.º 10
0
        private Operand MakeVariableAssignment(VariableAssignmentNode node, ILGenerator generator)
        {
            SymbolOperand leftHandSide  = node.Accessor.Visit(generator) as SymbolOperand;
            Operand       rightHandSide = node.Right.Visit(generator);

            // If right-hand side exists and has a type, do some type checking

            /*if (rightHandSide.Type != leftHandSide.Type)
             *  throw new AstWalkerException($"Cannot convert {rightHandSide.Type} to {leftHandSide.Type} ('{leftHandSide.Name}')");*/

            if (node.Operator.Type == TokenType.Assignment)
            {
                generator.Emmit(new StoreInstruction(leftHandSide, rightHandSide));
                return(leftHandSide);
            }


            Instruction instr = null;

            switch (node.Operator.Type)
            {
            case TokenType.IncrementAndAssign:
                instr = new AddInstruction(leftHandSide, leftHandSide, rightHandSide);
                break;

            case TokenType.DecrementAndAssign:
                instr = new SubInstruction(leftHandSide, leftHandSide, rightHandSide);
                break;

            case TokenType.MultAndAssign:
                instr = new MultInstruction(leftHandSide, leftHandSide, rightHandSide);
                break;

            case TokenType.DivideAndAssign:
                instr = new DivInstruction(leftHandSide, leftHandSide, rightHandSide);
                break;

            default:
                throw new InvalidInstructionException($"Unsupported operation: {node.Operator.Value} ({node.Operator.Type})");
            }

            generator.Emmit(instr);

            return(leftHandSide);
        }
Ejemplo n.º 11
0
        public static void Add(Compiler cmp, Argument[] args)
        {
            Function fct = cmp.Functions.Last();

            Variable v1 = fct.GetVariable(args[1]);
            Variable v2 = fct.GetVariable(args[2]) ?? TryConst(args[2]);

            AddInstruction i = new AddInstruction
            {
                From = new Variable[]
                {
                    v1,
                    v2
                },
                To = fct.GetVariable(args[3])
            };

            fct.Instructions.Add(i);
        }
Ejemplo n.º 12
0
        static AssemblerController()
        {
            Assemblers["ADD"] = new AddInstruction();
            Assemblers["AND"] = new AndInstruction();

            Assemblers["BR"]    = new BranchInstruction("BR", true, true, true); // Alias of BRNZP
            Assemblers["BRN"]   = new BranchInstruction("BRN", true, false, false);
            Assemblers["BRZ"]   = new BranchInstruction("BRZ", false, true, false);
            Assemblers["BRP"]   = new BranchInstruction("BRP", false, false, true);
            Assemblers["BRZP"]  = new BranchInstruction("BRZP", false, true, true);
            Assemblers["BRNP"]  = new BranchInstruction("BRNP", true, false, true);
            Assemblers["BRNZ"]  = new BranchInstruction("BRNZ", true, true, false);
            Assemblers["BRNZP"] = new BranchInstruction("BRNZP", true, true, true);

            Assemblers["JMP"]  = new RegisterBasedPCAccessInstruction("JMP", 0b1100);
            Assemblers["JSR"]  = new JSRInstruction();
            Assemblers["JSRR"] = new RegisterBasedPCAccessInstruction("JSRR", 0b0100);
            Assemblers["LD"]   = new LabelBasedMemoryAccessInstruction("LD", 0b0010);
            Assemblers["LDI"]  = new LabelBasedMemoryAccessInstruction("LDI", 0b1010);
            Assemblers["LDR"]  = new OffsetBasedMemoryAccessInstruction("LDR", 0b0110);
            Assemblers["LEA"]  = new LabelBasedMemoryAccessInstruction("LEA", 0b1110);
            Assemblers["NOT"]  = new NotInstruction();
            Assemblers["RET"]  = new ZeroArgumentsInstruction("RET", 0xC1C0); // 1100 000 111 000000, i.e. JMP R7
            Assemblers["RTI"]  = new ZeroArgumentsInstruction("RTI", 0x8000); // 1000 000000000000
            Assemblers["ST"]   = new LabelBasedMemoryAccessInstruction("ST", 0b0011);
            Assemblers["STI"]  = new LabelBasedMemoryAccessInstruction("STI", 0b1011);
            Assemblers["STR"]  = new OffsetBasedMemoryAccessInstruction("STR", 0b0111);
            Assemblers["TRAP"] = new TrapInstruction();

            Assemblers["GETC"]  = new ZeroArgumentsInstruction("GETC", 0xF020);  // TRAP x20
            Assemblers["OUT"]   = new ZeroArgumentsInstruction("OUT", 0xF021);   // TRAP x21
            Assemblers["PUTS"]  = new ZeroArgumentsInstruction("PUTS", 0xF022);  // TRAP x22
            Assemblers["IN"]    = new ZeroArgumentsInstruction("IN", 0xF023);    // TRAP x23
            Assemblers["PUTSP"] = new ZeroArgumentsInstruction("PUTSP", 0xF024); // TRAP x24
            Assemblers["HALT"]  = new ZeroArgumentsInstruction("HALT", 0xF025);  // TRAP x25
        }
Ejemplo n.º 13
0
 private bool ExecuteInstruction(AddInstruction instruction)
 {
     registers[instruction.RegisterAddress] += instruction.Value;
     return(true);
 }
Ejemplo n.º 14
0
 public void VisitAdd(AddInstruction instruction) => IncrementCallCount(nameof(VisitAdd));
Ejemplo n.º 15
0
        public static Machine GetSampleMachine()
        {
            var machine = new Machine(6);

            Instruction ix = new AssignValueInstruction();

            ix.SetParameters(new List <int> {
                0, 5
            });
            machine.AddInstruction(ix);

            Instruction iy = new AssignValueInstruction();

            iy.SetParameters(new List <int> {
                1, 6
            });
            machine.AddInstruction(iy);

            Instruction i0 = new AddInstruction();

            i0.SetParameters(new List <int> {
                2, 3, 0
            });                                          // M2 = 0 + M0
            machine.AddInstruction(i0);

            Instruction i1 = new AssignValueInstruction();

            i1.SetParameters(new List <int> {
                4, 1
            });                                       //M4 = 1
            machine.AddInstruction(i1);

            Instruction i2 = new GotoIfInstruction();

            i2.SetParameters(new List <int> {
                7, 2
            });                                       // goto 7 if M2 > 0
            machine.AddInstruction(i2);

            Instruction i3 = new AddInstruction();

            i3.SetParameters(new List <int> {
                0, 3, 5
            });                                          // M0 = M3 + 5
            machine.AddInstruction(i3);

            Instruction i4 = new HaltInstruction();

            machine.AddInstruction(i4); //halt

            Instruction i5 = new AddInstruction();

            i5.SetParameters(new List <int> {
                3, 3, 1
            });                                          // M3 = M3 + M1
            machine.AddInstruction(i5);

            Instruction i6 = new SubstractInstruction();

            i6.SetParameters(new List <int> {
                2, 2, 4
            });                                          // M2 = M2 - 1
            machine.AddInstruction(i6);

            Instruction i7 = new GotoIfInstruction();

            i7.SetParameters(new List <int> {
                4, 4
            });                                       // goto 4 if M4 > 0
            machine.AddInstruction(i7);

            // when machine halts result will be in M0
            return(machine);
        }
Ejemplo n.º 16
0
 public void EmitAdd(Type type, bool @checked)
 {
     Emit(@checked ? AddOvfInstruction.Create(type) : AddInstruction.Create(type));
 }
Ejemplo n.º 17
0
        public override CILInstructionNone BuildNode(ParseTreeNode node)
        {
            var instrNoneParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.INSTR_NONE);

            var addParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_add);

            if (addParseTreeNode != null)
            {
                var addInstruction = new AddInstruction();
                return(addInstruction);
            }

            var andParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_and);

            if (andParseTreeNode != null)
            {
                var andInstruction = new AndInstruction();
                return(andInstruction);
            }

            var ceqParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ceq);

            if (ceqParseTreeNode != null)
            {
                var checkIfEqualInstruction = new CheckIfEqualInstruction();
                return(checkIfEqualInstruction);
            }

            var cgtParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_cgt);

            if (cgtParseTreeNode != null)
            {
                var checkIfGreaterInstruction = new CheckIfGreaterInstruction();
                return(checkIfGreaterInstruction);
            }

            var cltParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_clt);

            if (cltParseTreeNode != null)
            {
                var checkIfLessInstruction = new CheckIfLessInstruction();
                return(checkIfLessInstruction);
            }

            var convi1ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convi1);

            if (convi1ParseTreeNode != null)
            {
                var convertToSByteInstruction = new ConvertToSByteInstruction();
                return(convertToSByteInstruction);
            }

            var convi2ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convi2);

            if (convi2ParseTreeNode != null)
            {
                var convertToShortInstruction = new ConvertToShortInstruction();
                return(convertToShortInstruction);
            }

            var convi4ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convi4);

            if (convi4ParseTreeNode != null)
            {
                var convertToIntInstruction = new ConvertToIntInstruction();
                return(convertToIntInstruction);
            }

            var convi8ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convi8);

            if (convi8ParseTreeNode != null)
            {
                var convertToLongInstruction = new ConvertToLongInstruction();
                return(convertToLongInstruction);
            }

            var convr4ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convr4);

            if (convr4ParseTreeNode != null)
            {
                var convertToFloatInstruction = new ConvertToFloatInstruction();
                return(convertToFloatInstruction);
            }

            var convr8ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convr8);

            if (convr8ParseTreeNode != null)
            {
                var convertToDoubleInstruction = new ConvertToDoubleInstruction();
                return(convertToDoubleInstruction);
            }

            var convu8ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_convu8);

            if (convu8ParseTreeNode != null)
            {
                var convertToUnsignedLongInstruction = new ConvertToUnsignedLongInstruction();
                return(convertToUnsignedLongInstruction);
            }

            var divParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_div);

            if (divParseTreeNode != null)
            {
                var divideInstruction = new DivideInstruction();
                return(divideInstruction);
            }

            var dupParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_dup);

            if (dupParseTreeNode != null)
            {
                var duplicateInstruction = new DuplicateInstruction();
                return(duplicateInstruction);
            }

            var ldarg0ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldarg0);

            if (ldarg0ParseTreeNode != null)
            {
                var loadArgument0Instruction = new LoadArgument0Instruction();
                return(loadArgument0Instruction);
            }

            var ldarg1ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldarg1);

            if (ldarg1ParseTreeNode != null)
            {
                var loadArgument1Instruction = new LoadArgument1Instruction();
                return(loadArgument1Instruction);
            }

            var ldarg2ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldarg2);

            if (ldarg2ParseTreeNode != null)
            {
                var loadArgument2Instruction = new LoadArgument2Instruction();
                return(loadArgument2Instruction);
            }

            var ldarg3ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldarg3);

            if (ldarg3ParseTreeNode != null)
            {
                var loadArgument3Instruction = new LoadArgument3Instruction();
                return(loadArgument3Instruction);
            }

            var ldci40ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci40);

            if (ldci40ParseTreeNode != null)
            {
                var loadConstantInt0Instruction = new LoadConstantInt0Instruction();
                return(loadConstantInt0Instruction);
            }

            var ldci41ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci41);

            if (ldci41ParseTreeNode != null)
            {
                var loadConstantInt1Instruction = new LoadConstantInt1Instruction();
                return(loadConstantInt1Instruction);
            }

            var ldci42ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci42);

            if (ldci42ParseTreeNode != null)
            {
                var loadConstantInt2Instruction = new LoadConstantInt2Instruction();
                return(loadConstantInt2Instruction);
            }

            var ldci43ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci43);

            if (ldci43ParseTreeNode != null)
            {
                var loadConstantInt3Instruction = new LoadConstantInt3Instruction();
                return(loadConstantInt3Instruction);
            }

            var ldci44ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci44);

            if (ldci44ParseTreeNode != null)
            {
                var loadConstantInt4Instruction = new LoadConstantInt4Instruction();
                return(loadConstantInt4Instruction);
            }

            var ldci45ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci45);

            if (ldci45ParseTreeNode != null)
            {
                var loadConstantInt5Instruction = new LoadConstantInt5Instruction();
                return(loadConstantInt5Instruction);
            }

            var ldci46ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci46);

            if (ldci46ParseTreeNode != null)
            {
                var loadConstantInt6Instruction = new LoadConstantInt6Instruction();
                return(loadConstantInt6Instruction);
            }

            var ldci47ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci47);

            if (ldci47ParseTreeNode != null)
            {
                var loadConstantInt7Instruction = new LoadConstantInt7Instruction();
                return(loadConstantInt7Instruction);
            }

            var ldci48ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci48);

            if (ldci48ParseTreeNode != null)
            {
                var loadConstantInt8Instruction = new LoadConstantInt8Instruction();
                return(loadConstantInt8Instruction);
            }

            var ldci4m1ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldci4m1);

            if (ldci4m1ParseTreeNode != null)
            {
                var loadConstantIntMinus1Instruction = new LoadConstantIntMinus1Instruction();
                return(loadConstantIntMinus1Instruction);
            }

            var ldelemi4ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldelemi4);

            if (ldelemi4ParseTreeNode != null)
            {
                var loadElementIntInstruction = new LoadElementIntInstruction();
                return(loadElementIntInstruction);
            }

            var ldelemrefParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldelemref);

            if (ldelemrefParseTreeNode != null)
            {
                var loadElementRefInstruction = new LoadElementRefInstruction();
                return(loadElementRefInstruction);
            }

            var ldlenParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldlen);

            if (ldlenParseTreeNode != null)
            {
                var loadLengthInstruction = new LoadLengthInstruction();
                return(loadLengthInstruction);
            }

            var ldloc0ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldloc0);

            if (ldloc0ParseTreeNode != null)
            {
                var loadLocalVariable0Instruction = new LoadLocalVariable0Instruction();
                return(loadLocalVariable0Instruction);
            }

            var ldloc1ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldloc1);

            if (ldloc1ParseTreeNode != null)
            {
                var loadLocalVariable1Instruction = new LoadLocalVariable1Instruction();
                return(loadLocalVariable1Instruction);
            }

            var ldloc2ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldloc2);

            if (ldloc2ParseTreeNode != null)
            {
                var loadLocalVariable2Instruction = new LoadLocalVariable2Instruction();
                return(loadLocalVariable2Instruction);
            }

            var ldloc3ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldloc3);

            if (ldloc3ParseTreeNode != null)
            {
                var loadLocalVariable3Instruction = new LoadLocalVariable3Instruction();
                return(loadLocalVariable3Instruction);
            }

            var mulParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_mul);

            if (mulParseTreeNode != null)
            {
                var multiplyInstruction = new MultiplyInstruction();
                return(multiplyInstruction);
            }

            var notParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_not);

            if (notParseTreeNode != null)
            {
                var notInstruction = new NotInstruction();
                return(notInstruction);
            }

            var orParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_or);

            if (orParseTreeNode != null)
            {
                var orInstruction = new OrInstruction();
                return(orInstruction);
            }

            var popParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_pop);

            if (popParseTreeNode != null)
            {
                var popInstruction = new PopInstruction();
                return(popInstruction);
            }

            var remParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_rem);

            if (remParseTreeNode != null)
            {
                var remainderInstruction = new RemainderInstruction();
                return(remainderInstruction);
            }

            var retParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ret);

            if (retParseTreeNode != null)
            {
                var returnInstruction = new ReturnInstruction();
                return(returnInstruction);
            }

            var shlParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_shl);

            if (shlParseTreeNode != null)
            {
                var shiftLeftInstruction = new ShiftLeftInstruction();
                return(shiftLeftInstruction);
            }

            var shrParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_shr);

            if (shrParseTreeNode != null)
            {
                var shiftRightInstruction = new ShiftRightInstruction();
                return(shiftRightInstruction);
            }

            var stelemi4ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stelemi4);

            if (stelemi4ParseTreeNode != null)
            {
                var setElementIntInstruction = new SetElementIntInstruction();
                return(setElementIntInstruction);
            }

            var stelemrefParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stelemref);

            if (stelemrefParseTreeNode != null)
            {
                var setElementRefInstruction = new SetElementRefInstruction();
                return(setElementRefInstruction);
            }

            var stloc0ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stloc0);

            if (stloc0ParseTreeNode != null)
            {
                var setLocalVariable0Instruction = new SetLocalVariable0Instruction();
                return(setLocalVariable0Instruction);
            }

            var stloc1ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stloc1);

            if (stloc1ParseTreeNode != null)
            {
                var setLocalVariable1Instruction = new SetLocalVariable1Instruction();
                return(setLocalVariable1Instruction);
            }

            var stloc2ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stloc2);

            if (stloc2ParseTreeNode != null)
            {
                var setLocalVariable2Instruction = new SetLocalVariable2Instruction();
                return(setLocalVariable2Instruction);
            }

            var stloc3ParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stloc3);

            if (stloc3ParseTreeNode != null)
            {
                var setLocalVariable3Instruction = new SetLocalVariable3Instruction();
                return(setLocalVariable3Instruction);
            }

            var subParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_sub);

            if (subParseTreeNode != null)
            {
                var subtractInstruction = new SubtractInstruction();
                return(subtractInstruction);
            }

            var xorParseTreeNode = instrNoneParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_xor);

            if (xorParseTreeNode != null)
            {
                var xorInstruction = new XorInstruction();
                return(xorInstruction);
            }

            throw new ArgumentException("Cannot recognize CIL instruction none.");
        }
Ejemplo n.º 18
0
        public void ToString_Formatted()
        {
            target = new AddInstruction(8, 9, 10);

            Assert.AreEqual("ADD $t0, $t1, $t2", target.ToString());
        }
Ejemplo n.º 19
0
        public Instruction Translate()
        {
            Instruction instruction;

            switch (Type)
            {
            case CommandType.AssignValue:
                instruction = new AssignValueInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.Add:
                instruction = new AddInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value, Arg3.Value
                });
                break;

            case CommandType.Substract:
                instruction = new SubstractInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value, Arg3.Value
                });
                break;

            case CommandType.Divide:
                instruction = new DivideByTwoInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value
                });
                break;

            case CommandType.CopyValue:
                instruction = new CopyValueInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.CopyValue2:
                instruction = new CopyValue2Instruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.GotoIf:
                instruction = new GotoIfInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.Halt:
                instruction = new HaltInstruction();
                instruction.SetParameters(new List <int>());
                break;

            default:
                throw new InvalidEnumArgumentException();
            }
            return(instruction);
        }
Ejemplo n.º 20
0
        private bool GenerateNode(out OneOperandNode node)
        {
            node = null;

            InstructionToken instructionToken;

            if (this.ExpectInstructionToken(out instructionToken))
            {
                Instructions instruction = instructionToken.Instruction;
                switch (instruction)
                {
                case Instructions.Mov:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new MovInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Add:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new AddInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Sub:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new SubInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Mul:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new MulInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Div:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new DivInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Cmp:
                {
                    Token operand1;
                    Token operand2;
                    if (this.ExpectTwoOperands(out operand1, out operand2))
                    {
                        node = new CmpInstruction(this.rawInput, instructionToken, operand1, operand2);
                    }
                    else
                    {
                        this.LogExpectedTwoOperandsError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Inc:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new IncInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Dec:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new DecInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Jmp:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new JmpInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Jeq:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new JeqInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Jne:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new JneInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Jsm:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new JsmInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;

                case Instructions.Jns:
                {
                    Token operand;
                    if (this.ExpectOneOperand(out operand))
                    {
                        node = new JnsInstruction(this.rawInput, instructionToken, operand);
                    }
                    else
                    {
                        this.LogExpectedOneOperandError(instructionToken);
                        return(false);
                    }
                }
                break;
                }
            }

            return(true);
        }
Ejemplo n.º 21
0
 protected abstract void VisitAddInstruction(AddInstruction instruction);
Ejemplo n.º 22
0
        public Operand Visit(ILGenerator generator, BinaryNode binary)
        {
            Operand left  = binary.Left.Visit(generator);
            Operand right = binary.Right.Visit(generator);

            SymbolOperand tmpname = generator.SymbolTable.NewTempSymbol(OperandType.Auto);

            Instruction instr = null;

            switch (binary.Operator.Type)
            {
            case TokenType.Addition:
                instr = new AddInstruction(tmpname, left, right);
                break;

            case TokenType.Minus:
                instr = new SubInstruction(tmpname, left, right);
                break;

            case TokenType.Multiplication:
                instr = new MultInstruction(tmpname, left, right);
                break;

            case TokenType.Division:
                instr = new DivInstruction(tmpname, left, right);
                break;


            case TokenType.Or:
                instr = new OrInstruction(tmpname, left, right);
                break;

            case TokenType.And:
                instr = new AndInstruction(tmpname, left, right);
                break;


            case TokenType.GreatThan:
                instr = new CgtInstruction(tmpname, left, right);
                break;

            case TokenType.GreatThanEqual:
                instr = new CgteInstruction(tmpname, left, right);
                break;

            case TokenType.LessThan:
                instr = new CltInstruction(tmpname, left, right);
                break;

            case TokenType.LessThanEqual:
                instr = new ClteInstruction(tmpname, left, right);
                break;


            case TokenType.Equal:
            case TokenType.NotEqual:     // Use NOT instruction
                instr = new CeqInstruction(tmpname, left, right);
                break;
            }

            generator.Emmit(new VarInstruction(tmpname, null));
            generator.Emmit(instr);

            if (binary.Operator.Type == TokenType.NotEqual)
            {
                SymbolOperand notname = generator.SymbolTable.NewTempSymbol(OperandType.Auto);
                generator.Emmit(new VarInstruction(notname, null));
                generator.Emmit(new NotInstruction(notname, tmpname));
                return(notname);
            }

            return(tmpname);
        }
Ejemplo n.º 23
0
		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();
		}
        protected override void PrepareNonCommonPar()
        {
            ParVar fv = GetUniquePar <ParVar>(DefPar.AddHHMembers.FlagVar);

            if (fv != null)
            {
                flagVar = new VarSpec()
                {
                    name = fv.name
                }
            }
            ;

            foreach (var g in GetParGroups(DefPar.AddHHMembers.GROUP_MAIN))
            {
                var            group = g.Value; int groupNo = g.Key;
                AddInstruction addInstruction = new AddInstruction();

                // first find out whether we are adding children or partners or other persons ...
                ParCateg parWho = GetUniqueGroupPar <ParCateg>(DefPar.AddHHMembers.Add_Who, group); if (parWho == null)
                {
                    continue;                                                                                                    // compulsory parameter, error already issued
                }
                addInstruction.addWho = parWho.GetCateg();

                // ... depending on that, check the add-condition
                ParCond parParent = GetUniqueGroupPar <ParCond>(DefPar.AddHHMembers.ParentCond, group);
                ParCond parPartner = GetUniqueGroupPar <ParCond>(DefPar.AddHHMembers.PartnerCond, group);
                ParCond parOther = GetUniqueGroupPar <ParCond>(DefPar.AddHHMembers.HHCond, group);
                string  missing = string.Empty; string tooMuch = string.Empty;
                switch (addInstruction.addWho)
                {
                case DefPar.Value.ADDHHMEMBERS_CHILD:
                    if (parParent != null)
                    {
                        addInstruction.cond = parParent;
                    }
                    else
                    {
                        missing = DefPar.AddHHMembers.ParentCond;
                    }
                    if (parPartner != null)
                    {
                        tooMuch = DefPar.AddHHMembers.PartnerCond + " ";
                    }
                    if (parOther != null)
                    {
                        tooMuch = DefPar.AddHHMembers.HHCond;
                    }
                    ParBool parIsPP = GetUniqueGroupPar <ParBool>(DefPar.AddHHMembers.IsPartnerParent, group);
                    if (parIsPP != null)
                    {
                        addInstruction.isPartnerParent = parIsPP.GetBoolValue();
                    }
                    break;

                case DefPar.Value.ADDHHMEMBERS_PARTNER:
                    if (parParent != null)
                    {
                        tooMuch = DefPar.AddHHMembers.ParentCond + " ";
                    }
                    if (parPartner != null)
                    {
                        addInstruction.cond = parPartner;
                    }
                    else
                    {
                        missing = DefPar.AddHHMembers.PartnerCond;
                    }
                    if (parOther != null)
                    {
                        tooMuch = DefPar.AddHHMembers.HHCond;
                    }
                    break;

                case DefPar.Value.ADDHHMEMBERS_OTHER:
                    if (parParent != null)
                    {
                        tooMuch = DefPar.AddHHMembers.ParentCond + " ";
                    }
                    if (parPartner != null)
                    {
                        tooMuch = DefPar.AddHHMembers.PartnerCond;
                    }
                    if (parOther != null)
                    {
                        addInstruction.cond = parOther;
                    }
                    else
                    {
                        missing = DefPar.AddHHMembers.HHCond;
                    }
                    break;

                default: continue;     // error is caught by general error handling
                }
                if (missing != string.Empty)
                {
                    infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                    {
                        isWarning = false,
                        message   = $"{description.Get()} (group {groupNo}): {DefPar.AddHHMembers.Add_Who}={addInstruction.addWho} requires parameter {missing}"
                    });
                }
                if (tooMuch != string.Empty)
                {
                    infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                    {
                        isWarning = true,
                        message   = $"{description.Get()} (group {groupNo}): {DefPar.AddHHMembers.Add_Who}={addInstruction.addWho} does not require {tooMuch}"
                    });
                }

                // here we are only gathering the variables that are to be set, but do not evaluate whether they exist
                // note: this function does not "register" variables, i.e. if a variable is not used anywhere else, it cannot be set
                foreach (ParFormula parVarDefinition in GetPlaceholderGroupPar <ParFormula>(group))
                {
                    string varName = parVarDefinition.description.GetParName();
                    if (addInstruction.prepVarDefinitions.ContainsKey(varName))
                    {
                        infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                        {
                            isWarning = false,
                            message   = $"{parVarDefinition.description.Get()}: double definition of variable {varName}"
                        });
                        continue;
                    }
                    if (varName == DefVarName.IDHH || varName == DefVarName.IDPERSON || varName == DefVarName.DWT || varName == DefVarName.DCT)
                    {
                        infoStore.communicator.ReportError(new Communicator.ErrorInfo()
                        {
                            isWarning = false,
                            message   = $"{parVarDefinition.description.Get()}: variable {varName} is system-set and cannot be changed"
                        });
                        continue;
                    }
                    addInstruction.prepVarDefinitions.Add(varName, parVarDefinition);
                }
                addInstructions.Add(addInstruction);
            }
        }