Annotation of gforth/see.fs, revision 1.32
1.1 anton 1: \ SEE.FS highend SEE for ANSforth 16may93jaw
2:
1.30 anton 3: \ Copyright (C) 1995,2000 Free Software Foundation, Inc.
1.9 anton 4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
1.31 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.9 anton 20:
21:
1.1 anton 22: \ May be cross-compiled
23:
24: \ I'm sorry. This is really not "forthy" enough.
25:
26: \ Ideas: Level should be a stack
27:
1.18 jwilke 28: require look.fs
1.10 anton 29: require termsize.fs
1.18 jwilke 30: require wordinfo.fs
1.32 ! anton 31: [IFUNDEF] .name
! 32: : id. ( nt -- ) \ gforth
! 33: \G Print the name of the word represented by @var{nt}.
! 34: \ this name comes from fig-Forth
! 35: name>string type space ;
! 36:
! 37: ' id. alias .id ( nt -- )
! 38: \G F83 name for @code{id.}.
! 39:
! 40: ' id. alias .name ( nt -- )
! 41: \G Gforth <=0.5.0 name for @code{id.}.
! 42:
! 43: [THEN]
1.10 anton 44:
1.1 anton 45: decimal
46:
47: \ Screen format words 16may93jaw
48:
49: VARIABLE C-Output 1 C-Output !
50: VARIABLE C-Formated 1 C-Formated !
51: VARIABLE C-Highlight 0 C-Highlight !
52: VARIABLE C-Clearline 0 C-Clearline !
53:
54: VARIABLE XPos
55: VARIABLE YPos
56: VARIABLE Level
57:
58: : Format C-Formated @ C-Output @ and
59: IF dup spaces XPos +! ELSE drop THEN ;
60:
61: : level+ 7 Level +!
62: Level @ XPos @ -
63: dup 0> IF Format ELSE drop THEN ;
64:
65: : level- -7 Level +! ;
66:
67: VARIABLE nlflag
1.15 pazsan 68: VARIABLE uppercase \ structure words are in uppercase
1.1 anton 69:
70: DEFER nlcount ' noop IS nlcount
71:
72: : nl nlflag on ;
73: : (nl) nlcount
1.18 jwilke 74: XPos @ Level @ = IF EXIT THEN \ ?Exit
1.1 anton 75: C-Formated @ IF
76: C-Output @
1.10 anton 77: IF C-Clearline @ IF cols XPos @ - spaces
1.1 anton 78: ELSE cr THEN
79: 1 YPos +! 0 XPos !
80: Level @ spaces
81: THEN Level @ XPos ! THEN ;
82:
83: : warp? ( len -- len )
84: nlflag @ IF (nl) nlflag off THEN
1.10 anton 85: XPos @ over + cols u>= IF (nl) THEN ;
1.1 anton 86:
1.22 crook 87: : c-to-upper ( c1 -- c2 ) \ gforth
88: \ nac05feb1999 there is a primitive, toupper, with this function
89: dup [char] a >= over [char] z <= and if bl - then ;
1.15 pazsan 90:
1.1 anton 91: : ctype ( adr len -- )
1.15 pazsan 92: warp? dup XPos +! C-Output @
93: IF uppercase @ IF bounds ?DO i c@ c-to-upper emit LOOP
94: uppercase off ELSE type THEN
95: ELSE 2drop THEN ;
1.1 anton 96:
97: : cemit 1 warp?
98: over bl = Level @ XPos @ = and
99: IF 2drop ELSE XPos +! C-Output @ IF emit ELSE drop THEN
100: THEN ;
101:
102: DEFER .string
103:
104: [IFDEF] Green
105: VARIABLE Colors Colors on
106:
107: : (.string) ( c-addr u n -- )
108: over warp? drop
109: Colors @
110: IF C-Highlight @ ?dup
111: IF CT@ swap CT@ or
112: ELSE CT@
113: THEN
114: attr! ELSE drop THEN
115: ctype ct @ attr! ;
116: [ELSE]
117: : (.string) ( c-addr u n -- )
118: drop ctype ;
119: [THEN]
120:
121: ' (.string) IS .string
122:
123:
1.15 pazsan 124: : .struc
125: uppercase on Str# .string ;
1.1 anton 126:
1.17 jwilke 127: \ CODES (Branchtypes) 15may93jaw
1.1 anton 128:
129: 21 CONSTANT RepeatCode
130: 22 CONSTANT AgainCode
131: 23 CONSTANT UntilCode
132: \ 09 CONSTANT WhileCode
133: 10 CONSTANT ElseCode
134: 11 CONSTANT AheadCode
135: 13 CONSTANT WhileCode2
136: 14 CONSTANT Disable
1.17 jwilke 137: 15 CONSTANT LeaveCode
138:
1.1 anton 139:
140: \ FORMAT WORDS 13jun93jaw
141:
142: VARIABLE C-Stop
143: VARIABLE Branches
144:
1.17 jwilke 145: VARIABLE BranchPointer \ point to the end of branch table
1.1 anton 146: VARIABLE SearchPointer
1.17 jwilke 147:
148: \ The branchtable consists of three entrys:
149: \ address of branch , branch destination , branch type
150:
1.25 pazsan 151: CREATE BranchTable 128 cells allot
1.1 anton 152: here 3 cells -
153: ACONSTANT MaxTable
154:
155: : FirstBranch BranchTable cell+ SearchPointer ! ;
156:
1.17 jwilke 157: : (BranchAddr?) ( a-addr1 -- a-addr2 true | false )
158: \ searches a branch with destination a-addr1
159: \ a-addr1: branch destination
160: \ a-addr2: pointer in branch table
1.1 anton 161: SearchPointer @
162: BEGIN dup BranchPointer @ u<
163: WHILE
164: dup @ 2 pick <>
165: WHILE 3 cells +
166: REPEAT
167: nip dup 3 cells + SearchPointer ! true
168: ELSE
169: 2drop false
170: THEN ;
171:
172: : BranchAddr?
173: FirstBranch (BranchAddr?) ;
174:
175: ' (BranchAddr?) ALIAS MoreBranchAddr?
176:
177: : CheckEnd ( a-addr -- true | false )
178: BranchTable cell+
179: BEGIN dup BranchPointer @ u<
180: WHILE
181: dup @ 2 pick u<=
182: WHILE 3 cells +
183: REPEAT
184: 2drop false
185: ELSE
186: 2drop true
187: THEN ;
188:
1.17 jwilke 189: : MyBranch ( a-addr -- a-addr a-addr2 )
190: \ finds branch table entry for branch at a-addr
191: dup @ over +
192: BranchAddr?
193: BEGIN
194: WHILE 1 cells - @
195: over <>
196: WHILE dup @ over +
197: MoreBranchAddr?
198: REPEAT
199: SearchPointer @ 3 cells -
200: ELSE true ABORT" SEE: Table failure"
201: THEN ;
202:
1.1 anton 203: \
204: \ addrw addrt
205: \ BEGIN ... WHILE ... AGAIN ... THEN
206: \ ^ ! ! ^
207: \ ----------+--------+ !
208: \ ! !
209: \ +-------------------+
210: \
211: \
212:
213: : CheckWhile ( a-addrw a-addrt -- true | false )
214: BranchTable
215: BEGIN dup BranchPointer @ u<
216: WHILE dup @ 3 pick u>
217: over @ 3 pick u< and
218: IF dup cell+ @ 3 pick u<
219: IF 2drop drop true EXIT THEN
220: THEN
221: 3 cells +
222: REPEAT
223: 2drop drop false ;
224:
225: : ,Branch ( a-addr -- )
226: BranchPointer @ dup MaxTable u> ABORT" SEE: Table overflow"
227: !
228: 1 cells BranchPointer +! ;
229:
230: : Type! ( u -- )
231: BranchPointer @ 1 cells - ! ;
232:
233: : Branch! ( a-addr rel -- a-addr )
234: over + over ,Branch ,Branch 0 ,Branch ;
235:
236: \ DEFER CheckUntil
237: VARIABLE NoOutput
238: VARIABLE C-Pass
239:
240: 0 CONSTANT ScanMode
241: 1 CONSTANT DisplayMode
242: 2 CONSTANT DebugMode
243:
244: : Scan? ( -- flag ) C-Pass @ 0= ;
245: : Display? ( -- flag ) C-Pass @ 1 = ;
246: : Debug? ( -- flag ) C-Pass @ 2 = ;
247:
248: : back? ( n -- flag ) 0< ;
249: : ahead? ( n -- flag ) 0> ;
250:
251: : c-lit
1.8 pazsan 252: Display? IF
253: dup @ dup abs 0 <# #S rot sign #> 0 .string bl cemit
254: THEN
255: cell+ ;
256:
1.18 jwilke 257: : .name-without ( addr -- addr )
258: \ prints a name without () e.g. (+LOOP) or (s")
259: dup 1 cells - @ look
260: IF name>string over c@ '( = IF 1 /string THEN
261: 2dup + 1- c@ ') = IF 1- THEN .struc ELSE drop
262: THEN ;
1.1 anton 263:
264: : c-c"
1.18 jwilke 265: Display? IF nl .name-without THEN
1.1 anton 266: count 2dup + aligned -rot
267: Display?
1.18 jwilke 268: IF bl cemit 0 .string
1.1 anton 269: [char] " cemit bl cemit
270: ELSE 2drop
271: THEN ;
272:
273:
1.17 jwilke 274: : Forward? ( a-addr true | false -- a-addr true | false )
275: \ a-addr1 is pointer into branch table
276: \ returns true when jump is a forward jump
1.1 anton 277: IF dup dup @ swap 1 cells - @ -
278: Ahead? IF true ELSE drop false THEN
279: \ only if forward jump
280: ELSE false THEN ;
281:
1.17 jwilke 282: : RepeatCheck ( a-addr1 a-addr2 true | false -- false )
1.1 anton 283: IF BEGIN 2dup
284: 1 cells - @ swap dup @ +
285: u<=
286: WHILE drop dup cell+
287: MoreBranchAddr? 0=
288: UNTIL false
289: ELSE true
290: THEN
291: ELSE false
292: THEN ;
293:
294: : c-branch
295: Scan?
296: IF dup @ Branch!
297: dup @ back?
298: IF \ might be: AGAIN, REPEAT
299: dup cell+ BranchAddr? Forward?
300: RepeatCheck
301: IF RepeatCode Type!
302: cell+ Disable swap !
303: ELSE AgainCode Type!
304: THEN
305: ELSE dup cell+ BranchAddr? Forward?
306: IF ElseCode Type! drop
307: ELSE AheadCode Type!
308: THEN
309: THEN
310: THEN
311: Display?
312: IF
313: dup @ back?
314: IF \ might be: AGAIN, REPEAT
315: level- nl
316: dup cell+ BranchAddr? Forward?
317: RepeatCheck
318: IF drop S" REPEAT " .struc nl
319: ELSE S" AGAIN " .struc nl
320: THEN
1.17 jwilke 321: ELSE MyBranch cell+ @ LeaveCode =
322: IF S" LEAVE " .struc
323: ELSE
324: dup cell+ BranchAddr? Forward?
325: IF dup cell+ @ WhileCode2 =
326: IF nl S" ELSE" .struc level+
327: ELSE level- nl S" ELSE" .struc level+ THEN
328: cell+ Disable swap !
329: ELSE S" AHEAD" .struc level+
330: THEN
331: THEN
1.1 anton 332: THEN
333: THEN
334: Debug?
335: IF dup @ +
336: ELSE cell+
337: THEN ;
338:
339: : DebugBranch
340: Debug?
341: IF dup @ over + swap THEN ; \ return 2 different addresses
342:
343: : c-?branch
344: Scan?
345: IF dup @ Branch!
346: dup @ Back?
347: IF UntilCode Type! THEN
348: THEN
349: Display?
350: IF dup @ Back?
351: IF level- nl S" UNTIL " .struc nl
352: ELSE dup dup @ over +
353: CheckWhile
354: IF MyBranch
355: cell+ dup @ 0=
356: IF WhileCode2 swap !
357: ELSE drop THEN
358: level- nl
1.8 pazsan 359: S" WHILE " .struc
1.1 anton 360: level+
1.17 jwilke 361: ELSE MyBranch cell+ @ LeaveCode =
362: IF s" 0= ?LEAVE " .struc
363: ELSE nl S" IF " .struc level+
364: THEN
1.1 anton 365: THEN
366: THEN
367: THEN
368: DebugBranch
369: cell+ ;
370:
371: : c-for
372: Display? IF nl S" FOR" .struc level+ THEN ;
373:
374: : c-loop
1.15 pazsan 375: Display? IF level- nl .name-without bl cemit nl THEN
1.17 jwilke 376: DebugBranch cell+
377: Scan?
378: IF dup BranchAddr?
379: BEGIN WHILE cell+ LeaveCode swap !
380: dup MoreBranchAddr?
381: REPEAT
382: THEN
383: cell+ ;
1.1 anton 384:
1.15 pazsan 385: : c-do
386: Display? IF nl .name-without level+ THEN ;
1.1 anton 387:
1.15 pazsan 388: : c-?do
389: Display? IF nl S" ?DO" .struc level+ THEN
390: DebugBranch cell+ ;
1.8 pazsan 391:
1.1 anton 392: : c-exit dup 1 cells -
393: CheckEnd
394: IF Display? IF nlflag off S" ;" Com# .string THEN
395: C-Stop on
396: ELSE Display? IF S" EXIT " .struc THEN
397: THEN
398: Debug? IF drop THEN ;
399:
400: : c-abort"
401: count 2dup + aligned -rot
402: Display?
403: IF S" ABORT" .struc
404: [char] " cemit bl cemit 0 .string
405: [char] " cemit bl cemit
406: ELSE 2drop
407: THEN ;
408:
1.23 jwilke 409: [IFDEF] (does>)
410: : c-does> \ end of create part
411: Display? IF S" DOES> " Com# .string THEN
412: maxaligned /does-handler + ;
413: [THEN]
414:
415: [IFDEF] (compile)
416: : c-(compile)
417: Display?
418: IF
419: s" POSTPONE " Com# .string
420: dup @ look 0= ABORT" SEE: No valid XT"
421: name>string 0 .string bl cemit
422: THEN
423: cell+ ;
424: [THEN]
1.1 anton 425:
426: CREATE C-Table
1.18 jwilke 427: ' lit A, ' c-lit A,
428: ' (s") A, ' c-c" A,
429: ' (.") A, ' c-c" A,
430: ' "lit A, ' c-c" A,
431: [IFDEF] (c") ' (c") A, ' c-c" A, [THEN]
432: ' (do) A, ' c-do A,
433: [IFDEF] (+do) ' (+do) A, ' c-do A, [THEN]
434: [IFDEF] (u+do) ' (u+do) A, ' c-do A, [THEN]
435: [IFDEF] (-do) ' (-do) A, ' c-do A, [THEN]
436: [IFDEF] (u-do) ' (u-do) A, ' c-do A, [THEN]
437: ' (?do) A, ' c-?do A,
438: ' (for) A, ' c-for A,
439: ' ?branch A, ' c-?branch A,
440: ' branch A, ' c-branch A,
441: ' (loop) A, ' c-loop A,
442: ' (+loop) A, ' c-loop A,
443: [IFDEF] (s+loop) ' (s+loop) A, ' c-loop A, [THEN]
444: [IFDEF] (-loop) ' (-loop) A, ' c-loop A, [THEN]
445: ' (next) A, ' c-loop A,
446: ' ;s A, ' c-exit A,
447: ' (abort") A, ' c-abort" A,
1.23 jwilke 448: \ only defined if compiler is loaded
449: [IFDEF] (compile) ' (compile) A, ' c-(compile) A, [THEN]
450: [IFDEF] (does>) ' (does>) A, ' c-does> A, [THEN]
1.18 jwilke 451: 0 , here 0 ,
1.15 pazsan 452:
453: avariable c-extender
454: c-extender !
1.1 anton 455:
456: \ DOTABLE 15may93jaw
457:
458: : DoTable ( cfa -- flag )
459: C-Table
1.15 pazsan 460: BEGIN dup @ dup 0=
461: IF drop cell+ @ dup
462: IF ( next table!) dup @ ELSE
463: ( end!) 2drop false EXIT THEN
464: THEN
465: \ jump over to extender, if any 26jan97jaw
466: 2 pick <>
1.1 anton 467: WHILE 2 cells +
468: REPEAT
1.11 anton 469: nip cell+ perform
1.1 anton 470: true
1.15 pazsan 471: ;
1.1 anton 472:
473: : BranchTo? ( a-addr -- a-addr )
1.17 jwilke 474: Display? IF dup BranchAddr?
1.15 pazsan 475: IF
476: BEGIN cell+ @ dup 20 u>
1.1 anton 477: IF drop nl S" BEGIN " .struc level+
478: ELSE
1.17 jwilke 479: dup Disable <> over LeaveCode <> and
1.1 anton 480: IF WhileCode2 =
481: IF nl S" THEN " .struc nl ELSE
482: level- nl S" THEN " .struc nl THEN
483: ELSE drop THEN
484: THEN
485: dup MoreBranchAddr? 0=
486: UNTIL
487: THEN
488: THEN ;
489:
490: : analyse ( a-addr1 -- a-addr2 )
491: Branches @ IF BranchTo? THEN
492: dup cell+ swap @
493: dup >r DoTable r> swap IF drop EXIT THEN
494: Display?
1.3 pazsan 495: IF look 0= IF drop dup 1 cells - @ . \ ABORT" SEE: Bua!"
1.16 anton 496: ELSE
497: dup cell+ count dup immediate-mask and
498: IF bl cemit ." POSTPONE " THEN
499: 31 and rot wordinfo .string THEN bl cemit
1.1 anton 500: ELSE drop
501: THEN ;
502:
503: : c-init
504: 0 YPos ! 0 XPos !
505: 0 Level ! nlflag off
506: BranchTable BranchPointer !
507: c-stop off
508: Branches on ;
509:
510: : makepass ( a-addr -- )
1.14 anton 511: c-stop off
512: BEGIN
513: analyse
514: c-stop @
515: UNTIL drop ;
516:
517: Defer xt-see-xt ( xt -- )
518: \ this one is just a forward declaration for indirect recursion
519:
520: : .defname ( xt c-addr u -- )
521: rot look
522: if ( c-addr u nfa )
523: -rot type space .name
524: else
525: drop ." noname " type
526: then
527: space ;
528:
1.28 anton 529: Defer discode ( addr u -- ) \ gforth
530: \G hook for the disassembler: disassemble code at addr of length u
1.27 anton 531: ' dump IS discode
532:
533: : next-head ( addr1 -- addr2 ) \ gforth
534: \G find the next header starting after addr1, up to here (unreliable).
535: here swap u+do
536: i head?
537: if
538: i unloop exit
539: then
540: cell +loop
541: here ;
542:
543: : umin ( u1 u2 -- u )
544: 2dup u>
545: if
546: swap
547: then
548: drop ;
549:
1.28 anton 550: : next-prim ( addr1 -- addr2 ) \ gforth
551: \G find the next primitive after addr1 (unreliable)
1.27 anton 552: 1+ >r -1 primstart
553: begin ( umin head R: boundary )
554: @ dup
555: while
1.28 anton 556: tuck name>int >code-address ( head1 umin ca R: boundary )
1.27 anton 557: r@ - umin
558: swap
559: repeat
1.28 anton 560: drop dup r@ negate u>=
561: \ "umin+boundary within [0,boundary)" = "umin within [-boundary,0)"
562: if ( umin R: boundary ) \ no primitive found behind -> use a default length
563: drop 31
564: then
565: r> + ;
1.14 anton 566:
567: : seecode ( xt -- )
568: dup s" Code" .defname
1.19 anton 569: threading-method
570: if
571: >code-address
572: then
1.27 anton 573: dup in-dictionary? \ user-defined code word?
574: if
575: dup next-head
576: else
577: dup next-prim
578: then
579: over - discode
580: ." end-code" cr ;
1.14 anton 581: : seevar ( xt -- )
582: s" Variable" .defname cr ;
583: : seeuser ( xt -- )
584: s" User" .defname cr ;
585: : seecon ( xt -- )
586: dup >body ?
587: s" Constant" .defname cr ;
588: : seevalue ( xt -- )
589: dup >body ?
590: s" Value" .defname cr ;
591: : seedefer ( xt -- )
592: dup >body @ xt-see-xt cr
593: dup s" Defer" .defname cr
1.26 anton 594: >name ?dup-if
595: ." IS " .name cr
1.14 anton 596: else
1.26 anton 597: ." lastxt >body !"
1.14 anton 598: then ;
599: : see-threaded ( addr -- )
600: C-Pass @ DebugMode = IF
601: ScanMode c-pass !
602: EXIT
1.10 anton 603: THEN
604: ScanMode c-pass ! dup makepass
605: DisplayMode c-pass ! makepass ;
1.14 anton 606: : seedoes ( xt -- )
607: dup s" create" .defname cr
608: S" DOES> " Com# .string XPos @ Level !
609: >does-code see-threaded ;
610: : seecol ( xt -- )
1.15 pazsan 611: dup s" :" .defname nl
1.14 anton 612: 2 Level !
613: >body see-threaded ;
614: : seefield ( xt -- )
615: dup >body ." 0 " ? ." 0 0 "
616: s" Field" .defname cr ;
617:
1.29 anton 618: : xt-see ( xt -- ) \ gforth
619: \G Decompile the definition represented by @i{xt}.
1.14 anton 620: cr c-init
621: dup >does-code
622: if
623: seedoes EXIT
624: then
1.18 jwilke 625: dup xtprim?
1.14 anton 626: if
627: seecode EXIT
628: then
629: dup >code-address
630: CASE
631: docon: of seecon endof
632: docol: of seecol endof
633: dovar: of seevar endof
1.18 jwilke 634: [ [IFDEF] douser: ]
1.14 anton 635: douser: of seeuser endof
1.18 jwilke 636: [ [THEN] ]
637: [ [IFDEF] dodefer: ]
1.14 anton 638: dodefer: of seedefer endof
1.18 jwilke 639: [ [THEN] ]
640: [ [IFDEF] dofield: ]
1.14 anton 641: dofield: of seefield endof
1.18 jwilke 642: [ [THEN] ]
1.27 anton 643: over of seecode endof \ direct threaded code words
644: over >body of seecode endof \ indirect threaded code words
1.14 anton 645: 2drop abort" unknown word type"
646: ENDCASE ;
647:
648: : (xt-see-xt) ( xt -- )
649: xt-see cr ." lastxt" ;
650: ' (xt-see-xt) is xt-see-xt
651:
652: : (.immediate) ( xt -- )
653: ['] execute = if
654: ." immediate"
655: then ;
656:
657: : name-see ( nfa -- )
658: dup name>int >r
659: dup name>comp
660: over r@ =
661: if \ normal or immediate word
662: swap xt-see (.immediate)
663: else
664: r@ ['] compile-only-error =
665: if \ compile-only word
666: swap xt-see (.immediate) ." compile-only"
667: else \ interpret/compile word
668: r@ xt-see-xt cr
669: swap xt-see-xt cr
670: ." interpret/compile " over .name (.immediate)
671: then
672: then
673: rdrop drop ;
1.3 pazsan 674:
1.21 crook 675: : see ( "<spaces>name" -- ) \ tools
676: \G Locate @var{name} using the current search order. Display the
677: \G definition of @var{name}. Since this is achieved by decompiling
678: \G the definition, the formatting is mechanised and some source
679: \G information (comments, interpreted sequences within definitions
680: \G etc.) is lost.
1.13 anton 681: name find-name dup 0=
682: IF
1.24 anton 683: drop -&13 throw
1.13 anton 684: THEN
1.14 anton 685: name-see ;
1.1 anton 686:
687:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>