[project @ 1999-03-01 17:41:50 by simonm]
[ghc-hetmet.git] / ghc / interpreter / codegen.c
index 9bc719e..5ef8e28 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: hugs-c; -*- */
+
 /* --------------------------------------------------------------------------
  * Code generator
  *
@@ -7,20 +7,20 @@
  * Hugs version 1.4, December 1997
  *
  * $RCSfile: codegen.c,v $
- * $Revision: 1.2 $
- * $Date: 1998/12/02 13:21:59 $
+ * $Revision: 1.4 $
+ * $Date: 1999/03/01 14:46:42 $
  * ------------------------------------------------------------------------*/
 
 #include "prelude.h"
 #include "storage.h"
+#include "backend.h"
 #include "connect.h"
 #include "errors.h"
-#include "stg.h"
 #include "Assembler.h"
-#include "lift.h"
 #include "link.h"
-#include "pp.h"
-#include "codegen.h"
+
+#include "Rts.h"    /* IF_DEBUG */
+#include "RtsFlags.h"
 
 /* --------------------------------------------------------------------------
  * Local function prototypes:
@@ -42,7 +42,7 @@ static Void  cgExpr        ( AsmBCO bco, AsmSp root, StgExpr e );
              
 static AsmBCO cgAlts       ( AsmSp root, AsmSp sp, List alts );
 static void   testPrimPats ( AsmBCO bco, AsmSp root, List pats, StgExpr e );
-static void   cgPrimAlt    ( AsmBCO bco, AsmSp root, List vs, StgExpr e );
+//static void   cgPrimAlt    ( AsmBCO bco, AsmSp root, List vs, StgExpr e );
 static AsmBCO cgLambda     ( StgExpr e );
 static AsmBCO cgRhs        ( StgRhs rhs );
 static void   beginTop     ( StgVar v );
@@ -105,7 +105,11 @@ static void cgBind( AsmBCO bco, StgVar v )
 static Void pushVar( AsmBCO bco, StgVar v )
 {
     Cell info = stgVarInfo(v);
-    assert(isStgVar(v));
+    //    if (!isStgVar(v)) {
+    //printf("\n\nprefail\n");
+    //print(v,1000);
+       assert(isStgVar(v));
+    //}
     if (isPtr(info)) {
         asmClosure(bco,ptrOf(info));
     } else if (isInt(info)) {
@@ -171,14 +175,14 @@ static Void pushAtom( AsmBCO bco, StgAtom e )
 
 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
 {
-    AsmBCO bco = asmBeginContinuation(sp);
+    AsmBCO bco = asmBeginContinuation(sp,alts);
     /* ppStgAlts(alts); */
     for(; nonNull(alts); alts=tl(alts)) {
         StgCaseAlt alt  = hd(alts);
         StgPat     pat  = stgCaseAltPat(alt);
         StgExpr    body = stgCaseAltBody(alt);
         if (isDefaultPat(pat)) {
-            AsmSp      begin = asmBeginAlt(bco);
+           //AsmSp      begin = asmBeginAlt(bco);
             cgBind(bco,pat);
             cgExpr(bco,root,body);
             asmEndContinuation(bco);
@@ -225,16 +229,19 @@ static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
     }
 }
 
+#if 0  /* appears to be unused */
 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
 {
     assert(0); /* ToDo: test for patterns */
     map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
     cgExpr(bco,root,e);
 }
