Changeset 4882

Show
Ignore:
Timestamp:
06/20/05 21:18:37 (4 years ago)
Author:
autrijus
svk:copy_cache_prev:
6641
Message:

* parametric blocks now works as specced!

Location:
src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/Emit/PIR.hs

    r4880 r4882  
    134134    emit (InsFun rets fun args) = emitFun "invokecc" fun args rets 
    135135    emit (InsTailFun (ExpLit (LitStr name)) args) = emitFunName "tailcall" name args [] 
     136    emit (InsTailFun fun args) = emitFun "tailcall" fun args [] 
    136137    emit (InsExp _) = empty 
    137138    emit (InsLabel label) = nest (-2) (emit label <> colon) 
    138139    emit (InsComment comment ins) = emit (StmtComment comment) $+$ emit ins 
    139     emit x = error $ "can't emit ins: " ++ show x 
    140140 
    141141emitRets :: [Sig] -> Doc 
  • src/Pugs/Compile/PIR.hs

    r4879 r4882  
    3535    PExp        :: !(PIL LValue) -> PIL Expression  
    3636    PLit        :: !(PIL Literal) -> PIL Expression 
    37     PPos        :: !Pos -> !Exp -> PIL a -> PIL a 
     37    PPos        :: !Pos -> !Exp -> !(PIL a) -> PIL a 
    3838    PStmt       :: !(PIL Expression) -> PIL Stmt  
    3939    PThunk      :: !(PIL Expression) -> PIL Expression  
    40     PCode       :: !SubType -> !(PIL [Stmt]) -> PIL Expression  
     40    PCode       :: !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Expression  
    4141 
    4242    PVal        :: !Val -> PIL Literal 
    4343    PVar        :: !VarName -> PIL LValue 
    4444 
    45     PStmts      :: !(PIL Stmt) -> PIL [Stmt] -> PIL [Stmt] 
     45    PStmts      :: !(PIL Stmt) -> !(PIL [Stmt]) -> PIL [Stmt] 
    4646    PApp        :: !TCxt -> !(PIL Expression) -> ![PIL Expression] -> PIL LValue 
    4747    PAssign     :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 
     
    8888    show (PPad x y) = "(PPad " ++ show x ++ " " ++ show y ++ ")" 
    8989    show (PThunk x) = "(PThunk " ++ show x ++ ")" 
    90     show (PCode x y) = unwords ["(PCode", show x, show y, ")"] 
     90    show (PCode x y z) = unwords ["(PCode", show x, show y, show z, ")"] 
    9191    show (PRawName x) = "(PRawName " ++ show x ++ ")" 
    9292    show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"] 
     
    167167    compile (name, MkCode{ subType = styp, subBody = Syn "block" [body], subParams = params }) = do 
    168168        bodyC   <- enter cxtItemAny $ compile body 
    169         paramsC <- mapM compile params 
     169        paramsC <- compile params 
    170170        return [PSub name styp paramsC bodyC] 
    171171    compile (name, code) = compile 
     
    247247 
    248248pBlock :: PIL [Stmt] -> PIL Expression 
    249 pBlock = PCode SubBlock 
     249pBlock = PCode SubBlock [] 
    250250 
    251251{- 
     
    360360            Syn "block" [exp]   -> exp 
    361361            exp                 -> exp 
    362         return $ PCode (subType sub) bodyC 
     362        paramsC <- compile $ subParams sub 
     363        return $ PCode (subType sub) paramsC bodyC 
    363364    compile (Syn "for" _) = compile Noop -- XXX TODO 
    364365    compile (Syn "module" _) = compile Noop 
     
    447448        tell [thisC] 
    448449        trans rest 
    449     trans (PApp _ exp@(PCode _ _) []) = do 
     450    trans (PApp _ exp@(PCode _ _ _) []) = do 
    450451        blockC  <- trans exp 
    451452        tellIns $ [reg tempPMC] <-& blockC $ [] 
     
    484485            return ([], (StmtPad (map fst pad `zip` valsC) expsC:)) 
    485486    trans (PExp exp) = fmap ExpLV $ trans exp 
    486     trans (PCode styp body) = do 
     487    trans (PCode styp params body) = do 
    487488        [begC, endC] <- genLabel ["blockBegin", "blockEnd"] 
    488489        this    <- genPMC "block" 
     
    490491        tellIns $ "goto" .- [bare endC] 
    491492        tellLabel begC 
     493        let prms = map tpParam params 
     494        mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
     495        tellIns $ "get_params" .- sigList (map prmToSig prms) 
    492496        tellIns $ "new_pad" .- [lit curPad] 
    493497        wrapSub styp $ do 
     498            mapM storeLex params 
    494499            trans body  -- XXX - consistency check 
    495500            bodyC   <- lastPMC 
     
    535540                tellIns $ "returncc" .- [] 
    536541        return (DeclSub name [] stmts) 
    537         where 
    538         -- XXX - slurpiness 
    539         prmToSig prm = MkSig (prmToArgs prm) . bare $ prmToIdent prm 
    540         prmToArgs prm = combine  
    541             [ if isSlurpy prm then (MkArgSlurpyArray:) else id 
    542             , if isOptional prm then (MkArgOptional:) else id 
    543             ] [] 
    544         prmToIdent = render . varText . paramName 
    545         storeLex param = do 
    546             let var = paramName prm 
    547                 name = prmToIdent prm 
    548                 prm = tpParam param 
    549             -- deal with defaults 
    550             when (isOptional prm) $ do 
    551                 [defC] <- genLabel ["defaultDone"] 
    552                 tellIns $ "unless_null" .- [bare name, bare defC] 
    553                 case tpDefault param of 
    554                     Nothing     -> tellIns $ InsNew (VAR name) PerlScalar 
    555                     (Just exp)  -> do 
    556                         expC <- trans exp 
    557                         -- compile it away 
    558                         tellIns $ VAR name <:= expC 
    559                 tellLabel defC 
    560             tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 
    561542    trans x = transError x 
    562543 
     
    581562    tellIns $ "set_returns" .- sigList [tempPMC] 
    582563    tellIns $ "returncc" .- [] 
     564 
     565-- XXX - slurpiness 
     566prmToSig prm = MkSig (prmToArgs prm) . bare $ prmToIdent prm 
     567prmToArgs prm = combine  
     568    [ if isSlurpy prm then (MkArgSlurpyArray:) else id 
     569    , if isOptional prm then (MkArgOptional:) else id 
     570    ] [] 
     571prmToIdent = render . varText . paramName 
     572storeLex param = do 
     573    let var = paramName prm 
     574        name = prmToIdent prm 
     575        prm = tpParam param 
     576    -- deal with defaults 
     577    when (isOptional prm) $ do 
     578        [defC] <- genLabel ["defaultDone"] 
     579        tellIns $ "unless_null" .- [bare name, bare defC] 
     580        case tpDefault param of 
     581            Nothing     -> tellIns $ InsNew (VAR name) PerlScalar 
     582            (Just exp)  -> do 
     583                expC <- trans exp 
     584                -- compile it away 
     585                tellIns $ VAR name <:= expC 
     586        tellLabel defC 
     587    tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 
    583588 
    584589tellIns :: Ins -> Trans ()