[project @ 1999-11-29 18:59:23 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / hugs.c
index 5a25988..7102d18 100644 (file)
@@ -9,8 +9,8 @@
  * included in the distribution.
  *
  * $RCSfile: hugs.c,v $
- * $Revision: 1.13 $
- * $Date: 1999/10/15 22:35:04 $
+ * $Revision: 1.25 $
+ * $Date: 1999/11/29 18:59:26 $
  * ------------------------------------------------------------------------*/
 
 #include <setjmp.h>
@@ -29,7 +29,7 @@
 #include "Rts.h"
 #include "RtsAPI.h"
 #include "Schedule.h"
-
+#include "Assembler.h"                                /* DEBUG_LoadSymbols */
 
 Bool haskell98 = TRUE;                  /* TRUE => Haskell 98 compatibility*/
 
@@ -90,7 +90,7 @@ static Void   local forgetScriptsFrom Args((Script));
 static Void   local setLastEdit       Args((String,Int));
 static Void   local failed            Args((Void));
 static String local strCopy           Args((String));
-static Void   local browseit         Args((Module,String));
+static Void   local browseit         Args((Module,String,Bool));
 static Void   local browse           Args((Void));
 
 /* --------------------------------------------------------------------------
@@ -108,13 +108,13 @@ static Void   local browse              Args((Void));
 
 static Bool   printing     = FALSE;     /* TRUE => currently printing value*/
 static Bool   showStats    = FALSE;     /* TRUE => print stats after eval  */
-static Bool   listScripts  = TRUE;      /* TRUE => list scripts after loading*/
+static Bool   listScripts  = TRUE;    /* TRUE => list scripts after loading*/
 static Bool   addType      = FALSE;     /* TRUE => print type with value   */
 static Bool   useDots      = RISCOS;    /* TRUE => use dots in progress    */
 static Bool   quiet        = FALSE;     /* TRUE => don't show progress     */
 static Bool   lastWasObject = FALSE;
        Bool   preludeLoaded = FALSE;
-       Bool   optimise      = FALSE;
+       Bool   debugSC       = FALSE;
 
 typedef 
    struct { 
@@ -189,10 +189,9 @@ return;
                   scriptInfo[i].path
              );
    }
-   //   printf ( "\n" );
    fflush(stdout);fflush(stderr);
-ppScripts();
-ppModules();
+   ppScripts();
+   ppModules();
    printf ( "\n" );
 }
 
