]> gitweb.factorcode.org Git - factor.git/blob - vm/words.cpp
Merge branch 'ogg' of git://double.co.nz/git/factor
[factor.git] / vm / words.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 word *factor_vm::allot_word(cell vocab_, cell name_)
7 {
8         gc_root<object> vocab(vocab_,this);
9         gc_root<object> name(name_,this);
10
11         gc_root<word> new_word(allot<word>(sizeof(word)),this);
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> ( name vocabulary -- word ) */
35 inline void factor_vm::primitive_word()
36 {
37         cell vocab = dpop();
38         cell name = dpop();
39         dpush(tag<word>(allot_word(vocab,name)));
40 }
41
42 PRIMITIVE_FORWARD(word)
43
44 /* word-xt ( word -- start end ) */
45 inline void factor_vm::primitive_word_xt()
46 {
47         gc_root<word> w(dpop(),this);
48         w.untag_check(this);
49
50         if(profiling_p)
51         {
52                 dpush(allot_cell((cell)w->profiling->xt()));
53                 dpush(allot_cell((cell)w->profiling + w->profiling->size));
54         }
55         else
56         {
57                 dpush(allot_cell((cell)w->code->xt()));
58                 dpush(allot_cell((cell)w->code + w->code->size));
59         }
60 }
61
62 PRIMITIVE_FORWARD(word_xt)
63
64 /* Allocates memory */
65 void factor_vm::update_word_xt(cell w_)
66 {
67         gc_root<word> w(w_,this);
68
69         if(profiling_p)
70         {
71                 if(!w->profiling)
72                 {
73                         /* Note: can't do w->profiling = ... since if LHS
74                         evaluates before RHS, since in that case if RHS does a
75                         GC, we will have an invalid pointer on the LHS */
76                         code_block *profiling = compile_profiling_stub(w.value());
77                         w->profiling = profiling;
78                 }
79
80                 w->xt = w->profiling->xt();
81         }
82         else
83                 w->xt = w->code->xt();
84 }
85
86 inline void factor_vm::primitive_optimized_p()
87 {
88         drepl(tag_boolean(word_optimized_p(untag_check<word>(dpeek()))));
89 }
90
91 PRIMITIVE_FORWARD(optimized_p)
92
93 inline void factor_vm::primitive_wrapper()
94 {
95         wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
96         new_wrapper->object = dpeek();
97         drepl(tag<wrapper>(new_wrapper));
98 }
99
100 PRIMITIVE_FORWARD(wrapper)
101
102 }