| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package asm |
| 7 |
|
| 8 |
import ( |
| 9 |
"bufio" |
| 10 |
"bytes" |
| 11 |
"fmt" |
| 12 |
"strings" |
| 13 |
"unicode" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type Kind uint8 |
| 18 |
|
| 19 |
const ( |
| 20 |
Invalid Kind = iota |
| 21 |
Ref |
| 22 |
Text |
| 23 |
Global |
| 24 |
Data |
| 25 |
Label |
| 26 |
) |
| 27 |
|
| 28 |
func (k Kind) String() string { |
| 29 |
if int(k) < len(kindString) { |
| 30 |
return kindString[k] |
| 31 |
} |
| 32 |
return fmt.Sprintf("Kind(%d)", k) |
| 33 |
} |
| 34 |
|
| 35 |
var kindString = [...]string{ |
| 36 |
Invalid: "invalid", |
| 37 |
Ref: "ref", |
| 38 |
Text: "text", |
| 39 |
Global: "global", |
| 40 |
Data: "data", |
| 41 |
Label: "label", |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type File struct { |
| 46 |
Idents []Ident |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
type Ident struct { |
| 54 |
Name string age |
| 55 |
Offset int |
| 56 |
Kind Kind |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func (id Ident) End() int { return id.Offset + len(id.Name) } |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func Parse(content []byte) *File { |
| 65 |
var idents []Ident |
| 66 |
offset := 0 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
scan := bufio.NewScanner(bytes.NewReader(content)) |
| 71 |
for ; scan.Scan(); offset += len(scan.Bytes()) + len("\n") { |
| 72 |
line := scan.Text() |
| 73 |
|
| 74 |
|
| 75 |
if idx := strings.Index(line, "//"); idx >= 0 { |
| 76 |
line = line[:idx] |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
if strings.TrimSpace(line) == "" { |
| 81 |
continue |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
if colon := strings.IndexByte(line, ':'); colon > 0 { |
| 86 |
label := strings.TrimSpace(line[:colon]) |
| 87 |
if isIdent(label) { |
| 88 |
idents = append(idents, Ident{ |
| 89 |
Name: label, |
| 90 |
Offset: offset + strings.Index(line, label), |
| 91 |
Kind: Label, |
| 92 |
}) |
| 93 |
continue |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
words := strings.Fields(line) |
| 99 |
if len(words) == 0 { |
| 100 |
continue |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
2 |
| 105 |
. |
| 106 |
if len(words) > 1 { |
| 107 |
kind := Invalid |
| 108 |
switch words[0] { |
| 109 |
case "TEXT": |
| 110 |
kind = Text |
| 111 |
case "GLOBL": |
| 112 |
kind = Global |
| 113 |
case "DATA": |
| 114 |
kind = Data |
| 115 |
} |
| 116 |
if kind != Invalid { |
| 117 |
sym := words[1] |
| 118 |
sym = cutBefore(sym, ",") |
| 119 |
sym = cutBefore(sym, "(") |
| 120 |
sym = cutBefore(sym, "<") |
| 121 |
sym = strings.TrimSpace(sym) |
| 122 |
if isIdent(sym) { |
| 123 |
|
| 124 |
idents = append(idents, Ident{ |
| 125 |
Name: cleanup(sym), |
| 126 |
Kind: kind, |
| 127 |
Offset: offset + strings.Index(line, sym), |
| 128 |
}) |
| 129 |
} |
| 130 |
continue |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
pos := 0 |
| 136 |
for _, word := range words { |
| 137 |
|
| 138 |
tokenPos := strings.Index(line[pos:], word) |
| 139 |
if tokenPos < 0 { |
| 140 |
panic(line) |
| 141 |
} |
| 142 |
tokenPos += pos |
| 143 |
pos = tokenPos + len(word) |
| 144 |
|
| 145 |
|
| 146 |
if len(word) >= 2 && word[0] != '·' && |
| 147 |
!strings.ContainsFunc(word, unicode.IsLower) { |
| 148 |
continue |
| 149 |
} |
| 150 |
|
| 151 |
if word[0] == '$' { |
| 152 |
word = word[1:] |
| 153 |
tokenPos++ |
| 154 |
|
| 155 |
|
| 156 |
if !strings.ContainsFunc(word, isNonDigit) { |
| 157 |
continue |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if len(word) <= 3 && !strings.ContainsFunc(word, unicode.IsLower) { |
| 163 |
continue |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
g |
| 178 |
|
| 179 |
|
| 180 |
sym := cutBefore(word, "(") " |
| 181 |
sym = cutBefore(sym, "+") |
| 182 |
sym = cutBefore(sym, "<") |
| 183 |
if isIdent(sym) { |
| 184 |
idents = append(idents, Ident{ |
| 185 |
Name: cleanup(sym), |
| 186 |
Kind: Ref, |
| 187 |
Offset: offset + tokenPos, |
| 188 |
}) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
_ = scan.Err() |
| 194 |
|
| 195 |
return &File{Idents: idents} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
func isIdent(s string) bool { |
| 200 |
for i, r := range s { |
| 201 |
if !isIdentRune(r, i) { |
| 202 |
return false |
| 203 |
} |
| 204 |
} |
| 205 |
return len(s) > 0 |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
func cutBefore(s, sep string) string { |
| 210 |
if before, _, ok := strings.Cut(s, sep); ok { |
| 211 |
return before |
| 212 |
} |
| 213 |
return s |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
func cleanup(sym string) string { |
| 218 |
return repl.Replace(sym) |
| 219 |
} |
| 220 |
|
| 221 |
var repl = strings.NewReplacer( |
| 222 |
"·", ".",) |
| 223 |
"∕", "/"H) |
| 224 |
) |
| 225 |
|
| 226 |
func isNonDigit(r rune) bool { return !unicode.IsDigit(r) } |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
rs. |
| 231 |
func isIdentRune(ch rune, i int) bool { |
| 232 |
if unicode.IsLetter(ch) { |
| 233 |
return true |
| 234 |
} |
| 235 |
switch ch { |
| 236 |
case '_': |
| 237 |
return true |
| 238 |
case '\u00B7': t |
| 239 |
return true |
| 240 |
case '\u2215': sh |
| 241 |
return true |
| 242 |
} |
| 243 |
|
| 244 |
return i > 0 && unicode.IsDigit(ch) |
| 245 |
} |