Пример #1
0
        public void Run()
        {
            UInt64 startOffset;

            byte[] byteCode = null;

            using (FileStream fileStream = new FileStream(raProgram, FileMode.Open))
            {
                UInt64 fileStreamLength = (UInt64)fileStream.Length;
                //Console.WriteLine("fileStreamLength = {0}", fileStreamLength);

                //
                // 1. Read Meta Data
                //
                UInt64 bytesRead = 0;
                //Console.WriteLine("bytesRead: {0}", bytesRead);
                startOffset = Util.ReadAddressValue(fileStream, ref bytesRead);
                //Console.WriteLine("bytesRead: {0} startOffset = {1}", bytesRead, startOffset);

                //
                // 2. Read the byte code
                //
                Int32 length = (Int32)(fileStreamLength - bytesRead);
                //Console.WriteLine();
                //Console.WriteLine("Reading Byte Code ({0} bytes)...", length);
                byteCode = new Byte[fileStreamLength - bytesRead];

                Int32 lastBytesRead;
                Int32 offset = 0;

                do
                {
                    lastBytesRead = fileStream.Read(byteCode, offset, length);
                    length       -= lastBytesRead;
                    if (length <= 0)
                    {
                        break;
                    }
                    offset += lastBytesRead;
                } while (lastBytesRead > 0);

                if (length > 0)
                {
                    throw new EndOfStreamException(String.Format("Still needed {0} bytes", length));
                }
                //Console.WriteLine("Done Reading Byte Code");
            }


            if (byteCode != null)
            {
                InstructionProcessor.Execute(byteCode, startOffset, processStack);
            }
        }
        public void OnAndOffTest()
        {
            UInt64 offset;

            memory.Reset();

            //
            // Generate Code
            //
            offset             = 0;
            offset             = ByteCode.EmitON(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 0, 0, 0, 0));
            offset             = ByteCode.EmitON(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 1));
            offset             = ByteCode.EmitON(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 0, 2));
            offset             = ByteCode.EmitON(byteCode, offset, new MemoryOp(3));
            byteCode[offset++] = Instructions.Halt;

            //
            // Run Code
            //
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));

            //
            // Test Results
            //
            Assert.IsTrue(memory.TestAddressEqualsValue(0, UInt64.MaxValue));
            Assert.IsTrue(memory.TestAddressEqualsValue(1, UInt64.MaxValue));
            Assert.IsTrue(memory.TestAddressEqualsValue(2, UInt64.MaxValue));
            Assert.IsTrue(memory.TestAddressEqualsValue(3, UInt64.MaxValue));


            //
            // Generate Code
            //
            offset             = 0;
            offset             = ByteCode.EmitOff(byteCode, offset, new MemoryOp(0));
            offset             = ByteCode.EmitOff(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 1));
            offset             = ByteCode.EmitOff(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 0, 0, 0, 2));
            offset             = ByteCode.EmitOff(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 3));
            byteCode[offset++] = Instructions.Halt;

            //
            // Run Code
            //
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));

            //
            // Test Results
            //
            Assert.IsTrue(memory.TestAddressEqualsValue(0, 0U));
            Assert.IsTrue(memory.TestAddressEqualsValue(1, 0U));
            Assert.IsTrue(memory.TestAddressEqualsValue(2, 0U));
            Assert.IsTrue(memory.TestAddressEqualsValue(3, 0U));
        }
Пример #3
0
        private void MoveTest(params MemAndValue[] t)
        {
            UInt64 offset;

            memory.Reset();

            //
            // Generate Code
            //
            offset = 0;
            for (int i = 0; i < t.Length; i++)
            {
                offset = ByteCode.EmitAssign(byteCode, offset, t[i].dst, t[i].src);
            }
            byteCode[offset++] = Instructions.Halt;

            //
            // Run Code
            //
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));

            //
            // Test Results
            //
            memory.Print(0, 16);
            for (int i = 0; i < t.Length; i++)
            {
                MemoryOp dst = t[i].dst;
                Op       src = t[i].src;
                Console.WriteLine("Asserting [{0}] EmitMem={1}, EmitVal={2}", i, dst, src);

                Byte[] emitWriteCode = new Byte[dst.byteLength];
                Byte[] emitReadCode  = new Byte[src.byteLength];

                dst.Emit(emitWriteCode, 0);
                src.Emit(emitReadCode, 0);

                offset = 0;
                Memory.ReadOperandLogic emitWriteReadOperand = memory.ParseWriteOperandForReading(emitWriteCode, ref offset);
                Assert.AreEqual((UInt64)emitWriteCode.Length, offset);

                offset = 0;
                Memory.ReadOperandLogic emitReadReadOperand = memory.ParseReadOperand(emitReadCode, ref offset);
                Assert.AreEqual((UInt64)emitReadCode.Length, offset);

                Assert.IsTrue(memory.TestEquals(emitWriteReadOperand, emitReadReadOperand));
            }
        }
