]> gitweb.factorcode.org Git - factor.git/blob - unfinished/compiler/alien/alien.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / unfinished / compiler / alien / alien.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel namespaces make math sequences layouts
4 alien.c-types alien.structs compiler.backend ;
5 IN: compiler.alien
6
7 ! Common utilities
8
9 : large-struct? ( ctype -- ? )
10     dup c-struct? [
11         heap-size struct-small-enough? not
12     ] [ drop f ] if ;
13
14 : alien-parameters ( params -- seq )
15     dup parameters>>
16     swap return>> large-struct? [ "void*" prefix ] when ;
17
18 : alien-return ( params -- ctype )
19     return>> dup large-struct? [ drop "void" ] when ;
20
21 : c-type-stack-align ( type -- align )
22     dup c-type-stack-align? [ c-type-align ] [ drop cell ] if ;
23
24 : parameter-align ( n type -- n delta )
25     over >r c-type-stack-align align dup r> - ;
26
27 : parameter-sizes ( types -- total offsets )
28     #! Compute stack frame locations.
29     [
30         0 [
31             [ parameter-align drop dup , ] keep stack-size +
32         ] reduce cell align
33     ] { } make ;
34
35 : return-size ( ctype -- n )
36     #! Amount of space we reserve for a return value.
37     dup large-struct? [ heap-size ] [ drop 0 ] if ;
38
39 : alien-stack-frame ( params -- n )
40     alien-parameters parameter-sizes drop ;
41     
42 : alien-invoke-frame ( params -- n )
43     #! One cell is temporary storage, temp@
44     dup return>> return-size
45     swap alien-stack-frame +
46     cell + ;