| 1 |
// Copyright 2015 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 asm implements the parser and instruction generator for the assembler. |
| 6 |
// TODO: Split apart? |
| 7 |
package asm |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"log" |
| 13 |
"os" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"text/scanner" |
| 17 |
"unicode/utf8" |
| 18 |
|
| 19 |
"cmd/asm/internal/arch" |
| 20 |
"cmd/asm/internal/flags" |
| 21 |
"cmd/asm/internal/lex" |
| 22 |
"cmd/internal/obj" |
| 23 |
"cmd/internal/obj/arm64" |
| 24 |
"cmd/internal/obj/riscv" |
| 25 |
"cmd/internal/obj/x86" |
| 26 |
"cmd/internal/objabi" |
| 27 |
"cmd/internal/src" |
| 28 |
"cmd/internal/sys" |
| 29 |
) |
| 30 |
|
| 31 |
type Parser struct { |
| 32 |
lex lex.TokenReader |
| 33 |
lineNum int // Line number in source file. |
| 34 |
errorLine int // Line number of last error. |
| 35 |
errorCount int // Number of errors. |
| 36 |
sawCode bool // saw code in this file (as opposed to comments and blank lines) |
| 37 |
pc int64 // virtual PC; count of Progs; doesn't advance for GLOBL or DATA. |
| 38 |
input []lex.Token |
| 39 |
inputPos int |
| 40 |
pendingLabels []string // Labels to attach to next instruction. |
| 41 |
labels map[string]*obj.Prog |
| 42 |
toPatch []Patch |
| 43 |
addr []obj.Addr |
| 44 |
arch *arch.Arch |
| 45 |
ctxt *obj.Link |
| 46 |
firstProg *obj.Prog |
| 47 |
lastProg *obj.Prog |
| 48 |
dataAddr map[string]int64 // Most recent address for DATA for this symbol. |
| 49 |
isJump bool // Instruction being assembled is a jump. |
| 50 |
allowABI bool // Whether ABI selectors are allowed. |
| 51 |
pkgPrefix string // Prefix to add to local symbols. |
| 52 |
errorWriter io.Writer |
| 53 |
} |
| 54 |
|
| 55 |
type Patch struct { |
| 56 |
addr *obj.Addr |
| 57 |
label string |
| 58 |
} |
| 59 |
|
| 60 |
func NewParser(ctxt *obj.Link, ar *arch.Arch, lexer lex.TokenReader) *Parser { |
| 61 |
pkgPrefix := obj.UnlinkablePkg |
| 62 |
if ctxt != nil { |
| 63 |
pkgPrefix = objabi.PathToPrefix(ctxt.Pkgpath) |
| 64 |
} |
| 65 |
return &Parser{ |
| 66 |
ctxt: ctxt, |
| 67 |
arch: ar, |
| 68 |
lex: lexer, |
| 69 |
labels: make(map[string]*obj.Prog), |
| 70 |
dataAddr: make(map[string]int64), |
| 71 |
errorWriter: os.Stderr, |
| 72 |
allowABI: ctxt != nil && objabi.LookupPkgSpecial(ctxt.Pkgpath).AllowAsmABI, |
| 73 |
pkgPrefix: pkgPrefix, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// panicOnError is enabled when testing to abort execution on the first error |
| 78 |
// and turn it into a recoverable panic. |
| 79 |
var panicOnError bool |
| 80 |
|
| 81 |
func (p *Parser) errorf(format string, args ...any) { |
| 82 |
if panicOnError { |
| 83 |
panic(fmt.Errorf(format, args...)) |
| 84 |
} |
| 85 |
if p.lineNum == p.errorLine { |
| 86 |
// Only one error per line. |
| 87 |
return |
| 88 |
} |
| 89 |
p.errorLine = p.lineNum |
| 90 |
if p.lex != nil { |
| 91 |
// Put file and line information on head of message. |
| 92 |
format = "%s:%d: " + format + "\n" |
| 93 |
args = append([]any{p.lex.File(), p.lineNum}, args...) |
| 94 |
} |
| 95 |
fmt.Fprintf(p.errorWriter, format, args...) |
| 96 |
p.errorCount++ |
| 97 |
if p.errorCount > 10 && !*flags.AllErrors { |
| 98 |
log.Fatal("too many errors") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func (p *Parser) pos() src.XPos { |
| 103 |
return p.ctxt.PosTable.XPos(src.MakePos(p.lex.Base(), uint(p.lineNum), 0)) |
| 104 |
} |
| 105 |
|
| 106 |
func (p *Parser) Parse() (*obj.Prog, bool) { |
| 107 |
scratch := make([][]lex.Token, 0, 3) |
| 108 |
for { |
| 109 |
word, cond, operands, ok := p.line(scratch) |
| 110 |
if !ok { |
| 111 |
break |
| 112 |
} |
| 113 |
scratch = operands |
| 114 |
|
| 115 |
if p.pseudo(word, operands) { |
| 116 |
continue |
| 117 |
} |
| 118 |
i, present := p.arch.Instructions[word] |
| 119 |
if present { |
| 120 |
p.instruction(i, word, cond, operands) |
| 121 |
continue |
| 122 |
} |
| 123 |
p.errorf("unrecognized instruction %q", word) |
| 124 |
} |
| 125 |
if p.errorCount > 0 { |
| 126 |
return nil, false |
| 127 |
} |
| 128 |
p.patch() |
| 129 |
return p.firstProg, true |
| 130 |
} |
| 131 |
|
| 132 |
// ParseSymABIs parses p's assembly code to find text symbol |
| 133 |
// definitions and references and writes a symabis file to w. |
| 134 |
func (p *Parser) ParseSymABIs(w io.Writer) bool { |
| 135 |
operands := make([][]lex.Token, 0, 3) |
| 136 |
for { |
| 137 |
word, _, operands1, ok := p.line(operands) |
| 138 |
if !ok { |
| 139 |
break |
| 140 |
} |
| 141 |
operands = operands1 |
| 142 |
|
| 143 |
p.symDefRef(w, word, operands) |
| 144 |
} |
| 145 |
return p.errorCount == 0 |
| 146 |
} |
| 147 |
|
| 148 |
// nextToken returns the next non-build-comment token from the lexer. |
| 149 |
// It reports misplaced //go:build comments but otherwise discards them. |
| 150 |
func (p *Parser) nextToken() lex.ScanToken { |
| 151 |
for { |
| 152 |
tok := p.lex.Next() |
| 153 |
if tok == lex.BuildComment { |
| 154 |
if p.sawCode { |
| 155 |
p.errorf("misplaced //go:build comment") |
| 156 |
} |
| 157 |
continue |
| 158 |
} |
| 159 |
if tok != '\n' { |
| 160 |
p.sawCode = true |
| 161 |
} |
| 162 |
if tok == '#' { |
| 163 |
// A leftover wisp of a #include/#define/etc, |
| 164 |
// to let us know that p.sawCode should be true now. |
| 165 |
// Otherwise ignored. |
| 166 |
continue |
| 167 |
} |
| 168 |
return tok |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// line consumes a single assembly line from p.lex of the form |
| 173 |
// |
| 174 |
// {label:} WORD[.cond] [ arg {, arg} ] (';' | '\n') |
| 175 |
// |
| 176 |
// It adds any labels to p.pendingLabels and returns the word, cond, |
| 177 |
// operand list, and true. If there is an error or EOF, it returns |
| 178 |
// ok=false. |
| 179 |
// |
| 180 |
// line may reuse the memory from scratch. |
| 181 |
func (p *Parser) line(scratch [][]lex.Token) (word, cond string, operands [][]lex.Token, ok bool) { |
| 182 |
next: |
| 183 |
// Skip newlines. |
| 184 |
var tok lex.ScanToken |
| 185 |
for { |
| 186 |
tok = p.nextToken() |
| 187 |
// We save the line number here so error messages from this instruction |
| 188 |
// are labeled with this line. Otherwise we complain after we've absorbed |
| 189 |
// the terminating newline and the line numbers are off by one in errors. |
| 190 |
p.lineNum = p.lex.Line() |
| 191 |
switch tok { |
| 192 |
case '\n', ';': |
| 193 |
continue |
| 194 |
case scanner.EOF: |
| 195 |
return "", "", nil, false |
| 196 |
} |
| 197 |
break |
| 198 |
} |
| 199 |
// First item must be an identifier. |
| 200 |
if tok != scanner.Ident { |
| 201 |
p.errorf("expected identifier, found %q", p.lex.Text()) |
| 202 |
return "", "", nil, false // Might as well stop now. |
| 203 |
} |
| 204 |
word, cond = p.lex.Text(), "" |
| 205 |
operands = scratch[:0] |
| 206 |
// Zero or more comma-separated operands, one per loop. |
| 207 |
nesting := 0 |
| 208 |
colon := -1 |
| 209 |
for tok != '\n' && tok != ';' { |
| 210 |
// Process one operand. |
| 211 |
var items []lex.Token |
| 212 |
if cap(operands) > len(operands) { |
| 213 |
// Reuse scratch items slice. |
| 214 |
items = operands[:cap(operands)][len(operands)][:0] |
| 215 |
} else { |
| 216 |
items = make([]lex.Token, 0, 3) |
| 217 |
} |
| 218 |
for { |
| 219 |
tok = p.nextToken() |
| 220 |
if len(operands) == 0 && len(items) == 0 { |
| 221 |
if p.arch.InFamily(sys.ARM, sys.ARM64, sys.AMD64, sys.I386, sys.Loong64, sys.RISCV64) && tok == '.' { |
| 222 |
// Suffixes: ARM conditionals, Loong64 vector instructions, RISCV rounding mode or x86 modifiers. |
| 223 |
tok = p.nextToken() |
| 224 |
str := p.lex.Text() |
| 225 |
if tok != scanner.Ident { |
| 226 |
p.errorf("instruction suffix expected identifier, found %s", str) |
| 227 |
} |
| 228 |
cond = cond + "." + str |
| 229 |
continue |
| 230 |
} |
| 231 |
if tok == ':' { |
| 232 |
// Labels. |
| 233 |
p.pendingLabels = append(p.pendingLabels, word) |
| 234 |
goto next |
| 235 |
} |
| 236 |
} |
| 237 |
if tok == scanner.EOF { |
| 238 |
p.errorf("unexpected EOF") |
| 239 |
return "", "", nil, false |
| 240 |
} |
| 241 |
// Split operands on comma. Also, the old syntax on x86 for a "register pair" |
| 242 |
// was AX:DX, for which the new syntax is DX, AX. Note the reordering. |
| 243 |
if tok == '\n' || tok == ';' || (nesting == 0 && (tok == ',' || tok == ':')) { |
| 244 |
if tok == ':' { |
| 245 |
// Remember this location so we can swap the operands below. |
| 246 |
if colon >= 0 { |
| 247 |
p.errorf("invalid ':' in operand") |
| 248 |
return word, cond, operands, true |
| 249 |
} |
| 250 |
colon = len(operands) |
| 251 |
} |
| 252 |
break |
| 253 |
} |
| 254 |
if tok == '(' || tok == '[' { |
| 255 |
nesting++ |
| 256 |
} |
| 257 |
if tok == ')' || tok == ']' { |
| 258 |
nesting-- |
| 259 |
} |
| 260 |
items = append(items, lex.Make(tok, p.lex.Text())) |
| 261 |
} |
| 262 |
if len(items) > 0 { |
| 263 |
operands = append(operands, items) |
| 264 |
if colon >= 0 && len(operands) == colon+2 { |
| 265 |
// AX:DX becomes DX, AX. |
| 266 |
operands[colon], operands[colon+1] = operands[colon+1], operands[colon] |
| 267 |
colon = -1 |
| 268 |
} |
| 269 |
} else if len(operands) > 0 || tok == ',' || colon >= 0 { |
| 270 |
// Had a separator with nothing after. |
| 271 |
p.errorf("missing operand") |
| 272 |
} |
| 273 |
} |
| 274 |
return word, cond, operands, true |
| 275 |
} |
| 276 |
|
| 277 |
func (p *Parser) instruction(op obj.As, word, cond string, operands [][]lex.Token) { |
| 278 |
p.addr = p.addr[0:0] |
| 279 |
p.isJump = p.arch.IsJump(word) |
| 280 |
for _, op := range operands { |
| 281 |
addr := p.address(op) |
| 282 |
if !p.isJump && addr.Reg < 0 { // Jumps refer to PC, a pseudo. |
| 283 |
p.errorf("illegal use of pseudo-register in %s", word) |
| 284 |
} |
| 285 |
p.addr = append(p.addr, addr) |
| 286 |
} |
| 287 |
if p.isJump { |
| 288 |
p.asmJump(op, cond, p.addr) |
| 289 |
return |
| 290 |
} |
| 291 |
p.asmInstruction(op, cond, p.addr) |
| 292 |
} |
| 293 |
|
| 294 |
func (p *Parser) pseudo(word string, operands [][]lex.Token) bool { |
| 295 |
switch word { |
| 296 |
case "DATA": |
| 297 |
p.asmData(operands) |
| 298 |
case "FUNCDATA": |
| 299 |
p.asmFuncData(operands) |
| 300 |
case "GLOBL": |
| 301 |
p.asmGlobl(operands) |
| 302 |
case "PCDATA": |
| 303 |
p.asmPCData(operands) |
| 304 |
case "PCALIGN": |
| 305 |
p.asmPCAlign(operands) |
| 306 |
case "TEXT": |
| 307 |
p.asmText(operands) |
| 308 |
default: |
| 309 |
return false |
| 310 |
} |
| 311 |
return true |
| 312 |
} |
| 313 |
|
| 314 |
// symDefRef scans a line for potential text symbol definitions and |
| 315 |
// references and writes symabis information to w. |
| 316 |
// |
| 317 |
// The symabis format is documented at |
| 318 |
// cmd/compile/internal/ssagen.ReadSymABIs. |
| 319 |
func (p *Parser) symDefRef(w io.Writer, word string, operands [][]lex.Token) { |
| 320 |
switch word { |
| 321 |
case "TEXT": |
| 322 |
// Defines text symbol in operands[0]. |
| 323 |
if len(operands) > 0 { |
| 324 |
p.start(operands[0]) |
| 325 |
if name, abi, ok := p.funcAddress(); ok { |
| 326 |
fmt.Fprintf(w, "def %s %s\n", name, abi) |
| 327 |
} |
| 328 |
} |
| 329 |
return |
| 330 |
case "GLOBL", "PCDATA": |
| 331 |
// No text definitions or symbol references. |
| 332 |
case "DATA", "FUNCDATA": |
| 333 |
// For DATA, operands[0] is defined symbol. |
| 334 |
// For FUNCDATA, operands[0] is an immediate constant. |
| 335 |
// Remaining operands may have references. |
| 336 |
if len(operands) < 2 { |
| 337 |
return |
| 338 |
} |
| 339 |
operands = operands[1:] |
| 340 |
} |
| 341 |
// Search for symbol references. |
| 342 |
for _, op := range operands { |
| 343 |
p.start(op) |
| 344 |
if name, abi, ok := p.funcAddress(); ok { |
| 345 |
fmt.Fprintf(w, "ref %s %s\n", name, abi) |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func (p *Parser) start(operand []lex.Token) { |
| 351 |
p.input = operand |
| 352 |
p.inputPos = 0 |
| 353 |
} |
| 354 |
|
| 355 |
// address parses the operand into a link address structure. |
| 356 |
func (p *Parser) address(operand []lex.Token) obj.Addr { |
| 357 |
p.start(operand) |
| 358 |
addr := obj.Addr{} |
| 359 |
p.operand(&addr) |
| 360 |
return addr |
| 361 |
} |
| 362 |
|
| 363 |
// parseScale converts a decimal string into a valid scale factor. |
| 364 |
func (p *Parser) parseScale(s string) int8 { |
| 365 |
switch s { |
| 366 |
case "1", "2", "4", "8": |
| 367 |
return int8(s[0] - '0') |
| 368 |
} |
| 369 |
p.errorf("bad scale: %s", s) |
| 370 |
return 0 |
| 371 |
} |
| 372 |
|
| 373 |
// operand parses a general operand and stores the result in *a. |
| 374 |
func (p *Parser) operand(a *obj.Addr) { |
| 375 |
//fmt.Printf("Operand: %v\n", p.input) |
| 376 |
if len(p.input) == 0 { |
| 377 |
p.errorf("empty operand: cannot happen") |
| 378 |
return |
| 379 |
} |
| 380 |
// General address (with a few exceptions) looks like |
| 381 |
// $sym±offset(SB)(reg)(index*scale) |
| 382 |
// Exceptions are: |
| 383 |
// |
| 384 |
// R1 |
| 385 |
// offset |
| 386 |
// $offset |
| 387 |
// Every piece is optional, so we scan left to right and what |
| 388 |
// we discover tells us where we are. |
| 389 |
|
| 390 |
// Prefix: $. |
| 391 |
var prefix rune |
| 392 |
switch tok := p.peek(); tok { |
| 393 |
case '$', '*': |
| 394 |
prefix = rune(tok) |
| 395 |
p.next() |
| 396 |
} |
| 397 |
|
| 398 |
// Symbol: sym±offset(SB) |
| 399 |
tok := p.next() |
| 400 |
name := tok.String() |
| 401 |
if tok.ScanToken == scanner.Ident && !p.atStartOfRegister(name) { |
| 402 |
// See if this is an architecture specific special operand. |
| 403 |
switch p.arch.Family { |
| 404 |
case sys.ARM64: |
| 405 |
if opd := arch.ARM64SpecialOperand(name); opd != arm64.SPOP_END { |
| 406 |
a.Type = obj.TYPE_SPECIAL |
| 407 |
a.Offset = int64(opd) |
| 408 |
} |
| 409 |
case sys.RISCV64: |
| 410 |
if opd := arch.RISCV64SpecialOperand(name); opd != riscv.SPOP_END { |
| 411 |
a.Type = obj.TYPE_SPECIAL |
| 412 |
a.Offset = int64(opd) |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
if a.Type != obj.TYPE_SPECIAL { |
| 417 |
// We have a symbol. Parse $sym±offset(symkind) |
| 418 |
p.symbolReference(a, p.qualifySymbol(name), prefix) |
| 419 |
} |
| 420 |
// fmt.Printf("SYM %s\n", obj.Dconv(&emptyProg, 0, a)) |
| 421 |
if p.peek() == scanner.EOF { |
| 422 |
return |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// Special register list syntax for arm: [R1,R3-R7] |
| 427 |
if tok.ScanToken == '[' { |
| 428 |
if prefix != 0 { |
| 429 |
p.errorf("illegal use of register list") |
| 430 |
} |
| 431 |
p.registerList(a) |
| 432 |
p.expectOperandEnd() |
| 433 |
return |
| 434 |
} |
| 435 |
|
| 436 |
// Register: R1 |
| 437 |
if tok.ScanToken == scanner.Ident && p.atStartOfRegister(name) { |
| 438 |
if p.atRegisterShift() { |
| 439 |
// ARM shifted register such as R1<<R2 or R1>>2. |
| 440 |
a.Type = obj.TYPE_SHIFT |
| 441 |
a.Offset = p.registerShift(tok.String(), prefix) |
| 442 |
if p.peek() == '(' { |
| 443 |
// Can only be a literal register here. |
| 444 |
p.next() |
| 445 |
tok := p.next() |
| 446 |
name := tok.String() |
| 447 |
if !p.atStartOfRegister(name) { |
| 448 |
p.errorf("expected register; found %s", name) |
| 449 |
} |
| 450 |
a.Reg, _ = p.registerReference(name) |
| 451 |
p.get(')') |
| 452 |
} |
| 453 |
} else if p.atRegisterExtension() { |
| 454 |
a.Type = obj.TYPE_REG |
| 455 |
p.registerExtension(a, tok.String(), prefix) |
| 456 |
p.expectOperandEnd() |
| 457 |
return |
| 458 |
} else if r1, r2, scale, ok := p.register(tok.String(), prefix); ok { |
| 459 |
if scale != 0 { |
| 460 |
p.errorf("expected simple register reference") |
| 461 |
} |
| 462 |
a.Type = obj.TYPE_REG |
| 463 |
a.Reg = r1 |
| 464 |
if r2 != 0 { |
| 465 |
// Form is R1:R2. It is on RHS and the second register |
| 466 |
// needs to go into the LHS. |
| 467 |
panic("cannot happen (Addr.Reg2)") |
| 468 |
} |
| 469 |
} |
| 470 |
// fmt.Printf("REG %s\n", obj.Dconv(&emptyProg, 0, a)) |
| 471 |
p.expectOperandEnd() |
| 472 |
return |
| 473 |
} |
| 474 |
|
| 475 |
// Constant. |
| 476 |
haveConstant := false |
| 477 |
switch tok.ScanToken { |
| 478 |
case scanner.Int, scanner.Float, scanner.String, scanner.Char, '+', '-', '~': |
| 479 |
haveConstant = true |
| 480 |
case '(': |
| 481 |
// Could be parenthesized expression or (R). Must be something, though. |
| 482 |
tok := p.next() |
| 483 |
if tok.ScanToken == scanner.EOF { |
| 484 |
p.errorf("missing right parenthesis") |
| 485 |
return |
| 486 |
} |
| 487 |
rname := tok.String() |
| 488 |
p.back() |
| 489 |
haveConstant = !p.atStartOfRegister(rname) |
| 490 |
if !haveConstant { |
| 491 |
p.back() // Put back the '('. |
| 492 |
} |
| 493 |
} |
| 494 |
if haveConstant { |
| 495 |
p.back() |
| 496 |
if p.have(scanner.Float) { |
| 497 |
if prefix != '$' { |
| 498 |
p.errorf("floating-point constant must be an immediate") |
| 499 |
} |
| 500 |
a.Type = obj.TYPE_FCONST |
| 501 |
a.Val = p.floatExpr() |
| 502 |
// fmt.Printf("FCONST %s\n", obj.Dconv(&emptyProg, 0, a)) |
| 503 |
p.expectOperandEnd() |
| 504 |
return |
| 505 |
} |
| 506 |
if p.have(scanner.String) { |
| 507 |
if prefix != '$' { |
| 508 |
p.errorf("string constant must be an immediate") |
| 509 |
return |
| 510 |
} |
| 511 |
str, err := strconv.Unquote(p.get(scanner.String).String()) |
| 512 |
if err != nil { |
| 513 |
p.errorf("string parse error: %s", err) |
| 514 |
} |
| 515 |
a.Type = obj.TYPE_SCONST |
| 516 |
a.Val = str |
| 517 |
// fmt.Printf("SCONST %s\n", obj.Dconv(&emptyProg, 0, a)) |
| 518 |
p.expectOperandEnd() |
| 519 |
return |
| 520 |
} |
| 521 |
a.Offset = int64(p.expr()) |
| 522 |
if p.peek() != '(' { |
| 523 |
switch prefix { |
| 524 |
case '$': |
| 525 |
a.Type = obj.TYPE_CONST |
| 526 |
case '*': |
| 527 |
a.Type = obj.TYPE_INDIR // Can appear but is illegal, will be rejected by the linker. |
| 528 |
default: |
| 529 |
a.Type = obj.TYPE_MEM |
| 530 |
} |
| 531 |
// fmt.Printf("CONST %d %s\n", a.Offset, obj.Dconv(&emptyProg, 0, a)) |
| 532 |
p.expectOperandEnd() |
| 533 |
return |
| 534 |
} |
| 535 |
// fmt.Printf("offset %d \n", a.Offset) |
| 536 |
} |
| 537 |
|
| 538 |
// Register indirection: (reg) or (index*scale). We are on the opening paren. |
| 539 |
p.registerIndirect(a, prefix) |
| 540 |
// fmt.Printf("DONE %s\n", p.arch.Dconv(&emptyProg, 0, a)) |
| 541 |
|
| 542 |
p.expectOperandEnd() |
| 543 |
return |
| 544 |
} |
| 545 |
|
| 546 |
// atStartOfRegister reports whether the parser is at the start of a register definition. |
| 547 |
func (p *Parser) atStartOfRegister(name string) bool { |
| 548 |
// Simple register: R10. |
| 549 |
_, present := p.arch.Register[name] |
| 550 |
if present { |
| 551 |
return true |
| 552 |
} |
| 553 |
// Parenthesized register: R(10). |
| 554 |
return p.arch.RegisterPrefix[name] && p.peek() == '(' |
| 555 |
} |
| 556 |
|
| 557 |
// atRegisterShift reports whether we are at the start of an ARM shifted register. |
| 558 |
// We have consumed the register or R prefix. |
| 559 |
func (p *Parser) atRegisterShift() bool { |
| 560 |
// ARM only. |
| 561 |
if !p.arch.InFamily(sys.ARM, sys.ARM64) { |
| 562 |
return false |
| 563 |
} |
| 564 |
// R1<<... |
| 565 |
if lex.IsRegisterShift(p.peek()) { |
| 566 |
return true |
| 567 |
} |
| 568 |
// R(1)<<... Ugly check. TODO: Rethink how we handle ARM register shifts to be |
| 569 |
// less special. |
| 570 |
if p.peek() != '(' || len(p.input)-p.inputPos < 4 { |
| 571 |
return false |
| 572 |
} |
| 573 |
return p.at('(', scanner.Int, ')') && lex.IsRegisterShift(p.input[p.inputPos+3].ScanToken) |
| 574 |
} |
| 575 |
|
| 576 |
// atRegisterExtension reports whether we are at the start of an ARM64 extended register. |
| 577 |
// We have consumed the register or R prefix. |
| 578 |
func (p *Parser) atRegisterExtension() bool { |
| 579 |
switch p.arch.Family { |
| 580 |
case sys.ARM64, sys.Loong64: |
| 581 |
// R1.xxx |
| 582 |
return p.peek() == '.' |
| 583 |
default: |
| 584 |
return false |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
// registerReference parses a register given either the name, R10, or a parenthesized form, SPR(10). |
| 589 |
func (p *Parser) registerReference(name string) (int16, bool) { |
| 590 |
r, present := p.arch.Register[name] |
| 591 |
if present { |
| 592 |
return r, true |
| 593 |
} |
| 594 |
if !p.arch.RegisterPrefix[name] { |
| 595 |
p.errorf("expected register; found %s", name) |
| 596 |
return 0, false |
| 597 |
} |
| 598 |
p.get('(') |
| 599 |
tok := p.get(scanner.Int) |
| 600 |
num, err := strconv.ParseInt(tok.String(), 10, 16) |
| 601 |
p.get(')') |
| 602 |
if err != nil { |
| 603 |
p.errorf("parsing register list: %s", err) |
| 604 |
return 0, false |
| 605 |
} |
| 606 |
r, ok := p.arch.RegisterNumber(name, int16(num)) |
| 607 |
if !ok { |
| 608 |
p.errorf("illegal register %s(%d)", name, r) |
| 609 |
return 0, false |
| 610 |
} |
| 611 |
return r, true |
| 612 |
} |
| 613 |
|
| 614 |
// register parses a full register reference where there is no symbol present (as in 4(R0) or R(10) but not sym(SB)) |
| 615 |
// including forms involving multiple registers such as R1:R2. |
| 616 |
func (p *Parser) register(name string, prefix rune) (r1, r2 int16, scale int8, ok bool) { |
| 617 |
// R1 or R(1) R1:R2 R1,R2 R1+R2, or R1*scale. |
| 618 |
r1, ok = p.registerReference(name) |
| 619 |
if !ok { |
| 620 |
return |
| 621 |
} |
| 622 |
if prefix != 0 && prefix != '*' { // *AX is OK. |
| 623 |
p.errorf("prefix %c not allowed for register: %c%s", prefix, prefix, name) |
| 624 |
} |
| 625 |
c := p.peek() |
| 626 |
if c == ':' || c == ',' || c == '+' { |
| 627 |
// 2nd register; syntax (R1+R2) etc. No two architectures agree. |
| 628 |
// Check the architectures match the syntax. |
| 629 |
switch p.next().ScanToken { |
| 630 |
case ',': |
| 631 |
if !p.arch.InFamily(sys.ARM, sys.ARM64) { |
| 632 |
p.errorf("(register,register) not supported on this architecture") |
| 633 |
return |
| 634 |
} |
| 635 |
case '+': |
| 636 |
if p.arch.Family != sys.PPC64 { |
| 637 |
p.errorf("(register+register) not supported on this architecture") |
| 638 |
return |
| 639 |
} |
| 640 |
} |
| 641 |
name := p.next().String() |
| 642 |
r2, ok = p.registerReference(name) |
| 643 |
if !ok { |
| 644 |
return |
| 645 |
} |
| 646 |
} |
| 647 |
if p.peek() == '*' { |
| 648 |
// Scale |
| 649 |
p.next() |
| 650 |
scale = p.parseScale(p.next().String()) |
| 651 |
} |
| 652 |
return r1, r2, scale, true |
| 653 |
} |
| 654 |
|
| 655 |
// registerShift parses an ARM/ARM64 shifted register reference and returns the encoded representation. |
| 656 |
// There is known to be a register (current token) and a shift operator (peeked token). |
| 657 |
func (p *Parser) registerShift(name string, prefix rune) int64 { |
| 658 |
if prefix != 0 { |
| 659 |
p.errorf("prefix %c not allowed for shifted register: $%s", prefix, name) |
| 660 |
} |
| 661 |
// R1 op R2 or r1 op constant. |
| 662 |
// op is: |
| 663 |
// "<<" == 0 |
| 664 |
// ">>" == 1 |
| 665 |
// "->" == 2 |
| 666 |
// "@>" == 3 |
| 667 |
r1, ok := p.registerReference(name) |
| 668 |
if !ok { |
| 669 |
return 0 |
| 670 |
} |
| 671 |
var op int16 |
| 672 |
switch p.next().ScanToken { |
| 673 |
case lex.LSH: |
| 674 |
op = 0 |
| 675 |
case lex.RSH: |
| 676 |
op = 1 |
| 677 |
case lex.ARR: |
| 678 |
op = 2 |
| 679 |
case lex.ROT: |
| 680 |
// following instructions on ARM64 support rotate right |
| 681 |
// AND, ANDS, TST, BIC, BICS, EON, EOR, ORR, MVN, ORN |
| 682 |
op = 3 |
| 683 |
} |
| 684 |
tok := p.next() |
| 685 |
str := tok.String() |
| 686 |
var count int16 |
| 687 |
switch tok.ScanToken { |
| 688 |
case scanner.Ident: |
| 689 |
if p.arch.Family == sys.ARM64 { |
| 690 |
p.errorf("rhs of shift must be integer: %s", str) |
| 691 |
} else { |
| 692 |
r2, ok := p.registerReference(str) |
| 693 |
if !ok { |
| 694 |
p.errorf("rhs of shift must be register or integer: %s", str) |
| 695 |
} |
| 696 |
count = (r2&15)<<8 | 1<<4 |
| 697 |
} |
| 698 |
case scanner.Int, '(': |
| 699 |
p.back() |
| 700 |
x := int64(p.expr()) |
| 701 |
if p.arch.Family == sys.ARM64 { |
| 702 |
if x >= 64 { |
| 703 |
p.errorf("register shift count too large: %s", str) |
| 704 |
} |
| 705 |
count = int16((x & 63) << 10) |
| 706 |
} else { |
| 707 |
if x >= 32 { |
| 708 |
p.errorf("register shift count too large: %s", str) |
| 709 |
} |
| 710 |
count = int16((x & 31) << 7) |
| 711 |
} |
| 712 |
default: |
| 713 |
p.errorf("unexpected %s in register shift", tok.String()) |
| 714 |
} |
| 715 |
if p.arch.Family == sys.ARM64 { |
| 716 |
off, err := arch.ARM64RegisterShift(r1, op, count) |
| 717 |
if err != nil { |
| 718 |
p.errorf("%v", err) |
| 719 |
} |
| 720 |
return off |
| 721 |
} else { |
| 722 |
return int64((r1 & 15) | op<<5 | count) |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
// registerExtension parses a register with extension or arrangement. |
| 727 |
// There is known to be a register (current token) and an extension operator (peeked token). |
| 728 |
func (p *Parser) registerExtension(a *obj.Addr, name string, prefix rune) { |
| 729 |
if prefix != 0 { |
| 730 |
p.errorf("prefix %c not allowed for shifted register: $%s", prefix, name) |
| 731 |
} |
| 732 |
|
| 733 |
reg, ok := p.registerReference(name) |
| 734 |
if !ok { |
| 735 |
p.errorf("unexpected %s in register extension", name) |
| 736 |
return |
| 737 |
} |
| 738 |
|
| 739 |
isIndex := false |
| 740 |
num := int16(0) |
| 741 |
isAmount := true // Amount is zero by default |
| 742 |
ext := "" |
| 743 |
if p.peek() == lex.LSH { |
| 744 |
// (Rn)(Rm<<2), the shifted offset register. |
| 745 |
ext = "LSL" |
| 746 |
} else { |
| 747 |
// (Rn)(Rm.UXTW<1), the extended offset register. |
| 748 |
// Rm.UXTW<<3, the extended register. |
| 749 |
p.get('.') |
| 750 |
tok := p.next() |
| 751 |
ext = tok.String() |
| 752 |
} |
| 753 |
if p.peek() == lex.LSH { |
| 754 |
// parses left shift amount applied after extension: <<Amount |
| 755 |
p.get(lex.LSH) |
| 756 |
tok := p.get(scanner.Int) |
| 757 |
amount, err := strconv.ParseInt(tok.String(), 10, 16) |
| 758 |
if err != nil { |
| 759 |
p.errorf("parsing left shift amount: %s", err) |
| 760 |
} |
| 761 |
num = int16(amount) |
| 762 |
} else if p.peek() == '[' { |
| 763 |
// parses an element: [Index] |
| 764 |
p.get('[') |
| 765 |
tok := p.get(scanner.Int) |
| 766 |
index, err := strconv.ParseInt(tok.String(), 10, 16) |
| 767 |
p.get(']') |
| 768 |
if err != nil { |
| 769 |
p.errorf("parsing element index: %s", err) |
| 770 |
} |
| 771 |
isIndex = true |
| 772 |
isAmount = false |
| 773 |
num = int16(index) |
| 774 |
} |
| 775 |
|
| 776 |
switch p.arch.Family { |
| 777 |
case sys.ARM64: |
| 778 |
err := arm64.ARM64RegisterExtension(a, ext, reg, num, isAmount, isIndex) |
| 779 |
if err != nil { |
| 780 |
p.errorf("%v", err) |
| 781 |
} |
| 782 |
case sys.Loong64: |
| 783 |
err := arch.Loong64RegisterExtension(a, ext, reg, num, isAmount, isIndex) |
| 784 |
if err != nil { |
| 785 |
p.errorf("%v", err) |
| 786 |
} |
| 787 |
default: |
| 788 |
p.errorf("register extension not supported on this architecture") |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
// qualifySymbol returns name as a package-qualified symbol name. If |
| 793 |
// name starts with a period, qualifySymbol prepends the package |
| 794 |
// prefix. Otherwise it returns name unchanged. |
| 795 |
func (p *Parser) qualifySymbol(name string) string { |
| 796 |
if strings.HasPrefix(name, ".") { |
| 797 |
name = p.pkgPrefix + name |
| 798 |
} |
| 799 |
return name |
| 800 |
} |
| 801 |
|
| 802 |
// symbolReference parses a symbol that is known not to be a register. |
| 803 |
func (p *Parser) symbolReference(a *obj.Addr, name string, prefix rune) { |
| 804 |
// Identifier is a name. |
| 805 |
switch prefix { |
| 806 |
case 0: |
| 807 |
a.Type = obj.TYPE_MEM |
| 808 |
case '$': |
| 809 |
a.Type = obj.TYPE_ADDR |
| 810 |
case '*': |
| 811 |
a.Type = obj.TYPE_INDIR |
| 812 |
} |
| 813 |
|
| 814 |
// Parse optional <> (indicates a static symbol) or |
| 815 |
// <ABIxxx> (selecting text symbol with specific ABI). |
| 816 |
doIssueError := true |
| 817 |
isStatic, abi := p.symRefAttrs(name, doIssueError) |
| 818 |
|
| 819 |
if p.peek() == '+' || p.peek() == '-' { |
| 820 |
a.Offset = int64(p.expr()) |
| 821 |
} |
| 822 |
if isStatic { |
| 823 |
a.Sym = p.ctxt.LookupStatic(name) |
| 824 |
} else { |
| 825 |
a.Sym = p.ctxt.LookupABI(name, abi) |
| 826 |
} |
| 827 |
if p.peek() == scanner.EOF { |
| 828 |
if prefix == 0 && p.isJump { |
| 829 |
// Symbols without prefix or suffix are jump labels. |
| 830 |
return |
| 831 |
} |
| 832 |
p.errorf("illegal or missing addressing mode for symbol %s", name) |
| 833 |
return |
| 834 |
} |
| 835 |
// Expect (SB), (FP), (PC), or (SP) |
| 836 |
p.get('(') |
| 837 |
reg := p.get(scanner.Ident).String() |
| 838 |
p.get(')') |
| 839 |
p.setPseudoRegister(a, reg, isStatic, prefix) |
| 840 |
} |
| 841 |
|
| 842 |
// setPseudoRegister sets the NAME field of addr for a pseudo-register reference such as (SB). |
| 843 |
func (p *Parser) setPseudoRegister(addr *obj.Addr, reg string, isStatic bool, prefix rune) { |
| 844 |
if addr.Reg != 0 { |
| 845 |
p.errorf("internal error: reg %s already set in pseudo", reg) |
| 846 |
} |
| 847 |
switch reg { |
| 848 |
case "FP": |
| 849 |
addr.Name = obj.NAME_PARAM |
| 850 |
case "PC": |
| 851 |
if prefix != 0 { |
| 852 |
p.errorf("illegal addressing mode for PC") |
| 853 |
} |
| 854 |
addr.Type = obj.TYPE_BRANCH // We set the type and leave NAME untouched. See asmJump. |
| 855 |
case "SB": |
| 856 |
addr.Name = obj.NAME_EXTERN |
| 857 |
if isStatic { |
| 858 |
addr.Name = obj.NAME_STATIC |
| 859 |
} |
| 860 |
case "SP": |
| 861 |
addr.Name = obj.NAME_AUTO // The pseudo-stack. |
| 862 |
default: |
| 863 |
p.errorf("expected pseudo-register; found %s", reg) |
| 864 |
} |
| 865 |
if prefix == '$' { |
| 866 |
addr.Type = obj.TYPE_ADDR |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
// symRefAttrs parses an optional function symbol attribute clause for |
| 871 |
// the function symbol 'name', logging an error for a malformed |
| 872 |
// attribute clause if 'issueError' is true. The return value is a |
| 873 |
// (boolean, ABI) pair indicating that the named symbol is either |
| 874 |
// static or a particular ABI specification. |
| 875 |
// |
| 876 |
// The expected form of the attribute clause is: |
| 877 |
// |
| 878 |
// empty, yielding (false, obj.ABI0) |
| 879 |
// "<>", yielding (true, obj.ABI0) |
| 880 |
// "<ABI0>" yielding (false, obj.ABI0) |
| 881 |
// "<ABIInternal>" yielding (false, obj.ABIInternal) |
| 882 |
// |
| 883 |
// Anything else beginning with "<" logs an error if issueError is |
| 884 |
// true, otherwise returns (false, obj.ABI0). |
| 885 |
func (p *Parser) symRefAttrs(name string, issueError bool) (bool, obj.ABI) { |
| 886 |
abi := obj.ABI0 |
| 887 |
isStatic := false |
| 888 |
if p.peek() != '<' { |
| 889 |
return isStatic, abi |
| 890 |
} |
| 891 |
p.next() |
| 892 |
tok := p.peek() |
| 893 |
if tok == '>' { |
| 894 |
isStatic = true |
| 895 |
} else if tok == scanner.Ident { |
| 896 |
abistr := p.get(scanner.Ident).String() |
| 897 |
if !p.allowABI { |
| 898 |
if issueError { |
| 899 |
p.errorf("ABI selector only permitted when compiling runtime, reference was to %q", name) |
| 900 |
} |
| 901 |
} else { |
| 902 |
theabi, valid := obj.ParseABI(abistr) |
| 903 |
if !valid { |
| 904 |
if issueError { |
| 905 |
p.errorf("malformed ABI selector %q in reference to %q", |
| 906 |
abistr, name) |
| 907 |
} |
| 908 |
} else { |
| 909 |
abi = theabi |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
p.get('>') |
| 914 |
return isStatic, abi |
| 915 |
} |
| 916 |
|
| 917 |
// funcAddress parses an external function address. This is a |
| 918 |
// constrained form of the operand syntax that's always SB-based, |
| 919 |
// non-static, and has at most a simple integer offset: |
| 920 |
// |
| 921 |
// [$|*]sym[<abi>][+Int](SB) |
| 922 |
func (p *Parser) funcAddress() (string, obj.ABI, bool) { |
| 923 |
switch p.peek() { |
| 924 |
case '$', '*': |
| 925 |
// Skip prefix. |
| 926 |
p.next() |
| 927 |
} |
| 928 |
|
| 929 |
tok := p.next() |
| 930 |
name := tok.String() |
| 931 |
if tok.ScanToken != scanner.Ident || p.atStartOfRegister(name) { |
| 932 |
return "", obj.ABI0, false |
| 933 |
} |
| 934 |
name = p.qualifySymbol(name) |
| 935 |
// Parse optional <> (indicates a static symbol) or |
| 936 |
// <ABIxxx> (selecting text symbol with specific ABI). |
| 937 |
noErrMsg := false |
| 938 |
isStatic, abi := p.symRefAttrs(name, noErrMsg) |
| 939 |
if isStatic { |
| 940 |
return "", obj.ABI0, false // This function rejects static symbols. |
| 941 |
} |
| 942 |
tok = p.next() |
| 943 |
if tok.ScanToken == '+' { |
| 944 |
if p.next().ScanToken != scanner.Int { |
| 945 |
return "", obj.ABI0, false |
| 946 |
} |
| 947 |
tok = p.next() |
| 948 |
} |
| 949 |
if tok.ScanToken != '(' { |
| 950 |
return "", obj.ABI0, false |
| 951 |
} |
| 952 |
if reg := p.next(); reg.ScanToken != scanner.Ident || reg.String() != "SB" { |
| 953 |
return "", obj.ABI0, false |
| 954 |
} |
| 955 |
if p.next().ScanToken != ')' || p.peek() != scanner.EOF { |
| 956 |
return "", obj.ABI0, false |
| 957 |
} |
| 958 |
return name, abi, true |
| 959 |
} |
| 960 |
|
| 961 |
// registerIndirect parses the general form of a register indirection. |
| 962 |
// It can be (R1), (R2*scale), (R1)(R2*scale), (R1)(R2.SXTX<<3) or (R1)(R2<<3) |
| 963 |
// where R1 may be a simple register or register pair R:R or (R, R) or (R+R). |
| 964 |
// Or it might be a pseudo-indirection like (FP). |
| 965 |
// We are sitting on the opening parenthesis. |
| 966 |
func (p *Parser) registerIndirect(a *obj.Addr, prefix rune) { |
| 967 |
p.get('(') |
| 968 |
tok := p.next() |
| 969 |
name := tok.String() |
| 970 |
r1, r2, scale, ok := p.register(name, 0) |
| 971 |
if !ok { |
| 972 |
p.errorf("indirect through non-register %s", tok) |
| 973 |
} |
| 974 |
p.get(')') |
| 975 |
a.Type = obj.TYPE_MEM |
| 976 |
if r1 < 0 { |
| 977 |
// Pseudo-register reference. |
| 978 |
if r2 != 0 { |
| 979 |
p.errorf("cannot use pseudo-register in pair") |
| 980 |
return |
| 981 |
} |
| 982 |
// For SB, SP, and FP, there must be a name here. 0(FP) is not legal. |
| 983 |
if name != "PC" && a.Name == obj.NAME_NONE { |
| 984 |
p.errorf("cannot reference %s without a symbol", name) |
| 985 |
} |
| 986 |
p.setPseudoRegister(a, name, false, prefix) |
| 987 |
return |
| 988 |
} |
| 989 |
a.Reg = r1 |
| 990 |
if r2 != 0 { |
| 991 |
// TODO: Consistency in the encoding would be nice here. |
| 992 |
if p.arch.InFamily(sys.ARM, sys.ARM64) { |
| 993 |
// Special form |
| 994 |
// ARM: destination register pair (R1, R2). |
| 995 |
// ARM64: register pair (R1, R2) for LDP/STP. |
| 996 |
if prefix != 0 || scale != 0 { |
| 997 |
p.errorf("illegal address mode for register pair") |
| 998 |
return |
| 999 |
} |
| 1000 |
a.Type = obj.TYPE_REGREG |
| 1001 |
a.Offset = int64(r2) |
| 1002 |
// Nothing may follow |
| 1003 |
return |
| 1004 |
} |
| 1005 |
if p.arch.Family == sys.PPC64 { |
| 1006 |
// Special form for PPC64: (R1+R2); alias for (R1)(R2). |
| 1007 |
if prefix != 0 || scale != 0 { |
| 1008 |
p.errorf("illegal address mode for register+register") |
| 1009 |
return |
| 1010 |
} |
| 1011 |
a.Type = obj.TYPE_MEM |
| 1012 |
a.Scale = 0 |
| 1013 |
a.Index = r2 |
| 1014 |
// Nothing may follow. |
| 1015 |
return |
| 1016 |
} |
| 1017 |
} |
| 1018 |
if r2 != 0 { |
| 1019 |
p.errorf("indirect through register pair") |
| 1020 |
} |
| 1021 |
if prefix == '$' { |
| 1022 |
a.Type = obj.TYPE_ADDR |
| 1023 |
} |
| 1024 |
if r1 == arch.RPC && prefix != 0 { |
| 1025 |
p.errorf("illegal addressing mode for PC") |
| 1026 |
} |
| 1027 |
if scale == 0 && p.peek() == '(' { |
| 1028 |
// General form (R)(R*scale). |
| 1029 |
p.next() |
| 1030 |
tok := p.next() |
| 1031 |
if p.atRegisterExtension() { |
| 1032 |
p.registerExtension(a, tok.String(), prefix) |
| 1033 |
} else if p.atRegisterShift() { |
| 1034 |
// (R1)(R2<<3) |
| 1035 |
p.registerExtension(a, tok.String(), prefix) |
| 1036 |
} else { |
| 1037 |
r1, r2, scale, ok = p.register(tok.String(), 0) |
| 1038 |
if !ok { |
| 1039 |
p.errorf("indirect through non-register %s", tok) |
| 1040 |
} |
| 1041 |
if r2 != 0 { |
| 1042 |
p.errorf("unimplemented two-register form") |
| 1043 |
} |
| 1044 |
a.Index = r1 |
| 1045 |
if scale != 0 && scale != 1 && (p.arch.Family == sys.ARM64 || |
| 1046 |
p.arch.Family == sys.PPC64) { |
| 1047 |
// Support (R1)(R2) (no scaling) and (R1)(R2*1). |
| 1048 |
p.errorf("%s doesn't support scaled register format", p.arch.Name) |
| 1049 |
} else { |
| 1050 |
a.Scale = int16(scale) |
| 1051 |
} |
| 1052 |
} |
| 1053 |
p.get(')') |
| 1054 |
} else if scale != 0 { |
| 1055 |
if p.arch.Family == sys.ARM64 { |
| 1056 |
p.errorf("arm64 doesn't support scaled register format") |
| 1057 |
} |
| 1058 |
// First (R) was missing, all we have is (R*scale). |
| 1059 |
a.Reg = 0 |
| 1060 |
a.Index = r1 |
| 1061 |
a.Scale = int16(scale) |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
// registerList parses an ARM or ARM64 register list expression, a list of |
| 1066 |
// registers in []. There may be comma-separated ranges or individual |
| 1067 |
// registers, as in [R1,R3-R5] or [V1.S4, V2.S4, V3.S4, V4.S4]. |
| 1068 |
// For ARM, only R0 through R15 may appear. |
| 1069 |
// For ARM64, V0 through V31 with arrangement may appear. |
| 1070 |
// |
| 1071 |
// For 386/AMD64 register list specifies 4VNNIW-style multi-source operand. |
| 1072 |
// For range of 4 elements, Intel manual uses "+3" notation, for example: |
| 1073 |
// |
| 1074 |
// VP4DPWSSDS zmm1{k1}{z}, zmm2+3, m128 |
| 1075 |
// |
| 1076 |
// Given asm line: |
| 1077 |
// |
| 1078 |
// VP4DPWSSDS Z5, [Z10-Z13], (AX) |
| 1079 |
// |
| 1080 |
// zmm2 is Z10, and Z13 is the only valid value for it (Z10+3). |
| 1081 |
// Only simple ranges are accepted, like [Z0-Z3]. |
| 1082 |
// |
| 1083 |
// The opening bracket has been consumed. |
| 1084 |
func (p *Parser) registerList(a *obj.Addr) { |
| 1085 |
if p.arch.InFamily(sys.I386, sys.AMD64) { |
| 1086 |
p.registerListX86(a) |
| 1087 |
} else { |
| 1088 |
p.registerListARM(a) |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
func (p *Parser) registerListARM(a *obj.Addr) { |
| 1093 |
// One range per loop. |
| 1094 |
var maxReg int |
| 1095 |
var bits uint16 |
| 1096 |
var arrangement int64 |
| 1097 |
switch p.arch.Family { |
| 1098 |
case sys.ARM: |
| 1099 |
maxReg = 16 |
| 1100 |
case sys.ARM64: |
| 1101 |
maxReg = 32 |
| 1102 |
default: |
| 1103 |
p.errorf("unexpected register list") |
| 1104 |
} |
| 1105 |
firstReg := -1 |
| 1106 |
nextReg := -1 |
| 1107 |
regCnt := 0 |
| 1108 |
ListLoop: |
| 1109 |
for { |
| 1110 |
tok := p.next() |
| 1111 |
switch tok.ScanToken { |
| 1112 |
case ']': |
| 1113 |
break ListLoop |
| 1114 |
case scanner.EOF: |
| 1115 |
p.errorf("missing ']' in register list") |
| 1116 |
return |
| 1117 |
} |
| 1118 |
switch p.arch.Family { |
| 1119 |
case sys.ARM64: |
| 1120 |
// Vn.T |
| 1121 |
name := tok.String() |
| 1122 |
r, ok := p.registerReference(name) |
| 1123 |
if !ok { |
| 1124 |
p.errorf("invalid register: %s", name) |
| 1125 |
} |
| 1126 |
reg := r - p.arch.Register["V0"] |
| 1127 |
p.get('.') |
| 1128 |
tok := p.next() |
| 1129 |
ext := tok.String() |
| 1130 |
curArrangement, err := arch.ARM64RegisterArrangement(reg, name, ext) |
| 1131 |
if err != nil { |
| 1132 |
p.errorf("%v", err) |
| 1133 |
} |
| 1134 |
if firstReg == -1 { |
| 1135 |
// only record the first register and arrangement |
| 1136 |
firstReg = int(reg) |
| 1137 |
nextReg = firstReg |
| 1138 |
arrangement = curArrangement |
| 1139 |
} else if curArrangement != arrangement { |
| 1140 |
p.errorf("inconsistent arrangement in ARM64 register list") |
| 1141 |
} else if nextReg != int(reg) { |
| 1142 |
p.errorf("incontiguous register in ARM64 register list: %s", name) |
| 1143 |
} |
| 1144 |
regCnt++ |
| 1145 |
nextReg = (nextReg + 1) % 32 |
| 1146 |
case sys.ARM: |
| 1147 |
// Parse the upper and lower bounds. |
| 1148 |
lo := p.registerNumber(tok.String()) |
| 1149 |
hi := lo |
| 1150 |
if p.peek() == '-' { |
| 1151 |
p.next() |
| 1152 |
hi = p.registerNumber(p.next().String()) |
| 1153 |
} |
| 1154 |
if hi < lo { |
| 1155 |
lo, hi = hi, lo |
| 1156 |
} |
| 1157 |
// Check there are no duplicates in the register list. |
| 1158 |
for i := 0; lo <= hi && i < maxReg; i++ { |
| 1159 |
if bits&(1<<lo) != 0 { |
| 1160 |
p.errorf("register R%d already in list", lo) |
| 1161 |
} |
| 1162 |
bits |= 1 << lo |
| 1163 |
lo++ |
| 1164 |
} |
| 1165 |
default: |
| 1166 |
p.errorf("unexpected register list") |
| 1167 |
} |
| 1168 |
if p.peek() != ']' { |
| 1169 |
p.get(',') |
| 1170 |
} |
| 1171 |
} |
| 1172 |
a.Type = obj.TYPE_REGLIST |
| 1173 |
switch p.arch.Family { |
| 1174 |
case sys.ARM: |
| 1175 |
a.Offset = int64(bits) |
| 1176 |
case sys.ARM64: |
| 1177 |
offset, err := arch.ARM64RegisterListOffset(firstReg, regCnt, arrangement) |
| 1178 |
if err != nil { |
| 1179 |
p.errorf("%v", err) |
| 1180 |
} |
| 1181 |
a.Offset = offset |
| 1182 |
default: |
| 1183 |
p.errorf("register list not supported on this architecture") |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
func (p *Parser) registerListX86(a *obj.Addr) { |
| 1188 |
// Accept only [RegA-RegB] syntax. |
| 1189 |
// Don't use p.get() to provide better error messages. |
| 1190 |
|
| 1191 |
loName := p.next().String() |
| 1192 |
lo, ok := p.arch.Register[loName] |
| 1193 |
if !ok { |
| 1194 |
if loName == "EOF" { |
| 1195 |
p.errorf("register list: expected ']', found EOF") |
| 1196 |
} else { |
| 1197 |
p.errorf("register list: bad low register in `[%s`", loName) |
| 1198 |
} |
| 1199 |
return |
| 1200 |
} |
| 1201 |
if tok := p.next().ScanToken; tok != '-' { |
| 1202 |
p.errorf("register list: expected '-' after `[%s`, found %s", loName, tok) |
| 1203 |
return |
| 1204 |
} |
| 1205 |
hiName := p.next().String() |
| 1206 |
hi, ok := p.arch.Register[hiName] |
| 1207 |
if !ok { |
| 1208 |
p.errorf("register list: bad high register in `[%s-%s`", loName, hiName) |
| 1209 |
return |
| 1210 |
} |
| 1211 |
if tok := p.next().ScanToken; tok != ']' { |
| 1212 |
p.errorf("register list: expected ']' after `[%s-%s`, found %s", loName, hiName, tok) |
| 1213 |
} |
| 1214 |
|
| 1215 |
a.Type = obj.TYPE_REGLIST |
| 1216 |
a.Reg = lo |
| 1217 |
a.Offset = x86.EncodeRegisterRange(lo, hi) |
| 1218 |
} |
| 1219 |
|
| 1220 |
// registerNumber is ARM-specific. It returns the number of the specified register. |
| 1221 |
func (p *Parser) registerNumber(name string) uint16 { |
| 1222 |
if p.arch.Family == sys.ARM && name == "g" { |
| 1223 |
return 10 |
| 1224 |
} |
| 1225 |
if name[0] != 'R' { |
| 1226 |
p.errorf("expected g or R0 through R15; found %s", name) |
| 1227 |
return 0 |
| 1228 |
} |
| 1229 |
r, ok := p.registerReference(name) |
| 1230 |
if !ok { |
| 1231 |
return 0 |
| 1232 |
} |
| 1233 |
reg := r - p.arch.Register["R0"] |
| 1234 |
if reg < 0 { |
| 1235 |
// Could happen for an architecture having other registers prefixed by R |
| 1236 |
p.errorf("expected g or R0 through R15; found %s", name) |
| 1237 |
return 0 |
| 1238 |
} |
| 1239 |
return uint16(reg) |
| 1240 |
} |
| 1241 |
|
| 1242 |
// Note: There are two changes in the expression handling here |
| 1243 |
// compared to the old yacc/C implementations. Neither has |
| 1244 |
// much practical consequence because the expressions we |
| 1245 |
// see in assembly code are simple, but for the record: |
| 1246 |
// |
| 1247 |
// 1) Evaluation uses uint64; the old one used int64. |
| 1248 |
// 2) Precedence uses Go rules not C rules. |
| 1249 |
|
| 1250 |
// expr = term | term ('+' | '-' | '|' | '^') term. |
| 1251 |
func (p *Parser) expr() uint64 { |
| 1252 |
value := p.term() |
| 1253 |
for { |
| 1254 |
switch p.peek() { |
| 1255 |
case '+': |
| 1256 |
p.next() |
| 1257 |
value += p.term() |
| 1258 |
case '-': |
| 1259 |
p.next() |
| 1260 |
value -= p.term() |
| 1261 |
case '|': |
| 1262 |
p.next() |
| 1263 |
value |= p.term() |
| 1264 |
case '^': |
| 1265 |
p.next() |
| 1266 |
value ^= p.term() |
| 1267 |
default: |
| 1268 |
return value |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
// floatExpr = fconst | '-' floatExpr | '+' floatExpr | '(' floatExpr ')' |
| 1274 |
func (p *Parser) floatExpr() float64 { |
| 1275 |
tok := p.next() |
| 1276 |
switch tok.ScanToken { |
| 1277 |
case '(': |
| 1278 |
v := p.floatExpr() |
| 1279 |
if p.next().ScanToken != ')' { |
| 1280 |
p.errorf("missing closing paren") |
| 1281 |
} |
| 1282 |
return v |
| 1283 |
case '+': |
| 1284 |
return +p.floatExpr() |
| 1285 |
case '-': |
| 1286 |
return -p.floatExpr() |
| 1287 |
case scanner.Float: |
| 1288 |
return p.atof(tok.String()) |
| 1289 |
} |
| 1290 |
p.errorf("unexpected %s evaluating float expression", tok) |
| 1291 |
return 0 |
| 1292 |
} |
| 1293 |
|
| 1294 |
// term = factor | factor ('*' | '/' | '%' | '>>' | '<<' | '&') factor |
| 1295 |
func (p *Parser) term() uint64 { |
| 1296 |
value := p.factor() |
| 1297 |
for { |
| 1298 |
switch p.peek() { |
| 1299 |
case '*': |
| 1300 |
p.next() |
| 1301 |
value *= p.factor() |
| 1302 |
case '/': |
| 1303 |
p.next() |
| 1304 |
if int64(value) < 0 { |
| 1305 |
p.errorf("divide of value with high bit set") |
| 1306 |
} |
| 1307 |
divisor := p.factor() |
| 1308 |
if divisor == 0 { |
| 1309 |
p.errorf("division by zero") |
| 1310 |
} else { |
| 1311 |
value /= divisor |
| 1312 |
} |
| 1313 |
case '%': |
| 1314 |
p.next() |
| 1315 |
divisor := p.factor() |
| 1316 |
if int64(value) < 0 { |
| 1317 |
p.errorf("modulo of value with high bit set") |
| 1318 |
} |
| 1319 |
if divisor == 0 { |
| 1320 |
p.errorf("modulo by zero") |
| 1321 |
} else { |
| 1322 |
value %= divisor |
| 1323 |
} |
| 1324 |
case lex.LSH: |
| 1325 |
p.next() |
| 1326 |
shift := p.factor() |
| 1327 |
if int64(shift) < 0 { |
| 1328 |
p.errorf("negative left shift count") |
| 1329 |
} |
| 1330 |
return value << shift |
| 1331 |
case lex.RSH: |
| 1332 |
p.next() |
| 1333 |
shift := p.term() |
| 1334 |
if int64(shift) < 0 { |
| 1335 |
p.errorf("negative right shift count") |
| 1336 |
} |
| 1337 |
if int64(value) < 0 { |
| 1338 |
p.errorf("right shift of value with high bit set") |
| 1339 |
} |
| 1340 |
value >>= shift |
| 1341 |
case '&': |
| 1342 |
p.next() |
| 1343 |
value &= p.factor() |
| 1344 |
default: |
| 1345 |
return value |
| 1346 |
} |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
// factor = const | '+' factor | '-' factor | '~' factor | '(' expr ')' |
| 1351 |
func (p *Parser) factor() uint64 { |
| 1352 |
tok := p.next() |
| 1353 |
switch tok.ScanToken { |
| 1354 |
case scanner.Int: |
| 1355 |
return p.atoi(tok.String()) |
| 1356 |
case scanner.Char: |
| 1357 |
str, err := strconv.Unquote(tok.String()) |
| 1358 |
if err != nil { |
| 1359 |
p.errorf("%s", err) |
| 1360 |
} |
| 1361 |
r, w := utf8.DecodeRuneInString(str) |
| 1362 |
if w == 1 && r == utf8.RuneError { |
| 1363 |
p.errorf("illegal UTF-8 encoding for character constant") |
| 1364 |
} |
| 1365 |
return uint64(r) |
| 1366 |
case '+': |
| 1367 |
return +p.factor() |
| 1368 |
case '-': |
| 1369 |
return -p.factor() |
| 1370 |
case '~': |
| 1371 |
return ^p.factor() |
| 1372 |
case '(': |
| 1373 |
v := p.expr() |
| 1374 |
if p.next().ScanToken != ')' { |
| 1375 |
p.errorf("missing closing paren") |
| 1376 |
} |
| 1377 |
return v |
| 1378 |
} |
| 1379 |
p.errorf("unexpected %s evaluating expression", tok) |
| 1380 |
return 0 |
| 1381 |
} |
| 1382 |
|
| 1383 |
// positiveAtoi returns an int64 that must be >= 0. |
| 1384 |
func (p *Parser) positiveAtoi(str string) int64 { |
| 1385 |
value, err := strconv.ParseInt(str, 0, 64) |
| 1386 |
if err != nil { |
| 1387 |
p.errorf("%s", err) |
| 1388 |
} |
| 1389 |
if value < 0 { |
| 1390 |
p.errorf("%s overflows int64", str) |
| 1391 |
} |
| 1392 |
return value |
| 1393 |
} |
| 1394 |
|
| 1395 |
func (p *Parser) atoi(str string) uint64 { |
| 1396 |
value, err := strconv.ParseUint(str, 0, 64) |
| 1397 |
if err != nil { |
| 1398 |
p.errorf("%s", err) |
| 1399 |
} |
| 1400 |
return value |
| 1401 |
} |
| 1402 |
|
| 1403 |
func (p *Parser) atof(str string) float64 { |
| 1404 |
value, err := strconv.ParseFloat(str, 64) |
| 1405 |
if err != nil { |
| 1406 |
p.errorf("%s", err) |
| 1407 |
} |
| 1408 |
return value |
| 1409 |
} |
| 1410 |
|
| 1411 |
// EOF represents the end of input. |
| 1412 |
var EOF = lex.Make(scanner.EOF, "EOF") |
| 1413 |
|
| 1414 |
func (p *Parser) next() lex.Token { |
| 1415 |
if !p.more() { |
| 1416 |
return EOF |
| 1417 |
} |
| 1418 |
tok := p.input[p.inputPos] |
| 1419 |
p.inputPos++ |
| 1420 |
return tok |
| 1421 |
} |
| 1422 |
|
| 1423 |
func (p *Parser) back() { |
| 1424 |
if p.inputPos == 0 { |
| 1425 |
p.errorf("internal error: backing up before BOL") |
| 1426 |
} else { |
| 1427 |
p.inputPos-- |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
func (p *Parser) peek() lex.ScanToken { |
| 1432 |
if p.more() { |
| 1433 |
return p.input[p.inputPos].ScanToken |
| 1434 |
} |
| 1435 |
return scanner.EOF |
| 1436 |
} |
| 1437 |
|
| 1438 |
func (p *Parser) more() bool { |
| 1439 |
return p.inputPos < len(p.input) |
| 1440 |
} |
| 1441 |
|
| 1442 |
// get verifies that the next item has the expected type and returns it. |
| 1443 |
func (p *Parser) get(expected lex.ScanToken) lex.Token { |
| 1444 |
p.expect(expected, expected.String()) |
| 1445 |
return p.next() |
| 1446 |
} |
| 1447 |
|
| 1448 |
// expectOperandEnd verifies that the parsing state is properly at the end of an operand. |
| 1449 |
func (p *Parser) expectOperandEnd() { |
| 1450 |
p.expect(scanner.EOF, "end of operand") |
| 1451 |
} |
| 1452 |
|
| 1453 |
// expect verifies that the next item has the expected type. It does not consume it. |
| 1454 |
func (p *Parser) expect(expectedToken lex.ScanToken, expectedMessage string) { |
| 1455 |
if p.peek() != expectedToken { |
| 1456 |
p.errorf("expected %s, found %s", expectedMessage, p.next()) |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
// have reports whether the remaining tokens (including the current one) contain the specified token. |
| 1461 |
func (p *Parser) have(token lex.ScanToken) bool { |
| 1462 |
for i := p.inputPos; i < len(p.input); i++ { |
| 1463 |
if p.input[i].ScanToken == token { |
| 1464 |
return true |
| 1465 |
} |
| 1466 |
} |
| 1467 |
return false |
| 1468 |
} |
| 1469 |
|
| 1470 |
// at reports whether the next tokens are as requested. |
| 1471 |
func (p *Parser) at(next ...lex.ScanToken) bool { |
| 1472 |
if len(p.input)-p.inputPos < len(next) { |
| 1473 |
return false |
| 1474 |
} |
| 1475 |
for i, r := range next { |
| 1476 |
if p.input[p.inputPos+i].ScanToken != r { |
| 1477 |
return false |
| 1478 |
} |
| 1479 |
} |
| 1480 |
return true |
| 1481 |
} |