private String disassembly()

in src/main/java/org/adoptopenjdk/jitwatch/ui/code/AssemblyTextBuilder.java [156:221]


    private String disassembly(String line)
    {
        String result = line;
        Matcher m = PATTERN_HEXA_CODE_INSTRUCTION.matcher(line);
        if (m.find())
        {
            long address = Long.parseUnsignedLong(m.group(1).substring(2), 16);
            String codeStr = m.group(2);
            String comment = m.group(3);

            if (comment == null)
            {
                comment = "";
            }

            String[] byteGroups = codeStr.trim().split("\\s+|\\|");
            List<Byte> byteList = new ArrayList<>();

            for (String group : byteGroups)
            {
                String hex = group.toLowerCase().trim();
                for (int i = 0; i < hex.length(); i+=2)
                {
                    String byteHex = hex.substring(i, Math.min(i+2, hex.length()));
                    byteList.add((byte) Integer.parseInt(byteHex, 16));
                }
            }

            byte[] codeBytes = new byte[byteList.size()];
            for (int i = 0; i < byteList.size(); i++)
            {
                codeBytes[i] = byteList.get(i);
            }

            String osArch = System.getProperty("os.arch");

            int arch;
            int mode;

            if (osArch != null && (osArch.contains("aarch64") || osArch.contains("arm64"))) {
                arch = Capstone.CS_ARCH_ARM64;
                mode = Capstone.CS_MODE_ARM;
            } else {
                arch = Capstone.CS_ARCH_X86;
                mode = Capstone.CS_MODE_64;
            }

            try (Capstone cs = new Capstone(arch, mode))
            {
                Instruction[] insns = cs.disasm(codeBytes, address);
                if (insns != null && insns.length > 0)
                {
                    for (Instruction insn : insns)
                    {
                        result = String.format("0x%x:\t%s\t%s %s\n", insn.getAddress(), insn.getMnemonic(), insn.getOpStr(), comment);
                    }
                }
            }
            catch (Exception e)
            {
                logger.error("Dissembly failed.", e);
                result = line;
            }
        }
        return result;
    }