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 inline void factor_vm::primitive_lookup_method()
118 cell methods = dpop();
120 dpush(lookup_method(obj,methods));
123 PRIMITIVE_FORWARD(lookup_method)
125 cell factor_vm::object_class(cell obj)
130 return untag<tuple>(obj)->layout;
132 return untag<object>(obj)->h.value;
134 return tag_fixnum(TAG(obj));
138 cell factor_vm::method_cache_hashcode(cell klass, array *array)
140 cell capacity = (array_capacity(array) >> 1) - 1;
141 return ((klass >> TAG_BITS) & capacity) << 1;
144 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
146 array *cache_elements = untag<array>(cache);
147 cell hashcode = method_cache_hashcode(klass,cache_elements);
148 set_array_nth(cache_elements,hashcode,klass);
149 set_array_nth(cache_elements,hashcode + 1,method);
152 inline void factor_vm::primitive_mega_cache_miss()
154 megamorphic_cache_misses++;
157 fixnum index = untag_fixnum(dpop());
158 cell methods = dpop();
160 cell object = ((cell *)ds)[-index];
161 cell klass = object_class(object);
162 cell method = lookup_method(object,methods);
164 update_method_cache(cache,klass,method);
169 PRIMITIVE_FORWARD(mega_cache_miss)
171 inline void factor_vm::primitive_reset_dispatch_stats()
173 megamorphic_cache_hits = megamorphic_cache_misses = 0;
176 PRIMITIVE_FORWARD(reset_dispatch_stats)
178 inline void factor_vm::primitive_dispatch_stats()
180 growable_array stats(this);
181 stats.add(allot_cell(megamorphic_cache_hits));
182 stats.add(allot_cell(megamorphic_cache_misses));
184 dpush(stats.elements.value());
187 PRIMITIVE_FORWARD(dispatch_stats)
189 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
191 gc_root<array> methods(methods_,parent_vm);
192 gc_root<array> cache(cache_,parent_vm);
194 /* Generate machine code to determine the object's class. */
195 emit_class_lookup(index,PIC_HI_TAG_TUPLE);
197 /* Do a cache lookup. */
198 emit_with(parent_vm->userenv[MEGA_LOOKUP],cache.value());
200 /* If we end up here, the cache missed. */
201 emit(parent_vm->userenv[JIT_PROLOG]);
203 /* Push index, method table and cache on the stack. */
204 push(methods.value());
205 push(tag_fixnum(index));
207 word_call(parent_vm->userenv[MEGA_MISS_WORD]);
209 /* Now the new method has been stored into the cache, and its on
211 emit(parent_vm->userenv[JIT_EPILOG]);
212 emit(parent_vm->userenv[JIT_EXECUTE_JUMP]);