factor/vm/words.cpp

100 lines
2.0 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-09-23 14:05:46 -04:00
word *factor_vm::allot_word(cell vocab_, cell name_)
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
2009-05-04 05:50:24 -04:00
new_word->hashcode = tag_fixnum((rand() << 16) ^ rand());
new_word->vocabulary = vocab.value();
new_word->name = name.value();
new_word->def = userenv[UNDEFINED_ENV];
new_word->props = F;
new_word->counter = tag_fixnum(0);
new_word->pic_def = F;
new_word->pic_tail_def = F;
2009-05-04 05:50:24 -04:00
new_word->subprimitive = F;
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 -- word ) */
2009-09-23 14:05:46 -04:00
inline void factor_vm::primitive_word()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell vocab = dpop();
cell name = dpop();
dpush(tag<word>(allot_word(vocab,name)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(word)
{
2009-09-23 13:05:17 -04:00
PRIMITIVE_GETVM()->primitive_word();
2009-08-17 16:37:07 -04:00
}
2009-05-02 05:04:19 -04:00
/* word-xt ( word -- start end ) */
2009-09-23 14:05:46 -04:00
inline void factor_vm::primitive_word_xt()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
word *w = untag_check<word>(dpop());
code_block *code = (profiling_p ? w->profiling : w->code);
dpush(allot_cell((cell)code->xt()));
dpush(allot_cell((cell)code + code->size));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(word_xt)
{
2009-09-23 13:05:17 -04:00
PRIMITIVE_GETVM()->primitive_word_xt();
2009-08-17 16:37:07 -04:00
}
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)
w->profiling = compile_profiling_stub(w.value());
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
}
2009-09-23 14:05:46 -04:00
inline 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
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(optimized_p)
{
2009-09-23 13:05:17 -04:00
PRIMITIVE_GETVM()->primitive_optimized_p();
2009-08-17 16:37:07 -04:00
}
2009-09-23 14:05:46 -04:00
inline 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
2009-08-17 16:37:07 -04:00
PRIMITIVE(wrapper)
{
2009-09-23 13:05:17 -04:00
PRIMITIVE_GETVM()->primitive_wrapper();
2009-08-17 16:37:07 -04:00
}
2009-05-04 02:46:13 -04:00
}