3a_asm: changed data structure again to calculate relative addresses
authorBernhard Urban <lewurm@gmail.com>
Sun, 31 Oct 2010 23:40:41 +0000 (00:40 +0100)
committerBernhard Urban <lewurm@gmail.com>
Sun, 31 Oct 2010 23:40:41 +0000 (00:40 +0100)
2_isa/src/sum.s
3a_asm/DT.hs
3a_asm/DTFormat.hs
3a_asm/Main.hs

index 3bee5e2370ce418c817258ed56092a6378c45779..0e9f5e22d011242f52754ea13bd93f5e3f0d14bf 100644 (file)
@@ -15,5 +15,5 @@ loop:
        add r0, r0, r4
        addi r2, r2, 4
        subi r3, r3, 1
-       branchnz+ loop
+       brnz+ loop
        ret
index caf8f0f3e447d65816dc57edfc9302db605281c1..7dee1c1662760c94b4316cef033a6da1b188440c 100644 (file)
@@ -16,7 +16,7 @@ import Control.Applicative hiding ((<|>))
 
 
 -- parsing --
-instruction :: [DictLabel] -> Parser Word32
+instruction :: Dict -> Parser Word32
 instruction dict = foldl1 (<|>) (fmap (\x -> try (x dict)) instructions) <* char '\n'
 
 testins :: String -> IO ()
@@ -25,17 +25,17 @@ testins input =
                Left err -> do { putStr "fail :/\n"; print err}
                Right x -> do { printf "0x%08X\n" x }
        where
-       dict = [("lolz", 0x1337), ("rofl", 0xaaaa)]
+       dict = (0x8000,[("lolz", 0x1337), ("rofl", 0xaaaa)])
 
 
 comma = char ','
 mnem m = string m
 
-iLabel :: [DictLabel] -> Parser Word32
-iLabel dict = do
+iLabel :: Dict -> Parser Word32
+iLabel (addr,dict) = do
        s <- foldl1 (<|>) (fmap (try . string . fst) dict)
-       let (Just ret) = get_label s dict
-       return ret
+       let lab = get_elem s dict
+       return $ ((lab - addr) .&. 0xefff)
 
 iLit :: Parser Word32
 iLit = do { parseMySpaces; ret <- liftM read (many1 digit); parseMySpaces; return ret}
@@ -94,8 +94,8 @@ infixl 1 <%>
 infixl 1 <@>
 
 ins m form e  = do {mnem m; form e}
-csv0i_sp dict f = f<$>sign<*>condition<*>branchpred<*>(iLabel dict)
-csv0i_sp' f = f<$>sign<*>condition<*>branchpred
+csv0i_p dict f = f<$>condition<*>branchpred<*>(iLabel dict)
+csv0i_p' f = f<$>condition<*>branchpred
 csv2_scd f = f<$>sign<*>carry<*>updateDisable<*>condition<%>reg<.>reg
 csv2m f = f<$>condition<%>reg<.>iLit15<@>reg
 csv2i_scd f = f<$>sign<*>carry<*>updateDisable<*>condition<%>reg<.>reg<.>iLit12
@@ -121,7 +121,7 @@ instructions = [add, addi, mov, sub, subi,
        -- ldx, stx
        -- call, reti
        br, ret
-       -- brreg
+       -- brr, callr
        -- cmp, cmpi
        ]
 
@@ -138,8 +138,10 @@ andx _ = ins "andx" csv2i_lfd $ lformi 0x05
 ldw _ = ins "ldw" csv2m $ mformi 0x0e
 ldi _ = ins "ldi" csv2i_sl $ lformi' 0x1a
 -- misc
