[project @ 2000-12-07 08:22:53 by simonpj]
[ghc-hetmet.git] / ghc / interpreter / derive.c
index d4dcdbd..fccff4f 100644 (file)
@@ -2,77 +2,24 @@
 /* --------------------------------------------------------------------------
  * Deriving
  *
- * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
- * All rights reserved. See NOTICE for details and conditions of use etc...
- * Hugs version 1.4, December 1997
+ * 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: derive.c,v $
- * $Revision: 1.5 $
- * $Date: 1999/03/09 14:51:06 $
+ * $Revision: 1.15 $
+ * $Date: 2000/04/27 16:35:29 $
  * ------------------------------------------------------------------------*/
 
-#include "prelude.h"
+#include "hugsbasictypes.h"
 #include "storage.h"
-#include "backend.h"
 #include "connect.h"
 #include "errors.h"
+
+#include "Rts.h"       /* to make StgPtr visible in Assembler.h */
 #include "Assembler.h"
-#include "link.h"
-
-#if 0
-static Cell varTrue;
-static Cell varFalse;
-#if DERIVE_ORD
-static Cell varCompAux;                /* auxiliary function for compares */
-static Cell varCompare;
-static Cell varEQ;
-#endif
-#if DERIVE_IX
-static Cell varRangeSize;              /* calculate size of index range   */
-static Cell varInRange;
-static Cell varRange;
-static Cell varIndex;
-static Cell varMult; 
-static Cell qvarPlus;
-static Cell varMap;
-static Cell qvarMinus;
-static Cell varError;
-#endif
-#if DERIVE_ENUM
-static Cell varToEnum;
-static Cell varFromEnum; 
-static Cell varEnumFromTo;    
-static Cell varEnumFromThenTo;  
-#endif
-#if DERIVE_BOUNDED
-static Cell varMinBound;
-static Cell varMaxBound;
-#endif
-#if DERIVE_SHOW
-       Cell conCons;
-static Cell varShowField;              /* display single field            */
-static Cell varShowParen;              /* wrap with parens                */
-static Cell varCompose;                /* function composition            */
-static Cell varShowsPrec;
-static Cell varLe;
-#endif                                 
-#if DERIVE_READ                        
-static Cell varReadField;              /* read single field               */
-static Cell varReadParen;              /* unwrap from parens              */
-static Cell varLex;                    /* lexer                           */
-static Cell varReadsPrec;
-static Cell varGt;
-#endif                                 
-#if DERIVE_SHOW || DERIVE_READ         
-static Cell varAppend;                 /* list append                     */
-#endif                                 
-#if DERIVE_EQ || DERIVE_IX             
-static Cell varAnd;                    /* built-in logical connectives    */
-#endif
-#if DERIVE_EQ || DERIVE_ORD            
-static Cell varEq;
-#endif
-#endif /* 0 */
 
 List cfunSfuns;                        /* List of (Cfun,[SelectorVar])    */
 
@@ -80,32 +27,25 @@ List cfunSfuns;                        /* List of (Cfun,[SelectorVar])    */
  * local function prototypes:
  * ------------------------------------------------------------------------*/
 
