[project @ 2000-10-16 14:14:20 by simonpj]
[ghc-hetmet.git] / ghc / includes / Assembler.h
index 98c1479..a1e6050 100644 (file)
@@ -1,6 +1,6 @@
-/* -*- mode: hugs-c; -*- */
+
 /* -----------------------------------------------------------------------------
- * $Id: Assembler.h,v 1.4 1999/02/05 16:02:18 simonm Exp $
+ * $Id: Assembler.h,v 1.18 2000/10/09 11:21:41 daan Exp $
  *
  * (c) The GHC Team 1994-1998.
  *
@@ -29,7 +29,7 @@ extern void DEBUG_LoadSymbols( char *name );
 typedef unsigned char   AsmNat8;
 typedef unsigned int    AsmNat;
 typedef signed   int    AsmInt;
-typedef signed long long int AsmInt64;  /* ToDo: not portable!  */
+typedef HsInt64_        AsmInt64;
 typedef unsigned int    AsmWord;
 typedef void*           AsmAddr;
 typedef unsigned char   AsmChar;
@@ -37,33 +37,81 @@ typedef float           AsmFloat;       /* ToDo: not on Alphas! */
 typedef double          AsmDouble;
 typedef char*           AsmString;
 
+typedef int   AsmSp;   /* stack offset                  */
+typedef int   AsmPc;   /* program counter              */
+typedef AsmSp AsmVar;  /* offset of a Var on the stack  */
+
 /* I want to #include this file into the file that defines the
  * functions but I don't want to expose the structures that
  * these types point to.
  * This hack is the best I could think of.  Surely there's a better way?
  */
 #ifdef INSIDE_ASSEMBLER_C
-typedef struct AsmObject_ *AsmObject;
-typedef struct AsmBCO_    *AsmBCO;
-typedef struct AsmCAF_    *AsmCAF;
-typedef struct AsmCon_    *AsmCon;
-typedef StgInfoTable      *AsmInfo;
-typedef StgClosure        *AsmClosure;
-typedef Instr              AsmInstr;
+/* these types are defined in Assembler.c */
+typedef 
+   enum { 
+     Asm_RefNoOp,    /* Pointer which needs no further messing with */
+     Asm_RefObject,   /* Reference to malloc'd AsmCAF/AsmBCO/AsmCon */
+     Asm_RefHugs,          /* Reference to Hugs name or tycon table */
+
+     Asm_NonPtrWord,                          /* A non-pointer word */
+     Asm_Insn8,                                /* One BCO insn byte */
+   }
+   Asm_Kind;
+
+typedef
+   struct {
+      Asm_Kind  kind;
+      StgWord   val;   /* StgWord is allegedly big enough to also hold
+                          a pointer, on all platforms */
+   }
+   Asm_Entity;
+
+
+   struct AsmObject_ {
+      unsigned int magic;
+      struct AsmObject_* next;
+      enum { Asm_BCO, Asm_CAF, Asm_Con } kind;
+      int           sizeEntities;
+      int           usedEntities;
+      Asm_Entity*   entities;
+      StgClosure*   closure;
+
+      int           n_refs;          /* number of ptr words  */
+      int           n_words;         /* number of words      */
+      int           n_insns;         /* number of insn BYTES */
+
+      /* AsmCon specifics */
+      StgInfoTable* itbl;
+
+      /* AsmBCO specifics */
+      int /*StgExpr*/ stgexpr;       /* stg tree for debugging */
+      AsmSp           sp;            /* simulated sp */
+      AsmSp           max_sp;        /* high-tide of sp */
+      Instr           lastOpc;       /* last opcode, for peephole opt */
+   };
+   /* AsmObject_ is only mentioned in Assembler.c; clients use
+      AsmObject/AsmBCO/AsmCAF/AsmCon. 
+   */
+
+typedef StgInfoTable*       AsmInfo;
+typedef struct AsmObject_*  AsmBCO;
+typedef struct AsmObject_*  AsmCAF;
+typedef struct AsmObject_*  AsmCon;
+typedef struct AsmObject_*  AsmObject;
+typedef Instr               AsmInstr;
 #else
 /* the types we export are totally opaque */
-typedef void              *AsmObject;
-typedef void              *AsmBCO;
-typedef void              *AsmCAF;
-typedef void              *AsmCon;
-typedef void              *AsmInfo;
-typedef void              *AsmClosure;
-typedef unsigned int       AsmInstr;
+typedef void*               AsmObject;
+typedef void*               AsmBCO;
+typedef void*               AsmCAF;
+typedef void*               AsmCon;
+typedef void*               AsmInfo;
+typedef void*               AsmClosure;
+typedef unsigned int        AsmInstr;
 #endif
 
