6 void factor_vm::init_inline_caching(int max_size)
8 max_pic_size = max_size;
9 cold_call_to_ic_transitions = 0;
10 ic_to_pic_transitions = 0;
11 pic_to_mega_transitions = 0;
16 void factor_vm::deallocate_inline_cache(cell return_address)
18 /* Find the call target. */
19 void *old_xt = get_call_target(return_address);
20 check_code_pointer((cell)old_xt);
22 code_block *old_block = (code_block *)old_xt - 1;
24 /* Free the old PIC since we know its unreachable */
25 if(old_block->pic_p())
26 code->code_heap_free(old_block);
29 /* Figure out what kind of type check the PIC needs based on the methods
31 cell factor_vm::determine_inline_cache_type(array *cache_entries)
33 bool seen_tuple = false;
36 for(i = 0; i < array_capacity(cache_entries); i += 2)
38 /* Is it a tuple layout? */
39 if(TAG(array_nth(cache_entries,i)) == ARRAY_TYPE)
46 return seen_tuple ? PIC_TUPLE : PIC_TAG;
49 void factor_vm::update_pic_count(cell type)
51 pic_counts[type - PIC_TAG]++;
54 struct inline_cache_jit : public jit {
57 explicit inline_cache_jit(cell generic_word_,factor_vm *vm) : jit(code_block_pic,generic_word_,vm) {};
59 void emit_check(cell klass);
60 void compile_inline_cache(fixnum index,
67 void inline_cache_jit::emit_check(cell klass)
70 if(TAG(klass) == FIXNUM_TYPE)
71 code_template = parent->special_objects[PIC_CHECK_TAG];
73 code_template = parent->special_objects[PIC_CHECK_TUPLE];
75 emit_with(code_template,klass);
78 /* index: 0 = top of stack, 1 = item underneath, etc
79 cache_entries: array of class/method pairs */
80 void inline_cache_jit::compile_inline_cache(fixnum index,
86 data_root<word> generic_word(generic_word_,parent);
87 data_root<array> methods(methods_,parent);
88 data_root<array> cache_entries(cache_entries_,parent);
90 cell inline_cache_type = parent->determine_inline_cache_type(cache_entries.untagged());
91 parent->update_pic_count(inline_cache_type);
93 /* Generate machine code to determine the object's class. */
94 emit_class_lookup(index,inline_cache_type);
96 /* Generate machine code to check, in turn, if the class is one of the cached entries. */
98 for(i = 0; i < array_capacity(cache_entries.untagged()); i += 2)
101 cell klass = array_nth(cache_entries.untagged(),i);
104 /* Yes? Jump to method */
105 cell method = array_nth(cache_entries.untagged(),i + 1);
106 emit_with(parent->special_objects[PIC_HIT],method);
109 /* Generate machine code to handle a cache miss, which ultimately results in
110 this function being called again.
112 The inline-cache-miss primitive call receives enough information to
113 reconstruct the PIC. */
114 push(generic_word.value());
115 push(methods.value());
116 push(tag_fixnum(index));
117 push(cache_entries.value());
118 word_special(parent->special_objects[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]);
121 code_block *factor_vm::compile_inline_cache(fixnum index,
127 data_root<word> generic_word(generic_word_,this);
128 data_root<array> methods(methods_,this);
129 data_root<array> cache_entries(cache_entries_,this);
131 inline_cache_jit jit(generic_word.value(),this);
132 jit.compile_inline_cache(index,
133 generic_word.value(),
135 cache_entries.value(),
137 code_block *code = jit.to_code_block();
138 relocate_code_block(code);
142 /* A generic word's definition performs general method lookup. Allocates memory */
143 void *factor_vm::megamorphic_call_stub(cell generic_word)
145 return untag<word>(generic_word)->xt;
148 cell factor_vm::inline_cache_size(cell cache_entries)
150 return array_capacity(untag_check<array>(cache_entries)) / 2;
153 /* Allocates memory */
154 cell factor_vm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_)
156 data_root<array> cache_entries(cache_entries_,this);
157 data_root<object> klass(klass_,this);
158 data_root<word> method(method_,this);
160 cell pic_size = array_capacity(cache_entries.untagged());
161 data_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2),this);
162 set_array_nth(new_cache_entries.untagged(),pic_size,klass.value());
163 set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value());
164 return new_cache_entries.value();
167 void factor_vm::update_pic_transitions(cell pic_size)
169 if(pic_size == max_pic_size)
170 pic_to_mega_transitions++;
171 else if(pic_size == 0)
172 cold_call_to_ic_transitions++;
173 else if(pic_size == 1)
174 ic_to_pic_transitions++;
177 /* The cache_entries parameter is either f (on cold call site) or an array
178 (on cache miss). Called from assembly with the actual return address.
179 Compilation of the inline cache may trigger a GC, which may trigger a compaction;
180 also, the block containing the return address may now be dead. Use a code_root
181 to take care of the details. */
182 void *factor_vm::inline_cache_miss(cell return_address_)
184 code_root return_address(return_address_,this);
186 check_code_pointer(return_address.value);
188 /* Since each PIC is only referenced from a single call site,
189 if the old call target was a PIC, we can deallocate it immediately,
190 instead of leaving dead PICs around until the next GC. */
191 deallocate_inline_cache(return_address.value);
193 data_root<array> cache_entries(dpop(),this);
194 fixnum index = untag_fixnum(dpop());
195 data_root<array> methods(dpop(),this);
196 data_root<word> generic_word(dpop(),this);
197 data_root<object> object(((cell *)ds)[-index],this);
201 cell pic_size = inline_cache_size(cache_entries.value());
203 update_pic_transitions(pic_size);
205 if(pic_size >= max_pic_size)
206 xt = megamorphic_call_stub(generic_word.value());
209 cell klass = object_class(object.value());
210 cell method = lookup_method(object.value(),methods.value());
212 data_root<array> new_cache_entries(add_inline_cache_entry(
213 cache_entries.value(),
216 xt = compile_inline_cache(index,
217 generic_word.value(),
219 new_cache_entries.value(),
220 tail_call_site_p(return_address.value))->xt();
223 /* Install the new stub. */
224 if(return_address.valid)
226 set_call_target(return_address.value,xt);
229 std::cout << "Updated "
230 << (tail_call_site_p(return_address) ? "tail" : "non-tail")
231 << " call site 0x" << std::hex << return_address << std::dec
232 << " with " << std::hex << (cell)xt << std::dec;
239 VM_C_API void *inline_cache_miss(cell return_address, factor_vm *parent)
241 return parent->inline_cache_miss(return_address);
244 void factor_vm::primitive_reset_inline_cache_stats()
246 cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0;
251 void factor_vm::primitive_inline_cache_stats()
253 growable_array stats(this);
254 stats.add(allot_cell(cold_call_to_ic_transitions));
255 stats.add(allot_cell(ic_to_pic_transitions));
256 stats.add(allot_cell(pic_to_mega_transitions));
257 stats.add(allot_cell(pic_counts[0]));
258 stats.add(allot_cell(pic_counts[1]));
260 dpush(stats.elements.value());