Changeset 22244 for v6

Show
Ignore:
Timestamp:
09/14/08 21:54:19 (3 months ago)
Author:
pmurias
Message:

[m0ld] added a --print-bytecode to print bytecode

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • v6/smop/m0ld/M0ld.hs

    r22204 r22244  
    11import Text.ParserCombinators.Parsec hiding (label) 
    22import qualified Data.Map as Map 
    3 import System.IO hiding (getContents,putStrLn,print) 
    4 import Prelude hiding (getContents,putStrLn,print) 
     3import System.IO hiding (getContents,putStrLn,print,putStr) 
     4import Prelude hiding (getContents,putStrLn,print,putStr) 
    55import System.IO.UTF8 
    66import Debug.Trace 
     7import System.Console.GetOpt 
     8import System.Environment 
    79 
    810type Register = [Char] 
     
    240242emit stmts regMap labelsMap = concatMap (\op -> toBytecode op regMap labelsMap) stmts ++ [0] 
    241243 
     244joinStr sep [] = "" 
    242245joinStr sep list = foldl (\a b -> a ++ sep ++ b) (head list) (tail list) 
    243246 
     
    269272        ++ "})" 
    270273 
     274prettyPrintBytecode stmts = 
     275    let labelsMap = mapLabels stmts 
     276        regMap    = mapRegisters stmts 
     277        freeRegs  = countRegister stmts 
     278        prettyPrintOp (Decl _ _) = "" 
     279        prettyPrintOp op = (joinStr " " $ ( map show (toBytecode op regMap labelsMap))) ++ "\n" 
     280        in concat $ map prettyPrintOp stmts 
     281 
    271282type ImplicitDecls = Map.Map Value [Char] 
    272283 
     
    274285 
    275286main = do 
     287    args <- getArgs 
     288    let (options,nonoptions,errors) =  getOpt RequireOrder [Option [] ["print-bytecode"] (NoArg "print-bytecode") "print resulting mold bytecode in a human readable form"] args  
     289    mapM putStr errors 
    276290    hFlush stdout 
    277291    line <- getContents 
     
    279293        Left err      -> error  $ show err 
    280294        Right (stmts,constants) -> do  
    281             putStrLn $ dumpToC $ (implicitDecls constants) ++ stmts 
     295            if elem "print-bytecode" options then putStrLn $ prettyPrintBytecode $ (implicitDecls constants) ++ stmts 
     296                else putStrLn $ dumpToC $ (implicitDecls constants) ++ stmts