on master help sign in
Started C generation
commitfa0ecde386ccf9670b4a336f40ea147b76dce259 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateWed 23 May 2012, 10:49 (14 years ago) parent73b5aa2
5 files changed+113−6
idris.cabal+1−1 src/Idris/REPL.hs+4 src/RTS/Bytecode.hs+5−2 src/RTS/CodegenC.hs+96 src/RTS/PreC.hs+7−3
idris.cabal+1−1
@@ -68,7 +68,7 @@ Executable idris
6868
6969 Util.Pretty,
7070
71− RTS.Bytecode, RTS.SC, RTS.PreC,
71+ RTS.Bytecode, RTS.SC, RTS.PreC, RTS.CodegenC,
7272
7373 Paths_idris
7474
src/Idris/REPL.hs+4
@@ -23,6 +23,7 @@ import Core.Constraints
2323import RTS.SC
2424import RTS.Bytecode
2525import RTS.PreC
26+import RTS.CodegenC
2627
2728import System.Console.Haskeline as H
2829import System.FilePath
@@ -204,12 +205,15 @@ process fn (DebugInfo n)
204205 let scs = toSC prims (n, head d)
205206 let bcs = bcdefs scs
206207 let pcs = preCdefs bcs
208+ let code = cdefs pcs
207209 putStrLn "Supercombinators:\n"
208210 print (toSC prims (n, head d))
209211 putStrLn "\nBytecode:\n"
210212 putStrLn (showSep "\n" (map show bcs))
211213 putStrLn "\nPre-C:\n"
212214 putStrLn (showSep "\n" (map show pcs))
215+ putStrLn "\nCode:\n"
216+ putStrLn code
213217process fn (Info n) = do i <- get
214218 case lookupCtxt Nothing n (idris_classes i) of
215219 [c] -> classInfo c
src/RTS/Bytecode.hs+5−2
@@ -54,10 +54,13 @@ bcExp v arity x
5454 return s
5555
5656 bc' :: Bool -> Int -> SCExp -> State Int Bytecode
57− bc' tl d (SRef n) = return $ BAtom (BP n)
57+ bc' tl d (SRef n) = if tl then return $ BTailApp (BP n) []
58+ else return $ BApp (BP n) []
5859 bc' tl d (SLoc i) = do ref i; return $ BAtom (BL i)
5960 bc' tl d (SApp f args)
60− = do f' <- bc' False d f
61+ = do f' <- case f of
62+ SRef n -> return $ BAtom (BP n)
63+ _ -> bc' False d f
6164 args' <- mapM (bc' False d) args
6265 let bapp = if tl then BTailApp else BApp
6366 case f' of
src/RTS/CodegenC.hsnew file+96
@@ -0,0 +1,96 @@
1+module RTS.CodegenC where
2+
3+import Core.TT
4+import RTS.PreC
5+import RTS.SC
6+
7+import Data.Char
8+
9+cdefs :: [(Name, (Int, PreC))] -> String
10+cdefs xs = concatMap (\ (n, (i, c)) -> proto n) xs ++ "\n" ++
11+ concatMap (\ (n, (i, c)) -> codegenC n i c) xs
12+
13+codegenC :: Name -> Int -> PreC -> String
14+codegenC n args prec
15+ = "void " ++ cname n ++ "(VM* vm) {\n" ++
16+ concatMap (cg 1) prec ++ "}\n\n"
17+
18+indent i = take (i * 4) (repeat ' ')
19+
20+reg RVal = "vm->ret"
21+reg (LVar v) = "*(vm->stack-" ++ show v ++ ")"
22+
23+catom :: CAtom -> String
24+catom (CL l) = reg (LVar l)
25+catom (CC (I i)) = "MKINT(" ++ show i ++ ")"
26+catom (CP n) = cname n
27+
28+assignCon i r tag args
29+ = indent i ++ reg r ++ " = MKCON(" ++ show tag ++ ", " ++
30+ show (length args) ++ ");\n" ++
31+ indent i ++ setArgs 0 args ++ "\n"
32+ where
33+ setArgs i [] = ""
34+ setArgs i (a : as) = "SETARG(" ++ reg r ++ ", " ++
35+ show i ++ ", " ++ catom a ++ "); "
36+ ++ setArgs (i + 1) as
37+
38+assignFn i r f args
39+ = indent i ++ "EXTEND(" ++ show (length args) ++ ");\n" ++
40+ indent i ++ setArgs 1 (reverse args) ++ "\n"
41+ -- indent i ++ "CALL(" ++ reg r ++ ", " ++ cname f ++ ");\n"
42+ where
43+ setArgs i [] = ""
44+ setArgs i (a : as) = reg (LVar i) ++ " = " ++ catom a ++ "; "
45+ ++ setArgs (i + 1) as
46+
47+tailCall i d f args
48+ = indent i ++ "EXTEND(" ++ show (length args) ++ ");\n" ++
49+ indent i ++ setArgs 1 (reverse args) ++ "\n"
50+-- indent i ++ "TAILCALL(" ++ show d ++ ", " ++ cname f ++ ");\n"
51+ where
52+ setArgs i [] = ""
53+ setArgs i (a : as) = reg (LVar i) ++ " = " ++ catom a ++ "; "
54+ ++ setArgs (i + 1) as
55+
56+cg :: Int -> CInst -> String
57+cg i (ASSIGN r (CCon t args))
58+ = assignCon i r t args
59+cg i (ASSIGN r (CApp f args))
60+ = assignFn i r f args
61+cg i (ASSIGN r (CAtom e)) = indent i ++ reg r ++ " = " ++ catom e ++ ";\n"
62+cg i (RESERVE s) = indent i ++ "EXTEND(" ++ show s ++ ");\n"
63+cg i (CLEAR s) = indent i ++ "CLEAR(" ++ show s ++ ");\n"
64+cg i (EVAL e) = indent i ++ "EVAL(" ++ reg (LVar e) ++ ");\n"
65+cg i (PROJECT scr loc num)
66+ = indent i ++ "PROJECT(" ++ reg (LVar scr) ++ ", " ++
67+ reg (LVar loc) ++ ", " ++ show num ++ ");\n"
68+cg i (SWITCH v bs def)
69+ = indent i ++ "switch(" ++ sval v ++ ") {\n" ++
70+ concatMap branch bs ++
71+ indent i ++ "default:\n" ++ concatMap (cg (i+1)) def ++
72+ indent (i+1) ++ "break;\n" ++
73+ indent i ++ "};\n"
74+ where sval (CTag l) = "TAG(" ++ reg (LVar l) ++ ")"
75+ sval (CIntVal l) = "GETINT(" ++ reg (LVar l) ++ ")"
76+
77+ branch (t, c) = indent i ++ "case " ++ show t ++ ":\n" ++
78+ concatMap (cg (i+1)) c ++
79+ indent (i+1) ++ "break;\n"
80+cg i (TAILCALL d f args) = tailCall i d f args
81+cg i (ERROR s) = indent i ++ "ERROR(" ++ show s ++ ")\n";
82+cg i _ = indent i ++ "NOT DONE;\n"
83+
84+cname :: Name -> String
85+cname (UN s) = "_I_" ++ toC s
86+cname (MN i s) = "_M_I_" ++ show i ++ "_" ++ toC s
87+cname (NS n ss) = "_" ++ showSep "_" (map toC ss) ++ cname n
88+
89+toC s = concatMap special s
90+ where special c | isAlphaNum c = [c]
91+ | c == '_' = "__"
92+ | otherwise = "_" ++ show (fromEnum c) ++ "_"
93+
94+proto :: Name -> String
95+proto n = "void " ++ cname n ++ "(VM* vm);\n"
96+
src/RTS/PreC.hs+7−3
@@ -14,6 +14,7 @@ data CAtom = CP Name | CL Local | CC Const
1414
1515data CExp = CAtom CAtom
1616 | CApp CAtom [CAtom]
17+ | CExactApp CAtom [CAtom]
1718 | CTailApp CAtom [CAtom]
1819 | CLazy CAtom [CAtom]
1920 | CFCall String CType [(CAtom, CType)]
@@ -31,11 +32,13 @@ data Reg = RVal | LVar Local
3132
3233data CInst = ASSIGN Reg CExp
3334 | RESERVE Int
35+ | CLEAR Int
3436 | EVAL Local
3537 | PROJECT Local Local Int
3638 | SWITCH CVal [(Int, PreC)] PreC
3739 | ERROR String
38− | TAILCALL CAtom [CAtom]
40+ | TAILCALL Int -- number of stack items to clear
41+ CAtom [CAtom]
3942 deriving Show
4043
4144type PreC = [CInst]
@@ -51,7 +54,7 @@ preC :: Bytecode -> (Int, PreC)
5154preC (BGetArgs ns bc) = (length ns, pc RVal (length ns) bc)
5255 where pc loc d (BAtom b) = [ASSIGN loc (CAtom (atom d b))]
5356 pc loc d (BApp f as) = [ASSIGN loc (CApp (atom d f) (map (atom d) as))]
54− pc loc d (BTailApp f as) = [TAILCALL (atom d f) (map (atom d) as)]
57+ pc loc d (BTailApp f as) = [TAILCALL d (atom d f) (map (atom d) as)]
5558 pc loc d (BLazy f as) = [ASSIGN loc (CLazy (atom d f) (map (atom d) as))]
5659 pc loc d (BLet x val sc) = pc (LVar (d - x)) d val ++ pc loc d sc
5760 pc loc d (BFCall c t args)
@@ -65,7 +68,8 @@ preC (BGetArgs ns bc) = (length ns, pc RVal (length ns) bc)
6568 SWITCH (caseTy alts (d - l))
6669 (map (pcAlt loc d l) (filter notDef alts))
6770 (getDefault loc d alts) : []
68− pc loc d (BReserve s bc) = RESERVE s : pc loc (d + s) bc
71+ pc loc d (BReserve s bc) = RESERVE s : pc loc (d + s) bc ++
72+ [CLEAR s]
6973
7074 notDef (BDefaultCase _) = False
7175 notDef _ = True