]> gitweb.factorcode.org Git - factor.git/blob - vm/words.cpp
Merge branch 'master' of git://factorcode.org/git/factor
[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. Allocates memory */
7 void factor_vm::jit_compile_word(cell word_, cell def_, bool relocating)
8 {
9         data_root<word> word(word_,this);
10         data_root<quotation> def(def_,this);
11
12         code_block *compiled = jit_compile_quot(word.value(),def.value(),relocating);
13         word->code = compiled;
14
15         if(to_boolean(word->pic_def)) jit_compile_quot(word->pic_def,relocating);
16         if(to_boolean(word->pic_tail_def)) jit_compile_quot(word->pic_tail_def,relocating);
17 }
18
19 cell factor_vm::find_all_words()
20 {
21         return instances(WORD_TYPE);
22 }
23
24 void factor_vm::compile_all_words()
25 {
26         data_root<array> words(find_all_words(),this);
27
28         cell length = array_capacity(words.untagged());
29         for(cell i = 0; i < length; i++)
30         {
31                 data_root<word> word(array_nth(words.untagged(),i),this);
32
33                 if(!word->code || !word->code->optimized_p())
34                         jit_compile_word(word.value(),word->def,false);
35
36                 update_word_xt(word.untagged());
37         }
38 }
39
40 word *factor_vm::allot_word(cell name_, cell vocab_, cell hashcode_)
41 {
42         data_root<object> vocab(vocab_,this);
43         data_root<object> name(name_,this);
44
45         data_root<word> new_word(allot<word>(sizeof(word)),this);
46
47         new_word->hashcode = hashcode_;
48         new_word->vocabulary = vocab.value();
49         new_word->name = name.value();
50         new_word->def = special_objects[OBJ_UNDEFINED];
51         new_word->props = false_object;
52         new_word->counter = tag_fixnum(0);
53         new_word->pic_def = false_object;
54         new_word->pic_tail_def = false_object;
55         new_word->subprimitive = false_object;
56         new_word->profiling = NULL;
57         new_word->code = NULL;
58
59         jit_compile_word(new_word.value(),new_word->def,true);
60         if(profiling_p)
61         {
62                 code_block *profiling_block = compile_profiling_stub(new_word.value());
63                 new_word->profiling = profiling_block;
64                 initialize_code_block(new_word->profiling);
65         }
66
67         update_word_xt(new_word.untagged());
68
69         return new_word.untagged();
70 }
71
72 /* (word) ( name vocabulary hashcode -- word ) */
73 void factor_vm::primitive_word()
74 {
75         cell hashcode = ctx->pop();
76         cell vocab = ctx->pop();
77         cell name = ctx->pop();
78         ctx->push(tag<word>(allot_word(name,vocab,hashcode)));
79 }
80
81 /* word-xt ( word -- start end ) */
82 void factor_vm::primitive_word_xt()
83 {
84         data_root<word> w(ctx->pop(),this);
85         w.untag_check(this);
86
87         if(profiling_p)
88         {
89                 ctx->push(allot_cell((cell)w->profiling->xt()));
90                 ctx->push(allot_cell((cell)w->profiling + w->profiling->size()));
91         }
92         else
93         {
94                 ctx->push(allot_cell((cell)w->code->xt()));
95                 ctx->push(allot_cell((cell)w->code + w->code->size()));
96         }
97 }
98
99 void factor_vm::update_word_xt(word *w)
100 {
101         if(profiling_p && w->profiling)
102                 w->xt = w->profiling->xt();
103         else
104                 w->xt = w->code->xt();
105 }
106
107 void factor_vm::primitive_optimized_p()
108 {
109         word *w = untag_check<word>(ctx->peek());
110         ctx->replace(tag_boolean(w->code->optimized_p()));
111 }
112
113 void factor_vm::primitive_wrapper()
114 {
115         wrapper *new_wrapper = allot<wrapper>(sizeof(wrapper));
116         new_wrapper->object = ctx->peek();
117         ctx->replace(tag<wrapper>(new_wrapper));
118 }
119
120 }