5 cell factor_vm::search_lookup_alist(cell table, cell klass) {
6 array* elements = untag<array>(table);
7 fixnum index = array_capacity(elements) - 2;
9 if (array_nth(elements, index) == klass)
10 return array_nth(elements, index + 1);
18 cell factor_vm::search_lookup_hash(cell table, cell klass, cell hashcode) {
19 array* buckets = untag<array>(table);
20 cell bucket = array_nth(buckets, hashcode & (array_capacity(buckets) - 1));
21 if (TAG(bucket) == ARRAY_TYPE)
22 return search_lookup_alist(bucket, klass);
27 cell factor_vm::nth_superclass(tuple_layout* layout, fixnum echelon) {
28 cell* ptr = (cell*)(layout + 1);
29 return ptr[echelon * 2];
32 cell factor_vm::nth_hashcode(tuple_layout* layout, fixnum echelon) {
33 cell* ptr = (cell*)(layout + 1);
34 return ptr[echelon * 2 + 1];
37 cell factor_vm::lookup_tuple_method(cell obj, cell methods) {
38 tuple_layout* layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
40 array* echelons = untag<array>(methods);
42 fixnum echelon = std::min(untag_fixnum(layout->echelon),
43 (fixnum)array_capacity(echelons) - 1);
45 while (echelon >= 0) {
46 cell echelon_methods = array_nth(echelons, echelon);
48 if (tagged<object>(echelon_methods).type_p(WORD_TYPE))
49 return echelon_methods;
50 else if (to_boolean(echelon_methods)) {
51 cell klass = nth_superclass(layout, echelon);
52 cell hashcode = untag_fixnum(nth_hashcode(layout, echelon));
53 cell result = search_lookup_hash(echelon_methods, klass, hashcode);
54 if (to_boolean(result))
61 critical_error("Cannot find tuple method", methods);
65 cell factor_vm::lookup_method(cell obj, cell methods) {
67 cell method = array_nth(untag<array>(methods), tag);
69 if (tag == TUPLE_TYPE) {
70 if (TAG(method) == ARRAY_TYPE)
71 return lookup_tuple_method(obj, method);
78 void factor_vm::primitive_lookup_method() {
79 cell methods = ctx->pop();
80 cell obj = ctx->pop();
81 ctx->push(lookup_method(obj, methods));
84 cell factor_vm::object_class(cell obj) {
86 if (tag == TUPLE_TYPE)
87 return untag<tuple>(obj)->layout;
89 return tag_fixnum(tag);
92 cell factor_vm::method_cache_hashcode(cell klass, array* array) {
93 cell capacity = (array_capacity(array) >> 1) - 1;
94 return ((klass >> TAG_BITS) & capacity) << 1;
97 void factor_vm::update_method_cache(cell cache, cell klass, cell method) {
98 array* cache_elements = untag<array>(cache);
99 cell hashcode = method_cache_hashcode(klass, cache_elements);
100 set_array_nth(cache_elements, hashcode, klass);
101 set_array_nth(cache_elements, hashcode + 1, method);
104 void factor_vm::primitive_mega_cache_miss() {
105 dispatch_stats.megamorphic_cache_misses++;
107 cell cache = ctx->pop();
108 fixnum index = untag_fixnum(ctx->pop());
109 cell methods = ctx->pop();
111 cell object = ((cell*)ctx->datastack)[-index];
112 cell klass = object_class(object);
113 cell method = lookup_method(object, methods);
115 update_method_cache(cache, klass, method);
120 void factor_vm::primitive_reset_dispatch_stats() {
121 memset(&dispatch_stats, 0, sizeof(dispatch_statistics));
124 /* Allocates memory */
125 void factor_vm::primitive_dispatch_stats() {
126 ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
129 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index,
131 data_root<array> methods(methods_, parent);
132 data_root<array> cache(cache_, parent);
134 /* The object must be on the top of the datastack at this point. */
136 /* Do a cache lookup. */
137 emit_with_literal(parent->special_objects[MEGA_LOOKUP], cache.value());
139 /* If we end up here, the cache missed. */
140 emit(parent->special_objects[JIT_PROLOG]);
142 /* Push index, method table and cache on the stack. */
143 push(methods.value());
144 push(tag_fixnum(index));
146 word_call(parent->special_objects[MEGA_MISS_WORD]);
148 /* Now the new method has been stored into the cache, and its on
150 emit(parent->special_objects[JIT_EPILOG]);
151 emit(parent->special_objects[JIT_EXECUTE]);