Changeset 8681
- Timestamp:
- 01/15/06 16:49:47 (3 years ago)
- Location:
- src
- Files:
-
- 5 modified
-
Emit/PIR.hs (modified) (14 diffs)
-
Emit/PIR.hs-drift (modified) (1 diff)
-
Pugs/CodeGen.hs (modified) (3 diffs)
-
Pugs/CodeGen/PIR.hs (modified) (17 diffs)
-
Pugs/Embed/Parrot.hsc (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Emit/PIR.hs
r7853 r8681 1 1 {-# 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" #-} 2 4 3 5 {-| … … 18 20 import Data.Typeable 19 21 import Emit.Common 22 import DrIFT.YAML 20 23 import Text.PrettyPrint 24 25 {-! global : YAML !-} 21 26 22 27 {-| PIR code consists of declarations. -} … … 24 29 25 30 data 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 } 29 43 deriving (Show, Eq, Typeable) 30 44 … … 35 49 | StmtRaw !Doc -- ^ Backdoor into raw @Doc@ 36 50 | StmtIns !Ins -- ^ Generic instructions 51 | StmtSub !SubName ![Stmt] -- ^ Inner subroutine 37 52 deriving (Show, Eq, Typeable) 38 53 … … 73 88 {-| Tags a PIR subroutine definition with @\@MAIN@, @\@LOAD@, @\@ANON@, 74 89 @\@METHOD@, or @\@MULTI@. -} 75 data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI [ObjType]90 data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI ![ObjType] | SubOUTER !SubName 76 91 deriving (Show, Eq, Typeable) 77 92 … … 107 122 ] 108 123 emit (DeclInc name) = emit ".include" <+> (quotes $ emit name) 124 -- Perform λ-lifting here 109 125 emit (DeclSub name styps stmts) 110 126 = (emit ".sub" <+> doubleQuotes (emit $ quoted name) <+> commaSep styps) 111 $+$ nested (emit Bodystmts)127 $+$ nested (emitStmts stmts) 112 128 $+$ 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 131 emitStmts :: [Stmt] -> Doc 132 emitStmts 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 117 143 118 144 {-| Emits PIR code for a 'SubFlag' (e.g. @\@MAIN@, @\@ANON@, etc.). -} 119 145 instance 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 121 148 122 149 curPad :: Int … … 129 156 emit (StmtIns ins) = emit ins 130 157 emit (StmtPad pad _) = vcat $ 158 map (\(var, exp) -> emit ("store_lex" .- [lit var, exp])) pad 159 {- 131 160 [ emit "new_pad" <+> int curPad 132 ] ++ map (\(var, exp) -> emit ("store_lex" .- [lit curPad, lit var, exp])) pad 161 ] ++ 162 -} 133 163 emit (StmtRaw doc) = doc 164 emit StmtSub{} = empty 134 165 135 166 instance Emit RegType where … … 144 175 emit (InsBind ident lit) = eqSep ident "set" [lit] 145 176 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) 149 180 emit (InsPrim Nothing name args) = emit name <+> commaSep args 150 181 emit (InsFun rets (ExpLit (LitStr name)) args) = emitFunName "invokecc" name args rets … … 251 282 lit0 :: Expression 252 283 lit0 = lit (0 :: Int) 253 254 {-| @P5@ register -}255 errPMC :: (RegClass a) => a256 errPMC = reg (VAR "P5")257 284 258 285 {-| @$P0@ register -} … … 543 570 bare = ExpLV . VAR 544 571 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] 572 collectCC :: [Ins] 573 collectCC = 574 [ "set_returns" .- retSigList [tempPMC] 575 , "returncc" .- [] 554 576 ] 555 577 556 578 callThunkCC :: Expression -> [Ins] 557 callThunkCC fun | parrotBrokenXXX =558 [ tempINT <-- "get_addr" $ [fun]559 , tempSTR <:= tempINT560 , "store_global" .- [tempSTR, funPMC]561 , "invokecc" .- [fun]562 ]563 579 callThunkCC fun = 564 [ "set_args" .- sigList [funPMC] 580 [ "set_args" .- sigList [] 581 , "get_results" .- sigList [tempPMC] 565 582 , "invokecc" .- [fun] 566 583 ] … … 597 614 postL = ("sc_" ++ name ++ "_post") 598 615 body = concat 599 [ newCont postL 600 , [ comp .- [arg0, bare altL] ] 616 [ [ comp .- [arg0, bare altL] ] 601 617 , callThunkCC arg1 618 , [ "goto" .- [bare postL] ] 602 619 , [ InsLabel altL ] 603 , callThunkCC arg2, 604 collectCC postL 620 , callThunkCC arg2 621 , [ InsLabel postL ] 622 , collectCC 605 623 ] 606 607 newCont :: VarName -> [Ins]608 newCont label =609 [ InsNew funPMC Continuation610 , "set_addr" .- [funPMC, bare label]611 ]612 624 613 625 {-| Creates appropriate @&infix:foo@ subs for logical operators (@||@, @&&@, … … 619 631 where 620 632 altL = ("sc_" ++ escaped name ++ "_alt") 621 postL = ("sc_" ++ escaped name ++ "_post") 622 body = newCont postL ++ 633 body = 623 634 [ comp .- [arg0, bare altL] 624 635 , "set_returns" .- retSigList [arg0] 625 636 , "returncc" .- [] 626 637 , InsLabel altL 627 ] ++ callThunkCC arg1 ++ collectCC postL638 ] ++ callThunkCC arg1 ++ collectCC 628 639 629 640 {-| Escapes characters which have a special meaning in PIR. -} … … 639 650 preludePIR = emit $ 640 651 [ include "iglobals.pasm" 652 , include "errors.pasm" 641 653 -- Control flowy 642 654 , sub "&return" [slurpy arg0] … … 955 967 ] 956 968 969 instance YAML Doc where 970 asYAML = asYAML . render 971 972 ------------------------------------------------------------------------ 973 {-* Generated by DrIFT : Look, but Don't Touch. *-} 974 instance 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 982 instance 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 993 instance 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 1014 instance YAML Expression where 1015 asYAML (ExpLV aa) = asYAMLseq "ExpLV" [asYAML aa] 1016 asYAML (ExpLit aa) = asYAMLseq "ExpLit" [asYAML aa] 1017 1018 instance 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 1026 instance 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 1031 instance 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 1039 instance YAML RegType where 1040 asYAML (RegInt) = asYAMLcls "RegInt" 1041 asYAML (RegNum) = asYAMLcls "RegNum" 1042 asYAML (RegStr) = asYAMLcls "RegStr" 1043 asYAML (RegPMC) = asYAMLcls "RegPMC" 1044 1045 instance 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 1058 instance YAML Sig where 1059 asYAML (MkSig aa ab) = asYAMLmap "MkSig" 1060 [("sigFlags", asYAML aa) , ("sigIdent", asYAML ab)] 1061 1062 instance 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 648 648 preludePIR = emit $ 649 649 [ include "iglobals.pasm" 650 , include "errors.pasm" 650 651 -- Control flowy 651 652 , sub "&return" [slurpy arg0] -
src/Pugs/CodeGen.hs
r8676 r8681 15 15 import Pugs.CodeGen.PIL1 (genPIL1) 16 16 import Pugs.CodeGen.PIL2 (genPIL2, genPIL2Perl5, genPIL2Binary, genPIL2JSON, genPIL2YAML) 17 import Pugs.CodeGen.PIR (genPIR )17 import Pugs.CodeGen.PIR (genPIR, genPIR_YAML) 18 18 import Pugs.CodeGen.Perl5 (genPerl5) 19 19 import Pugs.CodeGen.YAML (genYAML) … … 31 31 [ ("GHC", genGHC) 32 32 , ("PIR", genPIR) 33 , ("PIR-YAML", genPIR_YAML) 33 34 , ("PIL1", genPIL1) 34 35 , ("PIL1-Perl5", genPerl5) … … 54 55 norm' "parrot" = "!PIR" 55 56 norm' "pir" = "PIR" 57 norm' "piryaml"= "PIR-YAML" 56 58 norm' "pil" = "!PIL1" 57 59 norm' "pil1" = "PIL1" -
src/Pugs/CodeGen/PIR.hs
r7853 r8681 14 14 -} 15 15 16 module Pugs.CodeGen.PIR (genPIR ) where16 module Pugs.CodeGen.PIR (genPIR, genPIR_YAML) where 17 17 import Pugs.Internals 18 18 import Pugs.AST … … 27 27 import Pugs.Compile 28 28 import Pugs.Run (getLibs) 29 import DrIFT.YAML 29 30 30 31 type CodeGen a = WriterT [Stmt] (ReaderT TEnv IO) a … … 78 79 trans (PExp exp) = fmap ExpLV $ trans exp 79 80 trans (PLit (PVal VUndef)) = do 80 pmc <- gen LV"undef"81 pmc <- genScalar "undef" 81 82 return $ ExpLV pmc 82 83 trans (PLit lit) = do 83 84 -- generate fresh supply and things... 84 85 litC <- trans lit 85 pmc <- gen LV"lit"86 tellIns $ pmc <== ExpLitlitC86 pmc <- genScalar "lit" 87 tellIns $ pmc <== litC 87 88 return $ ExpLV pmc 88 89 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" .- [] 108 101 return (ExpLV this) 109 102 trans (PCode styp params _ _ body) = do 110 [begL , endL] <- genLabel ["blockBegin", "blockEnd"]103 [begL] <- genLabel ["block"] 111 104 this <- genPMC "block" 112 105 let begP = begL ++ "_C" 113 106 tellIns $ InsConst (VAR begP) Sub (lit begL) 114 107 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" .- [] 128 120 return (ExpLV this) 121 122 prmToPad :: Param -> (VarName, Expression) 123 prmToPad prm = (paramName prm, ExpLV (VAR $ prmToIdent prm)) 129 124 130 125 instance Translate PIL_Decl Decl where … … 135 130 (_, stmts) <- listen $ do 136 131 let prms = map tpParam params 137 mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms132 tell [StmtPad (map prmToPad prms) []] 138 133 tellIns $ "get_params" .- sigList (map prmToSig prms) 139 tellIns $ "new_pad" .- [lit curPad]134 -- tellIns $ "new_pad" .- [lit curPad] 140 135 wrapSub styp $ do 141 136 mapM storeLex params 142 trans body 143 bodyC <- lastPMC 137 bodyC <- case body of 138 PNil -> return nullPMC 139 _ -> trans body >> lastPMC 144 140 tellIns $ "set_returns" .- retSigList [bodyC] 145 141 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 144 instance 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 155 157 trans val@(PVal _) = transError val 156 158 157 159 instance Translate PIL_LValue LValue where 158 160 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 168 168 tellIns $ "store_global" .- [lit pkg, lit name', reg pmc] 169 tellLabel doneL170 tell Ins $ "clear_eh" .- []169 tellLabel globL 170 tell [StmtRaw (text "errorson .PARROT_ERRORS_GLOBALS_FLAG")] 171 171 return pmc 172 172 trans (PVar name) = do 173 pmc <- gen LV"lex"173 pmc <- genScalar "lex" 174 174 tellIns $ pmc <-- "find_name" $ [lit $ possiblyFixOperatorName name] 175 175 return pmc … … 179 179 tellIns $ lhsC <== rhsC 180 180 return lhsC 181 trans (PBind [PVar name] rhs) 182 | Just (pkg, name') <- isQualified (qualify name) = do 181 trans (PBind [PVar name] rhs) = do 183 182 rhsC <- trans rhs 184 tellIns $ "store_ global" .- [lit pkg, lit name', rhsC]183 tellIns $ "store_lex" .- [lit name, rhsC] 185 184 trans (PVar name) 186 185 trans (PBind [lhs] rhs) = do … … 189 188 tellIns $ lhsC <:= rhsC 190 189 return lhsC 191 trans (PApp _ exp@ (PCode _ _ _ _ _)Nothing []) = do190 trans (PApp _ exp@PCode{} Nothing []) = do 192 191 blockC <- trans exp 193 192 tellIns $ [reg tempPMC] <-& blockC $ [] … … 200 199 trans (PApp ctx fun Nothing (inv:args)) -- XXX wrong 201 200 trans (PApp _ fun Nothing args) = do 202 funC <- trans fun {- case fun of201 funC <- trans fun {- case fun of 203 202 PExp (PVar name) -> return $ lit name 204 203 _ -> trans fun … … 206 205 argsC <- mapM trans args 207 206 -- XXX WORKAROUND PARROT BUG (see below) 208 pmc <- gen LV"app"207 pmc <- genScalar "app" 209 208 -- XXX - probe if funC is slurpy, then modify ExpLV pmc accordingly 210 209 tellIns $ [reg pmc] <-& funC $ argsC … … 216 215 return nullPMC 217 216 _ -> do 218 pmc <- gen LV"app"217 pmc <- genScalar "app" 219 218 -- XXX - probe if funC is slurpy, then modify ExpLV pmc accordingly 220 219 tellIns $ [reg pmc] <-& funC $ argsC … … 222 221 -} 223 222 trans x = transError x 224 225 fetchCC :: LValue -> Expression -> CodeGen ()226 fetchCC cc begL | parrotBrokenXXX = do227 tellIns $ tempINT <-- "get_addr" $ [begL]228 tellIns $ tempSTR <:= tempINT229 tellIns $ "find_global" .- [reg cc, tempSTR]230 fetchCC cc _ = do231 tellIns $ "get_params" .- sigList [reg cc]232 223 233 224 -- XXX - slow way of implementing "return" … … 240 231 body 241 232 tellLabel retL 242 tellIns $ tempPMC <:= ExpLV (errPMC `KEYED` lit False)233 tellIns $ ("get_results" .- sigList [tempPMC, tempSTR]) 243 234 tellIns $ "clear_eh" .- [] 244 tellIns $ tempSTR <-- "typeof" $ [ errPMC]235 tellIns $ tempSTR <-- "typeof" $ [tempPMC] 245 236 tellIns $ "eq" .- [tempSTR, lit "Exception", bare errL] 246 237 tellIns $ "set_returns" .- sigList [tempPMC] 247 238 tellIns $ "returncc" .- [] 248 239 tellLabel errL 249 tellIns $ "throw" .- [ errPMC]240 tellIns $ "throw" .- [tempPMC] 250 241 251 242 prmToSig :: Param -> Sig … … 275 266 tellIns $ VAR name <:= expC 276 267 tellLabel defC 277 tellIns $ "store_lex" .- [lit curPad, litvar, bare name]268 tellIns $ "store_lex" .- [lit var, bare name] 278 269 where 279 270 var = paramName prm … … 291 282 lastPMC = do 292 283 tvar <- asks tReg 293 name' <-liftIO $ liftSTM $ do284 liftIO $ liftSTM $ do 294 285 (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)))) 297 289 298 290 genPMC :: (RegClass a) => String -> CodeGen a … … 302 294 (cur, _) <- readTVar tvar 303 295 writeTVar tvar (cur + 1, name) 304 return $ (' P':show (cur + 1)) ++ ('_':name)296 return $ ('p':show (cur + 1)) ++ ('_':name) 305 297 tellIns $ InsLocal RegPMC name' 306 298 return $ reg (VAR name') 307 299 308 gen LV :: (RegClass a) => String -> CodeGen a309 gen LVname = do300 genWith :: (RegClass a) => (LValue -> Ins) -> String -> CodeGen a 301 genWith f name = do 310 302 pmc <- genPMC name 311 tellIns $ InsNew pmc PerlScalar303 tellIns $ f pmc 312 304 return $ reg pmc 305 306 genScalar :: (RegClass a) => String -> CodeGen a 307 genScalar = genWith (`InsNew` PerlScalar) 308 309 genArray :: (RegClass a) => String -> CodeGen a 310 genArray = genWith (`InsNew` PerlArray) 311 312 genHash :: (RegClass a) => String -> CodeGen a 313 genHash = genWith (`InsNew` PerlHash) 313 314 314 315 genLabel :: [String] -> CodeGen [LabelName] … … 335 336 varInit x = internalError $ "Invalid name: " ++ x 336 337 338 genPIR_YAML :: Eval Val 339 genPIR_YAML = genPIRWith $ \globPIR mainPIR _ -> do 340 yaml <- liftIO (showYaml (mainPIR, globPIR)) 341 return (VStr yaml) 342 337 343 {-| Compiles the current environment to PIR code. -} 338 344 genPIR :: 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
