]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/build-stack-frame/build-stack-frame.factor
Merge branch 'gtk' of git://github.com/Blei/factor
[factor.git] / basis / compiler / cfg / build-stack-frame / build-stack-frame.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces accessors math math.order assocs kernel
4 sequences combinators classes words system fry locals
5 cpu.architecture layouts compiler.cfg compiler.cfg.rpo
6 compiler.cfg.instructions compiler.cfg.registers
7 compiler.cfg.stack-frame ;
8 IN: compiler.cfg.build-stack-frame
9
10 SYMBOLS: param-area-size allot-area-size allot-area-align
11 frame-required? ;
12
13 : frame-required ( -- ) frame-required? on ;
14
15 GENERIC: compute-stack-frame* ( insn -- )
16
17 M:: ##local-allot compute-stack-frame* ( insn -- )
18     frame-required
19     insn size>> :> s
20     insn align>> :> a
21     allot-area-align [ a max ] change
22     allot-area-size [ a align [ insn offset<< ] [ s + ] bi ] change ;
23
24 M: alien-call-insn compute-stack-frame*
25     frame-required
26     stack-size>> param-area-size [ max ] change ;
27
28 : vm-frame-required ( -- )
29     frame-required
30     vm-stack-space param-area-size [ max ] change ;
31
32 M: ##call-gc compute-stack-frame* drop vm-frame-required ;
33 M: ##box compute-stack-frame* drop vm-frame-required ;
34 M: ##unbox compute-stack-frame* drop vm-frame-required ;
35 M: ##box-long-long compute-stack-frame* drop vm-frame-required ;
36 M: ##callback-inputs compute-stack-frame* drop vm-frame-required ;
37 M: ##callback-outputs compute-stack-frame* drop vm-frame-required ;
38 M: ##unary-float-function compute-stack-frame* drop vm-frame-required ;
39 M: ##binary-float-function compute-stack-frame* drop vm-frame-required ;
40
41 M: ##call compute-stack-frame* drop frame-required ;
42 M: ##alien-callback compute-stack-frame* drop frame-required ;
43 M: ##spill compute-stack-frame* drop frame-required ;
44 M: ##reload compute-stack-frame* drop frame-required ;
45
46 M: ##float>integer compute-stack-frame*
47     drop integer-float-needs-stack-frame? [ frame-required ] when ;
48
49 M: ##integer>float compute-stack-frame*
50     drop integer-float-needs-stack-frame? [ frame-required ] when ;
51
52 M: insn compute-stack-frame* drop ;
53
54 : finalize-stack-frame ( stack-frame -- )
55     dup [ params>> ] [ allot-area-align>> ] bi align >>allot-area-base
56     dup [ [ allot-area-base>> ] [ allot-area-size>> ] bi + ] [ spill-area-align>> ] bi align >>spill-area-base
57     dup stack-frame-size >>total-size drop ;
58
59 : <stack-frame> ( cfg -- stack-frame )
60     [ stack-frame new ] dip
61     [ spill-area-size>> >>spill-area-size ]
62     [ spill-area-align>> >>spill-area-align ] bi
63     allot-area-size get >>allot-area-size
64     allot-area-align get >>allot-area-align
65     param-area-size get >>params
66     dup finalize-stack-frame ;
67
68 : compute-stack-frame ( cfg -- stack-frame/f )
69     [ [ instructions>> [ compute-stack-frame* ] each ] each-basic-block ]
70     [ frame-required? get [ <stack-frame> ] [ drop f ] if ]
71     bi ;
72
73 : build-stack-frame ( cfg -- cfg )
74     0 param-area-size set
75     0 allot-area-size set
76     cell allot-area-align set
77     dup compute-stack-frame >>stack-frame ;