# Possible to get structured assembly code?

**URL:** <https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931>\
**Category:** Internals & Design\
**Created:** [January 25, 2021, 8:49pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931 "2021-01-25T20:49:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![1m1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1m1/32/21365_2.png) [@1m1](https://discourse.julialang.org/u/1m1)\
**Post date:** [January 25, 2021, 8:49pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/1 "2021-01-25T20:49:18Z")

</div>

I have seen `code_native` and `code_typed` and `code_llvm`.

These seem to only provide an unstructured text. Though the text itself appears structured and could be parsed to create a structure, this is tedious.

Should the internals not have this information available in a structured manner?

`code_lowered` gives structured information, but not at assembly level.

_May I please present what I am actually trying to achieve, and perhaps someone can point to an idea:_

I would like to translate julia code into another computer language, one that is very basic and operates on a stack, with registers, like an assembly machine.

I am hoping that I can write a translation from the julia assembly code to this language and that this would be easiest way to get correct translations.

Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 25, 2021, 10:29pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/2 "2021-01-25T22:29:54Z")

</div>

I assume by unstructured you mean that it’s presented as text. This is simply because there’s nothing julia specific about them so there is nothing in julia to represent them. They are simply being handled by other library.

There are of course binary representation for both but not necessary **structured** representation. For LLVM code, there are ways to get pointer to the LLVM functions/modules (I believe you can just look at the implementation of `code_llvm`). For assembly code, there’s no builtin structured and just binary code.

---

<div class="post-metadata">

**Author:** ![1m1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1m1/32/21365_2.png) [@1m1](https://discourse.julialang.org/u/1m1)\
**Post date:** [January 26, 2021, 3:50pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/3 "2021-01-26T15:50:54Z")

</div>

I see, you are saying it’s an output of llvm.

Need to check out the code behind code\_native etc.

Might it be possible to get it structured from llvm?  
I guess If should ask there.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 26, 2021, 4:24pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/4 "2021-01-26T16:24:58Z")

</div>

As I said, you can get the pointer to llvm structure.

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [February 5, 2021, 8:20pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/5 "2021-02-05T20:20:41Z")

</div>

> [@yuyichao](#):
>
> there’s nothing julia specific about them so there is nothing in julia to represent them.

I would say that the first part is not quite true anymore. [GitHub - kimikage/ColoredLLCodes.jl: Color `code_llvm` and `code_native` printing](https://github.com/kimikage/ColoredLLCodes.jl) , which has been incorporated into the [stdlib](https://github.com/JuliaLang/julia/blob/7d34b0d93a56a2d9f511ae11d3f8c0208bebb389/stdlib/InteractiveUtils/src/codeview.jl), reconstructs some structure to stylize the output.

---

<div class="post-metadata">

**Author:** ![0x0f0f0f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0x0f0f0f/32/20806_2.png) [@0x0f0f0f](https://discourse.julialang.org/u/0x0f0f0f)\
**Post date:** [March 28, 2021, 10:09pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/6 "2021-03-28T22:09:26Z")

</div>

This would be really useful for doing rewriting on the SSA IR with Metatheory.jl. I’m interested.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [March 29, 2021, 12:10am UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/7 "2021-03-29T00:10:08Z")

</div>

Are you sure you want to do this at the assembly level (assembly is also not an SSA form)?

---

<div class="post-metadata">

**Author:** ![0x0f0f0f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0x0f0f0f/32/20806_2.png) [@0x0f0f0f](https://discourse.julialang.org/u/0x0f0f0f)\
**Post date:** [March 29, 2021, 9:58am UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/8 "2021-03-29T09:58:18Z")

</div>

I’d like to do this at the SSA IR level

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [March 29, 2021, 10:35am UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/9 "2021-03-29T10:35:58Z")

</div>

Tools for working with Julia’s untyped SSA IR already exist. You might want to check out [IRTools.jl](https://github.com/FluxML/IRTools.jl) or [Cassette.jl](https://github.com/JuliaLabs/Cassette.jl). Their documentation should help get you started.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [December 9, 2021, 11:29am UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/10 "2021-12-09T11:29:21Z")

</div>

Out of curiosity, could code emitted by `@code_native` be used as input into an assembler to generate an exe or dll? Like for  
[GNU Assembler Examples (lmu.edu)](https://cs.lmu.edu/~ray/notes/gasexamples/)  
[Creating Assembly Language DLL Modules for Windows - CodeProject](https://www.codeproject.com/Articles/1250804/Creating-Assembly-Language-DLL-Modules-for-Windows)  
[Writing DLL in Assembly Language for External Calling in Maple - Application Center (maplesoft.com)](https://jp.maplesoft.com/applications/view.aspx?SID=4491&view=html&L=J)

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [December 11, 2021, 10:33pm UTC](https://discourse.julialang.org/t/possible-to-get-structured-assembly-code/53931/11 "2021-12-11T22:33:28Z")

</div>

With dump\_module=true, it is now a proper asm file (in v1.8-dev)
