Repository navigation
Description
I propose to refactor the framework of the arm64 assembler.
Why ?
1, The current framework is a bit complicated, not easy to understand, maintain and extend. Especially the handling of constants and the design of optab.
2, Adding a new arm64 instruction is taking more and more effort. For some complex instructions with many formats, a lot of modifications are needed. For example, https://go-review.googlesource.com/c/go/+/273668, https://go-review.googlesource.com/c/go/+/233277, etc.
3, At the moment, we are still missing ~1000 assembly instructions, including NEON and SVE. The potential cost for adding those instructions are high.
People is paying more and more attention to arm64 platform, and there are more and more requests to add new instructions, see #40725, #42326, #41092 etc. Arm64 also has many new features, such as SVE. We hope to construct a better framework to solve the above problems and make future work easier.
Goals for the changes
1, More readable and easy to maintain.
2, Easy to add new instructions.
3, Friendly to testing, Can be cross checked with GNU tools.
4, Share instruction definition with disassembly to avoid mismatch between assembler and disassembler.
How to refactor ?
First let's take a look of the current framework.

We mainly focus on the span7 function which encodes a function's Prog list. The main idea of this function is that for a specific Prog, first find its corresponding item in optab (by oplook function), and then encode it according to optab._type (in asmout function).
In oplook, we need to handle the matching relationship between a Prog and an optab item, which is quite complex especially those constant types and constant offset types. In optab, each format of an instruction has an entry. In theory, we need to write an encoding function for each entry, fortunately we can reuse some similar cases. However sometimes we don't know whether there is a similar implementation, and as instructions increase, the code becomes more and more complex and difficult to maintain.
We propose to change this logic to: separate the preprocessing and encoding of a Prog. The specific method is to first unfold the Prog into a Prog corresponding to only one machine instruction (by hardcode), and then encode it according to the known bits and argument types. Namely: encoding_of_P = Known_bits | arg1 | arg2 | ... | argn
The control flow of span7 becomes:

