refactor f0 into separate files
[fleet.git] / src / edu / berkeley / fleet / f0 / Main.lhs
1 \begin{code}
2
3 {-
4  next step:
5
6   - Transform code involving standing moves into two sequential
7     blocks: setup and teardown.  Then establish proper sequencing
8     between them.  The teardown block consists only of kills.
9
10   - Implement sequencing.
11 -}
12
13 module Main
14 where
15 import SBP
16 import Types
17 import Util
18
19 main = do t <- parseFile "src/edu/berkeley/fleet/f0/f0.g" "contrib/demo.f0"
20           putStrLn ""
21           putStrLn $ show $ coalesceFlatHeadlessNodes t
22           putStrLn ""
23           putStrLn $ join "\n\n" $ map show $ ((fromTree $ coalesceFlatHeadlessNodes t) :: [Def])
24           putStrLn ""
25           compiled <- return $ join "\n\n" $ map compileDef $ ((fromTree $ coalesceFlatHeadlessNodes t) :: [Def])
26           putStrLn $ compiled
27           writeFile "compiled.fleet" ("// compiled with f0\n\n"++compiled++"\n")
28           putStrLn ""
29
30 compile :: Expr -> [Inst]
31 compile (Decl _ _)      = []
32 compile (Seq s)         = error "bleh"
33 compile (Par e)         = concatMap compile e
34 compile (Move 1 s [d])    = [ (move s) { m_dest=(Just d) }, (accept d) ]
35 compile (Move 1 s d)      = [itake s]++sends++recvs
36  where
37    sends = map (\x -> ((send   s) { m_dest=(Just x) })) d
38    recvs = map (\x -> ((accept x) )) d
39 compile (Literal 0 lit ds) =  concatMap (\d -> [ ILiteral lit d, (accept d) {m_count=0} ]) ds
40 compile (Literal 1 lit ds) =  concatMap (\d -> [ ILiteral lit d, (accept d)             ]) ds
41 compile (Literal n lit ds) =  concatMap (\d -> [ ILiteral lit d, (accept d) {m_count=n} ]) ds
42
43 getdecls (Decl n t) = ["#ship " ++ n ++ " : " ++ t]
44 getdecls (Seq es)   = concatMap getdecls es
45 getdecls (Par es)   = concatMap getdecls es
46 getdecls _          = []
47
48 compileDef (Def s _ _ e) =
49     "// " ++ s ++ "\n" ++
50     (join "\n" $ getdecls e)++"\n"++
51     (join "\n" $ map show (compile e))
52
53 itake  box = IMove { m_count=1, m_recycle=False, m_tokenIn=False, m_dataIn=True,
54                      m_latch=True, m_dataOut=False, m_tokenOut=False, m_dest=Nothing,
55                      m_benkobox=box }
56 move   box = (itake box){ m_dataOut=True }
57 send   box = (move box){ m_dataIn=False, m_latch=False }
58 accept box = move box
59
60
61
62 \end{code}
63
64
65
66