From 0df70b557af4fd0550f0db2aa56fa3d89828d0a2 Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Mon, 1 Nov 2010 00:40:48 +0100 Subject: [PATCH] 3a_asm: using Expr in both stages --- 2_isa/src/bootrom.s | 9 ++++---- 3a_asm/DT.hs | 44 ++++++++++++++------------------------ 3a_asm/Expr_eval.hs | 6 +++--- 3a_asm/Main.hs | 52 +++++++++++++++++++++------------------------ 3a_asm/notes | 3 ++- 5 files changed, 49 insertions(+), 65 deletions(-) diff --git a/2_isa/src/bootrom.s b/2_isa/src/bootrom.s index 98d6df3..d60c316 100644 --- a/2_isa/src/bootrom.s +++ b/2_isa/src/bootrom.s @@ -1,12 +1,11 @@ .data .text - ; TODO: will the assembler be able to evaluate these expressions? .define UART_BASE, 0x1000 - .define UART_STATUS, (UART_BASE+0x4) - .define UART_RECV, (UART_BASE+0x8) - .define UART_TRANS, (UART_BASE+0xC) - .define UART_BAUD, (UART_BAUD+0x10) + .define UART_STATUS, UART_BASE+0x4 + .define UART_RECV, UART_BASE+0x8 + .define UART_TRANS, UART_BASE+0xC + .define UART_BAUD, UART_BASE+0x10 .define UART_TRANS_EMPTY, 0 .define UART_RECV_NEW, 1 diff --git a/3a_asm/DT.hs b/3a_asm/DT.hs index 7dee1c1..3b22543 100644 --- a/3a_asm/DT.hs +++ b/3a_asm/DT.hs @@ -1,6 +1,7 @@ module DT where import DTFormat +import Expr_eval import Prelude hiding (and,or) @@ -31,19 +32,12 @@ testins input = comma = char ',' mnem m = string m -iLabel :: Dict -> Parser Word32 -iLabel (addr,dict) = do - s <- foldl1 (<|>) (fmap (try . string . fst) dict) - let lab = get_elem s dict - return $ ((lab - addr) .&. 0xefff) +iLit :: Dict -> Parser Word32 +iLit (_,d) = expr d -iLit :: Parser Word32 -iLit = do { parseMySpaces; ret <- liftM read (many1 digit); parseMySpaces; return ret} - --- TODO: ... -iLit12 = iLit -iLit15 = iLit -iLit16 = iLit +iLit12 d = do i <- iLit d; return $ i .&. 0x0fff +iLit15 d = do i <- iLit d; return $ i .&. 0xefff +iLit16 d = do i <- iLit d; return $ i .&. 0xffff imm4 :: Parser String imm4 = do @@ -94,22 +88,16 @@ infixl 1 <%> infixl 1 <@> ins m form e = do {mnem m; form e} -csv0i_p dict f = f<$>condition<*>branchpred<*>(iLabel dict) +csv0i_p dict f = f<$>condition<*>branchpred<*>(iLit 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 -csv2i_sl f = f<$>sign<*>highlow<*>condition<%>reg<.>iLit16 -csv2i_lfd f = f<$>highlow<*>fill<*>updateDisable<*>condition<%>reg<.>iLit16 +csv2m dict f = f<$>condition<%>reg<.>(iLit15 dict)<@>reg +csv2i_scd dict f = f<$>sign<*>carry<*>updateDisable<*>condition<%>reg<.>reg<.>(iLit12 dict) +csv2i_sl dict f = f<$>sign<*>highlow<*>condition<%>reg<.>(iLit16 dict) +csv2i_lfd dict f = f<$>highlow<*>fill<*>updateDisable<*>condition<%>reg<.>(iLit16 dict) csv3_cd f = f<$>carry<*>updateDisable<*>condition<%>reg<.>reg<.>reg csv3_d f = f<$>updateDisable<*>condition<%>reg<.>reg<.>reg --- ppc64 stuff (TODO: remove) -v1 f = f<$>iLit -csv4 f = f<$>iLit<.>iLit<.>iLit<.>iLit -csv5 f = f<$>iLit<.>iLit<.>iLit<.>iLit<.>iLit - - instructions = [add, addi, mov, sub, subi, and, andx, --, or, orx, xor, xorx,not] @@ -127,16 +115,16 @@ instructions = [add, addi, mov, sub, subi, -- arithmetic add _ = ins "add" csv3_cd $ aform 0x00 -addi _ = ins "addi" csv2i_scd $ aformi 0x02 +addi dict = ins "addi" (csv2i_scd dict) $ aformi 0x02 mov _ = ins "mov" csv2_scd $ aformi' 0x02 sub _ = ins "sub" csv3_cd $ aform 0x01 -subi _ = ins "subi" csv2i_scd $ aformi 0x03 +subi dict = ins "subi" (csv2i_scd dict) $ aformi 0x03 -- logic and _ = ins "and" csv3_d $ aform 0x04 0 -andx _ = ins "andx" csv2i_lfd $ lformi 0x05 +andx dict = ins "andx" (csv2i_lfd dict) $ lformi 0x05 -- memory -ldw _ = ins "ldw" csv2m $ mformi 0x0e -ldi _ = ins "ldi" csv2i_sl $ lformi' 0x1a +ldw dict = ins "ldw" (csv2m dict) $ mformi 0x0e +ldi dict = ins "ldi" (csv2i_sl dict) $ lformi' 0x1a -- misc -- set signed by default! in very rare cases this can be an issue. br dict = ins "br" (csv0i_p dict) $ bform 0x16 0x0 1 diff --git a/3a_asm/Expr_eval.hs b/3a_asm/Expr_eval.hs index 1b12f4b..6074074 100644 --- a/3a_asm/Expr_eval.hs +++ b/3a_asm/Expr_eval.hs @@ -1,4 +1,4 @@ -module Expr_eval (expr) where +module Expr_eval (expr, testExpr) where import DTFormat @@ -53,11 +53,11 @@ factor d = -- define or label s <- foldl1 (<|>) (fmap (try . string . fst) d); return $ (get_elem s d) - } <|> do { + } <|> try (do { string "0"; r <- hexadecimal; return $ fromInteger r - } <|> do { + } ) <|> do { r <- decimal; return $ fromInteger r } "factor" diff --git a/3a_asm/Main.hs b/3a_asm/Main.hs index fa5a12c..2e99807 100644 --- a/3a_asm/Main.hs +++ b/3a_asm/Main.hs @@ -1,9 +1,8 @@ -- as for deep thoughts ISA ----------------------------------------------------------------------------- - - import DT import DTFormat +import Expr_eval import Control.Applicative hiding ((<|>),many) @@ -25,9 +24,9 @@ main = do args <- getArgs content <- getContents let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content))) - let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [] + let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [("start_",0x00)] printf "\nlabels:\n" - sequence_ [printf "%10s @ 0x%08x\n" l a | (l,a) <- (reverse dict)] + sequence_ [printf "%20s @ 0x%08x\n" l a | (l,a) <- (reverse dict)] printf "\nparsed asm:\n" sequence_ [printf "%s" (show x) | x <- formatedsrc] let parsed = parseInstr dict formatedsrc @@ -87,7 +86,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt ndict (DTF_Define l v _) = dict `add_elem` (l,v) ndict _ = dict - newdtf = case (parse parseDTFLine "" (str++"\n")) of + newdtf = case (parse (parseDTFLine dict) "" (str++"\n")) of Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err)) Right (DTF_SectionToDet _ v c l s) -> case state of @@ -111,12 +110,14 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt testDTF :: String -> IO () testDTF input = - case (parse parseDTFLine "" (input++"\n")) of + case (parse (parseDTFLine dict) "" (input++"\n")) of Left err -> do { putStr "failz ;(\n"; print err} Right x -> do { print x } + where + dict = [("lolz", 0x1337), ("rofl", 0xaaaa)] -parseDTFLine :: Parser DTF -parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n' +parseDTFLine :: [DictElem] -> Parser DTF +parseDTFLine dict = foldl1 (<|>) (fmap (\x -> try (x dict)) lineFormats) <* char '\n' lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label] @@ -140,68 +141,63 @@ parseComment = do comment <- many $ noneOf "\n" return $ comment -parseConst :: Parser Word32 -parseConst = do - skipMany space - -- TODO: only decimal and hex (since this is supported by read) - -- TODO: howto check too big values? atm they get truncated - str <- try(do pref <- string "0x"; z <- many1 hexDigit; return $ (pref ++ z)) <|> (many1 digit) - let val = read str - return $ val +parseConst :: [DictElem] -> Parser Word32 +parseConst d = expr d -- teh pars0rs -lf_data = do +lf_data d = do l <- try (parseLabel) <|> string "" skipMany space fill <- string ".fill " -- TODO: atm 32bit imm only code <- many1 $ noneOf "\n;" -- TODO: this is quite ugly here :/ - let (Right val) = parse parseConst "" code + let (Right val) = parse (parseConst d) "" code comment <- try(parseComment) <|> parseMySpaces return $ DTF_SectionToDet 0 val (fill ++ code) l comment -lf_comment = do +lf_comment _ = do comment <- parseComment return $ DTF_Comment comment -lf_label = do +lf_label _ = do l <- parseLabel comment <- try(parseComment) <|> parseMySpaces return $ DTF_Label l comment 0 -lf_toparse = do +lf_toparse _ = do l <- try(parseLabel) <|> string "" skipMany space code <- many1 $ noneOf "\n;" comment <- try(parseComment) <|> parseMySpaces return $ DTF_InstrToParse 0 code code l comment -lf_org = do +lf_org d = do skipMany space string ".org" - val <- parseConst + val <- parseConst d parseMySpaces return $ DTF_Org val -lf_sdata = do +lf_sdata _ = do skipMany space string ".data" parseMySpaces return $ DTF_State InData -lf_stext = do +lf_stext _ = do skipMany space string ".text" parseMySpaces return $ DTF_State InText -lf_define = do +lf_define d = do skipMany space string ".define" parseMySpaces id <- parseIdent - parseMySpaces; char ','; parseMySpaces - ret <- parseConst + char ',' + -- TODO: expressions with (expr) do not work ;( + ret <- parseConst d comment <- try(parseComment) <|> parseMySpaces return $ DTF_Define id ret comment diff --git a/3a_asm/notes b/3a_asm/notes index 28ba566..157d08b 100644 --- a/3a_asm/notes +++ b/3a_asm/notes @@ -16,7 +16,7 @@ Adressenaufloesung nicht mehr viel schief gehen. einfaellt. - .org <- done - .fill aufloesen <- TODO: atm nur einmalig 32bit moeglich -- .define tabelle <- TODO +- .define tabelle <- done - @hi und @lo behandeln? <- TODO: doch eher second stage @@ -28,4 +28,5 @@ Adressenaufloesung nicht mehr viel schief gehen. einschraenkungen atm: - gleicher Namensraum fuer labels in .text und .data! wenn einer ein Label aus .data laedt und dann aber eigentlich auf .text zugreift ist er selber schuld :/ +- Expr geht zwar, aber nicht geklammert - viele viele mehr *testbench schreiben muss* -- 2.25.1