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