We basically have a complete argument type description list and arm64 instruction table, see argument types and instruction table When we know the known bits and parameter types of an instruction, it is easy to encode it.
With this change, we don't need to handle the matching relationship between the Prog and the item of optab any more, and we won't encode a specific instruction but the instruction argument type. The number of the instruction argument type is much less than the instruction number, so theoretically the reusability will increase and complexity will decrease. In the future to add new instructions we only need to do this:
1, Set an index to the goal instruction in the arm64 instruction table.
2, Unfold a Prog to one or multiple arm64 instructions.
3, Encode the parameters of the arm64 instruction if they have not been implemented.
I have a prototype patch for this proposal, including the complete framework and support for a few instructions such as ADD, SUB, MOV, LDR and STR. See https://go-review.googlesource.com/c/go/+/297776. The patch is incomplete, more work is required to make it work. If the proposal is accepted, we are committed to taking charge of the work.
TODO list:
1, Enable more instructions.
2, Add more tests.
3, Fix the assembly printing issue.
4, Cross check with GNU tools.
Activity
randall77 commentedon Mar 2, 2021
Keep in mind that the "Why?" section applies to pretty much all of the architectures. To the extent that we can design a system that works across architectures (sharing code, or even just sharing high-level design & testing), that would be great.
See also CL 275454 for the testing piece.
See also my comment in CL 283533.
[-]Proposal: refactor the framework of the arm64 assembler[/-][+]cmd/asm: refactor the framework of the arm64 assembler[/+]erifan commentedon Mar 3, 2021
I agree, but designing a general framework requires knowledge of multiple architectures. If Google can do this design, it would be best. We are happy to follow and complete the arm64 part.
Yes, I know this patch, Junchen is my colleague. This patch takes the mutual test of assembler and disassembler one step forward, and we also hope to use the gnu toolchain to test the Go assembler.
It would be great if we could merge the golang/arch repo into the golang/go repo, so that the disassembler and assembler could share a lot of code.
This table is best to be as simple as possible, preferably decoupled from the
Progstruct, and only contains the architecture-related instruction set.Refer to Openjdk and GCC, if we divide the assembly process into two stages, macro assembly and assembly. The assembly is only responsible for encoding an architecture-related instruction, and the macro assembly is responsible for the preprocessing of the Prog before encoding. Then we only need to use this table for encoding and decoding.
I hope to create a new branch to complete the refactoring of the arm64 assembler if this is acceptable.
Thanks for your comment.
erifan commentedon Mar 15, 2021
Hi, are there any further comments or suggestions?
If you think the above scheme is feasible, I am willing to extend it to all architectures. I can complete this architecture-independent framework and the arm64 part, and other architectures need to be completed by others. Of course, we will retain the old code before completing the porting of an architecture and set a switch for the new and old code paths.
erifan commentedon Mar 17, 2021
We have tried to add initial SVE support to the assembler under the existing framework, see CL 153358, but in order to get rid of the many existing cases, we had to add an
asmoutsvefunction, it looks just like a copy ofasmout. But it is foreseeable that with the increase of SVE instructions in the future, the same problem will appear again. Go may not currently support SVE instructions, but Go will always support new architecture instructions, maybe AVX512 instructions, or other new architecture instructions. But as long as this problem still exists, it will only become more and more difficult to deal with as time goes on. To be honest, we hope that upstream can give us a clear reply, so that we will feel easier to make the plan for next quarter. Can I apply for Upstream to discuss this issue at the next compiler & runtime meeting? Thank you.cherrymui commentedon Mar 17, 2021
Thanks for the proposal.
I think this is subjective. That said, I agree that the current optab approach is probably not the best fit for ARM64. I'm not the original author of the ARM64 assembler, but I'm pretty sure the design comes from the assembler of other RISC architectures, which comes from the Plan 9 assemblers. In my perspective the original design is actually quite simple, if the encoding of the machine instruction is simple. Think of MIPS or RISC-V, there are just a small number of encodings (that's where
oprrr,opirr, etc. come from); offsets are pretty much fixed width and encoded at the same bits. However, this is not the case of ARM64. Being RISC it started simple but more and more things get added. Offsets/immediates have so many ranges/alignment requirements that differs from instruction to instruction, not to mention the BITCON encoding. Vector instruction is much worse -- I would say there is no regularity (sorry about being subjective). That said, the encoding is quite bit-efficient for a fixed-width instruction set.Given that the ARM64 instruction encoding is not like what the optab approach is best suited, I think it is reasonable to come up a different approach. I'm willing to give this a try.
This sounds reasonable to me. It might simplify a bunch of things. It may also make it possible to machine-generate the encoding table that converts individual machine instruction to bytes.
But keep in mind that there are things handled in the Prog level, e.g. unsafe point and restartable sequence marking. Doing it at Prog level is simple because some Prog is restartable while its underlying individual instructions are not. I wonder how this is handled.
======
cc @laboger @pmur
I think the PPC64 assembler is also undergoing a refactor, and there may be similar issues. It would be nice if ARM64 and PPC64 contributors could work together on a common/similar design, so it is easier to maintain cross-architecture-wise.
Currently, in my perspective there are essentially 4 kinds of assembler backends in Go: x86, ARM32/ARM64/MIPS/PPC64/S390X which are somewhat similar, RISC-V which is a bit different, and WebAssembly. As someone maintaining or co-maintaining those backends, I'd really appreciate if we can keep this number small. Thanks.
pmur commentedon Mar 17, 2021
I have not had much time to digest this yet. Speaking for my PPC64 work, I have no existing plans to rewrite our assembler. Adding ISA 3.1 support (particularly for 64b instructions) requires some extra work, but not so much to make it look much different than it does today. A couple thoughts below on the current PPC64 asm deficiencies:
A framework to decompose go opcodes which don't map 1-1 with a machine instruction would likely simplify laying out code. Similarly, we need to fixup large jumps from conditional operations and respect alignment restrictions with 64b ISA 3.1 instructions.
Likewise, something to more reliably synchronize the supported assembler opcodes against pp64.csv (residing with the disassembler) would be desirable to avoid the churn of adding new instruction support as needed.
erifan commentedon Mar 18, 2021
This is easy to fix, we just need to mark the first instruction of the sequence as restartable. For example:
op $movcon, [R], R -> mov $movcon, REGTMP + op REGTMP, [R], RWe'll mark the first instruction
mov $movcon, REGTMPas restartable (although it uses REGTMP) so this instruction can be preempted, and leave the second instructionop REGTMP, [R], Ras non-restartable because it uses REGTMP so that it won't be preempted. Anyway we can fix this problem by making a mark when unfolding.Another problem is the printing of assembly instructions. Currently we print Go syntax assembly listing, namely unfolded "Prog"s, after unfolding, we can no longer print the previous
Progform. And we can't print before unfolding, because at that time we haven't got thePcvalue of eachProg. Of course, if we feel that the new print format is also reasonable, then this is not a problem.erifan commentedon Mar 18, 2021
The situation of different architectures seems to be different, but arm64 has many new features and instructions that are moving from design to real device, so I can see how much work is needed to add and maintain new instructions. so can we just do arm64 first? In the future, other architectures can decide whether to adopt the arm64 solution according to their own situation. I believe that even if Google designs a general assembly framework later, our work will not be wasted.
64 remaining items
gopherbot commentedon Aug 1, 2024
Change https://go.dev/cl/602355 mentions this issue:
cmd/internal/obj/arm64: Support ARM64 Z and P type SVE registersgopherbot commentedon Aug 16, 2024
Change https://go.dev/cl/606195 mentions this issue:
cmd/internal/obj/arm64: Implement ARM64 SVE assemblercmd/internal/obj/arm64: Add helpers for span7 passes
gopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/630523 mentions this issue:
cmd/internal/obj/arm64: Implement SVE instruction encodingsgopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/613455 mentions this issue:
cmd/internal/obj: Add TYPE_REGINDEX AddrType for indexed vector registersgopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/630520 mentions this issue:
cmd/internal/obj/arm64: Add SVE instruction mnemonicsgopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/630521 mentions this issue:
cmd/internal/obj/arm64: Implement ARM64 SVE assemblergopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/613456 mentions this issue:
cmd/internal/obj: Remove register range constraints from shared backend codegopherbot commentedon Nov 21, 2024
Change https://go.dev/cl/630522 mentions this issue:
cmd/internal/obj/arm64: Add encoding rules for SVE assemblermmcloughlin commentedon Dec 23, 2024
Exciting to see movement on this project!
Would it be possible to get a high-level review of the status and current plans here? I see a lot of CLs in various states.
CLs from mid-late 2023:
Also @erifan's XML ISA manual parser for code-generating instruction tables.
Recent CLs from @snickolls-arm:
Which of these CLs are active and likely to land in some form?
My primary interest is adding ARM support to
avo. As we talked about earlier in the thread, it would be really helpful for Go and third-party tools (such asavo) to have the assembler/disassembler derived from a source-of-truth instruction database and code generator. Is that still the plan?Thanks for all the work on this.
snickolls-arm commentedon Feb 6, 2025
The patches you see from me represent our contributions to address this issue. The other patches are deprecated. Our changes focus on developing a new assembler based on static data similar to the Optab and implementing instructions from SVE version 1 into this assembler.
We stopped our effort and are open for anyone interested to take over the issue. Feel free to reach out with any questions regarding these patches.
gopherbot commentedon Jan 30, 2026
Change https://go.dev/cl/740760 mentions this issue:
arm64: add arm64 XML ISA manual parsergopherbot commentedon Feb 5, 2026
Change https://go.dev/cl/742440 mentions this issue:
arm64: add arm64 XML ISA manual parsergopherbot commentedon Feb 5, 2026
Change https://go.dev/cl/742460 mentions this issue:
arm64: add arm64 XML ISA manual parserarm64: add arm64 ISA XML parser