3a_asm: .ascii directive
[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
26 data DTF =
27         DTF_Data Address Value Code Label Comment | -- 0;...
28         DTF_Instr Address Value Code Label Comment | -- 1;...
29         DTF_Comment Comment | -- 2;...
30         DTF_Label Label Comment Address | -- 3;...
31         -- types for intern processing
32         DTF_InstrToParse Address ValueToParse Code Label Comment |
33         DTF_SectionToDet Address Value Code Label Comment |
34         DTF_Org Address |
35         DTF_Define Label Value Comment |
36         DTF_Fill Repeat Value Label Code Comment |
37         DTF_Ascii String Label Code Comment |
38         DTF_State DT_State
39
40 instance Show (DTF) where
41         showsPrec n = showsDTF
42
43 showsDTF :: DTF -> ShowS
44 showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s)
45 showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s)
46 showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c)
47 showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c)
48 showsDTF (DTF_InstrToParse a v c l s) = (++) (printf "itp;%08x;%s;%s;%s;%s\n" a v c l s)
49 showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
50 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
51 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08x;%s\n" l a c)
52 showsDTF (DTF_Fill r v code l c) = (++) (printf "fill;%08x;%08x;%s;%s;%s\n" r v code l c)
53 showsDTF (DTF_Ascii str l code c) = (++) (printf "ascii;%s;%s;%s;%s" str code l c)
54 showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s))
55
56 datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
57 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
58
59 tobin :: Value -> String
60 tobin v = reverse $ take 32 $ reverse $ (['0' | _<-[0..32]]) ++ (showIntAtBase 2 (chr. ((+)0x30)) v "")
61
62 showsDTFBase :: DTF -> Int -> ShowS
63 showsDTFBase (DTF_Data a v c l s) 2 = (++) (datinsBin "0" a (tobin v) c l s)
64 showsDTFBase (DTF_Instr a v c l s) 2 = (++) (datinsBin "1" a (tobin v) c l s)
65 showsDTFBase d _ = showsDTF d
66
67 datinsBin :: String -> Address -> String -> Code -> Label -> Comment -> String
68 datinsBin = printf "%s;%08x;%s;%s;%s;%s\n"
69
70 -- datastructure for managing labels and defines
71 type Dict = (Address,[DictElem])
72 type DictElem = (String,Word32)
73
74 get_elem :: String -> [DictElem] -> Word32
75 get_elem s dict = case (lookup s dict) of
76                 Nothing -> error ("unknown label or define: \"" ++ s ++ "\"")
77                 Just z -> z
78
79 add_elem :: [DictElem] -> (String,Word32) -> [DictElem]
80 add_elem dic (s,w)
81         | s == "" = dic -- ignore empty string
82         | already_in =  error ("Label or define \"" ++ s ++ "\" already exists")
83         | otherwise = (s,w):dic
84         where
85         already_in = case (lookup s dic) of
86                 Just _ -> True
87                 Nothing -> False
88
89 -- some common functions
90 parseMySpaces :: Parser String
91 parseMySpaces = do
92         ret <- many $ oneOf "\t "
93         return $ ret