]> gitweb.factorcode.org Git - factor.git/blob - vm/words.cpp
vm: More allocates comments.
[factor.git] / vm / words.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 /* Compile a word definition with the non-optimizing compiler. */
7 /* Allocates memory */
8 void factor_vm::jit_compile_word(cell word_, cell def_, bool relocating)
9 {
10         data_root<word> word(word_,this);
11         data_root<quotation> def(def_,this);
12
13         /* Refuse to compile this word more than once, because quot_compiled_p()
14         depends on the identity of its code block */
15         if(word->entry_point && word.value() == special_objects[LAZY_JIT_COMPILE_WORD])
16                 return;
17
18         code_block *compiled = jit_compile_quot(word.value(),def.value(),relocating);
19         word->entry_point = compiled->entry_point();
20
21         if(to_boolean(word->pic_def)) jit_compile_quot(word->pic_def,relocating);
22         if(to_boolean(word->pic_tail_def)) jit_compile_quot(word->pic_tail_def,relocating);
23 }
24
25 /* Allocates memory */
26 cell factor_vm::find_all_words()
27 {
28         return instances(WORD_TYPE);
29 }
30
31 /* Allocates memory */
32 void factor_vm::compile_all_words()
33 {
34         data_root<array> words(find_all_words(),this);
35
36         cell length = array_capacity(words.untagged());
37         for(cell i = 0; i < length; i++)
38         {
39                 data_root<word> word(array_nth(words.untagged(),i),this);
40
41                 if(!word->entry_point || !word->code()->optimized_p())
42                         jit_compile_word(word.value(),word->def,false);
43         }
44 }
45
46 /* Allocates memory */
47 word *factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_)
48 {
49         data_root<object> vocab(vocab_,this);
50         data_root<object> name(name_,this);
51
52         data_root<word> new_word(allot<word>(sizeof(word)),this);
53
54         new_word->hashcode = hashcode_;
55         new_word->vocabulary = vocab.value();
56         new_word->name = name.value();
57         new_word->def = special_objects[OBJ_UNDEFINED];
58         new_word->props = false_object;
59         new_word->pic_def = false_object;
60         new_word->pic_tail_def = false_object;
61         new_word->subprimitive = false_object;
62         new_word->entry_point = NULL;
63
64         jit_compile_word(new_word.value(),new_word->def,true);
65
66         return new_word.untagged();
67 }
68
69 /* (word) ( name vocabulary hashcode -- word ) */
70 /* Allocates memory */
71 void factor_vm::primitive_word()
72 {
73         cell hashcode = ctx->pop();
74         cell vocab = ctx->pop();
75         cell name = ctx->pop();
76         ctx->push(tag<word>(allot_word(name,vocab,hashcode)));
77 }
78
79 /* word-code ( word -- start end ) */
80 /* Allocates memory (from_unsigned_cell allocates) */
81 void factor_vm::primitive_word_code()
82 {
83         data_root<word> w(ctx->pop(),this);
84         w.untag_check(this);
85
86         ctx->push(from_unsigned_cell((cell)w->entry_point));
87         ctx->push(from_unsigned_cell((cell)w->code() + w->code()->size()));
88 }
89
90 void factor_vm::primitive_optimized_p()
91 {
92         word *w = untag_check<word>(ctx->peek());
93         ctx->replace(tag_boolean(w->code()->optimized_p()));
94 }
95
96 /* Allocates memory */
97 void factor_vm::primitive_wrapper()
98 {
99         wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
100         new_wrapper->object = ctx->peek();
101         ctx->replace(tag<wrapper>(new_wrapper));
102 }
103
104 }