on master help sign in
javascript: improved jsSubst
d37c3d4raichoo
src/IRTS/CodegenJavaScript.hs+37−24
@@ -246,14 +246,17 @@ jsLet name value body =
246 ) [value]
246 ) [value]
247
247
248
248
249−jsSubst :: String -> JS -> JS -> JS
249+jsSubst :: JS -> JS -> JS -> JS
250−jsSubst var new (JSVar old)
250+jsSubst var new old
251+ | var == old = new
252+
253+jsSubst (JSIdent var) new (JSVar old)
251 | var == translateVariableName old = new
254 | var == translateVariableName old = new
252− | otherwise = JSVar old
255+ | otherwise = JSVar old
253
256
254−jsSubst var new (JSIdent old)
257+jsSubst var new old@(JSIdent _)
255 | var == old = new
258 | var == old = new
256− | otherwise = JSIdent old
259+ | otherwise = old
257
260
258jsSubst var new (JSArray fields) =
261jsSubst var new (JSArray fields) =
259 JSArray (map (jsSubst var new) fields)
262 JSArray (map (jsSubst var new) fields)
@@ -261,25 +264,25 @@ jsSubst var new (JSArray fields) =
261jsSubst var new (JSNew con vals) =
264jsSubst var new (JSNew con vals) =
262 JSNew con $ map (jsSubst var new) vals
265 JSNew con $ map (jsSubst var new) vals
263
266
264−jsSubst var new (JSApp (JSProj (JSFunction args body) "apply") vals)
267+jsSubst (JSIdent var) new (JSApp (JSProj (JSFunction args body) "apply") vals)
265 | var `notElem` args =
268 | var `notElem` args =
266− JSApp (JSProj (JSFunction args (jsSubst var new body)) "apply") (
269+ JSApp (JSProj (JSFunction args (jsSubst (JSIdent var) new body)) "apply") (
267− map (jsSubst var new) vals
270+ map (jsSubst (JSIdent var) new) vals
268 )
271 )
269 | otherwise =
272 | otherwise =
270 JSApp (JSProj (JSFunction args body) "apply") (
273 JSApp (JSProj (JSFunction args body) "apply") (
271− map (jsSubst var new) vals
274+ map (jsSubst (JSIdent var) new) vals
272 )
275 )
273
276
274−jsSubst var new (JSApp (JSFunction [arg] body) vals)
277+jsSubst (JSIdent var) new (JSApp (JSFunction [arg] body) vals)
275 | var /= arg =
278 | var /= arg =
276 JSApp (JSFunction [arg] (
279 JSApp (JSFunction [arg] (
277− jsSubst var new body
280+ jsSubst (JSIdent var) new body
278− )) $ map (jsSubst var new) vals
281+ )) $ map (jsSubst (JSIdent var) new) vals
279 | otherwise =
282 | otherwise =
280 JSApp (JSFunction [arg] (
283 JSApp (JSFunction [arg] (
281 body
284 body
282− )) $ map (jsSubst var new) vals
285+ )) $ map (jsSubst (JSIdent var) new) vals
283
286
284jsSubst var new (JSApp lhs rhs) =
287jsSubst var new (JSApp lhs rhs) =
285 JSApp (jsSubst var new lhs) (map (jsSubst var new) rhs)
288 JSApp (jsSubst var new lhs) (map (jsSubst var new) rhs)
@@ -324,7 +327,7 @@ removeAllocations (JSSeq body) =
324 removeHelper :: [JS] -> [JS]
327 removeHelper :: [JS] -> [JS]
325 removeHelper [js] = [js]
328 removeHelper [js] = [js]
326 removeHelper ((JSAlloc name (Just val@(JSIdent _))):js) =
329 removeHelper ((JSAlloc name (Just val@(JSIdent _))):js) =
327− map (jsSubst name val) (removeHelper js)
330+ map (jsSubst (JSIdent name) val) (removeHelper js)
328 removeHelper (j:js) = j : removeHelper js
331 removeHelper (j:js) = j : removeHelper js
329
332
330removeAllocations (JSFunction args body) =
333removeAllocations (JSFunction args body) =
@@ -378,7 +381,7 @@ isJSConstant js
378
381
379inlineJS :: JS -> JS
382inlineJS :: JS -> JS
380inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
383inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
381− inlineJS $ jsSubst "cse" val body
384+ inlineJS $ jsSubst (JSIdent "cse") val body
382
385
383inlineJS (JSReturn (JSApp (JSFunction [arg] cond@(JSCond _)) [val])) =
386inlineJS (JSReturn (JSApp (JSFunction [arg] cond@(JSCond _)) [val])) =
384 JSSeq [ JSAlloc arg (Just (inlineJS val))
387 JSSeq [ JSAlloc arg (Just (inlineJS val))
@@ -394,7 +397,7 @@ inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
394 inlineApply [] body _ = inlineJS body
397 inlineApply [] body _ = inlineJS body
395 inlineApply (a:as) body n =
398 inlineApply (a:as) body n =
396 inlineApply as (
399 inlineApply as (
397− jsSubst a (JSIndex (JSProj var "vars") (JSNum (JSInt n))) body
400+ jsSubst (JSIdent a) (JSIndex (JSProj var "vars") (JSNum (JSInt n))) body
398 ) (n + 1)
401 ) (n + 1)
399
402
400inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
403inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
@@ -408,33 +411,43 @@ inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
408inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
411inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
409 | JSNew con [tag, vals] <- ret
412 | JSNew con [tag, vals] <- ret
410 , opt <- inlineJS val =
413 , opt <- inlineJS val =
411− JSNew con [tag, jsSubst arg opt vals]
414+ JSNew con [tag, jsSubst (JSIdent arg) opt vals]
412
415
413 | JSNew con [JSFunction [] (JSReturn (JSApp fun vars))] <- ret
416 | JSNew con [JSFunction [] (JSReturn (JSApp fun vars))] <- ret
414 , opt <- inlineJS val =
417 , opt <- inlineJS val =
415 JSNew con [JSFunction [] (
418 JSNew con [JSFunction [] (
419+ JSReturn (
420+ JSApp (
421+ jsSubst (JSIdent arg) opt fun
422+ ) (
416− JSReturn $ JSApp (jsSubst arg opt fun) (map (jsSubst arg opt) vars)
423+ map (jsSubst (JSIdent arg) opt) vars
424+ )
425+ )
417 )]
426 )]
418
427
419 | JSApp (JSProj obj field) args <- ret
428 | JSApp (JSProj obj field) args <- ret
420 , opt <- inlineJS val =
429 , opt <- inlineJS val =
430+ JSApp (
421− JSApp (JSProj (jsSubst arg opt obj) field) $ map (jsSubst arg opt) args
431+ JSProj (jsSubst (JSIdent arg) opt obj) field
432+ ) (
433+ map (jsSubst (JSIdent arg) opt) args
434+ )
422
435
423 | JSIndex (JSProj obj field) idx <- ret
436 | JSIndex (JSProj obj field) idx <- ret
424 , opt <- inlineJS val =
437 , opt <- inlineJS val =
425 JSIndex (JSProj (
438 JSIndex (JSProj (
426− jsSubst arg opt obj
439+ jsSubst (JSIdent arg) opt obj
427 ) field
440 ) field
428− ) (jsSubst arg opt idx)
441+ ) (jsSubst (JSIdent arg) opt idx)
429
442
430 | JSOp op lhs rhs <- ret
443 | JSOp op lhs rhs <- ret
431 , opt <- inlineJS val =
444 , opt <- inlineJS val =
432− JSOp op (jsSubst arg opt lhs) $
445+ JSOp op (jsSubst (JSIdent arg) opt lhs) $
433− (jsSubst arg opt rhs)
446+ (jsSubst (JSIdent arg) opt rhs)
434
447
435 | JSApp (JSIdent fun) args <- ret
448 | JSApp (JSIdent fun) args <- ret
436 , opt <- inlineJS val =
449 , opt <- inlineJS val =
437− JSApp (JSIdent fun) $ map (jsSubst arg opt) args
450+ JSApp (JSIdent fun) $ map (jsSubst (JSIdent arg) opt) args
438
451
439inlineJS (JSApp fun args) =
452inlineJS (JSApp fun args) =
440 JSApp (inlineJS fun) (map inlineJS args)
453 JSApp (inlineJS fun) (map inlineJS args)