Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.1KB

  1. ( Inner core. This unit represents core definitions that
  2. happen right after native definitions. Before core.fs.
  3. Unlike core.fs and its followers, this unit isn't self-
  4. sustained. Like native defs it uses the machinery of a
  5. full Forth interpreter, notably for flow structures.
  6. Because of that, it has to obey specific rules:
  7. 1. It cannot compile a word from higher layers. Using
  8. immediates is fine though.
  9. 2. If it references a word from this unit or from native
  10. definitions, these need to be properly offsetted
  11. because their offset at compile time are not the same
  12. as their runtime offsets.
  13. 3. Anything they refer to in the boot binary has to be
  14. properly stabilized.
  15. 4. Make sure that the words you compile are not overridden
  16. by the full interpreter.
  17. )
  18. ( When referencing words from native defs or this very unit,
  19. use this compiling word, which subtract the proper offset
  20. from the compiled word. That proper offset is:
  21. 1. Take ROT-header addr, the first native def.
  22. 2. Subtract _bend, boot's last word.
  23. 3. That will give us the offset to subtract to get the addr
  24. of our word at runtime.
  25. This means, of course, that any word compiling a _c word
  26. can't be executed immediately.
  27. Also note that because of that "_c" mechanism, it might
  28. take two rounds of bootstrapping before the compiled
  29. z80c.bin file is "stabilized". That's because the 2nd time
  30. around, the recorded offset will have changed.
  31. )
  32. : _c
  33. [
  34. ' ROT
  35. 6 - ( header )
  36. ' _bend
  37. - ( our offset )
  38. LITN
  39. ]
  40. ' ( get word )
  41. -^ ( apply offset )
  42. , ( write! )
  43. ; IMMEDIATE
  44. : ABORT _c (resSP) QUIT ;
  45. : INTERPRET
  46. BEGIN
  47. WORD
  48. (find)
  49. IF
  50. 1 FLAGS !
  51. EXECUTE
  52. 0 FLAGS !
  53. ELSE
  54. (parse*) @ EXECUTE
  55. THEN
  56. AGAIN
  57. ;
  58. : BOOT
  59. LIT< (c<$) (find) IF EXECUTE ELSE DROP THEN
  60. _c INTERPRET
  61. ;
  62. ( This is only the "early parser" in earlier stages. No need
  63. for an abort message )
  64. : (parse)
  65. (parsed) SKIP? _c ABORT
  66. ;
  67. ( a -- )
  68. : (print)
  69. BEGIN
  70. DUP ( a a )
  71. _c C@ ( a c )
  72. ( exit if null )
  73. DUP NOT IF DROP DROP EXIT THEN
  74. EMIT ( a )
  75. 1 + ( a+1 )
  76. AGAIN
  77. ;
  78. : (uflw)
  79. LIT< stack-underflow _c (print) _c ABORT
  80. ;
  81. : C,
  82. HERE @ _c C!
  83. HERE @ 1 + HERE !
  84. ;
  85. : (entry)
  86. HERE @ ( h )
  87. WORD ( h s )
  88. SCPY ( h )
  89. ( Adjust HERE -1 because SCPY copies the null )
  90. HERE @ 1 _c - ( h h' )
  91. DUP HERE ! ( h h' )
  92. SWAP _c - ( sz )
  93. ( write prev value )
  94. HERE @ CURRENT @ _c - ,
  95. ( write size )
  96. _c C,
  97. HERE @ CURRENT !
  98. ;
  99. ( : and ; have to be defined last because it can't be
  100. executed now also, they can't have their real name
  101. right away )
  102. : X
  103. _c (entry)
  104. ( JTBL+6 == compiledWord )
  105. [ JTBL 6 + LITN ] ,
  106. BEGIN
  107. WORD
  108. (find)
  109. ( is word )
  110. IF DUP _c IMMED? IF EXECUTE ELSE , THEN
  111. ( maybe number )
  112. ELSE (parse*) @ EXECUTE LITN THEN
  113. AGAIN
  114. ; IMMEDIATE
  115. : Y
  116. ['] EXIT ,
  117. _c R> DROP ( exit : )
  118. ; IMMEDIATE
  119. ( Give ":" and ";" their real name )
  120. ':' ' X 4 - C!
  121. ';' ' Y 4 - C!