Пример #4
0
        public void JumpLiteralTest()
        {
            UInt64 offset;

            // Generate Code
            memory.Reset();

            offset             = 0;
            offset             = ByteCode.EmitJumpF(byteCode, offset, new Op(Operands.LiteralInfoMask, 0, 0, 0, 0, 0, 0, 0, 0));
            offset             = ByteCode.EmitJumpF(byteCode, offset, new Op(Operands.LiteralInfoMask, 0, 8));
            offset             = ByteCode.EmitJumpF(byteCode, offset, new Op(Operands.LiteralInfoMask, 0, 0, 0, 0, 0, 6));
            offset             = ByteCode.EmitJumpB(byteCode, offset, new Op(Operands.LiteralInfoMask, 0, 0, 0, 14));
            byteCode[offset++] = Instructions.Halt;

            // Run Code
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));
        }
        public void TestDecrement()
        {
            UInt64 offset;

            // Generate Code
            memory.Reset();
            memory.PushVar(0);

            offset             = 0;
            offset             = ByteCode.EmitDecrement(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 0, 0, 0, 0));
            offset             = ByteCode.EmitDecrement(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0));
            offset             = ByteCode.EmitDecrement(byteCode, offset, new MemoryOp(0));
            offset             = ByteCode.EmitDecrement(byteCode, offset, new MemoryOp(Operands.FrameOffsetInfoMask, 0, 0, 0, 0, 0, 0));
            byteCode[offset++] = Instructions.Halt;

            // Run Code
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));

            // Test Results
            Assert.IsTrue(memory.TestAddressEqualsValue(0, 0xFFFFFFFFFFFFFFFC));
        }
Пример #6
0
        public void JumpEQTest()
        {
            UInt64 offset;

            //
            // Generate Code
            //
            offset = 0;
            memory.Reset();
            memory.PushLiteral(0);
            memory.PushLiteral(5);
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(0));
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(1), new Op(Operands.LiteralInfoMask, 0), new Op(0));
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(0));
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(1), new Op(Operands.LiteralInfoMask, 0), new Op(0));
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(0));
            byteCode[offset++] = Instructions.Halt;

            //
            // Run Code
            //
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));


            //
            // Generate Code
            //
            Console.WriteLine("FBF");
            offset = 0;
            memory.Reset();
            memory.PushLiteral(0);
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(Operands.LiteralInfoMask, 6));
            offset             = ByteCode.EmitJumpFIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(Operands.LiteralInfoMask, 6));
            offset             = ByteCode.EmitJumpBIfEQ(byteCode, offset, new MemoryOp(0), new Op(Operands.LiteralInfoMask, 0), new Op(Operands.LiteralInfoMask, 12));
            byteCode[offset++] = Instructions.Halt;

            //
            // Run Code
            //
            Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));

            /*
             * //
             * // Generate Code
             * //
             * offset = 0;
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0, 0, 0, 0);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0, 0, 4);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0, 5);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0xFF, 0xFF, unchecked((Byte)(-9)));
             * byteCode[offset++] = ByteCode.EndFunction;
             *
             * //
             * // Run Code
             * //
             * Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));
             *
             * //
             * // Test Results
             * //
             *
             *
             * //
             * // Generate Code
             * //
             * offset = 0;
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0, 0, 0, 0);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0, 3);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 5);
             * offset = ByteCode.EmitJumpLiteral(byteCode, ref offset, 0xFF, 0xFF, unchecked((Byte)(-8)));
             * byteCode[offset++] = ByteCode.EndFunction;
             *
             * //
             * // Run Code
             * //
             * Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));
             *
             *
             *
             * //
             * // Generate Code
             * //
             * offset = 0;
             * stack[0] = new Variable(0x81);
             * stack[1] = new Variable(0x81);
             * stack[2] = new Variable(0x81);
             * stack[0].SetMemWithMask(0);
             * stack[1].SetMemWithMask(2);
             * stack[2].SetMemWithMask(unchecked((UInt64)(-4)));
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 0);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 1);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 1);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 2);
             * byteCode[offset++] = ByteCode.EndFunction;
             *
             * //
             * // Run Code
             * //
             * Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));
             *
             * //
             * // Test Results
             * //
             *
             * //
             * // Generate Code
             * //
             * offset = 0;
             * stack[0] = new Variable(0x84);
             * stack[1] = new Variable(0x83);
             * stack[2] = new Variable(0x82);
             * stack[0].SetMemWithMask(0);
             * stack[1].SetMemWithMask(2);
             * stack[2].SetMemWithMask(unchecked((UInt64)(-4)));
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 0);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 1);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 1);
             * offset = ByteCode.EmitJumpVar(byteCode, ref offset, 2);
             * byteCode[offset++] = ByteCode.EndFunction;
             *
             * //
             * // Run Code
             * //
             * Assert.AreEqual(offset, InstructionProcessor.Execute(byteCode, 0, memory));
             *
             * //
             * // Test Results
             * //
             */
        }