|
| 1 | +// This is a modified version of `VRC.Udon.Compiler.dll` |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using VRC.Udon.Common.Interfaces; |
| 6 | +using VRC.Udon.VM.Common; |
| 7 | + |
| 8 | +namespace Astrum |
| 9 | +{ |
| 10 | + partial class AstralUdonViewer |
| 11 | + { |
| 12 | + public static class Disassembler |
| 13 | + { |
| 14 | + public static System.Collections.IEnumerator DisassembleProgram(string path, IUdonProgram program) |
| 15 | + { |
| 16 | + string[] lines = new string[program.ByteCode.Length]; |
| 17 | + for (uint i = 0; i < program.ByteCode.Length;) |
| 18 | + { |
| 19 | + lines[i] = DisassembleInstruction(program, ref i); |
| 20 | + yield return null; |
| 21 | + } |
| 22 | + File.WriteAllLines(path, lines); |
| 23 | + } |
| 24 | + |
| 25 | + public static string DisassembleInstruction(IUdonProgram program, ref uint offset) |
| 26 | + { |
| 27 | + OpCode opCode = (OpCode)UIntFromBytes(program.ByteCode, offset); |
| 28 | + if (opCode == OpCode.NOP) |
| 29 | + return SimpleInstruction(ref offset, "NOP"); |
| 30 | + else if (opCode == OpCode.PUSH) |
| 31 | + return DirectInstruction(ref offset, "PUSH", program); |
| 32 | + else if (opCode == OpCode.POP) |
| 33 | + return SimpleInstruction(ref offset, "POP"); |
| 34 | + else if (opCode == OpCode.JUMP_IF_FALSE) |
| 35 | + return DirectInstruction(ref offset, "JUMP_IF_FALSE", program); |
| 36 | + else if (opCode == OpCode.JUMP) |
| 37 | + return DirectInstruction(ref offset, "JUMP", program); |
| 38 | + else if (opCode == OpCode.EXTERN) |
| 39 | + return ExternInstruction(ref offset, "EXTERN", program); |
| 40 | + else if (opCode == OpCode.ANNOTATION) |
| 41 | + return AnnotationInstruction(ref offset, "ANNOTATION", program); |
| 42 | + else if (opCode == OpCode.JUMP_INDIRECT) |
| 43 | + return JumpIndirectInstruction(ref offset, "JUMP_INDIRECT", program); |
| 44 | + else if (opCode == OpCode.COPY) |
| 45 | + return SimpleInstruction(ref offset, "COPY"); |
| 46 | + else return $"0x{(offset += 4) - 4:X}: INVALID (0x{opCode:X})"; |
| 47 | + } |
| 48 | + |
| 49 | + private static string SimpleInstruction(ref uint offset, string name) |
| 50 | + => string.Format("0x{0:X8}: {1}", (offset += 4) - 4, name); |
| 51 | + private static string DirectInstruction(ref uint offset, string name, IUdonProgram program) |
| 52 | + => string.Format("0x{0:X8}: {1}, 0x{2}", (offset += 8) - 8, name, Convert.ToString((long)(ulong)UIntFromBytes(program.ByteCode, offset - 4), 16).PadLeft(8, '0').ToUpper()); |
| 53 | + private static string AnnotationInstruction(ref uint offset, string name, IUdonProgram program) |
| 54 | + => ExternInstruction(ref offset, name, program); |
| 55 | + private static string ExternInstruction(ref uint offset, string name, IUdonProgram program) |
| 56 | + { |
| 57 | + string str = (offset + 4).ToString(); |
| 58 | + try { str = program.Heap.GetHeapVariable<string>(UIntFromBytes(program.ByteCode, offset + 4)); } |
| 59 | + catch { } |
| 60 | + return string.Format("0x{0:X8}: {1}, <{2}>", (offset += 8) - 8, name, str); |
| 61 | + } |
| 62 | + |
| 63 | + private static string JumpIndirectInstruction(ref uint offset, string name, IUdonProgram program) |
| 64 | + { |
| 65 | + uint addr = UIntFromBytes(program.ByteCode, (offset += 8) - 4); |
| 66 | + if (program.SymbolTable.HasSymbolForAddress(addr)) |
| 67 | + return string.Format("0x{0:X8}: {1}, {2}", offset - 8, name, program.SymbolTable.GetSymbolFromAddress(addr)); |
| 68 | + else return string.Format("0x{0:X8}: {1}, 0x{2}", offset - 8, name, Convert.ToString((long)(ulong)addr, 16).PadLeft(8, '0').ToUpper()); |
| 69 | + } |
| 70 | + |
| 71 | + private unsafe static uint UIntFromBytes(byte[] bytes, uint startIndex) |
| 72 | + => (uint)((bytes[(int)startIndex] << 24) + (bytes[(int)(startIndex + 1)] << 16) + (bytes[(int)(startIndex + 2)] << 8) + bytes[(int)(startIndex + 3)]); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments