[project @ 1999-11-02 17:08:28 by simonmar]
[ghc-hetmet.git] / ghc / rts / RtsAPI.c
index 2ae69a9..fb4df6c 100644 (file)
@@ -1,5 +1,7 @@
 /* ----------------------------------------------------------------------------
- * $Id: RtsAPI.c,v 1.2 1998/12/02 13:28:38 simonm Exp $
+ * $Id: RtsAPI.c,v 1.10 1999/11/02 15:05:59 simonmar Exp $
+ *
+ * (c) The GHC Team, 1998-1999
  *
  * API for invoking Haskell functions via the RTS
  *
@@ -8,6 +10,7 @@
 #include "Rts.h"
 #include "Storage.h"
 #include "RtsAPI.h"
+#include "SchedAPI.h"
 #include "RtsFlags.h"
 #include "RtsUtils.h"
 
@@ -18,7 +21,7 @@ HaskellObj
 rts_mkChar (char c)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &CZh_con_info;
+  p->header.info = (const StgInfoTable*)&Czh_con_info;
   p->payload[0]  = (StgClosure *)((StgInt)c);
   return p;
 }
@@ -27,7 +30,7 @@ HaskellObj
 rts_mkInt (int i)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &IZh_con_info;
+  p->header.info = (const StgInfoTable*)&Izh_con_info;
   p->payload[0]  = (StgClosure *)(StgInt)i;
   return p;
 }
@@ -40,7 +43,7 @@ rts_mkInt8 (int i)
      instead of the one for Int8, but the types have identical
      representation.
   */
-  p->header.info = &IZh_con_info;
+  p->header.info = (const StgInfoTable*)&Izh_con_info;
   /* Make sure we mask out the bits above the lowest 8 */
   p->payload[0]  = (StgClosure *)(StgInt)((unsigned)i & 0xff);
   return p;
@@ -54,7 +57,7 @@ rts_mkInt16 (int i)
      instead of the one for Int8, but the types have identical
      representation.
   */
-  p->header.info = &IZh_con_info;
+  p->header.info = (const StgInfoTable*)&Izh_con_info;
   /* Make sure we mask out the relevant bits */
   p->payload[0]  = (StgClosure *)(StgInt)((unsigned)i & 0xffff);
   return p;
@@ -65,7 +68,7 @@ rts_mkInt32 (int i)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
   /* see mk_Int8 comment */
-  p->header.info = &IZh_con_info;
+  p->header.info = (const StgInfoTable*)&Izh_con_info;
   p->payload[0]  = (StgClosure *)(StgInt)i;
   return p;
 }
@@ -76,7 +79,7 @@ rts_mkInt64 (long long int i)
   long long *tmp;
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
   /* see mk_Int8 comment */
-  p->header.info = &I64Zh_con_info;
+  p->header.info = (const StgInfoTable*)&I64zh_con_info;
   tmp  = (long long*)&(p->payload[0]);
   *tmp = (StgInt64)i;
   return p;
@@ -86,7 +89,7 @@ HaskellObj
 rts_mkWord (unsigned int i)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &WZh_con_info;
+  p->header.info = (const StgInfoTable*)&Wzh_con_info;
   p->payload[0]  = (StgClosure *)(StgWord)i;
   return p;
 }
@@ -96,7 +99,7 @@ rts_mkWord8 (unsigned int w)
 {
   /* see rts_mkInt* comments */
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &WZh_con_info;
+  p->header.info = (const StgInfoTable*)&Wzh_con_info;
   p->payload[0]  = (StgClosure *)(StgWord)(w & 0xff);
   return p;
 }
@@ -106,7 +109,7 @@ rts_mkWord16 (unsigned int w)
 {
   /* see rts_mkInt* comments */
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &WZh_con_info;
+  p->header.info = (const StgInfoTable*)&Wzh_con_info;
   p->payload[0]  = (StgClosure *)(StgWord)(w & 0xffff);
   return p;
 }
@@ -116,7 +119,7 @@ rts_mkWord32 (unsigned int w)
 {
   /* see rts_mkInt* comments */
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &WZh_con_info;
+  p->header.info = (const StgInfoTable*)&Wzh_con_info;
   p->payload[0]  = (StgClosure *)(StgWord)w;
   return p;
 }
