Prep to switch from Def to CExp for backend passes.

This commit is contained in:
2025-03-15 15:46:56 -07:00
parent 5c7d065a88
commit 5ab2a28bcf
13 changed files with 650 additions and 338 deletions

44
src/Lib/TCO.newt Normal file
View File

@@ -0,0 +1,44 @@
module Lib.TCO
import Prelude
import Data.Graph
import Lib.Ref2
import Lib.Common
import Lib.Types
import Lib.CompileExp
-- We need CompileExp here, so we know if it's
-- fully applied, needs eta, etc.
-- Maybe we should move Ref2 Defs over to CExp?
-- But we'll need CExp for constructors, etc.
-- I _could_ collect a stack and look up arity, but
-- at the next stage, we'd need to fake up constructor
-- records
tailNames : CExp List Name
-- This is tricky, we need to skip the first CLam, but
-- a deeper one is a return value
tailNames (CApp (CRef name) args 0) = name :: Nil
tailNames (CCase _ alts) = join $ map altTailNames alts
where
altTailNames : CAlt List Name
altTailNames (CConAlt _ _ exp) = tailNames exp
altTailNames (CDefAlt exp) = tailNames exp
altTailNames (CLitAlt _ exp) = tailNames exp
tailNames (CLet _ _ t) = tailNames t
tailNames (CLetRec _ _ t) = tailNames t
tailNames (CConstr _ args) = join $ map tailNames args
tailNames (CBnd _) = Nil
tailNames (CFun _ _) = Nil
tailNames (CLam _ _) = Nil
tailNames (CApp t args n) = Nil
tailNames (CRef _) = Nil
tailNames CErased = Nil
tailNames (CLit _) = Nil
tailNames (CMeta _) = Nil
tailNames (CRaw _) = Nil
tailCallOpt : {{Ref2 Defs St}} M Unit
tailCallOpt = do
defs <- getRef Defs
putStrLn "TODO TCO"