Why are `code_llvm` and `code_native` not displayed in color?

I love code_native, but I don’t like code_llvm very much. The reason is that the output of code_llvm is very hard to read.

I want the result like the image below. Are there any options or packages to make the printing colorful?

I found that I needed the script with ~150 lines and didn’t need any additional packages other than InteractiveUtils to show the screen above.

Edit:
I reported an issue Colored printing of `code_llvm` and `code_native` · Issue #36634 · JuliaLang/julia · GitHub

1 Like

Make a PR to Julia! Stacktraces in 1.5 or 1.6 (I forget) are colored and much better because someone wrote a little code to make them prettier. It was a really active pr with about 40 different visual tweeks until everyone was happy, but the result was great!

3 Likes

Here’s the issue which Oscar is referencing: https://github.com/JuliaLang/julia/issues/36026 . I didn’t think people would’ve wanted this in Base and maybe something like OhMyREPL.jl is better suited to have such features. Definitely open an issue and discuss it. I would love something like this.

EDIT: Here’s the PR https://github.com/JuliaLang/julia/pull/36134

4 Likes

FYI https://github.com/tkf/ColorfulCodeGen.jl has @ccode_llvm, @ccode_native, @cshow_sexpr, etc.

5 Likes

Thank you for your comments.
I reported an issue https://github.com/JuliaLang/julia/issues/36634.

I strongly recommend Cthulhu.jl.
It displays with color / syntax highlighting, and also lets you descend into Julia functions called by the one you’re looking at.

@noinline bar(x) = 3x
foo(x) = sqrt(3bar(x))
@descend foo(5)

Now I can press N for native code, L for llvm, w for code-typed, d to toggle debuginfo, etc.

Additionally, it shows a menu of functions to descend into:

 • %1  = invoke bar(::Int64)::Int64
   %6  = invoke throw_complex_domainerror(::Symbol,::Float64)::Union{}

You can select one and press enter to descend into it, and explore its code-typed/llvm/native, as well as any other functions it may be calling.

I’ve added it to my startup.jl, and use @descend a lot more often than any of @code_*s.
I’ve also set:

Cthulhu.CONFIG.asm_syntax = :intel

Which makes Intel the default syntax for asm. I prefer Intel syntax for 2 primary reasons:

  1. Addressing calculations are much clearer.
  2. Instructions assign/update the first argument, which is the same as Julia’s convention.
2 Likes

The heuristic highlighting algorithm I wrote works on line-by-line basis and doesn’t (cannot) deal with the global structure or backtracking. For this reason, we have an option of creating a separate package which is independent of Cthulhu and using its low-level API in Cthulhu.

BTW, I also prefer the Intel syntax. :smile: