1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Compute tables for classes and modules.
19 package table_computation
25 # Something that store color of table elements
27 var _colors
: HashMap[TableElt, Int] = new HashMap[TableElt, Int]
29 # The color of a table element.
30 fun color
(e
: TableElt): Int
35 # Is a table element already colored?
36 fun has_color
(e
: TableElt): Bool
38 return _colors
.has_key
(e
)
41 # Assign a color to a table element.
42 fun color
=(e
: TableElt, c
: Int)
46 for i
in [0..e
.length
[ do
47 _colors
[e
.item
(i
)] = idx
53 # All information and results of the global analysis.
54 class TableInformation
56 # FIXME: do something better.
57 readable writable var _max_class_table_length
: Int = 0
60 # A compiled class is a class in a program
63 # The corresponding local class in the main module of the prgram
64 readable var _local_class
: MMLocalClass
66 # The identifier of the class
67 readable writable var _id
: Int = 0
69 # The full class table of the class
70 readable var _class_table
: Array[nullable TableElt] = new Array[nullable TableElt]
72 # The full instance table of the class
73 readable var _instance_table
: Array[nullable TableElt] = new Array[nullable TableElt]
75 # The proper class table part (no superclasses but all refinements)
76 readable writable var _class_layout
: TableEltComposite = new TableEltComposite(self)
78 # The proper instance table part (no superclasses but all refinements)
79 readable writable var _instance_layout
: TableEltComposite = new TableEltComposite(self)
81 init(c
: MMLocalClass) do _local_class
= c
84 redef class MMConcreteClass
85 # The table element of the subtype check
86 fun class_color_pos
: TableEltClassColor do return _class_color_pos
.as(not null)
87 var _class_color_pos
: nullable TableEltClassColor
89 # The proper local class table part (nor superclasses nor refinments)
90 readable var _class_layout
: Array[TableElt] = new Array[TableElt]
92 # The proper local instance table part (nor superclasses nor refinments)
93 readable var _instance_layout
: Array[TableElt] = new Array[TableElt]
95 # Build the local layout of the class and feed the module table
96 private fun build_layout_in
(module_table
: Array[ModuleTableElt])
98 var clt
= _class_layout
99 var ilt
= _instance_layout
101 if global
.intro
== self then
102 module_table
.add
(new TableEltClassId(self))
103 var cpp
= new TableEltClassColor(self)
104 _class_color_pos
= cpp
105 module_table
.add
(cpp
)
106 clt
.add
(new TableEltClassInitTable(self))
108 for p
in local_local_properties
do
110 if pg
.intro
== p
then
111 if p
isa MMAttribute then
112 ilt
.add
(new TableEltAttr(p
))
113 else if p
isa MMMethod then
114 clt
.add
(new TableEltMeth(p
))
115 else if p
isa MMTypeProperty then
116 clt
.add
(new TableEltVTClassId(p
))
117 clt
.add
(new TableEltVTClassColor(p
))
120 if p
isa MMMethod and p
.need_super
then
121 clt
.add
(new TableEltSuper(p
))
125 if not ilt
.is_empty
then
126 var teg
= new ModuleTableEltGroup
127 teg
.elements
.append
(ilt
)
128 module_table
.add
(teg
)
131 if not clt
.is_empty
then
132 var teg
= new ModuleTableEltGroup
133 teg
.elements
.append
(clt
)
134 module_table
.add
(teg
)
140 # Information about the class tables
141 readable var _table_information
: TableInformation = new TableInformation
143 # Associate global classes to compiled classes
144 readable var _compiled_classes
: HashMap[MMGlobalClass, CompiledClass] = new HashMap[MMGlobalClass, CompiledClass]
146 fun do_table_computation
148 tc
.info
("Building tables",1)
149 for m
in main_module
.mhe
.greaters_and_self
do
150 tc
.info
("Building tables for module: {m.name}",2)
154 tc
.info
("Merging all tables",2)
155 do_global_table_analysis
158 # Do the complete global analysis
159 private fun do_global_table_analysis
161 var smallest_classes
= new Array[MMLocalClass]
162 var global_properties
= new HashSet[MMGlobalProperty]
163 var ctab
= new Array[TableElt]
164 var itab
= new Array[TableElt]
166 ctab
.add
(new TableEltClassSelfId)
167 ctab
.add
(new TableEltClassObjectSize)
168 itab
.add
(new TableEltVftPointer)
169 itab
.add
(new TableEltObjectId)
174 # We have to work on ALL the classes of the module
175 var classes
= new Array[MMLocalClass]
176 for c
in main_module
.local_classes
do classes
.add
(c
)
177 classes
.sort
!cmp
(x
,y
) = x
.total_order_compare
(y
)
180 # Associate a CompiledClass to the class
181 var cc
= new CompiledClass(c
)
182 compiled_classes
[c
.global
] = cc
184 # Assign a unique class identifier
185 # (negative are for primitive classes)
188 if c
.primitive_info
!= null then
190 pclassid
= pclassid
- 4
193 classid
= classid
+ 4
196 # Register is the class is a leaf
197 if c
.cshe
.direct_smallers
.is_empty
then
198 smallest_classes
.add
(c
)
201 # Store the colortableelt in the class table pool
202 var bc
= c
.global
.intro
203 assert bc
isa MMConcreteClass
204 ctab
.add
(bc
.class_color_pos
)
207 # Compute core and crown classes for colorization
208 var crown_classes
= new HashSet[MMLocalClass]
209 var core_classes
= new HashSet[MMLocalClass]
210 for c
in smallest_classes
do
211 while c
.cshe
.direct_greaters
.length
== 1 do
212 c
= c
.cshe
.direct_greaters
.first
215 core_classes
.add_all
(c
.cshe
.greaters_and_self
)
217 #print("nbclasses: {classes.length} leaves: {smallest_classes.length} crown: {crown_classes.length} core: {core_classes.length}")
219 # Colorize core color for typechecks
220 colorize
(ctab
, crown_classes
, 0)
222 # Compute tables for typechecks
225 var cc
= compiled_classes
[c
.global
]
226 if core_classes
.has
(c
) then
227 # For core classes, just build the table
228 build_tables_in
(cc
.class_table
, c
, ctab
)
229 if maxcolor
< cc
.class_table
.length
then maxcolor
= cc
.class_table
.length
231 # For other classes, it's easier: just append to the parent tables
232 var sc
= c
.cshe
.direct_greaters
.first
233 var scc
= compiled_classes
[sc
.global
]
234 assert cc
.class_table
.is_empty
235 cc
.class_table
.add_all
(scc
.class_table
)
236 var bc
= c
.global
.intro
237 assert bc
isa MMConcreteClass
238 var colpos
= bc
.class_color_pos
239 var colposcolor
= cc
.class_table
.length
240 table_information
.color
(colpos
) = colposcolor
241 cc
.class_table
.add
(colpos
)
242 if maxcolor
< colposcolor
then maxcolor
= colposcolor
245 table_information
.max_class_table_length
= maxcolor
+ 1
247 # Fill class table and instance tables pools
249 var cc
= compiled_classes
[c
.global
]
250 var cte
= cc
.class_layout
251 var ite
= cc
.instance_layout
252 for sc
in c
.crhe
.greaters_and_self
do
253 if sc
isa MMConcreteClass then
254 cte
.add
(sc
, sc
.class_layout
)
255 ite
.add
(sc
, sc
.instance_layout
)
259 if core_classes
.has
(c
) then
260 if cte
.length
> 0 then
263 if ite
.length
> 0 then
269 # Colorize all elements in pools tables
270 colorize
(ctab
, crown_classes
, maxcolor
+1)
271 colorize
(itab
, crown_classes
, 0)
273 # Build class and instance tables now things are colored
274 table_information
.max_class_table_length
= 0
276 var cc
= compiled_classes
[c
.global
]
277 if core_classes
.has
(c
) then
278 # For core classes, just build the table
279 build_tables_in
(cc
.class_table
, c
, ctab
)
280 build_tables_in
(cc
.instance_table
, c
, itab
)
282 # For other classes, it's easier: just append to the parent tables
283 var sc
= c
.cshe
.direct_greaters
.first
284 var scc
= compiled_classes
[sc
.global
]
286 cc
.class_table
.add_all
(scc
.class_table
)
287 var bc
= c
.global
.intro
288 assert bc
isa MMConcreteClass
289 var colpos
= bc
.class_color_pos
290 cc
.class_table
[table_information
.color
(colpos
)] = colpos
291 while cc
.class_table
.length
<= maxcolor
do
292 cc
.class_table
.add
(null)
294 append_to_table
(cc
.class_table
, cc
.class_layout
)
295 assert cc
.instance_table
.is_empty
296 cc
.instance_table
.add_all
(scc
.instance_table
)
297 append_to_table
(cc
.instance_table
, cc
.instance_layout
)
303 fun colorize
(elts
: Array[TableElt], classes
: Collection[MMLocalClass], startcolor
: Int)
305 var colors
= new HashMap[Int, Array[TableElt]]
306 var rel_classes
= new Array[MMLocalClass]
310 if table_information
.has_color
(e
) then
311 color
= table_information
.color
(e
)
315 if e
.is_related_to
(c
) then
319 var trycolor
= startcolor
320 while trycolor
!= color
do
322 for c
in rel_classes
do
325 if colors
.has_key
(trycolor
+ idx
) and not free_color
(colors
[trycolor
+ idx
], c
) then
326 trycolor
= trycolor
+ idx
+ 1
334 table_information
.color
(e
) = color
336 for idx
in [0..len
[ do
337 if colors
.has_key
(color
+ idx
) then
338 colors
[color
+ idx
].add
(e
)
340 colors
[color
+ idx
] = [e
]
346 private fun free_color
(es
: Array[TableElt], c
: MMLocalClass): Bool
349 if e2
.is_related_to
(c
) then
356 private fun append_to_table
(table
: Array[nullable TableElt], cmp
: TableEltComposite)
358 for j
in [0..cmp
.length
[ do
360 table_information
.color
(e
) = table
.length
365 private fun build_tables_in
(table
: Array[nullable TableElt], c
: MMLocalClass, elts
: Array[TableElt])
367 var tab
= new HashMap[Int, TableElt]
370 if e
.is_related_to
(c
) then
371 var col
= table_information
.color
(e
)
372 var l
= col
+ e
.length
381 if tab
.has_key
(i
) then
383 for j
in [0..e
.length
[ do
396 # The local table of the module (refers things introduced in the module)
397 readable var _local_table
: Array[ModuleTableElt] = new Array[ModuleTableElt]
399 # Builds the local tables and local classes layouts
400 private fun local_analysis
402 for c
in local_classes
do
403 if c
isa MMConcreteClass then
404 c
.build_layout_in
(_local_table
)
410 ###############################################################################
412 # An element of a class, an instance or a module table
413 abstract class AbsTableElt
416 # An element of a class or an instance table
417 # Such an elements represent method function pointers, attribute values, etc.
418 abstract class TableElt
420 # Is the element conflict to class `c' (used for coloring)
421 fun is_related_to
(c
: MMLocalClass): Bool is abstract
423 # Number of sub-elements. 1 if none
424 fun length
: Int do return 1
426 # Access the ith subelement.
427 fun item
(i
: Int): TableElt do return self
430 # An element of a module table
431 # Such an elements represent colors or identifiers
432 abstract class ModuleTableElt
436 # An element of a module table that represents a group of TableElt defined in the same local class
437 class ModuleTableEltGroup
439 readable var _elements
: Array[TableElt] = new Array[TableElt]
442 # An element that represents a class property
443 abstract class TableEltProp
445 readable var _property
: MMLocalProperty
447 init(p
: MMLocalProperty)
453 # An element that represents a function pointer to a global method
458 # An element that represents a class color value for a virtual type
459 class TableEltVTClassColor
463 # An element that represents a class id value for a virtual type
464 class TableEltVTClassId
468 # An element that represents a function pointer to the super method of a local method
473 # An element that represents the value stored for a global attribute
478 # An element representing a class information
479 class AbsTableEltClass
481 # The local class where the information comes from
482 readable var _local_class
: MMLocalClass
484 init(c
: MMLocalClass)
490 # An element of a class table representing a class information
493 super AbsTableEltClass
494 redef fun is_related_to
(c
)
496 var bc
= c
.mmmodule
[_local_class
.global
]
501 # An element representing the id of a class in a module table
502 class TableEltClassId
504 super AbsTableEltClass
507 # An element representing the constructor marker position in a class table
508 class TableEltClassInitTable
512 # An element used for a cast
513 # Note: this element is both a TableElt and a ModuleTableElt.
514 # At the TableElt offset, there is the id of the super-class
515 # At the ModuleTableElt offset, there is the TableElt offset (ie. the color of the super-class).
516 class TableEltClassColor
521 # A Group of elements introduced in the same global-class that are colored together
522 class TableEltComposite
524 var _table
: Array[TableElt]
525 var _cc
: CompiledClass
526 var _offsets
: HashMap[MMLocalClass, Int]
527 redef fun length
do return _table
.length
528 redef fun is_related_to
(c
) do return c
.cshe
<= _cc
.local_class
530 fun add
(c
: MMLocalClass, tab
: Array[TableElt])
532 _offsets
[c
] = _table
.length
536 redef fun item
(i
) do return _table
[i
]
538 init(cc
: CompiledClass)
541 _table
= new Array[TableElt]
542 _offsets
= new HashMap[MMLocalClass, Int]
546 # The element that represent the class id
547 class TableEltClassSelfId
549 redef fun is_related_to
(c
) do return true
552 # The element that represent the Object Size
553 class TableEltClassObjectSize
555 redef fun is_related_to
(c
) do return true
558 # The element that represent the object id
559 class TableEltObjectId
561 redef fun is_related_to
(c
) do return true
565 class TableEltVftPointer
567 redef fun is_related_to
(c
) do return true