backtrip 0.1.4

A decompiler in Rust — turns .NET assemblies (.dll/.exe) into C# and JVM class files (.class) into Java, with IL/javap-style disassembly and round-trip verification
Documentation
/// CIL operand kinds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpKind {
    None,
    SByte,   // ldc.i4.s, short branch
    Short,   // int16
    Int,     // int32
    Long,    // int64
    Float,   // r4
    Double,  // r8
    BrTarget,    // int32 relative
    ShortBrTarget, // int8 relative
    Switch,
    StringTok,
    FieldTok,
    MethodTok,
    TypeTok,
    Tok,     // any token
    SigTok,
    ShortVar, // uint8 index
    Var,      // uint16 index
}

#[derive(Debug, Clone, Copy)]
pub struct OpInfo {
    pub name: &'static str,
    pub kind: OpKind,
}

/// Lookup opcode info by the 1- or 2-byte opcode value.
/// Values sourced from dotnet/runtime opcode.def (ECMA-335 III).
pub fn lookup(op: u16) -> Option<OpInfo> {
    use OpKind::*;
    if op < 0xFE {
        let info = match op as u8 {
            0x00 => ("nop", None),
            0x01 => ("break", None),
            0x02 => ("ldarg.0", None),
            0x03 => ("ldarg.1", None),
            0x04 => ("ldarg.2", None),
            0x05 => ("ldarg.3", None),
            0x06 => ("ldloc.0", None),
            0x07 => ("ldloc.1", None),
            0x08 => ("ldloc.2", None),
            0x09 => ("ldloc.3", None),
            0x0A => ("stloc.0", None),
            0x0B => ("stloc.1", None),
            0x0C => ("stloc.2", None),
            0x0D => ("stloc.3", None),
            0x0E => ("ldarg.s", ShortVar),
            0x0F => ("ldarga.s", ShortVar),
            0x10 => ("starg.s", ShortVar),
            0x11 => ("ldloc.s", ShortVar),
            0x12 => ("ldloca.s", ShortVar),
            0x13 => ("stloc.s", ShortVar),
            0x14 => ("ldnull", None),
            0x15 => ("ldc.i4.m1", None),
            0x16 => ("ldc.i4.0", None),
            0x17 => ("ldc.i4.1", None),
            0x18 => ("ldc.i4.2", None),
            0x19 => ("ldc.i4.3", None),
            0x1A => ("ldc.i4.4", None),
            0x1B => ("ldc.i4.5", None),
            0x1C => ("ldc.i4.6", None),
            0x1D => ("ldc.i4.7", None),
            0x1E => ("ldc.i4.8", None),
            0x1F => ("ldc.i4.s", SByte),
            0x20 => ("ldc.i4", Int),
            0x21 => ("ldc.i8", Long),
            0x22 => ("ldc.r4", Float),
            0x23 => ("ldc.r8", Double),
            0x24 => ("unused", None),
            0x25 => ("dup", None),
            0x26 => ("pop", None),
            0x27 => ("jmp", MethodTok),
            0x28 => ("call", MethodTok),
            0x29 => ("calli", SigTok),
            0x2A => ("ret", None),
            0x2B => ("br.s", ShortBrTarget),
            0x2C => ("brfalse.s", ShortBrTarget),
            0x2D => ("brtrue.s", ShortBrTarget),
            0x2E => ("beq.s", ShortBrTarget),
            0x2F => ("bge.s", ShortBrTarget),
            0x30 => ("bgt.s", ShortBrTarget),
            0x31 => ("ble.s", ShortBrTarget),
            0x32 => ("blt.s", ShortBrTarget),
            0x33 => ("bne.un.s", ShortBrTarget),
            0x34 => ("bge.un.s", ShortBrTarget),
            0x35 => ("bgt.un.s", ShortBrTarget),
            0x36 => ("ble.un.s", ShortBrTarget),
            0x37 => ("blt.un.s", ShortBrTarget),
            0x38 => ("br", BrTarget),
            0x39 => ("brfalse", BrTarget),
            0x3A => ("brtrue", BrTarget),
            0x3B => ("beq", BrTarget),
            0x3C => ("bge", BrTarget),
            0x3D => ("bgt", BrTarget),
            0x3E => ("ble", BrTarget),
            0x3F => ("blt", BrTarget),
            0x40 => ("bne.un", BrTarget),
            0x41 => ("bge.un", BrTarget),
            0x42 => ("bgt.un", BrTarget),
            0x43 => ("ble.un", BrTarget),
            0x44 => ("blt.un", BrTarget),
            0x45 => ("switch", Switch),
            0x46 => ("ldind.i1", None),
            0x47 => ("ldind.u1", None),
            0x48 => ("ldind.i2", None),
            0x49 => ("ldind.u2", None),
            0x4A => ("ldind.i4", None),
            0x4B => ("ldind.u4", None),
            0x4C => ("ldind.i8", None),
            0x4D => ("ldind.i", None),
            0x4E => ("ldind.r4", None),
            0x4F => ("ldind.r8", None),
            0x50 => ("ldind.ref", None),
            0x51 => ("stind.ref", None),
            0x52 => ("stind.i1", None),
            0x53 => ("stind.i2", None),
            0x54 => ("stind.i4", None),
            0x55 => ("stind.i8", None),
            0x56 => ("stind.r4", None),
            0x57 => ("stind.r8", None),
            0x58 => ("add", None),
            0x59 => ("sub", None),
            0x5A => ("mul", None),
            0x5B => ("div", None),
            0x5C => ("div.un", None),
            0x5D => ("rem", None),
            0x5E => ("rem.un", None),
            0x5F => ("and", None),
            0x60 => ("or", None),
            0x61 => ("xor", None),
            0x62 => ("shl", None),
            0x63 => ("shr", None),
            0x64 => ("shr.un", None),
            0x65 => ("neg", None),
            0x66 => ("not", None),
            0x67 => ("conv.i1", None),
            0x68 => ("conv.i2", None),
            0x69 => ("conv.i4", None),
            0x6A => ("conv.i8", None),
            0x6B => ("conv.r4", None),
            0x6C => ("conv.r8", None),
            0x6D => ("conv.u4", None),
            0x6E => ("conv.u8", None),
            0x6F => ("callvirt", MethodTok),
            0x70 => ("cpobj", TypeTok),
            0x71 => ("ldobj", TypeTok),
            0x72 => ("ldstr", StringTok),
            0x73 => ("newobj", MethodTok),
            0x74 => ("castclass", TypeTok),
            0x75 => ("isinst", TypeTok),
            0x76 => ("conv.r.un", None),
            0x77 => ("unused", None),
            0x78 => ("unused", None),
            0x79 => ("unbox", TypeTok),
            0x7A => ("throw", None),
            0x7B => ("ldfld", FieldTok),
            0x7C => ("ldflda", FieldTok),
            0x7D => ("stfld", FieldTok),
            0x7E => ("ldsfld", FieldTok),
            0x7F => ("ldsflda", FieldTok),
            0x80 => ("stsfld", FieldTok),
            0x81 => ("stobj", TypeTok),
            0x82 => ("conv.ovf.i1.un", None),
            0x83 => ("conv.ovf.i2.un", None),
            0x84 => ("conv.ovf.i4.un", None),
            0x85 => ("conv.ovf.i8.un", None),
            0x86 => ("conv.ovf.u1.un", None),
            0x87 => ("conv.ovf.u2.un", None),
            0x88 => ("conv.ovf.u4.un", None),
            0x89 => ("conv.ovf.u8.un", None),
            0x8A => ("conv.ovf.i.un", None),
            0x8B => ("conv.ovf.u.un", None),
            0x8C => ("box", TypeTok),
            0x8D => ("newarr", TypeTok),
            0x8E => ("ldlen", None),
            0x8F => ("ldelema", TypeTok),
            0x90 => ("ldelem.i1", None),
            0x91 => ("ldelem.u1", None),
            0x92 => ("ldelem.i2", None),
            0x93 => ("ldelem.u2", None),
            0x94 => ("ldelem.i4", None),
            0x95 => ("ldelem.u4", None),
            0x96 => ("ldelem.i8", None),
            0x97 => ("ldelem.i", None),
            0x98 => ("ldelem.r4", None),
            0x99 => ("ldelem.r8", None),
            0x9A => ("ldelem.ref", None),
            0x9B => ("stelem.i", None),
            0x9C => ("stelem.i1", None),
            0x9D => ("stelem.i2", None),
            0x9E => ("stelem.i4", None),
            0x9F => ("stelem.i8", None),
            0xA0 => ("stelem.r4", None),
            0xA1 => ("stelem.r8", None),
            0xA2 => ("stelem.ref", None),
            0xA3 => ("ldelem", TypeTok),
            0xA4 => ("stelem", TypeTok),
            0xA5 => ("unbox.any", TypeTok),
            0xA6 => ("unused", None),
            0xA7 => ("unused", None),
            0xA8 => ("unused", None),
            0xA9 => ("unused", None),
            0xAA => ("unused", None),
            0xAB => ("unused", None),
            0xAC => ("unused", None),
            0xAD => ("unused", None),
            0xAE => ("unused", None),
            0xAF => ("unused", None),
            0xB0 => ("unused", None),
            0xB1 => ("unused", None),
            0xB2 => ("unused", None),
            0xB3 => ("conv.ovf.i1", None),
            0xB4 => ("conv.ovf.u1", None),
            0xB5 => ("conv.ovf.i2", None),
            0xB6 => ("conv.ovf.u2", None),
            0xB7 => ("conv.ovf.i4", None),
            0xB8 => ("conv.ovf.u4", None),
            0xB9 => ("conv.ovf.i8", None),
            0xBA => ("conv.ovf.u8", None),
            0xBB => ("unused", None),
            0xBC => ("unused", None),
            0xBD => ("unused", None),
            0xBE => ("unused", None),
            0xBF => ("unused", None),
            0xC0 => ("unused", None),
            0xC1 => ("unused", None),
            0xC2 => ("refanyval", TypeTok),
            0xC3 => ("ckfinite", None),
            0xC4 => ("unused", None),
            0xC5 => ("unused", None),
            0xC6 => ("mkrefany", TypeTok),
            0xC7 => ("unused", None),
            0xC8 => ("unused", None),
            0xC9 => ("unused", None),
            0xCA => ("unused", None),
            0xCB => ("unused", None),
            0xCC => ("unused", None),
            0xCD => ("unused", None),
            0xCE => ("unused", None),
            0xCF => ("unused", None),
            0xD0 => ("ldtoken", Tok),
            0xD1 => ("conv.u2", None),
            0xD2 => ("conv.u1", None),
            0xD3 => ("conv.i", None),
            0xD4 => ("conv.ovf.i", None),
            0xD5 => ("conv.ovf.u", None),
            0xD6 => ("add.ovf", None),
            0xD7 => ("add.ovf.un", None),
            0xD8 => ("mul.ovf", None),
            0xD9 => ("mul.ovf.un", None),
            0xDA => ("sub.ovf", None),
            0xDB => ("sub.ovf.un", None),
            0xDC => ("endfinally", None),
            0xDD => ("leave", BrTarget),
            0xDE => ("leave.s", ShortBrTarget),
            0xDF => ("stind.i", None),
            0xE0 => ("conv.u", None),
            0xE1 => ("unused", None),
            0xE2 => ("unused", None),
            0xE3 => ("unused", None),
            0xE4 => ("unused", None),
            0xE5 => ("unused", None),
            0xE6 => ("unused", None),
            0xE7 => ("unused", None),
            0xE8 => ("unused", None),
            0xE9 => ("unused", None),
            0xEA => ("unused", None),
            0xEB => ("unused", None),
            0xEC => ("unused", None),
            0xED => ("unused", None),
            0xEE => ("unused", None),
            0xEF => ("unused", None),
            0xF0 => ("unused", None),
            0xF1 => ("unused", None),
            0xF2 => ("unused", None),
            0xF3 => ("unused", None),
            0xF4 => ("unused", None),
            0xF5 => ("unused", None),
            0xF6 => ("unused", None),
            0xF7 => ("prefix7", None),
            0xF8 => ("prefix6", None),
            0xF9 => ("prefix5", None),
            0xFA => ("prefix4", None),
            0xFB => ("prefix3", None),
            0xFC => ("prefix2", None),
            0xFD => ("prefix1", None),
            0xFE => return Option::None, // handled by two-byte table
            0xFF => ("prefixref", None),
        };
        Some(OpInfo { name: info.0, kind: info.1 })
    } else {
        let lo = (op & 0xFF) as u8;
        let info = match lo {
            0x00 => ("arglist", None),
            0x01 => ("ceq", None),
            0x02 => ("cgt", None),
            0x03 => ("cgt.un", None),
            0x04 => ("clt", None),
            0x05 => ("clt.un", None),
            0x06 => ("ldftn", MethodTok),
            0x07 => ("ldvirtftn", MethodTok),
            0x08 => ("unused", None),
            0x09 => ("ldarg", Var),
            0x0A => ("ldarga", Var),
            0x0B => ("starg", Var),
            0x0C => ("ldloc", Var),
            0x0D => ("ldloca", Var),
            0x0E => ("stloc", Var),
            0x0F => ("localloc", None),
            0x10 => ("unused", None),
            0x11 => ("endfilter", None),
            0x12 => ("unaligned.", SByte),
            0x13 => ("volatile.", None),
            0x14 => ("tail.", None),
            0x15 => ("initobj", TypeTok),
            0x16 => ("constrained.", TypeTok),
            0x17 => ("cpblk", None),
            0x18 => ("initblk", None),
            0x19 => ("unused", None),
            0x1A => ("rethrow", None),
            0x1B => ("unused", None),
            0x1C => ("sizeof", TypeTok),
            0x1D => ("refanytype", None),
            0x1E => ("readonly.", None),
            _ => return Option::None,
        };
        Some(OpInfo { name: info.0, kind: info.1 })
    }
}