Skip to content

Commit 35d902f

Browse files
committed
Rework the way foreign imports are linked, add treefmt, reformat sources
1 parent 6caf468 commit 35d902f

60 files changed

Lines changed: 1084 additions & 1322 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Setup.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ module Main (main) where
22

33
import Distribution.Simple
44

5-
main :: IO ()
5+
main IO ()
66
main = defaultMain

exe/Main.hs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,30 @@ import Path.IO qualified as Path
1616
import Prettyprinter (defaultLayoutOptions, layoutPretty)
1717
import Prettyprinter.Render.Text (renderIO)
1818

19-
main :: IO ()
19+
main IO ()
2020
main = Utf8.withUtf8 do
2121
Cli.Args
2222
{ foreignPath
2323
, luaOutputFile
2424
, psOutputPath
2525
, appOrModule
26-
} <-
26+
}
2727
Cli.parseArguments
2828

29-
foreignDir :: Tagged "foreign" (Path Abs Dir) <-
29+
foreignDir Tagged "foreign" (Path Abs Dir)
3030
Tagged
3131
<$> case unTagged foreignPath of
32-
Path.Abs a -> pure a
33-
Path.Rel r -> Path.makeAbsolute r
32+
Path.Abs a pure a
33+
Path.Rel r Path.makeAbsolute r
3434

35-
luaOutput <-
35+
luaOutput
3636
case unTagged luaOutputFile of
37-
Path.Abs a -> pure a
38-
Path.Rel r -> Path.makeAbsolute r
37+
Path.Abs a pure a
38+
Path.Rel r Path.makeAbsolute r
3939

4040
putTextLn "Compiling modules:"
4141

42-
luaChunk <-
42+
luaChunk
4343
Backend.compileModules psOutputPath foreignDir appOrModule
4444
& handleModuleNotFoundError
4545
& handleModuleDecodingError
@@ -48,7 +48,7 @@ main = Utf8.withUtf8 do
4848
& Oops.runOops
4949

5050
let outputFile = toFilePath luaOutput
51-
withFile outputFile WriteMode \h ->
51+
withFile outputFile WriteMode \h
5252
renderIO h . layoutPretty defaultLayoutOptions $
5353
Printer.printLuaChunk luaChunk
5454

@@ -58,41 +58,41 @@ main = Utf8.withUtf8 do
5858
-- Error handlers --------------------------------------------------------------
5959