-static List  local getDiVars            Args((Int));
-static Cell  local mkBind               Args((String,List));
-static Cell  local mkVarAlts            Args((Int,Cell));
-
-#if DERIVE_EQ || DERIVE_ORD
-static List  local makeDPats2           Args((Cell,Int));
-#endif
-#if DERIVE_ORD || DERIVE_ENUM || DERIVE_IX || DERIVE_BOUNDED
-static Bool  local isEnumType           Args((Tycon));
-#endif
-
-static Pair   local mkAltEq             Args((Int,List));
-static Pair   local mkAltOrd            Args((Int,List));
-static Cell   local prodRange           Args((Int,List,Cell,Cell,Cell));
-static Cell   local prodIndex           Args((Int,List,Cell,Cell,Cell));
-static Cell   local prodInRange         Args((Int,List,Cell,Cell,Cell));
-static List   local mkIxBinds           Args((Int,Cell,Int));
-static Cell   local mkAltShow           Args((Int,Cell,Int));
-static Cell   local showsPrecRhs        Args((Cell,Cell,Int));
-static Cell   local mkReadCon           Args((Name,Cell,Cell));
-static Cell   local mkReadPrefix        Args((Cell));
-static Cell   local mkReadInfix         Args((Cell));
-static Cell   local mkReadTuple         Args((Cell));
-static Cell   local mkReadRecord        Args((Cell,List));
-static List   local mkBndBinds          Args((Int,Cell,Int));
-
+static List   local getDiVars           ( Int );
+static Cell   local mkBind              ( String,List );
+static Cell   local mkVarAlts           ( Int,Cell );
+static List   local makeDPats2          ( Cell,Int );
+static Bool   local isEnumType          ( Tycon );
+static Pair   local mkAltEq             ( Int,List );
+static Pair   local mkAltOrd            ( Int,List );
+static Cell   local prodRange           ( Int,List,Cell,Cell,Cell );
+static Cell   local prodIndex           ( Int,List,Cell,Cell,Cell );
+static Cell   local prodInRange         ( Int,List,Cell,Cell,Cell );
+static List   local mkIxBinds           ( Int,Cell,Int );
+static Cell   local mkAltShow           ( Int,Cell,Int );
+static Cell   local showsPrecRhs        ( Cell,Cell,Int );
+static Cell   local mkReadCon           ( Name,Cell,Cell );
+static Cell   local mkReadPrefix        ( Cell );
+static Cell   local mkReadInfix         ( Cell );
+static Cell   local mkReadTuple         ( Cell );
+static Cell   local mkReadRecord        ( Cell,List );
+static List   local mkBndBinds          ( Int,Cell,Int );
 
 
 /* --------------------------------------------------------------------------
@@ -135,7 +75,6 @@ Cell r; {
     return singleton(pair(NIL,pair(mkInt(line),r)));
 }
 
-#if DERIVE_EQ || DERIVE_ORD
 static List local makeDPats2(h,n)       /* generate pattern list           */
 Cell h;                                 /* by putting two new patterns with*/
 Int  n; {                               /* head h and new var components   */
@@ -156,9 +95,7 @@ Int  n; {                               /* head h and new var components   */
     }
     return cons(p,vs);
 }
-#endif
 
