factor/vm/words.cpp

96 lines
2.1 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
{
word *factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_)
2009-05-02 05:04:19 -04:00
{
gc_root<object> vocab(vocab_,this);
gc_root<object> name(name_,this);
2009-05-02 10:19:09 -04:00
gc_root<word> new_word(allot<word>(sizeof(word)),this);
2009-05-02 05:04:19 -04:00
new_word->hashcode = hashcode_;
2009-05-04 05:50:24 -04:00
new_word->vocabulary = vocab.value();
new_word->name = name.value();
new_word->def = userenv[UNDEFINED_ENV];
new_word->props = false_object;
2009-05-04 05:50:24 -04:00
new_word->counter = tag_fixnum(0);
new_word->pic_def = false_object;
new_word->pic_tail_def = false_object;
new_word->subprimitive = false_object;
2009-05-04 05:50:24 -04:00
new_word->profiling = NULL;
new_word->code = NULL;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
jit_compile_word(new_word.value(),new_word->def,true);
update_word_xt(new_word.value());
2009-05-02 05:04:19 -04:00
if(profiling_p)
2009-05-04 05:50:24 -04:00
relocate_code_block(new_word->profiling);
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
return new_word.untagged();
2009-05-02 05:04:19 -04:00
}
/* (word) ( name vocabulary hashcode -- word ) */
void factor_vm::primitive_word()
2009-05-02 05:04:19 -04:00
{
cell hashcode = dpop();
2009-05-04 05:50:24 -04:00
cell vocab = dpop();
cell name = dpop();
dpush(tag<word>(allot_word(name,vocab,hashcode)));
2009-05-02 05:04:19 -04:00
}
/* word-xt ( word -- start end ) */
void factor_vm::primitive_word_xt()
2009-05-02 05:04:19 -04:00
{
gc_root<word> w(dpop(),this);
w.untag_check(this);
if(profiling_p)
{
dpush(allot_cell((cell)w->profiling->xt()));
dpush(allot_cell((cell)w->profiling + w->profiling->size()));
}
else
{
dpush(allot_cell((cell)w->code->xt()));
dpush(allot_cell((cell)w->code + w->code->size()));
}
2009-05-02 05:04:19 -04:00
}
/* Allocates memory */
2009-09-23 14:05:46 -04:00
void factor_vm::update_word_xt(cell w_)
2009-05-02 05:04:19 -04:00
{
gc_root<word> w(w_,this);
2009-05-02 10:19:09 -04:00
2009-05-02 05:04:19 -04:00
if(profiling_p)
{
2009-05-04 05:50:24 -04:00
if(!w->profiling)
{
/* Note: can't do w->profiling = ... since if LHS
evaluates before RHS, since in that case if RHS does a
GC, we will have an invalid pointer on the LHS */
code_block *profiling = compile_profiling_stub(w.value());
w->profiling = profiling;
}
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
w->xt = w->profiling->xt();
2009-05-02 05:04:19 -04:00
}
else
2009-05-04 05:50:24 -04:00
w->xt = w->code->xt();
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_optimized_p()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag_boolean(word_optimized_p(untag_check<word>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_wrapper()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
new_wrapper->object = dpeek();
drepl(tag<wrapper>(new_wrapper));
2009-05-02 05:04:19 -04:00
}
2009-05-04 02:46:13 -04:00
}