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;
12 for(int i = 0; i < 4; i++) pic_counts[i] = 0;
15 void factor_vm::deallocate_inline_cache(cell return_address)
17 /* Find the call target. */
18 void *old_xt = get_call_target(return_address);
19 check_code_pointer((cell)old_xt);
21 code_block *old_block = (code_block *)old_xt - 1;
23 /* Free the old PIC since we know its unreachable */
24 if(old_block->pic_p())
25 code->code_heap_free(old_block);
28 /* Figure out what kind of type check the PIC needs based on the methods
30 cell factor_vm::determine_inline_cache_type(array *cache_entries)
32 bool seen_hi_tag = false, seen_tuple = false;
35 for(i = 0; i < array_capacity(cache_entries); i += 2)
37 cell klass = array_nth(cache_entries,i);
39 /* Is it a tuple layout? */
44 fixnum type = untag_fixnum(klass);
45 if(type >= HEADER_TYPE)
53 critical_error("Expected a fixnum or array",klass);
58 if(seen_hi_tag && seen_tuple) return PIC_HI_TAG_TUPLE;
59 if(seen_hi_tag && !seen_tuple) return PIC_HI_TAG;
60 if(!seen_hi_tag && seen_tuple) return PIC_TUPLE;
61 if(!seen_hi_tag && !seen_tuple) return PIC_TAG;
63 critical_error("Oops",0);
67 void factor_vm::update_pic_count(cell type)
69 pic_counts[type - PIC_TAG]++;
72 struct inline_cache_jit : public jit {
75 explicit inline_cache_jit(cell generic_word_,factor_vm *vm) : jit(code_block_pic,generic_word_,vm) {};
77 void emit_check(cell klass);
78 void compile_inline_cache(fixnum index,
85 void inline_cache_jit::emit_check(cell klass)
88 if(TAG(klass) == FIXNUM_TYPE && untag_fixnum(klass) < HEADER_TYPE)
89 code_template = parent->special_objects[PIC_CHECK_TAG];
91 code_template = parent->special_objects[PIC_CHECK];
93 emit_with(code_template,klass);
96 /* index: 0 = top of stack, 1 = item underneath, etc
97 cache_entries: array of class/method pairs */
98 void inline_cache_jit::compile_inline_cache(fixnum index,
104 gc_root<word> generic_word(generic_word_,parent);
105 gc_root<array> methods(methods_,parent);
106 gc_root<array> cache_entries(cache_entries_,parent);
108 cell inline_cache_type = parent->determine_inline_cache_type(cache_entries.untagged());
109 parent->update_pic_count(inline_cache_type);
111 /* Generate machine code to determine the object's class. */
112 emit_class_lookup(index,inline_cache_type);
114 /* Generate machine code to check, in turn, if the class is one of the cached entries. */
116 for(i = 0; i < array_capacity(cache_entries.untagged()); i += 2)
119 cell klass = array_nth(cache_entries.untagged(),i);
122 /* Yes? Jump to method */
123 cell method = array_nth(cache_entries.untagged(),i + 1);
124 emit_with(parent->special_objects[PIC_HIT],method);
127 /* Generate machine code to handle a cache miss, which ultimately results in
128 this function being called again.
130 The inline-cache-miss primitive call receives enough information to
131 reconstruct the PIC. */
132 push(generic_word.value());
133 push(methods.value());
134 push(tag_fixnum(index));
135 push(cache_entries.value());
136 word_special(parent->special_objects[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]);
139 code_block *factor_vm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p)
141 gc_root<word> generic_word(generic_word_,this);
142 gc_root<array> methods(methods_,this);
143 gc_root<array> cache_entries(cache_entries_,this);
145 inline_cache_jit jit(generic_word.value(),this);
146 jit.compile_inline_cache(index,
147 generic_word.value(),
149 cache_entries.value(),
151 code_block *code = jit.to_code_block();
152 relocate_code_block(code);
156 /* A generic word's definition performs general method lookup. Allocates memory */
157 void *factor_vm::megamorphic_call_stub(cell generic_word)
159 return untag<word>(generic_word)->xt;
162 cell factor_vm::inline_cache_size(cell cache_entries)
164 return array_capacity(untag_check<array>(cache_entries)) / 2;
167 /* Allocates memory */
168 cell factor_vm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_)
170 gc_root<array> cache_entries(cache_entries_,this);
171 gc_root<object> klass(klass_,this);
172 gc_root<word> method(method_,this);
174 cell pic_size = array_capacity(cache_entries.untagged());
175 gc_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2),this);
176 set_array_nth(new_cache_entries.untagged(),pic_size,klass.value());
177 set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value());
178 return new_cache_entries.value();
181 void factor_vm::update_pic_transitions(cell pic_size)
183 if(pic_size == max_pic_size)
184 pic_to_mega_transitions++;
185 else if(pic_size == 0)
186 cold_call_to_ic_transitions++;
187 else if(pic_size == 1)
188 ic_to_pic_transitions++;
191 /* The cache_entries parameter is either f (on cold call site) or an array (on cache miss).
192 Called from assembly with the actual return address */
193 void *factor_vm::inline_cache_miss(cell return_address)
195 check_code_pointer(return_address);
197 /* Since each PIC is only referenced from a single call site,
198 if the old call target was a PIC, we can deallocate it immediately,
199 instead of leaving dead PICs around until the next GC. */
200 deallocate_inline_cache(return_address);
202 gc_root<array> cache_entries(dpop(),this);
203 fixnum index = untag_fixnum(dpop());
204 gc_root<array> methods(dpop(),this);
205 gc_root<word> generic_word(dpop(),this);
206 gc_root<object> object(((cell *)ds)[-index],this);
210 cell pic_size = inline_cache_size(cache_entries.value());
212 update_pic_transitions(pic_size);
214 if(pic_size >= max_pic_size)
215 xt = megamorphic_call_stub(generic_word.value());
218 cell klass = object_class(object.value());
219 cell method = lookup_method(object.value(),methods.value());
221 gc_root<array> new_cache_entries(add_inline_cache_entry(
222 cache_entries.value(),
225 xt = compile_inline_cache(index,
226 generic_word.value(),
228 new_cache_entries.value(),
229 tail_call_site_p(return_address))->xt();
232 /* Install the new stub. */
233 set_call_target(return_address,xt);
236 std::cout << "Updated "
237 << (tail_call_site_p(return_address) ? "tail" : "non-tail")
238 << " call site 0x" << std::hex << return_address << std::dec
239 << " with " << std::hex << (cell)xt << std::dec;
245 VM_C_API void *inline_cache_miss(cell return_address, factor_vm *parent)
247 return parent->inline_cache_miss(return_address);
250 void factor_vm::primitive_reset_inline_cache_stats()
252 cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0;
254 for(i = 0; i < 4; i++) pic_counts[i] = 0;
257 void factor_vm::primitive_inline_cache_stats()
259 growable_array stats(this);
260 stats.add(allot_cell(cold_call_to_ic_transitions));
261 stats.add(allot_cell(ic_to_pic_transitions));
262 stats.add(allot_cell(pic_to_mega_transitions));
264 for(i = 0; i < 4; i++)
265 stats.add(allot_cell(pic_counts[i]));
267 dpush(stats.elements.value());