| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | namespace factor { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 21:03:07 -04:00
										 |  |  | static cell search_lookup_alist(cell table, cell klass) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   array* elements = untag<array>(table); | 
					
						
							| 
									
										
										
										
											2016-05-28 21:03:07 -04:00
										 |  |  |   for (fixnum index = array_capacity(elements) - 2; index >= 0; index -= 2) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |     if (array_nth(elements, index) == klass) | 
					
						
							|  |  |  |       return array_nth(elements, index + 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return false_object; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 21:03:07 -04:00
										 |  |  | static cell search_lookup_hash(cell table, cell klass, cell hashcode) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   array* buckets = untag<array>(table); | 
					
						
							|  |  |  |   cell bucket = array_nth(buckets, hashcode & (array_capacity(buckets) - 1)); | 
					
						
							|  |  |  |   if (TAG(bucket) == ARRAY_TYPE) | 
					
						
							|  |  |  |     return search_lookup_alist(bucket, klass); | 
					
						
							| 
									
										
										
										
											2016-03-30 18:06:35 -04:00
										 |  |  |   return bucket; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 21:03:07 -04:00
										 |  |  | static cell nth_superclass(tuple_layout* layout, fixnum echelon) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   cell* ptr = (cell*)(layout + 1); | 
					
						
							|  |  |  |   return ptr[echelon * 2]; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 21:03:07 -04:00
										 |  |  | static cell nth_hashcode(tuple_layout* layout, fixnum echelon) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   cell* ptr = (cell*)(layout + 1); | 
					
						
							|  |  |  |   return ptr[echelon * 2 + 1]; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | cell factor_vm::lookup_tuple_method(cell obj, cell methods) { | 
					
						
							|  |  |  |   tuple_layout* layout = untag<tuple_layout>(untag<tuple>(obj)->layout); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   array* echelons = untag<array>(methods); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   fixnum echelon = std::min(untag_fixnum(layout->echelon), | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |                             (fixnum)array_capacity(echelons) - 1); | 
					
						
							| 
									
										
										
										
											2009-10-31 03:30:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   while (echelon >= 0) { | 
					
						
							|  |  |  |     cell echelon_methods = array_nth(echelons, echelon); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 17:43:50 -05:00
										 |  |  |     if (TAG(echelon_methods) == WORD_TYPE) | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |       return echelon_methods; | 
					
						
							|  |  |  |     else if (to_boolean(echelon_methods)) { | 
					
						
							|  |  |  |       cell klass = nth_superclass(layout, echelon); | 
					
						
							|  |  |  |       cell hashcode = untag_fixnum(nth_hashcode(layout, echelon)); | 
					
						
							|  |  |  |       cell result = search_lookup_hash(echelon_methods, klass, hashcode); | 
					
						
							|  |  |  |       if (to_boolean(result)) | 
					
						
							|  |  |  |         return result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |     echelon--; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   critical_error("Cannot find tuple method", methods); | 
					
						
							|  |  |  |   return false_object; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | cell factor_vm::lookup_method(cell obj, cell methods) { | 
					
						
							|  |  |  |   cell tag = TAG(obj); | 
					
						
							|  |  |  |   cell method = array_nth(untag<array>(methods), tag); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (tag == TUPLE_TYPE) { | 
					
						
							|  |  |  |     if (TAG(method) == ARRAY_TYPE) | 
					
						
							|  |  |  |       return lookup_tuple_method(obj, method); | 
					
						
							|  |  |  |     return method; | 
					
						
							| 
									
										
										
										
											2016-03-30 18:06:35 -04:00
										 |  |  |   } | 
					
						
							|  |  |  |   return method; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | void factor_vm::primitive_lookup_method() { | 
					
						
							|  |  |  |   cell methods = ctx->pop(); | 
					
						
							|  |  |  |   cell obj = ctx->pop(); | 
					
						
							|  |  |  |   ctx->push(lookup_method(obj, methods)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | cell factor_vm::object_class(cell obj) { | 
					
						
							|  |  |  |   cell tag = TAG(obj); | 
					
						
							|  |  |  |   if (tag == TUPLE_TYPE) | 
					
						
							|  |  |  |     return untag<tuple>(obj)->layout; | 
					
						
							| 
									
										
										
										
											2016-03-30 18:06:35 -04:00
										 |  |  |   return tag_fixnum(tag); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:19:32 -04:00
										 |  |  | static cell method_cache_hashcode(cell klass, array* array) { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   cell capacity = (array_capacity(array) >> 1) - 1; | 
					
						
							|  |  |  |   return ((klass >> TAG_BITS) & capacity) << 1; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | void factor_vm::update_method_cache(cell cache, cell klass, cell method) { | 
					
						
							|  |  |  |   array* cache_elements = untag<array>(cache); | 
					
						
							|  |  |  |   cell hashcode = method_cache_hashcode(klass, cache_elements); | 
					
						
							|  |  |  |   set_array_nth(cache_elements, hashcode, klass); | 
					
						
							|  |  |  |   set_array_nth(cache_elements, hashcode + 1, method); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | void factor_vm::primitive_mega_cache_miss() { | 
					
						
							|  |  |  |   dispatch_stats.megamorphic_cache_misses++; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   cell cache = ctx->pop(); | 
					
						
							|  |  |  |   fixnum index = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   cell methods = ctx->pop(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   cell object = ((cell*)ctx->datastack)[-index]; | 
					
						
							|  |  |  |   cell klass = object_class(object); | 
					
						
							|  |  |  |   cell method = lookup_method(object, methods); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   update_method_cache(cache, klass, method); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  |   ctx->push(method); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | void factor_vm::primitive_reset_dispatch_stats() { | 
					
						
							|  |  |  |   memset(&dispatch_stats, 0, sizeof(dispatch_statistics)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-21 10:26:04 -04:00
										 |  |  | // Allocates memory
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:57:45 -04:00
										 |  |  | void factor_vm::primitive_dispatch_stats() { | 
					
						
							|  |  |  |   ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |