]> gitweb.factorcode.org Git - factor.git/blob - vm/dispatch.cpp
Merge branch 'master' into new_gc
[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_hi_tag_method(cell obj, cell methods)
74 {
75         array *hi_tag_methods = untag<array>(methods);
76         cell tag = untag<object>(obj)->h.hi_tag() - HEADER_TYPE;
77 #ifdef FACTOR_DEBUG
78         assert(tag < TYPE_COUNT - HEADER_TYPE);
79 #endif
80         return array_nth(hi_tag_methods,tag);
81 }
82
83 cell factor_vm::lookup_method(cell obj, cell methods)
84 {
85         cell tag = TAG(obj);
86         cell method = array_nth(untag<array>(methods),tag);
87
88         if(tag == TUPLE_TYPE)
89         {
90                 if(TAG(method) == ARRAY_TYPE)
91                         return lookup_tuple_method(obj,method);
92                 else
93                         return method;
94         }
95         else if(tag == OBJECT_TYPE)
96         {
97                 if(TAG(method) == ARRAY_TYPE)
98                         return lookup_hi_tag_method(obj,method);
99                 else
100                         return method;
101         }
102         else
103                 return method;
104 }
105
106 void factor_vm::primitive_lookup_method()
107 {
108         cell methods = dpop();
109         cell obj = dpop();
110         dpush(lookup_method(obj,methods));
111 }
112
113 cell factor_vm::object_class(cell obj)
114 {
115         switch(TAG(obj))
116         {
117         case TUPLE_TYPE:
118                 return untag<tuple>(obj)->layout;
119         case OBJECT_TYPE:
120                 return untag<object>(obj)->h.value;
121         default:
122                 return tag_fixnum(TAG(obj));
123         }
124 }
125
126 cell factor_vm::method_cache_hashcode(cell klass, array *array)
127 {
128         cell capacity = (array_capacity(array) >> 1) - 1;
129         return ((klass >> TAG_BITS) & capacity) << 1;
130 }
131
132 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
133 {
134         array *cache_elements = untag<array>(cache);
135         cell hashcode = method_cache_hashcode(klass,cache_elements);
136         set_array_nth(cache_elements,hashcode,klass);
137         set_array_nth(cache_elements,hashcode + 1,method);
138 }
139
140 void factor_vm::primitive_mega_cache_miss()
141 {
142         megamorphic_cache_misses++;
143
144         cell cache = dpop();
145         fixnum index = untag_fixnum(dpop());
146         cell methods = dpop();
147
148         cell object = ((cell *)ds)[-index];
149         cell klass = object_class(object);
150         cell method = lookup_method(object,methods);
151
152         update_method_cache(cache,klass,method);
153
154         dpush(method);
155 }
156
157 void factor_vm::primitive_reset_dispatch_stats()
158 {
159         megamorphic_cache_hits = megamorphic_cache_misses = 0;
160 }
161
162 void factor_vm::primitive_dispatch_stats()
163 {
164         growable_array stats(this);
165         stats.add(allot_cell(megamorphic_cache_hits));
166         stats.add(allot_cell(megamorphic_cache_misses));
167         stats.trim();
168         dpush(stats.elements.value());
169 }
170
171 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
172 {
173         gc_root<array> methods(methods_,parent);
174         gc_root<array> cache(cache_,parent);
175
176         /* Generate machine code to determine the object's class. */
177         emit_class_lookup(index,PIC_HI_TAG_TUPLE);
178
179         /* Do a cache lookup. */
180         emit_with(parent->special_objects[MEGA_LOOKUP],cache.value());
181         
182         /* If we end up here, the cache missed. */
183         emit(parent->special_objects[JIT_PROLOG]);
184
185         /* Push index, method table and cache on the stack. */
186         push(methods.value());
187         push(tag_fixnum(index));
188         push(cache.value());
189         word_call(parent->special_objects[MEGA_MISS_WORD]);
190
191         /* Now the new method has been stored into the cache, and its on
192            the stack. */
193         emit(parent->special_objects[JIT_EPILOG]);
194         emit(parent->special_objects[JIT_EXECUTE_JUMP]);
195 }
196
197 }