Beispiel #1
0
 public AsmILGenerator(bool isEntryPoint = false)
 {
     if (isEntryPoint)
     {
         OpCodes.Add(".entrypoint");
     }
 }
Beispiel #2
0
 public void EmitStloc(ILocalBuilder local)
 {
     if (local == null)
     {
         throw new ArgumentNullException(nameof(local));
     }
     OpCodes.Add($"stloc {local.LocalIndex}");
 }
Beispiel #3
0
        public void EmitTrue()
        {
            OpCodes.Add("ldc.i4.1");

            int @int = 42;

            GC.KeepAlive(@int);
        }
Beispiel #4
0
        public void MarkLabel(ILabel label)
        {
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }
            var idx = ((AsmLabel)label).Idx;

            OpCodes.Add($"Label_{idx}:");
        }
Beispiel #5
0
        public void EmitBrtrue(ILabel label)
        {
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }
            var idx = ((AsmLabel)label).Idx;

            OpCodes.Add($"brtrue Label_{idx}");
        }
        public void ShouldBeAbleToAddTwoRegistersTogether()
        {
            var source = new List <IOperation>
            {
                //OpCodes.Move(DataType.VectorOfByte, new VectorConstantOperand<byte>(Vector<byte>.One), new RegisterOperand(1)),

                //OpCodes.Move(DataType.VectorOfByte, new VectorConstantOperand<byte>(Vector<byte>.One), new RegisterOperand(2)),

                OpCodes.Add(DataType.VectorOfByte, new RegisterOperand(1), new RegisterOperand(2), new RegisterOperand(3))
            };

            var cpu = BuildCpu(source);

            RunProgram(cpu, source);

            cpu.Registers[3].ShouldBe(new Vector <byte>(2));
        }
Beispiel #7
0
 private void CreateOpCode(byte command, Action instruction, string name)
 {
     OpCodes.Add(command, new OpCode(instruction, name));
 }
Beispiel #8
0
 public void EmitRet()
 {
     OpCodes.Add("ret");
 }
Beispiel #9
0
 public void EmitStarg(short idx)
 {
     OpCodes.Add($"starg {idx}");
 }
Beispiel #10
0
 public void EmitLdthis()
 {
     OpCodes.Add("ldarg.0");
 }
Beispiel #11
0
 public void EmitMul()
 {
     OpCodes.Add("mul");
 }
Beispiel #12
0
 public void EmitCgt()
 {
     OpCodes.Add("cgt");
 }
Beispiel #13
0
 public void EmitLdstr(string value)
 {
     // TODO escaping is going to break this
     OpCodes.Add($"ldstr \"{value}\"");
 }
Beispiel #14
0
        /// <summary>
        ///     Decodes the script into op_codes and data sections
        /// </summary>
        private void Decode()
        {
            // Iterate over each byte in the script.
            // When a data chunk (non-op_code section) is encountered,
            //   i will be incremented so as to skip reading op_codes from the data
            for (var i = 0; i < ScriptBytes.Length; i++)
            {
                // OP_PUSH - indicates a data section of ScriptBytes[i] length
                if (ScriptBytes[i] > 1 && ScriptBytes[i] < 76)
                {
                    // save the data chunk and add an OP_DATA code to the script
                    var dataLength = ScriptBytes[i];
                    DataChunks.Add(ScriptBytes.Skip(i + 1).Take(dataLength).ToArray());
                    OpCodes.Add(OpCodeType.OP_DATA);
                    // increment i to skip over the data section during further op_code processing
                    i += dataLength;
                }
                else
                {
                    switch (ScriptBytes[i])
                    {
                    // OP_PUSHDATA1 - indicates a data section of ScriptBytes[i+1] length
                    case (byte)OpCodeType.OP_PUSHDATA1:
                    {
                        // save the data chunk and an OP_DATA code to the script
                        var dataLength = ScriptBytes[i + 1];
                        DataChunks.Add(ScriptBytes.Skip(i + 2).Take(dataLength).ToArray());
                        OpCodes.Add(OpCodeType.OP_DATA);
                        // increment i to skip over the data section during further op_code processing
                        i += 1 + dataLength;
                        break;
                    }

                    // OP_PUSHDATA2 - indicates a data section with 2 bytes indicating length
                    case (byte)OpCodeType.OP_PUSHDATA2:
                    {
                        // get 2 byte count and data
                        var dataLength = BitConverter.ToInt16(ScriptBytes, i + 1);
                        DataChunks.Add(ScriptBytes.Skip(i + 3).Take(dataLength).ToArray());
                        OpCodes.Add(OpCodeType.OP_DATA);
                        i += 2 + dataLength;
                        break;
                    }

                    // OP_PUSHDATA2 - indicates a data section with 4 bytes indicating length
                    case (byte)OpCodeType.OP_PUSHDATA4:
                    {
                        // get 4 byte count and data
                        var dataLength = BitConverter.ToInt32(ScriptBytes, i + 1);
                        DataChunks.Add(ScriptBytes.Skip(i + 5).Take(dataLength).ToArray());
                        OpCodes.Add(OpCodeType.OP_DATA);
                        i += 4 + dataLength;
                        break;
                    }

                    // any other OP_CODE (non-data identifier)
                    default:
                    {
                        // check if this is a valid/known op code, and add it to the list
                        if (Enum.IsDefined(typeof(OpCodeType), ScriptBytes[i]) && ScriptBytes[i] != 218) // 218 = DA
                        {
                            OpCodes.Add((OpCodeType)ScriptBytes[i]);
                        }
                        else
                        {
                            // TODO: handle unknown OP_CODE... this shouldn't happen
                            return;
                        }
                        break;
                    }
                    }
                }
            }
        }
Beispiel #15
0
 public void EmitLdcI40()
 {
     OpCodes.Add("ldc.i4.0");
 }
Beispiel #16
0
 public void EmitFalse()
 {
     OpCodes.Add("Ldc.i4.0");
 }
Beispiel #17
0
 public void EmitAdd()
 {
     OpCodes.Add("add");
 }
Beispiel #18
0
 public void EmitDup()
 {
     OpCodes.Add("dup");
 }
Beispiel #19
0
 public void EmitDiv()
 {
     OpCodes.Add("div");
 }
Beispiel #20
0
 public void EmitClt()
 {
     OpCodes.Add("clt");
 }
Beispiel #21
0
 public void EmitSub()
 {
     OpCodes.Add("sub");
 }
Beispiel #22
0
 public void EmitLdnull()
 {
     OpCodes.Add("ldnull");
 }
Beispiel #23
0
 public void EmitLdarga(short idx)
 {
     OpCodes.Add($"ldarga {idx}");
 }
Beispiel #24
0
 public void EmitCeq()
 {
     OpCodes.Add("ceq");
 }
Beispiel #25
0
 public void EmitPop()
 {
     OpCodes.Add("pop");
 }
Beispiel #26
0
 public void EmitLdcI4(int value)
 {
     OpCodes.Add($"ldc.i4 {value}");
 }