]> gitweb.factorcode.org Git - factor.git/blob - vm/factor.cpp
VM: better init of stdin, stdout and stderr
[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   max_pic_size = (int)p->max_pic_size;
75   special_objects[OBJ_CELL_SIZE] = tag_fixnum(sizeof(cell));
76   special_objects[OBJ_ARGS] = false_object;
77   special_objects[OBJ_EMBEDDED] = false_object;
78
79   cell aliens[][2] = {
80     {OBJ_STDIN,           (cell)stdin},
81     {OBJ_STDOUT,          (cell)stdout},
82     {OBJ_STDERR,          (cell)stderr},
83     {OBJ_CPU,             (cell)FACTOR_CPU_STRING},
84     {OBJ_EXECUTABLE,      (cell)safe_strdup(p->executable_path)},
85     {OBJ_IMAGE,           (cell)safe_strdup(p->image_path)},
86     {OBJ_OS,              (cell)FACTOR_OS_STRING},
87     {OBJ_VM_COMPILE_TIME, (cell)FACTOR_COMPILE_TIME},
88     {OBJ_VM_COMPILER,     (cell)FACTOR_COMPILER_VERSION},
89     {OBJ_VM_GIT_LABEL,    (cell)FACTOR_STRINGIZE(FACTOR_GIT_LABEL)},
90     {OBJ_VM_VERSION,      (cell)FACTOR_STRINGIZE(FACTOR_VERSION)},
91 #if defined(WINDOWS)
92     {WIN_EXCEPTION_HANDLER, (cell)&factor::exception_handler}
93 #endif
94   };
95   int n_items = sizeof(aliens) / sizeof(cell[2]);
96   for (int n = 0; n < n_items; n++) {
97     cell idx = aliens[n][0];
98     special_objects[idx] = allot_alien(false_object, aliens[n][1]);
99   }
100
101   // We can GC now
102   gc_off = false;
103
104   if (!to_boolean(special_objects[OBJ_STAGE2]))
105     prepare_boot_image();
106
107   if (p->signals)
108     init_signals();
109
110   if (p->console)
111     open_console();
112
113 }
114
115 // Allocates memory
116 void factor_vm::pass_args_to_factor(int argc, vm_char** argv) {
117   growable_array args(this);
118
119   for (fixnum i = 0; i < argc; i++)
120     args.add(allot_alien(false_object, (cell)argv[i]));
121
122   args.trim();
123   special_objects[OBJ_ARGS] = args.elements.value();
124 }
125
126 void factor_vm::stop_factor() {
127   c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
128 }
129
130 char* factor_vm::factor_eval_string(char* string) {
131   void* func = alien_offset(special_objects[OBJ_EVAL_CALLBACK]);
132   CODE_TO_FUNCTION_POINTER(func);
133   return ((char * (*)(char*)) func)(string);
134 }
135
136 void factor_vm::factor_eval_free(char* result) { free(result); }
137
138 void factor_vm::factor_yield() {
139   void* func = alien_offset(special_objects[OBJ_YIELD_CALLBACK]);
140   CODE_TO_FUNCTION_POINTER(func);
141   ((void(*)()) func)();
142 }
143
144 void factor_vm::factor_sleep(long us) {
145   void* func = alien_offset(special_objects[OBJ_SLEEP_CALLBACK]);
146   CODE_TO_FUNCTION_POINTER(func);
147   ((void(*)(long)) func)(us);
148 }
149
150 void factor_vm::start_standalone_factor(int argc, vm_char** argv) {
151   vm_parameters p;
152   p.init_from_args(argc, argv);
153   init_factor(&p);
154   pass_args_to_factor(argc, argv);
155
156   if (p.fep)
157     factorbug();
158
159   c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
160 }
161
162 factor_vm* new_factor_vm() {
163   THREADHANDLE thread = thread_id();
164   factor_vm* newvm = new factor_vm(thread);
165   register_vm_with_thread(newvm);
166   thread_vms[thread] = newvm;
167
168   return newvm;
169 }
170
171 VM_C_API void start_standalone_factor(int argc, vm_char** argv) {
172   factor_vm* newvm = new_factor_vm();
173   newvm->start_standalone_factor(argc, argv);
174   delete newvm;
175 }
176
177 }