Changeset 4907 for src/Pugs/Compile/PIR.hs
- Timestamp:
- 06/22/05 02:41:05 (4 years ago)
- svk:copy_cache_prev:
- 6641
- Files:
-
- 1 modified
-
src/Pugs/Compile/PIR.hs (modified) (35 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile/PIR.hs
r4905 r4907 1 {-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans -funbox-strict-fields -fallow-undecidable-instances -cpp #-} 1 {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances -fno-warn-orphans -funbox-strict-fields -cpp #-} 2 {-# OPTIONS_GHC -#include "UnicodeC.h" #-} 2 3 3 4 {-| … … 7 8 The general plan is to first compile the environment (subroutines, 8 9 statements, etc.) to an abstract syntax tree ('PIL' -- Pugs Intermediate 9 Representation) using the 'compile' function and 'Compile' class, and then10 Language) using the 'compile' function and 'Compile' class, and then 10 11 translate the PIL to a data structure of type 'PIR' using the 'trans' 11 12 function and 'Translate' class. This data structure is then reduced to … … 27 28 28 29 #ifndef HADDOCK 29 data PIL a where 30 -- Type-indexed with GADT; it is a bit too baroque -- refactor toward ANF? 31 data (Typeable a) => PIL a where 30 32 PNil :: PIL [a] 31 33 PNoop :: PIL Stmt … … 47 49 PAssign :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 48 50 PBind :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 49 PPad :: ! [(VarName, PIL Expression)] -> !(PIL [Stmt]) -> PIL [Stmt]51 PPad :: !Scope -> ![(VarName, PIL Expression)] -> !(PIL [Stmt]) -> PIL [Stmt] 50 52 51 53 PSub :: !SubName -> !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl … … 86 88 show (PAssign x y) = "(PAssign " ++ show x ++ " " ++ show y ++ ")" 87 89 show (PBind x y) = "(PBind " ++ show x ++ " " ++ show y ++ ")" 88 show (PPad x y) = "(PPad " ++ show x ++ " " ++ show y ++ ")"89 90 show (PThunk x) = "(PThunk " ++ show x ++ ")" 91 show (PRawName x) = "(PRawName " ++ show x ++ ")" 92 show (PPad x y z) = unwords ["(PPad", show x, show y, show z, ")"] 90 93 show (PCode x y z) = unwords ["(PCode", show x, show y, show z, ")"] 91 show (PRawName x) = "(PRawName " ++ show x ++ ")"92 94 show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"] 93 95 94 96 data TEnv = MkTEnv 95 { tLexDepth :: !Int 96 , tTokDepth :: !Int 97 , tEnv :: !Env 98 , tCxt :: !TCxt 99 , tReg :: !(TVar (Int, String)) 100 , tLabel :: !(TVar Int) 97 { tLexDepth :: !Int -- ^ Lexical scope depth 98 , tTokDepth :: !Int -- ^ Exp nesting depth 99 , tCxt :: !TCxt -- ^ Current context 100 , tReg :: !(TVar (Int, String))-- ^ Register name supply 101 , tLabel :: !(TVar Int) -- ^ Label name supply 101 102 } 102 103 deriving (Show, Eq) … … 149 150 ref <- liftSTM $ readTVar sym 150 151 cvList <- fromVals =<< readRef ref :: Comp [VCode] 151 decls <- forM ([0..] `zip` cvList) $ \(i :: Int, cv) -> do152 decls <- eachM cvList $ \(i, cv) -> do 152 153 compile (("&*END_" ++ show i), cv) :: Comp [PIL Decl] 153 154 compile ("&*END", concat decls) 155 canCompile ((_:twigil:_), _) | not (isAlphaNum twigil) = return [] 156 canCompile (name, [(_, sym)]) = do 157 -- translate them into store_global calls? 158 -- placing them each into one separate init function? 159 val <- readRef =<< liftSTM (readTVar sym) 160 valC <- compile val 161 let assignC = PAssign [PVar name'] valC 162 bodyC = PStmts (PStmt . PExp $ assignC) PNil 163 initL = "__init_" ++ (render $ varText name) 164 name' | ':' `elem` name = name 165 | otherwise = "main::" ++ name -- XXX wrong 166 return [PSub initL SubPrim [] bodyC] 154 167 canCompile _ = return [] 155 168 doCode name vsub = case subBody vsub of … … 157 170 _ -> compile (name, vsub) 158 171 159 instance Compile ([Char], [PIL Decl]) [PIL Decl] where 172 eachM :: (Monad m) => [a] -> ((Int, a) -> m b) -> m [b] 173 eachM = forM . ([0..] `zip`) 174 175 instance Compile (SubName, [PIL Decl]) [PIL Decl] where 160 176 compile (name, decls) = do 161 177 let bodyC = [ PStmts . PStmt . PExp $ PApp tcVoid (PExp (PVar sub)) [] … … 164 180 return (PSub name SubPrim [] (combine bodyC PNil):decls) 165 181 166 instance Compile ([Char], VCode) [PIL Decl] where 167 compile (name, MkCode{ subType = styp, subBody = Syn "block" [body], subParams = params }) = do 168 bodyC <- enter cxtItemAny $ compile body 169 paramsC <- compile params 170 return [PSub name styp paramsC bodyC] 171 compile (name, code) = compile 172 (name, code{ subBody = Syn "block" [subBody code] }) 173 174 {- 175 instance Compile [(TVar Bool, TVar VRef)] (PIL Expression) where 176 compile _ = return (PLit $ PVal undef) 177 -} 182 instance Compile (SubName, VCode) [PIL Decl] where 183 compile (name, vsub) | packageOf name /= packageOf (subName vsub) = do 184 -- This is an export! Huzzah Buzzah! 185 warn "export" (name, subName vsub) 186 let storeC = PBind [PVar $ qualify name] (PExp . PVar . qualify $ subName vsub) 187 bodyC = PStmts (PStmt . PExp $ storeC) PNil 188 exportL = "__export_" ++ (render $ varText name) 189 return [PSub exportL SubPrim [] bodyC] 190 compile (name, vsub) = do 191 bodyC <- enter cxtItemAny . compile $ case subBody vsub of 192 Syn "block" [body] -> body 193 body -> body 194 paramsC <- compile $ subParams vsub 195 return [PSub name (subType vsub) paramsC bodyC] 178 196 179 197 instance Compile (String, [(TVar Bool, TVar VRef)]) (PIL Expression) where … … 183 201 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 184 202 compile (Cxt cxt rest) = enter cxt $ compile rest 185 compile (Stmts (Pad SMy pad exp) rest) = do 203 compile (Stmts (Pad SOur _ exp) rest) = do 204 compile $ mergeStmts exp rest 205 compile (Stmts (Pad _ pad exp) rest) = do 186 206 expC <- compile $ mergeStmts exp rest 187 207 padC <- compile $ padToList pad 188 return $ PPad ((map fst (padToList pad)) `zip` padC) expC208 return $ PPad SMy ((map fst $ padToList pad) `zip` padC) expC 189 209 compile exp = compileStmts exp 190 210 … … 208 228 where 209 229 tailCall (PStmt (PExp (PApp cxt fun args))) 210 = PStmt (PExp (PApp (TTailCall cxt) fun args))230 = PStmt $ PExp $ PApp (TTailCall cxt) fun args 211 231 tailCall (PPos pos exp x) = PPos pos exp (tailCall x) 212 232 tailCall x = x … … 220 240 instance Compile Val (PIL Stmt) where 221 241 compile = fmap PStmt . compile . Val 242 243 instance Compile Val (PIL Expression) where 244 compile = compile . Val 222 245 223 246 instance Compile Exp (PIL Stmt) where … … 242 265 postC <- compile post 243 266 funC <- compile (Var "&statement_control:loop") 244 return $ PStmt $PExp $ PApp TCxtVoid funC267 return . PStmt . PExp $ PApp TCxtVoid funC 245 268 [preC, pBlock condC, pBlock bodyC, pBlock postC] 246 269 compile exp@(Syn "unless" _) = fmap (PStmt . PExp) $ compConditional exp … … 253 276 bodyC <- compile body 254 277 funC <- compile (Var "&statement_control:for") 255 return $ PStmt $PExp $ PApp TCxtVoid funC [expC, bodyC]278 return . PStmt . PExp $ PApp TCxtVoid funC [expC, bodyC] 256 279 compile (Syn "given" _) = compile (Var "$_") -- XXX 257 280 compile (Syn "when" _) = compile (Var "$_") -- XXX … … 309 332 return $ PApp cxt funC argsC 310 333 compile exp@(Syn "if" _) = compConditional exp 311 compile (Syn "{}" (x:xs)) = compile (App (Var "&postcircumfix:{}") (Just x) xs)334 compile (Syn "{}" (x:xs)) = compile $ App (Var "&postcircumfix:{}") (Just x) xs 312 335 compile (Syn "[]" (x:xs)) = do 313 336 compile (App (Var "&postcircumfix:[]") (Just x) xs) … … 345 368 compConditional (Syn name exps) = do 346 369 [condC, trueC, falseC] <- compile exps 347 funC <- compile (Var $"&statement_control:" ++ name)370 funC <- compile $ Var ("&statement_control:" ++ name) 348 371 cxt <- askTCxt 349 372 return $ PApp cxt funC [condC, PThunk trueC, PThunk falseC] … … 355 378 compile (Cxt cxt rest) = enter cxt $ compile rest 356 379 compile (Var name) = return . PExp $ PVar name 357 compile exp@(Val (VCode _)) = compile (Syn "sub" [exp])358 compile (Val val) = fmap PLit (compile val)380 compile exp@(Val (VCode _)) = compile $ Syn "sub" [exp] 381 compile (Val val) = fmap PLit $ compile val 359 382 compile Noop = compile (Val undef) 360 383 compile (Syn "block" [body]) = do … … 363 386 return $ PExp $ PApp cxt (pBlock bodyC) [] 364 387 compile (Syn "sub" [Val (VCode sub)]) = do 365 -- XXX I'd like to lambda lift... :-/366 388 bodyC <- enter sub $ compile $ case subBody sub of 367 389 Syn "block" [exp] -> exp … … 406 428 trans PNoop = return (StmtComment "") 407 429 trans (PPos pos exp rest) = do 408 -- tell [StmtLine (posName pos) (posBeginLine pos)] 409 dep <- asks tTokDepth 430 dep <- asks tTokDepth 410 431 tell [StmtComment $ (replicate dep ' ') ++ "{{{ " ++ pretty exp] 411 x<- local (\e -> e{ tTokDepth = dep + 1 }) $ trans rest432 expC <- local (\e -> e{ tTokDepth = dep + 1 }) $ trans rest 412 433 tell [StmtComment $ (replicate dep ' ') ++ "}}} " ++ pretty pos] 413 return x434 return expC 414 435 trans (PLit (PVal VUndef)) = do 415 436 pmc <- genLV "undef" 416 return (ExpLV pmc)437 return $ ExpLV pmc 417 438 trans (PLit lit) = do 418 439 -- generate fresh supply and things... … … 420 441 pmc <- genLV "lit" 421 442 tellIns $ pmc <== ExpLit litC 422 return (ExpLV pmc)443 return $ ExpLV pmc 423 444 trans (PVal (VBool bool)) = return $ LitInt (toInteger $ fromEnum bool) 424 445 trans (PVal (VStr str)) = return $ LitStr str … … 427 448 trans (PVal (VRat rat)) = return $ LitNum (ratToNum rat) 428 449 trans val@(PVal _) = transError val 450 trans (PVar name) | Just (pkg, name') <- isQualified name = do 451 -- XXX - this is terribly ugly. Fix at parrot side perhaps? 452 pmc <- genLV "glob" 453 let initL = "init_" ++ pmcStr 454 doneL = "done_" ++ pmcStr 455 pmcStr = render (emit pmc) 456 tellIns $ "push_eh" .- [bare initL] 457 tellIns $ pmc <-- "find_global" $ [lit pkg, lit name'] 458 tellIns $ "goto" .- [bare doneL] 459 tellLabel initL 460 tellIns $ "store_global" .- [lit pkg, lit name', reg pmc] 461 tellLabel doneL 462 tellIns $ "clear_eh" .- [] 463 return pmc 429 464 trans (PVar name) = do 430 pmc <- genLV " var"465 pmc <- genLV "lex" 431 466 tellIns $ pmc <-- "find_name" $ [lit $ possiblyFixOperatorName name] 432 467 return pmc 433 {- XXX - this interferes with the prototype checking :-(434 trans (PStmt (PExp (PApp TCxtVoid (PExp (PVar name)) args))) = do435 argsC <- mapM trans args436 return $ StmtIns $ InsFun [] (lit name) argsC437 -}438 468 trans (PStmt (PLit (PVal VUndef))) = return $ StmtComment "" 439 469 trans (PStmt exp) = do … … 458 488 tellIns $ [reg tempPMC] <-& blockC $ [] 459 489 return tempPMC 460 trans (PApp (TCxtLValue _) (PExp (PVar "&postcircumfix:[]")) [ (PExp lhs), rhs]) = do490 trans (PApp (TCxtLValue _) (PExp (PVar "&postcircumfix:[]")) [PExp lhs, rhs]) = do 461 491 lhsC <- trans lhs 462 492 rhsC <- trans rhs 463 return (KEYED lhsC rhsC)493 return $ lhsC `KEYED` rhsC 464 494 trans (PApp cxt fun args) = do 465 funC <- case fun of495 funC <- trans fun {- case fun of 466 496 PExp (PVar name) -> return $ lit name 467 497 _ -> trans fun 498 -} 468 499 argsC <- if isLogicalLazy fun 469 500 then mapM trans (head args : map PThunk (tail args)) … … 485 516 isLogicalLazy (PExp (PVar "&infix:&&")) = True 486 517 isLogicalLazy _ = False 487 trans (PPad pad exps) = do518 trans (PPad SMy pad exps) = do 488 519 valsC <- mapM trans (map snd pad) 489 520 pass $ do … … 492 523 trans (PExp exp) = fmap ExpLV $ trans exp 493 524 trans (PCode styp params body) = do 494 [beg C, endC] <- genLabel ["blockBegin", "blockEnd"]525 [begL, endL] <- genLabel ["blockBegin", "blockEnd"] 495 526 this <- genPMC "block" 496 tellIns $ "newsub" .- [reg this, bare ".Closure", bare beg C]497 tellIns $ "goto" .- [bare end C]498 tellLabel beg C527 tellIns $ "newsub" .- [reg this, bare ".Closure", bare begL] 528 tellIns $ "goto" .- [bare endL] 529 tellLabel begL 499 530 let prms = map tpParam params 500 531 mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms … … 507 538 tellIns $ "set_returns" .- retSigList [bodyC] 508 539 tellIns $ "returncc" .- [] 509 tellLabel end C540 tellLabel endL 510 541 return (ExpLV this) 511 542 trans (PThunk exp) = do 512 [beg C, sndC, retC, endC] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"]543 [begL, sndL, retL, endL] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 513 544 this <- genPMC "block" 514 tellIns $ "newsub" .- [reg this, bare ".Continuation", bare beg C]515 tellIns $ "goto" .- [bare end C]516 tellLabel beg C545 tellIns $ "newsub" .- [reg this, bare ".Continuation", bare begL] 546 tellIns $ "goto" .- [bare endL] 547 tellLabel begL 517 548 cc <- genPMC "cc" 518 549 fetchCC cc (reg this) 519 550 expC <- trans exp 520 tellIns $ "set_addr" .- [reg this, bare snd C]521 tellIns $ "goto" .- [bare ret C]522 tellLabel snd C551 tellIns $ "set_addr" .- [reg this, bare sndL] 552 tellIns $ "goto" .- [bare retL] 553 tellLabel sndL 523 554 fetchCC cc (reg this) 524 tellLabel ret C555 tellLabel retL 525 556 tellIns $ if parrotBrokenXXX 526 then "store_global" .- [tempSTR, expC] -- XXX HACK557 then "store_global" .- [tempSTR, expC] 527 558 else "set_args" .- [lit "(0b10)", expC] 528 559 tellIns $ "invoke" .- [reg cc] 529 tellLabel end C560 tellLabel endL 530 561 return (ExpLV this) 531 trans (PRawName name) = do532 -- generate fresh supply and things...533 pmc <- genName name534 return (ExpLV pmc)562 trans (PRawName name) = fmap ExpLV $ genName name 563 trans (PSub name styp params body) | Just (pkg, name') <- isQualified name = do 564 declC <- trans $ PSub name' styp params body 565 return $ DeclNS pkg [declC] 535 566 trans (PSub name styp params body) = do 536 567 (_, stmts) <- listen $ do … … 548 579 trans x = transError x 549 580 581 packageOf :: String -> String 582 packageOf name = case isQualified name of 583 Just (pkg, _) -> pkg 584 _ -> "main" 585 586 qualify :: String -> String 587 qualify name = case isQualified name of 588 Just _ -> name 589 _ -> let (sigil, name') = span (not . isAlphaNum) name 590 in sigil ++ "main::" ++ name' 591 592 isQualified :: String -> Maybe (String, String) 593 isQualified name | Just (post, pre) <- breakOnGlue "::" (reverse name) = 594 let (sigil, pkg) = span (not . isAlphaNum) preName 595 name' = possiblyFixOperatorName (sigil ++ postName) 596 preName = reverse pre 597 postName = reverse post 598 in Just (pkg, name') 599 isQualified _ = Nothing 600 550 601 fetchCC :: LValue -> Expression -> Trans () 551 fetchCC cc beg C| parrotBrokenXXX = do552 tellIns $ tempINT <-- "get_addr" $ [beg C]602 fetchCC cc begL | parrotBrokenXXX = do 603 tellIns $ tempINT <-- "get_addr" $ [begL] 553 604 tellIns $ tempSTR <:= tempINT 554 605 tellIns $ "find_global" .- [reg cc, tempSTR] … … 561 612 wrapSub SubBlock = id -- XXX not really 562 613 wrapSub _ = \body -> do 563 [retL ] <- genLabel ["returnHandler"]614 [retL, errL] <- genLabel ["returnHandler", "errHandler"] 564 615 tellIns $ "push_eh" .- [bare retL] 565 616 body 566 617 tellLabel retL 567 tellIns $ tempPMC <:= ExpLV (KEYED (VAR "P5") (lit False)) 618 tellIns $ tempPMC <:= ExpLV (errPMC `KEYED` lit False) 619 tellIns $ "clear_eh" .- [] 620 tellIns $ tempSTR <-- "typeof" $ [errPMC] 621 tellIns $ "eq" .- [tempSTR, lit "Exception", bare errL] 568 622 tellIns $ "set_returns" .- sigList [tempPMC] 569 623 tellIns $ "returncc" .- [] 624 tellLabel errL 625 tellIns $ "throw" .- [errPMC] 570 626 571 627 prmToSig :: Param -> Sig … … 574 630 prmToArgs :: Param -> [ArgFlag] 575 631 prmToArgs prm = combine 576 [ i f isSlurpy prm then (MkArgSlurpyArray:) else id577 , i f isOptional prm then (MkArgOptional:) else id632 [ isSlurpy ==> MkArgSlurpyArray 633 , isOptional ==> MkArgOptional 578 634 ] [] 635 where 636 f ==> arg = if f prm then (arg:) else id 579 637 580 638 prmToIdent :: Param -> String … … 583 641 storeLex :: TParam -> Trans () 584 642 storeLex param = do 585 let var = paramName prm586 name = prmToIdent prm587 prm = tpParam param588 -- deal with defaults589 643 when (isOptional prm) $ do 590 644 [defC] <- genLabel ["defaultDone"] … … 598 652 tellLabel defC 599 653 tellIns $ "store_lex" .- [lit curPad, lit var, bare name] 654 where 655 var = paramName prm 656 name = prmToIdent prm 657 prm = tpParam param 600 658 601 659 tellIns :: Ins -> Trans () … … 611 669 name' <- liftIO $ liftSTM $ do 612 670 (cur, name) <- readTVar tvar 613 return $ "P" ++ show cur++ (if null name then name else ('_':name))671 return $ ('P':show cur) ++ (if null name then name else ('_':name)) 614 672 return $ reg (VAR name') 615 673 … … 620 678 (cur, _) <- readTVar tvar 621 679 writeTVar tvar (cur + 1, name) 622 return $ "P" ++ show (cur + 1) ++ "_" ++ name680 return $ ('P':show (cur + 1)) ++ ('_':name) 623 681 tellIns $ InsLocal RegPMC name' 624 682 return $ reg (VAR name') … … 637 695 writeTVar tvar (cur + 1) 638 696 return cur 639 return $ map (\name -> "LABEL_" ++ show cnt ++ "_" ++ name) names697 return $ map (\name -> "LABEL_" ++ show cnt ++ ('_':name)) names 640 698 641 699 genName :: (RegClass a) => String -> Trans a … … 647 705 648 706 padSort :: (Var, [(TVar Bool, TVar VRef)]) -> (String, [(a, b)]) -> Ordering 649 padSort ( (a::[Char]), [(_, _)]) ((b::[Char]), [(_, _)])707 padSort (a, [(_, _)]) (b, [(_, _)]) 650 708 | (head a == ':' && head b == '&') = LT 651 709 | (head b == ':' && head a == '&') = GT … … 678 736 globPIL <- compile glob 679 737 mainPIL <- compile main 680 globPIR <- runTransGlob tenv globPIL :: Eval [Decl]681 mainPIR <- runTransMain tenv mainPIL :: Eval [Stmt]738 globPIR <- runTransGlob tenv globPIL 739 mainPIR <- runTransMain tenv mainPIL 682 740 return . VStr . unlines $ 683 741 [ "#!/usr/bin/env parrot" 684 -- , renderStyle (Style PageMode 0 0) init685 742 , renderStyle (Style PageMode 0 0) $ preludePIR $+$ vcat 686 -- Namespaces have bugs in both pugs and parrot. 687 -- [ emit $ namespace "main" 688 [ emit globPIR 689 , emit $ DeclSub "init" [SubMAIN, SubANON] $ map StmtIns 690 -- Eventually, we'll have to write our own find_name wrapper (or 691 -- fix Parrot's find_name appropriately). See Pugs.Eval.Var. 692 -- For now, we simply store $P0 twice. 693 [ "new_pad" .- [lit0] 694 , InsNew tempPMC PerlEnv 695 , "store_global" .- [lit "%*ENV", tempPMC] 696 , "store_global" .- [lit "%ENV", tempPMC] 697 , InsNew tempPMC PerlArray 698 , "store_global" .- [lit "@*END", tempPMC] 699 , "store_global" .- [lit "@END", tempPMC] 700 , "getstdin" .- [tempPMC] 701 , "store_global" .- [lit "$*IN", tempPMC] 702 , "store_global" .- [lit "$IN", tempPMC] 703 , "getstdout" .- [tempPMC] 704 , "store_global" .- [lit "$*OUT", tempPMC] 705 , "store_global" .- [lit "$OUT", tempPMC] 706 , "getstderr" .- [tempPMC] 707 , "store_global" .- [lit "$*ERR", tempPMC] 708 , "store_global" .- [lit "$ERR", tempPMC] 709 , "getinterp" .- [tempPMC] 710 , tempPMC <:= ExpLV (KEYED tempPMC (bare ".IGLOBALS_ARGV_LIST")) 711 , tempPMC2 <-- "shift" $ [tempPMC] 712 , "store_global" .- [lit "@*ARGS", tempPMC] 713 , "store_global" .- [lit "@ARGS", tempPMC] 714 , "store_global" .- [lit "$*PROGRAM_NAME", tempPMC2] 715 , "store_global" .- [lit "$PROGRAM_NAME", tempPMC2] 716 -- XXX wrong, should be lexical 717 , InsNew tempPMC PerlScalar 718 , "store_global" .- [lit "$_", tempPMC] 719 ] ++ [ StmtRaw (text "main()"), StmtIns (lit "&exit" .& [lit0]) ] 720 , text ".sub main @ANON" 721 , nest 4 (emit mainPIR) 722 , text ".end" 743 -- Namespaces have bugs in both pugs and parrot. 744 [ emit globPIR 745 , emit $ DeclNS "main" 746 [ DeclSub "init" [SubMAIN, SubANON] $ map StmtIns 747 -- Eventually, we'll have to write our own find_name wrapper (or 748 -- fix Parrot's find_name appropriately). See Pugs.Eval.Var. 749 -- For now, we simply store $P0 twice. 750 [ "new_pad" .- [lit0] 751 , InsNew tempPMC PerlEnv 752 , "store_global" .- [lit "%*ENV", tempPMC] 753 , "store_global" .- [lit "%ENV", tempPMC] 754 , InsNew tempPMC PerlArray 755 , "store_global" .- [lit "@*END", tempPMC] 756 , "store_global" .- [lit "@END", tempPMC] 757 , "getstdin" .- [tempPMC] 758 , "store_global" .- [lit "$*IN", tempPMC] 759 , "store_global" .- [lit "$IN", tempPMC] 760 , "getstdout" .- [tempPMC] 761 , "store_global" .- [lit "$*OUT", tempPMC] 762 , "store_global" .- [lit "$OUT", tempPMC] 763 , "getstderr" .- [tempPMC] 764 , "store_global" .- [lit "$*ERR", tempPMC] 765 , "store_global" .- [lit "$ERR", tempPMC] 766 , "getinterp" .- [tempPMC] 767 , tempPMC <:= ExpLV (tempPMC `KEYED` bare ".IGLOBALS_ARGV_LIST") 768 , tempPMC2 <-- "shift" $ [tempPMC] 769 , "store_global" .- [lit "@*ARGS", tempPMC] 770 , "store_global" .- [lit "@ARGS", tempPMC] 771 , "store_global" .- [lit "$*PROGRAM_NAME", tempPMC2] 772 , "store_global" .- [lit "$PROGRAM_NAME", tempPMC2] 773 -- XXX wrong, should be lexical 774 , InsNew tempPMC PerlScalar 775 , "store_global" .- [lit "$_", tempPMC] 776 ] ++ [ StmtRaw (text (name ++ "()")) | PSub name@('_':'_':_) _ _ _ <- globPIL ] ++ 777 [ StmtRaw (text "main()") 778 , StmtIns ("exit" .- [lit0]) 723 779 ] 724 ] 780 , DeclSub "main" [SubANON] [ StmtRaw $ nest 4 (emit mainPIR) ] 781 ] ] ] 725 782 where 726 style = MkEvalStyle{evalResult=EvalResultModule 727 ,evalError =EvalErrorFatal} 783 style = MkEvalStyle 784 { evalResult = EvalResultModule 785 , evalError = EvalErrorFatal 786 } 728 787 729 788 runTransGlob :: TEnv -> [PIL Decl] -> Eval [Decl] … … 735 794 initTEnv :: Eval TEnv 736 795 initTEnv = do 737 env <- ask 738 zero <- liftSTM $ newTVar (0, "") 739 none <- liftSTM $ newTVar 0 796 initReg <- liftSTM $ newTVar (0, "") 797 initLbl <- liftSTM $ newTVar 0 740 798 return $ MkTEnv 741 799 { tLexDepth = 0 742 800 , tTokDepth = 0 743 801 , tCxt = tcVoid 744 , tEnv = env 745 , tReg = zero 746 , tLabel = none 802 , tReg = initReg 803 , tLabel = initLbl 747 804 } 805
