6 cell megamorphic_cache_hits;
7 cell megamorphic_cache_misses;
9 static cell search_lookup_alist(cell table, cell klass)
11 array *elements = untag<array>(table);
12 fixnum index = array_capacity(elements) - 2;
15 if(array_nth(elements,index) == klass)
16 return array_nth(elements,index + 1);
24 static cell search_lookup_hash(cell table, cell klass, cell hashcode)
26 array *buckets = untag<array>(table);
27 cell bucket = array_nth(buckets,hashcode & (array_capacity(buckets) - 1));
28 if(tagged<object>(bucket).type_p(WORD_TYPE) || bucket == F)
31 return search_lookup_alist(bucket,klass);
34 static cell nth_superclass(tuple_layout *layout, fixnum echelon)
36 cell *ptr = (cell *)(layout + 1);
37 return ptr[echelon * 2];
40 static cell nth_hashcode(tuple_layout *layout, fixnum echelon)
42 cell *ptr = (cell *)(layout + 1);
43 return ptr[echelon * 2 + 1];
46 static cell lookup_tuple_method(cell obj, cell methods)
48 tuple_layout *layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
50 array *echelons = untag<array>(methods);
52 fixnum echelon = untag_fixnum(layout->echelon);
53 fixnum max_echelon = array_capacity(echelons) - 1;
54 if(echelon > max_echelon) echelon = max_echelon;
58 cell echelon_methods = array_nth(echelons,echelon);
60 if(tagged<object>(echelon_methods).type_p(WORD_TYPE))
61 return echelon_methods;
62 else if(echelon_methods != F)
64 cell klass = nth_superclass(layout,echelon);
65 cell hashcode = untag_fixnum(nth_hashcode(layout,echelon));
66 cell result = search_lookup_hash(echelon_methods,klass,hashcode);
74 critical_error("Cannot find tuple method",methods);
78 static cell lookup_hi_tag_method(cell obj, cell methods)
80 array *hi_tag_methods = untag<array>(methods);
81 cell tag = untag<object>(obj)->h.hi_tag() - HEADER_TYPE;
83 assert(tag < TYPE_COUNT - HEADER_TYPE);
85 return array_nth(hi_tag_methods,tag);
88 static cell lookup_hairy_method(cell obj, cell methods)
90 cell method = array_nth(untag<array>(methods),TAG(obj));
91 if(tagged<object>(method).type_p(WORD_TYPE))
98 return lookup_tuple_method(obj,method);
101 return lookup_hi_tag_method(obj,method);
104 critical_error("Bad methods array",methods);
110 cell lookup_method(cell obj, cell methods)
113 if(tag == TUPLE_TYPE || tag == OBJECT_TYPE)
114 return lookup_hairy_method(obj,methods);
116 return array_nth(untag<array>(methods),TAG(obj));
119 PRIMITIVE(lookup_method)
121 cell methods = dpop();
123 dpush(lookup_method(obj,methods));
126 cell object_class(cell obj)
131 return untag<tuple>(obj)->layout;
133 return untag<object>(obj)->h.value;
135 return tag_fixnum(TAG(obj));
139 static cell method_cache_hashcode(cell klass, array *array)
141 cell capacity = (array_capacity(array) >> 1) - 1;
142 return ((klass >> TAG_BITS) & capacity) << 1;
145 static void update_method_cache(cell cache, cell klass, cell method)
147 array *cache_elements = untag<array>(cache);
148 cell hashcode = method_cache_hashcode(klass,cache_elements);
149 set_array_nth(cache_elements,hashcode,klass);
150 set_array_nth(cache_elements,hashcode + 1,method);
153 PRIMITIVE(mega_cache_miss)
155 megamorphic_cache_misses++;
158 fixnum index = untag_fixnum(dpop());
159 cell methods = dpop();
161 cell object = ((cell *)ds)[-index];
162 cell klass = object_class(object);
163 cell method = lookup_method(object,methods);
165 update_method_cache(cache,klass,method);
170 PRIMITIVE(reset_dispatch_stats)
172 megamorphic_cache_hits = megamorphic_cache_misses = 0;
175 PRIMITIVE(dispatch_stats)
177 growable_array stats;
178 stats.add(allot_cell(megamorphic_cache_hits));
179 stats.add(allot_cell(megamorphic_cache_misses));
181 dpush(stats.elements.value());
184 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
186 gc_root<array> methods(methods_);
187 gc_root<array> cache(cache_);
189 /* Generate machine code to determine the object's class. */
190 emit_class_lookup(index,PIC_HI_TAG_TUPLE);
192 /* Do a cache lookup. */
193 emit_with(userenv[MEGA_LOOKUP],cache.value());
195 /* If we end up here, the cache missed. */
196 emit(userenv[JIT_PROLOG]);
198 /* Push index, method table and cache on the stack. */
199 push(methods.value());
200 push(tag_fixnum(index));
202 word_call(userenv[MEGA_MISS_WORD]);
204 /* Now the new method has been stored into the cache, and its on
206 emit(userenv[JIT_EPILOG]);
207 emit(userenv[JIT_EXECUTE_JUMP]);