mirror of
https://github.com/Xeeynamo/sotn-decomp.git
synced 2024-11-23 04:59:41 +00:00
Purge ASPATCH
This commit is contained in:
parent
85218f3adb
commit
bed586665a
4
Makefile
4
Makefile
@ -59,7 +59,6 @@ MASPSX_APP := $(MASPSX_DIR)/maspsx.py
|
||||
MASPSX := $(PYTHON) $(MASPSX_APP) --no-macro-inc --expand-div
|
||||
GO := $(HOME)/go/bin/go
|
||||
GOPATH := $(HOME)/go
|
||||
ASPATCH := $(GOPATH)/bin/aspatch
|
||||
SOTNDISK := $(GOPATH)/bin/sotn-disk
|
||||
GFXSTAGE := $(PYTHON) $(TOOLS_DIR)/gfxstage.py
|
||||
SATURN_SPLITTER_DIR := $(TOOLS_DIR)/saturn-splitter
|
||||
@ -364,7 +363,6 @@ disk: build $(SOTNDISK)
|
||||
|
||||
update-dependencies: $(SPLAT_APP) $(ASMDIFFER_APP) $(M2CTX_APP) $(M2C_APP) $(GO)
|
||||
pip3 install -r $(TOOLS_DIR)/requirements-python.txt
|
||||
$(GO) install github.com/xeeynamo/sotn-decomp/tools/aspatch@latest
|
||||
$(GO) install github.com/xeeynamo/sotn-decomp/tools/gfxsotn@latest
|
||||
$(GO) install github.com/xeeynamo/sotn-decomp/tools/sotn-disk@latest
|
||||
git clean -fd bin/
|
||||
@ -394,8 +392,6 @@ $(GO):
|
||||
curl -L -o go1.19.7.linux-amd64.tar.gz https://go.dev/dl/go1.19.7.linux-amd64.tar.gz
|
||||
tar -C $(HOME) -xzf go1.19.7.linux-amd64.tar.gz
|
||||
rm go1.19.7.linux-amd64.tar.gz
|
||||
$(ASPATCH): $(GO)
|
||||
$(GO) install github.com/xeeynamo/sotn-decomp/tools/aspatch@latest
|
||||
$(SOTNDISK): $(GO)
|
||||
$(GO) install github.com/xeeynamo/sotn-decomp/tools/sotn-disk@latest
|
||||
|
||||
|
2
go.work
2
go.work
@ -1,7 +1,5 @@
|
||||
go 1.19
|
||||
|
||||
use ./tools/aspatch
|
||||
|
||||
use ./tools/gfxsotn
|
||||
|
||||
use ./tools/sotn-disk
|
||||
|
@ -1,26 +0,0 @@
|
||||
module github.com/xeeynamo/sotn-decomp/tools/aspatch
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/stretchr/testify v1.8.1
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/go-cmp v0.5.8 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sergi/go-diff v1.1.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
|
||||
golang.org/x/exp/typeparams v0.0.0-20220722155223-a9213eeb770e // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/tools v0.1.13-0.20220908144252-ce397412b6a4 // indirect
|
||||
golang.org/x/tools/gopls v0.9.5 // indirect
|
||||
golang.org/x/vuln v0.0.0-20220901221904-62b0186a1058 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
honnef.co/go/tools v0.3.2 // indirect
|
||||
mvdan.cc/gofumpt v0.3.1 // indirect
|
||||
mvdan.cc/xurls/v2 v2.4.0 // indirect
|
||||
)
|
@ -1,59 +0,0 @@
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
|
||||
golang.org/x/exp/typeparams v0.0.0-20220722155223-a9213eeb770e h1:7Xs2YCOpMlNqSQSmrrnhlzBXIE/bpMecZplbLePTJvE=
|
||||
golang.org/x/exp/typeparams v0.0.0-20220722155223-a9213eeb770e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.1.13-0.20220908144252-ce397412b6a4 h1:glzimF7qHZuKVEiMbE7UqBu44MyTjt5u6j3Jz+rfMRM=
|
||||
golang.org/x/tools v0.1.13-0.20220908144252-ce397412b6a4/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools/gopls v0.9.5 h1:F3TO5th6TUg40FHrpxZfMkMlVaaTmByeEvylgVHGH2A=
|
||||
golang.org/x/tools/gopls v0.9.5/go.mod h1:lEy09TNKBzkKV0Yhr3fZlMxs+oLgWek+ADmJgS+NOEQ=
|
||||
golang.org/x/vuln v0.0.0-20220901221904-62b0186a1058 h1:YnB27EXBD8XxB0JcaOeluuvhF2kS4DrH0k+lpopG2xc=
|
||||
golang.org/x/vuln v0.0.0-20220901221904-62b0186a1058/go.mod h1:7tDfEDtOLlzHQRi4Yzfg5seVBSvouUIjyPzBx4q5CxQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.3.2 h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34=
|
||||
honnef.co/go/tools v0.3.2/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw=
|
||||
mvdan.cc/gofumpt v0.3.1 h1:avhhrOmv0IuvQVK7fvwV91oFSGAk5/6Po8GXTzICeu8=
|
||||
mvdan.cc/gofumpt v0.3.1/go.mod h1:w3ymliuxvzVx8DAutBnVyDqYb1Niy/yCJt/lk821YCE=
|
||||
mvdan.cc/xurls/v2 v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
|
||||
mvdan.cc/xurls/v2 v2.4.0/go.mod h1:+GEjq9uNjqs8LQfM9nVnM8rff0OQ5Iash5rzX+N1CSg=
|
@ -1,249 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type patcher struct {
|
||||
r *bufio.Reader
|
||||
w *bufio.Writer
|
||||
nLine int
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := initPatcher(os.Stdin, os.Stdout).patch()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initPatcher(r io.Reader, w io.Writer) *patcher {
|
||||
return &patcher{
|
||||
r: bufio.NewReader(r),
|
||||
w: bufio.NewWriter(w),
|
||||
nLine: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *patcher) readLine() (string, error) {
|
||||
line, err := p.r.ReadString('\n')
|
||||
p.nLine += 1
|
||||
if err == io.EOF {
|
||||
err = p.w.Flush()
|
||||
if err == nil {
|
||||
return "", io.EOF
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func (p *patcher) write(str string) {
|
||||
p.w.WriteString(str)
|
||||
}
|
||||
|
||||
func (p *patcher) makeError(err error) error {
|
||||
return fmt.Errorf("line %d: %s", p.nLine, err.Error())
|
||||
}
|
||||
|
||||
func (p *patcher) patch() error {
|
||||
for {
|
||||
line, err := p.readLine()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = p.patchLine(line)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return p.makeError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *patcher) isLabel(tokens []string) bool {
|
||||
return len(tokens) >= 2 && tokens[0] == "$" && tokens[2] == ":"
|
||||
}
|
||||
|
||||
func (p *patcher) patchLine(line string) error {
|
||||
tokens, err := tokenize(line)
|
||||
if err != nil {
|
||||
return p.makeError(err)
|
||||
}
|
||||
|
||||
if len(tokens) == 0 {
|
||||
p.write(line) // passtrough comments
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tokens[0] {
|
||||
case "lb":
|
||||
fallthrough
|
||||
case "lbu":
|
||||
fallthrough
|
||||
case "lh":
|
||||
fallthrough
|
||||
case "lhu":
|
||||
fallthrough
|
||||
case "lw":
|
||||
switch len(tokens) {
|
||||
case 5: // variant without pointer
|
||||
p.write(line)
|
||||
case 6: // variant lw $2,$LC2
|
||||
p.write(line)
|
||||
case 8: // variant lw $2,$LC2+4
|
||||
p.write(line)
|
||||
case 9: // variant with pointer
|
||||
if tokens[3] != "," || tokens[5] != "(" || tokens[8] != ")" {
|
||||
return fmt.Errorf("unable to parse '%s': token keys not recognised", line)
|
||||
}
|
||||
|
||||
op := tokens[0]
|
||||
dstReg := tokens[1] + tokens[2]
|
||||
addr := tokens[4]
|
||||
ptrReg := tokens[6] + tokens[7]
|
||||
|
||||
iAddr, err := strconv.Atoi(addr) // check if it's a digit or identifier
|
||||
if err != nil || iAddr > 0x10000 {
|
||||
p.write("\t.set\tnoat\n")
|
||||
p.write(fmt.Sprintf("\tlui\t$1,%%hi(%s)\n", addr))
|
||||
p.write(fmt.Sprintf("\taddu\t$1,$1,%s\n", ptrReg))
|
||||
p.write(fmt.Sprintf("\t%s\t%s,%%lo(%s)($1)\n", op, dstReg, addr))
|
||||
p.write("\t.set\tat\n")
|
||||
} else {
|
||||
// fallback
|
||||
p.write(line)
|
||||
}
|
||||
case 10: // variant with label
|
||||
p.write(line)
|
||||
default:
|
||||
return fmt.Errorf("unable to parse '%s': len(tokens)=%d", line, len(tokens))
|
||||
}
|
||||
case "rem":
|
||||
srcReg1 := tokens[4] + tokens[5]
|
||||
srcReg2 := tokens[7] + tokens[8]
|
||||
dstReg := tokens[1] + tokens[2]
|
||||
p.write("\t.set\tnoreorder\n")
|
||||
p.write("\t.set\tnomacro\n")
|
||||
p.write("\t.set\tnoat\n")
|
||||
p.write(fmt.Sprintf("\tdiv\t$0,%s,%s\n", srcReg1, srcReg2))
|
||||
p.write(fmt.Sprintf("\tbnez\t%s,.L_NOT_DIV_BY_ZERO_%d\n", srcReg2, p.nLine))
|
||||
p.write("\tnop\n")
|
||||
p.write("\tbreak\t0x7\n")
|
||||
p.write(fmt.Sprintf(".L_NOT_DIV_BY_ZERO_%d:\n", p.nLine))
|
||||
p.write("\taddiu\t$1,$0,-1\n")
|
||||
p.write(fmt.Sprintf("\tbne\t%s,$1,.L_DIV_BY_POSITIVE_SIGN_%d\n", srcReg2, p.nLine))
|
||||
p.write("\tlui\t$1,0x8000\n")
|
||||
p.write(fmt.Sprintf("\tbne\t%s,$1,.L_DIV_BY_POSITIVE_SIGN_%d\n", srcReg1, p.nLine))
|
||||
p.write("\tnop\n")
|
||||
p.write("\tbreak\t0x6\n")
|
||||
p.write(fmt.Sprintf(".L_DIV_BY_POSITIVE_SIGN_%d:\n", p.nLine))
|
||||
p.write(fmt.Sprintf("\tmfhi\t%s\n", dstReg))
|
||||
p.write("\t.set\tat\n")
|
||||
p.write("\t.set\tmacro\n")
|
||||
p.write("\t.set\treorder\n")
|
||||
case "div":
|
||||
srcReg1 := tokens[4] + tokens[5]
|
||||
srcReg2 := tokens[7] + tokens[8]
|
||||
dstReg := tokens[1] + tokens[2]
|
||||
p.write("\t.set\tnoreorder\n")
|
||||
p.write("\t.set\tnomacro\n")
|
||||
p.write("\t.set\tnoat\n")
|
||||
p.write(fmt.Sprintf("\tdiv\t$0,%s,%s\n", srcReg1, srcReg2))
|
||||
p.write(fmt.Sprintf("\tbnez\t%s,.L_NOT_DIV_BY_ZERO_%d\n", srcReg2, p.nLine))
|
||||
p.write("\tnop\n")
|
||||
p.write("\tbreak\t0x7\n")
|
||||
p.write(fmt.Sprintf(".L_NOT_DIV_BY_ZERO_%d:\n", p.nLine))
|
||||
p.write("\taddiu\t$1,$0,-1\n")
|
||||
p.write(fmt.Sprintf("\tbne\t%s,$1,.L_DIV_BY_POSITIVE_SIGN_%d\n", srcReg2, p.nLine))
|
||||
p.write("\tlui\t$1,0x8000\n")
|
||||
p.write(fmt.Sprintf("\tbne\t%s,$1,.L_DIV_BY_POSITIVE_SIGN_%d\n", srcReg1, p.nLine))
|
||||
p.write("\tnop\n")
|
||||
p.write("\tbreak\t0x6\n")
|
||||
p.write(fmt.Sprintf(".L_DIV_BY_POSITIVE_SIGN_%d:\n", p.nLine))
|
||||
p.write(fmt.Sprintf("\tmflo\t%s\n", dstReg))
|
||||
p.write("\t.set\tat\n")
|
||||
p.write("\t.set\tmacro\n")
|
||||
p.write("\t.set\treorder\n")
|
||||
case "divu":
|
||||
srcReg1 := tokens[4] + tokens[5]
|
||||
srcReg2 := tokens[7] + tokens[8]
|
||||
dstReg := tokens[1] + tokens[2]
|
||||
p.write("\t.set\tnoat\n")
|
||||
p.write(fmt.Sprintf("\tdivu\t$0,%s,%s\n", srcReg1, srcReg2))
|
||||
p.write(fmt.Sprintf("\tbnez\t%s,.L_NOT_DIV_BY_ZERO_%d\n", srcReg2, p.nLine))
|
||||
p.write("\tbreak\t0x7\n")
|
||||
p.write(fmt.Sprintf(".L_NOT_DIV_BY_ZERO_%d:\n", p.nLine))
|
||||
p.write(fmt.Sprintf("\tmflo\t%s\n", dstReg))
|
||||
p.write("\t.set\tat\n")
|
||||
default:
|
||||
p.write(line)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tokenize(line string) ([]string, error) {
|
||||
var tokens []string
|
||||
var token string
|
||||
r := strings.NewReader(line)
|
||||
|
||||
flush := func() {
|
||||
if token != "" {
|
||||
tokens = append(tokens, token)
|
||||
token = ""
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
ch, _, err := r.ReadRune()
|
||||
if err != nil {
|
||||
flush()
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case ch == ' ' || ch == '\t' || ch == '\n':
|
||||
flush()
|
||||
case unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '_' || ch == '+' || ch == '-':
|
||||
token += string(ch)
|
||||
case ch == '"':
|
||||
flush()
|
||||
token += string(ch)
|
||||
for {
|
||||
ch, _, err = r.ReadRune()
|
||||
if err != nil {
|
||||
return tokens, fmt.Errorf("unexpected end of string")
|
||||
}
|
||||
token += string(ch)
|
||||
if ch == '"' {
|
||||
break
|
||||
}
|
||||
}
|
||||
case ch == '$', ch == '(', ch == ')', ch == ':', ch == '.', ch == ',':
|
||||
flush()
|
||||
tokens = append(tokens, string(ch))
|
||||
case ch == '#':
|
||||
flush()
|
||||
return tokens, nil
|
||||
default:
|
||||
return tokens, fmt.Errorf("unrecognized character '%s'", string(ch))
|
||||
}
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIgnorePatch(t *testing.T) {
|
||||
// Essentially test the patcher does not break on silly stuff
|
||||
for _, tcase := range []string{
|
||||
"foo\n",
|
||||
"\"a string\"\n",
|
||||
"\t.someStuff\n",
|
||||
"#comment\n",
|
||||
"\tlh\t$2,65535($4)\n",
|
||||
"\tlh\t$2,D_801809EC\n",
|
||||
"\tlw\t$2,$L37($2)\n",
|
||||
} {
|
||||
t.Run(tcase, func(t *testing.T) {
|
||||
assertPatch(t, tcase, tcase)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchLoadByteWithPointer(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tlb\t$2,D_801809EC($4)\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tlui\t$1,%hi(D_801809EC)\n",
|
||||
"\taddu\t$1,$1,$4\n",
|
||||
"\tlb\t$2,%lo(D_801809EC)($1)\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchLoadByteUnsignedWithPointer(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tlbu\t$2,D_801809EC($4)\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tlui\t$1,%hi(D_801809EC)\n",
|
||||
"\taddu\t$1,$1,$4\n",
|
||||
"\tlbu\t$2,%lo(D_801809EC)($1)\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchLoadHalfWithPointer(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tlh\t$2,D_801809EC($4)\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tlui\t$1,%hi(D_801809EC)\n",
|
||||
"\taddu\t$1,$1,$4\n",
|
||||
"\tlh\t$2,%lo(D_801809EC)($1)\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchLoadHalfUnsignedWithPointer(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tlhu\t$2,D_801809EC($4)\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tlui\t$1,%hi(D_801809EC)\n",
|
||||
"\taddu\t$1,$1,$4\n",
|
||||
"\tlhu\t$2,%lo(D_801809EC)($1)\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchLoadWordWithPointer(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tlw\t$2,D_801809EC($4)\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tlui\t$1,%hi(D_801809EC)\n",
|
||||
"\taddu\t$1,$1,$4\n",
|
||||
"\tlw\t$2,%lo(D_801809EC)($1)\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchRemWithAspsxRem(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\trem $4,$2,$3\n",
|
||||
|
||||
"\t.set\tnoreorder\n",
|
||||
"\t.set\tnomacro\n",
|
||||
"\t.set\tnoat\n",
|
||||
"\tdiv\t$0,$2,$3\n",
|
||||
"\tbnez\t$3,.L_NOT_DIV_BY_ZERO_1\n",
|
||||
"\tnop\n",
|
||||
"\tbreak\t0x7\n",
|
||||
".L_NOT_DIV_BY_ZERO_1:\n",
|
||||
"\taddiu\t$1,$0,-1\n",
|
||||
"\tbne\t$3,$1,.L_DIV_BY_POSITIVE_SIGN_1\n",
|
||||
"\tlui\t$1,0x8000\n",
|
||||
"\tbne\t$2,$1,.L_DIV_BY_POSITIVE_SIGN_1\n",
|
||||
"\tnop\n",
|
||||
"\tbreak\t0x6\n",
|
||||
".L_DIV_BY_POSITIVE_SIGN_1:\n",
|
||||
"\tmfhi\t$4\n",
|
||||
"\t.set\tat\n",
|
||||
"\t.set\tmacro\n",
|
||||
"\t.set\treorder\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchRemWithAspsxDiv(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tdiv $4,$2,$3\n",
|
||||
|
||||
"\t.set\tnoreorder\n",
|
||||
"\t.set\tnomacro\n",
|
||||
"\t.set\tnoat\n",
|
||||
"\tdiv\t$0,$2,$3\n",
|
||||
"\tbnez\t$3,.L_NOT_DIV_BY_ZERO_1\n",
|
||||
"\tnop\n",
|
||||
"\tbreak\t0x7\n",
|
||||
".L_NOT_DIV_BY_ZERO_1:\n",
|
||||
"\taddiu\t$1,$0,-1\n",
|
||||
"\tbne\t$3,$1,.L_DIV_BY_POSITIVE_SIGN_1\n",
|
||||
"\tlui\t$1,0x8000\n",
|
||||
"\tbne\t$2,$1,.L_DIV_BY_POSITIVE_SIGN_1\n",
|
||||
"\tnop\n",
|
||||
"\tbreak\t0x6\n",
|
||||
".L_DIV_BY_POSITIVE_SIGN_1:\n",
|
||||
"\tmflo\t$4\n",
|
||||
"\t.set\tat\n",
|
||||
"\t.set\tmacro\n",
|
||||
"\t.set\treorder\n",
|
||||
)
|
||||
}
|
||||
|
||||
func TestPatchRemWithAspsxDivu(t *testing.T) {
|
||||
assertPatch(t,
|
||||
"\tdivu $4,$2,$3\n",
|
||||
|
||||
"\t.set\tnoat\n",
|
||||
"\tdivu\t$0,$2,$3\n",
|
||||
"\tbnez\t$3,.L_NOT_DIV_BY_ZERO_1\n",
|
||||
"\tbreak\t0x7\n",
|
||||
".L_NOT_DIV_BY_ZERO_1:\n",
|
||||
"\tmflo\t$4\n",
|
||||
"\t.set\tat\n",
|
||||
)
|
||||
}
|
||||
|
||||
func assertPatch(t *testing.T, in string, out ...string) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := initPatcher(strings.NewReader(in), buf).patch()
|
||||
|
||||
assert := require.New(t)
|
||||
assert.NoError(err)
|
||||
|
||||
patched := bufio.NewReader(buf)
|
||||
for _, expected := range out {
|
||||
actual, err := patched.ReadString('\n')
|
||||
assert.NoError(err, "Should have read '%s'", expected)
|
||||
assert.Equal(expected, actual)
|
||||
}
|
||||
|
||||
_, err = patched.ReadString('\n')
|
||||
assert.Equal(err, io.EOF)
|
||||
}
|
Loading…
Reference in New Issue
Block a user