X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=compiler%2Fcmm%2FCmmParse.y;h=4690f6989c9f8ba491814668498c44774578f213;hb=9c4b3b77b7c1e07031326ddfd6c60997d3397e6d;hp=73618bc35bd8e3ba87360e1d9d216ed6ef01cbe7;hpb=0065d5ab628975892cea1ec7303f968c3338cbe1;p=ghc-hetmet.git diff --git a/compiler/cmm/CmmParse.y b/compiler/cmm/CmmParse.y index 73618bc..4690f69 100644 --- a/compiler/cmm/CmmParse.y +++ b/compiler/cmm/CmmParse.y @@ -1,6 +1,6 @@ ----------------------------------------------------------------------------- -- --- (c) The University of Glasgow, 2004 +-- (c) The University of Glasgow, 2004-2006 -- -- Parser for concrete Cmm. -- @@ -16,39 +16,39 @@ import CgProf import CgTicky import CgInfoTbls import CgForeignCall -import CgTailCall ( pushUnboxedTuple ) -import CgStackery ( emitPushUpdateFrame ) -import ClosureInfo ( C_SRT(..) ) -import CgCallConv ( smallLiveness ) -import CgClosure ( emitBlackHoleCode ) -import CostCentre ( dontCareCCS ) +import CgTailCall +import CgStackery +import ClosureInfo +import CgCallConv +import CgClosure +import CostCentre import Cmm import PprCmm -import CmmUtils ( mkIntCLit ) +import CmmUtils import CmmLex import CLabel import MachOp -import SMRep ( fixedHdrSize, CgRep(..) ) +import SMRep import Lexer -import ForeignCall ( CCallConv(..), Safety(..) ) -import Literal ( mkMachInt ) +import ForeignCall +import Literal import Unique import UniqFM import SrcLoc -import DynFlags ( DynFlags, DynFlag(..) ) -import Packages ( HomeModules ) -import StaticFlags ( opt_SccProfilingOn ) -import ErrUtils ( printError, dumpIfSet_dyn, showPass ) -import StringBuffer ( hGetStringBuffer ) +import DynFlags +import StaticFlags +import ErrUtils +import StringBuffer import FastString -import Panic ( panic ) -import Constants ( wORD_SIZE ) +import Panic +import Constants import Outputable -import Monad ( when ) +import Control.Monad import Data.Char ( ord ) +import System.Exit #include "HsVersions.h" } @@ -103,6 +103,8 @@ import Data.Char ( ord ) 'if' { L _ (CmmT_if) } 'jump' { L _ (CmmT_jump) } 'foreign' { L _ (CmmT_foreign) } + 'prim' { L _ (CmmT_prim) } + 'return' { L _ (CmmT_return) } 'import' { L _ (CmmT_import) } 'switch' { L _ (CmmT_switch) } 'case' { L _ (CmmT_case) } @@ -223,18 +225,14 @@ info :: { ExtFCode (CLabel, [CmmLit],[CmmLit]) } | 'INFO_TABLE_CONSTR' '(' NAME ',' INT ',' INT ',' INT ',' INT ',' STRING ',' STRING ')' -- ptrs, nptrs, tag, closure type, description, type - { stdInfo $3 $5 $7 $9 $11 $13 $15 } + { conInfo $3 $5 $7 $9 $11 $13 $15 } | 'INFO_TABLE_SELECTOR' '(' NAME ',' INT ',' INT ',' STRING ',' STRING ')' -- selector, closure type, description, type { basicInfo $3 (mkIntCLit (fromIntegral $5)) 0 $7 $9 $11 } - | 'INFO_TABLE_RET' '(' NAME ',' INT ',' INT ',' INT maybe_vec ')' - { retInfo $3 $5 $7 $9 $10 } - -maybe_vec :: { [CmmLit] } - : {- empty -} { [] } - | ',' NAME maybe_vec { CmmLabel (mkRtsCodeLabelFS $2) : $3 } + | 'INFO_TABLE_RET' '(' NAME ',' INT ',' INT ',' INT ')' + { retInfo $3 $5 $7 $9 } body :: { ExtCode } : {- empty -} { return () } @@ -253,31 +251,31 @@ names :: { [FastString] } stmt :: { ExtCode } : ';' { nopEC } - | block_id ':' { code (labelC $1) } + | NAME ':' + { do l <- newLabel $1; code (labelC l) } - | lreg '=' expr ';' - { do reg <- $1; e <- $3; stmtEC (CmmAssign reg e) } +-- HACK: this should just be lregs but that causes a shift/reduce conflict +-- with foreign calls + | hint_lregs '=' expr ';' + { do reg <- head $1; e <- $3; stmtEC (CmmAssign (fst reg) e) } | type '[' expr ']' '=' expr ';' { doStore $1 $3 $6 } - | 'foreign' STRING expr '(' hint_exprs0 ')' vols ';' - {% foreignCall $2 [] $3 $5 $7 } - | lreg '=' 'foreign' STRING expr '(' hint_exprs0 ')' vols ';' - {% let result = do r <- $1; return (r,NoHint) in - foreignCall $4 [result] $5 $7 $9 } - | STRING lreg '=' 'foreign' STRING expr '(' hint_exprs0 ')' vols ';' - {% do h <- parseHint $1; - let result = do r <- $2; return (r,h) in - foreignCall $5 [result] $6 $8 $10 } + | maybe_results 'foreign' STRING expr '(' hint_exprs0 ')' vols ';' + {% foreignCall $3 $1 $4 $6 $8 } + | maybe_results 'prim' '%' NAME '(' hint_exprs0 ')' vols ';' + {% primCall $1 $4 $6 $8 } -- stmt-level macros, stealing syntax from ordinary C-- function calls. -- Perhaps we ought to use the %%-form? | NAME '(' exprs0 ')' ';' {% stmtMacro $1 $3 } | 'switch' maybe_range expr '{' arms default '}' { doSwitch $2 $3 $5 $6 } - | 'goto' block_id ';' - { stmtEC (CmmBranch $2) } - | 'jump' expr {-maybe_actuals-} ';' - { do e <- $2; stmtEC (CmmJump e []) } + | 'goto' NAME ';' + { do l <- lookupLabel $2; stmtEC (CmmBranch l) } + | 'jump' expr maybe_actuals ';' + { do e1 <- $2; e2 <- sequence $3; stmtEC (CmmJump e1 e2) } + | 'return' maybe_actuals ';' + { do e <- sequence $2; stmtEC (CmmReturn e) } | 'if' bool_expr '{' body '}' else { ifThenElse $2 $4 $6 } @@ -369,6 +367,10 @@ maybe_ty :: { MachRep } : {- empty -} { wordRep } | '::' type { $2 } +maybe_actuals :: { [ExtFCode (CmmExpr, MachHint)] } + : {- empty -} { [] } + | '(' hint_exprs0 ')' { $2 } + hint_exprs0 :: { [ExtFCode (CmmExpr, MachHint)] } : {- empty -} { [] } | hint_exprs { $1 } @@ -395,6 +397,21 @@ reg :: { ExtFCode CmmExpr } : NAME { lookupName $1 } | GLOBALREG { return (CmmReg (CmmGlobal $1)) } +maybe_results :: { [ExtFCode (CmmReg, MachHint)] } + : {- empty -} { [] } + | hint_lregs '=' { $1 } + +hint_lregs :: { [ExtFCode (CmmReg, MachHint)] } + : hint_lreg ',' { [$1] } + | hint_lreg { [$1] } + | hint_lreg ',' hint_lregs { $1 : $3 } + +hint_lreg :: { ExtFCode (CmmReg, MachHint) } + : lreg { do e <- $1; return (e, inferHint (CmmReg e)) } + | STRING lreg {% do h <- parseHint $1; + return $ do + e <- $2; return (e,h) } + lreg :: { ExtFCode CmmReg } : NAME { do e <- lookupName $1; return $ @@ -403,13 +420,6 @@ lreg :: { ExtFCode CmmReg } other -> pprPanic "CmmParse:" (ftext $1 <> text " not a register") } | GLOBALREG { return (CmmGlobal $1) } -block_id :: { BlockId } - : NAME { BlockId (newTagUnique (getUnique $1) 'L') } - -- TODO: ugh. The unique of a FastString has a null - -- tag, so we have to put our own tag on. We should - -- really make a new unique for every label, and keep - -- them in an environment. - type :: { MachRep } : 'bits8' { I8 } | typenot8 { $1 } @@ -425,6 +435,7 @@ section :: String -> Section section "text" = Text section "data" = Data section "rodata" = ReadOnlyData +section "relrodata" = RelocatableReadOnlyData section "bss" = UninitialisedData section s = OtherSection s @@ -472,8 +483,7 @@ exprMacros = listToUFM [ ( FSLIT("GET_FUN_INFO"), \ [x] -> funInfoTable (closureInfoPtr x) ), ( FSLIT("INFO_TYPE"), \ [x] -> infoTableClosureType x ), ( FSLIT("INFO_PTRS"), \ [x] -> infoTablePtrs x ), - ( FSLIT("INFO_NPTRS"), \ [x] -> infoTableNonPtrs x ), - ( FSLIT("RET_VEC"), \ [info, conZ] -> retVec info conZ ) + ( FSLIT("INFO_NPTRS"), \ [x] -> infoTableNonPtrs x ) ] -- we understand a subset of C-- primitives: @@ -529,13 +539,19 @@ machOps = listToUFM $ ( "f2f32", flip MO_S_Conv F32 ), -- TODO; rounding mode ( "f2f64", flip MO_S_Conv F64 ), -- TODO; rounding mode ( "f2i8", flip MO_S_Conv I8 ), - ( "f2i16", flip MO_S_Conv I8 ), - ( "f2i32", flip MO_S_Conv I8 ), - ( "f2i64", flip MO_S_Conv I8 ), + ( "f2i16", flip MO_S_Conv I16 ), + ( "f2i32", flip MO_S_Conv I32 ), + ( "f2i64", flip MO_S_Conv I64 ), ( "i2f32", flip MO_S_Conv F32 ), ( "i2f64", flip MO_S_Conv F64 ) ] +callishMachOps = listToUFM $ + map (\(x, y) -> (mkFastString x, y)) [ + ( "write_barrier", MO_WriteBarrier ) + -- ToDo: the rest, maybe + ] + parseHint :: String -> P MachHint parseHint "ptr" = return PtrHint parseHint "signed" = return SignedHint @@ -605,6 +621,7 @@ stmtMacros = listToUFM [ ( FSLIT("RET_NN"), \[a,b] -> emitRetUT [(NonPtrArg,a),(NonPtrArg,b)]), ( FSLIT("RET_NP"), \[a,b] -> emitRetUT [(NonPtrArg,a),(PtrArg,b)]), ( FSLIT("RET_PPP"), \[a,b,c] -> emitRetUT [(PtrArg,a),(PtrArg,b),(PtrArg,c)]), + ( FSLIT("RET_NPP"), \[a,b,c] -> emitRetUT [(NonPtrArg,a),(PtrArg,b),(PtrArg,c)]), ( FSLIT("RET_NNP"), \[a,b,c] -> emitRetUT [(NonPtrArg,a),(NonPtrArg,b),(PtrArg,c)]), ( FSLIT("RET_NNNP"), \[a,b,c,d] -> emitRetUT [(NonPtrArg,a),(NonPtrArg,b),(NonPtrArg,c),(PtrArg,d)]), ( FSLIT("RET_NPNP"), \[a,b,c,d] -> emitRetUT [(NonPtrArg,a),(PtrArg,b),(NonPtrArg,c),(PtrArg,d)]) @@ -623,8 +640,9 @@ stmtMacros = listToUFM [ -- to collect declarations as we parse the proc, and feed the environment -- back in circularly (to avoid a two-pass algorithm). -type Decls = [(FastString,CmmExpr)] -type Env = UniqFM CmmExpr +data Named = Var CmmExpr | Label BlockId +type Decls = [(FastString,Named)] +type Env = UniqFM Named newtype ExtFCode a = EC { unEC :: Env -> Decls -> FCode (Decls, a) } @@ -649,13 +667,30 @@ getEnv :: ExtFCode Env getEnv = EC $ \e s -> return (s, e) addVarDecl :: FastString -> CmmExpr -> ExtCode -addVarDecl var expr = EC $ \e s -> return ((var,expr):s, ()) +addVarDecl var expr = EC $ \e s -> return ((var, Var expr):s, ()) + +addLabel :: FastString -> BlockId -> ExtCode +addLabel name block_id = EC $ \e s -> return ((name, Label block_id):s, ()) newLocal :: MachRep -> FastString -> ExtCode newLocal ty name = do u <- code newUnique addVarDecl name (CmmReg (CmmLocal (LocalReg u ty))) +newLabel :: FastString -> ExtFCode BlockId +newLabel name = do + u <- code newUnique + addLabel name (BlockId u) + return (BlockId u) + +lookupLabel :: FastString -> ExtFCode BlockId +lookupLabel name = do + env <- getEnv + return $ + case lookupUFM env name of + Just (Label l) -> l + _other -> BlockId (newTagUnique (getUnique name) 'L') + -- Unknown names are treated as if they had been 'import'ed. -- This saves us a lot of bother in the RTS sources, at the expense of -- deferring some errors to link time. @@ -664,8 +699,8 @@ lookupName name = do env <- getEnv return $ case lookupUFM env name of - Nothing -> CmmLit (CmmLabel (mkRtsCodeLabelFS name)) - Just e -> e + Just (Var e) -> e + _other -> CmmLit (CmmLabel (mkRtsCodeLabelFS name)) -- Lifting FCode computations into the ExtFCode monad: code :: FCode a -> ExtFCode a @@ -684,28 +719,38 @@ forkLabelledCodeEC ec = do stmts <- getCgStmtsEC ec code (forkCgStmts stmts) -retInfo name size live_bits cl_type vector = do +retInfo name size live_bits cl_type = do let liveness = smallLiveness (fromIntegral size) (fromIntegral live_bits) info_lbl = mkRtsRetInfoLabelFS name (info1,info2) = mkRetInfoTable info_lbl liveness NoC_SRT - (fromIntegral cl_type) vector + (fromIntegral cl_type) return (info_lbl, info1, info2) stdInfo name ptrs nptrs srt_bitmap cl_type desc_str ty_str = basicInfo name (packHalfWordsCLit ptrs nptrs) srt_bitmap cl_type desc_str ty_str +conInfo name ptrs nptrs srt_bitmap cl_type desc_str ty_str = do + (lbl, info1, _) <- basicInfo name (packHalfWordsCLit ptrs nptrs) + srt_bitmap cl_type desc_str ty_str + desc_lit <- code $ mkStringCLit desc_str + let desc_field = makeRelativeRefTo lbl desc_lit + return (lbl, info1, [desc_field]) + basicInfo name layout srt_bitmap cl_type desc_str ty_str = do + let info_lbl = mkRtsInfoLabelFS name lit1 <- if opt_SccProfilingOn - then code $ mkStringCLit desc_str + then code $ do lit <- mkStringCLit desc_str + return (makeRelativeRefTo info_lbl lit) else return (mkIntCLit 0) lit2 <- if opt_SccProfilingOn - then code $ mkStringCLit ty_str + then code $ do lit <- mkStringCLit ty_str + return (makeRelativeRefTo info_lbl lit) else return (mkIntCLit 0) let info1 = mkStdInfoTable lit1 lit2 (fromIntegral cl_type) (fromIntegral srt_bitmap) layout - return (mkRtsInfoLabelFS name, info1, []) + return (info_lbl, info1, []) funInfo name ptrs nptrs cl_type desc_str ty_str fun_type = do (label,info1,_) <- stdInfo name ptrs nptrs 0{-srt_bitmap-} @@ -729,15 +774,30 @@ foreignCall -> ExtFCode CmmExpr -> [ExtFCode (CmmExpr,MachHint)] -> Maybe [GlobalReg] -> P ExtCode -foreignCall "C" results_code expr_code args_code vols - = return $ do - results <- sequence results_code - expr <- expr_code - args <- sequence args_code - code (emitForeignCall' PlayRisky results - (CmmForeignCall expr CCallConv) args vols) -foreignCall conv _ _ _ _ - = fail ("unknown calling convention: " ++ conv) +foreignCall conv_string results_code expr_code args_code vols + = do convention <- case conv_string of + "C" -> return CCallConv + "C--" -> return CmmCallConv + _ -> fail ("unknown calling convention: " ++ conv_string) + return $ do + results <- sequence results_code + expr <- expr_code + args <- sequence args_code + code (emitForeignCall' PlayRisky results + (CmmForeignCall expr convention) args vols) where + +primCall + :: [ExtFCode (CmmReg,MachHint)] + -> FastString + -> [ExtFCode (CmmExpr,MachHint)] + -> Maybe [GlobalReg] -> P ExtCode +primCall results_code name args_code vols + = case lookupUFM callishMachOps name of + Nothing -> fail ("unknown primitive " ++ unpackFS name) + Just p -> return $ do + results <- sequence results_code + args <- sequence args_code + code (emitForeignCall' PlayRisky results (CmmPrim p) args vols) doStore :: MachRep -> ExtFCode CmmExpr -> ExtFCode CmmExpr -> ExtCode doStore rep addr_code val_code @@ -865,13 +925,13 @@ doSwitch mb_range scrut arms deflt initEnv :: Env initEnv = listToUFM [ ( FSLIT("SIZEOF_StgHeader"), - CmmLit (CmmInt (fromIntegral (fixedHdrSize * wORD_SIZE)) wordRep) ), + Var (CmmLit (CmmInt (fromIntegral (fixedHdrSize * wORD_SIZE)) wordRep) )), ( FSLIT("SIZEOF_StgInfoTable"), - CmmLit (CmmInt (fromIntegral stdInfoTableSizeB) wordRep) ) + Var (CmmLit (CmmInt (fromIntegral stdInfoTableSizeB) wordRep) )) ] -parseCmmFile :: DynFlags -> HomeModules -> FilePath -> IO (Maybe Cmm) -parseCmmFile dflags hmods filename = do +parseCmmFile :: DynFlags -> FilePath -> IO (Maybe Cmm) +parseCmmFile dflags filename = do showPass dflags "ParseCmm" buf <- hGetStringBuffer filename let @@ -881,9 +941,12 @@ parseCmmFile dflags hmods filename = do -- in there we don't want. case unP cmmParse init_state of PFailed span err -> do printError span err; return Nothing - POk _ code -> do - cmm <- initC dflags hmods no_module (getCmm (unEC code initEnv [] >> return ())) - dumpIfSet_dyn dflags Opt_D_dump_cmm "Cmm" (pprCmms [cmm]) + POk pst code -> do + cmm <- initC dflags no_module (getCmm (unEC code initEnv [] >> return ())) + let ms = getMessages pst + printErrorsAndWarnings dflags ms + when (errorsFound dflags ms) $ exitWith (ExitFailure 1) + dumpIfSet_dyn dflags Opt_D_dump_cmm "Cmm" (pprCmms [cmm]) return (Just cmm) where no_module = panic "parseCmmFile: no module"