Class: Flux::Disassembler
- Inherits:
-
Object
- Object
- Flux::Disassembler
- Includes:
- OpcodeRegistry
- Defined in:
- lib/superinstance/flux-runtime/disassembler.rb
Overview
Bytecode to text disassembly
Constant Summary collapse
- MNEMONIC_MAP =
OPCODE_CLASSES.transform_keys(&:to_i).freeze
Constants included from OpcodeRegistry
OpcodeRegistry::OPCODE_CLASSES, OpcodeRegistry::OPCODE_NAMES
Instance Method Summary collapse
Methods included from OpcodeRegistry
Instance Method Details
#disassemble(bytecode_string) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/superinstance/flux-runtime/disassembler.rb', line 12 def disassemble(bytecode_string) bytecode = bytecode_string.dup.force_encoding('BINARY') output = [] pc = 0 while pc < bytecode.bytesize offset = pc opcode = bytecode.getbyte(pc) pc += 1 mnemonic = OPCODE_NAMES[opcode] || "Unknown_0x#{opcode.to_s(16).upcase}" line = "#{mnemonic}" case opcode # Format A: 1 byte when 0x00, 0x01, 0x02, 0x08, 0x09, 0x0A # No operands # Format B: +Rd +Rs (2 bytes) when 0x10, 0x11, 0x12, 0x20, 0x40 rd = bytecode.getbyte(pc) rs = bytecode.getbyte(pc + 1) line = "#{mnemonic} R#{rd}, R#{rs}" pc += 2 when 0x13 # Swap: Ra, Rb ra = bytecode.getbyte(pc) rb = bytecode.getbyte(pc + 1) line = "#{mnemonic} R#{ra}, R#{rb}" pc += 2 # Format C: +Rd +Ra +Rb (3 bytes) when 0x21..0x37, 0x41..0x59, 0x60..0x63, 0x90..0x92, 0xA0..0xA5, 0xB2..0xB4 rd = bytecode.getbyte(pc) ra = bytecode.getbyte(pc + 1) rb = bytecode.getbyte(pc + 2) line = "#{mnemonic} R#{rd}, R#{ra}, R#{rb}" pc += 3 # Format D: +Rd +imm16 (3 bytes) when 0x28, 0x29, 0x79 rd = bytecode.getbyte(pc) imm_lo = bytecode.getbyte(pc + 1) imm_hi = bytecode.getbyte(pc + 2) imm = imm_lo | (imm_hi << 8) # Sign extend imm = (imm << 16) >> 16 line = "#{mnemonic} R#{rd}, #{imm}" pc += 3 # Format E: +Rd +Rb +off16 (4 bytes) when 0x70..0x78 rd = bytecode.getbyte(pc) rb = bytecode.getbyte(pc + 1) off_lo = bytecode.getbyte(pc + 2) off_hi = bytecode.getbyte(pc + 3) off = off_lo | (off_hi << 8) line = "#{mnemonic} R#{rd}, R#{rb}, #{off}" pc += 4 when 0xB0, 0xB1 # VLoad, VStore rd = bytecode.getbyte(pc) rb = bytecode.getbyte(pc + 1) off_lo = bytecode.getbyte(pc + 2) off_hi = bytecode.getbyte(pc + 3) off = off_lo | (off_hi << 8) line = "#{mnemonic} R#{rd}, R#{rb}, #{off}" pc += 4 # Format G: variable when 0x03, 0x04, 0x05 # Jump, JumpIf, JumpIfNot length = bytecode.getbyte(pc) pc += 1 off_lo = bytecode.getbyte(pc) off_hi = bytecode.getbyte(pc + 1) off = off_lo | (off_hi << 8) off = (off << 16) >> 16 # Sign extend line = "#{mnemonic} #{off}" pc += 2 when 0x06 # Call length = bytecode.getbyte(pc) pc += 1 func_lo = bytecode.getbyte(pc) func_hi = bytecode.getbyte(pc + 1) func_idx = func_lo | (func_hi << 8) line = "#{mnemonic} #{func_idx}" pc += 2 when 0x07 # CallIndirect length = bytecode.getbyte(pc) pc += 1 reg = bytecode.getbyte(pc) line = "#{mnemonic} R#{reg}" pc += 1 when 0x80 # ASend length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) reg = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, R#{reg}" pc += 2 when 0x81 # ARecv length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) reg = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, R#{reg}" pc += 2 when 0x82 # AAsk length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) reg = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, R#{reg}" pc += 2 when 0x83 # ATell length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) reg = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, R#{reg}" pc += 2 when 0x84 # ADelegate length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) bc_lo = bytecode.getbyte(pc + 1) bc_hi = bytecode.getbyte(pc + 2) bc_start = bc_lo | (bc_hi << 8) line = "#{mnemonic} #{agent_id}, #{bc_start}" pc += 3 when 0x85 # ABroadcast length = bytecode.getbyte(pc) pc += 1 reg = bytecode.getbyte(pc) line = "#{mnemonic} R#{reg}" pc += 1 when 0x86 # ASubscribe length = bytecode.getbyte(pc) pc += 1 channel_id = bytecode.getbyte(pc) line = "#{mnemonic} #{channel_id}" pc += 1 when 0x87 # AWait length = bytecode.getbyte(pc) pc += 1 cond_reg = bytecode.getbyte(pc) line = "#{mnemonic} R#{cond_reg}" pc += 1 when 0x88 # ATrust length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) level = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, #{level}" pc += 2 when 0x89 # AVerify length = bytecode.getbyte(pc) pc += 1 agent_id = bytecode.getbyte(pc) result_reg = bytecode.getbyte(pc + 1) line = "#{mnemonic} #{agent_id}, R#{result_reg}" pc += 2 else pc += 3 # Skip unknown bytes end output << " #{offset.to_s(16).rjust(8, '0')}: #{line}" end output.join("\n") end |