5 static cell search_lookup_alist(cell table, cell klass) {
6 array* elements = untag<array>(table);
7 for (fixnum index = array_capacity(elements) - 2; index >= 0; index -= 2) {
8 if (array_nth(elements, index) == klass)
9 return array_nth(elements, index + 1);
14 static cell search_lookup_hash(cell table, cell klass, cell hashcode) {
15 array* buckets = untag<array>(table);
16 cell bucket = array_nth(buckets, hashcode & (array_capacity(buckets) - 1));
17 if (TAG(bucket) == ARRAY_TYPE)
18 return search_lookup_alist(bucket, klass);
22 static cell nth_superclass(tuple_layout* layout, fixnum echelon) {
23 cell* ptr = (cell*)(layout + 1);
24 return ptr[echelon * 2];
27 static cell nth_hashcode(tuple_layout* layout, fixnum echelon) {
28 cell* ptr = (cell*)(layout + 1);
29 return ptr[echelon * 2 + 1];
32 cell factor_vm::lookup_tuple_method(cell obj, cell methods) {
33 tuple_layout* layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
35 array* echelons = untag<array>(methods);
37 fixnum echelon = std::min(untag_fixnum(layout->echelon),
38 (fixnum)array_capacity(echelons) - 1);
40 while (echelon >= 0) {
41 cell echelon_methods = array_nth(echelons, echelon);
43 if (TAG(echelon_methods) == WORD_TYPE)
44 return echelon_methods;
45 else if (to_boolean(echelon_methods)) {
46 cell klass = nth_superclass(layout, echelon);
47 cell hashcode = untag_fixnum(nth_hashcode(layout, echelon));
48 cell result = search_lookup_hash(echelon_methods, klass, hashcode);
49 if (to_boolean(result))
56 critical_error("Cannot find tuple method", methods);
60 cell factor_vm::lookup_method(cell obj, cell methods) {
62 cell method = array_nth(untag<array>(methods), tag);
64 if (tag == TUPLE_TYPE) {
65 if (TAG(method) == ARRAY_TYPE)
66 return lookup_tuple_method(obj, method);
72 void factor_vm::primitive_lookup_method() {
73 cell methods = ctx->pop();
74 cell obj = ctx->pop();
75 ctx->push(lookup_method(obj, methods));
78 cell factor_vm::object_class(cell obj) {
80 if (tag == TUPLE_TYPE)
81 return untag<tuple>(obj)->layout;
82 return tag_fixnum(tag);
85 static cell method_cache_hashcode(cell klass, array* array) {
86 cell capacity = (array_capacity(array) >> 1) - 1;
87 return ((klass >> TAG_BITS) & capacity) << 1;
90 void factor_vm::update_method_cache(cell cache, cell klass, cell method) {
91 array* cache_elements = untag<array>(cache);
92 cell hashcode = method_cache_hashcode(klass, cache_elements);
93 set_array_nth(cache_elements, hashcode, klass);
94 set_array_nth(cache_elements, hashcode + 1, method);
97 void factor_vm::primitive_mega_cache_miss() {
98 dispatch_stats.megamorphic_cache_misses++;
100 cell cache = ctx->pop();
101 fixnum index = untag_fixnum(ctx->pop());
102 cell methods = ctx->pop();
104 cell object = ((cell*)ctx->datastack)[-index];
105 cell klass = object_class(object);
106 cell method = lookup_method(object, methods);
108 update_method_cache(cache, klass, method);
113 void factor_vm::primitive_reset_dispatch_stats() {
114 memset(&dispatch_stats, 0, sizeof(dispatch_statistics));
118 void factor_vm::primitive_dispatch_stats() {
119 ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats)));