-#if DERIVE_ORD || DERIVE_ENUM || DERIVE_IX || DERIVE_BOUNDED
 static Bool local isEnumType(t) /* Determine whether t is an enumeration   */
 Tycon t; {                      /* type (i.e. all constructors arity == 0) */
     if (isTycon(t) && (tycon(t).what==DATATYPE || tycon(t).what==NEWTYPE)) {
@@ -173,7 +110,7 @@ Tycon t; {                      /* type (i.e. all constructors arity == 0) */
     }
     return FALSE;
 }
-#endif
+
 
 /* --------------------------------------------------------------------------
  * Given a datatype:   data T a b = A a b | B Int | C  deriving (Eq, Ord)
@@ -193,9 +130,7 @@ Tycon t; {                      /* type (i.e. all constructors arity == 0) */
  * constructors in the datatype definition.
  * ------------------------------------------------------------------------*/
 
-#if DERIVE_EQ
-
-static Pair  local mkAltEq              Args((Int,List));
+static Pair  local mkAltEq              ( Int,List );
 
 List deriveEq(t)                        /* generate binding for derived == */
 Type t; {                               /* for some TUPLE or DATATYPE t    */
@@ -233,11 +168,9 @@ List pats; {                            /* arguments                       */
     }
     return pair(pats,pair(mkInt(line),e));
 }
-#endif /* DERIVE_EQ */
 
-#if DERIVE_ORD
 
-static Pair  local mkAltOrd             Args((Int,List));
+static Pair  local mkAltOrd             ( Int,List );
 
 List deriveOrd(t)                       /* make binding for derived compare*/
 Type t; {                               /* for some TUPLE or DATATYPE t    */
@@ -296,14 +229,12 @@ List pats; {                            /* arguments                       */
 
     return pair(pats,pair(mkInt(line),e));
 }
-#endif /* DERIVE_ORD */
 
 
 /* --------------------------------------------------------------------------
  * Deriving Ix and Enum:
  * ------------------------------------------------------------------------*/
 
-#if DERIVE_ENUM
 List deriveEnum(t)              /* Construct definition of enumeration     */
 Tycon t; {
     Int  l     = tycon(t).line;
@@ -324,26 +255,15 @@ Tycon t; {
     implementTagToCon(t);
     return cons(mkBind("toEnum",      mkVarAlts(l,tycon(t).tagToCon)),
            cons(mkBind("fromEnum",    mkVarAlts(l,tycon(t).conToTag)),
-           cons(mkBind("enumFrom",    singleton(pair(singleton(x),  
-                                        pair(mkInt(l),
-                                        ap2(nameFromTo,x,last))))),
-           /* default instance of enumFromTo is good */
-           cons(mkBind("enumFromThen",singleton(pair(doubleton(x,y),
-                                        pair(mkInt(l),
-                                        ap3(nameFromThenTo,x,y,
-                                        ap(COND,triple(ap2(nameLe,x,y),
-                                        last,first))))))),
-           /* default instance of enumFromThenTo is good */
-           NIL))));
+           NIL));
 }
-#endif /* DERIVE_ENUM */
 
-#if DERIVE_IX
-static List  local mkIxBindsEnum        Args((Tycon));
-static List  local mkIxBinds            Args((Int,Cell,Int));
-static Cell  local prodRange            Args((Int,List,Cell,Cell,Cell));
-static Cell  local prodIndex            Args((Int,List,Cell,Cell,Cell));
-static Cell  local prodInRange          Args((Int,List,Cell,Cell,Cell));
+
+static List  local mkIxBindsEnum        ( Tycon );
+static List  local mkIxBinds            ( Int,Cell,Int );
+static Cell  local prodRange            ( Int,List,Cell,Cell,Cell );
+static Cell  local prodIndex            ( Int,List,Cell,Cell,Cell );
+static Cell  local prodInRange          ( Int,List,Cell,Cell,Cell );
 
 List deriveIx(t)                /* Construct definition of indexing        */
 Tycon t; {
@@ -410,19 +330,22 @@ Int  n; {
     Cell ls   = h;
     Cell us   = h;
     Cell is   = h;
+    Cell js   = h;
     Cell pr   = NIL;
     Cell pats = NIL;
+    
     Int  i;
 
     for (i=0; i<n; ++i, vs=tl(vs)) {    /* build three patterns for values */
         ls = ap(ls,hd(vs));             /* of the datatype concerned       */
         us = ap(us,hd(vs=tl(vs)));
         is = ap(is,hd(vs=tl(vs)));
+       js = ap(js,hd(vs));             /* ... and one expression          */
     }
     pr   = ap2(mkTuple(2),ls,us);       /* Build (ls,us)                   */
     pats = cons(pr,cons(is,NIL));       /* Build [(ls,us),is]              */
 
-    return cons(prodRange(line,singleton(pr),ls,us,is),
+    return cons(prodRange(line,singleton(pr),ls,us,js),
            cons(prodIndex(line,pats,ls,us,is),
            cons(prodInRange(line,pats,ls,us,is),
            NIL)));
@@ -489,7 +412,6 @@ Cell ls, us, is; {
     e = singleton(pair(pats,pair(mkInt(line),e)));
     return mkBind("inRange",e);
 }
-#endif /* DERIVE_IX */
 
 
 /* --------------------------------------------------------------------------
@@ -615,10 +537,11 @@ Int  a; {
             if (defaultSyntax(name(h).text)==APPLIC) {
                 rhs = ap(showsBQ,
                          ap2(nameComp,
-                             ap(nameApp,mkStr(name(h).text)),
+                            ap(nameApp,mkStr(fixLitText(name(h).text))),
                              ap(showsBQ,rhs)));
             } else {
-                rhs = ap2(nameComp,ap(nameApp,mkStr(name(h).text)),rhs);
+               rhs = ap2(nameComp,
+                         ap(nameApp,mkStr(fixLitText(name(h).text))),rhs);
             }
 
             rhs = ap2(nameComp,
@@ -920,8 +843,6 @@ List fs; {
  * Deriving Bounded:
  * ------------------------------------------------------------------------*/
 
-#if DERIVE_BOUNDED
-
 List deriveBounded(t)             /* construct definition of bounds        */
 Tycon t; {
     if (isEnumType(t)) {
@@ -960,8 +881,6 @@ Int  n; {
             cons(mkBind("maxBound",mkVarAlts(line,maxB)),
              NIL));
 }
-#endif /* DERIVE_BOUNDED */
-
 
 
 /* --------------------------------------------------------------------------
@@ -993,14 +912,13 @@ Tycon t; {
             alts = cons(mkStgCaseAlt(c,vs,tag),alts);
         }
 
-        name(nm).line   = tycon(t).line;
-        name(nm).type   = conToTagType(t);
-        name(nm).arity  = 1;
-        name(nm).stgVar = mkStgVar(mkStgLambda(singleton(v),mkStgCase(v,alts)),
-                                   NIL);
+        name(nm).line    = tycon(t).line;
+        name(nm).type    = conToTagType(t);
+        name(nm).arity   = 1;
+        name(nm).closure = mkStgVar(mkStgLambda(singleton(v),mkStgCase(v,alts)),
+                                    NIL);
         tycon(t).conToTag = nm;
-        /* hack to make it print out */
-        stgGlobals = cons(pair(nm,name(nm).stgVar),stgGlobals); 
+        addToCodeList ( currentModule, nm );
     }
 }
 
@@ -1008,7 +926,6 @@ Tycon t; {
 Void implementTagToCon(t)
 Tycon t; {
     if (isNull(tycon(t).tagToCon)) {
-        String etxt;
         String tyconname;
         List   cs;
         Name   nm;
@@ -1019,6 +936,7 @@ Tycon t; {
         StgVar bind2;
         StgVar bind3;
         List   alts;
+        char   etxt[200];
 
         assert(nameMkA);
         assert(nameUnpackString);
@@ -1027,8 +945,9 @@ Tycon t; {
                               || tycon(t).what==NEWTYPE));
 
         tyconname  = textToStr(tycon(t).text);
-        etxt       = malloc(100+strlen(tyconname));
-        assert(etxt);
+        if (strlen(tyconname) > 100) 
+           internal("implementTagToCon: tycon name too long");
+
         sprintf(etxt, 
                 "out-of-range arg for `toEnum' "
                 "in derived `instance Enum %s'", 
@@ -1061,25 +980,23 @@ Tycon t; {
             alts = cons(mkStgPrimAlt(singleton(pat),c),alts);
         }
 
-        name(nm).line   = tycon(t).line;
-        name(nm).type   = tagToConType(t);
-        name(nm).arity  = 1;
-        name(nm).stgVar = mkStgVar(
-                            mkStgLambda(
-                              singleton(v1),
-                              mkStgCase(
-                                v1,
-                                singleton(
-                                  mkStgCaseAlt(
-                                    nameMkI,
-                                    singleton(v2),
-                                    mkStgPrimCase(v2,alts))))),
-                            NIL
-                          );
+        name(nm).line    = tycon(t).line;
+        name(nm).type    = tagToConType(t);
+        name(nm).arity   = 1;
+        name(nm).closure = mkStgVar(
+                             mkStgLambda(
+                               singleton(v1),
+                               mkStgCase(
+                                 v1,
+                                 singleton(
+                                   mkStgCaseAlt(
+                                     nameMkI,
+                                     singleton(v2),
+                                     mkStgPrimCase(v2,alts))))),
+                             NIL
+                           );
         tycon(t).tagToCon = nm;
-        /* hack to make it print out */
-        stgGlobals = cons(pair(nm,name(nm).stgVar),stgGlobals); 
-        if (etxt) free(etxt);
+        addToCodeList ( currentModule, nm );
     }
 }
 
@@ -1090,132 +1007,20 @@ Tycon t; {
 
 Void deriveControl(what)
 Int what; {
-    Text textPrelude = findText("Prelude");
     switch (what) {
-        case INSTALL :
-#if 0
-                varTrue           = mkQVar(textPrelude,findText("True"));
-                varFalse          = mkQVar(textPrelude,findText("False"));
-#if DERIVE_ORD
-                varCompAux        = mkQVar(textPrelude,findText("primCompAux"));
-                varCompare        = mkQVar(textPrelude,findText("compare"));
-                varEQ             = mkQVar(textPrelude,findText("EQ"));
-#endif
-#if DERIVE_IX   
-                varRangeSize      = mkQVar(textPrelude,findText("rangeSize"));
-                varInRange        = mkQVar(textPrelude,findText("inRange"));
-                varRange          = mkQVar(textPrelude,findText("range"));
-                varIndex          = mkQVar(textPrelude,findText("index"));
-                varMult           = mkQVar(textPrelude,findText("*"));
-                qvarPlus          = mkQVar(textPrelude,findText("+"));
-                varMap            = mkQVar(textPrelude,findText("map"));
-                qvarMinus         = mkQVar(textPrelude,findText("-"));
-                varError          = mkQVar(textPrelude,findText("error"));
-#endif
-#if DERIVE_ENUM
-                varToEnum         = mkQVar(textPrelude,findText("toEnum"));
-                varFromEnum       = mkQVar(textPrelude,findText("fromEnum"));  
-                varEnumFromTo     = mkQVar(textPrelude,findText("enumFromTo"));
-                varEnumFromThenTo = mkQVar(textPrelude,findText("enumFromThenTo"));
-#endif
-#if DERIVE_BOUNDED
-                varMinBound       = mkQVar(textPrelude,findText("minBound"));
-                varMaxBound       = mkQVar(textPrelude,findText("maxBound"));
-#endif
-#if DERIVE_SHOW 
-                conCons           = mkQCon(textPrelude,findText(":"));
-                varShowField      = mkQVar(textPrelude,findText("primShowField"));
-                varShowParen      = mkQVar(textPrelude,findText("showParen"));
-                varCompose        = mkQVar(textPrelude,findText("."));
-                varShowsPrec      = mkQVar(textPrelude,findText("showsPrec"));
-                varLe             = mkQVar(textPrelude,findText("<="));
-#endif          
-#if DERIVE_READ
-                varReadField      = mkQVar(textPrelude,findText("primReadField"));
-                varReadParen      = mkQVar(textPrelude,findText("readParen"));
-                varLex            = mkQVar(textPrelude,findText("lex"));
-                varReadsPrec      = mkQVar(textPrelude,findText("readsPrec"));
-                varGt             = mkQVar(textPrelude,findText(">"));
-#endif
-#if DERIVE_SHOW || DERIVE_READ         
-                varAppend         = mkQVar(textPrelude,findText("++"));
-#endif                                 
-#if DERIVE_EQ || DERIVE_IX             
-                varAnd            = mkQVar(textPrelude,findText("&&"));
-#endif
-#if DERIVE_EQ || DERIVE_ORD            
-                varEq             = mkQVar(textPrelude,findText("=="));
-#endif
-#endif /* 0 */
-                /* deliberate fall through */
+        case PREPREL :
         case RESET   : 
                 diVars      = NIL;
                 diNum       = 0;
-#if DERIVE_SHOW | DERIVE_READ
                 cfunSfuns   = NIL;
-#endif
                 break;
 
         case MARK    : 
                 mark(diVars);
-#if DERIVE_SHOW | DERIVE_READ
                 mark(cfunSfuns);
-#endif
-#if 0
-                mark(varTrue);        
-                mark(varFalse);        
-#if DERIVE_ORD
-                mark(varCompAux);        
-                mark(varCompare);        
-                mark(varEQ);        
-#endif                            
-#if DERIVE_IX                     
-                mark(varRangeSize);      
-                mark(varInRange);        
-                mark(varRange);          
-                mark(varIndex);          
-                mark(varMult);           
-                mark(qvarPlus);           
-                mark(varMap);           
-                mark(qvarMinus);           
-                mark(varError);           
-#endif                            
-#if DERIVE_ENUM                   
-                mark(varToEnum); 
-                mark(varFromEnum);   
-                mark(varEnumFromTo);     
-                mark(varEnumFromThenTo);   
-#endif                            
-#if DERIVE_BOUNDED                
-                mark(varMinBound);       
-                mark(varMaxBound);       
-#endif                            
-#if DERIVE_SHOW                   
-                mark(conCons);
-                mark(varShowField);      
-                mark(varShowParen);      
-                mark(varCompose);        
-                mark(varShowsPrec);      
-                mark(varLe);             
-#endif                            
-#if DERIVE_READ                   
-                mark(varReadField);      
-                mark(varReadParen);      
-                mark(varLex);            
-                mark(varReadsPrec);      
-                mark(varGt);             
-#endif                            
-#if DERIVE_SHOW || DERIVE_READ    
-                mark(varAppend);         
-#endif                            
-#if DERIVE_EQ || DERIVE_IX        
-                mark(varAnd);            
-#endif                            
-#if DERIVE_EQ || DERIVE_ORD       
-                mark(varEq);             
-#endif
-#endif /* 0 */
                 break;
+
+       case POSTPREL: break;
     }
 }