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(tagged<object>(bucket).type_p(WORD_TYPE) || bucket == F)
28 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 = untag_fixnum(layout->echelon);
50 fixnum max_echelon = array_capacity(echelons) - 1;
51 if(echelon > max_echelon) echelon = max_echelon;
55 cell echelon_methods = array_nth(echelons,echelon);
57 if(tagged<object>(echelon_methods).type_p(WORD_TYPE))
58 return echelon_methods;
59 else if(echelon_methods != F)
61 cell klass = nth_superclass(layout,echelon);
62 cell hashcode = untag_fixnum(nth_hashcode(layout,echelon));
63 cell result = search_lookup_hash(echelon_methods,klass,hashcode);
71 critical_error("Cannot find tuple method",methods);
75 cell factor_vm::lookup_hi_tag_method(cell obj, cell methods)
77 array *hi_tag_methods = untag<array>(methods);
78 cell tag = untag<object>(obj)->h.hi_tag() - HEADER_TYPE;
80 assert(tag < TYPE_COUNT - HEADER_TYPE);
82 return array_nth(hi_tag_methods,tag);
85 cell factor_vm::lookup_hairy_method(cell obj, cell methods)
87 cell method = array_nth(untag<array>(methods),TAG(obj));
88 if(tagged<object>(method).type_p(WORD_TYPE))
95 return lookup_tuple_method(obj,method);
98 return lookup_hi_tag_method(obj,method);
101 critical_error("Bad methods array",methods);
107 cell factor_vm::lookup_method(cell obj, cell methods)
110 if(tag == TUPLE_TYPE || tag == OBJECT_TYPE)
111 return lookup_hairy_method(obj,methods);
113 return array_nth(untag<array>(methods),TAG(obj));
116 void factor_vm::primitive_lookup_method()
118 cell methods = dpop();
120 dpush(lookup_method(obj,methods));
123 cell factor_vm::object_class(cell obj)
128 return untag<tuple>(obj)->layout;
130 return untag<object>(obj)->h.value;
132 return tag_fixnum(TAG(obj));
136 cell factor_vm::method_cache_hashcode(cell klass, array *array)
138 cell capacity = (array_capacity(array) >> 1) - 1;
139 return ((klass >> TAG_BITS) & capacity) << 1;
142 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
144 array *cache_elements = untag<array>(cache);
145 cell hashcode = method_cache_hashcode(klass,cache_elements);
146 set_array_nth(cache_elements,hashcode,klass);
147 set_array_nth(cache_elements,hashcode + 1,method);
150 void factor_vm::primitive_mega_cache_miss()
152 megamorphic_cache_misses++;
155 fixnum index = untag_fixnum(dpop());
156 cell methods = dpop();
158 cell object = ((cell *)ds)[-index];
159 cell klass = object_class(object);
160 cell method = lookup_method(object,methods);
162 update_method_cache(cache,klass,method);
167 void factor_vm::primitive_reset_dispatch_stats()
169 megamorphic_cache_hits = megamorphic_cache_misses = 0;
172 void factor_vm::primitive_dispatch_stats()
174 growable_array stats(this);
175 stats.add(allot_cell(megamorphic_cache_hits));
176 stats.add(allot_cell(megamorphic_cache_misses));
178 dpush(stats.elements.value());
181 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
183 gc_root<array> methods(methods_,parent_vm);
184 gc_root<array> cache(cache_,parent_vm);
186 /* Generate machine code to determine the object's class. */
187 emit_class_lookup(index,PIC_HI_TAG_TUPLE);
189 /* Do a cache lookup. */
190 emit_with(parent_vm->userenv[MEGA_LOOKUP],cache.value());
192 /* If we end up here, the cache missed. */
193 emit(parent_vm->userenv[JIT_PROLOG]);
195 /* Push index, method table and cache on the stack. */
196 push(methods.value());
197 push(tag_fixnum(index));
199 word_call(parent_vm->userenv[MEGA_MISS_WORD]);
201 /* Now the new method has been stored into the cache, and its on
203 emit(parent_vm->userenv[JIT_EPILOG]);
204 emit(parent_vm->userenv[JIT_EXECUTE_JUMP]);