-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathparse.go
254 lines (218 loc) · 6.61 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package asm provides a simple parser for Go assembly files.
package asm
import (
"bufio"
"bytes"
"fmt"
"strings"
"unicode"
"golang.org/x/tools/gopls/internal/protocol"
)
// Kind describes the nature of an identifier in an assembly file.
type Kind uint8
const (
Invalid Kind = iota // reserved zero value; not used by Ident
Ref // arbitrary reference to symbol or control label
Text // definition of TEXT (function) symbol
Global // definition of GLOBL (var) symbol
Data // initialization of GLOBL (var) symbol; effectively a reference
Label // definition of control label
)
func (k Kind) String() string {
if int(k) < len(kindString) {
return kindString[k]
}
return fmt.Sprintf("Kind(%d)", k)
}
var kindString = [...]string{
Invalid: "invalid",
Ref: "ref",
Text: "text",
Global: "global",
Data: "data",
Label: "label",
}
// A file represents a parsed file of Go assembly language.
type File struct {
URI protocol.DocumentURI
Idents []Ident
Mapper *protocol.Mapper // may map fixed Src, not file content
// TODO(adonovan): use token.File? This may be important in a
// future in which analyzers can report diagnostics in .s files.
}
func (f *File) NodeRange(ident Ident) (protocol.Range, error) {
return f.Mapper.OffsetRange(ident.Offset, ident.End())
}
// Ident represents an identifier in an assembly file.
type Ident struct {
Name string // symbol name (after correcting [·∕]); Name[0]='.' => current package
Offset int // zero-based byte offset
Kind Kind
}
// End returns the identifier's end offset.
func (id Ident) End() int { return id.Offset + len(id.Name) }
// Parse extracts identifiers from Go assembly files.
// Since it is a best-effort parser, it never returns an error.
func Parse(uri protocol.DocumentURI, content []byte) *File {
var idents []Ident
offset := 0 // byte offset of start of current line
// TODO(adonovan) use a proper tokenizer that respects
// comments, string literals, line continuations, etc.
scan := bufio.NewScanner(bytes.NewReader(content))
for ; scan.Scan(); offset += len(scan.Bytes()) + len("\n") {
line := scan.Text()
// Strip comments.
if idx := strings.Index(line, "//"); idx >= 0 {
line = line[:idx]
}
// Skip blank lines.
if strings.TrimSpace(line) == "" {
continue
}
// Check for label definitions (ending with colon).
if colon := strings.IndexByte(line, ':'); colon > 0 {
label := strings.TrimSpace(line[:colon])
if isIdent(label) {
idents = append(idents, Ident{
Name: label,
Offset: offset + strings.Index(line, label),
Kind: Label,
})
continue
}
}
// Split line into words.
words := strings.Fields(line)
if len(words) == 0 {
continue
}
// A line of the form
// TEXT ·sym<ABIInternal>(SB),NOSPLIT,$12
// declares a text symbol "·sym".
if len(words) > 1 {
kind := Invalid
switch words[0] {
case "TEXT":
kind = Text
case "GLOBL":
kind = Global
case "DATA":
kind = Data
}
if kind != Invalid {
sym := words[1]
sym = cutBefore(sym, ",") // strip ",NOSPLIT,$12" etc
sym = cutBefore(sym, "(") // "sym(SB)" -> "sym"
sym = cutBefore(sym, "<") // "sym<ABIInternal>" -> "sym"
sym = strings.TrimSpace(sym)
if isIdent(sym) {
// (The Index call assumes sym is not itself "TEXT" etc.)
idents = append(idents, Ident{
Name: cleanup(sym),
Kind: kind,
Offset: offset + strings.Index(line, sym),
})
}
continue
}
}
// Find references in the rest of the line.
pos := 0
for _, word := range words {
// Find actual position of word within line.
tokenPos := strings.Index(line[pos:], word)
if tokenPos < 0 {
panic(line)
}
tokenPos += pos
pos = tokenPos + len(word)
// Reject probable instruction mnemonics (e.g. MOV).
if len(word) >= 2 && word[0] != '·' &&
!strings.ContainsFunc(word, unicode.IsLower) {
continue
}
if word[0] == '$' {
word = word[1:]
tokenPos++
// Reject probable immediate values (e.g. "$123").
if !strings.ContainsFunc(word, isNonDigit) {
continue
}
}
// Reject probably registers (e.g. "PC").
if len(word) <= 3 && !strings.ContainsFunc(word, unicode.IsLower) {
continue
}
// Probable identifier reference.
//
// TODO(adonovan): handle FP symbols correctly;
// sym+8(FP) is essentially a comment about
// stack slot 8, not a reference to a symbol
// with a declaration somewhere; so they form
// an equivalence class without a canonical
// declaration.
//
// TODO(adonovan): handle pseudoregisters and field
// references such as:
// MOVD $runtime·g0(SB), g // pseudoreg
// MOVD R0, g_stackguard0(g) // field ref
sym := cutBefore(word, "(") // "·sym(SB)" => "sym"
sym = cutBefore(sym, "+") // "sym+8(FP)" => "sym"
sym = cutBefore(sym, "<") // "sym<ABIInternal>" =>> "sym"
if isIdent(sym) {
idents = append(idents, Ident{
Name: cleanup(sym),
Kind: Ref,
Offset: offset + tokenPos,
})
}
}
}
_ = scan.Err() // ignore scan errors
return &File{Idents: idents, Mapper: protocol.NewMapper(uri, content), URI: uri}
}
// isIdent reports whether s is a valid Go assembly identifier.
func isIdent(s string) bool {
for i, r := range s {
if !isIdentRune(r, i) {
return false
}
}
return len(s) > 0
}
// cutBefore returns the portion of s before the first occurrence of sep, if any.
func cutBefore(s, sep string) string {
if before, _, ok := strings.Cut(s, sep); ok {
return before
}
return s
}
// cleanup converts a symbol name from assembler syntax to linker syntax.
func cleanup(sym string) string {
return repl.Replace(sym)
}
var repl = strings.NewReplacer(
"·", ".", // (U+00B7 MIDDLE DOT)
"∕", "/", // (U+2215 DIVISION SLASH)
)
func isNonDigit(r rune) bool { return !unicode.IsDigit(r) }
// -- plundered from GOROOT/src/cmd/asm/internal/asm/parse.go --
// We want center dot (·) and division slash (∕) to work as identifier characters.
func isIdentRune(ch rune, i int) bool {
if unicode.IsLetter(ch) {
return true
}
switch ch {
case '_': // Underscore; traditional.
return true
case '\u00B7': // Represents the period in runtime.exit. U+00B7 '·' middle dot
return true
case '\u2215': // Represents the slash in runtime/debug.setGCPercent. U+2215 '∕' division slash
return true
}
// Digits are OK only after the first character.
return i > 0 && unicode.IsDigit(ch)
}