[project @ 2000-03-13 10:53:55 by simonmar]
[ghc-hetmet.git] / ghc / rts / Assembler.c
index 004321e..dec5bd9 100644 (file)
@@ -5,8 +5,8 @@
  * Copyright (c) 1994-1998.
  *
  * $RCSfile: Assembler.c,v $
- * $Revision: 1.19 $
- * $Date: 1999/11/29 18:59:40 $
+ * $Revision: 1.22 $
+ * $Date: 1999/12/07 11:49:09 $
  *
  * This module provides functions to construct BCOs and other closures
  * required by the bytecode compiler.
@@ -79,25 +79,33 @@ typedef struct {
 
 #define Queue Instrs
 #define Type  StgWord8
+#define MAKE_findIn 0
 #include "QueueTemplate.h"
+#undef MAKE_findIn
 #undef Type
 #undef Queue
 
 #define Queue Ptrs
 #define Type  AsmObject
+#define MAKE_findIn 0
 #include "QueueTemplate.h"
+#undef MAKE_findIn
 #undef Type
 #undef Queue
 
 #define Queue Refs
 #define Type  AsmRef
+#define MAKE_findIn 0
 #include "QueueTemplate.h"
+#undef MAKE_findIn
 #undef Type
 #undef Queue
 
 #define Queue NonPtrs
 #define Type  StgWord
+#define MAKE_findIn 1
 #include "QueueTemplate.h"
+#undef MAKE_findIn
 #undef Type
 #undef Queue
 
@@ -146,8 +154,6 @@ struct AsmBCO_ {
     /* abstract machine ("executed" during compilation) */
     AsmSp    sp;          /* stack ptr */
     AsmSp    max_sp;
-    StgWord  hp;          /* heap ptr  */
-    StgWord  max_hp;
     Instr    lastOpc;
 };
 
@@ -262,26 +268,6 @@ static StgClosure* asmAlloc( nat size )
     return o;
 }
 
-static void grabHpUpd( AsmBCO bco, nat size )
-{
-    /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
-    ASSERT( size >= MIN_UPD_SIZE + sizeofW(StgHeader) );
-    bco->hp += size;
-}
-
-static void grabHpNonUpd( AsmBCO bco, nat size )
-{
-    /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
-    ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
-    bco->hp += size;
-}
-
-static void resetHp( AsmBCO bco, nat hp )
-{
-    bco->max_hp = stg_max(bco->hp,bco->max_hp);
-    bco->hp     = hp;
-}
-
 static void setSp( AsmBCO bco, AsmSp sp )
 {
     bco->max_sp = stg_max(bco->sp,bco->max_sp);
@@ -377,7 +363,6 @@ AsmBCO asmBeginBCO( int /*StgExpr*/ e )
 
     bco->stgexpr = e;
     bco->max_sp = bco->sp = 0;
-    bco->max_hp = bco->hp = 0;
     bco->lastOpc = i_INTERNAL_ERROR;
     return bco;
 }
@@ -400,7 +385,6 @@ void asmEndBCO( AsmBCO bco )
     {
         nat j = 0;
         bco->max_sp = stg_max(bco->sp,bco->max_sp);
-        bco->max_hp = stg_max(bco->hp,bco->max_hp);
 
         ASSERT(bco->max_sp <= 65535);
         if (bco->max_sp <= 255) {
@@ -467,6 +451,11 @@ static void asmWord( AsmBCO bco, StgWord i )
     insertNonPtrs( &bco->nps, i );
 }
 
+static int asmFindInNonPtrs ( AsmBCO bco, StgWord i )
+{
+   return findInNonPtrs ( &bco->nps, i );
+}
+
 #define asmWords(bco,ty,x)                               \
     {                                                    \
         union { ty a; AsmWord b[sizeofW(ty)]; } p;       \
@@ -764,6 +753,14 @@ static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
       emiti_16(bco,i_RETADDR_big,arg1);
 }
 
+static void emit_i_ALLOC_CONSTR ( AsmBCO bco, int arg1 )
+{
+   ASSERT(arg1 >= 0);
+   if (arg1 < 256)
+      emiti_8 (bco,i_ALLOC_CONSTR,    arg1); else
+      emiti_16(bco,i_ALLOC_CONSTR_big,arg1);
+}
+
 
 /* --------------------------------------------------------------------------
  * Arg checks.
@@ -780,8 +777,6 @@ void   asmEndArgCheck   ( AsmBCO bco, AsmSp last_arg )
     nat args = bco->sp - last_arg;
     if (args != 0) { /* optimisation */
         emiti_8(bco,i_ARG_CHECK,args);
-        grabHpNonUpd(bco,PAP_sizeW(args-1));
-        resetHp(bco,0);
     }
 }
 
@@ -890,32 +885,25 @@ AsmVar asmBox( AsmBCO bco, AsmRep rep )
     switch (rep) {
     case CHAR_REP:
             emiti_(bco,i_PACK_CHAR);
-            grabHpNonUpd(bco,Czh_sizeW);
             break;
     case INT_REP:
             emiti_(bco,i_PACK_INT);
-            grabHpNonUpd(bco,Izh_sizeW);
             break;
     case THREADID_REP:
     case WORD_REP:
             emiti_(bco,i_PACK_WORD);
-            grabHpNonUpd(bco,Wzh_sizeW);
             break;
     case ADDR_REP:
             emiti_(bco,i_PACK_ADDR);
-            grabHpNonUpd(bco,Azh_sizeW);
             break;
     case FLOAT_REP:
             emiti_(bco,i_PACK_FLOAT);
-            grabHpNonUpd(bco,Fzh_sizeW);
             break;
     case DOUBLE_REP:
             emiti_(bco,i_PACK_DOUBLE);
-            grabHpNonUpd(bco,Dzh_sizeW);
             break;
     case STABLE_REP:
             emiti_(bco,i_PACK_STABLE);
-            grabHpNonUpd(bco,Stablezh_sizeW);
             break;
 
     default:
@@ -1560,11 +1548,21 @@ AsmBCO asm_BCO_takeMVar ( void )
 
 AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info )
 {
+    int i;
     ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
-    emiti_8(bco,i_ALLOC_CONSTR,bco->nps.len);
-    asmWords(bco,AsmInfo,info);
+
+    /* Look in this bco's collection of nonpointers (literals)
+       to see if the itbl pointer is already there.  If so, re-use it. */
+    i = asmFindInNonPtrs ( bco, (StgWord)info );
+
+    if (i == -1) {
+       emit_i_ALLOC_CONSTR(bco,bco->nps.len);
+       asmWords(bco,AsmInfo,info);
+    } else {
+       emit_i_ALLOC_CONSTR(bco,i);
+    }
+
     incSp(bco, sizeofW(StgClosurePtr));
-    grabHpNonUpd(bco,sizeW_fromITBL(info));
     return bco->sp;
 }
 
@@ -1598,7 +1596,6 @@ AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
 {
     emiti_8(bco,i_ALLOC_AP,words);
     incSp(bco, sizeofW(StgPtr));
-    grabHpUpd(bco,AP_sizeW(words));
     return bco->sp;
 }