Skip to content

Commit

Permalink
Fix unsafeIndex inliner issue andyarvanitis/purescript-native-go-ffi#11
Browse files Browse the repository at this point in the history
  • Loading branch information
andyarvanitis committed Dec 4, 2019
1 parent fead9a4 commit b5b1635
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/CodeGen/IL/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ nameIsILBuiltIn name =
, "Fn"
, "Foreign"
, "Get"
, "Index"
, "Length"
, "Once"
, "Run"
Expand Down Expand Up @@ -184,6 +185,9 @@ string = "string"
arrayLengthFn :: Text
arrayLengthFn = "Length"

indexFn :: Text
indexFn = "Index"

freshName' :: MonadSupply m => m Text
freshName' = do
name <- freshName
Expand Down
11 changes: 10 additions & 1 deletion src/CodeGen/IL/Optimizer/Inliner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ inlineCommonOperators = everywhereTopDown $ applyAll $

, inlineNonClassFunction (isModFn (C.dataFunction, C.apply)) $ \f x -> App Nothing f [x]
, inlineNonClassFunction (isModFn (C.dataFunction, C.applyFlipped)) $ \x f -> App Nothing f [x]
, inlineNonClassFunction (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
, inlineUnsafeIndex (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
] ++
[ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ] ++
[ fn | i <- [0..10], fn <- [ mkEffFn C.controlMonadEffUncurried C.mkEffFn i, runEffFn C.controlMonadEffUncurried C.runEffFn i ] ] ++
Expand Down Expand Up @@ -211,6 +211,15 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
convert (App _ (App _ op' [x]) [y]) | p op' = f x y
convert other = other

inlineUnsafeIndex :: (AST -> Bool) -> (AST -> AST -> AST) -> AST -> AST
inlineUnsafeIndex p _ = convert where
convert :: AST -> AST
convert (App _ (App ss op' [x]) [y@NumericLiteral{}])
| p op' = Indexer ss y x
convert (App _ (App ss op' [x]) [y])
| p op' = App ss (Var Nothing indexFn) [x, y]
convert other = other

isModFn :: (Text, PSString) -> AST -> Bool
isModFn (m, op) (Indexer _ (Var _ op') (Var _ m')) =
m == m' && decodeString op == Just op'
Expand Down
6 changes: 6 additions & 0 deletions src/CodeGen/IL/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ literals = mkPattern' match'
, prettyPrintIL' arg
, return $ emit ")"
]
match (App _ (Var _ fn) args) | fn == indexFn = mconcat <$> sequence
[ return $ emit fn
, return $ emit "("
, intercalate (emit ", ") <$> forM args prettyPrintIL'
, return $ emit ")"
]
match (App _ (Var _ fn) args) | fn == tcoLoop = mconcat <$> sequence
[ return $ emit fn
, return $ emit "("
Expand Down

0 comments on commit b5b1635

Please sign in to comment.