1 {- `Deep Thought', a softcore CPU implemented on a FPGA
3 Copyright (C) 2010 Markus Hofstaetter <markus.manrow@gmx.at>
4 Copyright (C) 2010 Martin Perner <e0725782@student.tuwien.ac.at>
5 Copyright (C) 2010 Stefan Rebernig <stefan.rebernig@gmail.com>
6 Copyright (C) 2010 Manfred Schwarz <e0725898@student.tuwien.ac.at>
7 Copyright (C) 2010 Bernhard Urban <lewurm@gmail.com>
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. -}
22 import DT hiding (not)
26 import Control.Applicative hiding ((<|>),many)
29 import System.Environment
32 import Text.Parsec.String
33 import Text.Parsec.Combinator
34 import qualified Data.Map as M
39 import qualified Data.ByteString.Lazy as BL
46 content <- getContents
47 let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
48 let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [("start_",0x00)]
49 if (not $ null args) && ("-d" `elem` args)
52 sequence_ [printf "%20s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
53 printf "\nparsed asm:\n"
54 sequence_ [printf "%s" (show x) | x <- formatedsrc]
55 printf "\nafter parsing the instructions:\n"
58 let base = if "-b" `elem` args then 2 else 16
59 let parsed = parseInstr (reverse $ sort dict) formatedsrc
60 sequence_ [printf "%s" (showsDTFBase x base "") | x <- parsed]
63 parseInstr :: [DictElem] -> [DTF] -> [DTF]
65 parseInstr dict ((DTF_InstrToParse a instr c l s lno):xs) =
66 (DTF_Instr a bytecode c l s):(parseInstr dict xs)
68 bytecode = case (parse (instruction lno (a,dict)) "" (instr++"\n")) of
69 Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
71 parseInstr dict (x:xs) = x:(parseInstr dict xs)
76 inc :: Counter -> Counter
80 convertDTF :: [(LineNo,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
81 convertDTF [] _ _ _ d = (d,[])
82 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
84 actlist (DTF_Org _) = next
85 actlist (DTF_State _) = next
86 actlist (DTF_Define _ _ _) = next
87 actlist (DTF_Fill rep val code l c) = if state == InData then
88 (DTF_Data datacnt val code l c):[(DTF_Data dc val "" "" "") | i<-[2..repi], let dc = [(datacnt+0x4),(datacnt+0x8)..]!!(i-2)] ++ next
90 (DTF_Instr instrcnt val code l c):[(DTF_Instr ic val "" "" "") | i<-[2..repi], let ic = [(instrcnt+0x4),(instrcnt+0x8)..]!!(i-2)] ++ next
93 repi = fromInteger (toInteger rep)
94 actlist (DTF_Ascii str lab code comment) =
95 (DTF_Data datacnt (strval!!0) code lab comment):
96 [(DTF_Data ic (strval!!i) code lab comment)
98 , let ic = [datacnt,(datacnt+0x4)..]!!i
102 len = ((length str)`div`4)
104 strval = pf $ map (\x -> fromIntegral $ ord x :: Word8) str
105 pf :: [Word8] -> [Word32]
107 pf xs@(_:[]) = pf (xs ++ [0,0,0])
108 pf xs@(_:_:[]) = pf (xs ++ [0,0])
109 pf xs@(_:_:_:[]) = pf (xs ++ [0])
110 pf (a:b:c:d:xs) = (foldl' accum 0 [d,c,b,a]):(pf xs)
112 accum x o = (x `shiftL` 8) .|. fromIntegral o
115 (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
117 ndatacnt (DTF_Org adr)
118 | state == InData = adr
119 | otherwise = datacnt
120 ndatacnt (DTF_Fill rep _ _ _ _)
121 | state == InData = datacnt + (4*rep)
122 | otherwise = datacnt
123 ndatacnt (DTF_Ascii str _ _ _)
124 | state == InData = datacnt + (4*len)
125 where len = fromIntegral $ ((length str)`div`4)+1
126 ndatacnt (DTF_Data _ _ _ _ _) = inc datacnt
129 ninstrcnt (DTF_Org adr)
130 | state == InText = adr
131 | otherwise = instrcnt
132 ninstrcnt (DTF_Fill rep _ _ _ _)
133 | state == InText = instrcnt + (4*rep)
134 | otherwise = instrcnt
135 ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
136 ninstrcnt (DTF_InstrToParse _ _ _ _ _ _) = inc instrcnt
137 ninstrcnt _ = instrcnt
139 nstate (DTF_State s) = s
142 ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
143 ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
144 ndict (DTF_InstrToParse a _ _ l _ _) = dict `add_elem` (l,a)
145 ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
146 ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
147 ndict (DTF_Define l v _) = dict `add_elem` (l,v)
148 ndict (DTF_Fill _ _ _ l _)
149 | state == InText = dict `add_elem` (l,instrcnt)
150 | state == InData = dict `add_elem` (l,datacnt)
151 ndict (DTF_Ascii _ l c _)
152 | state == InData = dict `add_elem` (l,datacnt)
153 | otherwise = error $ "don't use .ascii in .text here: " ++ c
156 newdtf = case (parse (parseDTFLine lno dict) "" (str++"\n")) of
157 Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
158 Right (DTF_SectionToDet _ v c l s) ->
160 NoState -> error "missing .data or .text"
161 InData -> (DTF_Data datacnt v c l s)
162 InText -> (DTF_Instr instrcnt v c l s)
163 Right (DTF_InstrToParse _ v c l s ln) ->
164 (DTF_InstrToParse instrcnt v c l s ln)
165 Right y@(DTF_Org _) ->
167 NoState -> error "missing .data or .text"
169 Right (DTF_Label l c _) ->
171 NoState -> error "missing .data or .text"
172 InData -> (DTF_Label l c datacnt)
173 InText -> (DTF_Label l c instrcnt)
174 Right z -> z -- DTF_Comment, DTF_State, DTF_Define, DTF_Fill, DTF_Ascii
176 testDTF :: String -> IO ()
178 case (parse (parseDTFLine 0 dict) "" (input++"\n")) of
179 Left err -> do { putStr "failz ;(\n"; print err}
180 Right x -> do { print x }
182 dict = [("lolz", 0x1337), ("rofl", 0xaaaa)]
184 parseDTFLine :: LineNo -> [DictElem] -> Parser DTF
185 parseDTFLine lno dict = foldl1 (<|>) (fmap (\x -> try (x lno dict)) lineFormats) <* char '\n'
187 lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_ifill, lf_ascii, lf_comment, lf_toparse, lf_label, lf_nothing]
190 parseIdent :: Parser String
193 idents <- many $ (letter <|> digit <|> char '_')
194 return $ (ident:idents)
196 parseLabel :: Parser String
202 parseComment :: Parser String
206 comment <- many $ noneOf "\n"
209 parseConst :: [DictElem] -> Parser Word32
210 parseConst d = expr d
214 l <- try (parseLabel) <|> string ""
216 fill <- string ".fill "
217 repeat <- try (do {size <- many1 $ noneOf "\n;,"; char ','; return $ size}) <|> return "1"
218 -- TODO: atm 32bit imm only
219 code <- many1 $ noneOf "\n;"
220 let val = case parse (parseConst d) "" code of
222 Left err -> error $ show err
223 let r = case parse (parseConst d) "" repeat of
225 Left err -> error $ show err
226 comment <- try(parseComment) <|> parseMySpaces
227 return $ DTF_Fill r val (fill ++ (if repeat == "1" then "" else repeat) ++ code) l comment
230 l <- try (parseLabel) <|> string ""
232 fill <- string ".ifill "
233 code <- many1 $ noneOf "\n;"
234 let val = case parse (instruction lno (0,d)) "" (code++"\n") of
236 Left err -> error $ show err
237 comment <- try(parseComment) <|> parseMySpaces
238 return $ DTF_Fill 1 val (fill ++ code) l comment
241 l <- try (parseLabel) <|> string ""
243 ascii <- string ".ascii "
245 str <- many1 $ noneOf "\"";
247 comment <- try(parseComment) <|> parseMySpaces
248 return $ DTF_Ascii str (ascii ++ "\"" ++ str ++ "\"") l comment
251 comment <- parseComment
252 return $ DTF_Comment comment
256 return $ DTF_Comment wtf
260 comment <- try(parseComment) <|> parseMySpaces
261 return $ DTF_Label l comment 0
263 lf_toparse lno _ = do
264 l <- try(parseLabel) <|> string ""
266 code <- many1 $ noneOf "\n;"
267 comment <- try(parseComment) <|> parseMySpaces
268 return $ DTF_InstrToParse 0 code code l comment lno
281 return $ DTF_State InData
287 return $ DTF_State InText
295 -- TODO: expressions with (expr) do not work ;(
297 comment <- try(parseComment) <|> parseMySpaces
298 return $ DTF_Define id ret comment