2020-01-05 13:54:01 -05:00
|
|
|
// This file is part of Gopher2600.
|
|
|
|
//
|
|
|
|
// Gopher2600 is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Gopher2600 is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-04-08 12:55:33 -04:00
|
|
|
package disassembly
|
|
|
|
|
|
|
|
import (
|
2023-06-16 13:21:28 -04:00
|
|
|
"fmt"
|
2019-04-08 12:55:33 -04:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-10-16 11:03:36 -04:00
|
|
|
// GrepScope limits the scope of the search.
|
2019-12-23 03:54:02 -05:00
|
|
|
type GrepScope int
|
|
|
|
|
2020-10-16 11:03:36 -04:00
|
|
|
// List of available scopes.
|
2019-12-23 03:54:02 -05:00
|
|
|
const (
|
2020-10-18 05:57:54 -04:00
|
|
|
GrepAll GrepScope = iota
|
2021-01-17 03:28:41 -05:00
|
|
|
GrepOperator
|
2019-12-23 03:54:02 -05:00
|
|
|
GrepOperand
|
|
|
|
)
|
|
|
|
|
2019-12-11 03:00:58 -05:00
|
|
|
// Grep searches the disassembly for the specified search string.
|
2020-02-13 19:19:52 -05:00
|
|
|
func (dsm *Disassembly) Grep(output io.Writer, scope GrepScope, search string, caseSensitive bool) error {
|
2019-04-08 12:55:33 -04:00
|
|
|
if !caseSensitive {
|
|
|
|
search = strings.ToUpper(search)
|
|
|
|
}
|
|
|
|
|
2023-06-16 13:21:28 -04:00
|
|
|
includeBankNumber := len(dsm.disasmEntries.Entries) > 0
|
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
for b := range dsm.disasmEntries.Entries {
|
2023-06-16 13:21:28 -04:00
|
|
|
bankNumberPrinted := false
|
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
for _, e := range dsm.disasmEntries.Entries[b] {
|
2022-10-08 13:41:07 -04:00
|
|
|
if e != nil && e.Level >= EntryLevelBlessed {
|
2022-01-07 09:12:55 -05:00
|
|
|
var s, m string
|
2021-02-06 07:06:21 -05:00
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
// limit scope of grep to the correct Instruction field
|
|
|
|
switch scope {
|
|
|
|
case GrepOperator:
|
|
|
|
s = e.Operator
|
|
|
|
case GrepOperand:
|
2023-11-14 10:18:32 -05:00
|
|
|
s = e.Operand.Resolve()
|
2022-01-07 09:12:55 -05:00
|
|
|
case GrepAll:
|
|
|
|
s = e.String()
|
|
|
|
}
|
2019-04-11 02:13:51 -04:00
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
if !caseSensitive {
|
|
|
|
m = strings.ToUpper(s)
|
|
|
|
} else {
|
|
|
|
m = s
|
|
|
|
}
|
2020-02-13 19:19:52 -05:00
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
if strings.Contains(m, search) {
|
2023-06-16 13:21:28 -04:00
|
|
|
if includeBankNumber && !bankNumberPrinted {
|
|
|
|
bankNumberPrinted = true
|
|
|
|
output.Write([]byte(fmt.Sprintf("Bank %d\n", b)))
|
|
|
|
}
|
2022-01-07 09:12:55 -05:00
|
|
|
output.Write([]byte(e.StringColumnated(ColumnAttr{})))
|
|
|
|
output.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:55:33 -04:00
|
|
|
}
|
2022-01-07 09:12:55 -05:00
|
|
|
}
|
2020-02-13 19:19:52 -05:00
|
|
|
|
2022-01-07 09:12:55 -05:00
|
|
|
return nil
|
2019-04-08 12:55:33 -04:00
|
|
|
}
|