fa1ddb1571e3bfacc8297155fc552c6485fa615f
[calu.git] / 3a_asm / DTFormat.hs
1 module DTFormat where
2
3 import Numeric
4 import Data.Word
5 import Data.Char
6 import Text.Printf
7 import Text.Parsec
8 import Text.Parsec.String
9 import Text.Parsec.Combinator
10 import Text.Parsec.Expr
11 import Text.ParserCombinators.Parsec.Token
12 import Text.ParserCombinators.Parsec.Expr
13 import Control.Monad
14
15 data DT_State = NoState | InData | InText deriving (Show,Eq)
16
17 type Address = Word32
18 type Value = Word32
19 type Repeat = Word32
20 type ValueToParse = String
21 type Code = String
22 type Label = String
23 type Comment = String
24 type Ascii = String
25 type LineNo = Word32
26
27 data DTF =
28         DTF_Data Address Value Code Label Comment | -- 0;...
29         DTF_Instr Address Value Code Label Comment | -- 1;...
30         DTF_Comment Comment | -- 2;...
31         DTF_Label Label Comment Address | -- 3;...
32         -- types for intern processing
33         DTF_InstrToParse Address ValueToParse Code Label Comment LineNo |
34         DTF_SectionToDet Address Value Code Label Comment |
35         DTF_Org Address |
36         DTF_Define Label Value Comment |
37         DTF_Fill Repeat Value Label Code Comment |
38         DTF_Ascii String Label Code Comment |
39         DTF_State DT_State
40
41 instance Show (DTF) where
42         showsPrec n = showsDTF
43
44 showsDTF :: DTF -> ShowS
45 showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s)
46 showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s)
47 showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c)
48 showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c)
49 showsDTF (DTF_InstrToParse a v c l s lno) = (++) (printf "itp;%08x;%s;%s;%s;%s@%d\n" a v c l s lno)
50 showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
51 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
52 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08x;%s\n" l a c)
53 showsDTF (DTF_Fill r v code l c) = (++) (printf "fill;%08x;%08x;%s;%s;%s\n" r v code l c)
54 showsDTF (DTF_Ascii str l code c) = (++) (printf "ascii;%s;%s;%s;%s" str code l c)
55 showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s))
56
57 datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
58 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
59
60 tobin :: Value -> String
61 tobin v = reverse $ take 32 $ reverse $ (['0' | _<-[0..32]]) ++ (showIntAtBase 2 (chr. ((+)0x30)) v "")
62
63 showsDTFBase :: DTF -> Int -> ShowS
64 showsDTFBase (DTF_Data a v c l s) 2 = (++) (datinsBin "0" a (tobin v) c l s)
65 showsDTFBase (DTF_Instr a v c l s) 2 = (++) (datinsBin "1" a (tobin v) c l s)
66 showsDTFBase d _ = showsDTF d
67
68 datinsBin :: String -> Address -> String -> Code -> Label -> Comment -> String
69 datinsBin = printf "%s;%08x;%s;%s;%s;%s\n"
70
71 -- datastructure for managing labels and defines
72 type Dict = (Address,[DictElem])
73 type DictElem = (String,Word32)
74
75 get_elem :: String -> [DictElem] -> Word32
76 get_elem s dict = case (lookup s dict) of
77                 Nothing -> error ("unknown label or define: \"" ++ s ++ "\"")
78                 Just z -> z
79
80 add_elem :: [DictElem] -> (String,Word32) -> [DictElem]
81 add_elem dic (s,w)
82         | s == "" = dic -- ignore empty string
83         | already_in =  error ("Label or define \"" ++ s ++ "\" already exists")
84         | otherwise = (s,w):dic
85         where
86         already_in = case (lookup s dic) of
87                 Just _ -> True
88                 Nothing -> False
89
90 -- some common functions
91 parseMySpaces :: Parser String
92 parseMySpaces = do
93         ret <- many $ oneOf "\t "
94         return $ ret