Changeset 4879

Show
Ignore:
Timestamp:
06/20/05 20:52:17 (4 years ago)
Author:
autrijus
svk:copy_cache_prev:
6641
Message:

* inline subroutine: my &x = sub { return 3 }; say x()

Files:
1 modified

Legend:

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

    r4878 r4879  
    3838    PStmt       :: !(PIL Expression) -> PIL Stmt  
    3939    PThunk      :: !(PIL Expression) -> PIL Expression  
    40     PBlock      :: !(PIL [Stmt]) -> PIL Expression  
     40    PCode       :: !SubType -> !(PIL [Stmt]) -> PIL Expression  
    4141 
    4242    PVal        :: !Val -> PIL Literal 
     
    8888    show (PPad x y) = "(PPad " ++ show x ++ " " ++ show y ++ ")" 
    8989    show (PThunk x) = "(PThunk " ++ show x ++ ")" 
    90     show (PBlock x) = "(PBlock " ++ show x ++ ")" 
     90    show (PCode x y) = unwords ["(PCode", show x, show y, ")"] 
    9191    show (PRawName x) = "(PRawName " ++ show x ++ ")" 
    9292    show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"] 
     
    242242        postC   <- compile post 
    243243        funC    <- compile (Var "&statement_control:loop") 
    244         return $ PStmt $ PExp $ PApp TCxtVoid funC [preC, PBlock condC, PBlock bodyC, PBlock postC] 
     244        return $ PStmt $ PExp $ PApp TCxtVoid funC 
     245            [preC, pBlock condC, pBlock bodyC, pBlock postC] 
    245246    compile exp = fmap PStmt $ compile exp 
     247 
     248pBlock :: PIL [Stmt] -> PIL Expression 
     249pBlock = PCode SubBlock 
    246250 
    247251{- 
     
    325329    bodyC   <- compile body 
    326330    funC    <- compile (Var $ "&statement_control:" ++ name) 
    327     return $ PApp cxt funC [PBlock condC, PBlock bodyC] 
     331    return $ PApp cxt funC [pBlock condC, pBlock bodyC] 
    328332compLoop exp = compError exp 
    329333 
     
    350354        cxt     <- askTCxt 
    351355        bodyC   <- compile body 
    352         return $ PExp $ PApp cxt (PBlock bodyC) [] 
     356        return $ PExp $ PApp cxt (pBlock bodyC) [] 
    353357    compile (Syn "sub" [Val (VCode sub)]) = do 
    354358        -- XXX I'd like to lambda lift... :-/ 
     
    356360            Syn "block" [exp]   -> exp 
    357361            exp                 -> exp 
    358         return $ PBlock bodyC 
     362        return $ PCode (subType sub) bodyC 
    359363    compile (Syn "for" _) = compile Noop -- XXX TODO 
    360364    compile (Syn "module" _) = compile Noop 
     
    443447        tell [thisC] 
    444448        trans rest 
    445     trans (PApp _ exp@(PBlock _) []) = do 
     449    trans (PApp _ exp@(PCode _ _) []) = do 
    446450        blockC  <- trans exp 
    447451        tellIns $ [reg tempPMC] <-& blockC $ [] 
     
    480484            return ([], (StmtPad (map fst pad `zip` valsC) expsC:)) 
    481485    trans (PExp exp) = fmap ExpLV $ trans exp 
    482     trans (PBlock body) = do 
     486    trans (PCode styp body) = do 
    483487        [begC, endC] <- genLabel ["blockBegin", "blockEnd"] 
    484488        this    <- genPMC "block" 
     
    486490        tellIns $ "goto" .- [bare endC] 
    487491        tellLabel begC 
    488         trans body  -- XXX - consistency check 
    489         bodyC   <- lastPMC 
    490         tellIns $ "set_returns" .- retSigList [bodyC] 
    491         tellIns $ "returncc" .- [] 
     492        tellIns $ "new_pad" .- [lit curPad] 
     493        wrapSub styp $ do 
     494            trans body  -- XXX - consistency check 
     495            bodyC   <- lastPMC 
     496            tellIns $ "set_returns" .- retSigList [bodyC] 
     497            tellIns $ "returncc" .- [] 
    492498        tellLabel endC 
    493499        return (ExpLV this) 
     
    522528            tellIns $ "get_params" .- sigList (map prmToSig prms) 
    523529            tellIns $ "new_pad" .- [lit curPad] 
    524  
    525  
    526530            wrapSub styp $ do 
    527531                mapM storeLex params 
     
    530534                tellIns $ "set_returns" .- retSigList [bodyC] 
    531535                tellIns $ "returncc" .- [] 
    532  
    533536        return (DeclSub name [] stmts) 
    534537        where 
     
    556559                tellLabel defC 
    557560            tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 
    558         -- XXX - slow way of implementing "return" 
    559         wrapSub SubPrim = id 
    560         wrapSub _ = \body -> do 
    561             [retL] <- genLabel ["returnHandler"] 
    562             tellIns $ "push_eh" .- [bare retL] 
    563             body 
    564             tellLabel retL 
    565             tellIns $ tempPMC <:= ExpLV (KEYED (VAR "P5") (lit False)) 
    566             tellIns $ "set_returns" .- sigList [tempPMC] 
    567             tellIns $ "returncc" .- [] 
    568561    trans x = transError x 
    569562 
     
    575568fetchCC cc _ = do 
    576569    tellIns $ "get_params" .- sigList [reg cc] 
     570 
     571-- XXX - slow way of implementing "return" 
     572wrapSub :: SubType -> Trans () -> Trans () 
     573wrapSub SubPrim = id 
     574wrapSub SubBlock = id -- XXX not really 
     575wrapSub _ = \body -> do 
     576    [retL] <- genLabel ["returnHandler"] 
     577    tellIns $ "push_eh" .- [bare retL] 
     578    body 
     579    tellLabel retL 
     580    tellIns $ tempPMC <:= ExpLV (KEYED (VAR "P5") (lit False)) 
     581    tellIns $ "set_returns" .- sigList [tempPMC] 
     582    tellIns $ "returncc" .- [] 
    577583 
    578584tellIns :: Ins -> Trans ()