[separate declarations of c varaiables from their uses, push variable declarations inwards in c code
John Meacham <john@repetae.net>**20080312134931] hunk ./C/FromGrin2.hs 34
-import Util.SetLike
hunk ./C/FromGrin2.hs 57
+    rDeclare :: Bool,
hunk ./C/FromGrin2.hs 65
-    deriving(Monad,UniqueProducer,MonadState HcHash,MonadWriter Written,MonadReader Env)
+    deriving(Monad,UniqueProducer,MonadState HcHash,MonadWriter Written,MonadReader Env,Functor)
hunk ./C/FromGrin2.hs 69
-runC grin (C m) =  execUniq1 (runRWST m Env { rGrin = grin, rTodo = TodoExp [], rEMap = mempty, rInscope = mempty } emptyHcHash)
+runC grin (C m) =  execUniq1 (runRWST m Env { rGrin = grin, rDeclare = False, rTodo = TodoExp [], rEMap = mempty, rInscope = mempty } emptyHcHash)
hunk ./C/FromGrin2.hs 82
-stringNameToTy :: String -> Op.Ty
-stringNameToTy n = (archOpTy archInfo n)
-
hunk ./C/FromGrin2.hs 146
-    return $ if n `member` is then variable n else localVariable t n
+    dclare <- asks rDeclare
+    return $ if not dclare then variable n else localVariable t n
hunk ./C/FromGrin2.hs 222
+        _ -> error "tyToC: unknown"
hunk ./C/FromGrin2.hs 227
+        _ -> error "tyToC: unknown"
hunk ./C/FromGrin2.hs 232
+        _ -> error "tyToC: unknown"
+    f _ _ = error "tyToC: unknown"
hunk ./C/FromGrin2.hs 240
+localScope xs action = do
+    let fvs = freeVars xs
+    aas <- mapM (\ (v,t) -> do t <- convertType t ; return . toStatement $ localVariable t (varName v)) (filter ((v0 /=) . fst) $ Set.toList fvs)
+    local (rInscope_u $ Set.union (Set.map varName (freeVars xs))) (action . statementOOB $ mconcat aas)
+
+iDeclare action = local (\e -> e { rDeclare = True }) action
hunk ./C/FromGrin2.hs 253
-        return (name,(nm,vs'))
+        return (as,(name,(nm,vs')))
hunk ./C/FromGrin2.hs 255
-    let localJumps xs = local (rEMap_u (Map.fromList xs `mappend`))
+    let localJumps xs action = localScope (fsts xs) $ \dcls ->  local (rEMap_u (Map.fromList (snds xs) `mappend`)) (fmap (dcls &) action)
hunk ./C/FromGrin2.hs 257
-    ss <- (convertBody body)
hunk ./C/FromGrin2.hs 258
-       ss <- convertBody b
-       return (annotate (show as) (label (toName (show name ++ "_" ++ show u))) & subBlock ss)
+        ss <- convertBody b
+        return (annotate (show as) (label (toName (show name ++ "_" ++ show u))) & subBlock ss)
+    ss <- (convertBody body)
hunk ./C/FromGrin2.hs 277
-            v'' <- convertVal v
+            v'' <- iDeclare $ convertVal v
hunk ./C/FromGrin2.hs 284
-            as' <- mapM convertVal as
+            as' <- iDeclare $ mapM convertVal as
hunk ./C/FromGrin2.hs 317
-            v'' <- convertVal v
+            v'' <- iDeclare $ convertVal v
hunk ./C/FromGrin2.hs 327
-            as' <- mapM convertVal as
+            as' <- iDeclare $ mapM convertVal as
hunk ./C/FromGrin2.hs 342
-            v'' <- convertVal v
+            v'' <- iDeclare $ convertVal v
hunk ./C/FromGrin2.hs 373
-convertBody (e :>>= [(Var vn vt)] :-> e') | not $ isCompound e = do
-    (vn,vt) <- fetchVar' vn vt
+convertBody (e :>>= [(Var vn' vt')] :-> e') | not $ isCompound e = do
+    (vn,vt) <- fetchVar' vn' vt'
hunk ./C/FromGrin2.hs 376
-    ss' <- local (rInscope_u (Set.insert vn)) $ convertBody e'
+    ss' <- convertBody e'
hunk ./C/FromGrin2.hs 379
-convertBody (e :>>= [v@(Var _ _)] :-> e') = do
+convertBody (e :>>= [v@(Var vn vt)] :-> e') = do
hunk ./C/FromGrin2.hs 381
+    vt <- convertType vt
+    let sdecl = statementOOB $ toStatement (localVariable vt (varName vn))
hunk ./C/FromGrin2.hs 385
-    return (ss & ss')
+    return (sdecl & ss & ss')
hunk ./C/FromGrin2.hs 389
-    st <- newVar (anonStructType ts)
-    ss <- localTodo (TodoExp [st]) (convertBody e)
+    (dcl,st) <- newDeclVar (anonStructType ts)
+    vs <- iDeclare $ mapM convertVal xs
+    ss <- localTodo (TodoExp [st]) (convertBody e)
hunk ./C/FromGrin2.hs 393
-    vs <- mapM convertVal xs
-    return $  ss & mconcat [ v =* projectAnon i st | v <- vs | i <- [0..] ] & ss'
+    return $ dcl & ss & mconcat [ v =* projectAnon i st | v <- vs | i <- [0..] ] & ss'
hunk ./C/FromGrin2.hs 455
-    as' <- mapM convertVal as
+    as' <- iDeclare $ mapM convertVal as
hunk ./C/FromGrin2.hs 459
-    return $  mconcat ass & ss'
+    return $ mconcat ass & ss'
hunk ./C/FromGrin2.hs 772
-f_VALUE e     = functionCall (name "VALUE") [e]
-f_ISVALUE e   = functionCall (name "ISVALUE") [e]
hunk ./C/FromGrin2.hs 801
-size_t    = basicType "size_t"
-uintptr_t = basicType "uintptr_t"
hunk ./C/Generate.hs 22
+    statementOOB,
hunk ./C/Generate.hs 38
+    newDeclVar,
hunk ./C/Generate.hs 92
+--envInScope_u f e = e { envInScope = f $ envInScope e }
+
hunk ./C/Generate.hs 97
-    deriving(Monad,MonadWriter [(Name,Type)],MonadState (Int,Map.Map [Type] Name),MonadReader Env)
+    deriving(Monad,MonadWriter [(Name,Type)],MonadState (Int,Map.Map [Type] Name),MonadReader Env,MonadFix)
hunk ./C/Generate.hs 183
-subBlockBody s = draw s
+--subBlockBody s = draw s
hunk ./C/Generate.hs 188
-    (body,uv) <- censor (const []) $ listen (draw s)
+    (body,uv) <-  censor (const []) $ listen (draw s)
hunk ./C/Generate.hs 275
+statementOOB :: Statement -> Statement
+statementOOB s = (sd $ draw s >> return empty)
+
+
hunk ./C/Generate.hs 402
-newVar t = snd `liftM` newDeclVar t
+newVar t = do
+    u <- newUniq
+    let n = name $ 'x':show u
+    return (localVariable t n)
+
hunk ./C/Generate.hs 411
-    return (sd (tell [(n,t)] >> return mempty),localVariable t n)
+    return (sd (tell [(n,t)] >> return mempty),variable n)