-typedef int   AsmSp;   /* stack offset                  */
-typedef int   AsmPc;   /* program counter              */
-typedef AsmSp AsmVar;  /* offset of a Var on the stack  */
+
 
 /* --------------------------------------------------------------------------
  * "Types" used within the assembler
@@ -84,71 +132,64 @@ typedef enum {
   /* The following can be passed to C */
   CHAR_REP    = 'C',     
   INT_REP     = 'I',      
-#ifdef PROVIDE_INT64
-  INT64_REP   = 'z', 
-#endif
-#ifdef PROVIDE_INTEGER
   INTEGER_REP = 'Z',  
-#endif
-#ifdef PROVIDE_WORD
   WORD_REP    = 'W',     
-#endif
-#ifdef PROVIDE_ADDR
   ADDR_REP    = 'A',     
-#endif
   FLOAT_REP   = 'F',    
   DOUBLE_REP  = 'D',   
-#ifdef PROVIDE_STABLE
   STABLE_REP  = 's',   /* StablePtr a */
-#endif
 #ifdef PROVIDE_FOREIGN
   FOREIGN_REP = 'f',   /* ForeignObj  */
 #endif
 #ifdef PROVIDE_WEAK
   WEAK_REP    = 'w',   /* Weak a      */
 #endif
-#ifdef PROVIDE_ARRAY
   BARR_REP     = 'x',  /* PrimByteArray          a */
   MUTBARR_REP  = 'm',  /* PrimMutableByteArray s a */
-#endif
 
   /* The following can't be passed to C */
   PTR_REP      = 'P',      
   ALPHA_REP    = 'a',  /* a                        */
   BETA_REP     = 'b',  /* b                       */
   GAMMA_REP    = 'c',  /* c                        */
+  DELTA_REP    = 'd',  /* d                        */
   BOOL_REP     = 'B',  /* Bool                    */
   IO_REP       = 'i',  /* IO a                    */
   HANDLER_REP  = 'H',  /* Exception -> IO a       */
   ERROR_REP    = 'E',  /* Exception               */
-#ifdef PROVIDE_ARRAY           
   ARR_REP      = 'X',  /* PrimArray              a */
   REF_REP      = 'R',  /* Ref                  s a */
   MUTARR_REP   = 'M',  /* PrimMutableArray     s a */
-#endif
-#ifdef PROVIDE_CONCURRENT
   THREADID_REP = 'T',  /* ThreadId                 */
   MVAR_REP     = 'r',  /* MVar a                   */
-#endif
 
   /* Allegedly used in the IO monad */
   VOID_REP     = 'v'      
 } AsmRep;
 
 /* --------------------------------------------------------------------------
+ * Top-level control of the BCO generation + linking mechanism
+ * ------------------------------------------------------------------------*/
+
+extern void asmInitialise         ( void );
+extern void asmAllocateHeapSpace  ( void );
+extern void asmCopyAndLink        ( void );
+extern void asmShutdown           ( void );
+
+extern void* /* StgClosure* */ asmGetClosureOfObject ( AsmObject );
+
+/* --------------------------------------------------------------------------
  * Allocating (top level) heap objects
  * ------------------------------------------------------------------------*/
 
-extern AsmBCO     asmBeginBCO        ( void );
+extern AsmBCO     asmBeginBCO        ( int /*StgExpr*/ e );
 extern void       asmEndBCO          ( AsmBCO bco );
 
-extern AsmBCO     asmBeginContinuation ( AsmSp sp );
+extern AsmBCO     asmBeginContinuation ( AsmSp sp, int /*List*/ alts );
 extern void       asmEndContinuation   ( AsmBCO bco );
 
-extern AsmObject  asmMkObject        ( AsmClosure c );
-
 extern AsmCAF     asmBeginCAF        ( void );
-extern void       asmEndCAF          ( AsmCAF caf, AsmBCO body );
+extern void       asmEndCAF          ( AsmCAF caf );
 
 extern AsmInfo    asmMkInfo          ( AsmNat tag, AsmNat ptrs );
 extern AsmCon     asmBeginCon        ( AsmInfo info );
@@ -159,10 +200,7 @@ extern void       asmEndCon          ( AsmCon con );
  * in right to left order.
  */
 extern void       asmAddPtr          ( AsmObject obj, AsmObject arg );
