home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / love / chap03.doc < prev    next >
Text File  |  1993-04-11  |  15KB  |  364 lines

  1. Chapter3                L.O.V.E. FORTH
  2.  
  3.  
  4.  
  5. 3.0  L.O.V.E. Forth addressing and segmentation
  6.      ------------------------------------------
  7.  
  8.  
  9.      Almost all languages have problems running on the 8086/88 CPUs, but
  10. these problems for FORTH are especially severe. Most FORTH systems on
  11. this architecture are restricted to 64K of main memory for program and
  12. data, and are referred to as small memory models. This restricts the
  13. user and programs to a small amount of memory, but offers the highest
  14. possible execution speed.  32 bit FORTHS have been produced that offer
  15. a large address space, but performance has been severely degraded.  The
  16. segmentation approach taken in L.O.V.E. Forth offers both a large memory
  17. size ( 320K ) and very fast execution speed.
  18.  
  19.      Rather than offer a large contiguous memory space, L.O.V.E. Forth
  20. has divided up the forth model by function. There are separate
  21. segments for machine code, threaded addresses, data, stacks and
  22. dictionary headers.  As the source code is compiled, it is parcelled
  23. into these five segments. There is no execution time penalty over
  24. Forths with the small memory model.
  25.  
  26.  
  27.      Note that this implementation is quite compatible with standard
  28. 16 bit models.  For example, @ (fetch) and !  (store) access the data
  29. segment (the vast majority of FORTH programs use @ and !  to access
  30. data).  Another example is that the assembler always puts its code into
  31. the code segment. The programmer need not worry that the code has been
  32. separated from the rest of the program.  Even though segmentation is
  33. provided for in a logical fashion, some compiler words must be
  34. implemented differently than in standard FORTH.
  35.  
  36.      There are numerous indirect benefits to this segmentation, over
  37. and above that of memory conservation.  Target systems can easily be
  38. saved without heads (the head segment is simply not written to disk).
  39. The segments can be compressed to provide small target systems.  And
  40. because machine code is separated from threads, it is actually possible
  41. to save space in the thread segment by re-coding some words in machine
  42. code.  (The thread segment always fills the fastest). This gives
  43. simultaneously a speed and size advantage.
  44.  
  45.      Note that this conforms closely to the intended usage of the
  46. architecture of 8086/88 microprocessors.  The ususal programming battle
  47. with these processors is to overcome this limited architecture.
  48.  
  49. Here is a summary of the contents of each segment:
  50.  
  51. 3.1  Segment    Description                                     Name
  52.      -------    -----------                                     ----
  53.  
  54. CODE            Contains 8086 machine code                       CS:
  55.                 pointed to by CS register
  56.  
  57. THREAD          Contains threaded address lists generated by     TS:
  58.                 high level words.
  59.                 The code field address points here.
  60.                 pointed to by DS register
  61.  
  62. DATA            holds data from variables, alphanumeric strings, VS:
  63.                 and block buffers.
  64.                 pointed to by ES register
  65.  
  66. HEAD            holds the compile-time word headers, and         HS:
  67.                 vocabulary links.
  68.                 (segment value calculated when req'd)
  69.  
  70. STACK           holds the parameter, return and vocabulary       SS:
  71.                 stacks and local variables, if used.
  72.                 pointed to by SS register
  73.  
  74.      Each segment has a corresponding dictionary pointer, and a set
  75. of basic manipulation words such as   CS:@   or   HS:, . Note that all the
  76. addresses within these segments are 16 bits.   The programmer must
  77. specify the segment to be operated upon by the type of operator used
  78. (eg. @   TS:@  CS:@   etc.)
  79.  
  80.      As MS-DOS tends to vary the position in RAM at which a program
  81. is loaded, each segment also has a word to return the actual position of
  82. the segment     GET:CS   GET:SS   etc.  The handy command    MEM-MAP
  83. displays  all the segments and their respective dictionary pointers.
  84.  
  85.      In this documentation and elsewhere, addresses are abbreviated.
  86. For example TS:addr represents an address in the thread segment.
  87. Simply 'addr' refers to the the variable segment (most often used).
  88. Some names assume a segment, for example 'compilation address' is always
  89. in the thread segment; name field address is always in the head segment.
  90.  
  91.  
  92.  
  93. 3.2  CODE SEGMENT
  94.      ------------
  95.  
  96.  
  97.      This is the only segment that contains 8086/8088 machine code.
  98. Apart from the space taken by a few pointers used in     CREATE   DOES>
  99. words, this allows code to reach a full 64K.  The assembler places the
  100. definition body into the code segment automatically.
  101.  
  102.      This is always the lowest of the 5 segments.  Startup code in
  103.      this segment, sets up the other segments.
  104.      This segment contains the  MS-DOS "PSP"
  105.      (program segment prefix) in the first 256 bytes, in
  106.      version 1.28 and prior ones.   Use    GET:PSP
  107.      in newer versions.
  108.  
  109. Basic operators:
  110.         CS:C@  CS:@  CS:!  CS:C!  CS:,  CS:C,  CS:HERE
  111.  
  112.      These are analogous to the standard words: C@ @ ! C! , C, and
  113.      HERE   but operate on the code segment.
  114.  
  115.      'CODE    operates like ' but returns the address of the executed
  116.      code extracted from the compilation address.   For example all :
  117.      words return the same value from   'CODE   because they all call
  118.      the common code for nesting colon definitions.  It is thus most
  119.      useful with    CODE    words, where it returns the address of the
  120.      code loaded by the assembler.
  121.      CS:DUMP is a utility that allows bytes to be dumped from this
  122.      segment.     ( CS:addr,  #bytes  -- )
  123.  
  124.      There are also some system 'variables' which are used, for
  125.      example, at start-up before all the segments have been loaded or
  126.      properly positioned.
  127.  
  128.      TOPSEG STACKSIZE TOPSEG  SEGPAK  LOVEF
  129.      CSEG   TSEG VSEG HSEG SSEG
  130.  
  131.      The current segment (position in RAM) is returned by:
  132.      GET:CS (8086 CS register)
  133.  
  134.  
  135. 3.3  THREAD SEGMENT
  136.      --------------
  137.  
  138.      Forth high-level  :  words are compiled into a sequence of 16
  139.      bit addresses, called threads. This segment contains these threads,
  140.      CONSTANT   and   LITERAL  values, and pointers to data and code.
  141.      In the majority of applications this segment fills up the fastest.
  142.  
  143. Basic operators:
  144.      TS:@ TS:! TS:,  TS:HERE
  145.      Note that there are no single byte operators - all elements in
  146.      this segment are two bytes.
  147.  
  148.      EXECUTE      ( TS:addr  --  )
  149.      Accepts the code field address.
  150.  
  151.      TS:DUMP      ( TS:addr, #bytes --  )
  152.      Dumps bytes from the specified address.
  153.  
  154.      Many words with compile-time usage accept or return addresses in
  155.      this segment:
  156.  
  157.         '  [']   -FIND    (  --  TS:addr )
  158.  
  159.         FIND    ( VS:addr  --  VS:addr, 0    or    TS:addr, n  )
  160.  
  161.      Words created with the following return a thread segment address
  162.      at run-time:
  163.      CREATE:   (alone)   or  CREATE: DOES:> ( pair)
  164.      The most often used words for creation are CREATE and
  165.      CREATE DOES> (pair).  See the Variable segment (below).
  166.  
  167.      In addition the following words add to this segment and have
  168.      functions as expected:
  169.      COMPILE   [COMPILE] wordname   LITERAL    DLITERAL
  170.  
  171.      See also the technical note on L.O.V.E. Forth compatibility for
  172.      examples of compile-time word usage.
  173.  
  174.      TS:BODY> TS:>BODY   ( TS: addr   --   TS: addr   )
  175.      are like >BODY  and  BODY> but operate on the thread segment
  176.      only.  (see discussion of 'Field access operators' below)
  177.  
  178.      operates in LOVE Forth to accept a code field address of a
  179.      >BODY     ( TS:addr  --   VS:addr  )
  180.      VARIABLE (or word created by CREATE) and return the data field
  181.      address.
  182.  
  183.      >LINK  >NAME  ( TS:addr  --  HS:addr  )
  184.      are used to access the dictionary header of the specified word.
  185.      If TS:addr is not a valid code field address, an error message
  186.      is displayed.
  187.  
  188.      NAME>  LINK>  ( HS:addr   --  TS:addr  )
  189.      are used to find the compilation address from the head address.
  190.  
  191.      FIND-1VOC  FIND-VOCS
  192.      ( addr, addr  --  TS:addr,true  or false)
  193.      are used by FIND - address of word to find (usually at
  194.      HERE) and vocab body input and cfa output (if found).
  195.  
  196.      GET:TS - returns current segment value (8086 DS register)
  197.  
  198.  
  199. 3.4  VARIABLE (DATA) SEGMENT
  200.      -----------------------
  201.  
  202.      This segment is accessed the most often by application programs.
  203. This contains the data for variables, alphanumeric strings compiled by
  204. ."   "   ,   BLOCK  buffers   text input buffer TIB   PAD   HERE
  205. and where space is allocated for programmer defined data structures.
  206. Most standard Forth memory access words work relative to this segment.
  207.  
  208. Basic operators:
  209.         @ ! C@ C!  C, , D@ D! +! +C!  TYPE ALLOT
  210.      TOGGLE BMOVE CMOVE CMOVE> FILL
  211.      ENCLOSE EXPECT COUNT TYPE -TRAILING
  212.      CONVERT NUMBER  #>
  213.      HERE  PAD  WORD
  214.      BLOCK  LIMIT  FIRST  BUFFER +BUF R/W
  215.  
  216.      Various I/O words:
  217.      L->CRT N$ N$. 'STREAM
  218.  
  219.      TIB  HLD and other VARIABLEs all return addresses in VS:
  220.  
  221.      File name strings passed into DOS words:
  222.      <OPEN> OPEN <CREATE> FCREATE INQUIRE <CREATE-NEW> CREATE-NEW
  223.      DELETE RENAME CHDIR
  224.  
  225.      Other DOS words:
  226.      READ WRITE ENV-SRCH DIR-GET ASCIIZ. ASCIIZ"
  227.  
  228.      -words created by VARIABLE DVARIABLE CREATE
  229.      CREATE ... DOES>
  230.  
  231.      DUMP ( addr, #bytes-- ) Dumps the specified bytes.
  232.  
  233.      GET:VS - returns current segment value (8086 ES register)
  234.  
  235.  
  236. 3.5  HEAD SEGMENT
  237.      ------------
  238.  
  239.      The head segment is normally used during compilation only.
  240. It contains the header part of a Forth word definition, including name,
  241. dictionary links and pointers to the locations of the word in other
  242. segements.   This segment may be discarded when creating a stand-alone
  243. application program.  Utilities such as   WORDS   and   FORGET   access this
  244. segment automatically.
  245.  
  246. Basic operators:
  247.      HS:@   HS:!   HS:C@    HS:C!   HS:,  HS:C,  TRAVERSE
  248.      N>LINK  L>NAME  LINK>  NAME>
  249.      .ID   LAST
  250.      HS:HERE returns the next available address in this segment.
  251.  
  252.      GET:HS - returns current head segment value (calculated)
  253.      Note   TOGGLE   does not act on HS: (often used to toggle header
  254.      bits)
  255. Note: the form of the head segment is subject to change in future
  256.       versions by the authors without prior notice.
  257.  
  258.  
  259. 3.6  STACK SEGMENT
  260.      -------------
  261.  
  262.      This segment holds the Forth parameter, return, vocabulary and
  263. local variables stacks.  The operation of words on this segment is
  264. transparent to the programmer.  During development, allowing a full 64K
  265. to the stack segment means that system crashes due to stack overflow are
  266. minimized.
  267.         Basic operators: SS:@  SS:!     .S
  268.  
  269.      SP@  RP@  LP@  ( -- SS:addr )
  270.      These words return stack limits or current positions
  271.      S0
  272.      is a variable that contains the address of the bottom of stack
  273.  
  274.      SS:HERE
  275.      Is the dictonary pointer in this segment, but is currently
  276.      unused by any words in L.O.V.E. Forth and may be used by
  277.      the programmer if so desired.
  278.  
  279.      GET:SS - returns current segment value (8086 SS register)
  280.  
  281.  
  282. 3.7  Field Access Operators
  283.      ----------------------
  284.  
  285.  
  286.      Every word in Forth has a number of parts or fields.  These
  287. include the name, link, code and parameter fields.  Field access
  288. operators are used to gain access to the various portions of forth
  289. words. In L.O.V.E. Forth, as the parts of words are parcelled between
  290. segments, many of these operators accept an address in one segment and
  291. deliver an address in another.   Here is a summary of the standard
  292. field access operators and their functions in L.O.V.E. Forth.
  293.  
  294.      >BODY     ( TS:addr  --  addr  )
  295.      accepts a code field address of a VARIABLE (or word created by
  296.      CREATE   and returns the data field address (in VS:) .
  297.  
  298.      TS:>BODY TS:BODY>   ( TS: addr   --   TS: addr   )
  299.      are like >BODY  and  BODY> but operate on the thread segment
  300.      only.   Given the compilation address, TS:>BODY returns the
  301.      address of the first threaded address (of a : definition), the
  302.      data field of a   CONSTANT   or the address pointer of a
  303.      VARIABLE  .
  304.      Note that there are thus two types of >BODY  .  >BODY could be
  305.      rewritten:
  306.                 : >BODY TS:>BODY TS:@ ;
  307.  
  308.      >LINK  >NAME  ( TS:addr  --  HS:addr  )
  309.      are used to access the dictionary header of the specified word.
  310.      If TS:addr is not a valid compilation address, an error message
  311.      is displayed and execution is ABORTED.
  312.  
  313.      NAME> LINK>  ( HS:addr   --  TS:addr  )
  314.      are used to find the compilation address from the header
  315.      addresses name and link fields.
  316.  
  317.      N>LINK L>NAME ( HS:addr  --  HS:addr  )
  318.      are used to move between the name and link fields which are
  319.      both in the head segment.
  320.  
  321.      Note that there is no word BODY> to move from the VS: parameter
  322.      address of a   VARIABLE   or   CREATED   word to the compilation
  323.      address.  This is not supported in L.O.V.E. Forth.
  324.  
  325.  
  326. 3.8  Long Operators
  327.      --------------
  328.  
  329.      L.O.V.E. Forth contains a set of basic operators which operate on
  330. any area of memory.  These words allow the specification of both the
  331. segment and address of the word to be operated upon.
  332.  
  333. Basic operators: @L  !L  C@L  C!L  BMOVEL
  334.  
  335.      Some disk operators will operate on any segment:
  336.      READL WRITEL <READL> <WRITEL>
  337.      RWTSL EXEC
  338.  
  339.      ENV-SRCH ( string -- seg, addr, f     or     t )
  340.      returns both segment and address of DOS environment
  341.  
  342.      DUMPL ( seg,addr,#bytes -- )
  343.      Allows memory to be dumped relative to any segment.
  344.  
  345.  
  346. 3.9  Memory map
  347.      ----------
  348.  
  349.      The dictionary pointers move up as more is compiled.  Certain
  350. words only use certain segments (eg. a   CONSTANT   occupies only the thread
  351. and head segments).   When any of the dictionary pointers reaches within
  352. 400 bytes of the maximum available address a warning message is
  353. displayed     'GETTING CLOSE TO FULL'.
  354.  
  355.         The maximum available address in each segment is dependent on
  356. several things.  Virtual vocabularies are loaded in high memory; disk
  357. buffers are also here (in the VS: only - minimum of 2k bytes).  The
  358. current maximum addresses are always stored in the     VARIABLE TOPS
  359. (contains one cell for each of    CS:  TS: VS:  and HS:).
  360. If the program   is very large, it is best to remove any resident virtual
  361. vocabulary with     FORGET-SYS  .
  362.  
  363.  
  364.