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