2006-11-03 03:54:30 -05:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2005-04-09 18:30:46 -04:00
|
|
|
IN: alien
|
2006-04-03 02:18:56 -04:00
|
|
|
USING: assembler compiler errors generic
|
2006-05-15 01:01:47 -04:00
|
|
|
hashtables kernel kernel-internals math namespaces parser
|
2005-12-02 02:25:44 -05:00
|
|
|
sequences strings words ;
|
2005-04-09 18:30:46 -04:00
|
|
|
|
|
|
|
|
! Some code for interfacing with C structures.
|
|
|
|
|
|
2006-05-19 15:53:30 -04:00
|
|
|
: c-getter* ( name -- quot )
|
|
|
|
|
c-getter [
|
|
|
|
|
[ "Cannot read struct fields with type" throw ]
|
|
|
|
|
] unless* ;
|
|
|
|
|
|
2005-04-09 18:30:46 -04:00
|
|
|
: define-getter ( offset type name -- )
|
|
|
|
|
#! Define a word with stack effect ( alien -- obj ) in the
|
|
|
|
|
#! current 'in' vocabulary.
|
2006-05-19 15:53:30 -04:00
|
|
|
create-in >r c-getter* swap add* r> swap define-compound ;
|
|
|
|
|
|
|
|
|
|
: c-setter* ( name -- quot )
|
|
|
|
|
c-setter [
|
|
|
|
|
[ "Cannot write struct fields with type" throw ]
|
|
|
|
|
] unless* ;
|
2005-04-09 18:30:46 -04:00
|
|
|
|
|
|
|
|
: define-setter ( offset type name -- )
|
|
|
|
|
#! Define a word with stack effect ( obj alien -- ) in the
|
|
|
|
|
#! current 'in' vocabulary.
|
2006-05-19 15:53:30 -04:00
|
|
|
"set-" swap append create-in >r c-setter* swap add* r>
|
2005-10-29 16:53:47 -04:00
|
|
|
swap define-compound ;
|
|
|
|
|
|
2006-09-14 19:51:47 -04:00
|
|
|
: parse-c-decl ( string -- count name )
|
2006-11-03 03:54:30 -05:00
|
|
|
"[]" split "" swap remove unclip
|
|
|
|
|
>r
|
|
|
|
|
dup empty? [ drop 1 ] [ [ string>number ] map product ] if
|
|
|
|
|
r> over 1 > [ "[]" append ] when ;
|
2006-09-14 19:51:47 -04:00
|
|
|
|
2005-04-09 18:30:46 -04:00
|
|
|
: define-field ( offset type name -- offset )
|
2006-11-03 03:54:30 -05:00
|
|
|
>r parse-c-decl [ c-type c-type-align ] keep
|
2006-09-14 19:51:47 -04:00
|
|
|
>r swapd align r> r>
|
2006-12-10 14:59:32 -05:00
|
|
|
"struct-name" get swap "-" swap 3append
|
2005-04-09 18:30:46 -04:00
|
|
|
3dup define-getter 3dup define-setter
|
2006-09-14 19:51:47 -04:00
|
|
|
drop c-size rot * + ;
|
2005-04-09 18:30:46 -04:00
|
|
|
|
|
|
|
|
: define-member ( max type -- max )
|
2005-10-29 16:53:47 -04:00
|
|
|
c-size max ;
|
2005-04-09 18:30:46 -04:00
|
|
|
|
2006-11-03 03:54:30 -05:00
|
|
|
TUPLE: struct-type ;
|
|
|
|
|
|
|
|
|
|
M: struct-type c-type-unbox c-type-size %unbox-struct ;
|
|
|
|
|
|
|
|
|
|
M: struct-type c-type-box c-type-size %box-struct ;
|
|
|
|
|
|
|
|
|
|
C: struct-type ( width -- type )
|
|
|
|
|
<c-type> over set-delegate
|
|
|
|
|
bootstrap-cell over set-c-type-align
|
|
|
|
|
[ swap <displaced-alien> ] over set-c-type-getter
|
|
|
|
|
[ set-c-type-size ] keep ;
|
|
|
|
|
|
2005-04-09 18:30:46 -04:00
|
|
|
: define-struct-type ( width -- )
|
|
|
|
|
#! Define inline and pointer type for the struct. Pointer
|
|
|
|
|
#! type is exactly like void*.
|
2006-11-03 03:54:30 -05:00
|
|
|
<struct-type> "struct-name" get in get define-c-type ;
|
|
|
|
|
|
|
|
|
|
: c-struct? ( type -- ? ) c-types get hash struct-type? ;
|