1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays accessors io.backend io.streams.c init fry
4 namespaces math make assocs kernel parser parser.notes lexer
5 strings.parser vocabs sequences sequences.deep sequences.private
6 words memory kernel.private continuations io vocabs.loader
7 system strings sets vectors quotations byte-arrays sorting
8 compiler.units definitions generic generic.standard
9 generic.single tools.deploy.config combinators classes
10 classes.builtin slots.private grouping command-line ;
11 QUALIFIED: bootstrap.stage2
12 QUALIFIED: compiler.crossref
13 QUALIFIED: compiler.errors
14 QUALIFIED: continuations
15 QUALIFIED: definitions
18 QUALIFIED: source-files
19 QUALIFIED: source-files.errors
21 IN: tools.deploy.shaker
23 ! This file is some hairy shit.
25 : add-command-line-hook ( -- )
26 [ (command-line) command-line set-global ] "command-line"
27 startup-hooks get set-at ;
29 : strip-startup-hooks ( -- )
30 "Stripping startup hooks" show
38 [ startup-hooks get delete-at ] each
40 "threads" startup-hooks get delete-at
43 "io.thread" startup-hooks get delete-at
46 "io.files" startup-hooks get delete-at
47 "io.backend" startup-hooks get delete-at
48 "io.thread" startup-hooks get delete-at
56 } [ startup-hooks get delete-at ] each
59 : strip-debugger ( -- )
60 strip-debugger? "debugger" vocab and [
61 "Stripping debugger" show
62 "vocab:tools/deploy/shaker/strip-debugger.factor"
68 "Stripping manual memory management debug code" show
69 "vocab:tools/deploy/shaker/strip-libc.factor"
73 : strip-destructors ( -- )
74 "Stripping destructor debug code" show
75 "vocab:tools/deploy/shaker/strip-destructors.factor"
79 "Stripping stack effect checking from call( and execute(" show
80 "vocab:tools/deploy/shaker/strip-call.factor" run-file ;
84 "Stripping unused Cocoa methods" show
85 "vocab:tools/deploy/shaker/strip-cocoa.factor"
89 : strip-specialized-arrays ( -- )
90 strip-dictionary? "specialized-arrays" vocab and [
91 "Stripping specialized arrays" show
92 "vocab:tools/deploy/shaker/strip-specialized-arrays.factor"
96 : strip-word-names ( words -- )
97 "Stripping word names" show
98 [ f >>name f >>vocabulary drop ] each ;
100 : strip-word-defs ( words -- )
101 "Stripping symbolic word definitions" show
102 [ "no-def-strip" word-prop not ] filter
103 [ [ ] >>def drop ] each ;
105 : sift-assoc ( assoc -- assoc' ) [ nip ] assoc-filter ;
107 : strip-word-props ( stripped-props words -- )
108 "Stripping word properties" show
112 [ drop _ member? not ] assoc-filter sift-assoc
118 [ [ _ [ ] cache ] map ] change-props drop
122 : stripped-word-props ( -- seq )
130 "compiled-generic-uses"
139 "default-output-classes"
172 "predicate-definition"
212 deploy-c-types? get [
213 { "c-type" "struct-slots" "struct-align" } %
217 : strip-words ( props -- )
219 deploy-word-props? get [ 2dup strip-word-props ] unless
220 deploy-word-defs? get [ dup strip-word-defs ] unless
221 strip-word-names? [ dup strip-word-names strip-stack-traces ] when
224 : compiler-classes ( -- seq )
225 { "compiler" "stack-checker" }
226 [ child-vocabs [ words ] map concat [ class? ] filter ]
229 : prune-decision-tree ( tree classes -- )
230 [ tuple class>type ] 2dip '[
236 [ drop _ key? not ] assoc-filter
244 : strip-compiler-classes ( -- )
246 "Stripping compiler classes" show
247 [ single-generic? ] instances
248 compiler-classes '[ "decision-tree" word-prop _ prune-decision-tree ] each
251 : recursive-subst ( seq old new -- )
256 { [ 3dup drop eq? ] [ 2nip ] }
257 ! recurse into arrays
258 { [ pick array? ] [ [ dup ] 2dip recursive-subst ] }
259 ! otherwise do nothing
264 : strip-default-method ( generic new-default -- )
266 [ "decision-tree" word-prop ]
267 [ "default-method" word-prop ] bi
271 : new-default-method ( -- gensym )
272 [ [ "No method" throw ] (( -- * )) define-temp ] with-compilation-unit ;
274 : strip-default-methods ( -- )
275 ! In a development image, each generic has its own default method.
276 ! This gives better error messages for runtime type errors, but
277 ! takes up space. For deployment we merge them all together.
279 "Stripping default methods" show
280 [ single-generic? ] instances
281 new-default-method '[ _ strip-default-method ] each
284 : strip-vocab-globals ( except names -- words )
285 [ child-vocabs [ words ] map concat ] map concat
286 swap [ first2 lookup ] map sift diff ;
288 : stripped-globals ( -- seq )
290 "inspector-hook" "inspector" lookup ,
294 continuations:error-continuation
295 continuations:error-thread
296 continuations:restarts
298 source-files:source-files
304 "io-thread" "io.thread" lookup ,
306 "disposables" "destructors" lookup ,
308 "functor-words" "functors.backend" lookup ,
311 "initial-thread" "threads" lookup ,
314 strip-io? [ io-backend , ] when
324 } strip-vocab-globals %
327 "libraries" "alien" lookup ,
329 { { "yield-hook" "compiler.utilities" } }
330 { "cpu" "compiler" } strip-vocab-globals %
335 next-method-quot-cache
340 classes-intersect-cache
344 compiler.crossref:compiled-crossref
345 compiler.crossref:compiled-generic-crossref
347 compiler.errors:compiler-errors
351 source-files.errors:error-types
352 source-files.errors:error-observers
354 vocabs:load-vocab-hook
355 vocabs:vocab-observers
360 { } { "layouts" } strip-vocab-globals %
362 { } { "math.partial-dispatch" } strip-vocab-globals %
364 { } { "math.vectors.simd" } strip-vocab-globals %
366 { } { "peg" } strip-vocab-globals %
370 { } { "prettyprint.config" } strip-vocab-globals %
375 compiler.errors:compiler-errors
376 continuations:thread-error-hook
380 "ui-error-hook" "ui.gadgets.worlds" lookup ,
384 deploy-c-types? get [
385 "c-types" "alien.c-types" lookup ,
388 "windows-messages" "windows.messages" lookup [ , ] when*
391 : strip-globals ( stripped-globals -- )
393 "Stripping globals" show
395 '[ drop _ member? not ] assoc-filter
396 [ drop string? not ] assoc-filter ! strip CLI args
403 deploy-io get 3 = os windows? not and
407 "io.streams.c" forget-vocab
408 "io-thread-running?" "io.thread" lookup [
411 ] with-compilation-unit
414 : compress ( pred post-process string -- )
415 "Compressing " prepend show
416 [ instances dup H{ } clone [ [ ] cache ] curry map ] dip call
419 : compress-object? ( obj -- ? )
421 { [ dup array? ] [ empty? ] }
422 { [ dup byte-array? ] [ drop t ] }
423 { [ dup string? ] [ drop t ] }
424 { [ dup wrapper? ] [ drop t ] }
428 : compress-objects ( -- )
429 [ compress-object? ] [ ] "objects" compress ;
431 : remain-compiled ( old new -- old new )
432 ! Quotations which were formerly compiled must remain
435 2dup [ quot-compiled? ] [ quot-compiled? not ] bi* and
436 [ nip jit-compile ] [ 2drop ] if
439 : compress-quotations ( -- )
440 [ quotation? ] [ remain-compiled ] "quotations" compress
441 [ quotation? ] instances [ f >>cached-effect f >>cache-counter drop ] each ;
445 : [:c] ( -- word ) ":c" "debugger" lookup ;
447 : [print-error] ( -- word ) "print-error" "debugger" lookup ;
449 : deploy-boot-quot ( word -- )
452 startup-hooks get values concat %
453 strip-debugger? [ , ] [
454 ! Don't reference 'try' directly since we don't want
455 ! to pull in the debugger and prettyprinter into every
460 [ _ execute( obj -- ) ] [
461 _ execute( obj -- ) nl
466 strip-io? [ [ flush ] % ] unless
471 : startup-stripper ( -- )
473 f output-stream set-global ;
475 : next-method* ( method -- quot )
476 [ "method-class" word-prop ]
477 [ "method-generic" word-prop ] bi
480 : calls-next-method? ( method -- ? )
481 def>> flatten \ (call-next-method) swap member-eq? ;
483 : compute-next-methods ( -- )
484 [ standard-generic? ] instances [
485 "methods" word-prop values [ calls-next-method? ] filter
486 [ dup next-method* "next-method" set-word-prop ] each
488 "vocab:tools/deploy/shaker/next-methods.factor" run-file ;
490 : (clear-megamorphic-cache) ( i array -- )
491 ! Can't do any dispatch while clearing caches since that
492 ! might leave them in an inconsistent state.
494 2dup [ f ] 2dip set-array-nth
495 [ 1 + ] dip (clear-megamorphic-cache)
498 : clear-megamorphic-cache ( array -- )
499 [ 0 ] dip (clear-megamorphic-cache) ;
501 : find-megamorphic-caches ( -- seq )
502 "Finding megamorphic caches" show
503 [ standard-generic? ] instances [ def>> third ] map ;
505 : clear-megamorphic-caches ( cache -- )
506 "Clearing megamorphic caches" show
507 [ clear-megamorphic-cache ] each ;
516 strip-specialized-arrays
519 add-command-line-hook
521 strip-default-methods
522 strip-compiler-classes
523 f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
524 deploy-vocab get vocab-main deploy-boot-quot
525 find-megamorphic-caches
527 stripped-globals strip-globals
531 clear-megamorphic-caches ;
533 : deploy-error-handler ( quot -- )
536 [ error-continuation get call>> callstack>array die 1 exit ]
537 ! Don't reference these words literally, if we're stripping the
538 ! debugger out we don't want to load the prettyprinter at all
539 [ [:c] execute( -- ) nl [print-error] execute( error -- ) flush ] if
543 : (deploy) ( final-image vocab config -- )
544 #! Does the actual work of a deployment in the slave
552 "ui.debugger" require
556 deploy-vocab get require
557 deploy-vocab get vocab-main [
558 "Vocabulary has no MAIN: word." print flush 1 exit
561 "Saving final image" show
563 ] deploy-error-handler
569 "Deploying " write dup write "..." print
570 "deploy-config" get parse-file first