From 44e8e7b3441b45e09c016760af671492cb7d2a1d Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 25 Jun 2015 18:02:03 -0700 Subject: [PATCH] primitives: Change PRIMITIVE: to check that the word is in that vocabulary and the stack effect is correct. Use PRIMITIVE: in core/ and basis/ --- basis/alien/libraries/libraries.factor | 6 ++ basis/locals/backend/backend.factor | 5 ++ basis/threads/threads.factor | 9 +++ basis/tools/dispatch/dispatch.factor | 5 ++ basis/tools/memory/memory.factor | 6 ++ basis/tools/profiler/sampling/sampling.factor | 6 ++ core/alien/accessors/accessors.factor | 30 ++++++++ core/alien/alien.factor | 9 +++ core/arrays/arrays.factor | 3 + core/byte-arrays/byte-arrays.factor | 4 ++ core/classes/tuple/tuple.factor | 5 ++ core/compiler/units/units.factor | 2 + core/generic/single/single.factor | 8 +++ core/io/files/files.factor | 4 ++ core/io/streams/c/c.factor | 10 +++ core/kernel/kernel.factor | 59 ++++++++++++++++ core/math/math.factor | 68 +++++++++++++++++++ core/math/parser/parser.factor | 4 ++ core/memory/memory.factor | 11 +++ core/quotations/quotations.factor | 5 ++ core/slots/slots.factor | 5 ++ core/strings/strings.factor | 5 ++ core/syntax/syntax.factor | 3 +- core/system/system.factor | 3 + core/words/words.factor | 15 ++++ 25 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 core/alien/accessors/accessors.factor diff --git a/basis/alien/libraries/libraries.factor b/basis/alien/libraries/libraries.factor index 0fcb197335..91d6494c21 100755 --- a/basis/alien/libraries/libraries.factor +++ b/basis/alien/libraries/libraries.factor @@ -5,6 +5,12 @@ kernel namespaces destructors sequences strings system io.pathnames fry combinators vocabs ; IN: alien.libraries +PRIMITIVE: dll-valid? ( dll -- ? ) +PRIMITIVE: (dlopen) ( path -- dll ) +PRIMITIVE: (dlsym) ( name dll -- alien ) +PRIMITIVE: dlclose ( dll -- ) +PRIMITIVE: (dlsym-raw) ( name dll -- alien ) + : dlopen ( path -- dll ) native-string>alien (dlopen) ; : dlsym ( name dll -- alien ) [ string>symbol ] dip (dlsym) ; diff --git a/basis/locals/backend/backend.factor b/basis/locals/backend/backend.factor index 1c1f288797..5f0cf9963f 100644 --- a/basis/locals/backend/backend.factor +++ b/basis/locals/backend/backend.factor @@ -3,6 +3,11 @@ USING: slots.private ; IN: locals.backend +PRIMITIVE: drop-locals ( n -- ) +PRIMITIVE: get-local ( n -- obj ) +PRIMITIVE: load-local ( obj -- ) +PRIMITIVE: load-locals ( ... n -- ) + : local-value ( box -- value ) 2 slot ; inline : set-local-value ( value box -- ) 2 set-slot ; inline diff --git a/basis/threads/threads.factor b/basis/threads/threads.factor index d8bccf9f8a..51097a6043 100644 --- a/basis/threads/threads.factor +++ b/basis/threads/threads.factor @@ -9,6 +9,15 @@ FROM: assocs => change-at ; IN: threads + SYMBOL: last-dispatch-stats : dispatch-stats. ( -- ) diff --git a/basis/tools/memory/memory.factor b/basis/tools/memory/memory.factor index 7250c47edd..d8e6921105 100644 --- a/basis/tools/memory/memory.factor +++ b/basis/tools/memory/memory.factor @@ -9,6 +9,12 @@ splitting strings system vm words hints hashtables ; IN: tools.memory change-nth ; FROM: assocs => change-at ; IN: tools.profiler.sampling + + SYMBOL: samples-per-second samples-per-second [ 1,000 ] initialize diff --git a/core/alien/accessors/accessors.factor b/core/alien/accessors/accessors.factor new file mode 100644 index 0000000000..c439b1dfce --- /dev/null +++ b/core/alien/accessors/accessors.factor @@ -0,0 +1,30 @@ +! Copyright (C) 2015 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +IN: alien.accessors + +PRIMITIVE: alien-cell ( c-ptr n -- value ) +PRIMITIVE: alien-double ( c-ptr n -- value ) +PRIMITIVE: alien-float ( c-ptr n -- value ) +PRIMITIVE: alien-signed-1 ( c-ptr n -- value ) +PRIMITIVE: alien-signed-2 ( c-ptr n -- value ) +PRIMITIVE: alien-signed-4 ( c-ptr n -- value ) +PRIMITIVE: alien-signed-8 ( c-ptr n -- value ) +PRIMITIVE: alien-signed-cell ( c-ptr n -- value ) +PRIMITIVE: alien-unsigned-1 ( c-ptr n -- value ) +PRIMITIVE: alien-unsigned-2 ( c-ptr n -- value ) +PRIMITIVE: alien-unsigned-4 ( c-ptr n -- value ) +PRIMITIVE: alien-unsigned-8 ( c-ptr n -- value ) +PRIMITIVE: alien-unsigned-cell ( c-ptr n -- value ) +PRIMITIVE: set-alien-cell ( value c-ptr n -- ) +PRIMITIVE: set-alien-double ( value c-ptr n -- ) +PRIMITIVE: set-alien-float ( value c-ptr n -- ) +PRIMITIVE: set-alien-signed-1 ( value c-ptr n -- ) +PRIMITIVE: set-alien-signed-2 ( value c-ptr n -- ) +PRIMITIVE: set-alien-signed-4 ( value c-ptr n -- ) +PRIMITIVE: set-alien-signed-8 ( value c-ptr n -- ) +PRIMITIVE: set-alien-signed-cell ( value c-ptr n -- ) +PRIMITIVE: set-alien-unsigned-1 ( value c-ptr n -- ) +PRIMITIVE: set-alien-unsigned-2 ( value c-ptr n -- ) +PRIMITIVE: set-alien-unsigned-4 ( value c-ptr n -- ) +PRIMITIVE: set-alien-unsigned-8 ( value c-ptr n -- ) +PRIMITIVE: set-alien-unsigned-cell ( value c-ptr n -- ) diff --git a/core/alien/alien.factor b/core/alien/alien.factor index 15db709137..b60331d8cb 100755 --- a/core/alien/alien.factor +++ b/core/alien/alien.factor @@ -8,6 +8,15 @@ IN: alien BUILTIN: alien { underlying c-ptr read-only initial: f } expired ; BUILTIN: dll { path byte-array read-only initial: B{ } } ; +PRIMITIVE: ( word return-rewind -- alien ) +PRIMITIVE: ( displacement c-ptr -- alien ) +PRIMITIVE: alien-address ( c-ptr -- addr ) +PRIMITIVE: free-callback ( alien -- ) + + + PREDICATE: pinned-alien < alien underlying>> not ; UNION: pinned-c-ptr pinned-alien POSTPONE: f ; diff --git a/core/arrays/arrays.factor b/core/arrays/arrays.factor index 384f0af678..8fc2f88b65 100644 --- a/core/arrays/arrays.factor +++ b/core/arrays/arrays.factor @@ -5,6 +5,9 @@ IN: arrays BUILTIN: array { length array-capacity read-only initial: 0 } ; +PRIMITIVE: ( n elt -- array ) +PRIMITIVE: resize-array ( n array -- new-array ) + M: array clone (clone) ; inline M: array length length>> ; inline M: array nth-unsafe [ integer>fixnum ] dip array-nth ; inline diff --git a/core/byte-arrays/byte-arrays.factor b/core/byte-arrays/byte-arrays.factor index d5d61c2980..36a54b9515 100644 --- a/core/byte-arrays/byte-arrays.factor +++ b/core/byte-arrays/byte-arrays.factor @@ -7,6 +7,10 @@ IN: byte-arrays BUILTIN: byte-array { length array-capacity read-only initial: 0 } ; +PRIMITIVE: (byte-array) ( n -- byte-array ) +PRIMITIVE: ( n -- byte-array ) +PRIMITIVE: resize-byte-array ( n byte-array -- new-byte-array ) + M: byte-array clone (clone) ; inline M: byte-array clone-like over byte-array? [ drop clone ] [ call-next-method ] if ; inline diff --git a/core/classes/tuple/tuple.factor b/core/classes/tuple/tuple.factor index 4c9102e4f9..d8ea8b02d4 100644 --- a/core/classes/tuple/tuple.factor +++ b/core/classes/tuple/tuple.factor @@ -7,6 +7,11 @@ make math math.private memory namespaces quotations sequences sequences.private slots slots.private strings words ; IN: classes.tuple + ( layout -- tuple ) +PRIMITIVE: ( slots... layout -- tuple ) +PRIVATE> + PREDICATE: tuple-class < class "metaclass" word-prop tuple-class eq? ; diff --git a/core/compiler/units/units.factor b/core/compiler/units/units.factor index 0c0d05b52d..b3430175fd 100644 --- a/core/compiler/units/units.factor +++ b/core/compiler/units/units.factor @@ -8,6 +8,8 @@ FROM: namespaces => set ; FROM: sets => members ; IN: compiler.units +PRIMITIVE: modify-code-heap ( alist update-existing? reset-pics? -- ) + SYMBOL: old-definitions SYMBOL: new-definitions diff --git a/core/generic/single/single.factor b/core/generic/single/single.factor index 938e56bb36..a89444bb6d 100644 --- a/core/generic/single/single.factor +++ b/core/generic/single/single.factor @@ -7,6 +7,14 @@ sequences words ; FROM: assocs => change-at ; IN: generic.single + + ERROR: no-method object generic ; ERROR: inconsistent-next-method class generic ; diff --git a/core/io/files/files.factor b/core/io/files/files.factor index aa77ebd15d..96f9b92ead 100644 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -5,6 +5,10 @@ io.encodings io.encodings.utf8 io.files.private io.pathnames kernel kernel.private namespaces sequences splitting system ; IN: io.files + + SYMBOL: +retry+ ! just try the operation again without blocking SYMBOL: +input+ SYMBOL: +output+ diff --git a/core/io/streams/c/c.factor b/core/io/streams/c/c.factor index 59f009194e..455b1176a3 100644 --- a/core/io/streams/c/c.factor +++ b/core/io/streams/c/c.factor @@ -5,6 +5,16 @@ destructors io io.backend io.encodings.utf8 io.files kernel kernel.private math sequences threads.private ; IN: io.streams.c +PRIMITIVE: (fopen) ( path mode -- alien ) +PRIMITIVE: fclose ( alien -- ) +PRIMITIVE: fflush ( alien -- ) +PRIMITIVE: fgetc ( alien -- byte/f ) +PRIMITIVE: fputc ( byte alien -- ) +PRIMITIVE: fread-unsafe ( n buf alien -- count ) +PRIMITIVE: fseek ( alien offset whence -- ) +PRIMITIVE: ftell ( alien -- n ) +PRIMITIVE: fwrite ( data length alien -- ) + TUPLE: c-stream < disposable handle ; : new-c-stream ( handle class -- c-stream ) diff --git a/core/kernel/kernel.factor b/core/kernel/kernel.factor index f7ac28dec7..1f83aeb316 100644 --- a/core/kernel/kernel.factor +++ b/core/kernel/kernel.factor @@ -7,6 +7,65 @@ BUILTIN: callstack ; BUILTIN: tuple ; BUILTIN: wrapper { wrapped read-only } ; +PRIMITIVE: -rot ( x y z -- z x y ) +PRIMITIVE: dup ( x -- x x ) +PRIMITIVE: dupd ( x y -- x x y ) +PRIMITIVE: drop ( x -- ) +PRIMITIVE: nip ( x y -- y ) +PRIMITIVE: over ( x y -- x y x ) +PRIMITIVE: pick ( x y z -- x y z x ) +PRIMITIVE: rot ( x y z -- y z x ) +PRIMITIVE: swap ( x y -- y x ) +PRIMITIVE: swapd ( x y z -- y x z ) +PRIMITIVE: 2drop ( x y -- ) +PRIMITIVE: 2dup ( x y -- x y x y ) +PRIMITIVE: 2nip ( x y z -- z ) +PRIMITIVE: 3drop ( x y z -- ) +PRIMITIVE: 3dup ( x y z -- x y z x y z ) +PRIMITIVE: 4drop ( w x y z -- ) +PRIMITIVE: 4dup ( w x y z -- w x y z w x y z ) + +PRIMITIVE: (clone) ( obj -- newobj ) +PRIMITIVE: eq? ( obj1 obj2 -- ? ) +PRIMITIVE: ( obj -- wrapper ) +PRIMITIVE: callstack ( -- callstack ) +PRIMITIVE: datastack ( -- array ) +PRIMITIVE: retainstack ( -- array ) +PRIMITIVE: die ( -- ) +PRIMITIVE: callstack>array ( callstack -- array ) + + + DEFER: dip DEFER: 2dip DEFER: 3dip diff --git a/core/math/math.factor b/core/math/math.factor index d1c8b801f0..600c1d7efc 100644 --- a/core/math/math.factor +++ b/core/math/math.factor @@ -7,6 +7,74 @@ BUILTIN: fixnum ; BUILTIN: bignum ; BUILTIN: float ; +PRIMITIVE: bits>double ( n -- x ) +PRIMITIVE: bits>float ( n -- x ) +PRIMITIVE: double>bits ( x -- n ) +PRIMITIVE: float>bits ( x -- n ) + + ( x y -- ? ) +PRIMITIVE: bignum>= ( x y -- ? ) +PRIMITIVE: bignum>fixnum ( x -- y ) +PRIMITIVE: bignum>fixnum-strict ( x -- y ) +PRIMITIVE: both-fixnums? ( x y -- ? ) +PRIMITIVE: fixnum* ( x y -- z ) +PRIMITIVE: fixnum*fast ( x y -- z ) +PRIMITIVE: fixnum+ ( x y -- z ) +PRIMITIVE: fixnum+fast ( x y -- z ) +PRIMITIVE: fixnum- ( x y -- z ) +PRIMITIVE: fixnum-bitand ( x y -- z ) +PRIMITIVE: fixnum-bitnot ( x -- y ) +PRIMITIVE: fixnum-bitor ( x y -- z ) +PRIMITIVE: fixnum-bitxor ( x y -- z ) +PRIMITIVE: fixnum-fast ( x y -- z ) +PRIMITIVE: fixnum-mod ( x y -- z ) +PRIMITIVE: fixnum-shift ( x y -- z ) +PRIMITIVE: fixnum-shift-fast ( x y -- z ) +PRIMITIVE: fixnum/i ( x y -- z ) +PRIMITIVE: fixnum/i-fast ( x y -- z ) +PRIMITIVE: fixnum/mod ( x y -- z w ) +PRIMITIVE: fixnum/mod-fast ( x y -- z w ) +PRIMITIVE: fixnum< ( x y -- ? ) +PRIMITIVE: fixnum<= ( x y -- z ) +PRIMITIVE: fixnum> ( x y -- ? ) +PRIMITIVE: fixnum>= ( x y -- ? ) +PRIMITIVE: fixnum>bignum ( x -- y ) +PRIMITIVE: fixnum>float ( x -- y ) +PRIMITIVE: float* ( x y -- z ) +PRIMITIVE: float+ ( x y -- z ) +PRIMITIVE: float- ( x y -- z ) +PRIMITIVE: float-u< ( x y -- ? ) +PRIMITIVE: float-u<= ( x y -- ? ) +PRIMITIVE: float-u> ( x y -- ? ) +PRIMITIVE: float-u>= ( x y -- ? ) +PRIMITIVE: float/f ( x y -- z ) +PRIMITIVE: float< ( x y -- ? ) +PRIMITIVE: float<= ( x y -- ? ) +PRIMITIVE: float= ( x y -- ? ) +PRIMITIVE: float> ( x y -- ? ) +PRIMITIVE: float>= ( x y -- ? ) +PRIMITIVE: float>bignum ( x -- y ) +PRIMITIVE: float>fixnum ( x -- y ) +PRIVATE> + GENERIC: >fixnum ( x -- n ) foldable GENERIC: >bignum ( x -- n ) foldable GENERIC: >integer ( x -- n ) foldable diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index a8b13b9a41..c10ac80cd5 100644 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -5,6 +5,10 @@ layouts make math math.private namespaces sbufs sequences sequences.private splitting strings strings.private ; IN: math.parser + + : digit> ( ch -- n ) { { [ dup CHAR: 9 <= ] [ CHAR: 0 - dup 0 < [ drop 255 ] when ] } diff --git a/core/memory/memory.factor b/core/memory/memory.factor index c9bef9a23f..f9daf425e8 100644 --- a/core/memory/memory.factor +++ b/core/memory/memory.factor @@ -4,6 +4,17 @@ USING: alien.strings io.backend kernel memory.private sequences system ; IN: memory +PRIMITIVE: all-instances ( -- array ) +PRIMITIVE: compact-gc ( -- ) +PRIMITIVE: gc ( -- ) +PRIMITIVE: minor-gc ( -- ) +PRIMITIVE: size ( obj -- n ) + + + : instances ( quot -- seq ) [ all-instances ] dip filter ; inline diff --git a/core/quotations/quotations.factor b/core/quotations/quotations.factor index d779f16944..1cf8f6ff64 100644 --- a/core/quotations/quotations.factor +++ b/core/quotations/quotations.factor @@ -9,7 +9,12 @@ BUILTIN: quotation cached-effect cache-counter ; +PRIMITIVE: jit-compile ( quot -- ) +PRIMITIVE: quot-compiled? ( quot -- ? ) +PRIMITIVE: quotation-code ( quot -- start end ) + quotation ( array -- quot ) : uncurry ( curry -- obj quot ) { curry } declare dup 2 slot swap 3 slot ; inline diff --git a/core/slots/slots.factor b/core/slots/slots.factor index aed7499673..d31dd93837 100644 --- a/core/slots/slots.factor +++ b/core/slots/slots.factor @@ -7,6 +7,11 @@ kernel.private make math quotations sequences sequences.private slots.private strings words ; IN: slots + + TUPLE: slot-spec name offset class initial read-only ; PREDICATE: reader < word "reader" word-prop ; diff --git a/core/strings/strings.factor b/core/strings/strings.factor index 8761335c6f..4bbf642ca0 100644 --- a/core/strings/strings.factor +++ b/core/strings/strings.factor @@ -7,7 +7,12 @@ IN: strings BUILTIN: string { length array-capacity read-only initial: 0 } aux ; +PRIMITIVE: ( n ch -- string ) +PRIMITIVE: resize-string ( n str -- newstr ) + >" } [ define-delimiter ] each "PRIMITIVE:" [ - "Primitive definition is not supported" throw + current-vocab name>> + scan-word scan-effect ensure-primitive ] define-core-syntax "CS{" [ diff --git a/core/system/system.factor b/core/system/system.factor index f3f847efc8..3b26353332 100644 --- a/core/system/system.factor +++ b/core/system/system.factor @@ -4,6 +4,9 @@ USING: assocs continuations init io kernel kernel.private make math.parser namespaces sequences ; IN: system +PRIMITIVE: (exit) ( n -- * ) +PRIMITIVE: nano-count ( -- ns ) + SINGLETONS: x86.32 x86.64 arm ppc.32 ppc.64 ; UNION: x86 x86.32 x86.64 ; diff --git a/core/words/words.factor b/core/words/words.factor index b71e670e51..8c7946e2bb 100644 --- a/core/words/words.factor +++ b/core/words/words.factor @@ -11,6 +11,13 @@ BUILTIN: word { def quotation initial: [ ] } props pic-def pic-tail-def { sub-primitive read-only } ; +PRIMITIVE: optimized? ( word -- ? ) +PRIMITIVE: word-code ( word -- start end ) + + + ! Need a dummy word here because BUILTIN: word is not a real word ! and parse-datum looks for things that are actually words instead of ! also looking for classes @@ -68,6 +75,14 @@ PREDICATE: primitive < word "primitive" word-prop ; M: primitive definer drop \ PRIMITIVE: f ; M: primitive definition drop f ; +ERROR: invalid-primitive vocabulary word effect ; +: ensure-primitive ( vocabulary word effect -- ) + 3dup + [ drop vocabulary>> = ] + [ drop nip primitive? ] + [ [ nip "declared-effect" word-prop ] dip = ] 3tri and and + [ 3drop ] [ invalid-primitive ] if ; + : lookup-word ( name vocab -- word ) vocab-words-assoc at ; : target-word ( word -- target ) -- 2.34.1