6 cell factor_vm::search_lookup_alist(cell table, cell klass)
8 array *elements = untag<array>(table);
9 fixnum index = array_capacity(elements) - 2;
12 if(array_nth(elements,index) == klass)
13 return array_nth(elements,index + 1);
21 cell factor_vm::search_lookup_hash(cell table, cell klass, cell hashcode)
23 array *buckets = untag<array>(table);
24 cell bucket = array_nth(buckets,hashcode & (array_capacity(buckets) - 1));
25 if(TAG(bucket) == ARRAY_TYPE)
26 return search_lookup_alist(bucket,klass);
31 cell factor_vm::nth_superclass(tuple_layout *layout, fixnum echelon)
33 cell *ptr = (cell *)(layout + 1);
34 return ptr[echelon * 2];
37 cell factor_vm::nth_hashcode(tuple_layout *layout, fixnum echelon)
39 cell *ptr = (cell *)(layout + 1);
40 return ptr[echelon * 2 + 1];
43 cell factor_vm::lookup_tuple_method(cell obj, cell methods)
45 tuple_layout *layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
47 array *echelons = untag<array>(methods);
49 fixnum echelon = std::min(untag_fixnum(layout->echelon),(fixnum)array_capacity(echelons) - 1);
53 cell echelon_methods = array_nth(echelons,echelon);
55 if(tagged<object>(echelon_methods).type_p(WORD_TYPE))
56 return echelon_methods;
57 else if(to_boolean(echelon_methods))
59 cell klass = nth_superclass(layout,echelon);
60 cell hashcode = untag_fixnum(nth_hashcode(layout,echelon));
61 cell result = search_lookup_hash(echelon_methods,klass,hashcode);
62 if(to_boolean(result))
69 critical_error("Cannot find tuple method",methods);
73 cell factor_vm::lookup_method(cell obj, cell methods)
76 cell method = array_nth(untag<array>(methods),tag);
80 if(TAG(method) == ARRAY_TYPE)
81 return lookup_tuple_method(obj,method);
89 void factor_vm::primitive_lookup_method()
91 cell methods = ctx->pop();
92 cell obj = ctx->pop();
93 ctx->push(lookup_method(obj,methods));
96 cell factor_vm::object_class(cell obj)
100 return untag<tuple>(obj)->layout;
102 return tag_fixnum(tag);
105 cell factor_vm::method_cache_hashcode(cell klass, array *array)
107 cell capacity = (array_capacity(array) >> 1) - 1;
108 return ((klass >> TAG_BITS) & capacity) << 1;
111 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
113 array *cache_elements = untag<array>(cache);
114 cell hashcode = method_cache_hashcode(klass,cache_elements);
115 set_array_nth(cache_elements,hashcode,klass);
116 set_array_nth(cache_elements,hashcode + 1,method);
119 void factor_vm::primitive_mega_cache_miss()
121 dispatch_stats.megamorphic_cache_misses++;
123 cell cache = ctx->pop();
124 fixnum index = untag_fixnum(ctx->pop());
125 cell methods = ctx->pop();
127 cell object = ((cell *)ctx->datastack)[-index];
128 cell klass = object_class(object);
129 cell method = lookup_method(object,methods);
131 update_method_cache(cache,klass,method);
136 void factor_vm::primitive_reset_dispatch_stats()
138 memset(&dispatch_stats,0,sizeof(dispatch_statistics));
141 void factor_vm::primitive_dispatch_stats()
143 ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
146 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
148 data_root<array> methods(methods_,parent);
149 data_root<array> cache(cache_,parent);
151 /* Generate machine code to determine the object's class. */
152 emit_class_lookup(index,PIC_TUPLE);
154 /* Do a cache lookup. */
155 emit_with_literal(parent->special_objects[MEGA_LOOKUP],cache.value());
157 /* If we end up here, the cache missed. */
158 emit(parent->special_objects[JIT_PROLOG]);
160 /* Push index, method table and cache on the stack. */
161 push(methods.value());
162 push(tag_fixnum(index));
164 word_call(parent->special_objects[MEGA_MISS_WORD]);
166 /* Now the new method has been stored into the cache, and its on
168 emit(parent->special_objects[JIT_EPILOG]);
169 emit(parent->special_objects[JIT_EXECUTE]);