X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FRtsAPI.c;h=178d1f6af26cd772ea619bbef9ba1065d49f3db4;hb=d5a43d65c49456ddfe129e36e21cf4326f138149;hp=fb4df6ced2fda128dbdd8470e133c8e36d231e47;hpb=f6692611aad945e46ffb615bde1df7def3fc742f;p=ghc-hetmet.git diff --git a/ghc/rts/RtsAPI.c b/ghc/rts/RtsAPI.c index fb4df6c..178d1f6 100644 --- a/ghc/rts/RtsAPI.c +++ b/ghc/rts/RtsAPI.c @@ -1,193 +1,185 @@ /* ---------------------------------------------------------------------------- - * $Id: RtsAPI.c,v 1.10 1999/11/02 15:05:59 simonmar Exp $ + * $Id: RtsAPI.c,v 1.28 2001/08/14 13:40:09 sewardj Exp $ * - * (c) The GHC Team, 1998-1999 + * (c) The GHC Team, 1998-2001 * * API for invoking Haskell functions via the RTS * * --------------------------------------------------------------------------*/ +#include "PosixSource.h" #include "Rts.h" #include "Storage.h" #include "RtsAPI.h" #include "SchedAPI.h" #include "RtsFlags.h" #include "RtsUtils.h" +#include "Prelude.h" /* ---------------------------------------------------------------------------- Building Haskell objects from C datatypes. ------------------------------------------------------------------------- */ HaskellObj -rts_mkChar (char c) +rts_mkChar (HsChar c) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Czh_con_info; - p->payload[0] = (StgClosure *)((StgInt)c); + p->header.info = Czh_con_info; + p->payload[0] = (StgClosure *)(StgChar)c; return p; } HaskellObj -rts_mkInt (int i) +rts_mkInt (HsInt i) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Izh_con_info; + p->header.info = Izh_con_info; p->payload[0] = (StgClosure *)(StgInt)i; return p; } HaskellObj -rts_mkInt8 (int i) +rts_mkInt8 (HsInt8 i) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - /* This is a 'cheat', using the static info table for Ints, - instead of the one for Int8, but the types have identical - representation. - */ - p->header.info = (const StgInfoTable*)&Izh_con_info; + p->header.info = I8zh_con_info; /* Make sure we mask out the bits above the lowest 8 */ p->payload[0] = (StgClosure *)(StgInt)((unsigned)i & 0xff); return p; } HaskellObj -rts_mkInt16 (int i) +rts_mkInt16 (HsInt16 i) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - /* This is a 'cheat', using the static info table for Ints, - instead of the one for Int8, but the types have identical - representation. - */ - p->header.info = (const StgInfoTable*)&Izh_con_info; + p->header.info = I16zh_con_info; /* Make sure we mask out the relevant bits */ p->payload[0] = (StgClosure *)(StgInt)((unsigned)i & 0xffff); return p; } HaskellObj -rts_mkInt32 (int i) +rts_mkInt32 (HsInt32 i) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - /* see mk_Int8 comment */ - p->header.info = (const StgInfoTable*)&Izh_con_info; + p->header.info = I32zh_con_info; p->payload[0] = (StgClosure *)(StgInt)i; return p; } HaskellObj -rts_mkInt64 (long long int i) +rts_mkInt64 (HsInt64 i) { long long *tmp; StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2)); - /* see mk_Int8 comment */ - p->header.info = (const StgInfoTable*)&I64zh_con_info; + p->header.info = I64zh_con_info; tmp = (long long*)&(p->payload[0]); *tmp = (StgInt64)i; return p; } HaskellObj -rts_mkWord (unsigned int i) +rts_mkWord (HsWord i) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Wzh_con_info; + p->header.info = Wzh_con_info; p->payload[0] = (StgClosure *)(StgWord)i; return p; } HaskellObj -rts_mkWord8 (unsigned int w) +rts_mkWord8 (HsWord8 w) { /* see rts_mkInt* comments */ StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Wzh_con_info; + p->header.info = W8zh_con_info; p->payload[0] = (StgClosure *)(StgWord)(w & 0xff); return p; } HaskellObj -rts_mkWord16 (unsigned int w) +rts_mkWord16 (HsWord16 w) { /* see rts_mkInt* comments */ StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Wzh_con_info; + p->header.info = W16zh_con_info; p->payload[0] = (StgClosure *)(StgWord)(w & 0xffff); return p; } HaskellObj -rts_mkWord32 (unsigned int w) +rts_mkWord32 (HsWord32 w) { /* see rts_mkInt* comments */ StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Wzh_con_info; + p->header.info = W32zh_con_info; p->payload[0] = (StgClosure *)(StgWord)w; return p; } HaskellObj -rts_mkWord64 (unsigned long long w) +rts_mkWord64 (HsWord64 w) { unsigned long long *tmp; StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,2)); /* see mk_Int8 comment */ - p->header.info = (const StgInfoTable*)&W64zh_con_info; + p->header.info = W64zh_con_info; tmp = (unsigned long long*)&(p->payload[0]); *tmp = (StgWord64)w; return p; } HaskellObj -rts_mkFloat (float f) +rts_mkFloat (HsFloat f) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,1)); - p->header.info = (const StgInfoTable*)&Fzh_con_info; + p->header.info = Fzh_con_info; ASSIGN_FLT((P_)p->payload, (StgFloat)f); return p; } HaskellObj -rts_mkDouble (double d) +rts_mkDouble (HsDouble d) { StgClosure *p = (StgClosure *)allocate(CONSTR_sizeW(0,sizeofW(StgDouble))); - p->header.info = (const StgInfoTable*)&Dzh_con_info; + p->header.info = Dzh_con_info; ASSIGN_DBL((P_)p->payload, (StgDouble)d); return p; } HaskellObj -rts_mkStablePtr (StgStablePtr s) +rts_mkStablePtr (HsStablePtr s) { StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1); - p->header.info = (const StgInfoTable*)&StablePtr_con_info; + p->header.info = StablePtr_con_info; p->payload[0] = (StgClosure *)s; return p; } HaskellObj -rts_mkAddr (void *a) +rts_mkPtr (HsPtr a) { StgClosure *p = (StgClosure *)allocate(sizeofW(StgHeader)+1); - p->header.info = (const StgInfoTable*)&Azh_con_info; + p->header.info = Ptr_con_info; p->payload[0] = (StgClosure *)a; return p; } #ifdef COMPILER /* GHC has em, Hugs doesn't */ HaskellObj -rts_mkBool (int b) +rts_mkBool (HsBool b) { if (b) { - return (StgClosure *)&True_closure; + return (StgClosure *)True_closure; } else { - return (StgClosure *)&False_closure; + return (StgClosure *)False_closure; } } HaskellObj rts_mkString (char *s) { - return rts_apply((StgClosure *)&unpackCString_closure, rts_mkAddr(s)); + return rts_apply((StgClosure *)unpackCString_closure, rts_mkPtr(s)); } #endif /* COMPILER */ @@ -195,10 +187,10 @@ HaskellObj rts_apply (HaskellObj f, HaskellObj arg) { StgAP_UPD *ap = (StgAP_UPD *)allocate(AP_sizeW(1)); - ap->header.info = &AP_UPD_info; + SET_HDR(ap, &stg_AP_UPD_info, CCS_SYSTEM); ap->n_args = 1; ap->fun = f; - ap->payload[0] = (P_)arg; + ap->payload[0] = arg; return (StgClosure *)ap; } @@ -206,120 +198,195 @@ rts_apply (HaskellObj f, HaskellObj arg) Deconstructing Haskell objects ------------------------------------------------------------------------- */ -char +HsChar rts_getChar (HaskellObj p) { - if ( p->header.info == (const StgInfoTable*)&Czh_con_info || - p->header.info == (const StgInfoTable*)&Czh_static_info) { - return (char)(StgWord)(p->payload[0]); + if ( p->header.info == Czh_con_info || + p->header.info == Czh_static_info) { + return (StgChar)(StgWord)(p->payload[0]); } else { - barf("getChar: not a Char"); + barf("rts_getChar: not a Char"); } } -int +HsInt rts_getInt (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]); + p->header.info == Izh_con_info || + p->header.info == Izh_static_info ) { + return (HsInt)(p->payload[0]); } else { - barf("getInt: not an Int"); + barf("rts_getInt: not an Int"); } } -int +HsInt8 +rts_getInt8 (HaskellObj p) +{ + if ( 1 || + p->header.info == I8zh_con_info || + p->header.info == I8zh_static_info ) { + return (HsInt8)(HsInt)(p->payload[0]); + } else { + barf("rts_getInt8: not an Int8"); + } +} + +HsInt16 +rts_getInt16 (HaskellObj p) +{ + if ( 1 || + p->header.info == I16zh_con_info || + p->header.info == I16zh_static_info ) { + return (HsInt16)(HsInt)(p->payload[0]); + } else { + barf("rts_getInt16: not an Int16"); + } +} + +HsInt32 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]); + p->header.info == I32zh_con_info || + p->header.info == I32zh_static_info ) { + return (HsInt32)(p->payload[0]); } else { - barf("getInt: not an Int"); + barf("rts_getInt32: not an Int32"); } } -unsigned int +HsInt64 +rts_getInt64 (HaskellObj p) +{ + HsInt64* tmp; + if ( 1 || + p->header.info == I64zh_con_info || + p->header.info == I64zh_static_info ) { + tmp = (HsInt64*)&(p->payload[0]); + return *tmp; + } else { + barf("rts_getInt64: not an Int64"); + } +} +HsWord rts_getWord (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]); + p->header.info == Wzh_con_info || + p->header.info == Wzh_static_info ) { + return (HsWord)(p->payload[0]); + } else { + barf("rts_getWord: not a Word"); + } +} + +HsWord8 +rts_getWord8 (HaskellObj p) +{ + if ( 1 || /* see above comment */ + p->header.info == W8zh_con_info || + p->header.info == W8zh_static_info ) { + return (HsWord8)(HsWord)(p->payload[0]); + } else { + barf("rts_getWord8: not a Word8"); + } +} + +HsWord16 +rts_getWord16 (HaskellObj p) +{ + if ( 1 || /* see above comment */ + p->header.info == W16zh_con_info || + p->header.info == W16zh_static_info ) { + return (HsWord16)(HsWord)(p->payload[0]); } else { - barf("getWord: not a Word"); + barf("rts_getWord16: not a Word16"); } } -unsigned int +HsWord32 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 ) { + p->header.info == W32zh_con_info || + p->header.info == W32zh_static_info ) { return (unsigned int)(p->payload[0]); } else { - barf("getWord: not a Word"); + barf("rts_getWord: not a Word"); } } -float + +HsWord64 +rts_getWord64 (HaskellObj p) +{ + HsWord64* tmp; + if ( 1 || /* see above comment */ + p->header.info == W64zh_con_info || + p->header.info == W64zh_static_info ) { + tmp = (HsWord64*)&(p->payload[0]); + return *tmp; + } else { + barf("rts_getWord64: not a Word64"); + } +} + +HsFloat rts_getFloat (HaskellObj p) { - if ( p->header.info == (const StgInfoTable*)&Fzh_con_info || - p->header.info == (const StgInfoTable*)&Fzh_static_info ) { + if ( p->header.info == Fzh_con_info || + p->header.info == Fzh_static_info ) { return (float)(PK_FLT((P_)p->payload)); } else { - barf("getFloat: not a Float"); + barf("rts_getFloat: not a Float"); } } -double +HsDouble rts_getDouble (HaskellObj p) { - if ( p->header.info == (const StgInfoTable*)&Dzh_con_info || - p->header.info == (const StgInfoTable*)&Dzh_static_info ) { + if ( p->header.info == Dzh_con_info || + p->header.info == Dzh_static_info ) { return (double)(PK_DBL((P_)p->payload)); } else { - barf("getDouble: not a Double"); + barf("rts_getDouble: not a Double"); } } -StgStablePtr +HsStablePtr rts_getStablePtr (HaskellObj p) { - if ( p->header.info == (const StgInfoTable*)&StablePtr_con_info || - p->header.info == (const StgInfoTable*)&StablePtr_static_info ) { + if ( p->header.info == StablePtr_con_info || + p->header.info == StablePtr_static_info ) { return (StgStablePtr)(p->payload[0]); } else { - barf("getStablePtr: not a StablePtr"); + barf("rts_getStablePtr: not a StablePtr"); } } -void * -rts_getAddr (HaskellObj p) +HsPtr +rts_getPtr (HaskellObj p) { - if ( p->header.info == (const StgInfoTable*)&Azh_con_info || - p->header.info == (const StgInfoTable*)&Azh_static_info ) { - + if ( p->header.info == Ptr_con_info || + p->header.info == Ptr_static_info ) { return (void *)(p->payload[0]); } else { - barf("getAddr: not an Addr"); + barf("rts_getPtr: not an Ptr"); } } #ifdef COMPILER /* GHC has em, Hugs doesn't */ -int +HsBool rts_getBool (HaskellObj p) { - if (p == &True_closure) { + if (p == True_closure) { return 1; - } else if (p == &False_closure) { + } else if (p == False_closure) { return 0; } else { - barf("getBool: not a Bool"); + barf("rts_getBool: not a Bool"); } } #endif /* COMPILER */ @@ -368,11 +435,19 @@ rts_evalLazyIO (HaskellObj p, unsigned int stack_size, /*out*/HaskellObj *ret) /* Convenience function for decoding the returned status. */ -void rts_checkSchedStatus ( char* site, SchedulerStatus rc ) -{ - if ( rc == Success ) { - return; - } else { - barf("%s: Return code (%d) not ok",(site),(rc)); - } +void +rts_checkSchedStatus ( char* site, SchedulerStatus rc ) +{ + switch (rc) { + case Success: + return; + case Killed: + barf("%s: uncaught exception",site); + case Interrupted: + barf("%s: interrupted", site); + case Deadlock: + barf("%s: no threads to run: infinite loop or deadlock?", site); + default: + barf("%s: Return code (%d) not ok",(site),(rc)); + } }