make-image can now generate images with Unicode strings
[factor/jcg.git] / vm / factor.c
blobd9042c945563a854a3b149dc9df24ea554b72c25
1 #include "master.h"
3 void default_parameters(F_PARAMETERS *p)
5 p->image_path = NULL;
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;
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;
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
29 p->secure_gc = false;
30 p->fep = false;
32 #ifdef WINDOWS
33 p->console = false;
34 #else
35 p->console = true;
36 #endif
38 p->stack_traces = true;
41 INLINE bool factor_arg(const F_CHAR* str, const F_CHAR* arg, CELL* value)
43 int val;
44 if(SSCANF(str,arg,&val) > 0)
46 *value = val;
47 return true;
49 else
50 return false;
53 void init_parameters_from_args(F_PARAMETERS *p, int argc, F_CHAR **argv)
55 default_parameters(p);
56 p->executable_path = argv[0];
58 int i = 0;
60 for(i = 1; i < argc; i++)
62 if(factor_arg(argv[i],STRING_LITERAL("-datastack=%d"),&p->ds_size));
63 else if(factor_arg(argv[i],STRING_LITERAL("-retainstack=%d"),&p->rs_size));
64 else if(factor_arg(argv[i],STRING_LITERAL("-generations=%d"),&p->gen_count));
65 else if(factor_arg(argv[i],STRING_LITERAL("-young=%d"),&p->young_size));
66 else if(factor_arg(argv[i],STRING_LITERAL("-aging=%d"),&p->aging_size));
67 else if(factor_arg(argv[i],STRING_LITERAL("-tenured=%d"),&p->tenured_size));
68 else if(factor_arg(argv[i],STRING_LITERAL("-codeheap=%d"),&p->code_size));
69 else if(STRCMP(argv[i],STRING_LITERAL("-securegc")) == 0) p->secure_gc = true;
70 else if(STRCMP(argv[i],STRING_LITERAL("-fep")) == 0) p->fep = true;
71 else if(STRNCMP(argv[i],STRING_LITERAL("-i="),3) == 0) p->image_path = argv[i] + 3;
72 else if(STRCMP(argv[i],STRING_LITERAL("-console")) == 0) p->console = true;
73 else if(STRCMP(argv[i],STRING_LITERAL("-no-stack-traces")) == 0) p->stack_traces = false;
77 /* Do some initialization that we do once only */
78 void do_stage1_init(void)
80 print_string("*** Stage 2 early init... ");
81 fflush(stdout);
83 compile_all_words();
84 userenv[STAGE2_ENV] = T;
86 print_string("done\n");
87 fflush(stdout);
90 void init_factor(F_PARAMETERS *p)
92 /* Kilobytes */
93 p->ds_size = align_page(p->ds_size << 10);
94 p->rs_size = align_page(p->rs_size << 10);
96 /* Megabytes */
97 p->young_size <<= 20;
98 p->aging_size <<= 20;
99 p->tenured_size <<= 20;
100 p->code_size <<= 20;
102 /* Disable GC during init as a sanity check */
103 gc_off = true;
105 /* OS-specific initialization */
106 early_init();
108 const F_CHAR *executable_path = vm_executable_path();
110 if(executable_path)
111 p->executable_path = executable_path;
113 if(p->image_path == NULL)
114 p->image_path = default_image_path();
116 srand(current_micros());
117 init_ffi();
118 init_stacks(p->ds_size,p->rs_size);
119 load_image(p);
120 init_c_io();
121 init_signals();
123 if(p->console)
124 open_console();
126 stack_chain = NULL;
127 profiling_p = false;
128 performing_gc = false;
129 last_code_heap_scan = NURSERY;
130 collecting_aging_again = false;
132 userenv[CPU_ENV] = tag_object(from_char_string(FACTOR_CPU_STRING));
133 userenv[OS_ENV] = tag_object(from_char_string(FACTOR_OS_STRING));
134 userenv[CELL_SIZE_ENV] = tag_fixnum(sizeof(CELL));
135 userenv[STACK_TRACES_ENV] = tag_boolean(p->stack_traces);
136 userenv[EXECUTABLE_ENV] = (p->executable_path ?
137 tag_object(from_native_string(p->executable_path)) : F);
138 userenv[ARGS_ENV] = F;
139 userenv[EMBEDDED_ENV] = F;
141 /* We can GC now */
142 gc_off = false;
144 if(!stage2)
145 do_stage1_init();
148 /* May allocate memory */
149 void pass_args_to_factor(int argc, F_CHAR **argv)
151 F_ARRAY *args = allot_array(ARRAY_TYPE,argc,F);
152 int i;
154 for(i = 1; i < argc; i++)
156 REGISTER_UNTAGGED(args);
157 CELL arg = tag_object(from_native_string(argv[i]));
158 UNREGISTER_UNTAGGED(args);
159 set_array_nth(args,i,arg);
162 userenv[ARGS_ENV] = tag_object(args);
165 void start_factor(F_PARAMETERS *p)
167 if(p->fep) factorbug();
169 nest_stacks();
170 c_to_factor_toplevel(userenv[BOOT_ENV]);
171 unnest_stacks();
174 void start_embedded_factor(F_PARAMETERS *p)
176 userenv[EMBEDDED_ENV] = T;
177 start_factor(p);
180 void start_standalone_factor(int argc, F_CHAR **argv)
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);
190 char *factor_eval_string(char *string)
192 char* (*callback)(char*) = alien_offset(userenv[EVAL_CALLBACK_ENV]);
193 return callback(string);
196 void factor_eval_free(char *result)
198 free(result);
201 void factor_yield(void)
203 void (*callback)() = alien_offset(userenv[YIELD_CALLBACK_ENV]);
204 callback();
207 void factor_sleep(long us)
209 void (*callback)() = alien_offset(userenv[SLEEP_CALLBACK_ENV]);
210 callback(us);