-br dict = ins "br" (csv0i_sp dict) $ bform 0x16 0x0
-ret _ = ins "ret" csv0i_sp' $ bform' 0x16 0x2
+-- set signed by default! in very rare cases this can be an issue.
+br dict = ins "br" (csv0i_p dict) $ bform 0x16 0x0 1 
+ret _ = ins "ret" csv0i_p' $ bform' 0x16 0x2
+reti _ = ins "reti" csv0i_p' $ bform' 0x16 0x3
 
 -- instruction formats
 aform opcd c d cond rd ra rb = pack [(cond,28),(opcd,23),(rd,19),(ra,15),(rb,11),(free,2),(c,1),(d,0)]
@@ -155,7 +157,7 @@ mformi opcd cond rd disp ra = pack [(cond,28),(opcd,23),(rd,19),(ra,15),(disp,0)
 
 bform opcd typ s cond bp imm = pack [(cond,28),(opcd,23),(imm,7),(free,4),(typ,2),(bp,1),(s,0)]
        where free = 0
-bform' opcd typ s cond bp = bform opcd typ s cond bp 0
+bform' opcd typ cond bp = bform opcd typ 0 cond bp 0
 
 -- ppc64 stuff (TODO: remove)
 iform opcd aa lk li = pack [(opcd,6),(li,30),(aa,31),(lk,0)]
index 8c3d495655cce50852446804b29f849d00f13285..139367018da8ce0ca23e5f2f80361bb4310085c6 100644 (file)
@@ -44,18 +44,22 @@ datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
 
 
-type DictLabel = (String,Word32)
+-- datastructure for managing labels and defines
+type Dict = (Address,[DictElem])
+type DictElem = (String,Word32)
 
-get_label :: String -> [DictLabel] -> Maybe Word32
-get_label = lookup
+get_elem :: String -> [DictElem] -> Word32
+get_elem s dict = case (lookup s dict) of
+               Nothing -> error ("unknown label or define: \"" ++ s ++ "\"")
+               Just z -> z
 
-add_label :: [DictLabel] -> (String,Word32) -> [DictLabel]
-add_label dic (s,w)
+add_elem :: [DictElem] -> (String,Word32) -> [DictElem]
+add_elem dic (s,w)
        | s == "" = dic -- ignore empty string
-       | already_in =  error ("Label " ++ s ++ " already exists")
+       | already_in =  error ("Label or define \"" ++ s ++ "\" already exists")
        | otherwise = (s,w):dic
        where
-       already_in = case (get_label s dic) of
+       already_in = case (lookup s dic) of
                Just _ -> True
                Nothing -> False
 
index 2515674cf1444255fe6604d116ff55cc0ea7aa09..2952e8b53925251ba684962ee0b4455ccfcb0ecc 100644 (file)
@@ -35,12 +35,12 @@ main = do
        sequence_ [printf "%s" (show x) | x <- parsed]
 
 
-parseInstr :: [DictLabel] -> [DTF] -> [DTF]
+parseInstr :: [DictElem] -> [DTF] -> [DTF]
 parseInstr _ [] = []
 parseInstr dict ((DTF_InstrToParse a instr c l s):xs) =
        (DTF_Instr a bytecode c l s):(parseInstr dict xs)
        where
-       bytecode = case (parse (instruction dict) "" (instr++"\n")) of
+       bytecode = case (parse (instruction (a,dict)) "" (instr++"\n")) of
                Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
                Right x -> x
 parseInstr dict (x:xs) = x:(parseInstr dict xs)
@@ -52,7 +52,7 @@ inc :: Counter -> Counter
 inc = ((+) 4)
 
 
-convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictLabel] -> ([DictLabel], [DTF])
+convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
 convertDTF [] _ _ _ d = (d,[])
 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
        where
@@ -78,11 +78,11 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        nstate (DTF_State s) = s
        nstate _ = state
 
-       ndict (DTF_Label l _ a) = dict `add_label` (l,a)
-       ndict (DTF_SectionToDet a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_InstrToParse a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_Data a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_Instr a _ _ l _) = dict `add_label` (l,a)
+       ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
+       ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_InstrToParse a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
        ndict _ = dict
 
        newdtf = case (parse parseDTFLine "" (str++"\n")) of