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