1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Table layout builders
16 module layout_builders
18 import abstract_compiler
22 class Layout[E
: Object]
24 var ids
: Map[E
, Int] = new HashMap[E
, Int]
25 # Fixed positions of each element in all tables
26 var pos
: Map[E
, Int] = new HashMap[E
, Int]
29 class PHLayout[HOLDER: Object, E
: Object]
31 # Masks used by hash function
32 var masks
: Map[HOLDER, Int] = new HashMap[HOLDER, Int]
33 # Positions of each element for each tables
34 var hashes
: Map[HOLDER, Map[E
, Int]] = new HashMap[HOLDER, Map[E
, Int]]
37 class PropertyLayout[E
: Object]
38 # Fixed positions of each element in all tables
39 var pos
: Map[E
, Int] = new HashMap[E
, Int]
44 interface TypingLayoutBuilder[E
: Object]
45 # Build typing table layout
46 fun build_layout
(elements
: Set[E
]): Layout[E
] is abstract
49 interface PropertyLayoutBuilder[E
: MProperty]
50 # Build table layout for attributes, methods and virtual types
51 fun build_layout
(elements
: Set[MClass]): Layout[E
] is abstract
54 interface ResolutionLayoutBuilder
55 # Build resolution table layout
56 fun build_layout
(elements
: Map[MClassType, Set[MType]]): Layout[MType] is abstract
61 abstract class TypingBMizer[E
: Object]
62 super TypingLayoutBuilder[E
]
66 init(mmodule
: MModule) do
67 self.mmodule
= mmodule
70 # Compute mtypes ids and position using BM
71 redef fun build_layout
(elements
: Set[E
]): Layout[E
] do
72 var result
= new Layout[E
]
73 var ids
= new HashMap[E
, Int]
74 var lin
= self.reverse_linearize
(elements
)
76 ids
[element
] = ids
.length
83 private fun reverse_linearize
(elements
: Set[E
]): Array[E
] is abstract
87 super TypingBMizer[MType]
89 init(mmodule
: MModule) do super(mmodule
)
91 redef fun reverse_linearize
(elements
) do
92 return self.mmodule
.reverse_linearize_mtypes
(elements
)
97 super TypingBMizer[MClass]
99 init(mmodule
: MModule) do super(mmodule
)
101 redef fun reverse_linearize
(elements
) do
102 return self.mmodule
.reverse_linearize_mclasses
(elements
)
106 # Layout builder for resolution tables using Binary Matrix (BM)
107 class ResolutionBMizer
108 super ResolutionLayoutBuilder
112 redef fun build_layout
(elements
) do
113 var result
= new Layout[MType]
114 var ids
= new HashMap[MType, Int]
116 for mclasstype
, mclasstypes
in elements
do
117 for element
in mclasstypes
do
118 if ids
.has_key
(element
) then continue
131 abstract class TypingColorer[E
: Object]
132 super TypingLayoutBuilder[E
]
134 private var core
: Set[E
] = new HashSet[E
]
135 private var crown
: Set[E
] = new HashSet[E
]
136 private var border
: Set[E
] = new HashSet[E
]
138 private var coloration_result
: Map[E
, Int] = new HashMap[E
, Int]
142 # Compute the layout with coloring
143 redef fun build_layout
(elements
: Set[E
]): Layout[E
] do
144 var result
= new Layout[E
]
145 result
.ids
= compute_ids
(elements
)
146 result
.pos
= colorize
(elements
)
150 private fun compute_ids
(elements
: Set[E
]): Map[E
, Int] do
151 var ids
= new HashMap[E
, Int]
152 var lin
= reverse_linearize
(elements
)
153 for element
in lin
do
154 ids
[element
] = ids
.length
159 private fun colorize
(elements
: Set[E
]): Map[E
, Int] do
160 tag_elements
(elements
)
161 build_conflicts_graph
(elements
)
162 colorize_elements
(core
)
163 colorize_elements
(border
)
164 colorize_elements
(crown
)
165 return coloration_result
168 # Colorize a collection of elements
169 private fun colorize_elements
(elements
: Set[E
]) do
172 var lin
= reverse_linearize
(elements
)
173 for element
in lin
do
174 var color
= min_color
175 while not self.is_color_free
(element
, elements
, color
) do
178 coloration_result
[element
] = color
183 # Check if a related element to the element already use the color
184 private fun is_color_free
(element
: E
, elements
: Set[E
], color
: Int): Bool do
185 if conflicts_graph
.has_key
(element
) then
186 for st
in conflicts_graph
[element
] do
187 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
190 for st
in self.super_elements
(element
, elements
) do
191 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
196 # Tag elements as core, crown or border
197 private fun tag_elements
(elements
: Set[E
]) do
198 for element
in elements
do
199 # Check if sub elements are all in single inheritance
200 var all_subelements_si
= true
201 for subelem
in self.sub_elements
(element
, elements
) do
202 if self.is_element_mi
(subelem
, elements
) then
203 all_subelements_si
= false
208 # Tag as core, crown or border
209 if self.is_element_mi
(element
, elements
) then
210 core
.add_all
(self.super_elements
(element
, elements
))
212 if all_subelements_si
then
215 else if not all_subelements_si
then
216 core
.add_all
(self.super_elements
(element
, elements
))
224 # Conflicts graph of elements hierarchy (two types are in conflict if they have common subelements)
225 private fun build_conflicts_graph
(elements
: Set[E
]) do
226 self.conflicts_graph
= new HashMap[E
, HashSet[E
]]
227 var core
= reverse_linearize
(self.core
)
229 for i
in self.linear_extension
(t
, elements
) do
230 if t
== i
then continue
232 var lin_i
= self.linear_extension
(i
, elements
)
234 for j
in self.linear_extension
(t
, elements
) do
235 if i
== j
or j
== t
then continue
236 var lin_j
= self.linear_extension
(j
, elements
)
238 var d_ij
= lin_i
- lin_j
239 var d_ji
= lin_j
- lin_i
242 if not conflicts_graph
.has_key
(ed1
) then conflicts_graph
[ed1
] = new HashSet[E
]
243 # add ed1 x ed2 to conflicts graph
244 for ed2
in d_ji
do conflicts_graph
[ed1
].add
(ed2
)
247 if not conflicts_graph
.has_key
(ed1
) then conflicts_graph
[ed1
] = new HashSet[E
]
248 # add ed1 x ed2 to conflicts graph
249 for ed2
in d_ji
do conflicts_graph
[ed1
].add
(ed2
)
256 private var conflicts_graph
: nullable HashMap[E
, Set[E
]]
258 # cache for linear_extensions
259 private var linear_extensions_cache
: Map[E
, Array[E
]] = new HashMap[E
, Array[E
]]
261 # Return a linear_extension of super_elements of the element
262 private fun linear_extension
(element
: E
, elements
: Set[E
]): Array[E
] do
263 if not self.linear_extensions_cache
.has_key
(element
) then
264 var supers
= new HashSet[E
]
266 supers
.add_all
(self.super_elements
(element
, elements
))
267 self.linear_extensions_cache
[element
] = self.linearize
(supers
)
269 return self.linear_extensions_cache
[element
]
272 private fun super_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
273 private fun sub_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
274 private fun is_element_mi
(element
: E
, elements
: Set[E
]): Bool is abstract
275 private fun linearize
(elements
: Set[E
]): Array[E
] is abstract
276 private fun reverse_linearize
(elements
: Set[E
]): Array[E
] is abstract
281 super TypingColorer[MType]
285 init(mmodule
: MModule) do self.mmodule
= mmodule
287 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mtypes
(element
, elements
)
288 redef fun is_element_mi
(element
, elements
) do return self.super_elements
(element
, elements
).length
> 1
289 redef fun sub_elements
(element
, elements
) do do return self.mmodule
.sub_mtypes
(element
, elements
)
290 redef fun linearize
(elements
) do return self.mmodule
.linearize_mtypes
(elements
)
291 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mtypes
(elements
)
296 super TypingColorer[MClass]
298 private var mmodule
: MModule
300 init(mmodule
: MModule) do self.mmodule
= mmodule
302 redef fun super_elements
(element
, elements
) do return self.mmodule
.super_mclasses
(element
)
303 fun parent_elements
(element
: MClass): Set[MClass] do return self.mmodule
.parent_mclasses
(element
)
304 redef fun is_element_mi
(element
, elements
) do return self.parent_elements
(element
).length
> 1
305 redef fun sub_elements
(element
, elements
) do do return self.mmodule
.sub_mclasses
(element
)
306 redef fun linearize
(elements
) do return self.mmodule
.linearize_mclasses
(elements
)
307 redef fun reverse_linearize
(elements
) do return self.mmodule
.reverse_linearize_mclasses
(elements
)
311 abstract class MPropertyColorer[E
: MProperty]
312 super PropertyLayoutBuilder[E
]
314 private var mmodule
: MModule
315 private var class_colorer
: MClassColorer
316 private var coloration_result
: Map[E
, Int] = new HashMap[E
, Int]
318 init(mmodule
: MModule) do
319 self.mmodule
= mmodule
320 self.class_colorer
= new MClassColorer(mmodule
)
323 # Compute mclasses ids and position using BM
324 redef fun build_layout
(mclasses
: Set[MClass]): Layout[E
] do
325 var result
= new Layout[E
]
326 result
.pos
= self.colorize
(mclasses
)
330 private fun colorize
(mclasses
: Set[MClass]): Map[E
, Int] do
331 self.class_colorer
.tag_elements
(mclasses
)
332 self.class_colorer
.build_conflicts_graph
(mclasses
)
333 self.colorize_core
(self.class_colorer
.core
)
334 self.colorize_crown
(self.class_colorer
.crown
)
335 return self.coloration_result
338 # Colorize properties of the core hierarchy
339 private fun colorize_core
(mclasses
: Set[MClass]) do
341 for mclass
in mclasses
do
342 var color
= min_color
344 # if the class is root, get the minimal color
345 if self.mmodule
.parent_mclasses
(mclass
).length
== 0 then
346 colorize_elements
(self.properties
(mclass
), color
)
348 # check last color used by parents
349 color
= max_color
(color
, self.mmodule
.parent_mclasses
(mclass
))
350 # check max color used in conflicts
351 if self.class_colorer
.conflicts_graph
.has_key
(mclass
) then
352 color
= max_color
(color
, self.class_colorer
.conflicts_graph
[mclass
])
354 colorize_elements
(self.properties
(mclass
), color
)
359 # Colorize properties of the crown hierarchy
360 private fun colorize_crown
(mclasses
: Set[MClass]) do
361 for mclass
in mclasses
do
362 colorize_elements
(self.properties
(mclass
), max_color
(0, self.mmodule
.parent_mclasses
(mclass
)))
366 # Colorize a collection of mproperties given a starting color
367 private fun colorize_elements
(elements
: Collection[E
], start_color
: Int) do
368 for element
in elements
do
369 if self.coloration_result
.has_key
(element
) then continue
370 self.coloration_result
[element
] = start_color
375 private fun max_color
(min_color
: Int, mclasses
: Collection[MClass]): Int do
376 var max_color
= min_color
378 for mclass
in mclasses
do
379 for mproperty
in self.properties
(mclass
) do
380 var color
= min_color
381 if self.coloration_result
.has_key
(mproperty
) then
382 color
= self.coloration_result
[mproperty
]
383 if color
>= max_color
then max_color
= color
+ 1
391 private fun properties
(mclass
: MClass): Set[E
] is abstract
394 # Coloring for MMethods
396 super MPropertyColorer[MMethod]
398 init(mmodule
: MModule) do super
400 redef fun properties
(mclass
) do
401 var properties
= new HashSet[MMethod]
402 for mprop
in self.mmodule
.properties
(mclass
) do
403 if mprop
isa MMethod then properties
.add
(mprop
)
409 # Coloring for MMAttributes
410 class MAttributeColorer
411 super MPropertyColorer[MAttribute]
413 init(mmodule
: MModule) do super
415 redef fun properties
(mclass
) do
416 var properties
= new HashSet[MAttribute]
417 for mprop
in self.mmodule
.properties
(mclass
) do
418 if mprop
isa MAttribute then properties
.add
(mprop
)
424 # Coloring for MVirtualTypeProps
425 class MVirtualTypePropColorer
426 super MPropertyColorer[MVirtualTypeProp]
428 init(mmodule
: MModule) do super
430 redef fun properties
(mclass
) do
431 var properties
= new HashSet[MVirtualTypeProp]
432 for mprop
in self.mmodule
.properties
(mclass
) do
433 if mprop
isa MVirtualTypeProp then properties
.add
(mprop
)
439 # Colorer for type resolution table
440 class ResolutionColorer
441 super ResolutionLayoutBuilder
443 private var coloration_result
: Map[MType, Int] = new HashMap[MType, Int]
447 # Compute resolved types colors
448 redef fun build_layout
(elements
) do
449 self.build_conflicts_graph
(elements
)
450 var result
= new Layout[MType]
451 result
.ids
= self.compute_ids
(elements
)
452 result
.pos
= self.colorize_elements
(elements
)
456 private fun compute_ids
(elements
: Map[MClassType, Set[MType]]): Map[MType, Int] do
457 var ids
= new HashMap[MType, Int]
459 for mclasstype
, mclasstypes
in elements
do
460 for element
in mclasstypes
do
461 if ids
.has_key
(element
) then continue
469 # Colorize a collection of elements
470 private fun colorize_elements
(elements
: Map[MClassType, Set[MType]]): Map[MType, Int] do
472 for mclasstype
, mclasstypes
in elements
do
473 for element
in mclasstypes
do
474 if self.coloration_result
.has_key
(element
) then continue
475 var color
= min_color
476 while not self.is_color_free
(element
, color
) do
479 coloration_result
[element
] = color
483 return self.coloration_result
486 # Check if a related element to the element already use the color
487 private fun is_color_free
(element
: MType, color
: Int): Bool do
488 if conflicts_graph
.has_key
(element
) then
489 for st
in conflicts_graph
[element
] do
490 if coloration_result
.has_key
(st
) and coloration_result
[st
] == color
then return false
496 # look for unanchored types generated by the same type
497 private fun build_conflicts_graph
(elements
: Map[MClassType, Set[MType]]) do
498 for mclasstype
, mtypes
in elements
do
499 for mtype
in mtypes
do
500 for otype
in mtypes
do
501 if otype
== mtype
then continue
502 self.add_conflict
(mtype
, otype
)
508 private var conflicts_graph
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
510 private fun add_conflict
(mtype
: MType, otype
: MType) do
511 if mtype
== otype
then return
512 if not self.conflicts_graph
.has_key
(mtype
) then self.conflicts_graph
[mtype
] = new HashSet[MType]
513 self.conflicts_graph
[mtype
].add
(otype
)
514 if not self.conflicts_graph
.has_key
(otype
) then self.conflicts_graph
[otype
] = new HashSet[MType]
515 self.conflicts_graph
[otype
].add
(mtype
)
519 # Perfect Hashing (PH)
521 # U = type of elements to hash
522 private class PerfectHasher[T
: Object, U
: Object]
524 var operator
: PHOperator
526 init(operator
: PHOperator) do self.operator
= operator
528 fun compute_masks
(conflicts
: Map[T
, Set[U
]], ids
: Map[U
, Int]): Map[T
, Int] do
529 var masks
= new HashMap[T
, Int]
530 for mclasstype
, mtypes
in conflicts
do
531 masks
[mclasstype
] = compute_mask
(mtypes
, ids
)
536 private fun compute_mask
(mtypes
: Set[U
], ids
: Map[U
, Int]): Int do
539 var used
= new List[Int]
540 for mtype
in mtypes
do
541 var res
= operator
.op
(mask
, ids
[mtype
])
542 if used
.has
(res
) then
548 if used
.length
== mtypes
.length
then break
554 fun compute_hashes
(elements
: Map[T
, Set[U
]], ids
: Map[U
, Int], masks
: Map[T
, Int]): Map[T
, Map[U
, Int]] do
555 var hashes
= new HashMap[T
, Map[U
, Int]]
556 for mclasstype
, mtypes
in elements
do
557 var mask
= masks
[mclasstype
]
558 var inhashes
= new HashMap[U
, Int]
559 for mtype
in mtypes
do
560 inhashes
[mtype
] = operator
.op
(mask
, ids
[mtype
])
562 hashes
[mclasstype
] = inhashes
568 # Abstract operator used for perfect hashing
569 abstract class PHOperator
570 fun op
(mask
: Int, id
:Int): Int is abstract
573 # Hashing using modulo (MOD) operator
578 redef fun op
(mask
, id
) do return mask
% id
581 # Hashing using binary and (AND) operator
586 redef fun op
(mask
, id
) do return mask
.bin_and
(id
)
589 class TypingHasher[E
: Object]
590 super PerfectHasher[E
, E
]
591 super TypingLayoutBuilder[E
]
595 init(operator
: PHOperator, mmodule
: MModule) do
597 self.mmodule
= mmodule
600 redef fun build_layout
(elements
: Set[E
]): PHLayout[E
, E
] do
601 var result
= new PHLayout[E
, E
]
602 var conflicts
= self.build_conflicts
(elements
)
603 result
.ids
= self.compute_ids
(elements
)
604 result
.masks
= self.compute_masks
(conflicts
, result
.ids
)
605 result
.hashes
= self.compute_hashes
(conflicts
, result
.ids
, result
.masks
)
609 # Ids start from 1 instead of 0
610 private fun compute_ids
(elements
: Set[E
]): Map[E
, Int] do
611 var ids
= new HashMap[E
, Int]
612 var lin
= self.reverse_linearize
(elements
)
614 ids
[e
] = ids
.length
+ 1
619 private fun build_conflicts
(elements
: Set[E
]): Map[E
, Set[E
]] do
620 var conflicts
= new HashMap[E
, Set[E
]]
622 var supers
= self.super_elements
(e
, elements
)
624 conflicts
[e
] = supers
629 private fun super_elements
(element
: E
, elements
: Set[E
]): Set[E
] is abstract
630 private fun reverse_linearize
(elements
: Set[E
]): Array[E
] is abstract
634 super TypingHasher[MType]
636 init(operator
: PHOperator, mmodule
: MModule) do super(operator
, mmodule
)
638 redef fun super_elements
(element
, elements
) do
639 return self.mmodule
.super_mtypes
(element
, elements
)
642 redef fun reverse_linearize
(elements
) do
643 return self.mmodule
.reverse_linearize_mtypes
(elements
)
648 super TypingHasher[MClass]
650 init(operator
: PHOperator, mmodule
: MModule) do super(operator
, mmodule
)
652 redef fun super_elements
(element
, elements
) do
653 return self.mmodule
.super_mclasses
(element
)
656 redef fun reverse_linearize
(elements
) do
657 return self.mmodule
.reverse_linearize_mclasses
(elements
)
661 # Layout builder for MProperty using Perfect Hashing (PH)
662 # TODO implement this class without sublcassing CL builder
663 class MPropertyHasher[E
: MProperty]
664 super MPropertyColorer[E
]
667 class ResolutionHasher
668 super PerfectHasher[MClassType, MType]
669 super ResolutionLayoutBuilder
671 init(operator
: PHOperator) do super(operator
)
673 # Compute resolved types masks and hashes
674 redef fun build_layout
(elements
) do
675 var result
= new PHLayout[MClassType, MType]
676 var ids
= new HashMap[MType, Int]
678 for mclasstype
, mclasstypes
in elements
do
679 for element
in mclasstypes
do
680 if ids
.has_key
(element
) then continue
687 result
.masks
= self.compute_masks
(elements
, ids
)
688 result
.hashes
= self.compute_hashes
(elements
, ids
, result
.masks
)