Changeset 8681

Show
Ignore:
Timestamp:
01/15/06 16:49:47 (3 years ago)
Author:
audreyt
Message:

* -BPIR now returned to its full glory.
* Test.pm doesn't work yet because of named args.

Location:
src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • src/Emit/PIR.hs

    r7853 r8681  
    11{-# OPTIONS_GHC -fglasgow-exts -fallow-overlapping-instances -fno-warn-orphans -funbox-strict-fields -cpp #-} 
     2{- Generated by DrIFT (Automatic class derivations for Haskell) -} 
     3{-# LINE 1 "src/Emit/PIR.hs-drift" #-} 
    24 
    35{-| 
     
    1820import Data.Typeable 
    1921import Emit.Common 
     22import DrIFT.YAML 
    2023import Text.PrettyPrint 
     24 
     25{-! global : YAML !-} 
    2126 
    2227{-| PIR code consists of declarations. -} 
     
    2429 
    2530data Decl 
    26     = DeclSub       !SubName ![SubFlag] ![Stmt]  -- ^ Subroutine declaration 
    27     | DeclNS        !PkgName ![Decl]             -- ^ Namespace declaration 
    28     | DeclInc       !FilePath                    -- ^ @.include@ directive 
     31    = DeclSub                           -- ^ Subroutine declaration 
     32        { dsName    :: !SubName 
     33        , dsFlags   :: ![SubFlag] 
     34        , dsBody    :: ![Stmt] 
     35        }  
     36    | DeclNS                            -- ^ Namespace declaration 
     37        { dnPackage :: !PkgName 
     38        , dnBody    :: ![Decl] 
     39        } 
     40    | DeclInc                           -- ^ @.include@ directive 
     41        { diFile    :: !FilePath 
     42        } 
    2943    deriving (Show, Eq, Typeable) 
    3044 
     
    3549    | StmtRaw       !Doc                     -- ^ Backdoor into raw @Doc@ 
    3650    | StmtIns       !Ins                     -- ^ Generic instructions 
     51    | StmtSub       !SubName ![Stmt]         -- ^ Inner subroutine 
    3752    deriving (Show, Eq, Typeable) 
    3853 
     
    7388{-| Tags a PIR subroutine definition with @\@MAIN@, @\@LOAD@, @\@ANON@, 
    7489    @\@METHOD@, or @\@MULTI@. -} 
    75 data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI [ObjType] 
     90data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI ![ObjType] | SubOUTER !SubName 
    7691    deriving (Show, Eq, Typeable) 
    7792 
     
    107122        ] 
    108123    emit (DeclInc name) = emit ".include" <+> (quotes $ emit name) 
     124    -- Perform λ-lifting here 
    109125    emit (DeclSub name styps stmts) 
    110126        =  (emit ".sub" <+> doubleQuotes (emit $ quoted name) <+> commaSep styps) 
    111         $+$ nested (emitBody stmts) 
     127        $+$ nested (emitStmts stmts) 
    112128        $+$ emit ".end" 
    113         where 
    114         emitBody [] = [] 
    115         emitBody [(StmtIns (InsFun _ name args))] = [emit $ StmtIns (InsTailFun name args)] 
    116         emitBody (x:xs) = emit x : emitBody xs 
     129        $+$ emit [DeclSub name' [SubANON, SubOUTER name] body' | StmtSub name' body' <- stmts ] 
     130 
     131emitStmts :: [Stmt] -> Doc 
     132emitStmts stmts = vcat (emitLex:emitBody stmts) 
     133    where 
     134    emitBody [] = [] 
     135    emitBody [(StmtIns (InsFun _ name args))] = [emit $ StmtIns (InsTailFun name args)] 
     136    emitBody (x:xs) = emit x : emitBody xs 
     137    emitLex = vcat (map emitVar $ nub (concat [ pad | StmtPad pad _ <- stmts ])) 
     138    emitVar :: (VarName, Expression) -> Doc 
     139    emitVar (var, exp@(ExpLV (VAR name))) 
     140        = emit (InsLocal RegPMC name) 
     141        $+$ emit ".lex" <+> commaSep [emit (lit var), emit exp] 
     142    emitVar _ = empty 
    117143 
    118144{-| Emits PIR code for a 'SubFlag' (e.g. @\@MAIN@, @\@ANON@, etc.). -} 
    119145instance Emit SubFlag where 
    120     emit = emit . ('@':) . drop 3 . show 
     146    emit (SubOUTER x) = colon <> text "outer" <> parens (doubleQuotes $ emit x) 
     147    emit x = (emit . ('@':) . drop 3 . show) x 
    121148 
    122149curPad :: Int 
     
    129156    emit (StmtIns ins) = emit ins 
    130157    emit (StmtPad pad _) = vcat $ 
     158        map (\(var, exp) -> emit ("store_lex" .- [lit var, exp])) pad 
     159        {- 
    131160        [ emit "new_pad" <+> int curPad 
    132         ] ++ map (\(var, exp) -> emit ("store_lex" .- [lit curPad, lit var, exp])) pad 
     161        ] ++  
     162        -} 
    133163    emit (StmtRaw doc) = doc 
     164    emit StmtSub{} = empty 
    134165 
    135166instance Emit RegType where 
     
    144175    emit (InsBind ident lit) = eqSep ident "set" [lit] 
    145176    emit (InsPrim (Just ret) name args) = eqSep ret name args 
    146     emit (InsPrim Nothing "store_lex" (_:args)) = 
    147         -- XXX - horrible hack! perl 4! 
    148         emit (InsPrim Nothing "store_global" args) 
     177--  emit (InsPrim Nothing "store_lex" (_:args)) = 
     178--      -- XXX - horrible hack! perl 4! 
     179--      emit (InsPrim Nothing "store_global" args) 
    149180    emit (InsPrim Nothing name args) = emit name <+> commaSep args 
    150181    emit (InsFun rets (ExpLit (LitStr name)) args) = emitFunName "invokecc" name args rets 
     
    251282lit0 :: Expression 
    252283lit0 = lit (0 :: Int) 
    253  
    254 {-| @P5@ register -} 
    255 errPMC :: (RegClass a) => a 
    256 errPMC = reg (VAR "P5") 
    257284 
    258285{-| @$P0@ register -} 
     
    543570bare = ExpLV . VAR 
    544571 
    545 parrotBrokenXXX :: Bool 
    546 parrotBrokenXXX = True 
    547  
    548 collectCC :: LabelName -> [Ins] 
    549 collectCC label = 
    550     [ InsLabel label 
    551     , if parrotBrokenXXX 
    552         then "find_global" .- [tempPMC, tempSTR] 
    553         else "get_params" .- sigList [tempPMC] 
     572collectCC :: [Ins] 
     573collectCC = 
     574    [ "set_returns" .- retSigList [tempPMC] 
     575    , "returncc" .- [] 
    554576    ] 
    555577 
    556578callThunkCC :: Expression -> [Ins] 
    557 callThunkCC fun | parrotBrokenXXX = 
    558     [ tempINT   <-- "get_addr" $ [fun] 
    559     , tempSTR   <:= tempINT 
    560     , "store_global" .- [tempSTR, funPMC] 
    561     , "invokecc" .- [fun] 
    562     ] 
    563579callThunkCC fun = 
    564     [ "set_args" .- sigList [funPMC] 
     580    [ "set_args" .- sigList [] 
     581    , "get_results" .- sigList [tempPMC] 
    565582    , "invokecc" .- [fun] 
    566583    ] 
     
    597614    postL = ("sc_" ++ name ++ "_post") 
    598615    body = concat 
    599         [ newCont postL 
    600         , [ comp .- [arg0, bare altL] ] 
     616        [ [ comp .- [arg0, bare altL] ] 
    601617        , callThunkCC arg1 
     618        , [ "goto" .- [bare postL] ] 
    602619        , [ InsLabel altL ] 
    603         , callThunkCC arg2, 
    604         collectCC postL 
     620        , callThunkCC arg2 
     621        , [ InsLabel postL ] 
     622        , collectCC 
    605623        ] 
    606  
    607 newCont :: VarName -> [Ins] 
    608 newCont label = 
    609     [ InsNew funPMC Continuation 
    610     , "set_addr" .- [funPMC, bare label] 
    611     ] 
    612624 
    613625{-| Creates appropriate @&infix:foo@ subs for logical operators (@||@, @&&@, 
     
    619631    where 
    620632    altL = ("sc_" ++ escaped name ++ "_alt") 
    621     postL = ("sc_" ++ escaped name ++ "_post") 
    622     body = newCont postL ++ 
     633    body = 
    623634        [ comp .- [arg0, bare altL] 
    624635        , "set_returns" .- retSigList [arg0] 
    625636        , "returncc" .- [] 
    626637        , InsLabel altL 
    627         ] ++ callThunkCC arg1 ++ collectCC postL 
     638        ] ++ callThunkCC arg1 ++ collectCC 
    628639 
    629640{-| Escapes characters which have a special meaning in PIR. -} 
     
    639650preludePIR = emit $ 
    640651    [ include "iglobals.pasm" 
     652    , include "errors.pasm" 
    641653    -- Control flowy 
    642654    , sub "&return" [slurpy arg0] 
     
    955967    ] 
    956968 
     969instance YAML Doc where 
     970    asYAML = asYAML . render 
     971 
     972------------------------------------------------------------------------ 
     973{-* Generated by DrIFT : Look, but Don't Touch. *-} 
     974instance YAML Decl where 
     975    asYAML (DeclSub aa ab ac) = asYAMLmap "DeclSub" 
     976           [("dsName", asYAML aa) , ("dsFlags", asYAML ab) , 
     977            ("dsBody", asYAML ac)] 
     978    asYAML (DeclNS aa ab) = asYAMLmap "DeclNS" 
     979           [("dnPackage", asYAML aa) , ("dnBody", asYAML ab)] 
     980    asYAML (DeclInc aa) = asYAMLmap "DeclInc" [("diFile", asYAML aa)] 
     981 
     982instance YAML Stmt where 
     983    asYAML (StmtComment aa) = asYAMLseq "StmtComment" [asYAML aa] 
     984    asYAML (StmtLine aa ab) = asYAMLseq "StmtLine" 
     985           [asYAML aa , asYAML ab] 
     986    asYAML (StmtPad aa ab) = asYAMLseq "StmtPad" 
     987           [asYAML aa , asYAML ab] 
     988    asYAML (StmtRaw aa) = asYAMLseq "StmtRaw" [asYAML aa] 
     989    asYAML (StmtIns aa) = asYAMLseq "StmtIns" [asYAML aa] 
     990    asYAML (StmtSub aa ab) = asYAMLseq "StmtSub" 
     991           [asYAML aa , asYAML ab] 
     992 
     993instance YAML Ins where 
     994    asYAML (InsLocal aa ab) = asYAMLseq "InsLocal" 
     995           [asYAML aa , asYAML ab] 
     996    asYAML (InsNew aa ab) = asYAMLseq "InsNew" [asYAML aa , asYAML ab] 
     997    asYAML (InsBind aa ab) = asYAMLseq "InsBind" 
     998           [asYAML aa , asYAML ab] 
     999    asYAML (InsAssign aa ab) = asYAMLseq "InsAssign" 
     1000           [asYAML aa , asYAML ab] 
     1001    asYAML (InsPrim aa ab ac) = asYAMLseq "InsPrim" 
     1002           [asYAML aa , asYAML ab , asYAML ac] 
     1003    asYAML (InsFun aa ab ac) = asYAMLseq "InsFun" 
     1004           [asYAML aa , asYAML ab , asYAML ac] 
     1005    asYAML (InsTailFun aa ab) = asYAMLseq "InsTailFun" 
     1006           [asYAML aa , asYAML ab] 
     1007    asYAML (InsLabel aa) = asYAMLseq "InsLabel" [asYAML aa] 
     1008    asYAML (InsComment aa ab) = asYAMLseq "InsComment" 
     1009           [asYAML aa , asYAML ab] 
     1010    asYAML (InsExp aa) = asYAMLseq "InsExp" [asYAML aa] 
     1011    asYAML (InsConst aa ab ac) = asYAMLseq "InsConst" 
     1012           [asYAML aa , asYAML ab , asYAML ac] 
     1013 
     1014instance YAML Expression where 
     1015    asYAML (ExpLV aa) = asYAMLseq "ExpLV" [asYAML aa] 
     1016    asYAML (ExpLit aa) = asYAMLseq "ExpLit" [asYAML aa] 
     1017 
     1018instance YAML LValue where 
     1019    asYAML (VAR aa) = asYAMLseq "VAR" [asYAML aa] 
     1020    asYAML (PMC aa) = asYAMLseq "PMC" [asYAML aa] 
     1021    asYAML (STR aa) = asYAMLseq "STR" [asYAML aa] 
     1022    asYAML (INT aa) = asYAMLseq "INT" [asYAML aa] 
     1023    asYAML (NUM aa) = asYAMLseq "NUM" [asYAML aa] 
     1024    asYAML (KEYED aa ab) = asYAMLseq "KEYED" [asYAML aa , asYAML ab] 
     1025 
     1026instance YAML Literal where 
     1027    asYAML (LitStr aa) = asYAMLseq "LitStr" [asYAML aa] 
     1028    asYAML (LitInt aa) = asYAMLseq "LitInt" [asYAML aa] 
     1029    asYAML (LitNum aa) = asYAMLseq "LitNum" [asYAML aa] 
     1030 
     1031instance YAML SubFlag where 
     1032    asYAML (SubMAIN) = asYAMLcls "SubMAIN" 
     1033    asYAML (SubLOAD) = asYAMLcls "SubLOAD" 
     1034    asYAML (SubANON) = asYAMLcls "SubANON" 
     1035    asYAML (SubMETHOD) = asYAMLcls "SubMETHOD" 
     1036    asYAML (SubMULTI aa) = asYAMLseq "SubMULTI" [asYAML aa] 
     1037    asYAML (SubOUTER aa) = asYAMLseq "SubOUTER" [asYAML aa] 
     1038 
     1039instance YAML RegType where 
     1040    asYAML (RegInt) = asYAMLcls "RegInt" 
     1041    asYAML (RegNum) = asYAMLcls "RegNum" 
     1042    asYAML (RegStr) = asYAMLcls "RegStr" 
     1043    asYAML (RegPMC) = asYAMLcls "RegPMC" 
     1044 
     1045instance YAML ObjType where 
     1046    asYAML (PerlScalar) = asYAMLcls "PerlScalar" 
     1047    asYAML (PerlArray) = asYAMLcls "PerlArray" 
     1048    asYAML (PerlHash) = asYAMLcls "PerlHash" 
     1049    asYAML (PerlInt) = asYAMLcls "PerlInt" 
     1050    asYAML (PerlPair) = asYAMLcls "PerlPair" 
     1051    asYAML (PerlRef) = asYAMLcls "PerlRef" 
     1052    asYAML (PerlEnv) = asYAMLcls "PerlEnv" 
     1053    asYAML (Sub) = asYAMLcls "Sub" 
     1054    asYAML (Closure) = asYAMLcls "Closure" 
     1055    asYAML (Continuation) = asYAMLcls "Continuation" 
     1056    asYAML (BareType aa) = asYAMLseq "BareType" [asYAML aa] 
     1057 
     1058instance YAML Sig where 
     1059    asYAML (MkSig aa ab) = asYAMLmap "MkSig" 
     1060           [("sigFlags", asYAML aa) , ("sigIdent", asYAML ab)] 
     1061 
     1062instance YAML ArgFlag where 
     1063    asYAML (MkArgFlatten) = asYAMLcls "MkArgFlatten" 
     1064    asYAML (MkArgSlurpyArray) = asYAMLcls "MkArgSlurpyArray" 
     1065    asYAML (MkArgMaybeFlatten) = asYAMLcls "MkArgMaybeFlatten" 
     1066    asYAML (MkArgOptional) = asYAMLcls "MkArgOptional" 
     1067 
     1068--  Imported from other files :- 
  • src/Emit/PIR.hs-drift

    r8679 r8681  
    648648preludePIR = emit $ 
    649649    [ include "iglobals.pasm" 
     650    , include "errors.pasm" 
    650651    -- Control flowy 
    651652    , sub "&return" [slurpy arg0] 
  • src/Pugs/CodeGen.hs

    r8676 r8681  
    1515import Pugs.CodeGen.PIL1 (genPIL1) 
    1616import Pugs.CodeGen.PIL2 (genPIL2, genPIL2Perl5, genPIL2Binary, genPIL2JSON, genPIL2YAML) 
    17 import Pugs.CodeGen.PIR (genPIR) 
     17import Pugs.CodeGen.PIR (genPIR, genPIR_YAML) 
    1818import Pugs.CodeGen.Perl5 (genPerl5) 
    1919import Pugs.CodeGen.YAML (genYAML) 
     
    3131    [ ("GHC",         genGHC) 
    3232    , ("PIR",         genPIR) 
     33    , ("PIR-YAML",    genPIR_YAML) 
    3334    , ("PIL1",        genPIL1) 
    3435    , ("PIL1-Perl5",  genPerl5) 
     
    5455    norm' "parrot" = "!PIR" 
    5556    norm' "pir"    = "PIR" 
     57    norm' "piryaml"= "PIR-YAML" 
    5658    norm' "pil"    = "!PIL1" 
    5759    norm' "pil1"   = "PIL1" 
  • src/Pugs/CodeGen/PIR.hs

    r7853 r8681  
    1414-} 
    1515 
    16 module Pugs.CodeGen.PIR (genPIR) where 
     16module Pugs.CodeGen.PIR (genPIR, genPIR_YAML) where 
    1717import Pugs.Internals 
    1818import Pugs.AST 
     
    2727import Pugs.Compile 
    2828import Pugs.Run (getLibs) 
     29import DrIFT.YAML 
    2930 
    3031type CodeGen a = WriterT [Stmt] (ReaderT TEnv IO) a 
     
    7879    trans (PExp exp) = fmap ExpLV $ trans exp 
    7980    trans (PLit (PVal VUndef)) = do 
    80         pmc     <- genLV "undef" 
     81        pmc     <- genScalar "undef" 
    8182        return $ ExpLV pmc 
    8283    trans (PLit lit) = do 
    8384        -- generate fresh supply and things... 
    8485        litC    <- trans lit 
    85         pmc     <- genLV "lit" 
    86         tellIns $ pmc <== ExpLit litC 
     86        pmc     <- genScalar "lit" 
     87        tellIns $ pmc <== litC 
    8788        return $ ExpLV pmc 
    8889    trans (PThunk exp) = do 
    89         [begL, sndL, retL, endL] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 
    90         this    <- genPMC "block" 
    91         tellIns $ InsNew (reg this) Continuation 
    92         tellIns $ "set_addr" .- [reg this, bare begL] 
    93         tellIns $ "goto" .- [bare endL] 
    94         tellLabel begL 
    95         cc      <- genPMC "cc" 
    96         fetchCC cc (reg this) 
    97         expC    <- trans exp 
    98         tellIns $ "set_addr" .- [reg this, bare sndL] 
    99         tellIns $ "goto" .- [bare retL] 
    100         tellLabel sndL 
    101         fetchCC cc (reg this) 
    102         tellLabel retL 
    103         tellIns $ if parrotBrokenXXX 
    104             then "store_global" .- [tempSTR, expC] 
    105             else "set_args" .- [lit "(0b10)", expC] 
    106         tellIns $ "invokecc" .- [reg cc] 
    107         tellLabel endL 
     90        [begL, initL]  <- genLabel ["thunk", "thunkInit"] 
     91        this    <- genPMC "thunk" 
     92        let begP = begL ++ "_C" 
     93        tellIns $ InsConst (VAR begP) Sub (lit begL) 
     94        tellIns $ reg this <-- "newclosure" $ [bare begP] 
     95        -- inner subroutine begins 
     96        censor ((:[]) . StmtSub begL) $ do 
     97            -- tellIns $ "push_eh" .- [bare initL] 
     98            expC <- trans exp 
     99            tellIns $ "set_returns" .- retSigList [expC] 
     100            tellIns $ "returncc" .- [] 
    108101        return (ExpLV this) 
    109102    trans (PCode styp params _ _ body) = do 
    110         [begL, endL] <- genLabel ["blockBegin", "blockEnd"] 
     103        [begL]  <- genLabel ["block"] 
    111104        this    <- genPMC "block" 
    112105        let begP = begL ++ "_C" 
    113106        tellIns $ InsConst (VAR begP) Sub (lit begL) 
    114107        tellIns $ reg this <-- "newclosure" $ [bare begP] 
    115         tellIns $ "goto" .- [bare endL] 
    116         tellLabel begL 
    117         let prms = map tpParam params 
    118         mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
    119         tellIns $ "get_params" .- sigList (map prmToSig prms) 
    120         tellIns $ "new_pad" .- [lit curPad] 
    121         wrapSub styp $ do 
    122             mapM storeLex params 
    123             trans body  -- XXX - consistency check 
    124             bodyC   <- lastPMC 
    125             tellIns $ "set_returns" .- retSigList [bodyC] 
    126             tellIns $ "returncc" .- [] 
    127         tellLabel endL 
     108        -- inner subroutine begins 
     109        censor ((:[]) . StmtSub begL) $ do 
     110            let prms = map tpParam params 
     111            tell [StmtPad (map prmToPad prms) []] 
     112            tellIns $ "get_params" .- sigList (map prmToSig prms) 
     113            wrapSub styp $ do 
     114                mapM storeLex params 
     115                bodyC   <- case body of 
     116                    PNil -> return nullPMC 
     117                    _    -> trans body >> lastPMC 
     118                tellIns $ "set_returns" .- retSigList [bodyC] 
     119                tellIns $ "returncc" .- [] 
    128120        return (ExpLV this) 
     121 
     122prmToPad :: Param -> (VarName, Expression) 
     123prmToPad prm = (paramName prm, ExpLV (VAR $ prmToIdent prm)) 
    129124 
    130125instance Translate PIL_Decl Decl where 
     
    135130        (_, stmts)  <- listen $ do 
    136131            let prms = map tpParam params 
    137             mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
     132            tell [StmtPad (map prmToPad prms) []] 
    138133            tellIns $ "get_params" .- sigList (map prmToSig prms) 
    139             tellIns $ "new_pad" .- [lit curPad] 
     134            -- tellIns $ "new_pad" .- [lit curPad] 
    140135            wrapSub styp $ do 
    141136                mapM storeLex params 
    142                 trans body 
    143                 bodyC <- lastPMC 
     137                bodyC   <- case body of 
     138                    PNil -> return nullPMC 
     139                    _    -> trans body >> lastPMC 
    144140                tellIns $ "set_returns" .- retSigList [bodyC] 
    145141                tellIns $ "returncc" .- [] 
    146         return (DeclSub name [] stmts) 
    147  
    148 instance Translate PIL_Literal Literal where 
    149     trans (PVal (VBool bool)) = return $ LitInt (toInteger $ fromEnum bool) 
    150     trans (PVal (VStr str)) = return $ LitStr str 
    151     trans (PVal (VInt int)) = return $ LitInt int 
    152     trans (PVal (VNum num)) = return $ LitNum num 
    153     trans (PVal (VRat rat)) = return $ LitNum (ratToNum rat) 
    154     trans (PVal (VList [])) = return $ LitInt 0 -- XXX Wrong 
     142        return (DeclSub name [SubOUTER "main"] stmts) 
     143 
     144instance Translate PIL_Literal Expression where 
     145    trans (PVal (VBool bool)) = return $ ExpLit (LitInt (toInteger $ fromEnum bool)) 
     146    trans (PVal (VStr str)) = return $ ExpLit (LitStr str) 
     147    trans (PVal (VInt int)) = return $ ExpLit (LitInt int) 
     148    trans (PVal (VNum num)) = return $ ExpLit (LitNum num) 
     149    trans (PVal (VRat rat)) = return $ ExpLit (LitNum (ratToNum rat)) 
     150    -- trans (PVal (VList [])) = return $ LitInt 0 -- XXX Wrong 
     151    trans (PVal (VList vs)) = do 
     152        pmc <- genArray "vlist" 
     153        forM vs $ \val -> do 
     154            valC <- trans (PVal val) 
     155            tellIns $ "push" .- [pmc, valC] 
     156        return pmc 
    155157    trans val@(PVal _) = transError val 
    156158 
    157159instance Translate PIL_LValue LValue where 
    158160    trans (PVar name) | Just (pkg, name') <- isQualified name = do 
    159         -- XXX - this is terribly ugly.  Fix at parrot side perhaps? 
    160         pmc     <- genLV "glob" 
    161         let initL   = "init_" ++ pmcStr 
    162             doneL   = "done_" ++ pmcStr 
    163             pmcStr  = render (emit pmc) 
    164         tellIns $ "push_eh" .- [bare initL] 
    165         tellIns $ pmc <-- "find_global" $ [lit pkg, lit name'] 
    166         tellIns $ "goto" .- [bare doneL] 
    167         tellLabel initL 
     161        [globL] <- genLabel ["glob"] 
     162        pmc     <- genScalar "glob" 
     163        tell [StmtRaw (text "errorsoff .PARROT_ERRORS_GLOBALS_FLAG")] 
     164        tellIns $ pmc       <-- "find_global" $ [lit pkg, lit name'] 
     165        tellIns $ tempINT   <-- "defined" $ [reg pmc] 
     166        tellIns $ "if" .- [tempINT, bare globL] 
     167        tellIns $ InsNew pmc PerlScalar 
    168168        tellIns $ "store_global" .- [lit pkg, lit name', reg pmc] 
    169         tellLabel doneL 
    170         tellIns $ "clear_eh" .- [] 
     169        tellLabel globL 
     170        tell [StmtRaw (text "errorson .PARROT_ERRORS_GLOBALS_FLAG")] 
    171171        return pmc 
    172172    trans (PVar name) = do 
    173         pmc     <- genLV "lex" 
     173        pmc     <- genScalar "lex" 
    174174        tellIns $ pmc <-- "find_name" $ [lit $ possiblyFixOperatorName name] 
    175175        return pmc 
     
    179179        tellIns $ lhsC <== rhsC 
    180180        return lhsC 
    181     trans (PBind [PVar name] rhs) 
    182         | Just (pkg, name') <- isQualified (qualify name) = do 
     181    trans (PBind [PVar name] rhs) = do 
    183182        rhsC    <- trans rhs 
    184         tellIns $ "store_global" .- [lit pkg, lit name', rhsC] 
     183        tellIns $ "store_lex" .- [lit name, rhsC] 
    185184        trans (PVar name) 
    186185    trans (PBind [lhs] rhs) = do 
     
    189188        tellIns $ lhsC <:= rhsC 
    190189        return lhsC 
    191     trans (PApp _ exp@(PCode _ _ _ _ _) Nothing []) = do 
     190    trans (PApp _ exp@PCode{} Nothing []) = do 
    192191        blockC  <- trans exp 
    193192        tellIns $ [reg tempPMC] <-& blockC $ [] 
     
    200199        trans (PApp ctx fun Nothing (inv:args))  -- XXX wrong 
    201200    trans (PApp _ fun Nothing args) = do 
    202         funC    <- trans fun {- case fun of 
     201        funC <- trans fun {- case fun of 
    203202            PExp (PVar name) -> return $ lit name 
    204203            _           -> trans fun 
     
    206205        argsC   <- mapM trans args 
    207206        -- XXX WORKAROUND PARROT BUG (see below) 
    208         pmc     <- genLV "app" 
     207        pmc     <- genScalar "app" 
    209208        -- XXX - probe if funC is slurpy, then modify ExpLV pmc accordingly 
    210209        tellIns $ [reg pmc] <-& funC $ argsC 
     
    216215                return nullPMC 
    217216            _ -> do 
    218                 pmc     <- genLV "app" 
     217                pmc     <- genScalar "app" 
    219218                -- XXX - probe if funC is slurpy, then modify ExpLV pmc accordingly 
    220219                tellIns $ [reg pmc] <-& funC $ argsC 
     
    222221        -} 
    223222    trans x = transError x 
    224  
    225 fetchCC :: LValue -> Expression -> CodeGen () 
    226 fetchCC cc begL | parrotBrokenXXX = do 
    227     tellIns $ tempINT   <-- "get_addr" $ [begL] 
    228     tellIns $ tempSTR   <:= tempINT 
    229     tellIns $ "find_global" .- [reg cc, tempSTR] 
    230 fetchCC cc _ = do 
    231     tellIns $ "get_params" .- sigList [reg cc] 
    232223 
    233224-- XXX - slow way of implementing "return" 
     
    240231    body 
    241232    tellLabel retL 
    242     tellIns $ tempPMC <:= ExpLV (errPMC `KEYED` lit False) 
     233    tellIns $ ("get_results" .- sigList [tempPMC, tempSTR]) 
    243234    tellIns $ "clear_eh" .- [] 
    244     tellIns $ tempSTR <-- "typeof" $ [errPMC] 
     235    tellIns $ tempSTR <-- "typeof" $ [tempPMC] 
    245236    tellIns $ "eq" .- [tempSTR, lit "Exception", bare errL] 
    246237    tellIns $ "set_returns" .- sigList [tempPMC] 
    247238    tellIns $ "returncc" .- [] 
    248239    tellLabel errL 
    249     tellIns $ "throw" .- [errPMC] 
     240    tellIns $ "throw" .- [tempPMC] 
    250241 
    251242prmToSig :: Param -> Sig 
     
    275266                tellIns $ VAR name <:= expC 
    276267        tellLabel defC 
    277     tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 
     268    tellIns $ "store_lex" .- [lit var, bare name] 
    278269    where 
    279270    var     = paramName prm 
     
    291282lastPMC = do 
    292283    tvar    <- asks tReg 
    293     name'   <- liftIO $ liftSTM $ do 
     284    liftIO $ liftSTM $ do 
    294285        (cur, name) <- readTVar tvar 
    295         return $ ('P':show cur) ++ (if null name then name else ('_':name)) 
    296     return $ reg (VAR name') 
     286        return $ case cur of 
     287            0 -> nullPMC 
     288            _ -> reg (VAR (('p':show cur) ++ (if null name then name else ('_':name)))) 
    297289 
    298290genPMC :: (RegClass a) => String -> CodeGen a 
     
    302294        (cur, _) <- readTVar tvar 
    303295        writeTVar tvar (cur + 1, name) 
    304         return $ ('P':show (cur + 1)) ++ ('_':name) 
     296        return $ ('p':show (cur + 1)) ++ ('_':name) 
    305297    tellIns $ InsLocal RegPMC name' 
    306298    return $ reg (VAR name') 
    307299 
    308 genLV :: (RegClass a) => String -> CodeGen a 
    309 genLV name = do 
     300genWith :: (RegClass a) => (LValue -> Ins) -> String -> CodeGen a 
     301genWith f name = do 
    310302    pmc <- genPMC name 
    311     tellIns $ InsNew pmc PerlScalar 
     303    tellIns $ f pmc 
    312304    return $ reg pmc 
     305 
     306genScalar :: (RegClass a) => String -> CodeGen a 
     307genScalar = genWith (`InsNew` PerlScalar) 
     308 
     309genArray :: (RegClass a) => String -> CodeGen a 
     310genArray = genWith (`InsNew` PerlArray) 
     311 
     312genHash :: (RegClass a) => String -> CodeGen a 
     313genHash = genWith (`InsNew` PerlHash) 
    313314 
    314315genLabel :: [String] -> CodeGen [LabelName] 
     
    335336varInit x       = internalError $ "Invalid name: " ++ x 
    336337 
     338genPIR_YAML :: Eval Val 
     339genPIR_YAML = genPIRWith $ \globPIR mainPIR _ -> do 
     340    yaml <- liftIO (showYaml (mainPIR, globPIR)) 
     341    return (VStr yaml) 
     342 
    337343{-| Compiles the current environment to PIR code. -} 
    338344genPIR :: Eval Val 
    339 genPIR = do 
    340     tenv        <- initTEnv 
    341     -- Load the PIR Prelude. 
    342     local (\env -> env{ envDebug = Nothing }) $ do 
    343         opEval style "<prelude-pir>" preludeStr 
    344     penv        <- compile () 
    345     globPIR     <- runCodeGenGlob tenv (pilGlob penv) 
    346     mainPIR     <- runCodeGenMain tenv (pilMain penv) 
     345