]> gitweb.factorcode.org Git - factor.git/blob - vm/dispatch.cpp
Merge branch 'work' of git://github.com/carlo-kokoth/factor
[factor.git] / vm / dispatch.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 cell factor_vm::search_lookup_alist(cell table, cell klass)
7 {
8         array *elements = untag<array>(table);
9         fixnum index = array_capacity(elements) - 2;
10         while(index >= 0)
11         {
12                 if(array_nth(elements,index) == klass)
13                         return array_nth(elements,index + 1);
14                 else
15                         index -= 2;
16         }
17
18         return false_object;
19 }
20
21 cell factor_vm::search_lookup_hash(cell table, cell klass, cell hashcode)
22 {
23         array *buckets = untag<array>(table);
24         cell bucket = array_nth(buckets,hashcode & (array_capacity(buckets) - 1));
25         if(TAG(bucket) == ARRAY_TYPE)
26                 return search_lookup_alist(bucket,klass);
27         else
28                 return bucket;
29 }
30
31 cell factor_vm::nth_superclass(tuple_layout *layout, fixnum echelon)
32 {
33         cell *ptr = (cell *)(layout + 1);
34         return ptr[echelon * 2];
35 }
36
37 cell factor_vm::nth_hashcode(tuple_layout *layout, fixnum echelon)
38 {
39         cell *ptr = (cell *)(layout + 1);
40         return ptr[echelon * 2 + 1];
41 }
42
43 cell factor_vm::lookup_tuple_method(cell obj, cell methods)
44 {
45         tuple_layout *layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
46
47         array *echelons = untag<array>(methods);
48
49         fixnum echelon = std::min(untag_fixnum(layout->echelon),(fixnum)array_capacity(echelons) - 1);
50
51         while(echelon >= 0)
52         {
53                 cell echelon_methods = array_nth(echelons,echelon);
54
55                 if(tagged<object>(echelon_methods).type_p(WORD_TYPE))
56                         return echelon_methods;
57                 else if(to_boolean(echelon_methods))
58                 {
59                         cell klass = nth_superclass(layout,echelon);
60                         cell hashcode = untag_fixnum(nth_hashcode(layout,echelon));
61                         cell result = search_lookup_hash(echelon_methods,klass,hashcode);
62                         if(to_boolean(result))
63                                 return result;
64                 }
65
66                 echelon--;
67         }
68
69         critical_error("Cannot find tuple method",methods);
70         return false_object;
71 }
72
73 cell factor_vm::lookup_method(cell obj, cell methods)
74 {
75         cell tag = TAG(obj);
76         cell method = array_nth(untag<array>(methods),tag);
77
78         if(tag == TUPLE_TYPE)
79         {
80                 if(TAG(method) == ARRAY_TYPE)
81                         return lookup_tuple_method(obj,method);
82                 else
83                         return method;
84         }
85         else
86                 return method;
87 }
88
89 void factor_vm::primitive_lookup_method()
90 {
91         cell methods = dpop();
92         cell obj = dpop();
93         dpush(lookup_method(obj,methods));
94 }
95
96 cell factor_vm::object_class(cell obj)
97 {
98         cell tag = TAG(obj);
99         if(tag == TUPLE_TYPE)
100                 return untag<tuple>(obj)->layout;
101         else
102                 return tag_fixnum(tag);
103 }
104
105 cell factor_vm::method_cache_hashcode(cell klass, array *array)
106 {
107         cell capacity = (array_capacity(array) >> 1) - 1;
108         return ((klass >> TAG_BITS) & capacity) << 1;
109 }
110
111 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
112 {
113         array *cache_elements = untag<array>(cache);
114         cell hashcode = method_cache_hashcode(klass,cache_elements);
115         set_array_nth(cache_elements,hashcode,klass);
116         set_array_nth(cache_elements,hashcode + 1,method);
117 }
118
119 void factor_vm::primitive_mega_cache_miss()
120 {
121         dispatch_stats.megamorphic_cache_misses++;
122
123         cell cache = dpop();
124         fixnum index = untag_fixnum(dpop());
125         cell methods = dpop();
126
127         cell object = ((cell *)ds)[-index];
128         cell klass = object_class(object);
129         cell method = lookup_method(object,methods);
130
131         update_method_cache(cache,klass,method);
132
133         dpush(method);
134 }
135
136 void factor_vm::primitive_reset_dispatch_stats()
137 {
138         memset(&dispatch_stats,0,sizeof(dispatch_statistics));
139 }
140
141 void factor_vm::primitive_dispatch_stats()
142 {
143         dpush(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
144 }
145
146 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
147 {
148         data_root<array> methods(methods_,parent);
149         data_root<array> cache(cache_,parent);
150
151         /* Generate machine code to determine the object's class. */
152         emit_class_lookup(index,PIC_TUPLE);
153
154         /* Do a cache lookup. */
155         emit_with(parent->special_objects[MEGA_LOOKUP],cache.value());
156         
157         /* If we end up here, the cache missed. */
158         emit(parent->special_objects[JIT_PROLOG]);
159
160         /* Push index, method table and cache on the stack. */
161         push(methods.value());
162         push(tag_fixnum(index));
163         push(cache.value());
164         word_call(parent->special_objects[MEGA_MISS_WORD]);
165
166         /* Now the new method has been stored into the cache, and its on
167            the stack. */
168         emit(parent->special_objects[JIT_EPILOG]);
169         emit(parent->special_objects[JIT_EXECUTE_JUMP]);
170 }
171
172 }