]> gitweb.factorcode.org Git - factor.git/blob - vm/dispatch.cpp
scryfall: better moxfield words
[factor.git] / vm / dispatch.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
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);
10   }
11   return false_object;
12 }
13
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);
19   return bucket;
20 }
21
22 static cell nth_superclass(tuple_layout* layout, fixnum echelon) {
23   cell* ptr = (cell*)(layout + 1);
24   return ptr[echelon * 2];
25 }
26
27 static cell nth_hashcode(tuple_layout* layout, fixnum echelon) {
28   cell* ptr = (cell*)(layout + 1);
29   return ptr[echelon * 2 + 1];
30 }
31
32 cell factor_vm::lookup_tuple_method(cell obj, cell methods) {
33   tuple_layout* layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
34
35   array* echelons = untag<array>(methods);
36
37   fixnum echelon = std::min(untag_fixnum(layout->echelon),
38                             (fixnum)array_capacity(echelons) - 1);
39
40   while (echelon >= 0) {
41     cell echelon_methods = array_nth(echelons, echelon);
42
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))
50         return result;
51     }
52
53     echelon--;
54   }
55
56   critical_error("Cannot find tuple method", methods);
57   return false_object;
58 }
59
60 cell factor_vm::lookup_method(cell obj, cell methods) {
61   cell tag = TAG(obj);
62   cell method = array_nth(untag<array>(methods), tag);
63
64   if (tag == TUPLE_TYPE) {
65     if (TAG(method) == ARRAY_TYPE)
66       return lookup_tuple_method(obj, method);
67     return method;
68   }
69   return method;
70 }
71
72 void factor_vm::primitive_lookup_method() {
73   cell methods = ctx->pop();
74   cell obj = ctx->pop();
75   ctx->push(lookup_method(obj, methods));
76 }
77
78 cell factor_vm::object_class(cell obj) {
79   cell tag = TAG(obj);
80   if (tag == TUPLE_TYPE)
81     return untag<tuple>(obj)->layout;
82   return tag_fixnum(tag);
83 }
84
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;
88 }
89
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);
95 }
96
97 void factor_vm::primitive_mega_cache_miss() {
98   dispatch_stats.megamorphic_cache_misses++;
99
100   cell cache = ctx->pop();
101   fixnum index = untag_fixnum(ctx->pop());
102   cell methods = ctx->pop();
103
104   cell object = ((cell*)ctx->datastack)[-index];
105   cell klass = object_class(object);
106   cell method = lookup_method(object, methods);
107
108   update_method_cache(cache, klass, method);
109
110   ctx->push(method);
111 }
112
113 void factor_vm::primitive_reset_dispatch_stats() {
114   memset(&dispatch_stats, 0, sizeof(dispatch_statistics));
115 }
116
117 // Allocates memory
118 void factor_vm::primitive_dispatch_stats() {
119   ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
120 }
121
122 }