on master help sign in
Provide Debug.crash with module names
commitc64aa8ec9101957c454e23c1263846cf65ecb13a authorEvan Czaplicki <info@elm-lang.org> dateFri 23 Oct 2015, 14:45 (11 years ago) parentbda1dc9
7 files changed+96−47
src/AST/Expression/Optimized.hs+1−1 src/Generate/JavaScript/BuiltIn.hs+20−4 src/Generate/JavaScript/Expression.hs+2−2 src/Optimize.hs+54−30 src/Optimize/Case.hs+2−2 src/Optimize/Environment.hs+15−6 src/Optimize/Inline.hs+2−2
src/AST/Expression/Optimized.hs+1−1
@@ -54,7 +54,7 @@ data Expr
5454 | Record [(String, Expr)]
5555 | Port (General.PortImpl Expr Type.Canonical)
5656 | GLShader String String Literal.GLShaderTipe
57− | Crash R.Region (Maybe String)
57+ | Crash ModuleName.Canonical R.Region (Maybe String)
5858
5959
6060data Decider a
src/Generate/JavaScript/BuiltIn.hs+20−4
@@ -3,17 +3,21 @@ module Generate.JavaScript.BuiltIn where
33import Control.Arrow (first)
44import qualified Language.ECMAScript3.Syntax as JS
55
6+import qualified AST.Module.Name as ModuleName
67import Generate.JavaScript.Helpers
78import qualified Reporting.Region as R
89
910
11+
1012utils :: String -> [JS.Expression ()] -> JS.Expression ()
1113utils func args =
1214 obj ["_U", func] `call` args
1315
1416
17+
1518-- LITERALS
1619
20+
1721character :: Char -> JS.Expression ()
1822character char =
1923 utils "chr" [ JS.StringLit () [char] ]
@@ -24,8 +28,10 @@ string str =
2428 JS.StringLit () str
2529
2630
31+
2732-- LISTS
2833
34+
2935list :: [JS.Expression ()] -> JS.Expression ()
3036list elements =
3137 utils "list" [ JS.ArrayLit () elements ]
@@ -36,8 +42,10 @@ range low high =
3642 utils "range" [ low, high ]
3743
3844
45+
3946-- RECORDS
4047
48+
4149recordUpdate :: JS.Expression () -> [(String, JS.Expression ())] -> JS.Expression ()
4250recordUpdate record fields =
4351 utils "update"
@@ -46,8 +54,10 @@ recordUpdate record fields =
4654 ]
4755
4856
57+
4958-- COMPARISIONS
5059
60+
5161eq :: JS.Expression () -> JS.Expression () -> JS.Expression ()
5262eq left right =
5363 utils "eq" [ left, right ]
@@ -58,16 +68,22 @@ cmp left right =
5868 utils "cmp" [ left, right ]
5969
6070
71+
6172-- CRASH
6273
63−crash :: R.Region -> Maybe String -> JS.Expression ()
64−crash region maybeCaseCrashValue =
74+
75+crash :: ModuleName.Canonical -> R.Region -> Maybe String -> JS.Expression ()
76+crash home region maybeCaseCrashValue =
77+ let
78+ homeString =
79+ JS.StringLit () (ModuleName.canonicalToString home)
80+ in
6581 case maybeCaseCrashValue of
6682 Nothing ->
67− utils "crash" [ regionToJs region ]
83+ utils "crash" [ homeString, regionToJs region ]
6884
6985 Just crashValue ->
70− utils "caseCrash" [ regionToJs region, ref crashValue ]
86+ utils "caseCrash" [ homeString, regionToJs region, ref crashValue ]
7187
7288
7389regionToJs :: R.Region -> JS.Expression ()
src/Generate/JavaScript/Expression.hs+2−2
@@ -209,8 +209,8 @@ generateCode expr =
209209 do expr' <- generateJsExpr expr
210210 jsExpr (Port.task name expr' portType)
211211
212− Crash region maybeCaseCrashValue ->
213− jsExpr $ BuiltIn.crash region maybeCaseCrashValue
212+ Crash home region maybeCaseCrashValue ->
213+ jsExpr $ BuiltIn.crash home region maybeCaseCrashValue
214214
215215
216216-- FUNCTIONS
src/Optimize.hs+54−30
@@ -19,8 +19,10 @@ import qualified Reporting.Annotation as A
1919import qualified Reporting.Region as R
2020
2121
22+
2223-- OPTIMIZE
2324
25+
2426optimize :: DT.VariantDict -> Module.CanonicalModule -> Module.Optimized
2527optimize variantDict module_@(Module.Module home _ _ _ _ body) =
2628 let
@@ -28,7 +30,7 @@ optimize variantDict module_@(Module.Module home _ _ _ _ body) =
2830 flattenLets [] (Module.program body)
2931
3032 optDefs =
31− Env.run variantDict (concat <$> mapM (optimizeDef (Just home)) defs)
33+ Env.run variantDict home (concat <$> mapM (optimizeDef True) defs)
3234 in
3335 module_ { Module.body = body { Module.program = optDefs } }
3436
@@ -43,56 +45,77 @@ flattenLets defs annExpr@(A.A _ expr) =
4345 (defs, annExpr)
4446
4547
48+
4649-- CONVERT DEFINITIONS
4750
48−optimizeDef :: Maybe ModuleName.Canonical -> Can.Def -> Env.Optimizer [Opt.Def]
49−optimizeDef home (Can.Definition (Can.Facts deps) pattern@(A.A _ ptrn) expression _) =
51+
52+optimizeDef :: Bool -> Can.Def -> Env.Optimizer [Opt.Def]
53+optimizeDef isRoot (Can.Definition (Can.Facts deps) pattern expression _) =
5054 let
51− (args, rawBody) =
55+ (args, canBody) =
5256 Expr.collectLambdas expression
5357 in
54− case (ptrn, args) of
55− (P.Var name, _ : _) ->
58+ do home <-
59+ if isRoot then
60+ Just <$> Env.getHome
61+ else
62+ return Nothing
5663
57− do htc <- Env.getTailCall
58− Env.setTailCall False
64+ optimizeDefHelp home deps pattern args canBody
5965
60− (args, body) <- optimizeFunction (Just name) args rawBody
6166
62− isTailCall <- Env.getTailCall
63− Env.setTailCall htc
67+optimizeDefHelp
68+ :: Maybe ModuleName.Canonical
69+ -> [Var.TopLevel]
70+ -> P.CanonicalPattern
71+ -> [P.CanonicalPattern]
72+ -> Can.Expr
73+ -> Env.Optimizer [Opt.Def]
74+optimizeDefHelp home deps pattern@(A.A _ ptrn) args rawBody =
75+ let
76+ facts =
77+ Opt.Facts home deps
78+ in
79+ case (ptrn, args) of
80+ (P.Var name, _ : _) ->
6481
65− let facts = Opt.Facts home deps
82+ do htc <- Env.getTailCall
83+ Env.setTailCall False
6684
67− return $
68− if isTailCall then
85+ (args, body) <- optimizeFunction (Just name) args rawBody
6986
70− [ Opt.TailDef facts name args body ]
87+ isTailCall <- Env.getTailCall
88+ Env.setTailCall htc
7189
72− else
90+ return $
91+ if isTailCall then
92+ [ Opt.TailDef facts name args body ]
7393
94+ else
7495 [ Opt.Def facts name (Opt.Function args body) ]
7596
76− (P.Var name, []) ->
97+ (P.Var name, []) ->
98+
99+ do hasTailCall <- Env.getTailCall
100+ body <- optimizeExpr Nothing rawBody
101+ Env.setTailCall hasTailCall
102+ return [ Opt.Def facts name body ]
77103
78− do hasTailCall <- Env.getTailCall
79− body <- optimizeExpr Nothing rawBody
80− Env.setTailCall hasTailCall
81− return [ Opt.Def (Opt.Facts home deps) name body ]
104+ (_, []) ->
82105
83− (_, []) ->
106+ do name <- Env.freshName
107+ optBody <- optimizeExpr Nothing rawBody
108+ let optDef = Opt.Def facts name optBody
109+ return (optDef : patternToDefs home name pattern)
84110
85− do name <- Env.freshName
86− optBody <- optimizeExpr Nothing rawBody
87− let optDef = Opt.Def (Opt.Facts home deps) name optBody
88− return (optDef : patternToDefs home name pattern)
111+ _ ->
112+ error "there should never be a function where the name is not a P.Var"
89113
90− _ ->
91− error "there should never be a function where the name is not a P.Var"
92114
93115
94116-- OPTIMIZE FUNCTION
95117
118+
96119optimizeFunction
97120 :: Maybe String
98121 -> [P.CanonicalPattern]
@@ -196,7 +219,8 @@ optimizeExpr context annExpr@(A.A region expression) =
196219
197220 Expr.Var name ->
198221 if name == Var.inCore ["Debug"] "crash" then
199− pure (Opt.Crash region Nothing)
222+ do home <- Env.getHome
223+ pure (Opt.Crash home region Nothing)
200224
201225 else
202226 pure (Opt.Var name)
@@ -248,7 +272,7 @@ optimizeExpr context annExpr@(A.A region expression) =
248272 <*> keepLooking finally
249273
250274 Expr.Let defs body ->
251− do optDefs <- concat <$> T.traverse (optimizeDef Nothing) defs
275+ do optDefs <- concat <$> T.traverse (optimizeDef False) defs
252276 Opt.Let optDefs <$> keepLooking body
253277
254278 Expr.Case expr branches ->
src/Optimize/Case.hs+2−2
@@ -43,11 +43,11 @@ tagCrashBranches
4343 -> Env.Optimizer (P.CanonicalPattern, Opt.Expr)
4444tagCrashBranches region branch@(pattern@(A.A pr _), expr) =
4545 case expr of
46− Opt.Call (Opt.Crash _ _) [arg] ->
46+ Opt.Call (Opt.Crash home _ _) [arg] ->
4747 do name <- Env.freshName
4848 return
4949 ( A.A pr (P.Alias name pattern)
50− , Opt.Call (Opt.Crash region (Just name)) [arg]
50+ , Opt.Call (Opt.Crash home region (Just name)) [arg]
5151 )
5252
5353 _ ->
src/Optimize/Environment.hs+15−6
@@ -2,12 +2,14 @@ module Optimize.Environment
22 ( Optimizer, run
33 , Env
44 , getVariantDict
5+ , getHome
56 , getTailCall, setTailCall
67 , freshName
78 ) where
89
910import qualified Control.Monad.State as State
1011
12+import qualified AST.Module.Name as ModuleName
1113import qualified Optimize.DecisionTree as DT
1214
1315
@@ -15,15 +17,16 @@ type Optimizer a =
1517 State.State Env a
1618
1719
18−run :: DT.VariantDict -> Optimizer a -> a
19−run variantDict optimizer =
20− State.evalState optimizer (Env False 0 variantDict)
20+run :: DT.VariantDict -> ModuleName.Canonical -> Optimizer a -> a
21+run variantDict home optimizer =
22+ State.evalState optimizer (Env False 0 variantDict home)
2123
2224
2325data Env = Env
2426 { _hasTailCall :: Bool
2527 , _uid :: Int
2628 , _variantDict :: DT.VariantDict
29+ , _home :: ModuleName.Canonical
2730 }
2831
2932
@@ -32,6 +35,11 @@ getVariantDict =
3235 State.gets _variantDict
3336
3437
38+getHome :: Optimizer ModuleName.Canonical
39+getHome =
40+ State.gets _home
41+
42+
3543getTailCall :: Optimizer Bool
3644getTailCall =
3745 State.gets _hasTailCall
@@ -45,6 +53,7 @@ setTailCall bool =
4553
4654freshName :: Optimizer String
4755freshName =
48− do (Env htc uid vd) <- State.get
49− State.put (Env htc (uid + 1) vd)
50− return ("_p" ++ show uid)
56+ do (Env htc uid vd home) <- State.get
57+ State.put (Env htc (uid + 1) vd home)
58+ return ("_p" ++ show uid)
59+
src/Optimize/Inline.hs+2−2
@@ -178,7 +178,7 @@ count expression =
178178 GLShader _ _ _ ->
179179 Map.empty
180180
181− Crash _ _ ->
181+ Crash _ _ _ ->
182182 Map.empty
183183
184184
@@ -287,5 +287,5 @@ replace substitutions expression =
287287 GLShader _ _ _ ->
288288 expression
289289
290− Crash _ _ ->
290+ Crash _ _ _ ->
291291 expression