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.

99 lines
3.6KB

  1. # Cross-compilation
  2. When Forth words are compiled, they are compiled for the system
  3. currently running. Those compiled words are tricky to relocate
  4. because their wordrefs reference offsets within the running
  5. system.
  6. If you want to deploy to a new system, you need tricks, and
  7. those tricks are located at B260, the cross-compilation toolset.
  8. The mechanism is simple: override ":" so that it adds an offset
  9. to every wordrefs it compiles.
  10. What should that offset be? the beginning of the binary being
  11. built. That offset if the value in the ORG variable, supplied
  12. by the assembler. It's logical: every binary begins with a bit
  13. of assembler, which makes every following Forth word aligned
  14. with this value.
  15. # Dual-CURRENT
  16. Although the principle behind cross-compilation is simple, the
  17. devil's in the details. While building our new binary, we still
  18. need access to a full-fledged Forth interpreter. To allow this,
  19. we'll maintain two CURRENT: the regular one and XCURRENT, the
  20. CURRENT value of the cross-compiled binary.
  21. XCURRENT's value is a *host* address, not a cross one. For
  22. example, if our cross binary begins at offset 0x1000 and the
  23. last word added to it was at offset 0x234, then XCURRENT is
  24. 0x1234.
  25. During cross compilation, we constantly switch CURRENT (through
  26. the CURRENT* sysvar, see impl.txt) between the host's and
  27. XCURRENT.
  28. As a general rule, switching happens this way: When interpret-
  29. ing, we're in host mode. When compiling, we're in XCURRENT mode.
  30. When we encounter an IMMEDIATE during compilation, we execute
  31. the *host* version of that word. The reason for this is simple:
  32. any word freshly cross-compiled is utterly un-runable because
  33. its wordrefs are misaligned under the current host.
  34. # xcomp unit
  35. Cross-compilation is achieved through the writing of a cross-
  36. compilation unit of code, xcomp unit for short.
  37. The xcomp toolset at B260 alters core words in a deep way, so
  38. ordering is important. First, we load our tools. Assembler,
  39. xcomp toolset, etc. The xcomp toolset is designed to not over-
  40. shadow core words directly, so initial loading, B262, is harm-
  41. less.
  42. Now is also the time to define some support words that will not
  43. be part of our resulting binary, but will be used during xcomp,
  44. for example, declarations units and macros.
  45. Then, we load B270 to apply xcomp overrides. From this point on.
  46. every defining word is messed up and will produce offsetted
  47. binaries.
  48. At this point, it's critical to set ORG before spitting any-
  49. thing. Boot binaries will usually take care of this, so you
  50. don't have to do it yourself. You just have to make sure that
  51. you load the boot binary right after loading B270.
  52. Then, you spit your content following instructions in
  53. bootstrap.txt.
  54. After you're done, you can run EMPTY to go back to a usable
  55. system.
  56. # Immediate compiling words trickyness
  57. When using an immediate compiling word such as "IF" during
  58. xcomp, things are a bit tricky for two reasons:
  59. 1. Immediates used during xcomp are from the host system.
  60. 2. The reference of the word(s) they compile is for the host
  61. system.
  62. Therefore, unless the compiled word (for example (?br) compiled
  63. by IF) has exactly the same address in both the host and guest,
  64. the resulting binary will be broken.
  65. For this reason, we re-implement many of those compiling words
  66. in xcomp overrides, hacking our way through, so that those
  67. compiling words compile proper guest references. We don't do
  68. this for all compiling words though. This means that some words
  69. can't be used in core and drivers, for example, ABORT" and .".
  70. How to know whether a word can be used?
  71. 1. If it's not an immediate compiling word, it's fine.
  72. 2. If its overriden in B270, it's fine.
  73. 3. Otherwise, you can't cross-compile it.