Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 173 lines (139 sloc) 4.14 kB
7274aff Porting VM to C++
Slava Pestov authored
1 #include "master.hpp"
2
7f48eb2 Use C++ namespaces
Slava Pestov authored
3 namespace factor
4 {
5
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
6 cell factor_vm::search_lookup_alist(cell table, cell klass)
7274aff Porting VM to C++
Slava Pestov authored
7 {
0b7eee6 Slightly more space-efficient dispatch table representation
Slava Pestov authored
8 array *elements = untag<array>(table);
9 fixnum index = array_capacity(elements) - 2;
7274aff Porting VM to C++
Slava Pestov authored
10 while(index >= 0)
11 {
0b7eee6 Slightly more space-efficient dispatch table representation
Slava Pestov authored
12 if(array_nth(elements,index) == klass)
13 return array_nth(elements,index + 1);
7274aff Porting VM to C++
Slava Pestov authored
14 else
0b7eee6 Slightly more space-efficient dispatch table representation
Slava Pestov authored
15 index -= 2;
7274aff Porting VM to C++
Slava Pestov authored
16 }
17
f0a1a3c vm: rename F to false_object, and rename T to true_object
Slava Pestov authored
18 return false_object;
7274aff Porting VM to C++
Slava Pestov authored
19 }
20
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
21 cell factor_vm::search_lookup_hash(cell table, cell klass, cell hashcode)
7274aff Porting VM to C++
Slava Pestov authored
22 {
7c0a59f The great type renaming
Slava Pestov authored
23 array *buckets = untag<array>(table);
24 cell bucket = array_nth(buckets,hashcode & (array_capacity(buckets) - 1));
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
25 if(TAG(bucket) == ARRAY_TYPE)
7274aff Porting VM to C++
Slava Pestov authored
26 return search_lookup_alist(bucket,klass);
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
27 else
28 return bucket;
7274aff Porting VM to C++
Slava Pestov authored
29 }
30
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
31 cell factor_vm::nth_superclass(tuple_layout *layout, fixnum echelon)
7274aff Porting VM to C++
Slava Pestov authored
32 {
7c0a59f The great type renaming
Slava Pestov authored
33 cell *ptr = (cell *)(layout + 1);
7274aff Porting VM to C++
Slava Pestov authored
34 return ptr[echelon * 2];
35 }
36
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
37 cell factor_vm::nth_hashcode(tuple_layout *layout, fixnum echelon)
7274aff Porting VM to C++
Slava Pestov authored
38 {
7c0a59f The great type renaming
Slava Pestov authored
39 cell *ptr = (cell *)(layout + 1);
7274aff Porting VM to C++
Slava Pestov authored
40 return ptr[echelon * 2 + 1];
41 }
42
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
43 cell factor_vm::lookup_tuple_method(cell obj, cell methods)
7274aff Porting VM to C++
Slava Pestov authored
44 {
7c0a59f The great type renaming
Slava Pestov authored
45 tuple_layout *layout = untag<tuple_layout>(untag<tuple>(obj)->layout);
7274aff Porting VM to C++
Slava Pestov authored
46
7c0a59f The great type renaming
Slava Pestov authored
47 array *echelons = untag<array>(methods);
7274aff Porting VM to C++
Slava Pestov authored
48
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
49 fixnum echelon = std::min(untag_fixnum(layout->echelon),(fixnum)array_capacity(echelons) - 1);
50
7274aff Porting VM to C++
Slava Pestov authored
51 while(echelon >= 0)
52 {
7c0a59f The great type renaming
Slava Pestov authored
53 cell echelon_methods = array_nth(echelons,echelon);
7274aff Porting VM to C++
Slava Pestov authored
54
7c0a59f The great type renaming
Slava Pestov authored
55 if(tagged<object>(echelon_methods).type_p(WORD_TYPE))
7274aff Porting VM to C++
Slava Pestov authored
56 return echelon_methods;
f0a1a3c vm: rename F to false_object, and rename T to true_object
Slava Pestov authored
57 else if(to_boolean(echelon_methods))
7274aff Porting VM to C++
Slava Pestov authored
58 {
7c0a59f The great type renaming
Slava Pestov authored
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);
f0a1a3c vm: rename F to false_object, and rename T to true_object
Slava Pestov authored
62 if(to_boolean(result))
7274aff Porting VM to C++
Slava Pestov authored
63 return result;
64 }
65
66 echelon--;
67 }
68
69 critical_error("Cannot find tuple method",methods);
f0a1a3c vm: rename F to false_object, and rename T to true_object
Slava Pestov authored
70 return false_object;
7274aff Porting VM to C++
Slava Pestov authored
71 }
72
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
73 cell factor_vm::lookup_method(cell obj, cell methods)
7274aff Porting VM to C++
Slava Pestov authored
74 {
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
75 cell tag = TAG(obj);
76 cell method = array_nth(untag<array>(methods),tag);
77
78 if(tag == TUPLE_TYPE)
7274aff Porting VM to C++
Slava Pestov authored
79 {
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
80 if(TAG(method) == ARRAY_TYPE)
7c0a59f The great type renaming
Slava Pestov authored
81 return lookup_tuple_method(obj,method);
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
82 else
83 return method;
84 }
1857f90 VM: use better abstractions for tagged pointers, eliminate get()/set(…
Slava Pestov authored
85 else
2d0a368 vm: various minor optimizations speeding up gc0 and dispatch1 benchma…
Slava Pestov authored
86 return method;
7274aff Porting VM to C++
Slava Pestov authored
87 }
88
6326652 @phildawes Moved PRIMITIVE and PRIMITIVE_FORWARDs to primitives.[ch]pp
phildawes authored
89 void factor_vm::primitive_lookup_method()
7274aff Porting VM to C++
Slava Pestov authored
90 {
7c0a59f The great type renaming
Slava Pestov authored
91 cell methods = dpop();
92 cell obj = dpop();
93 dpush(lookup_method(obj,methods));
7274aff Porting VM to C++
Slava Pestov authored
94 }
95
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
96 cell factor_vm::object_class(cell obj)
7274aff Porting VM to C++
Slava Pestov authored
97 {
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c…
Slava Pestov authored
98 cell tag = TAG(obj);
99 if(tag == TUPLE_TYPE)
7c0a59f The great type renaming
Slava Pestov authored
100 return untag<tuple>(obj)->layout;
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c…
Slava Pestov authored
101 else
102 return tag_fixnum(tag);
7274aff Porting VM to C++
Slava Pestov authored
103 }
104
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
105 cell factor_vm::method_cache_hashcode(cell klass, array *array)
7274aff Porting VM to C++
Slava Pestov authored
106 {
7c0a59f The great type renaming
Slava Pestov authored
107 cell capacity = (array_capacity(array) >> 1) - 1;
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c…
Slava Pestov authored
108 return ((klass >> TAG_BITS) & capacity) << 1;
7274aff Porting VM to C++
Slava Pestov authored
109 }
110
1c5329c @phildawes renamed factorvm to factor_vm
phildawes authored
111 void factor_vm::update_method_cache(cell cache, cell klass, cell method)
7274aff Porting VM to C++
Slava Pestov authored
112 {
7c0a59f The great type renaming
Slava Pestov authored
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);
7274aff Porting VM to C++
Slava Pestov authored
117 }
118
6326652 @phildawes Moved PRIMITIVE and PRIMITIVE_FORWARDs to primitives.[ch]pp
phildawes authored
119 void factor_vm::primitive_mega_cache_miss()
7274aff Porting VM to C++
Slava Pestov authored
120 {
40a82ab tools.time: overhaul
Slava Pestov authored
121 dispatch_stats.megamorphic_cache_misses++;
7274aff Porting VM to C++
Slava Pestov authored
122
7c0a59f The great type renaming
Slava Pestov authored
123 cell cache = dpop();
124 fixnum index = untag_fixnum(dpop());
125 cell methods = dpop();
7274aff Porting VM to C++
Slava Pestov authored
126
7c0a59f The great type renaming
Slava Pestov authored
127 cell object = ((cell *)ds)[-index];
128 cell klass = object_class(object);
129 cell method = lookup_method(object,methods);
7274aff Porting VM to C++
Slava Pestov authored
130
131 update_method_cache(cache,klass,method);
132
133 dpush(method);
134 }
135
6326652 @phildawes Moved PRIMITIVE and PRIMITIVE_FORWARDs to primitives.[ch]pp
phildawes authored
136 void factor_vm::primitive_reset_dispatch_stats()
7274aff Porting VM to C++
Slava Pestov authored
137 {
40a82ab tools.time: overhaul
Slava Pestov authored
138 memset(&dispatch_stats,0,sizeof(dispatch_statistics));
7274aff Porting VM to C++
Slava Pestov authored
139 }
140
6326652 @phildawes Moved PRIMITIVE and PRIMITIVE_FORWARDs to primitives.[ch]pp
phildawes authored
141 void factor_vm::primitive_dispatch_stats()
7274aff Porting VM to C++
Slava Pestov authored
142 {
40a82ab tools.time: overhaul
Slava Pestov authored
143 dpush(tag<byte_array>(byte_array_from_value(&dispatch_stats)));
7274aff Porting VM to C++
Slava Pestov authored
144 }
145
7c0a59f The great type renaming
Slava Pestov authored
146 void quotation_jit::emit_mega_cache_lookup(cell methods_, fixnum index, cell cache_)
7274aff Porting VM to C++
Slava Pestov authored
147 {
b341527 vm: rename gc_root to data_root, add code_root to fix a problem where…
Slava Pestov authored
148 data_root<array> methods(methods_,parent);
149 data_root<array> cache(cache_,parent);
7274aff Porting VM to C++
Slava Pestov authored
150
151 /* Generate machine code to determine the object's class. */
d65296b vm: 4 bit tags, new representation of alien objects makes unbox-any-c…
Slava Pestov authored
152 emit_class_lookup(index,PIC_TUPLE);
7274aff Porting VM to C++
Slava Pestov authored
153
154 /* Do a cache lookup. */
912b126 Split literal table into literal and parameter tables, literal table …
Slava Pestov authored
155 emit_with_literal(parent->special_objects[MEGA_LOOKUP],cache.value());
7274aff Porting VM to C++
Slava Pestov authored
156
157 /* If we end up here, the cache missed. */
6c561b5 vm: rename userenv to special_objects
Slava Pestov authored
158 emit(parent->special_objects[JIT_PROLOG]);
7274aff Porting VM to C++
Slava Pestov authored
159
160 /* Push index, method table and cache on the stack. */
d44957b Cleaning up VM code
Slava Pestov authored
161 push(methods.value());
162 push(tag_fixnum(index));
163 push(cache.value());
6c561b5 vm: rename userenv to special_objects
Slava Pestov authored
164 word_call(parent->special_objects[MEGA_MISS_WORD]);
7274aff Porting VM to C++
Slava Pestov authored
165
166 /* Now the new method has been stored into the cache, and its on
167 the stack. */
6c561b5 vm: rename userenv to special_objects
Slava Pestov authored
168 emit(parent->special_objects[JIT_EPILOG]);
31a260b vm: big overhaul of non-optimizing compiler
Slava Pestov authored
169 emit(parent->special_objects[JIT_EXECUTE]);
7274aff Porting VM to C++
Slava Pestov authored
170 }
7f48eb2 Use C++ namespaces
Slava Pestov authored
171
172 }
Something went wrong with that request. Please try again.