@@ -2,6 +2,7 @@ package debugger
22
33import (
44 "bufio"
5+ "fmt"
56 "os"
67 "strconv"
78 "strings"
@@ -14,6 +15,10 @@ type debugSymbol struct {
1415
1516type debugSymbols []debugSymbol
1617
18+ func (d debugSymbol ) String () string {
19+ return fmt .Sprintf ("{%s => $%04X}" , d .name , d .address )
20+ }
21+
1722// addressesFor returns the addresses labelled with the given name.
1823func (symbols debugSymbols ) addressesFor (name string ) (result []uint16 ) {
1924 for _ , s := range symbols {
@@ -58,8 +63,13 @@ func readDebugSymbols(debugFile string) (symbols debugSymbols, err error) {
5863 t := & tokenizer {state : sBegin }
5964
6065 handleLine := func () {
61- if t .line .data ["type" ] == "label" {
62- addr , err := strconv .ParseUint (t .line .data ["value" ], 0 , 16 )
66+ // old format: "label", new format: "lab"
67+ if t .line .data ["type" ][0 :3 ] == "lab" {
68+ val , ok := t .line .data ["val" ] // new format
69+ if ! ok {
70+ val = t .line .data ["value" ] // old format
71+ }
72+ addr , err := strconv .ParseUint (val , 0 , 16 )
6373 if err != nil {
6474 panic (err )
6575 }
@@ -75,19 +85,30 @@ func readDebugSymbols(debugFile string) (symbols debugSymbols, err error) {
7585 case sBegin :
7686 if s .Text () == "sym" {
7787 t .line = debugLine {prefix : "sym" , data : make (map [string ]string )}
78- t .enter (sName )
88+ t .enter (sTab )
7989 } else {
8090 t .enter (sReject )
8191 }
8292 case sReject :
8393 if bytes [0 ] == '\n' {
8494 t .enter (sBegin )
8595 }
86- case sName :
87- if bytes [0 ] != '\t' {
96+ case sTab :
97+ if bytes [0 ] == '\t' {
98+ t .enter (sNameOrMap )
99+ } else {
100+ panic ("Expected TAB after line type" )
101+ }
102+ case sNameOrMap :
103+ if bytes [0 ] == '"' {
104+ // name (old debug format)
88105 text := s .Text ()
89106 t .line .name = text [1 : len (text )- 1 ] // strip quotes
90107 t .enter (sMap )
108+ } else {
109+ // map key (new debug format)
110+ t .line .key = s .Text ()
111+ t .enter (sMapEquals )
91112 }
92113 case sMap :
93114 if bytes [0 ] == ',' {
@@ -107,7 +128,12 @@ func readDebugSymbols(debugFile string) (symbols debugSymbols, err error) {
107128 }
108129 case sMapValue :
109130 t .enter (sMap )
110- t .line .data [t .line .key ] = s .Text ()
131+ if t .line .key == "name" {
132+ text := s .Text ()
133+ t .line .name = text [1 : len (text )- 1 ] // strip quotes
134+ } else {
135+ t .line .data [t .line .key ] = s .Text ()
136+ }
111137 }
112138 }
113139 if err = s .Err (); err != nil {
@@ -119,10 +145,11 @@ func readDebugSymbols(debugFile string) (symbols debugSymbols, err error) {
119145
120146// Tokenizer states.
121147const (
122- sBegin = iota // initial state
123- sReject // line is being rejected
124- sName // expecting name
125- sMap // expecting ,key=value,key=value
148+ sBegin = iota // initial state
149+ sReject // line is being rejected
150+ sTab // expect tab
151+ sNameOrMap // expecting name in old format, map in new format.
152+ sMap // expecting ,key=value,key=value
126153 sMapKey
127154 sMapEquals
128155 sMapValue
@@ -141,7 +168,6 @@ type tokenizer struct {
141168}
142169
143170func (t * tokenizer ) enter (state int ) {
144- //fmt.Printf("cc65 debug tokenizer: %d => %d\n", t.state, state)
145171 t .state = state
146172}
147173
0 commit comments