| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | ! Copyright (C) 2011 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:10:21 -05:00
										 |  |  | USING: accessors arrays assocs bit-arrays classes.tuple | 
					
						
							| 
									
										
										
										
											2015-03-04 15:06:42 -05:00
										 |  |  | compiler.codegen.relocation cpu.architecture fry kernel layouts make math | 
					
						
							|  |  |  | math.order namespaces sequences ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | IN: compiler.codegen.gc-maps | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOLS: return-addresses gc-maps ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 14:40:01 -04:00
										 |  |  | : gc-map-needed? ( gc-map/f -- ? )
 | 
					
						
							|  |  |  |     dup [ tuple-slots [ empty? ] all? not ] when ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : gc-map-here ( gc-map -- )
 | 
					
						
							|  |  |  |     dup gc-map-needed? [ | 
					
						
							|  |  |  |         gc-maps get push
 | 
					
						
							|  |  |  |         compiled-offset return-addresses get push
 | 
					
						
							|  |  |  |     ] [ drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : integers>bits ( seq n -- bit-array )
 | 
					
						
							|  |  |  |     <bit-array> [ '[ [ t ] dip _ set-nth ] each ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : largest-spill-slot ( seqs -- n )
 | 
					
						
							| 
									
										
										
										
											2015-06-13 22:15:38 -04:00
										 |  |  |     concat [ 0 ] [ supremum 1 + ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : emit-gc-roots ( seqs -- n )
 | 
					
						
							|  |  |  |     ! seqs is a sequence of sequences of integers 0..n-1 | 
					
						
							| 
									
										
										
										
											2015-03-04 15:06:42 -05:00
										 |  |  |     dup largest-spill-slot [ '[ _ integers>bits % ] each ] keep ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : emit-uint ( n -- )
 | 
					
						
							|  |  |  |     building get push-uint ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : emit-uints ( n -- )
 | 
					
						
							|  |  |  |     [ emit-uint ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gc-root-offsets ( gc-map -- offsets )
 | 
					
						
							|  |  |  |     gc-roots>> [ gc-root-offset ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 14:34:44 -04:00
										 |  |  | : emit-gc-info-bitmap ( gc-maps -- spill-count )
 | 
					
						
							|  |  |  |     [ gc-root-offsets ] map
 | 
					
						
							|  |  |  |     [ emit-gc-roots ] ?{ } make underlying>> % ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : emit-base-table ( alist longest -- )
 | 
					
						
							| 
									
										
										
										
											2017-07-03 15:00:44 -04:00
										 |  |  |     -1 <array> <enumerated> swap assoc-union! seq>> emit-uints ;
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : derived-root-offsets ( gc-map -- offsets )
 | 
					
						
							|  |  |  |     derived-roots>> [ [ gc-root-offset ] bi@ ] assoc-map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-13 22:15:38 -04:00
										 |  |  | : emit-base-tables ( gc-maps -- count )
 | 
					
						
							|  |  |  |     [ derived-root-offsets ] map
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  |     dup [ keys ] map largest-spill-slot | 
					
						
							|  |  |  |     [ '[ _ emit-base-table ] each ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : serialize-gc-maps ( -- byte-array )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2015-06-13 22:15:38 -04:00
										 |  |  |         return-addresses get empty? [ { } ] [ | 
					
						
							| 
									
										
										
										
											2016-09-11 14:34:44 -04:00
										 |  |  |             gc-maps get [ emit-gc-info-bitmap ] [ emit-base-tables ] bi 2array
 | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2015-06-13 22:15:38 -04:00
										 |  |  |         return-addresses get emit-uints | 
					
						
							|  |  |  |         emit-uints | 
					
						
							|  |  |  |         return-addresses get length emit-uint | 
					
						
							| 
									
										
										
										
											2011-09-14 00:38:03 -04:00
										 |  |  |     ] B{ } make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : emit-gc-maps ( -- )
 | 
					
						
							|  |  |  |     serialize-gc-maps [ | 
					
						
							|  |  |  |         length compiled-offset +
 | 
					
						
							|  |  |  |         [ data-alignment get align ] keep -
 | 
					
						
							|  |  |  |         (align-code) | 
					
						
							|  |  |  |     ] [ % ] bi ;
 |