]> gitweb.factorcode.org Git - factor.git/blob - vm/words.cpp
moved words functions to vm
[factor.git] / vm / words.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 word *factorvm::allot_word(cell vocab_, cell name_)
7 {
8         gc_root<object> vocab(vocab_);
9         gc_root<object> name(name_);
10
11         gc_root<word> new_word(allot<word>(sizeof(word)));
12
13         new_word->hashcode = tag_fixnum((rand() << 16) ^ rand());
14         new_word->vocabulary = vocab.value();
15         new_word->name = name.value();
16         new_word->def = userenv[UNDEFINED_ENV];
17         new_word->props = F;
18         new_word->counter = tag_fixnum(0);
19         new_word->pic_def = F;
20         new_word->pic_tail_def = F;
21         new_word->subprimitive = F;
22         new_word->profiling = NULL;
23         new_word->code = NULL;
24
25         jit_compile_word(new_word.value(),new_word->def,true);
26         update_word_xt(new_word.value());
27
28         if(profiling_p)
29                 relocate_code_block(new_word->profiling);
30
31         return new_word.untagged();
32 }
33
34 word *allot_word(cell vocab_, cell name_)
35 {
36         return vm->allot_word(vocab_,name_);
37 }
38
39 /* <word> ( name vocabulary -- word ) */
40 inline void factorvm::vmprim_word()
41 {
42         cell vocab = dpop();
43         cell name = dpop();
44         dpush(tag<word>(allot_word(vocab,name)));
45 }
46
47 PRIMITIVE(word)
48 {
49         PRIMITIVE_GETVM()->vmprim_word();
50 }
51
52 /* word-xt ( word -- start end ) */
53 inline void factorvm::vmprim_word_xt()
54 {
55         word *w = untag_check<word>(dpop());
56         code_block *code = (profiling_p ? w->profiling : w->code);
57         dpush(allot_cell((cell)code->xt()));
58         dpush(allot_cell((cell)code + code->size));
59 }
60
61 PRIMITIVE(word_xt)
62 {
63         PRIMITIVE_GETVM()->vmprim_word_xt();
64 }
65
66 /* Allocates memory */
67 void factorvm::update_word_xt(cell w_)
68 {
69         gc_root<word> w(w_);
70
71         if(profiling_p)
72         {
73                 if(!w->profiling)
74                         w->profiling = compile_profiling_stub(w.value());
75
76                 w->xt = w->profiling->xt();
77         }
78         else
79                 w->xt = w->code->xt();
80 }
81
82 void update_word_xt(cell w_)
83 {
84         return vm->update_word_xt(w_);
85 }
86
87 inline void factorvm::vmprim_optimized_p()
88 {
89         drepl(tag_boolean(word_optimized_p(untag_check<word>(dpeek()))));
90 }
91
92 PRIMITIVE(optimized_p)
93 {
94         PRIMITIVE_GETVM()->vmprim_optimized_p();
95 }
96
97 inline void factorvm::vmprim_wrapper()
98 {
99         wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
100         new_wrapper->object = dpeek();
101         drepl(tag<wrapper>(new_wrapper));
102 }
103
104 PRIMITIVE(wrapper)
105 {
106         PRIMITIVE_GETVM()->vmprim_wrapper();
107 }
108
109 }