public InstructionMemoryOpAndOpAndOp(String label, Byte instruction, MemoryOp memoryOp, Op op1, Op op2)
     : base(label, instruction, (Byte)(1 + memoryOp.byteLength + op1.byteLength + op2.byteLength))
 {
     this.memoryOp = memoryOp;
     this.op1      = op1;
     this.op2      = op2;
 }
        public void JE(String label)
        {
            Debug("MOV");
            if (!assemblyBuilder.inFunction)
            {
                throw ParseError("JE Instruction must be in a func");
            }

            OperandToken condOperand1 = tokenizer.NextOperand();

            Debug("COND1 : {0}", condOperand1);
            OperandToken condOperand2 = tokenizer.NextOperand();

            Debug("COND2 : {0}", condOperand2);
            OperandToken jumpOperand = tokenizer.NextOperand();

            Debug("JMP  : {0}", jumpOperand);

            MemoryOp condOp1 = condOperand1.GetMemoryOp(assemblyBuilder.currentFunction);
            Op       condOp2 = condOperand2.GetOp(assemblyBuilder.currentFunction);
            Op       jumpOp  = jumpOperand.GetJumpOp(assemblyBuilder.currentFunction);

            assemblyBuilder.currentFunction.AddInstruction(new InstructionMemoryOpAndOpAndOp(label, Instructions.Move,
                                                                                             condOp1, condOp2, jumpOp));
        }
Beispiel #3
0
 public static UInt64 EmitJumpBIfEQ(byte[] byteCode, UInt64 offset, MemoryOp cnd1, Op cnd2, Op jmp)
 {
     byteCode[offset++] = Instructions.JumpBIfEQ;
     offset             = cnd1.Emit(byteCode, offset);
     offset             = cnd2.Emit(byteCode, offset);
     return(jmp.Emit(byteCode, offset));
 }
Beispiel #4
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));
            }
        }
        /*
         * public void dotMain(String label)
         * {
         *  Debug(".main");
         *
         *  if (!assembly.inFunction) throw ParseError(".main command must be in a func");
         *  if (label != null) throw ParseError(".main can't be labeled");
         *
         *  assembly.CurrentFunctionIsMain();
         *  parseState = ParseState.Frame;
         * }
         *
         * public void dotException(String label)
         * {
         *  Debug(".exception");
         *
         *  if (!assembly.inFunction) throw ParseError(".exception command must be in a func");
         *  if(parseState != ParseState.StartOfFrame) throw ParseError(".exception must be at the start of a FRAME");
         *
         *  assembly.currentFunction.IncludeException();
         *  parseState = ParseState.Frame;
         * }
         * public void dotStack(String label)
         * {
         *  Debug(".stack");
         *  UInt64 num = tokenizer.GetUnsignedNumber();
         *
         *  if (!assembly.inFunction) throw ParseError(".stack command must be in a func");
         *
         *  if (label == null)
         *  {
         *      assembly.currentFunction.FrameAllocateVariable(num);
         *  }
         *  else
         *  {
         *      assembly.currentFunction.AddFrameOffsetLabel(label, num);
         *  }
         *  parseState = ParseState.Frame;
         * }
         * public void dotReturn(String label)
         * {
         *  Debug(".return");
         *  if (label != null) throw ParseError("A return statement can't have a Label?");
         *
         *  UInt64 size = tokenizer.GetUnsignedNumber();
         *
         *  if (!assembly.inFunction) throw ParseError(".return command must be in a func");
         *  assembly.currentFunction.SetReturnPointerSettings(false, (UInt32)size);
         * }
         * public void dotReturnTable(String label)
         * {
         *  Debug(".returntable");
         *  if (label != null) throw ParseError("A return statement can't have a Label?");
         *
         *  UInt64 size = tokenizer.GetUnsignedNumber();
         *
         *  if (!assembly.inFunction) throw ParseError(".return-table command must be in a func");
         *  assembly.currentFunction.SetReturnPointerSettings(true, (UInt32)size);
         * }
         *
         * public void func(String label)
         * {
         *  Debug("func");
         *  if (label != null) throw ParseError("A func command can't have a label");
         *
         *  assembly.NewFunction();
         *
         *
         *  parseState = ParseState.StartOfFrame;
         * }
         */

        public void MOV(String label)
        {
            Debug("MOV");
            if (!assemblyBuilder.inFunction)
            {
                throw ParseError("MOV Instruction must be in a func");
            }

            OperandToken dstOperand = tokenizer.NextOperand();

            Debug("DST : {0}", dstOperand);
            OperandToken srcOperand = tokenizer.NextOperand();

            Debug("SRC : {0}", srcOperand);

            MemoryOp dstOp = dstOperand.GetMemoryOp(assemblyBuilder.currentFunction);

            Op srcOp = srcOperand.GetOp(assemblyBuilder.currentFunction);

            assemblyBuilder.currentFunction.AddInstruction(new InstructionMemoryOpAndOp(label, Instructions.Move,
                                                                                        dstOp, srcOp));
        }
