File:  [gforth] / gforth / see.fs
Revision 1.70: download - view: text, annotated - select for diffs
Sat May 1 20:36:59 2010 UTC (10 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
See works for DOES> again

    1: \ SEE.FS       highend SEE for ANSforth                16may93jaw
    2: 
    3: \ Copyright (C) 1995,2000,2003,2004,2006,2007,2008 Free Software Foundation, Inc.
    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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: 
   21: \ May be cross-compiled
   22: 
   23: \ I'm sorry. This is really not "forthy" enough.
   24: 
   25: \ Ideas:        Level should be a stack
   26: 
   27: require look.fs
   28: require termsize.fs
   29: require wordinfo.fs
   30: 
   31: decimal
   32: 
   33: \ Screen format words                                   16may93jaw
   34: 
   35: VARIABLE C-Output   1 C-Output  !
   36: VARIABLE C-Formated 1 C-Formated !
   37: VARIABLE C-Highlight 0 C-Highlight !
   38: VARIABLE C-Clearline 0 C-Clearline !
   39: 
   40: VARIABLE XPos
   41: VARIABLE YPos
   42: VARIABLE Level
   43: 
   44: : Format        C-Formated @ C-Output @ and
   45:                 IF dup spaces XPos +! ELSE drop THEN ;
   46: 
   47: : level+        7 Level +!
   48:                 Level @ XPos @ -
   49:                 dup 0> IF Format ELSE drop THEN ;
   50: 
   51: : level-        -7 Level +! ;
   52: 
   53: VARIABLE nlflag
   54: VARIABLE uppercase	\ structure words are in uppercase
   55: 
   56: DEFER nlcount ' noop IS nlcount
   57: 
   58: : nl            nlflag on ;
   59: : (nl)          nlcount
   60:                 XPos @ Level @ = IF EXIT THEN \ ?Exit
   61:                 C-Formated @ IF
   62:                 C-Output @
   63:                 IF C-Clearline @ IF cols XPos @ - spaces
   64:                                  ELSE cr THEN
   65:                 1 YPos +! 0 XPos !
   66:                 Level @ spaces
   67:                 THEN Level @ XPos ! THEN ;
   68: 
   69: : warp?         ( len -- len )
   70:                 nlflag @ IF (nl) nlflag off THEN
   71:                 XPos @ over + cols u>= IF (nl) THEN ;
   72: 
   73: : ctype         ( adr len -- )
   74:                 warp? dup XPos +! C-Output @ 
   75: 		IF uppercase @ IF bounds ?DO i c@ toupper emit LOOP
   76: 				  uppercase off ELSE type THEN
   77: 		ELSE 2drop THEN ;
   78: 
   79: : cemit         1 warp?
   80:                 over bl = Level @ XPos @ = and
   81:                 IF 2drop ELSE XPos +! C-Output @ IF emit ELSE drop THEN
   82:                 THEN ;
   83: 
   84: DEFER .string ( c-addr u n -- )
   85: 
   86: [IFDEF] Green
   87: VARIABLE Colors Colors on
   88: 
   89: : (.string)     ( c-addr u n -- )
   90:                 over warp? drop
   91:                 Colors @
   92:                 IF C-Highlight @ ?dup
   93:                    IF   CT@ swap CT@ or
   94:                    ELSE CT@
   95:                    THEN
   96:                 attr! ELSE drop THEN
   97:                 ctype  ct @ attr! ;
   98: [ELSE]
   99: : (.string)     ( c-addr u n -- )
  100:                 drop ctype ;
  101: [THEN]
  102: 
  103: ' (.string) IS .string
  104: 
  105: : c-\type ( c-addr u -- )
  106:     \ type string in \-escaped form
  107:     begin
  108: 	dup while
  109: 	    2dup newline string-prefix? if
  110: 		'\ cemit 'n cemit
  111: 		newline nip /string
  112: 	    else
  113: 		over c@
  114: 		dup '" = over '\ = or if
  115: 		    '\ cemit cemit
  116: 		else
  117: 		    dup bl 127 within if
  118: 			cemit
  119: 		    else
  120: 			base @ >r try
  121: 			    8 base ! 0 <<# # # # '\ hold #> ctype #>> 0
  122: 			restore
  123: 			    r@ base !
  124: 			endtry
  125: 			rdrop throw
  126: 		    endif
  127: 		endif
  128: 		1 /string
  129: 	    endif
  130:     repeat
  131:     2drop ;
  132: 
  133: : .struc        
  134: 	uppercase on Str# .string ;
  135: 
  136: \ CODES (Branchtypes)                                    15may93jaw
  137: 
  138: 21 CONSTANT RepeatCode
  139: 22 CONSTANT AgainCode
  140: 23 CONSTANT UntilCode
  141: \ 09 CONSTANT WhileCode
  142: 10 CONSTANT ElseCode
  143: 11 CONSTANT AheadCode
  144: 13 CONSTANT WhileCode2
  145: 14 CONSTANT Disable
  146: 15 CONSTANT LeaveCode
  147: 
  148: 
  149: \ FORMAT WORDS                                          13jun93jaw
  150: 
  151: VARIABLE C-Stop
  152: VARIABLE Branches
  153: 
  154: VARIABLE BranchPointer	\ point to the end of branch table
  155: VARIABLE SearchPointer
  156: 
  157: \ The branchtable consists of three entrys:
  158: \ address of branch , branch destination , branch type
  159: 
  160: CREATE BranchTable 128 cells allot
  161: here 3 cells -
  162: ACONSTANT MaxTable
  163: 
  164: : FirstBranch BranchTable cell+ SearchPointer ! ;
  165: 
  166: : (BranchAddr?) ( a-addr1 -- a-addr2 true | false )
  167: \ searches a branch with destination a-addr1
  168: \ a-addr1: branch destination
  169: \ a-addr2: pointer in branch table
  170:         SearchPointer @
  171:         BEGIN   dup BranchPointer @ u<
  172:         WHILE
  173:                 dup @ 2 pick <>
  174:         WHILE   3 cells +
  175:         REPEAT
  176:         nip dup  3 cells + SearchPointer ! true
  177:         ELSE
  178:         2drop false
  179:         THEN ;
  180: 
  181: : BranchAddr?
  182:         FirstBranch (BranchAddr?) ;
  183: 
  184: ' (BranchAddr?) ALIAS MoreBranchAddr?
  185: 
  186: : CheckEnd ( a-addr -- true | false )
  187:         BranchTable cell+
  188:         BEGIN   dup BranchPointer @ u<
  189:         WHILE
  190:                 dup @ 2 pick u<=
  191:         WHILE   3 cells +
  192:         REPEAT
  193:         2drop false
  194:         ELSE
  195:         2drop true
  196:         THEN ;
  197: 
  198: : MyBranch      ( a-addr -- a-addr a-addr2 )
  199: \ finds branch table entry for branch at a-addr
  200:                 dup @
  201:                 BranchAddr?
  202:                 BEGIN
  203:                 WHILE 1 cells - @
  204:                       over <>
  205:                 WHILE dup @
  206:                       MoreBranchAddr?
  207:                 REPEAT
  208:                 SearchPointer @ 3 cells -
  209:                 ELSE    true ABORT" SEE: Table failure"
  210:                 THEN ;
  211: 
  212: \
  213: \                 addrw               addrt
  214: \       BEGIN ... WHILE ... AGAIN ... THEN
  215: \         ^         !        !          ^
  216: \         ----------+--------+          !
  217: \                   !                   !
  218: \                   +-------------------+
  219: \
  220: \
  221: 
  222: : CheckWhile ( a-addrw a-addrt -- true | false )
  223:         BranchTable
  224:         BEGIN   dup BranchPointer @ u<
  225:         WHILE   dup @ 3 pick u>
  226:                 over @ 3 pick u< and
  227:                 IF dup cell+ @ 3 pick u<
  228:                         IF 2drop drop true EXIT THEN
  229:                 THEN
  230:                 3 cells +
  231:         REPEAT
  232:         2drop drop false ;
  233: 
  234: : ,Branch ( a-addr -- )
  235:         BranchPointer @ dup MaxTable u> ABORT" SEE: Table overflow"
  236:         !
  237:         1 cells BranchPointer +! ;
  238: 
  239: : Type!   ( u -- )
  240:         BranchPointer @ 1 cells - ! ;
  241: 
  242: : Branch! ( a-addr rel -- a-addr )
  243:     over ,Branch ,Branch 0 ,Branch ;
  244: \        over + over ,Branch ,Branch 0 ,Branch ;
  245: 
  246: \ DEFER CheckUntil
  247: VARIABLE NoOutput
  248: VARIABLE C-Pass
  249: 
  250: 0 CONSTANT ScanMode
  251: 1 CONSTANT DisplayMode
  252: 2 CONSTANT DebugMode
  253: 
  254: : Scan? ( -- flag ) C-Pass @ 0= ;
  255: : Display? ( -- flag ) C-Pass @ 1 = ;
  256: : Debug? ( -- flag ) C-Pass @ 2 = ;
  257: 
  258: : back? ( addr target -- addr flag )
  259:     over u< ;
  260: 
  261: : .word ( addr x -- addr )
  262:     \ print x as a word if possible
  263:     dup look 0= IF
  264: 	drop dup threaded>name dup 0= if
  265: 	    drop over 1 cells - @ dup body> look
  266: 	    IF
  267: 		nip nip dup ." <" name>string rot wordinfo .string ." > "
  268: 	    ELSE
  269: 		2drop ." <" 0 .r ." > "
  270: 	    THEN
  271: 	    EXIT
  272: 	then
  273:     THEN
  274:     nip dup cell+ @ immediate-mask and
  275:     IF
  276: 	bl cemit  ." POSTPONE "
  277:     THEN
  278:     dup name>string rot wordinfo .string
  279:     ;
  280: 
  281: : c-call ( addr1 -- addr2 )
  282:     Display? IF
  283: 	dup @ body> .word bl cemit
  284:     THEN
  285:     cell+ ;
  286: 
  287: : c-callxt ( addr1 -- addr2 )
  288:     Display? IF
  289: 	dup @ .word bl cemit
  290:     THEN
  291:     cell+ ;
  292: 
  293: \ here docon: , docol: , dovar: , douser: , dodefer: , dofield: ,
  294: \ here over - 2constant doers
  295: 
  296: [IFDEF] !does
  297: : c-does>               \ end of create part
  298:         Display? IF S" DOES> " Com# .string THEN ;
  299: \	maxaligned /does-handler + ; \ !! no longer needed for non-cross stuff
  300: [THEN]
  301: 
  302: : c-lit ( addr1 -- addr2 )
  303:     Display? IF
  304: 	dup @ dup body> dup cfaligned over = swap in-dictionary? and if
  305: 	    ( addr1 addr1@ )
  306: 	    dup body> @ dovar: = if
  307: 		drop c-call EXIT
  308: 	    endif
  309: 	endif
  310: 	over 4 cells + over = if
  311: 	    over 1 cells + @ decompile-prim ['] call xt>threaded = >r
  312: 	    over 2 cells + @ ['] !does >body = >r
  313: 	    over 3 cells + @ decompile-prim ['] ;S xt>threaded =
  314: 	    r> and r> and
  315: 	    if  drop c-does>  4 cells + EXIT  endif
  316: 	endif
  317: 	\ !! test for cfa here, and print "['] ..."
  318: 	dup abs 0 <# #S rot sign #> 0 .string bl cemit
  319:     endif
  320:     cell+ ;
  321: 
  322: : c-lit+ ( addr1 -- addr2 )
  323:     Display? if
  324: 	dup @ dup abs 0 <# #S rot sign #> 0 .string bl cemit
  325: 	s" + " 0 .string
  326:     endif
  327:     cell+ ;
  328: 
  329: : .name-without ( addr -- addr )
  330:     \ !! the stack effect cannot be correct
  331:     \ prints a name without a() e.g. a(+LOOP) or (s")
  332:     dup 1 cells - @ threaded>name dup IF
  333: 	name>string over c@ 'a = IF
  334: 	    1 /string
  335: 	THEN
  336: 	 over c@ '( = IF
  337: 	    1 /string
  338: 	THEN
  339: 	2dup + 1- c@ ') = IF 1- THEN .struc ELSE drop 
  340:     THEN ;
  341: 
  342: [ifdef] (s")
  343: : c-c"
  344: 	Display? IF nl .name-without THEN
  345:         count 2dup + aligned -rot
  346:         Display?
  347:         IF      bl cemit 0 .string
  348:                 [char] " cemit bl cemit
  349:         ELSE    2drop
  350:         THEN ;
  351: [endif]
  352: 
  353: : c-string? ( addr1 -- addr2 f )
  354:     \ f is true if a string was found and decompiled.
  355:     \ if f is false, addr2=addr1
  356:     \ recognizes the following patterns:
  357:     \ c":     ahead X: len string then lit X
  358:     \ flit:   ahead X: float      then lit X f@
  359:     \ s\":    ahead X: string     then lit X lit len
  360:     \ .\":    ahead X: string     then lit X lit len type
  361:     \ !! not recognized anywhere:
  362:     \ abort": if ahead X: len string then lit X c(abort") then
  363:     dup @ back? if false exit endif
  364:     dup @ >r
  365:     r@ @ decompile-prim ['] lit xt>threaded <> if rdrop false exit endif
  366:     r@ cell+ @ over cell+ <> if rdrop false exit endif
  367:     \ we have at least C"
  368:     r@ 2 cells + @ decompile-prim dup ['] lit xt>threaded = if
  369: 	drop r@ 3 cells + @ over cell+ + aligned r@ = if
  370: 	    \ we have at least s"
  371: 	    r@ 4 cells + @ decompile-prim ['] lit-perform xt>threaded =
  372: 	    r@ 5 cells + @ ['] type >body = and if
  373: 		6 s\" .\\\" "
  374: 	    else
  375: 		4 s\" s\\\" "
  376: 	    endif
  377: 	    \ !! make newline if string too long?
  378: 	    display? if
  379: 		0 .string r@ cell+ @ r@ 3 cells + @ c-\type '" cemit bl cemit
  380: 	    else
  381: 		2drop
  382: 	    endif
  383: 	    nip cells r> + true exit
  384: 	endif
  385:     endif
  386:     ['] f@ xt>threaded = if
  387: 	display? if
  388: 	    r@ cell+ @ f@ 10 8 16 f>str-rdp 0 .string bl cemit
  389: 	endif
  390: 	drop r> 3 cells + true exit
  391:     endif
  392:     \ !! check if count matches space?
  393:     display? if
  394: 	s\" c\" " 0 .string r@ cell+ @ count 0 .string '" cemit bl cemit
  395:     endif
  396:     drop r> 2 cells + true ;
  397: 
  398: : Forward? ( a-addr true | false -- a-addr true | false )
  399:     \ a-addr is pointer into branch table
  400:     \ returns true when jump is a forward jump
  401:     IF
  402: 	dup dup @ swap 1 cells - @ u> IF
  403: 	    true
  404: 	ELSE
  405: 	    drop false
  406: 	THEN
  407: 	\ only if forward jump
  408:     ELSE
  409: 	false
  410:     THEN ;
  411: 
  412: : RepeatCheck ( a-addr1 a-addr2 true | false -- false )
  413:         IF  BEGIN  2dup
  414:                    1 cells - @ swap @
  415:                    u<=
  416:             WHILE  drop dup cell+
  417:                    MoreBranchAddr? 0=
  418:             UNTIL  false
  419:             ELSE   true
  420:             THEN
  421:         ELSE false
  422:         THEN ;
  423: 
  424: : c-branch ( addr1 -- addr2 )
  425:     c-string? ?exit
  426:         Scan?
  427:         IF      dup @ Branch!
  428:                 dup @ back?
  429:                 IF                      \ might be: AGAIN, REPEAT
  430:                         dup cell+ BranchAddr? Forward?
  431:                         RepeatCheck
  432:                         IF      RepeatCode Type!
  433:                                 cell+ Disable swap !
  434:                         ELSE    AgainCode Type!
  435:                         THEN
  436:                 ELSE    dup cell+ BranchAddr? Forward?
  437:                         IF      ElseCode Type! drop
  438:                         ELSE    AheadCode Type!
  439:                         THEN
  440:                 THEN
  441:         THEN
  442:         Display?
  443:         IF
  444:                 dup @ back?
  445:                 IF                      \ might be: AGAIN, REPEAT
  446:                         level- nl
  447:                         dup cell+ BranchAddr? Forward?
  448:                         RepeatCheck
  449:                         IF      drop S" REPEAT " .struc nl
  450:                         ELSE    S" AGAIN " .struc nl
  451:                         THEN
  452:                 ELSE    MyBranch cell+ @ LeaveCode =
  453: 			IF 	S" LEAVE " .struc
  454: 			ELSE
  455: 				dup cell+ BranchAddr? Forward?
  456:        	                 	IF      dup cell+ @ WhileCode2 =
  457:        	                         	IF nl S" ELSE" .struc level+
  458:                                 	ELSE level- nl S" ELSE" .struc level+ THEN
  459:                                 	cell+ Disable swap !
  460:                         	ELSE    S" AHEAD" .struc level+
  461:                         	THEN
  462: 			THEN
  463:                 THEN
  464:         THEN
  465:         Debug?
  466:         IF      @ \ !!! cross-interacts with debugger !!!
  467:         ELSE    cell+
  468:         THEN ;
  469: 
  470: : DebugBranch
  471:         Debug?
  472:         IF      dup @ swap THEN ; \ return 2 different addresses
  473: 
  474: : c-?branch
  475:         Scan?
  476:         IF      dup @ Branch!
  477:                 dup @ Back?
  478:                 IF      UntilCode Type! THEN
  479:         THEN
  480:         Display?
  481:         IF      dup @ Back?
  482:                 IF      level- nl S" UNTIL " .struc nl
  483:                 ELSE    dup    dup @ over +
  484:                         CheckWhile
  485:                         IF      MyBranch
  486:                                 cell+ dup @ 0=
  487:                                          IF WhileCode2 swap !
  488:                                          ELSE drop THEN
  489:                                 level- nl
  490:                                 S" WHILE " .struc
  491:                                 level+
  492:                         ELSE    MyBranch cell+ @ LeaveCode =
  493: 				IF   s" 0= ?LEAVE " .struc
  494: 				ELSE nl S" IF " .struc level+
  495: 				THEN
  496:                         THEN
  497:                 THEN
  498:         THEN
  499:         DebugBranch
  500:         cell+ ;
  501: 
  502: : c-for
  503:         Display? IF nl S" FOR" .struc level+ THEN ;
  504: 
  505: : c-loop
  506:         Display? IF level- nl .name-without nl bl cemit THEN
  507:         DebugBranch cell+ 
  508: 	Scan? 
  509: 	IF 	dup BranchAddr? 
  510: 		BEGIN   WHILE cell+ LeaveCode swap !
  511: 			dup MoreBranchAddr?
  512: 		REPEAT
  513: 	THEN
  514: 	cell+ ;
  515: 
  516: : c-do
  517:         Display? IF nl .name-without level+ THEN ;
  518: 
  519: : c-?do ( addr1 -- addr2 )
  520:     Display? IF
  521: 	nl .name-without level+
  522:     THEN
  523:     DebugBranch cell+ ;
  524: 
  525: : c-exit ( addr1 -- addr2 )
  526:     dup 1 cells -
  527:     CheckEnd
  528:     IF
  529: 	Display? IF nlflag off S" ;" Com# .string THEN
  530: 	C-Stop on
  531:     ELSE
  532: 	Display? IF S" EXIT " .struc THEN
  533:     THEN
  534:     Debug? IF drop THEN ; \ !!! cross-interacts with debugger !!!
  535: 
  536: : c-abort"
  537:         count 2dup + aligned -rot
  538:         Display?
  539:         IF      S" ABORT" .struc
  540:                 [char] " cemit bl cemit 0 .string
  541:                 [char] " cemit bl cemit
  542:         ELSE    2drop
  543:         THEN ;
  544: 
  545: [IFDEF] (compile)
  546: : c-(compile)
  547:     Display?
  548:     IF
  549: 	s" POSTPONE " Com# .string
  550: 	dup @ look 0= ABORT" SEE: No valid XT"
  551: 	name>string 0 .string bl cemit
  552:     THEN
  553:     cell+ ;
  554: [THEN]
  555: 
  556: CREATE C-Table
  557: 	        ' lit A,            ' c-lit A,
  558: 		' does-exec A,	    ' c-callxt A,
  559: 		' lit@ A,	    ' c-call A,
  560: [IFDEF] call	' call A,           ' c-call A, [THEN]
  561: \		' useraddr A,	    ....
  562: 		' lit-perform A,    ' c-call A,
  563: 		' lit+ A,	    ' c-lit+ A,
  564: [IFDEF] (s")	' (s") A,	    ' c-c" A, [THEN]
  565: [IFDEF] (.")	' (.") A,	    ' c-c" A, [THEN]
  566: [IFDEF] "lit    ' "lit A,           ' c-c" A, [THEN]
  567: [IFDEF] (c")	' (c") A,	    ' c-c" A, [THEN]
  568:         	' (do) A,           ' c-do A,
  569: [IFDEF] (+do)	' (+do) A,	    ' c-?do A, [THEN]
  570: [IFDEF] (u+do)	' (u+do) A,	    ' c-?do A, [THEN]
  571: [IFDEF] (-do)	' (-do) A,	    ' c-?do A, [THEN]
  572: [IFDEF] (u-do)	' (u-do) A,	    ' c-?do A, [THEN]
  573:         	' (?do) A,          ' c-?do A,
  574:         	' (for) A,          ' c-for A,
  575:         	' ?branch A,        ' c-?branch A,
  576:         	' branch A,         ' c-branch A,
  577:         	' (loop) A,         ' c-loop A,
  578:         	' (+loop) A,        ' c-loop A,
  579: [IFDEF] (s+loop) ' (s+loop) A,      ' c-loop A, [THEN]
  580: [IFDEF] (-loop) ' (-loop) A,        ' c-loop A, [THEN]
  581:         	' (next) A,         ' c-loop A,
  582:         	' ;s A,             ' c-exit A,
  583: [IFDEF] (abort") ' (abort") A,      ' c-abort" A, [THEN]
  584: \ only defined if compiler is loaded
  585: [IFDEF] (compile) ' (compile) A,      ' c-(compile) A, [THEN]
  586:         	0 ,		here 0 ,
  587: 
  588: avariable c-extender
  589: c-extender !
  590: 
  591: \ DOTABLE                                               15may93jaw
  592: 
  593: : DoTable ( ca/cfa -- flag )
  594:     decompile-prim C-Table BEGIN ( cfa table-entry )
  595: 	dup @ dup 0=  IF
  596: 	    drop cell+ @ dup IF ( next table!)
  597: 		dup @
  598: 	    ELSE ( end!)
  599: 		2drop false EXIT
  600: 	    THEN 
  601: 	THEN
  602: 	\ jump over to extender, if any 26jan97jaw
  603: 	xt>threaded 2 pick <>
  604:     WHILE
  605: 	    2 cells +
  606:     REPEAT
  607:     nip cell+ perform
  608:     true
  609: ;
  610: 
  611: : BranchTo? ( a-addr -- a-addr )
  612:         Display?  IF    dup BranchAddr?
  613:                         IF
  614: 				BEGIN cell+ @ dup 20 u>
  615:                                 IF drop nl S" BEGIN " .struc level+
  616:                                 ELSE
  617:                                   dup Disable <> over LeaveCode <> and
  618:                                   IF   WhileCode2 =
  619:                                        IF nl S" THEN " .struc nl ELSE
  620:                                        level- nl S" THEN " .struc nl THEN
  621:                                   ELSE drop THEN
  622:                                 THEN
  623:                                   dup MoreBranchAddr? 0=
  624:                            UNTIL
  625:                         THEN
  626:                   THEN ;
  627: 
  628: : analyse ( a-addr1 -- a-addr2 )
  629:     Branches @ IF BranchTo? THEN
  630:     dup cell+ swap @
  631:     dup >r DoTable r> swap IF drop EXIT THEN
  632:     Display?
  633:     IF
  634: 	.word bl cemit
  635:     ELSE
  636: 	drop
  637:     THEN ;
  638: 
  639: : c-init
  640:         0 YPos ! 0 XPos !
  641:         0 Level ! nlflag off
  642:         BranchTable BranchPointer !
  643:         c-stop off
  644:         Branches on ;
  645: 
  646: : makepass ( a-addr -- )
  647:     c-stop off
  648:     BEGIN
  649: 	analyse
  650: 	c-stop @
  651:     UNTIL drop ;
  652: 
  653: Defer xt-see-xt ( xt -- )
  654: \ this one is just a forward declaration for indirect recursion
  655: 
  656: : .defname ( xt c-addr u -- )
  657:     rot look
  658:     if ( c-addr u nfa )
  659: 	-rot type space .name
  660:     else
  661: 	drop ." noname " type
  662:     then
  663:     space ;
  664: 
  665: Defer discode ( addr u -- ) \ gforth
  666: \G hook for the disassembler: disassemble u bytes of code at addr
  667: ' dump IS discode
  668: 
  669: : next-head ( addr1 -- addr2 ) \ gforth
  670:     \G find the next header starting after addr1, up to here (unreliable).
  671:     here swap u+do
  672: 	i head? -2 and if
  673: 	    i unloop exit
  674: 	then
  675:     cell +loop
  676:     here ;
  677: 
  678: [ifundef] umin \ !! bootstrapping help
  679: : umin ( u1 u2 -- u )
  680:     2dup u>
  681:     if
  682: 	swap
  683:     then
  684:     drop ;
  685: [then]
  686: 
  687: : next-prim ( addr1 -- addr2 ) \ gforth
  688:     \G find the next primitive after addr1 (unreliable)
  689:     1+ >r -1 primstart
  690:     begin ( umin head R: boundary )
  691: 	@ dup
  692:     while
  693: 	tuck name>int >code-address ( head1 umin ca R: boundary )
  694: 	r@ - umin
  695: 	swap
  696:     repeat
  697:     drop dup r@ negate u>=
  698:     \ "umin+boundary within [0,boundary)" = "umin within [-boundary,0)"
  699:     if ( umin R: boundary ) \ no primitive found behind -> use a default length
  700: 	drop 31
  701:     then
  702:     r> + ;
  703: 
  704: : seecode ( xt -- )
  705:     dup s" Code" .defname
  706:     >code-address
  707:     dup in-dictionary? \ user-defined code word?
  708:     if
  709: 	dup next-head
  710:     else
  711: 	dup next-prim
  712:     then
  713:     over - discode
  714:     ." end-code" cr ;
  715: : seeabicode ( xt -- )
  716:     dup s" ABI-Code" .defname
  717:     >body dup dup next-head 
  718:     swap - discode
  719:     ." end-code" cr ;
  720: : seevar ( xt -- )
  721:     s" Variable" .defname cr ;
  722: : seeuser ( xt -- )
  723:     s" User" .defname cr ;
  724: : seecon ( xt -- )
  725:     dup >body ?
  726:     s" Constant" .defname cr ;
  727: : seevalue ( xt -- )
  728:     dup >body ?
  729:     s" Value" .defname cr ;
  730: : seedefer ( xt -- )
  731:     dup >body @ xt-see-xt cr
  732:     dup s" Defer" .defname cr
  733:     >name ?dup-if
  734: 	." IS " .name cr
  735:     else
  736: 	." latestxt >body !"
  737:     then ;
  738: : see-threaded ( addr -- )
  739:     C-Pass @ DebugMode = IF
  740: 	ScanMode c-pass !
  741: 	EXIT
  742:     THEN
  743:     ScanMode c-pass ! dup makepass
  744:     DisplayMode c-pass ! makepass ;
  745: : seedoes ( xt -- )
  746:     dup s" create" .defname cr
  747:     S" DOES> " Com# .string XPos @ Level !
  748:     >does-code see-threaded ;
  749: : seecol ( xt -- )
  750:     dup s" :" .defname nl
  751:     2 Level !
  752:     >body see-threaded ;
  753: : seefield ( xt -- )
  754:     dup >body ." 0 " ? ." 0 0 "
  755:     s" Field" .defname cr ;
  756: 
  757: : xt-see ( xt -- ) \ gforth
  758:     \G Decompile the definition represented by @i{xt}.
  759:     cr c-init
  760:     dup >does-code
  761:     if
  762: 	seedoes EXIT
  763:     then
  764:     dup xtprim?
  765:     if
  766: 	seecode EXIT
  767:     then
  768:     dup >code-address
  769:     CASE
  770: 	docon: of seecon endof
  771: [IFDEF] dovalue:
  772:         dovalue: of seevalue endof
  773: [THEN]
  774: 	docol: of seecol endof
  775: 	dovar: of seevar endof
  776: [IFDEF] douser:
  777: 	douser: of seeuser endof
  778: [THEN]
  779: [IFDEF] dodefer:
  780: 	dodefer: of seedefer endof
  781: [THEN]
  782: [IFDEF] dofield:
  783: 	dofield: of seefield endof
  784: [THEN]
  785: [IFDEF] doabicode:
  786:         doabicode: of seeabicode endof
  787: [THEN]
  788: 	over       of seecode endof \ direct threaded code words
  789: 	over >body of seecode endof \ indirect threaded code words
  790: 	2drop abort" unknown word type"
  791:     ENDCASE ;
  792: 
  793: : (xt-see-xt) ( xt -- )
  794:     xt-see cr ." latestxt" ;
  795: ' (xt-see-xt) is xt-see-xt
  796: 
  797: : (.immediate) ( xt -- )
  798:     ['] execute = if
  799: 	."  immediate"
  800:     then ;
  801: 
  802: : name-see ( nfa -- )
  803:     dup name>int >r
  804:     dup name>comp 
  805:     over r@ =
  806:     if \ normal or immediate word
  807: 	swap xt-see (.immediate)
  808:     else
  809: 	r@ ['] ticking-compile-only-error =
  810: 	if \ compile-only word
  811: 	    swap xt-see (.immediate) ."  compile-only"
  812: 	else \ interpret/compile word
  813: 	    r@ xt-see-xt cr
  814: 	    swap xt-see-xt cr
  815: 	    ." interpret/compile: " over .name drop
  816: 	then
  817:     then
  818:     rdrop drop ;
  819: 
  820: : see ( "<spaces>name" -- ) \ tools
  821:     \G Locate @var{name} using the current search order. Display the
  822:     \G definition of @var{name}. Since this is achieved by decompiling
  823:     \G the definition, the formatting is mechanised and some source
  824:     \G information (comments, interpreted sequences within definitions
  825:     \G etc.) is lost.
  826:     name find-name dup 0=
  827:     IF
  828: 	drop -&13 throw
  829:     THEN
  830:     name-see ;
  831: 
  832: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>