factor/vm/inline_cache.cpp

262 lines
6.9 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
2009-05-04 05:50:24 -04:00
cell max_pic_size;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell cold_call_to_ic_transitions;
cell ic_to_pic_transitions;
cell pic_to_mega_transitions;
2009-05-02 05:04:19 -04:00
/* PIC_TAG, PIC_HI_TAG, PIC_TUPLE, PIC_HI_TAG_TUPLE */
2009-05-04 05:50:24 -04:00
cell pic_counts[4];
2009-05-02 05:04:19 -04:00
void init_inline_caching(int max_size)
{
max_pic_size = max_size;
}
2009-05-04 05:50:24 -04:00
void deallocate_inline_cache(cell return_address)
2009-05-02 05:04:19 -04:00
{
/* Find the call target. */
2009-05-04 05:50:24 -04:00
void *old_xt = get_call_target(return_address);
code_block *old_block = (code_block *)old_xt - 1;
cell old_type = old_block->type;
2009-05-02 05:04:19 -04:00
#ifdef FACTOR_DEBUG
/* The call target was either another PIC,
or a compiled quotation (megamorphic stub) */
assert(old_type == PIC_TYPE || old_type == QUOTATION_TYPE);
#endif
if(old_type == PIC_TYPE)
heap_free(&code,old_block);
2009-05-02 05:04:19 -04:00
}
/* Figure out what kind of type check the PIC needs based on the methods
it contains */
2009-05-04 05:50:24 -04:00
static cell determine_inline_cache_type(array *cache_entries)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
bool seen_hi_tag = false, seen_tuple = false;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 10:19:09 -04:00
for(i = 0; i < array_capacity(cache_entries); i += 2)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell klass = array_nth(cache_entries,i);
2009-05-02 05:04:19 -04:00
/* Is it a tuple layout? */
switch(TAG(klass))
2009-05-02 05:04:19 -04:00
{
case FIXNUM_TYPE:
{
fixnum type = untag_fixnum(klass);
if(type >= HEADER_TYPE)
seen_hi_tag = true;
}
2009-05-02 05:04:19 -04:00
break;
case ARRAY_TYPE:
seen_tuple = true;
break;
default:
critical_error("Expected a fixnum or array",klass);
break;
}
}
if(seen_hi_tag && seen_tuple) return PIC_HI_TAG_TUPLE;
if(seen_hi_tag && !seen_tuple) return PIC_HI_TAG;
if(!seen_hi_tag && seen_tuple) return PIC_TUPLE;
if(!seen_hi_tag && !seen_tuple) return PIC_TAG;
critical_error("Oops",0);
return -1;
}
2009-05-04 05:50:24 -04:00
static void update_pic_count(cell type)
2009-05-02 05:04:19 -04:00
{
pic_counts[type - PIC_TAG]++;
}
2009-05-02 10:19:09 -04:00
struct inline_cache_jit : public jit {
2009-05-04 05:50:24 -04:00
fixnum index;
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
inline_cache_jit(cell generic_word_) : jit(PIC_TYPE,generic_word_) {};
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
void emit_check(cell klass);
void compile_inline_cache(fixnum index, cell generic_word_, cell methods_, cell cache_entries_);
2009-05-02 10:19:09 -04:00
};
2009-05-04 05:50:24 -04:00
void inline_cache_jit::emit_check(cell klass)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell code_template;
if(TAG(klass) == FIXNUM_TYPE && untag_fixnum(klass) < HEADER_TYPE)
2009-05-02 05:04:19 -04:00
code_template = userenv[PIC_CHECK_TAG];
else
code_template = userenv[PIC_CHECK];
2009-05-02 10:19:09 -04:00
emit_with(code_template,klass);
2009-05-02 05:04:19 -04:00
}
/* index: 0 = top of stack, 1 = item underneath, etc
cache_entries: array of class/method pairs */
2009-05-04 05:50:24 -04:00
void inline_cache_jit::compile_inline_cache(fixnum index, cell generic_word_, cell methods_, cell cache_entries_)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
gc_root<word> generic_word(generic_word_);
gc_root<array> methods(methods_);
gc_root<array> cache_entries(cache_entries_);
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell inline_cache_type = determine_inline_cache_type(cache_entries.untagged());
2009-05-02 05:04:19 -04:00
update_pic_count(inline_cache_type);
/* Generate machine code to determine the object's class. */
2009-05-02 10:19:09 -04:00
emit_class_lookup(index,inline_cache_type);
2009-05-02 05:04:19 -04:00
/* Generate machine code to check, in turn, if the class is one of the cached entries. */
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 10:19:09 -04:00
for(i = 0; i < array_capacity(cache_entries.untagged()); i += 2)
2009-05-02 05:04:19 -04:00
{
/* Class equal? */
2009-05-04 05:50:24 -04:00
cell klass = array_nth(cache_entries.untagged(),i);
2009-05-02 10:19:09 -04:00
emit_check(klass);
2009-05-02 05:04:19 -04:00
/* Yes? Jump to method */
2009-05-04 05:50:24 -04:00
cell method = array_nth(cache_entries.untagged(),i + 1);
2009-05-02 10:19:09 -04:00
emit_with(userenv[PIC_HIT],method);
2009-05-02 05:04:19 -04:00
}
/* Generate machine code to handle a cache miss, which ultimately results in
this function being called again.
The inline-cache-miss primitive call receives enough information to
reconstruct the PIC. */
2009-05-02 10:19:09 -04:00
push(generic_word.value());
push(methods.value());
push(tag_fixnum(index));
push(cache_entries.value());
word_jump(userenv[PIC_MISS_WORD]);
}
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
static code_block *compile_inline_cache(fixnum index,
cell generic_word_,
cell methods_,
cell cache_entries_)
2009-05-02 10:19:09 -04:00
{
2009-05-04 05:50:24 -04:00
gc_root<word> generic_word(generic_word_);
gc_root<array> methods(methods_);
gc_root<array> cache_entries(cache_entries_);
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
inline_cache_jit jit(generic_word.value());
jit.compile_inline_cache(index,generic_word.value(),methods.value(),cache_entries.value());
2009-05-04 05:50:24 -04:00
code_block *code = jit.to_code_block();
2009-05-02 10:19:09 -04:00
relocate_code_block(code);
2009-05-02 05:04:19 -04:00
return code;
}
/* A generic word's definition performs general method lookup. Allocates memory */
2009-05-04 05:50:24 -04:00
static void *megamorphic_call_stub(cell generic_word)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
return untag<word>(generic_word)->xt;
2009-05-02 05:04:19 -04:00
}
2009-05-04 05:50:24 -04:00
static cell inline_cache_size(cell cache_entries)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
return array_capacity(untag_check<array>(cache_entries)) / 2;
2009-05-02 05:04:19 -04:00
}
/* Allocates memory */
2009-05-04 05:50:24 -04:00
static cell add_inline_cache_entry(cell cache_entries_, cell klass_, cell method_)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
gc_root<array> cache_entries(cache_entries_);
gc_root<object> klass(klass_);
gc_root<word> method(method_);
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
cell pic_size = array_capacity(cache_entries.untagged());
gc_root<array> new_cache_entries(reallot_array(cache_entries.untagged(),pic_size + 2));
2009-05-02 10:19:09 -04:00
set_array_nth(new_cache_entries.untagged(),pic_size,klass.value());
set_array_nth(new_cache_entries.untagged(),pic_size + 1,method.value());
return new_cache_entries.value();
2009-05-02 05:04:19 -04:00
}
2009-05-04 05:50:24 -04:00
static void update_pic_transitions(cell pic_size)
2009-05-02 05:04:19 -04:00
{
if(pic_size == max_pic_size)
pic_to_mega_transitions++;
else if(pic_size == 0)
cold_call_to_ic_transitions++;
else if(pic_size == 1)
ic_to_pic_transitions++;
}
/* The cache_entries parameter is either f (on cold call site) or an array (on cache miss).
Called from assembly with the actual return address */
2009-05-04 05:50:24 -04:00
void *inline_cache_miss(cell return_address)
2009-05-02 05:04:19 -04:00
{
check_code_pointer(return_address);
/* Since each PIC is only referenced from a single call site,
if the old call target was a PIC, we can deallocate it immediately,
instead of leaving dead PICs around until the next GC. */
deallocate_inline_cache(return_address);
2009-05-04 05:50:24 -04:00
gc_root<array> cache_entries(dpop());
fixnum index = untag_fixnum(dpop());
gc_root<array> methods(dpop());
gc_root<word> generic_word(dpop());
gc_root<object> object(((cell *)ds)[-index]);
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
void *xt;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell pic_size = inline_cache_size(cache_entries.value());
2009-05-02 05:04:19 -04:00
update_pic_transitions(pic_size);
if(pic_size >= max_pic_size)
2009-05-02 10:19:09 -04:00
xt = megamorphic_call_stub(generic_word.value());
2009-05-02 05:04:19 -04:00
else
{
2009-05-04 05:50:24 -04:00
cell klass = object_class(object.value());
cell method = lookup_method(object.value(),methods.value());
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
gc_root<array> new_cache_entries(add_inline_cache_entry(
2009-05-02 10:19:09 -04:00
cache_entries.value(),
klass,
method));
xt = compile_inline_cache(index,
generic_word.value(),
methods.value(),
new_cache_entries.value()) + 1;
2009-05-02 05:04:19 -04:00
}
/* Install the new stub. */
2009-05-04 05:50:24 -04:00
set_call_target(return_address,xt);
2009-05-02 05:04:19 -04:00
#ifdef PIC_DEBUG
2009-05-04 05:50:24 -04:00
printf("Updated call site 0x%lx with 0x%lx\n",return_address,(cell)xt);
2009-05-02 05:04:19 -04:00
#endif
return xt;
}
PRIMITIVE(reset_inline_cache_stats)
2009-05-02 05:04:19 -04:00
{
cold_call_to_ic_transitions = ic_to_pic_transitions = pic_to_mega_transitions = 0;
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 05:04:19 -04:00
for(i = 0; i < 4; i++) pic_counts[i] = 0;
}
PRIMITIVE(inline_cache_stats)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
growable_array stats;
stats.add(allot_cell(cold_call_to_ic_transitions));
stats.add(allot_cell(ic_to_pic_transitions));
stats.add(allot_cell(pic_to_mega_transitions));
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 05:04:19 -04:00
for(i = 0; i < 4; i++)
2009-05-02 10:19:09 -04:00
stats.add(allot_cell(pic_counts[i]));
stats.trim();
2009-05-04 05:50:24 -04:00
dpush(stats.elements.value());
2009-05-02 05:04:19 -04:00
}
2009-05-04 02:46:13 -04:00
}