public static void Print(Compiler c) { var bs = c.Compile().bytecode; Compiler.Definition op = null; int op_size = 0; for (int i = 0; i < bs.Length; i++) { string str = string.Format("0x{0:x2}", bs[i]); if (op != null) { --op_size; if (op_size == 0) { op = null; } } else { op = Compiler.LookupOpcode((Opcodes)bs[i]); op_size = PredictOpcodeSize(op, bs, i); str += "(" + op.name.ToString() + ")"; if (op_size == 0) { op = null; } } Console.WriteLine(string.Format("{0:x2}", i) + " " + str); } Console.WriteLine("============"); }
static void Dump(byte[] bs) { string res = ""; Compiler.Definition op = null; int op_size = 0; for (int i = 0; i < bs?.Length; i++) { res += string.Format("{1:00} 0x{0:x2} {0}", bs[i], i); if (op != null) { --op_size; if (op_size == 0) { op = null; } } else { op = Compiler.LookupOpcode((Opcodes)bs[i]); op_size = PredictOpcodeSize(op, bs, i); res += "(" + op.name.ToString() + ")"; if (op_size == 0) { op = null; } } res += "\n"; } Console.WriteLine(res); }
public static int PredictOpcodeSize(Compiler.Definition op, byte[] bytes, int start_pos) { if (op.operand_width == null) { return(0); } int pos = start_pos; foreach (int ow in op.operand_width) { Bytecode.Decode(bytes, ow, ref pos); } return(pos - start_pos); }
static bool CompareCode(byte[] a, byte[] b, out string cmp) { Compiler.Definition aop = null; int aop_size = 0; Compiler.Definition bop = null; int bop_size = 0; bool equal = true; cmp = ""; var lens = new List <int>(); int max_len = 0; for (int i = 0; i < (a?.Length > b?.Length ? a?.Length : b?.Length); i++) { string astr = ""; if (i < a?.Length) { astr = string.Format("0x{0:x2} {0}", a[i]); if (aop != null) { --aop_size; if (aop_size == 0) { aop = null; } } else { aop = Compiler.LookupOpcode((Opcodes)a[i]); aop_size = PredictOpcodeSize(aop, a, i); astr += "(" + aop.name.ToString() + ")"; if (aop_size == 0) { aop = null; } } } string bstr = ""; if (i < b?.Length) { bstr = string.Format("0x{0:x2} {0}", b[i]); if (bop != null) { --bop_size; if (bop_size == 0) { bop = null; } } else { bop = Compiler.LookupOpcode((Opcodes)b[i]); bop_size = PredictOpcodeSize(bop, b, i); bstr += "(" + bop.name.ToString() + ")"; if (bop_size == 0) { bop = null; } } } lens.Add(astr.Length); if (astr.Length > max_len) { max_len = astr.Length; } cmp += string.Format("{0,2}", i) + " " + astr + "{fill" + lens.Count + "} | " + bstr; if (a?.Length <= i || b?.Length <= i || a[i] != b[i]) { equal = false; cmp += " <============== actual vs expected"; } cmp += "\n"; } for (int i = 1; i <= lens.Count; ++i) { cmp = cmp.Replace("{fill" + i + "}", new String(' ', max_len - lens[i - 1])); } return(equal); }