-
Notifications
You must be signed in to change notification settings - Fork 18
/
Foreign.purs
317 lines (275 loc) · 10.4 KB
/
Foreign.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
module PureScript.Backend.Optimizer.Codegen.EcmaScript.Foreign where
import Prelude
import Data.Array.NonEmpty as NonEmptyArray
import Data.Enum (fromEnum)
import Data.Lazy as Lazy
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import PureScript.Backend.Optimizer.CoreFn (Ident, Literal(..), Qualified)
import PureScript.Backend.Optimizer.Semantics (BackendSemantics(..), ExternSpine(..), SemConditional(..), evalApp, evalPrimOp, liftInt, makeEffectBind, makeLet)
import PureScript.Backend.Optimizer.Semantics.Foreign (ForeignEval, ForeignSemantics, qualified)
import PureScript.Backend.Optimizer.Syntax (BackendOperator(..), BackendOperator1(..), BackendOperator2(..), BackendOperatorOrd(..))
import PureScript.Backend.Optimizer.Utils (foldr1Array)
esForeignSemantics :: Map (Qualified Ident) ForeignEval
esForeignSemantics = Map.fromFoldable
[ control_monad_st_internal_for
, control_monad_st_internal_foreach
, control_monad_st_internal_while
, data_array_indexImpl
, data_array_st_push
, data_array_st_unshift
, data_array_st_unsafeFreeze
, data_array_st_unsafeThaw
, data_bounded_topInt
, data_bounded_bottomInt
, data_bounded_topChar
, data_bounded_bottomChar
, data_bounded_topNumber
, data_bounded_bottomNumber
, data_enum_toCharCode
, data_show_showCharImpl
, data_show_showIntImpl
, data_show_showNumberImpl
, data_show_showStringImpl
, data_show_showArrayImpl
, data_unit_unit
, effect_forE
, effect_foreachE
, effect_whileE
, foreign_object_copyST
, foreign_object_member
, foreign_object_st_delete
, foreign_object_st_poke
, foreign_object_st_unsafe_unsafeFreeze
]
data_array_indexImpl :: ForeignSemantics
data_array_indexImpl = Tuple (qualified "Data.Array" "indexImpl") go
where
go env _ = case _ of
[ ExternUncurriedApp [ just, nothing, arr, ix ] ] ->
Just $ makeLet Nothing arr \arr' ->
makeLet Nothing ix \ix' ->
SemBranch
( NonEmptyArray.singleton $ SemConditional
( Lazy.defer \_ ->
evalPrimOp env $ Op2 OpBooleanAnd
(evalPrimOp env (Op2 (OpIntOrd OpGte) ix' (liftInt 0)))
(evalPrimOp env (Op2 (OpIntOrd OpLt) ix' (evalPrimOp env (Op1 OpArrayLength arr'))))
)
( Lazy.defer \_ ->
evalApp env just
[ evalPrimOp env (Op2 OpArrayIndex arr' ix') ]
)
)
(pure nothing)
_ ->
Nothing
data_array_st_push :: ForeignSemantics
data_array_st_push = Tuple (qualified "Data.Array.ST" "push") $ arraySTAll (qualified "Data.Array.ST" "pushAll")
data_array_st_unshift :: ForeignSemantics
data_array_st_unshift = Tuple (qualified "Data.Array.ST" "unshift") $ arraySTAll (qualified "Data.Array.ST" "unshiftAll")
arraySTAll :: Qualified Ident -> ForeignEval
arraySTAll ident env _ = case _ of
[ ExternApp [ val ] ] ->
Just $
makeLet Nothing val \nextVal ->
SemLam Nothing \nextRef ->
SemEffectDefer $
evalApp env (NeutStop ident) [ NeutLit (LitArray [ nextVal ]), nextRef ]
_ ->
Nothing
data_array_st_unsafeFreeze :: ForeignSemantics
data_array_st_unsafeFreeze = Tuple (qualified "Data.Array.ST" "unsafeFreeze") unsafeSTCoerce
data_array_st_unsafeThaw :: ForeignSemantics
data_array_st_unsafeThaw = Tuple (qualified "Data.Array.ST" "unsafeThaw") unsafeSTCoerce
unsafeSTCoerce :: ForeignEval
unsafeSTCoerce _ _ = case _ of
[ ExternApp [ ref ] ] ->
Just $ makeLet Nothing ref \nextRef ->
SemEffectPure nextRef
_ ->
Nothing
data_bounded_topInt :: ForeignSemantics
data_bounded_topInt = Tuple (qualified "Data.Bounded" "topInt") go
where
go _ _ = const $ Just $ litInt top
data_bounded_bottomInt :: ForeignSemantics
data_bounded_bottomInt = Tuple (qualified "Data.Bounded" "bottomInt") go
where
go _ _ = const $ Just $ litInt bottom
data_bounded_topNumber :: ForeignSemantics
data_bounded_topNumber = Tuple (qualified "Data.Bounded" "topNumber") go
where
go _ _ = const $ Just $ litNumber top
data_bounded_bottomNumber :: ForeignSemantics
data_bounded_bottomNumber = Tuple (qualified "Data.Bounded" "bottomNumber") go
where
go _ _ = const $ Just $ litNumber bottom
data_bounded_topChar :: ForeignSemantics
data_bounded_topChar = Tuple (qualified "Data.Bounded" "topChar") go
where
go _ _ = const $ Just $ litChar top
data_bounded_bottomChar :: ForeignSemantics
data_bounded_bottomChar = Tuple (qualified "Data.Bounded" "bottomChar") go
where
go _ _ = const $ Just $ litChar bottom
data_enum_toCharCode :: ForeignSemantics
data_enum_toCharCode = Tuple (qualified "Data.Enum" "toCharCode") go
where
go _ _ = case _ of
[ ExternApp [ NeutLit (LitChar c) ] ] ->
Just $ NeutLit $ LitInt $ fromEnum c
_ ->
Nothing
data_show_showStringImpl :: ForeignSemantics
data_show_showStringImpl = Tuple (qualified "Data.Show" "showStringImpl") $ showLit case _ of
NeutLit (LitString n) -> Just n
_ -> Nothing
data_show_showIntImpl :: ForeignSemantics
data_show_showIntImpl = Tuple (qualified "Data.Show" "showIntImpl") $ showLit case _ of
NeutLit (LitInt n) -> Just n
_ -> Nothing
data_show_showNumberImpl :: ForeignSemantics
data_show_showNumberImpl = Tuple (qualified "Data.Show" "showNumberImpl") $ showLit case _ of
NeutLit (LitNumber n) -> Just n
_ -> Nothing
data_show_showCharImpl :: ForeignSemantics
data_show_showCharImpl = Tuple (qualified "Data.Show" "showCharImpl") $ showLit case _ of
NeutLit (LitChar n) -> Just n
_ -> Nothing
data_show_showArrayImpl :: ForeignSemantics
data_show_showArrayImpl = Tuple (qualified "Data.Show" "showArrayImpl") go
where
go env _ = case _ of
[ ExternApp [ showDict, NeutLit (LitArray arr) ] ] ->
Just case NonEmptyArray.fromArray arr of
Nothing ->
litString "[]"
Just nea -> do
let appendStrings l r = evalPrimOp env (Op2 OpStringAppend l r)
let showVal val = evalApp env showDict [ val ]
let foldFn next acc = appendStrings (showVal next) $ appendStrings (litString ",") acc
appendStrings (litString "[") $ foldr1Array foldFn (\last -> appendStrings (showVal last) (litString "]")) nea
_ ->
Nothing
data_unit_unit :: ForeignSemantics
data_unit_unit = Tuple (qualified "Data.Unit" "unit") go
where
go _ _ = case _ of
[] ->
Just NeutPrimUndefined
_ ->
Nothing
showLit :: forall a. Show a => (BackendSemantics -> Maybe a) -> ForeignEval
showLit match _ _ = case _ of
[ ExternApp [ sem ] ] | Just val <- match sem ->
Just $ NeutLit $ LitString (show val)
_ ->
Nothing
control_monad_st_internal_for :: ForeignSemantics
control_monad_st_internal_for = Tuple (qualified "Control.Monad.ST.Internal" "for") forRangeLoop
control_monad_st_internal_foreach :: ForeignSemantics
control_monad_st_internal_foreach = Tuple (qualified "Control.Monad.ST.Internal" "foreach") foreachLoop
control_monad_st_internal_while :: ForeignSemantics
control_monad_st_internal_while = Tuple (qualified "Control.Monad.ST.Internal" "while") whileLoop
effect_forE :: ForeignSemantics
effect_forE = Tuple (qualified "Effect" "forE") forRangeLoop
effect_foreachE :: ForeignSemantics
effect_foreachE = Tuple (qualified "Effect" "foreachE") foreachLoop
effect_whileE :: ForeignSemantics
effect_whileE = Tuple (qualified "Effect" "whileE") whileLoop
foreign_object_copyST :: ForeignSemantics
foreign_object_copyST = Tuple (qualified "Foreign.Object" "_copyST") go
where
go env qual = case _ of
[ ExternApp [ obj ] ] ->
Just $
makeLet Nothing obj \nextObj ->
SemEffectDefer $ evalApp env (NeutStop qual) [ nextObj ]
_ ->
Nothing
foreign_object_member :: ForeignSemantics
foreign_object_member = Tuple (qualified "Foreign.Object" "member") go
where
go _ qual = case _ of
[] ->
Just $ NeutStop qual
_ ->
Nothing
foreign_object_st_delete :: ForeignSemantics
foreign_object_st_delete = Tuple (qualified "Foreign.Object.ST" "delete") go
where
go env qual = case _ of
[ ExternApp [ a ] ] ->
Just $
makeLet Nothing a \a' ->
SemLam Nothing \b ->
makeEffectBind Nothing (evalApp env (NeutStop qual) [ a', b ]) \_ ->
SemEffectPure b
_ ->
Nothing
foreign_object_st_poke :: ForeignSemantics
foreign_object_st_poke = Tuple (qualified "Foreign.Object.ST" "poke") go
where
go env qual = case _ of
[ ExternApp [ a, b ] ] ->
Just $
makeLet Nothing a \a' ->
makeLet Nothing b \b' ->
SemLam Nothing \c ->
makeEffectBind Nothing (evalApp env (NeutStop qual) [ a', b', c ]) \_ ->
SemEffectPure c
_ ->
Nothing
foreign_object_st_unsafe_unsafeFreeze :: ForeignSemantics
foreign_object_st_unsafe_unsafeFreeze = Tuple (qualified "Foreign.Object.ST.Unsafe" "unsafeFreeze") unsafeSTCoerce
forRangeLoop :: ForeignEval
forRangeLoop env qual = case _ of
[ ExternApp [ a, b, SemLam ident c ] ] ->
Just $
makeLet Nothing a \a' ->
makeLet Nothing b \b' ->
SemEffectDefer $ evalApp env (NeutStop qual)
[ a', b', SemLam ident (SemEffectDefer <<< c) ]
[ ExternApp [ a, b, c ] ] ->
Just $
makeLet Nothing a \a' ->
makeLet Nothing b \b' ->
makeLet Nothing c \c' ->
SemEffectDefer $ evalApp env (NeutStop qual)
[ a', b', SemLam Nothing (SemEffectDefer <<< evalApp env c' <<< pure) ]
_ ->
Nothing
foreachLoop :: ForeignEval
foreachLoop env qual = case _ of
[ ExternApp [ a, SemLam ident b ] ] ->
Just $ makeLet Nothing a \a' ->
SemEffectDefer $ evalApp env (NeutStop qual)
[ a', SemLam ident (SemEffectDefer <<< b) ]
[ ExternApp [ a, b ] ] ->
Just $
makeLet Nothing a \a' ->
makeLet Nothing b \b' ->
SemEffectDefer $ evalApp env (NeutStop qual)
[ a', SemLam Nothing (SemEffectDefer <<< evalApp env b' <<< pure) ]
_ ->
Nothing
whileLoop :: ForeignEval
whileLoop env qual = case _ of
[ ExternApp [ a, b ] ] ->
Just $
makeLet Nothing a \a' ->
makeLet Nothing b \b' ->
SemEffectDefer $ evalApp env (NeutStop qual) [ a', SemEffectDefer b' ]
_ ->
Nothing
litInt :: Int -> BackendSemantics
litInt = NeutLit <<< LitInt
litNumber :: Number -> BackendSemantics
litNumber = NeutLit <<< LitNumber
litChar :: Char -> BackendSemantics
litChar = NeutLit <<< LitChar
litString :: String -> BackendSemantics
litString = NeutLit <<< LitString