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