@@ -226,26 +225,29 @@ char *argv[]; {
 
     CStackBase = &argc;                 /* Save stack base for use in gc   */
 
-    /* Try and figure out an absolute path to the executable, so
-       we can make a reasonable guess about where the default
-       libraries (Prelude etc) are.
-    */
-    setDefaultLibDir ( argv[0] );
-
     /* If first arg is +Q or -Q, be entirely silent, and automatically run
        main after loading scripts.  Useful for running the nofib suite.    */
     if (argc > 1 && (strcmp(argv[1],"+Q") == 0 || strcmp(argv[1],"-Q")==0)) {
        autoMain = TRUE;
-       hugsEnableOutput(0);
+       if (strcmp(argv[1],"-Q") == 0) {
+        hugsEnableOutput(0);
+       }
     }
 
     Printf("__   __ __  __  ____   ___      _________________________________________\n");
-    Printf("||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard\n");
+    Printf("||   || ||  || ||  || ||__      STGHugs: Based on the Haskell 98 standard\n");
     Printf("||___|| ||__|| ||__||  __||     Copyright (c) 1994-1999\n");
     Printf("||---||         ___||           World Wide Web: http://haskell.org/hugs\n");
     Printf("||   ||                         Report bugs to: hugs-bugs@haskell.org\n");
     Printf("||   || Version: %s _________________________________________\n\n",HUGS_VERSION);
 
+    /* Get the absolute path to the directory containing the hugs 
+       executable, so that we know where the Prelude and nHandle.so/.dll are.
+       We do this by reading env var STGHUGSDIR.  This needs to succeed, so
+       setInstallDir won't return unless it succeeds.
+    */
+    setInstallDir ( argv[0] );
+
 #if SYMANTEC_C
     Printf("   Ported to Macintosh by Hans Aberg, compiled " __DATE__ ".\n\n");
 #endif
@@ -300,8 +302,8 @@ String argv[]; {
    startupHaskell (argc,argv);
    argc = prog_argc; argv = prog_argv;
 
-    namesUpto = numScripts = 0;
-    addStackEntry("Prelude");
+   namesUpto = numScripts = 0;
+   addStackEntry("Prelude");
 
    for (i=1; i<argc; ++i) {            /* process command line arguments  */
         if (strcmp(argv[i], "--")==0) break;
@@ -318,12 +320,16 @@ String argv[]; {
         }
     }
 
-#ifdef DEBUG
-    DEBUG_LoadSymbols(argv_0_orig);
+#if DEBUG
+    { 
+       char exe_name[N_INSTALLDIR + 6];
+       strcpy(exe_name, installDir);
+       strcat(exe_name, "hugs");
+       DEBUG_LoadSymbols(exe_name);
+    }
 #endif
 
 
-
 #if 0
     if (!scriptName[0]) {
         Printf("Prelude not found on current path: \"%s\"\n",
@@ -576,7 +582,9 @@ String s; {                             /* return FALSE if none found.     */
             default  : if (strcmp("98",s)==0) {
                            if (heapBuilt() && ((state && !haskell98) ||
                                                (!state && haskell98))) {
-                               FPrintf(stderr,"Haskell 98 compatibility cannot be changed while the interpreter is running\n");
+                               FPrintf(stderr,
+                                       "Haskell 98 compatibility cannot be changed"
+                                       " while the interpreter is running\n");
                            } else {
                                haskell98 = state;
                            }
@@ -747,7 +755,6 @@ struct options toggle[] = {             /* List of command line toggles    */
     {'w', 1, "Always show which modules are loaded",  &listScripts},
     {'k', 1, "Show kind errors in full",              &kindExpert},
     {'o', 0, "Allow overlapping instances",           &allowOverlap},
-    {'O', 1, "Optimise (improve?) generated code",    &optimise},
 
 
 #if DEBUG_CODE
@@ -762,9 +769,7 @@ struct options toggle[] = {             /* List of command line toggles    */
 #if DEBUG_CODE
     {'D', 1, "Debug: show generated G code",          &debugCode},
 #endif
-#if DEBUG_SHOWSC
     {'S', 1, "Debug: show generated SC code",         &debugSC},
-#endif
 #if 0
     {'f', 1, "Terminate evaluation on first error",   &failOnError},
     {'u', 1, "Use \"show\" to display results",       &useShow},
@@ -854,8 +859,8 @@ static Void local makeStackEntry ( ScriptInfo* ent, String iname )
         );
    if (!ok) {
       ERRMSG(0) 
-       /* "Can't file source or object+interface for module \"%s\"", */
-         "Can't file source for module \"%s\"",
+         "Can't find source or object+interface for module \"%s\"",
+         /* "Can't find source for module \"%s\"", */
          iname
       EEND;
    }
@@ -865,11 +870,10 @@ static Void local makeStackEntry ( ScriptInfo* ent, String iname )
    /* Load objects in preference to sources if both are available */
    /* 11 Oct 99: disable object loading in the interim.
       Will probably only reinstate when HEP becomes available.
+   */
    fromObj = sAvail
                 ? (oAvail && iAvail && timeEarlier(sTime,oTime))
                 : TRUE;
-   */
-   fromObj = FALSE;
 
    /* ToDo: namesUpto overflow */
    ent->modName     = strCopy(iname);
@@ -1198,7 +1202,13 @@ if (numScripts==namesUpto) ppSmStack( "readscripts-final") ;
 
     { Int  m     = namesUpto-1;
       Text mtext = findText(scriptInfo[m].modName);
-      setCurrModule(mtext);
+      /* Commented out till we understand what
+       * this is trying to do.
+       * Problem, you cant find a module till later.
+       */
+#if 0
+       setCurrModule(findModule(mtext)); 
+#endif
       evalModule = mtext;
     }
 
@@ -1338,7 +1348,7 @@ static Void local evaluator() {        /* evaluate expr and print value    */
 #endif
 
 #if 1
-    if (typeMatches(type,ap(typeIO,typeUnit))) {
+    if (isProgType(ks,bd)) {
         inputExpr = ap(nameRunIO,inputExpr);
         evalExp();
         Putchar('\n');
@@ -1413,18 +1423,19 @@ static Void local showtype() {         /* print type of expression (if any)*/
 }
 
 
-static Void local browseit(mod,t)
+static Void local browseit(mod,t,all)
 Module mod; 
-String t; {
-#if 0
-  /* AJG: DISABLED FOR NOW */
+String t;
+Bool all; {
     if (nonNull(mod)) {
        Cell cs;
-       Printf("module %s where\n",textToStr(module(mod).text));
+       if (nonNull(t))
+           Printf("module %s where\n",textToStr(module(mod).text));
        for (cs = module(mod).names; nonNull(cs); cs=tl(cs)) {
            Name nm = hd(cs);
-           /* only look at things defined in this module */
-           if (name(nm).mod == mod) {
+           /* only look at things defined in this module,
+              unless `all' flag is set */
+           if (all || name(nm).mod == mod) {
                /* unwanted artifacts, like lambda lifted values,
                   are in the list of names, but have no types */
                if (nonNull(name(nm).type)) {
@@ -1438,9 +1449,6 @@ String t; {
                    } else if (isSfun(nm)) {
                        Printf("  -- selector function");
                    }
-                   if (name(nm).primDef) {
-                       Printf("   -- primitive");
-                   }
                    Printf("\n");
                }
            }
@@ -1450,26 +1458,28 @@ String t; {
        Printf("Unknown module %s\n",t);
       }
     }
-#endif
 }
 
 static Void local browse() {            /* browse modules                  */
     Int    count = 0;                   /* or give menu of commands        */
     String s;
+    Bool all = FALSE;
 
     setCurrModule(findEvalModule());
     startNewScript(0);                  /* for recovery of storage         */
-    for (; (s=readFilename())!=0; count++) {
-       browseit(findModule(findText(s)),s);
-    }
+    for (; (s=readFilename())!=0; count++)
+       if (strcmp(s,"all") == 0) {
+           all = TRUE;
+           --count;
+       } else
+           browseit(findModule(findText(s)),s,all);
     if (count == 0) {
-       whatScripts();
+       browseit(findEvalModule(),NULL,all);
     }
 }
 
 #if EXPLAIN_INSTANCE_RESOLUTION
 static Void local xplain() {         /* print type of expression (if any)*/
-    Cell type;
     Cell d;
     Bool sir = showInstRes;
 
@@ -1581,7 +1591,6 @@ static Void local dumpStg( void ) {       /* print STG stuff                 */
 
         if (isNull(n) && whatIs(v)==STGVAR) {
            Printf ( "\n{- `%s' has no nametable entry -}\n", s );
-           Printf ( "{- stgSize of body is %d -}\n\n", stgSize(stgVarBody(v)));
            printStg(stderr, v );
         } else
         if (isNull(n)) {
@@ -1594,8 +1603,6 @@ static Void local dumpStg( void ) {       /* print STG stuff                 */
            Printf ( "Doesn't have a STG tree: %s\n", s );
         } else {
            Printf ( "\n{- stgVar of `%s' is id%d -}\n", s, -name(n).stgVar);
-           Printf ( "{- stgSize of body is %d -}\n\n", 
-                    stgSize(stgVarBody(name(n).stgVar)));
            printStg(stderr, name(n).stgVar);
         }
     }
@@ -1709,8 +1716,7 @@ Text t; {
             Printf(" => ");
         }
         printPred(stdout,cclass(cl).head);
-#if 0
-       /* AJG: commented out for now */
+
        if (nonNull(cclass(cl).fds)) {
            List   fds = cclass(cl).fds;
            String pre = " | ";
@@ -1720,12 +1726,15 @@ Text t; {
                pre = ", ";
            }
        }
-#endif
+
         if (nonNull(cclass(cl).members)) {
             List ms = cclass(cl).members;
             Printf(" where");
             do {
-                Type t = monotypeOf(name(hd(ms)).type);
+               Type t = name(hd(ms)).type;
+                if (isPolyType(t)) {
+                   t = monotypeOf(t);
+               }
                 Printf("\n  ");
                 printExp(stdout,hd(ms));
                 Printf(" :: ");
@@ -1898,6 +1907,9 @@ String argv[]; {
         forHelp();
     }
 
+    /* initialize calls startupHaskell, which trashes our signal handlers */
+    breakOn(TRUE);
+
     for (;;) {
         Command cmd;
         everybody(RESET);               /* reset to sensible initial state */
@@ -1997,7 +2009,12 @@ static Int    charCount;
 Void setGoal(what, t)                  /* Set goal for what to be t        */
 String what;
 Target t; {
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     currTarget = (t?t:1);
     aiming     = TRUE;
     if (useDots) {
@@ -2013,7 +2030,12 @@ Target t; {
 
 Void soFar(t)                          /* Indicate progress towards goal   */
 Target t; {                            /* has now reached t                */
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     if (useDots) {
         Int newPos = (Int)((maxPos * ((long)t))/currTarget);
 
@@ -2031,7 +2053,12 @@ Target t; {                            /* has now reached t                */
 }
 
 Void done() {                          /* Goal has now been achieved       */
-    if (quiet) return;
+    if (quiet)
+      return;
+#if EXPLAIN_INSTANCE_RESOLUTION
+    if (showInstRes)
+      return;
+#endif
     if (useDots) {
         while (maxPos>currPos++)
             Putchar('.');
@@ -2342,7 +2369,6 @@ Int what; {                     /* system to respond as appropriate ...    */
     typeChecker(what);
     compiler(what);   
     codegen(what);
-    optimiser(what);
 }
 
 /* --------------------------------------------------------------------------