Annotation of gforth/see.fs, revision 1.68

1.1       anton       1: \ SEE.FS       highend SEE for ANSforth                16may93jaw
                      2: 
1.65      anton       3: \ Copyright (C) 1995,2000,2003,2004,2006,2007,2008 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
1.63      anton       9: \ as published by the Free Software Foundation, either version 3
1.9       anton      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
1.63      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.9       anton      19: 
                     20: 
1.1       anton      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: 
1.18      jwilke     27: require look.fs
1.10      anton      28: require termsize.fs
1.18      jwilke     29: require wordinfo.fs
1.10      anton      30: 
1.1       anton      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
1.15      pazsan     54: VARIABLE uppercase     \ structure words are in uppercase
1.1       anton      55: 
                     56: DEFER nlcount ' noop IS nlcount
                     57: 
                     58: : nl            nlflag on ;
                     59: : (nl)          nlcount
1.18      jwilke     60:                 XPos @ Level @ = IF EXIT THEN \ ?Exit
1.1       anton      61:                 C-Formated @ IF
                     62:                 C-Output @
1.10      anton      63:                 IF C-Clearline @ IF cols XPos @ - spaces
1.1       anton      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
1.10      anton      71:                 XPos @ over + cols u>= IF (nl) THEN ;
1.1       anton      72: 
                     73: : ctype         ( adr len -- )
1.15      pazsan     74:                 warp? dup XPos +! C-Output @ 
1.58      anton      75:                IF uppercase @ IF bounds ?DO i c@ toupper emit LOOP
1.15      pazsan     76:                                  uppercase off ELSE type THEN
                     77:                ELSE 2drop THEN ;
1.1       anton      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: 
1.34      anton      84: DEFER .string ( c-addr u n -- )
1.1       anton      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: 
1.45      anton     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
1.60      anton     122:                        restore
                    123:                            r@ base !
1.45      anton     124:                        endtry
1.60      anton     125:                        rdrop throw
1.45      anton     126:                    endif
                    127:                endif
                    128:                1 /string
                    129:            endif
                    130:     repeat
                    131:     2drop ;
1.1       anton     132: 
1.15      pazsan    133: : .struc        
                    134:        uppercase on Str# .string ;
1.1       anton     135: 
1.17      jwilke    136: \ CODES (Branchtypes)                                    15may93jaw
1.1       anton     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
1.17      jwilke    146: 15 CONSTANT LeaveCode
                    147: 
1.1       anton     148: 
                    149: \ FORMAT WORDS                                          13jun93jaw
                    150: 
                    151: VARIABLE C-Stop
                    152: VARIABLE Branches
                    153: 
1.17      jwilke    154: VARIABLE BranchPointer \ point to the end of branch table
1.1       anton     155: VARIABLE SearchPointer
1.17      jwilke    156: 
                    157: \ The branchtable consists of three entrys:
                    158: \ address of branch , branch destination , branch type
                    159: 
1.25      pazsan    160: CREATE BranchTable 128 cells allot
1.1       anton     161: here 3 cells -
                    162: ACONSTANT MaxTable
                    163: 
                    164: : FirstBranch BranchTable cell+ SearchPointer ! ;
                    165: 
1.17      jwilke    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
1.1       anton     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: 
1.17      jwilke    198: : MyBranch      ( a-addr -- a-addr a-addr2 )
                    199: \ finds branch table entry for branch at a-addr
1.45      anton     200:                 dup @
1.17      jwilke    201:                 BranchAddr?
                    202:                 BEGIN
                    203:                 WHILE 1 cells - @
                    204:                       over <>
1.45      anton     205:                 WHILE dup @
1.17      jwilke    206:                       MoreBranchAddr?
                    207:                 REPEAT
                    208:                 SearchPointer @ 3 cells -
                    209:                 ELSE    true ABORT" SEE: Table failure"
                    210:                 THEN ;
                    211: 
1.1       anton     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 )
1.45      anton     243:     over ,Branch ,Branch 0 ,Branch ;
                    244: \        over + over ,Branch ,Branch 0 ,Branch ;
