on master help sign in
javascript: remove allocations
b059c08raichoo
src/IRTS/CodegenJavaScript.hs+53−1
@@ -317,9 +317,61 @@ jsSubst var new (JSCond conds) =
317jsSubst var new (JSAssign lhs rhs) =
317jsSubst var new (JSAssign lhs rhs) =
318 JSAssign (jsSubst var new lhs) (jsSubst var new rhs)
318 JSAssign (jsSubst var new lhs) (jsSubst var new rhs)
319
319
320+jsSubst var new (JSAlloc val (Just js)) =
321+ JSAlloc val (Just (jsSubst var new js))
322+
320jsSubst _ _ js = js
323jsSubst _ _ js = js
321
324
322
325
326+removeAllocations :: JS -> JS
327+removeAllocations (JSSeq body) =
328+ JSSeq $ removeHelper (map removeAllocations body)
329+ where
330+ removeHelper :: [JS] -> [JS]
331+ removeHelper [js] = [js]
332+ removeHelper ((JSAlloc name (Just val@(JSIdent _))):js) =
333+ map (jsSubst name val) (removeHelper js)
334+ removeHelper (j:js) = j : removeHelper js
335+
336+removeAllocations (JSFunction args body) =
337+ JSFunction args $ removeAllocations body
338+
339+removeAllocations (JSReturn ret) =
340+ JSReturn $ removeAllocations ret
341+
342+removeAllocations (JSApp fun args) =
343+ JSApp (removeAllocations fun) (map removeAllocations args)
344+
345+removeAllocations (JSNew con args) =
346+ JSNew con $ map removeAllocations args
347+
348+removeAllocations (JSOp op lhs rhs) =
349+ JSOp op (removeAllocations lhs) (removeAllocations rhs)
350+
351+removeAllocations (JSProj obj field) =
352+ JSProj (removeAllocations obj) field
353+
354+removeAllocations (JSArray vals) =
355+ JSArray $ map removeAllocations vals
356+
357+removeAllocations (JSAssign lhs rhs) =
358+ JSAssign (removeAllocations lhs) (removeAllocations rhs)
359+
360+removeAllocations (JSAlloc name (Just val)) =
361+ JSAlloc name $ Just (removeAllocations val)
362+
363+removeAllocations (JSIndex val idx) =
364+ JSIndex (removeAllocations val) (removeAllocations idx)
365+
366+removeAllocations (JSCond conds) =
367+ JSCond $ map (removeAllocations *** removeAllocations) conds
368+
369+removeAllocations (JSTernary c t f) =
370+ JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
371+
372+removeAllocations js = js
373+
374+
323isJSConstant :: JS -> Bool
375isJSConstant :: JS -> Bool
324isJSConstant js
376isJSConstant js
325 | JSString _ <- js = True
377 | JSString _ <- js = True
@@ -749,7 +801,7 @@ codegenJavaScript target definitions filename outputType = do
749
801
750 functions :: [String]
802 functions :: [String]
751 functions =
803 functions =
752− map compileJS $ initConstructors (map reduceConstants ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def))
804+ map (compileJS . removeAllocations) $ initConstructors (map reduceConstants ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def))
753
805
754 mainLoop :: String
806 mainLoop :: String
755 mainLoop = compileJS $
807 mainLoop = compileJS $