Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing raw machine code along with disassembled code #40220

Merged
merged 6 commits into from
May 27, 2021

Conversation

bicycle1885
Copy link
Member

@bicycle1885 bicycle1885 commented Mar 26, 2021

This pull request implements a new option to code_native to print raw machine code along with disassembled code. If the keyword argument rawcode is set to true (false by default), raw machine code in hexadecimal format is printed as a comment just before each disassembled instruction line. This kind of information may sometimes be useful when the user is interested in code size or alignment of a tight loop (see #39108 (comment), for example).

Here is an example. You can see L16 is aligned with a nop instruction.

julia> function collatz(n)
           s = 0
           while n != 1
               if iseven(n)
                   n = n ÷ 2
               else
                   n = 3n + 1
               end
               s += 1
           end
           return s
       end
collatz (generic function with 1 method)

julia> code_native(collatz, (Int,), syntax = :intel, binary = true)
        .text
; code origin: 00007f4736da69a0, code size: 64
; ┌ @ REPL[1]:1 within `collatz'
; 69a0: 31 c0
        xor     eax, eax
; │ @ REPL[1]:3 within `collatz'
; │┌ @ operators.jl:264 within `!='
; ││┌ @ promotion.jl:409 within `=='
; 69a2: 48 83 ff 01
        cmp     rdi, 1
; │└└
; 69a6: 75 16
        jne     L30
; │ @ REPL[1]:11 within `collatz'
L8:
; 69a8: c3
        ret
; 69a9: 0f 1f 80 00 00 00 00
        nop     dword ptr [rax]
; │ @ REPL[1]:7 within `collatz'
; │┌ @ int.jl:87 within `+'
L16:
; 69b0: 48 8d 7c 7f 01
        lea     rdi, [rdi + 2*rdi + 1]
; │└
; │ @ REPL[1]:9 within `collatz'
; │┌ @ int.jl:87 within `+'
L21:
; 69b5: 48 ff c0
        inc     rax
; │└
; │ @ REPL[1]:3 within `collatz'
; │┌ @ operators.jl:264 within `!='
; ││┌ @ promotion.jl:409 within `=='
; 69b8: 48 83 ff 01
        cmp     rdi, 1
; │└└
; 69bc: 74 ea
        je      L8
; │ @ REPL[1]:4 within `collatz'
L30:
; 69be: 40 f6 c7 01
        test    dil, 1
; 69c2: 75 ec
        jne     L16
; │ @ REPL[1]:5 within `collatz'
; │┌ @ int.jl:263 within `div'
; 69c4: 48 89 f9
        mov     rcx, rdi
; 69c7: 48 c1 e9 3f
        shr     rcx, 63
; 69cb: 48 01 f9
        add     rcx, rdi
; 69ce: 48 d1 f9
        sar     rcx
; 69d1: 48 89 cf
        mov     rdi, rcx
; │└
; 69d4: eb df
        jmp     L21
; 69d6: 66 2e 0f 1f 84 00 00 00 00 00
        nop     word ptr cs:[rax + rax]
; └

Closes #39108.

@yuyichao
Copy link
Contributor

Note that for AArch32 and AArch64 (and possibly other ISA with fixed size instructions, not sure about Thumb....) at least gdb and objdump uses the convention of printing out the instruction in 32bit hex instead of bytes. It'll be nice if this can be consistent with that.

@bicycle1885
Copy link
Member Author

Thank you. I've updated the printer to follow the convention but I cannot test it because I have no access to ARM machines.

@StefanKarpinski StefanKarpinski added the status:triage This should be discussed on a triage call label May 14, 2021
llvm::write_hex(Stream, Address & 0xffff, HexPrintStyle::Lower, 4);
Stream << ":";
auto Arch = Triple.getArch();
bool FixedLength = !(Arch == Triple::x86 || Arch == Triple::x86_64);
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash vtjnash merged commit 97d5f6a into JuliaLang:master May 27, 2021
@oscardssmith oscardssmith removed the status:triage This should be discussed on a triage call label May 27, 2021
@bicycle1885 bicycle1885 deleted the disasm-rawcode branch May 27, 2021 23:33
shirodkara pushed a commit to shirodkara/julia that referenced this pull request Jun 9, 2021
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
aviatesk added a commit to JuliaDebug/Cthulhu.jl that referenced this pull request Jul 23, 2021
This commit corresponds to JuliaLang/julia#40220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement Request: Show instruction size in code_native output.
5 participants