clucmp.r 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .dv xgp
  2. .fo 0 fonts;25vg kst
  3. .fo 1 fonts;25vgb kst
  4. .fo 2 fonts;25vri kst
  5. .fo 3 fonts;31vgb kst
  6. .nr verbose 1
  7. .so r;r macros
  8. .ls 1.5
  9. .tr `
  10. An idealized scheme of compilation is that the parser produces a tree with
  11. "forms" in it.
  12. A form is an "unknown" idn appearing as an expression or constant
  13. (in particular, as a type).
  14. The legality of the form is unknown until equates are processed.
  15. After equates have been processed (or as they are processed),
  16. a new tree is built that contains no forms.
  17. The type checker and code generator work on this new tree.
  18. This is not a suitable implementation of compilation;
  19. rebuilding the parse tree is expensive in time (it requires an extra pass)
  20. and space.
  21. The important decision to make is: when and how to process equates.
  22. There are two possible ways to go.
  23. One way is to process equates after parsing,
  24. and make the parse tree (in particular, idns and typespecs) mutable
  25. so that rebuilding the tree is not necessary.
  26. The other way is to process equates during the parse.
  27. This way,
  28. only module headers need to be rebuilt,
  29. and typespecs (and perhaps idns) can be immutable.
  30. Before going on,
  31. some background will be helpful.
  32. For efficiency,
  33. the parser needs to do two things (in addition to parsing):
  34. canonicalize idns, and canonicalize typespecs.
  35. Idns need to be canonicalized for declaration- and type- checking,
  36. and for emitting (short, uniform) variable names.
  37. Typespecs need to be canonicalized for efficient type comparison,
  38. and for emitting (a minimal number of) type descriptors.
  39. Canonicalization involves,
  40. at the least,
  41. inserting a unique (to the compilation) id in the idn or typespec,
  42. and may also mean having a unique object for each idn or typespec.
  43. If we process equates after parsing then we need mutable idns so that,
  44. once we compute the value of an equate,
  45. we can "substitute" the value everywhere in one fell swoop.
  46. (For efficiency I want an idn to contain info, not just a pointer to info.)
  47. We need mutable typespecs so that,
  48. once we discover that x`=`int, we can substitute int for the typespec x
  49. everywhere in the tree in one operation.
  50. (I don't want to just change the idn contained in the typespec,
  51. because this makes type comparison slower and more complicated.)
  52. Note that equality on typespecs is now equality on uids,
  53. not rep equality.
  54. However,
  55. the parser is fairly free of semantic information;
  56. it knows about scope,
  57. but that's all.
  58. Canonicalizing idns allows declaration checking to be done along with type checking.
  59. If we put declaration checking and equate processing into the "parser",
  60. we get a cleaner type checker,
  61. one that does not have to worry about the possibility that x is not a type
  62. in x$foo.
  63. However,
  64. we have not gotten rid of any problems;
  65. the parser still has to do the checking.
  66. We could get immutable typespecs.
  67. Idns might still be mutable,
  68. but the only idns a typespec could contain are parameters,
  69. and these could be represented by integers instead of actual idns.
  70. The good thing about this scheme is that there is no tricky use of mutability.
  71. One bad thing is that we have thrown a lot of semantic information into the "parser".
  72. But even worse,
  73. I think,
  74. is that there are then two sets of abstract syntax equates for types and expressions,
  75. and three sets of procedures for dealing with them.
  76. (One for parsing equates and module headers,
  77. one for rebuilding module headers,
  78. and one for "normal" parsing.)
  79. The addition of these procedures would double the size of the (existing) parser.
  80. A question that is sort of related to all of this is,
  81. how does the canonicalization of idns and typespecs interact with our notion of
  82. using a compilation environment (CE), and using the library for type-checking?
  83. A CE need not be the most obvious map from strings to values.
  84. Rather,
  85. we note that the parser uses two (hash) tables for canonicalizing idns and typespecs;
  86. a CE can almost be represented by such a pair of tables.
  87. However, the idn table cannot be used by the parser unless the parser does
  88. both declaration checking and equate processing.
  89. If the parser does not do these,
  90. then a CE is represented by a type table and a map from strings to values.
  91. A CE might contain information only down to DUs,
  92. and the DU specs would have to be retrieved from the library,
  93. or it might contain all of the specs, too,
  94. in which case the library need not be accessed during type checking.
  95. As for the representation of typespecs within DU specs,
  96. it doesn't really matter if they are actual typespecs or just something like typespecs.
  97. They will have to be (re-)canonicalized when read in,
  98. so a recursive walk is necessary regardless of their representation.
  99. Writing them out as actual typespecs puts out some "useless" info,
  100. but not all that much.