Skip to content

Commit 706a52c

Browse files
committed
added location-printing for the backtrace (not yet in image)
1 parent 670be5a commit 706a52c

File tree

8 files changed

+114
-39
lines changed

8 files changed

+114
-39
lines changed

backtrac.fs

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
IS store-backtrace
3838

3939
: print-bt-entry ( return-stack-item -- )
40-
cell - dup in-dictionary? over dup aligned = and
40+
cell- dup in-dictionary? over dup aligned = and
4141
if
4242
@ dup threaded>name dup if
4343
.name drop
@@ -57,13 +57,14 @@ IS store-backtrace
5757
drop
5858
then ;
5959

60+
defer .backtrace-pos ( addr -- )
61+
' drop is .backtrace-pos
62+
6063
: print-backtrace ( addr1 addr2 -- )
6164
\G print a backtrace for the return stack addr1..addr2
6265
2dup u< IF cr ." Backtrace:" THEN
6366
swap u+do
64-
cr
65-
i @ dup hex. ( return-addr? )
66-
print-bt-entry
67+
cr i @ dup .backtrace-pos dup hex. print-bt-entry
6768
cell +loop ;
6869

6970
: bt ( -- )

except.fs

+28-1
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,36 @@ is catch
183183
Defer kill-task ' noop IS kill-task
184184
Variable located-xpos
185185
Variable located-len
186+
variable bn-xpos \ first contains located-xpos, but is updated by B and N
187+
variable located-top \ first line to display with l
188+
variable located-bottom \ last line to display with l
189+
2variable located-slurped \ the contents of the file in located-xpos, or 0 0
190+
191+
\ lines to show before and after locate
192+
3 value before-locate
193+
12 value after-locate
194+
195+
: xpos>file# ( xpos -- u )
196+
23 rshift ;
197+
198+
: xpos>line ( xpos -- u )
199+
8 rshift $7fff and ;
200+
201+
: set-located-xpos ( xpos len -- )
202+
over xpos>file# located-xpos @ xpos>file# <> if
203+
located-slurped 2@ drop ?dup-if
204+
free throw then
205+
0 0 located-slurped 2! then
206+
located-len ! dup located-xpos ! dup bn-xpos !
207+
xpos>line
208+
dup before-locate - 0 max located-top !
209+
after-locate + located-bottom ! ;
186210

187211
: set-current-xpos ( -- )
188-
input-lexeme @ located-len ! current-sourcepos1 located-xpos ! ;
212+
current-sourcepos1 input-lexeme @ set-located-xpos ;
213+
214+
\ : set-current-xpos ( -- )
215+
\ input-lexeme @ located-len ! current-sourcepos1 located-xpos ! ;
189216

190217
:noname ( y1 .. ym error/0 -- y1 .. ym / z1 .. zn error ) \ exception
191218
?DUP IF

gforth.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ PARSED-TYPE specifies what kind of text is parsed. It should be on of 'name',
314314
"fconstant" "value" "2value" "field" "user" "vocabulary" "voctable"
315315
"create-interpret/compile" "interpret/compile:"
316316
"debug:" "field:" "2field:" "ffield:" "sffield:" "dffield:"
317-
"uvar" "uvalue" "cfield:" "wfield:" "lfield:")
317+
"uvar" "uvalue" "cfield:" "wfield:" "lfield:" "+field")
318318
non-immediate (font-lock-type-face . 2)
319319
"[ \t\n]" t name (font-lock-variable-name-face . 3))
320320
("\\S-+%" non-immediate (font-lock-type-face . 2))

kernel/comp.fs

+5-1
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,16 @@ defer basic-block-end ( -- )
285285
0 compile-prim1 ;
286286
is basic-block-end
287287

288+
defer xt-location ( addr -- addr )
289+
\ note that an xt was compiled at addr, for backtrace-locate functionality
290+
' noop is xt-location
291+
288292
has? primcentric [IF]
289293
has? peephole [IF]
290294
\ dynamic only
291295
: peephole-compile, ( xt -- )
292296
\ compile xt, appending its code to the current dynamic superinstruction
293-
here swap , compile-prim1 ;
297+
here swap , xt-location compile-prim1 ;
294298
[ELSE]
295299
: peephole-compile, ( xt -- addr ) @ , ;
296300
[THEN]

locate1.fs

+72-23
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@
1717
\ You should have received a copy of the GNU General Public License
1818
\ along with this program. If not, see http://www.gnu.org/licenses/.
1919

