1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math accessors sequences namespaces make
4 compiler.cfg compiler.instructions compiler.machine ;
5 IN: compiler.machine.builder
7 ! Convert CFG IR to machine IR.
11 : number-basic-block ( basic-block -- )
12 #! Make this fancy later.
13 dup number>> [ drop ] [
15 block-counter [ dup 1+ ] change >>number
17 successors>> <reversed>
18 [ number-basic-block ] each
22 : flatten-basic-blocks ( procedure -- blocks )
25 [ number-basic-block ]
29 GENERIC: linearize* ( basic-block insn -- )
31 M: object linearize* , drop ;
34 drop successors>> first label>> _branch ;
36 : conditional ( basic-block -- label1 label2 )
37 successors>> first2 [ label>> ] bi@ swap ; inline
39 : boolean-conditional ( basic-block insn -- label1 vreg label2 )
40 [ conditional ] [ vreg>> ] bi* swap ; inline
42 M: %branch-f linearize*
43 boolean-conditional _branch-f _branch ;
45 M: %branch-t linearize*
46 boolean-conditional _branch-t _branch ;
48 M: %if-intrinsic linearize*
49 [ conditional ] [ [ quot>> ] [ vregs>> ] bi ] bi*
50 _if-intrinsic _branch ;
52 M: %boolean-intrinsic linearize*
56 "false" get over [ quot>> ] [ vregs>> ] bi _if-intrinsic
57 t over out>> %load-literal
60 f over out>> %load-literal
65 : build-machine ( procedure -- insns )
67 entry>> flatten-basic-blocks [
69 [ dup instructions>> [ linearize* ] with each ]