Beispiel #6
0
 public MemAndValue(MemoryOp ew, Op er)
 {
     this.dst = ew;
     this.src = er;
 }
Beispiel #7
0
 public MachineInstructionMemoryOp(Byte instruction, MemoryOp memoryOp)
     : base(instruction, memoryOp.byteLength)
 {
 }
Beispiel #8
0
 public static UInt64 EmitDecrement(byte[] byteCode, UInt64 offset, MemoryOp var)
 {
     byteCode[offset++] = Instructions.Decrement;
     return(var.Emit(byteCode, offset));
 }
Beispiel #9
0
 public static UInt64 EmitAssign(byte[] byteCode, UInt64 offset, MemoryOp dst, Op src)
 {
     byteCode[offset++] = Instructions.Move;
     offset             = dst.Emit(byteCode, offset);
     return(src.Emit(byteCode, offset));
 }
Beispiel #10
0
        public void Test()
        {
            Int32 off;

            byte[]     byteCode = new byte[10];
            MemoryOp[] emitW    = new MemoryOp[32];
            Op[]       emitR    = new Op[32];

            off          = 0;
            emitW[off++] = new MemoryOp(0);
            emitW[off++] = new MemoryOp(1);
            emitW[off++] = new MemoryOp(23);
            emitW[off++] = new MemoryOp(127);
            emitW[off++] = new MemoryOp(Operands.WritableOpTypeFrameOffsetShortMax);
            for (int i = 0; i < off; i++)
            {
                Assert.AreEqual(1U, emitW[i].Emit(byteCode, 0));
                Assert.AreEqual(emitW[i].infoMask, byteCode[0]);
            }

            off          = 0;
            emitR[off++] = new Op(0);
            emitR[off++] = new Op(1);
            emitR[off++] = new Op(23);
            emitR[off++] = new Op(127);
            emitR[off++] = new Op(Operands.ReadableOpTypeFrameOffsetShortMax);
            for (int i = 0; i < off; i++)
            {
                Assert.AreEqual(1U, emitW[i].Emit(byteCode, 0));
                Assert.AreEqual(emitW[i].infoMask, byteCode[0]);
            }

            off          = 0;
            emitW[off++] = new MemoryOp(Operands.AddressInfoMask, 248);
            emitW[off++] = new MemoryOp(Operands.AddressInfoMask, 88, 1);
            emitW[off++] = new MemoryOp(Operands.AddressInfoMask, 1, 2, 255);
            emitW[off++] = new MemoryOp(Operands.AddressInfoMask, 55, 23, 255, 8, 2, 1, 100, 98);
            emitW[off++] = new MemoryOp(Operands.AddressInfoMask, 32, 2, 255, 204);
            for (int i = 0; i < off; i++)
            {
                MemoryOp w = emitW[i];

                Assert.AreEqual((UInt64)(w.op.Length + 1), emitW[i].Emit(byteCode, 0));
                //Assert.AreEqual((Byte)(Operands.LongVariableMask | (l.longAddress.Length-1)), byteCode[0]);
                for (Byte j = 0; j < w.op.Length; j++)
                {
                    Assert.AreEqual(w.op[j], byteCode[j + 1]);
                }
            }

            /*
             * off = 0;
             * emitW[off++] = new EmitLiteral(34);
             * emitW[off++] = new EmitLiteral(88, 1);
             * emitW[off++] = new EmitLiteral(1, 2, 255);
             * emitW[off++] = new EmitLiteral(55, 23, 255, 8, 2, 1, 100, 98);
             * emitW[off++] = new EmitLiteral(32, 2, 255, 204);
             * for (int i = 0; i < off; i++)
             * {
             *  EmitLiteral lit = (EmitLiteral)emitW[i];
             *
             *  Assert.AreEqual((UInt64)(lit.literal.Length + 1), emitW[i].Emit(byteCode, 0));
             *  //Assert.AreEqual((Byte)(Operands.LiteralInfoMask | (lit.literal.Length - 1)), byteCode[0]);
             *  for (Byte j = 0; j < lit.literal.Length; j++)
             *  {
             *      Assert.AreEqual(lit.literal[j], byteCode[j + 1]);
             *  }
             * }
             */
        }