6060
handleModuleNotFoundError
61-
:: ExceptT (Oops.Variant (CoreFn.ModuleNotFound ': e)) IO a
62-
-> ExceptT (Oops.Variant e) IO a
63-
handleModuleNotFoundError = Oops.catch \(CoreFn.ModuleNotFound p) ->
61+
ExceptT (Oops.Variant (CoreFn.ModuleNotFound ': e)) IO a
62+
ExceptT (Oops.Variant e) IO a
63+
handleModuleNotFoundError = Oops.catch \(CoreFn.ModuleNotFound p)
6464
die . toString . unlines $
6565
[ "Can't find CoreFn module file: " <> toText (toFilePath p)
6666
, "Please make sure you did run purs with the `-g corefn` arg."
6767
]
6868

6969
handleModuleDecodingError
70-
:: ExceptT (Oops.Variant (CoreFn.ModuleDecodingErr ': e)) IO a
71-
-> ExceptT (Oops.Variant e) IO a
72-
handleModuleDecodingError = Oops.catch \(CoreFn.ModuleDecodingErr p e) ->
70+
ExceptT (Oops.Variant (CoreFn.ModuleDecodingErr ': e)) IO a
71+
ExceptT (Oops.Variant e) IO a
72+
handleModuleDecodingError = Oops.catch \(CoreFn.ModuleDecodingErr p e)
7373
die . toString . unlines $
7474
[ "Can't parse CoreFn module file: " <> toText (toFilePath p)
7575
, toText e
7676
]
7777

7878
handleCoreFnError
79-
:: ExceptT (Oops.Variant (IR.CoreFnError ': e)) IO a
80-
-> ExceptT (Oops.Variant e) IO a
79+
ExceptT (Oops.Variant (IR.CoreFnError ': e)) IO a
80+
ExceptT (Oops.Variant e) IO a
8181
handleCoreFnError =
82-
Oops.catch \(e :: IR.CoreFnError) ->
82+
Oops.catch \(e IR.CoreFnError)
8383
die $ "CoreFn contains an unexpected value: " <> show e
8484

8585
handleLuaError
86-
:: ExceptT (Oops.Variant (Lua.Error ': e)) IO a
87-
-> ExceptT (Oops.Variant e) IO a
86+
ExceptT (Oops.Variant (Lua.Error ': e)) IO a
87+
ExceptT (Oops.Variant e) IO a
8888
handleLuaError =
8989
Oops.catch \case
90-
Lua.UnexpectedRefBound modname expr ->
90+
Lua.UnexpectedRefBound modname expr
9191
die . toString . unwords $
9292
[ "Unexpected bound reference:"
9393
, show expr
9494
, "in module"
9595
, runModuleName modname
9696
]
97-
Lua.LinkerErrorForeign e ->
97+
Lua.LinkerErrorForeign e
9898
die $ "Linker error:\n" <> show e

flake.nix

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525
src = ./.;
2626
evalSystem = "x86_64-linux";
2727
# index-state = "2023-06-28T00:00:00Z";
28-
modules = let prof = false;
29-
in [{
30-
doHaddock = false;
31-
doHoogle = false;
32-
enableProfiling = prof;
33-
enableLibraryProfiling = prof;
34-
}];
28+
modules =
29+
let prof = false;
30+
in [{
31+
doHaddock = false;
32+
doHoogle = false;
33+
enableProfiling = prof;
34+
enableLibraryProfiling = prof;
35+
}];
3536
};
3637
})
3738
];
3839
flake = pkgs.hixProject.flake { };
39-
in flake // {
40+
in
41+
flake // {
4042
legacyPackages = pkgs;
4143
packages.default = flake.packages."pslua:exe:pslua";
4244
});

fourmolu.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
indentation: 2
21
comma-style: leading
3-
function-arrows: leading
4-
record-brace-space: true
5-
indent-wheres: false
62
diff-friendly-import-export: true
7-
respectful: true
3+
function-arrows: leading
84
haddock-style: multi-line
9-
newlines-between-decls: 1
105
import-export-style: leading
6+
indent-wheres: false
7+
indentation: 2
8+
newlines-between-decls: 1
9+
record-brace-space: true
10+
respectful: true
11+
single-constraint-parens: never
1112
unicode: always

lib/Language/PureScript/Backend/IR/DCE.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ import Language.PureScript.Backend.IR.Types
2424
, rewriteExpTopDown
2525
)
2626
import Language.PureScript.Names (ModuleName)
27+
import Shower (shower)
2728

2829
data EntryPoint = EntryPoint ModuleName [Name]
2930
deriving stock (Show)
3031

32+
deriving stock instance Show AExp
33+
3134
eliminateDeadCode UberModule UberModule
3235
eliminateDeadCode uber@UberModule {..} =
3336
-- trace ("\n\nannotatedBindings:\n" <> shower annotatedBindings <> "\n") $
3437
-- trace ("\nannotatedExports:\n" <> shower annotatedExports <> "\n") $
3538
-- trace ("\nadjacencyList:\n" <> shower adjacencyList <> "\n") $
3639
-- trace ("\nreachableIds:\n" <> shower reachableIds <> "\n\n") $
3740
uber
38-
{ uberModuleForeigns
39-
, uberModuleBindings = preserveBindings
41+
{ uberModuleBindings = preserveBindings
4042
, uberModuleExports = preservedExports
4143
}
4244
where
@@ -45,6 +47,8 @@ eliminateDeadCode uber@UberModule {..} =
4547
grouping annotatedBindings
4648
case grouping of
4749
Standalone (nodeId, qname, expr) do
50+
-- unless (nodeId `Set.member` reachableIds) $
51+
-- traceM $ "nodeId " <> shower nodeId <> " not in reachable: " <> shower qname
4852
guard $ nodeId `Set.member` reachableIds
4953
[Standalone (qname, dceAnnotatedExp expr)]
5054
RecursiveGroup recBinds
@@ -167,6 +171,7 @@ eliminateDeadCode uber@UberModule {..} =
167171
LiteralArray as foldMap (adjacencyListForExpr scope) as
168172
LiteralObject ps foldMap (adjacencyListForExpr scope . snd) ps
169173
Exception {} mempty
174+
ForeignImport {} mempty
170175
Ctor {} mempty
171176
ReflectCtor a adjacencyListForExpr scope a
172177
Eq a b adjacencyListForExpr scope a <> adjacencyListForExpr scope b
@@ -236,6 +241,7 @@ eliminateDeadCode uber@UberModule {..} =
236241
LiteralChar {} []
237242
LiteralBool {} []
238243
Exception {} []
244+
ForeignImport {} []
239245
Ctor {} []
240246
ReflectCtor a [fst a]
241247
Eq a b [fst a, fst b]
@@ -308,6 +314,7 @@ annotateExp = \case
308314
IfThenElse i t e IfThenElse <$> ann i <*> ann t <*> ann e
309315
Ctor aty ty ctor fs pure $ Ctor aty ty ctor fs
310316
Exception m pure $ Exception m
317+
ForeignImport m p pure $ ForeignImport m p
311318
where
312319
ann Annotated Identity RawExp AnnM (Id, AExp)
313320
ann = liftA2 (,) nextId . annotateExp . runIdentity
@@ -338,6 +345,7 @@ deannotateExp = \case
338345
IfThenElse i t e IfThenElse (de i) (de t) (de e)
339346
Ctor aty ty ctor fs Ctor aty ty ctor fs
340347
Exception m Exception m
348+
ForeignImport m p ForeignImport m p
341349
where
342350
de (a, AExp) Identity Exp
343351
de = pure . deannotateExp . snd

lib/Language/PureScript/Backend/IR/Linker.hs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ data LinkMode
3131

3232
data UberModule = UberModule
3333
{ uberModuleBindings [Grouping (QName, Exp)]
34-
, uberModuleForeigns [(ModuleName, FilePath)]
3534
, uberModuleExports [(Name, Exp)]
3635
}
3736
deriving stock (Show, Eq)
@@ -41,15 +40,14 @@ data UberModule = UberModule
4140

4241
makeUberModule LinkMode [Module] UberModule
4342
makeUberModule linkMode modules =
44-
UberModule
45-
{ uberModuleBindings
46-
, uberModuleForeigns
47-
, uberModuleExports
48-
}
43+
UberModule {uberModuleBindings, uberModuleExports}
4944
where
5045
sortedModules = topoSorted modules
51-
uberModuleBindings = concatMap qualifiedModuleBindings sortedModules
52-
uberModuleForeigns = concatMap qualifiedModuleForeigns sortedModules
46+
47+
uberModuleBindings =
48+
concatMap foreignBindings sortedModules
49+
<> concatMap qualifiedModuleBindings sortedModules
50+
5351
uberModuleExports [(Name, Exp)] =
5452
case linkMode of
5553
LinkAsApplication moduleName name
@@ -61,30 +59,41 @@ makeUberModule linkMode modules =
6159
, exportedName moduleExports
6260
]
6361

64-
qualifiedModuleBindings Module [Grouping (QName, Exp)]
65-
qualifiedModuleBindings Module {moduleName, moduleBindings, moduleForeigns} =
66-
foreignBindings <> flip fmap moduleBindings \case
67-
Standalone binding Standalone $ qualifyBinding binding
68-
RecursiveGroup bindings RecursiveGroup $ qualifyBinding <$> bindings
62+
foreignBindings Module [Grouping (QName, Exp)]
63+
foreignBindings Module {moduleName, modulePath, moduleForeigns} =
64+
foreignModuleBinding <> foreignNamesBindings
6965
where
70-
foreignModule = refImported moduleName (Name "foreign") 0
71-
foreignBindings [Grouping (QName, Exp)] =
66+
foreignName = Name "foreign"
67+
foreignModuleRef = refImported moduleName foreignName 0
68+
69+
foreignModuleBinding [Grouping (QName, Exp)]
70+
foreignModuleBinding =
71+
[ Standalone
72+
( QName moduleName foreignName
73+
, ForeignImport moduleName modulePath
74+
)
75+
| not (null moduleForeigns)
76+
]
77+
78+
foreignNamesBindings [Grouping (QName, Exp)] =
7279
moduleForeigns <&> \name
7380
Standalone
7481
( QName moduleName name
75-
, objectProp foreignModule (PropName (nameToText name))
82+
, objectProp foreignModuleRef (PropName (nameToText name))
7683
)
7784

85+
qualifiedModuleBindings Module [Grouping (QName, Exp)]
86+
qualifiedModuleBindings Module {moduleName, moduleBindings, moduleForeigns} =
87+
moduleBindings <&> \case
88+
Standalone binding Standalone $ qualifyBinding binding
89+
RecursiveGroup bindings RecursiveGroup $ qualifyBinding <$> bindings
90+
where
7891
qualifyBinding (Name, Exp) (QName, Exp)
7992
qualifyBinding = bimap (QName moduleName) (qualifyTopRefs moduleName topRefs)
8093
where
8194
topRefs Map Name Index = Map.fromList do
8295
(,0) <$> ((moduleBindings >>= bindingNames) <> moduleForeigns)
8396

84-
qualifiedModuleForeigns Module [(ModuleName, FilePath)]
85-
qualifiedModuleForeigns Module {moduleName, modulePath, moduleForeigns} =
86-
[(moduleName, modulePath) | not (null moduleForeigns)]
87-
8897
qualifyTopRefs ModuleName Map Name Index Exp Exp
8998
qualifyTopRefs moduleName = go
9099
where

lib/Language/PureScript/Backend/IR/Query.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ import Language.PureScript.Backend.IR.Types
1212
)
1313
import Language.PureScript.Names (runModuleName)
1414

15-
usesRuntimeLazy :: UberModule -> Bool
15+
usesRuntimeLazy UberModule Bool
1616
usesRuntimeLazy UberModule {uberModuleBindings, uberModuleExports} =
1717
getAny $
1818
foldMap
19-
(foldMap (\(_qname, e) -> Any (findRuntimeLazyInExpr e)) . listGrouping)
19+
(foldMap (\(_qname, e) Any (findRuntimeLazyInExpr e)) . listGrouping)
2020
uberModuleBindings
2121
<> foldMap (Any . findRuntimeLazyInExpr . snd) uberModuleExports
2222

23-
findRuntimeLazyInExpr :: Exp -> Bool
23+
findRuntimeLazyInExpr Exp Bool
2424
findRuntimeLazyInExpr expr =
2525
countFreeRef (Local (Name "$__runtime_lazy")) expr > 0
2626

27-
usesPrimModule :: UberModule -> Bool
27+
usesPrimModule UberModule Bool
2828
usesPrimModule UberModule {uberModuleBindings, uberModuleExports} =
2929
getAny $
3030
foldMap
31-
(foldMap (\(_qname, e) -> Any (findPrimModuleInExpr e)) . listGrouping)
31+
(foldMap (\(_qname, e) Any (findPrimModuleInExpr e)) . listGrouping)
3232
uberModuleBindings
3333
<> foldMap (Any . findPrimModuleInExpr . snd) uberModuleExports
3434

35-
findPrimModuleInExpr :: Exp -> Bool
35+
findPrimModuleInExpr Exp Bool
3636
findPrimModuleInExpr expr =
3737
Map.keys (countFreeRefs expr) & any \case
38-
Local _name -> False
39-
Imported moduleName _name -> runModuleName moduleName == "Prim"
38+
Local _name False
39+
Imported moduleName _name runModuleName moduleName == "Prim"

lib/Language/PureScript/Backend/IR/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ data RawExp (n ∷ Type → Type)
7070
| Let (NonEmpty (Grouping (n Name, Annotated n RawExp))) (Annotated n RawExp)
7171
| IfThenElse (Annotated n RawExp) (Annotated n RawExp) (Annotated n RawExp)
7272
| Exception Text
73+
| ForeignImport ModuleName FilePath
7374

7475
type Exp = RawExp Identity
7576

@@ -478,6 +479,7 @@ countFreeRefs = fmap getSum . MMap.toMap . countFreeRefs' mempty
478479
LiteralChar {} mempty
479480
Ctor {} mempty
480481
Exception {} mempty
482+
ForeignImport {} mempty
481483
where
482484
go = countFreeRefs' minIndexes
483485

@@ -568,6 +570,7 @@ substitute name idx replacement = substitute' idx
568570
LiteralChar {} subExpression
569571
Ctor {} subExpression
570572
Exception {} subExpression
573+
ForeignImport {} subExpression
571574
where
572575
go Exp Exp = substitute' index
573576

0 commit comments

Comments
 (0)