]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/words.cpp
io.streams.256color: faster by caching styles
[factor.git] / vm / words.cpp
index a9375cfbc669188b385dd2b0842c3dfc10a0293a..cce37446ed5ce91926388172a2eb9480560aec83 100644 (file)
@@ -1,86 +1,81 @@
 #include "master.hpp"
 
-namespace factor
-{
-
-word *factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_)
-{
-       data_root<object> vocab(vocab_,this);
-       data_root<object> name(name_,this);
-
-       data_root<word> new_word(allot<word>(sizeof(word)),this);
-
-       new_word->hashcode = hashcode_;
-       new_word->vocabulary = vocab.value();
-       new_word->name = name.value();
-       new_word->def = special_objects[OBJ_UNDEFINED];
-       new_word->props = false_object;
-       new_word->counter = tag_fixnum(0);
-       new_word->pic_def = false_object;
-       new_word->pic_tail_def = false_object;
-       new_word->subprimitive = false_object;
-       new_word->profiling = NULL;
-       new_word->code = NULL;
-
-       jit_compile_word(new_word.value(),new_word->def,true);
-       if(profiling_p)
-       {
-               code_block *profiling_block = compile_profiling_stub(new_word.value());
-               new_word->profiling = profiling_block;
-               relocate_code_block(new_word->profiling);
-       }
-
-       update_word_xt(new_word.untagged());
-
-       return new_word.untagged();
+namespace factor {
+
+// Compile a word definition with the non-optimizing compiler.
+// Allocates memory
+void factor_vm::jit_compile_word(cell word_, cell def_, bool relocating) {
+  data_root<word> word(word_, this);
+  data_root<quotation> def(def_, this);
+
+  // Refuse to compile this word more than once, because quot_compiled_p()
+  // depends on the identity of its code block
+  if (word->entry_point &&
+      word.value() == special_objects[LAZY_JIT_COMPILE_WORD])
+    return;
+
+  code_block* compiled =
+      jit_compile_quotation(word.value(), def.value(), relocating);
+  word->entry_point = compiled->entry_point();
+
+  if (to_boolean(word->pic_def))
+    jit_compile_quotation(word->pic_def, relocating);
+  if (to_boolean(word->pic_tail_def))
+    jit_compile_quotation(word->pic_tail_def, relocating);
 }
 
-/* (word) ( name vocabulary hashcode -- word ) */
-void factor_vm::primitive_word()
-{
-       cell hashcode = dpop();
-       cell vocab = dpop();
-       cell name = dpop();
-       dpush(tag<word>(allot_word(name,vocab,hashcode)));
+// Allocates memory
+word* factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_) {
+  data_root<object> vocab(vocab_, this);
+  data_root<object> name(name_, this);
+
+  data_root<word> new_word(allot<word>(sizeof(word)), this);
+
+  new_word->hashcode = hashcode_;
+  new_word->vocabulary = vocab.value();
+  new_word->name = name.value();
+  new_word->def = special_objects[OBJ_UNDEFINED];
+  new_word->props = false_object;
+  new_word->pic_def = false_object;
+  new_word->pic_tail_def = false_object;
+  new_word->subprimitive = false_object;
+  new_word->entry_point = 0;
+
+  jit_compile_word(new_word.value(), new_word->def, true);
+
+  return new_word.untagged();
 }
 
-/* word-xt ( word -- start end ) */
-void factor_vm::primitive_word_xt()
-{
-       data_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()));
-       }
+// (word) ( name vocabulary hashcode -- word )
+// Allocates memory
+void factor_vm::primitive_word() {
+  cell hashcode = ctx->pop();
+  cell vocab = ctx->pop();
+  cell name = ctx->pop();
+  ctx->push(tag<word>(allot_word(name, vocab, hashcode)));
 }
 
-void factor_vm::update_word_xt(word *w)
-{
-       if(profiling_p && w->profiling)
-               w->xt = w->profiling->xt();
-       else
-               w->xt = w->code->xt();
+// word-code ( word -- start end )
+// Allocates memory (from_unsigned_cell allocates)
+void factor_vm::primitive_word_code() {
+  data_root<word> w(ctx->pop(), this);
+  check_tagged(w);
+
+  ctx->push(from_unsigned_cell(w->entry_point));
+  ctx->push(from_unsigned_cell((cell)w->code() + w->code()->size()));
 }
 
-void factor_vm::primitive_optimized_p()
-{
-       word *w = untag_check<word>(dpeek());
-       drepl(tag_boolean(w->code->optimized_p()));
+void factor_vm::primitive_word_optimized_p() {
+  word* w = untag_check<word>(ctx->peek());
+  cell t = w->code()->type();
+  ctx->replace(tag_boolean(t == CODE_BLOCK_OPTIMIZED));
 }
 
-void factor_vm::primitive_wrapper()
-{
-       wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
-       new_wrapper->object = dpeek();
-       drepl(tag<wrapper>(new_wrapper));
+// Allocates memory
+void factor_vm::primitive_wrapper() {
+  wrapper* new_wrapper = allot<wrapper>(sizeof(wrapper));
+  new_wrapper->object = ctx->peek();
+  ctx->replace(tag<wrapper>(new_wrapper));
 }
 
 }