on master help sign in
Merge pull request #115 from DanielWaterworth/dump_bytecode

Added the ability to dump bytecode to a file

commitd1ee9113add8073ba59b860b42f19c90d431e198 authorEdwin Brady <edwin.brady@gmail.com> dateMon 3 Dec 2012, 03:50 (14 years ago) parenta41e971e3d393c
7 files changed+101−13
idris.cabal+1−1 src/IRTS/CodegenC.hs+6−4 src/IRTS/Compiler.hs+2 src/IRTS/DumpBC.hs+76 src/Idris/AbsSyntaxTree.hs+1−1 src/Idris/REPL.hs+11−4 src/Main.hs+4−3
idris.cabal+1−1
@@ -84,7 +84,7 @@ Executable idris
8484 IRTS.Lang, IRTS.LParser, IRTS.Bytecode, IRTS.Simplified,
8585 IRTS.CodegenC, IRTS.Defunctionalise, IRTS.Inliner,
8686 IRTS.Compiler, IRTS.CodegenJava, IRTS.BCImp,
87− IRTS.CodegenCommon
87+ IRTS.CodegenCommon, IRTS.DumpBC
8888
8989 Paths_idris
9090
src/IRTS/CodegenC.hs+6−4
@@ -59,9 +59,10 @@ codegenC defs out exec incs objs libs dbg
5959 when (exit /= ExitSuccess) $
6060 putStrLn ("FAILURE: " ++ gcc)
6161
62−headers [] = "#include <idris_rts.h>\n#include <idris_stdfgn.h>\n" ++
63− "#include <gmp.h>\n#include <assert.h>\n"
64−headers (x : xs) = "#include <" ++ x ++ ">\n" ++ headers xs
62+headers xs =
63+ concatMap
64+ (\h -> "#include <" ++ h ++ ">\n")
65+ (xs ++ ["idris_rts.h", "idris_stdfgn.h", "gmp.h", "assert.h"])
6566
6667debug TRACE = "#define IDRIS_TRACE\n\n"
6768debug _ = ""
@@ -75,7 +76,8 @@ cname n = "_idris_" ++ concatMap cchar (show n)
7576 where cchar x | isAlpha x || isDigit x = [x]
7677 | otherwise = "_" ++ show (fromEnum x) ++ "_"
7778
78−indent i = take (i * 4) (repeat ' ')
79+indent :: Int -> String
80+indent n = replicate (n*4) ' '
7981
8082creg RVal = "RVAL"
8183creg (L i) = "LOC(" ++ show i ++ ")"
src/IRTS/Compiler.hs+2
@@ -8,6 +8,7 @@ import IRTS.Simplified
88import IRTS.CodegenCommon
99import IRTS.CodegenC
1010import IRTS.CodegenJava
11+import IRTS.DumpBC
1112import IRTS.Inliner
1213
1314import Idris.AbsSyntax
@@ -68,6 +69,7 @@ compile target f tm
6869 (concatMap mkObj objs)
6970 (concatMap mkLib libs) NONE
7071 ViaJava -> liftIO $ codegenJava c f outty
72+ Bytecode -> liftIO $ dumpBC c f
7173 Error e -> fail $ show e
7274 where checkMVs = do i <- get
7375 case idris_metavars i \\ primDefs of
src/IRTS/DumpBC.hsnew file+76
@@ -0,0 +1,76 @@
1+module IRTS.DumpBC where
2+
3+import IRTS.Lang
4+import IRTS.Simplified
5+import Core.TT
6+
7+import IRTS.Bytecode
8+import Data.List
9+
10+interMap :: [a] -> [b] -> (a -> [b]) -> [b]
11+interMap xs y f = concat (intersperse y (map f xs))
12+
13+indent :: Int -> String
14+indent n = replicate (n*4) ' '
15+
16+serializeReg :: Reg -> String
17+serializeReg (L n) = "L" ++ show n
18+serializeReg (T n) = "T" ++ show n
19+serializeReg r = show r
20+
21+serializeCase :: Show a => Int -> (a, [BC]) -> String
22+serializeCase n (x, bcs) =
23+ indent n ++ show x ++ ":\n" ++ interMap bcs "\n" (serializeBC (n + 1))
24+
25+serializeDefault :: Int -> [BC] -> String
26+serializeDefault n bcs =
27+ indent n ++ "default:\n" ++ interMap bcs "\n" (serializeBC (n + 1))
28+
29+serializeBC :: Int -> BC -> String
30+serializeBC n bc = indent n ++
31+ case bc of
32+ ASSIGN a b ->
33+ "ASSIGN " ++ serializeReg a ++ " " ++ serializeReg b
34+ ASSIGNCONST a b ->
35+ "ASSIGNCONST " ++ serializeReg a ++ " " ++ show b
36+ UPDATE a b ->
37+ "UPDATE " ++ serializeReg a ++ " " ++ serializeReg b
38+ MKCON a b xs ->
39+ "MKCON " ++ serializeReg a ++ " " ++ show b ++ " [" ++ (interMap xs ", " serializeReg) ++ "]"
40+ CASE r cases def ->
41+ "CASE " ++ serializeReg r ++ ":\n" ++ interMap cases "\n" (serializeCase (n + 1)) ++
42+ maybe "" (\def' -> "\n" ++ serializeDefault (n + 1) def') def
43+ PROJECT a b c ->
44+ "PROJECT " ++ serializeReg a ++ " " ++ show b ++ " " ++ show c
45+ PROJECTINTO a b c ->
46+ "PROJECTINTO " ++ serializeReg a ++ " " ++ serializeReg b ++ " " ++ show c
47+ CONSTCASE r cases def ->
48+ "CONSTCASE " ++ serializeReg r ++ ":\n" ++ interMap cases "\n" (serializeCase (n + 1)) ++
49+ maybe "" (\def' -> "\n" ++ serializeDefault (n + 1) def') def
50+ CALL x -> "CALL " ++ show x
51+ TAILCALL x -> "TAILCALL " ++ show x
52+ FOREIGNCALL r _ ret name args ->
53+ "FOREIGNCALL " ++ serializeReg r ++ " \"" ++ name ++ "\" " ++ show ret ++
54+ " [" ++ interMap args ", " (\(ty, r) -> serializeReg r ++ " : " ++ show ty) ++ "]"
55+ SLIDE n -> "SLIDE " ++ show n
56+ REBASE -> "REBASE"
57+ RESERVE n -> "RESERVE " ++ show n
58+ ADDTOP n -> "ADDTOP " ++ show n
59+ TOPBASE n -> "TOPBASE " ++ show n
60+ BASETOP n -> "BASETOP " ++ show n
61+ STOREOLD -> "STOREOLD"
62+ OP a b c ->
63+ "OP " ++ serializeReg a ++ " " ++ show b ++ " [" ++ interMap c ", " serializeReg ++ "]"
64+ NULL r -> "NULL " ++ show r
65+ ERROR s -> "ERROR \"" ++ s ++ "\"" -- FIXME: s may contain quotes
66+
67+serialize :: [(Name, [BC])] -> String
68+serialize decls =
69+ interMap decls "\n\n" serializeDecl
70+ where
71+ serializeDecl :: (Name, [BC]) -> String
72+ serializeDecl (name, bcs) =
73+ show name ++ ":\n" ++ interMap bcs "\n" (serializeBC 1)
74+
75+dumpBC :: [(Name, SDecl)] -> String -> IO ()
76+dumpBC c output = writeFile output $ serialize $ map toBC c
src/Idris/AbsSyntaxTree.hs+1−1
@@ -140,7 +140,7 @@ type Idris = StateT IState (InputT IO)
140140
141141-- Commands in the REPL
142142
143−data Target = ViaC | ViaJava
143+data Target = ViaC | ViaJava | Bytecode
144144 deriving (Show, Eq)
145145
146146data Command = Quit
src/Idris/REPL.hs+11−4
@@ -416,6 +416,12 @@ displayHelp = let vstr = showVersion version in
416416 l ++ take (c1 - length l) (repeat ' ') ++
417417 m ++ take (c2 - length m) (repeat ' ') ++ r ++ "\n"
418418
419+parseTarget :: String -> Target
420+parseTarget "C" = ViaC
421+parseTarget "Java" = ViaJava
422+parseTarget "bytecode" = Bytecode
423+parseTarget _ = error "unknown target" -- FIXME: partial function
424+
419425parseArgs :: [String] -> [Opt]
420426parseArgs [] = []
421427parseArgs ("--log":lvl:ns) = OLogging (read lvl) : (parseArgs ns)
@@ -450,6 +456,7 @@ parseArgs ("-S":ns) = OutputTy Raw : (parseArgs ns)
450456parseArgs ("-c":ns) = OutputTy Object : (parseArgs ns)
451457parseArgs ("--dumpdefuns":n:ns) = DumpDefun n : (parseArgs ns)
452458parseArgs ("--dumpcases":n:ns) = DumpCases n : (parseArgs ns)
459+parseArgs ("--target":n:ns) = UseTarget (parseTarget n) : (parseArgs ns)
453460parseArgs (n:ns) = Filename n : (parseArgs ns)
454461
455462help =
@@ -510,12 +517,12 @@ idrisMain opts =
510517 mapM_ makeOption opts
511518 -- if we have the --fovm flag, drop into the first order VM testing
512519 case vm of
513− [] -> return ()
514− xs -> liftIO $ mapM_ (fovm tgt outty) xs
520+ [] -> return ()
521+ xs -> liftIO $ mapM_ (fovm tgt outty) xs
515522 -- if we have the --bytecode flag, drop into the bytecode assembler
516523 case bcs of
517− [] -> return ()
518− xs -> return () -- liftIO $ mapM_ bcAsm xs
524+ [] -> return ()
525+ xs -> return () -- liftIO $ mapM_ bcAsm xs
519526 case ibcsubdir of
520527 [] -> setIBCSubDir ""
521528 (d:_) -> setIBCSubDir d
src/Main.hs+4−3
@@ -89,6 +89,7 @@ usagemsg = "Idris version " ++ ver ++ "\n" ++
8989 "\t--log [level] Set debugging log level\n" ++
9090 "\t-S Do no further compilation of code generator output\n" ++
9191 "\t-c Compile to object files rather than an executable\n" ++
92− "\t--libdir Show library install directory and exit\n" ++
93− "\t--link Show C library directories and exit (for C linking)\n" ++
94− "\t--include Show C include directories and exit (for C linking)\n"
92+ "\t--libdir Show library install directory and exit\n" ++
93+ "\t--link Show C library directories and exit (for C linking)\n" ++
94+ "\t--include Show C include directories and exit (for C linking)\n" ++
95+ "\t--target [target] Set the target: C, Java, bytecode\n"