Changeset 4907

Show
Ignore:
Timestamp:
06/22/05 02:41:05 (4 years ago)
Author:
autrijus
svk:copy_cache_prev:
6641
Message:

* better handling of modules and qualified names in PIR generation.

Location:
src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • src/Emit/PIR.hs

    r4891 r4907  
    1212 
    1313data Decl 
    14     = DeclSub   !SubName ![SubFlag] ![Stmt]  -- ^ Subroutine declaration 
    15     | DeclNS    !PkgName                     -- ^ Namespace declaration 
    16     | DeclInc   !FilePath                    -- ^ @.include@ directive 
     14    = DeclSub       !SubName ![SubFlag] ![Stmt]  -- ^ Subroutine declaration 
     15    | DeclNS        !PkgName ![Decl]             -- ^ Namespace declaration 
     16    | DeclInc       !FilePath                    -- ^ @.include@ directive 
    1717    deriving (Show, Eq, Typeable) 
    1818 
     
    2020    = StmtComment   !String                  -- ^ Comment 
    2121    | StmtLine      !FilePath !Int           -- ^ @#line@ directive 
    22     | StmtIns       !Ins 
    23     | StmtPad       ![(VarName, Expression)] ![Stmt] 
    24     | StmtRaw       !Doc                     -- ^ Backdoor into 
    25                                              --   "Pugs.Compile.Parrot". 
     22    | StmtPad       ![(VarName, Expression)] ![Stmt]    -- ^ Lexical Pad 
     23    | StmtRaw       !Doc                     -- ^ Backdoor into raw @Doc@ 
     24    | StmtIns       !Ins                     -- ^ Generic instructions 
    2625    deriving (Show, Eq, Typeable) 
    2726 
    2827data Ins 
    29     = InsLocal      !RegType !VarName               -- ^ Inserts a @.local@ directive 
    30     | InsNew        !LValue !ObjType                -- ^ Inserts a @new@ opcode 
    31     | InsBind       !LValue !Expression             -- ^ Inserts a @set@ opcode 
    32     | InsAssign     !LValue !Expression             -- ^ Inserts an @assign@ opcode 
    33     | InsExp        !Expression 
    34     | InsFun        ![Sig] !Expression ![Expression] -- ^ Inserts a function call 
    35     | InsTailFun    !Expression ![Expression]        -- ^ Inserts a tailcall 
    36     | InsPrim       !(Maybe LValue) !PrimName ![Expression] 
    37     | InsLabel      !LabelName                      -- ^ Inserts a label 
    38     | InsComment    !String !(Maybe Ins)            -- ^ Inserts a comment 
     28    = InsLocal      !RegType !VarName               -- ^ @.local@ directive 
     29    | InsNew        !LValue !ObjType                -- ^ @new@ opcode 
     30    | InsBind       !LValue !Expression             -- ^ @set@ opcode 
     31    | InsAssign     !LValue !Expression             -- ^ @assign@ opcode 
     32    | InsPrim       !(Maybe LValue) !PrimName ![Expression] -- ^ Other opcodes 
     33    | InsFun        ![Sig] !Expression ![Expression]-- ^ Function call 
     34    | InsTailFun    !Expression ![Expression]       -- ^ Tail call 
     35    | InsLabel      !LabelName                      -- ^ Label 
     36    | InsComment    !String !(Maybe Ins)            -- ^ Comment 
     37    | InsExp        !Expression                     -- ^ Generic expressions 
    3938    deriving (Show, Eq, Typeable) 
    4039 
    41 {-| Tags a PIR subroutine definition with @\@MAIN@, @\@LOAD@, @\@ANON@, 
    42     @\@METHOD@, or @\@MULTI@. -} 
    43 data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI [ObjType] 
     40data Expression 
     41    = ExpLV !LValue       -- ^ Variables 
     42    | ExpLit !Literal     -- ^ Literals 
    4443    deriving (Show, Eq, Typeable) 
    45  
    46 data RegType 
    47     = RegInt                            -- ^ @I@ (integer) register 
    48     | RegNum                            -- ^ @N@ (number) register 
    49     | RegStr                            -- ^ @S@ (string) register 
    50     | RegPMC                            -- ^ @P@ (PMC) register 
    51     deriving (Show, Eq, Typeable) 
    52  
    53 {-| Appears to be unused. -} 
    54 data RelOp = RelLT | RelLE | RelEQ | RelNE | RelGE | RelGT 
    55     deriving (Show, Eq, Typeable) 
    56  
    57 {-| A PMC type, which, for example, can be given as an argument to the @new@ 
    58     opcode (e.g. @new .PerlScalar@). -} 
    59 data ObjType 
    60     = PerlScalar | PerlArray | PerlHash 
    61     | PerlInt | PerlPair | PerlRef | PerlEnv 
    62     deriving (Show, Eq, Typeable, Read) 
    63  
    64 type LabelName  = String 
    65 type SubName    = String 
    66 type VarName    = String 
    67 type PrimName   = String 
    68 type PkgName    = String 
    69 type CallConv   = String 
    7044 
    7145data LValue 
     
    7852    deriving (Show, Eq, Typeable) 
    7953 
    80 data Expression 
    81     = ExpLV !LValue 
    82     | ExpLit !Literal 
    83     deriving (Show, Eq, Typeable) 
    84  
    8554data Literal 
    8655    = LitStr !String      -- ^ A literal string 
     
    8958    deriving (Show, Eq, Typeable) 
    9059 
     60{-| Tags a PIR subroutine definition with @\@MAIN@, @\@LOAD@, @\@ANON@, 
     61    @\@METHOD@, or @\@MULTI@. -} 
     62data SubFlag = SubMAIN | SubLOAD | SubANON | SubMETHOD | SubMULTI [ObjType] 
     63    deriving (Show, Eq, Typeable) 
     64 
     65data RegType 
     66    = RegInt                            -- ^ @I@ (Integer) register 
     67    | RegNum                            -- ^ @N@ (Number) register 
     68    | RegStr                            -- ^ @S@ (String) register 
     69    | RegPMC                            -- ^ @P@ (PMC) register 
     70    deriving (Show, Eq, Typeable) 
     71 
     72{-| A PMC type, which, for example, can be given as an argument to the @new@ 
     73    opcode (e.g. @new .PerlScalar@). -} 
     74data ObjType 
     75    = PerlScalar | PerlArray | PerlHash 
     76    | PerlInt | PerlPair | PerlRef | PerlEnv 
     77    deriving (Show, Eq, Typeable, Read) 
     78 
     79type LabelName  = String 
     80type SubName    = String 
     81type VarName    = String 
     82type PrimName   = String 
     83type PkgName    = String 
     84type CallConv   = String 
     85 
    9186{-| Emits PIR code for declarations (namespace, include, or sub declarations). -} 
    9287instance Emit Decl where 
    93     emit (DeclNS name) = emit ".namespace" <+> brackets (quotes $ emit name) 
     88    emit (DeclNS name decls) = vcat 
     89        [ emit ".namespace" <+> brackets (quotes $ emit name) 
     90        , emit decls 
     91        , emit ".namespace" <+> brackets (quotes $ emit "main") 
     92        ] 
    9493    emit (DeclInc name) = emit ".include" <+> (quotes $ emit name) 
    9594    emit (DeclSub name styps stmts) 
     
    207206#endif 
    208207 
    209 {-| @.namespace@ directive. -} 
    210 namespace :: PkgName -> Decl 
    211208{-| @.include@ directive. -} 
    212209include :: PkgName -> Decl 
     
    224221(.&) :: Expression -> [Expression] -> Ins 
    225222 
    226 namespace = DeclNS 
    227223include = DeclInc 
    228224 
     
    237233lit0 :: Expression 
    238234lit0 = lit (0 :: Int) 
     235 
     236{-| @P5@ register -} 
     237errPMC :: (RegClass a) => a 
     238errPMC = reg (VAR "P5") 
    239239 
    240240{-| @$P0@ register -} 
     
    604604    where 
    605605    esc :: Char -> String 
    606     esc '|' = "_or_" 
    607     esc '&' = "_and_" 
    608     esc '?' = "_q_" 
    609     esc '_' = "__" 
    610     esc x = [x] 
     606    esc c | isAlphaNum c = [c] 
     607    esc c = ('_':show (ord c)) 
    611608 
    612609{-| The Prelude, defining primitives like @&say@, @&infix:+@, etc. -} 
     
    617614    , sub "&return" [slurpy arg0] 
    618615        [ InsNew tempPMC PerlArray 
    619         , (KEYED tempPMC (lit False)) <:= arg0 
     616        , (tempPMC `KEYED` lit False) <:= arg0 
    620617        , "throw" .- [tempPMC] 
    621618        ] 
     
    653650 
    654651    -- IO 
    655     , sub "&Pugs::Internals::sleep" [arg0] 
    656         [ tempNUM <:= arg0 
    657         , "sleep" .- [tempNUM] 
    658         ] 
    659652    , sub "&print" [slurpy arg0] 
    660653        [ tempSTR <-- "join" $ [lit "", arg0] 
     
    666659        , "print" .- [lit "\n"] 
    667660        ] --> [lit True] 
    668     , sub "&Pugs::Internals::exit" [arg0] 
    669         [ lit "&*END" .& [] 
    670         , tempINT <:= arg0 
    671         , "exit" .- [tempINT] 
    672         ] 
    673661    , vop1is "&system" "spawnw" 
    674662 
     
    745733    , sub "&infix:=>" [arg0, arg1] 
    746734        [ InsNew rv PerlPair 
    747         , KEYED rv arg0 <:= arg1 
     735        , rv `KEYED` arg0 <:= arg1 
    748736        ] --> [rv] 
    749737    , sub "&infix:.." [arg0, arg1] 
     
    813801        ] --> [rv] 
    814802 
    815     --, namespace "Perl6::Internals" 
    816     , sub "&Pugs::Internals::symbolic_deref" [arg0, slurpy arg1] 
     803    , DeclNS "Perl6::Internals" 
     804    [ sub "&symbolic_deref" [arg0, slurpy arg1] 
    817805        -- find_name($arg0 ~ join "::", @arg1) 
    818806        [ tempSTR  <-- "join" $ [lit "::", arg1] 
     
    822810        , rv       <-- "find_name" $ [tempSTR] 
    823811        ] --> [rv] 
    824  
     812    , sub "&exit" [arg0] 
     813        [ lit "&*END" .& [] 
     814        , tempINT <:= arg0 
     815        , "exit" .- [tempINT] 
     816        ] 
     817    , sub "&sleep" [arg0] 
     818        [ tempNUM <:= arg0 
     819        , "sleep" .- [tempNUM] 
     820        ] 
     821    ] 
    825822    -- Supporting Math::Basic 
    826823    , sub "&abs" [arg0] 
  • src/Main.hs

    r4852 r4907  
    301301 
    302302compPIR :: String -> IO () 
    303 compPIR = (putStr =<<) . doCompile "PIR" "-" 
     303compPIR prog = do 
     304    pir <- doCompile "PIR" "-" prog 
     305    putStr $ (subMain ++ (last $ split subMain pir)) 
     306    where 
     307    subMain = ".sub main" 
    304308 
    305309runPIR :: String -> IO () 
  • 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" #-} 
    23 
    34{-| 
     
    78    The general plan is to first compile the environment (subroutines, 
    89    statements, etc.) to an abstract syntax tree ('PIL' -- Pugs Intermediate 
    9     Representation) using the 'compile' function and 'Compile' class, and then 
     10    Language) using the 'compile' function and 'Compile' class, and then 
    1011    translate the PIL to a data structure of type 'PIR' using the 'trans' 
    1112    function and 'Translate' class. This data structure is then reduced to 
     
    2728 
    2829#ifndef HADDOCK 
    29 data PIL a where 
     30-- Type-indexed with GADT; it is a bit too baroque -- refactor toward ANF? 
     31data (Typeable a) => PIL a where 
    3032    PNil        :: PIL [a] 
    3133    PNoop       :: PIL Stmt 
     
    4749    PAssign     :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 
    4850    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] 
    5052 
    5153    PSub        :: !SubName -> !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl 
     
    8688    show (PAssign x y) = "(PAssign " ++ show x ++ " " ++ show y ++ ")" 
    8789    show (PBind x y) = "(PBind " ++ show x ++ " " ++ show y ++ ")" 
    88     show (PPad x y) = "(PPad " ++ show x ++ " " ++ show y ++ ")" 
    8990    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, ")"] 
    9093    show (PCode x y z) = unwords ["(PCode", show x, show y, show z, ")"] 
    91     show (PRawName x) = "(PRawName " ++ show x ++ ")" 
    9294    show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"] 
    9395 
    9496data 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 
    101102    } 
    102103    deriving (Show, Eq) 
     
    149150            ref     <- liftSTM $ readTVar sym 
    150151            cvList  <- fromVals =<< readRef ref :: Comp [VCode] 
    151             decls   <- forM ([0..] `zip` cvList) $ \(i :: Int, cv) -> do 
     152            decls   <- eachM cvList $ \(i, cv) -> do 
    152153                compile (("&*END_" ++ show i), cv) :: Comp [PIL Decl] 
    153154            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] 
    154167        canCompile _ = return [] 
    155168        doCode name vsub = case subBody vsub of 
     
    157170            _       -> compile (name, vsub) 
    158171 
    159 instance Compile ([Char], [PIL Decl]) [PIL Decl] where 
     172eachM :: (Monad m) => [a] -> ((Int, a) -> m b) -> m [b] 
     173eachM = forM . ([0..] `zip`) 
     174 
     175instance Compile (SubName, [PIL Decl]) [PIL Decl] where 
    160176    compile (name, decls) = do 
    161177        let bodyC = [ PStmts . PStmt . PExp $ PApp tcVoid (PExp (PVar sub)) [] 
     
    164180        return (PSub name SubPrim [] (combine bodyC PNil):decls) 
    165181 
    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 -} 
     182instance 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] 
    178196 
    179197instance Compile (String, [(TVar Bool, TVar VRef)]) (PIL Expression) where 
     
    183201    compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 
    184202    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 
    186206        expC    <- compile $ mergeStmts exp rest 
    187207        padC    <- compile $ padToList pad 
    188         return $ PPad ((map fst (padToList pad)) `zip` padC) expC 
     208        return $ PPad SMy ((map fst $ padToList pad) `zip` padC) expC 
    189209    compile exp = compileStmts exp 
    190210 
     
    208228        where 
    209229        tailCall (PStmt (PExp (PApp cxt fun args))) 
    210             = PStmt (PExp (PApp (TTailCall cxt) fun args)) 
     230            = PStmt $ PExp $ PApp (TTailCall cxt) fun args 
    211231        tailCall (PPos pos exp x) = PPos pos exp (tailCall x) 
    212232        tailCall x = x 
     
    220240instance Compile Val (PIL Stmt) where 
    221241    compile = fmap PStmt . compile . Val 
     242 
     243instance Compile Val (PIL Expression) where 
     244    compile = compile . Val 
    222245 
    223246instance Compile Exp (PIL Stmt) where 
     
    242265        postC   <- compile post 
    243266        funC    <- compile (Var "&statement_control:loop") 
    244         return $ PStmt $ PExp $ PApp TCxtVoid funC 
     267        return . PStmt . PExp $ PApp TCxtVoid funC 
    245268            [preC, pBlock condC, pBlock bodyC, pBlock postC] 
    246269    compile exp@(Syn "unless" _) = fmap (PStmt . PExp) $ compConditional exp 
     
    253276        bodyC   <- compile body 
    254277        funC    <- compile (Var "&statement_control:for") 
    255         return $ PStmt $ PExp $ PApp TCxtVoid funC [expC, bodyC] 
     278        return . PStmt . PExp $ PApp TCxtVoid funC [expC, bodyC] 
    256279    compile (Syn "given" _) = compile (Var "$_") -- XXX 
    257280    compile (Syn "when" _) = compile (Var "$_") -- XXX 
     
    309332        return $ PApp cxt funC argsC 
    310333    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 
    312335    compile (Syn "[]" (x:xs)) = do 
    313336        compile (App (Var "&postcircumfix:[]") (Just x) xs) 
     
    345368compConditional (Syn name exps) = do 
    346369    [condC, trueC, falseC] <- compile exps 
    347     funC    <- compile (Var $ "&statement_control:" ++ name) 
     370    funC    <- compile $ Var ("&statement_control:" ++ name) 
    348371    cxt     <- askTCxt 
    349372    return $ PApp cxt funC [condC, PThunk trueC, PThunk falseC] 
     
    355378    compile (Cxt cxt rest) = enter cxt $ compile rest 
    356379    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 
    359382    compile Noop = compile (Val undef) 
    360383    compile (Syn "block" [body]) = do 
     
    363386        return $ PExp $ PApp cxt (pBlock bodyC) [] 
    364387    compile (Syn "sub" [Val (VCode sub)]) = do 
    365         -- XXX I'd like to lambda lift... :-/ 
    366388        bodyC   <- enter sub $ compile $ case subBody sub of 
    367389            Syn "block" [exp]   -> exp 
     
    406428    trans PNoop = return (StmtComment "") 
    407429    trans (PPos pos exp rest) = do 
    408         -- tell [StmtLine (posName pos) (posBeginLine pos)] 
    409         dep <- asks tTokDepth 
     430        dep     <- asks tTokDepth 
    410431        tell [StmtComment $ (replicate dep ' ') ++ "{{{ " ++ pretty exp] 
    411         x   <- local (\e -> e{ tTokDepth = dep + 1 }) $ trans rest 
     432        expC    <- local (\e -> e{ tTokDepth = dep + 1 }) $ trans rest 
    412433        tell [StmtComment $ (replicate dep ' ') ++ "}}} " ++ pretty pos] 
    413         return x 
     434        return expC 
    414435    trans (PLit (PVal VUndef)) = do 
    415436        pmc     <- genLV "undef" 
    416         return (ExpLV pmc) 
     437        return $ ExpLV pmc 
    417438    trans (PLit lit) = do 
    418439        -- generate fresh supply and things... 
     
    420441        pmc     <- genLV "lit" 
    421442        tellIns $ pmc <== ExpLit litC 
    422         return (ExpLV pmc) 
     443        return $ ExpLV pmc 
    423444    trans (PVal (VBool bool)) = return $ LitInt (toInteger $ fromEnum bool) 
    424445    trans (PVal (VStr str)) = return $ LitStr str 
     
    427448    trans (PVal (VRat rat)) = return $ LitNum (ratToNum rat) 
    428449    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 
    429464    trans (PVar name) = do 
    430         pmc     <- genLV "var" 
     465        pmc     <- genLV "lex" 
    431466        tellIns $ pmc <-- "find_name" $ [lit $ possiblyFixOperatorName name] 
    432467        return pmc 
    433 {- XXX - this interferes with the prototype checking :-( 
    434     trans (PStmt (PExp (PApp TCxtVoid (PExp (PVar name)) args))) = do 
    435         argsC   <- mapM trans args 
    436         return $ StmtIns $ InsFun [] (lit name) argsC 
    437 -} 
    438468    trans (PStmt (PLit (PVal VUndef))) = return $ StmtComment "" 
    439469    trans (PStmt exp) = do 
     
    458488        tellIns $ [reg tempPMC] <-& blockC $ [] 
    459489        return tempPMC 
    460     trans (PApp (TCxtLValue _) (PExp (PVar "&postcircumfix:[]")) [(PExp lhs), rhs]) = do 
     490    trans (PApp (TCxtLValue _) (PExp (PVar "&postcircumfix:[]")) [PExp lhs, rhs]) = do 
    461491        lhsC    <- trans lhs 
    462492        rhsC    <- trans rhs 
    463         return (KEYED lhsC rhsC)  
     493        return $ lhsC `KEYED` rhsC 
    464494    trans (PApp cxt fun args) = do 
    465         funC    <- case fun of 
     495        funC    <- trans fun {- case fun of 
    466496            PExp (PVar name) -> return $ lit name 
    467497            _           -> trans fun 
     498        -} 
    468499        argsC   <- if isLogicalLazy fun 
    469500            then mapM trans (head args : map PThunk (tail args)) 
     
    485516        isLogicalLazy (PExp (PVar "&infix:&&"))     = True 
    486517        isLogicalLazy _ = False 
    487     trans (PPad pad exps) = do 
     518    trans (PPad SMy pad exps) = do 
    488519        valsC   <- mapM trans (map snd pad) 
    489520        pass $ do 
     
    492523    trans (PExp exp) = fmap ExpLV $ trans exp 
    493524    trans (PCode styp params body) = do 
    494         [begC, endC] <- genLabel ["blockBegin", "blockEnd"] 
     525        [begL, endL] <- genLabel ["blockBegin", "blockEnd"] 
    495526        this    <- genPMC "block" 
    496         tellIns $ "newsub" .- [reg this, bare ".Closure", bare begC] 
    497         tellIns $ "goto" .- [bare endC] 
    498         tellLabel begC 
     527        tellIns $ "newsub" .- [reg this, bare ".Closure", bare begL] 
     528        tellIns $ "goto" .- [bare endL] 
     529        tellLabel begL 
    499530        let prms = map tpParam params 
    500531        mapM_ (tellIns . InsLocal RegPMC . prmToIdent) prms 
     
    507538            tellIns $ "set_returns" .- retSigList [bodyC] 
    508539            tellIns $ "returncc" .- [] 
    509         tellLabel endC 
     540        tellLabel endL 
    510541        return (ExpLV this) 
    511542    trans (PThunk exp) = do 
    512         [begC, sndC, retC, endC] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 
     543        [begL, sndL, retL, endL] <- genLabel ["thunkBegin", "thunkAgain", "thunkReturn", "thunkEnd"] 
    513544        this    <- genPMC "block" 
    514         tellIns $ "newsub" .- [reg this, bare ".Continuation", bare begC] 
    515         tellIns $ "goto" .- [bare endC] 
    516         tellLabel begC 
     545        tellIns $ "newsub" .- [reg this, bare ".Continuation", bare begL] 
     546        tellIns $ "goto" .- [bare endL] 
     547        tellLabel begL 
    517548        cc      <- genPMC "cc" 
    518549        fetchCC cc (reg this) 
    519550        expC    <- trans exp 
    520         tellIns $ "set_addr" .- [reg this, bare sndC] 
    521         tellIns $ "goto" .- [bare retC] 
    522         tellLabel sndC 
     551        tellIns $ "set_addr" .- [reg this, bare sndL] 
     552        tellIns $ "goto" .- [bare retL] 
     553        tellLabel sndL 
    523554        fetchCC cc (reg this) 
    524         tellLabel retC 
     555        tellLabel retL 
    525556        tellIns $ if parrotBrokenXXX 
    526             then "store_global" .- [tempSTR, expC] -- XXX HACK 
     557            then "store_global" .- [tempSTR, expC] 
    527558            else "set_args" .- [lit "(0b10)", expC] 
    528559        tellIns $ "invoke" .- [reg cc] 
    529         tellLabel endC 
     560        tellLabel endL 
    530561        return (ExpLV this) 
    531     trans (PRawName name) = do 
    532         -- generate fresh supply and things... 
    533         pmc     <- genName name 
    534         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] 
    535566    trans (PSub name styp params body) = do 
    536567        (_, stmts)  <- listen $ do 
     
    548579    trans x = transError x 
    549580 
     581packageOf :: String -> String 
     582packageOf name = case isQualified name of 
     583    Just (pkg, _)   -> pkg 
     584    _               -> "main" 
     585 
     586qualify :: String -> String 
     587qualify name = case isQualified name of 
     588    Just _  -> name 
     589    _       -> let (sigil, name') = span (not . isAlphaNum) name 
     590        in sigil ++ "main::" ++ name' 
     591 
     592isQualified :: String -> Maybe (String, String) 
     593isQualified 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') 
     599isQualified _ = Nothing 
     600 
    550601fetchCC :: LValue -> Expression -> Trans () 
    551 fetchCC cc begC | parrotBrokenXXX = do 
    552     tellIns $ tempINT   <-- "get_addr" $ [begC] 
     602fetchCC cc begL | parrotBrokenXXX = do 
     603    tellIns $ tempINT   <-- "get_addr" $ [begL] 
    553604    tellIns $ tempSTR   <:= tempINT 
    554605    tellIns $ "find_global" .- [reg cc, tempSTR]