Пример #1
0
        public void Add_load_two_numbers_then_sum()
        {
            var builder = new CodeBuilder();

            builder.Method("main", isEntryPoint: true);
            builder.Ldc_i4(1);
            builder.Ldc_i4(2);
            builder.Add();
            var instructions = builder.GetBinaryProgram();

            ExecNoDataSingleResultTest(instructions, 3);
        }
Пример #2
0
        public void Add_load_two_numbers_then_sum()
        {
            // Arrange
            var builder = new CodeBuilder();

            builder.Method("main", isEntryPoint: true);
            builder.Ldc_i4(0x01);
            builder.Ldc_i4(2);
            builder.Add();

            // Act
            var actual = builder.GetBinaryProgram();

            // Assert
            Assert.Equal(16, actual.Length);

            // header: entry point address
            Assert.Equal((byte)4, actual[0]);
            Assert.Equal((byte)0, actual[1]);
            Assert.Equal((byte)0, actual[2]);
            Assert.Equal((byte)0, actual[3]);

            // .args 0
            Assert.Equal((byte)0, actual[4]);

            // Ldc.i4
            Assert.Equal((byte)InstructionCode.Ldc_i4, actual[5]);

            // 0x01
            Assert.Equal((byte)1, actual[6]);
            Assert.Equal((byte)0, actual[7]);
            Assert.Equal((byte)0, actual[8]);
            Assert.Equal((byte)0, actual[9]);

            // Ldc.i4
            Assert.Equal((byte)InstructionCode.Ldc_i4, actual[10]);

            // 2
            Assert.Equal((byte)2, actual[11]);
            Assert.Equal((byte)0, actual[12]);
            Assert.Equal((byte)0, actual[13]);
            Assert.Equal((byte)0, actual[14]);

            // Add
            Assert.Equal((byte)InstructionCode.Add, actual[15]);
        }
Пример #3
0
        public void Add_load_two_numbers_then_sum()
        {
            // Arrange
            var expected = new object[] { InstructionCode.Ldc_i4, 0x01, InstructionCode.Ldc_i4, 2, InstructionCode.Add }.ToHashSet();
            var builder = new CodeBuilder();

            builder.Method("main", isEntryPoint: true);
            builder.Ldc_i4(0x01);
            builder.Ldc_i4(2);
            builder.Add();

            // Act
            var actual = builder.GetIlProgram().IlMethods.Single().IlObjects.Select(m => m.Obj).ToHashSet();

            // Assert
            Assert.Subset(expected, actual);
            Assert.Superset(expected, actual);
        }
Пример #4
0
        public void Br_branch_with_two_jump()
        {
            var builder = new CodeBuilder();

            builder.Method("main", isEntryPoint: true);
            builder.Br("start");
            builder.Label("pop");
            builder.Pop();
            builder.Br("end");
            builder.Label("start");
            builder.Ldc_i4(0);
            builder.Ldc_i4(1);
            builder.Ldc_i4(2);
            builder.Add();
            builder.Br("pop");
            builder.Label("end");
            builder.Nop();
            var instructions = builder.GetBinaryProgram();

            ExecNoDataSingleResultTest(instructions, 0);
        }
Пример #5
0
            static CodeBuilder GetBuilder()
            {
                var builder = new CodeBuilder();

                //one
                builder.Method("one");
                builder.Ldc_i4(1);
                builder.Ret();

                //two
                builder.Method("two");
                builder.Ldc_i4(2);
                builder.Ret();

                //main
                builder.Method("main", isEntryPoint: true);
                builder.Ldc_i4(1);
                builder.Call("two");
                builder.Add();
                builder.Ret();

                return(builder);
            }
