File:  [gforth] / gforth / see.fs
Revision 1.48: download - view: text, annotated - select for diffs
Mon Jan 6 11:18:59 2003 UTC (21 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Changed interfaces of PRIM>NAME THREADED>NAME to be like >NAME.

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

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