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_hi_tag_method(cell obj, cell methods)
75 array *hi_tag_methods = untag<array>(methods);
76 cell tag = untag<object>(obj)->h.hi_tag() - HEADER_TYPE;
78 assert(tag < TYPE_COUNT - HEADER_TYPE);
80 return array_nth(hi_tag_methods,tag);
83 cell factor_vm::lookup_method(cell obj, cell methods)
86 cell method = array_nth(untag<array>(methods),tag);
90 if(TAG(method) == ARRAY_TYPE)
91 return lookup_tuple_method(obj,method);
95 else if(tag == OBJECT_TYPE)
97 if(TAG(method) == ARRAY_TYPE)
98 return lookup_hi_tag_method(obj,method);
106 void factor_vm::primitive_lookup_method()
108 cell methods = dpop();
110 dpush(lookup_method(obj,methods));
113 cell factor_vm::object_class(cell obj)
118 return untag<tuple>(obj)->layout;
120 return untag<object>(obj)->h.value;
122 return tag_fixnum(TAG(obj));
126 cell factor_vm::method_cache_hashcode(cell klass, array *array)
128 cell capacity = (array_capacity(array) >> 1) - 1;
129 return (((klass >> 3) + (klass >> 8) + (klass >> 13)) & capacity) << 1;
132 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
134 array *cache_elements = untag<array>(cache);
135 cell hashcode = method_cache_hashcode(klass,cache_elements);
136 set_array_nth(cache_elements,hashcode,klass);
137 set_array_nth(cache_elements,hashcode + 1,method);
140 void factor_vm::primitive_mega_cache_miss()
142 megamorphic_cache_misses++;
145 fixnum index = untag_fixnum(dpop());
146 cell methods = dpop();
148 cell object = ((cell *)ds)[-index];
149 cell klass = object_class(object);
150 cell method = lookup_method(object,methods);
152 update_method_cache(cache,klass,method);
157 void factor_vm::primitive_reset_dispatch_stats()
159 megamorphic_cache_hits = megamorphic_cache_misses = 0;
162 void factor_vm::primitive_dispatch_stats()
164 growable_array stats(this);
165 stats.add(allot_cell(megamorphic_cache_hits));
166 stats.add(allot_cell(megamorphic_cache_misses));
168 dpush(stats.elements.value());
171 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
173 gc_root<array> methods(methods_,parent);
174 gc_root<array> cache(cache_,parent);
176 /* Generate machine code to determine the object's class. */
177 emit_class_lookup(index,PIC_HI_TAG_TUPLE);
179 /* Do a cache lookup. */
180 emit_with(parent->special_objects[MEGA_LOOKUP],cache.value());
182 /* If we end up here, the cache missed. */
183 emit(parent->special_objects[JIT_PROLOG]);
185 /* Push index, method table and cache on the stack. */
186 push(methods.value());
187 push(tag_fixnum(index));
189 word_call(parent->special_objects[MEGA_MISS_WORD]);
191 /* Now the new method has been stored into the cache, and its on
193 emit(parent->special_objects[JIT_EPILOG]);
194 emit(parent->special_objects[JIT_EXECUTE_JUMP]);