Пример #6
0
            public static void Test1()
            {
                CodeBuilder code = new CodeBuilder();
                List <Cell> data = new List <Cell>();
                Dictionary <string, int> labels = new Dictionary <string, int>();
                int cp = 0;                 // code position

                // func fac(n)
                //   locals: none
                labels.Add("fac", code.Add(OpCode.ENTER, 0));
                //   if (<= n 1) return 1
                //      load n, push
                code.Add(OpCode.LEA, 2);
                code.Add(OpCode.PUSH);
                //      load 1
                code.Add(OpCode.DATA, code.Data(new Cell(1)));
                //      n <= 1
                code.Add(OpCode.LE);
                //      if A=true return 1
                cp = code.Add(OpCode.BZ); code.Add(cp + 5);                 // if !(n <= 1) skip next instructions
                //        load 1, leave
                code.Add(OpCode.DATA, code.Data(new Cell(1)));
                code.Add(OpCode.LEAVE);

                // return n * fac(n - 1)
                //    n *
                code.Add(OpCode.LEA, 2);
                code.Add(OpCode.PUSH);
                //     fac( n - 1)
                code.Add(OpCode.LEA, 2);
                code.Add(OpCode.PUSH);
                code.Add(OpCode.DATA, code.Data(new Cell(1)));
                code.Add(OpCode.SUB);
                //    fac(n - 1)
                code.Add(OpCode.PUSH);
                code.Add(OpCode.JSR, labels["fac"]);
                code.Add(OpCode.ADJ, 1);
                //    on return result will be in A
                //    n * fac(n - 1)
                code.Add(OpCode.MUL);
#if DEBUG_OPS
                code.Add(OpCode.STATE);
#endif
                //    return
                code.Add(OpCode.LEAVE);

                // int main (int n)
                cp             = code.Add(OpCode.ENTER, 0);
                labels["main"] = cp;
                //    print "Factorial of "
                code.Add(OpCode.DATA, code.Data(new Cell("Factorial of ")));
                code.Add(OpCode.PRINT);
                //    print n
                code.Add(OpCode.LEA, 1);
                code.Add(OpCode.PRINT);
                //    print " is "
                code.Add(OpCode.DATA, code.Data(new Cell(" is ")));
                code.Add(OpCode.PRINT);
                //    print fac(n)
                code.Add(OpCode.LEA, 1);
                code.Add(OpCode.PUSH);
                code.Add(OpCode.JSR, labels["fac"]);
                code.Add(OpCode.PRINT);
                code.Add(OpCode.ADJ, 2);
                code.Add(OpCode.DATA, code.Data(new Cell(Environment.NewLine)));
                code.Add(OpCode.PRINT);
                code.Add(OpCode.DATA, code.Data(new Cell(0)));
                code.Add(OpCode.EXIT);

                code.Entry = labels["main"];
                List <Cell> args = new List <Cell>();
                args.Add(new Cell(10));

                Machine mach = new Machine(code.Generate(), args);
                //int i = 0;
                while (mach.Finished == false)
                {
                    mach.Step();
                }
            }
Пример #7
0
            public void Assemble()
            {
                string[] lines    = readLines();
                int      position = 0;

                builder = new CodeBuilder();
                string[]      words = readWords(lines);
                AssembleState state = AssembleState.None;

                addStandardLabels();

                foreach (string word in words)
                {
                    if (word == "!define")
                    {
                        state.Status = AssembleStatus.DEFINE_NAME;
                        continue;
                    }
                    else if (word == "" && state.Status == AssembleStatus.NONE)
                    {
                        continue;
                    }

                    if (word != "" && state.Status == AssembleStatus.DEFINE_NAME)
                    {
                        state.DefineName = word;
                        state.Status     = AssembleStatus.DEFINE_VALUE;
                    }
                    else if (word != "" && state.Status == AssembleStatus.DEFINE_VALUE)
                    {
                        state.DefineValue = word;
                        if (builder.HasLabel(state.DefineName))
                        {
                            throw new CBDuplicateLabelException(state.DefineName);
                        }
                        builder.SetLabel(state.DefineName, assembleValue(state.DefineValue));
                        state.Status = AssembleStatus.NONE;
                    }
                    else
                    {
                        int value;
                        if (word == "\"")
                        {
                            if (state.Status == AssembleStatus.IN_STRING)
                            {
                                state.StrWords.Add("");
                                string str = string.Join(" ", state.StrWords);
                                position     = builder.Add(builder.Data(str));
                                state.Status = AssembleStatus.NONE;
                            }
                            else
                            {
                                state.StrWords.Clear();
                                state.StrWords.Add("");
                                state.Status = AssembleStatus.IN_STRING;
                            }
                        }
                        else if (state.Status == AssembleStatus.IN_STRING && word.EndsWith("\""))
                        {
                            state.StrWords.Add(word.Substring(0, word.Length - 1));
                            string str = string.Join(" ", state.StrWords);
                            position     = builder.Add(builder.Data(str));
                            state.Status = AssembleStatus.NONE;
                        }
                        else if (state.Status == AssembleStatus.IN_STRING)
                        {
                            state.StrWords.Add(word);
                        }
                        else if (word.StartsWith("\""))
                        {
                            if (word.EndsWith("\""))                             // Have full string
                            {
                                position = builder.Add(builder.Data(word.Substring(1, word.Length - 2)));
                            }
                            else
                            {
                                // string got broken up
                                state.StrWords.Clear();
                                state.StrWords.Add(word.Substring(1));
                                state.Status = AssembleStatus.IN_STRING;
                            }
                        }
                        else if (word.StartsWith("$"))
                        {
                            if (!int.TryParse(word.Substring(1), out value))
                            {
                                value = assembleValue(word.Substring(1));
                            }
                            position = builder.Add(builder.Data(value));
                        }
                        else if (int.TryParse(word, out value))
                        {
                            position = builder.Add(value);
                        }
                        else if (word.EndsWith(":"))                             // label
                        // off-by-one error on first label
                        {
                            int relPosition = (position > 0) ? position + 1 : 0;
                            builder.SetLabel(word.Substring(0, word.Length - 1), relPosition);
                        }
                        else if (word != "")
                        {
                            position = builder.Delayed(word);
                        }
                    }
                }
            }
Пример #8
0
        private void ParseAdd()
        {
            var op = ExpectToken(SyntaxKind.OpCode_Add);

            _codeBuilder.Add();
        }