[project @ 2001-10-29 17:59:37 by simonmar]
[ghc-hetmet.git] / ghc / compiler / javaGen / JavaGen.lhs
index 6278a70..9b5bcba 100644 (file)
@@ -53,11 +53,10 @@ import Name ( NamedThing(..), getOccString, isGlobalName, isLocalName
                , nameModule )
 import PrimRep  ( PrimRep(..) )
 import DataCon ( DataCon, dataConRepArity, dataConRepArgTys, dataConId )
-import qualified TypeRep
 import qualified Type
 import qualified CoreSyn
 import CoreSyn ( CoreBind, CoreExpr, CoreAlt, CoreBndr,
-                 Bind(..), Alt, AltCon(..), collectBinders, isValArg
+                 Bind(..), AltCon(..), collectBinders, isValArg
                )
 import TysWiredIn      ( boolTy, trueDataCon, falseDataCon )
 import qualified CoreUtils
@@ -67,6 +66,7 @@ import Outputable
 
 import Maybe
 import PrimOp
+import Util     ( lengthIs )
 
 #include "HsVersions.h"
 
@@ -267,7 +267,7 @@ javaCase :: (Expr -> Statement) -> CoreExpr -> Id -> [CoreAlt] -> [Statement]
 -- If we've got the wrong one, this is _|_, and the
 -- casting will catch this with an exception.
 
-javaCase r e x [(DataAlt d,bs,rhs)] | length bs > 0
+javaCase r e x [(DataAlt d,bs,rhs)] | not (null bs)
   = java_expr PushExpr e ++
     [ var [Final] (javaName x)
                  (whnf primRep (vmPOP (primRepToType primRep))) ] ++
@@ -287,37 +287,34 @@ javaCase r e x [(DataAlt d,bs,rhs)] | length bs > 0
                      ]
    
 javaCase r e x alts
-  | isIfThenElse && isPrimCmp = 
-       javaIfThenElse r (fromJust maybePrim) tExpr fExpr
-  | otherwise =
-       java_expr PushExpr e ++
+  | isIfThenElse && isPrimCmp
+  = javaIfThenElse r (fromJust maybePrim) tExpr fExpr
+  | otherwise
+  = java_expr PushExpr e ++
        [ var [Final] (javaName x)
                           (whnf primRep (vmPOP (primRepToType primRep)))
-       , mkIfThenElse (map mk_alt alts) 
+       , IfThenElse (map mk_alt con_alts) (Just default_code)
        ]
   where
-     isIfThenElse = CoreUtils.exprType e == boolTy
+     isIfThenElse = CoreUtils.exprType e `Type.eqType` boolTy
                    -- also need to check that x is not free in
                    -- any of the branches.
      maybePrim    = findCmpPrim e []
      isPrimCmp    = isJust maybePrim
-     tExpr        = matches trueDataCon alts
-     fExpr        = matches falseDataCon alts
-
-     matches con [] = error "no match for true or false branch of if/then/else"
-     matches con ((DataAlt d,[],rhs):rest) | con == d = rhs
-     matches con ((DEFAULT,[],rhs):_)                 = rhs
-     matches con (other:rest)                         = matches con rest
+     (_,_,tExpr)  = CoreUtils.findAlt (DataAlt trueDataCon) alts 
+     (_,_,fExpr)  = CoreUtils.findAlt (DataAlt falseDataCon) alts 
 
      primRep = idPrimRep x
      whnf PtrRep = vmWHNF      -- needs evaluation
      whnf _      = id
 
-     mk_alt (DEFAULT, [], rhs)   = (true,          Block (javaExpr r rhs))
-     mk_alt (DataAlt d, bs, rhs) = (instanceOf x d, Block (bind_args d bs ++ javaExpr r rhs))
-     mk_alt alt@(LitAlt lit, [], rhs) 
-                                = (eqLit lit     , Block (javaExpr r rhs))
-     mk_alt alt@(LitAlt _, _, _) = pprPanic "mk_alt" (ppr alt)
+     (con_alts, maybe_default) = CoreUtils.findDefault alts
+     default_code = case maybe_default of
+                       Nothing  -> ExprStatement (Raise excName [Literal (StringLit "case failure")])
+                       Just rhs -> Block (javaExpr r rhs)
+
+     mk_alt (DataAlt d,  bs, rhs) = (instanceOf x d, Block (bind_args d bs ++ javaExpr r rhs))
+     mk_alt (LitAlt lit, bs, rhs) = (eqLit lit     , Block (javaExpr r rhs))
 
 
      eqLit (MachInt n) = Op (Literal (IntLit n))
@@ -337,14 +334,6 @@ javaCase r e x alts
                      , not (isDeadBinder b)
                      ]
 
-
-mkIfThenElse [(Var (Name "true" _),code)] = code
-mkIfThenElse other = IfThenElse other 
-               (Just (ExprStatement 
-                       (Raise excName [Literal (StringLit "case failure")])
-                      )
-                )
-
 javaIfThenElse r cmp tExpr fExpr 
 {-
  - Now what we need to do is generate code for the if/then/else.
@@ -432,7 +421,7 @@ javaApp r (CoreSyn.App f a) as
        | otherwise  = javaApp r f as
 javaApp r (CoreSyn.Var f) as 
   = case isDataConId_maybe f of {
-       Just dc | length as == dataConRepArity dc
+       Just dc | as `lengthIs` dataConRepArity dc
         -- NOTE: Saturated constructors never returning a primitive at this point
         --
         -- We push the arguments backwards, because we are using