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
10 SYMBOLS: param-area-size allot-area-size allot-area-align
13 : frame-required ( -- ) frame-required? on ;
15 GENERIC: compute-stack-frame* ( insn -- )
17 M:: ##local-allot compute-stack-frame* ( insn -- )
21 allot-area-align [ a max ] change
22 allot-area-size [ a align [ insn offset<< ] [ s + ] bi ] change ;
24 M: alien-call-insn compute-stack-frame*
26 stack-size>> param-area-size [ max ] change ;
28 : vm-frame-required ( -- )
30 vm-stack-space param-area-size [ max ] change ;
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 ;
39 M: ##call compute-stack-frame* drop frame-required ;
40 M: ##spill compute-stack-frame* drop frame-required ;
41 M: ##reload compute-stack-frame* drop frame-required ;
43 M: ##float>integer compute-stack-frame*
44 drop integer-float-needs-stack-frame? [ frame-required ] when ;
46 M: ##integer>float compute-stack-frame*
47 drop integer-float-needs-stack-frame? [ frame-required ] when ;
49 M: insn compute-stack-frame* drop ;
51 : finalize-stack-frame ( stack-frame -- )
52 dup [ params>> ] [ allot-area-align>> ] bi align >>allot-area-base
53 dup [ [ allot-area-base>> ] [ allot-area-size>> ] bi + ] [ spill-area-align>> ] bi align >>spill-area-base
54 dup stack-frame-size >>total-size drop ;
56 : <stack-frame> ( cfg -- stack-frame )
57 [ stack-frame new ] dip
58 [ spill-area-size>> >>spill-area-size ]
59 [ spill-area-align>> >>spill-area-align ] bi
60 allot-area-size get >>allot-area-size
61 allot-area-align get >>allot-area-align
62 param-area-size get >>params
63 dup finalize-stack-frame ;
65 : compute-stack-frame ( cfg -- stack-frame/f )
66 [ [ instructions>> [ compute-stack-frame* ] each ] each-basic-block ]
67 [ frame-required? get [ <stack-frame> ] [ drop f ] if ]
70 : build-stack-frame ( cfg -- )
73 cell allot-area-align set
74 [ compute-stack-frame ] keep stack-frame<< ;