Changeset 4873 for src/Pugs/Compile

Show
Ignore:
Timestamp:
06/20/05 18:38:20 (3 years ago)
Author:
autrijus
svk:copy_cache_prev:
6641
Message:

* return() from subroutines now work in PIR.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Compile/PIR.hs

    r4871 r4873  
    3737    PPad        :: ![(VarName, PIL Expression)] -> !(PIL [Stmt]) -> PIL [Stmt] 
    3838 
    39     PSub        :: !SubName -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl 
     39    PSub        :: !SubName -> !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl 
    4040#endif 
    4141 
     
    7878    show (PBlock x) = "(PBlock " ++ show x ++ ")" 
    7979    show (PRawName x) = "(PRawName " ++ show x ++ ")" 
    80     show (PSub x y z) = "(PSub " ++ show x ++ " " ++ show y ++ " " ++ show z ++ ")" 
     80    show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"] 
    8181 
    8282data TEnv = MkTEnv 
     
    141141            compile ("&*END", concat decls) 
    142142        canCompile _ = return [] 
    143         doCode name vsub = if subType vsub == SubPrim 
    144             then return [] 
    145             else compile (name, vsub) 
     143        doCode name vsub = case subBody vsub of 
     144            Prim _  -> return [] 
     145            _       -> compile (name, vsub) 
    146146 
    147147instance Compile ([Char], [PIL Decl]) [PIL Decl] where 
    148148    compile (name, decls) = do 
    149149        let bodyC = [ PStmts . PStmt . PExp $ PApp tcVoid (PExp (PVar sub)) [] 
    150                     | PSub sub _ _ <- decls 
     150                    | PSub sub _ _ _ <- decls 
    151151                    ] 
    152         return (PSub name [] (combine bodyC PNil):decls) 
     152        return (PSub name SubPrim [] (combine bodyC PNil):decls) 
    153153 
    154154instance Compile ([Char], VCode) [PIL Decl] where 
    155     compile (name, MkCode{ subBody = Syn "block" [body], subParams = params }) = do 
     155    compile (name, MkCode{ subType = styp, subBody = Syn "block" [body], subParams = params }) = do 
    156156        bodyC   <- enter cxtItemAny $ compile body 
    157157        paramsC <- mapM compile params 
    158         return [PSub name paramsC bodyC] 
    159     compile x = compError x 
     158        return [PSub name styp paramsC bodyC] 
     159    compile (name, code) = compile 
     160        (name, code{ subBody = Syn "block" [subBody code] }) 
    160161 
    161162{- 
     
    274275    compile exp@(Syn "while" _) = compLoop exp 
    275276    compile exp@(Syn "until" _) = compLoop exp 
     277    compile (Syn "given" _) = compile (Var "$_") -- XXX 
     278    compile (Syn "when" _) = compile (Var "$_") -- XXX 
    276279    compile (Syn "{}" (x:xs)) = compile (App (Var "&postcircumfix:{}") (Just x) xs) 
    277280    compile (Syn "[]" (x:xs)) = do 
     
    328331    compile (Syn "sub" [Val (VCode sub)]) = do 
    329332        -- XXX I'd like to lambda lift... :-/ 
    330         cxt     <- askTCxt 
     333             <- askTCxt 
    331334        bodyC   <- compile (subBody sub) 
    332         return $ PExp $ PApp cxt (PBlock bodyC) [] 
     335        return $ PBlock bodyC 
    333336    compile (Syn "for" _) = compile Noop -- XXX TODO 
    334337    compile (Syn "module" _) = compile Noop 
     
    342345 
    343346compError :: forall a b. Compile a b => a -> Comp b 
    344 compError = die $ "Compile error -- invalid PIL " 
    345     ++ (drop 12 . show $ typeOf (undefined :: b)) 
     347compError = die $ "Compile error -- invalid " 
     348    ++ (show $ typeOf (undefined :: b)) 
    346349 
    347350transError :: forall a b. Translate a b => a -> Trans b 
     
    468471            then "store_global" .- [tempSTR, bodyC] -- XXX HACK 
    469472            else "set_args" .- [lit "(0b10)", bodyC] 
    470         tellIns $ "invoke" .- [reg cc] 
     473--      tellIns $ "invoke" .- [reg cc] 
    471474        tellLabel endC 
    472475        return (ExpLV this) 
     
    495498        pmc     <- genName name 
    496499        return (ExpLV pmc) 
    497     trans (PSub name params body) = do 
     500    trans (PSub name styp params body) = do 
    498501        (_, stmts)  <- listen $ do 
    499502            let prms = map tpParam params 
     
    501504            tellIns $ "get_params" .- sigList (map prmToSig prms) 
    502505            tellIns $ "new_pad" .- [lit curPad] 
    503             mapM storeLex params 
    504             trans body 
    505             bodyC <- lastPMC 
    506             tellIns $ "set_returns" .- retSigList [bodyC] 
    507             tellIns $ "returncc" .- [] 
     506 
     507 
     508            wrapSub styp $ do 
     509                mapM storeLex params 
     510                trans body 
     511                bodyC <- lastPMC 
     512                tellIns $ "set_returns" .- retSigList [bodyC] 
     513                tellIns $ "returncc" .- [] 
     514 
    508515        return (DeclSub name [] stmts) 
    509516        where 
     
    531538                tellLabel defC 
    532539            tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 
     540        -- XXX - slow way of implementing "return" 
     541        wrapSub SubPrim = id 
     542        wrapSub _ = \body -> do 
     543            [retL] <- genLabel ["returnHandler"] 
     544            tellIns $ "push_eh" .- [bare retL] 
     545            body 
     546            tellLabel retL 
     547            tellIns $ tempPMC <:= ExpLV (KEYED (VAR "P5") (lit False)) 
     548            tellIns $ "set_returns" .- sigList [tempPMC] 
     549            tellIns $ "returncc" .- [] 
    533550    trans x = transError x 
    534551 
     
    553570    name'   <- liftIO $ liftSTM $ do 
    554571        (cur, name) <- readTVar tvar 
    555         return $ "P" ++ show cur ++ "_" ++ name 
     572        return $ "P" ++ show cur ++ (if null name then name else ('_':name)) 
    556573    return $ reg (VAR name') 
    557574