1.1       anton     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: 
1.45      anton     258: : back? ( addr target -- addr flag )
                    259:     over u< ;
1.1       anton     260: 
1.47      anton     261: : .word ( addr x -- addr )
                    262:     \ print x as a word if possible
                    263:     dup look 0= IF
1.48      anton     264:        drop dup threaded>name dup 0= if
1.57      anton     265:            drop over 1 cells - @ dup body> look
1.47      anton     266:            IF
1.57      anton     267:                nip nip dup ." <" name>string rot wordinfo .string ." > "
1.47      anton     268:            ELSE
1.57      anton     269:                2drop ." <" 0 .r ." > "
1.47      anton     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:     ;
1.35      pazsan    280: 
1.44      anton     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: : c-lit ( addr1 -- addr2 )
                    297:     Display? IF
                    298:        dup @ dup body> dup cfaligned over = swap in-dictionary? and if
                    299:            ( addr1 addr1@ )
                    300:            dup body> @ dovar: = if
                    301:                drop c-call EXIT
                    302:            endif
                    303:        endif
                    304:        \ !! test for cfa here, and print "['] ..."
                    305:        dup abs 0 <# #S rot sign #> 0 .string bl cemit
                    306:     endif
                    307:     cell+ ;
                    308: 
                    309: : c-lit+ ( addr1 -- addr2 )
                    310:     Display? if
                    311:        dup @ dup abs 0 <# #S rot sign #> 0 .string bl cemit
                    312:        s" + " 0 .string
                    313:     endif
                    314:     cell+ ;
