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 ;
38 M: ##unary-float-function compute-stack-frame* drop vm-frame-required ;
39 M: ##binary-float-function compute-stack-frame* drop vm-frame-required ;
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 ;
46 M: ##float>integer compute-stack-frame*
47 drop integer-float-needs-stack-frame? [ frame-required ] when ;
49 M: ##integer>float compute-stack-frame*
50 drop integer-float-needs-stack-frame? [ frame-required ] when ;
52 M: insn compute-stack-frame* drop ;
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 ;
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 ;
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 ]
73 : build-stack-frame ( cfg -- cfg )
76 cell allot-area-align set
77 dup compute-stack-frame >>stack-frame ;