git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix unsafeIndex inliner issue https://github.com/andyarvanitis/purescript-native-go-ffi/issues/11
commitb5b1635138ba5e3f5dc90df77e3a2fb043e1a7e1 authorAndy Arvanitis <andyarvanitis@gmail.com> dateTue 3 Dec 2019, 19:51 (7 years ago) parentfead9a4
3 files changed+20−1
src/CodeGen/IL/Common.hs+4 src/CodeGen/IL/Optimizer/Inliner.hs+10−1 src/CodeGen/IL/Printer.hs+6
src/CodeGen/IL/Common.hs+4
@@ -81,6 +81,7 @@ nameIsILBuiltIn name =
8181 , "Fn"
8282 , "Foreign"
8383 , "Get"
84+ , "Index"
8485 , "Length"
8586 , "Once"
8687 , "Run"
@@ -184,6 +185,9 @@ string = "string"
184185arrayLengthFn :: Text
185186arrayLengthFn = "Length"
186187
188+indexFn :: Text
189+indexFn = "Index"
190+
187191freshName' :: MonadSupply m => m Text
188192freshName' = do
189193 name <- freshName
src/CodeGen/IL/Optimizer/Inliner.hs+10−1
@@ -122,7 +122,7 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
122122
123123 , inlineNonClassFunction (isModFn (C.dataFunction, C.apply)) $ \f x -> App Nothing f [x]
124124 , inlineNonClassFunction (isModFn (C.dataFunction, C.applyFlipped)) $ \x f -> App Nothing f [x]
125− , inlineNonClassFunction (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
125+ , inlineUnsafeIndex (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
126126 ] ++
127127 [ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ] ++
128128 [ fn | i <- [0..10], fn <- [ mkEffFn C.controlMonadEffUncurried C.mkEffFn i, runEffFn C.controlMonadEffUncurried C.runEffFn i ] ] ++
@@ -211,6 +211,15 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
211211 convert (App _ (App _ op' [x]) [y]) | p op' = f x y
212212 convert other = other
213213
214+ inlineUnsafeIndex :: (AST -> Bool) -> (AST -> AST -> AST) -> AST -> AST
215+ inlineUnsafeIndex p _ = convert where
216+ convert :: AST -> AST
217+ convert (App _ (App ss op' [x]) [y@NumericLiteral{}])
218+ | p op' = Indexer ss y x
219+ convert (App _ (App ss op' [x]) [y])
220+ | p op' = App ss (Var Nothing indexFn) [x, y]
221+ convert other = other
222+
214223 isModFn :: (Text, PSString) -> AST -> Bool
215224 isModFn (m, op) (Indexer _ (Var _ op') (Var _ m')) =
216225 m == m' && decodeString op == Just op'
src/CodeGen/IL/Printer.hs+6
@@ -106,6 +106,12 @@ literals = mkPattern' match'
106106 , prettyPrintIL' arg
107107 , return $ emit ")"
108108 ]
109+ match (App _ (Var _ fn) args) | fn == indexFn = mconcat <$> sequence
110+ [ return $ emit fn
111+ , return $ emit "("
112+ , intercalate (emit ", ") <$> forM args prettyPrintIL'
113+ , return $ emit ")"
114+ ]
109115 match (App _ (Var _ fn) args) | fn == tcoLoop = mconcat <$> sequence
110116 [ return $ emit fn
111117 , return $ emit "("