Merge branch 'master' of git://factorcode.org/git/factor

db4
Joe Groff 2008-12-03 09:51:47 -08:00
commit d2599b3633
1385 changed files with 22264 additions and 10688 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ temp
logs logs
work work
build-support/wordsize build-support/wordsize
*.bak

View File

@ -161,7 +161,7 @@ factor: $(DLL_OBJS) $(EXE_OBJS)
clean: clean:
rm -f vm/*.o rm -f vm/*.o
rm -f factor*.dll libfactor*.* rm -f factor*.dll libfactor.{a,so,dylib}
vm/resources.o: vm/resources.o:
$(WINDRES) vm/factor.rs vm/resources.o $(WINDRES) vm/factor.rs vm/resources.o

View File

@ -43,13 +43,10 @@ Compilation will yield an executable named 'factor' on Unix,
For X11 support, you need recent development libraries for libc, For X11 support, you need recent development libraries for libc,
Freetype, X11, OpenGL and GLUT. On a Debian-derived Linux distribution Freetype, X11, OpenGL and GLUT. On a Debian-derived Linux distribution
(like Ubuntu), you can use the line (like Ubuntu), you can use the following line to grab everything:
sudo apt-get install libc6-dev libfreetype6-dev libx11-dev glutg3-dev sudo apt-get install libc6-dev libfreetype6-dev libx11-dev glutg3-dev
to grab everything (if you're on a non-debian-derived distro please tell
us what the equivalent command is on there and it can be added).
* Bootstrapping the Factor image * Bootstrapping the Factor image
Once you have compiled the Factor runtime, you must bootstrap the Factor Once you have compiled the Factor runtime, you must bootstrap the Factor

View File

@ -5,7 +5,7 @@ HELP: alarm
{ $class-description "An alarm. Can be passed to " { $link cancel-alarm } "." } ; { $class-description "An alarm. Can be passed to " { $link cancel-alarm } "." } ;
HELP: add-alarm HELP: add-alarm
{ $values { "quot" quotation } { "time" timestamp } { "frequency" "a " { $link duration } " or " { $link f } } { "alarm" alarm } } { $values { "quot" quotation } { "time" timestamp } { "frequency" { $maybe duration } } { "alarm" alarm } }
{ $description "Creates and registers an alarm. If " { $snippet "frequency" } " is " { $link f } ", this will be a one-time alarm, otherwise it will fire with the given frequency. The quotation will be called from the alarm thread." } ; { $description "Creates and registers an alarm. If " { $snippet "frequency" } " is " { $link f } ", this will be a one-time alarm, otherwise it will fire with the given frequency. The quotation will be called from the alarm thread." } ;
HELP: later HELP: later

View File

@ -35,7 +35,7 @@ ERROR: bad-alarm-frequency frequency ;
[ time>> ] dip before=? ; [ time>> ] dip before=? ;
: reschedule-alarm ( alarm -- ) : reschedule-alarm ( alarm -- )
dup [ swap interval>> time+ ] change-time register-alarm ; dup [ swap interval>> time+ now max ] change-time register-alarm ;
: call-alarm ( alarm -- ) : call-alarm ( alarm -- )
[ entry>> box> drop ] [ entry>> box> drop ]

View File

@ -16,7 +16,7 @@ HELP: ALIAS:
} }
} ; } ;
ARTICLE: "alias" "Alias" ARTICLE: "alias" "Word aliasing"
"The " { $vocab-link "alias" } " vocabulary implements a way to make many different names for the same word. Although creating new names for words is generally frowned upon, aliases are useful for the Win32 API and other cases where words need to be renamed for symmetry." $nl "The " { $vocab-link "alias" } " vocabulary implements a way to make many different names for the same word. Although creating new names for words is generally frowned upon, aliases are useful for the Win32 API and other cases where words need to be renamed for symmetry." $nl
"Make a new word that aliases another word:" "Make a new word that aliases another word:"
{ $subsection define-alias } { $subsection define-alias }

View File

@ -1,69 +1,7 @@
IN: alien.arrays IN: alien.arrays
USING: help.syntax help.markup byte-arrays alien.c-types ; USING: help.syntax help.markup byte-arrays alien.c-types ;
ARTICLE: "c-arrays-factor" "Converting C arrays to and from Factor arrays"
"Each primitive C type has a pair of words, " { $snippet ">" { $emphasis "type" } "-array" } " and " { $snippet { $emphasis "type" } "-array>" } ", for converting an array of Factor objects to and from a " { $link byte-array } " of C values. This set of words consists of:"
{ $subsection >c-bool-array }
{ $subsection >c-char-array }
{ $subsection >c-double-array }
{ $subsection >c-float-array }
{ $subsection >c-int-array }
{ $subsection >c-long-array }
{ $subsection >c-longlong-array }
{ $subsection >c-short-array }
{ $subsection >c-uchar-array }
{ $subsection >c-uint-array }
{ $subsection >c-ulong-array }
{ $subsection >c-ulonglong-array }
{ $subsection >c-ushort-array }
{ $subsection >c-void*-array }
{ $subsection c-bool-array> }
{ $subsection c-char-array> }
{ $subsection c-double-array> }
{ $subsection c-float-array> }
{ $subsection c-int-array> }
{ $subsection c-long-array> }
{ $subsection c-longlong-array> }
{ $subsection c-short-array> }
{ $subsection c-uchar-array> }
{ $subsection c-uint-array> }
{ $subsection c-ulong-array> }
{ $subsection c-ulonglong-array> }
{ $subsection c-ushort-array> }
{ $subsection c-void*-array> } ;
ARTICLE: "c-arrays-get/set" "Reading and writing elements in C arrays"
"Each C type has a pair of words, " { $snippet { $emphasis "type" } "-nth" } " and " { $snippet "set-" { $emphasis "type" } "-nth" } ", for reading and writing values of this type stored in an array. This set of words includes but is not limited to:"
{ $subsection char-nth }
{ $subsection set-char-nth }
{ $subsection uchar-nth }
{ $subsection set-uchar-nth }
{ $subsection short-nth }
{ $subsection set-short-nth }
{ $subsection ushort-nth }
{ $subsection set-ushort-nth }
{ $subsection int-nth }
{ $subsection set-int-nth }
{ $subsection uint-nth }
{ $subsection set-uint-nth }
{ $subsection long-nth }
{ $subsection set-long-nth }
{ $subsection ulong-nth }
{ $subsection set-ulong-nth }
{ $subsection longlong-nth }
{ $subsection set-longlong-nth }
{ $subsection ulonglong-nth }
{ $subsection set-ulonglong-nth }
{ $subsection float-nth }
{ $subsection set-float-nth }
{ $subsection double-nth }
{ $subsection set-double-nth }
{ $subsection void*-nth }
{ $subsection set-void*-nth } ;
ARTICLE: "c-arrays" "C arrays" ARTICLE: "c-arrays" "C arrays"
"C arrays are allocated in the same manner as other C data; see " { $link "c-byte-arrays" } " and " { $link "malloc" } "." "C arrays are allocated in the same manner as other C data; see " { $link "c-byte-arrays" } " and " { $link "malloc" } "."
$nl $nl
"C type specifiers for array types are documented in " { $link "c-types-specs" } "." "C type specifiers for array types are documented in " { $link "c-types-specs" } "." ;
{ $subsection "c-arrays-factor" }
{ $subsection "c-arrays-get/set" } ;

View File

@ -8,6 +8,8 @@ UNION: value-type array struct-type ;
M: array c-type ; M: array c-type ;
M: array c-type-class drop object ;
M: array heap-size unclip heap-size [ * ] reduce ; M: array heap-size unclip heap-size [ * ] reduce ;
M: array c-type-align first c-type-align ; M: array c-type-align first c-type-align ;

View File

@ -39,12 +39,12 @@ HELP: byte-length
{ $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ; { $contract "Outputs the size of the byte array or float array data in bytes as presented to the C library interface." } ;
HELP: c-getter HELP: c-getter
{ $values { "name" string } { "quot" "a quotation with stack effect " { $snippet "( c-ptr n -- obj )" } } } { $values { "name" string } { "quot" { $quotation "( c-ptr n -- obj )" } } }
{ $description "Outputs a quotation which reads values of this C type from a C structure." } { $description "Outputs a quotation which reads values of this C type from a C structure." }
{ $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ; { $errors "Throws a " { $link no-c-type } " error if the type does not exist." } ;
HELP: c-setter HELP: c-setter
{ $values { "name" string } { "quot" "a quotation with stack effect " { $snippet "( obj c-ptr n -- )" } } } { $values { "name" string } { "quot" { $quotation "( obj c-ptr n -- )" } } }
{ $description "Outputs a quotation which writes values of this C type to a C structure." } { $description "Outputs a quotation which writes values of this C type to a C structure." }
{ $errors "Throws an error if the type does not exist." } ; { $errors "Throws an error if the type does not exist." } ;
@ -89,16 +89,6 @@ HELP: malloc-byte-array
{ $warning "Don't forget to deallocate the memory with a call to " { $link free } "." } { $warning "Don't forget to deallocate the memory with a call to " { $link free } "." }
{ $errors "Throws an error if memory allocation fails." } ; { $errors "Throws an error if memory allocation fails." } ;
HELP: define-nth
{ $values { "name" "a word name" } { "vocab" "a vocabulary name" } }
{ $description "Defines a word " { $snippet { $emphasis "name" } "-nth" } " with stack effect " { $snippet "( n c-ptr -- value )" } " for reading the value with C type " { $snippet "name" } " stored at an alien pointer, displaced by a multiple of the C type's size." }
{ $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ;
HELP: define-set-nth
{ $values { "name" "a word name" } { "vocab" "a vocabulary name" } }
{ $description "Defines a word " { $snippet "set-" { $emphasis "name" } "-nth" } " with stack effect " { $snippet "( value n c-ptr -- )" } " for writing the value with C type " { $snippet "name" } " to an alien pointer, displaced by a multiple of the C type's size." }
{ $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ;
HELP: box-parameter HELP: box-parameter
{ $values { "n" integer } { "ctype" string } } { $values { "n" integer } { "ctype" string } }
{ $description "Generates code for converting a C value stored at offset " { $snippet "n" } " from the top of the stack into a Factor object to be pushed on the data stack." } { $description "Generates code for converting a C value stored at offset " { $snippet "n" } " from the top of the stack into a Factor object to be pushed on the data stack." }
@ -115,12 +105,12 @@ HELP: unbox-return
{ $notes "This is an internal word used by the compiler when compiling callbacks." } ; { $notes "This is an internal word used by the compiler when compiling callbacks." } ;
HELP: define-deref HELP: define-deref
{ $values { "name" "a word name" } { "vocab" "a vocabulary name" } } { $values { "name" "a word name" } }
{ $description "Defines a word " { $snippet "*name" } " with stack effect " { $snippet "( c-ptr -- value )" } " for reading a value with C type " { $snippet "name" } " stored at an alien pointer." } { $description "Defines a word " { $snippet "*name" } " with stack effect " { $snippet "( c-ptr -- value )" } " for reading a value with C type " { $snippet "name" } " stored at an alien pointer." }
{ $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ; { $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ;
HELP: define-out HELP: define-out
{ $values { "name" "a word name" } { "vocab" "a vocabulary name" } } { $values { "name" "a word name" } }
{ $description "Defines a word " { $snippet "<" { $emphasis "name" } ">" } " with stack effect " { $snippet "( value -- array )" } ". This word allocates a byte array large enough to hold a value with C type " { $snippet "name" } ", and writes the value at the top of the stack to the array." } { $description "Defines a word " { $snippet "<" { $emphasis "name" } ">" } " with stack effect " { $snippet "( value -- array )" } ". This word allocates a byte array large enough to hold a value with C type " { $snippet "name" } ", and writes the value at the top of the stack to the array." }
{ $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ; { $notes "This is an internal word called when defining C types, there is no need to call it on your own." } ;
@ -230,9 +220,7 @@ $nl
"You can copy a range of bytes from memory into a byte array:" "You can copy a range of bytes from memory into a byte array:"
{ $subsection memory>byte-array } { $subsection memory>byte-array }
"You can copy a byte array to memory unsafely:" "You can copy a byte array to memory unsafely:"
{ $subsection byte-array>memory } { $subsection byte-array>memory } ;
"A wrapper for temporarily allocating a block of memory:"
{ $subsection with-malloc } ;
ARTICLE: "c-data" "Passing data between Factor and C" ARTICLE: "c-data" "Passing data between Factor and C"
"Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers." "Two defining characteristics of Factor are dynamic typing and automatic memory management, which are somewhat incompatible with the machine-level data model exposed by C. Factor's C library interface defines its own set of C data types, distinct from Factor language types, together with automatic conversion between Factor values and C types. For example, C integer types must be declared and are fixed-width, whereas Factor supports arbitrary-precision integers."

View File

@ -55,4 +55,6 @@ TYPEDEF: uchar* MyLPBYTE
0 B{ 1 2 3 4 } <displaced-alien> <void*> 0 B{ 1 2 3 4 } <displaced-alien> <void*>
] must-fail ] must-fail
[ t ] [ { t f t } >c-bool-array { 1 0 1 } >c-int-array = ] unit-test os windows? cpu x86.64? and [
[ -2147467259 ] [ 2147500037 <long> *long ] unit-test
] when

View File

@ -4,7 +4,7 @@ USING: byte-arrays arrays assocs kernel kernel.private libc math
namespaces make parser sequences strings words assocs splitting namespaces make parser sequences strings words assocs splitting
math.parser cpu.architecture alien alien.accessors quotations math.parser cpu.architecture alien alien.accessors quotations
layouts system compiler.units io.files io.encodings.binary layouts system compiler.units io.files io.encodings.binary
accessors combinators effects continuations ; accessors combinators effects continuations fry ;
IN: alien.c-types IN: alien.c-types
DEFER: <int> DEFER: <int>
@ -13,13 +13,15 @@ DEFER: *char
: little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
TUPLE: c-type TUPLE: c-type
class
boxer boxer-quot unboxer unboxer-quot boxer boxer-quot unboxer unboxer-quot
getter setter getter setter
reg-class size align stack-align? ; reg-class size align stack-align? ;
: new-c-type ( class -- type ) : new-c-type ( class -- type )
new new
int-regs >>reg-class ; int-regs >>reg-class
object >>class ; inline
: <c-type> ( -- type ) : <c-type> ( -- type )
\ c-type new-c-type ; \ c-type new-c-type ;
@ -50,7 +52,7 @@ GENERIC: c-type ( name -- type ) foldable
: parse-array-type ( name -- array ) : parse-array-type ( name -- array )
"[" split unclip "[" split unclip
>r [ "]" ?tail drop string>number ] map r> prefix ; [ [ "]" ?tail drop string>number ] map ] dip prefix ;
M: string c-type ( name -- type ) M: string c-type ( name -- type )
CHAR: ] over member? [ CHAR: ] over member? [
@ -63,6 +65,12 @@ M: string c-type ( name -- type )
] ?if ] ?if
] if ; ] if ;
GENERIC: c-type-class ( name -- class )
M: c-type c-type-class class>> ;
M: string c-type-class c-type c-type-class ;
GENERIC: c-type-boxer ( name -- boxer ) GENERIC: c-type-boxer ( name -- boxer )
M: c-type c-type-boxer boxer>> ; M: c-type c-type-boxer boxer>> ;
@ -164,7 +172,7 @@ GENERIC: stack-size ( type -- size ) foldable
M: string stack-size c-type stack-size ; M: string stack-size c-type stack-size ;
M: c-type stack-size size>> ; M: c-type stack-size size>> cell align ;
GENERIC: byte-length ( seq -- n ) flushable GENERIC: byte-length ( seq -- n ) flushable
@ -172,12 +180,12 @@ M: byte-array byte-length length ;
: c-getter ( name -- quot ) : c-getter ( name -- quot )
c-type-getter [ c-type-getter [
[ "Cannot read struct fields with type" throw ] [ "Cannot read struct fields with this type" throw ]
] unless* ; ] unless* ;
: c-setter ( name -- quot ) : c-setter ( name -- quot )
c-type-setter [ c-type-setter [
[ "Cannot write struct fields with type" throw ] [ "Cannot write struct fields with this type" throw ]
] unless* ; ] unless* ;
: <c-array> ( n type -- array ) : <c-array> ( n type -- array )
@ -193,36 +201,21 @@ M: byte-array byte-length length ;
1 swap malloc-array ; inline 1 swap malloc-array ; inline
: malloc-byte-array ( byte-array -- alien ) : malloc-byte-array ( byte-array -- alien )
dup length dup malloc [ -rot memcpy ] keep ; dup length [ nip malloc dup ] 2keep memcpy ;
: memory>byte-array ( alien len -- byte-array ) : memory>byte-array ( alien len -- byte-array )
dup <byte-array> [ -rot memcpy ] keep ; [ nip <byte-array> dup ] 2keep memcpy ;
: byte-array>memory ( byte-array base -- ) : byte-array>memory ( byte-array base -- )
swap dup length memcpy ; swap dup length memcpy ;
: (define-nth) ( word type quot -- ) : array-accessor ( type quot -- def )
[ [
\ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi* \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
] [ ] make define-inline ; ] [ ] make ;
: nth-word ( name vocab -- word )
>r "-nth" append r> create ;
: define-nth ( name vocab -- )
dupd nth-word swap dup c-getter (define-nth) ;
: set-nth-word ( name vocab -- word )
>r "set-" swap "-nth" 3append r> create ;
: define-set-nth ( name vocab -- )
dupd set-nth-word swap dup c-setter (define-nth) ;
: typedef ( old new -- ) c-types get set-at ; : typedef ( old new -- ) c-types get set-at ;
: define-c-type ( type name vocab -- )
>r tuck typedef r> [ define-nth ] 2keep define-set-nth ;
TUPLE: long-long-type < c-type ; TUPLE: long-long-type < c-type ;
: <long-long-type> ( -- type ) : <long-long-type> ( -- type )
@ -240,62 +233,34 @@ M: long-long-type box-parameter ( n type -- )
M: long-long-type box-return ( type -- ) M: long-long-type box-return ( type -- )
f swap box-parameter ; f swap box-parameter ;
: define-deref ( name vocab -- ) : define-deref ( name -- )
>r dup CHAR: * prefix r> create [ CHAR: * prefix "alien.c-types" create ]
swap c-getter 0 prefix define-inline ; [ c-getter 0 prefix ] bi
define-inline ;
: define-out ( name vocab -- ) : define-out ( name -- )
over [ <c-object> tuck 0 ] over c-setter append swap [ "alien.c-types" constructor-word ]
>r >r constructor-word r> r> prefix define-inline ; [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ]
bi define-inline ;
: c-bool> ( int -- ? ) : c-bool> ( int -- ? )
zero? not ; zero? not ;
: >c-array ( seq type word -- byte-array )
[ [ dup length ] dip <c-array> ] dip
[ [ execute ] 2curry each-index ] 2keep drop ; inline
: >c-array-quot ( type vocab -- quot )
dupd set-nth-word [ >c-array ] 2curry ;
: to-array-word ( name vocab -- word )
>r ">c-" swap "-array" 3append r> create ;
: define-to-array ( type vocab -- )
[ to-array-word ] 2keep >c-array-quot
(( array -- byte-array )) define-declared ;
: c-array>quot ( type vocab -- quot )
[
\ swap ,
nth-word 1quotation ,
[ curry map ] %
] [ ] make ;
: from-array-word ( name vocab -- word )
>r "c-" swap "-array>" 3append r> create ;
: define-from-array ( type vocab -- )
[ from-array-word ] 2keep c-array>quot
(( c-ptr n -- array )) define-declared ;
: define-primitive-type ( type name -- ) : define-primitive-type ( type name -- )
"alien.c-types" [ typedef ]
{
[ define-c-type ]
[ define-deref ] [ define-deref ]
[ define-to-array ]
[ define-from-array ]
[ define-out ] [ define-out ]
} 2cleave ; tri ;
: expand-constants ( c-type -- c-type' ) : expand-constants ( c-type -- c-type' )
dup array? [ dup array? [
unclip >r [ unclip [
[
dup word? [ dup word? [
def>> { } swap with-datastack first def>> { } swap with-datastack first
] when ] when
] map r> prefix ] map
] dip prefix
] when ; ] when ;
: malloc-file-contents ( path -- alien len ) : malloc-file-contents ( path -- alien len )
@ -304,8 +269,20 @@ M: long-long-type box-return ( type -- )
: if-void ( type true false -- ) : if-void ( type true false -- )
pick "void" = [ drop nip call ] [ nip call ] if ; inline pick "void" = [ drop nip call ] [ nip call ] if ; inline
: primitive-types
{
"char" "uchar"
"short" "ushort"
"int" "uint"
"long" "ulong"
"longlong" "ulonglong"
"float" "double"
"void*" "bool"
} ;
[ [
<c-type> <c-type>
c-ptr >>class
[ alien-cell ] >>getter [ alien-cell ] >>getter
[ set-alien-cell ] >>setter [ set-alien-cell ] >>setter
bootstrap-cell >>size bootstrap-cell >>size
@ -315,6 +292,7 @@ M: long-long-type box-return ( type -- )
"void*" define-primitive-type "void*" define-primitive-type
<long-long-type> <long-long-type>
integer >>class
[ alien-signed-8 ] >>getter [ alien-signed-8 ] >>getter
[ set-alien-signed-8 ] >>setter [ set-alien-signed-8 ] >>setter
8 >>size 8 >>size
@ -324,6 +302,7 @@ M: long-long-type box-return ( type -- )
"longlong" define-primitive-type "longlong" define-primitive-type
<long-long-type> <long-long-type>
integer >>class
[ alien-unsigned-8 ] >>getter [ alien-unsigned-8 ] >>getter
[ set-alien-unsigned-8 ] >>setter [ set-alien-unsigned-8 ] >>setter
8 >>size 8 >>size
@ -333,6 +312,7 @@ M: long-long-type box-return ( type -- )
"ulonglong" define-primitive-type "ulonglong" define-primitive-type
<c-type> <c-type>
integer >>class
[ alien-signed-cell ] >>getter [ alien-signed-cell ] >>getter
[ set-alien-signed-cell ] >>setter [ set-alien-signed-cell ] >>setter
bootstrap-cell >>size bootstrap-cell >>size
@ -342,6 +322,7 @@ M: long-long-type box-return ( type -- )
"long" define-primitive-type "long" define-primitive-type
<c-type> <c-type>
integer >>class
[ alien-unsigned-cell ] >>getter [ alien-unsigned-cell ] >>getter
[ set-alien-unsigned-cell ] >>setter [ set-alien-unsigned-cell ] >>setter
bootstrap-cell >>size bootstrap-cell >>size
@ -351,6 +332,7 @@ M: long-long-type box-return ( type -- )
"ulong" define-primitive-type "ulong" define-primitive-type
<c-type> <c-type>
integer >>class
[ alien-signed-4 ] >>getter [ alien-signed-4 ] >>getter
[ set-alien-signed-4 ] >>setter [ set-alien-signed-4 ] >>setter
4 >>size 4 >>size
@ -360,6 +342,7 @@ M: long-long-type box-return ( type -- )
"int" define-primitive-type "int" define-primitive-type
<c-type> <c-type>
integer >>class
[ alien-unsigned-4 ] >>getter [ alien-unsigned-4 ] >>getter
[ set-alien-unsigned-4 ] >>setter [ set-alien-unsigned-4 ] >>setter
4 >>size 4 >>size
@ -369,6 +352,7 @@ M: long-long-type box-return ( type -- )
"uint" define-primitive-type "uint" define-primitive-type
<c-type> <c-type>
fixnum >>class
[ alien-signed-2 ] >>getter [ alien-signed-2 ] >>getter
[ set-alien-signed-2 ] >>setter [ set-alien-signed-2 ] >>setter
2 >>size 2 >>size
@ -378,6 +362,7 @@ M: long-long-type box-return ( type -- )
"short" define-primitive-type "short" define-primitive-type
<c-type> <c-type>
fixnum >>class
[ alien-unsigned-2 ] >>getter [ alien-unsigned-2 ] >>getter
[ set-alien-unsigned-2 ] >>setter [ set-alien-unsigned-2 ] >>setter
2 >>size 2 >>size
@ -387,6 +372,7 @@ M: long-long-type box-return ( type -- )
"ushort" define-primitive-type "ushort" define-primitive-type
<c-type> <c-type>
fixnum >>class
[ alien-signed-1 ] >>getter [ alien-signed-1 ] >>getter
[ set-alien-signed-1 ] >>setter [ set-alien-signed-1 ] >>setter
1 >>size 1 >>size
@ -396,6 +382,7 @@ M: long-long-type box-return ( type -- )
"char" define-primitive-type "char" define-primitive-type
<c-type> <c-type>
fixnum >>class
[ alien-unsigned-1 ] >>getter [ alien-unsigned-1 ] >>getter
[ set-alien-unsigned-1 ] >>setter [ set-alien-unsigned-1 ] >>setter
1 >>size 1 >>size
@ -414,6 +401,7 @@ M: long-long-type box-return ( type -- )
"bool" define-primitive-type "bool" define-primitive-type
<c-type> <c-type>
float >>class
[ alien-float ] >>getter [ alien-float ] >>getter
[ [ >float ] 2dip set-alien-float ] >>setter [ [ >float ] 2dip set-alien-float ] >>setter
4 >>size 4 >>size
@ -425,6 +413,7 @@ M: long-long-type box-return ( type -- )
"float" define-primitive-type "float" define-primitive-type
<c-type> <c-type>
float >>class
[ alien-double ] >>getter [ alien-double ] >>getter
[ [ >float ] 2dip set-alien-double ] >>setter [ [ >float ] 2dip set-alien-double ] >>setter
8 >>size 8 >>size
@ -436,6 +425,6 @@ M: long-long-type box-return ( type -- )
"double" define-primitive-type "double" define-primitive-type
"long" "ptrdiff_t" typedef "long" "ptrdiff_t" typedef
"long" "intptr_t" typedef
"ulong" "size_t" typedef "ulong" "size_t" typedef
] with-compilation-unit ] with-compilation-unit

View File

@ -0,0 +1,19 @@
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types arrays assocs effects grouping kernel
parser sequences splitting words fry locals ;
IN: alien.parser
: parse-arglist ( parameters return -- types effect )
[ 2 group unzip [ "," ?tail drop ] map ]
[ [ { } ] [ 1array ] if-void ]
bi* <effect> ;
: function-quot ( return library function types -- quot )
'[ _ _ _ _ alien-invoke ] ;
:: define-function ( return library function parameters -- )
function create-in dup reset-generic
return library function
parameters return parse-arglist [ function-quot ] dip
define-declared ;

View File

@ -3,13 +3,13 @@
USING: arrays sequences kernel accessors math alien.accessors USING: arrays sequences kernel accessors math alien.accessors
alien.c-types byte-arrays words io io.encodings alien.c-types byte-arrays words io io.encodings
io.streams.byte-array io.streams.memory io.encodings.utf8 io.streams.byte-array io.streams.memory io.encodings.utf8
io.encodings.utf16 system alien strings cpu.architecture ; io.encodings.utf16 system alien strings cpu.architecture fry ;
IN: alien.strings IN: alien.strings
GENERIC# alien>string 1 ( c-ptr encoding -- string/f ) GENERIC# alien>string 1 ( c-ptr encoding -- string/f )
M: c-ptr alien>string M: c-ptr alien>string
>r <memory-stream> r> <decoder> [ <memory-stream> ] [ <decoder> ] bi*
"\0" swap stream-read-until drop ; "\0" swap stream-read-until drop ;
M: f alien>string M: f alien>string
@ -40,6 +40,9 @@ PREDICATE: string-type < pair
M: string-type c-type ; M: string-type c-type ;
M: string-type c-type-class
drop object ;
M: string-type heap-size M: string-type heap-size
drop "void*" heap-size ; drop "void*" heap-size ;
@ -74,10 +77,10 @@ M: string-type c-type-unboxer
drop "void*" c-type-unboxer ; drop "void*" c-type-unboxer ;
M: string-type c-type-boxer-quot M: string-type c-type-boxer-quot
second [ alien>string ] curry [ ] like ; second '[ _ alien>string ] ;
M: string-type c-type-unboxer-quot M: string-type c-type-unboxer-quot
second [ string>alien ] curry [ ] like ; second '[ _ string>alien ] ;
M: string-type c-type-getter M: string-type c-type-getter
drop [ alien-cell ] ; drop [ alien-cell ] ;

View File

@ -29,10 +29,10 @@ PREDICATE: slot-writer < word "writing" word-prop >boolean ;
writer>> swap "writing" set-word-prop ; writer>> swap "writing" set-word-prop ;
: reader-word ( class name vocab -- word ) : reader-word ( class name vocab -- word )
>r >r "-" r> 3append r> create ; [ "-" swap 3append ] dip create ;
: writer-word ( class name vocab -- word ) : writer-word ( class name vocab -- word )
>r [ swap "set-" % % "-" % % ] "" make r> create ; [ [ swap "set-" % % "-" % % ] "" make ] dip create ;
: <field-spec> ( struct-name vocab type field-name -- spec ) : <field-spec> ( struct-name vocab type field-name -- spec )
field-spec new field-spec new
@ -52,25 +52,21 @@ PREDICATE: slot-writer < word "writing" word-prop >boolean ;
[ (>>offset) ] [ type>> heap-size + ] 2bi [ (>>offset) ] [ type>> heap-size + ] 2bi
] reduce ; ] reduce ;
: define-struct-slot-word ( spec word quot -- ) : define-struct-slot-word ( word quot spec -- )
rot offset>> prefix define-inline ; offset>> prefix define-inline ;
: define-getter ( type spec -- ) : define-getter ( type spec -- )
[ set-reader-props ] keep [ set-reader-props ] keep
[ ]
[ reader>> ] [ reader>> ]
[ [
type>> type>>
[ c-getter ] [ c-type-boxer-quot ] bi append [ c-getter ] [ c-type-boxer-quot ] bi append
] tri ]
define-struct-slot-word ; [ ] tri define-struct-slot-word ;
: define-setter ( type spec -- ) : define-setter ( type spec -- )
[ set-writer-props ] keep [ set-writer-props ] keep
[ ] [ writer>> ] [ type>> c-setter ] [ ] tri define-struct-slot-word ;
[ writer>> ]
[ type>> c-setter ] tri
define-struct-slot-word ;
: define-field ( type spec -- ) : define-field ( type spec -- )
[ define-getter ] [ define-setter ] 2bi ; [ define-getter ] [ define-setter ] 2bi ;

View File

@ -38,7 +38,7 @@ C-UNION: barx
[ 120 ] [ "barx" heap-size ] unit-test [ 120 ] [ "barx" heap-size ] unit-test
"help" vocab [ "help" vocab [
"help" "help" lookup "help" set "print-topic" "help" lookup "help" set
[ ] [ \ foox-x "help" get execute ] unit-test [ ] [ \ foox-x "help" get execute ] unit-test
[ ] [ \ set-foox-x "help" get execute ] unit-test [ ] [ \ set-foox-x "help" get execute ] unit-test
] when ] when

View File

@ -1,58 +1,63 @@
! Copyright (C) 2004, 2008 Slava Pestov. ! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays generic hashtables kernel kernel.private USING: accessors arrays generic hashtables kernel kernel.private
math namespaces parser sequences strings words libc math namespaces parser sequences strings words libc fry
alien.c-types alien.structs.fields cpu.architecture ; alien.c-types alien.structs.fields cpu.architecture ;
IN: alien.structs IN: alien.structs
: if-value-structs? ( ctype true false -- )
value-structs?
[ drop call ] [ >r 2drop "void*" r> call ] if ; inline
TUPLE: struct-type size align fields ; TUPLE: struct-type size align fields ;
M: struct-type heap-size size>> ; M: struct-type heap-size size>> ;
M: struct-type c-type-class drop object ;
M: struct-type c-type-align align>> ; M: struct-type c-type-align align>> ;
M: struct-type c-type-stack-align? drop f ; M: struct-type c-type-stack-align? drop f ;
M: struct-type unbox-parameter : if-value-struct ( ctype true false -- )
[ %unbox-struct ] [ unbox-parameter ] if-value-structs? ; [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline
M: struct-type unbox-return M: struct-type unbox-parameter
f swap %unbox-struct ; [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
M: struct-type box-parameter M: struct-type box-parameter
[ %box-struct ] [ box-parameter ] if-value-structs? ; [ %box-large-struct ] [ box-parameter ] if-value-struct ;
: if-small-struct ( c-type true false -- ? )
[ dup struct-small-enough? ] 2dip '[ f swap @ ] if ; inline
M: struct-type unbox-return
[ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
M: struct-type box-return M: struct-type box-return
f swap %box-struct ; [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
M: struct-type stack-size M: struct-type stack-size
[ heap-size ] [ stack-size ] if-value-structs? ; [ heap-size ] [ stack-size ] if-value-struct ;
: c-struct? ( type -- ? ) (c-type) struct-type? ; : c-struct? ( type -- ? ) (c-type) struct-type? ;
: (define-struct) ( name vocab size align fields -- ) : (define-struct) ( name size align fields -- )
>r [ align ] keep r> [ [ align ] keep ] dip
struct-type boa struct-type boa
-rot define-c-type ; swap typedef ;
: define-struct-early ( name vocab fields -- fields ) : make-fields ( name vocab fields -- fields )
-rot [ rot first2 <field-spec> ] 2curry map ; [ first2 <field-spec> ] with with map ;
: compute-struct-align ( types -- n ) : compute-struct-align ( types -- n )
[ c-type-align ] map supremum ; [ c-type-align ] map supremum ;
: define-struct ( name vocab fields -- ) : define-struct ( name vocab fields -- )
pick >r [
[ 2drop ] [ make-fields ] 3bi
[ struct-offsets ] keep [ struct-offsets ] keep
[ [ type>> ] map compute-struct-align ] keep [ [ type>> ] map compute-struct-align ] keep
[ (define-struct) ] keep [ (define-struct) ] keep
r> [ swap define-field ] curry each ; ] [ 2drop '[ _ swap define-field ] ] 3bi each ;
: define-union ( name vocab members -- ) : define-union ( name members -- )
[ expand-constants ] map [ expand-constants ] map
[ [ heap-size ] map supremum ] keep [ [ heap-size ] map supremum ] keep
compute-struct-align f (define-struct) ; compute-struct-align f (define-struct) ;

View File

@ -1,5 +1,5 @@
IN: alien.syntax IN: alien.syntax
USING: alien alien.c-types alien.structs alien.syntax.private USING: alien alien.c-types alien.parser alien.structs
help.markup help.syntax ; help.markup help.syntax ;
HELP: DLL" HELP: DLL"
@ -54,12 +54,6 @@ HELP: TYPEDEF:
{ $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } " if ." } { $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } " if ." }
{ $notes "This word differs from " { $link typedef } " in that it runs at parse time, to ensure correct ordering of operations when loading source files. Words defined in source files are compiled before top-level forms are run, so if a source file defines C binding words and uses " { $link typedef } ", the type alias won't be available at compile time." } ; { $notes "This word differs from " { $link typedef } " in that it runs at parse time, to ensure correct ordering of operations when loading source files. Words defined in source files are compiled before top-level forms are run, so if a source file defines C binding words and uses " { $link typedef } ", the type alias won't be available at compile time." } ;
HELP: TYPEDEF-IF:
{ $syntax "TYPEDEF-IF: word old new" }
{ $values { "word" "a word with stack effect " { $snippet "( -- ? )" } } { "old" "a C type" } { "new" "a C type" } }
{ $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } " if " { $snippet "word" } " evaluates to a true value." }
{ $notes "This word differs from " { $link typedef } " in that it runs at parse time, to ensure correct ordering of operations when loading source files. Words defined in source files are compiled before top-level forms are run, so if a source file defines C binding words and uses " { $link typedef } ", the type alias won't be available at compile time." } ;
HELP: C-STRUCT: HELP: C-STRUCT:
{ $syntax "C-STRUCT: name pairs... ;" } { $syntax "C-STRUCT: name pairs... ;" }
{ $values { "name" "a new C type name" } { "pairs" "C type / field name string pairs" } } { $values { "name" "a new C type name" } { "pairs" "C type / field name string pairs" } }
@ -88,7 +82,7 @@ HELP: typedef
{ $description "Alises the C type " { $snippet "old" } " under the name " { $snippet "new" } "." } { $description "Alises the C type " { $snippet "old" } " under the name " { $snippet "new" } "." }
{ $notes "Using this word in the same source file which defines C bindings can cause problems, because words are compiled before top-level forms are run. Use the " { $link POSTPONE: TYPEDEF: } " word instead." } ; { $notes "Using this word in the same source file which defines C bindings can cause problems, because words are compiled before top-level forms are run. Use the " { $link POSTPONE: TYPEDEF: } " word instead." } ;
{ POSTPONE: TYPEDEF-IF: POSTPONE: TYPEDEF: typedef } related-words { POSTPONE: TYPEDEF: typedef } related-words
HELP: c-struct? HELP: c-struct?
{ $values { "type" "a string" } { "?" "a boolean" } } { $values { "type" "a string" } { "?" "a boolean" } }

View File

@ -4,35 +4,9 @@ USING: accessors arrays alien alien.c-types alien.structs
alien.arrays alien.strings kernel math namespaces parser alien.arrays alien.strings kernel math namespaces parser
sequences words quotations math.parser splitting grouping sequences words quotations math.parser splitting grouping
effects prettyprint prettyprint.sections prettyprint.backend effects prettyprint prettyprint.sections prettyprint.backend
assocs combinators lexer strings.parser ; assocs combinators lexer strings.parser alien.parser ;
IN: alien.syntax IN: alien.syntax
<PRIVATE
: parse-arglist ( return seq -- types effect )
2 group dup keys swap values [ "," ?tail drop ] map
rot dup "void" = [ drop { } ] [ 1array ] if <effect> ;
: function-quot ( type lib func types -- quot )
[ alien-invoke ] 2curry 2curry ;
: define-function ( return library function parameters -- )
>r pick r> parse-arglist
pick create-in dup reset-generic
>r >r function-quot r> r>
-rot define-declared ;
PRIVATE>
: indirect-quot ( function-ptr-quot return types abi -- quot )
[ alien-indirect ] 3curry compose ;
: define-indirect ( abi return function-ptr-quot function-name parameters -- )
>r pick r> parse-arglist
rot create-in dup reset-generic
>r >r swapd roll indirect-quot r> r>
-rot define-declared ;
: DLL" lexer get skip-blank parse-string dlopen parsed ; parsing : DLL" lexer get skip-blank parse-string dlopen parsed ; parsing
: ALIEN: scan string>number <alien> parsed ; parsing : ALIEN: scan string>number <alien> parsed ; parsing
@ -49,22 +23,16 @@ PRIVATE>
: TYPEDEF: : TYPEDEF:
scan scan typedef ; parsing scan scan typedef ; parsing
: TYPEDEF-IF:
scan-word execute scan scan rot [ typedef ] [ 2drop ] if ; parsing
: C-STRUCT: : C-STRUCT:
scan in get scan in get parse-definition define-struct ; parsing
parse-definition
>r 2dup r> define-struct-early
define-struct ; parsing
: C-UNION: : C-UNION:
scan in get parse-definition define-union ; parsing scan parse-definition define-union ; parsing
: C-ENUM: : C-ENUM:
";" parse-tokens ";" parse-tokens
dup length dup length
[ >r create-in r> 1quotation define ] 2each ; [ [ create-in ] dip 1quotation define ] 2each ;
parsing parsing
M: alien pprint* M: alien pprint*

View File

@ -2,7 +2,7 @@ IN: binary-search
USING: help.markup help.syntax sequences kernel math.order ; USING: help.markup help.syntax sequences kernel math.order ;
HELP: search HELP: search
{ $values { "seq" "a sorted sequence" } { "quot" "a quotation with stack effect " { $snippet "( elt -- <=> )" } } { "i" "an index, or " { $link f } } { "elt" "an element, or " { $link f } } } { $values { "seq" "a sorted sequence" } { "quot" { $quotation "( elt -- <=> )" } } { "i" "an index, or " { $link f } } { "elt" "an element, or " { $link f } } }
{ $description "Performs a binary search on a sequence, calling the quotation to decide whether to end the search (" { $link +eq+ } "), search lower (" { $link +lt+ } ") or search higher (" { $link +gt+ } ")." { $description "Performs a binary search on a sequence, calling the quotation to decide whether to end the search (" { $link +eq+ } "), search lower (" { $link +lt+ } ") or search higher (" { $link +gt+ } ")."
$nl $nl
"If the sequence is non-empty, outputs the index and value of the closest match, which is either an element for which the quotation output " { $link +eq+ } ", or failing that, least element for which the quotation output " { $link +lt+ } "." "If the sequence is non-empty, outputs the index and value of the closest match, which is either an element for which the quotation output " { $link +eq+ } ", or failing that, least element for which the quotation output " { $link +lt+ } "."

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types accessors math alien.accessors kernel USING: alien.c-types accessors math alien.accessors kernel
kernel.private locals sequences sequences.private byte-arrays kernel.private locals sequences sequences.private byte-arrays
parser prettyprint.backend ; parser prettyprint.backend fry ;
IN: bit-arrays IN: bit-arrays
TUPLE: bit-array TUPLE: bit-array
@ -24,9 +24,8 @@ TUPLE: bit-array
: bits>bytes 7 + n>byte ; inline : bits>bytes 7 + n>byte ; inline
: (set-bits) ( bit-array n -- ) : (set-bits) ( bit-array n -- )
[ [ length bits>cells ] keep ] dip [ [ length bits>cells ] keep ] dip swap underlying>>
[ -rot underlying>> set-uint-nth ] 2curry '[ [ _ _ ] dip set-alien-unsigned-4 ] each ; inline
each ; inline
PRIVATE> PRIVATE>
@ -84,9 +83,9 @@ M: bit-array byte-length length 7 + -3 shift ;
] if ; ] if ;
: bit-array>integer ( bit-array -- n ) : bit-array>integer ( bit-array -- n )
0 swap underlying>> [ length ] keep [ 0 swap underlying>> dup length [
uchar-nth swap 8 shift bitor alien-unsigned-1 swap 8 shift bitor
] curry each ; ] with each ;
INSTANCE: bit-array sequence INSTANCE: bit-array sequence

View File

@ -4,7 +4,7 @@ USING: tools.test bit-vectors vectors sequences kernel math ;
[ 0 ] [ 123 <bit-vector> length ] unit-test [ 0 ] [ 123 <bit-vector> length ] unit-test
: do-it : do-it
1234 swap [ >r even? r> push ] curry each ; 1234 swap [ [ even? ] dip push ] curry each ;
[ t ] [ [ t ] [
3 <bit-vector> dup do-it 3 <bit-vector> dup do-it

View File

@ -1,6 +1,6 @@
USING: help help.topics help.syntax help.crossref USING: help help.topics help.syntax help.crossref
help.definitions io io.files kernel namespaces vocabs sequences help.definitions io io.files kernel namespaces vocabs sequences
parser vocabs.loader ; parser vocabs.loader vocabs.loader.private accessors assocs ;
IN: bootstrap.help IN: bootstrap.help
: load-help ( -- ) : load-help ( -- )
@ -10,8 +10,8 @@ IN: bootstrap.help
t load-help? set-global t load-help? set-global
[ drop ] load-vocab-hook [ [ drop ] load-vocab-hook [
vocabs dictionary get values
[ vocab-docs-loaded? not ] filter [ docs-loaded?>> not ] filter
[ load-docs ] each [ load-docs ] each
] with-variable ; ] with-variable ;

View File

@ -1,6 +1,6 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: http.client checksums checksums.openssl splitting assocs USING: http.client checksums checksums.md5 splitting assocs
kernel io.files bootstrap.image sequences io urls ; kernel io.files bootstrap.image sequences io urls ;
IN: bootstrap.image.download IN: bootstrap.image.download
@ -13,7 +13,7 @@ IN: bootstrap.image.download
: need-new-image? ( image -- ? ) : need-new-image? ( image -- ? )
dup exists? dup exists?
[ [
[ openssl-md5 checksum-file hex-string ] [ md5 checksum-file hex-string ]
[ download-checksums at ] [ download-checksums at ]
bi = not bi = not
] [ drop t ] if ; ] [ drop t ] if ;

View File

@ -72,7 +72,7 @@ SYMBOL: objects
: put-object ( n obj -- ) (objects) set-at ; : put-object ( n obj -- ) (objects) set-at ;
: cache-object ( obj quot -- value ) : cache-object ( obj quot -- value )
>r (objects) r> [ obj>> ] prepose cache ; inline [ (objects) ] dip [ obj>> ] prepose cache ; inline
! Constants ! Constants
@ -97,10 +97,10 @@ SYMBOL: sub-primitives
{ [ { } make ] [ ] [ ] [ ] } spread 4array ; inline { [ { } make ] [ ] [ ] [ ] } spread 4array ; inline
: jit-define ( quot rc rt offset name -- ) : jit-define ( quot rc rt offset name -- )
>r make-jit r> set ; inline [ make-jit ] dip set ; inline
: define-sub-primitive ( quot rc rt offset word -- ) : define-sub-primitive ( quot rc rt offset word -- )
>r make-jit r> sub-primitives get set-at ; [ make-jit ] dip sub-primitives get set-at ;
! The image being constructed; a vector of word-size integers ! The image being constructed; a vector of word-size integers
SYMBOL: image SYMBOL: image
@ -124,22 +124,29 @@ SYMBOL: jit-primitive-word
SYMBOL: jit-primitive SYMBOL: jit-primitive
SYMBOL: jit-word-jump SYMBOL: jit-word-jump
SYMBOL: jit-word-call SYMBOL: jit-word-call
SYMBOL: jit-push-literal
SYMBOL: jit-push-immediate SYMBOL: jit-push-immediate
SYMBOL: jit-if-word SYMBOL: jit-if-word
SYMBOL: jit-if-jump SYMBOL: jit-if-1
SYMBOL: jit-if-2
SYMBOL: jit-dispatch-word SYMBOL: jit-dispatch-word
SYMBOL: jit-dispatch SYMBOL: jit-dispatch
SYMBOL: jit-dip-word
SYMBOL: jit-dip
SYMBOL: jit-2dip-word
SYMBOL: jit-2dip
SYMBOL: jit-3dip-word
SYMBOL: jit-3dip
SYMBOL: jit-epilog SYMBOL: jit-epilog
SYMBOL: jit-return SYMBOL: jit-return
SYMBOL: jit-profiling SYMBOL: jit-profiling
SYMBOL: jit-declare-word SYMBOL: jit-declare-word
SYMBOL: jit-save-stack
! Default definition for undefined words ! Default definition for undefined words
SYMBOL: undefined-quot SYMBOL: undefined-quot
: userenv-offset ( symbol -- n ) : userenvs ( -- assoc )
{ H{
{ bootstrap-boot-quot 20 } { bootstrap-boot-quot 20 }
{ bootstrap-global 21 } { bootstrap-global 21 }
{ jit-code-format 22 } { jit-code-format 22 }
@ -148,9 +155,9 @@ SYMBOL: undefined-quot
{ jit-primitive 25 } { jit-primitive 25 }
{ jit-word-jump 26 } { jit-word-jump 26 }
{ jit-word-call 27 } { jit-word-call 27 }
{ jit-push-literal 28 } { jit-if-word 28 }
{ jit-if-word 29 } { jit-if-1 29 }
{ jit-if-jump 30 } { jit-if-2 30 }
{ jit-dispatch-word 31 } { jit-dispatch-word 31 }
{ jit-dispatch 32 } { jit-dispatch 32 }
{ jit-epilog 33 } { jit-epilog 33 }
@ -158,8 +165,18 @@ SYMBOL: undefined-quot
{ jit-profiling 35 } { jit-profiling 35 }
{ jit-push-immediate 36 } { jit-push-immediate 36 }
{ jit-declare-word 42 } { jit-declare-word 42 }
{ jit-save-stack 43 }
{ jit-dip-word 44 }
{ jit-dip 45 }
{ jit-2dip-word 46 }
{ jit-2dip 47 }
{ jit-3dip-word 48 }
{ jit-3dip 49 }
{ undefined-quot 60 } { undefined-quot 60 }
} at header-size + ; } ; inline
: userenv-offset ( symbol -- n )
userenvs at header-size + ;
: emit ( cell -- ) image get push ; : emit ( cell -- ) image get push ;
@ -188,7 +205,7 @@ SYMBOL: undefined-quot
: emit-fixnum ( n -- ) tag-fixnum emit ; : emit-fixnum ( n -- ) tag-fixnum emit ;
: emit-object ( header tag quot -- addr ) : emit-object ( header tag quot -- addr )
swap here-as >r swap tag-fixnum emit call align-here r> ; swap here-as [ swap tag-fixnum emit call align-here ] dip ;
inline inline
! Write an object to the image. ! Write an object to the image.
@ -441,6 +458,9 @@ M: quotation '
\ dispatch jit-dispatch-word set \ dispatch jit-dispatch-word set
\ do-primitive jit-primitive-word set \ do-primitive jit-primitive-word set
\ declare jit-declare-word set \ declare jit-declare-word set
\ dip jit-dip-word set
\ 2dip jit-2dip-word set
\ 3dip jit-3dip-word set
[ undefined ] undefined-quot set [ undefined ] undefined-quot set
{ {
jit-code-format jit-code-format
@ -449,16 +469,23 @@ M: quotation '
jit-primitive jit-primitive
jit-word-jump jit-word-jump
jit-word-call jit-word-call
jit-push-literal
jit-push-immediate jit-push-immediate
jit-if-word jit-if-word
jit-if-jump jit-if-1
jit-if-2
jit-dispatch-word jit-dispatch-word
jit-dispatch jit-dispatch
jit-dip-word
jit-dip
jit-2dip-word
jit-2dip
jit-3dip-word
jit-3dip
jit-epilog jit-epilog
jit-return jit-return
jit-profiling jit-profiling
jit-declare-word jit-declare-word
jit-save-stack
undefined-quot undefined-quot
} [ emit-userenv ] each ; } [ emit-userenv ] each ;

View File

@ -8,6 +8,8 @@ definitions assocs compiler.errors compiler.units
math.parser generic sets debugger command-line ; math.parser generic sets debugger command-line ;
IN: bootstrap.stage2 IN: bootstrap.stage2
SYMBOL: core-bootstrap-time
SYMBOL: bootstrap-time SYMBOL: bootstrap-time
: default-image-name ( -- string ) : default-image-name ( -- string )
@ -30,11 +32,15 @@ SYMBOL: bootstrap-time
: count-words ( pred -- ) : count-words ( pred -- )
all-words swap count number>string write ; all-words swap count number>string write ;
: print-report ( time -- ) : print-time ( ms -- )
1000 /i 1000 /i
60 /mod swap 60 /mod swap
"Bootstrap completed in " write number>string write number>string write
" minutes and " write number>string write " seconds." print " minutes and " write number>string write " seconds." print ;
: print-report ( -- )
"Core bootstrap completed in " write core-bootstrap-time get print-time
"Bootstrap completed in " write bootstrap-time get print-time
[ compiled>> ] count-words " compiled words" print [ compiled>> ] count-words " compiled words" print
[ symbol? ] count-words " symbol words" print [ symbol? ] count-words " symbol words" print
@ -46,22 +52,22 @@ SYMBOL: bootstrap-time
[ [
! We time bootstrap ! We time bootstrap
millis >r millis
default-image-name "output-image" set-global default-image-name "output-image" set-global
"math compiler threads help io tools ui ui.tools unicode handbook" "include" set-global "math compiler threads help io tools ui ui.tools unicode handbook" "include" set-global
"" "exclude" set-global "" "exclude" set-global
parse-command-line (command-line) parse-command-line
"-no-crossref" cli-args member? [ do-crossref ] unless do-crossref
! Set dll paths ! Set dll paths
os wince? [ "windows.ce" require ] when os wince? [ "windows.ce" require ] when
os winnt? [ "windows.nt" require ] when os winnt? [ "windows.nt" require ] when
"deploy-vocab" get [ "staging" get "deploy-vocab" get or [
"stage2: deployment mode" print "stage2: deployment mode" print
] [ ] [
"listener" require "listener" require
@ -71,6 +77,8 @@ SYMBOL: bootstrap-time
[ [
load-components load-components
millis over - core-bootstrap-time set-global
run-bootstrap-init run-bootstrap-init
] with-compiler-errors ] with-compiler-errors
:errors :errors
@ -84,15 +92,10 @@ SYMBOL: bootstrap-time
[ [
boot boot
do-init-hooks do-init-hooks
[ handle-command-line
parse-command-line
run-user-init
"run" get run
output-stream get [ stream-flush ] when*
] [ print-error 1 exit ] recover
] set-boot-quot ] set-boot-quot
millis r> - dup bootstrap-time set-global millis swap - bootstrap-time set-global
print-report print-report
"output-image" get save-image-and-exit "output-image" get save-image-and-exit

View File

@ -23,4 +23,4 @@ ERROR: box-empty box ;
dup occupied>> [ box> t ] [ drop f f ] if ; dup occupied>> [ box> t ] [ drop f f ] if ;
: if-box? ( box quot -- ) : if-box? ( box quot -- )
>r ?box r> [ drop ] if ; inline [ ?box ] dip [ drop ] if ; inline

View File

@ -99,6 +99,48 @@ HELP: seconds-per-year
{ $values { "integer" integer } } { $values { "integer" integer } }
{ $description "Returns the number of seconds in a year averaged over 400 years. Used internally for adding an arbitrary real number of seconds to a timestamp." } ; { $description "Returns the number of seconds in a year averaged over 400 years. Used internally for adding an arbitrary real number of seconds to a timestamp." } ;
HELP: biweekly
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of two week periods in a year." } ;
HELP: daily-360
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of days in a 360-day year." } ;
HELP: daily-365
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of days in a 365-day year." } ;
HELP: monthly
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of months in a year." } ;
HELP: semimonthly
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of half-months in a year. Note that biweekly has two more periods than semimonthly." } ;
HELP: weekly
{ $values
{ "x" number }
{ "y" number }
}
{ $description "Divides a number by the number of weeks in a year." } ;
HELP: julian-day-number HELP: julian-day-number
{ $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } } { $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } }
{ $description "Calculates the Julian day number from a year, month, and day. The difference between two Julian day numbers is the number of days that have elapsed between the two corresponding dates." } { $description "Calculates the Julian day number from a year, month, and day. The difference between two Julian day numbers is the number of days that have elapsed between the two corresponding dates." }
@ -365,12 +407,12 @@ HELP: unix-1970
{ $values { "timestamp" timestamp } } { $values { "timestamp" timestamp } }
{ $description "Outputs the beginning of UNIX time, or midnight, January 1, 1970." } ; { $description "Outputs the beginning of UNIX time, or midnight, January 1, 1970." } ;
HELP: millis>timestamp HELP: micros>timestamp
{ $values { "x" number } { "timestamp" timestamp } } { $values { "x" number } { "timestamp" timestamp } }
{ $description "Converts a number of milliseconds into a timestamp value in GMT time." } { $description "Converts a number of microseconds into a timestamp value in GMT time." }
{ $examples { $examples
{ $example "USING: accessors calendar prettyprint ;" { $example "USING: accessors calendar prettyprint ;"
"1000 millis>timestamp year>> ." "1000 micros>timestamp year>> ."
"1970" "1970"
} }
} ; } ;
@ -540,6 +582,8 @@ ARTICLE: "calendar" "Calendar"
{ $subsection "years" } { $subsection "years" }
{ $subsection "months" } { $subsection "months" }
{ $subsection "days" } { $subsection "days" }
"Calculating amounts per period of time:"
{ $subsection "time-period-calculations" }
"Meta-data about the calendar:" "Meta-data about the calendar:"
{ $subsection "calendar-facts" } { $subsection "calendar-facts" }
; ;
@ -626,6 +670,18 @@ ARTICLE: "calendar-facts" "Calendar facts"
{ $subsection day-of-week } { $subsection day-of-week }
; ;
ARTICLE: "time-period-calculations" "Calculations over periods of time"
{ $subsection monthly }
{ $subsection semimonthly }
{ $subsection biweekly }
{ $subsection weekly }
{ $subsection daily-360 }
{ $subsection daily-365 }
{ $subsection biweekly }
{ $subsection biweekly }
{ $subsection biweekly }
;
ARTICLE: "years" "Year operations" ARTICLE: "years" "Year operations"
"Leap year predicate:" "Leap year predicate:"
{ $subsection leap-year? } { $subsection leap-year? }

View File

@ -143,10 +143,10 @@ IN: calendar.tests
[ +gt+ ] [ 2005 1 1 12 30 0 instant <timestamp> [ +gt+ ] [ 2005 1 1 12 30 0 instant <timestamp>
2004 1 1 13 30 0 instant <timestamp> <=> ] unit-test 2004 1 1 13 30 0 instant <timestamp> <=> ] unit-test
[ t ] [ now timestamp>millis millis - 1000 < ] unit-test [ t ] [ now timestamp>micros micros - 1000000 < ] unit-test
[ t ] [ 0 millis>timestamp unix-1970 = ] unit-test [ t ] [ 0 micros>timestamp unix-1970 = ] unit-test
[ t ] [ 123456789000 [ millis>timestamp timestamp>millis ] keep = ] unit-test [ t ] [ 123456789000000 [ micros>timestamp timestamp>micros ] keep = ] unit-test
[ t ] [ 123456789123456 [ millis>timestamp timestamp>millis ] keep = ] unit-test [ t ] [ 123456789123456000 [ micros>timestamp timestamp>micros ] keep = ] unit-test
: checktime+ now dup clone [ rot time+ drop ] keep = ; : checktime+ now dup clone [ rot time+ drop ] keep = ;
@ -167,3 +167,5 @@ IN: calendar.tests
[ t ] [ now 50 milliseconds sleep now before? ] unit-test [ t ] [ now 50 milliseconds sleep now before? ] unit-test
[ t ] [ now 50 milliseconds sleep now swap after? ] unit-test [ t ] [ now 50 milliseconds sleep now swap after? ] unit-test
[ t ] [ now 50 milliseconds sleep now 50 milliseconds sleep now swapd between? ] unit-test [ t ] [ now 50 milliseconds sleep now 50 milliseconds sleep now swapd between? ] unit-test
[ 4+1/6 ] [ 100 semimonthly ] unit-test

View File

@ -89,6 +89,13 @@ PRIVATE>
: minutes-per-year ( -- ratio ) 5259492/10 ; inline : minutes-per-year ( -- ratio ) 5259492/10 ; inline
: seconds-per-year ( -- integer ) 31556952 ; inline : seconds-per-year ( -- integer ) 31556952 ; inline
: monthly ( x -- y ) 12 / ; inline
: semimonthly ( x -- y ) 24 / ; inline
: biweekly ( x -- y ) 26 / ; inline
: weekly ( x -- y ) 52 / ; inline
: daily-360 ( x -- y ) 360 / ; inline
: daily-365 ( x -- y ) 365 / ; inline
:: julian-day-number ( year month day -- n ) :: julian-day-number ( year month day -- n )
#! Returns a composite date number #! Returns a composite date number
#! Not valid before year -4800 #! Not valid before year -4800
@ -173,7 +180,7 @@ M: real +year ( timestamp n -- timestamp )
12 /rem dup zero? [ drop 1- 12 ] when swap ; inline 12 /rem dup zero? [ drop 1- 12 ] when swap ; inline
M: integer +month ( timestamp n -- timestamp ) M: integer +month ( timestamp n -- timestamp )
[ over month>> + months/years >r >>month r> +year ] unless-zero ; [ over month>> + months/years [ >>month ] dip +year ] unless-zero ;
M: real +month ( timestamp n -- timestamp ) M: real +month ( timestamp n -- timestamp )
[ float>whole-part swapd average-month * +day swap +month ] unless-zero ; [ float>whole-part swapd average-month * +day swap +month ] unless-zero ;
@ -181,7 +188,7 @@ M: real +month ( timestamp n -- timestamp )
M: integer +day ( timestamp n -- timestamp ) M: integer +day ( timestamp n -- timestamp )
[ [
over >date< julian-day-number + julian-day-number>date over >date< julian-day-number + julian-day-number>date
>r >r >>year r> >>month r> >>day [ >>year ] [ >>month ] [ >>day ] tri*
] unless-zero ; ] unless-zero ;
M: real +day ( timestamp n -- timestamp ) M: real +day ( timestamp n -- timestamp )
@ -191,7 +198,7 @@ M: real +day ( timestamp n -- timestamp )
24 /rem swap ; 24 /rem swap ;
M: integer +hour ( timestamp n -- timestamp ) M: integer +hour ( timestamp n -- timestamp )
[ over hour>> + hours/days >r >>hour r> +day ] unless-zero ; [ over hour>> + hours/days [ >>hour ] dip +day ] unless-zero ;
M: real +hour ( timestamp n -- timestamp ) M: real +hour ( timestamp n -- timestamp )
float>whole-part swapd 60 * +minute swap +hour ; float>whole-part swapd 60 * +minute swap +hour ;
@ -200,7 +207,7 @@ M: real +hour ( timestamp n -- timestamp )
60 /rem swap ; 60 /rem swap ;
M: integer +minute ( timestamp n -- timestamp ) M: integer +minute ( timestamp n -- timestamp )
[ over minute>> + minutes/hours >r >>minute r> +hour ] unless-zero ; [ over minute>> + minutes/hours [ >>minute ] dip +hour ] unless-zero ;
M: real +minute ( timestamp n -- timestamp ) M: real +minute ( timestamp n -- timestamp )
[ float>whole-part swapd 60 * +second swap +minute ] unless-zero ; [ float>whole-part swapd 60 * +second swap +minute ] unless-zero ;
@ -209,7 +216,7 @@ M: real +minute ( timestamp n -- timestamp )
60 /rem swap >integer ; 60 /rem swap >integer ;
M: number +second ( timestamp n -- timestamp ) M: number +second ( timestamp n -- timestamp )
[ over second>> + seconds/minutes >r >>second r> +minute ] unless-zero ; [ over second>> + seconds/minutes [ >>second ] dip +minute ] unless-zero ;
: (time+) : (time+)
[ second>> +second ] keep [ second>> +second ] keep
@ -226,7 +233,7 @@ PRIVATE>
GENERIC# time+ 1 ( time1 time2 -- time3 ) GENERIC# time+ 1 ( time1 time2 -- time3 )
M: timestamp time+ M: timestamp time+
>r clone r> (time+) drop ; [ clone ] dip (time+) drop ;
M: duration time+ M: duration time+
dup timestamp? [ dup timestamp? [
@ -284,7 +291,7 @@ M: timestamp <=> ( ts1 ts2 -- n )
: (time-) ( timestamp timestamp -- n ) : (time-) ( timestamp timestamp -- n )
[ >gmt ] bi@ [ >gmt ] bi@
[ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep [ [ >date< julian-day-number ] bi@ - 86400 * ] 2keep
[ >time< >r >r 3600 * r> 60 * r> + + ] bi@ - + ; [ >time< [ [ 3600 * ] [ 60 * ] bi* ] dip + + ] bi@ - + ;
M: timestamp time- M: timestamp time-
#! Exact calendar-time difference #! Exact calendar-time difference
@ -320,14 +327,20 @@ M: duration time-
1970 1 1 0 0 0 instant <timestamp> ; 1970 1 1 0 0 0 instant <timestamp> ;
: millis>timestamp ( x -- timestamp ) : millis>timestamp ( x -- timestamp )
>r unix-1970 r> milliseconds time+ ; [ unix-1970 ] dip milliseconds time+ ;
: timestamp>millis ( timestamp -- n ) : timestamp>millis ( timestamp -- n )
unix-1970 (time-) 1000 * >integer ; unix-1970 (time-) 1000 * >integer ;
: micros>timestamp ( x -- timestamp )
[ unix-1970 ] dip microseconds time+ ;
: timestamp>micros ( timestamp -- n )
unix-1970 (time-) 1000000 * >integer ;
: gmt ( -- timestamp ) : gmt ( -- timestamp )
#! GMT time, right now #! GMT time, right now
unix-1970 millis milliseconds time+ ; unix-1970 micros microseconds time+ ;
: now ( -- timestamp ) gmt >local-time ; : now ( -- timestamp ) gmt >local-time ;
: hence ( duration -- timestamp ) now swap time+ ; : hence ( duration -- timestamp ) now swap time+ ;
@ -337,10 +350,11 @@ M: duration time-
#! Zeller Congruence #! Zeller Congruence
#! http://web.textfiles.com/computers/formulas.txt #! http://web.textfiles.com/computers/formulas.txt
#! good for any date since October 15, 1582 #! good for any date since October 15, 1582
>r dup 2 <= [ 12 + >r 1- r> ] when [
>r dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + r> dup 2 <= [ [ 1- ] [ 12 + ] bi* ] when
[ 1+ 3 * 5 /i + ] keep 2 * + r> [ dup [ 4 /i + ] keep [ 100 /i - ] keep 400 /i + ] dip
1+ + 7 mod ; [ 1+ 3 * 5 /i + ] keep 2 * +
] dip 1+ + 7 mod ;
GENERIC: days-in-year ( obj -- n ) GENERIC: days-in-year ( obj -- n )
@ -404,7 +418,7 @@ PRIVATE>
: since-1970 ( duration -- timestamp ) : since-1970 ( duration -- timestamp )
unix-1970 time+ >local-time ; unix-1970 time+ >local-time ;
M: timestamp sleep-until timestamp>millis sleep-until ; M: timestamp sleep-until timestamp>micros sleep-until ;
M: duration sleep hence sleep-until ; M: duration sleep hence sleep-until ;

View File

@ -138,11 +138,11 @@ M: timestamp year. ( timestamp -- )
: read-rfc3339-gmt-offset ( ch -- dt ) : read-rfc3339-gmt-offset ( ch -- dt )
dup CHAR: Z = [ drop instant ] [ dup CHAR: Z = [ drop instant ] [
>r [
read-00 hours read-00 hours
read1 { { CHAR: : [ read-00 ] } { f [ 0 ] } } case minutes read1 { { CHAR: : [ read-00 ] } { f [ 0 ] } } case minutes
time+ time+
r> signed-gmt-offset ] dip signed-gmt-offset
] if ; ] if ;
: read-ymd ( -- y m d ) : read-ymd ( -- y m d )
@ -152,8 +152,9 @@ M: timestamp year. ( timestamp -- )
read-00 ":" expect read-00 ":" expect read-00 ; read-00 ":" expect read-00 ":" expect read-00 ;
: read-rfc3339-seconds ( s -- s' ch ) : read-rfc3339-seconds ( s -- s' ch )
"+-Z" read-until >r "+-Z" read-until [
[ string>number ] [ length 10 swap ^ ] bi / + r> ; [ string>number ] [ length 10 swap ^ ] bi / +
] dip ;
: (rfc3339>timestamp) ( -- timestamp ) : (rfc3339>timestamp) ( -- timestamp )
read-ymd read-ymd
@ -181,9 +182,9 @@ ERROR: invalid-timestamp-format ;
: parse-rfc822-gmt-offset ( string -- dt ) : parse-rfc822-gmt-offset ( string -- dt )
dup "GMT" = [ drop instant ] [ dup "GMT" = [ drop instant ] [
unclip >r unclip [
2 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+ 2 cut [ string>number ] bi@ [ hours ] [ minutes ] bi* time+
r> signed-gmt-offset ] dip signed-gmt-offset
] if ; ] if ;
: (rfc822>timestamp) ( -- timestamp ) : (rfc822>timestamp) ( -- timestamp )

View File

@ -7,7 +7,7 @@ SYMBOL: time
: (time-thread) ( -- ) : (time-thread) ( -- )
now time get set-model now time get set-model
1000 sleep (time-thread) ; 1 seconds sleep (time-thread) ;
: time-thread ( -- ) : time-thread ( -- )
[ [

1
basis/calendar/windows/tags.txt Normal file → Executable file
View File

@ -1,2 +1 @@
unportable unportable
windows

View File

@ -14,7 +14,7 @@ IN: channels.remote
PRIVATE> PRIVATE>
: publish ( channel -- id ) : publish ( channel -- id )
256 random-bits dup >r remote-channels set-at r> ; 256 random-bits dup [ remote-channels set-at ] dip ;
: get-channel ( id -- channel ) : get-channel ( id -- channel )
remote-channels at ; remote-channels at ;

View File

@ -18,4 +18,4 @@ SYMBOL: bytes-read
] "" make 64 group ; ] "" make 64 group ;
: update-old-new ( old new -- ) : update-old-new ( old new -- )
[ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline [ [ get ] bi@ w+ dup ] 2keep [ set ] bi@ ; inline

View File

@ -14,7 +14,7 @@ IN: checksums.md5
SYMBOLS: a b c d old-a old-b old-c old-d ; SYMBOLS: a b c d old-a old-b old-c old-d ;
: T ( N -- Y ) : T ( N -- Y )
sin abs 4294967296 * >bignum ; foldable sin abs 4294967296 * >integer ; foldable
: initialize-md5 ( -- ) : initialize-md5 ( -- )
0 bytes-read set 0 bytes-read set

View File

@ -28,7 +28,7 @@ M: evp-md-context dispose
handle>> EVP_MD_CTX_cleanup drop ; handle>> EVP_MD_CTX_cleanup drop ;
: with-evp-md-context ( quot -- ) : with-evp-md-context ( quot -- )
maybe-init-ssl >r <evp-md-context> r> with-disposal ; inline maybe-init-ssl [ <evp-md-context> ] dip with-disposal ; inline
: digest-named ( name -- md ) : digest-named ( name -- md )
dup EVP_get_digestbyname dup EVP_get_digestbyname

View File

@ -41,9 +41,9 @@ SYMBOLS: h0 h1 h2 h3 h4 A B C D E w K ;
: sha1-f ( B C D t -- f_tbcd ) : sha1-f ( B C D t -- f_tbcd )
20 /i 20 /i
{ {
{ 0 [ >r over bitnot r> bitand >r bitand r> bitor ] } { 0 [ [ over bitnot ] dip bitand [ bitand ] dip bitor ] }
{ 1 [ bitxor bitxor ] } { 1 [ bitxor bitxor ] }
{ 2 [ 2dup bitand >r pick bitand >r bitand r> r> bitor bitor ] } { 2 [ 2dup bitand [ pick bitand [ bitand ] dip ] dip bitor bitor ] }
{ 3 [ bitxor bitxor ] } { 3 [ bitxor bitxor ] }
} case ; } case ;

View File

@ -59,7 +59,7 @@ SYMBOLS: vars M K H S0 S1 process-M word-size block-size ;
[ 15 - swap nth s0-256 ] 2keep [ 15 - swap nth s0-256 ] 2keep
[ 7 - swap nth ] 2keep [ 7 - swap nth ] 2keep
[ 2 - swap nth s1-256 ] 2keep [ 2 - swap nth s1-256 ] 2keep
>r >r + + w+ r> r> swap set-nth ; inline [ + + w+ ] 2dip swap set-nth ; inline
: prepare-message-schedule ( seq -- w-seq ) : prepare-message-schedule ( seq -- w-seq )
word-size get group [ be> ] map block-size get 0 pad-right word-size get group [ be> ] map block-size get 0 pad-right
@ -71,7 +71,7 @@ SYMBOLS: vars M K H S0 S1 process-M word-size block-size ;
[ bitxor bitand ] keep bitxor ; [ bitxor bitand ] keep bitxor ;
: maj ( x y z -- x' ) : maj ( x y z -- x' )
>r [ bitand ] 2keep bitor r> bitand bitor ; [ [ bitand ] 2keep bitor ] dip bitand bitor ;
: S0-256 ( x -- x' ) : S0-256 ( x -- x' )
[ -2 bitroll-32 ] keep [ -2 bitroll-32 ] keep
@ -83,7 +83,7 @@ SYMBOLS: vars M K H S0 S1 process-M word-size block-size ;
[ -11 bitroll-32 ] keep [ -11 bitroll-32 ] keep
-25 bitroll-32 bitxor bitxor ; inline -25 bitroll-32 bitxor bitxor ; inline
: slice3 ( n seq -- a b c ) >r dup 3 + r> <slice> first3 ; inline : slice3 ( n seq -- a b c ) [ dup 3 + ] dip <slice> first3 ; inline
: T1 ( W n -- T1 ) : T1 ( W n -- T1 )
[ swap nth ] keep [ swap nth ] keep
@ -105,7 +105,7 @@ SYMBOLS: vars M K H S0 S1 process-M word-size block-size ;
d c pick exchange d c pick exchange
c b pick exchange c b pick exchange
b a pick exchange b a pick exchange
>r w+ a r> set-nth ; [ w+ a ] dip set-nth ;
: process-chunk ( M -- ) : process-chunk ( M -- )
H get clone vars set H get clone vars set
@ -118,7 +118,7 @@ SYMBOLS: vars M K H S0 S1 process-M word-size block-size ;
: preprocess-plaintext ( string big-endian? -- padded-string ) : preprocess-plaintext ( string big-endian? -- padded-string )
#! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits #! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
>r >sbuf r> over [ [ >sbuf ] dip over [
HEX: 80 , HEX: 80 ,
dup length HEX: 3f bitand dup length HEX: 3f bitand
calculate-pad-length 0 <string> % calculate-pad-length 0 <string> %

View File

@ -40,12 +40,13 @@ FUNCTION: void NSBeep ( ) ;
dup next-event [ -> sendEvent: t ] [ drop f ] if* ; dup next-event [ -> sendEvent: t ] [ drop f ] if* ;
: add-observer ( observer selector name object -- ) : add-observer ( observer selector name object -- )
>r >r >r >r NSNotificationCenter -> defaultCenter [
r> r> sel_registerName [ NSNotificationCenter -> defaultCenter ] 2dip
r> r> -> addObserver:selector:name:object: ; sel_registerName
] 2dip -> addObserver:selector:name:object: ;
: remove-observer ( observer -- ) : remove-observer ( observer -- )
>r NSNotificationCenter -> defaultCenter r> [ NSNotificationCenter -> defaultCenter ] dip
-> removeObserver: ; -> removeObserver: ;
: finish-launching ( -- ) NSApp -> finishLaunching ; : finish-launching ( -- ) NSApp -> finishLaunching ;

View File

@ -1,4 +1,4 @@
! Copyright (C) 2006, 2007 Slava Pestov ! Copyright (C) 2006, 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel cocoa cocoa.messages cocoa.classes USING: kernel cocoa cocoa.messages cocoa.classes
cocoa.application sequences splitting core-foundation ; cocoa.application sequences splitting core-foundation ;
@ -26,9 +26,9 @@ IN: cocoa.dialogs
[ -> filenames CF>string-array ] [ drop f ] if ; [ -> filenames CF>string-array ] [ drop f ] if ;
: split-path ( path -- dir file ) : split-path ( path -- dir file )
"/" last-split1 [ <NSString> ] bi@ ; "/" split1-last [ <NSString> ] bi@ ;
: save-panel ( path -- paths ) : save-panel ( path -- paths )
<NSSavePanel> dup [ <NSSavePanel> dup ] dip
rot split-path -> runModalForDirectory:file: NSOKButton = split-path -> runModalForDirectory:file: NSOKButton =
[ -> filename CF>string ] [ drop f ] if ; [ -> filename CF>string ] [ drop f ] if ;

View File

@ -1,26 +1,31 @@
USING: kernel cocoa cocoa.types alien.c-types locals math sequences ! Copyright (C) 2008 Joe Groff.
vectors fry libc ; ! See http://factorcode.org/license.txt for BSD license.
USING: kernel cocoa cocoa.types alien.c-types locals math
sequences vectors fry libc destructors
specialized-arrays.direct.alien ;
IN: cocoa.enumeration IN: cocoa.enumeration
: NS-EACH-BUFFER-SIZE 16 ; inline : NS-EACH-BUFFER-SIZE 16 ; inline
: (with-enumeration-buffers) ( quot -- ) : with-enumeration-buffers ( quot -- )
"NSFastEnumerationState" heap-size swap '[ [
NS-EACH-BUFFER-SIZE "id" heap-size * [ [
NS-EACH-BUFFER-SIZE @ "NSFastEnumerationState" malloc-object &free
] with-malloc NS-EACH-BUFFER-SIZE "id" heap-size * malloc-object &free
] with-malloc ; inline NS-EACH-BUFFER-SIZE
] dip call
] with-destructors ; inline
:: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- ) :: (NSFastEnumeration-each) ( object quot: ( elt -- ) state stackbuf count -- )
object state stackbuf count -> countByEnumeratingWithState:objects:count: object state stackbuf count -> countByEnumeratingWithState:objects:count:
dup zero? [ drop ] [ dup 0 = [ drop ] [
state NSFastEnumerationState-itemsPtr [ stackbuf ] unless* state NSFastEnumerationState-itemsPtr [ stackbuf ] unless*
'[ _ void*-nth quot call ] each swap <direct-void*-array> quot each
object quot state stackbuf count (NSFastEnumeration-each) object quot state stackbuf count (NSFastEnumeration-each)
] if ; inline recursive ] if ; inline recursive
: NSFastEnumeration-each ( object quot -- ) : NSFastEnumeration-each ( object quot -- )
[ (NSFastEnumeration-each) ] (with-enumeration-buffers) ; inline [ (NSFastEnumeration-each) ] with-enumeration-buffers ; inline
: NSFastEnumeration-map ( object quot -- vector ) : NSFastEnumeration-map ( object quot -- vector )
NS-EACH-BUFFER-SIZE <vector> NS-EACH-BUFFER-SIZE <vector>

View File

@ -31,7 +31,7 @@ HELP: alien>objc-types
{ objc>alien-types alien>objc-types } related-words { objc>alien-types alien>objc-types } related-words
HELP: import-objc-class HELP: import-objc-class
{ $values { "name" string } { "quot" "a quotation with stack effect " { $snippet "( -- )" } } } { $values { "name" string } { "quot" { $quotation "( -- )" } } }
{ $description "If a class named " { $snippet "name" } " is already known to the Objective C interface, does nothing. Otherwise, first calls the quotation. The quotation should make the class available to the Objective C runtime if necessary, either by loading a framework or defining it directly. After the quotation returns, this word makes the class available to Factor programs by importing methods and creating a class word the class object in the " { $vocab-link "cocoa.classes" } " vocabulary." } ; { $description "If a class named " { $snippet "name" } " is already known to the Objective C interface, does nothing. Otherwise, first calls the quotation. The quotation should make the class available to the Objective C runtime if necessary, either by loading a framework or defining it directly. After the quotation returns, this word makes the class available to Factor programs by importing methods and creating a class word the class object in the " { $vocab-link "cocoa.classes" } " vocabulary." } ;
HELP: root-class HELP: root-class

View File

@ -5,7 +5,8 @@ combinators compiler compiler.alien kernel math namespaces make
parser prettyprint prettyprint.sections quotations sequences parser prettyprint prettyprint.sections quotations sequences
strings words cocoa.runtime io macros memoize debugger strings words cocoa.runtime io macros memoize debugger
io.encodings.ascii effects libc libc.private parser lexer init io.encodings.ascii effects libc libc.private parser lexer init
core-foundation fry ; core-foundation fry generalizations
specialized-arrays.direct.alien ;
IN: cocoa.messages IN: cocoa.messages
: make-sender ( method function -- quot ) : make-sender ( method function -- quot )
@ -27,7 +28,7 @@ super-message-senders global [ H{ } assoc-like ] change-at
: cache-stub ( method function hash -- ) : cache-stub ( method function hash -- )
[ [
over get [ 2drop ] [ over >r sender-stub r> set ] if over get [ 2drop ] [ over [ sender-stub ] dip set ] if
] bind ; ] bind ;
: cache-stubs ( method -- ) : cache-stubs ( method -- )
@ -37,7 +38,7 @@ super-message-senders global [ H{ } assoc-like ] change-at
: <super> ( receiver -- super ) : <super> ( receiver -- super )
"objc-super" <c-object> [ "objc-super" <c-object> [
>r dup object_getClass class_getSuperclass r> [ dup object_getClass class_getSuperclass ] dip
set-objc-super-class set-objc-super-class
] keep ] keep
[ set-objc-super-receiver ] keep ; [ set-objc-super-receiver ] keep ;
@ -62,23 +63,18 @@ objc-methods global [ H{ } assoc-like ] change-at
dup objc-methods get at dup objc-methods get at
[ ] [ "No such method: " prepend throw ] ?if ; [ ] [ "No such method: " prepend throw ] ?if ;
: make-dip ( quot n -- quot' )
dup
\ >r <repetition> >quotation -rot
\ r> <repetition> >quotation 3append ;
MEMO: make-prepare-send ( selector method super? -- quot ) MEMO: make-prepare-send ( selector method super? -- quot )
[ [
[ \ <super> , ] when [ \ <super> , ] when
swap <selector> , \ selector , swap <selector> , \ selector ,
] [ ] make ] [ ] make
swap second length 2 - make-dip ; swap second length 2 - '[ _ _ ndip ] ;
MACRO: (send) ( selector super? -- quot ) MACRO: (send) ( selector super? -- quot )
>r dup lookup-method r> [ dup lookup-method ] dip
[ make-prepare-send ] 2keep [ make-prepare-send ] 2keep
super-message-senders message-senders ? get at super-message-senders message-senders ? get at
[ slip execute ] 2curry ; '[ _ call _ execute ] ;
: send ( receiver args... selector -- return... ) f (send) ; inline : send ( receiver args... selector -- return... ) f (send) ; inline
@ -165,14 +161,14 @@ objc>alien-types get [ swap ] assoc-map
assoc-union alien>objc-types set-global assoc-union alien>objc-types set-global
: objc-struct-type ( i string -- ctype ) : objc-struct-type ( i string -- ctype )
2dup CHAR: = -rot index-from swap subseq [ CHAR: = ] 2keep index-from swap subseq
dup c-types get key? [ dup c-types get key? [
"Warning: no such C type: " write dup print "Warning: no such C type: " write dup print
drop "void*" drop "void*"
] unless ; ] unless ;
: (parse-objc-type) ( i string -- ctype ) : (parse-objc-type) ( i string -- ctype )
2dup nth >r >r 1+ r> r> { [ [ 1+ ] dip ] [ nth ] 2bi {
{ [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] } { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
{ [ dup CHAR: ^ = ] [ 3drop "void*" ] } { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
{ [ dup CHAR: { = ] [ drop objc-struct-type ] } { [ dup CHAR: { = ] [ drop objc-struct-type ] }
@ -203,8 +199,11 @@ assoc-union alien>objc-types set-global
objc-methods get set-at ; objc-methods get set-at ;
: each-method-in-class ( class quot -- ) : each-method-in-class ( class quot -- )
[ 0 <uint> [ class_copyMethodList ] keep *uint over ] dip [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
'[ _ void*-nth @ ] each (free) ; inline over 0 = [ 3drop ] [
[ <direct-void*-array> ] dip
[ each ] [ drop underlying>> (free) ] 2bi
] if ; inline
: register-objc-methods ( class -- ) : register-objc-methods ( class -- )
[ register-objc-method ] each-method-in-class ; [ register-objc-method ] each-method-in-class ;
@ -223,22 +222,23 @@ assoc-union alien>objc-types set-global
: class-exists? ( string -- class ) objc_getClass >boolean ; : class-exists? ( string -- class ) objc_getClass >boolean ;
: unless-defined ( class quot -- ) : unless-defined ( class quot -- )
>r class-exists? r> unless ; inline [ class-exists? ] dip unless ; inline
: define-objc-class-word ( name quot -- ) : define-objc-class-word ( name quot -- )
[ [
over , , \ unless-defined , dup , \ objc-class , over , , \ unless-defined , dup , \ objc-class ,
] [ ] make >r "cocoa.classes" create r> ] [ ] make [ "cocoa.classes" create ] dip
(( -- class )) define-declared ; (( -- class )) define-declared ;
: import-objc-class ( name quot -- ) : import-objc-class ( name quot -- )
2dup unless-defined 2dup unless-defined
dupd define-objc-class-word dupd define-objc-class-word
[ '[
_
dup dup
objc-class register-objc-methods objc-class register-objc-methods
objc-meta-class register-objc-methods objc-meta-class register-objc-methods
] curry try ; ] try ;
: root-class ( class -- root ) : root-class ( class -- root )
dup class_getSuperclass [ root-class ] [ ] ?if ; dup class_getSuperclass [ root-class ] [ ] ?if ;

View File

@ -1,8 +1,8 @@
! Copyright (C) 2006, 2007 Slava Pestov. ! Copyright (C) 2006, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types arrays kernel cocoa.messages USING: alien.accessors arrays kernel cocoa.messages
cocoa.classes cocoa.application cocoa core-foundation cocoa.classes cocoa.application cocoa core-foundation sequences
sequences ; ;
IN: cocoa.pasteboard IN: cocoa.pasteboard
: NSStringPboardType "NSStringPboardType" ; : NSStringPboardType "NSStringPboardType" ;
@ -20,11 +20,11 @@ IN: cocoa.pasteboard
: set-pasteboard-string ( str pasteboard -- ) : set-pasteboard-string ( str pasteboard -- )
NSStringPboardType <NSString> NSStringPboardType <NSString>
dup 1array pick set-pasteboard-types dup 1array pick set-pasteboard-types
>r swap <NSString> r> -> setString:forType: drop ; [ swap <NSString> ] dip -> setString:forType: drop ;
: pasteboard-error ( error -- f ) : pasteboard-error ( error -- f )
"Pasteboard does not hold a string" <NSString> "Pasteboard does not hold a string" <NSString>
0 spin set-void*-nth f ; 0 set-alien-cell f ;
: ?pasteboard-string ( pboard error -- str/f ) : ?pasteboard-string ( pboard error -- str/f )
over pasteboard-string? [ over pasteboard-string? [

View File

@ -36,7 +36,7 @@ IN: cocoa.subclassing
] map concat ; ] map concat ;
: prepare-method ( ret types quot -- type imp ) : prepare-method ( ret types quot -- type imp )
>r [ encode-types ] 2keep r> [ [ [ encode-types ] 2keep ] dip [
"cdecl" swap 4array % \ alien-callback , "cdecl" swap 4array % \ alien-callback ,
] [ ] make define-temp ; ] [ ] make define-temp ;

View File

@ -1,8 +1,8 @@
! Copyright (C) 2006, 2007 Slava Pestov ! Copyright (C) 2006, 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types arrays kernel math namespaces make cocoa USING: specialized-arrays.int arrays kernel math namespaces make
cocoa.messages cocoa.classes cocoa.types sequences cocoa cocoa.messages cocoa.classes cocoa.types sequences
continuations ; continuations accessors ;
IN: cocoa.views IN: cocoa.views
: NSOpenGLPFAAllRenderers 1 ; : NSOpenGLPFAAllRenderers 1 ;
@ -69,12 +69,12 @@ PRIVATE>
NSOpenGLPFASamples , 8 , NSOpenGLPFASamples , 8 ,
] when ] when
0 , 0 ,
] { } make >c-int-array ] int-array{ } make underlying>>
-> initWithAttributes: -> initWithAttributes:
-> autorelease ; -> autorelease ;
: <GLView> ( class dim -- view ) : <GLView> ( class dim -- view )
>r -> alloc 0 0 r> first2 <NSRect> <PixelFormat> [ -> alloc 0 0 ] dip first2 <NSRect> <PixelFormat>
-> initWithFrame:pixelFormat: -> initWithFrame:pixelFormat:
dup 1 -> setPostsBoundsChangedNotifications: dup 1 -> setPostsBoundsChangedNotifications:
dup 1 -> setPostsFrameChangedNotifications: ; dup 1 -> setPostsFrameChangedNotifications: ;
@ -85,10 +85,11 @@ PRIVATE>
swap NSRect-h >fixnum 2array ; swap NSRect-h >fixnum 2array ;
: mouse-location ( view event -- loc ) : mouse-location ( view event -- loc )
over >r [
-> locationInWindow f -> convertPoint:fromView: -> locationInWindow f -> convertPoint:fromView:
dup NSPoint-x swap NSPoint-y [ NSPoint-x ] [ NSPoint-y ] bi
r> -> frame NSRect-h swap - 2array ; ] [ drop -> frame NSRect-h ] 2bi
swap - 2array ;
USE: opengl.gl USE: opengl.gl
USE: alien.syntax USE: alien.syntax

View File

@ -34,5 +34,6 @@ IN: cocoa.windows
dup 0 -> setReleasedWhenClosed: ; dup 0 -> setReleasedWhenClosed: ;
: window-content-rect ( window -- rect ) : window-content-rect ( window -- rect )
NSWindow over -> frame rot -> styleMask [ NSWindow ] dip
[ -> frame ] [ -> styleMask ] bi
-> contentRectForFrameRect:styleMask: ; -> contentRectForFrameRect:styleMask: ;

View File

@ -52,17 +52,17 @@ HELP: 3||
{ "quot" quotation } } { "quot" quotation } }
{ $description "Returns true if any quotation in the sequence returns true. Each quotation takes the same three elements from the datastack and must return a boolean." } ; { $description "Returns true if any quotation in the sequence returns true. Each quotation takes the same three elements from the datastack and must return a boolean." } ;
HELP: n&&-rewrite HELP: n&&
{ $values { $values
{ "quots" "a sequence of quotations" } { "N" integer } { "quots" "a sequence of quotations" } { "N" integer }
{ "quot" quotation } } { "quot" quotation } }
{ $description "A macro that reqrites the code to pass " { $snippet "N" } " parameters from the stack to each AND quotation." } ; { $description "A macro that rewrites the code to pass " { $snippet "n" } " parameters from the stack to each AND quotation." } ;
HELP: n||-rewrite HELP: n||
{ $values { $values
{ "quots" "a sequence of quotations" } { "N" integer } { "quots" "a sequence of quotations" } { "n" integer }
{ "quot" quotation } } { "quot" quotation } }
{ $description "A macro that reqrites the code to pass " { $snippet "N" } " parameters from the stack to each OR quotation." } ; { $description "A macro that rewrites the code to pass " { $snippet "n" } " parameters from the stack to each OR quotation." } ;
ARTICLE: "combinators.short-circuit" "Short-circuit combinators" ARTICLE: "combinators.short-circuit" "Short-circuit combinators"
"The " { $vocab-link "combinators.short-circuit" } " vocabulary stops a computation early once a condition is met." $nl "The " { $vocab-link "combinators.short-circuit" } " vocabulary stops a computation early once a condition is met." $nl
@ -77,8 +77,8 @@ ARTICLE: "combinators.short-circuit" "Short-circuit combinators"
{ $subsection 2|| } { $subsection 2|| }
{ $subsection 3|| } { $subsection 3|| }
"Generalized combinators:" "Generalized combinators:"
{ $subsection n&&-rewrite } { $subsection n&& }
{ $subsection n||-rewrite } { $subsection n|| }
; ;
ABOUT: "combinators.short-circuit" ABOUT: "combinators.short-circuit"

View File

@ -1,35 +1,33 @@
USING: kernel combinators quotations arrays sequences assocs USING: kernel combinators quotations arrays sequences assocs
locals generalizations macros fry ; locals generalizations macros fry ;
IN: combinators.short-circuit IN: combinators.short-circuit
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MACRO:: n&& ( quots n -- quot )
[ f ] quots [| q |
n
[ q '[ drop _ ndup @ dup not ] ]
[ '[ drop _ ndrop f ] ]
bi 2array
] map
n '[ _ nnip ] suffix 1array
[ cond ] 3append ;
:: n&&-rewrite ( quots N -- quot ) MACRO: 0&& ( quots -- quot ) '[ _ 0 n&& ] ;
quots MACRO: 1&& ( quots -- quot ) '[ _ 1 n&& ] ;
[ '[ drop N ndup @ dup not ] [ drop N ndrop f ] 2array ] MACRO: 2&& ( quots -- quot ) '[ _ 2 n&& ] ;
map MACRO: 3&& ( quots -- quot ) '[ _ 3 n&& ] ;
[ t ] [ N nnip ] 2array suffix
'[ f _ cond ] ;
MACRO: 0&& ( quots -- quot ) 0 n&&-rewrite ; MACRO:: n|| ( quots n -- quot )
MACRO: 1&& ( quots -- quot ) 1 n&&-rewrite ; [ f ] quots [| q |
MACRO: 2&& ( quots -- quot ) 2 n&&-rewrite ; n
MACRO: 3&& ( quots -- quot ) 3 n&&-rewrite ; [ q '[ drop _ ndup @ dup ] ]
[ '[ _ nnip ] ]
bi 2array
] map
n '[ drop _ ndrop t ] [ f ] 2array suffix 1array
[ cond ] 3append ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MACRO: 0|| ( quots -- quot ) '[ _ 0 n|| ] ;
MACRO: 1|| ( quots -- quot ) '[ _ 1 n|| ] ;
:: n||-rewrite ( quots N -- quot ) MACRO: 2|| ( quots -- quot ) '[ _ 2 n|| ] ;
quots MACRO: 3|| ( quots -- quot ) '[ _ 3 n|| ] ;
[ '[ drop N ndup @ dup ] [ N nnip ] 2array ]
map
[ drop N ndrop t ] [ f ] 2array suffix
'[ f _ cond ] ;
MACRO: 0|| ( quots -- quot ) 0 n||-rewrite ;
MACRO: 1|| ( quots -- quot ) 1 n||-rewrite ;
MACRO: 2|| ( quots -- quot ) 2 n||-rewrite ;
MACRO: 3|| ( quots -- quot ) 3 n||-rewrite ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

View File

@ -1,7 +1,5 @@
USING: kernel sequences math stack-checker effects accessors macros USING: kernel sequences math stack-checker effects accessors macros
combinators.short-circuit ; fry combinators.short-circuit ;
IN: combinators.short-circuit.smart IN: combinators.short-circuit.smart
<PRIVATE <PRIVATE
@ -13,6 +11,6 @@ IN: combinators.short-circuit.smart
PRIVATE> PRIVATE>
MACRO: && ( quots -- quot ) dup arity n&&-rewrite ; MACRO: && ( quots -- quot ) dup arity '[ _ _ n&& ] ;
MACRO: || ( quots -- quot ) dup arity n||-rewrite ; MACRO: || ( quots -- quot ) dup arity '[ _ _ n|| ] ;

View File

@ -1,13 +1,17 @@
USING: help.markup help.syntax parser vocabs.loader strings ; USING: help.markup help.syntax parser vocabs.loader strings
command-line.private ;
IN: command-line IN: command-line
HELP: run-bootstrap-init HELP: run-bootstrap-init
{ $description "Runs the " { $snippet ".factor-boot-rc" } " file in the user's home directory unless the " { $snippet "-no-user-init" } " command line switch was given." } ; { $description "Runs the bootstrap initialization file in the user's home directory, unless the " { $snippet "-no-user-init" } " command line switch was given. This file is named " { $snippet ".factor-boot-rc" } " on Unix and " { $snippet "factor-boot-rc" } " on Windows." } ;
HELP: run-user-init HELP: run-user-init
{ $description "Runs the " { $snippet ".factor-rc" } " file in the user's home directory unless the " { $snippet "-no-user-init" } " command line switch was given." } ; { $description "Runs the startup initialization file in the user's home directory, unless the " { $snippet "-no-user-init" } " command line switch was given. This file is named " { $snippet ".factor-rc" } " on Unix and " { $snippet "factor-rc" } " on Windows." } ;
HELP: cli-param HELP: load-vocab-roots
{ $description "Loads the newline-separated list of additional vocabulary roots from the file named " { $snippet ".factor-roots" } " on Unix and " { $snippet "factor-roots" } " on Windows." } ;
HELP: param
{ $values { "param" string } } { $values { "param" string } }
{ $description "Process a command-line switch." { $description "Process a command-line switch."
$nl $nl
@ -17,10 +21,13 @@ $nl
$nl $nl
"Otherwise, sets the global variable named by the parameter to " { $link t } "." } ; "Otherwise, sets the global variable named by the parameter to " { $link t } "." } ;
HELP: cli-args HELP: (command-line)
{ $values { "args" "a sequence of strings" } } { $values { "args" "a sequence of strings" } }
{ $description "Outputs the command line parameters which were passed to the Factor VM on startup." } ; { $description "Outputs the command line parameters which were passed to the Factor VM on startup." } ;
HELP: command-line
{ $var-description "The command line parameters which follow the name of the script on the command line." } ;
HELP: main-vocab-hook HELP: main-vocab-hook
{ $var-description "Global variable holding a quotation which outputs a vocabulary name. UI backends set this so that the UI can automatically start if the prerequisites are met (for example, " { $snippet "$DISPLAY" } " being set on X11)." } ; { $var-description "Global variable holding a quotation which outputs a vocabulary name. UI backends set this so that the UI can automatically start if the prerequisites are met (for example, " { $snippet "$DISPLAY" } " being set on X11)." } ;
@ -35,9 +42,6 @@ HELP: ignore-cli-args?
{ $values { "?" "a boolean" } } { $values { "?" "a boolean" } }
{ $description "On Mac OS X, source files to run are supplied by the Cocoa API, so to avoid running them twice the startup code has to call this word." } ; { $description "On Mac OS X, source files to run are supplied by the Cocoa API, so to avoid running them twice the startup code has to call this word." } ;
HELP: parse-command-line
{ $description "Called on startup to process command line arguments. This sets global variables with " { $link cli-param } ", runs source files, and evaluates the string given by the " { $snippet "-e" } " switch, if there is one." } ;
ARTICLE: "runtime-cli-args" "Command line switches for the VM" ARTICLE: "runtime-cli-args" "Command line switches for the VM"
"A handful of command line switches are processed by the VM and not the library. They control low-level features." "A handful of command line switches are processed by the VM and not the library. They control low-level features."
{ $table { $table
@ -57,16 +61,19 @@ ARTICLE: "bootstrap-cli-args" "Command line switches for bootstrap"
"A number of command line switches can be passed to a bootstrap image to modify the behavior of the resulting image:" "A number of command line switches can be passed to a bootstrap image to modify the behavior of the resulting image:"
{ $table { $table
{ { $snippet "-output-image=" { $emphasis "image" } } { "Save the result to " { $snippet "image" } ". The default is " { $snippet "factor.image" } "." } } { { $snippet "-output-image=" { $emphasis "image" } } { "Save the result to " { $snippet "image" } ". The default is " { $snippet "factor.image" } "." } }
{ { $snippet "-no-user-init" } { "Inhibits the running of the " { $snippet ".factor-boot-rc" } " file in the user's home directory." } } { { $snippet "-no-user-init" } { "Inhibits the running of user initialization files on startup. See " { $link "rc-files" } "." } }
{ { $snippet "-include=" { $emphasis "components..." } } "A list of components to include (see below)." } { { $snippet "-include=" { $emphasis "components..." } } "A list of components to include (see below)." }
{ { $snippet "-exclude=" { $emphasis "components..." } } "A list of components to exclude." } { { $snippet "-exclude=" { $emphasis "components..." } } "A list of components to exclude." }
{ { $snippet "-ui-backend=" { $emphasis "backend" } } { "One of " { $snippet "x11" } ", " { $snippet "windows" } ", or " { $snippet "cocoa" } ". The default is platform-specific." } } { { $snippet "-ui-backend=" { $emphasis "backend" } } { "One of " { $snippet "x11" } ", " { $snippet "windows" } ", or " { $snippet "cocoa" } ". The default is platform-specific." } }
} }
"Bootstrap can load various optional components:" "Bootstrap can load various optional components:"
{ $table { $table
{ { $snippet "math" } "Rational and complex number support." }
{ { $snippet "threads" } "Thread support." }
{ { $snippet "compiler" } "The compiler." } { { $snippet "compiler" } "The compiler." }
{ { $snippet "tools" } "Terminal-based developer tools." } { { $snippet "tools" } "Terminal-based developer tools." }
{ { $snippet "help" } "The help system." } { { $snippet "help" } "The help system." }
{ { $snippet "help.handbook" } "The help handbook." }
{ { $snippet "ui" } "The graphical user interface." } { { $snippet "ui" } "The graphical user interface." }
{ { $snippet "ui.tools" } "Graphical developer tools." } { { $snippet "ui.tools" } "Graphical developer tools." }
{ { $snippet "io" } "Non-blocking I/O and networking." } { { $snippet "io" } "Non-blocking I/O and networking." }
@ -74,9 +81,9 @@ ARTICLE: "bootstrap-cli-args" "Command line switches for bootstrap"
"By default, all optional components are loaded. To load all optional components except for a given list, use the " { $snippet "-exclude=" } " switch; to only load specified optional components, use the " { $snippet "-include=" } "." "By default, all optional components are loaded. To load all optional components except for a given list, use the " { $snippet "-exclude=" } " switch; to only load specified optional components, use the " { $snippet "-include=" } "."
$nl $nl
"For example, to build an image with the compiler but no other components, you could do:" "For example, to build an image with the compiler but no other components, you could do:"
{ $code "./factor -i=boot.ppc.image -include=compiler" } { $code "./factor -i=boot.macosx-ppc.image -include=compiler" }
"To build an image with everything except for the user interface and graphical tools," "To build an image with everything except for the user interface and graphical tools,"
{ $code "./factor -i=boot.ppc.image -exclude=\"ui ui.tools\"" } { $code "./factor -i=boot.macosx-ppc.image -exclude=\"ui ui.tools\"" }
"To generate a bootstrap image in the first place, see " { $link "bootstrap.image" } "." ; "To generate a bootstrap image in the first place, see " { $link "bootstrap.image" } "." ;
ARTICLE: "standard-cli-args" "Command line switches for general usage" ARTICLE: "standard-cli-args" "Command line switches for general usage"
@ -84,20 +91,60 @@ ARTICLE: "standard-cli-args" "Command line switches for general usage"
{ $table { $table
{ { $snippet "-e=" { $emphasis "code" } } { "This specifies a code snippet to evaluate. If you want Factor to exit immediately after, also specify " { $snippet "-run=none" } "." } } { { $snippet "-e=" { $emphasis "code" } } { "This specifies a code snippet to evaluate. If you want Factor to exit immediately after, also specify " { $snippet "-run=none" } "." } }
{ { $snippet "-run=" { $emphasis "vocab" } } { { $snippet { $emphasis "vocab" } } " is the name of a vocabulary with a " { $link POSTPONE: MAIN: } " hook to run on startup, for example " { $vocab-link "listener" } ", " { $vocab-link "ui" } " or " { $vocab-link "none" } "." } } { { $snippet "-run=" { $emphasis "vocab" } } { { $snippet { $emphasis "vocab" } } " is the name of a vocabulary with a " { $link POSTPONE: MAIN: } " hook to run on startup, for example " { $vocab-link "listener" } ", " { $vocab-link "ui" } " or " { $vocab-link "none" } "." } }
{ { $snippet "-no-user-init" } { "Inhibits the running of the " { $snippet ".factor-rc" } " file in the user's home directory on startup." } } { { $snippet "-no-user-init" } { "Inhibits the running of user initialization files on startup. See " { $link "rc-files" } "." } }
{ { $snippet "-quiet" } { "If set, " { $link run-file } " and " { $link require } " will not print load messages." } } { { $snippet "-quiet" } { "If set, " { $link run-file } " and " { $link require } " will not print load messages." } }
{ { $snippet "-script" } { "Equivalent to " { $snippet "-quiet -run=none" } "." $nl "On Unix systems, Factor can be used for scripting - just create an executable text file whose first line is:" { $code "#! /usr/local/bin/factor -script" } "The space after " { $snippet "#!" } " is necessary because of Factor syntax." } }
} ; } ;
ARTICLE: "rc-files" "Running code on startup" ARTICLE: "factor-boot-rc" "Bootstrap initialization file"
"Unless the " { $snippet "-no-user-init" } " command line switch is specified, The startup routine runs the " { $snippet ".factor-rc" } " file in the user's home directory, if it exists. This file can contain initialization and customization for your development environment." "The botstrap initialization file is named " { $snippet "factor-boot-rc" } " on Windows and " { $snippet ".factor-boot-rc" } " on Unix. This file can contain " { $link require } " calls for vocabularies you use frequently, and other such long-running tasks that you do not want to perform every time Factor starts."
$nl $nl
"The " { $snippet ".factor-rc" } " and " { $snippet ".factor-boot-rc" } " files can be run explicitly:" "A word to run this file from an existing Factor session:"
{ $subsection run-user-init } { $subsection run-bootstrap-init }
{ $subsection run-bootstrap-init } ; "For example, if you changed " { $snippet ".factor-boot-rc" } " and do not want to bootstrap again, you can just invoke " { $link run-bootstrap-init } " in the listener." ;
ARTICLE: "cli" "Command line usage" ARTICLE: "factor-rc" "Startup initialization file"
"Zero or more command line arguments may be passed to the Factor runtime. Command line arguments starting with a dash (" { $snippet "-" } ") is interpreted as switches. All other arguments are taken to be file names to be run by " { $link run-file } "." "The startup initialization file is named " { $snippet "factor-rc" } " on Windows and " { $snippet ".factor-rc" } " on Unix. If it exists, it is run every time Factor starts."
$nl
"A word to run this file from an existing Factor session:"
{ $subsection run-user-init } ;
ARTICLE: "factor-roots" "Additional vocabulary roots file"
"The vocabulary roots file is named " { $snippet "factor-roots" } " on Windows and " { $snippet ".factor-roots" } " on Unix. If it exists, it is loaded every time Factor starts. It contains a newline-separated list of " { $link "vocabs.roots" } "."
$nl
"A word to run this file from an existing Factor session:"
{ $subsection load-vocab-roots } ;
ARTICLE: "rc-files" "Running code on startup"
"Factor looks for three optional files in your home directory."
{ $subsection "factor-boot-rc" }
{ $subsection "factor-rc" }
{ $subsection "factor-roots" }
"The " { $snippet "-no-user-init" } " command line switch will inhibit loading running of these files."
$nl
"If you are unsure where the files should be located, evaluate the following code:"
{ $code
"USE: command-line"
"\"factor-rc\" rc-path print"
"\"factor-boot-rc\" rc-path print"
}
"Here is an example " { $snippet ".factor-boot-rc" } " which sets up GVIM editor integration, adds an additional vocabulary root (see " { $link "vocabs.roots" } "), and increases the font size in the UI by setting the DPI (dots-per-inch) variable:"
{ $code
"USING: editors.gvim vocabs.loader ui.freetype namespaces sequences ;"
"\"/opt/local/bin\" \\ gvim-path set-global"
"\"/home/jane/src/\" vocab-roots get push"
"100 dpi set-global"
} ;
ARTICLE: "cli" "Command line arguments"
"Factor command line usage:"
{ $code "factor [system switches...] [script args...]" }
"Zero or more system switches can be passed in, followed by an optional script file name. If the script file is specified, it will be run on startup, any arguments after the script file are stored in a variable, with no further processing by Factor itself:"
{ $subsection command-line }
"Instead of running a script, it is also possible to run a vocabulary; this invokes the vocabulary's " { $link POSTPONE: MAIN: } " word:"
{ $code "factor [system switches...] -run=<vocab name>" }
"If no script file or " { $snippet "-run=" } " switch is specified, Factor will start " { $link "listener" } " or " { $link "ui-tools" } ", depending on the operating system."
$nl
"As stated above, arguments in the first part of the command line, before the optional script name, are interpreted by to the Factor system. These arguments all start with a dash (" { $snippet "-" } ")."
$nl $nl
"Switches can take one of the following three forms:" "Switches can take one of the following three forms:"
{ $list { $list
@ -108,9 +155,9 @@ $nl
{ $subsection "runtime-cli-args" } { $subsection "runtime-cli-args" }
{ $subsection "bootstrap-cli-args" } { $subsection "bootstrap-cli-args" }
{ $subsection "standard-cli-args" } { $subsection "standard-cli-args" }
"The list of command line arguments can be obtained and inspected directly:" "The raw list of command line arguments can also be obtained and inspected directly:"
{ $subsection cli-args } { $subsection (command-line) }
"There is a way to override the default vocabulary to run on startup:" "There is a way to override the default vocabulary to run on startup, if no script name or " { $snippet "-run" } " switch is specified:"
{ $subsection main-vocab-hook } ; { $subsection main-vocab-hook } ;
ABOUT: "cli" ABOUT: "cli"

View File

@ -1,12 +0,0 @@
USING: namespaces tools.test kernel command-line ;
IN: command-line.tests
[
[ f ] [ "-no-user-init" cli-arg ] unit-test
[ f ] [ "user-init" get ] unit-test
[ f ] [ "-user-init" cli-arg ] unit-test
[ t ] [ "user-init" get ] unit-test
[ "sdl.factor" ] [ "sdl.factor" cli-arg ] unit-test
] with-scope

View File

@ -1,31 +1,56 @@
! Copyright (C) 2003, 2008 Slava Pestov. ! Copyright (C) 2003, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: init continuations debugger hashtables io kernel USING: init continuations debugger hashtables io
kernel.private namespaces parser sequences strings system io.encodings.utf8 io.files kernel kernel.private namespaces
splitting io.files eval ; parser sequences strings system splitting eval vocabs.loader ;
IN: command-line IN: command-line
SYMBOL: script
SYMBOL: command-line
: (command-line) ( -- args ) 10 getenv sift ;
: rc-path ( name -- path )
os windows? [ "." prepend ] unless
home prepend-path ;
: run-bootstrap-init ( -- ) : run-bootstrap-init ( -- )
"user-init" get [ "user-init" get [
home ".factor-boot-rc" append-path ?run-file "factor-boot-rc" rc-path ?run-file
] when ; ] when ;
: run-user-init ( -- ) : run-user-init ( -- )
"user-init" get [ "user-init" get [
home ".factor-rc" append-path ?run-file "factor-rc" rc-path ?run-file
] when ; ] when ;
: cli-var-param ( name value -- ) swap set-global ; : load-vocab-roots ( -- )
"user-init" get [
"factor-roots" rc-path dup exists? [
utf8 file-lines [ add-vocab-root ] each
] [ drop ] if
] when ;
: cli-bool-param ( name -- ) "no-" ?head not cli-var-param ; <PRIVATE
: cli-param ( param -- ) : var-param ( name value -- ) swap set-global ;
"=" split1 [ cli-var-param ] [ cli-bool-param ] if* ;
: cli-arg ( argument -- argument ) : bool-param ( name -- ) "no-" ?head not var-param ;
"-" ?head [ cli-param f ] when ;
: cli-args ( -- args ) 10 getenv ; : param ( param -- )
"=" split1 [ var-param ] [ bool-param ] if* ;
: run-script ( file -- )
t "quiet" set-global run-file ;
PRIVATE>
: parse-command-line ( args -- )
[ command-line off script off ] [
unclip "-" ?head
[ param parse-command-line ]
[ script set command-line set ] if
] if-empty ;
SYMBOL: main-vocab-hook SYMBOL: main-vocab-hook
@ -49,14 +74,17 @@ SYMBOL: main-vocab-hook
: ignore-cli-args? ( -- ? ) : ignore-cli-args? ( -- ? )
os macosx? "run" get "ui" = and ; os macosx? "run" get "ui" = and ;
: script-mode ( -- ) : script-mode ( -- ) ;
t "quiet" set-global
"none" "run" set-global ;
: parse-command-line ( -- ) : handle-command-line ( -- )
cli-args [ cli-arg ] filter [
"script" get [ script-mode ] when (command-line) parse-command-line
ignore-cli-args? [ drop ] [ [ run-file ] each ] if load-vocab-roots
"e" get [ eval ] when* ; run-user-init
"e" get [ eval ] when*
ignore-cli-args? not script get and
[ run-script ] [ "run" get run ] if*
output-stream get [ stream-flush ] when*
] [ print-error 1 exit ] recover ;
[ default-cli-args ] "command-line" add-init-hook [ default-cli-args ] "command-line" add-init-hook

View File

@ -18,7 +18,7 @@ IN: compiler.alien
dup c-type-stack-align? [ c-type-align ] [ drop cell ] if ; dup c-type-stack-align? [ c-type-align ] [ drop cell ] if ;
: parameter-align ( n type -- n delta ) : parameter-align ( n type -- n delta )
over >r c-type-stack-align align dup r> - ; [ c-type-stack-align align dup ] [ drop ] 2bi - ;
: parameter-sizes ( types -- total offsets ) : parameter-sizes ( types -- total offsets )
#! Compute stack frame locations. #! Compute stack frame locations.

View File

@ -21,8 +21,6 @@ IN: compiler.cfg.builder
! Convert tree SSA IR to CFG SSA IR. ! Convert tree SSA IR to CFG SSA IR.
: stop-iterating ( -- next ) end-basic-block f ;
SYMBOL: procedures SYMBOL: procedures
SYMBOL: current-word SYMBOL: current-word
SYMBOL: current-label SYMBOL: current-label
@ -190,7 +188,7 @@ M: #if emit-node
: emit-dispatch ( node -- ) : emit-dispatch ( node -- )
##epilogue ##epilogue
ds-pop ^^offset>slot i ##dispatch ds-pop ^^offset>slot i 0 ##dispatch
dispatch-branches ; dispatch-branches ;
: <dispatch-block> ( -- word ) : <dispatch-block> ( -- word )
@ -211,7 +209,7 @@ M: #dispatch emit-node
! #call ! #call
M: #call emit-node M: #call emit-node
dup word>> dup "intrinsic" word-prop dup word>> dup "intrinsic" word-prop
[ emit-intrinsic iterate-next ] [ nip emit-call ] if ; [ emit-intrinsic ] [ nip emit-call ] if ;
! #call-recursive ! #call-recursive
M: #call-recursive emit-node label>> id>> emit-call ; M: #call-recursive emit-node label>> id>> emit-call ;
@ -221,21 +219,14 @@ M: #push emit-node
literal>> ^^load-literal ds-push iterate-next ; literal>> ^^load-literal ds-push iterate-next ;
! #shuffle ! #shuffle
: emit-shuffle ( effect -- )
[ out>> ] [ in>> dup length ds-load zip ] bi
'[ _ at ] map ds-store ;
M: #shuffle emit-node M: #shuffle emit-node
shuffle-effect emit-shuffle iterate-next ; dup
H{ } clone
M: #>r emit-node [ [ in-d>> [ length ds-load ] keep ] dip '[ _ set-at ] 2each ]
[ in-d>> length ] [ out-r>> empty? ] bi [ [ in-r>> [ length rs-load ] keep ] dip '[ _ set-at ] 2each ]
[ neg ##inc-d ] [ ds-load rs-store ] if [ nip ] 2tri
iterate-next ; [ [ [ out-d>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map ds-store ]
[ [ [ out-r>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map rs-store ] 2bi
M: #r> emit-node
[ in-r>> length ] [ out-d>> empty? ] bi
[ neg ##inc-r ] [ rs-load ds-store ] if
iterate-next ; iterate-next ;
! #return ! #return
@ -269,7 +260,7 @@ M: #terminate emit-node drop stop-iterating ;
: emit-alien-node ( node quot -- next ) : emit-alien-node ( node quot -- next )
[ params>> ] dip [ drop alien-stack-frame ] [ call ] 2bi [ params>> ] dip [ drop alien-stack-frame ] [ call ] 2bi
begin-basic-block iterate-next ; inline ##branch begin-basic-block iterate-next ; inline
M: #alien-invoke emit-node M: #alien-invoke emit-node
[ ##alien-invoke ] emit-alien-node ; [ ##alien-invoke ] emit-alien-node ;

View File

@ -12,9 +12,14 @@ M: ##write-barrier defs-vregs [ card#>> ] [ table>> ] bi 2array ;
M: ##unary/temp defs-vregs dst/tmp-vregs ; M: ##unary/temp defs-vregs dst/tmp-vregs ;
M: ##allot defs-vregs dst/tmp-vregs ; M: ##allot defs-vregs dst/tmp-vregs ;
M: ##dispatch defs-vregs temp>> 1array ; M: ##dispatch defs-vregs temp>> 1array ;
M: ##slot defs-vregs [ dst>> ] [ temp>> ] bi 2array ; M: ##slot defs-vregs dst/tmp-vregs ;
M: ##set-slot defs-vregs temp>> 1array ; M: ##set-slot defs-vregs temp>> 1array ;
M: ##string-nth defs-vregs [ dst>> ] [ temp>> ] bi 2array ; M: ##string-nth defs-vregs dst/tmp-vregs ;
M: ##compare defs-vregs dst/tmp-vregs ;
M: ##compare-imm defs-vregs dst/tmp-vregs ;
M: ##compare-float defs-vregs dst/tmp-vregs ;
M: ##fixnum-mul defs-vregs [ temp1>> ] [ temp2>> ] bi 2array ;
M: ##fixnum-mul-tail defs-vregs [ temp1>> ] [ temp2>> ] bi 2array ;
M: insn defs-vregs drop f ; M: insn defs-vregs drop f ;
M: ##unary uses-vregs src>> 1array ; M: ##unary uses-vregs src>> 1array ;
@ -31,6 +36,7 @@ M: ##compare-imm-branch uses-vregs src1>> 1array ;
M: ##dispatch uses-vregs src>> 1array ; M: ##dispatch uses-vregs src>> 1array ;
M: ##alien-getter uses-vregs src>> 1array ; M: ##alien-getter uses-vregs src>> 1array ;
M: ##alien-setter uses-vregs [ src>> ] [ value>> ] bi 2array ; M: ##alien-setter uses-vregs [ src>> ] [ value>> ] bi 2array ;
M: ##fixnum-overflow uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
M: _conditional-branch uses-vregs [ src1>> ] [ src2>> ] bi 2array ; M: _conditional-branch uses-vregs [ src1>> ] [ src2>> ] bi 2array ;
M: _compare-imm-branch uses-vregs src1>> 1array ; M: _compare-imm-branch uses-vregs src1>> 1array ;
M: insn uses-vregs drop f ; M: insn uses-vregs drop f ;
@ -40,6 +46,7 @@ UNION: vreg-insn
##write-barrier ##write-barrier
##dispatch ##dispatch
##effect ##effect
##fixnum-overflow
##conditional-branch ##conditional-branch
##compare-imm-branch ##compare-imm-branch
_conditional-branch _conditional-branch

View File

@ -65,9 +65,9 @@ IN: compiler.cfg.hats
: ^^alien-cell ( src -- dst ) ^^i1 ##alien-cell ; inline : ^^alien-cell ( src -- dst ) ^^i1 ##alien-cell ; inline
: ^^alien-float ( src -- dst ) ^^d1 ##alien-float ; inline : ^^alien-float ( src -- dst ) ^^d1 ##alien-float ; inline
: ^^alien-double ( src -- dst ) ^^d1 ##alien-double ; inline : ^^alien-double ( src -- dst ) ^^d1 ##alien-double ; inline
: ^^compare ( src1 src2 cc -- dst ) ^^i3 ##compare ; inline : ^^compare ( src1 src2 cc -- dst ) ^^i3 i ##compare ; inline
: ^^compare-imm ( src1 src2 cc -- dst ) ^^i3 ##compare-imm ; inline : ^^compare-imm ( src1 src2 cc -- dst ) ^^i3 i ##compare-imm ; inline
: ^^compare-float ( src1 src2 cc -- dst ) ^^i3 ##compare-float ; inline : ^^compare-float ( src1 src2 cc -- dst ) ^^i3 i ##compare-float ; inline
: ^^offset>slot ( vreg -- vreg' ) cell 4 = [ 1 ^^shr-imm ] when ; inline : ^^offset>slot ( vreg -- vreg' ) cell 4 = [ 1 ^^shr-imm ] when ; inline
: ^^tag-fixnum ( src -- dst ) ^^i1 ##tag-fixnum ; inline : ^^tag-fixnum ( src -- dst ) ^^i1 ##tag-fixnum ; inline
: ^^untag-fixnum ( src -- dst ) ^^i1 ##untag-fixnum ; inline : ^^untag-fixnum ( src -- dst ) ^^i1 ##untag-fixnum ; inline

View File

@ -62,7 +62,7 @@ INSN: ##jump word ;
INSN: ##return ; INSN: ##return ;
! Jump tables ! Jump tables
INSN: ##dispatch src temp ; INSN: ##dispatch src temp offset ;
INSN: ##dispatch-label label ; INSN: ##dispatch-label label ;
! Slot access ! Slot access
@ -92,6 +92,15 @@ INSN: ##shr-imm < ##binary-imm ;
INSN: ##sar-imm < ##binary-imm ; INSN: ##sar-imm < ##binary-imm ;
INSN: ##not < ##unary ; INSN: ##not < ##unary ;
! Overflowing arithmetic
TUPLE: ##fixnum-overflow < insn src1 src2 ;
INSN: ##fixnum-add < ##fixnum-overflow ;
INSN: ##fixnum-add-tail < ##fixnum-overflow ;
INSN: ##fixnum-sub < ##fixnum-overflow ;
INSN: ##fixnum-sub-tail < ##fixnum-overflow ;
INSN: ##fixnum-mul < ##fixnum-overflow temp1 temp2 ;
INSN: ##fixnum-mul-tail < ##fixnum-overflow temp1 temp2 ;
: ##tag-fixnum ( dst src -- ) tag-bits get ##shl-imm ; inline : ##tag-fixnum ( dst src -- ) tag-bits get ##shl-imm ; inline
: ##untag-fixnum ( dst src -- ) tag-bits get ##sar-imm ; inline : ##untag-fixnum ( dst src -- ) tag-bits get ##sar-imm ; inline
@ -198,11 +207,11 @@ TUPLE: ##conditional-branch < insn { src1 vreg } { src2 vreg } cc ;
INSN: ##compare-branch < ##conditional-branch ; INSN: ##compare-branch < ##conditional-branch ;
INSN: ##compare-imm-branch { src1 vreg } { src2 integer } cc ; INSN: ##compare-imm-branch { src1 vreg } { src2 integer } cc ;
INSN: ##compare < ##binary cc ; INSN: ##compare < ##binary cc temp ;
INSN: ##compare-imm < ##binary-imm cc ; INSN: ##compare-imm < ##binary-imm cc temp ;
INSN: ##compare-float-branch < ##conditional-branch ; INSN: ##compare-float-branch < ##conditional-branch ;
INSN: ##compare-float < ##binary cc ; INSN: ##compare-float < ##binary cc temp ;
! Instructions used by machine IR only. ! Instructions used by machine IR only.
INSN: _prologue stack-frame ; INSN: _prologue stack-frame ;

View File

@ -3,10 +3,22 @@
USING: sequences accessors layouts kernel math namespaces USING: sequences accessors layouts kernel math namespaces
combinators fry locals combinators fry locals
compiler.tree.propagation.info compiler.tree.propagation.info
compiler.cfg.stacks compiler.cfg.hats compiler.cfg.instructions compiler.cfg.hats
compiler.cfg.utilities ; compiler.cfg.stacks
compiler.cfg.iterator
compiler.cfg.instructions
compiler.cfg.utilities
compiler.cfg.registers ;
IN: compiler.cfg.intrinsics.fixnum IN: compiler.cfg.intrinsics.fixnum
: emit-both-fixnums? ( -- )
D 0 ^^peek
D 1 ^^peek
^^or
tag-mask get ^^and-imm
0 cc= ^^compare-imm
ds-push ;
: (emit-fixnum-imm-op) ( infos insn -- dst ) : (emit-fixnum-imm-op) ( infos insn -- dst )
ds-drop ds-drop
[ ds-pop ] [ ds-pop ]
@ -64,3 +76,16 @@ IN: compiler.cfg.intrinsics.fixnum
: emit-fixnum>bignum ( -- ) : emit-fixnum>bignum ( -- )
ds-pop ^^untag-fixnum ^^integer>bignum ds-push ; ds-pop ^^untag-fixnum ^^integer>bignum ds-push ;
: emit-fixnum-overflow-op ( quot quot-tail -- next )
[ 2inputs 1 ##inc-d ] 2dip
tail-call? [
##epilogue
nip call
stop-iterating
] [
drop call
##branch
begin-basic-block
iterate-next
] if ; inline

View File

@ -8,7 +8,8 @@ compiler.cfg.intrinsics.alien
compiler.cfg.intrinsics.allot compiler.cfg.intrinsics.allot
compiler.cfg.intrinsics.fixnum compiler.cfg.intrinsics.fixnum
compiler.cfg.intrinsics.float compiler.cfg.intrinsics.float
compiler.cfg.intrinsics.slots ; compiler.cfg.intrinsics.slots
compiler.cfg.iterator ;
QUALIFIED: kernel QUALIFIED: kernel
QUALIFIED: arrays QUALIFIED: arrays
QUALIFIED: byte-arrays QUALIFIED: byte-arrays
@ -22,6 +23,10 @@ IN: compiler.cfg.intrinsics
{ {
kernel.private:tag kernel.private:tag
math.private:both-fixnums?
math.private:fixnum+
math.private:fixnum-
math.private:fixnum*
math.private:fixnum+fast math.private:fixnum+fast
math.private:fixnum-fast math.private:fixnum-fast
math.private:fixnum-bitand math.private:fixnum-bitand
@ -85,60 +90,64 @@ IN: compiler.cfg.intrinsics
alien.accessors:set-alien-double alien.accessors:set-alien-double
} [ t "intrinsic" set-word-prop ] each ; } [ t "intrinsic" set-word-prop ] each ;
: emit-intrinsic ( node word -- ) : emit-intrinsic ( node word -- node/f )
{ {
{ \ kernel.private:tag [ drop emit-tag ] } { \ kernel.private:tag [ drop emit-tag iterate-next ] }
{ \ math.private:fixnum+fast [ [ ^^add ] [ ^^add-imm ] emit-fixnum-op ] } { \ math.private:both-fixnums? [ drop emit-both-fixnums? iterate-next ] }
{ \ math.private:fixnum-fast [ [ ^^sub ] [ ^^sub-imm ] emit-fixnum-op ] } { \ math.private:fixnum+ [ drop [ ##fixnum-add ] [ ##fixnum-add-tail ] emit-fixnum-overflow-op ] }
{ \ math.private:fixnum-bitand [ [ ^^and ] [ ^^and-imm ] emit-fixnum-op ] } { \ math.private:fixnum- [ drop [ ##fixnum-sub ] [ ##fixnum-sub-tail ] emit-fixnum-overflow-op ] }
{ \ math.private:fixnum-bitor [ [ ^^or ] [ ^^or-imm ] emit-fixnum-op ] } { \ math.private:fixnum* [ drop [ i i ##fixnum-mul ] [ i i ##fixnum-mul-tail ] emit-fixnum-overflow-op ] }
{ \ math.private:fixnum-bitxor [ [ ^^xor ] [ ^^xor-imm ] emit-fixnum-op ] } { \ math.private:fixnum+fast [ [ ^^add ] [ ^^add-imm ] emit-fixnum-op iterate-next ] }
{ \ math.private:fixnum-shift-fast [ emit-fixnum-shift-fast ] } { \ math.private:fixnum-fast [ [ ^^sub ] [ ^^sub-imm ] emit-fixnum-op iterate-next ] }
{ \ math.private:fixnum-bitnot [ drop emit-fixnum-bitnot ] } { \ math.private:fixnum-bitand [ [ ^^and ] [ ^^and-imm ] emit-fixnum-op iterate-next ] }
{ \ math.private:fixnum*fast [ emit-fixnum*fast ] } { \ math.private:fixnum-bitor [ [ ^^or ] [ ^^or-imm ] emit-fixnum-op iterate-next ] }
{ \ math.private:fixnum< [ cc< emit-fixnum-comparison ] } { \ math.private:fixnum-bitxor [ [ ^^xor ] [ ^^xor-imm ] emit-fixnum-op iterate-next ] }
{ \ math.private:fixnum<= [ cc<= emit-fixnum-comparison ] } { \ math.private:fixnum-shift-fast [ emit-fixnum-shift-fast iterate-next ] }
{ \ math.private:fixnum>= [ cc>= emit-fixnum-comparison ] } { \ math.private:fixnum-bitnot [ drop emit-fixnum-bitnot iterate-next ] }
{ \ math.private:fixnum> [ cc> emit-fixnum-comparison ] } { \ math.private:fixnum*fast [ emit-fixnum*fast iterate-next ] }
{ \ kernel:eq? [ cc= emit-fixnum-comparison ] } { \ math.private:fixnum< [ cc< emit-fixnum-comparison iterate-next ] }
{ \ math.private:bignum>fixnum [ drop emit-bignum>fixnum ] } { \ math.private:fixnum<= [ cc<= emit-fixnum-comparison iterate-next ] }
{ \ math.private:fixnum>bignum [ drop emit-fixnum>bignum ] } { \ math.private:fixnum>= [ cc>= emit-fixnum-comparison iterate-next ] }
{ \ math.private:float+ [ drop [ ^^add-float ] emit-float-op ] } { \ math.private:fixnum> [ cc> emit-fixnum-comparison iterate-next ] }
{ \ math.private:float- [ drop [ ^^sub-float ] emit-float-op ] } { \ kernel:eq? [ cc= emit-fixnum-comparison iterate-next ] }
{ \ math.private:float* [ drop [ ^^mul-float ] emit-float-op ] } { \ math.private:bignum>fixnum [ drop emit-bignum>fixnum iterate-next ] }
{ \ math.private:float/f [ drop [ ^^div-float ] emit-float-op ] } { \ math.private:fixnum>bignum [ drop emit-fixnum>bignum iterate-next ] }
{ \ math.private:float< [ drop cc< emit-float-comparison ] } { \ math.private:float+ [ drop [ ^^add-float ] emit-float-op iterate-next ] }
{ \ math.private:float<= [ drop cc<= emit-float-comparison ] } { \ math.private:float- [ drop [ ^^sub-float ] emit-float-op iterate-next ] }
{ \ math.private:float>= [ drop cc>= emit-float-comparison ] } { \ math.private:float* [ drop [ ^^mul-float ] emit-float-op iterate-next ] }
{ \ math.private:float> [ drop cc> emit-float-comparison ] } { \ math.private:float/f [ drop [ ^^div-float ] emit-float-op iterate-next ] }
{ \ math.private:float= [ drop cc= emit-float-comparison ] } { \ math.private:float< [ drop cc< emit-float-comparison iterate-next ] }
{ \ math.private:float>fixnum [ drop emit-float>fixnum ] } { \ math.private:float<= [ drop cc<= emit-float-comparison iterate-next ] }
{ \ math.private:fixnum>float [ drop emit-fixnum>float ] } { \ math.private:float>= [ drop cc>= emit-float-comparison iterate-next ] }
{ \ slots.private:slot [ emit-slot ] } { \ math.private:float> [ drop cc> emit-float-comparison iterate-next ] }
{ \ slots.private:set-slot [ emit-set-slot ] } { \ math.private:float= [ drop cc= emit-float-comparison iterate-next ] }
{ \ strings.private:string-nth [ drop emit-string-nth ] } { \ math.private:float>fixnum [ drop emit-float>fixnum iterate-next ] }
{ \ classes.tuple.private:<tuple-boa> [ emit-<tuple-boa> ] } { \ math.private:fixnum>float [ drop emit-fixnum>float iterate-next ] }
{ \ arrays:<array> [ emit-<array> ] } { \ slots.private:slot [ emit-slot iterate-next ] }
{ \ byte-arrays:<byte-array> [ emit-<byte-array> ] } { \ slots.private:set-slot [ emit-set-slot iterate-next ] }
{ \ math.private:<complex> [ emit-simple-allot ] } { \ strings.private:string-nth [ drop emit-string-nth iterate-next ] }
{ \ math.private:<ratio> [ emit-simple-allot ] } { \ classes.tuple.private:<tuple-boa> [ emit-<tuple-boa> iterate-next ] }
{ \ kernel:<wrapper> [ emit-simple-allot ] } { \ arrays:<array> [ emit-<array> iterate-next ] }
{ \ alien.accessors:alien-unsigned-1 [ 1 emit-alien-unsigned-getter ] } { \ byte-arrays:<byte-array> [ emit-<byte-array> iterate-next ] }
{ \ alien.accessors:set-alien-unsigned-1 [ 1 emit-alien-integer-setter ] } { \ math.private:<complex> [ emit-simple-allot iterate-next ] }
{ \ alien.accessors:alien-signed-1 [ 1 emit-alien-signed-getter ] } { \ math.private:<ratio> [ emit-simple-allot iterate-next ] }
{ \ alien.accessors:set-alien-signed-1 [ 1 emit-alien-integer-setter ] } { \ kernel:<wrapper> [ emit-simple-allot iterate-next ] }
{ \ alien.accessors:alien-unsigned-2 [ 2 emit-alien-unsigned-getter ] } { \ alien.accessors:alien-unsigned-1 [ 1 emit-alien-unsigned-getter iterate-next ] }
{ \ alien.accessors:set-alien-unsigned-2 [ 2 emit-alien-integer-setter ] } { \ alien.accessors:set-alien-unsigned-1 [ 1 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-signed-2 [ 2 emit-alien-signed-getter ] } { \ alien.accessors:alien-signed-1 [ 1 emit-alien-signed-getter iterate-next ] }
{ \ alien.accessors:set-alien-signed-2 [ 2 emit-alien-integer-setter ] } { \ alien.accessors:set-alien-signed-1 [ 1 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-unsigned-4 [ 4 emit-alien-unsigned-getter ] } { \ alien.accessors:alien-unsigned-2 [ 2 emit-alien-unsigned-getter iterate-next ] }
{ \ alien.accessors:set-alien-unsigned-4 [ 4 emit-alien-integer-setter ] } { \ alien.accessors:set-alien-unsigned-2 [ 2 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-signed-4 [ 4 emit-alien-signed-getter ] } { \ alien.accessors:alien-signed-2 [ 2 emit-alien-signed-getter iterate-next ] }
{ \ alien.accessors:set-alien-signed-4 [ 4 emit-alien-integer-setter ] } { \ alien.accessors:set-alien-signed-2 [ 2 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-cell [ emit-alien-cell-getter ] } { \ alien.accessors:alien-unsigned-4 [ 4 emit-alien-unsigned-getter iterate-next ] }
{ \ alien.accessors:set-alien-cell [ emit-alien-cell-setter ] } { \ alien.accessors:set-alien-unsigned-4 [ 4 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-float [ single-float-regs emit-alien-float-getter ] } { \ alien.accessors:alien-signed-4 [ 4 emit-alien-signed-getter iterate-next ] }
{ \ alien.accessors:set-alien-float [ single-float-regs emit-alien-float-setter ] } { \ alien.accessors:set-alien-signed-4 [ 4 emit-alien-integer-setter iterate-next ] }
{ \ alien.accessors:alien-double [ double-float-regs emit-alien-float-getter ] } { \ alien.accessors:alien-cell [ emit-alien-cell-getter iterate-next ] }
{ \ alien.accessors:set-alien-double [ double-float-regs emit-alien-float-setter ] } { \ alien.accessors:set-alien-cell [ emit-alien-cell-setter iterate-next ] }
{ \ alien.accessors:alien-float [ single-float-regs emit-alien-float-getter iterate-next ] }
{ \ alien.accessors:set-alien-float [ single-float-regs emit-alien-float-setter iterate-next ] }
{ \ alien.accessors:alien-double [ double-float-regs emit-alien-float-getter iterate-next ] }
{ \ alien.accessors:set-alien-double [ double-float-regs emit-alien-float-setter iterate-next ] }
} case ; } case ;

View File

@ -43,8 +43,8 @@ M: ##branch linearize-insn
: binary-conditional ( basic-block insn -- basic-block successor label2 src1 src2 cc ) : binary-conditional ( basic-block insn -- basic-block successor label2 src1 src2 cc )
[ (binary-conditional) ] [ (binary-conditional) ]
[ drop dup successors>> first useless-branch? ] 2bi [ drop dup successors>> second useless-branch? ] 2bi
[ [ number>> ] 3dip negate-cc ] [ [ swap number>> ] 3dip ] if ; [ [ swap number>> ] 3dip ] [ [ number>> ] 3dip negate-cc ] if ;
M: ##compare-branch linearize-insn M: ##compare-branch linearize-insn
binary-conditional _compare-branch emit-branch ; binary-conditional _compare-branch emit-branch ;

View File

@ -9,7 +9,10 @@ SYMBOL: visited
: post-order-traversal ( bb -- ) : post-order-traversal ( bb -- )
dup id>> visited get key? [ drop ] [ dup id>> visited get key? [ drop ] [
dup id>> visited get conjoin dup id>> visited get conjoin
[ successors>> [ post-order-traversal ] each ] [ , ] bi [
successors>> <reversed>
[ post-order-traversal ] each
] [ , ] bi
] if ; ] if ;
: post-order ( bb -- blocks ) : post-order ( bb -- blocks )

View File

@ -34,6 +34,12 @@ M: insn compute-stack-frame*
\ _gc t frame-required? set-word-prop \ _gc t frame-required? set-word-prop
\ _spill t frame-required? set-word-prop \ _spill t frame-required? set-word-prop
\ ##fixnum-add t frame-required? set-word-prop
\ ##fixnum-sub t frame-required? set-word-prop
\ ##fixnum-mul t frame-required? set-word-prop
\ ##fixnum-add-tail f frame-required? set-word-prop
\ ##fixnum-sub-tail f frame-required? set-word-prop
\ ##fixnum-mul-tail f frame-required? set-word-prop
: compute-stack-frame ( insns -- ) : compute-stack-frame ( insns -- )
frame-required? off frame-required? off

View File

@ -15,16 +15,28 @@ IN: compiler.cfg.stacks
1 ##inc-d D 0 ##replace ; 1 ##inc-d D 0 ##replace ;
: ds-load ( n -- vregs ) : ds-load ( n -- vregs )
[ <reversed> [ <ds-loc> ^^peek ] map ] [ neg ##inc-d ] bi ; dup 0 =
[ drop f ]
[ [ <reversed> [ <ds-loc> ^^peek ] map ] [ neg ##inc-d ] bi ] if ;
: ds-store ( vregs -- ) : ds-store ( vregs -- )
<reversed> [ length ##inc-d ] [ [ <ds-loc> ##replace ] each-index ] bi ; [
<reversed>
[ length ##inc-d ]
[ [ <ds-loc> ##replace ] each-index ] bi
] unless-empty ;
: rs-load ( n -- vregs ) : rs-load ( n -- vregs )
[ <reversed> [ <rs-loc> ^^peek ] map ] [ neg ##inc-r ] bi ; dup 0 =
[ drop f ]
[ [ <reversed> [ <rs-loc> ^^peek ] map ] [ neg ##inc-r ] bi ] if ;
: rs-store ( vregs -- ) : rs-store ( vregs -- )
<reversed> [ length ##inc-r ] [ [ <rs-loc> ##replace ] each-index ] bi ; [
<reversed>
[ length ##inc-r ]
[ [ <rs-loc> ##replace ] each-index ] bi
] unless-empty ;
: 2inputs ( -- vreg1 vreg2 ) : 2inputs ( -- vreg1 vreg2 )
D 1 ^^peek D 0 ^^peek -2 ##inc-d ; D 1 ^^peek D 0 ^^peek -2 ##inc-d ;

View File

@ -33,5 +33,7 @@ IN: compiler.cfg.utilities
building off building off
basic-block off ; basic-block off ;
: stop-iterating ( -- next ) end-basic-block f ;
: emit-primitive ( node -- ) : emit-primitive ( node -- )
word>> ##call ##branch begin-basic-block ; word>> ##call ##branch begin-basic-block ;

View File

@ -62,4 +62,8 @@ M: ##compare-imm-branch propagate
M: ##dispatch propagate M: ##dispatch propagate
[ resolve ] change-src ; [ resolve ] change-src ;
M: ##fixnum-overflow propagate
[ resolve ] change-src1
[ resolve ] change-src2 ;
M: insn propagate ; M: insn propagate ;

View File

@ -1,7 +1,8 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences layouts accessors combinators namespaces USING: kernel sequences layouts accessors combinators namespaces
math math fry
compiler.cfg.hats
compiler.cfg.instructions compiler.cfg.instructions
compiler.cfg.value-numbering.graph compiler.cfg.value-numbering.graph
compiler.cfg.value-numbering.simplify compiler.cfg.value-numbering.simplify
@ -63,7 +64,7 @@ M: ##compare-imm-branch rewrite-tagged-comparison
M: ##compare-imm rewrite-tagged-comparison M: ##compare-imm rewrite-tagged-comparison
[ dst>> ] [ (rewrite-tagged-comparison) ] bi [ dst>> ] [ (rewrite-tagged-comparison) ] bi
f \ ##compare-imm boa ; i f \ ##compare-imm boa ;
M: ##compare-imm-branch rewrite M: ##compare-imm-branch rewrite
dup rewrite-boolean-comparison? [ rewrite-boolean-comparison ] when dup rewrite-boolean-comparison? [ rewrite-boolean-comparison ] when
@ -78,7 +79,7 @@ M: ##compare-imm-branch rewrite
[ dst>> ] [ dst>> ]
[ src2>> ] [ src2>> ]
[ src1>> vreg>vn vn>constant ] tri [ src1>> vreg>vn vn>constant ] tri
cc= f \ ##compare-imm boa ; cc= f i \ ##compare-imm boa ;
M: ##compare rewrite M: ##compare rewrite
dup flip-comparison? [ dup flip-comparison? [
@ -95,9 +96,9 @@ M: ##compare rewrite
: rewrite-redundant-comparison ( insn -- insn' ) : rewrite-redundant-comparison ( insn -- insn' )
[ cc>> ] [ dst>> ] [ src1>> vreg>expr dup op>> ] tri { [ cc>> ] [ dst>> ] [ src1>> vreg>expr dup op>> ] tri {
{ \ ##compare [ >compare-expr< f \ ##compare boa ] } { \ ##compare [ >compare-expr< i f \ ##compare boa ] }
{ \ ##compare-imm [ >compare-imm-expr< f \ ##compare-imm boa ] } { \ ##compare-imm [ >compare-imm-expr< i f \ ##compare-imm boa ] }
{ \ ##compare-float [ >compare-expr< f \ ##compare-float boa ] } { \ ##compare-float [ >compare-expr< i f \ ##compare-float boa ] }
} case } case
swap cc= eq? [ [ negate-cc ] change-cc ] when ; swap cc= eq? [ [ negate-cc ] change-cc ] when ;
@ -113,4 +114,18 @@ M: ##compare-imm rewrite
] when ] when
] when ; ] when ;
: dispatch-offset ( expr -- n )
[ in2>> vn>constant tag-bits get neg shift ] [ op>> ] bi
\ ##sub-imm eq? [ neg ] when ;
: add-dispatch-offset? ( insn -- expr ? )
src>> vreg>expr dup op>> { ##add-imm ##sub-imm } memq? ; inline
M: ##dispatch rewrite
dup add-dispatch-offset? [
[ clone ] dip
[ in1>> vn>vreg >>src ]
[ dispatch-offset '[ _ + ] change-offset ] bi
] [ drop ] if ;
M: insn rewrite ; M: insn rewrite ;

View File

@ -1,6 +1,17 @@
IN: compiler.cfg.value-numbering.tests IN: compiler.cfg.value-numbering.tests
USING: compiler.cfg.value-numbering compiler.cfg.instructions USING: compiler.cfg.value-numbering compiler.cfg.instructions
compiler.cfg.registers cpu.architecture tools.test kernel math ; compiler.cfg.registers cpu.architecture tools.test kernel math
combinators.short-circuit accessors sequences ;
: trim-temps ( insns -- insns )
[
dup {
[ ##compare? ]
[ ##compare-imm? ]
[ ##compare-float? ]
} 1|| [ f >>temp ] when
] map ;
[ [
{ {
T{ ##peek f V int-regs 45 D 1 } T{ ##peek f V int-regs 45 D 1 }
@ -34,7 +45,7 @@ compiler.cfg.registers cpu.architecture tools.test kernel math ;
[ t ] [ [ t ] [
{ {
T{ ##peek f V int-regs 1 D 0 } T{ ##peek f V int-regs 1 D 0 }
T{ ##dispatch f V int-regs 1 V int-regs 2 } T{ ##dispatch f V int-regs 1 V int-regs 2 0 }
} dup value-numbering = } dup value-numbering =
] unit-test ] unit-test
@ -82,7 +93,7 @@ compiler.cfg.registers cpu.architecture tools.test kernel math ;
T{ ##compare f V int-regs 4 V int-regs 2 V int-regs 1 cc> } T{ ##compare f V int-regs 4 V int-regs 2 V int-regs 1 cc> }
T{ ##compare-imm f V int-regs 6 V int-regs 4 7 cc/= } T{ ##compare-imm f V int-regs 6 V int-regs 4 7 cc/= }
T{ ##replace f V int-regs 6 D 0 } T{ ##replace f V int-regs 6 D 0 }
} value-numbering } value-numbering trim-temps
] unit-test ] unit-test
[ [
@ -100,7 +111,7 @@ compiler.cfg.registers cpu.architecture tools.test kernel math ;
T{ ##compare f V int-regs 4 V int-regs 2 V int-regs 1 cc<= } T{ ##compare f V int-regs 4 V int-regs 2 V int-regs 1 cc<= }
T{ ##compare-imm f V int-regs 6 V int-regs 4 7 cc= } T{ ##compare-imm f V int-regs 6 V int-regs 4 7 cc= }
T{ ##replace f V int-regs 6 D 0 } T{ ##replace f V int-regs 6 D 0 }
} value-numbering } value-numbering trim-temps
] unit-test ] unit-test
[ [
@ -122,7 +133,7 @@ compiler.cfg.registers cpu.architecture tools.test kernel math ;
T{ ##compare-float f V int-regs 12 V double-float-regs 10 V double-float-regs 11 cc< } T{ ##compare-float f V int-regs 12 V double-float-regs 10 V double-float-regs 11 cc< }
T{ ##compare-imm f V int-regs 14 V int-regs 12 7 cc= } T{ ##compare-imm f V int-regs 14 V int-regs 12 7 cc= }
T{ ##replace f V int-regs 14 D 0 } T{ ##replace f V int-regs 14 D 0 }
} value-numbering } value-numbering trim-temps
] unit-test ] unit-test
[ [
@ -138,5 +149,5 @@ compiler.cfg.registers cpu.architecture tools.test kernel math ;
T{ ##peek f V int-regs 30 D -2 } T{ ##peek f V int-regs 30 D -2 }
T{ ##compare f V int-regs 33 V int-regs 29 V int-regs 30 cc<= } T{ ##compare f V int-regs 33 V int-regs 29 V int-regs 30 cc<= }
T{ ##compare-imm-branch f V int-regs 33 7 cc/= } T{ ##compare-imm-branch f V int-regs 33 7 cc/= }
} value-numbering } value-numbering trim-temps
] unit-test ] unit-test

View File

@ -93,7 +93,7 @@ M: ##return generate-insn drop %return ;
M: ##dispatch-label generate-insn label>> %dispatch-label ; M: ##dispatch-label generate-insn label>> %dispatch-label ;
M: ##dispatch generate-insn M: ##dispatch generate-insn
[ src>> register ] [ temp>> register ] bi %dispatch ; [ src>> register ] [ temp>> register ] [ offset>> ] tri %dispatch ;
: >slot< : >slot<
{ {
@ -156,6 +156,19 @@ M: ##shr-imm generate-insn dst/src1/src2 %shr-imm ;
M: ##sar-imm generate-insn dst/src1/src2 %sar-imm ; M: ##sar-imm generate-insn dst/src1/src2 %sar-imm ;
M: ##not generate-insn dst/src %not ; M: ##not generate-insn dst/src %not ;
: src1/src2 ( insn -- src1 src2 )
[ src1>> register ] [ src2>> register ] bi ; inline
: src1/src2/temp1/temp2 ( insn -- src1 src2 temp1 temp2 )
[ src1/src2 ] [ temp1>> register ] [ temp2>> register ] tri ; inline
M: ##fixnum-add generate-insn src1/src2 %fixnum-add ;
M: ##fixnum-add-tail generate-insn src1/src2 %fixnum-add-tail ;
M: ##fixnum-sub generate-insn src1/src2 %fixnum-sub ;
M: ##fixnum-sub-tail generate-insn src1/src2 %fixnum-sub-tail ;
M: ##fixnum-mul generate-insn src1/src2/temp1/temp2 %fixnum-mul ;
M: ##fixnum-mul-tail generate-insn src1/src2/temp1/temp2 %fixnum-mul-tail ;
: dst/src/temp ( insn -- dst src temp ) : dst/src/temp ( insn -- dst src temp )
[ dst/src ] [ temp>> register ] bi ; inline [ dst/src ] [ temp>> register ] bi ; inline
@ -235,7 +248,7 @@ M: float-regs reg-class-variable drop float-regs ;
GENERIC: inc-reg-class ( register-class -- ) GENERIC: inc-reg-class ( register-class -- )
: ?dummy-stack-params ( reg-class -- ) : ?dummy-stack-params ( reg-class -- )
dummy-stack-params? [ reg-size stack-params +@ ] [ drop ] if ; dummy-stack-params? [ reg-size cell align stack-params +@ ] [ drop ] if ;
: ?dummy-int-params ( reg-class -- ) : ?dummy-int-params ( reg-class -- )
dummy-int-params? [ reg-size cell /i 1 max int-regs +@ ] [ drop ] if ; dummy-int-params? [ reg-size cell /i 1 max int-regs +@ ] [ drop ] if ;
@ -264,7 +277,7 @@ M: object reg-class-full?
: spill-param ( reg-class -- n reg-class ) : spill-param ( reg-class -- n reg-class )
stack-params get stack-params get
>r reg-size stack-params +@ r> [ reg-size cell align stack-params +@ ] dip
stack-params ; stack-params ;
: fastcall-param ( reg-class -- n reg-class ) : fastcall-param ( reg-class -- n reg-class )
@ -300,10 +313,10 @@ M: long-long-type flatten-value-type ( type -- types )
] { } make ; ] { } make ;
: each-parameter ( parameters quot -- ) : each-parameter ( parameters quot -- )
>r [ parameter-sizes nip ] keep r> 2each ; inline [ [ parameter-sizes nip ] keep ] dip 2each ; inline
: reverse-each-parameter ( parameters quot -- ) : reverse-each-parameter ( parameters quot -- )
>r [ parameter-sizes nip ] keep r> 2reverse-each ; inline [ [ parameter-sizes nip ] keep ] dip 2reverse-each ; inline
: reset-freg-counts ( -- ) : reset-freg-counts ( -- )
{ int-regs float-regs stack-params } [ 0 swap set ] each ; { int-regs float-regs stack-params } [ 0 swap set ] each ;
@ -316,15 +329,13 @@ M: long-long-type flatten-value-type ( type -- types )
#! Moves values from C stack to registers (if word is #! Moves values from C stack to registers (if word is
#! %load-param-reg) and registers to C stack (if word is #! %load-param-reg) and registers to C stack (if word is
#! %save-param-reg). #! %save-param-reg).
>r [ alien-parameters flatten-value-types ]
alien-parameters [ '[ alloc-parameter _ execute ] ]
flatten-value-types bi* each-parameter ; inline
r> '[ alloc-parameter _ execute ] each-parameter ;
inline
: unbox-parameters ( offset node -- ) : unbox-parameters ( offset node -- )
parameters>> [ parameters>> [
%prepare-unbox >r over + r> unbox-parameter %prepare-unbox [ over + ] dip unbox-parameter
] reverse-each-parameter drop ; ] reverse-each-parameter drop ;
: prepare-box-struct ( node -- offset ) : prepare-box-struct ( node -- offset )
@ -491,9 +502,10 @@ M: _label generate-insn
M: _branch generate-insn M: _branch generate-insn
label>> lookup-label %jump-label ; label>> lookup-label %jump-label ;
: >compare< ( insn -- label cc src1 src2 ) : >compare< ( insn -- dst temp cc src1 src2 )
{ {
[ dst>> register ] [ dst>> register ]
[ temp>> register ]
[ cc>> ] [ cc>> ]
[ src1>> register ] [ src1>> register ]
[ src2>> ?register ] [ src2>> ?register ]

View File

@ -46,34 +46,33 @@ M: integer fixup* , ;
: indq ( elt seq -- n ) [ eq? ] with find drop ; : indq ( elt seq -- n ) [ eq? ] with find drop ;
: adjoin* ( obj table -- n ) : adjoin* ( obj table -- n )
2dup indq [ 2nip ] [ dup length >r push r> ] if* ; 2dup indq [ 2nip ] [ dup length [ push ] dip ] if* ;
SYMBOL: literal-table SYMBOL: literal-table
: add-literal ( obj -- n ) literal-table get adjoin* ; : add-literal ( obj -- n ) literal-table get adjoin* ;
: add-dlsym-literals ( symbol dll -- ) : add-dlsym-literals ( symbol dll -- )
>r string>symbol r> 2array literal-table get push-all ; [ string>symbol ] dip 2array literal-table get push-all ;
: rel-dlsym ( name dll class -- ) : rel-dlsym ( name dll class -- )
>r literal-table get length >r [ literal-table get length [ add-dlsym-literals ] dip ] dip
add-dlsym-literals rt-dlsym rel-fixup ;
r> r> rt-dlsym rel-fixup ;
: rel-word ( word class -- ) : rel-word ( word class -- )
>r add-literal r> rt-xt rel-fixup ; [ add-literal ] dip rt-xt rel-fixup ;
: rel-primitive ( word class -- ) : rel-primitive ( word class -- )
>r def>> first r> rt-primitive rel-fixup ; [ def>> first ] dip rt-primitive rel-fixup ;
: rel-literal ( literal class -- ) : rel-immediate ( literal class -- )
>r add-literal r> rt-literal rel-fixup ; [ add-literal ] dip rt-immediate rel-fixup ;
: rel-this ( class -- ) : rel-this ( class -- )
0 swap rt-label rel-fixup ; 0 swap rt-label rel-fixup ;
: rel-here ( class -- ) : rel-here ( offset class -- )
0 swap rt-here rel-fixup ; rt-here rel-fixup ;
: init-fixup ( -- ) : init-fixup ( -- )
BV{ } clone relocation-table set BV{ } clone relocation-table set

View File

@ -6,7 +6,7 @@ HELP: enable-compiler
{ $description "Enables the optimizing compiler." } ; { $description "Enables the optimizing compiler." } ;
HELP: disable-compiler HELP: disable-compiler
{ $description "Enables the optimizing compiler." } ; { $description "Disable the optimizing compiler." } ;
ARTICLE: "compiler-usage" "Calling the optimizing compiler" ARTICLE: "compiler-usage" "Calling the optimizing compiler"
"Normally, new word definitions are recompiled automatically. This can be changed:" "Normally, new word definitions are recompiled automatically. This can be changed:"

View File

@ -1,8 +1,8 @@
! Copyright (C) 2004, 2008 Slava Pestov. ! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel namespaces arrays sequences io debugger USING: accessors kernel namespaces arrays sequences io debugger
words fry continuations vocabs assocs dlists definitions math words fry continuations vocabs assocs dlists definitions
threads graphs generic combinators deques search-deques math threads graphs generic combinators deques search-deques
prettyprint io stack-checker stack-checker.state prettyprint io stack-checker stack-checker.state
stack-checker.inlining compiler.errors compiler.units stack-checker.inlining compiler.errors compiler.units
compiler.tree.builder compiler.tree.optimizer compiler.tree.builder compiler.tree.optimizer
@ -91,8 +91,8 @@ t compile-dependencies? set-global
[ [
dup crossref? dup crossref?
[ [
dependencies get >alist dependencies get
generic-dependencies get >alist generic-dependencies get
compiled-xref compiled-xref
] [ drop ] if ] [ drop ] if
] tri ; ] tri ;

View File

@ -39,12 +39,12 @@ IN: compiler.constants
! Relocation types ! Relocation types
: rt-primitive 0 ; inline : rt-primitive 0 ; inline
: rt-dlsym 1 ; inline : rt-dlsym 1 ; inline
: rt-literal 2 ; inline : rt-dispatch 2 ; inline
: rt-dispatch 3 ; inline : rt-xt 3 ; inline
: rt-xt 4 ; inline : rt-here 4 ; inline
: rt-here 5 ; inline : rt-label 5 ; inline
: rt-label 6 ; inline : rt-immediate 6 ; inline
: rt-immediate 7 ; inline : rt-stack-chain 7 ; inline
: rc-absolute? ( n -- ? ) : rc-absolute? ( n -- ? )
[ rc-absolute-ppc-2/2 = ] [ rc-absolute-ppc-2/2 = ]

View File

@ -3,7 +3,8 @@ USING: alien alien.c-types alien.syntax compiler kernel
namespaces namespaces tools.test sequences stack-checker namespaces namespaces tools.test sequences stack-checker
stack-checker.errors words arrays parser quotations stack-checker.errors words arrays parser quotations
continuations effects namespaces.private io io.streams.string continuations effects namespaces.private io io.streams.string
memory system threads tools.test math accessors combinators ; memory system threads tools.test math accessors combinators
specialized-arrays.float ;
FUNCTION: void ffi_test_0 ; FUNCTION: void ffi_test_0 ;
[ ] [ ffi_test_0 ] unit-test [ ] [ ffi_test_0 ] unit-test
@ -146,13 +147,21 @@ FUNCTION: void ffi_test_20 double x1, double x2, double x3,
! Make sure XT doesn't get clobbered in stack frame ! Make sure XT doesn't get clobbered in stack frame
: ffi_test_31 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a ptr -- result y ) : ffi_test_31 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a -- result y )
"void" "int"
f "ffi_test_31" f "ffi_test_31"
{ "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" } { "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" "int" }
alien-invoke gc 3 ; alien-invoke gc 3 ;
[ 3 ] [ 42 [ ] each ffi_test_31 ] unit-test [ 861 3 ] [ 42 [ ] each ffi_test_31 ] unit-test
: ffi_test_31_point_5 ( a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a -- result )
"float"
f "ffi_test_31_point_5"
{ "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" "float" }
alien-invoke ;
[ 861.0 ] [ 42 [ >float ] each ffi_test_31_point_5 ] unit-test
FUNCTION: longlong ffi_test_21 long x long y ; FUNCTION: longlong ffi_test_21 long x long y ;
@ -188,7 +197,11 @@ FUNCTION: int ffi_test_12 int a int b rect c int d int e int f ;
FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ; FUNCTION: float ffi_test_23 ( float[3] x, float[3] y ) ;
[ 32.0 ] [ { 1.0 2.0 3.0 } >c-float-array { 4.0 5.0 6.0 } >c-float-array ffi_test_23 ] unit-test [ 32.0 ] [
{ 1.0 2.0 3.0 } >float-array underlying>>
{ 4.0 5.0 6.0 } >float-array underlying>>
ffi_test_23
] unit-test
! Test odd-size structs ! Test odd-size structs
C-STRUCT: test-struct-1 { { "char" 1 } "x" } ; C-STRUCT: test-struct-1 { { "char" 1 } "x" } ;
@ -353,7 +366,7 @@ FUNCTION: ulonglong ffi_test_38 ( ulonglong x, ulonglong y ) ;
[ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test [ 1 2 3 ] [ callback-6 callback_test_1 1 2 3 ] unit-test
: callback-7 ( -- callback ) : callback-7 ( -- callback )
"void" { } "cdecl" [ 1000 sleep ] alien-callback ; "void" { } "cdecl" [ 1000000 sleep ] alien-callback ;
[ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test [ 1 2 3 ] [ callback-7 callback_test_1 1 2 3 ] unit-test

View File

@ -3,7 +3,7 @@ kernel.private math hashtables.private math.private namespaces
sequences sequences.private tools.test namespaces.private sequences sequences.private tools.test namespaces.private
slots.private sequences.private byte-arrays alien slots.private sequences.private byte-arrays alien
alien.accessors layouts words definitions compiler.units io alien.accessors layouts words definitions compiler.units io
combinators vectors float-arrays ; combinators vectors grouping make ;
IN: compiler.tests IN: compiler.tests
! Originally, this file did black box testing of templating ! Originally, this file did black box testing of templating
@ -230,3 +230,49 @@ TUPLE: id obj ;
10000000 [ "hi" 0 (gc-check-bug) drop ] times ; 10000000 [ "hi" 0 (gc-check-bug) drop ] times ;
[ ] [ gc-check-bug ] unit-test [ ] [ gc-check-bug ] unit-test
! New optimization
: test-1 ( a -- b ) 8 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
[ "a" ] [ 8 test-1 ] unit-test
[ "b" ] [ 9 test-1 ] unit-test
: test-2 ( a -- b ) 1 fixnum-fast { [ "a" ] [ "b" ] } dispatch ;
[ "a" ] [ 1 test-2 ] unit-test
[ "b" ] [ 2 test-2 ] unit-test
! I accidentally fixnum/i-fast on PowerPC
[ { { 1 2 } { 3 4 } } ] [
{ 1 2 3 4 }
[
[ { array } declare 2 <groups> [ , ] each ] compile-call
] { } make
] unit-test
[ 2 ] [
{ 1 2 3 4 }
[ { array } declare 2 <groups> length ] compile-call
] unit-test
! Oops with new intrinsics
: fixnum-overflow-control-flow-test ( a b -- c )
[ 1 fixnum- ] [ 2 fixnum- ] if 3 fixnum+fast ;
[ 3 ] [ 1 t fixnum-overflow-control-flow-test ] unit-test
[ 2 ] [ 1 f fixnum-overflow-control-flow-test ] unit-test
! LOL
: blah ( a -- b )
{ float } declare dup 0 =
[ drop 1 ] [
dup 0 >=
[ 2 "double" "libm" "pow" { "double" "double" } alien-invoke ]
[ -0.5 "double" "libm" "pow" { "double" "double" } alien-invoke ]
if
] if ;
[ 4.0 ] [ 2.0 blah ] unit-test
[ 4 ] [ 2 [ dup fixnum* ] compile-call ] unit-test
[ 7 ] [ 2 [ dup fixnum* 3 fixnum+fast ] compile-call ] unit-test

View File

@ -160,6 +160,11 @@ IN: compiler.tests
[ -2 ] [ 4 [ -2 fixnum/i ] compile-call ] unit-test [ -2 ] [ 4 [ -2 fixnum/i ] compile-call ] unit-test
[ 3 1 ] [ 10 3 [ fixnum/mod ] compile-call ] unit-test [ 3 1 ] [ 10 3 [ fixnum/mod ] compile-call ] unit-test
[ 2 ] [ 4 2 [ fixnum/i-fast ] compile-call ] unit-test
[ 2 ] [ 4 [ 2 fixnum/i-fast ] compile-call ] unit-test
[ -2 ] [ 4 [ -2 fixnum/i-fast ] compile-call ] unit-test
[ 3 1 ] [ 10 3 [ fixnum/mod-fast ] compile-call ] unit-test
[ 4 ] [ 1 3 [ fixnum+ ] compile-call ] unit-test [ 4 ] [ 1 3 [ fixnum+ ] compile-call ] unit-test
[ 4 ] [ 1 [ 3 fixnum+ ] compile-call ] unit-test [ 4 ] [ 1 [ 3 fixnum+ ] compile-call ] unit-test
[ 4 ] [ [ 1 3 fixnum+ ] compile-call ] unit-test [ 4 ] [ [ 1 3 fixnum+ ] compile-call ] unit-test
@ -208,6 +213,7 @@ IN: compiler.tests
[ -1 ] [ [ -123 -64 fixnum-shift ] compile-call ] unit-test [ -1 ] [ [ -123 -64 fixnum-shift ] compile-call ] unit-test
[ -1 ] [ -123 -64 [ fixnum-shift ] compile-call ] unit-test [ -1 ] [ -123 -64 [ fixnum-shift ] compile-call ] unit-test
[ HEX: 10000000 ] [ HEX: 1000000 HEX: 10 [ fixnum* ] compile-call ] unit-test
[ HEX: 10000000 ] [ HEX: -10000000 >fixnum [ 0 swap fixnum- ] compile-call ] unit-test [ HEX: 10000000 ] [ HEX: -10000000 >fixnum [ 0 swap fixnum- ] compile-call ] unit-test
[ HEX: 10000000 ] [ HEX: -fffffff >fixnum [ 1 swap fixnum- ] compile-call ] unit-test [ HEX: 10000000 ] [ HEX: -fffffff >fixnum [ 1 swap fixnum- ] compile-call ] unit-test

View File

@ -286,9 +286,7 @@ HINTS: recursive-inline-hang-2 array ;
HINTS: recursive-inline-hang-3 array ; HINTS: recursive-inline-hang-3 array ;
! Regression ! Regression
USE: sequences.private [ ] [ { 3append-as } compile ] unit-test
[ ] [ { (3append) } compile ] unit-test
! Wow ! Wow
: counter-example ( a b c d -- a' b' c' d' ) : counter-example ( a b c d -- a' b' c' d' )

View File

@ -0,0 +1,14 @@
USING: math fry macros eval tools.test ;
IN: compiler.tests.redefine13
: breakage-word ( a b -- c ) + ;
MACRO: breakage-macro ( a -- ) '[ _ breakage-word ] ;
GENERIC: breakage-caller ( a -- c )
M: fixnum breakage-caller 2 breakage-macro ;
: breakage ( -- obj ) 2 breakage-caller ;
! [ ] [ "IN: compiler.tests.redefine13 : breakage-word ( a b -- c ) ;" eval ] unit-test

View File

@ -0,0 +1,8 @@
USING: compiler.units definitions tools.test sequences ;
IN: compiler.tests.redefine14
! TUPLE: bad ;
!
! M: bad length 1 2 3 ;
!
! [ ] [ [ { bad length } forget ] with-compilation-unit ] unit-test

View File

@ -1,5 +1,5 @@
USING: math.private kernel combinators accessors arrays USING: math.private kernel combinators accessors arrays
generalizations float-arrays tools.test ; generalizations tools.test ;
IN: compiler.tests IN: compiler.tests
: float-spill-bug ( a -- b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b ) : float-spill-bug ( a -- b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b )

View File

@ -1,9 +1,13 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: fry accessors quotations kernel sequences namespaces USING: fry accessors quotations kernel sequences namespaces
assocs words arrays vectors hints combinators stack-checker assocs words arrays vectors hints combinators compiler.tree
stack-checker.state stack-checker.visitor stack-checker.errors stack-checker
stack-checker.backend compiler.tree ; stack-checker.state
stack-checker.errors
stack-checker.visitor
stack-checker.backend
stack-checker.recursive-state ;
IN: compiler.tree.builder IN: compiler.tree.builder
: with-tree-builder ( quot -- nodes ) : with-tree-builder ( quot -- nodes )
@ -12,12 +16,13 @@ IN: compiler.tree.builder
: build-tree ( quot -- nodes ) : build-tree ( quot -- nodes )
#! Not safe to call from inference transforms. #! Not safe to call from inference transforms.
[ f infer-quot ] with-tree-builder nip ; [ f initial-recursive-state infer-quot ] with-tree-builder nip ;
: build-tree-with ( in-stack quot -- nodes out-stack ) : build-tree-with ( in-stack quot -- nodes out-stack )
#! Not safe to call from inference transforms. #! Not safe to call from inference transforms.
[ [
[ >vector meta-d set ] [ f infer-quot ] bi* [ >vector meta-d set ]
[ f initial-recursive-state infer-quot ] bi*
] with-tree-builder nip ] with-tree-builder nip
unclip-last in-d>> ; unclip-last in-d>> ;
@ -29,14 +34,10 @@ IN: compiler.tree.builder
if ; if ;
: (build-tree-from-word) ( word -- ) : (build-tree-from-word) ( word -- )
dup dup initial-recursive-state recursive-state set
[ "inline" word-prop ] dup [ "inline" word-prop ] [ "recursive" word-prop ] bi and
[ "recursive" word-prop ] bi and [ [ 1quotation ] [ specialized-def ] if
1quotation f infer-quot infer-quot-here ;
] [
[ specialized-def ]
[ dup 2array 1array ] bi infer-quot
] if ;
: check-cannot-infer ( word -- ) : check-cannot-infer ( word -- )
dup "cannot-infer" word-prop [ cannot-infer-effect ] [ drop ] if ; dup "cannot-infer" word-prop [ cannot-infer-effect ] [ drop ] if ;

View File

@ -22,8 +22,8 @@ ERROR: check-use-error value message ;
GENERIC: check-node* ( node -- ) GENERIC: check-node* ( node -- )
M: #shuffle check-node* M: #shuffle check-node*
[ [ mapping>> values ] [ in-d>> ] bi subset? [ "Bad mapping inputs" throw ] unless ] [ [ mapping>> values ] [ [ in-d>> ] [ in-r>> ] bi append ] bi subset? [ "Bad mapping inputs" throw ] unless ]
[ [ mapping>> keys ] [ out-d>> ] bi set= [ "Bad mapping outputs" throw ] unless ] [ [ mapping>> keys ] [ [ out-d>> ] [ out-r>> ] bi append ] bi set= [ "Bad mapping outputs" throw ] unless ]
bi ; bi ;
: check-lengths ( seq -- ) : check-lengths ( seq -- )
@ -31,13 +31,6 @@ M: #shuffle check-node*
M: #copy check-node* inputs/outputs 2array check-lengths ; M: #copy check-node* inputs/outputs 2array check-lengths ;
: check->r/r> ( node -- )
inputs/outputs [ drop ] [ 2array check-lengths ] if-empty ;
M: #>r check-node* check->r/r> ;
M: #r> check-node* check->r/r> ;
M: #return-recursive check-node* inputs/outputs 2array check-lengths ; M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
M: #phi check-node* M: #phi check-node*
@ -113,11 +106,8 @@ M: #push check-stack-flow* check-out-d ;
M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ; M: #call check-stack-flow* [ check-in-d ] [ check-out-d ] bi ;
M: #shuffle check-stack-flow* [ check-in-d ] [ check-out-d ] bi ; M: #shuffle check-stack-flow*
{ [ check-in-d ] [ check-in-r ] [ check-out-d ] [ check-out-r ] } cleave ;
M: #>r check-stack-flow* [ check-in-d ] [ check-out-r ] bi ;
M: #r> check-stack-flow* [ check-in-r ] [ check-out-d ] bi ;
: assert-datastack-empty ( -- ) : assert-datastack-empty ( -- )
datastack get empty? [ "Data stack not empty" throw ] unless ; datastack get empty? [ "Data stack not empty" throw ] unless ;

View File

@ -5,7 +5,7 @@ strings sbufs sequences.private slots.private combinators
definitions system layouts vectors math.partial-dispatch definitions system layouts vectors math.partial-dispatch
math.order math.functions accessors hashtables classes assocs math.order math.functions accessors hashtables classes assocs
io.encodings.utf8 io.encodings.ascii io.encodings fry slots io.encodings.utf8 io.encodings.ascii io.encodings fry slots
sorting.private combinators.short-circuit sorting.private combinators.short-circuit grouping prettyprint
compiler.tree compiler.tree
compiler.tree.combinators compiler.tree.combinators
compiler.tree.cleanup compiler.tree.cleanup
@ -71,7 +71,7 @@ M: object xyz ;
2over fixnum>= [ 2over fixnum>= [
3drop 3drop
] [ ] [
[ swap >r call 1 fixnum+fast r> ] keep (fx-repeat) [ swap [ call 1 fixnum+fast ] dip ] keep (fx-repeat)
] if ; inline recursive ] if ; inline recursive
: fx-repeat ( n quot -- ) : fx-repeat ( n quot -- )
@ -87,10 +87,10 @@ M: object xyz ;
2over dup xyz drop >= [ 2over dup xyz drop >= [
3drop 3drop
] [ ] [
[ swap >r call 1+ r> ] keep (i-repeat) [ swap [ call 1+ ] dip ] keep (i-repeat)
] if ; inline recursive ] if ; inline recursive
: i-repeat >r { integer } declare r> 0 -rot (i-repeat) ; inline : i-repeat [ { integer } declare ] dip 0 -rot (i-repeat) ; inline
[ t ] [ [ t ] [
[ [ dup xyz drop ] i-repeat ] \ xyz inlined? [ [ dup xyz drop ] i-repeat ] \ xyz inlined?
@ -194,7 +194,7 @@ M: fixnum annotate-entry-test-1 drop ;
2dup >= [ 2dup >= [
2drop 2drop
] [ ] [
>r dup annotate-entry-test-1 1+ r> (annotate-entry-test-2) [ dup annotate-entry-test-1 1+ ] dip (annotate-entry-test-2)
] if ; inline recursive ] if ; inline recursive
: annotate-entry-test-2 0 -rot (annotate-entry-test-2) ; inline : annotate-entry-test-2 0 -rot (annotate-entry-test-2) ; inline
@ -448,7 +448,7 @@ cell-bits 32 = [
] unit-test ] unit-test
[ ] [ [ ] [
[ [ >r "A" throw r> ] [ "B" throw ] if ] [ [ [ "A" throw ] dip ] [ "B" throw ] if ]
cleaned-up-tree drop cleaned-up-tree drop
] unit-test ] unit-test
@ -463,7 +463,7 @@ cell-bits 32 = [
: buffalo-wings ( i seq -- ) : buffalo-wings ( i seq -- )
2dup < [ 2dup < [
2dup chicken-fingers 2dup chicken-fingers
>r 1+ r> buffalo-wings [ 1+ ] dip buffalo-wings
] [ ] [
2drop 2drop
] if ; inline recursive ] if ; inline recursive
@ -482,7 +482,7 @@ cell-bits 32 = [
: ribs ( i seq -- ) : ribs ( i seq -- )
2dup < [ 2dup < [
steak steak
>r 1+ r> ribs [ 1+ ] dip ribs
] [ ] [
2drop 2drop
] if ; inline recursive ] if ; inline recursive
@ -500,3 +500,13 @@ cell-bits 32 = [
[ { array-capacity } declare 1 fixnum+ ] cleaned-up-tree [ { array-capacity } declare 1 fixnum+ ] cleaned-up-tree
[ { [ #call? ] [ node-input-infos second literal>> 1 = ] } 1&& ] contains? [ { [ #call? ] [ node-input-infos second literal>> 1 = ] } 1&& ] contains?
] unit-test ] unit-test
[ ] [
[ { null } declare [ 1 ] [ 2 ] if ]
build-tree normalize propagate cleanup check-nodes
] unit-test
[ t ] [
[ { array } declare 2 <groups> [ . . ] assoc-each ]
\ nth-unsafe inlined?
] unit-test

View File

@ -102,7 +102,7 @@ M: #declare cleanup* drop f ;
#! If only one branch is live we don't need to branch at #! If only one branch is live we don't need to branch at
#! all; just drop the condition value. #! all; just drop the condition value.
dup live-children sift dup length { dup live-children sift dup length {
{ 0 [ 2drop f ] } { 0 [ drop in-d>> #drop ] }
{ 1 [ first swap in-d>> #drop prefix ] } { 1 [ first swap in-d>> #drop prefix ] }
[ 2drop ] [ 2drop ]
} case ; } case ;

View File

@ -39,7 +39,7 @@ M: #branch remove-dead-code*
[ drop filter-live ] [ swap nths ] 2bi [ drop filter-live ] [ swap nths ] 2bi
[ make-values ] keep [ make-values ] keep
[ drop ] [ zip ] 2bi [ drop ] [ zip ] 2bi
#shuffle ; #data-shuffle ;
: insert-drops ( nodes values indices -- nodes' ) : insert-drops ( nodes values indices -- nodes' )
'[ '[

View File

@ -75,9 +75,9 @@ IN: compiler.tree.dead-code.tests
remove-dead-code remove-dead-code
"no-check" get [ dup check-nodes ] unless nodes>quot ; "no-check" get [ dup check-nodes ] unless nodes>quot ;
[ [ drop 1 ] ] [ [ >r 1 r> drop ] optimize-quot ] unit-test [ [ drop 1 ] ] [ [ [ 1 ] dip drop ] optimize-quot ] unit-test
[ [ read drop 1 2 ] ] [ [ read >r 1 2 r> drop ] optimize-quot ] unit-test [ [ read drop 1 2 ] ] [ [ read [ 1 2 ] dip drop ] optimize-quot ] unit-test
[ [ over >r + r> ] ] [ [ [ + ] [ drop ] 2bi ] optimize-quot ] unit-test [ [ over >r + r> ] ] [ [ [ + ] [ drop ] 2bi ] optimize-quot ] unit-test

View File

@ -1,8 +1,9 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: fry accessors namespaces assocs deques search-deques USING: fry accessors namespaces assocs deques search-deques
kernel sequences sequences.deep words sets stack-checker.branches dlists kernel sequences sequences.deep words sets
compiler.tree compiler.tree.def-use compiler.tree.combinators ; stack-checker.branches compiler.tree compiler.tree.def-use
compiler.tree.combinators ;
IN: compiler.tree.dead-code.liveness IN: compiler.tree.dead-code.liveness
SYMBOL: work-list SYMBOL: work-list

View File

@ -39,12 +39,6 @@ M: #copy compute-live-values*
M: #call compute-live-values* nip look-at-inputs ; M: #call compute-live-values* nip look-at-inputs ;
M: #>r compute-live-values*
[ out-r>> ] [ in-d>> ] bi look-at-mapping ;
M: #r> compute-live-values*
[ out-d>> ] [ in-r>> ] bi look-at-mapping ;
M: #shuffle compute-live-values* M: #shuffle compute-live-values*
mapping>> at look-at-value ; mapping>> at look-at-value ;
@ -61,7 +55,7 @@ M: #alien-indirect compute-live-values* nip look-at-inputs ;
zip filter-mapping values ; zip filter-mapping values ;
: filter-live ( values -- values' ) : filter-live ( values -- values' )
[ live-value? ] filter ; dup empty? [ [ live-value? ] filter ] unless ;
:: drop-values ( inputs outputs mapping-keys mapping-values -- #shuffle ) :: drop-values ( inputs outputs mapping-keys mapping-values -- #shuffle )
inputs inputs
@ -69,7 +63,7 @@ M: #alien-indirect compute-live-values* nip look-at-inputs ;
outputs outputs
mapping-keys mapping-keys
mapping-values mapping-values
filter-corresponding zip #shuffle ; inline filter-corresponding zip #data-shuffle ; inline
:: drop-dead-values ( outputs -- #shuffle ) :: drop-dead-values ( outputs -- #shuffle )
[let* | new-outputs [ outputs make-values ] [let* | new-outputs [ outputs make-values ]
@ -95,16 +89,6 @@ M: #alien-indirect compute-live-values* nip look-at-inputs ;
M: #introduce remove-dead-code* ( #introduce -- nodes ) M: #introduce remove-dead-code* ( #introduce -- nodes )
maybe-drop-dead-outputs ; maybe-drop-dead-outputs ;
M: #>r remove-dead-code*
[ filter-live ] change-out-r
[ filter-live ] change-in-d
dup in-d>> empty? [ drop f ] when ;
M: #r> remove-dead-code*
[ filter-live ] change-out-d
[ filter-live ] change-in-r
dup in-r>> empty? [ drop f ] when ;
M: #push remove-dead-code* M: #push remove-dead-code*
dup out-d>> first live-value? [ drop f ] unless ; dup out-d>> first live-value? [ drop f ] unless ;
@ -125,12 +109,14 @@ M: #call remove-dead-code*
M: #shuffle remove-dead-code* M: #shuffle remove-dead-code*
[ filter-live ] change-in-d [ filter-live ] change-in-d
[ filter-live ] change-out-d [ filter-live ] change-out-d
[ filter-live ] change-in-r
[ filter-live ] change-out-r
[ filter-mapping ] change-mapping [ filter-mapping ] change-mapping
dup in-d>> empty? [ drop f ] when ; dup [ in-d>> empty? ] [ in-r>> empty? ] bi and [ drop f ] when ;
M: #copy remove-dead-code* M: #copy remove-dead-code*
[ in-d>> ] [ out-d>> ] bi [ in-d>> ] [ out-d>> ] bi
2dup swap zip #shuffle 2dup swap zip #data-shuffle
remove-dead-code* ; remove-dead-code* ;
M: #terminate remove-dead-code* M: #terminate remove-dead-code*

View File

@ -3,7 +3,7 @@
USING: kernel assocs match fry accessors namespaces make effects USING: kernel assocs match fry accessors namespaces make effects
sequences sequences.private quotations generic macros arrays sequences sequences.private quotations generic macros arrays
prettyprint prettyprint.backend prettyprint.sections math words prettyprint prettyprint.backend prettyprint.sections math words
combinators io sorting hints qualified combinators combinators.short-circuit io sorting hints qualified
compiler.tree compiler.tree
compiler.tree.recursive compiler.tree.recursive
compiler.tree.normalization compiler.tree.normalization
@ -57,11 +57,43 @@ TUPLE: shuffle-node { effect effect } ;
M: shuffle-node pprint* effect>> effect>string text ; M: shuffle-node pprint* effect>> effect>string text ;
M: #shuffle node>quot : (shuffle-effect) ( in out #shuffle -- effect )
shuffle-effect dup pretty-shuffle mapping>> '[ _ at ] map <effect> ;
[ % ] [ shuffle-node boa , ] ?if ;
M: #push node>quot literal>> , ; : shuffle-effect ( #shuffle -- effect )
[ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
: #>r? ( #shuffle -- ? )
{
[ in-d>> length 1 = ]
[ out-r>> length 1 = ]
[ in-r>> empty? ]
[ out-d>> empty? ]
} 1&& ;
: #r>? ( #shuffle -- ? )
{
[ in-d>> empty? ]
[ out-r>> empty? ]
[ in-r>> length 1 = ]
[ out-d>> length 1 = ]
} 1&& ;
M: #shuffle node>quot
{
{ [ dup #>r? ] [ drop \ >r , ] }
{ [ dup #r>? ] [ drop \ r> , ] }
{
[ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
[
shuffle-effect dup pretty-shuffle
[ % ] [ shuffle-node boa , ] ?if
]
}
[ drop "COMPLEX SHUFFLE" , ]
} cond ;
M: #push node>quot literal>> literalize , ;
M: #call node>quot word>> , ; M: #call node>quot word>> , ;
@ -82,16 +114,6 @@ M: #if node>quot
M: #dispatch node>quot M: #dispatch node>quot
children>> [ nodes>quot ] map , \ dispatch , ; children>> [ nodes>quot ] map , \ dispatch , ;
M: #>r node>quot
[ in-d>> length ] [ out-r>> empty? \ drop \ >r ? ] bi
<repetition> % ;
DEFER: rdrop
M: #r> node>quot
[ in-r>> length ] [ out-d>> empty? \ rdrop \ r> ? ] bi
<repetition> % ;
M: #alien-invoke node>quot params>> , \ #alien-invoke , ; M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
M: #alien-indirect node>quot params>> , \ #alien-indirect , ; M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
@ -103,9 +125,13 @@ M: node node>quot drop ;
: nodes>quot ( node -- quot ) : nodes>quot ( node -- quot )
[ [ node>quot ] each ] [ ] make ; [ [ node>quot ] each ] [ ] make ;
: optimized. ( quot/word -- ) GENERIC: optimized. ( quot/word -- )
dup word? [ specialized-def ] when
build-tree optimize-tree nodes>quot . ; M: method-spec optimized. first2 method optimized. ;
M: word optimized. specialized-def optimized. ;
M: callable optimized. build-tree optimize-tree nodes>quot . ;
SYMBOL: words-called SYMBOL: words-called
SYMBOL: generics-called SYMBOL: generics-called

View File

@ -18,12 +18,16 @@ TUPLE: definition value node uses ;
swap >>node swap >>node
V{ } clone >>uses ; V{ } clone >>uses ;
ERROR: no-def-error value ;
: def-of ( value -- definition ) : def-of ( value -- definition )
def-use get at* [ "No def" throw ] unless ; dup def-use get at* [ nip ] [ no-def-error ] if ;
ERROR: multiple-defs-error ;
: def-value ( node value -- ) : def-value ( node value -- )
def-use get 2dup key? [ def-use get 2dup key? [
"Multiple defs" throw multiple-defs-error
] [ ] [
[ [ <definition> ] keep ] dip set-at [ [ <definition> ] keep ] dip set-at
] if ; ] if ;
@ -38,16 +42,16 @@ GENERIC: node-uses-values ( node -- values )
M: #introduce node-uses-values drop f ; M: #introduce node-uses-values drop f ;
M: #push node-uses-values drop f ; M: #push node-uses-values drop f ;
M: #r> node-uses-values in-r>> ;
M: #phi node-uses-values phi-in-d>> concat remove-bottom prune ; M: #phi node-uses-values phi-in-d>> concat remove-bottom prune ;
M: #declare node-uses-values declaration>> keys ; M: #declare node-uses-values declaration>> keys ;
M: #terminate node-uses-values [ in-d>> ] [ in-r>> ] bi append ; M: #terminate node-uses-values [ in-d>> ] [ in-r>> ] bi append ;
M: #shuffle node-uses-values [ in-d>> ] [ in-r>> ] bi append ;
M: #alien-callback node-uses-values drop f ; M: #alien-callback node-uses-values drop f ;
M: node node-uses-values in-d>> ; M: node node-uses-values in-d>> ;
GENERIC: node-defs-values ( node -- values ) GENERIC: node-defs-values ( node -- values )
M: #>r node-defs-values out-r>> ; M: #shuffle node-defs-values [ out-d>> ] [ out-r>> ] bi append ;
M: #branch node-defs-values drop f ; M: #branch node-defs-values drop f ;
M: #declare node-defs-values drop f ; M: #declare node-defs-values drop f ;
M: #return node-defs-values drop f ; M: #return node-defs-values drop f ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs namespaces sequences kernel math USING: accessors assocs namespaces sequences kernel math
combinators sets disjoint-sets fry stack-checker.state ; combinators sets disjoint-sets fry stack-checker.values ;
IN: compiler.tree.escape-analysis.allocations IN: compiler.tree.escape-analysis.allocations
! A map from values to one of the following: ! A map from values to one of the following:

View File

@ -8,6 +8,7 @@ math.private kernel tools.test accessors slots.private
quotations.private prettyprint classes.tuple.private classes quotations.private prettyprint classes.tuple.private classes
classes.tuple namespaces classes.tuple namespaces
compiler.tree.propagation.info stack-checker.errors compiler.tree.propagation.info stack-checker.errors
compiler.tree.checker
kernel.private ; kernel.private ;
\ escape-analysis must-infer \ escape-analysis must-infer
@ -34,6 +35,7 @@ M: node count-unboxed-allocations* drop ;
propagate propagate
cleanup cleanup
escape-analysis escape-analysis
dup check-nodes
0 swap [ count-unboxed-allocations* ] each-node ; 0 swap [ count-unboxed-allocations* ] each-node ;
[ 0 ] [ [ [ + ] curry ] count-unboxed-allocations ] unit-test [ 0 ] [ [ [ + ] curry ] count-unboxed-allocations ] unit-test
@ -307,7 +309,7 @@ C: <ro-box> ro-box
: bleach-node ( quot: ( node -- ) -- ) : bleach-node ( quot: ( node -- ) -- )
[ bleach-node ] curry [ ] compose impeach-node ; inline recursive [ bleach-node ] curry [ ] compose impeach-node ; inline recursive
[ 2 ] [ [ [ ] bleach-node ] count-unboxed-allocations ] unit-test [ 3 ] [ [ [ ] bleach-node ] count-unboxed-allocations ] unit-test
[ 0 ] [ [ 0 ] [
[ dup -1 over >= [ 0 >= [ "A" throw ] unless ] [ drop ] if ] [ dup -1 over >= [ 0 >= [ "A" throw ] unless ] [ drop ] if ]

View File

@ -1,6 +1,7 @@
! Copyright (C) 2008 Slava Pestov. ! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel accessors sequences words memoize classes.builtin USING: kernel accessors sequences words memoize classes.builtin
fry assocs
compiler.tree compiler.tree
compiler.tree.combinators compiler.tree.combinators
compiler.tree.propagation.info compiler.tree.propagation.info
@ -27,9 +28,10 @@ GENERIC: finalize* ( node -- nodes )
M: #copy finalize* drop f ; M: #copy finalize* drop f ;
M: #shuffle finalize* M: #shuffle finalize*
dup shuffle-effect dup
[ in>> ] [ out>> ] bi sequence= [ [ in-d>> ] [ out-d>> ] [ mapping>> ] tri '[ _ at ] map sequence= ]
[ drop f ] when ; [ [ in-r>> ] [ out-r>> ] [ mapping>> ] tri '[ _ at ] map sequence= ]
bi and [ drop f ] when ;
: builtin-predicate? ( #call -- ? ) : builtin-predicate? ( #call -- ? )
word>> "predicating" word-prop builtin-class? ; word>> "predicating" word-prop builtin-class? ;

View File

@ -79,7 +79,7 @@ GENERIC: apply-identities* ( node -- node )
: select-input ( node n -- #shuffle ) : select-input ( node n -- #shuffle )
[ [ in-d>> ] [ out-d>> ] bi ] dip [ [ in-d>> ] [ out-d>> ] bi ] dip
pick nth over first associate #shuffle ; pick nth over first associate #data-shuffle ;
M: #call apply-identities* M: #call apply-identities*
dup word>> "identities" word-prop [ dup word>> "identities" word-prop [

View File

@ -34,8 +34,8 @@ sequences accessors tools.test kernel math ;
[ ] [ [ [ 1 ] [ 2 ] if + * ] test-normalization ] unit-test [ ] [ [ [ 1 ] [ 2 ] if + * ] test-normalization ] unit-test
DEFER: bbb DEFER: bbb
: aaa ( x -- ) dup [ dup >r bbb r> aaa ] [ drop ] if ; inline recursive : aaa ( x -- ) dup [ dup [ bbb ] dip aaa ] [ drop ] if ; inline recursive
: bbb ( x -- ) >r drop 0 r> aaa ; inline recursive : bbb ( x -- ) [ drop 0 ] dip aaa ; inline recursive
[ ] [ [ bbb ] test-normalization ] unit-test [ ] [ [ bbb ] test-normalization ] unit-test

View File

@ -10,7 +10,7 @@ SYMBOL: rename-map
[ rename-map get at ] keep or ; [ rename-map get at ] keep or ;
: rename-values ( values -- values' ) : rename-values ( values -- values' )
rename-map get '[ [ _ at ] keep or ] map ; dup empty? [ rename-map get '[ [ _ at ] keep or ] map ] unless ;
: add-renamings ( old new -- ) : add-renamings ( old new -- )
[ rename-values ] dip [ rename-values ] dip
@ -22,13 +22,11 @@ M: #introduce rename-node-values* ;
M: #shuffle rename-node-values* M: #shuffle rename-node-values*
[ rename-values ] change-in-d [ rename-values ] change-in-d
[ rename-values ] change-in-r
[ [ rename-value ] assoc-map ] change-mapping ; [ [ rename-value ] assoc-map ] change-mapping ;
M: #push rename-node-values* ; M: #push rename-node-values* ;
M: #r> rename-node-values*
[ rename-values ] change-in-r ;
M: #terminate rename-node-values* M: #terminate rename-node-values*
[ rename-values ] change-in-d [ rename-values ] change-in-d
[ rename-values ] change-in-r ; [ rename-values ] change-in-r ;

View File

@ -40,8 +40,8 @@ M: #dispatch live-branches
SYMBOL: infer-children-data SYMBOL: infer-children-data
: copy-value-info ( -- ) : copy-value-info ( -- )
value-infos [ clone ] change value-infos [ H{ } clone suffix ] change
constraints [ clone ] change ; constraints [ H{ } clone suffix ] change ;
: no-value-info ( -- ) : no-value-info ( -- )
value-infos off value-infos off

View File

@ -32,7 +32,7 @@ TUPLE: true-constraint value ;
M: true-constraint assume* M: true-constraint assume*
[ \ f class-not <class-info> swap value>> refine-value-info ] [ \ f class-not <class-info> swap value>> refine-value-info ]
[ constraints get at [ assume ] when* ] [ constraints get assoc-stack [ assume ] when* ]
bi ; bi ;
M: true-constraint satisfied? M: true-constraint satisfied?
@ -44,7 +44,7 @@ TUPLE: false-constraint value ;
M: false-constraint assume* M: false-constraint assume*
[ \ f <class-info> swap value>> refine-value-info ] [ \ f <class-info> swap value>> refine-value-info ]
[ constraints get at [ assume ] when* ] [ constraints get assoc-stack [ assume ] when* ]
bi ; bi ;
M: false-constraint satisfied? M: false-constraint satisfied?
@ -83,7 +83,7 @@ TUPLE: implication p q ;
C: --> implication C: --> implication
: assume-implication ( p q -- ) : assume-implication ( p q -- )
[ constraints get [ swap suffix ] change-at ] [ constraints get [ assoc-stack swap suffix ] 2keep peek set-at ]
[ satisfied? [ assume ] [ drop ] if ] 2bi ; [ satisfied? [ assume ] [ drop ] if ] 2bi ;
M: implication assume* M: implication assume*

View File

@ -70,3 +70,7 @@ TUPLE: test-tuple { x read-only } ;
f f 3 <literal-info> 3array test-tuple <tuple-info> dup f f 3 <literal-info> 3array test-tuple <tuple-info> dup
object-info value-info-intersect = object-info value-info-intersect =
] unit-test ] unit-test
[ t ] [
null-info 3 <literal-info> value-info<=
] unit-test

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs classes classes.algebra classes.tuple USING: assocs classes classes.algebra classes.tuple
classes.tuple.private kernel accessors math math.intervals classes.tuple.private kernel accessors math math.intervals
namespaces sequences words combinators combinators.short-circuit namespaces sequences words combinators
arrays compiler.tree.propagation.copy ; arrays compiler.tree.propagation.copy ;
IN: compiler.tree.propagation.info IN: compiler.tree.propagation.info
@ -34,7 +34,7 @@ slots ;
: null-info T{ value-info f null empty-interval } ; inline : null-info T{ value-info f null empty-interval } ; inline
: object-info T{ value-info f object T{ interval f { -1.0/0.0 t } { 1.0/0.0 t } } } ; inline : object-info T{ value-info f object full-interval } ; inline
: class-interval ( class -- interval ) : class-interval ( class -- interval )
dup real class<= dup real class<=
@ -43,7 +43,7 @@ slots ;
: interval>literal ( class interval -- literal literal? ) : interval>literal ( class interval -- literal literal? )
#! If interval has zero length and the class is sufficiently #! If interval has zero length and the class is sufficiently
#! precise, we can turn it into a literal #! precise, we can turn it into a literal
dup empty-interval eq? [ dup special-interval? [
2drop f f 2drop f f
] [ ] [
dup from>> first { dup from>> first {
@ -243,7 +243,7 @@ DEFER: (value-info-union)
: literals<= ( info1 info2 -- ? ) : literals<= ( info1 info2 -- ? )
{ {
{ [ dup literal?>> not ] [ 2drop t ] } { [ dup literal?>> not ] [ 2drop t ] }
{ [ over literal?>> not ] [ 2drop f ] } { [ over literal?>> not ] [ drop class>> null-class? ] }
[ [ literal>> ] bi@ eql? ] [ [ literal>> ] bi@ eql? ]
} cond ; } cond ;
@ -253,26 +253,29 @@ DEFER: (value-info-union)
{ [ over not ] [ 2drop f ] } { [ over not ] [ 2drop f ] }
[ [
{ {
[ [ class>> ] bi@ class<= ] { [ 2dup [ class>> ] bi@ class<= not ] [ f ] }
[ [ interval>> ] bi@ interval-subset? ] { [ 2dup [ interval>> ] bi@ interval-subset? not ] [ f ] }
[ literals<= ] { [ 2dup literals<= not ] [ f ] }
[ [ length>> ] bi@ value-info<= ] { [ 2dup [ length>> ] bi@ value-info<= not ] [ f ] }
[ [ slots>> ] bi@ [ value-info<= ] 2all? ] { [ 2dup [ slots>> ] bi@ [ value-info<= ] 2all? not ] [ f ] }
} 2&& [ t ]
} cond 2nip
] ]
} cond ; } cond ;
! Current value --> info mapping ! Assoc stack of current value --> info mapping
SYMBOL: value-infos SYMBOL: value-infos
: value-info ( value -- info ) : value-info ( value -- info )
resolve-copy value-infos get at null-info or ; resolve-copy value-infos get assoc-stack null-info or ;
: set-value-info ( info value -- ) : set-value-info ( info value -- )
resolve-copy value-infos get set-at ; resolve-copy value-infos get peek set-at ;
: refine-value-info ( info value -- ) : refine-value-info ( info value -- )
resolve-copy value-infos get [ value-info-intersect ] change-at ; resolve-copy value-infos get
[ assoc-stack value-info-intersect ] 2keep
peek set-at ;
: value-literal ( value -- obj ? ) : value-literal ( value -- obj ? )
value-info >literal< ; value-info >literal< ;

Some files were not shown because too many files have changed in this diff Show More