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;
22 cell old_type = old_block->type();
25 /* The call target was either another PIC,
26 or a compiled quotation (megamorphic stub) */
27 assert(old_type == PIC_TYPE || old_type == QUOTATION_TYPE);
30 if(old_type == PIC_TYPE)
31 code->code_heap_free(old_block);
34 /* Figure out what kind of type check the PIC needs based on the methods
36 cell factor_vm::determine_inline_cache_type(array *cache_entries)
38 bool seen_hi_tag = false, seen_tuple = false;
41 for(i = 0; i < array_capacity(cache_entries); i += 2)
43 cell klass = array_nth(cache_entries,i);
45 /* Is it a tuple layout? */
50 fixnum type = untag_fixnum(klass);
51 if(type >= HEADER_TYPE)
59 critical_error("Expected a fixnum or array",klass);
64 if(seen_hi_tag && seen_tuple) return PIC_HI_TAG_TUPLE;
65 if(seen_hi_tag && !seen_tuple) return PIC_HI_TAG;
66 if(!seen_hi_tag && seen_tuple) return PIC_TUPLE;
67 if(!seen_hi_tag && !seen_tuple) return PIC_TAG;
69 critical_error("Oops",0);
73 void factor_vm::update_pic_count(cell type)
75 pic_counts[type - PIC_TAG]++;
78 struct inline_cache_jit : public jit {
81 explicit inline_cache_jit(cell generic_word_,factor_vm *vm) : jit(PIC_TYPE,generic_word_,vm) {};
83 void emit_check(cell klass);
84 void compile_inline_cache(fixnum index,
91 void inline_cache_jit::emit_check(cell klass)
94 if(TAG(klass) == FIXNUM_TYPE && untag_fixnum(klass) < HEADER_TYPE)
95 code_template = parent_vm->userenv[PIC_CHECK_TAG];
97 code_template = parent_vm->userenv[PIC_CHECK];
99 emit_with(code_template,klass);
102 /* index: 0 = top of stack, 1 = item underneath, etc
103 cache_entries: array of class/method pairs */
104 void inline_cache_jit::compile_inline_cache(fixnum index,
110 gc_root<word> generic_word(generic_word_,parent_vm);
111 gc_root<array> methods(methods_,parent_vm);
112 gc_root<array> cache_entries(cache_entries_,parent_vm);
114 cell inline_cache_type = parent_vm->determine_inline_cache_type(cache_entries.untagged());
115 parent_vm->update_pic_count(inline_cache_type);
117 /* Generate machine code to determine the object's class. */
118 emit_class_lookup(index,inline_cache_type);
120 /* Generate machine code to check, in turn, if the class is one of the cached entries. */
122 for(i = 0; i < array_capacity(cache_entries.untagged()); i += 2)
125 cell klass = array_nth(cache_entries.untagged(),i);
128 /* Yes? Jump to method */
129 cell method = array_nth(cache_entries.untagged(),i + 1);
130 emit_with(parent_vm->userenv[PIC_HIT],method);
133 /* Generate machine code to handle a cache miss, which ultimately results in
134 this function being called again.
136 The inline-cache-miss primitive call receives enough information to
137 reconstruct the PIC. */
138 push(generic_word.value());
139 push(methods.value());
140 push(tag_fixnum(index));
141 push(cache_entries.value());
142 word_special(parent_vm->userenv[tail_call_p ? PIC_MISS_TAIL_WORD : PIC_MISS_WORD]);
145 code_block *factor_vm::compile_inline_cache(fixnum index,cell generic_word_,cell methods_,cell cache_entries_,bool tail_call_p)
147 gc_root<word> generic_word(generic_word_,this);
148 gc_root<array> methods(methods_,this);
149 gc_root<array> cache_entries(cache_entries_,this);
151 inline_cache_jit jit(generic_word.value(),this);
152 jit.compile_inline_cache(index,
153 generic_word.value(),
155 cache_entries.value(),
157 code_block *code = jit.to_code_block();
158 relocate_code_block(code);
162 /* A generic word's definition performs general method lookup. Allocates memory */
163 void *factor_vm::megamorphic_call_stub(cell generic_word)
165 return untag<word>(generic_word)->xt;
168 cell factor_vm::inline_cache_size(cell cache_entries)
170 return array_capacity(untag_check<array>(cache_entries)) / 2;
173 /* Allocates memory */
174 cell factor_vm::add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_)
176 gc_root<array> cache_entries(cache_entries_,this);
177 gc_root<object> klass(klass_,this);
178 gc_root<word> method(method_,this);
180 cell pic_size = array_capacity(cache_entries.untagged());
181 gc_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2),this);
182 set_array_nth(new_cache_entries.untagged(),pic_size,klass.value());
183 set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value());
184 return new_cache_entries.value();
187 void factor_vm::update_pic_transitions(cell pic_size)
189 if(pic_size == max_pic_size)
190 pic_to_mega_transitions++;
191 else if(pic_size == 0)
192 cold_call_to_ic_transitions++;
193 else if(pic_size == 1)
194 ic_to_pic_transitions++;
197 /* The cache_entries parameter is either f (on cold call site) or an array (on cache miss).
198 Called from assembly with the actual return address */
199 void *factor_vm::inline_cache_miss(cell return_address)
201 check_code_pointer(return_address);
203 /* Since each PIC is only referenced from a single call site,
204 if the old call target was a PIC, we can deallocate it immediately,
205 instead of leaving dead PICs around until the next GC. */
206 deallocate_inline_cache(return_address);
208 gc_root<array> cache_entries(dpop(),this);
209 fixnum index = untag_fixnum(dpop());
210 gc_root<array> methods(dpop(),this);
211 gc_root<word> generic_word(dpop(),this);
212 gc_root<object> object(((cell *)ds)[-index],this);
216 cell pic_size = inline_cache_size(cache_entries.value());
218 update_pic_transitions(pic_size);
220 if(pic_size >= max_pic_size)
221 xt = megamorphic_call_stub(generic_word.value());
224 cell klass = object_class(object.value());
225 cell method = lookup_method(object.value(),methods.value());
227 gc_root<array> new_cache_entries(add_inline_cache_entry(
228 cache_entries.value(),
231 xt = compile_inline_cache(index,
232 generic_word.value(),
234 new_cache_entries.value(),
235 tail_call_site_p(return_address))->xt();
238 /* Install the new stub. */
239 set_call_target(return_address,xt);
242 printf("Updated %s call site 0x%lx with 0x%lx\n",
243 tail_call_site_p(return_address) ? "tail" : "non-tail",
251 VM_C_API void *inline_cache_miss(cell return_address, factor_vm *myvm)
253 return myvm->inline_cache_miss(return_address);
256 void factor_vm::primitive_reset_inline_cache_stats()
258 cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0;
260 for(i = 0; i < 4; i++) pic_counts[i] = 0;
263 void factor_vm::primitive_inline_cache_stats()
265 growable_array stats(this);
266 stats.add(allot_cell(cold_call_to_ic_transitions));
267 stats.add(allot_cell(ic_to_pic_transitions));
268 stats.add(allot_cell(pic_to_mega_transitions));
270 for(i = 0; i < 4; i++)
271 stats.add(allot_cell(pic_counts[i]));
273 dpush(stats.elements.value());