| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // instgen is used to generate the ARM64 instruction table for encoding and decoding. |
| 6 | // Usage: |
| 7 | // |
| 8 | // instgen [-i=inputDir] [-o=outputDir] [-url=url] [-version=version] |
| 9 | // |
| 10 | // Users can download the ARM64 XML instruction manual from |
| 11 | // https://developer.arm.com/downloads/-/exploration-tools, |
| 12 | // decompress it, and pass the directory containing the latest XML |
| 13 | // instruction files by "-i" to instgen. Example: |
| 14 | // |
| 15 | // curl -o foo.gz https://developer.arm.com/-/cdn-downloads/permalink/Exploration-Tools-A64-ISA/ISA_A64/ISA_A64_xml_A_profile-2025-12.tar.gz |
| 16 | // mkdir foo |
| 17 | // (cd foo; tar xfz ../foo.gz) |
| 18 | // ./instgen -i foo/ISA_A64_xml_A_profile-2025-12 |
| 19 | // |
| 20 | // If the user does not provide "-i" option, the program will try to do the above |
| 21 | // automatically, the temporary directory will be cleaned up after the program exits. |
| 22 | // |
| 23 | // The program parses and processes all the XML files, and generates four .go files: |
| 24 | // inst_gen.go, elem_gen.go, goops_gen.go and arm64ops_gen.go to the output directory. |
| 25 | // The output directory is assumed to be GOROOT, these four files will be generated to |
| 26 | // <outputDir>/src/cmd/internal/obj/arm64/. |
| 27 | // |
| 28 | // If -o option is not specified, no output files will be generated. |
| 29 | // |
| 30 | // Since the format of the ARM64 instruction specification document may update, |
| 31 | // this parser may not work for some versions of the XML document. |
| 32 | // Due to differences in documents between different versions, the generated instruction |
| 33 | // table may also be inconsistent, mainly elements. These differences need to be fixed |
| 34 | // when applying the output to the assembler and disassembler. |
| 35 | // |
| 36 | // The current implementation is based on version: |
| 37 | // https://developer.arm.com/-/cdn-downloads/permalink/Exploration-Tools-A64-ISA/ISA_A64/ISA_A64_xml_A_profile-2025-12.tar.gz |
| 38 | // And after decompressing the tarball, the latest version will be used, i.e. "ISA_A64_xml_A_profile-2025-12". |
| 39 | // Users can provide the url and version of the xml files by "-url" and "-version" options. |
| 40 | // But there is no guarantee that the logic of instgen will work for other versions. |
| 41 | package main |
| 42 | |
| 43 | import ( |
| 44 | "flag" |
| 45 | "log" |
| 46 | "os" |
| 47 | |
| 48 | "golang.org/x/arch/arm64/instgen/xmlspec" |
| 49 | ) |
| 50 | |
| 51 | var input = flag.String("i", "", "the input directory of the xml files, this is an optional argument") |
| 52 | var output = flag.String("o", "", "the output directory of the generated files, this is an optional argument") |
| 53 | var genE2E = flag.Bool("e2e", false, "generate end-to-end test data") |
| 54 | |
| 55 | var url = flag.String("url", xmlspec.ExpectedURL, "the url of the xml files") |
| 56 | var version = flag.String("version", xmlspec.ExpectedVersion, "the version of the xml files") |
| 57 | |
| 58 | func main() { |
| 59 | log.SetFlags(log.LstdFlags | log.Lshortfile) |
| 60 | flag.Parse() |
| 61 | |
| 62 | xmlDir := *input |
| 63 | if *input == "" { |
| 64 | tmpDir, err := os.MkdirTemp("", "instgen_xml_spec") |
| 65 | if err != nil { |
| 66 | // Failed to create a temporary directory, ask the user to provide the data then. |
| 67 | flag.Usage() |
| 68 | os.Exit(1) |
| 69 | } |
| 70 | defer os.RemoveAll(tmpDir) |
| 71 | |
| 72 | xmlDir, err = xmlspec.GetArm64XMLSpec(tmpDir, *url, *version) |
| 73 | if err != nil { |
| 74 | // Get XML data from remote failed, ask the user to provide the data. |
| 75 | flag.Usage() |
| 76 | return |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Parse each xml file to insts. |
| 81 | insts := xmlspec.ParseXMLFiles(xmlDir) |
| 82 | xmlspec.ProcessXMLFiles(insts) |
| 83 | if *output != "" { |
| 84 | Generate(insts, *output, *genE2E) |
| 85 | } |
| 86 | errCnt := 0 |
| 87 | for _, inst := range insts { |
| 88 | if inst == nil { |
| 89 | continue |
| 90 | } |
| 91 | if inst.ParseError != "" { |
| 92 | errCnt++ |
| 93 | log.Printf("error: %s", inst.ParseError) |
| 94 | } |
| 95 | } |
| 96 | log.Printf("len(insts) = %v, error count = %v\n", len(insts), errCnt) |
| 97 | } |