20-
variable located-xpos \ contains xpos of LOCATEd/EDITed word
21-
variable located-len \ contains the length of the word
22-
variable bn-xpos \ first contains located-xpos, but is updated by B and N
23-
variable located-top \ first line to display with l
24-
variable located-bottom \ last line to display with l
25-
2variable located-slurped \ the contents of the file in located-xpos, or 0 0
26-
27-
: set-located-xpos ( xpos len -- )
28-
over xpos>file# located-xpos @ xpos>file# <> if
29-
located-slurped 2@ drop ?dup-if
30-
free throw then
31-
0 0 located-slurped 2! then
32-
located-len ! dup located-xpos ! dup bn-xpos !
33-
xpos>line
34-
dup before-locate - 0 max located-top !
35-
after-locate + located-bottom ! ;
36-
37-
:noname {: uline c-addr1 u1 -- uline c-addr1 u1 :}
38-
c-addr1 u1 str>loadfilename# uline
39-
input-lexeme 2@ >r source drop - encode-pos1 r> set-located-xpos
40-
uline c-addr1 u1
41-
; is set-current-xpos
42-
4320
: set-bn-xpos ( -- )
4421
bn-xpos @ xpos>file# located-top @ 0 encode-pos1 bn-xpos ! ;
4522

@@ -120,3 +97,75 @@ variable located-bottom \ last line to display with l
12097
: edit ( "name" -- )
12198
\g Enter the editor at the place of "name"
12299
(') name-set-located-xpos g ;
100+
101+
\ backtrace locate stuff:
102+
103+
256 1024 * constant bl-data-size
104+
105+
0
106+
2field: bl-bounds
107+
field: bl-next
108+
bl-data-size cell+ +field bl-data
109+
constant bl-size
110+
111+
variable code-locations 0 code-locations !
112+
113+
: .bl {: bl -- :}
114+
cr bl bl-bounds 2@ swap 16 hex.r 17 hex.r
115+
bl bl-data 17 hex.r
116+
bl bl-next @ 17 hex.r ;
117+
118+
: .bls ( -- )
119+
cr ." code-start code-end bl-data bl-next"
120+
code-locations @ begin
121+
dup while
122+
dup .bl
123+
bl-next @ repeat
124+
drop ;
125+
126+
: lookup-location ( addr -- pos1|0 )
127+
code-locations @ begin ( addr bl )
128+
dup while
129+
2dup bl-bounds 2@ within if
130+
tuck bl-bounds 2@ drop - + bl-data @ exit then
131+
bl-next @ repeat
132+
2drop 0 ;
133+
134+
[ifdef] .backtrace-pos
135+
: .backtrace-pos1 ( addr -- )
136+
cell- lookup-location dup if
137+
.sourcepos1 space then ;
138+
' .backtrace-pos1 is .backtrace-pos
139+
[then]
140+
141+
: xt-location2 ( addr bl -- addr )
142+
\ knowing that addr is within bl, record the current source
143+
\ position for addr
144+
2dup bl-bounds 2@ drop - + bl-data ( addr addr' )
145+
current-sourcepos1 swap 2dup ! cell+ ! ;
146+
147+
: new-bl ( addr blp -- )
148+
bl-size allocate throw >r
149+
swap dup bl-data-size + r@ bl-bounds 2!
150+
dup @ r@ bl-next !
151+
r@ bl-data bl-data-size cell+ erase
152+
r> swap ! ;
153+
154+
: xt-location1 ( addr -- addr )
155+
code-locations begin ( addr blp )
156+
dup @ 0= if
157+
2dup new-bl then
158+
@ 2dup bl-bounds 2@ within 0= while ( addr bl )
159+
bl-next repeat
160+
xt-location2 ;
161+
162+
' xt-location1 is xt-location
163+
164+
\ test
165+
166+
: foo 0 @ ;
167+
168+
: foo1 foo ;
169+
: foo2 foo1 ;
170+
171+
foo2

sections.fs

-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ constant extra-section-error
4848
: current-section-addr ( -- addr )
4949
current-section @ section-addr ;
5050

51-
: hex.r ( u1 u2 -- )
52-
['] .r #16 base-execute ;
53-
5451
: .sections ( -- )
5552
cr ." start end dp "
5653
sections hex.

source.fs

-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ require string.fs
3737
: decode-pos1 ( xpos -- nfile nline nchar )
3838
dup 23 arshift swap $7fffff and decode-pos ;
3939

40-
: xpos>file# ( xpos -- u )
41-
23 rshift ;
42-
43-
: xpos>line ( xpos -- u )
44-
8 rshift $7fff and ;
45-
4640
: xpos>char ( xpos -- u )
4741
$ff and ;
4842

stuff.fs

+3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ previous
427427
endtry
428428
throw ;
429429

430+
: hex.r ( u1 u2 -- )
431+
['] .r #16 base-execute ;
432+
430433
: dump ( addr u -- ) ['] dump $10 base-execute ;
431434
\ wrap dump into base-execute
432435

0 commit comments

Comments
 (0)