sim: labels and comments are now shown
[calu.git] / 3a_asm / Main.hs
index 2e99807e06f0cf10a41347045dcf3d427becbeb3..08522c658d34fabbc0d97ac1cb7d2744d543418c 100644 (file)
@@ -1,6 +1,6 @@
 -- as for deep thoughts ISA
 -----------------------------------------------------------------------------
-import DT
+import DT hiding (not)
 import DTFormat
 import Expr_eval
 
@@ -25,13 +25,18 @@ main = do
        content <- getContents
        let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
        let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [("start_",0x00)]
-       printf "\nlabels:\n"
-       sequence_ [printf "%20s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
-       printf "\nparsed asm:\n"
-       sequence_ [printf "%s" (show x) | x <- formatedsrc]
+       if (not $ null args) && ("-d" `elem` args)
+               then do
+                       printf "\nlabels:\n"
+                       sequence_ [printf "%20s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
+                       printf "\nparsed asm:\n"
+                       sequence_ [printf "%s" (show x) | x <- formatedsrc]
+                       printf "\nafter parsing the instructions:\n"
+               else do
+                       printf ""
+       let base = if "-b" `elem` args then 2 else 16
        let parsed = parseInstr dict formatedsrc
-       printf "\nafter parsing the instructions:\n"
-       sequence_ [printf "%s" (show x) | x <- parsed]
+       sequence_ [printf "%s" (showsDTFBase x base "") | x <- parsed]
 
 
 parseInstr :: [DictElem] -> [DTF] -> [DTF]
@@ -58,6 +63,13 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        actlist (DTF_Org _) = next
        actlist (DTF_State _) = next
        actlist (DTF_Define _ _ _) = next
+       actlist (DTF_Fill rep val code l c) = if state == InData then
+               (DTF_Data datacnt val code l c):[(DTF_Data dc val "" "" "") | i<-[2..repi], let dc = [(datacnt+0x4),(datacnt+0x8)..]!!(i-2)] ++ next
+               else
+               (DTF_Instr instrcnt val code l c):[(DTF_Instr ic val "" "" "") | i<-[2..repi], let ic = [(instrcnt+0x4),(instrcnt+0x8)..]!!(i-2)] ++ next
+               where
+               repi :: Int
+               repi = fromInteger (toInteger rep)
        actlist y = y:next
 
        (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
@@ -65,12 +77,18 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        ndatacnt (DTF_Org adr)
                | state == InData = adr
                | otherwise = datacnt
+       ndatacnt (DTF_Fill rep _ _ _ _)
+               | state == InData = datacnt + (4*rep)
+               | otherwise = datacnt
        ndatacnt (DTF_Data _ _ _ _ _) = inc datacnt
        ndatacnt _ = datacnt
 
        ninstrcnt (DTF_Org adr)
                | state == InText = adr
                | otherwise = instrcnt
+       ninstrcnt (DTF_Fill rep _ _ _ _)
+               | state == InText = instrcnt + (4*rep)
+               | otherwise = instrcnt
        ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
        ninstrcnt (DTF_InstrToParse _ _ _ _ _) = inc instrcnt
        ninstrcnt _ = instrcnt
@@ -84,6 +102,9 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
        ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
        ndict (DTF_Define l v _) = dict `add_elem` (l,v)
+       ndict (DTF_Fill _ _ _ l _)
+               | state == InText = dict `add_elem` (l,instrcnt)
+               | state == InData = dict `add_elem` (l,datacnt)
        ndict _ = dict
 
        newdtf = case (parse (parseDTFLine dict) "" (str++"\n")) of
@@ -104,7 +125,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
                                NoState -> error "missing .data or .text"
                                InData -> (DTF_Label l c datacnt)
                                InText -> (DTF_Label l c instrcnt)
-               Right z -> z -- DTF_Comment, DTF_State, DTF_Define
+               Right z -> z -- DTF_Comment, DTF_State, DTF_Define, DTF_Fill
 
 
 
@@ -119,7 +140,7 @@ testDTF input =
 parseDTFLine :: [DictElem] -> Parser DTF
 parseDTFLine dict = foldl1 (<|>) (fmap (\x -> try (x dict)) lineFormats) <* char '\n'
 
-lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
+lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_ifill, lf_comment, lf_toparse, lf_label]
 
 -- helper
 parseIdent :: Parser String
@@ -149,12 +170,28 @@ lf_data d = do
        l <- try (parseLabel) <|> string ""
        skipMany space
        fill <- string ".fill "
+       repeat <- try (do {size <- many1 $ noneOf "\n;,"; char ','; return $ size}) <|> return "1"
        -- TODO: atm 32bit imm only
        code <- many1 $ noneOf "\n;"
-       -- TODO: this is quite ugly here :/
-       let (Right val) = parse (parseConst d) "" code
+       let val = case parse (parseConst d) "" code of
+               Right v -> v
+               Left err -> error $ show err
+       let r = case parse (parseConst d) "" repeat of
+               Right v -> v
+               Left err -> error $ show err
+       comment <- try(parseComment) <|> parseMySpaces
+       return $ DTF_Fill r val (fill ++ (if repeat == "1" then "" else repeat) ++ code) l comment
+
+lf_ifill d = do
+       l <- try (parseLabel) <|> string ""
+       skipMany space
+       fill <- string ".ifill "
+       code <- many1 $ noneOf "\n;"
+       let val = case parse (instruction (0,d)) "" (code++"\n") of
+               Right v -> v
+               Left err -> error $ show err
        comment <- try(parseComment) <|> parseMySpaces
-       return $ DTF_SectionToDet 0 val (fill ++ code) l comment
+       return $ DTF_Fill 1 val (fill ++ code) l comment
 
 lf_comment _ = do
        comment <- parseComment