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 :: [DictLabel] -> [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 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 -> [DictLabel] -> ([DictLabel], [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
63 (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
65 ndatacnt (DTF_Org adr)
66 | state == InData = adr
68 ndatacnt (DTF_Data _ _ _ _ _) = inc datacnt
71 ninstrcnt (DTF_Org adr)
72 | state == InText = adr
73 | otherwise = instrcnt
74 ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
75 ninstrcnt (DTF_InstrToParse _ _ _ _ _) = inc instrcnt
76 ninstrcnt _ = instrcnt
78 nstate (DTF_State s) = s
81 ndict (DTF_Label l _ a) = dict `add_label` (l,a)
82 ndict (DTF_SectionToDet a _ _ l _) = dict `add_label` (l,a)
83 ndict (DTF_InstrToParse a _ _ l _) = dict `add_label` (l,a)
84 ndict (DTF_Data a _ _ l _) = dict `add_label` (l,a)
85 ndict (DTF_Instr a _ _ l _) = dict `add_label` (l,a)
88 newdtf = case (parse parseDTFLine "" (str++"\n")) of
89 Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
90 Right (DTF_SectionToDet _ v c l s) ->
92 NoState -> error "missing .data or .text"
93 InData -> (DTF_Data datacnt v c l s)
94 InText -> (DTF_Instr instrcnt v c l s)
95 Right (DTF_InstrToParse _ v c l s) ->
96 (DTF_InstrToParse instrcnt v c l s)
97 Right y@(DTF_Org _) ->
99 NoState -> error "missing .data or .text"
101 Right (DTF_Label l c _) ->
103 NoState -> error "missing .data or .text"
104 InData -> (DTF_Label l c datacnt)
105 InText -> (DTF_Label l c instrcnt)
106 Right z -> z -- DTF_Comment, DTF_State
110 testDTF :: String -> IO ()
112 case (parse parseDTFLine "" (input++"\n")) of
113 Left err -> do { putStr "failz ;(\n"; print err}
114 Right x -> do { print x }
116 parseDTFLine :: Parser DTF
117 parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n'
119 lineFormats = [lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
122 parseLabel :: Parser String
125 labels <- many $ (letter <|> digit <|> char '_')
127 return $ (label:labels)
129 parseComment :: Parser String
133 comment <- many $ noneOf "\n"
136 parseConst :: Parser Word32
139 -- TODO: only decimal and hex (since this is supported by read)
140 -- TODO: howto check too big values? atm they get truncated
141 str <- try(do pref <- string "0x"; z <- many1 hexDigit; return $ (pref ++ z)) <|> (many1 digit)
147 l <- try (parseLabel) <|> string ""
149 fill <- string ".fill "
150 -- TODO: atm 32bit imm only
151 code <- many1 $ noneOf "\n;"
152 -- TODO: this is quite ugly here :/
153 let (Right val) = parse parseConst "" code
154 comment <- try(parseComment) <|> parseMySpaces
155 return $ DTF_SectionToDet 0 val (fill ++ code) l comment
158 comment <- parseComment
159 return $ DTF_Comment comment
163 comment <- try(parseComment) <|> parseMySpaces
164 return $ DTF_Label l comment 0
167 l <- try(parseLabel) <|> string ""
169 code <- many1 $ noneOf "\n;"
170 comment <- try(parseComment) <|> parseMySpaces
171 return $ DTF_InstrToParse 0 code code l comment
184 return $ DTF_State InData
190 return $ DTF_State InText