+#endif
+
 
 static AsmBCO cgLambda( StgExpr e )
 {
-    AsmBCO bco = asmBeginBCO();
+    AsmBCO bco = asmBeginBCO(e);
 
     AsmSp root = asmBeginArgCheck(bco);
     map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
@@ -249,7 +256,7 @@ static AsmBCO cgLambda( StgExpr e )
 
 static AsmBCO cgRhs( StgRhs rhs )
 {
-    AsmBCO bco = asmBeginBCO( );
+    AsmBCO bco = asmBeginBCO(rhs );
 
     AsmSp root = asmBeginArgCheck(bco);
     asmEndArgCheck(bco,root);
@@ -261,8 +268,10 @@ static AsmBCO cgRhs( StgRhs rhs )
     return bco;
 }
 
+
 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
 {
+  //printf("cgExpr:");ppStgExpr(e);printf("\n");
     switch (whatIs(e)) {
     case LETREC:
         {
@@ -390,6 +399,9 @@ static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
     }
 }
 
+void* itblNames[1000];
+int   nItblNames = 0;
+
 /* allocate space for top level variable
  * any change requires a corresponding change in 'build'.
  */
@@ -406,7 +418,23 @@ static Void alloc( AsmBCO bco, StgVar v )
                 pushAtom(bco,hd(args));
                 setPos(v,asmBox(bco,boxingConRep(con)));
             } else {
-                setPos(v,asmAllocCONSTR(bco,stgConInfo(con)));
+
+                void* vv = stgConInfo(con);
+                assert (nItblNames < (1000-2));
+                if (isName(con)) {
+                   itblNames[nItblNames++] = vv;
+                   itblNames[nItblNames++] = textToStr(name(con).text);
+                } else
+                if (isTuple(con)) {
+                   char* cc = malloc(10);
+                   assert(cc);
+                   sprintf(cc, "Tuple%d", tupleOf(con) );
+                   itblNames[nItblNames++] = vv;
+                   itblNames[nItblNames++] = cc;
+                } else
+                assert ( /* cant identify constructor name */ 0 );
+
+                setPos(v,asmAllocCONSTR(bco, vv));
             }
             break;
         }
@@ -426,6 +454,7 @@ static Void build( AsmBCO bco, StgVar v )
 {
     StgRhs rhs = stgVarBody(v);
     assert(isStgVar(v));
+
     switch (whatIs(rhs)) {
     case STGCON:
         {
@@ -500,6 +529,7 @@ static Void build( AsmBCO bco, StgVar v )
  * for each top level variable - this should be simpler!
  * ------------------------------------------------------------------------*/
 
+#if 0   /* appears to be unused */
 static void cgAddVar( AsmObject obj, StgAtom v )
 {
     if (isName(v)) {
@@ -508,6 +538,8 @@ static void cgAddVar( AsmObject obj, StgAtom v )
     assert(isStgVar(v));
     asmAddPtr(obj,getObj(v));
 }
+#endif
+
 
 /* allocate AsmObject for top level variables
  * any change requires a corresponding change in endTop
@@ -520,12 +552,12 @@ static void beginTop( StgVar v )
     switch (whatIs(rhs)) {
     case STGCON:
         {
-            List as = stgConArgs(rhs);
+           //List as = stgConArgs(rhs);
             setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
             break;
         }
     case LAMBDA:
-            setObj(v,asmBeginBCO());
+            setObj(v,asmBeginBCO(rhs));
             break;
     default:
             setObj(v,asmBeginCAF());
@@ -536,7 +568,7 @@ static void beginTop( StgVar v )
 static void endTop( StgVar v )
 {
     StgRhs rhs = stgVarBody(v);
-    ppStgRhs(rhs);
+    //ppStgRhs(rhs);
     switch (whatIs(rhs)) {
     case STGCON:
         {
@@ -594,16 +626,48 @@ static void endTop( StgVar v )
 
 static void zap( StgVar v )
 {
-    stgVarBody(v) = NIL;
+  // ToDo: reinstate
+  //    stgVarBody(v) = NIL;
 }
 
 /* external entry point */
 Void cgBinds( List binds )
 {
+    List b;
+    int i;
+
+    //if (lastModule() != modulePrelude) {
+    //    printf("\n\ncgBinds: before ll\n\n" );
+    //    for (b=binds; nonNull(b); b=tl(b)) {
+    //       printStg ( stdout, hd(b) ); printf("\n\n");
+    //    }
+    //}
+
     binds = liftBinds(binds);
-    mapProc(beginTop,binds);
-    mapProc(endTop,binds);
-    mapProc(zap,binds);
+
+    //if (lastModule() != modulePrelude) {
+    //    printf("\n\ncgBinds: after ll\n\n" );
+    //    for (b=binds; nonNull(b); b=tl(b)) {
+    //       printStg ( stdout, hd(b) ); printf("\n\n");
+    //    }
+    //}
+
+
+    //mapProc(beginTop,binds);
+    for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
+      //printf("beginTop %d\n", i);
+       beginTop(hd(b));
+    }
+
+    //mapProc(endTop,binds);
+    for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
+       endTop(hd(b));
+       //if (lastModule() != modulePrelude) {
+       //    printStg ( stdout, hd(b) ); printf("\n\n");
+       //}
+    }
+
+    //mapProc(zap,binds);
 }
 
 /* --------------------------------------------------------------------------