From 97e62780d5ca3dddbddfd5c2a0e419a9b794cbe6 Mon Sep 17 00:00:00 2001 From: kamome <2038975825@qq.com> Date: Sun, 16 Jun 2024 18:24:17 +0800 Subject: [PATCH] feat: multi param --- app/Main.hs | 38 +++---- runtime/main.s | 241 +++++++++++++++++---------------------------- src/CPS/Ast.hs | 6 +- src/CPS/Trans.hs | 17 ++-- src/Closure/Ast.hs | 6 +- src/Closure/Clo.hs | 16 +-- src/Flat/Hoist.hs | 6 +- src/ML/Ast.hs | 12 +-- 8 files changed, 136 insertions(+), 206 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 4b1f2a7..00c3c1a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -14,33 +14,23 @@ main = do let ml = LetFix "filter" - "x" - ( Let - "num" - (Proj 0 (Var "x")) - ( Let - "f" - (Proj 1 (Var "x")) - ( If0 - (App (Var "f") (Var "num")) - ( App - (Var "filter") - ( Tuple - [ Prim Sub [Var "num", Num 1], - Var "f" - ] - ) - ) - (Var "num") - ) + ["num", "f"] + ( If0 + (App (Var "f") [Var "num"]) + ( App + (Var "filter") + [ Prim Sub [Var "num", Num 1], + Var "f" + ] ) + (Var "num") ) ( LetFix "f" - "num" + ["num"] ( LetFix "help" - "i" + ["i"] ( If0 (Var "i") (Num 0) @@ -52,12 +42,12 @@ main = do ] ) (Num 1) - (App (Var "help") (Prim Sub [Var "i", Num 1])) + (App (Var "help") [Prim Sub [Var "i", Num 1]]) ) ) - (App (Var "help") (Var "num")) + (App (Var "help") [Var "num"]) ) - (App (Var "filter") (Tuple [Num 99, Var "f"])) + (App (Var "filter") [Num 99, Var "f"]) ) print ml let code = runTrans (CPS.trans ml >>= Clo.cloConv >>= Flat.hoist >>= Spill.rename 3 >>= Machine.lowering) diff --git a/runtime/main.s b/runtime/main.s index b6e3ff1..1bda325 100644 --- a/runtime/main.s +++ b/runtime/main.s @@ -2,48 +2,47 @@ main: la s0 , _stack_bottom la sp , _stack_bottom - j main150 -main150: + j main134 +main134: mv s0 , sp - addi sp , sp , 16 + addi sp , sp , 24 mv t3 , sp addi sp , sp , 16 - la t0 , fCode46 + la t0 , fCode40 sd t0 , 0(t3) sd t2 , 8(t3) mv t4 , sp addi sp , sp , 16 - la t0 , fCode89 + la t0 , fCode73 sd t0 , 0(t4) sd t2 , 8(t4) - addi t2 , zero , 99 - mv t1 , sp - addi sp , sp , 16 - sd t2 , 0(t1) - sd t4 , 8(t1) + addi t1 , zero , 99 sd t1 , 0(s0) - mv t4 , sp + mv t1 , sp addi sp , sp , 16 - la t0 , kCode145 - sd t0 , 0(t4) - sd t2 , 8(t4) + la t0 , kCode129 + sd t0 , 0(t1) + sd t2 , 8(t1) + sd t1 , 8(s0) ld t2 , 0(t3) ld t1 , 8(t3) - sd t1 , 8(s0) - ld t0 , 8(s0) + sd t1 , 16(s0) + ld t0 , 16(s0) mv a0 , t0 - mv a1 , t4 + ld t0 , 8(s0) + mv a1 , t0 ld t0 , 0(s0) mv a2 , t0 + mv a3 , t4 jr t2 -kCode66: +kCode54: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -kCode73: +kCode61: mv s0 , sp addi sp , sp , 8 ld t2 , 0(a0) @@ -55,50 +54,45 @@ kCode73: mv a0 , t0 mv a1 , t3 jr t4 -kCode63: +kCode51: mv s0 , sp - addi sp , sp , 32 - ld t2 , 0(a0) - ld t1 , 8(a0) + addi sp , sp , 24 + ld t1 , 0(a0) sd t1 , 0(s0) - ld t1 , 16(a0) + ld t1 , 8(a0) sd t1 , 8(s0) - ld t3 , 24(a0) - addi t4 , zero , 1 - sub t1 , t3 , t4 + ld t4 , 16(a0) + ld t2 , 24(a0) + addi t3 , zero , 1 + sub t1 , t2 , t3 sd t1 , 16(s0) + mv t2 , sp + addi sp , sp , 8 + sd t4 , 0(t2) mv t3 , sp addi sp , sp , 16 - ld t0 , 16(s0) + la t0 , kCode54 sd t0 , 0(t3) sd t2 , 8(t3) - mv t2 , sp - addi sp , sp , 8 ld t0 , 8(s0) - sd t0 , 0(t2) - mv t4 , sp - addi sp , sp , 16 - la t0 , kCode66 - sd t0 , 0(t4) - sd t2 , 8(t4) - ld t0 , 0(s0) ld t2 , 0(t0) + ld t0 , 8(s0) + ld t4 , 8(t0) + mv a0 , t4 + mv a1 , t3 + ld t0 , 16(s0) + mv a2 , t0 ld t0 , 0(s0) - ld t1 , 8(t0) - sd t1 , 24(s0) - ld t0 , 24(s0) - mv a0 , t0 - mv a1 , t4 - mv a2 , t3 + mv a3 , t0 jr t2 -kCode58: +kCode46: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -kCode55: +kCode43: mv s0 , sp addi sp , sp , 40 ld t2 , 0(a0) @@ -112,7 +106,7 @@ kCode55: sd t4 , 0(t3) mv t4 , sp addi sp , sp , 16 - la t0 , kCode58 + la t0 , kCode46 sd t0 , 0(t4) sd t3 , 8(t4) mv t3 , sp @@ -125,7 +119,7 @@ kCode55: sd t0 , 24(t3) mv t2 , sp addi sp , sp , 16 - la t0 , kCode63 + la t0 , kCode51 sd t0 , 0(t2) sd t3 , 8(t2) mv t3 , sp @@ -135,7 +129,7 @@ kCode55: sd t0 , 8(t3) mv t4 , sp addi sp , sp , 16 - la t0 , kCode73 + la t0 , kCode61 sd t0 , 0(t4) sd t3 , 8(t4) mv t3 , zero @@ -146,79 +140,27 @@ kCode55: ld t2 , 0(t4) ld t1 , 8(t4) sd t1 , 32(s0) - beq a1 , zero , block151 - j block152 -block151: + beq a1 , zero , block135 + j block136 +block135: ld t0 , 24(s0) mv a0 , t0 mv a1 , t3 ld t0 , 16(s0) jr t0 -block152: +block136: ld t0 , 32(s0) mv a0 , t0 mv a1 , t3 jr t2 -kCode52: - mv s0 , sp - addi sp , sp , 8 - ld t2 , 0(a0) - ld t3 , 8(a0) - ld t1 , 16(a0) - sd t1 , 0(s0) - mv t4 , sp - addi sp , sp , 32 - sd a1 , 0(t4) - sd t2 , 8(t4) - sd t3 , 16(t4) - ld t0 , 0(s0) - sd t0 , 24(t4) - mv t2 , sp - addi sp , sp , 16 - la t0 , kCode55 - sd t0 , 0(t2) - sd t4 , 8(t2) - ld t3 , 0(a1) - ld t4 , 8(a1) - mv a0 , t4 - mv a1 , t2 - ld t0 , 0(s0) - mv a2 , t0 - jr t3 -kCode49: - mv s0 , sp - addi sp , sp , 16 - ld t2 , 0(a0) - ld t3 , 8(a0) - ld t1 , 16(a0) - sd t1 , 0(s0) - mv t4 , sp - addi sp , sp , 24 - sd t2 , 0(t4) - sd t3 , 8(t4) - sd a1 , 16(t4) - mv t2 , sp - addi sp , sp , 16 - la t0 , kCode52 - sd t0 , 0(t2) - sd t4 , 8(t2) - ld t0 , 0(s0) - ld t3 , 8(t0) - ld t4 , 0(t2) - ld t1 , 8(t2) - sd t1 , 8(s0) - ld t0 , 8(s0) - mv a0 , t0 - mv a1 , t3 - jr t4 -kCode121: +kCode105: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -kCode118: +kCode102: mv s0 , sp addi sp , sp , 16 ld t1 , 0(a0) @@ -233,7 +175,7 @@ kCode118: sd t4 , 0(t2) mv t3 , sp addi sp , sp , 16 - la t0 , kCode121 + la t0 , kCode105 sd t0 , 0(t3) sd t2 , 8(t3) ld t0 , 0(s0) @@ -245,7 +187,7 @@ kCode118: ld t0 , 8(s0) mv a2 , t0 jr t2 -kCode113: +kCode97: mv s0 , sp addi sp , sp , 8 ld t2 , 0(a0) @@ -257,14 +199,14 @@ kCode113: mv a0 , t0 mv a1 , t3 jr t4 -kCode108: +kCode92: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -kCode105: +kCode89: mv s0 , sp addi sp , sp , 48 ld t1 , 0(a0) @@ -283,7 +225,7 @@ kCode105: sd t4 , 0(t2) mv t3 , sp addi sp , sp , 16 - la t0 , kCode108 + la t0 , kCode92 sd t0 , 0(t3) sd t2 , 8(t3) mv t2 , sp @@ -291,7 +233,7 @@ kCode105: sd t3 , 0(t2) mv t4 , sp addi sp , sp , 16 - la t0 , kCode113 + la t0 , kCode97 sd t0 , 0(t4) sd t2 , 8(t4) mv t2 , sp @@ -303,7 +245,7 @@ kCode105: sd t3 , 16(t2) mv t3 , sp addi sp , sp , 16 - la t0 , kCode118 + la t0 , kCode102 sd t0 , 0(t3) sd t2 , 8(t3) mv t2 , zero @@ -315,20 +257,20 @@ kCode105: ld t1 , 8(t3) sd t1 , 40(s0) ld t0 , 16(s0) - beq t0 , zero , block153 - j block154 -block153: + beq t0 , zero , block137 + j block138 +block137: ld t0 , 32(s0) mv a0 , t0 mv a1 , t2 ld t0 , 24(s0) jr t0 -block154: +block138: ld t0 , 40(s0) mv a0 , t0 mv a1 , t2 jr t4 -kCode100: +kCode84: mv s0 , sp addi sp , sp , 8 ld t2 , 0(a0) @@ -340,26 +282,26 @@ kCode100: mv a0 , t0 mv a1 , t3 jr t4 -kCode95: +kCode79: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -kCode138: +kCode122: ld t2 , 0(a0) ld t3 , 0(t2) ld t4 , 8(t2) mv a0 , t4 mv a1 , a1 jr t3 -fCode92: +fCode76: mv s0 , sp addi sp , sp , 40 mv t2 , sp addi sp , sp , 16 - la t0 , fCode92 + la t0 , fCode76 sd t0 , 0(t2) sd a0 , 8(t2) ld t3 , 0(a0) @@ -368,7 +310,7 @@ fCode92: sd a1 , 0(t4) mv t1 , sp addi sp , sp , 16 - la t0 , kCode95 + la t0 , kCode79 sd t0 , 0(t1) sd t4 , 8(t1) sd t1 , 0(s0) @@ -378,7 +320,7 @@ fCode92: sd t0 , 0(t4) mv t1 , sp addi sp , sp , 16 - la t0 , kCode100 + la t0 , kCode84 sd t0 , 0(t1) sd t4 , 8(t1) sd t1 , 8(s0) @@ -391,7 +333,7 @@ fCode92: sd t3 , 24(t4) mv t2 , sp addi sp , sp , 16 - la t0 , kCode105 + la t0 , kCode89 sd t0 , 0(t2) sd t4 , 8(t2) mv t3 , zero @@ -404,28 +346,28 @@ fCode92: sd t1 , 24(s0) ld t1 , 8(t2) sd t1 , 32(s0) - beq a2 , zero , block155 - j block156 -block155: + beq a2 , zero , block139 + j block140 +block139: ld t0 , 16(s0) mv a0 , t0 mv a1 , t3 jr t4 -block156: +block140: ld t0 , 32(s0) mv a0 , t0 mv a1 , t3 ld t0 , 24(s0) jr t0 -kCode145: +kCode129: mv a0 , a1 j halt -fCode89: +fCode73: mv s0 , sp addi sp , sp , 8 mv t2 , sp addi sp , sp , 16 - la t0 , fCode89 + la t0 , fCode73 sd t0 , 0(t2) sd a0 , 8(t2) mv t2 , sp @@ -433,7 +375,7 @@ fCode89: sd a2 , 0(t2) mv t3 , sp addi sp , sp , 16 - la t0 , fCode92 + la t0 , fCode76 sd t0 , 0(t3) sd t2 , 8(t3) mv t2 , sp @@ -441,7 +383,7 @@ fCode89: sd a1 , 0(t2) mv t4 , sp addi sp , sp , 16 - la t0 , kCode138 + la t0 , kCode122 sd t0 , 0(t4) sd t2 , 8(t4) ld t2 , 0(t3) @@ -452,29 +394,26 @@ fCode89: mv a1 , t4 mv a2 , a2 jr t2 -fCode46: - mv s0 , sp - addi sp , sp , 8 +fCode40: mv t2 , sp addi sp , sp , 16 - la t0 , fCode46 + la t0 , fCode40 sd t0 , 0(t2) sd a0 , 8(t2) mv t3 , sp - addi sp , sp , 24 - sd t2 , 0(t3) - sd a1 , 8(t3) - sd a2 , 16(t3) + addi sp , sp , 32 + sd a3 , 0(t3) + sd t2 , 8(t3) + sd a1 , 16(t3) + sd a2 , 24(t3) mv t2 , sp addi sp , sp , 16 - la t0 , kCode49 + la t0 , kCode43 sd t0 , 0(t2) sd t3 , 8(t2) - ld t3 , 0(a2) - ld t4 , 0(t2) - ld t1 , 8(t2) - sd t1 , 0(s0) - ld t0 , 0(s0) - mv a0 , t0 - mv a1 , t3 - jr t4 + ld t3 , 0(a3) + ld t4 , 8(a3) + mv a0 , t4 + mv a1 , t2 + mv a2 , a2 + jr t3 diff --git a/src/CPS/Ast.hs b/src/CPS/Ast.hs index 6077cf0..82f6b3a 100644 --- a/src/CPS/Ast.hs +++ b/src/CPS/Ast.hs @@ -12,12 +12,12 @@ data CTm = LetVal X CVal CTm | LetProj X Int X CTm | LetCont K X CTm CTm - | ContApp K X - | FuncApp F K X + | ContApp K [X] + | FuncApp F K [X] | Case X K K | LetPrim X PrimOp [X] CTm | If0 X K K - | LetFix F K X CTm CTm + | LetFix F K [X] CTm CTm | Halt X deriving (Show) diff --git a/src/CPS/Trans.hs b/src/CPS/Trans.hs index 593c447..1f9d30a 100644 --- a/src/CPS/Trans.hs +++ b/src/CPS/Trans.hs @@ -28,7 +28,8 @@ transCont ml cont = transContH ml transCont e1 ( \z1 -> - transCont + traverseC + transCont e2 ( \z2 -> do k <- fresh "k" @@ -62,11 +63,11 @@ transCont ml cont = transContH ml transContH (ML.Lam x e) = do f <- fresh "f" k <- fresh "k" - z <- transCont e (return . ContApp k) + z <- traverseC transCont [e] (return . ContApp k) LetVal f (Lam k x z) <$> cont f transContH (ML.Let x e1 e2) = do j <- fresh "j" - LetCont j x <$> transCont e2 cont <*> transCont e1 (return . ContApp j) + LetCont j x <$> transCont e2 cont <*> traverseC transCont [e1] (return . ContApp j) transContH (ML.Case e (x1, e1) (x2, e2)) = transCont e @@ -76,8 +77,8 @@ transCont ml cont = transContH ml k1 <- fresh "k" k2 <- fresh "k" kx0 <- cont x0 - z1 <- transCont e1 (return . ContApp k0) - z2 <- transCont e2 (return . ContApp k0) + z1 <- traverseC transCont [e1] (return . ContApp k0) + z2 <- traverseC transCont [e2] (return . ContApp k0) return ( LetCont k0 @@ -106,7 +107,7 @@ transCont ml cont = transContH ml ) transContH (ML.LetFix f x e1 e2) = do k <- fresh "k" - LetFix f k x <$> transCont e1 (return . ContApp k) <*> transCont e2 cont + LetFix f k x <$> traverseC transCont [e1] (return . ContApp k) <*> transCont e2 cont transContH (ML.If0 e e1 e2) = transCont e @@ -118,8 +119,8 @@ transCont ml cont = transContH ml k1 <- fresh "k" k2 <- fresh "k" kx0 <- cont x0 - z1 <- transCont e1 (return . ContApp k0) - z2 <- transCont e2 (return . ContApp k0) + z1 <- traverseC transCont [e1] (return . ContApp k0) + z2 <- traverseC transCont [e2] (return . ContApp k0) return ( LetCont k0 diff --git a/src/Closure/Ast.hs b/src/Closure/Ast.hs index 249455e..22d40fb 100644 --- a/src/Closure/Ast.hs +++ b/src/Closure/Ast.hs @@ -14,12 +14,12 @@ data CloTm = LetVal X CloVal CloTm | LetProj X Int X CloTm | LetCont K Env X CloTm CloTm - | ContApp K Env X - | FuncApp F Env K X + | ContApp K Env [X] + | FuncApp F Env K [X] | Case X (X, CloTm) (X, CloTm) | LetPrim X PrimOp [X] CloTm | If0 X CloTm CloTm - | LetFix F Env K X CloTm CloTm + | LetFix F Env K [X] CloTm CloTm | Halt X deriving (Show) diff --git a/src/Closure/Clo.hs b/src/Closure/Clo.hs index 9afb67e..754abfb 100644 --- a/src/Closure/Clo.hs +++ b/src/Closure/Clo.hs @@ -9,12 +9,12 @@ freeF :: CPS.CTm -> Set Ident freeF (CPS.LetVal x v k) = (freeF k \\ singleton x) `union` freeH v freeF (CPS.LetProj x _ y k) = (freeF k \\ singleton x) `union` singleton y freeF (CPS.LetCont k x k1 k2) = (freeF k1 \\ singleton x) `union` (freeF k2 \\ singleton k) -freeF (CPS.ContApp k x) = fromList [k, x] -freeF (CPS.FuncApp f k x) = fromList [f, k, x] +freeF (CPS.ContApp k x) = fromList (k : x) +freeF (CPS.FuncApp f k x) = fromList ([f, k] ++ x) freeF (CPS.Case x k1 k2) = fromList [x, k1, k2] freeF (CPS.LetPrim x _ ys k) = (freeF k \\ singleton x) `union` fromList ys freeF (CPS.If0 x k1 k2) = fromList [x, k1, k2] -freeF (CPS.LetFix f k x k1 k2) = (freeF k1 \\ fromList [f, k, x]) `union` (freeF k2 \\ singleton f) +freeF (CPS.LetFix f k x k1 k2) = (freeF k1 \\ fromList ([f, k] ++ x)) `union` (freeF k2 \\ singleton f) freeF (CPS.Halt x) = singleton x freeH :: CPS.CVal -> Set Ident @@ -83,17 +83,17 @@ cloConv (CPS.FuncApp f k x) = do cloConv (CPS.Case x k1 k2) = do x1 <- fresh "x" x2 <- fresh "x" - clokx1 <- cloConv (CPS.ContApp k1 x1) - clokx2 <- cloConv (CPS.ContApp k2 x2) + clokx1 <- cloConv (CPS.ContApp k1 [x1]) + clokx2 <- cloConv (CPS.ContApp k2 [x2]) return (Case x (k1, clokx1) (k2, clokx2)) cloConv (CPS.LetPrim x op ys k) = LetPrim x op ys <$> cloConv k cloConv (CPS.If0 x k1 k2) = do x1 <- fresh "x" - clokx1 <- cloConv (CPS.ContApp k1 x1) - clokx2 <- cloConv (CPS.ContApp k2 x1) + clokx1 <- cloConv (CPS.ContApp k1 [x1]) + clokx2 <- cloConv (CPS.ContApp k2 [x1]) return (LetVal x1 Unit (If0 x clokx1 clokx2)) cloConv (CPS.LetFix f k x k1 k2) = do - let ys = toList (freeF k1 \\ fromList [f, k, x]) + let ys = toList (freeF k1 \\ fromList ([f, k] ++ x)) fCode <- fresh "fCode" env <- fresh "env" env1 <- fresh "env" diff --git a/src/Flat/Hoist.hs b/src/Flat/Hoist.hs index 8ad5254..ec1bcb3 100644 --- a/src/Flat/Hoist.hs +++ b/src/Flat/Hoist.hs @@ -39,9 +39,9 @@ hoistExpr (Cl.LetCont k env x k1 k2) = do let fk = Func k [env, x] b1 e1 return (f1 ++ f2 ++ [fk], b2, e2) hoistExpr (Cl.ContApp k env x) = do - return ([], [], App k [env, x]) + return ([], [], App k (env : x)) hoistExpr (Cl.FuncApp f env k x) = do - return ([], [], App f [env, k, x]) + return ([], [], App f ([env, k] ++ x)) hoistExpr (Cl.Case x (x1, k1) (x2, k2)) = do (f1, b1, e1) <- hoistExpr k1 (f2, b2, e2) <- hoistExpr k2 @@ -56,6 +56,6 @@ hoistExpr (Cl.If0 x k1 k2) = do hoistExpr (Cl.LetFix f env k x k1 k2) = do (f1, b1, e1) <- hoistExpr k1 (f2, b2, e2) <- hoistExpr k2 - let ff = Func f [env, k, x] b1 e1 + let ff = Func f ([env, k] ++ x) b1 e1 return (f1 ++ f2 ++ [ff], b2, e2) hoistExpr (Cl.Halt x) = return ([], [], Halt x) \ No newline at end of file diff --git a/src/ML/Ast.hs b/src/ML/Ast.hs index 1d34793..b5251cf 100644 --- a/src/ML/Ast.hs +++ b/src/ML/Ast.hs @@ -1,20 +1,20 @@ module ML.Ast where -import Utils.Ident (PrimOp) +import Utils.Ident (Ident, PrimOp) data Core = Var String | Unit | Num Int | Str String - | Lam String Core - | App Core Core + | Lam Ident Core + | App Core [Core] | Tuple [Core] | Proj Int Core | Tag Int Core - | Case Core (String, Core) (String, Core) + | Case Core (Ident, Core) (Ident, Core) | Prim PrimOp [Core] - | Let String Core Core + | Let Ident Core Core | If0 Core Core Core - | LetFix String String Core Core + | LetFix Ident [Ident] Core Core deriving (Show)