Mirror of CollapseOS
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

230 lines
7.7KB

  1. # Implementation notes
  2. # Execution model
  3. After having read a line through readln, we want to interpret
  4. it. As a general rule, we go like this:
  5. 1. read single word from line
  6. 2. Can we find the word in dict?
  7. 3. If yes, execute that word, goto 1
  8. 4. Is it a number?
  9. 5. If yes, push that number to PS, goto 1
  10. 6. Error: undefined word.
  11. # Executing a word
  12. At its core, executing a word is pushing the wordref on PS and
  13. calling EXECUTE. Then, we let the word do its things. Some
  14. words are special, but most of them are of the "compiled"
  15. type (regular nonnative word), and that's their execution that
  16. we describe here.
  17. First of all, at all time during execution, the Interpreter
  18. Pointer (IP) points to the wordref we're executing next.
  19. When we execute a compiled word, the first thing we do is push
  20. IP to the Return Stack (RS). Therefore, RS' top of stack will
  21. contain a wordref to execute next, after we EXIT.
  22. At the end of every compiled word is an EXIT. This pops RS, sets
  23. IP to it, and continues.
  24. A compiled word is simply a list of wordrefs, but not all those
  25. wordrefs are 2 bytes in length. Some wordrefs are special. For
  26. example, a reference to (n) will be followed by an extra 2 bytes
  27. number. It's the responsibility of the (n) word to advance IP
  28. by 2 extra bytes.
  29. # Stack management
  30. In all supported arches, The Parameter Stack and Return Stack
  31. tops are tracked by a registered assigned to this purpose. For
  32. example, in z80, it's SP and IX that do that. The value in those
  33. registers are referred to as PS Pointer (PSP) and RS Pointer
  34. (RSP).
  35. Those stacks are contiguous and grow in opposite directions. PS
  36. grows "down", RS grows "up".
  37. Stack underflow and overflow: In each native word involving
  38. PS popping, we check whether the stack is big enough. If it's
  39. not we go in "uflw" (underflow) error condition, then abort.
  40. This means that if you implement a native word that involves
  41. popping from PS, you are expected to call chkPS, for under-
  42. flow situations.
  43. We don't check RS for underflow because the cost of the check
  44. is significant and its usefulness is dubious: if RS isn't
  45. tightly in control, we're screwed anyways, and that, well
  46. before we reach underflow.
  47. Overflow condition happen when RSP and PSP meet somewhere in
  48. the middle. That check is made at each "next" call.
  49. # Dictionary entry
  50. A dictionary entry has this structure:
  51. - Xb name. Arbitrary long number of character (but can't be
  52. bigger than input buffer, of course). not null-terminated
  53. - 2b prev offset
  54. - 1b name size + IMMEDIATE flag (7th bit)
  55. - 1b entry type
  56. - Parameter field (PF)
  57. The prev offset is the number of bytes between the prev field
  58. and the previous word's entry type.
  59. The size + flag indicate the size of the name field, with the
  60. 7th bit being the IMMEDIATE flag.
  61. The entry type is simply a number corresponding to a type which
  62. will determine how the word will be executed. See "Word types"
  63. below.
  64. # Word types
  65. There are 6 word types in Collapse OS. Whenever you have a
  66. wordref, it's pointing to a byte with numbers 0 to 5. This
  67. number is the word type and the word's behavior depends on it.
  68. 0: native. This words PFA contains native binary code and is
  69. jumped to directly.
  70. 1: compiled. This word's PFA contains a list of wordrefs and its
  71. execution is described in "Execution model" above.
  72. 2: cell. This word is usually followed by a 2-byte value in its
  73. PFA. Upon execution, the address of the PFA is pushed to PS.
  74. 3: DOES>. This word is created by "DOES>" and is followed
  75. by a 2-bytes value as well as the address where "DOES>" was
  76. compiled. At that address is an wordref list exactly like in a
  77. compiled word. Upon execution, after having pushed its cell
  78. addr to PSP, it executes its reference exactly like a
  79. compiled word.
  80. 4: alias. See usage.txt. PFA is like a cell, but instead of
  81. pushing it to PS, we execute it.
  82. 5: ialias. Same as alias, but with an added indirection.
  83. # System variables
  84. There are some core variables in the core system that are
  85. referred to directly by their address in memory throughout the
  86. code. The place where they live is configurable by the SYSVARS
  87. constant in xcomp unit, but their relative offset is not. In
  88. fact, they're mostly referred to directly as their numerical
  89. offset along with a comment indicating what this offset refers
  90. to.
  91. This system is a bit fragile because every time we change those
  92. offsets, we have to be careful to adjust all system variables
  93. offsets, but thankfully, there aren't many system variables.
  94. Here's a list of them:
  95. SYSVARS FUTURE USES +3c BLK(*
  96. +02 CURRENT +3e A@ ialias
  97. +04 HERE +40 A! ialias
  98. +06 C<? +42 A, ialias
  99. +08 C<* override +44 FUTURE USES
  100. +0a NL ialias +51 CURRENTPTR
  101. +0c C<* +53 EMIT ialias
  102. +0e WORDBUF +55 KEY ialias
  103. +2e BOOT C< PTR +57 FUTURE USES
  104. +30 IN>
  105. +32 IN(* +70 DRIVERS
  106. +34 BLK@* +80 RAMEND
  107. +36 BLK!*
  108. +38 BLK>
  109. +3a BLKDTY
  110. CURRENT points to the last dict entry.
  111. HERE points to current write offset.
  112. C<* holds routine address called on C<. If the C<* override
  113. at 0x08 is nonzero, this routine is called instead.
  114. IN> is the current position in IN(, which is the input buffer.
  115. IN(* is a pointer to the input buffer, allocated at runtime.
  116. CURRENTPTR points to current CURRENT. The Forth CURRENT word
  117. doesn't return RAM+2 directly, but rather the value at this
  118. address. Most of the time, it points to RAM+2, but sometimes,
  119. when maintaining alternative dicts (during cross compilation
  120. for example), it can point elsewhere.
  121. NLPTR points to an alternative routine for NL (by default,
  122. CRLF).
  123. BLK* see B416.
  124. DRIVERS section is reserved for recipe-specific drivers.
  125. FUTURE USES section is unused for now.
  126. # Initialization sequence
  127. (this describes the z80 boot sequence, but other arches have
  128. a very similar sequence, and, of course, once we enter Forth
  129. territory, identical)
  130. On boot, we jump to the "main" routine in B289 which does
  131. very few things.
  132. 1. Set SP to PS_ADDR and IX to RS_ADDR.
  133. 2. Set CURRENT to value of LATEST field in stable ABI.
  134. 3. Set HERE to HERESTART const if defined, to CURRENT other-
  135. wise.
  136. 4. Execute the word referred to by 0x04 (BOOT) in stable ABI.
  137. In a normal system, BOOT is in core words at B396 and does a
  138. few things:
  139. 1. Initialize a few core variables:
  140. CURRENT* -> CURRENT (RAM+02)
  141. BOOT C< PTR -> LATEST
  142. C<* override -> 0
  143. 2. Initialized ialiases in this way:
  144. EMIT -> (emit)
  145. KEY -> (key)
  146. NL -> CRLF
  147. A@ -> C@
  148. A! -> C!
  149. A, -> C,
  150. 3. Set "C<*", the word that C< calls, to (boot<).
  151. 4. Call INTERPRET which interprets boot source code until
  152. ASCII EOT (4) is met. This usually initializes drivers.
  153. 5. Initialize rdln buffer, _sys entry (for EMPTY), prints
  154. "CollapseOS" and then calls (main).
  155. 6. (main) interprets from rdln input (usually from KEY) until
  156. EOT is met, then calls BYE.
  157. If, for some reason, you need to override an ialias at some
  158. point, you de-override it by re-setting it to the address of
  159. the word specified at step 2.
  160. # Stable ABI
  161. The Stable ABI lives at the beginning of the binary and prov-
  162. ides a way for Collapse OS code to access values that would
  163. otherwise be difficult to access. Here's the complete list of
  164. these references:
  165. 04 BOOT addr 06 (uflw) addr 08 LATEST
  166. 13 (oflw) addr 1a next addr
  167. BOOT, (uflw) and (oflw) exist because they are referred to
  168. before those words are defined (in core words). LATEST is a
  169. critical part of the initialization sequence.
  170. All Collapse OS binaries, regardless of architecture, have
  171. those values at those offsets of them. Some binaries are built
  172. to run at offset different than zero. This stable ABI lives at
  173. that offset, not 0.