[project @ 2000-03-10 14:53:00 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / compiler.c
index e3d2d4c..41799cc 100644 (file)
@@ -4,14 +4,15 @@
  * `kernel' language, elimination of pattern matching and translation to
  * super combinators (lambda lifting).
  *
- * Hugs 98 is Copyright (c) Mark P Jones, Alastair Reid and the Yale
- * Haskell Group 1994-99, and is distributed as Open Source software
- * under the Artistic License; see the file "Artistic" that is included
- * in the distribution for details.
+ * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
+ * Yale Haskell Group, and the Oregon Graduate Institute of Science and
+ * Technology, 1994-1999, All rights reserved.  It is distributed as
+ * free software under the license in the file "License", which is
+ * included in the distribution.
  *
  * $RCSfile: compiler.c,v $
- * $Revision: 1.8 $
- * $Date: 1999/07/06 15:24:36 $
+ * $Revision: 1.20 $
+ * $Date: 2000/03/10 14:53:00 $
  * ------------------------------------------------------------------------*/
 
 #include "prelude.h"
@@ -21,6 +22,7 @@
 #include "errors.h"
 #include "Rts.h"                       /* for rts_eval and related stuff   */
 #include "RtsAPI.h"                    /* for rts_eval and related stuff   */
+#include "SchedAPI.h"                  /* for RevertCAFs                   */
 #include "Schedule.h"
 #include "link.h"
 
@@ -92,6 +94,9 @@ static List local addStgVar             Args((List,Pair));
 
 static Cell local translate(e)         /* Translate expression:            */
 Cell e; {
+#if 0
+    printf ( "translate: " );print(e,100);printf("\n");
+#endif
     switch (whatIs(e)) {
         case LETREC     : snd(snd(e)) = translate(snd(snd(e)));
                           return expandLetrec(e);
@@ -135,7 +140,9 @@ Cell e; {
         case STRCELL    :
         case BIGCELL    :
         case CHARCELL   : return e;
-
+#if IPARAM
+       case IPVAR      : return nameId;
+#endif
         case FINLIST    : mapOver(translate,snd(e));
                           return mkConsList(snd(e));
 
@@ -181,7 +188,8 @@ Cell e; {
                                              nv));
                           }
 
-        default         : fprintf(stderr, "stuff=%d\n",whatIs(e));internal("translate");
+        default         : fprintf(stderr, "stuff=%d\n",whatIs(e));
+                          internal("translate");
     }
     return e;
 }
@@ -201,6 +209,9 @@ Triple tr; {                           /* triple of expressions.           */
 
 static Void local transAlt(e)          /* Translate alt:                   */
 Cell e; {                              /* ([Pat], Rhs) ==> ([Pat], Rhs')   */
+#if 0
+    printf ( "transAlt:  " );print(snd(e),100);printf("\n");
+#endif
     snd(e) = transRhs(snd(e));
 }
 
@@ -214,7 +225,15 @@ static List local transBinds(bs)        /* Translate list of bindings:     */
 List bs; {                              /* eliminating pattern matching on */
     List newBinds = NIL;                /* lhs of bindings.                */
     for (; nonNull(bs); bs=tl(bs)) {
+#if IPARAM
+       Cell v = fst(hd(bs));
+       while (isAp(v) && fst(v) == nameInd)
+           v = arg(v);
+       fst(hd(bs)) = v;
+       if (isVar(v)) {
+#else
         if (isVar(fst(hd(bs)))) {
+#endif
             mapProc(transAlt,snd(hd(bs)));
             newBinds = cons(hd(bs),newBinds);
         }
@@ -600,9 +619,7 @@ Cell pat; {                       /* test with pat.                        */
 
         case STRCELL   :
         case CHARCELL  :
-#if NPLUSK
         case ADDPAT    :
-#endif
         case TUPLE     :
         case NAME      : return pat;
 
@@ -618,10 +635,8 @@ Cell p; {
     Cell h = getHead(p);
     if (h==nameFromInt || h==nameFromInteger || h==nameFromDouble)
         return p;
-#if NPLUSK
     else if (whatIs(h)==ADDPAT)
         return ap(fun(p),refutePat(arg(p)));
-#endif
 #if TREX
     else if (isExt(h)) {
         Cell pf = refutePat(extField(p));
@@ -689,10 +704,8 @@ Cell pat; {                     /* replaces parts of pattern that do not   */
                              if (h==nameFromInt     ||
                                  h==nameFromInteger || h==nameFromDouble)
                                  return WILDCARD;
-#if NPLUSK
                              else if (whatIs(h)==ADDPAT)
                                  return pat;
-#endif
 #if TREX
                              else if (isExt(h)) {
                                  Cell pf = matchPat(extField(pat));
@@ -792,14 +805,12 @@ List lds; {
                              return remPat(snd(pat),nv,lds);
                          }
 
-#if NPLUSK
         case ADDPAT    : return remPat1(arg(pat),       /* n + k = expr */
                                         ap(ap(ap(namePmSub,
                                                  arg(fun(pat))),
                                                  mkInt(snd(fun(fun(pat))))),
                                                  expr),
                                         lds);
-#endif
 
         case FINLIST   : return remPat1(mkConsList(snd(pat)),expr,lds);
 
@@ -919,9 +930,7 @@ Cell e;  {                             /* e  = expr to transform           */
 
         case AP       : return pmcPair(co,sc,e);
 
-#if NPLUSK
         case ADDPAT   :
-#endif
 #if TREX
         case EXT      :
 #endif
@@ -1328,10 +1337,8 @@ Cell ma; {                      /* match, ma.                              */
     Cell h = getHead(p);
     switch (whatIs(h)) {
         case CONFLDS : return fst(snd(p));
-#if NPLUSK
         case ADDPAT  : arg(fun(p)) = translate(arg(fun(p)));
                        return fun(p);
-#endif
 #if TREX
         case EXT     : h      = fun(fun(p));
                        arg(h) = translate(arg(h));
@@ -1372,18 +1379,12 @@ Cell d; {
         case CHARCELL  : return 0;
 #if TREX
         case AP        : switch (whatIs(fun(d))) {
-#if NPLUSK
                              case ADDPAT : return 1;
-#endif
                              case EXT    : return 2;
                              default     : return 0;
                          }
 #else
-#if NPLUSK
         case AP        : return (whatIs(fun(d))==ADDPAT) ? 1 : 0;
-#else
-        case AP        : return 0;      /* must be an Int or Float lit     */
-#endif
 #endif
     }
     internal("discrArity");
@@ -1392,10 +1393,8 @@ Cell d; {
 
 static Bool local eqNumDiscr(d1,d2)     /* Determine whether two numeric   */
 Cell d1, d2; {                          /* descriptors have same value     */
-#if NPLUSK
     if (whatIs(fun(d1))==ADDPAT)
         return whatIs(fun(d2))==ADDPAT && snd(fun(d1))==snd(fun(d2));
-#endif
     if (isInt(arg(d1)))
         return isInt(arg(d2)) && intOf(arg(d1))==intOf(arg(d2));
     if (isFloat(arg(d1)))
@@ -1474,43 +1473,46 @@ Void evalExp() {                    /* compile and run input expression    */
     /* Run thread (and any other runnable threads) */
 
     /* Re-initialise the scheduler - ToDo: do I need this? */
-    initScheduler();
+    /* JRS, 991118: on SM's advice, don't call initScheduler every time.
+       This causes an assertion failure in GC.c(revert_dead_cafs)
+       unless doRevertCAFs below is permanently TRUE.
+     */
+    /* initScheduler(); */
 #ifdef CRUDE_PROFILING
     cp_init();
 #endif
 
-    /* ToDo: don't really initScheduler every time.  fix */
     {
         HaskellObj      result; /* ignored */
         sighandler_t    old_ctrlbrk;
         SchedulerStatus status;
-        old_ctrlbrk    = signal(SIGINT, eval_ctrlbrk);
-        assert(old_ctrlbrk != SIG_ERR);
-        status         = rts_eval_(closureOfVar(v),10000,&result);
+        Bool            doRevertCAFs = TRUE;  /* do not change -- comment above */
+        old_ctrlbrk         = signal(SIGINT, eval_ctrlbrk);
+        ASSERT(old_ctrlbrk != SIG_ERR);
+        status              = rts_eval_(closureOfVar(v),10000,&result);
         signal(SIGINT,old_ctrlbrk);
+        fflush (stderr); 
+        fflush (stdout);
         switch (status) {
         case Deadlock:
-        case AllBlocked: /* I don't understand the distinction - ADR */
-                printf("{Deadlock}");
-                RevertCAFs();
+                printf("{Deadlock or Blackhole}");
+                if (doRevertCAFs) RevertCAFs();
                 break;
         case Interrupted:
                 printf("{Interrupted}");
-                RevertCAFs();
+                if (doRevertCAFs) RevertCAFs();
                 break;
         case Killed:
-                printf("{Killed}");
-                RevertCAFs();
+                printf("{Interrupted or Killed}");
+                if (doRevertCAFs) RevertCAFs();
                 break;
         case Success:
-         //fflush(stderr);fflush(stdout);
-         //fprintf(stderr, "\n\nFinal top-of-stack is\n" );
-         //printObj ( *(MainRegTable.rSp) );
-               RevertCAFs();
+               if (doRevertCAFs) RevertCAFs();
                 break;
         default:
                 internal("evalExp: Unrecognised SchedulerStatus");
         }
+        deleteAllThreads();
         fflush(stdout);
         fflush(stderr);
     }
@@ -1541,14 +1543,6 @@ Void compileDefns() {                  /* compile script definitions       */
     Target i = 0;
     List binds = NIL;
 
-    /* a nasty hack.  But I don't know an easier way to make */
-    /* these things appear.                                  */
-    if (lastModule() == modulePrelude) {
-       implementCfun ( nameCons, NIL );
-       implementCfun ( nameNil, NIL );
-       implementCfun ( nameUnit, NIL );
-    }
-
     {
         List vss;
         List vs;
@@ -1590,14 +1584,6 @@ Void compileDefns() {                  /* compile script definitions       */
 
     binds = addGlobals(binds);
     done();
-#if USE_HUGS_OPTIMIZER
-    if (optimise) {
-       t = length(binds);
-       setGoal("Simplifying",t);
-       optimiseTopBinds(binds);
-       done();
-    }
-#endif
     setGoal("Generating code",t);
     stgCGBinds(binds);
 
@@ -1618,6 +1604,9 @@ static Void local compileGenFunction(n) /* Produce code for internally     */
 Name n; {                               /* generated function              */
     List defs  = name(n).defn;
     Int  arity = length(fst(hd(defs)));
+#if 0
+    printf ( "compGenFn: " );print(defs,100);printf("\n");
+#endif
     compiler(RESET);
     currentName = n;
     mapProc(transAlt,defs);
@@ -1645,20 +1634,17 @@ Pair p; {                               /* Should be merged with genDefns, */
 Void compiler(what)
 Int what; {
     switch (what) {
-        case INSTALL :
+        case PREPREL :
         case RESET   : freeVars      = NIL;
                        freeFuns      = NIL;
                        freeBegin     = mkOffset(0);
-                       //extraVars     = NIL;
-                       //numExtraVars  = 0;
-                       //localOffset   = 0;
-                       //localArity    = 0;
                        break;
 
         case MARK    : mark(freeVars);
                        mark(freeFuns);
-                       //mark(extraVars);
                        break;
+
+        case POSTPREL: break;
     }
 }