-
-extern int        asmObjectHasClosure( AsmObject obj );
-extern AsmClosure asmClosureOfObject ( AsmObject obj );
-extern void       asmMarkObject      ( AsmObject obj );
+extern int        asmRepSizeW        ( AsmRep rep );
 
 /* --------------------------------------------------------------------------
  * Generating instruction streams
@@ -180,7 +218,7 @@ extern void   asmVar           ( AsmBCO bco, AsmVar v, AsmRep rep );
 extern AsmSp  asmBeginCase     ( AsmBCO bco );
 extern void   asmEndCase       ( AsmBCO bco );
 extern AsmSp  asmContinuation  ( AsmBCO bco, AsmBCO ret_addr );
-                               
+
 extern AsmSp  asmBeginAlt      ( AsmBCO bco );
 extern void   asmEndAlt        ( AsmBCO bco, AsmSp  sp );
 extern AsmPc  asmTest          ( AsmBCO bco, AsmWord tag );
@@ -194,21 +232,12 @@ extern void   asmReturnUnboxed ( AsmBCO bco, AsmRep rep );
 
 /* push unboxed Ints, Floats, etc */
 extern void   asmConstInt      ( AsmBCO bco, AsmInt     x );
-#ifdef PROVIDE_ADDR
 extern void   asmConstAddr     ( AsmBCO bco, AsmAddr    x );
-#endif
-#ifdef PROVIDE_WORD
 extern void   asmConstWord     ( AsmBCO bco, AsmWord    x );
-#endif
 extern void   asmConstChar     ( AsmBCO bco, AsmChar    x );
 extern void   asmConstFloat    ( AsmBCO bco, AsmFloat   x );
 extern void   asmConstDouble   ( AsmBCO bco, AsmDouble  x );
-#ifdef PROVIDE_INT64
-extern void   asmConstInt64    ( AsmBCO bco, AsmInt64   x );
-#endif
-#ifdef PROVIDE_INTEGER
 extern void   asmConstInteger  ( AsmBCO bco, AsmString  x );
-#endif
              
 /* Which monad (if any) does the primop live in? */
 typedef enum {
@@ -226,23 +255,38 @@ typedef struct {
     AsmNat8  opcode; /* should be Primop1 or Primop2 */
 } AsmPrim;
 
-extern const AsmPrim asmPrimOps[]; /* null terminated list */
+extern AsmPrim asmPrimOps[]; /* null terminated list */
+
+extern AsmPrim* asmFindPrim      ( char* s );
+extern AsmPrim* asmFindPrimop    ( AsmInstr prefix, AsmInstr op );
+extern AsmSp    asmBeginPrim     ( AsmBCO bco );
+extern void     asmEndPrim       ( AsmBCO bco, const AsmPrim* prim, 
+                                               AsmSp base );
+extern char*    asmGetPrimopName ( AsmPrim* p );
+
+extern void* /* StgBCO* */ asm_BCO_catch    ( void );
+extern void* /* StgBCO* */ asm_BCO_raise    ( void );
+extern void* /* StgBCO* */ asm_BCO_seq      ( void );
+extern void* /* StgBCO* */ asm_BCO_takeMVar ( void );
 
-extern const AsmPrim* asmFindPrim    ( char* s );
-extern const AsmPrim* asmFindPrimop  ( AsmInstr prefix, AsmInstr op );
-extern AsmSp          asmBeginPrim   ( AsmBCO bco );
-extern void           asmEndPrim     ( AsmBCO bco, const AsmPrim* prim, AsmSp base );
 
 /* --------------------------------------------------------------------------
  * Heap manipulation
  * ------------------------------------------------------------------------*/
 
-extern AsmVar asmClosure       ( AsmBCO bco, AsmObject p );
+extern AsmVar asmPushRefHugs   ( AsmBCO bco, int /*Name*/ n );
+extern AsmVar asmPushRefObject ( AsmBCO bco, AsmObject p );
+extern AsmVar asmPushRefNoOp   ( AsmBCO bco, StgPtr p );
+
+extern void   asmAddRefObject  ( AsmObject obj, AsmObject p );
+extern void   asmAddRefNoOp    ( AsmObject obj, StgPtr p );
+extern void   asmAddRefHugs    ( AsmObject obj,int /*Name*/ n );
 
 extern AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info );
 
 extern AsmSp  asmBeginPack     ( AsmBCO bco );
-extern void   asmEndPack       ( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info );
+extern void   asmEndPack       ( AsmBCO bco, AsmVar v, AsmSp start, 
+                                                       AsmInfo info );
 
 extern void   asmBeginUnpack   ( AsmBCO bco );
 extern void   asmEndUnpack     ( AsmBCO bco );
