]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
vm: replace line comments // with block comments /**/ for consintency
[factor.git] / vm / factor.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 void init_globals() { init_mvm(); }
6
7 /* Compile code in boot image so that we can execute the startup quotation */
8 /* Allocates memory */
9 void factor_vm::prepare_boot_image() {
10   std::cout << "*** Stage 2 early init... " << std::flush;
11
12   /* Compile all words. */
13   data_root<array> words(instances(WORD_TYPE), this);
14
15   cell n_words = array_capacity(words.untagged());
16   for (cell i = 0; i < n_words; i++) {
17     data_root<word> word(array_nth(words.untagged(), i), this);
18
19     FACTOR_ASSERT(!word->entry_point);
20     jit_compile_word(word.value(), word->def, false);
21   }
22   update_code_heap_words(true);
23
24   /* Initialize all quotations */
25   data_root<array> quotations(instances(QUOTATION_TYPE), this);
26
27   cell n_quots = array_capacity(quotations.untagged());
28   for (cell i = 0; i < n_quots; i++) {
29     data_root<quotation> quot(array_nth(quotations.untagged(), i), this);
30
31     if (!quot->entry_point)
32       quot->entry_point = lazy_jit_compile_entry_point();
33   }
34
35   special_objects[OBJ_STAGE2] = special_objects[OBJ_CANONICAL_TRUE];
36
37   std::cout << "done" << std::endl;
38 }
39
40 void factor_vm::init_factor(vm_parameters* p) {
41   /* Kilobytes */
42   p->datastack_size = align_page(p->datastack_size << 10);
43   p->retainstack_size = align_page(p->retainstack_size << 10);
44   p->callstack_size = align_page(p->callstack_size << 10);
45   p->callback_size = align_page(p->callback_size << 10);
46
47   /* Megabytes */
48   p->young_size <<= 20;
49   p->aging_size <<= 20;
50   p->tenured_size <<= 20;
51   p->code_size <<= 20;
52
53   /* Disable GC during init as a sanity check */
54   gc_off = true;
55
56   /* OS-specific initialization */
57   early_init();
58
59   p->executable_path = vm_executable_path();
60
61   if (p->image_path == NULL) {
62     if (embedded_image_p()) {
63       p->embedded_image = true;
64       p->image_path = safe_strdup(p->executable_path);
65     } else
66       p->image_path = default_image_path();
67   }
68
69   srand((unsigned int)nano_count());
70   init_ffi();
71   init_contexts(p->datastack_size, p->retainstack_size, p->callstack_size);
72   callbacks = new callback_heap(p->callback_size, this);
73   load_image(p);
74   init_c_io();
75   init_inline_caching((int)p->max_pic_size);
76   special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell));
77   special_objects[OBJ_ARGS] = false_object;
78   special_objects[OBJ_EMBEDDED] = false_object;
79
80   cell aliens[][2] = {
81     {OBJ_CPU,             (cell)FACTOR_CPU_STRING},
82     {OBJ_EXECUTABLE,      (cell)safe_strdup(p->executable_path)},
83     {OBJ_IMAGE,           (cell)safe_strdup(p->image_path)},
84     {OBJ_OS,              (cell)FACTOR_OS_STRING},
85     {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
86     {OBJ_VM_COMPILER,     (cell)FACTOR_COMPILER_VERSION},
87     {OBJ_VM_GIT_LABEL,    (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)},
88     {OBJ_VM_VERSION,      (cell)FACTOR_STRINGIZE(FACTOR_VERSION)},
89 #if defined(WINDOWS)
90     {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler}
91 #endif
92   };
93   int n_items = sizeof(aliens) / sizeof(cell[2]);
94   for (int n = 0; n < n_items; n++) {
95     cell idx = aliens[n][0];
96     special_objects[idx] = allot_alien(false_object, aliens[n][1]);
97   }
98
99   /* We can GC now */
100   gc_off = false;
101
102   if (!to_boolean(special_objects[OBJ_STAGE2]))
103     prepare_boot_image();
104
105   if (p->signals)
106     init_signals();
107
108   if (p->console)
109     open_console();
110
111 }
112
113 /* Allocates memory */
114 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
115   growable_array args(this);
116
117   for (fixnum i = 0; i < argc; i++)
118     args.add(allot_alien(false_object, (cell)argv[i]));
119
120   args.trim();
121   special_objects[OBJ_ARGS] = args.elements.value();
122 }
123
124 void factor_vm::stop_factor() {
125   c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
126 }
127
128 char* factor_vm::factor_eval_string(char* string) {
129   void* func = alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
130   CODE_TO_FUNCTION_POINTER(func);
131   return ((char * (*)(char*)) func)(string);
132 }
133
134 void factor_vm::factor_eval_free(char* result) { free(result); }
135
136 void factor_vm::factor_yield() {
137   void* func = alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
138   CODE_TO_FUNCTION_POINTER(func);
139   ((void(*)()) func)();
140 }
141
142 void factor_vm::factor_sleep(long us) {
143   void* func = alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
144   CODE_TO_FUNCTION_POINTER(func);
145   ((void(*)(long)) func)(us);
146 }
147
148 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
149   vm_parameters p;
150   p.init_from_args(argc, argv);
151   init_factor(&p);
152   pass_args_to_factor(argc, argv);
153
154   if (p.fep)
155     factorbug();
156
157   c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
158 }
159
160 factor_vm* new_factor_vm() {
161   THREADHANDLE thread = thread_id();
162   factor_vm* newvm = new factor_vm(thread);
163   register_vm_with_thread(newvm);
164   thread_vms[thread] = newvm;
165
166   return newvm;
167 }
168
169 VM_C_API void start_standalone_factor(int argc, vm_char** argv) {
170   factor_vm* newvm = new_factor_vm();
171   newvm->start_standalone_factor(argc, argv);
172   delete newvm;
173 }
174
175 }