1 -- as for deep thoughts ISA
2 -----------------------------------------------------------------------------
8 import Control.Applicative hiding ((<|>),many)
11 import System.Environment
14 import Text.Parsec.String
15 import Text.Parsec.Combinator
16 import qualified Data.Map as M
19 import qualified Data.ByteString.Lazy as BL
26 content <- getContents
27 let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
28 let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 []
30 sequence_ [printf "%10s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
31 printf "\nparsed asm:\n"
32 sequence_ [printf "%s" (show x) | x <- formatedsrc]
33 let parsed = parseInstr dict formatedsrc
34 printf "\nafter parsing the instructions:\n"
35 sequence_ [printf "%s" (show x) | x <- parsed]
38 parseInstr :: [DictElem] -> [DTF] -> [DTF]
40 parseInstr dict ((DTF_InstrToParse a instr c l s):xs) =
41 (DTF_Instr a bytecode c l s):(parseInstr dict xs)
43 bytecode = case (parse (instruction (a,dict)) "" (instr++"\n")) of
44 Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
46 parseInstr dict (x:xs) = x:(parseInstr dict xs)
51 inc :: Counter -> Counter
55 convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
56 convertDTF [] _ _ _ d = (d,[])
57 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
59 actlist (DTF_Org _) = next
60 actlist (DTF_State _) = next
61 actlist (DTF_Define _ _ _) = next
64 (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
66 ndatacnt (DTF_Org adr)
67 | state == InData = adr
69 ndatacnt (DTF_Data _ _ _ _ _) = inc datacnt
72 ninstrcnt (DTF_Org adr)
73 | state == InText = adr
74 | otherwise = instrcnt
75 ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
76 ninstrcnt (DTF_InstrToParse _ _ _ _ _) = inc instrcnt
77 ninstrcnt _ = instrcnt
79 nstate (DTF_State s) = s
82 ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
83 ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
84 ndict (DTF_InstrToParse a _ _ l _) = dict `add_elem` (l,a)
85 ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
86 ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
87 ndict (DTF_Define l v _) = dict `add_elem` (l,v)
90 newdtf = case (parse parseDTFLine "" (str++"\n")) of
91 Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
92 Right (DTF_SectionToDet _ v c l s) ->
94 NoState -> error "missing .data or .text"
95 InData -> (DTF_Data datacnt v c l s)
96 InText -> (DTF_Instr instrcnt v c l s)
97 Right (DTF_InstrToParse _ v c l s) ->
98 (DTF_InstrToParse instrcnt v c l s)
99 Right y@(DTF_Org _) ->
101 NoState -> error "missing .data or .text"
103 Right (DTF_Label l c _) ->
105 NoState -> error "missing .data or .text"
106 InData -> (DTF_Label l c datacnt)
107 InText -> (DTF_Label l c instrcnt)
108 Right z -> z -- DTF_Comment, DTF_State, DTF_Define
112 testDTF :: String -> IO ()
114 case (parse parseDTFLine "" (input++"\n")) of
115 Left err -> do { putStr "failz ;(\n"; print err}
116 Right x -> do { print x }
118 parseDTFLine :: Parser DTF
119 parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n'
121 lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
124 parseIdent :: Parser String
127 idents <- many $ (letter <|> digit <|> char '_')
128 return $ (ident:idents)
130 parseLabel :: Parser String
136 parseComment :: Parser String
140 comment <- many $ noneOf "\n"
143 parseConst :: Parser Word32
146 -- TODO: only decimal and hex (since this is supported by read)
147 -- TODO: howto check too big values? atm they get truncated
148 str <- try(do pref <- string "0x"; z <- many1 hexDigit; return $ (pref ++ z)) <|> (many1 digit)
154 l <- try (parseLabel) <|> string ""
156 fill <- string ".fill "
157 -- TODO: atm 32bit imm only
158 code <- many1 $ noneOf "\n;"
159 -- TODO: this is quite ugly here :/
160 let (Right val) = parse parseConst "" code
161 comment <- try(parseComment) <|> parseMySpaces
162 return $ DTF_SectionToDet 0 val (fill ++ code) l comment
165 comment <- parseComment
166 return $ DTF_Comment comment
170 comment <- try(parseComment) <|> parseMySpaces
171 return $ DTF_Label l comment 0
174 l <- try(parseLabel) <|> string ""
176 code <- many1 $ noneOf "\n;"
177 comment <- try(parseComment) <|> parseMySpaces
178 return $ DTF_InstrToParse 0 code code l comment
191 return $ DTF_State InData
197 return $ DTF_State InText
204 parseMySpaces; char ','; parseMySpaces
206 comment <- try(parseComment) <|> parseMySpaces
207 return $ DTF_Define id ret comment