Show
Ignore:
Timestamp:
08/13/05 21:16:15 (3 years ago)
Author:
autrijus
svk:copy_cache_prev:
8452
Message:

* de-GADT PIL structure -- likely to break all PIL2JS work

Files:
1 modified

Legend:

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

    r5891 r6229  
    99    statements, etc.) to an abstract syntax tree ('PIL' -- Pugs Intermediate 
    1010    Language) using the 'compile' function and 'Compile' class, and then 
    11     translate the PIL to a data structure of type 'PIR' using the 'trans' 
     11    translate the PIL_to a data structure of type 'PIR' using the 'trans' 
    1212    function and 'Translate' class. This data structure is then reduced to 
    1313    final PIR code by "Emit.PIR". 
     
    4545    ++ (show $ typeOf (undefined :: b)) 
    4646 
    47 instance (Typeable a) => Translate (PIL a) a where 
     47instance Translate PIL_Stmts [Stmt] where 
    4848    trans PNil = return [] 
     49    trans (PStmts this rest) = do 
     50        thisC   <- trans this 
     51        tell [thisC] 
     52        trans rest 
     53    trans (PPad SMy pad exps) = do 
     54        valsC   <- mapM trans (map snd pad) 
     55        pass $ do 
     56            expsC   <- trans exps 
     57            return ([], (StmtPad (map fst pad `zip` valsC) expsC:)) 
     58    trans (PPad _ pad exps) = do 
     59        -- XXX - maybe warn about bad pads? 
     60        trans (PPad SMy pad exps) 
     61 
     62instance Translate PIL_Stmt Stmt where 
    4963    trans PNoop = return (StmtComment "") 
     64    trans (PStmt (PLit (PVal VUndef))) = return $ StmtComment "" 
     65    trans (PStmt exp) = do 
     66        expC    <- trans exp 
     67        return $ StmtIns $ InsExp expC 
    5068    trans (PPos pos exp rest) = do 
    5169        dep     <- asks tTokDepth 
     
    5472        tell [StmtComment $ (replicate dep ' ') ++ "}}} " ++ pretty pos] 
    5573        return expC 
     74 
     75instance Translate PIL_Expr Expression where 
     76    trans (PRawName name) = fmap ExpLV $ genName name 
     77    trans (PExp exp) = fmap ExpLV $ trans exp 
    5678    trans (PLit (PVal VUndef)) = do 
    5779        pmc     <- genLV "undef" 
     
    6385        tellIns $ pmc <== ExpLit litC 
    6486        return $ ExpLV pmc 
     87    trans (PThunk exp) = do 
     88        [begL, sndL, retL, endL] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 
     89        this    <- genPMC "block" 
     90        tellIns $ "newsub" .- [reg this, bare ".Continuation", bare begL] 
     91        tellIns $ "goto" .- [bare endL] 
     92        tellLabel begL 
     93        cc      <- genPMC "cc" 
     94        fetchCC cc (reg this) 
     95        expC    <- trans exp 
     96        tellIns $ "set_addr" .- [reg this, bare sndL] 
     97        tellIns $ "goto" .- [bare retL] 
     98        tellLabel sndL 
     99        fetchCC cc (reg this) 
     100        tellLabel retL 
     101        tellIns $ if parrotBrokenXXX 
     102            then "store_global" .- [tempSTR, expC] 
     103            else "set_args" .- [lit "(0b10)", expC] 
     104        tellIns $ "invoke" .- [reg cc] 
     105        tellLabel endL 
     106        return (ExpLV this) 
     107    trans (PCode styp params body) = do 
     108        [begL, endL] <- genLabel ["blockBegin", "blockEnd"] 
     109        this    <- genPMC "block" 
     110        tellIns $ "newsub" .- [reg this, bare ".Closure", bare begL] 
     111        tellIns $ "goto" .- [bare endL] 
     112        tellLabel begL 
     113        let prms = map tpParam params 
     114        mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
     115        tellIns $ "get_params" .- sigList (map prmToSig prms) 
     116        tellIns $ "new_pad" .- [lit curPad] 
     117        wrapSub styp $ do 
     118            mapM storeLex params 
     119            trans body  -- XXX - consistency check 
     120            bodyC   <- lastPMC 
     121            tellIns $ "set_returns" .- retSigList [bodyC] 
     122            tellIns $ "returncc" .- [] 
     123        tellLabel endL 
     124        return (ExpLV this) 
     125 
     126instance Translate PIL_Decl Decl where 
     127    trans (PSub name styp params body) | Just (pkg, name') <- isQualified name = do 
     128        declC <- trans $ PSub name' styp params body 
     129        return $ DeclNS pkg [declC] 
     130    trans (PSub name styp params body) = do 
     131        (_, stmts)  <- listen $ do 
     132            let prms = map tpParam params 
     133            mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
     134            tellIns $ "get_params" .- sigList (map prmToSig prms) 
     135            tellIns $ "new_pad" .- [lit curPad] 
     136            wrapSub styp $ do 
     137                mapM storeLex params 
     138                trans body 
     139                bodyC <- lastPMC 
     140                tellIns $ "set_returns" .- retSigList [bodyC] 
     141                tellIns $ "returncc" .- [] 
     142        return (DeclSub name [] stmts) 
     143 
     144instance Translate PIL_Literal Literal where 
    65145    trans (PVal (VBool bool)) = return $ LitInt (toInteger $ fromEnum bool) 
    66146    trans (PVal (VStr str)) = return $ LitStr str 
     
    70150    trans (PVal (VList [])) = return $ LitInt 0 -- XXX Wrong 
    71151    trans val@(PVal _) = transError val 
     152 
     153instance Translate PIL_LValue LValue where 
    72154    trans (PVar name) | Just (pkg, name') <- isQualified name = do 
    73155        -- XXX - this is terribly ugly.  Fix at parrot side perhaps? 
     
    88170        tellIns $ pmc <-- "find_name" $ [lit $ possiblyFixOperatorName name] 
    89171        return pmc 
    90     trans (PStmt (PLit (PVal VUndef))) = return $ StmtComment "" 
    91     trans (PStmt exp) = do 
    92         expC    <- trans exp 
    93         return $ StmtIns $ InsExp expC 
    94172    trans (PAssign [lhs] rhs) = do 
    95173        lhsC    <- enter tcLValue $ trans lhs 
     
    107185        tellIns $ lhsC <:= rhsC 
    108186        return lhsC 
    109     trans (PStmts this rest) = do 
    110         thisC   <- trans this 
    111         tell [thisC] 
    112         trans rest 
    113187    trans (PApp _ exp@(PCode _ _ _) Nothing []) = do 
    114188        blockC  <- trans exp 
     
    143217                return pmc 
    144218        -} 
    145     trans (PPad SMy pad exps) = do 
    146         valsC   <- mapM trans (map snd pad) 
    147         pass $ do 
    148             expsC   <- trans exps 
    149             return ([], (StmtPad (map fst pad `zip` valsC) expsC:)) 
    150     trans (PExp exp) = fmap ExpLV $ trans exp 
    151     trans (PCode styp params body) = do 
    152         [begL, endL] <- genLabel ["blockBegin", "blockEnd"] 
    153         this    <- genPMC "block" 
    154         tellIns $ "newsub" .- [reg this, bare ".Closure", bare begL] 
    155         tellIns $ "goto" .- [bare endL] 
    156         tellLabel begL 
    157         let prms = map tpParam params 
    158         mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
    159         tellIns $ "get_params" .- sigList (map prmToSig prms) 
    160         tellIns $ "new_pad" .- [lit curPad] 
    161         wrapSub styp $ do 
    162             mapM storeLex params 
    163             trans body  -- XXX - consistency check 
    164             bodyC   <- lastPMC 
    165             tellIns $ "set_returns" .- retSigList [bodyC] 
    166             tellIns $ "returncc" .- [] 
    167         tellLabel endL 
    168         return (ExpLV this) 
    169     trans (PThunk exp) = do 
    170         [begL, sndL, retL, endL] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 
    171         this    <- genPMC "block" 
    172         tellIns $ "newsub" .- [reg this, bare ".Continuation", bare begL] 
    173         tellIns $ "goto" .- [bare endL] 
    174         tellLabel begL 
    175         cc      <- genPMC "cc" 
    176         fetchCC cc (reg this) 
    177         expC    <- trans exp 
    178         tellIns $ "set_addr" .- [reg this, bare sndL] 
    179         tellIns $ "goto" .- [bare retL] 
    180         tellLabel sndL 
    181         fetchCC cc (reg this) 
    182         tellLabel retL 
    183         tellIns $ if parrotBrokenXXX 
    184             then "store_global" .- [tempSTR, expC] 
    185             else "set_args" .- [lit "(0b10)", expC] 
    186         tellIns $ "invoke" .- [reg cc] 
    187         tellLabel endL 
    188         return (ExpLV this) 
    189     trans (PRawName name) = fmap ExpLV $ genName name 
    190     trans (PSub name styp params body) | Just (pkg, name') <- isQualified name = do 
    191         declC <- trans $ PSub name' styp params body 
    192         return $ DeclNS pkg [declC] 
    193     trans (PSub name styp params body) = do 
    194         (_, stmts)  <- listen $ do 
    195             let prms = map tpParam params 
    196             mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
    197             tellIns $ "get_params" .- sigList (map prmToSig prms) 
    198             tellIns $ "new_pad" .- [lit curPad] 
    199             wrapSub styp $ do 
    200                 mapM storeLex params 
    201                 trans body 
    202                 bodyC <- lastPMC 
    203                 tellIns $ "set_returns" .- retSigList [bodyC] 
    204                 tellIns $ "returncc" .- [] 
    205         return (DeclSub name [] stmts) 
    206219    trans x = transError x 
    207220 
     
    372385            , InsNew tempPMC PerlScalar 
    373386            , "store_global"    .- [lit "$_", tempPMC] 
    374             ]) ++ [ StmtRaw (text (name ++ "()")) | PSub name@('_':'_':_) _ _ _ <- globPIL ] ++ 
     387            ]) ++ [ StmtRaw (text (name ++ "()")) | PSub name@('_':'_':_) _ _ _ <- globPIL] ++ 
    375388            [ StmtRaw (text "main()") 
    376389            , StmtIns $ tempPMC  <-- "find_global" $ [lit "Perl6::Internals", lit "&exit"] 
     
    386399        } 
    387400 
    388 runCodeGenGlob :: TEnv -> [PIL Decl] -> Eval [Decl] 
     401runCodeGenGlob :: TEnv -> [PIL_Decl] -> Eval [Decl] 
    389402runCodeGenGlob tenv = mapM $ fmap fst . runCodeGen tenv 
    390403 
    391 runCodeGenMain :: TEnv -> PIL [Stmt] -> Eval [Stmt] 
     404runCodeGenMain :: TEnv -> PIL_Stmts -> Eval [Stmt] 
    392405runCodeGenMain tenv = fmap snd . runCodeGen tenv 
    393406