| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | namespace factor { | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_special_object() { | 
					
						
							|  |  |  |   fixnum n = untag_fixnum(ctx->peek()); | 
					
						
							|  |  |  |   ctx->replace(special_objects[n]); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_set_special_object() { | 
					
						
							|  |  |  |   fixnum n = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   cell value = ctx->pop(); | 
					
						
							|  |  |  |   special_objects[n] = value; | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_identity_hashcode() { | 
					
						
							|  |  |  |   cell tagged = ctx->peek(); | 
					
						
							|  |  |  |   object* obj = untag<object>(tagged); | 
					
						
							|  |  |  |   ctx->replace(tag_fixnum(obj->hashcode())); | 
					
						
							| 
									
										
										
										
											2009-11-11 01:50:57 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::compute_identity_hashcode(object* obj) { | 
					
						
							|  |  |  |   object_counter++; | 
					
						
							|  |  |  |   if (object_counter == 0) | 
					
						
							|  |  |  |     object_counter++; | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   obj->set_hashcode((cell)obj ^ object_counter); | 
					
						
							| 
									
										
										
										
											2009-11-11 01:50:57 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_compute_identity_hashcode() { | 
					
						
							|  |  |  |   object* obj = untag<object>(ctx->pop()); | 
					
						
							|  |  |  |   compute_identity_hashcode(obj); | 
					
						
							| 
									
										
										
										
											2009-11-10 22:06:36 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_set_slot() { | 
					
						
							|  |  |  |   fixnum slot = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   object* obj = untag<object>(ctx->pop()); | 
					
						
							|  |  |  |   cell value = ctx->pop(); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   cell* slot_ptr = &obj->slots()[slot]; | 
					
						
							|  |  |  |   *slot_ptr = value; | 
					
						
							|  |  |  |   write_barrier(slot_ptr); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 17:05:05 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | cell factor_vm::clone_object(cell obj_) { | 
					
						
							|  |  |  |   data_root<object> obj(obj_, this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (immediate_p(obj.value())) | 
					
						
							|  |  |  |     return obj.value(); | 
					
						
							| 
									
										
										
										
											2016-03-30 18:06:35 -04:00
										 |  |  |   cell size = object_size(obj.value()); | 
					
						
							|  |  |  |   object* new_obj = allot_object(obj.type(), size); | 
					
						
							|  |  |  |   memcpy(new_obj, obj.untagged(), size); | 
					
						
							|  |  |  |   new_obj->set_hashcode(0); | 
					
						
							|  |  |  |   return tag_dynamic(new_obj); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 17:05:05 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_clone() { ctx->replace(clone_object(ctx->peek())); } | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Size of the object pointed to by a tagged pointer */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | cell factor_vm::object_size(cell tagged) { | 
					
						
							|  |  |  |   if (immediate_p(tagged)) | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-03-30 18:06:35 -04:00
										 |  |  |   return untag<object>(tagged)->size(); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_size() { | 
					
						
							| 
									
										
										
										
											2014-06-07 15:16:43 -04:00
										 |  |  |   ctx->replace(from_unsigned_cell(object_size(ctx->peek()))); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 20:06:00 -04:00
										 |  |  | struct slot_become_fixup : no_fixup { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   std::map<object*, object*>* become_map; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-12 23:20:43 -04:00
										 |  |  |   slot_become_fixup(std::map<object*, object*>* become_map) | 
					
						
							|  |  |  |       : become_map(become_map) {} | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   object* fixup_data(object* old) { | 
					
						
							|  |  |  |     std::map<object*, object*>::const_iterator iter = become_map->find(old); | 
					
						
							|  |  |  |     if (iter != become_map->end()) | 
					
						
							|  |  |  |       return iter->second; | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |     return old; | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
 | 
					
						
							|  |  |  |    to coalesce equal but distinct quotations and wrappers. */ | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Calls gc */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | void factor_vm::primitive_become() { | 
					
						
							| 
									
										
										
										
											2014-11-20 17:30:43 -05:00
										 |  |  |   primitive_minor_gc(); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   array* new_objects = untag_check<array>(ctx->pop()); | 
					
						
							|  |  |  |   array* old_objects = untag_check<array>(ctx->pop()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cell capacity = array_capacity(new_objects); | 
					
						
							|  |  |  |   if (capacity != array_capacity(old_objects)) | 
					
						
							|  |  |  |     critical_error("bad parameters to become", 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Build the forwarding map */ | 
					
						
							|  |  |  |   std::map<object*, object*> become_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (cell i = 0; i < capacity; i++) { | 
					
						
							| 
									
										
										
										
											2015-11-22 20:27:22 -05:00
										 |  |  |     cell old_ptr = array_nth(old_objects, i); | 
					
						
							|  |  |  |     cell new_ptr = array_nth(new_objects, i); | 
					
						
							|  |  |  |     if (old_ptr != new_ptr) | 
					
						
							|  |  |  |       become_map[untag<object>(old_ptr)] = untag<object>(new_ptr); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Update all references to old objects to point to new objects */ | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-02-18 23:51:45 -05:00
										 |  |  |     slot_visitor<slot_become_fixup> visitor(this, | 
					
						
							|  |  |  |                                             slot_become_fixup(&become_map)); | 
					
						
							|  |  |  |     visitor.visit_all_roots(); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |     auto object_become_func = [&](object* obj) { | 
					
						
							| 
									
										
										
										
											2015-07-06 11:49:55 -04:00
										 |  |  |       visitor.visit_slots(obj); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |     each_object(object_become_func); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |     auto code_block_become_func = [&](code_block* compiled, cell size) { | 
					
						
							| 
									
										
										
										
											2015-07-06 11:49:55 -04:00
										 |  |  |       visitor.visit_code_block_objects(compiled); | 
					
						
							|  |  |  |       visitor.visit_embedded_literals(compiled); | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |       code->write_barrier(compiled); | 
					
						
							| 
									
										
										
										
											2015-07-06 11:49:55 -04:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-08-01 11:30:20 -04:00
										 |  |  |     each_code_block(code_block_become_func); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:17:38 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Since we may have introduced old->new references, need to revisit
 | 
					
						
							|  |  |  |      all objects and code blocks on a minor GC. */ | 
					
						
							|  |  |  |   data->mark_all_cards(); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |