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 : calculate-allot-area-base ( stack-frame -- n )
52 [ params>> ] [ allot-area-align>> ] bi align ;
54 : calculate-spill-area-base ( stack-frame -- n )
56 [ allot-area-size>> + ]
57 [ spill-area-align>> ] tri align ;
59 : finalize-stack-frame ( stack-frame -- stack-frame )
60 dup calculate-allot-area-base >>allot-area-base
61 dup calculate-spill-area-base >>spill-area-base
62 dup stack-frame-size >>total-size ;
64 : <stack-frame> ( cfg -- stack-frame )
66 over spill-area-size>> >>spill-area-size
67 swap spill-area-align>> >>spill-area-align
68 allot-area-size get >>allot-area-size
69 allot-area-align get >>allot-area-align
70 param-area-size get >>params
71 finalize-stack-frame ;
73 : compute-stack-frame ( cfg -- stack-frame/f )
74 [ [ instructions>> [ compute-stack-frame* ] each ] each-basic-block ]
75 [ frame-required? get [ <stack-frame> ] [ drop f ] if ]
78 : build-stack-frame ( cfg -- )
81 cell allot-area-align set
82 [ compute-stack-frame ] keep stack-frame<< ;