3a_asm: changed data structure again to calculate relative addresses
[calu.git] / 3a_asm / DT.hs
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)]