@@ -255,25 +299,96 @@ extern AsmVar asmAllocPAP      ( AsmBCO bco, AsmNat size );
 extern AsmSp  asmBeginMkPAP    ( AsmBCO bco );
 extern void   asmEndMkPAP      ( AsmBCO bco, AsmVar v, AsmSp start );
 
+#ifdef XMLAMBDA
+/*------------------------------------------------------------------------
+ XMlambda primitives.
+------------------------------------------------------------------------*/
+typedef AsmWord      AsmWitness;
+#define WITNESS_REP  WORD_REP
+
+/* insert/remove primitives on rows */
+extern void   asmEndPrimRowChainInsert( AsmBCO bco, AsmSp base, AsmWord n /* number of args */ );
+extern void   asmEndPrimRowChainBuild ( AsmBCO bco, AsmSp base, AsmWord n /* number of args */ );
+extern void   asmEndPrimRowChainRemove( AsmBCO bco, AsmSp base, AsmWord n /* number of args */ );
+extern void   asmEndPrimRowChainSelect( AsmBCO bco, AsmSp base, AsmWord n /* number of args */ );
+
+/* pack/unpack instructions for rows */
+extern AsmVar asmAllocRow      ( AsmBCO bco, AsmWord /*number of fields*/ n );
+extern AsmSp  asmBeginPackRow  ( AsmBCO bco );
+extern void   asmEndPackRow    ( AsmBCO bco, AsmVar v, AsmSp start, 
+                                             AsmWord /*number of fields*/ n );
+
+extern void   asmBeginUnpackRow( AsmBCO bco );
+extern void   asmEndUnpackRow  ( AsmBCO bco );
+
+extern void   asmConstRowTriv  ( AsmBCO bco );
+
+/* Inj primitives */
+extern AsmVar asmInjRel( AsmBCO bco, AsmVar var, AsmWitness w );
+extern AsmVar asmInjConst( AsmBCO bco, AsmWitness w );
+
+extern AsmPc  asmTestInjRel( AsmBCO bco, AsmVar var, AsmWitness w );
+extern AsmPc  asmTestInjConst( AsmBCO, AsmWitness w );
+
+extern void   asmWitnessRel( AsmBCO bco, AsmVar var, AsmWitness w );
+extern void   asmWitnessConst( AsmBCO bco, AsmWitness w );
+
+extern AsmVar asmUnInj( AsmBCO bco );
+
+#endif
+
 /* --------------------------------------------------------------------------
  * C-call and H-call
  * ------------------------------------------------------------------------*/
 
-extern const AsmPrim ccall_Id;
-extern const AsmPrim ccall_IO;
+extern AsmPrim ccall_ccall_Id;
+extern AsmPrim ccall_ccall_IO;
+extern AsmPrim ccall_stdcall_Id;
+extern AsmPrim ccall_stdcall_IO;
 
 typedef struct {
-  char *        arg_tys;
-  int           arg_size;
-  char *        result_tys;
-  int           result_size;
+  unsigned int  num_args;
+  char*         arg_tys;
+  unsigned int  num_results;
+  char*         result_tys;
 } CFunDescriptor;
 
-typedef struct {
-  char *        arg_tys;
-  char *        result_tys;
-} HFunDescriptor;
-
 CFunDescriptor* mkDescriptor( char* as, char* rs );
 
+#ifdef XMLAMBDA
+
+typedef enum _CallType
+{ CCall    = 'c'  /* C calling convention */
+, StdCall  = 's'  /* Standard calling convention */
+} CallType;
+
+/* The asmEndPrimCall*** functions call external functions.
+  Just start with "asmBeginPrim", push the arguments
+  and end with one of these functions. The argument and
+  result types are given as an argument string containing
+  the character representation of AsmRep's. */
+
+/* asmEndPrimCallDynamic calls a function defined in a dynamic link library. 
+  If decorate is true, the funName will be decorated according to its
+  calling convention, for example, with CCall an underscore is prefixed */
+extern void asmEndPrimCallDynamic(  AsmBCO       bco
+                                  , AsmSp        base
+                                  , const char*  libName
+                                  , const char*  funName
+                                  , const char*  argTypes
+                                  , const char*  resultTypes
+                                  , CallType     callType
+                                  , int /*bool*/ decorate);
+
+/* asmEndPrimCallIndirect calls the function given by its first
+  argument. (ie. push the address just before calling) */
+extern void asmEndPrimCallIndirect(  AsmBCO      bco
+                                   , AsmSp       base
+                                   , const char* argTypes
+                                   , const char* resultTypes
+                                   , CallType    callType );
+
+
+#endif
+
 /*-------------------------------------------------------------------------*/