]> gitweb.factorcode.org Git - factor.git/blob - vmpp/factor.cpp
147dff913be43d204957a37d23506a2a26c7bcd5
[factor.git] / vmpp / factor.cpp
1 #include "master.hpp"
2
3 void default_parameters(F_PARAMETERS *p)
4 {
5         p->image_path = NULL;
6
7         /* We make a wild guess here that if we're running on ARM, we don't
8         have a lot of memory. */
9 #ifdef FACTOR_ARM
10         p->ds_size = 8 * CELLS;
11         p->rs_size = 8 * CELLS;
12
13         p->gen_count = 2;
14         p->code_size = 4;
15         p->young_size = 1;
16         p->aging_size = 1;
17         p->tenured_size = 6;
18 #else
19         p->ds_size = 32 * CELLS;
20         p->rs_size = 32 * CELLS;
21
22         p->gen_count = 3;
23         p->code_size = 8 * CELLS;
24         p->young_size = CELLS / 4;
25         p->aging_size = CELLS / 2;
26         p->tenured_size = 4 * CELLS;
27 #endif
28
29         p->max_pic_size = 3;
30
31         p->secure_gc = false;
32         p->fep = false;
33
34 #ifdef WINDOWS
35         p->console = false;
36 #else
37         p->console = true;
38 #endif
39
40         p->stack_traces = true;
41 }
42
43 INLINE bool factor_arg(const F_CHAR* str, const F_CHAR* arg, CELL* value)
44 {
45         int val;
46         if(SSCANF(str,arg,&val) > 0)
47         {
48                 *value = val;
49                 return true;
50         }
51         else
52                 return false;
53 }
54
55 void init_parameters_from_args(F_PARAMETERS *p, int argc, F_CHAR **argv)
56 {
57         default_parameters(p);
58         p->executable_path = argv[0];
59
60         int i = 0;
61
62         for(i = 1; i < argc; i++)
63         {
64                 if(factor_arg(argv[i],STRING_LITERAL("-datastack=%d"),&p->ds_size));
65                 else if(factor_arg(argv[i],STRING_LITERAL("-retainstack=%d"),&p->rs_size));
66                 else if(factor_arg(argv[i],STRING_LITERAL("-generations=%d"),&p->gen_count));
67                 else if(factor_arg(argv[i],STRING_LITERAL("-young=%d"),&p->young_size));
68                 else if(factor_arg(argv[i],STRING_LITERAL("-aging=%d"),&p->aging_size));
69                 else if(factor_arg(argv[i],STRING_LITERAL("-tenured=%d"),&p->tenured_size));
70                 else if(factor_arg(argv[i],STRING_LITERAL("-codeheap=%d"),&p->code_size));
71                 else if(factor_arg(argv[i],STRING_LITERAL("-pic=%d"),&p->max_pic_size));
72                 else if(STRCMP(argv[i],STRING_LITERAL("-securegc")) == 0) p->secure_gc = true;
73                 else if(STRCMP(argv[i],STRING_LITERAL("-fep")) == 0) p->fep = true;
74                 else if(STRNCMP(argv[i],STRING_LITERAL("-i="),3) == 0) p->image_path = argv[i] + 3;
75                 else if(STRCMP(argv[i],STRING_LITERAL("-console")) == 0) p->console = true;
76                 else if(STRCMP(argv[i],STRING_LITERAL("-no-stack-traces")) == 0) p->stack_traces = false;
77         }
78 }
79
80 /* Do some initialization that we do once only */
81 void do_stage1_init(void)
82 {
83         print_string("*** Stage 2 early init... ");
84         fflush(stdout);
85
86         compile_all_words();
87         userenv[STAGE2_ENV] = T;
88
89         print_string("done\n");
90         fflush(stdout);
91 }
92
93 void init_factor(F_PARAMETERS *p)
94 {
95         /* Kilobytes */
96         p->ds_size = align_page(p->ds_size << 10);
97         p->rs_size = align_page(p->rs_size << 10);
98
99         /* Megabytes */
100         p->young_size <<= 20;
101         p->aging_size <<= 20;
102         p->tenured_size <<= 20;
103         p->code_size <<= 20;
104
105         /* Disable GC during init as a sanity check */
106         gc_off = true;
107
108         /* OS-specific initialization */
109         early_init();
110
111         const F_CHAR *executable_path = vm_executable_path();
112
113         if(executable_path)
114                 p->executable_path = executable_path;
115
116         if(p->image_path == NULL)
117                 p->image_path = default_image_path();
118
119         srand(current_micros());
120         init_ffi();
121         init_stacks(p->ds_size,p->rs_size);
122         load_image(p);
123         init_c_io();
124         init_inline_caching(p->max_pic_size);
125
126 #ifndef FACTOR_DEBUG
127         init_signals();
128 #endif
129
130         if(p->console)
131                 open_console();
132
133         init_profiler();
134
135         userenv[CPU_ENV] = tag_object(from_char_string(FACTOR_CPU_STRING));
136         userenv[OS_ENV] = tag_object(from_char_string(FACTOR_OS_STRING));
137         userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(CELL));
138         userenv[EXECUTABLE_ENV] = (p->executable_path ? tag_object(from_native_string(p->executable_path)) : F);
139         userenv[ARGS_ENV] = F;
140         userenv[EMBEDDED_ENV] = F;
141
142         /* We can GC now */
143         gc_off = false;
144
145         if(userenv[STAGE2_ENV] == F)
146         {
147                 userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces);
148                 do_stage1_init();
149         }
150 }
151
152 /* May allocate memory */
153 void pass_args_to_factor(int argc, F_CHAR **argv)
154 {
155         growable_array args;
156         int i;
157
158         for(i = 1; i < argc; i++)
159                 args.add(tag_object(from_native_string(argv[i])));
160
161         args.trim();
162         userenv[ARGS_ENV] = args.array.value();
163 }
164
165 void start_factor(F_PARAMETERS *p)
166 {
167         if(p->fep) factorbug();
168
169         nest_stacks();
170         c_to_factor_toplevel(userenv[BOOT_ENV]);
171         unnest_stacks();
172 }
173
174 void start_embedded_factor(F_PARAMETERS *p)
175 {
176         userenv[EMBEDDED_ENV] = T;
177         start_factor(p);
178 }
179
180 void start_standalone_factor(int argc, F_CHAR **argv)
181 {
182         F_PARAMETERS p;
183         default_parameters(&p);
184         init_parameters_from_args(&p,argc,argv);
185         init_factor(&p);
186         pass_args_to_factor(argc,argv);
187         start_factor(&p);
188 }
189
190 char *factor_eval_string(char *string)
191 {
192         char *(*callback)(char *) = (char *(*)(char *))alien_offset(userenv[EVAL_CALLBACK_ENV]);
193         return callback(string);
194 }
195
196 void factor_eval_free(char *result)
197 {
198         free(result);
199 }
200
201 void factor_yield(void)
202 {
203         void (*callback)(void) = (void (*)(void))alien_offset(userenv[YIELD_CALLBACK_ENV]);
204         callback();
205 }
206
207 void factor_sleep(long us)
208 {
209         void (*callback)(long) = (void (*)(long))alien_offset(userenv[SLEEP_CALLBACK_ENV]);
210         callback(us);
211 }