@@ -83,8 +83,8 @@ codegenJs conf ci =
83 if optim then putStrLn "compiling width idris-js optimizations"
83 if optim then putStrLn "compiling width idris-js optimizations"
84 else putStrLn "compiling widthout idris-js optimizations"
84 else putStrLn "compiling widthout idris-js optimizations"
85 else pure ()
85 else pure ()
86− let defs = addAlist (liftDecls ci) emptyContext
86+ let defs = Map.fromList $ liftDecls ci
87− let used = used_decls defs [sMN 0 "runMain"]
87+ let used = Map.elems $ used_defs defs [sMN 0 "runMain"]
88 used `deepseq` if debug then
88 used `deepseq` if debug then
90 putStrLn $ "Finished calculating used"
90 putStrLn $ "Finished calculating used"
@@ -137,7 +137,7 @@ doPartials x =
137 in jsAst2Text $
137 in jsAst2Text $
138 JsFun (jsPartialName p) vars1 $ JsReturn $ JsCurryLambda vars2 (JsApp (jsName n) (map JsVar (vars1 ++ vars2)) )
138 JsFun (jsPartialName p) vars1 $ JsReturn $ JsCurryLambda vars2 (JsApp (jsName n) (map JsVar (vars1 ++ vars2)) )
140−doCodegen :: CGConf -> LDefs -> [LDecl] -> (Text, CGStats)
140+doCodegen :: CGConf -> Map Name LDecl -> [LDecl] -> (Text, CGStats)
141doCodegen conf defs decls =
141doCodegen conf defs decls =
142 let xs = map (doCodegenDecl conf defs) decls
142 let xs = map (doCodegenDecl conf defs) decls
143 groupCGStats x y = CGStats {usedWriteStr = usedWriteStr x || usedWriteStr y
143 groupCGStats x y = CGStats {usedWriteStr = usedWriteStr x || usedWriteStr y
@@ -147,7 +147,7 @@ doCodegen conf defs decls =
148 in (T.intercalate "\n" $ map fst xs, foldl' groupCGStats emptyStats (map snd xs) )
148 in (T.intercalate "\n" $ map fst xs, foldl' groupCGStats emptyStats (map snd xs) )
150−doCodegenDecl :: CGConf -> LDefs -> LDecl -> (Text, CGStats)
150+doCodegenDecl :: CGConf -> Map Name LDecl -> LDecl -> (Text, CGStats)
151doCodegenDecl conf defs (LFun _ n args def) =
151doCodegenDecl conf defs (LFun _ n args def) =
152 let (ast, stats) = cgFun conf defs n args def
152 let (ast, stats) = cgFun conf defs n args def
153 in (jsAst2Text $ ast, stats)
153 in (jsAst2Text $ ast, stats)
@@ -159,7 +159,7 @@ seqJs [] = JsEmpty
159seqJs (x:xs) = JsSeq x (seqJs xs)
159seqJs (x:xs) = JsSeq x (seqJs xs)
162−data CGBodyState = CGBodyState { defs :: LDefs
162+data CGBodyState = CGBodyState { defs :: Map Name LDecl
163 , lastIntName :: Int
163 , lastIntName :: Int
164 , currentFnNameAndArgs :: (Text, [Text])
164 , currentFnNameAndArgs :: (Text, [Text])
165 , usedArgsTailCallOptim :: Set (Text, Text)
165 , usedArgsTailCallOptim :: Set (Text, Text)
@@ -195,7 +195,7 @@ getConsId :: Name -> State CGBodyState Int
195getConsId n =
195getConsId n =
197 st <- get
197 st <- get
198− case lookupCtxtExact n (defs st) of
198+ case Map.lookup n (defs st) of
199 Just (LConstructor _ conId _) -> pure conId
199 Just (LConstructor _ conId _) -> pure conId
200 _ -> error $ "Internal JS Backend error " ++ showCG n ++ " is not a constructor."
200 _ -> error $ "Internal JS Backend error " ++ showCG n ++ " is not a constructor."
@@ -203,7 +203,7 @@ getArgList :: Name -> State CGBodyState (Maybe [Name])
203getArgList n =
203getArgList n =
205 st <- get
205 st <- get
206− case lookupCtxtExact n (defs st) of
206+ case Map.lookup n (defs st) of
207 Just (LFun _ _ a _) -> pure $ Just a
207 Just (LFun _ _ a _) -> pure $ Just a
208 _ -> pure Nothing
208 _ -> pure Nothing
@@ -224,7 +224,7 @@ replaceVarsByProj n d z =
224 Just i -> JsArrayProj (JsInt i) n
224 Just i -> JsArrayProj (JsInt i) n
227−cgFun :: CGConf -> LDefs -> Name -> [Name] -> LExp -> (JsAST, CGStats)
227+cgFun :: CGConf -> Map Name LDecl -> Name -> [Name] -> LExp -> (JsAST, CGStats)
228cgFun cnf dfs n args def =
228cgFun cnf dfs n args def =
230 fnName = jsName n
230 fnName = jsName n
@@ -295,10 +295,9 @@ cgBody rt (LV (Glob n)) =
295 pure $ ([], addRT rt $ JsApp (jsName n) [])
295 pure $ ([], addRT rt $ JsApp (jsName n) [])
296 Just a ->
296 Just a ->
298− --newNames <- getNewCGNames $ length a
299 let part = Partial n 0 (length a)
298 let part = Partial n 0 (length a)
300 addPartial part
299 addPartial part
301− pure ([], addRT rt $ JsApp (jsPartialName part) []) --JsCurryLambda newNames (JsApp (jsName n) (map JsVar newNames)))
300+ pure ([], addRT rt $ JsApp (jsPartialName part) [])
302 Nothing ->
301 Nothing ->
303 pure $ ([], addRT rt $ JsVar $ jsName n)
302 pure $ ([], addRT rt $ JsVar $ jsName n)
304cgBody rt (LApp tailcall (LV (Glob fn)) args) =
303cgBody rt (LApp tailcall (LV (Glob fn)) args) =
@@ -315,10 +314,6 @@ cgBody rt (LApp tailcall (LV (Glob fn)) args) =
315 modify (\x-> x {isTailRec = True})
314 modify (\x-> x {isTailRec = True})
316 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
315 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
317 addUsedArgsTailCallOptim y3
316 addUsedArgsTailCallOptim y3
318− --vars <- getNewCGNames $ length argN --sequence $ map (\_->getNewCGName) argN
319− --let refreshArgs = JsSetVar "cgArgs2" (JsArray argVals)
320− --let calcs = map (\(n,v) -> JsDecVar n v) (zip vars argVals)
321− --let calcsToArgs = map (\(n,v) -> JsSetVar n (JsVar v)) (zip argN vars)
322 pure (preDecs, y1 `JsSeq` y2)
317 pure (preDecs, y1 `JsSeq` y2)
@@ -339,8 +334,7 @@ cgBody rt (LApp tailcall (LV (Glob fn)) args) =
340 let part = Partial fn lenArgs lenAgFn
335 let part = Partial fn lenArgs lenAgFn
341 addPartial part
336 addPartial part
342− --newNames <- getNewCGNames $ lenAgFn - lenArgs
343− pure (preDecs, addRT rt $ JsApp (jsPartialName part) argVals ) -- JsCurryLambda newNames (JsApp fname (argVals ++ (map JsVar newNames) ) ) )
337+ pure (preDecs, addRT rt $ JsApp (jsPartialName part) argVals )
345cgBody rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
339cgBody rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
346cgBody rt (LLazyApp n args) =
340cgBody rt (LLazyApp n args) =
@@ -431,7 +425,6 @@ cgAlts rt resName scrvar ((LConCase _ n args exp):r) =
431 (d, v) <- cgBody (altsRT resName rt) exp
425 (d, v) <- cgBody (altsRT resName rt) exp
432 (ar, def) <- cgAlts rt resName scrvar r
426 (ar, def) <- cgAlts rt resName scrvar r
433 conId <- getConsId n
427 conId <- getConsId n
434− -- let branchBody = JsSeq (conCaseToProjs 1 scrvar args) $ JsSeq (seqJs d) v
435 let replace = replaceVarsByProj scrvar (Map.fromList $ zip (map jsName args) [1..])
428 let replace = replaceVarsByProj scrvar (Map.fromList $ zip (map jsName args) [1..])
436 let branchBody = JsSeq (seqJs $ map replace d) (replace v)
429 let branchBody = JsSeq (seqJs $ map replace d) (replace v)
437 pure ((JsInt conId, branchBody) : ar, def)
430 pure ((JsInt conId, branchBody) : ar, def)
@@ -454,7 +447,7 @@ cgForeignArg (FApp (UN "JS_FnT") [_,FApp (UN "JS_Fn") [_,_, a, FApp (UN "JS_FnIO
454cgForeignArg (desc, _) =
447cgForeignArg (desc, _) =
456 st <- get
449 st <- get
457− error $ "Foreign arg type " ++ show desc ++ " not supported yet. While generating function " ++ (show $ fst $ currentFnNameAndArgs st)
450+ error $ "Foreign arg type " ++ show desc ++ " not supported. While generating function " ++ (show $ fst $ currentFnNameAndArgs st)
459cgForeignRes :: FDesc -> JsAST -> State CGBodyState JsAST
452cgForeignRes :: FDesc -> JsAST -> State CGBodyState JsAST
460cgForeignRes (FApp (UN "JS_IntT") _) x = pure x
453cgForeignRes (FApp (UN "JS_IntT") _) x = pure x
@@ -466,7 +459,7 @@ cgForeignRes (FCon (UN "JS_Float")) x = pure x
466cgForeignRes desc val =
459cgForeignRes desc val =
468 st <- get
461 st <- get
469− error $ "Foreign return type " ++ show desc ++ " not supported yet. While generating function " ++ (show $ fst $ currentFnNameAndArgs st)
462+ error $ "Foreign return type " ++ show desc ++ " not supported. While generating function " ++ (show $ fst $ currentFnNameAndArgs st)
471setUsedITBig :: State CGBodyState ()
464setUsedITBig :: State CGBodyState ()
472setUsedITBig = modify (\s -> s {usedITBig = True})
465setUsedITBig = modify (\s -> s {usedITBig = True})