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