]> gitweb.factorcode.org Git - factor.git/blob - unfinished/compiler/machine/builder/builder.factor
ogg plays but 1) sound is broken and 2) it doesn't recognize EOF anymore, so it hangs...
[factor.git] / unfinished / compiler / machine / builder / builder.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math accessors sequences namespaces
4 compiler.cfg compiler.vops compiler.lvops ;
5 IN: compiler.machine.builder
6
7 SYMBOL: block-counter
8
9 : number-basic-block ( basic-block -- )
10     #! Make this fancy later.
11     dup number>> [ drop ] [
12         block-counter [ dup 1+ ] change >>number
13         [ , ] [
14             successors>> <reversed>
15             [ number-basic-block ] each
16         ] bi
17     ] if ;
18
19 : flatten-basic-blocks ( procedure -- blocks )
20     [
21         0 block-counter
22         [ number-basic-block ]
23         with-variable
24     ] { } make ;
25
26 GENERIC: linearize-instruction ( basic-block insn -- )
27
28 M: object linearize-instruction
29     , drop ;
30
31 M: %b linearize-instruction
32     drop successors>> first number>> _b emit ;
33
34 : conditional-branch ( basic-block insn class -- )
35     [ successors>> ] 2dip
36     [ [ first number>> ] [ [ in>> ] [ code>> ] bi ] [ ] tri* emit ]
37     [ 2drop second number>> _b emit ]
38     3bi ; inline
39
40 M: %bi linearize-instruction _bi conditional-branch ;
41 M: %bf linearize-instruction _bf conditional-branch ;
42
43 : build-mr ( procedure -- insns )
44     [
45         flatten-basic-blocks [
46             [ number>> _label emit ]
47             [ dup instructions>> [ linearize-instruction ] with each ]
48             bi
49         ] each
50     ] { } make ;