design.rst 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ====================================
  2. Incremental Recompilations
  3. ====================================
  4. We split the Nim compiler into a frontend and a backend.
  5. The frontend produces a set of `.rod` files. Every `.nim` module
  6. produces its own `.rod` file.
  7. - The IR must be a faithful representation of the AST in memory.
  8. - The backend can do its own caching but doesn't have to. In the
  9. current implementation the backend also caches its results.
  10. Advantage of the "set of files" vs the previous global database:
  11. - By construction, we either read from the `.rod` file or from the
  12. `.nim` file, there can be no inconsistency. There can also be no
  13. partial updates.
  14. - No dependency to external packages (SQLite). SQLite simply is too
  15. slow and the old way of serialization was too slow too. We use a
  16. format designed for Nim and expect to base further tools on this
  17. file format.
  18. References to external modules must be (moduleId, symId) pairs.
  19. The symbol IDs are module specific. This way no global ID increment
  20. mechanism needs to be implemented that we could get wrong. ModuleIds
  21. are rod-file specific too.
  22. Global state
  23. ------------
  24. There is no global state.
  25. Rod File Format
  26. ---------------
  27. It's a simple binary file format. `rodfiles.nim` contains some details.
  28. Backend
  29. -------
  30. Nim programmers have to come to enjoy whole-program dead code elimination,
  31. by default. Since this is a "whole program" optimization, it does break
  32. modularity. However, thanks to the packed AST representation we can perform
  33. this global analysis without having to unpack anything. This is basically
  34. a mark&sweep GC algorithm:
  35. - Start with the top level statements. Every symbol that is referenced
  36. from a top level statement is not "dead" and needs to be compiled by
  37. the backend.
  38. - Every symbol referenced from a referenced symbol also has to be
  39. compiled.
  40. Caching logic: Only if the set of alive symbols is different from the
  41. last run, the module has to be regenerated.