6 cell factorvm::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 factorvm::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 factorvm::nth_superclass(tuple_layout *layout, fixnum echelon)
33 cell *ptr = (cell *)(layout + 1);
34 return ptr[echelon * 2];
37 cell factorvm::nth_hashcode(tuple_layout *layout, fixnum echelon)
39 cell *ptr = (cell *)(layout + 1);
40 return ptr[echelon * 2 + 1];
43 cell factorvm::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 factorvm::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 factorvm::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 factorvm::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 factorvm::primitive_lookup_method()
118 cell methods = dpop();
120 dpush(lookup_method(obj,methods));
123 PRIMITIVE(lookup_method)
125 PRIMITIVE_GETVM()->primitive_lookup_method();
128 cell factorvm::object_class(cell obj)
133 return untag<tuple>(obj)->layout;
135 return untag<object>(obj)->h.value;
137 return tag_fixnum(TAG(obj));
141 cell factorvm::method_cache_hashcode(cell klass, array *array)
143 cell capacity = (array_capacity(array) >> 1) - 1;
144 return ((klass >> TAG_BITS) & capacity) << 1;
147 void factorvm::update_method_cache(cell cache, cell klass, cell method)
149 array *cache_elements = untag<array>(cache);
150 cell hashcode = method_cache_hashcode(klass,cache_elements);
151 set_array_nth(cache_elements,hashcode,klass);
152 set_array_nth(cache_elements,hashcode + 1,method);
155 inline void factorvm::primitive_mega_cache_miss()
157 megamorphic_cache_misses++;
160 fixnum index = untag_fixnum(dpop());
161 cell methods = dpop();
163 cell object = ((cell *)ds)[-index];
164 cell klass = object_class(object);
165 cell method = lookup_method(object,methods);
167 update_method_cache(cache,klass,method);
172 PRIMITIVE(mega_cache_miss)
174 PRIMITIVE_GETVM()->primitive_mega_cache_miss();
177 inline void factorvm::primitive_reset_dispatch_stats()
179 megamorphic_cache_hits = megamorphic_cache_misses = 0;
182 PRIMITIVE(reset_dispatch_stats)
184 PRIMITIVE_GETVM()->primitive_reset_dispatch_stats();
187 inline void factorvm::primitive_dispatch_stats()
189 growable_array stats(this);
190 stats.add(allot_cell(megamorphic_cache_hits));
191 stats.add(allot_cell(megamorphic_cache_misses));
193 dpush(stats.elements.value());
196 PRIMITIVE(dispatch_stats)
198 PRIMITIVE_GETVM()->primitive_dispatch_stats();
201 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
203 gc_root<array> methods(methods_,myvm);
204 gc_root<array> cache(cache_,myvm);
206 /* Generate machine code to determine the object's class. */
207 emit_class_lookup(index,PIC_HI_TAG_TUPLE);
209 /* Do a cache lookup. */
210 emit_with(myvm->userenv[MEGA_LOOKUP],cache.value());
212 /* If we end up here, the cache missed. */
213 emit(myvm->userenv[JIT_PROLOG]);
215 /* Push index, method table and cache on the stack. */
216 push(methods.value());
217 push(tag_fixnum(index));
219 word_call(myvm->userenv[MEGA_MISS_WORD]);
221 /* Now the new method has been stored into the cache, and its on
223 emit(myvm->userenv[JIT_EPILOG]);
224 emit(myvm->userenv[JIT_EXECUTE_JUMP]);