Debian Codegraph @

arm64/instgen/xmlspec/parser_test.go

raw symbols SCIP
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 package xmlspec
6
7 import (
8 "flag"
9 "regexp"
10 "strings"
11 "testing"
12 )
13
14 var remoteData = flag.Bool("remote", false, "use remote data")
15
16 func getData(t *testing.T) []*InstructionParsed {
17 if *remoteData {
18 xmlDir, err := GetArm64XMLSpec(t.TempDir(), ExpectedURL, ExpectedVersion)
19 if err != nil {
20 t.Fatalf("GetArm64XMLSpec failed: %v", err)
21 }
22 return ParseXMLFiles(xmlDir)
23 }
24 return ParseXMLFiles("testdata")
25 }
26
27 func TestParseXMLFiles(t *testing.T) {
28 insts := getData(t)
29 // Merely check the parser runs.
30 t.Logf("Number of instructions: %d\n", len(insts))
31 }
32
33 func TestProcessXMLFiles(t *testing.T) {
34 insts := getData(t)
35 ProcessXMLFiles(insts)
36 specialInsts := map[string]bool{
37 "HISTSEG -- A64": true,
38 "REVB, REVH, REVW -- A64": true,
39 "SXTB, SXTH, SXTW -- A64": true,
40 "URECPE -- A64": true,
41 "URSQRTE -- A64": true,
42 "UXTB, UXTH, UXTW -- A64": true,
43 }
44 outer:
45 for _, inst := range insts {
46 if inst == nil {
47 continue
48 }
49 if specialInsts[inst.Title] {
50 // These instructions's size is not encoded in the assembler symbols,
51 // but specified in the mnemonic and decoding ASL.
52 // The parser should have special logic to handle them, skip them for now here.
53 continue
54 }
55 isAlias := false
56 for _, doc := range inst.DocVars {
57 if doc.Key == "alias_mnemonic" {
58 isAlias = true
59 break
60 }
61 }
62 if isAlias {
63 // Alias instructions are not fully specified in their own XML files,
64 // skip them.
65 continue
66 }
67 // Check RegDiagram and Encodings all parsed
68 for _, iclass := range inst.Classes.Iclass {
69 if !iclass.RegDiagram.Parsed {
70 continue
71 }
72 for _, encoding := range iclass.Encodings {
73 if !encoding.Parsed {
74 continue outer
75 }
76 }
77 // Check for all encodings, every named box should be encoded
78 // by some encoding elements
79 encodedBoxes := make(map[string]bool)
80 for _, encoding := range iclass.Encodings {
81 if encoding.Alias {
82 // Alias encodings are not fully specified in their own section,
83 // skip them.
84 continue
85 }
86 for _, operand := range encoding.Operands {
87 for _, elem := range operand.Elems {
88 encodedIn := elem.encodedIn
89 encodedBoxes[encodedIn] = true
90 if strings.Contains(encodedIn, ":") {
91 // Some weird instructions like smlalb_z_zzzi
92 // encodes an immediate in 2 or 3 disjoin fields
93 // appended together. We need to record all of them.
94 // They are represented as "(name1 :: name2)" or
95 // "(name1 :: name2 :: name3)".
96 // We need to extract these names.
97 re := regexp.MustCompile(`\((.*?) :: (.*?)( :: (.*?))?\)`)
98 matches := re.FindStringSubmatch(encodedIn)
99 if len(matches) == 3 {
100 encodedBoxes[matches[1]] = true
101 encodedBoxes[matches[2]] = true
102 } else if len(matches) == 5 {
103 encodedBoxes[matches[1]] = true
104 encodedBoxes[matches[2]] = true
105 encodedBoxes[matches[4]] = true
106 }
107 }
108 if strings.Contains(encodedIn, "[") {
109 // Some weird instructions like histcnt_z_p_zz
110 // encodes only part of a field:
111 // size[0] (meanwhile size is of size 2).
112 // We need to record the full field name.
113 re := regexp.MustCompile(`(.*)\[.*?\]`)
114 matches := re.FindStringSubmatch(encodedIn)
115 if len(matches) == 2 {
116 encodedBoxes[matches[1]] = true
117 }
118 }
119 }
120 }
121 }
122 for name := range iclass.RegDiagram.varBin {
123 if !encodedBoxes[name] {
124 t.Errorf("Box %s not encoded in %s", name, inst.file)
125 }
126 }
127 }
128 }
129 }