1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors compiler.cfg.instructions compiler.cfg.rpo
4 compiler.cfg.stack-frame cpu.architecture kernel layouts locals
5 math math.order namespaces sequences ;
6 IN: compiler.cfg.build-stack-frame
8 SYMBOLS: param-area-size allot-area-size allot-area-align
11 : frame-required ( -- ) frame-required? on ;
13 GENERIC: compute-stack-frame* ( insn -- )
15 M:: ##local-allot compute-stack-frame* ( insn -- )
19 allot-area-align [ a max ] change
20 allot-area-size [ a align [ insn offset<< ] [ s + ] bi ] change ;
22 M: alien-call-insn compute-stack-frame*
24 stack-size>> param-area-size [ max ] change ;
26 : vm-frame-required ( -- )
28 vm-stack-space param-area-size [ max ] change ;
30 M: ##call-gc compute-stack-frame* drop vm-frame-required ;
31 M: ##box compute-stack-frame* drop vm-frame-required ;
32 M: ##unbox compute-stack-frame* drop vm-frame-required ;
33 M: ##box-long-long compute-stack-frame* drop vm-frame-required ;
34 M: ##callback-inputs compute-stack-frame* drop vm-frame-required ;
35 M: ##callback-outputs compute-stack-frame* drop vm-frame-required ;
37 M: ##call compute-stack-frame* drop frame-required ;
38 M: ##spill compute-stack-frame* drop frame-required ;
39 M: ##reload compute-stack-frame* drop frame-required ;
41 M: ##float>integer compute-stack-frame*
42 drop integer-float-needs-stack-frame? [ frame-required ] when ;
44 M: ##integer>float compute-stack-frame*
45 drop integer-float-needs-stack-frame? [ frame-required ] when ;
47 M: insn compute-stack-frame* drop ;
49 : calculate-allot-area-base ( stack-frame -- n )
50 [ params>> ] [ allot-area-align>> ] bi align ;
52 : calculate-spill-area-base ( stack-frame -- n )
54 [ allot-area-size>> + ]
55 [ spill-area-align>> ] tri align ;
57 : finalize-stack-frame ( stack-frame -- stack-frame )
58 dup calculate-allot-area-base >>allot-area-base
59 dup calculate-spill-area-base >>spill-area-base
60 dup stack-frame-size >>total-size ;
62 : <stack-frame> ( cfg -- stack-frame )
64 over spill-area-size>> >>spill-area-size
65 swap spill-area-align>> >>spill-area-align
66 allot-area-size get >>allot-area-size
67 allot-area-align get >>allot-area-align
68 param-area-size get >>params
69 finalize-stack-frame ;
71 : compute-stack-frame ( cfg -- stack-frame/f )
72 [ [ instructions>> [ compute-stack-frame* ] each ] each-basic-block ]
73 [ frame-required? get [ <stack-frame> ] [ drop f ] if ]
76 : build-stack-frame ( cfg -- )
79 cell allot-area-align set
80 [ compute-stack-frame ] keep stack-frame<< ;