]> gitweb.factorcode.org Git - factor.git/blob - core/bootstrap/primitives.factor
Fix conflict
[factor.git] / core / bootstrap / primitives.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays byte-arrays generic hashtables
4 hashtables.private io kernel math math.private math.order
5 namespaces make parser sequences strings vectors words
6 quotations assocs layouts classes classes.builtin classes.tuple
7 classes.tuple.private kernel.private vocabs vocabs.loader
8 source-files definitions slots classes.union
9 classes.intersection classes.predicate compiler.units
10 bootstrap.image.private io.files accessors combinators ;
11 IN: bootstrap.primitives
12
13 "Creating primitives and basic runtime structures..." print flush
14
15 crossref off
16
17 H{ } clone sub-primitives set
18
19 "resource:core/bootstrap/syntax.factor" parse-file
20
21 "resource:basis/cpu/" architecture get {
22     { "x86.32" "x86/32" }
23     { "winnt-x86.64" "x86/64/winnt" }
24     { "unix-x86.64" "x86/64/unix" }
25     { "linux-ppc" "ppc/linux" }
26     { "macosx-ppc" "ppc/macosx" }
27     { "arm" "arm" }
28 } at "/bootstrap.factor" 3append parse-file
29
30 "resource:core/bootstrap/layouts/layouts.factor" parse-file
31
32 ! Now we have ( syntax-quot arch-quot layouts-quot ) on the stack
33
34 ! Bring up a bare cross-compiling vocabulary.
35 "syntax" vocab vocab-words bootstrap-syntax set
36 H{ } clone dictionary set
37 H{ } clone new-classes set
38 H{ } clone changed-definitions set
39 H{ } clone changed-generics set
40 H{ } clone remake-generics set
41 H{ } clone forgotten-definitions set
42 H{ } clone root-cache set
43 H{ } clone source-files set
44 H{ } clone update-map set
45 H{ } clone implementors-map set
46 init-caches
47
48 ! Vocabulary for slot accessors
49 "accessors" create-vocab drop
50
51 ! Trivial recompile hook. We don't want to touch the code heap
52 ! during stage1 bootstrap, it would just waste time.
53 [ drop { } ] recompile-hook set
54
55 call
56 call
57 call
58
59 ! After we execute bootstrap/layouts
60 num-types get f <array> builtins set
61
62 bootstrapping? on
63
64 ! Create some empty vocabs where the below primitives and
65 ! classes will go
66 {
67     "alien"
68     "alien.accessors"
69     "arrays"
70     "byte-arrays"
71     "classes.private"
72     "classes.tuple"
73     "classes.tuple.private"
74     "classes.predicate"
75     "compiler.units"
76     "continuations.private"
77     "growable"
78     "hashtables"
79     "hashtables.private"
80     "io"
81     "io.files"
82     "io.files.private"
83     "io.streams.c"
84     "locals.backend"
85     "kernel"
86     "kernel.private"
87     "math"
88     "math.private"
89     "memory"
90     "quotations"
91     "quotations.private"
92     "sbufs"
93     "sbufs.private"
94     "scratchpad"
95     "sequences"
96     "sequences.private"
97     "slots.private"
98     "strings"
99     "strings.private"
100     "system"
101     "system.private"
102     "threads.private"
103     "tools.profiler.private"
104     "words"
105     "words.private"
106     "vectors"
107     "vectors.private"
108 } [ create-vocab drop ] each
109
110 ! Builtin classes
111 : lookup-type-number ( word -- n )
112     global [ target-word ] bind type-number ;
113
114 : register-builtin ( class -- )
115     [ dup lookup-type-number "type" set-word-prop ]
116     [ dup "type" word-prop builtins get set-nth ]
117     [ f f f builtin-class define-class ]
118     tri ;
119
120 : prepare-slots ( slots -- slots' )
121     [ [ dup pair? [ first2 create ] when ] map ] map ;
122
123 : define-builtin-slots ( class slots -- )
124     prepare-slots make-slots 1 finalize-slots
125     [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
126
127 : define-builtin ( symbol slotspec -- )
128     [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
129
130 "fixnum" "math" create register-builtin
131 "bignum" "math" create register-builtin
132 "tuple" "kernel" create register-builtin
133 "ratio" "math" create register-builtin
134 "float" "math" create register-builtin
135 "complex" "math" create register-builtin
136 "f" "syntax" lookup register-builtin
137 "array" "arrays" create register-builtin
138 "wrapper" "kernel" create register-builtin
139 "callstack" "kernel" create register-builtin
140 "string" "strings" create register-builtin
141 "quotation" "quotations" create register-builtin
142 "dll" "alien" create register-builtin
143 "alien" "alien" create register-builtin
144 "word" "words" create register-builtin
145 "byte-array" "byte-arrays" create register-builtin
146
147 ! For predicate classes
148 "predicate-instance?" "classes.predicate" create drop
149
150 ! We need this before defining c-ptr below
151 "f" "syntax" lookup { } define-builtin
152
153 "f" "syntax" create [ not ] "predicate" set-word-prop
154 "f?" "syntax" vocab-words delete-at
155
156 ! Some unions
157 "integer" "math" create
158 "fixnum" "math" lookup
159 "bignum" "math" lookup
160 2array
161 define-union-class
162
163 "rational" "math" create
164 "integer" "math" lookup
165 "ratio" "math" lookup
166 2array
167 define-union-class
168
169 "real" "math" create
170 "rational" "math" lookup
171 "float" "math" lookup
172 2array
173 define-union-class
174
175 "c-ptr" "alien" create [
176     "alien" "alien" lookup ,
177     "f" "syntax" lookup ,
178     "byte-array" "byte-arrays" lookup ,
179 ] { } make define-union-class
180
181 ! A predicate class used for declarations
182 "array-capacity" "sequences.private" create
183 "fixnum" "math" lookup
184 [
185     [ dup 0 fixnum>= ] %
186     bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
187     [ [ drop f ] if ] %
188 ] [ ] make
189 define-predicate-class
190
191 "array-capacity" "sequences.private" lookup
192 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
193 "coercer" set-word-prop
194
195 ! Catch-all class for providing a default method.
196 "object" "kernel" create
197 [ f f { } intersection-class define-class ]
198 [ [ drop t ] "predicate" set-word-prop ]
199 bi
200
201 "object?" "kernel" vocab-words delete-at
202
203 ! Class of objects with object tag
204 "hi-tag" "kernel.private" create
205 builtins get num-tags get tail define-union-class
206
207 ! Empty class with no instances
208 "null" "kernel" create
209 [ f { } f union-class define-class ]
210 [ [ drop f ] "predicate" set-word-prop ]
211 bi
212
213 "null?" "kernel" vocab-words delete-at
214
215 "fixnum" "math" create { } define-builtin
216 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
217
218 "bignum" "math" create { } define-builtin
219 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
220
221 "ratio" "math" create {
222     { "numerator" { "integer" "math" } read-only }
223     { "denominator" { "integer" "math" } read-only }
224 } define-builtin
225
226 "float" "math" create { } define-builtin
227 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
228
229 "complex" "math" create {
230     { "real" { "real" "math" } read-only }
231     { "imaginary" { "real" "math" } read-only }
232 } define-builtin
233
234 "array" "arrays" create {
235     { "length" { "array-capacity" "sequences.private" } read-only }
236 } define-builtin
237
238 "wrapper" "kernel" create {
239     { "wrapped" read-only }
240 } define-builtin
241
242 "string" "strings" create {
243     { "length" { "array-capacity" "sequences.private" } read-only }
244     "aux"
245 } define-builtin
246
247 "quotation" "quotations" create {
248     { "array" { "array" "arrays" } read-only }
249     { "compiled" read-only }
250 } define-builtin
251
252 "dll" "alien" create {
253     { "path" { "byte-array" "byte-arrays" } read-only }
254 } define-builtin
255
256 "alien" "alien" create {
257     { "underlying" { "c-ptr" "alien" } read-only }
258     "expired"
259 } define-builtin
260
261 "word" "words" create {
262     { "hashcode" { "fixnum" "math" } }
263     "name"
264     "vocabulary"
265     { "def" { "quotation" "quotations" } initial: [ ] }
266     "props"
267     { "compiled" read-only }
268     { "counter" { "fixnum" "math" } }
269     { "sub-primitive" read-only }
270 } define-builtin
271
272 "byte-array" "byte-arrays" create {
273     { "length" { "array-capacity" "sequences.private" } read-only }
274 } define-builtin
275
276 "callstack" "kernel" create { } define-builtin
277
278 "tuple" "kernel" create
279 [ { } define-builtin ]
280 [ define-tuple-layout ]
281 bi
282
283 ! Create special tombstone values
284 "tombstone" "hashtables.private" create
285 tuple
286 { "state" } define-tuple-class
287
288 "((empty))" "hashtables.private" create
289 "tombstone" "hashtables.private" lookup f
290 2array >tuple 1quotation define-inline
291
292 "((tombstone))" "hashtables.private" create
293 "tombstone" "hashtables.private" lookup t
294 2array >tuple 1quotation define-inline
295
296 ! Some tuple classes
297 "curry" "kernel" create
298 tuple
299 {
300     { "obj" read-only }
301     { "quot" read-only }
302 } prepare-slots define-tuple-class
303
304 "curry" "kernel" lookup
305 {
306     [ f "inline" set-word-prop ]
307     [ make-flushable ]
308     [ ]
309     [
310         [
311             callable instance-check-quot %
312             tuple-layout ,
313             \ <tuple-boa> ,
314         ] [ ] make
315     ]
316 } cleave
317 (( obj quot -- curry )) define-declared
318
319 "compose" "kernel" create
320 tuple
321 {
322     { "first" read-only }
323     { "second" read-only }
324 } prepare-slots define-tuple-class
325
326 "compose" "kernel" lookup
327 {
328     [ f "inline" set-word-prop ]
329     [ make-flushable ]
330     [ ]
331     [
332         [
333             callable instance-check-quot [ dip ] curry %
334             callable instance-check-quot %
335             tuple-layout ,
336             \ <tuple-boa> ,
337         ] [ ] make
338     ]
339 } cleave
340 (( quot1 quot2 -- compose )) define-declared
341
342 ! Sub-primitive words
343 : make-sub-primitive ( word vocab -- )
344     create
345     dup reset-word
346     dup 1quotation define ;
347
348 {
349     { "(execute)" "words.private" }
350     { "(call)" "kernel.private" }
351     { "both-fixnums?" "math.private" }
352     { "fixnum+fast" "math.private" }
353     { "fixnum-fast" "math.private" }
354     { "fixnum*fast" "math.private" }
355     { "fixnum-bitand" "math.private" }
356     { "fixnum-bitor" "math.private" }
357     { "fixnum-bitxor" "math.private" }
358     { "fixnum-bitnot" "math.private" }
359     { "fixnum-mod" "math.private" }
360     { "fixnum-shift-fast" "math.private" }
361     { "fixnum/i-fast" "math.private" }
362     { "fixnum/mod-fast" "math.private" }
363     { "fixnum<" "math.private" }
364     { "fixnum<=" "math.private" }
365     { "fixnum>" "math.private" }
366     { "fixnum>=" "math.private" }
367     { "drop" "kernel" }
368     { "2drop" "kernel" }
369     { "3drop" "kernel" }
370     { "dup" "kernel" }
371     { "2dup" "kernel" }
372     { "3dup" "kernel" }
373     { "rot" "kernel" }
374     { "-rot" "kernel" }
375     { "dupd" "kernel" }
376     { "swapd" "kernel" }
377     { "nip" "kernel" }
378     { "2nip" "kernel" }
379     { "tuck" "kernel" }
380     { "over" "kernel" }
381     { "pick" "kernel" }
382     { "swap" "kernel" }
383     { ">r" "kernel" }
384     { "r>" "kernel" }
385     { "eq?" "kernel" }
386     { "tag" "kernel.private" }
387     { "slot" "slots.private" }
388     { "get-local" "locals.backend" }
389     { "drop-locals" "locals.backend" }
390 } [ make-sub-primitive ] assoc-each
391
392 ! Primitive words
393 : make-primitive ( word vocab n -- )
394     [ create dup reset-word ] dip
395     [ do-primitive ] curry [ ] like define ;
396
397 {
398     { "bignum>fixnum" "math.private" }
399     { "float>fixnum" "math.private" }
400     { "fixnum>bignum" "math.private" }
401     { "float>bignum" "math.private" }
402     { "fixnum>float" "math.private" }
403     { "bignum>float" "math.private" }
404     { "<ratio>" "math.private" }
405     { "string>float" "math.private" }
406     { "float>string" "math.private" }
407     { "float>bits" "math" }
408     { "double>bits" "math" }
409     { "bits>float" "math" }
410     { "bits>double" "math" }
411     { "<complex>" "math.private" }
412     { "fixnum+" "math.private" }
413     { "fixnum-" "math.private" }
414     { "fixnum*" "math.private" }
415     { "fixnum/i" "math.private" }
416     { "fixnum/mod" "math.private" }
417     { "fixnum-shift" "math.private" }
418     { "bignum=" "math.private" }
419     { "bignum+" "math.private" }
420     { "bignum-" "math.private" }
421     { "bignum*" "math.private" }
422     { "bignum/i" "math.private" }
423     { "bignum-mod" "math.private" }
424     { "bignum/mod" "math.private" }
425     { "bignum-bitand" "math.private" }
426     { "bignum-bitor" "math.private" }
427     { "bignum-bitxor" "math.private" }
428     { "bignum-bitnot" "math.private" }
429     { "bignum-shift" "math.private" }
430     { "bignum<" "math.private" }
431     { "bignum<=" "math.private" }
432     { "bignum>" "math.private" }
433     { "bignum>=" "math.private" }
434     { "bignum-bit?" "math.private" }
435     { "bignum-log2" "math.private" }
436     { "byte-array>bignum" "math" }
437     { "float=" "math.private" }
438     { "float+" "math.private" }
439     { "float-" "math.private" }
440     { "float*" "math.private" }
441     { "float/f" "math.private" }
442     { "float-mod" "math.private" }
443     { "float<" "math.private" }
444     { "float<=" "math.private" }
445     { "float>" "math.private" }
446     { "float>=" "math.private" }
447     { "<word>" "words" }
448     { "word-xt" "words" }
449     { "getenv" "kernel.private" }
450     { "setenv" "kernel.private" }
451     { "(exists?)" "io.files.private" }
452     { "gc" "memory" }
453     { "gc-stats" "memory" }
454     { "save-image" "memory" }
455     { "save-image-and-exit" "memory" }
456     { "datastack" "kernel" }
457     { "retainstack" "kernel" }
458     { "callstack" "kernel" }
459     { "set-datastack" "kernel" }
460     { "set-retainstack" "kernel" }
461     { "set-callstack" "kernel" }
462     { "exit" "system" }
463     { "data-room" "memory" }
464     { "code-room" "memory" }
465     { "micros" "system" }
466     { "modify-code-heap" "compiler.units" }
467     { "dlopen" "alien" }
468     { "dlsym" "alien" }
469     { "dlclose" "alien" }
470     { "<byte-array>" "byte-arrays" }
471     { "(byte-array)" "byte-arrays" }
472     { "<displaced-alien>" "alien" }
473     { "alien-signed-cell" "alien.accessors" }
474     { "set-alien-signed-cell" "alien.accessors" }
475     { "alien-unsigned-cell" "alien.accessors" }
476     { "set-alien-unsigned-cell" "alien.accessors" }
477     { "alien-signed-8" "alien.accessors" }
478     { "set-alien-signed-8" "alien.accessors" }
479     { "alien-unsigned-8" "alien.accessors" }
480     { "set-alien-unsigned-8" "alien.accessors" }
481     { "alien-signed-4" "alien.accessors" }
482     { "set-alien-signed-4" "alien.accessors" }
483     { "alien-unsigned-4" "alien.accessors" }
484     { "set-alien-unsigned-4" "alien.accessors" }
485     { "alien-signed-2" "alien.accessors" }
486     { "set-alien-signed-2" "alien.accessors" }
487     { "alien-unsigned-2" "alien.accessors" }
488     { "set-alien-unsigned-2" "alien.accessors" }
489     { "alien-signed-1" "alien.accessors" }
490     { "set-alien-signed-1" "alien.accessors" }
491     { "alien-unsigned-1" "alien.accessors" }
492     { "set-alien-unsigned-1" "alien.accessors" }
493     { "alien-float" "alien.accessors" }
494     { "set-alien-float" "alien.accessors" }
495     { "alien-double" "alien.accessors" }
496     { "set-alien-double" "alien.accessors" }
497     { "alien-cell" "alien.accessors" }
498     { "set-alien-cell" "alien.accessors" }
499     { "(throw)" "kernel.private" }
500     { "alien-address" "alien" }
501     { "set-slot" "slots.private" }
502     { "string-nth" "strings.private" }
503     { "set-string-nth-fast" "strings.private" }
504     { "set-string-nth-slow" "strings.private" }
505     { "resize-array" "arrays" }
506     { "resize-string" "strings" }
507     { "<array>" "arrays" }
508     { "begin-scan" "memory" }
509     { "next-object" "memory" }
510     { "end-scan" "memory" }
511     { "size" "memory" }
512     { "die" "kernel" }
513     { "fopen" "io.streams.c" }
514     { "fgetc" "io.streams.c" }
515     { "fread" "io.streams.c" }
516     { "fputc" "io.streams.c" }
517     { "fwrite" "io.streams.c" }
518     { "fflush" "io.streams.c" }
519     { "fclose" "io.streams.c" }
520     { "<wrapper>" "kernel" }
521     { "(clone)" "kernel" }
522     { "<string>" "strings" }
523     { "(string)" "strings.private" }
524     { "array>quotation" "quotations.private" }
525     { "quotation-xt" "quotations" }
526     { "<tuple>" "classes.tuple.private" }
527     { "profiling" "tools.profiler.private" }
528     { "become" "kernel.private" }
529     { "(sleep)" "threads.private" }
530     { "<tuple-boa>" "classes.tuple.private" }
531     { "callstack>array" "kernel" }
532     { "innermost-frame-quot" "kernel.private" }
533     { "innermost-frame-scan" "kernel.private" }
534     { "set-innermost-frame-quot" "kernel.private" }
535     { "call-clear" "kernel" }
536     { "resize-byte-array" "byte-arrays" }
537     { "dll-valid?" "alien" }
538     { "unimplemented" "kernel.private" }
539     { "gc-reset" "memory" }
540     { "jit-compile" "quotations" }
541     { "load-locals" "locals.backend" }
542 }
543 [ [ first2 ] dip make-primitive ] each-index
544
545 ! Bump build number
546 "build" "kernel" create build 1+ 1quotation define