--- gforth/see.fs 1997/01/29 21:32:38 1.15 +++ gforth/see.fs 2000/06/14 20:31:47 1.27 @@ -25,7 +25,10 @@ \ Ideas: Level should be a stack +require look.fs require termsize.fs +require wordinfo.fs +[IFUNDEF] .name : .name name>string type space ; [THEN] decimal @@ -56,7 +59,7 @@ DEFER nlcount ' noop IS nlcount : nl nlflag on ; : (nl) nlcount - XPos @ Level @ = ?Exit + XPos @ Level @ = IF EXIT THEN \ ?Exit C-Formated @ IF C-Output @ IF C-Clearline @ IF cols XPos @ - spaces @@ -69,8 +72,9 @@ DEFER nlcount ' noop IS nlcount nlflag @ IF (nl) nlflag off THEN XPos @ over + cols u>= IF (nl) THEN ; -: c-to-upper - dup [char] a >= over [char] z <= and if bl - then ; +: c-to-upper ( c1 -- c2 ) \ gforth + \ nac05feb1999 there is a primitive, toupper, with this function + dup [char] a >= over [char] z <= and if bl - then ; : ctype ( adr len -- ) warp? dup XPos +! C-Output @ @@ -108,7 +112,7 @@ VARIABLE Colors Colors on : .struc uppercase on Str# .string ; -\ CODES 15may93jaw +\ CODES (Branchtypes) 15may93jaw 21 CONSTANT RepeatCode 22 CONSTANT AgainCode @@ -118,21 +122,30 @@ VARIABLE Colors Colors on 11 CONSTANT AheadCode 13 CONSTANT WhileCode2 14 CONSTANT Disable +15 CONSTANT LeaveCode + \ FORMAT WORDS 13jun93jaw VARIABLE C-Stop VARIABLE Branches -VARIABLE BranchPointer +VARIABLE BranchPointer \ point to the end of branch table VARIABLE SearchPointer -CREATE BranchTable 500 allot + +\ The branchtable consists of three entrys: +\ address of branch , branch destination , branch type + +CREATE BranchTable 128 cells allot here 3 cells - ACONSTANT MaxTable : FirstBranch BranchTable cell+ SearchPointer ! ; -: (BranchAddr?) ( a-addr -- a-addr true | false ) +: (BranchAddr?) ( a-addr1 -- a-addr2 true | false ) +\ searches a branch with destination a-addr1 +\ a-addr1: branch destination +\ a-addr2: pointer in branch table SearchPointer @ BEGIN dup BranchPointer @ u< WHILE @@ -161,6 +174,20 @@ ACONSTANT MaxTable 2drop true THEN ; +: MyBranch ( a-addr -- a-addr a-addr2 ) +\ finds branch table entry for branch at a-addr + dup @ over + + BranchAddr? + BEGIN + WHILE 1 cells - @ + over <> + WHILE dup @ over + + MoreBranchAddr? + REPEAT + SearchPointer @ 3 cells - + ELSE true ABORT" SEE: Table failure" + THEN ; + \ \ addrw addrt \ BEGIN ... WHILE ... AGAIN ... THEN @@ -209,54 +236,38 @@ VARIABLE C-Pass : back? ( n -- flag ) 0< ; : ahead? ( n -- flag ) 0> ; -: c-(compile) - Display? - IF - s" POSTPONE " Com# .string - dup @ look 0= ABORT" SEE: No valid XT" - name>string 0 .string bl cemit - THEN - cell+ ; - : c-lit Display? IF dup @ dup abs 0 <# #S rot sign #> 0 .string bl cemit THEN cell+ ; -: c-s" - count 2dup + aligned -rot - Display? - IF [char] S cemit [char] " cemit bl cemit 0 .string - [char] " cemit bl cemit - ELSE 2drop - THEN ; - -: c-." - count 2dup + aligned -rot - Display? - IF [char] . cemit - [char] " cemit bl cemit 0 .string - [char] " cemit bl cemit - ELSE 2drop - THEN ; +: .name-without ( addr -- addr ) +\ prints a name without () e.g. (+LOOP) or (s") + dup 1 cells - @ look + IF name>string over c@ '( = IF 1 /string THEN + 2dup + 1- c@ ') = IF 1- THEN .struc ELSE drop + THEN ; : c-c" + Display? IF nl .name-without THEN count 2dup + aligned -rot Display? - IF [char] C cemit [char] " cemit bl cemit 0 .string + IF bl cemit 0 .string [char] " cemit bl cemit ELSE 2drop THEN ; -: Forward? ( a-addr true | false -- ) +: Forward? ( a-addr true | false -- a-addr true | false ) +\ a-addr1 is pointer into branch table +\ returns true when jump is a forward jump IF dup dup @ swap 1 cells - @ - Ahead? IF true ELSE drop false THEN \ only if forward jump ELSE false THEN ; -: RepeatCheck +: RepeatCheck ( a-addr1 a-addr2 true | false -- false ) IF BEGIN 2dup 1 cells - @ swap dup @ + u<= @@ -295,13 +306,17 @@ VARIABLE C-Pass IF drop S" REPEAT " .struc nl ELSE S" AGAIN " .struc nl THEN - ELSE dup cell+ BranchAddr? Forward? - IF dup cell+ @ WhileCode2 = - IF nl S" ELSE" .struc level+ - ELSE level- nl S" ELSE" .struc level+ THEN - cell+ Disable swap ! - ELSE S" AHEAD" .struc level+ - THEN + ELSE MyBranch cell+ @ LeaveCode = + IF S" LEAVE " .struc + ELSE + dup cell+ BranchAddr? Forward? + IF dup cell+ @ WhileCode2 = + IF nl S" ELSE" .struc level+ + ELSE level- nl S" ELSE" .struc level+ THEN + cell+ Disable swap ! + ELSE S" AHEAD" .struc level+ + THEN + THEN THEN THEN Debug? @@ -309,19 +324,6 @@ VARIABLE C-Pass ELSE cell+ THEN ; -: MyBranch ( a-addr -- a-addr a-addr2 ) - dup @ over + - BranchAddr? - BEGIN - WHILE 1 cells - @ - over <> - WHILE dup @ over + - MoreBranchAddr? - REPEAT - SearchPointer @ 3 cells - - ELSE true ABORT" SEE: Table failure" - THEN ; - : DebugBranch Debug? IF dup @ over + swap THEN ; \ return 2 different addresses @@ -344,7 +346,10 @@ VARIABLE C-Pass level- nl S" WHILE " .struc level+ - ELSE nl S" IF " .struc level+ + ELSE MyBranch cell+ @ LeaveCode = + IF s" 0= ?LEAVE " .struc + ELSE nl S" IF " .struc level+ + THEN THEN THEN THEN @@ -354,12 +359,16 @@ VARIABLE C-Pass : c-for Display? IF nl S" FOR" .struc level+ THEN ; -: .name-without - dup 1 cells - @ look IF name>string 1 /string 1- .struc ELSE drop THEN ; - : c-loop Display? IF level- nl .name-without bl cemit nl THEN - DebugBranch cell+ cell+ ; + DebugBranch cell+ + Scan? + IF dup BranchAddr? + BEGIN WHILE cell+ LeaveCode swap ! + dup MoreBranchAddr? + REPEAT + THEN + cell+ ; : c-do Display? IF nl .name-without level+ THEN ; @@ -368,14 +377,6 @@ VARIABLE C-Pass Display? IF nl S" ?DO" .struc level+ THEN DebugBranch cell+ ; -: c-leave - Display? IF S" LEAVE " .struc THEN - Debug? IF dup @ + THEN cell+ ; - -: c-?leave - Display? IF S" ?LEAVE " .struc THEN - cell+ DebugBranch swap cell+ swap cell+ ; - : c-exit dup 1 cells - CheckEnd IF Display? IF nlflag off S" ;" Com# .string THEN @@ -384,10 +385,6 @@ VARIABLE C-Pass THEN Debug? IF drop THEN ; -: c-does> \ end of create part - Display? IF S" DOES> " Com# .string THEN - Cell+ cell+ ; - : c-abort" count 2dup + aligned -rot Display? @@ -397,33 +394,49 @@ VARIABLE C-Pass ELSE 2drop THEN ; +[IFDEF] (does>) +: c-does> \ end of create part + Display? IF S" DOES> " Com# .string THEN + maxaligned /does-handler + ; +[THEN] + +[IFDEF] (compile) +: c-(compile) + Display? + IF + s" POSTPONE " Com# .string + dup @ look 0= ABORT" SEE: No valid XT" + name>string 0 .string bl cemit + THEN + cell+ ; +[THEN] CREATE C-Table - ' lit A, ' c-lit A, - ' (s") A, ' c-s" A, - ' (.") A, ' c-." A, - ' "lit A, ' c-c" A, - comp' leave drop A, ' c-leave A, - comp' ?leave drop A, ' c-?leave A, - ' (do) A, ' c-do A, - ' (+do) A, ' c-do A, - ' (u+do) A, ' c-do A, - ' (-do) A, ' c-do A, - ' (u-do) A, ' c-do A, - ' (?do) A, ' c-?do A, - ' (for) A, ' c-for A, - ' ?branch A, ' c-?branch A, - ' branch A, ' c-branch A, - ' (loop) A, ' c-loop A, - ' (+loop) A, ' c-loop A, - ' (s+loop) A, ' c-loop A, - ' (-loop) A, ' c-loop A, - ' (next) A, ' c-loop A, - ' ;s A, ' c-exit A, - ' (does>) A, ' c-does> A, - ' (abort") A, ' c-abort" A, - ' (compile) A, ' c-(compile) A, - 0 , here 0 , + ' lit A, ' c-lit A, + ' (s") A, ' c-c" A, + ' (.") A, ' c-c" A, + ' "lit A, ' c-c" A, +[IFDEF] (c") ' (c") A, ' c-c" A, [THEN] + ' (do) A, ' c-do A, +[IFDEF] (+do) ' (+do) A, ' c-do A, [THEN] +[IFDEF] (u+do) ' (u+do) A, ' c-do A, [THEN] +[IFDEF] (-do) ' (-do) A, ' c-do A, [THEN] +[IFDEF] (u-do) ' (u-do) A, ' c-do A, [THEN] + ' (?do) A, ' c-?do A, + ' (for) A, ' c-for A, + ' ?branch A, ' c-?branch A, + ' branch A, ' c-branch A, + ' (loop) A, ' c-loop A, + ' (+loop) A, ' c-loop A, +[IFDEF] (s+loop) ' (s+loop) A, ' c-loop A, [THEN] +[IFDEF] (-loop) ' (-loop) A, ' c-loop A, [THEN] + ' (next) A, ' c-loop A, + ' ;s A, ' c-exit A, + ' (abort") A, ' c-abort" A, +\ only defined if compiler is loaded +[IFDEF] (compile) ' (compile) A, ' c-(compile) A, [THEN] +[IFDEF] (does>) ' (does>) A, ' c-does> A, [THEN] + 0 , here 0 , avariable c-extender c-extender ! @@ -446,12 +459,12 @@ c-extender ! ; : BranchTo? ( a-addr -- a-addr ) - Display? IF dup BranchAddr? + Display? IF dup BranchAddr? IF BEGIN cell+ @ dup 20 u> IF drop nl S" BEGIN " .struc level+ ELSE - dup Disable <> + dup Disable <> over LeaveCode <> and IF WhileCode2 = IF nl S" THEN " .struc nl ELSE level- nl S" THEN " .struc nl THEN @@ -468,7 +481,10 @@ c-extender ! dup >r DoTable r> swap IF drop EXIT THEN Display? IF look 0= IF drop dup 1 cells - @ . \ ABORT" SEE: Bua!" - ELSE dup cell+ count 31 and rot wordinfo .string THEN bl cemit + ELSE + dup cell+ count dup immediate-mask and + IF bl cemit ." POSTPONE " THEN + 31 and rot wordinfo .string THEN bl cemit ELSE drop THEN ; @@ -498,17 +514,53 @@ Defer xt-see-xt ( xt -- ) then space ; -Defer discode ( addr -- ) -\ hook for the disassembler: disassemble code at addr (as far as the -\ disassembler thinks is sensible) -:noname ( addr -- ) - drop ." ..." ; -IS discode +Defer discode ( addr u -- ) +\ hook for the disassembler: disassemble code at addr of length u +' dump IS discode + +: next-head ( addr1 -- addr2 ) \ gforth + \G find the next header starting after addr1, up to here (unreliable). + here swap u+do + i head? + if + i unloop exit + then + cell +loop + here ; + +: umin ( u1 u2 -- u ) + 2dup u> + if + swap + then + drop ; + +: next-prim ( addr1 -- addr2 ) + \G find the next primitive after addr1 + 1+ >r -1 primstart + begin ( umin head R: boundary ) + @ dup + while + tuck name>int >code-address ( head1 umin c-addr ) + r@ - umin + swap + repeat + drop r> + ; : seecode ( xt -- ) dup s" Code" .defname - >body discode - ." end-code" cr ; + threading-method + if + >code-address + then + dup in-dictionary? \ user-defined code word? + if + dup next-head + else + dup next-prim + then + over - discode + ." end-code" cr ; : seevar ( xt -- ) s" Variable" .defname cr ; : seeuser ( xt -- ) @@ -522,10 +574,10 @@ IS discode : seedefer ( xt -- ) dup >body @ xt-see-xt cr dup s" Defer" .defname cr - >name dup ??? = if - drop ." lastxt >body !" - else + >name ?dup-if ." IS " .name cr + else + ." lastxt >body !" then ; : see-threaded ( addr -- ) C-Pass @ DebugMode = IF @@ -552,7 +604,7 @@ IS discode if seedoes EXIT then - dup forthstart u< + dup xtprim? if seecode EXIT then @@ -561,10 +613,17 @@ IS discode docon: of seecon endof docol: of seecol endof dovar: of seevar endof +[ [IFDEF] douser: ] douser: of seeuser endof +[ [THEN] ] +[ [IFDEF] dodefer: ] dodefer: of seedefer endof +[ [THEN] ] +[ [IFDEF] dofield: ] dofield: of seefield endof - over >body of seecode endof +[ [THEN] ] + over of seecode endof \ direct threaded code words + over >body of seecode endof \ indirect threaded code words 2drop abort" unknown word type" ENDCASE ; @@ -595,10 +654,15 @@ IS discode then rdrop drop ; -: see ( "name" -- ) \ tools +: see ( "name" -- ) \ tools + \G Locate @var{name} using the current search order. Display the + \G definition of @var{name}. Since this is achieved by decompiling + \G the definition, the formatting is mechanised and some source + \G information (comments, interpreted sequences within definitions + \G etc.) is lost. name find-name dup 0= IF - drop -&13 bounce + drop -&13 throw THEN name-see ;