1.35      pazsan    315: 
1.18      jwilke    316: : .name-without ( addr -- addr )
1.48      anton     317:     \ !! the stack effect cannot be correct
                    318:     \ prints a name without a() e.g. a(+LOOP) or (s")
                    319:     dup 1 cells - @ threaded>name dup IF
1.45      anton     320:        name>string over c@ 'a = IF
                    321:            1 /string
                    322:        THEN
                    323:         over c@ '( = IF
                    324:            1 /string
                    325:        THEN
                    326:        2dup + 1- c@ ') = IF 1- THEN .struc ELSE drop 
                    327:     THEN ;
1.1       anton     328: 
1.45      anton     329: [ifdef] (s")
1.1       anton     330: : c-c"
1.18      jwilke    331:        Display? IF nl .name-without THEN
1.1       anton     332:         count 2dup + aligned -rot
                    333:         Display?
1.18      jwilke    334:         IF      bl cemit 0 .string
1.1       anton     335:                 [char] " cemit bl cemit
                    336:         ELSE    2drop
                    337:         THEN ;
1.45      anton     338: [endif]
1.1       anton     339: 
1.45      anton     340: : c-string? ( addr1 -- addr2 f )
                    341:     \ f is true if a string was found and decompiled.
                    342:     \ if f is false, addr2=addr1
                    343:     \ recognizes the following patterns:
                    344:     \ c":     ahead X: len string then lit X
1.49      anton     345:     \ flit:   ahead X: float      then lit X f@
                    346:     \ s\":    ahead X: string     then lit X lit len
                    347:     \ .\":    ahead X: string     then lit X lit len type
1.45      anton     348:     \ !! not recognized anywhere:
                    349:     \ abort": if ahead X: len string then lit X c(abort") then
                    350:     dup @ back? if false exit endif
                    351:     dup @ >r
                    352:     r@ @ decompile-prim ['] lit xt>threaded <> if rdrop false exit endif
                    353:     r@ cell+ @ over cell+ <> if rdrop false exit endif
                    354:     \ we have at least C"
1.49      anton     355:     r@ 2 cells + @ decompile-prim dup ['] lit xt>threaded = if
                    356:        drop r@ 3 cells + @ over cell+ + aligned r@ = if
1.45      anton     357:            \ we have at least s"
                    358:            r@ 4 cells + @ decompile-prim ['] lit-perform xt>threaded =
                    359:            r@ 5 cells + @ ['] type >body = and if
                    360:                6 s\" .\\\" "
                    361:            else
                    362:                4 s\" s\\\" "
                    363:            endif
                    364:            \ !! make newline if string too long?
                    365:            display? if
                    366:                0 .string r@ cell+ @ r@ 3 cells + @ c-\type '" cemit bl cemit
                    367:            else
                    368:                2drop
                    369:            endif
                    370:            nip cells r> + true exit
                    371:        endif
1.49      anton     372:     endif
                    373:     ['] f@ xt>threaded = if
                    374:        display? if
                    375:            r@ cell+ @ f@ 10 8 16 f>str-rdp 0 .string bl cemit
                    376:        endif
                    377:        drop r> 3 cells + true exit
1.45      anton     378:     endif
                    379:     \ !! check if count matches space?
                    380:     display? if
                    381:        s\" c\" " 0 .string r@ cell+ @ count 0 .string '" cemit bl cemit
                    382:     endif
                    383:     drop r> 2 cells + true ;
1.1       anton     384: 
1.17      jwilke    385: : Forward? ( a-addr true | false -- a-addr true | false )
1.45      anton     386:     \ a-addr is pointer into branch table
                    387:     \ returns true when jump is a forward jump
                    388:     IF
                    389:        dup dup @ swap 1 cells - @ u> IF
                    390:            true
                    391:        ELSE
                    392:            drop false
                    393:        THEN
                    394:        \ only if forward jump
                    395:     ELSE
                    396:        false
                    397:     THEN ;
1.1       anton     398: 
1.17      jwilke    399: : RepeatCheck ( a-addr1 a-addr2 true | false -- false )
1.1       anton     400:         IF  BEGIN  2dup
1.45      anton     401:                    1 cells - @ swap @
1.1       anton     402:                    u<=
                    403:             WHILE  drop dup cell+
                    404:                    MoreBranchAddr? 0=
                    405:             UNTIL  false
                    406:             ELSE   true
                    407:             THEN
                    408:         ELSE false
                    409:         THEN ;
                    410: 
1.45      anton     411: : c-branch ( addr1 -- addr2 )
                    412:     c-string? ?exit
1.1       anton     413:         Scan?
                    414:         IF      dup @ Branch!
                    415:                 dup @ back?
                    416:                 IF                      \ might be: AGAIN, REPEAT
                    417:                         dup cell+ BranchAddr? Forward?
                    418:                         RepeatCheck
                    419:                         IF      RepeatCode Type!
                    420:                                 cell+ Disable swap !
                    421:                         ELSE    AgainCode Type!
                    422:                         THEN
                    423:                 ELSE    dup cell+ BranchAddr? Forward?
                    424:                         IF      ElseCode Type! drop
                    425:                         ELSE    AheadCode Type!
                    426:                         THEN
                    427:                 THEN
                    428:         THEN
                    429:         Display?
                    430:         IF
                    431:                 dup @ back?
                    432:                 IF                      \ might be: AGAIN, REPEAT
                    433:                         level- nl
                    434:                         dup cell+ BranchAddr? Forward?
                    435:                         RepeatCheck
                    436:                         IF      drop S" REPEAT " .struc nl
                    437:                         ELSE    S" AGAIN " .struc nl
                    438:                         THEN
1.17      jwilke    439:                 ELSE    MyBranch cell+ @ LeaveCode =
                    440:                        IF      S" LEAVE " .struc
                    441:                        ELSE
                    442:                                dup cell+ BranchAddr? Forward?
                    443:                                        IF      dup cell+ @ WhileCode2 =
                    444:                                                IF nl S" ELSE" .struc level+
                    445:                                        ELSE level- nl S" ELSE" .struc level+ THEN
                    446:                                        cell+ Disable swap !
                    447:                                ELSE    S" AHEAD" .struc level+
                    448:                                THEN
                    449:                        THEN
1.1       anton     450:                 THEN
                    451:         THEN
                    452:         Debug?
1.54      pazsan    453:         IF      @ \ !!! cross-interacts with debugger !!!
1.1       anton     454:         ELSE    cell+
                    455:         THEN ;
                    456: 
                    457: : DebugBranch
                    458:         Debug?
1.54      pazsan    459:         IF      dup @ swap THEN ; \ return 2 different addresses
1.1       anton     460: 
                    461: : c-?branch
                    462:         Scan?
                    463:         IF      dup @ Branch!
                    464:                 dup @ Back?
                    465:                 IF      UntilCode Type! THEN
                    466:         THEN
                    467:         Display?
                    468:         IF      dup @ Back?
                    469:                 IF      level- nl S" UNTIL " .struc nl
                    470:                 ELSE    dup    dup @ over +
                    471:                         CheckWhile
                    472:                         IF      MyBranch
                    473:                                 cell+ dup @ 0=
                    474:                                          IF WhileCode2 swap !
                    475:                                          ELSE drop THEN
                    476:                                 level- nl
1.8       pazsan    477:                                 S" WHILE " .struc
1.1       anton     478:                                 level+
1.17      jwilke    479:                         ELSE    MyBranch cell+ @ LeaveCode =
                    480:                                IF   s" 0= ?LEAVE " .struc
                    481:                                ELSE nl S" IF " .struc level+
                    482:                                THEN
1.1       anton     483:                         THEN
                    484:                 THEN
                    485:         THEN
                    486:         DebugBranch
                    487:         cell+ ;
                    488: 
                    489: : c-for
                    490:         Display? IF nl S" FOR" .struc level+ THEN ;
                    491: 
                    492: : c-loop
1.54      pazsan    493:         Display? IF level- nl .name-without nl bl cemit THEN
1.17      jwilke    494:         DebugBranch cell+ 
                    495:        Scan? 
                    496:        IF      dup BranchAddr? 
                    497:                BEGIN   WHILE cell+ LeaveCode swap !
                    498:                        dup MoreBranchAddr?
                    499:                REPEAT
                    500:        THEN
                    501:        cell+ ;
1.1       anton     502: 
1.15      pazsan    503: : c-do
                    504:         Display? IF nl .name-without level+ THEN ;
1.1       anton     505: 
1.45      anton     506: : c-?do ( addr1 -- addr2 )
                    507:     Display? IF
                    508:        nl .name-without level+
                    509:     THEN
                    510:     DebugBranch cell+ ;
1.8       pazsan    511: 
1.54      pazsan    512: : c-exit ( addr1 -- addr2 )
                    513:     dup 1 cells -
                    514:     CheckEnd
                    515:     IF
                    516:        Display? IF nlflag off S" ;" Com# .string THEN
                    517:        C-Stop on
                    518:     ELSE
                    519:        Display? IF S" EXIT " .struc THEN
                    520:     THEN
                    521:     Debug? IF drop THEN ; \ !!! cross-interacts with debugger !!!
1.1       anton     522: 
                    523: : c-abort"
                    524:         count 2dup + aligned -rot
                    525:         Display?
                    526:         IF      S" ABORT" .struc
                    527:                 [char] " cemit bl cemit 0 .string
                    528:                 [char] " cemit bl cemit
                    529:         ELSE    2drop
                    530:         THEN ;
                    531: 
1.67      pazsan    532: [IFDEF] !does
1.23      jwilke    533: : c-does>               \ end of create part
1.66      anton     534:         Display? IF S" DOES> " Com# .string THEN ;
                    535: \      maxaligned /does-handler + ; \ !! no longer needed for non-cross stuff
1.23      jwilke    536: [THEN]
                    537: 
                    538: [IFDEF] (compile)
                    539: : c-(compile)
                    540:     Display?
                    541:     IF
                    542:        s" POSTPONE " Com# .string
                    543:        dup @ look 0= ABORT" SEE: No valid XT"
                    544:        name>string 0 .string bl cemit
                    545:     THEN
                    546:     cell+ ;
                    547: [THEN]
1.1       anton     548: 
                    549: CREATE C-Table
1.18      jwilke    550:                ' lit A,            ' c-lit A,
1.44      anton     551:                ' does-exec A,      ' c-callxt A,
                    552:                ' lit@ A,           ' c-call A,
1.37      pazsan    553: [IFDEF] call   ' call A,           ' c-call A, [THEN]
1.44      anton     554: \              ' useraddr A,       ....
                    555:                ' lit-perform A,    ' c-call A,
                    556:                ' lit+ A,           ' c-lit+ A,
1.42      anton     557: [IFDEF] (s")   ' (s") A,           ' c-c" A, [THEN]
                    558: [IFDEF] (.")   ' (.") A,           ' c-c" A, [THEN]
                    559: [IFDEF] "lit    ' "lit A,           ' c-c" A, [THEN]
1.18      jwilke    560: [IFDEF] (c")   ' (c") A,           ' c-c" A, [THEN]
                    561:                ' (do) A,           ' c-do A,
1.46      pazsan    562: [IFDEF] (+do)  ' (+do) A,          ' c-?do A, [THEN]
                    563: [IFDEF] (u+do) ' (u+do) A,         ' c-?do A, [THEN]
                    564: [IFDEF] (-do)  ' (-do) A,          ' c-?do A, [THEN]
                    565: [IFDEF] (u-do) ' (u-do) A,         ' c-?do A, [THEN]
                    566:                ' (?do) A,          ' c-?do A,
1.18      jwilke    567:                ' (for) A,          ' c-for A,
1.46      pazsan    568:                ' ?branch A,        ' c-?branch A,
                    569:                ' branch A,         ' c-branch A,
                    570:                ' (loop) A,         ' c-loop A,
                    571:                ' (+loop) A,        ' c-loop A,
                    572: [IFDEF] (s+loop) ' (s+loop) A,      ' c-loop A, [THEN]
                    573: [IFDEF] (-loop) ' (-loop) A,        ' c-loop A, [THEN]
                    574:                ' (next) A,         ' c-loop A,
1.18      jwilke    575:                ' ;s A,             ' c-exit A,
1.42      anton     576: [IFDEF] (abort") ' (abort") A,      ' c-abort" A, [THEN]
1.23      jwilke    577: \ only defined if compiler is loaded
                    578: [IFDEF] (compile) ' (compile) A,      ' c-(compile) A, [THEN]
1.67      pazsan    579: [IFDEF] !does   ' !does A,          ' c-does> A, [THEN]
1.18      jwilke    580:                0 ,             here 0 ,
1.15      pazsan    581: 
                    582: avariable c-extender
                    583: c-extender !
1.1       anton     584: 
                    585: \ DOTABLE                                               15may93jaw
                    586: 
1.44      anton     587: : DoTable ( ca/cfa -- flag )
                    588:     decompile-prim C-Table BEGIN ( cfa table-entry )
                    589:        dup @ dup 0=  IF
                    590:            drop cell+ @ dup IF ( next table!)
                    591:                dup @
                    592:            ELSE ( end!)
                    593:                2drop false EXIT
                    594:            THEN 
                    595:        THEN
                    596:        \ jump over to extender, if any 26jan97jaw
                    597:        xt>threaded 2 pick <>
                    598:     WHILE
                    599:            2 cells +
                    600:     REPEAT
                    601:     nip cell+ perform
                    602:     true
                    603: ;
1.1       anton     604: 
                    605: : BranchTo? ( a-addr -- a-addr )
1.17      jwilke    606:         Display?  IF    dup BranchAddr?
1.15      pazsan    607:                         IF
                    608:                                BEGIN cell+ @ dup 20 u>
1.1       anton     609:                                 IF drop nl S" BEGIN " .struc level+
                    610:                                 ELSE
1.17      jwilke    611:                                   dup Disable <> over LeaveCode <> and
1.1       anton     612:                                   IF   WhileCode2 =
                    613:                                        IF nl S" THEN " .struc nl ELSE
                    614:                                        level- nl S" THEN " .struc nl THEN
                    615:                                   ELSE drop THEN
                    616:                                 THEN
                    617:                                   dup MoreBranchAddr? 0=
                    618:                            UNTIL
                    619:                         THEN
                    620:                   THEN ;
                    621: 
                    622: : analyse ( a-addr1 -- a-addr2 )
1.34      anton     623:     Branches @ IF BranchTo? THEN
                    624:     dup cell+ swap @
                    625:     dup >r DoTable r> swap IF drop EXIT THEN
                    626:     Display?
                    627:     IF
1.35      pazsan    628:        .word bl cemit
1.34      anton     629:     ELSE
                    630:        drop
                    631:     THEN ;
1.1       anton     632: 
                    633: : c-init
                    634:         0 YPos ! 0 XPos !
                    635:         0 Level ! nlflag off
                    636:         BranchTable BranchPointer !
                    637:         c-stop off
                    638:         Branches on ;
                    639: 
                    640: : makepass ( a-addr -- )
1.14      anton     641:     c-stop off
                    642:     BEGIN
                    643:        analyse
                    644:        c-stop @
                    645:     UNTIL drop ;
                    646: 
                    647: Defer xt-see-xt ( xt -- )
                    648: \ this one is just a forward declaration for indirect recursion
                    649: 
                    650: : .defname ( xt c-addr u -- )
                    651:     rot look
                    652:     if ( c-addr u nfa )
                    653:        -rot type space .name
                    654:     else
                    655:        drop ." noname " type
                    656:     then
                    657:     space ;
                    658: 
1.28      anton     659: Defer discode ( addr u -- ) \ gforth
                    660: \G hook for the disassembler: disassemble code at addr of length u
1.27      anton     661: ' dump IS discode
                    662: 
                    663: : next-head ( addr1 -- addr2 ) \ gforth
                    664:     \G find the next header starting after addr1, up to here (unreliable).
                    665:     here swap u+do
1.43      anton     666:        i head? -2 and if
1.27      anton     667:            i unloop exit
                    668:        then
                    669:     cell +loop
                    670:     here ;
                    671: 
1.55      anton     672: [ifundef] umin \ !! bootstrapping help
1.27      anton     673: : umin ( u1 u2 -- u )
                    674:     2dup u>
                    675:     if
                    676:        swap
                    677:     then
                    678:     drop ;
1.55      anton     679: [then]
                    680: 
1.28      anton     681: : next-prim ( addr1 -- addr2 ) \ gforth
                    682:     \G find the next primitive after addr1 (unreliable)
1.27      anton     683:     1+ >r -1 primstart
                    684:     begin ( umin head R: boundary )
                    685:        @ dup
                    686:     while
1.28      anton     687:        tuck name>int >code-address ( head1 umin ca R: boundary )
1.27      anton     688:        r@ - umin
                    689:        swap
                    690:     repeat
1.28      anton     691:     drop dup r@ negate u>=
                    692:     \ "umin+boundary within [0,boundary)" = "umin within [-boundary,0)"
                    693:     if ( umin R: boundary ) \ no primitive found behind -> use a default length
                    694:        drop 31
                    695:     then
                    696:     r> + ;
1.14      anton     697: 
                    698: : seecode ( xt -- )
                    699:     dup s" Code" .defname
1.39      anton     700:     >code-address
1.27      anton     701:     dup in-dictionary? \ user-defined code word?
                    702:     if
                    703:        dup next-head
                    704:     else
                    705:        dup next-prim
                    706:     then
                    707:     over - discode
                    708:     ." end-code" cr ;
1.68    ! dvdkhlng  709: : seeabicode ( xt -- )
        !           710:     dup s" ABI-Code" .defname
        !           711:     >body dup dup next-head 
        !           712:     swap - discode
        !           713:     ." end-code" cr ;
1.14      anton     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
1.26      anton     727:     >name ?dup-if
                    728:        ." IS " .name cr
1.14      anton     729:     else
1.52      anton     730:        ." latestxt >body !"
1.14      anton     731:     then ;
                    732: : see-threaded ( addr -- )
                    733:     C-Pass @ DebugMode = IF
                    734:        ScanMode c-pass !
                    735:        EXIT
1.10      anton     736:     THEN
                    737:     ScanMode c-pass ! dup makepass
                    738:     DisplayMode c-pass ! makepass ;
1.14      anton     739: : seedoes ( xt -- )
                    740:     dup s" create" .defname cr
                    741:     S" DOES> " Com# .string XPos @ Level !
                    742:     >does-code see-threaded ;
                    743: : seecol ( xt -- )
1.15      pazsan    744:     dup s" :" .defname nl
1.14      anton     745:     2 Level !
                    746:     >body see-threaded ;
                    747: : seefield ( xt -- )
                    748:     dup >body ." 0 " ? ." 0 0 "
                    749:     s" Field" .defname cr ;
                    750: 
1.29      anton     751: : xt-see ( xt -- ) \ gforth
                    752:     \G Decompile the definition represented by @i{xt}.
1.14      anton     753:     cr c-init
                    754:     dup >does-code
                    755:     if
                    756:        seedoes EXIT
                    757:     then
1.18      jwilke    758:     dup xtprim?
1.14      anton     759:     if
                    760:        seecode EXIT
                    761:     then
                    762:     dup >code-address
                    763:     CASE
                    764:        docon: of seecon endof
1.64      anton     765: [IFDEF] dovalue:
                    766:         dovalue: of seevalue endof
                    767: [THEN]
1.14      anton     768:        docol: of seecol endof
                    769:        dovar: of seevar endof
1.64      anton     770: [IFDEF] douser:
1.14      anton     771:        douser: of seeuser endof
1.64      anton     772: [THEN]
                    773: [IFDEF] dodefer:
1.14      anton     774:        dodefer: of seedefer endof
1.64      anton     775: [THEN]
                    776: [IFDEF] dofield:
1.14      anton     777:        dofield: of seefield endof
1.64      anton     778: [THEN]
1.68    ! dvdkhlng  779: [IFDEF] doabicode:
        !           780:         doabicode: of seeabicode endof
        !           781: [THEN]
1.27      anton     782:        over       of seecode endof \ direct threaded code words
                    783:        over >body of seecode endof \ indirect threaded code words
1.14      anton     784:        2drop abort" unknown word type"
                    785:     ENDCASE ;
                    786: 
                    787: : (xt-see-xt) ( xt -- )
1.52      anton     788:     xt-see cr ." latestxt" ;
1.14      anton     789: ' (xt-see-xt) is xt-see-xt
                    790: 
                    791: : (.immediate) ( xt -- )
                    792:     ['] execute = if
                    793:        ."  immediate"
                    794:     then ;
                    795: 
                    796: : name-see ( nfa -- )
                    797:     dup name>int >r
                    798:     dup name>comp 
                    799:     over r@ =
                    800:     if \ normal or immediate word
                    801:        swap xt-see (.immediate)
                    802:     else
1.40      anton     803:        r@ ['] ticking-compile-only-error =
1.14      anton     804:        if \ compile-only word
                    805:            swap xt-see (.immediate) ."  compile-only"
                    806:        else \ interpret/compile word
                    807:            r@ xt-see-xt cr
                    808:            swap xt-see-xt cr
1.53      anton     809:            ." interpret/compile: " over .name drop
1.14      anton     810:        then
                    811:     then
                    812:     rdrop drop ;
1.3       pazsan    813: 
1.21      crook     814: : see ( "<spaces>name" -- ) \ tools
                    815:     \G Locate @var{name} using the current search order. Display the
                    816:     \G definition of @var{name}. Since this is achieved by decompiling
                    817:     \G the definition, the formatting is mechanised and some source
                    818:     \G information (comments, interpreted sequences within definitions
                    819:     \G etc.) is lost.
1.13      anton     820:     name find-name dup 0=
                    821:     IF
1.24      anton     822:        drop -&13 throw
1.13      anton     823:     THEN
1.14      anton     824:     name-see ;
1.1       anton     825: 
                    826: 

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