@@ -125,13 +128,12 @@ HaskellObj
 rts_mkWord64 (unsigned long long w)
 {
   unsigned long long *tmp;
-  extern StgInfoTable W64Zh_con_info;
 
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2));
   /* see mk_Int8 comment */
-  p->header.info = &W64Zh_con_info;
+  p->header.info = (const StgInfoTable*)&W64zh_con_info;
   tmp  = (unsigned long long*)&(p->payload[0]);
-  *tmp = (StgNat64)w;
+  *tmp = (StgWord64)w;
   return p;
 }
 
@@ -139,7 +141,7 @@ HaskellObj
 rts_mkFloat (float f)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1));
-  p->header.info = &FZh_con_info;
+  p->header.info = (const StgInfoTable*)&Fzh_con_info;
   ASSIGN_FLT((P_)p->payload, (StgFloat)f);
   return p;
 }
@@ -148,7 +150,7 @@ HaskellObj
 rts_mkDouble (double d)
 {
   StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,sizeofW(StgDouble)));
-  p->header.info = &DZh_con_info;
+  p->header.info = (const StgInfoTable*)&Dzh_con_info;
   ASSIGN_DBL((P_)p->payload, (StgDouble)d);
   return p;
 }
@@ -157,7 +159,7 @@ HaskellObj
 rts_mkStablePtr (StgStablePtr s)
 {
   StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1);
-  p->header.info = &StablePtr_con_info;
+  p->header.info = (const StgInfoTable*)&StablePtr_con_info;
   p->payload[0]  = (StgClosure *)s;
   return p;
 }
@@ -166,7 +168,7 @@ HaskellObj
 rts_mkAddr (void *a)
 {
   StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1);
-  p->header.info = &AZh_con_info;
+  p->header.info = (const StgInfoTable*)&Azh_con_info;
   p->payload[0]  = (StgClosure *)a;
   return p;
 }
@@ -187,6 +189,7 @@ rts_mkString (char *s)
 {
   return rts_apply((StgClosure *)&unpackCString_closure, rts_mkAddr(s));
 }
+#endif /* COMPILER */
 
 HaskellObj
 rts_apply (HaskellObj f, HaskellObj arg)
@@ -198,7 +201,6 @@ rts_apply (HaskellObj f, HaskellObj arg)
   ap->payload[0] = (P_)arg;
   return (StgClosure *)ap;
 }
