1 -- as for deep thoughts ISA
2 -----------------------------------------------------------------------------
7 import Control.Applicative hiding ((<|>),many)
10 import System.Environment
13 import Text.Parsec.String
14 import Text.Parsec.Combinator
15 import qualified Data.Map as M
20 import qualified Data.ByteString.Lazy as BL
27 content <- getContents
28 let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
29 let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [("start_",0x00)]
30 if (not $ null args) && ("-d" `elem` args)
33 sequence_ [printf "%20s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
34 printf "\nparsed asm:\n"
35 sequence_ [printf "%s" (show x) | x <- formatedsrc]
36 printf "\nafter parsing the instructions:\n"
39 let base = if "-b" `elem` args then 2 else 16
40 let parsed = parseInstr (reverse $ sort dict) formatedsrc
41 sequence_ [printf "%s" (showsDTFBase x base "") | x <- parsed]
44 parseInstr :: [DictElem] -> [DTF] -> [DTF]
46 parseInstr dict ((DTF_InstrToParse a instr c l s lno):xs) =
47 (DTF_Instr a bytecode c l s):(parseInstr dict xs)
49 bytecode = case (parse (instruction lno (a,dict)) "" (instr++"\n")) of
50 Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
52 parseInstr dict (x:xs) = x:(parseInstr dict xs)
57 inc :: Counter -> Counter
61 convertDTF :: [(LineNo,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
62 convertDTF [] _ _ _ d = (d,[])
63 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
65 actlist (DTF_Org _) = next
66 actlist (DTF_State _) = next
67 actlist (DTF_Define _ _ _) = next
68 actlist (DTF_Fill rep val code l c) = if state == InData then
69 (DTF_Data datacnt val code l c):[(DTF_Data dc val "" "" "") | i<-[2..repi], let dc = [(datacnt+0x4),(datacnt+0x8)..]!!(i-2)] ++ next
71 (DTF_Instr instrcnt val code l c):[(DTF_Instr ic val "" "" "") | i<-[2..repi], let ic = [(instrcnt+0x4),(instrcnt+0x8)..]!!(i-2)] ++ next
74 repi = fromInteger (toInteger rep)
75 actlist (DTF_Ascii str lab code comment) =
76 (DTF_Data datacnt (strval!!0) code lab comment):
77 [(DTF_Data ic (strval!!i) code lab comment)
79 , let ic = [datacnt,(datacnt+0x4)..]!!i
83 len = ((length str)`div`4)
85 strval = pf $ map (\x -> fromIntegral $ ord x :: Word8) str
86 pf :: [Word8] -> [Word32]
88 pf xs@(_:[]) = pf (xs ++ [0,0,0])
89 pf xs@(_:_:[]) = pf (xs ++ [0,0])
90 pf xs@(_:_:_:[]) = pf (xs ++ [0])
91 pf (a:b:c:d:xs) = (foldl' accum 0 [d,c,b,a]):(pf xs)
93 accum x o = (x `shiftL` 8) .|. fromIntegral o
96 (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
98 ndatacnt (DTF_Org adr)
99 | state == InData = adr
100 | otherwise = datacnt
101 ndatacnt (DTF_Fill rep _ _ _ _)
102 | state == InData = datacnt + (4*rep)
103 | otherwise = datacnt
104 ndatacnt (DTF_Ascii str _ _ _)
105 | state == InData = datacnt + (4*len)
106 where len = fromIntegral $ ((length str)`div`4)+1
107 ndatacnt (DTF_Data _ _ _ _ _) = inc datacnt
110 ninstrcnt (DTF_Org adr)
111 | state == InText = adr
112 | otherwise = instrcnt
113 ninstrcnt (DTF_Fill rep _ _ _ _)
114 | state == InText = instrcnt + (4*rep)
115 | otherwise = instrcnt
116 ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
117 ninstrcnt (DTF_InstrToParse _ _ _ _ _ _) = inc instrcnt
118 ninstrcnt _ = instrcnt
120 nstate (DTF_State s) = s
123 ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
124 ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
125 ndict (DTF_InstrToParse a _ _ l _ _) = dict `add_elem` (l,a)
126 ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
127 ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
128 ndict (DTF_Define l v _) = dict `add_elem` (l,v)
129 ndict (DTF_Fill _ _ _ l _)
130 | state == InText = dict `add_elem` (l,instrcnt)
131 | state == InData = dict `add_elem` (l,datacnt)
132 ndict (DTF_Ascii _ l c _)
133 | state == InData = dict `add_elem` (l,datacnt)
134 | otherwise = error $ "don't use .ascii in .text here: " ++ c
137 newdtf = case (parse (parseDTFLine lno dict) "" (str++"\n")) of
138 Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
139 Right (DTF_SectionToDet _ v c l s) ->
141 NoState -> error "missing .data or .text"
142 InData -> (DTF_Data datacnt v c l s)
143 InText -> (DTF_Instr instrcnt v c l s)
144 Right (DTF_InstrToParse _ v c l s ln) ->
145 (DTF_InstrToParse instrcnt v c l s ln)
146 Right y@(DTF_Org _) ->
148 NoState -> error "missing .data or .text"
150 Right (DTF_Label l c _) ->
152 NoState -> error "missing .data or .text"
153 InData -> (DTF_Label l c datacnt)
154 InText -> (DTF_Label l c instrcnt)
155 Right z -> z -- DTF_Comment, DTF_State, DTF_Define, DTF_Fill, DTF_Ascii
157 testDTF :: String -> IO ()
159 case (parse (parseDTFLine 0 dict) "" (input++"\n")) of
160 Left err -> do { putStr "failz ;(\n"; print err}
161 Right x -> do { print x }
163 dict = [("lolz", 0x1337), ("rofl", 0xaaaa)]
165 parseDTFLine :: LineNo -> [DictElem] -> Parser DTF
166 parseDTFLine lno dict = foldl1 (<|>) (fmap (\x -> try (x lno dict)) lineFormats) <* char '\n'
168 lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_ifill, lf_ascii, lf_comment, lf_toparse, lf_label, lf_nothing]
171 parseIdent :: Parser String
174 idents <- many $ (letter <|> digit <|> char '_')
175 return $ (ident:idents)
177 parseLabel :: Parser String
183 parseComment :: Parser String
187 comment <- many $ noneOf "\n"
190 parseConst :: [DictElem] -> Parser Word32
191 parseConst d = expr d
195 l <- try (parseLabel) <|> string ""
197 fill <- string ".fill "
198 repeat <- try (do {size <- many1 $ noneOf "\n;,"; char ','; return $ size}) <|> return "1"
199 -- TODO: atm 32bit imm only
200 code <- many1 $ noneOf "\n;"
201 let val = case parse (parseConst d) "" code of
203 Left err -> error $ show err
204 let r = case parse (parseConst d) "" repeat of
206 Left err -> error $ show err
207 comment <- try(parseComment) <|> parseMySpaces
208 return $ DTF_Fill r val (fill ++ (if repeat == "1" then "" else repeat) ++ code) l comment
211 l <- try (parseLabel) <|> string ""
213 fill <- string ".ifill "
214 code <- many1 $ noneOf "\n;"
215 let val = case parse (instruction lno (0,d)) "" (code++"\n") of
217 Left err -> error $ show err
218 comment <- try(parseComment) <|> parseMySpaces
219 return $ DTF_Fill 1 val (fill ++ code) l comment
222 l <- try (parseLabel) <|> string ""
224 ascii <- string ".ascii "
226 str <- many1 $ noneOf "\"";
228 comment <- try(parseComment) <|> parseMySpaces
229 return $ DTF_Ascii str (ascii ++ "\"" ++ str ++ "\"") l comment
232 comment <- parseComment
233 return $ DTF_Comment comment
237 return $ DTF_Comment wtf
241 comment <- try(parseComment) <|> parseMySpaces
242 return $ DTF_Label l comment 0
244 lf_toparse lno _ = do
245 l <- try(parseLabel) <|> string ""
247 code <- many1 $ noneOf "\n;"
248 comment <- try(parseComment) <|> parseMySpaces
249 return $ DTF_InstrToParse 0 code code l comment lno
262 return $ DTF_State InData
268 return $ DTF_State InText
276 -- TODO: expressions with (expr) do not work ;(
278 comment <- try(parseComment) <|> parseMySpaces
279 return $ DTF_Define id ret comment