-- parsing --
-instruction :: [DictLabel] -> Parser Word32
+instruction :: Dict -> Parser Word32
instruction dict = foldl1 (<|>) (fmap (\x -> try (x dict)) instructions) <* char '\n'
testins :: String -> IO ()
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}
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
-- ldx, stx
-- call, reti
br, ret
- -- brreg
+ -- brr, callr
-- cmp, cmpi
]
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)]
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)]