-#endif /* COMPILER */
 
 /* ----------------------------------------------------------------------------
    Deconstructing Haskell objects
@@ -207,7 +209,8 @@ rts_apply (HaskellObj f, HaskellObj arg)
 char
 rts_getChar (HaskellObj p)
 {
-  if (p->header.info == &CZh_con_info || p->header.info == &CZh_static_info) {
+  if ( p->header.info == (const StgInfoTable*)&Czh_con_info || 
+       p->header.info == (const StgInfoTable*)&Czh_static_info) {
     return (char)(StgWord)(p->payload[0]);
   } else {
     barf("getChar: not a Char");
@@ -217,7 +220,21 @@ rts_getChar (HaskellObj p)
 int
 rts_getInt (HaskellObj p)
 {
-  if (p->header.info == &IZh_con_info || p->header.info == &IZh_static_info) {
+  if ( 1 ||
+       p->header.info == (const StgInfoTable*)&Izh_con_info || 
+       p->header.info == (const StgInfoTable*)&Izh_static_info ) {
+    return (int)(p->payload[0]);
+  } else {
+    barf("getInt: not an Int");
+  }
+}
+
+int
+rts_getInt32 (HaskellObj p)
+{
+  if ( 1 ||
+       p->header.info == (const StgInfoTable*)&Izh_con_info || 
+       p->header.info == (const StgInfoTable*)&Izh_static_info ) {
     return (int)(p->payload[0]);
   } else {
     barf("getInt: not an Int");
@@ -227,7 +244,21 @@ rts_getInt (HaskellObj p)
 unsigned int
 rts_getWord (HaskellObj p)
 {
-  if (p->header.info == &WZh_con_info || p->header.info == &WZh_static_info) {
+  if ( 1 || /* see above comment */
+       p->header.info == (const StgInfoTable*)&Wzh_con_info ||
+       p->header.info == (const StgInfoTable*)&Wzh_static_info ) {
+    return (unsigned int)(p->payload[0]);
+  } else {
+    barf("getWord: not a Word");
+  }
+}
+
+unsigned int
+rts_getWord32 (HaskellObj p)
+{
+  if ( 1 || /* see above comment */
+       p->header.info == (const StgInfoTable*)&Wzh_con_info ||
+       p->header.info == (const StgInfoTable*)&Wzh_static_info ) {
     return (unsigned int)(p->payload[0]);
   } else {
     barf("getWord: not a Word");
@@ -237,7 +268,8 @@ rts_getWord (HaskellObj p)
 float
 rts_getFloat (HaskellObj p)
 {
-  if (p->header.info == &FZh_con_info || p->header.info == &FZh_static_info) {
+  if ( p->header.info == (const StgInfoTable*)&Fzh_con_info || 
+       p->header.info == (const StgInfoTable*)&Fzh_static_info ) {
     return (float)(PK_FLT((P_)p->payload));
   } else {
     barf("getFloat: not a Float");
@@ -247,7 +279,8 @@ rts_getFloat (HaskellObj p)
 double
 rts_getDouble (HaskellObj p)
 {
-  if (p->header.info == &DZh_con_info || p->header.info == &DZh_static_info) {
+  if ( p->header.info == (const StgInfoTable*)&Dzh_con_info || 
+       p->header.info == (const StgInfoTable*)&Dzh_static_info ) {
     return (double)(PK_DBL((P_)p->payload));
   } else {
     barf("getDouble: not a Double");
@@ -257,8 +290,8 @@ rts_getDouble (HaskellObj p)
 StgStablePtr
 rts_getStablePtr (HaskellObj p)
 {
-  if (p->header.info == &StablePtr_con_info || 
-      p->header.info == &StablePtr_static_info) {
+  if ( p->header.info == (const StgInfoTable*)&StablePtr_con_info || 
+       p->header.info == (const StgInfoTable*)&StablePtr_static_info ) {
     return (StgStablePtr)(p->payload[0]);
   } else {
     barf("getStablePtr: not a StablePtr");
@@ -268,7 +301,9 @@ rts_getStablePtr (HaskellObj p)
 void *
 rts_getAddr (HaskellObj p)
 {
-  if (p->header.info == &AZh_con_info || p->header.info == &AZh_static_info) {
+  if ( p->header.info == (const StgInfoTable*)&Azh_con_info || 
+       p->header.info == (const StgInfoTable*)&Azh_static_info ) {
+  
     return (void *)(p->payload[0]);
   } else {
     barf("getAddr: not an Addr");
@@ -296,28 +331,39 @@ SchedulerStatus
 rts_eval (HaskellObj p, /*out*/HaskellObj *ret)
 {
   StgTSO *tso = createGenThread(RtsFlags.GcFlags.initialStkSize, p);
-  return schedule(tso, ret);
+  scheduleThread(tso);
+  return waitThread(tso, ret);
 }
 
 SchedulerStatus
 rts_eval_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
 {
   StgTSO *tso = createGenThread(stack_size, p);
-  return schedule(tso, ret);
+  scheduleThread(tso);
+  return waitThread(tso, ret);
 }
 
+/*
+ * rts_evalIO() evaluates a value of the form (IO a), forcing the action's
+ * result to WHNF before returning.
+ */
 SchedulerStatus
 rts_evalIO (HaskellObj p, /*out*/HaskellObj *ret)
 {
-  StgTSO *tso = createIOThread(RtsFlags.GcFlags.initialStkSize, p);
-  return schedule(tso, ret);
+  StgTSO* tso = createStrictIOThread(RtsFlags.GcFlags.initialStkSize, p);
+  scheduleThread(tso);
+  return waitThread(tso, ret);
 }
 
+/*
+ * Like rts_evalIO(), but doesn't force the action's result.
+ */
 SchedulerStatus
-rts_evalIO_ (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
+rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret)
 {
   StgTSO *tso = createIOThread(stack_size, p);
-  return schedule(tso, ret);
+  scheduleThread(tso);
+  return waitThread(tso, ret);
 }
 
 /* Convenience function for decoding the returned status. */