public AsmILGenerator(bool isEntryPoint = false) { if (isEntryPoint) { OpCodes.Add(".entrypoint"); } }
public void EmitStloc(ILocalBuilder local) { if (local == null) { throw new ArgumentNullException(nameof(local)); } OpCodes.Add($"stloc {local.LocalIndex}"); }
public void EmitTrue() { OpCodes.Add("ldc.i4.1"); int @int = 42; GC.KeepAlive(@int); }
public void MarkLabel(ILabel label) { if (label == null) { throw new ArgumentNullException(nameof(label)); } var idx = ((AsmLabel)label).Idx; OpCodes.Add($"Label_{idx}:"); }
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)); }
private void CreateOpCode(byte command, Action instruction, string name) { OpCodes.Add(command, new OpCode(instruction, name)); }
public void EmitRet() { OpCodes.Add("ret"); }
public void EmitStarg(short idx) { OpCodes.Add($"starg {idx}"); }
public void EmitLdthis() { OpCodes.Add("ldarg.0"); }
public void EmitMul() { OpCodes.Add("mul"); }
public void EmitCgt() { OpCodes.Add("cgt"); }
public void EmitLdstr(string value) { // TODO escaping is going to break this OpCodes.Add($"ldstr \"{value}\""); }
/// <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; } } } } }
public void EmitLdcI40() { OpCodes.Add("ldc.i4.0"); }
public void EmitFalse() { OpCodes.Add("Ldc.i4.0"); }
public void EmitAdd() { OpCodes.Add("add"); }
public void EmitDup() { OpCodes.Add("dup"); }
public void EmitDiv() { OpCodes.Add("div"); }
public void EmitClt() { OpCodes.Add("clt"); }
public void EmitSub() { OpCodes.Add("sub"); }
public void EmitLdnull() { OpCodes.Add("ldnull"); }
public void EmitLdarga(short idx) { OpCodes.Add($"ldarga {idx}"); }
public void EmitCeq() { OpCodes.Add("ceq"); }
public void EmitPop() { OpCodes.Add("pop"); }
public void EmitLdcI4(int value) { OpCodes.Add($"ldc.i4 {value}"); }