]> gitweb.factorcode.org Git - factor.git/blob - vm/dispatch.cpp
VM: fixes to use the TAG macro over tagged<object>(obj).type()
[factor.git] / vm / dispatch.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 cell factor_vm::search_lookup_alist(cell table, cell klass) {
6   array* elements = untag<array>(table);
7   fixnum index = array_capacity(elements) - 2;
8   while (index >= 0) {
9     if (array_nth(elements, index) == klass)
10       return array_nth(elements, index + 1);
11     else
12       index -= 2;
13   }
14
15   return false_object;
16 }
17
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);
23   else
24     return bucket;
25 }
26
27 cell factor_vm::nth_superclass(tuple_layout* layout, fixnum echelon) {
28   cell* ptr = (cell*)(layout + 1);
29   return ptr[echelon * 2];
30 }
31
32 cell factor_vm::nth_hashcode(tuple_layout* layout, fixnum echelon) {
33   cell* ptr = (cell*)(layout + 1);
34   return ptr[echelon * 2 + 1];
35 }
36
37 cell factor_vm::lookup_tuple_method(cell obj, cell methods) {
38   tuple_layout* layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
39
40   array* echelons = untag<array>(methods);
41
42   fixnum echelon = std::min(untag_fixnum(layout->echelon),
43                             (fixnum)array_capacity(echelons) - 1);
44
45   while (echelon >= 0) {
46     cell echelon_methods = array_nth(echelons, echelon);
47
48     if (TAG(echelon_methods) == 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))
55         return result;
56     }
57
58     echelon--;
59   }
60
61   critical_error("Cannot find tuple method", methods);
62   return false_object;
63 }
64
65 cell factor_vm::lookup_method(cell obj, cell methods) {
66   cell tag = TAG(obj);
67   cell method = array_nth(untag<array>(methods), tag);
68
69   if (tag == TUPLE_TYPE) {
70     if (TAG(method) == ARRAY_TYPE)
71       return lookup_tuple_method(obj, method);
72     else
73       return method;
74   } else
75     return method;
76 }
77
78 void factor_vm::primitive_lookup_method() {
79   cell methods = ctx->pop();
80   cell obj = ctx->pop();
81   ctx->push(lookup_method(obj, methods));
82 }
83
84 cell factor_vm::object_class(cell obj) {
85   cell tag = TAG(obj);
86   if (tag == TUPLE_TYPE)
87     return untag<tuple>(obj)->layout;
88   else
89     return tag_fixnum(tag);
90 }
91
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;
95 }
96
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);
102 }
103
104 void factor_vm::primitive_mega_cache_miss() {
105   dispatch_stats.megamorphic_cache_misses++;
106
107   cell cache = ctx->pop();
108   fixnum index = untag_fixnum(ctx->pop());
109   cell methods = ctx->pop();
110
111   cell object = ((cell*)ctx->datastack)[-index];
112   cell klass = object_class(object);
113   cell method = lookup_method(object, methods);
114
115   update_method_cache(cache, klass, method);
116
117   ctx->push(method);
118 }
119
120 void factor_vm::primitive_reset_dispatch_stats() {
121   memset(&dispatch_stats, 0, sizeof(dispatch_statistics));
122 }
123
124 /* Allocates memory */
125 void factor_vm::primitive_dispatch_stats() {
126   ctx->push(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
127 }
128
129 /* Allocates memory */
130 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index,
131                                            cell cache_) {
132   data_root<array> methods(methods_, parent);
133   data_root<array> cache(cache_, parent);
134
135   /* The object must be on the top of the datastack at this point. */
136
137   /* Do a cache lookup. */
138   emit_with_literal(parent->special_objects[MEGA_LOOKUP], cache.value());
139
140   /* If we end up here, the cache missed. */
141   emit(parent->special_objects[JIT_PROLOG]);
142
143   /* Push index, method table and cache on the stack. */
144   push(methods.value());
145   push(tag_fixnum(index));
146   push(cache.value());
147   word_call(parent->special_objects[MEGA_MISS_WORD]);
148
149   /* Now the new method has been stored into the cache, and its on
150      the stack. */
151   emit(parent->special_objects[JIT_EPILOG]);
152   emit(parent->special_objects[JIT_EXECUTE]);
153 }
154
155 }