]> gitweb.factorcode.org Git - factor.git/blob - core/bootstrap/primitives.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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     "vectors"
105     "vectors.private"
106 } [ create-vocab drop ] each
107
108 ! Builtin classes
109 : lookup-type-number ( word -- n )
110     global [ target-word ] bind type-number ;
111
112 : register-builtin ( class -- )
113     [ dup lookup-type-number "type" set-word-prop ]
114     [ dup "type" word-prop builtins get set-nth ]
115     [ f f f builtin-class define-class ]
116     tri ;
117
118 : prepare-slots ( slots -- slots' )
119     [ [ dup pair? [ first2 create ] when ] map ] map ;
120
121 : define-builtin-slots ( class slots -- )
122     prepare-slots make-slots 1 finalize-slots
123     [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
124
125 : define-builtin ( symbol slotspec -- )
126     [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
127
128 "fixnum" "math" create register-builtin
129 "bignum" "math" create register-builtin
130 "tuple" "kernel" create register-builtin
131 "float" "math" create register-builtin
132 "f" "syntax" lookup register-builtin
133 "array" "arrays" create register-builtin
134 "wrapper" "kernel" create register-builtin
135 "callstack" "kernel" create register-builtin
136 "string" "strings" create register-builtin
137 "quotation" "quotations" create register-builtin
138 "dll" "alien" create register-builtin
139 "alien" "alien" create register-builtin
140 "word" "words" create register-builtin
141 "byte-array" "byte-arrays" create register-builtin
142
143 ! We need this before defining c-ptr below
144 "f" "syntax" lookup { } define-builtin
145
146 "f" "syntax" create [ not ] "predicate" set-word-prop
147 "f?" "syntax" vocab-words delete-at
148
149 ! Some unions
150 "c-ptr" "alien" create [
151     "alien" "alien" lookup ,
152     "f" "syntax" lookup ,
153     "byte-array" "byte-arrays" lookup ,
154 ] { } make define-union-class
155
156 ! A predicate class used for declarations
157 "array-capacity" "sequences.private" create
158 "fixnum" "math" lookup
159 [
160     [ dup 0 fixnum>= ] %
161     bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
162     [ [ drop f ] if ] %
163 ] [ ] make
164 define-predicate-class
165
166 "array-capacity" "sequences.private" lookup
167 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
168 "coercer" set-word-prop
169
170 ! Catch-all class for providing a default method.
171 "object" "kernel" create
172 [ f f { } intersection-class define-class ]
173 [ [ drop t ] "predicate" set-word-prop ]
174 bi
175
176 "object?" "kernel" vocab-words delete-at
177
178 ! Class of objects with object tag
179 "hi-tag" "kernel.private" create
180 builtins get num-tags get tail define-union-class
181
182 ! Empty class with no instances
183 "null" "kernel" create
184 [ f { } f union-class define-class ]
185 [ [ drop f ] "predicate" set-word-prop ]
186 bi
187
188 "null?" "kernel" vocab-words delete-at
189
190 "fixnum" "math" create { } define-builtin
191 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
192
193 "bignum" "math" create { } define-builtin
194 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
195
196 "float" "math" create { } define-builtin
197 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
198
199 "array" "arrays" create {
200     { "length" { "array-capacity" "sequences.private" } read-only }
201 } define-builtin
202
203 "wrapper" "kernel" create {
204     { "wrapped" read-only }
205 } define-builtin
206
207 "string" "strings" create {
208     { "length" { "array-capacity" "sequences.private" } read-only }
209     "aux"
210 } define-builtin
211
212 "quotation" "quotations" create {
213     { "array" { "array" "arrays" } read-only }
214     { "compiled" read-only }
215     "cached-effect"
216     "cache-counter"
217 } define-builtin
218
219 "dll" "alien" create {
220     { "path" { "byte-array" "byte-arrays" } read-only }
221 } define-builtin
222
223 "alien" "alien" create {
224     { "underlying" { "c-ptr" "alien" } read-only }
225     "expired"
226 } define-builtin
227
228 "word" "words" create {
229     { "hashcode" { "fixnum" "math" } }
230     "name"
231     "vocabulary"
232     { "def" { "quotation" "quotations" } initial: [ ] }
233     "props"
234     { "direct-entry-def" }
235     { "counter" { "fixnum" "math" } }
236     { "sub-primitive" read-only }
237 } define-builtin
238
239 "byte-array" "byte-arrays" create {
240     { "length" { "array-capacity" "sequences.private" } read-only }
241 } define-builtin
242
243 "callstack" "kernel" create { } define-builtin
244
245 "tuple" "kernel" create
246 [ { } define-builtin ]
247 [ define-tuple-layout ]
248 bi
249
250 ! Create special tombstone values
251 "tombstone" "hashtables.private" create
252 tuple
253 { "state" } define-tuple-class
254
255 "((empty))" "hashtables.private" create
256 "tombstone" "hashtables.private" lookup f
257 2array >tuple 1quotation (( -- value )) define-inline
258
259 "((tombstone))" "hashtables.private" create
260 "tombstone" "hashtables.private" lookup t
261 2array >tuple 1quotation (( -- value )) define-inline
262
263 ! Some tuple classes
264 "curry" "kernel" create
265 tuple
266 {
267     { "obj" read-only }
268     { "quot" read-only }
269 } prepare-slots define-tuple-class
270
271 "curry" "kernel" lookup
272 {
273     [ f "inline" set-word-prop ]
274     [ make-flushable ]
275     [ ]
276     [
277         [
278             callable instance-check-quot %
279             tuple-layout ,
280             \ <tuple-boa> ,
281         ] [ ] make
282     ]
283 } cleave
284 (( obj quot -- curry )) define-declared
285
286 "compose" "kernel" create
287 tuple
288 {
289     { "first" read-only }
290     { "second" read-only }
291 } prepare-slots define-tuple-class
292
293 "compose" "kernel" lookup
294 {
295     [ f "inline" set-word-prop ]
296     [ make-flushable ]
297     [ ]
298     [
299         [
300             callable instance-check-quot [ dip ] curry %
301             callable instance-check-quot %
302             tuple-layout ,
303             \ <tuple-boa> ,
304         ] [ ] make
305     ]
306 } cleave
307 (( quot1 quot2 -- compose )) define-declared
308
309 ! Sub-primitive words
310 : make-sub-primitive ( word vocab effect -- )
311     [ create dup 1quotation ] dip define-declared ;
312
313 {
314     { "(execute)" "kernel.private" (( word -- )) }
315     { "(call)" "kernel.private" (( quot -- )) }
316     { "both-fixnums?" "math.private" (( x y -- ? )) }
317     { "fixnum+fast" "math.private" (( x y -- z )) }
318     { "fixnum-fast" "math.private" (( x y -- z )) }
319     { "fixnum*fast" "math.private" (( x y -- z )) }
320     { "fixnum-bitand" "math.private" (( x y -- z )) }
321     { "fixnum-bitor" "math.private" (( x y -- z )) }
322     { "fixnum-bitxor" "math.private" (( x y -- z )) }
323     { "fixnum-bitnot" "math.private" (( x -- y )) }
324     { "fixnum-mod" "math.private" (( x y -- z )) }
325     { "fixnum-shift-fast" "math.private" (( x y -- z )) }
326     { "fixnum/i-fast" "math.private" (( x y -- z )) }
327     { "fixnum/mod-fast" "math.private" (( x y -- z w )) }
328     { "fixnum<" "math.private" (( x y -- ? )) }
329     { "fixnum<=" "math.private" (( x y -- z )) }
330     { "fixnum>" "math.private" (( x y -- ? )) }
331     { "fixnum>=" "math.private" (( x y -- ? )) }
332     { "drop" "kernel" (( x -- )) }
333     { "2drop" "kernel" (( x y -- )) }
334     { "3drop" "kernel" (( x y z -- )) }
335     { "dup" "kernel" (( x -- x x )) }
336     { "2dup" "kernel" (( x y -- x y x y )) }
337     { "3dup" "kernel" (( x y z -- x y z x y z )) }
338     { "rot" "kernel" (( x y z -- y z x )) }
339     { "-rot" "kernel" (( x y z -- z x y )) }
340     { "dupd" "kernel" (( x y -- x x y )) }
341     { "swapd" "kernel" (( x y z -- y x z )) }
342     { "nip" "kernel" (( x y -- y )) }
343     { "2nip" "kernel" (( x y z -- z )) }
344     { "tuck" "kernel" (( x y -- y x y )) }
345     { "over" "kernel" (( x y -- x y x )) }
346     { "pick" "kernel" (( x y z -- x y z x )) }
347     { "swap" "kernel" (( x y -- y x )) }
348     { "eq?" "kernel" (( obj1 obj2 -- ? )) }
349     { "tag" "kernel.private" (( object -- n )) }
350     { "slot" "slots.private" (( obj m -- value )) }
351     { "get-local" "locals.backend" (( n -- obj )) }
352     { "load-local" "locals.backend" (( obj -- )) }
353     { "drop-locals" "locals.backend" (( n -- )) }
354     { "mega-cache-lookup" "generic.single.private" (( methods index cache -- )) }
355 } [ first3 make-sub-primitive ] each
356
357 ! Primitive words
358 : make-primitive ( word vocab n effect -- )
359     [
360         [ create dup reset-word ] dip
361         [ do-primitive ] curry
362     ] dip define-declared ;
363
364 {
365     { "bignum>fixnum" "math.private" (( x -- y )) }
366     { "float>fixnum" "math.private" (( x -- y )) }
367     { "fixnum>bignum" "math.private" (( x -- y )) }
368     { "float>bignum" "math.private" (( x -- y )) }
369     { "fixnum>float" "math.private" (( x -- y )) }
370     { "bignum>float" "math.private" (( x -- y )) }
371     { "(string>float)" "math.parser.private" (( str -- n/f )) }
372     { "(float>string)" "math.parser.private" (( n -- str )) }
373     { "float>bits" "math" (( x -- n )) }
374     { "double>bits" "math" (( x -- n )) }
375     { "bits>float" "math" (( n -- x )) }
376     { "bits>double" "math" (( n -- x )) }
377     { "fixnum+" "math.private" (( x y -- z )) }
378     { "fixnum-" "math.private" (( x y -- z )) }
379     { "fixnum*" "math.private" (( x y -- z )) }
380     { "fixnum/i" "math.private" (( x y -- z )) }
381     { "fixnum/mod" "math.private" (( x y -- z w )) }
382     { "fixnum-shift" "math.private" (( x y -- z )) }
383     { "bignum=" "math.private" (( x y -- ? )) }
384     { "bignum+" "math.private" (( x y -- z )) }
385     { "bignum-" "math.private" (( x y -- z )) }
386     { "bignum*" "math.private" (( x y -- z )) }
387     { "bignum/i" "math.private" (( x y -- z )) }
388     { "bignum-mod" "math.private" (( x y -- z )) }
389     { "bignum/mod" "math.private" (( x y -- z w )) }
390     { "bignum-bitand" "math.private" (( x y -- z )) }
391     { "bignum-bitor" "math.private" (( x y -- z )) }
392     { "bignum-bitxor" "math.private" (( x y -- z )) }
393     { "bignum-bitnot" "math.private" (( x -- y )) }
394     { "bignum-shift" "math.private" (( x y -- z )) }
395     { "bignum<" "math.private" (( x y -- ? )) }
396     { "bignum<=" "math.private" (( x y -- ? )) }
397     { "bignum>" "math.private" (( x y -- ? )) }
398     { "bignum>=" "math.private" (( x y -- ? )) }
399     { "bignum-bit?" "math.private" (( n x -- ? )) }
400     { "bignum-log2" "math.private" (( x -- n )) }
401     { "byte-array>bignum" "math" (( x -- y ))  }
402     { "float=" "math.private" (( x y -- ? )) }
403     { "float+" "math.private" (( x y -- z )) }
404     { "float-" "math.private" (( x y -- z )) }
405     { "float*" "math.private" (( x y -- z )) }
406     { "float/f" "math.private" (( x y -- z )) }
407     { "float-mod" "math.private" (( x y -- z )) }
408     { "float<" "math.private" (( x y -- ? )) }
409     { "float<=" "math.private" (( x y -- ? )) }
410     { "float>" "math.private" (( x y -- ? )) }
411     { "float>=" "math.private" (( x y -- ? )) }
412     { "<word>" "words" (( name vocab -- word )) }
413     { "word-xt" "words" (( word -- start end )) }
414     { "getenv" "kernel.private" (( n -- obj )) }
415     { "setenv" "kernel.private" (( obj n -- )) }
416     { "(exists?)" "io.files.private" (( path -- ? )) }
417     { "gc" "memory" (( -- )) }
418     { "gc-stats" "memory" f }
419     { "(save-image)" "memory.private" (( path -- )) }
420     { "(save-image-and-exit)" "memory.private" (( path -- )) }
421     { "datastack" "kernel" (( -- ds )) }
422     { "retainstack" "kernel" (( -- rs )) }
423     { "callstack" "kernel" (( -- cs )) }
424     { "set-datastack" "kernel" (( ds -- )) }
425     { "set-retainstack" "kernel" (( rs -- )) }
426     { "set-callstack" "kernel" (( cs -- )) }
427     { "exit" "system" (( n -- )) }
428     { "data-room" "memory" (( -- cards generations )) }
429     { "code-room" "memory" (( -- code-free code-total )) }
430     { "micros" "system" (( -- us )) }
431     { "modify-code-heap" "compiler.units" (( alist -- )) }
432     { "(dlopen)" "alien.libraries" (( path -- dll )) }
433     { "(dlsym)" "alien.libraries" (( name dll -- alien )) }
434     { "dlclose" "alien.libraries" (( dll -- )) }
435     { "<byte-array>" "byte-arrays" (( n -- byte-array )) }
436     { "(byte-array)" "byte-arrays" (( n -- byte-array )) }
437     { "<displaced-alien>" "alien" (( displacement c-ptr -- alien )) }
438     { "alien-signed-cell" "alien.accessors" (( c-ptr n -- value )) }
439     { "set-alien-signed-cell" "alien.accessors" (( value c-ptr n -- )) }
440     { "alien-unsigned-cell" "alien.accessors" (( c-ptr n -- value )) }
441     { "set-alien-unsigned-cell" "alien.accessors" (( value c-ptr n -- )) }
442     { "alien-signed-8" "alien.accessors" (( c-ptr n -- value )) }
443     { "set-alien-signed-8" "alien.accessors" (( value c-ptr n -- )) }
444     { "alien-unsigned-8" "alien.accessors" (( c-ptr n -- value )) }
445     { "set-alien-unsigned-8" "alien.accessors" (( value c-ptr n -- )) }
446     { "alien-signed-4" "alien.accessors" (( c-ptr n -- value )) }
447     { "set-alien-signed-4" "alien.accessors" (( value c-ptr n -- )) }
448     { "alien-unsigned-4" "alien.accessors" (( c-ptr n -- value )) }
449     { "set-alien-unsigned-4" "alien.accessors" (( value c-ptr n -- )) }
450     { "alien-signed-2" "alien.accessors" (( c-ptr n -- value )) }
451     { "set-alien-signed-2" "alien.accessors" (( value c-ptr n -- )) }
452     { "alien-unsigned-2" "alien.accessors" (( c-ptr n -- value )) }
453     { "set-alien-unsigned-2" "alien.accessors" (( value c-ptr n -- )) }
454     { "alien-signed-1" "alien.accessors" (( c-ptr n -- value )) }
455     { "set-alien-signed-1" "alien.accessors" (( value c-ptr n -- )) }
456     { "alien-unsigned-1" "alien.accessors" (( c-ptr n -- value )) }
457     { "set-alien-unsigned-1" "alien.accessors" (( value c-ptr n -- )) }
458     { "alien-float" "alien.accessors" (( c-ptr n -- value )) }
459     { "set-alien-float" "alien.accessors" (( value c-ptr n -- )) }
460     { "alien-double" "alien.accessors" (( c-ptr n -- value )) }
461     { "set-alien-double" "alien.accessors" (( value c-ptr n -- )) }
462     { "alien-cell" "alien.accessors" (( c-ptr n -- value )) }
463     { "set-alien-cell" "alien.accessors" (( value c-ptr n -- )) }
464     { "alien-address" "alien" (( c-ptr -- addr )) }
465     { "set-slot" "slots.private" (( value obj n -- )) }
466     { "string-nth" "strings.private" (( n string -- ch )) }
467     { "set-string-nth-fast" "strings.private" (( ch n string -- )) }
468     { "set-string-nth-slow" "strings.private" (( ch n string -- )) }
469     { "resize-array" "arrays" (( n array -- newarray )) }
470     { "resize-string" "strings" (( n str -- newstr )) }
471     { "<array>" "arrays" (( n elt -- array )) }
472     { "begin-scan" "memory" (( -- )) }
473     { "next-object" "memory" (( -- obj )) }
474     { "end-scan" "memory" (( -- )) }
475     { "size" "memory" (( obj -- n )) }
476     { "die" "kernel" (( -- )) }
477     { "(fopen)" "io.streams.c" (( path mode -- alien )) }
478     { "fgetc" "io.streams.c" (( alien -- ch/f )) }
479     { "fread" "io.streams.c" (( n alien -- str/f )) }
480     { "fputc" "io.streams.c" (( ch alien -- )) }
481     { "fwrite" "io.streams.c" (( string alien -- )) }
482     { "fflush" "io.streams.c" (( alien -- )) }
483     { "fseek" "io.streams.c" (( alien offset whence -- )) }
484     { "fclose" "io.streams.c" (( alien -- )) }
485     { "<wrapper>" "kernel" (( obj -- wrapper )) }
486     { "(clone)" "kernel" (( obj -- newobj )) }
487     { "<string>" "strings" (( n ch -- string )) }
488     { "array>quotation" "quotations.private" (( array -- quot )) }
489     { "quotation-xt" "quotations" (( quot -- xt )) }
490     { "<tuple>" "classes.tuple.private" (( layout -- tuple )) }
491     { "profiling" "tools.profiler.private" (( ? -- )) }
492     { "become" "kernel.private" (( old new -- )) }
493     { "(sleep)" "threads.private" (( us -- )) }
494     { "<tuple-boa>" "classes.tuple.private" (( ... layout -- tuple )) }
495     { "callstack>array" "kernel" (( callstack -- array )) }
496     { "innermost-frame-quot" "kernel.private" (( callstack -- quot )) }
497     { "innermost-frame-scan" "kernel.private" (( callstack -- n )) }
498     { "set-innermost-frame-quot" "kernel.private" (( n callstack -- )) }
499     { "call-clear" "kernel" (( quot -- )) }
500     { "resize-byte-array" "byte-arrays" (( n byte-array -- newbyte-array )) }
501     { "dll-valid?" "alien" (( dll -- ? )) }
502     { "unimplemented" "kernel.private" (( -- * )) }
503     { "gc-reset" "memory" (( -- )) }
504     { "jit-compile" "quotations" (( quot -- )) }
505     { "load-locals" "locals.backend" (( ... n -- )) }
506     { "check-datastack" "kernel.private" (( array in# out# -- ? )) }
507     { "inline-cache-miss" "generic.single.private" (( generic methods index cache -- )) }
508     { "mega-cache-miss" "generic.single.private" (( methods index cache -- method )) }
509     { "lookup-method" "generic.single.private" (( object methods -- method )) }
510     { "reset-dispatch-stats" "generic.single" (( -- )) }
511     { "dispatch-stats" "generic.single" (( -- stats )) }
512     { "reset-inline-cache-stats" "generic.single" (( -- )) }
513     { "inline-cache-stats" "generic.single" (( -- stats )) }
514     { "optimized?" "words" (( word -- ? )) }
515 } [ [ first3 ] dip swap make-primitive ] each-index
516
517 ! Bump build number
518 "build" "kernel" create build 1 + [ ] curry (( -- n )) define-declared