packages feed

MicroHs-0.17.4.0: src/runtime/eval.c

/* Copyright 2023,2024,2025 Lennart Augustsson
 * See LICENSE file for full license.
 */
#include "mhsffi.h"  /* this includes config.h */
#include "extra.c"

#if !defined(EXTRASANITY)
#define EXTRASANITY 0           /* sanity checks that cost time, unlike SANITY */
#endif

/* Extra asserts for debugging */
#if EXTRASANITY
#if WANT_STDIO
#define XASSERT(p) do { if (!(p)) { fprintf(stderr, "XASSERT line %d: %s\n", __LINE__, #p); exit(1); } } while(0)
#else
#define XASSERT(p) do { if (!(p)) { ERR("XASSERT"); } } while(0)
#endif
#else
#define XASSERT(p) ((void)0)
#endif

//#define ROUND_UP(x, d) ((((x) + (d) - 1) / (d)) * (d))

#if !defined(WANT_GMP)
#define WANT_GMP 0
#endif /* defined(WANT_GMP) */

#if !defined(WANT_IMATH) && !WANT_GMP
#define WANT_IMATH 1
#endif /* defined(WANT_MATH) */

#if WANT_GMP && WANT_IMATH
#error "Cannot have both WANT_GMP and WANT_IMATH"
#endif

#if !defined(WANT_OVERFLOW)
#define WANT_OVERFLOW 0
#endif /* defined(WANT_OVERFLOW) */

#if !defined(WANT_IO_POLL)
#define WANT_IO_POLL 0
#endif /* defined(WANT_IO_POLL) */

#if !defined(WANT_SOCKET)
#define WANT_SOCKET 0
#endif /* defined(WANT_SOCKET) */

#if WANT_STDIO
#include <stdio.h>
#include <stdarg.h>
#include <locale.h>
#endif
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#if WANT_MATH
#include <math.h>
#endif  /* WANT_MATH */
#if defined(__EMSCRIPTEN__)
#include "emscripten.h"
#ifndef EM_ASM_PTR
#define EM_ASM_PTR(...) (void*)(uintptr_t)EM_ASM_INT(__VA_ARGS__)
#endif
#endif /* __EMSCRIPTEN__ */
#if WANT_DIR
#include <sys/stat.h>
#include <sys/types.h>
#endif  /* WANT_DIR */
#if WANT_TIME
#include <time.h>
#endif
#if WANT_GMP
#include <gmp.h>
#endif  /* WANT_GMP */
#if WANT_IMATH
#include "imgmp.h"
#endif  /* WANT_IMATH */
#if WANT_SIGINT
#include <signal.h>
#endif
#if WANT_IO_POLL
#include <poll.h>
#endif

extern char **environ;          /* should probably be behind some WANT_ */

#if WANT_MD5
#include "md5.h"
#endif

#if !defined(WANT_UTF8)
#define WANT_UTF8 1
#endif

#if !defined(WANT_BUF)
#define WANT_BUF 1
#endif

#if !defined(WANT_CRLF)
#define WANT_CRLF 1
#endif

#if !defined(WANT_BASE64)
#define WANT_BASE64 1
#endif

#if !defined(WANT_LZ77)
#define WANT_LZ77 1
#endif

#if !defined(WANT_LZMA)
#define WANT_LZMA 1
#endif

#if !defined(WANT_RLE)
#define WANT_RLE 1
#endif

#if !defined(WANT_BWT)
#define WANT_BWT 1
#endif

#if !defined(WANT_ENV)
#define WANT_ENV 1
#endif

#if !defined(WANT_ERRNO)
#define WANT_ERRNO 0
#else
#include "ffi_errno.c"
#endif

#if !defined(GET_EXECUTABLE_PATH)
char *get_executable_path(void) { return NULL; }
#endif

#if !defined(YIELD_EXTRA)
#define YIELD_EXTRA do {} while(0)
#endif

#if !defined(MKDIR)
#define MKDIR mkdir
#endif

#if WANT_LZ77
size_t lz77d(uint8_t *src, size_t srclen, uint8_t **bufp);
size_t lz77c(uint8_t *src, size_t srclen, uint8_t **bufp);
#endif

const struct ffi_entry ffi_table[];
int num_ffi;
#define FFI_IX(i) ((i) < num_ffi ? ffi_table[i] : xffi_table[i - num_ffi])

#if WANT_STDIO
#define THREAD_DEBUG 1
#else
#define THREAD_DEBUG 0
#endif


#define VERSION "v9.3\n"

#define PRIvalue PRIdPTR
#define PRIuvalue PRIuPTR
typedef uintptr_t heapoffs_t;   /* Heap offsets */
#define PRIheap PRIuPTR

typedef uintptr_t counter_t;    /* Statistics counter, can be smaller since overflow doesn't matter */
#define PRIcounter PRIuPTR
typedef uintptr_t bits_t;       /* One word of bits */

#if !defined(WANT_ARGS)
#define WANT_ARGS 1
#endif

#if !defined(MALLOC)
#define MALLOC malloc
#endif

#if !defined(REALLOC)
#define REALLOC realloc
#endif

#if !defined(CALLOC)
#define CALLOC calloc
#endif

#if !defined(FREE)
#define FREE free
#endif

#if !defined(EXIT)
#define EXIT exit
#endif

#if !defined(PRINT)
#define PRINT printf
#endif

#if !defined(PCOMMA)
#define PCOMMA "'"
#endif  /* !defined(PCOMMA) */

#if !defined(GETRAW)
int GETRAW(void) { return -1; }
#endif  /* !defined(GETRAW) */

#if !defined(GETTIMEMICRO)
value_t GETTIMEMICRO(void) { return 0; }
#endif  /* !define(GETTIMEMICRO) */
#define GETTIMEMILLI() (GETTIMEMICRO() / 1000)

#if !defined(GETCPUTIME)
void GETCPUTIME(long *sec, long *nsec) { *sec = 0; *nsec = 0; }
#endif  /* !define(GETCPUTIME) */

#if !defined(INLINE)
#define INLINE inline
#endif  /* !define(INLINE) */

#if !defined(NOINLINE)
#if defined(__GNUC__) || defined(__clang__)
#define NOINLINE __attribute__((noinline))
#else
#define NOINLINE
#endif
#endif  /* !defined(NOINLINE) */

#if !defined(ALWAYS_INLINE)
#if defined(__GNUC__) || defined(__clang__)
#define ALWAYS_INLINE inline __attribute__((always_inline))
#else
#define ALWAYS_INLINE inline
#endif
#endif  /* !defined(ALWAYS_INLINE) */

#if !defined(UNLIKELY)
#if defined(__GNUC__) || defined(__clang__)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define UNLIKELY(x) (x)
#endif
#endif  /* !defined(UNLIKELY) */

#if !defined(NORETURN)
/*#define NORETURN [[noreturn]]*/
#define NORETURN _Noreturn
#endif /* !defined(NORETURN) */

#if !defined(PACKED)
#if WORD_SIZE == 32
/* Avoid 64 bit alignment to keep nodes to 12 bytes */
#define PACKED __attribute__((packed, aligned(4)))
#else
#define PACKED
#endif  /* WORD_SIZE == 32 */
#endif  /* !defined(PACKED) */

/* tcc doesn't understand noreturn attribute */
#if defined(__TCC__)
#define NOTREACHED return 0
#else
#define NOTREACHED
#endif

#if !defined(SLICE)
#define SLICE 100000
#endif

NORETURN void memerr(void);

void *
mmalloc(size_t s)
{
  if (s == 0) s = 1;            /* avoid malloc() returning NULL */
  void *p = MALLOC(s);
  if (!p)
    memerr();
  return p;
}

void *
mrealloc(void *q, size_t s)
{
  if (s == 0) s = 1;            /* avoid realloc() returning NULL */
  void *p = REALLOC(q, s);
  if (!p)
    memerr();
  return p;
}

void *
mcalloc(size_t n, size_t s)
{
  if (n * s == 0) n = s = 1;    /* avoid calloc() returning NULL */
  void *p = CALLOC(n, s);
  if (!p)
    memerr();
  return p;
}

#if !defined(ERR)
#if WANT_STDIO
static NORETURN NOINLINE void
mhs_err(const char *s)
{
  fprintf(stderr, "ERR: %s\n", s);
  EXIT(1);
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 1, 2)))
#endif
static NORETURN NOINLINE void
mhs_err1(const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);
  EXIT(1);
}
#define ERR(s)    mhs_err(s)
#define ERR1(s,a) mhs_err1("ERR: " s "\n", (a))
#else  /* WANT_STDIO */
#define ERR(s) EXIT(1)
#define ERR1(s,a) EXIT(1)
#endif  /* WANT_STDIO */
#endif  /* !define(ERR) */

#if !defined(TMPNAME)
/* This is a really bad implementation, since it doesn't check for anything. */
char* TMPNAME(const char* pre, const char* suf) {
  ERR("no TMPNAME");
}
#endif

#if !defined(FFS)
/* This is pretty bad, could use deBruijn multiplication instead. */
int
FFS(bits_t x)
{
  int i;
  if (!x)
    return 0;
  for(i = 1; !(x & 1); x >>= 1, i++)
    ;
  return i;
}
#endif  /* !defined(FFS) */

/***** popcount *****/
#if defined(__has_builtin)

#if UINTPTR_MAX == ULONG_MAX && __has_builtin(__builtin_popcountl)
#define BUILTIN_POPCOUNT __builtin_popcountl
#elif UINTPTR_MAX == ULLONG_MAX && __has_builtin(__builtin_popcountll)
#define BUILTIN_POPCOUNT __builtin_popcountll
#elif UINTPTR_MAX == UINT_MAX && __has_builtin(__builtin_popcount)
#define BUILTIN_POPCOUNT __builtin_popcount
#endif

#if ULLONG_MAX == UINT64_MAX && __has_builtin(__builtin_popcountll)
#define BUILTIN_POPCOUNT64 __builtin_popcountll
#elif ULONG_MAX == UINT64_MAX && __has_builtin(__builtin_popcountl)
#define BUILTIN_POPCOUNT64 __builtin_popcountl
#endif

/* If there are compiler intrinsics to detect overflow, do so */
#if __has_builtin(__builtin_add_overflow) && WANT_OVERFLOW
#define ADD_OVERFLOW(T, r, a, b, ovf) do { T vr; if (__builtin_add_overflow((T)(a), (T)(b), &(vr))) ovf(exn_overflow); (r) = vr; } while(0)
#endif
#if __has_builtin(__builtin_sub_overflow) && WANT_OVERFLOW
#define SUB_OVERFLOW(T, r, a, b, ovf) do { T vr; if (__builtin_sub_overflow((T)(a), (T)(b), &(vr))) ovf(exn_overflow); (r) = vr; } while(0)
#endif
#if __has_builtin(__builtin_mul_overflow) && WANT_OVERFLOW
#define MUL_OVERFLOW(T, r, a, b, ovf) do { T vr; if (__builtin_mul_overflow((T)(a), (T)(b), &(vr))) ovf(exn_overflow); (r) = vr; } while(0)
#endif

#endif

/* If we can't detect overflow, just ignore it. */
#if !defined(ADD_OVERFLOW)
#define ADD_OVERFLOW(T, r, a, b, ovf) ((r) = (a) + (b))
#endif
#if !defined(SUB_OVERFLOW)
#define SUB_OVERFLOW(T, r, a, b, ovf) ((r) = (a) - (b))
#endif
#if !defined(MUL_OVERFLOW)
#define MUL_OVERFLOW(T, r, a, b, ovf) ((r) = (a) * (b))
#endif


#if !defined(POPCOUNT)
uvalue_t POPCOUNT(uvalue_t x) {
#if defined(BUILTIN_POPCOUNT)
  return BUILTIN_POPCOUNT(x);
#else   /* !defined(BUILTIN_POPCOUNT) */
  uvalue_t count = 0;
  while (x) {
    x = x & (x - 1); // clear lowest 1 bit
    count++;
  }
  return count;
#endif   /* !defined(BUILTIN_POPCOUNT) */
}
#endif  /* !defined(POPCOUNT) */

#if !defined(POPCOUNT64)
uvalue_t POPCOUNT64(uint64_t x) {
#if defined(BUILTIN_POPCOUNT64)
  return BUILTIN_POPCOUNT64(x);
#else   /* !defined(BUILTIN_POPCOUNT64) */
  uvalue_t count = 0;
  while (x) {
    x = x & (x - 1); // clear lowest 1 bit
    count++;
  }
  return count;
#endif   /* !defined(BUILTIN_POPCOUNT64) */
}
#endif  /* !defined(POPCOUNT64) */
/***** end popcount *****/

/***** clz *****/
#if defined(__has_builtin)

#if UINTPTR_MAX == ULONG_MAX && __has_builtin(__builtin_clzl)
#define BUILTIN_CLZ __builtin_clzl
#elif UINTPTR_MAX == ULLONG_MAX && __has_builtin(__builtin_clzll)
#define BUILTIN_CLZ __builtin_clzll
#elif UINTPTR_MAX == UINT_MAX && __has_builtin(__builtin_clz)
#define BUILTIN_CLZ __builtin_clz
#endif

#if ULLONG_MAX == UINT64_MAX && __has_builtin(__builtin_clzll)
#define BUILTIN_CLZ64 __builtin_clzll
#elif ULONG_MAX == UINT64_MAX && __has_builtin(__builtin_clzl)
#define BUILTIN_CLZ64 __builtin_clzl
#endif

#endif


#if !defined(CLZ)
uvalue_t CLZ(uvalue_t x) {
#if defined(BUILTIN_CLZ)
  if (x == 0) return WORD_SIZE;
  return BUILTIN_CLZ(x);
#else   /* defined(BUILTIN_CLZ) */
  value_t count = WORD_SIZE;
  while (x) {
    x = x >> 1;
    count--;
  }
  return count;
#endif  /* defined(BUILTIN_CLZ) */
}
#endif  /* !defined(CLZ) */

#if !defined(CLZ64)
uvalue_t CLZ64(uint64_t x) {
#if defined(BUILTIN_CLZ64)
  if (x == 0) return 64;
  return BUILTIN_CLZ64(x);
#else   /* defined(BUILTIN_CLZ64) */
  value_t count = 64;
  while (x) {
    x = x >> 1;
    count--;
  }
  return count;
#endif  /* defined(BUILTIN_CLZ64) */
}
#endif  /* !defined(CLZ64) */
/***** end clz *****/

/***** ctz *****/
#if defined(__has_builtin)

#if UINTPTR_MAX == ULONG_MAX && __has_builtin(__builtin_ctzl)
#define BUILTIN_CTZ __builtin_ctzl
#elif UINTPTR_MAX == ULLONG_MAX && __has_builtin(__builtin_ctzll)
#define BUILTIN_CTZ __builtin_ctzll
#elif UINTPTR_MAX == UINT_MAX && __has_builtin(__builtin_ctz)
#define BUILTIN_CTZ __builtin_ctz
#endif

#if ULLONG_MAX == UINT64_MAX && __has_builtin(__builtin_ctzll)
#define BUILTIN_CTZ64 __builtin_ctzll
#elif ULONG_MAX == UINT64_MAX && __has_builtin(__builtin_ctzl)
#define BUILTIN_CTZ64 __builtin_ctzl
#endif

#endif  /* defined(__has_builtin) */


#if !defined(CTZ)
uvalue_t CTZ(uvalue_t x) {
  if (x == 0) return WORD_SIZE;
#if defined(BUILTIN_CTZ)
  return BUILTIN_CTZ(x);
#else  /* defined(BUILTIN_CTZ) */
  uvalue_t count = 0;
  while ((x & 1) == 0) {
    x = x >> 1;
    count += 1;
  }
  return count;
#endif  /* defined(BUILTIN_CTZ) */
}
#endif  /* !defined(CTZ) */

#if !defined(CTZ64)
uvalue_t CTZ64(uint64_t x) {
  if (x == 0) return 64;
#if defined(BUILTIN_CTZ64)
  return BUILTIN_CTZ64(x);
#else  /* defined(BUILTIN_CTZ64) */
  uvalue_t count = 0;
  while ((x & 1) == 0) {
    x = x >> 1;
    count++;
  }
  return count;
#endif  /* defined(BUILTIN_CTZ64) */
}
#endif  /* !defined(CTZ) */

/***** end ctz *****/

#if !defined(COUNT)
#define COUNT(n) ++(n)
#endif

value_t
iswindows(void)
{
#if defined(ISWINDOWS)
  return 1;
#else
  return 0;
#endif
}

/* Was the runtime compiled with MinGW (rather than, e.g., cl)? */
value_t
ismingw(void)
{
#if defined(__MINGW32__)
  return 1;
#else
  return 0;
#endif
}

value_t
ismacos(void)
{
#if defined(ISMACOS)
  return 1;
#else
  return 0;
#endif
}

value_t
islinux(void)
{
#if defined(ISLINUX)
  return 1;
#else
  return 0;
#endif
}

/***************************************/

/* Keep permanent nodes for LOW_INT <= i < HIGH_INT */
#define LOW_INT (-10)
#define HIGH_INT 256

#if !defined(HEAP_CELLS)
#define HEAP_CELLS 50000000
#endif

#if !defined(STACK_SIZE)
#define STACK_SIZE 300000
#endif

/***************************************/

/* All node tags for nodes with a payload */

enum node_tag { D_NONE,
                X_AP,           /* if we need to indicate an AP node */

                /* NODEPTR with a "virtual" pointer, and special tags */
                D_IND,          /* indirection, pointer payload */
                /* Tags with a payload */
                D_INT, D_INT64, D_DBL, D_FLT32, D_PTR, D_FUNPTR, D_FORPTR,
                D_ARR, D_THID, D_MVAR, D_WEAK,
                D_IO_CCALL,
                D_BADDYN, D_TICK,

                /* Tags with extra info */
                V_CONSTR, V_TAG, V_CASED, V_LOOKD, V_CASES, V_LOOKS,

                T_FIRST_REAL_TAG,
                /* NODEPTR with a "virtual" pointer, and regular tags */
                /* Regular tags */
                T_S, T_K, T_I, T_B, T_C,
                T_A, T_Y, T_SS, T_BB, T_CC, T_P, T_R, T_O, T_U, T_Z,
                T_K2, T_K3, T_K4, T_CCB,
                T_ADD, T_SUB, T_MUL, T_QUOT, T_REM, T_SUBR,
                T_UADD, T_USUB, T_UMUL, T_UQUOT, T_UREM, T_USUBR,
                T_AND, T_OR, T_XOR, T_SHL, T_SHR, T_ASHR,
                T_EQ, T_NE, T_LT, T_LE, T_GT, T_GE, T_ULT, T_ULE, T_UGT, T_UGE, T_ICMP, T_UCMP,
                T_NEG, T_UNEG, T_INV, T_POPCOUNT, T_CLZ, T_CTZ,
                T_SEQ, T_IO_BIND, T_IO_THEN, T_IO_RETURN,
                T_ADD64, T_SUB64, T_MUL64, T_QUOT64, T_REM64, T_SUBR64, T_NEG64,
                T_UADD64, T_USUB64, T_UMUL64, T_UQUOT64, T_UREM64, T_USUBR64, T_UNEG64,
                T_AND64, T_OR64, T_XOR64, T_INV64, T_SHL64, T_SHR64, T_ASHR64,
                T_POPCOUNT64, T_CLZ64, T_CTZ64,
                T_EQ64, T_NE64, T_LT64, T_LE64, T_GT64, T_GE64, T_ULT64, T_ULE64, T_UGT64, T_UGE64, T_ICMP64, T_UCMP64,
                T_ITOI64, T_I64TOI, T_UTOU64, T_U64TOU,
                T_FPADD, T_FP2P, T_FPNEW, T_FPFIN,
                T_FP2BS, T_BS2FP,
                T_TOPTR, T_TOINT, T_TODBL, T_TOFLT, T_TOFUNPTR,
                T_FROMDBL, T_FROMFLT,
                T_BININT2, T_BININT1, T_UNINT1,
                T_BININT64_2, T_BININT64_1, T_UNINT64_1,
                T_BINFLT2, T_BINFLT1, T_UNFLT1,
                T_BINDBL2, T_BINDBL1, T_UNDBL1,
                T_BINBS2, T_BINBS1,
                T_ISINT,
                T_FADD, T_FSUB, T_FMUL, T_FDIV, T_FNEG, T_ITOF, T_I64TOF, T_FTOI, T_UTOF,
                T_FEQ, T_FNE, T_FLT, T_FLE, T_FGT, T_FGE,
                T_DADD, T_DSUB, T_DMUL, T_DDIV, T_DNEG, T_ITOD, T_I64TOD, T_DTOI, T_UTOD,
                T_DEQ, T_DNE, T_DLT, T_DLE, T_DGT, T_DGE,
                T_FTOD, T_DTOF,
                T_ARR_ALLOC, T_ARR_COPY, T_ARR_SIZE, T_ARR_READ, T_ARR_WRITE, T_ARR_TRUNC, T_ARR_EQ,
                T_RAISE, T_RNF,
                T_IO_SERIALIZE, T_IO_DESERIALIZE,
                T_IO_GETARGREF,
                T_IO_PERFORMIO, T_IO_ATOMIC, T_IO_PRINT, T_CATCH, T_CATCHR,
                T_IO_GC, T_IO_STATS,
                T_IO_LAZYBIND, T_IO_STRICT, T_IO_LAZYBINDST,
                T_IO_FORK, T_IO_THID, T_THNUM, T_IO_THROWTO, T_IO_YIELD,
                T_IO_NEWMVAR,
                T_IO_TAKEMVAR, T_IO_PUTMVAR, T_IO_READMVAR,
                T_IO_TRYTAKEMVAR, T_IO_TRYPUTMVAR, T_IO_TRYREADMVAR,
                T_IO_THREADDELAY, T_IO_THREADSTATUS,
                T_IO_GETMASKINGSTATE, T_IO_SETMASKINGSTATE,
                T_PACKCSTRING, T_PACKCSTRINGLEN,
                T_BSAPPEND, T_BSEQ, T_BSNE, T_BSLT, T_BSLE, T_BSGT, T_BSGE, T_BSCMP,
                T_BSUNPACK, T_BSREPLICATE, T_BSLENGTH, T_BSSUBSTR, T_BSINDEX,
                T_BSNEW, T_BSREAD, T_BSWRITE, T_BSFREEZE, T_BSAPPBYTE, T_BSAPPCHAR, 
                T_BSFROMUTF8, T_BSHEADUTF8, T_BSTAILUTF8,
                T_UTF8LENGTH, T_UTF8OFFSET, T_UTF8OFFSETEND, T_UTF8OFFSETFROM, T_UTF8OFFSETENDBEFORE,
                T_UTF8DECODE, T_UTF8DECODEEND,
                T_UTF8REVERSE, T_UTF8SKIPSPACE, T_UTF8SKIPNONSPACE, T_UTF8TRIMSPACEEND,
                T_BSFIND, T_BSRFIND, T_BSASCIICASE, T_BSISASCII, T_BSCONCAT,
                T_BSAPPENDDOT, T_BSGRAB, T_BSGRABLEN,
                T_SPNEW, T_SPDEREF, T_SPFREE,
                T_WKNEWFIN, T_WKNEW, T_WKDEREF, T_WKFINAL,
                T_IO_PP,           /* for debugging */
                T_NODEADDR, T_IO_RTSINFO, /* for reading the graph, see rts_info */
                T_IO_STDIN, T_IO_STDOUT, T_IO_STDERR,
                T_IO_WAITRDFD, T_IO_WAITWRFD,
                T_LAST_TAG,
                T_XXX = 0x7fffffff
};

/* Most entries are initialized from the primops table. */
static const char* tag_names [T_LAST_TAG+1] =
  { [D_NONE] = "NONE", [X_AP] = "AP", [D_IND] = "IND",
    [D_INT] = "INT", [D_INT64] = "INT64", [D_DBL] = "DBL", [D_FLT32] = "FLT32", [D_PTR] = "PTR",
    [D_FUNPTR] = "FUNPTR", [D_FORPTR] = "FORPTR", [D_ARR] = "ARR", [D_THID] = "THID", [D_MVAR] = "MVAR", [D_WEAK] = "WEAK",
    [D_IO_CCALL] = "IO.ccall", [D_BADDYN] = "BADDYN", [D_TICK] = "TICK",
    [V_CONSTR] = "C_", [V_TAG] = "TAG", [V_CASED] = "D_", [V_LOOKD] = "L_", [V_CASES] = "E_", [V_LOOKS] = "M_",
  };
#define TAGNAME(t) tag_names[t]
/* The arity of each tag, from primops[], see arity_virt. */
static uint8_t tag_arity[T_LAST_TAG+1];

/***************************************/

/*
 * A NODEPTR is a basically a pointer to a struct node.
 * But to save memory references, we never point to a constant
 * node that is just a tag, instead we encode the tag straight in
 * the NODEPTR.
 * A struct node is always at least 4 byte aligned, so the two lower
 * bits of a real pointer are always 0.
 * A NODEPTR always has an LSB=0.
 * ...pppp00         real pointer = ...pppp00
 * ...tttt10         tag = ...tttt, basically a node_tag
 */

/* 
 * For a node (i.e., two NODEPTR) the two lower bits of the FUN part
 * decides what kind of node it is.
 * xxx0  yyyy        AP node, xxx0 and yyyy are NODEPTRs
 * xx01  yyyy        unused
 * xx11  yyyy        node with payload (IND, INT, DBL, etc), xx=D_INT,D_DBL,..., yyyy is the payload
 */

/*
 * For a NODEPTR we have
 * ppppppppp00        ppppppppp00 is a real pointer
 * nnnkkkttt10        ttt 11 bits is a node_tag, kkk 11 bits, nnn 8 bits
 *   when ttt=
 *     V_CONSTR       kkk = constructor number, nnn = number of constructor arguments
 *     V_CASED        nnnkkk = index in the table of case arities, see below
 *     V_CASES        as above
 *     V_LOOKD        kkk = number of arms, nnn unused
 *     V_LOOKS        as V_CASES
 *     otherwise      kkk, nnn unused
 * 
 */
typedef uintptr_t tag_t;
typedef union NodePtr {
  tag_t          node_bits;
  struct node   *node_ptr;
  union NodePtr *node_pptr;      /* only used during GC for values on the stack */
} NODEPTR;

const NODEPTR NIL_NODEPTR = { 0 };

/* To fit in a word we should have
 * PTR_TAG_BITS + PTR_TAG_TAG_BITS + PTR_TAG_NCON_BITS + PTR_TAG_NARG_BITS <= size_in_bits(tag_t)
 * We make them add to 32
 */
#define PTR_TAG_BITS 2                                   /* number of reserved tag bits in every pointer */
#define PTR_TAG_MASK ((1 << PTR_TAG_BITS) - 1)           /* 0x3 */
#define PTR_TAG_PTR  0
#define PTR_TAG_TAG  2
#define PTR_TAG_DATA 3
#define PTR_AP_MASK  1
#define PTR_AP_AP    0
#define PTR_TAG_TAG_BITS 11     /* max number of bits in a enum node_tag */
#define PTR_TAG_TAG_MASK ((1 << PTR_TAG_TAG_BITS) - 1)   /* 0x7ff */
#define PTR_TAG_NCON_BITS 11    /* max number of constructors/case-arms */
#define PTR_TAG_NCON_MASK ((1 << PTR_TAG_NCON_BITS) - 1) /* 0x7ff */
#define PTR_TAG_NARG_BITS 8     /* max number of constructor arguments */
#define PTR_TAG_NARG_MASK ((1 << PTR_TAG_NARG_BITS) - 1) /* 0xff */

#define MK_TAG(t) (((tag_t)(t) << PTR_TAG_BITS) | PTR_TAG_TAG)
#define MK_DATA_TAG(t) (((tag_t)(t) << PTR_TAG_BITS) | PTR_TAG_DATA)
/* Encode the CASEx/LOOKx/CON/ARM as above */
#define MK_CASE(t, m) MK_TAG(((tag_t)(m) << PTR_TAG_TAG_BITS) | (t))
#define MK_CON(t, k, n) MK_CASE(t, ((tag_t)(n) << PTR_TAG_NCON_BITS) | (k))

/* is it NIL_NODEPTR? */
static INLINE bool
is_NIL(NODEPTR n)
{
  return n.node_bits == 0;
}

/* Is the NODEPTR a virtual pointer, i.e., an encoded tag?
 * Every NODEPTR has LSB=0 (...00 real pointer, ...10 virtual), so
 * testing PTR_TAG_TAG alone is sufficient.
 */
static ALWAYS_INLINE bool
is_VIRT(NODEPTR n)
{
  XASSERT((n.node_bits & PTR_AP_MASK) == 0);
  return (n.node_bits & PTR_TAG_TAG) != 0;
}

/* If the NODEPTR is a real pointer, return it. */
static INLINE struct node*
get_node_ptr(NODEPTR n)
{
  if (!is_VIRT(n))
    return n.node_ptr;
  else
    return NULL;
}

/* For a FUN part of a struct node */
static INLINE bool
is_AP(NODEPTR n)
{
  return (n.node_bits & PTR_AP_MASK) == PTR_AP_AP;
}

#if EXTRASANITY
/* For a FUN part of a struct node */
static INLINE bool
is_DATA(NODEPTR n)
{
  return (n.node_bits & PTR_TAG_MASK) == PTR_TAG_DATA;
}
#endif

/* For a non-AP FUN part of a struct node (i.e., is_DATA(n) is true) */
static ALWAYS_INLINE enum node_tag
get_DATA_TAG(NODEPTR n)
{
  XASSERT(is_DATA(n));
  return (enum node_tag)(n.node_bits >> PTR_TAG_BITS);
}

static INLINE NODEPTR
mk_TAG(enum node_tag t)
{
  NODEPTR n;
  n.node_bits = MK_TAG(t);
  return n;
}

static INLINE NODEPTR
mk_DATA_TAG(enum node_tag t)
{
  NODEPTR n;
  n.node_bits = MK_DATA_TAG(t);
  return n;
}

static INLINE NODEPTR
mk_CASE(enum node_tag t, int num_constr)
{
  NODEPTR n;
  n.node_bits = MK_CASE(t, num_constr);
  return n;
}

/* t is V_CONSTR, k is the constructor number, n the number of arguments */
static INLINE NODEPTR
mk_CON(enum node_tag t, int constr_no, int num_arg)
{
  NODEPTR n;
  n.node_bits = MK_CON(t, constr_no, num_arg);
  return n;
}

/* The tag of a virtual NODEPTR (without the extra info) */
static INLINE enum node_tag
get_VIRT_TAG(NODEPTR n)
{
  return (enum node_tag)((n.node_bits >> PTR_TAG_BITS) & PTR_TAG_TAG_MASK);
}

static INLINE bool
eq_NODEPTR(NODEPTR p, NODEPTR q)
{
  return p.node_ptr == q.node_ptr;
}

/***************************************/


struct ioarray;
struct bytestring;
struct forptr;
struct mthread;
struct mvar;
struct weak_ptr;

typedef struct PACKED node {
  NODEPTR            ufun;
  union {
    NODEPTR          uuarg;
    NODEPTR          uuind;
    value_t          uuvalue;
#if WANT_FLOAT32
    flt32_t          uuflt32value;
    uint32_t         uuint32value;
#endif  /* WANT_FLOAT32 */
#if WANT_FLOAT64
    flt64_t          uuflt64value;
#endif  /* WANT_FLOAT32 */
#if WANT_INT64
    int64_t          uuint64value;
#endif  /* WANT_INT64 */
    const char      *uucstring;
    void            *uuptr;
    HsFunPtr         uufunptr;
    struct ioarray  *uuarray;
    struct forptr   *uuforptr;      /* foreign pointers and byte arrays */
    struct mthread  *uuthread;
    struct mvar     *uumvar;
    struct weak_ptr *uuweak;
  } uarg;
} node;

#if 0
/* If the NODEPTR points to an application, return the FUN */
static INLINE NODEPTR
get_FUN(NODEPTR n)
{
  struct node *p = get_node_ptr(n);
  if (p) {
    NODEPTR q = p->ufun;
    if ((q.node_bits & PTR_AP_MASK) == PTR_AP_AP)
      return q;
  }
  return NIL_NODEPTR;
}
#endif

/* Is the non-NIL NODEPTR a data node with tag t? */
static ALWAYS_INLINE bool
is_DATA_NODE(NODEPTR n, enum node_tag t)
{
  XASSERT(!is_NIL(n));
  return !is_VIRT(n) && n.node_ptr->ufun.node_bits == MK_DATA_TAG(t);
}

/* If the NODEPTR points to an indirection, return it */
static INLINE NODEPTR
get_IND(NODEPTR n)
{
  struct node *p = get_node_ptr(n);
  if (p && p->ufun.node_bits == MK_DATA_TAG(D_IND))
    return p->uarg.uuind;
  return NIL_NODEPTR;
}

static ALWAYS_INLINE void
set_IND(NODEPTR n, NODEPTR t)
{
#if EXTRASANITY
  struct node *p = get_node_ptr(n);
  if (!p)
    ERR("set_IND");
#else
  struct node *p = n.node_ptr;
#endif
  p->ufun = mk_DATA_TAG(D_IND);
  p->uarg.uuind = t;
}

/* Set the tag of a data node, n is a real pointer */
static ALWAYS_INLINE void
set_DATA_TAG(NODEPTR n, enum node_tag t)
{
  XASSERT(get_node_ptr(n) != NULL);
  n.node_ptr->ufun = mk_DATA_TAG(t);
}

/*
 * The tag of any NODEPTR (this is what GETTAG used to be):
 *  - virtual pointer: the encoded tag (T_xxx or V_xxx)
 *  - real pointer to an application: X_AP
 *  - real pointer to a data node: the D_xxx tag (including D_IND)
 */
static INLINE enum node_tag
get_XTAG(NODEPTR n)
{
  if (is_VIRT(n))
    return get_VIRT_TAG(n);
  NODEPTR f = n.node_ptr->ufun;
  if (is_AP(f))
    return X_AP;
  return get_DATA_TAG(f);
}

/* Number of arms of a virtual V_LOOKD */
static ALWAYS_INLINE int
get_CASE_size(NODEPTR n)
{
  XASSERT(is_VIRT(n));
  tag_t t = n.node_bits >> PTR_TAG_BITS;
  return (t >> PTR_TAG_TAG_BITS) & PTR_TAG_NCON_MASK;
}

/*
 * Case arities.
 * A dense case, D_a0_..._a{m-1}, has m arms, and the arm for constructor k
 * takes ak arguments, the fields of the constructor.  A sparse case,
 * E_k1_n1_..._km_nm, has m arms, and arm j is for constructor kj, which has
 * nj fields; the kj are increasing.  The interpreter uses m, and the
 * constructor numbers of a sparse case, but the arities are only needed to
 * turn the graph back into code, since they cannot be read back from the arms.
 * They are kept in a table, and a V_CASED or V_CASES holds an index into it.
 * Equal entries are shared, and the entries are never freed.
 */
struct case_arity {
  uint16_t m;                   /* number of arms */
  uint8_t *arities;             /* arities[j] is the number of fields of the constructor of arm j */
  uint16_t *tags;               /* sparse: tags[j] is the constructor number of arm j; dense: NULL */
};
static struct case_arity *case_arities;  /* the table */
static uint32_t num_case_arities;        /* number of entries in use */
static uint32_t max_case_arities;        /* number of entries allocated */

#define PTR_TAG_CASED_BITS (PTR_TAG_NCON_BITS + PTR_TAG_NARG_BITS)  /* bits for the table index */
#define MAX_CASE_ARITIES ((uint32_t)1 << PTR_TAG_CASED_BITS)

/* The index in the table of a virtual V_CASED, V_CASES, or V_LOOKS */
static ALWAYS_INLINE uint32_t
get_case_index(NODEPTR n)
{
  XASSERT(is_VIRT(n));
  tag_t t = n.node_bits >> PTR_TAG_BITS;
  return (t >> PTR_TAG_TAG_BITS) & (MAX_CASE_ARITIES - 1);
}

/* The case arities of a virtual V_CASED, V_CASES, or V_LOOKS */
static ALWAYS_INLINE struct case_arity *
get_case_arity(NODEPTR n)
{
  return &case_arities[get_case_index(n)];
}

/* Find the entry in the table, or add it, and return the index.
 * tags is NULL for a dense case.
 * Combinator code is rarely loaded, and the table is small,
 * so a linear search will do. */
static uint32_t
find_case_arity(uint32_t m, const uint16_t *tags, const uint8_t *arities)
{
  for (uint32_t i = 0; i < num_case_arities; i++) {
    struct case_arity *c = &case_arities[i];
    if (c->m == m && memcmp(c->arities, arities, m) == 0 &&
        (tags ? c->tags && memcmp(c->tags, tags, m * sizeof(uint16_t)) == 0 : !c->tags))
      return i;
  }
  if (num_case_arities >= max_case_arities) {
    if (num_case_arities >= MAX_CASE_ARITIES)
      ERR("too many case arities");
    max_case_arities = max_case_arities ? 2 * max_case_arities : 64;
    case_arities = mrealloc(case_arities, max_case_arities * sizeof(struct case_arity));
  }
  struct case_arity *c = &case_arities[num_case_arities];
  c->m = m;
  c->arities = mmalloc(m);
  memcpy(c->arities, arities, m);
  c->tags = 0;
  if (tags) {
    c->tags = mmalloc(m * sizeof(uint16_t));
    memcpy(c->tags, tags, m * sizeof(uint16_t));
  }
  return num_case_arities++;
}

/* Constructor number and number of arguments of a virtual V_CONSTR */
static ALWAYS_INLINE void
get_CON_info(NODEPTR n, int *kp, int *np)
{
  XASSERT(is_VIRT(n));
  tag_t t = n.node_bits >> PTR_TAG_BITS;
  *kp = (t >> PTR_TAG_TAG_BITS) & PTR_TAG_NCON_MASK;
  *np = (t >> (PTR_TAG_TAG_BITS + PTR_TAG_NCON_BITS)) & PTR_TAG_NARG_MASK;
}

static INLINE void
mk_AP(NODEPTR n, NODEPTR fun, NODEPTR arg)
{
  n.node_ptr->ufun = fun;
  n.node_ptr->uarg.uuarg = arg;
}

#define HEAPREF(i) &cells[(i)]
#define NPTR(n) ((n).node_ptr)
#define GETVALUE(p) NPTR(p)->uarg.uuvalue
#define GETINT64VALUE(p) NPTR(p)->uarg.uuint64value
#define GETINT32VALUE(p) NPTR(p)->uarg.uuint32value
#define GETFLTVALUE(p) NPTR(p)->uarg.uuflt32value
#define GETDBLVALUE(p) NPTR(p)->uarg.uuflt64value
#define SETVALUE(p,v) NPTR(p)->uarg.uuvalue = v
#define SETINT64VALUE(p,v) NPTR(p)->uarg.uuint64value = v
#define SETINT32VALUE(p,v) NPTR(p)->uarg.uuint32value = v
#define SETFLTVALUE(p,v) NPTR(p)->uarg.uuflt32value = v
#define SETDBLVALUE(p,v) NPTR(p)->uarg.uuflt64value = v
#define FUN(p) NPTR(p)->ufun
#define ARG(p) NPTR(p)->uarg.uuarg
#define CSTR(p) NPTR(p)->uarg.uucstring
#define PTR(p) NPTR(p)->uarg.uuptr
#define FUNPTR(p) NPTR(p)->uarg.uufunptr
#define FORPTR(p) NPTR(p)->uarg.uuforptr
#define BSTR(p) NPTR(p)->uarg.uuforptr->payload
#define ARR(p) NPTR(p)->uarg.uuarray
#define THR(p) NPTR(p)->uarg.uuthread
#define MVAR(p) NPTR(p)->uarg.uumvar
#define WEAK(p) NPTR(p)->uarg.uuweak

#define NODE_SIZE sizeof(node)
#define LABEL(n) ((heapoffs_t)((n).node_ptr - cells))
node *cells;                 /* All cells */

/* Prefetch a node we know we'll need soon but haven't dereferenced yet. */
#if defined(__GNUC__) || defined(__clang__)
#define PREFETCH_READ(addr) __builtin_prefetch((addr), 0, 2)
#else
#define PREFETCH_READ(addr) do { } while(0)
#endif

/*
 * Byte arrays.
 * This is used for both immutable and mutable arrays.
 * This struct is often passed by value.
 */
struct bytestring {
  size_t   bs_size;                  /* current size of string */
  size_t   bs_capacity;              /* size allocated for string, 0 if immutable */
  void    *bs_array;                 /* bytes */
};

/* Create a new immutable bytestring, if buf is NULL also allocates the array */
struct bytestring
mk_ro_bytestring(size_t size, void *buf)
{
  struct bytestring bs;
  bs.bs_capacity = 0;
  bs.bs_size = size;
  bs.bs_array = buf ? buf : mmalloc(size);
  return bs;
}

/*
 * Arrays are allocated with malloc()/free().
 * During GC they are marked, and all elements in the array are
 * recursively marked.
 * At the end of the the mark phase there is a scan of all
 * arrays, and the unmarked ones are freed.
 */
struct ioarray {
  struct ioarray *next;         /* all ioarrays are linked together */
  bool permanent;               /* this array should never be GC-ed */
  size_t marked;                /* marked during GC */
  size_t size;                  /* number of elements in the array */
  NODEPTR array[];              /* actual size may be bigger */
};
struct ioarray *array_root = 0; /* root of all allocated arrays, linked by next */

enum fptype {
  FP_FORPTR = 0,                /* a regular foreign pointer to unknown memory */
  FP_BSTR,                      /* a bytestring */
  FP_MPZ,                       /* a GMP MPZ pointer */
};

/*
 * A Haskell ForeignPtr has a normal pointer, and a finalizer
 * function that is to be called when there are no more references
 * to the ForeignPtr.
 * A complication is that using plusForeignPtr creates a new
 * ForeignPtr that must share the same finalizer.
 * There is one struct forptr for each ForeignPtr.  It has pointer
 * to the actual data, and to a struct final which is shared between
 * all ForeignPtrs that have been created with plusForeignPtr.
 * During GC the used bit is set for any references to the forptr.
 * The scan phase will traverse the struct final chain and run
 * the finalizer, and free associated structs.
 */
struct final {
  struct final  *next;      /* the next finalizer */
  HsFunPtr       final;     /* function to call to release resource */
  void          *arg;       /* argument to final when called */
  size_t         size;      /* size of memory, if known, otherwise NOSIZE */
#define NOSIZE (~(size_t)0) /* used as the size in payload for actual foreign pointers */
  struct forptr *back;      /* back pointer to the first forptr */
  short          marked;    /* mark bit for GC */
  enum fptype    fptype;    /* what kind of foreign pointer */
};

/*
 * Foreign pointers are also used to represent bytestrings.
 * The difference between a foreign pointer and a bytestring
 * is that we can serialize the latter.
 * The size field is non-zero only for bytestrings.
 */
struct forptr {
  struct forptr *next;       /* the next ForeignPtr that shares the same finalizer */
  struct final  *finalizer;  /* the finalizer for this ForeignPtr */
  struct bytestring payload; /* the actual pointer to allocated data, and maybe a size */
  //  char          *desc;
};
struct final *final_root = 0;   /* root of all allocated foreign pointers, linked by next */

/*
 * For Haskell code that reads the graph, see lib/Mhs/Extract.hs.
 * The primitive nodeAddr gives the NODEPTR of a value, without evaluating it,
 * and IO.rtsinfo a pointer to this: the layout of the nodes, and the tables
 * and tags needed to make sense of them.  Every field is a word, so field i
 * is at offset i * sizeof(void*), and the Haskell code knows them by their
 * positions; change the version when they change.  It also relies on the
 * layout of struct case_arity and struct ffi_entry: three words each, with
 * the fields in order.
 */
static struct {
  uintptr_t                version;
  /* The nodes */
  uintptr_t                node_size;         /* sizeof(struct node) */
  uintptr_t                node_payload;      /* offsetof(struct node, uarg) */
  /* The tables */
  uintptr_t                last_tag;          /* T_LAST_TAG */
  const char             **tag_names;         /* the name of each tag, or NULL */
  const uint8_t           *tag_arity;         /* the arity of each tag, see arity_virt */
  struct case_arity      **case_arities;      /* the table, which moves when it grows */
  const struct ffi_entry  *ffi_table;         /* the FFI functions of the runtime, */
  int                     *num_ffi;           /*   and how many there are, */
  const struct ffi_entry **xffi_table;        /*   then those of the program, see FFI_IX */
  /* Tags */
  uintptr_t                d_ind, d_int, d_int64, d_dbl, d_flt32, d_ptr, d_funptr, d_forptr,
                           d_arr, d_thid, d_mvar, d_weak, d_io_ccall, d_baddyn, d_tick;
  uintptr_t                v_constr, v_cased, v_cases;
  /* Bytestrings */
  uintptr_t                forptr_finalizer;  /* offsetof(struct forptr, finalizer) */
  uintptr_t                forptr_payload;    /* offsetof(struct forptr, payload) */
  uintptr_t                final_fptype;      /* offsetof(struct final, fptype) */
  uintptr_t                fp_bstr;           /* FP_BSTR */
} rts_info = {
  2,
  sizeof(struct node), offsetof(struct node, uarg),
  T_LAST_TAG, tag_names, tag_arity, &case_arities, ffi_table, &num_ffi, &xffi_table,
  D_IND, D_INT, D_INT64, D_DBL, D_FLT32, D_PTR, D_FUNPTR, D_FORPTR,
  D_ARR, D_THID, D_MVAR, D_WEAK, D_IO_CCALL, D_BADDYN, D_TICK,
  V_CONSTR, V_CASED, V_CASES,
  offsetof(struct forptr, finalizer), offsetof(struct forptr, payload),
  offsetof(struct final, fptype), FP_BSTR,
};

counter_t num_reductions = 0;
counter_t num_alloc = 0;
counter_t num_gc = 0;
counter_t num_yield = 0;
counter_t num_resched = 0;
counter_t num_thread_reap = 0;
counter_t num_mvar_alloc = 0;
counter_t num_mvar_free = 0;
counter_t num_stable_alloc = 0;
counter_t num_stable_free = 0;
counter_t num_new_weak = 0;
counter_t num_gc_weak = 0;
uintptr_t gc_mark_time = 0;
uintptr_t gc_scan_time = 0;
uintptr_t run_time = 0;
uintptr_t boot_time = 0;

#define MAIN_THREAD 1
uvalue_t num_thread_create = MAIN_THREAD;

#define MAXSTACKDEPTH 0
#if MAXSTACKDEPTH
stackptr_t max_stack_depth = 0;
counter_t max_c_stack = 0;
counter_t cur_c_stack = 0;
#define MAXSTACK if (STACK_INDEX(stack_ptr) > max_stack_depth) max_stack_depth = STACK_INDEX(stack_ptr)
#else
#define MAXSTACK
#endif

NODEPTR atptr;

/* The stack grows downwards, stack_ptr points to the top element.
 * The empty stack has stack_ptr == stack_bot, just past the end of the stack area. */
NODEPTR *stack_bot;         /* the empty stack_ptr; the first element is stack_bot[-1] */
NODEPTR *stack_ptr;         /* the top element */
NODEPTR *stack_limit;       /* PUSH fails when stack_ptr reaches this */
#if STACKOVL
#define PUSH(x) do { if (stack_ptr <= stack_limit) stackerr(); *--stack_ptr = (x); MAXSTACK; } while(0)
#else  /* STACKOVL */
#define PUSH(x) do {                                        *--stack_ptr = (x); MAXSTACK; } while(0)
#endif  /* STACKOVL */
#define TOP(n) stack_ptr[(n)]
#define POP(n) stack_ptr += (n)
#define POPTOP() (*stack_ptr++)
/* The number of entries pushed after stack_ptr was p */
#define STACK_ABOVE(p) ((p) - stack_ptr)
/* The index of the stack slot p, counting from the bottom, i.e., stack_bot[-1] has index 0 */
#define STACK_INDEX(p) (stack_bot - 1 - (p))
#define GCCHECK(n) gc_check((n))
#define CLEARSTK() do { stack_ptr = stack_bot; } while(0)
#define GCCHECKSAVE(p, n) do { PUSH(p); GCCHECK(n); (p) = TOP(0); POP(1); } while(0)

heapoffs_t heap_size;       /* number of heap cells */
heapoffs_t heap_start;      /* first location in heap that needs GC */
stackptr_t stack_size;      /* number of stack slots */

counter_t num_marked;
counter_t max_num_marked = 0;
counter_t num_free;
counter_t num_arr_alloc;
counter_t num_arr_free;
counter_t num_fin_alloc;
counter_t num_fin_free;
counter_t num_bs_alloc;
counter_t num_bs_alloc_max;
counter_t num_bs_free;
counter_t num_bs_bytes;
counter_t num_bs_inuse;
counter_t num_bs_inuse_max;

#define BITS_PER_WORD (sizeof(bits_t) * 8)
bits_t *free_map;             /* 1 bit per node, 0=free, 1=used */
heapoffs_t free_map_nwords;
heapoffs_t next_scan_index;

int want_gc_red = 0;

NORETURN void
memerr(void)
{
  ERR("Out of memory");
}

NORETURN
void
stackerr(void)
{
  ERR("stack overflow");
}

/***************************************/

#include "bfile.c"

/***************************************/

struct ioarray*
arr_alloc(size_t sz, NODEPTR e)
{
  if (sz > (SIZE_MAX - offsetof(struct ioarray, array)) / sizeof(NODEPTR))
    ERR("ARR_ALLOC");
  struct ioarray *arr = mmalloc(offsetof(struct ioarray, array) + sz * sizeof(NODEPTR));
  size_t i;

  arr->next = array_root;
  array_root = arr;
  arr->marked = 0;
  arr->permanent = false;
  arr->size = sz;
  for(i = 0; i < sz; i++)
    arr->array[i] = e;
  //PRINT("arr_alloc(%d, %p) = %p\n", (int)sz, e, arr);
  num_arr_alloc++;
  return arr;
}

struct ioarray*
arr_copy(struct ioarray *oarr)
{
  size_t sz = oarr->size;
  struct ioarray *arr = mmalloc(offsetof(struct ioarray, array) + sz * sizeof(NODEPTR));

  arr->next = array_root;
  array_root = arr;
  arr->marked = 0;
  arr->permanent = false;
  arr->size = sz;
  memcpy(arr->array, oarr->array, sz * sizeof(NODEPTR));
  num_arr_alloc++;
  return arr;
}

/*****************************************************************************/

#if WANT_TICK
struct tick_entry {
  struct bytestring tick_name;
  counter_t tick_count;
} *tick_table = 0;
size_t tick_table_size;
size_t tick_index;

/* Allocate a new tick table entry and return the index. */
size_t
add_tick_table(struct bytestring name)
{
  if (!tick_table) {
    tick_table_size = 100;
    tick_table = mmalloc(tick_table_size * sizeof(struct tick_entry));
    tick_index = 0;
  }
  if (tick_index >= tick_table_size) {
    tick_table_size *= 2;
    tick_table = mrealloc(tick_table, tick_table_size * sizeof(struct tick_entry));
  }
  tick_table[tick_index].tick_name = name;
  tick_table[tick_index].tick_count = 0;
  return tick_index++;
}

/* Called with the tick index. */
static INLINE void
dotick(value_t i)
{
  tick_table[i].tick_count++;
}

void
dump_tick_table(FILE *f)
{
  if (!tick_table) {
    fprintf(f, "Tick table empty\n");
    return;
  }
  for (size_t i = 0; i < tick_index; i++) {
    counter_t n = tick_table[i].tick_count;
    if (n)
      fprintf(f, "%-60s %10"PRIcounter"\n", (char *)tick_table[i].tick_name.bs_array, n);
  }
}
#endif

enum th_sched { mt_main, mt_resched, mt_raise };
/* The two enums below are known by the Haskell code.  Do not change order */
enum th_state {
  ts_runnable,
  ts_wait_mvar,
  ts_wait_time,
  ts_finished,
  ts_died,
  ts_wait_io,   /* not visible to Haskell; must stay after ts_died */
};
enum mask_state { mask_unmasked, mask_interruptible, mask_uninterruptible };

/***************** HANDLER *****************/

struct handler {
  jmp_buf         hdl_buf;      /* env storage */
  struct handler *hdl_old;      /* old handler */
  NODEPTR         hdl_exn;      /* used temporarily to pass the exception value */
} *cur_handler = 0;

/***************** THREAD ******************/

struct mthread {
  enum th_state   mt_state;      /* thread state */
  enum mask_state mt_mask;       /* making state. */
  struct mthread *mt_next;       /* all threads linked together */
  struct mthread *mt_queue;      /* runq/waitq link */
  counter_t       mt_slice;      /* reduction steps until yielding */
  counter_t       mt_num_slices; /* number of slices so far */
  NODEPTR         mt_root;       /* root of the graph to reduce */
  struct mvar    *mt_exn;        /* possible thrown exception */
  struct mthread *mt_throwto;    /* When set, we are in throwTo to this thread and have
                                  * already handed over the exception; we are waiting for
                                  * the target to take it.  See throwto(). */
  bool            mt_throwdone;  /* The target has taken the exception (or is gone). */
  NODEPTR         mt_mval;       /* Filled when put_mvar wakes a thread waiting in read_mvar.
                                  * The value cannot be taken from the mvar by the read, because
                                  * we need to guarantee that all reads get the same value. */
  bool            mt_mark;       /* marked as accessible */
  uvalue_t        mt_id;         /* thread number, thread 1 is the main thread */
#if WANT_IO_POLL
  int             mt_fd;         /* The file descriptor that we are waiting on,
                                  * IO_POLL_WAITING_FOR_NONE, or IO_POLL_EVENT_HAS_HAPPENED */
#define             IO_POLL_WAITING_FOR_NONE (-1)
#define             IO_POLL_EVENT_HAS_HAPPENED (-2)
  int             mt_events;     /* POLLIN or POLLOUT */
#endif /* WANT_IO_POLL */
#if defined(CLOCK_INIT)
  CLOCK_T         mt_at;         /* time to wake up when in threadDelay */
#endif
};
struct mthread  *all_threads = 0;   /* all threads */

struct mqueue {
  struct mthread *mq_head;
  struct mthread *mq_tail;
};
struct mqueue runq  = { 0, 0 }; /* runnable threads */
struct mqueue timeq = { 0, 0 }; /* waiting for a timer to expire, sorted in time order */
struct mqueue pollq = { 0, 0 }; /* waiting for I/O on a file descriptor */

struct mvar {
  struct mvar    *mv_next;      /* all mvars linked together */
  NODEPTR         mv_data;      /* contents of the mvar, or NIL when empty */
  struct mqueue   mv_takeput;   /* queue of threads waiting for take or put, single wakeup */
  struct mqueue   mv_read;      /* queue of threads waiting for read, multiple wakeup */
  bool            mv_mark;      /* marked as accessible */
};
struct mvar      *all_mvars = 0;   /* all mvars */

jmp_buf          sched;             /* jump here to yield */
counter_t        slice = SLICE;     /* normal time slice;
                                     * on an M4 Mac this is about 0.3ms */
int glob_slice;

NODEPTR          the_exn;       /* Used to propagate the exception for longjmp(sched, mt_raise) */

/****** StablePtr ******/

size_t sp_capacity = 4;         /* initial size of stable pointer table */
NODEPTR *sp_table;              /* stable pointer table */

static void
init_stableptr(void)
{
  sp_table = mmalloc(sp_capacity * sizeof(NODEPTR)); /* stable pointer table, all free */
  for (size_t i = 0; i < sp_capacity; i++)
    sp_table[i] = NIL_NODEPTR;
}

static uvalue_t
new_stableptr(NODEPTR n)
{
  size_t i;

  COUNT(num_stable_alloc);
  /* Linear search for an empty slot. */
  /* Not ideal, but fine for a small number of StablePtr. */
  for(i = 1; i < sp_capacity; i++) { /* index 0 reserved according to the spec */
    if (is_NIL(sp_table[i]))
      break;
  }
  if (i == sp_capacity) {
    /* table is full, so double its size */
    sp_capacity *= 2;
    sp_table = mrealloc(sp_table, sp_capacity * sizeof(NODEPTR));
    for(size_t j = i; j < sp_capacity; j++)
      sp_table[j] = NIL_NODEPTR;
  }
  sp_table[i] = n;
  return (uvalue_t)i;
}

static NODEPTR
deref_stableptr(uvalue_t sp)
{
  if (sp >= sp_capacity || is_NIL(sp_table[sp]))
    ERR("deref_stableptr");
  return sp_table[sp];
}

static void
free_stableptr(uvalue_t sp)
{
  if (sp >= sp_capacity || is_NIL(sp_table[sp]))
    ERR("free_stableptr");
  COUNT(num_stable_free);
  sp_table[sp] = NIL_NODEPTR;
}

/* The order of these must be kept in sync with Control.Exception.Internal.rtsExn */
enum rts_exn { exn_stackoverflow, exn_heapoverflow, exn_threadkilled, exn_userinterrupt,
               exn_dividebyzero, exn_blockedmvar, exn_blockedstm, exn_overflow,
               exn_serialize, exn_deserialize, exn_nontermination, exn_last };

NORETURN void raise_exn(NODEPTR exn);
struct mvar* new_mvar(void);
NODEPTR take_mvar(bool try, struct mvar *mv);
NORETURN void die_exn(NODEPTR exn);
void thread_intr(struct mthread *mt);
int put_mvar(bool try, struct mvar *mv, NODEPTR v);
NODEPTR mkInt(value_t i);
NODEPTR mkInt64(int64_t i);
NODEPTR mkFlt32(flt32_t d);
NODEPTR mkFlt64(flt64_t d);
NODEPTR mkPtr(void* p);
struct mthread* new_thread(NODEPTR root);
void gc(void);
void async_throwto(struct mthread*, NODEPTR);

#if WANT_STDIO
void pp(FILE*, NODEPTR);
#endif

/* Needed during reduction */
NODEPTR intTable[HIGH_INT - LOW_INT];

#define combK               mk_TAG(T_K)
#define combA               mk_TAG(T_A)
#define combI               mk_TAG(T_I)
#define combB               mk_TAG(T_B)
#define combC               mk_TAG(T_C)
#define combZ               mk_TAG(T_Z)
#define combU               mk_TAG(T_U)
#define combK2              mk_TAG(T_K2)
#define combK3              mk_TAG(T_K3)
#define combBB              mk_TAG(T_BB)
#define combCC              mk_TAG(T_CC)
#define combP               mk_TAG(T_P)
#define combIOBIND          mk_TAG(T_IO_BIND)
#define combIORETURN        mk_TAG(T_IO_RETURN)
#define combIOTHEN          mk_TAG(T_IO_THEN)
#define combSETMASKINGSTATE mk_TAG(T_IO_SETMASKINGSTATE)
#define combPERFORMIO       mk_TAG(T_IO_PERFORMIO)
#define combBININT1         mk_TAG(T_BININT1)
#define combBININT2         mk_TAG(T_BININT2)
#define combUNINT1          mk_TAG(T_UNINT1)
#define combBININT64_1      mk_TAG(T_BININT64_1)
#define combBININT64_2      mk_TAG(T_BININT64_2)
#define combUNINT64_1       mk_TAG(T_UNINT64_1)
#define combBINFLT1         mk_TAG(T_BINFLT1)
#define combBINFLT2         mk_TAG(T_BINFLT2)
#define combUNFLT1          mk_TAG(T_UNFLT1)
#define combBINDBL1         mk_TAG(T_BINDBL1)
#define combBINDBL2         mk_TAG(T_BINDBL2)
#define combUNDBL1          mk_TAG(T_UNDBL1)
#define combBINBS1          mk_TAG(T_BINBS1)
#define combBINBS2          mk_TAG(T_BINBS2)
#define combTHROWTO         mk_TAG(T_IO_THROWTO)
#define combCATCHR          mk_TAG(T_CATCHR)
#define combFP2P            mk_TAG(T_FP2P)

#define combFalse           mk_CON(V_CONSTR, 0, 0)
#define combTrue            mk_CON(V_CONSTR, 1, 0)
#define combNothing         mk_CON(V_CONSTR, 0, 0)
#define combJust            mk_CON(V_CONSTR, 1, 1)
#define combUnit            mk_CON(V_CONSTR, 0, 0)
#define combNil             mk_CON(V_CONSTR, 0, 0)
#define combCons            mk_CON(V_CONSTR, 1, 2)
#define combPair            mk_CON(V_CONSTR, 0, 2)
#define combLT              mk_CON(V_CONSTR, 0, 0)
#define combEQ              mk_CON(V_CONSTR, 1, 0)
#define combGT              mk_CON(V_CONSTR, 2, 0)

NODEPTR spare_node;             /* an unused node in the heap, used in printrec */
NODEPTR combShowExn;
NODEPTR comb_stdin, comb_stdout, comb_stderr;

NODEPTR combPFst, combPSnd;
NODEPTR combPUnit;
NODEPTR combWorld;

/*******************************/

#if WANT_ARGS
/* This single element array hold a list of the program arguments. */
struct ioarray *argarray;
#endif  /* WANT_ARGS */

int verbose = 0;
int gcbell = 0;


#if WANT_SIGINT
volatile bool has_sigint = false;
void
handle_sigint(int s)
{
  has_sigint = true;
}
#endif

/* Run GC and verify at least k nodes are free afterward. */
static NOINLINE void
gc_for(size_t k)
{
  gc();
  if (k >= num_free)
    ERR("heap exhausted");
}

/* Check that there are k nodes available, if not then GC. */
INLINE void
gc_check(size_t k)
{
  if (k < num_free)
    return;
  gc_for(k);
}

/* Add the thread to the tail of runq */
void
add_q_tail(struct mqueue *q, struct mthread *mt)
{
  if (!q->mq_head) {
    /* q is empty, so mt goes first */
    q->mq_head = mt;
  } else {
    /* link mt to the end of the runq */
    q->mq_tail->mt_queue = mt;
  }
  q->mq_tail = mt;               /* mt is now last */
  mt->mt_queue = 0;           /* mt is last, so no next */
}

void
add_runq_tail(struct mthread *mt)
{
  mt->mt_state = ts_runnable;
  add_q_tail(&runq, mt);
}

/* Add the thread to the head of runq */
void
add_runq_head(struct mthread *mt)
{
  mt->mt_state = ts_runnable;
  mt->mt_queue = runq.mq_head;
  runq.mq_head = mt;
  if (!runq.mq_tail)
    runq.mq_tail = mt;           /* runq was empty */
}

/* Add the thread k to runq right after mt, which is in runq */
void
add_runq_after(struct mthread *mt, struct mthread *k)
{
  k->mt_state = ts_runnable;
  k->mt_queue = mt->mt_queue;
  mt->mt_queue = k;
  if (runq.mq_tail == mt)
    runq.mq_tail = k;            /* mt was last */
}

struct mthread*
remove_q_head(struct mqueue *q)
{
  struct mthread *mt = q->mq_head; /* front thread */
  if (!mt)
    return 0;
  q->mq_head = mt->mt_queue;       /* skip to next thread */
  if (!q->mq_head)
    q->mq_tail = 0;                /* q is now empty */
  return mt;
}

int
find_and_unlink(struct mqueue *mq, struct mthread *mt)
{
  struct mthread **mtp;

  for(mtp = &mq->mq_head; *mtp && *mtp != mt; mtp = &(*mtp)->mt_queue)
    ;
  if (!*mtp)
    return 0;                   /* not found */
  *mtp = mt->mt_queue;          /* unlink */
  if (*mtp)
    return 1;                   /* the unlinked thread was not the tail */
  if (mq->mq_head) {
    for (mt = mq->mq_head; mt->mt_queue; mt = mt->mt_queue)
      ;                         /* find the last element */
    mq->mq_tail = mt;
  } else {
    /* q is empty */
    mq->mq_tail = 0;
  }
  return 1;
}

/* This is a yucky hack */
bool doing_rnf = false;              /* REMOVE */
jmp_buf rnf_skip;                    /* rnf() without errors: give up evaluating a node */
struct rnf_state {
  bits_t           *rs_done;         /* visited-node bitmap to free on unwind */
  struct handler   *rs_hdl;          /* cur_handler active when rnf() started */
  struct rnf_state *rs_old;
} *cur_rnf = 0;
#if THREAD_DEBUG
const bool thread_trace = false;
#endif  /* THREAD_DEBUG */

/* clean up temporary globals to prepare for rescheduling */
void
cleanup(struct mthread *mt, enum th_state ts)
{
  /* We are going to reschedule, so clean up thread state:
   *  stack pointer
   *  error handlers
   */
#if THREAD_DEBUG
  if (thread_trace)
    printf("cleanup: %d state=%d\n", (int)mt->mt_id, ts);
#endif  /* THREAD_DEBUG */
  mt->mt_slice = STACK_INDEX(stack_ptr); /* we need this many reductions to just reach where we left off */
  mt->mt_state = ts;
  CLEARSTK();                 /* reset stack */
  doing_rnf = false;
  for (struct rnf_state *rs = cur_rnf; rs; rs = rs->rs_old)
    FREE(rs->rs_done);
  cur_rnf = 0;
  /* free all error handlers */
  for (struct handler *h = cur_handler; h; ) {
    struct handler *n = h;
    h = h->hdl_old;
    FREE(n);
  }
  cur_handler = 0;
}

/* reschedule, does not return */
NORETURN void
resched(struct mthread *mt, enum th_state ts)
{
  cleanup(mt, ts);
  longjmp(sched, mt_resched);
}

#if THREAD_DEBUG
void
dump_q(const char *s, struct mqueue q)
{
  printf("   %s=[", s);
  for(struct mthread *mt = q.mq_head; mt; mt = mt->mt_queue) {
    printf("%d ", (int)mt->mt_id);
  }
  printf("]\n");
}
#endif  /* THREAD_DEBUG */

/* Check if its time to wake up some threads waiting for a time. */
void
check_timeq(void)
{
#if defined(CLOCK_INIT)
  CLOCK_T now = CLOCK_GET();
  while (timeq.mq_head && timeq.mq_head->mt_at <= now) {
    struct mthread *mt = remove_q_head(&timeq);
    add_runq_tail(mt);
    mt->mt_at = -1;             /* indicate that the delay has expired */
#if THREAD_DEBUG
    if (thread_trace)
      printf("check_timeq: %d done\n", (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
  }
#if THREAD_DEBUG
  if (thread_trace) {
    printf("check_timeq: exit\n");
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
#endif
}

void
check_pollq(int timeout)
{
#if WANT_IO_POLL
#define MAX_POLL_FDS 100
  struct pollfd fds[MAX_POLL_FDS];
  int nfds = 0;
  for(struct mthread *mt = pollq.mq_head; mt; mt = mt->mt_queue) {
    if (nfds >= MAX_POLL_FDS)
      ERR("check_pollq: too many FDs");
    fds[nfds].fd = mt->mt_fd;
    fds[nfds].events = mt->mt_events;
    nfds++;
  }
#if THREAD_DEBUG
  if (thread_trace)
    printf("check_pollq: enter poll(_, %d, %d)\n", nfds, timeout);
#endif  /* THREAD_DEBUG */
  int r = poll(fds, nfds, timeout);
  if (r < 0)
    return;                     /* silently ignore errors */
  nfds = 0;
  struct mthread *next;
  for(struct mthread *mt = pollq.mq_head; mt; mt = next) {
    next = mt->mt_queue;
    if (fds[nfds].revents & (mt->mt_events | POLLHUP | POLLERR | POLLNVAL)) {
      /* Some event has happened, move the thread back to the runq. */
      find_and_unlink(&pollq, mt); /* remove from I/O queue */
      add_runq_tail(mt);
#if THREAD_DEBUG
    if (thread_trace)
      printf("check_pollq: FD=%d thread=%d done\n", mt->mt_fd, (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
      mt->mt_fd = IO_POLL_EVENT_HAS_HAPPENED;
    }
    nfds++;
  }
#if THREAD_DEBUG
  if (thread_trace) {
    printf("check_pollq: exit\n");
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
#endif  /* WANT_IO_POLL */
}

/* Wake every thread that is blocked in throwTo waiting for mt to take its
 * exception.  Called when mt takes one, and when mt finishes or dies. */
void
wake_throwto(struct mthread *mt)
{
  for (struct mthread *k; (k = remove_q_head(&mt->mt_exn->mv_read)); ) {
#if THREAD_DEBUG
    if (thread_trace)
      printf("wake_throwto: wake %d, which threw to %d\n", (int)k->mt_id, (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
    /* The exception has been delivered, so the thrower continues next: at
     * the tail of the run queue it would first wait for a slice of every
     * other runnable thread, which made a loop of killThread quadratic. */
    k->mt_throwdone = true;
    if (runq.mq_head == mt)
      add_runq_after(mt, k);    /* mt is the running thread (check_thrown): k goes right after it */
    else
      add_runq_head(k);
  }
}

/* mt has finished or died, so it takes no more exceptions.  Release everyone
 * blocked in throwTo to it: the thrower whose exception is in mt's exception
 * MVar waits on its read queue, and further throwers wait in its put queue
 * because the MVar is full.  Each of them runs throwTo again, finds mt gone,
 * and returns. */
void
release_throwto(struct mthread *mt)
{
  wake_throwto(mt);
  mt->mt_exn->mv_data = NIL_NODEPTR; /* an exception that was never taken */
  for (struct mthread *k; (k = remove_q_head(&mt->mt_exn->mv_takeput)); ) {
#if THREAD_DEBUG
    if (thread_trace)
      printf("release_throwto: wake %d, which threw to %d\n", (int)k->mt_id, (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
    add_runq_tail(k);
  }
}

void
throwto(struct mthread *mt, NODEPTR exn)
{
#if THREAD_DEBUG
  if (thread_trace) {
    printf("throwto: id=%d\n", (int)mt->mt_id);
  }
#endif  /* THREAD_DEBUG */
  struct mthread *me = runq.mq_head;

  if (mt == me) {
    /* Throwing to ourselves: just hand the exception over.  It is raised at the
     * next interruption point, so there is nothing to wait for. */
    (void)put_mvar(false, mt->mt_exn, exn); /* never returns if it blocks */
    return;
  }
  if (me->mt_throwto == mt) {
    /* We handed the exception over in an earlier execution of this throwTo and
     * blocked; being back here means the target has taken it, or has died. */
#if THREAD_DEBUG
    if (thread_trace)
      printf("throwto: id=%d delivered\n", (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
    me->mt_throwto = 0;
    return;
  }
  thread_intr(mt);
  if (mt->mt_state == ts_died || mt->mt_state == ts_finished)
    return;                     /* there is nothing left to interrupt */
#if THREAD_DEBUG
  if (thread_trace) {
    printf("throwto: id=%d put_mvar exn\n", (int)mt->mt_id);
  }
#endif  /* THREAD_DEBUG */
  (void)put_mvar(false, mt->mt_exn, exn); /* never returns if it blocks */
  /* GHC's throwTo does not return until the exception has been raised in the
   * target thread, so wait for the target to take it.  Without this the target
   * stays runnable with an exception pending, and can still run a stretch of
   * code -- and consume shared state -- before it is interrupted.
   * We wait on the read queue of the target's exception MVar: check_thrown and
   * a finishing thread wake it, and thread_intr searches it, so the wait is
   * itself interruptible. */
  me = remove_q_head(&runq);
  me->mt_throwto = mt;
  me->mt_throwdone = false;
  add_q_tail(&mt->mt_exn->mv_read, me);
  /* Let the target run next.  If it is somewhere down the run queue (it was
   * runnable, or thread_intr has just put it at the tail) it would otherwise
   * take the exception only after every thread ahead of it has had a slice,
   * so killing n runnable threads cost n passes over the run queue. */
  if (mt->mt_state == ts_runnable && find_and_unlink(&runq, mt))
    add_runq_head(mt);
  resched(me, ts_wait_mvar);     /* never returns */
}

/* mt is interrupted while it waits in throwTo.  As in GHC, an interrupted throwTo
 * has no effect: if the target has not taken mt's exception yet, take it back.
 * (It is the one in the target's exception MVar, since mt waits on its read queue
 * until the target takes it.)  The next thrower waiting to put one may then go on. */
void
revoke_throwto(struct mthread *mt)
{
  struct mthread *target = mt->mt_throwto;
  if (target && !mt->mt_throwdone) {
#if THREAD_DEBUG
    if (thread_trace)
      printf("revoke_throwto: %d revokes its exception to %d\n", (int)mt->mt_id, (int)target->mt_id);
#endif  /* THREAD_DEBUG */
    /* Take our exception back; a thread waiting to throw to target may then put its own.
     * The MVar holds our exception, so this does not block. */
    (void)take_mvar(true, target->mt_exn);
  }
  mt->mt_throwto = 0;
  mt->mt_throwdone = false;
}

void
check_thrown(bool intr)
{
  struct mthread *me = runq.mq_head;
  if (me->mt_mask == mask_uninterruptible ||
      (!intr && me->mt_mask == mask_interruptible)) {
    /* interrupts are masked, so don't throw */
    return;
  }
  NODEPTR exn = take_mvar(true, me->mt_exn); /* get the exception */
  if (is_NIL(exn))
    return;            /* no thrown exception */
  /* the current thread has an async exception */
  wake_throwto(me);    /* the throwTo that sent it may now return */
  revoke_throwto(me);  /* we are unwinding; drop any throwTo of our own */
  /* Clear any pending resume state so a caught exception cannot leak it. */
  me->mt_mval = NIL_NODEPTR;
#if defined(CLOCK_INIT)
  me->mt_at = 0;
#endif
#if WANT_IO_POLL
  me->mt_fd = IO_POLL_WAITING_FOR_NONE;
#endif
#if THREAD_DEBUG
  if (thread_trace)
    printf("check_thrown: exn for %d\n", (int)me->mt_id);
#endif  /* THREAD_DEBUG */

  raise_exn(exn);
}

void
check_sigint(void)
{
#if WANT_SIGINT
  if (has_sigint) {
    /* We have a signal, so send an async exception to the main thread */
    has_sigint = false;
    for(struct mthread *mt = all_threads; mt; mt = mt->mt_next) {
      if (mt->mt_id == MAIN_THREAD) {
#if THREAD_DEBUG
        if (thread_trace)
          printf("sending signal to main\n");
#endif  /* THREAD_DEBUG */
        async_throwto(mt, mkInt(exn_userinterrupt));
        break;
      }
    }
  }
#endif
}

/* Used to detect calls to error while we are already in a call to error. */
bool in_raise = false;

/* Inlining makes very little difference */
/*static INLINE*/ void
yield(void)
{
  if (in_raise)                 /* don't context switch when we are dying */
    return;
  COUNT(num_yield);
  runq.mq_head->mt_num_slices++;
  // XXX should check mt_thrown here

  YIELD_EXTRA;                  /* platform specific extra stuff */

  if (timeq.mq_head)
    check_timeq();
  check_thrown(false);
  check_sigint();

  if (pollq.mq_head) {
    /* Check if any threads blocked on IO can be scheduled. Since we pass in a delay of 0, checking
     * for the events will not block. */
    check_pollq(0);
  }

// printf("yield %p %d\n", runq, (int)stack_ptr);
  /* if there is nothing after in the runq then there is no need to reschedule */
  if (!runq.mq_head->mt_queue) {
#if THREAD_DEBUG
    if (thread_trace) {
      printf("yield: %d no other threads\n", (int)runq.mq_head->mt_id);
      dump_q("runq", runq);
    }
#endif  /* THREAD_DEBUG */
    glob_slice = slice;
    num_reductions += glob_slice-1;
    return;
  }

  /* Unlink from runq */
  struct mthread *mt = remove_q_head(&runq);
  /* link into back of runq */
  add_runq_tail(mt);
#if THREAD_DEBUG
  if (thread_trace) {
    printf("yield: resched %d\n", (int)mt->mt_id);
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
  resched(mt, ts_runnable);
}

struct mthread*
new_thread(NODEPTR root)
{
  struct mthread *mt = mmalloc(sizeof(struct mthread));

#if THREAD_DEBUG
  if (thread_trace) {
    printf("new_thread: mt=%p root=%p\n", mt, root.node_ptr);
  }
#endif  /* THREAD_DEBUG */
  mt->mt_mask = mask_unmasked;
  mt->mt_root = root;
  mt->mt_exn = new_mvar();
  mt->mt_throwto = 0;
  mt->mt_throwdone = false;
  mt->mt_mval = NIL_NODEPTR;
  mt->mt_slice = 0;
  mt->mt_mark = false;
  mt->mt_num_slices = 0;
  mt->mt_id = num_thread_create++;
#if WANT_IO_POLL
  mt->mt_fd = IO_POLL_WAITING_FOR_NONE;
  mt->mt_events = 0;
#endif
#if defined(CLOCK_INIT)
  mt->mt_at = 0;                /* delay has not expired */
#endif

  /* add to all_threads */
  mt->mt_next = all_threads;
  all_threads = mt;

  /* add to tail of runq */
  add_runq_tail(mt);            /* sets runnable */
#if THREAD_DEBUG
  if (thread_trace) {
    printf("new_thread: add %d to runq tail\n", (int)mt->mt_id);
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
  return mt;
}

struct mvar*
new_mvar(void)
{
  COUNT(num_mvar_alloc);
  struct mvar *mv = mmalloc(sizeof(struct mvar));

  mv->mv_data = NIL_NODEPTR;
  mv->mv_takeput.mq_head = 0;
  mv->mv_takeput.mq_tail = 0;
  mv->mv_read.mq_head = 0;
  mv->mv_read.mq_tail = 0;

  /* add to all_mvars */
  mv->mv_next = all_mvars;
  mv->mv_mark = false;
  all_mvars = mv;

#if THREAD_DEBUG
  if (thread_trace)
    printf("new_mvar: mvar=%p\n", mv);
#endif  /* THREAD_DEBUG */
  return mv;
}

NODEPTR
take_mvar(bool try, struct mvar *mv)
{
#if THREAD_DEBUG
  if (thread_trace) {
    printf("take_mvar: start mvar=%p\n", mv);
    dump_q("takeput", mv->mv_takeput);
  }
#endif  /* THREAD_DEBUG */
  NODEPTR n;
  if (!is_NIL(n = mv->mv_data)) {
    /* The mvar is full. */
#if THREAD_DEBUG
    if (thread_trace)
      printf("take_mvar: mvar=%p full\n", mv);
#endif  /* THREAD_DEBUG */
    mv->mv_data = NIL_NODEPTR;           /* now empty */

    /* move one thread waiting to put to the runq */
    struct mthread *mt;
    if((mt = remove_q_head(&mv->mv_takeput))) {
#if THREAD_DEBUG
      if (thread_trace) {
        printf("take_mvar: mvar=%p wake %d\n", mv, (int)mt->mt_id);
      }
#endif  /* THREAD_DEBUG */
      add_runq_tail(mt);
#if THREAD_DEBUG
      if (thread_trace) {
        dump_q("runq", runq);
      }
#endif  /* THREAD_DEBUG */
    }
#if THREAD_DEBUG
    if (thread_trace) {
      printf("take_mvar: end mvar=%p return %p\n", mv, n.node_ptr);
    }
#endif  /* THREAD_DEBUG */
    return n;                   /* return the data */
  } else {
    /* The mvar is empty. */
#if THREAD_DEBUG
    if (thread_trace)
      printf("take_mvar: mvar=%p empty\n", mv);
#endif  /* THREAD_DEBUG */
    /* mvar is empty */
    if (try)
      return NIL_NODEPTR;
    struct mthread *mt = remove_q_head(&runq);
    add_q_tail(&mv->mv_takeput, mt);
#if THREAD_DEBUG
    if (thread_trace) {
      printf("take_mvar: end mvar=%p suspend %d\n", mv, (int)mt->mt_id);
      dump_q("runq", runq);
      dump_q("takeput", mv->mv_takeput);
    }
#endif  /* THREAD_DEBUG */
    /* Unlink from runq */
    resched(mt, ts_wait_mvar);    /* never returns */
    NOTREACHED;
  }
}

NODEPTR
read_mvar(bool try, struct mvar *mv)
{
  NODEPTR n;
  if (!is_NIL(n = runq.mq_head->mt_mval)) {
    /* The putMVar delivers the data in mt_mval and wakes the thread.
     * So first first check if we already have data in mt_mval. */
    runq.mq_head->mt_mval = NIL_NODEPTR; /* empty again */
    return n;                    /* returned the stashed data */
  }
  if (!is_NIL(n = mv->mv_data)) {
    /* mvar is full */
    return n;                   /* return the data */
  } else {
    /* mvar is empty */
    if (try)
      return NIL_NODEPTR;
#if THREAD_DEBUG
    if (thread_trace) {
      printf("read_mvar: suspend %d\n", (int)runq.mq_head->mt_id);
      dump_q("runq", runq);
    }
#endif  /* THREAD_DEBUG */
    struct mthread *mt = remove_q_head(&runq);
    add_q_tail(&mv->mv_read, mt);
    resched(mt, ts_wait_mvar);                /* never returns */
    NOTREACHED;
  }
}

int
put_mvar(bool try, struct mvar *mv, NODEPTR v)
{
#if THREAD_DEBUG
  if (thread_trace) {
    printf("put_mvar: mvar=%p\n", mv);
    dump_q("takeput", mv->mv_takeput);
    dump_q("read", mv->mv_read);
  }
#endif  /* THREAD_DEBUG */
  if (!is_NIL(mv->mv_data)) {
    /* The mvar is full. */
#if THREAD_DEBUG
    if (thread_trace)
      printf("put_mvar: mvar=%p full\n", mv);
#endif  /* THREAD_DEBUG */
    if (try)
      return 0;
    struct mthread *mt = remove_q_head(&runq);
    add_q_tail(&mv->mv_takeput, mt); /* put on mvar queue */
#if THREAD_DEBUG
    if (thread_trace) {
      printf("put_mvar: suspend %d\n", (int)mt->mt_id);
      dump_q("runq", runq);
      dump_q("takeput", mv->mv_takeput);
    }
#endif  /* THREAD_DEBUG */
    resched(mt, ts_wait_mvar);                  /* never returns */
  } else {
    /* The mvar is empty. */
#if THREAD_DEBUG
    if (thread_trace)
      printf("put_mvar: mvar=%p empty\n", mv);
#endif  /* THREAD_DEBUG */
    /* mvar is empty */
    mv->mv_data = v;              /* put the data in the mvar */
    if (mv->mv_takeput.mq_head || mv->mv_read.mq_head) {
      /* one or more threads are waiting */
      struct mthread *mt;
      if ((mt = remove_q_head(&mv->mv_takeput))) {
        /* wake up one 'take' */
#if THREAD_DEBUG
        if (thread_trace)
          printf("put_mvar: wake-1 %d\n", (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
        add_runq_tail(mt);             /* and schedule for execution later */
      }
      for(;;) {
        /* wake up all 'read' */
        mt = remove_q_head(&mv->mv_read);
        if (!mt)
          break;
#if THREAD_DEBUG
        if (thread_trace)
          printf("put_mvar: wake-N %d\n", (int)mt->mt_id);
#endif  /* THREAD_DEBUG */
        if (!is_NIL(mt->mt_mval))
          ERR("put_mvar: mt_mval != NIL");
        mt->mt_mval = v;               /* value for restarted read */
        add_runq_tail(mt);             /* and schedule for execution later */
      }
#if THREAD_DEBUG
      if (thread_trace) {
        printf("put_mvar: end\n");
        dump_q("runq", runq);
      }
#endif  /* THREAD_DEBUG */
      /* return to caller */
    } else {
#if THREAD_DEBUG
      if (thread_trace) {
        printf("put_mvar: mvar=%p no waiters\n", mv);
      }
#endif  /* THREAD_DEBUG */
      /* no threads waiting, so store the value */
      mv->mv_data = v;
      /* return to caller */
    }
  }
  return 1;
}

NORETURN void
thread_delay(uvalue_t usecs)
{
#if !defined(CLOCK_INIT)
  ERR("thread_delay: no clock");
#else
  /* XXX should check if there is already a throw exn */
  struct mthread *mt = remove_q_head(&runq);
  mt->mt_at = CLOCK_GET() + usecs; /* wakeup time */
#if THREAD_DEBUG
  if (thread_trace)
    printf("thread_delay: id=%d usecs=%ld\n", (int)mt->mt_id, (long)usecs);
#endif  /* THREAD_DEBUG */
  /* insert in delayq which is kept sorted in time order */
  struct mthread **tq;
  for (tq = &timeq.mq_head; *tq; tq = &(*tq)->mt_queue) {
    if (mt->mt_at <= (*tq)->mt_at)
      break;
  }
  mt->mt_queue = *tq;           /* forward link */
  *tq = mt;                     /* and put mt in place */
  if (!mt->mt_queue)            /* no forward link */
    timeq.mq_tail = mt;
  resched(mt, ts_wait_time);
#endif
}

/* Pause execution if something might still happen */
void
pause_exec(void)
{
/*
 * We end up here if the run queue is empty. If there is no thread waiting for
 * a delay to expire, we will never resume operation and we are deadlocked. However, if
 * we compile with WANT_IO_POLL there might be threads waiting for IO events, so in
 * that case we check for them as well. If there is no thread waiting for a delay or an
 * IO event, we are deadlocked.
 */
#if WANT_IO_POLL
  /* Check for deadlock situation */
  if (!pollq.mq_head
#if defined(CLOCK_INIT)
     && !timeq.mq_head
#endif
    ) ERR("deadlock");

  /* Loop until at least one thread is runnable.*/
  while (!runq.mq_head) {
    int timeout_ms = -1; /* block indefinitely if only io_waiters */
#if defined(CLOCK_INIT)
    /* If there are threads blocked on delays, compute the timeout_ms to account for that. */
    if (timeq.mq_head) {
      CLOCK_T dly = timeq.mq_head->mt_at - CLOCK_GET();
      if (dly > 0) {
        /* poll() can be unreliable, so sleep shorter than the delay */
        dly /= 1100;            /* 1.1=sleep shorter, 1000=convert us to ms */
        timeout_ms = dly == 0 ? 1 : dly; /* sleep at least 1ms to avoid busy wait */
      } else {
        timeout_ms = 0;         /* delay has already expired */
      }
    }
    check_timeq();
    if (runq.mq_head)
      break;
#endif  /* defined(CLOCK_INIT) */
    check_pollq(timeout_ms);
    check_sigint();		/* if there is a SIGINT, this will put a thread on the runq */
  }

#else /* !WANT_IO_POLL */

#if defined(CLOCK_INIT)
  if (timeq.mq_head) {
    struct mthread *mt;
    while (!runq.mq_head && (mt = timeq.mq_head)) {
      /* We are waiting for a delay to expire, so sleep a while */
      CLOCK_T dly = mt->mt_at - CLOCK_GET();
      if (dly > 0) {
        /* usleep() can be unreliable, so sleep shorter than the delay */
        dly /= 4;
        if (dly < 50) dly = 50;
        CLOCK_SLEEP((useconds_t)dly);
      }
      check_timeq();
      check_sigint();
    }
  } else {
#if THREAD_DEBUG
    if (0) {
      dump_q("runq", runq);
      dump_q("timeq", timeq);
      if (0) {
        for(struct mvar *mv = all_mvars; mv; mv = mv->mv_next) {
          printf("mvar %p, data=%p\n", mv, mv->mv_data.node_ptr);
          dump_q("takeput", mv->mv_takeput);
        }
      }
      for(struct mthread *mt = all_threads; mt; mt = mt->mt_next) {
        if (!is_NIL(mt->mt_exn->mv_data)) {
          printf("### bad thread ThreadId#%d mask=%d state=%d\n", (int)mt->mt_id, mt->mt_mask, mt->mt_state);
        }
      }
    }
#endif               /* THREAD_DEBUG */
    ERR("deadlock");            /* XXX throw async to main thread */
  }
#else  /* CLOCK_INIT */
  ERR("no clock");
#endif  /* CLOCK_INIT */
#endif /* !WANT_IO_POLL */
}

/* Interrupt a sleeping thread in a throwTo/threadDelay */
void
thread_intr(struct mthread *mt)
{
#if THREAD_DEBUG
  if (thread_trace)
    printf("thread_intr: id=%d state=%d\n", (int)mt->mt_id, mt->mt_state);
#endif  /* THREAD_DEBUG */
  switch(mt->mt_state) {
  case ts_runnable:
    break;                      /* already on runq */
  case ts_wait_mvar:
    if (mt->mt_mask == mask_uninterruptible) /* uninterruptible */
      break;
    /* we don't know which mvar we are waiting on, so look at all of them */
    /* XXX should add a pointer in mthread to the mvar */
    for (struct mvar *mv = all_mvars; mv; mv = mv->mv_next) {
      if (find_and_unlink(&mv->mv_takeput, mt))
          goto found;
      if (find_and_unlink(&mv->mv_read, mt))
          goto found;
    }
    ERR("thread_intr: mvar");
  found:
    mt->mt_mval = NIL_NODEPTR;          /* no longer waiting on the mvar */
    add_runq_tail(mt);
    break;
  case ts_wait_time:
#if THREAD_DEBUG
    if (thread_trace) {
      printf("thread_intr: ts_wait_time mask=%d\n", (int)mt->mt_mask);
    }
#endif  /* THREAD_DEBUG */
    if (mt->mt_mask == mask_uninterruptible) /* uninterruptible */
      break;
    /* find thread in timeq */
    if (!find_and_unlink(&timeq, mt))
      ERR("thread_intr: timeq");
    /* XXX should adjust mq_tail */
    add_runq_tail(mt);
    break;
  case ts_wait_io:
#if THREAD_DEBUG
    if (thread_trace) {
      printf("thread_intr: ts_wait_io mask=%d\n", (int)mt->mt_mask);
    }
#endif  /* THREAD_DEBUG */
    if (mt->mt_mask == mask_uninterruptible) /* uninterruptible */
      break;
    /* find thread in pollq */
    if (!find_and_unlink(&pollq, mt))
      ERR("thread_intr: pollq");
#if WANT_IO_POLL
    mt->mt_fd = IO_POLL_WAITING_FOR_NONE;
#endif
    add_runq_tail(mt);
    break;
  case ts_finished:
  case ts_died:
#if THREAD_DEBUG
    if (thread_trace) {
      printf("thread_intr: finished/died\n");
    }
#endif  /* THREAD_DEBUG */
    break;
  default:
    ERR("thread_intr");
  }
#if THREAD_DEBUG
  if (thread_trace) {
    printf("thread_intr: done\n");
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
}

NORETURN void
raise_exn(NODEPTR exn)
{
  if (doing_rnf)
    longjmp(rnf_skip, 1);
  /* Free bitmaps of any rnf() calls we are unwinding past. */
  while (cur_rnf && cur_rnf->rs_hdl == cur_handler) {
    FREE(cur_rnf->rs_done);
    cur_rnf = cur_rnf->rs_old;
  }
#if THREAD_DEBUG
  if (thread_trace) {
    printf("raise_exn: %p\n", exn.node_ptr);
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */

  if (cur_handler) {
    /* Pass the exception to the handler */
    cur_handler->hdl_exn = exn;
    longjmp(cur_handler->hdl_buf, 1);
  } else {
    /* No exception handler, jump to the scheduler */
    the_exn = exn;
    longjmp(sched, mt_raise);
  }
}

/* Raise a RTS exception identified by a number rather than an exception value */
NORETURN void
raise_rts(enum rts_exn exn) {
  raise_exn(mkInt(exn));
}

/***************** GC ******************/

/* Test if FREE bit is 0 */
static INLINE int is_marked_used(NODEPTR n)
{
  struct node *p = get_node_ptr(n);
  if (!p)
    return 1;
  heapoffs_t i = p - cells;
  if (i < heap_start)
    return 1;
#if SANITY
  if (i >= free_map_nwords * BITS_PER_WORD)
    ERR("is_marked_used");
#endif
  return (free_map[i / BITS_PER_WORD] & (1ULL << (i % BITS_PER_WORD))) == 0;
}

/*
 * Bump allocation inside a run of free cells (BUMP_ALLOC, default on).
 *
 * The free map is scanned first-fit in address order, so consecutive
 * allocations almost always come from consecutive cells anyway.  Finding the
 * cell one at a time costs a word load, FFS, a bit clear and a store to
 * next_scan_index per allocation, and the store-forwarding chain through
 * free_map[i] and next_scan_index serialises back-to-back allocations.
 * Instead bump_refill() claims a whole run of free cells at once (clearing
 * their map bits eagerly) and alloc_node just bumps a pointer through it.
 * Runs average a few dozen cells on the self-compile, so the refill is cold.
 *
 * Invariants: num_free counts the cells not yet claimed, and num_alloc the
 * cells claimed, so neither changes per allocation.  The claimed-but-unused
 * tail of the run (at most a map word) is counted as allocated: gc_check may
 * collect that much early, and NUM_ALLOC subtracts the tail, which a GC
 * abandons (mark_all_free).  The map overstates "used" by that tail, which is
 * harmless because the map is only read by the GC, and gc() rebuilds it from
 * scratch.
 * bump_ptr/bump_end are globals, never cached in evali locals: longjmp
 * (T_CATCHR, resched) and nested GCs would otherwise see stale values.
 */
#if !defined(BUMP_ALLOC)
#define BUMP_ALLOC 1
#endif

#if BUMP_ALLOC
static struct node *bump_ptr, *bump_end; /* current run: [bump_ptr, bump_end) */
#define NUM_ALLOC (num_alloc - (counter_t)(bump_end - bump_ptr)) /* cells allocated */
#else
#define NUM_ALLOC num_alloc
#endif

static INLINE void mark_all_free(void)
{
  memset(free_map, ~0, free_map_nwords * sizeof(bits_t));
  next_scan_index = heap_start;
#if BUMP_ALLOC
  /* The map is about to be rebuilt by the marker (or is fresh at init), so a
   * run claimed from the old map is stale: its cells may now be live.
   * The unused tail of the run was never allocated. */
  num_alloc -= (counter_t)(bump_end - bump_ptr);
  bump_ptr = bump_end = 0;
#endif
}

#if BUMP_ALLOC
#if !defined(BUMP_MAX_WORDS)
#define BUMP_MAX_WORDS 16       /* max extra bitmap words claimed per refill */
#endif
/* Claim the next run of free cells into bump_ptr..bump_end. */
static ALWAYS_INLINE void
bump_refill_body(void)
{
  counter_t nf = num_free;
  heapoffs_t i = next_scan_index / BITS_PER_WORD;
  heapoffs_t nwords = free_map_nwords;
  heapoffs_t h_size = heap_size;
  bits_t *fmap = free_map;
  heapoffs_t pos;
  bits_t word, inv, mask;
  int k, run;

  /* This can happen if we run out of memory when parsing.
   * It also guarantees that the map has a free cell. */
  if (UNLIKELY(nf == 0))
    ERR("alloc_node");
  for(;;) {
    /* Not just under SANITY: an inconsistent num_free must not turn into a
     * read past the map. */
    if (UNLIKELY(i >= nwords))
      ERR("bump_refill: free_map");
    word = fmap[i];
    if (word)
      break;
    i++;
  }
  k = FFS(word) - 1;            /* bit index of the first free cell */
  /* Run length = number of consecutive 1 bits starting at k.  Bit 0 of
   * word >> k is set, so inv is 0 only for k == 0 with every bit set; that
   * case is handled explicitly because CTZ(0) is not portable. */
  inv = ~(word >> k);
  run = inv ? (int)CTZ(inv) : (int)(BITS_PER_WORD - k);
  pos = i * BITS_PER_WORD + k;
  /* The last map word has bits past heap_size when heap_size is not a
   * multiple of the word size; those bits are set but the cells do not exist. */
  if (pos + run > h_size)
    run = (int)(h_size - pos);
  mask = (run == (int)BITS_PER_WORD ? ~(bits_t)0 : (((bits_t)1 << run) - 1)) << k;
  fmap[i] = word & ~mask;       /* claim the whole run now */
  /* If the run reached the end of this bitmap word and there is plenty of
   * headroom before GC, extend across consecutive free bits in following words. */
  if (k + run == (int)BITS_PER_WORD && nf >= 2 * (BUMP_MAX_WORDS + 1) * BITS_PER_WORD) {
    heapoffs_t j = i + 1;
    heapoffs_t full_words = h_size / BITS_PER_WORD;
    heapoffs_t j_end = j + BUMP_MAX_WORDS;
    if (j_end > full_words)
      j_end = full_words;
    while (j < j_end && fmap[j] == ~(bits_t)0) {
      fmap[j++] = 0;
      run += (int)BITS_PER_WORD;
    }
    if (j < j_end) {
      bits_t w2 = fmap[j];
      if (w2 & 1) {
        int r2 = (int)CTZ(~w2);
        fmap[j] = w2 & ~(((bits_t)1 << r2) - 1);
        run += r2;
      }
    }
  }
  num_free = nf - run;
  num_alloc += run;
  next_scan_index = pos + run;
  bump_ptr = HEAPREF(pos);
  bump_end = bump_ptr + run;
}

static NOINLINE void
bump_refill(void)
{
  bump_refill_body();
}
#endif  /* BUMP_ALLOC */

/* Allocate a new node */
static INLINE struct node*
alloc_node_ptr(void)
{
  struct node *n;

#if BUMP_ALLOC
  if (bump_ptr == bump_end)
    bump_refill();
  n = bump_ptr++;
#else  /* BUMP_ALLOC */
  /* This can happen if we run out of memory when parsing. */
  if (num_free <= 0)
    ERR("alloc_node");

  heapoffs_t i = next_scan_index / BITS_PER_WORD;
  int k;                        /* will contain bit pos + 1 */
  heapoffs_t pos;
  heapoffs_t word;

  for(;;) {
    word = free_map[i];
    if (word)
      break;
    i++;
#if SANITY
    if (i >= free_map_nwords) {
#if 0
      fprintf(stderr, "wordsize=%u, num_free=%u next_scan_index=%u i=%u free_map_nwords=%u\n", (uint)BITS_PER_WORD,
              (uint)num_free, (uint)next_scan_index, (uint)i, (uint)free_map_nwords);
#endif
      ERR("alloc_node: free_map");
    }
#endif
  }
  k = FFS(word);
  pos = i * BITS_PER_WORD + k - 1; /* first free node */
  n = HEAPREF(pos);
  // mark_used(n); // equivalent to:
  free_map[i] = word & (word-1);
  next_scan_index = pos;
  COUNT(num_alloc);
  num_free--;
#endif  /* BUMP_ALLOC */

  return n;
}

/* Create a NODEPTR from a tag.  Either a real node or an inline tag */
static INLINE NODEPTR
alloc_node(enum node_tag t)
{
  XASSERT(t < V_CONSTR || t > T_FIRST_REAL_TAG); /* V_ tags need extra info, use mk_CON/mk_CASE */
  if (t > T_FIRST_REAL_TAG)
    return mk_TAG(t);
  else {
    struct node *p = alloc_node_ptr();
    NODEPTR n;
    n.node_ptr = p;
    FUN(n) = mk_DATA_TAG(t);
    /* ARG slot uninitialized */
    return n;
  }
}

/* Create a new application node */
static INLINE NODEPTR
new_ap(NODEPTR f, NODEPTR a)
{
  struct node *p = alloc_node_ptr();
  NODEPTR n;
  n.node_ptr = p;
  FUN(n) = f;
  ARG(n) = a;
  return n;
}

NODEPTR evali(NODEPTR n);

/* If this is non-0 it means that the threading system is active. */
struct mthread *main_thread = 0;

void
start_exec(NODEPTR root)
{
  struct mthread *mt;

  mt = new_thread(new_ap(root, combWorld)); /* main thread */
  mt->mt_id = MAIN_THREAD;                  /* make it the main thread in case this is foreign export calling */
  main_thread = mt;

  switch(setjmp(sched)) {
  case mt_main:
    break;
  case mt_resched:
    COUNT(num_resched);
    break;
  case mt_raise:
    /* We have an uncaught exception.
     * If it's the main thread, this kills the program.
     * Otherwise, it just kills the thread.
     */
    if (in_raise) {
      ERR("FATAL: exception while trying to die");
      EXIT(1);
    }
    mt = remove_q_head(&runq);
    if (mt->mt_id == MAIN_THREAD) {
      die_exn(the_exn);
    } else {
#if THREAD_DEBUG
      if (thread_trace) {
        printf("start_exec: mt=%p id=%d died from exn\n", mt, (int)mt->mt_id);
      }
#endif  /* THREAD_DEBUG */
      mt->mt_state = ts_died;
      mt->mt_root = NIL_NODEPTR;
      release_throwto(mt);      /* release anyone blocked in throwTo to it */
    }
  }
#if THREAD_DEBUG
  if (thread_trace) {
    printf("start_exec:\n");
    dump_q("runq", runq);
  }
#endif  /* THREAD_DEBUG */
  for(;;) {
    if (!runq.mq_head)
      pause_exec();
    mt = runq.mq_head;          /* front thread */
    if (!mt)                    /* this should never happen */
      ERR("no threads");

    glob_slice = mt->mt_slice + slice;
#if THREAD_DEBUG
    if (thread_trace)
      printf("start_exec: start %d, slice=%d\n", (int)mt->mt_id, (int)glob_slice);
#endif  /* THREAD_DEBUG */
    num_reductions += glob_slice-1;
    (void)evali(mt->mt_root);         /* run it */
    num_reductions -= glob_slice;
    /* when evali() returns the thread is done */
    (void)remove_q_head(&runq);                      /* remove front thread */

#if THREAD_DEBUG
    if (thread_trace) {
      printf("start_exec: mt=%p id=%d finished\n", mt, (int)mt->mt_id);
    }
#endif  /* THREAD_DEBUG */
    mt->mt_state = ts_finished;
    mt->mt_root = NIL_NODEPTR;
    release_throwto(mt);        /* release anyone blocked in throwTo to it */
    /* XXX mt_mval, mt_thrown */

    if (mt->mt_id == MAIN_THREAD) {
      main_thread = 0;
#if THREAD_DEBUG
      if (thread_trace) {
        printf("start_exec: main thread done\n");
      }
#endif  /* THREAD_DEBUG */
      return;                   /* when the main thread dies it's all over */
    }
  }
}

/* One node of each kind for primitives, these are never GCd. */
/* We use linear search in this, because almost all lookups
 * are among the combinators.
 */
static const
struct {
  const char *name;
  const enum node_tag tag;
  const uint8_t arity;                /* the number of arguments it needs to reduce, see arity_virt */
  const enum node_tag flipped;        /* What should (C op) reduce to? defaults to T_FREE */
  //  NODEPTR node;
} primops[] = {
  /* combinators */
  { "B", T_B, 3 },
  { "O", T_O, 4 },
  { "K", T_K, 2, T_A },
  { "C'", T_CC, 4 },
  { "C", T_C, 3 },
  { "A", T_A, 2, T_K },
  { "S'", T_SS, 4 },
  { "P", T_P, 3 },
  { "R", T_R, 2 },
  { "I", T_I, 1 },
  { "S", T_S, 3 },
  { "U", T_U, 2 },
  { "Y", T_Y, 1 },
  { "B'", T_BB, 2 },
  { "Z", T_Z, 2 },
  { "K2", T_K2, 2 },
  { "K3", T_K3, 2 },
  { "K4", T_K4, 2 },
  { "C'B", T_CCB, 3 },
/* primops */
  { "+", T_ADD, 2, T_ADD },
  { "-", T_SUB, 2, T_SUBR },
  { "*", T_MUL, 2, T_MUL },
  { "quot", T_QUOT, 2 },
  { "rem", T_REM, 2 },
  { "u+", T_UADD, 2, T_UADD },
  { "u-", T_USUB, 2, T_USUBR },
  { "u*", T_UMUL, 2, T_UMUL },
  { "uquot", T_UQUOT, 2 },
  { "urem", T_UREM, 2 },
  { "subtract", T_SUBR, 2, T_SUB },
  { "usubtract", T_USUBR, 2, T_USUB },
  { "neg", T_NEG, 1 },
  { "uneg", T_UNEG, 1 },
  { "and", T_AND, 2, T_AND },
  { "or", T_OR, 2, T_OR },
  { "xor", T_XOR, 2, T_XOR },
  { "inv", T_INV, 1 },
  { "shl", T_SHL, 2 },
  { "shr", T_SHR, 2 },
  { "ashr", T_ASHR, 2 },
  { "popcount", T_POPCOUNT, 1 },
  { "clz", T_CLZ, 1 },
  { "ctz", T_CTZ, 1 },
#if WANT_FLOAT64
  { "d+", T_DADD, 2, T_DADD },
  { "d-", T_DSUB, 2 },
  { "d*", T_DMUL, 2, T_DMUL },
  { "d/", T_DDIV, 2 },
  { "dneg", T_DNEG, 1 },
  { "itod", T_ITOD, 1 },
  { "Itod", T_I64TOD, 1 },
  { "utod", T_UTOD, 1 },
  { "dtoi", T_DTOI, 1 },
  { "d==", T_DEQ, 2, T_DEQ },
  { "d/=", T_DNE, 2, T_DNE },
  { "d<", T_DLT, 2, T_DGT },
  { "d<=", T_DLE, 2, T_DGE },
  { "d>", T_DGT, 2, T_DLT },
  { "d>=", T_DGE, 2, T_DLE },
#endif  /* WANT_FLOAT64 */
#if WANT_FLOAT64 && WANT_FLOAT32
  { "dtof", T_DTOF, 1 },
  { "ftod", T_FTOD, 1 },
#endif  /* WANT_FLOAT64 && WANT_FLOAT32 */
#if WANT_FLOAT32
  { "f+", T_FADD, 2, T_FADD },
  { "f-", T_FSUB, 2 },
  { "f*", T_FMUL, 2, T_FMUL },
  { "f/", T_FDIV, 2 },
  { "fneg", T_FNEG, 1 },
  { "Itof", T_I64TOF, 1 },
  { "itof", T_ITOF, 1 },
  { "utof", T_UTOF, 1 },
  { "ftoi", T_FTOI, 1 },
  { "f==", T_FEQ, 2, T_FEQ },
  { "f/=", T_FNE, 2, T_FNE },
  { "f<", T_FLT, 2, T_FGT },
  { "f<=", T_FLE, 2, T_FGE },
  { "f>", T_FGT, 2, T_FLT },
  { "f>=", T_FGE, 2, T_FLE },
#endif  /* WANT_FLOAT32 */

  { "bs++", T_BSAPPEND, 2 },
  { "bs++.", T_BSAPPENDDOT, 2 },
  { "bs==", T_BSEQ, 2, T_BSEQ },
  { "bs/=", T_BSNE, 2, T_BSNE },
  { "bs<", T_BSLT, 2, T_BSGT },
  { "bs<=", T_BSLE, 2, T_BSGE },
  { "bs>", T_BSGT, 2, T_BSLT },
  { "bs>=", T_BSGE, 2, T_BSLE },
  { "bscmp", T_BSCMP, 2 },
  { "bsunpack", T_BSUNPACK, 1 },
  { "bsreplicate", T_BSREPLICATE, 2 },
  { "bslength", T_BSLENGTH, 1 },
  { "bssubstr", T_BSSUBSTR, 3 },
  { "bsindex", T_BSINDEX, 2 },
  { "bsnew", T_BSNEW, 3 },
  { "bsread", T_BSREAD, 3 },
  { "bswrite", T_BSWRITE, 4 },
  { "bsfreeze", T_BSFREEZE, 2 },
  { "bsappbyte", T_BSAPPBYTE, 3 },
  { "bsappchar", T_BSAPPCHAR, 3 },

  { "ord", T_I, 1 },
  { "chr", T_I, 1 },
  { "==", T_EQ, 2, T_EQ },
  { "/=", T_NE, 2, T_NE },
  { "<", T_LT, 2, T_GT },
  { "u<", T_ULT, 2, T_UGT },
  { "u<=", T_ULE, 2, T_UGE },
  { "u>", T_UGT, 2, T_ULT },
  { "u>=", T_UGE, 2, T_ULE },
  { "<=", T_LE, 2, T_GE },
  { ">", T_GT, 2, T_LT },
  { ">=", T_GE, 2, T_LE },
  { "fp+", T_FPADD, 2 },
  { "fp2p", T_FP2P, 1 },
  { "fpnew", T_FPNEW, 2 },
  { "fpfin", T_FPFIN, 3 },
  //  { "fpstr", T_FPSTR },
  { "fp2bs", T_FP2BS, 2 },
  { "bs2fp", T_BS2FP, 1 },
  { "seq", T_SEQ, 2 },
  { "icmp", T_ICMP, 2 },
  { "ucmp", T_UCMP, 2 },
  { "rnf", T_RNF, 2 },
  { "fromUTF8", T_BSFROMUTF8, 1 },
  { "headUTF8", T_BSHEADUTF8, 1 },
  { "tailUTF8", T_BSTAILUTF8, 1 },
  /* Data.Text helpers: lengths and offsets are in code points and bytes */
  { "utf8length", T_UTF8LENGTH, 1 },
  { "utf8offset", T_UTF8OFFSET, 2 },
  { "utf8offsetend", T_UTF8OFFSETEND, 2 },
  { "utf8offsetfrom", T_UTF8OFFSETFROM, 3 },
  { "utf8offsetendbefore", T_UTF8OFFSETENDBEFORE, 3 },
  { "utf8decode", T_UTF8DECODE, 2 },
  { "utf8decodeend", T_UTF8DECODEEND, 2 },
  { "utf8reverse", T_UTF8REVERSE, 1 },
  { "utf8skipspace", T_UTF8SKIPSPACE, 2 },
  { "utf8skipnonspace", T_UTF8SKIPNONSPACE, 2 },
  { "utf8trimspaceend", T_UTF8TRIMSPACEEND, 1 },
  { "bsfind", T_BSFIND, 3 },
  { "bsrfind", T_BSRFIND, 2 },
  { "bsasciicase", T_BSASCIICASE, 2 },
  { "bsisascii", T_BSISASCII, 1 },
  { "bsconcat", T_BSCONCAT, 1 },
  /* IO primops */
  { "IO.>>=", T_IO_BIND, 3 },
  { "IO.>>", T_IO_THEN, 2 },
  { "IO.return", T_IO_RETURN, 3 },
  { "IO.serialize", T_IO_SERIALIZE, 3 },
  { "IO.print", T_IO_PRINT, 3 },
  { "IO.deserialize", T_IO_DESERIALIZE, 2 },
  { "IO.stdin", T_IO_STDIN, 0 },
  { "IO.stdout", T_IO_STDOUT, 0 },
  { "IO.stderr", T_IO_STDERR, 0 },
  { "IO.getArgRef", T_IO_GETARGREF, 1 },
  { "IO.performIO", T_IO_PERFORMIO, 1 },
  { "IO.atomic", T_IO_ATOMIC, 2 },
  { "IO.gc", T_IO_GC, 2 },
  { "IO.stats", T_IO_STATS, 1 },
  { "IO.pp", T_IO_PP, 2 },
  { "nodeAddr", T_NODEADDR, 1 },
  { "IO.rtsinfo", T_IO_RTSINFO, 1 },
  { "IO.lazyBind", T_IO_LAZYBIND, 3 },
  { "IO.lazyBindST", T_IO_LAZYBINDST, 3 },
  { "IO.strict", T_IO_STRICT, 2 },
  { "raise", T_RAISE, 1 },
  { "catch", T_CATCH, 3 },
  { "catchr", T_CATCHR, 4 },
  { "A.alloc", T_ARR_ALLOC, 3 },
  { "A.copy", T_ARR_COPY, 2 },
  { "A.size", T_ARR_SIZE, 2 },
  { "A.read", T_ARR_READ, 3 },
  { "A.write", T_ARR_WRITE, 4 },
  { "A.trunc", T_ARR_TRUNC, 3 },
  { "A.==", T_ARR_EQ, 2 },
  { "IO.fork", T_IO_FORK, 2 },
  { "IO.thid", T_IO_THID, 1 },
  { "thnum", T_THNUM, 1 },
  { "IO.throwto", T_IO_THROWTO, 3 },
  { "IO.yield", T_IO_YIELD, 1 },
  { "IO.newmvar", T_IO_NEWMVAR, 1 },
  { "IO.takemvar", T_IO_TAKEMVAR, 2 },
  { "IO.putmvar", T_IO_PUTMVAR, 3 },
  { "IO.readmvar", T_IO_READMVAR, 2 },
  { "IO.trytakemvar", T_IO_TRYTAKEMVAR, 2 },
  { "IO.tryputmvar", T_IO_TRYPUTMVAR, 3 },
  { "IO.tryreadmvar", T_IO_TRYREADMVAR, 2 },
  { "IO.threaddelay", T_IO_THREADDELAY, 2 },
  { "IO.threadstatus", T_IO_THREADSTATUS, 2 },
  { "IO.getmaskingstate", T_IO_GETMASKINGSTATE, 1 },
  { "IO.setmaskingstate", T_IO_SETMASKINGSTATE, 2 },
  { "packCString", T_PACKCSTRING, 2 },
  { "packCStringLen", T_PACKCSTRINGLEN, 3 },
  { "bsgrab", T_BSGRAB, 2 },
  { "bsgrablen", T_BSGRABLEN, 3 },
  { "toPtr", T_TOPTR, 1 },
  { "toInt", T_TOINT, 1 },
  { "toDbl", T_TODBL, 1 },
  { "toFlt", T_TOFLT, 1 },
  { "fromDbl", T_FROMDBL, 1 },
  { "fromFlt", T_FROMFLT, 1 },
  { "toFunPtr", T_TOFUNPTR, 1 },
  { "IO.ccall", D_IO_CCALL, 0 },
  { "isint", T_ISINT, 1 },
  { "SPnew", T_SPNEW, 2 },
  { "SPderef", T_SPDEREF, 2 },
  { "SPfree", T_SPFREE, 2 },
  { "Wknew", T_WKNEW, 3 },
  { "Wknewfin", T_WKNEWFIN, 4 },
  { "Wkderef", T_WKDEREF, 2 },
  { "Wkfinal", T_WKFINAL, 2 },
  { "binint2", T_BININT2, 0 },
  { "binint1", T_BININT1, 0 },
  { "bindbl2", T_BINDBL2, 0 },
  { "bindbl1", T_BINDBL1, 0 },
  { "binbs2", T_BINBS2, 0 },
  { "binbs1", T_BINBS1, 0 },
  { "unint1", T_UNINT1, 0 },
  { "undbl1", T_UNDBL1, 0 },
  { "IO.waitrdfd", T_IO_WAITRDFD, 2 },
  { "IO.waitwrfd", T_IO_WAITWRFD, 2 },
#if WANT_INT64
  { "I+", T_ADD64, 2, T_ADD64 },
  { "I-", T_SUB64, 2, T_SUBR64 },
  { "I*", T_MUL64, 2, T_MUL64 },
  { "Iquot", T_QUOT64, 2 },
  { "Irem", T_REM64, 2 },
  { "Iu+", T_UADD64, 2, T_UADD64 },
  { "Iu-", T_USUB64, 2, T_USUBR64 },
  { "Iu*", T_UMUL64, 2, T_UMUL64 },
  { "Iuquot", T_UQUOT64, 2 },
  { "Iurem", T_UREM64, 2 },
  { "Isubtract", T_SUBR64, 2, T_SUB64 },
  { "Iusubtract", T_USUBR64, 2, T_USUB64 },
  { "Ineg", T_NEG64, 1 },
  { "Iuneg", T_UNEG64, 1 },
  { "Iand", T_AND64, 2, T_AND64 },
  { "Ior", T_OR64, 2, T_OR64 },
  { "Ixor", T_XOR64, 2, T_XOR64 },
  { "Iinv", T_INV64, 1 },
  { "Ishl", T_SHL64, 2 },
  { "Ishr", T_SHR64, 2 },
  { "Iashr", T_ASHR64, 2 },
  { "Ipopcount", T_POPCOUNT64, 1 },
  { "Iclz", T_CLZ64, 1 },
  { "Ictz", T_CTZ64, 1 },
  { "I==", T_EQ64, 2, T_EQ64 },
  { "I/=", T_NE64, 2, T_NE64 },
  { "I<", T_LT64, 2, T_GT64 },
  { "Iu<", T_ULT64, 2, T_UGT64 },
  { "Iu<=", T_ULE64, 2, T_UGE64 },
  { "Iu>", T_UGT64, 2, T_ULT64 },
  { "Iu>=", T_UGE64, 2, T_ULE64 },
  { "I<=", T_LE64, 2, T_GE64 },
  { "I>", T_GT64, 2, T_LT64 },
  { "I>=", T_GE64, 2, T_LE64 },
  { "Iicmp", T_ICMP64, 2 },
  { "Iucmp", T_UCMP64, 2 },
  { "itoI", T_ITOI64, 1 },
  { "Itoi", T_I64TOI, 1 },
  { "utoU", T_UTOU64, 1 },
  { "Utou", T_U64TOU, 1 },
#endif  /* WANT_INT64 */
  { "tick", D_TICK, 1 },           /* fake op */
  { "D_", V_CASED, 0 },
  { "L_", V_LOOKD, 0 },
  { "E_", V_CASES, 0 },
  { "M_", V_LOOKS, 0 },
  { "C_", V_CONSTR, 0 },
};

#if GCRED
enum node_tag flip_ops[T_LAST_TAG+1];
#endif

/*
 * Use a hash table with open linear probing for the lookup of primpo names.
 * Make the hash table twice the size of the number of primops so we get a load factor of 0.5.
 */
#define PRIMOP_HASH_SIZE (2 * sizeof primops / sizeof primops[0] + 5)
/*
 * Without the + 5 above the time on my MacBook more that doubles!
 * It's a mystery why.
 */
static struct {
  const char   *name;           /* NULL marks an empty slot */
  enum node_tag tag;
} primop_hash[PRIMOP_HASH_SIZE];

/* FNV-1a hashing. It's simple and fast. */
static uint32_t
primop_hash_str(const char *s)
{
  uint32_t h = 2166136261;
  while (*s) {
    h ^= (uint8_t)*s++;
    h *= 16777619;
  }
  return h % PRIMOP_HASH_SIZE;
}

/* Build the initial table that maps a hash to a pair of a name and tag */
static void
init_primop_hash(void)
{
  size_t i;

  for (i = 0; i < sizeof primops / sizeof primops[0]; i++) {
    uint32_t k = primop_hash_str(primops[i].name);

    /* This slot is already occupied, so we progress to the next slot. */
    /* This must terminate, size the hash table is twice the size of the primops. */
    while (primop_hash[k].name) {
      k = (k + 1) % PRIMOP_HASH_SIZE;
    }
    primop_hash[k].name = primops[i].name;
    primop_hash[k].tag  = primops[i].tag;
  }
}

/* Return the tag for a primop name, or -1 if there is no such primop. */
static int
lookup_primop(const char *name)
{
  uint32_t k = primop_hash_str(name);

  while (primop_hash[k].name) {
    if (strcmp(primop_hash[k].name, name) == 0)
      return (int)primop_hash[k].tag;
    k = (k + 1) % PRIMOP_HASH_SIZE;
  }
  return -1;
}

#if WANT_STDIO
/* Create a dummy foreign pointer for the standard stdio handles. */
/* These handles are never gc():d. */
void
mk_std(NODEPTR n, FILE *f)
{
  struct final *fin = mcalloc(1, sizeof(struct final));
  struct forptr *fp = mcalloc(1, sizeof(struct forptr));
  BFILE *bf = add_utf8(add_FILE(f));
  FORPTR(n) = fp;
  fin->arg = bf;
  fin->back = fp;
  fp->payload.bs_array = bf;
  fp->finalizer = fin;
}
#endif

void
init_nodes(void)
{
  size_t j;
  NODEPTR x;

  cells = mmalloc(heap_size * sizeof(node));
  free_map_nwords = (heap_size + BITS_PER_WORD - 1) / BITS_PER_WORD; /* bytes needed for free map */
  free_map = mmalloc(free_map_nwords * sizeof(bits_t));

  /* The permanent nodes are allocated from the start of the heap.
   * Afterwards heap_start is moved past them, so they are never GCed. */
  heap_start = 0;
  mark_all_free();
  num_free = heap_size;

  /* Initialize node name array.
   * Go backwards, so that the first name of a tag wins, e.g., I and not its aliases ord and chr. */
  for (j = sizeof primops / sizeof primops[0]; j-- > 0; ) {
    tag_names[primops[j].tag] = primops[j].name;
    tag_arity[primops[j].tag] = primops[j].arity;
#if GCRED
    flip_ops[primops[j].tag] = primops[j].flipped;
#endif
  }
  init_primop_hash();

  /* Set up permanent nodes */
#if INTTABLE
  /* Allocate permanent Int nodes */
  for (int i = LOW_INT; i < HIGH_INT; i++) {
    NODEPTR n = alloc_node(D_INT);
    intTable[i - LOW_INT] = n;
    SETVALUE(n, i);
  }
#endif

#if WANT_STDIO
  comb_stdin  = alloc_node(D_FORPTR); mk_std(comb_stdin,  stdin);
  comb_stdout = alloc_node(D_FORPTR); mk_std(comb_stdout, stdout);
  comb_stderr = alloc_node(D_FORPTR); mk_std(comb_stderr, stderr);
#endif

  /* The displaySomeException compiles to (D_2 (U (K2 A))) */
  x = new_ap(combK2, combA);                 /* (K2 A) */
  x = new_ap(combU, x);                      /* (U (K2 A)) */
  {
    uint8_t arities[1] = { 2 };              /* SomeException has a dictionary and a value */
    combShowExn = new_ap(mk_CASE(V_CASED, find_case_arity(1, 0, arities)), x); /* (D_2 (U (K2 A))) */
  }

  /* The IO monad uses pairs implemented with Scott encoding, i.e., with P */
  combPUnit = new_ap(combP, combUnit);
  combPFst  = new_ap(combU, combK); /* fst for pairs constructed with P */
  combPSnd  = new_ap(combU, combA); /* snd for pairs constructed with P */
  combWorld = mkInt(99999);

  spare_node = alloc_node(D_INT);

  /* The permanent nodes occupy [0 .. next_scan_index].
   * Round up heap_start to the next bitword boundary to avoid the permanent nodes. */
  heap_start = (next_scan_index + 1 + BITS_PER_WORD - 1) / BITS_PER_WORD * BITS_PER_WORD;
  if (heap_start >= heap_size)
    ERR("heap too small");
  mark_all_free();              /* also sets next_scan_index = heap_start */

  num_free = heap_size - heap_start;
}

#if GCRED
counter_t red_a, red_k, red_i, red_int, red_flip, red_bi, red_bxi, red_ccbi, red_cc, red_cci, red_ccbbcp;
#endif
counter_t red_bb, red_k4, red_k3, red_k2, red_ccb, red_z, red_r;

//counter_t mark_depth;
//counter_t max_mark_depth = 0;

void mark(NODEPTR *np);
void mark_mvar(struct mvar *mv);
void mark_thread(struct mthread *mt);
void mark_parse(void);

/* Follow indirections */
static INLINE NODEPTR
indir(NODEPTR *np)
{
  NODEPTR n = *np;
  NODEPTR p = get_IND(n);
  if (is_NIL(p))
    return n;
  do {
    n = p;
    p = get_IND(n);
  } while (!is_NIL(p));
  *np = n;
  return n;
}

/***** weak pointers *****/

struct weak_ptr {
  struct weak_ptr *next;        /* list of all weak pointers. */
  int marked;                   /* seen by GC */
  NODEPTR key;                  /* key, this is the weak pointer */
  NODEPTR value;                /* associated value */
  NODEPTR finalize;             /* maybe finalizer */
};
struct weak_ptr *allweaks;      /* head of all weak pointers */

/* After GC mark phase, deal with weak pointers */
void
sweep_weaks(void)
{
  /* Fixed-point mark: while any live weak pointer has a marked key,
   * mark its value and finalizer, which may in turn mark other keys. */
  bool changed;
  do {
    changed = false;
    for (struct weak_ptr *wp = allweaks; wp; wp = wp->next) {
      if (is_NIL(wp->value))
        continue;                 /* the weak pointer is already dead */
      (void)indir(&wp->key);
      if (is_marked_used(wp->key)) {
        /* The key is used, so mark the other parts */
        if (!is_marked_used(wp->value) ||
            (!is_NIL(wp->finalize) && !is_marked_used(wp->finalize))) {
          /* Not already marked */
          mark(&wp->value);
          if (!is_NIL(wp->finalize))
            mark(&wp->finalize);
          /* This marking might have marked other keys, so scan again */
          changed = true;
        }
      }
    }
  } while (changed);

  /* Kill all weak pointers whose keys remained unmarked.
   * Done before marking finalizers so finalizers cannot resurrect keys. */
  for (struct weak_ptr *wp = allweaks; wp; wp = wp->next) {
    if (!is_NIL(wp->value) && !is_marked_used(wp->key)) {
      wp->value = NIL_NODEPTR;
      wp->key = NIL_NODEPTR;            /* not needed, but for sanity */
    }
  }

  /* Create finalizers for all weak pointers that just died */
  for (struct weak_ptr *wp = allweaks; wp; wp = wp->next) {
    if (is_NIL(wp->value) && !is_NIL(wp->finalize)) {
      struct mthread *mt = new_thread(wp->finalize);
      mark_thread(mt);        /* mark it, since overall thread marking has already run */
      wp->finalize = NIL_NODEPTR;
      /* Marking the finalizer does not resurrect keys */
    }
  }

  /* If a weak pointer object is unreferenced and it has been finalized,
   * then it can be garbage collected. */
  for (struct weak_ptr **wpp = &allweaks; *wpp; ) {
    struct weak_ptr *wp = *wpp;
    if (!wp->marked && is_NIL(wp->value)) {
      /* not marked, so unlink and free */
      *wpp = wp->next;
      COUNT(num_gc_weak);
      FREE(wp);
    } else {
      wp->marked = 0;           /* reset for next GC */
      /* point to the next weak_ptr */
      wpp = &wp->next;
    }
  }
}

NODEPTR
new_weak_ptr(NODEPTR key, NODEPTR value, NODEPTR finalize)
{
  struct weak_ptr *wp = mmalloc(sizeof(struct weak_ptr));
  wp->next = allweaks;
  allweaks = wp;
  wp->marked = 0;
  wp->key = key;
  wp->value = value;
  if (!is_NIL(finalize)) {
    wp->finalize = new_ap(finalize, combWorld);
  } else {
    wp->finalize = NIL_NODEPTR;
  }

  COUNT(num_new_weak);
  NODEPTR n = alloc_node(D_WEAK);
  WEAK(n) = wp;
  return n;
}

NODEPTR
deref_weak_ptr(struct weak_ptr *wp)
{
  if (is_NIL(wp->value))
    return combNothing;
  return new_ap(combJust, wp->value);
}

void
finalize_weak_ptr(struct weak_ptr *wp)
{
  NODEPTR final = wp->finalize;
  wp->value = NIL_NODEPTR;
  wp->key = NIL_NODEPTR;
  if (is_NIL(final))
    return;
  wp->finalize = NIL_NODEPTR;
  PUSH(final);
  (void)evali(TOP(0));
  POP(1);
}

/**********************************************************/

/* Throwing, e.g., a UserInterrupt exception, to the main thread
 * can happen from any thread (the one that happens to poll).
 * Throwing an exception can block, so we can't throw it from
 * the current thread.  Instead, we spawn a new thread, whose
 * only job it is to throw the exception.
 */
void
async_throwto(struct mthread *mt, NODEPTR exn)
{
  GCCHECKSAVE(exn, 4);
  NODEPTR thid = alloc_node(D_THID);
  THR(thid) = mt;		/* thread ID for mt */
  NODEPTR root = new_ap(new_ap(new_ap(combTHROWTO, thid), exn), combWorld); /* root = throwTo thid exn */
  (void)new_thread(root);       /* spawn and put on runq, i.e., forkIO root */
}

void
mark_thread(struct mthread *mt)
{
  if (mt->mt_mark)
    return;                     /* already marked */
  mt->mt_mark = true;
  if (!is_NIL(mt->mt_root))
    mark(&mt->mt_root);
  mark_mvar(mt->mt_exn);
  if (mt->mt_throwto)
    mark_thread(mt->mt_throwto);
  if (!is_NIL(mt->mt_mval))
    mark(&mt->mt_mval);
}

void
mark_mvar(struct mvar *mv)
{
  if (mv->mv_mark)
    return;
  mv->mv_mark = true;
  if (!is_NIL(mv->mv_data))
    mark(&mv->mv_data);
  for (struct mthread *mt = mv->mv_takeput.mq_head; mt; mt = mt->mt_queue)
    mark_thread(mt);
  for (struct mthread *mt = mv->mv_read.mq_head; mt; mt = mt->mt_queue)
    mark_thread(mt);
}

#if GCRED
/* The evaluation stack pointer when the GC started.
 * The mark phase pushes on the same stack, so stack_ptr cannot be used by gc_red_ok(). */
NODEPTR *gc_eval_stack_ptr;

/*
 * Only allow GC reductions when the node is not near the top of the stack.
 * The reason is that when GC is triggered we are just starting a reduction
 * and the combinator at the left-bottom of the spine is being reduced.
 * If a GC reduction removes this combinator, then bad things happen.
 */
static int
gc_red_ok(NODEPTR n)
{
  for (NODEPTR *s = gc_eval_stack_ptr; s < stack_bot && s - gc_eval_stack_ptr <= 5; s++)
    if (n.node_ptr == s->node_ptr)
      return 0;
  return 1;
}

/*
 * Try to do a reduction of the application node n during GC.
 * If one applies, n is turned into an indirection to the result, and the result is returned.
 * Otherwise NIL is returned.
 * All reductions just select an existing node or a combinator, so nothing is allocated.
 */
static NOINLINE NODEPTR
gc_reduce(NODEPTR n)
{
  NODEPTR fun = indir(&FUN(n));
  NODEPTR arg = indir(&ARG(n));
  enum node_tag funt = get_XTAG(fun);
  enum node_tag argt = get_XTAG(arg);
  enum node_tag funfunt = funt == X_AP ? get_XTAG(indir(&FUN(fun))) : D_NONE;
  enum node_tag funargt = argt == X_AP ? get_XTAG(indir(&FUN(arg))) : D_NONE;
  NODEPTR r;

  switch (funfunt) {
  case T_A:
    /* A x y --> y.  This is really only fruitful just after parsing. */
    r = arg;
    COUNT(red_a);
    goto red;
  case T_K:
    /* K x y --> x */
    r = indir(&ARG(fun));
    COUNT(red_k);
    goto red;
  case T_B:
    if (argt == T_I) {
      /* B x I --> x */
      r = indir(&ARG(fun));
      COUNT(red_bxi);
      goto red;
    }
    break;
  case T_CCB:
    if (argt == T_I) {
      /* C'B x I --> x */
      r = indir(&ARG(fun));
      COUNT(red_ccbi);
      goto red;
    }
    break;
  default:
    break;
  }

  switch (funt) {
  case T_I:
    /* I x --> x */
    r = arg;
    COUNT(red_i);
    goto red;
  case T_CC:
    if (argt == T_I) {
      /* C' I --> C */
      r = combC;
      COUNT(red_cci);
      goto red;
    }
    break;
  case T_CCB:
    if (argt == X_AP && get_XTAG(indir(&ARG(arg))) == T_P) {
      NODEPTR funarg = indir(&FUN(arg));
      if (get_XTAG(funarg) == X_AP &&
          get_XTAG(indir(&FUN(funarg))) == T_B &&
          get_XTAG(indir(&ARG(funarg))) == T_C) {
        /* C'B ((B C) P) --> C */
        r = combC;
        COUNT(red_ccbbcp);
        goto red;
      }
    }
    break;
  case T_B:
    if (argt == T_I) {
      /* B I --> I */
      r = combI;
      COUNT(red_bi);
      goto red;
    }
    break;
  case T_C:
    if (funargt == T_C) {
      /* C (C x) --> x */
      r = indir(&ARG(arg));
      COUNT(red_cc);
      goto red;
    }
    if (flip_ops[argt] != D_NONE) {
      /* C op --> flipped op */
      r = mk_TAG(flip_ops[argt]);
      COUNT(red_flip);
      goto red;
    }
    break;
  default:
    break;
  }
  return NIL_NODEPTR;

 red:
  /* Don't create an indirection loop, e.g., for x = I x */
  if (r.node_bits == n.node_bits || !gc_red_ok(n))
    return NIL_NODEPTR;
  set_IND(n, r);
  return r;
}
#endif  /* GCRED */

/*
 * Marking is a DRAM-latency bound pointer chase: a cell is visited once per
 * GC and almost never in cache, so each visit costs a full miss.
 * The work stack lets us know the next MARK_PFQ cells in advance, so we
 * prefetch them and keep that many misses in flight (a FIFO prefetch queue
 * as in Cher et al., "Software prefetching for mark-sweep GC").
 * On an Apple M1 this cut mark time by about 3x; 16 entries was the knee.
 * Set -DMARK_PFQ=0 to get the plain stack-driven loop (e.g. embedded targets).
 */
#if !defined(MARK_PFQ)
#define MARK_PFQ 16
#endif
#if defined(__has_builtin)
#if __has_builtin(__builtin_prefetch)
#define MARK_PREFETCH(p) __builtin_prefetch(p)
#endif
#elif defined(__GNUC__)
#define MARK_PREFETCH(p) __builtin_prefetch(p)
#endif
#if !defined(MARK_PREFETCH)
/* No prefetch available, so the ring would be pure overhead. */
#undef MARK_PFQ
#define MARK_PFQ 0
#endif
#if MARK_PFQ > 0 && !defined(MARK_PFQ_MAXDEPTH)
/*
 * The ring reorders the depth-first walk: it always refills from the newest
 * work-stack entries and consumes one entry per visit, so on a long spine
 * whose visits push more entries than they consume (a list of records,
 * a string read from a handle, ...) the older entries are stranded on the
 * stack and it grows linearly with the length of the list instead of staying
 * at the depth of the structure.  Above this much growth within one mark()
 * call the plain depth-first step is used until the stack has drained again,
 * which bounds the extra stack to this limit plus the structure's depth.
 * The self-compile peaks around 20K slots, well below the default.
 */
#define MARK_PFQ_MAXDEPTH ((stackptr_t)(stack_size / 8))
#endif

static INLINE int
is_marked_used_fast(NODEPTR n, struct node *h_cells, struct node *h_end, heapoffs_t h_start, bits_t *h_fmap)
{
  struct node *p = get_node_ptr(n);
  if (!p)
    return 1;
#if SANITY
  if (p < h_cells || p >= h_end)
    ERR("is_marked_used");
#else
  (void)h_end;
#endif
  heapoffs_t i = (heapoffs_t)(p - h_cells);
  if (i < h_start)
    return 1;
  return (h_fmap[i / BITS_PER_WORD] & ((bits_t)1 << (i % BITS_PER_WORD))) == 0;
}

/* Push a pointer to a NODEPTR slot on the stack, for the mark phase. */
#if MAXSTACKDEPTH
#define MARK_MAXSTACK if (STACK_INDEX(sp) > max_stack_depth) max_stack_depth = STACK_INDEX(sp)
#else
#define MARK_MAXSTACK
#endif
#if STACKOVL
#define MARK_PUSH_SLOT(s) do { if (sp <= sp_limit) stackerr(); (*--sp).node_pptr = (s); MARK_MAXSTACK; } while(0)
#else
#define MARK_PUSH_SLOT(s) do { (*--sp).node_pptr = (s); MARK_MAXSTACK; } while(0)
#endif

/* Mark all used nodes reachable from *np, updating *np. */
void
mark(NODEPTR *np)
{
  NODEPTR * const stk = stack_ptr;
  NODEPTR *sp = stk;
#if STACKOVL
  NODEPTR * const sp_limit = stack_limit;
#endif
  struct node * const h_cells = cells;
  struct node * const h_end = h_cells + heap_size;
  const heapoffs_t h_start = heap_start;
  bits_t * const h_fmap = free_map;
#if GCRED
  const int h_gc_red = want_gc_red;
#endif
  counter_t marked = 0;
  NODEPTR n;
  struct node *p;
#if MARK_PFQ > 0
  /* Ring of pending slots whose cells have been prefetched.
   * Every (possibly nested) mark() call owns its own ring and drains it
   * before returning, so recursion for integer leaves and the
   * thread/mvar side structures is unaffected. */
  NODEPTR *pfq[MARK_PFQ];
  unsigned pfq_head = 0, pfq_tail = 0, pfq_n = 0;
  NODEPTR * const pfq_floor = stk - stack_limit > MARK_PFQ_MAXDEPTH ? stk - MARK_PFQ_MAXDEPTH : stack_limit;
#endif

  //  mark_depth++;
  //  if (mark_depth % 10000 == 0)
  //    PRINT("mark depth %"PRIcounter"\n", mark_depth);
 top:
  n = indir(np);
  p = get_node_ptr(n);
  if (!p)
    goto fin;                   /* not a real pointer, but there may be more pending work */

  /* n/p points to a real node */
  if (p < h_cells || p >= h_end)
    ERR("bad n");
  heapoffs_t idx = (heapoffs_t)(p - h_cells);
  if (idx < h_start)
    goto fin;
  bits_t *fp = &h_fmap[idx / BITS_PER_WORD];
  bits_t mask = (bits_t)1 << (idx % BITS_PER_WORD);
  bits_t word = *fp;
  if ((word & mask) == 0)
    goto fin;
  *fp = word & ~mask;
  marked++;

  NODEPTR f = FUN(n);
  if (is_AP(f)) {
#if GCRED
    if (h_gc_red) {
      NODEPTR r = gc_reduce(n);
      if (!is_NIL(r)) {
        /* n is now an indirection to r.  Redirect this reference and mark r;
         * other references to n will be redirected when they are marked. */
        *np = r;
        goto top;
      }
      f = FUN(n);               /* gc_reduce may have redirected FUN(n) */
    }
#endif  /* GCRED */
#if MARK_PFQ > 0
    if (sp >= pfq_floor) {
      /* The probe keeps already-marked (shared) nodes, and virtual pointers,
       * off the work stack; it only touches the bitmap, not the cell.
       * The cell itself is not read until the ring has prefetched it, which is
       * why the mark-integer-leaves shortcut of the plain step below (it needs
       * the cell's tag) is not used. */
      NODEPTR a = ARG(n);
      if (!is_marked_used_fast(a, h_cells, h_end, h_start, h_fmap))
        MARK_PUSH_SLOT(&ARG(n));
      /* FUN becomes a work item too, so the ring can prefetch it.
       * It is pushed last so that the spine is still walked first. */
      if (!is_marked_used_fast(f, h_cells, h_end, h_start, h_fmap))
        MARK_PUSH_SLOT(&FUN(n));
      goto fin;
    }
    /* Stack has grown too much (see MARK_PFQ_MAXDEPTH): plain step. */
#endif  /* MARK_PFQ */
    /* Mark integer leaves immediately instead of deferring them while walking
     * the other branch.  In particular, a string can contain a distinct integer
     * node (behind an indirection) for every character read from a handle.
     */
    if (get_XTAG(indir(&ARG(n))) == D_INT) {
      stack_ptr = sp;
      mark(&ARG(n));
      sp = stack_ptr;
    } else if (!is_marked_used_fast(ARG(n), h_cells, h_end, h_start, h_fmap)) {
      /* The probe keeps already-marked (shared) nodes off the work stack. */
      PREFETCH_READ(ARG(n).node_ptr);   /* won't be popped and dereferenced until later */
      MARK_PUSH_SLOT(&ARG(n));
    }
    /* Iterate straight down the spine; keeps the work stack shallow. */
    np = &FUN(n);
    goto top;
  }

  switch (get_DATA_TAG(f)) {
#if GCRED && INTTABLE
  case D_INT:
    {
      /* Replace a small INT by the shared permanent node */
      value_t val = GETVALUE(n);
      if (LOW_INT <= val && val < HIGH_INT) {
        NODEPTR i = intTable[val - LOW_INT];
        set_IND(n, i);          /* other references will be redirected later */
        *np = i;                /* redirect this reference */
        COUNT(red_int);
      }
      goto fin;
    }
#endif  /* GCRED && INTTABLE */
  case D_ARR:
    {
      struct ioarray *arr = ARR(n);

      // arr->marked records marking progress through arr.
      if (arr->marked >= arr->size) {
        // An empty array has nothing to scan, but the sweep must keep it.
        if (arr->size == 0)
          arr->marked = 1;
        goto fin;
      }
      // We unmark the array as a whole and push it as long
      // as there's more entries to scan.
      *fp |= mask;
      marked--;
      MARK_PUSH_SLOT(np);
      if (!is_marked_used_fast(arr->array[arr->marked], h_cells, h_end, h_start, h_fmap))
        MARK_PUSH_SLOT(&arr->array[arr->marked]);
      arr->marked++;
      goto fin;
    }

  case D_FORPTR:
    FORPTR(n)->finalizer->marked = 1;
    goto fin;

  case D_THID:
    stack_ptr = sp;
    mark_thread(THR(n));
    sp = stack_ptr;
    goto fin;

  case D_MVAR:
    stack_ptr = sp;
    mark_mvar(MVAR(n));
    sp = stack_ptr;
    goto fin;

  case D_WEAK:
    WEAK(n)->marked = 1;
    goto fin;

  default:
    goto fin;
  }

 fin:
#if MARK_PFQ > 0
  if (sp >= pfq_floor) {
    /* Top up the ring from the work stack, issuing a prefetch for each
     * pending cell, then process the oldest entry: by now its cell has had
     * MARK_PFQ-1 other visits worth of time to arrive in cache. */
    while (pfq_n < MARK_PFQ && sp < stk) {
      NODEPTR *slot = (*sp++).node_pptr;
      MARK_PREFETCH(slot->node_ptr);
      pfq[pfq_tail] = slot;
      pfq_tail = (pfq_tail + 1) % MARK_PFQ;
      pfq_n++;
    }
    if (pfq_n > 0) {
      np = pfq[pfq_head];
      pfq_head = (pfq_head + 1) % MARK_PFQ;
      pfq_n--;
      goto top;
    }
    /* Ring empty here means the loop above found the stack empty. */
    stack_ptr = sp;
    num_marked += marked;
    return;
  }
  /* Too deep (see MARK_PFQ_MAXDEPTH): plain depth-first pops until the
   * stack has drained; the ring's pending entries wait, they are picked up
   * again when the ring is resumed. */
#endif  /* MARK_PFQ */
  if (sp < stk) {
    np = (*sp++).node_pptr;
    goto top;
  }
  stack_ptr = sp;
  num_marked += marked;
}

/* Perform a garbage collection:
   - Mark nodes from the stack
   - Mark permanent arrays
   - Mark threads that have a root
   - Scan and free arrays
   - Scan and free foreign pointers and run finalizers
   - Scan and free threads
   - Scan and free mvars
*/
void
gc(void)
{
  stackptr_t i;
  NODEPTR *sp;
  //printf("****** GC ********\n");

  // gc_tot += stack_ptr+1;

  num_gc++;
  num_marked = 0;
#if WANT_STDIO
  if (verbose > 1)
    PRINT("gc mark\n");
#endif
  gc_mark_time -= GETTIMEMILLI();
  mark_all_free();
#if GCRED
  gc_eval_stack_ptr = stack_ptr;
#endif
  /* Mark everything reachable from the stack */
  for (sp = stack_bot; sp > stack_ptr; )
    mark(--sp);

  /* Mark the labelled nodes of a parse in progress */
  mark_parse();

  /* Mark everything reachable from permanent array nodes */
  for (struct ioarray *arr = array_root; arr; arr = arr->next) {
    if (arr->permanent) {
      for (i = 0; i < arr->size; i++)
        mark(&arr->array[i]);
    }
  }

  /* Mark all FFI exports */
  if (xffe_table) {
    for(struct ffe_entry *f = xffe_table; f->ffe_name; f++) {
      mark((NODEPTR*)&f->ffe_value);
    }
  }

  /* Mark used stable pointers */
  for (size_t i = 0; i < sp_capacity; i++) {
    if (!is_NIL(sp_table[i]))
      mark(&sp_table[i]);
  }

  /* Mark everything reachable from the threads.
   * Note, zombie threads have no root so they are not marked.
   */
  for (struct mthread *mt = all_threads; mt; mt = mt->mt_next) {
    if (!is_NIL(mt->mt_root))
      mark_thread(mt);
  }

  /* check for unmarked weak pointers */
  sweep_weaks();

  gc_mark_time += GETTIMEMILLI();

  if (num_marked > max_num_marked)
    max_num_marked = num_marked;
  num_free = heap_size - heap_start - num_marked;
  if (num_free < heap_size / 50)
    ERR("heap exhausted");

  gc_scan_time -= GETTIMEMILLI();
  /* Free unused arrays */
  for (struct ioarray **arrp = &array_root; *arrp; ) {
    struct ioarray *arr = *arrp;
    if (arr->marked || arr->permanent) {
      arr->marked = 0;
      arrp = &arr->next;
    } else {
      *arrp = arr->next;        /* unlink */
      num_arr_free++;
      FREE(arr);                /* and FREE */
    }
  }

  /* Run finalizers on unused foreign pointers. */
  for (struct final **finp = &final_root; *finp; ) {
    struct final *fin = *finp;
    if (fin->marked) {
      fin->marked = 0;
      finp = &fin->next;
    } else {
      /* Unused, run finalizer and free all associated memory */
      if (fin->size == NOSIZE) {
        num_fin_free++;
      } else {
        num_bs_free++;
        num_bs_inuse -= fin->size;
        if (num_bs_alloc - num_bs_free > num_bs_alloc_max)
          num_bs_alloc_max = num_bs_alloc - num_bs_free;
      }
      void (*f)(void *) = (void (*)(void *))fin->final;
      //printf("forptr free fin=%p, f=%p", fin, f);
      //fflush(stdout);
      if (f) {
        //printf("finalizer fin=%p final=%p\n", fin, f);
        (*f)(fin->arg);
      }
      for (struct forptr *p = fin->back; p; ) {
        struct forptr *q = p->next;
        //printf("free fp=%p\n", p);
        //printf(" p=%p desc=%s", p, p->desc ? p->desc : "NONE");
        //fflush(stdout);
        FREE(p);
        //memset(p, 0x55, sizeof *p);
        p = q;
      }
      //printf("\n");
      *finp = fin->next;
      //printf("free fin=%p\n", fin);
      FREE(fin);
      //memset(fin, 0x77, sizeof *fin);
    }
  }

  /* Remove unreferenced zombie threads */
  for (struct mthread **mtp = &all_threads; *mtp; ) {
    struct mthread *mt = *mtp;
    if ((mt->mt_state == ts_died || mt->mt_state == ts_finished) && !mt->mt_mark) {
      COUNT(num_thread_reap);
      *mtp = mt->mt_next;
      FREE(mt);
    } else {
      mt->mt_mark = false;
      mtp = &mt->mt_next;
    }
  }

  /* Remove unreferences mvars */
  for (struct mvar **mvp = &all_mvars; *mvp; ) {
    struct mvar *mv = *mvp;
    if (!mv->mv_mark) {
      COUNT(num_mvar_free);
      *mvp = mv->mv_next;
      FREE(mv);
    } else {
      mv->mv_mark = false;
      mvp = &mv->mv_next;
    }
  }

  gc_scan_time += GETTIMEMILLI();

#if WANT_STDIO
  if (verbose > 1) {
    PRINT("gc done, %"PRIcounter" free\n", num_free);
    /*PRINT(" GC reductions A=%"PRIcounter", K=%"PRIcounter", I=%"PRIcounter", int=%"PRIcounter" flip=%"PRIcounter"\n",
      red_a, red_k, red_i, red_int, red_flip);*/
  }
  if (gcbell) {
    fputc('\007', stderr);      /* ring the bell */
    fflush(stderr);
  }
#endif  /* !WANT_STDIO */

#if 0
  /* For debugging only: mark all free cells */
  for(int n = 0; n < heap_size; n++) {
    NODEPTR p;
    p.node_ptr = HEAPREF(n);
    if (!is_marked_used(p)) {
      set_DATA_TAG(p, D_NONE);
    }
  }
#endif
#if 0
  {
    BFILE *err = add_fd(2);
    putsb("GC ", err); putdecb(num_free, err); putsb(" free\r\n", err);
    closeb(err);
  }
#endif
}

static INLINE
value_t
peekWord(value_t *p)
{
  return *p;
}

static INLINE
void
pokeWord(value_t *p, value_t w)
{
  *p = w;
}

static INLINE
void *
peekPtr(void **p)
{
  return *p;
}

static INLINE
void
pokePtr(void **p, void *w)
{
  *p = w;
}

static INLINE
uvalue_t
peek_uint8(uint8_t *p)
{
  return *p;
}

static INLINE
void
poke_uint8(uint8_t *p, value_t w)
{
  *p = (uint8_t)w;
}

static INLINE
uvalue_t
peek_uint16(uint16_t *p)
{
  return *p;
}

static INLINE
void
poke_uint16(uint16_t *p, value_t w)
{
  *p = (uint16_t)w;
}

static INLINE
uvalue_t
peek_uint32(uint32_t *p)
{
  return *p;
}

static INLINE
void
poke_uint32(uint32_t *p, value_t w)
{
  *p = (uint32_t)w;
}

#if WANT_INT64
static INLINE
uint64_t
peek_uint64(uint64_t *p)
{
  return *p;
}

static INLINE
void
poke_uint64(uint64_t *p, uint64_t w)
{
  *p = w;
}
#endif  /* WANT_INT64 */

static INLINE
value_t
peek_int8(int8_t *p)
{
  return *p;
}

static INLINE
void
poke_int8(int8_t *p, value_t w)
{
  *p = (int8_t)w;
}

static INLINE
value_t
peek_int16(int16_t *p)
{
  return *p;
}

static INLINE
void
poke_int16(int16_t *p, value_t w)
{
  *p = (int16_t)w;
}

static INLINE
value_t
peek_int32(int32_t *p)
{
  return *p;
}

static INLINE
void
poke_int32(int32_t *p, value_t w)
{
  *p = (int32_t)w;
}

#if WANT_INT64
static INLINE
int64_t
peek_int64(int64_t *p)
{
  return *p;
}

static INLINE
void
poke_int64(int64_t *p, int64_t w)
{
  *p = w;
}
#endif  /* WANT_INT64 */

static INLINE
value_t
peek_int(int *p)
{
  return *p;
}

static INLINE
void
poke_int(int *p, value_t w)
{
  *p = (int)w;
}

static INLINE
value_t
peek_uint(unsigned int *p)
{
  return *p;
}

static INLINE
void
poke_uint(unsigned int *p, value_t w)
{
  *p = (unsigned int)w;
}

static INLINE
value_t
peek_char(char *p)
{
  return *p;
}

static INLINE
void
poke_char(char *p, value_t w)
{
  *p = (char)w;
}

static INLINE
value_t
peek_schar(signed char *p)
{
  return *p;
}

static INLINE
void
poke_schar(signed char *p, value_t w)
{
  *p = (signed char)w;
}

static INLINE
value_t
peek_uchar(unsigned char *p)
{
  return *p;
}

static INLINE
void
poke_uchar(unsigned char *p, value_t w)
{
  *p = (unsigned char)w;
}

static INLINE
value_t
peek_short(short *p)
{
  return *p;
}

static INLINE
void
poke_short(short *p, value_t w)
{
  *p = (short)w;
}

static INLINE
value_t
peek_ushort(unsigned short *p)
{
  return *p;
}

static INLINE
void
poke_ushort(unsigned short *p, value_t w)
{
  *p = (unsigned short)w;
}

static INLINE
value_t
peek_long(long *p)
{
  return *p;
}

static INLINE
void
poke_long(long *p, value_t w)
{
  *p = (long)w;
}

static INLINE
value_t
peek_ulong(unsigned long *p)
{
  return *p;
}

static INLINE
void
poke_ulong(unsigned long *p, value_t w)
{
  *p = (unsigned long)w;
}

/* A long long can be bigger than a value_t, so it is passed as itself. */
static INLINE
long long
peek_llong(long long *p)
{
  return *p;
}

static INLINE
void
poke_llong(long long *p, long long w)
{
  *p = w;
}

static INLINE
unsigned long long
peek_ullong(unsigned long long *p)
{
  return *p;
}

static INLINE
void
poke_ullong(unsigned long long *p, unsigned long long w)
{
  *p = w;
}

static INLINE
value_t
peek_size_t(size_t *p)
{
  return *p;
}

static INLINE
void
poke_size_t(size_t *p, value_t w)
{
  *p = (size_t)w;
}

#if WANT_FLOAT32
static INLINE
flt32_t
peek_flt32(flt32_t *p)
{
  return *p;
}

static INLINE
void
poke_flt32(flt32_t *p, flt32_t w)
{
  *p = w;
}
#endif  /* WANT_FLOAT32 */

#if WANT_FLOAT64
static INLINE
flt64_t
peek_flt64(flt64_t *p)
{
  return *p;
}

static INLINE
void
poke_flt64(flt64_t *p, flt64_t w)
{
  *p = w;
}
#endif  /* WANT_FLOAT64 */

/* Look up an FFI function by name */
value_t
lookupFFIname(const char *name)
{
  size_t i;

  for(i = 0; ffi_table[i].ffi_name; i++)
    if (strcmp(ffi_table[i].ffi_name, name) == 0)
      return (value_t)i;
  if (xffi_table) {
    for(i = 0; xffi_table[i].ffi_name; i++)
      if (strcmp(xffi_table[i].ffi_name, name) == 0)
        return (value_t)(i + num_ffi);
  }
  return -1;
}

NODEPTR
ffiNode(const char *buf)
{
  NODEPTR r;
  value_t i = lookupFFIname(buf);
  char *fun;

  if (i < 0) {
    /* lookup failed, generate a node that will dynamically generate an error */
    r = alloc_node(D_BADDYN);
    fun = mmalloc(strlen(buf) + 1);
    strcpy(fun, buf);
    CSTR(r) = fun;
  } else {
    r = alloc_node(D_IO_CCALL);
    SETVALUE(r, i);
  }
  return r;
}

/* If the next input character is c, then consume it, else leave it alone. */
int
gobble(BFILE *f, int c)
{
  int d = getb(f);
  if (c == d) {
    return 1;
  } else {
    if (d >= 0)
      ungetb(d, f);
    return 0;
  }
}

value_t
parse_int(BFILE *f)
{
  // Parse using uvalue_t, which wraps on overflow.
  uvalue_t i = 0;
  uvalue_t neg = 1;
  int c = getb(f);
  if (c == '-') {
    neg = -1;
    c = getb(f);
  }
  for(;;) {
    i = i * 10 + (c - '0');
    c = getb(f);
    if (c < '0' || c > '9') {
      if (c >= 0)
        ungetb(c, f);
      break;
    }
  }
  // Multiply by neg without triggering undefined behavior.
  return (value_t)(neg * i);
}

#if WANT_INT64
int64_t
parse_int64(BFILE *f)
{
  // Parse using uint64_t, which wraps on overflow.
  uint64_t i = 0;
  uint64_t neg = 1;
  int c = getb(f);
  if (c == '-') {
    neg = -1;
    c = getb(f);
  }
  for(;;) {
    i = i * 10 + (c - '0');
    c = getb(f);
    if (c < '0' || c > '9') {
      if (c >= 0)
        ungetb(c, f);
      break;
    }
  }
  // Multiply by neg without triggering undefined behavior.
  return (int64_t)(neg * i);
}
#endif  /* WANT_INT64 */

struct forptr *mkForPtr(struct bytestring bs);
NODEPTR mkFunPtr(HsFunPtr p);

/* Create a forptr that has a free() finalizer. */
struct forptr *
mkForPtrFree(struct bytestring str)
{
  struct forptr *fp = mkForPtr(str);         /* Create a foreign pointer */
  fp->finalizer->final = (HsFunPtr)FREE;     /* and set the finalizer to just free it */
  return fp;
}

/* Create a ByteString node.  It's a foreign pointer tagged with FP_BSTR. */
NODEPTR
mkStrNode(struct bytestring str)
{
  NODEPTR n = alloc_node(D_FORPTR);
  struct forptr *fp = mkForPtrFree(str);
  FORPTR(n) = fp;
  fp->finalizer->fptype = FP_BSTR;
  //printf("mkForPtr n=%p fp=%p %d %s payload.string=%p\n", n, fp, (int)FORPTR(n)->payload.size, (char*)FORPTR(n)->payload.string, FORPTR(n)->payload.string);
  return n;
}

/* Table of labelled nodes for sharing during parsing. */
struct shared_entry {
  heapoffs_t label;
  NODEPTR node;                 /* NIL indicates unused */
};

/* The memory allocated here is never freed.
 * This could be fixed by using a forptr and a
 * finalizer for read UTF-8 strings.
 * Fix this if there is a lot of deserialization.
 */
struct bytestring
parse_string(BFILE *f)
{
  size_t sz = 20;
  uint8_t *buffer = mmalloc(sz);
  size_t i;
  int c;

  for(i = 0;;) {
    c = getb(f);
    if (c < 0)
      ERR("parse string EOF");
    if (c == '"')
      break;
    if (i >= sz - 1) {
      sz *= 2;
      buffer = mrealloc(buffer, sz);
    }
#if 0
    if (c == '\\') {
      buffer[i++] = (uint8_t)parse_int(f);
      if (!gobble(f, '&'))
        ERR("parse string");
    } else {
      buffer[i++] = c;
    }
#else
    /* See src/MicroHs/ExpPrint.hs for how strings are encoded. */
    switch (c) {
    case '\\':
      c = getb(f);
      if (c == '?')
        c = 0x7f;
      else if (c == '_')
        c = 0xff;
      break;
    case '^':
      c = getb(f);
      if (c < 0x40)
        c &= 0x1f;
      else
        c = (c & 0x1f) | 0x80;
      break;
    case '|':
      c = getb(f);
      c |= 0x80;
      break;
    default:
      /* Unencoded */
      ;
    }
    buffer[i++] = c;
#endif
  }
  buffer[i] = 0;    /* add a trailing 0 in case we need a C string, not counted in size */
  buffer = mrealloc(buffer, i + 1);
  //printf("parse_string %d %s\n", (int)bs.size, (char*)bs.string);
  return mk_ro_bytestring(i, buffer);
}

struct forptr *new_mpz(void);

struct parse_ctx {
  struct shared_entry *shared_table;
  heapoffs_t shared_table_size;
  jmp_buf deser_err;
  const char *message;
};

/* The parse in progress, if any. */
struct parse_ctx *cur_parse_ctx = 0;

/* The labelled nodes are GC roots while parsing.
 * They are usually reachable from the stack as well, but the GC can
 * redirect all those references, e.g., past an indirection made for a
 * forward reference, or to a permanent Int node.  The table would then
 * refer to a freed node.  Marking the table redirects its entries too.
 * An undefined forward reference is an indirection to NIL; the marking
 * stops there, so the entry stays and can be filled in later.
 */
void
mark_parse(void)
{
  struct parse_ctx *pc = cur_parse_ctx;
  if (!pc)
    return;
  for (heapoffs_t i = 0; i < pc->shared_table_size; i++) {
    if (!is_NIL(pc->shared_table[i].node))
      mark(&pc->shared_table[i].node);
  }
}

/* Look for the label in the table.
 * If it's found, return the node.
 * If not found, return the first empty entry.
*/
NODEPTR *
find_label(struct parse_ctx *pc, heapoffs_t label)
{
  heapoffs_t i;

  for(i = label; ; i++) {
    i %= pc->shared_table_size;
    if (is_NIL(pc->shared_table[i].node)) {
      /* The slot is empty, so claim and return it */
      pc->shared_table[i].label = label;
      return &pc->shared_table[i].node;
    } else if (pc->shared_table[i].label == label) {
      /* Found the label, so return it. */
      return &pc->shared_table[i].node;
    }
    /* Not empty and not found, try next. */
  }
}

#define DESER_ERR(s) do { pc->message = (s); longjmp(pc->deser_err, 1); } while(0)

/* Read a token terminated by ' ' or '\n' into buf[j..size-1], NUL-terminated. */
static void
parse_token(struct parse_ctx *pc, BFILE *f, char *buf, size_t j, size_t size)
{
  for (;;) {
    int c = getb(f);
    if (c < 0)
      DESER_ERR("parse EOF");
    if (c == ' ' || c == '\n') {
      buf[j] = 0;
      return;
    }
    if (c == 0 || j >= size - 1)
      DESER_ERR("bad input");
    buf[j++] = (char)c;
  }
}

NODEPTR
parse(struct parse_ctx *pc, BFILE *f)
{
  NODEPTR *stk = stack_ptr;
  NODEPTR r, x, y;
  NODEPTR *nodep;
  heapoffs_t l;
  int c;
  char buf[80];                 /* store names of primitives. */

  for(;;) {
    c = getb(f);
    if (c < 0) DESER_ERR("parse EOF");
    switch (c) {
    case ' ':
    case '\n':
      continue;
    }
    GCCHECK(1);                 /* each step allocates at most one node */
    if (num_free <= 1)
      DESER_ERR("out of heap reading code");
    switch(c) {
    case '@':
      if (STACK_ABOVE(stk) < 2)
        DESER_ERR("parse: stack");
      x = TOP(0);
      y = TOP(1);
      POP(2);
      PUSH(new_ap(y, x));
      break;
    case '}':
      if (STACK_ABOVE(stk) != 1)
        DESER_ERR("parse: stack");
      x = TOP(0);
      POP(1);
      return x;
#if WANT_GMP || WANT_IMATH
    case '%':
      {
        struct bytestring bs = parse_string(f); /* get all the digits, terminated by " */
        struct forptr *fp = new_mpz();          /* a new mpz */
        mpz_ptr op = fp->payload.bs_array;      /* get actual pointer */
        mpz_set_str(op, bs.bs_array, 10);       /* convert to an mpz */
        FREE(bs.bs_array);
        r = alloc_node(D_FORPTR);
        FORPTR(r) = fp;
        PUSH(r);
        break;
      }
#endif
    case '&':
      {
        int is32 = gobble(f, '&');
        parse_token(pc, f, buf, 0, sizeof buf);
        if (is32) {
#if WANT_FLOAT32
          r = mkFlt32(strtof(buf, NULL));
#else
          r = alloc_node(D_FLT32);
          SETVALUE(r, 0);
#endif
        } else {
#if WANT_FLOAT64
          r = mkFlt64(strtod(buf, NULL));
#else
          r = alloc_node(D_DBL);
          SETVALUE(r, 0);
#endif
        }
        PUSH(r);
        break;
      }
    case '#':
      if (gobble(f, '#')) {
#if WANT_INT64
        r = mkInt64(parse_int64(f));
#else
        DESER_ERR("no Int64");
#endif /* WANT_INT64 */
      } else {
        r = mkInt(parse_int(f));
      }
      PUSH(r);
      break;
    case '[':
      {
        value_t vsz;
        size_t sz;
        struct ioarray *arr;
        size_t i;
        vsz = parse_int(f);
        if (!gobble(f, ']'))
          DESER_ERR("parse arr 1");
        if (vsz < 0 || vsz > STACK_ABOVE(stk))
          DESER_ERR("parse: stack");
        sz = (size_t)vsz;
        arr = arr_alloc(sz, NIL_NODEPTR);
        for (i = 0; i < sz; i++) {
          arr->array[i] = TOP(sz - i - 1);
        }
        r = alloc_node(D_ARR);
        ARR(r) = arr;
        POP(sz);
        PUSH(r);
        break;
      }
    case '_':
      /* Reference to a shared value: _label */
      l = parse_int(f);  /* The label */
      nodep = find_label(pc, l);
      if (is_NIL(*nodep)) {
        /* Not yet defined, so make it an indirection */
        *nodep = alloc_node(D_NONE);
        set_IND(*nodep, NIL_NODEPTR);
      }
      PUSH(*nodep);
      break;
    case ':':
      /* Define a shared expression: :label e */
      l = parse_int(f);  /* The label */
      if (!gobble(f, ' '))
        DESER_ERR("parse ' '");
      if (STACK_ABOVE(stk) < 1)
        DESER_ERR("parse: stack");
      nodep = find_label(pc, l);
      x = TOP(0);
      if (is_NIL(*nodep)) {
        /* not referenced yet, so add a direct reference */
        *nodep = x;
      } else {
#if 0
        /* Sanity check */
        NODEPTR q = get_IND(*nodep);
        if (!q || is_NIL(q))
          ERR("shared != NIL");
#endif
        set_IND(*nodep, x);
      }
      break;
    case '"':
      /* Everything up to the next " is a string.
       * Special characters are encoded as \NNN&,
       * where NNN is the decimal value of the character */
      PUSH(mkStrNode(parse_string(f)));
      break;
    case '$':
      {
        struct bytestring bs = mk_ro_bytestring(parse_int(f), NULL);
        if (!gobble(f, ' '))
          DESER_ERR("bytestring parse error");
        for(size_t i = 0; i < bs.bs_size; i++) {
          ((uint8_t*)bs.bs_array)[i] = getb(f);
        }
        PUSH(mkStrNode(bs));
        break;
      }

#if WANT_TICK
    case '!':
      if (!gobble(f, '"'))
        DESER_ERR("parse !");
      r = alloc_node(D_TICK);
      SETVALUE(r, (value_t)add_tick_table(parse_string(f)));
      PUSH(r);
      break;
#endif
    case '^':
      /* An FFI name */
      parse_token(pc, f, buf, 0, sizeof buf);
      r = ffiNode(buf);
      PUSH(r);
      break;
    case ';':
      /* <name is a C function pointer to name */
      parse_token(pc, f, buf, 0, sizeof buf);
      if (strcmp(buf, "0") == 0) {
        PUSH(mkFunPtr((HsFunPtr)0));
      } else if (strcmp(buf, "closeb") == 0) {
        PUSH(mkFunPtr((HsFunPtr)closeb));
      } else {
        DESER_ERR("unknown funptr");
      }
      break;
    case 'C':
      if (gobble(f, '_')) {
        value_t k = parse_int(f);
        if (!gobble(f, '_'))
          DESER_ERR("parse C");
        value_t n = parse_int(f);
        if (k < 0 || k > PTR_TAG_NCON_MASK || n < 0 || n > PTR_TAG_NARG_MASK)
          DESER_ERR("parse C");
        r = mk_CON(V_CONSTR, (int)k, (int)n);
        PUSH(r);
        break;
      } else {
        goto def;
      }
    case 'D':
      if (gobble(f, '_')) {
        /* D_a0_..._a{m-1}, m is at most 2047 since the LOOKD marker holds m in 11 bits */
        uint8_t arities[PTR_TAG_NCON_MASK];
        uint32_t m = 0;
        do {
          value_t a = parse_int(f);
          if (m >= PTR_TAG_NCON_MASK || a < 0 || a > PTR_TAG_NARG_MASK)
            DESER_ERR("parse D");
          arities[m++] = (uint8_t)a;
        } while (gobble(f, '_'));
        r = mk_CASE(V_CASED, find_case_arity(m, 0, arities));
        PUSH(r);
        break;
      } else {
        goto def;
      }      
    case 'L':
      if (gobble(f, '_')) {
        int m = parse_int(f);
        r = mk_CASE(V_LOOKD, m);
        PUSH(r);
        break;
      } else {
        goto def;
      }      
    case 'E':
    case 'M':
      if (gobble(f, '_')) {
        /* E_k1_n1_..._km_nm, the constructor numbers increasing (M_ is the same, for the marker) */
        uint16_t tags[PTR_TAG_NCON_MASK];
        uint8_t arities[PTR_TAG_NCON_MASK];
        uint32_t m = 0;
        do {
          value_t k = parse_int(f);
          if (!gobble(f, '_'))
            DESER_ERR("parse E");
          value_t a = parse_int(f);
          if (m >= PTR_TAG_NCON_MASK || k < 0 || k > PTR_TAG_NCON_MASK || (m > 0 && k <= tags[m-1]) ||
              a < 0 || a > PTR_TAG_NARG_MASK)
            DESER_ERR("parse E");
          tags[m] = (uint16_t)k;
          arities[m++] = (uint8_t)a;
        } while (gobble(f, '_'));
        r = mk_CASE(c == 'E' ? V_CASES : V_LOOKS, find_case_arity(m, tags, arities));
        PUSH(r);
        break;
      } else {
        goto def;
      }
    def:
    default:
      if (c == 0)
        DESER_ERR("bad input");
      buf[0] = (char)c;
      /* A primitive, keep getting char's until end */
      parse_token(pc, f, buf, 1, sizeof buf);

      /* Look up the primop (O(1) hash lookup) and make a virtual pointer for it. */
      {
        int t = lookup_primop(buf);
        if (t < 0)
          DESER_ERR("unknown primop");
        switch (t) {
#if WANT_STDIO
        /* The standard handles are real (permanent) FORPTR nodes */
        case T_IO_STDIN:  r = comb_stdin;  break;
        case T_IO_STDOUT: r = comb_stdout; break;
        case T_IO_STDERR: r = comb_stderr; break;
#endif  /* WANT_STDIO */
        default:          r = alloc_node(t); break;
        }
      }
      PUSH(r);
      break;
    }
  }
}

void
checkversion(struct parse_ctx *pc, BFILE *f)
{
  char *p = VERSION;
  int c;

  while ((c = *p++)) {
    if (c != getb(f))
      DESER_ERR("version mismatch");
  }
  (void)gobble(f, '\r');                 /* allow extra CR */
}

/* Parse a file */
/* XXX We should not be using the ordinary stack here. */
NODEPTR
parse_top(BFILE *f, struct ffe_entry *ffe)
{
  heapoffs_t numLabels, i;
  NODEPTR n;
  NODEPTR *ostack = stack_ptr;
  struct parse_ctx spc, *pc = &spc;

  pc->shared_table = 0;
  if (setjmp(pc->deser_err)) {
    /* exception */
    stack_ptr = ostack;
    cur_parse_ctx = 0;
    if (pc->shared_table)
      FREE(pc->shared_table);
    return NIL_NODEPTR;
  } else {
    checkversion(pc, f);
    numLabels = parse_int(f);
    if (!gobble(f, '\n'))
      DESER_ERR("size parse");
    gobble(f, '\r');                 /* allow extra CR */
    pc->shared_table_size = 3 * numLabels; /* sparsely populated hashtable */
    pc->shared_table = mmalloc(pc->shared_table_size * sizeof(struct shared_entry));
    for(i = 0; i < pc->shared_table_size; i++)
      pc->shared_table[i].node = NIL_NODEPTR;
    cur_parse_ctx = pc;           /* the labels are GC roots */
    n = parse(pc, f);
    if (ffe) {
      for(struct ffe_entry *f = ffe; f->ffe_name; f++) {
        heapoffs_t l = atoi(f->ffe_name+1); /* the name must be numerical */
        f->ffe_value = find_label(pc, l)->node_ptr;
      }
    }
    cur_parse_ctx = 0;
    FREE(pc->shared_table);
    return n;
  }
}

/* Two bits per node: marked, shared
 * 0, 0   -- not visited
 * 1, 0   -- visited once
 * 1, 1   -- visited more than once
 * 0, 1   -- printed
 */
struct print_bits {
  bits_t *marked_bits;
  bits_t *shared_bits;
  jmp_buf ser_err;
  const char *message;
};
/* Virtual NODEPTRs are never shared, so they have no bits. */
static INLINE void set_bit(bits_t *bits, NODEPTR n)
{
  if (is_VIRT(n))
    return;
  heapoffs_t i = LABEL(n);
  bits[i / BITS_PER_WORD] |= (1ULL << (i % BITS_PER_WORD));
}
#if WANT_STDIO
static INLINE void clear_bit(bits_t *bits, NODEPTR n)
{
  if (is_VIRT(n))
    return;
  heapoffs_t i = LABEL(n);
  bits[i / BITS_PER_WORD] &= ~(1ULL << (i % BITS_PER_WORD));
}
#endif
static INLINE int test_bit(bits_t *bits, NODEPTR n)
{
  if (is_VIRT(n))
    return 0;
  heapoffs_t i = LABEL(n);
  return (bits[i / BITS_PER_WORD] & (1ULL << (i % BITS_PER_WORD))) != 0;
}

size_t strNodes(size_t len);
NODEPTR mkStringC(char *str);

#if WANT_STDIO
void
convdbl(char *str, char *fmt, flt64_t x)
{
  /* Using 16 decimals will lose some precision.
   * 17 would keep the precision, but it frequently looks very ugly.
   */
  (void)snprintf(str, 25, fmt, x);
  if (strcmp(str, "nan") != 0 && strcmp(str, "-nan") != 0 &&
      strcmp(str, "inf") != 0 && strcmp(str, "-inf") != 0 &&
      !strchr(str, '.') && !strchr(str, 'e') && !strchr(str, 'E')) {
    /* There is no decimal point and no exponent, so add a decimal point */
    strcat(str, ".0");
  }
}

void
putdblb(flt64_t x, BFILE *p)
{
  char str[30];
  convdbl(str, "%.16g", x);
  putsb(str, p);
}

void printrec(BFILE *f, struct print_bits *pb, NODEPTR n, bool prefix);

/* Mark all reachable nodes, when a marked node is reached, mark it as shared. */
void
find_sharing(counter_t *num_sharedp, struct print_bits *pb, NODEPTR n)
{
 top:
  { NODEPTR np = n;
    n = indir(&np);
  }
  struct node *p = get_node_ptr(n);
  if (!p)
    return;
  if (p < cells || p >= cells + heap_size) abort();

  //PRINT("find_sharing %p %llu ", n, LABEL(n));
  tag_t tag = get_XTAG(n);
  if (tag == X_AP || tag == D_ARR || tag == D_FORPTR) {
    if (test_bit(pb->shared_bits, n)) {
      /* Alread marked as shared */
      //PRINT("shared\n");
      ;
    } else if (test_bit(pb->marked_bits, n)) {
      /* Already marked, so now mark as shared */
      //PRINT("marked\n");
      set_bit(pb->shared_bits, n);
      (*num_sharedp)++;
    } else {
      /* Mark as visited, and recurse */
      //PRINT("unmarked\n");
      set_bit(pb->marked_bits, n);
      switch(tag) {
      case X_AP:
        find_sharing(num_sharedp, pb, FUN(n));
        n = ARG(n);
        goto top;
      case D_ARR:
        for(size_t i = 0; i < ARR(n)->size; i++) {
          find_sharing(num_sharedp, pb, ARR(n)->array[i]);
        }
        break;
      default:
        break;
      }
    }
  } else {
    /* Not an sharable node, so do nothing */
    //PRINT("not T_AP\n");
    ;
  }
}

void
print_string(BFILE *f, struct bytestring bs)
{
  uint8_t *str = bs.bs_array;
  putb('"', f);
  for (size_t i = 0; i < bs.bs_size; i++) {
    int c = str[i];
#if 0
    if (c == '"' || c == '\\' || c < ' ' || c > '~') {
      putb('\\', f);
      putdecb(c, f);
      putb('&', f);
    } else {
      putb(c, f);
    }
#else
    if (c < 0 || c > 0xff)
      ERR("print_string");
    if (c < 0x20) {
      putb('^', f); putb(c + 0x20, f);
    } else if (c == '"' || c == '^' || c == '|' || c == '\\') {
      putb('\\', f); putb(c, f);
    } else if (c < 0x7f) {
      putb(c, f);
    } else if (c == 0x7f) {
      putb('\\', f); putb('?', f);
    } else if (c < 0xa0) {
      putb('^', f); putb(c - 0x80 + 0x40, f);
    } else if (c < 0xff) {
      putb('|', f); putb(c - 0x80, f);
    } else {                    /* must be< c == 0xff */
      putb('\\', f); putb('_', f);
    }
#endif
  }
  putb('"', f);
}

#define SER_ERR(s) do { pb->message = (s); longjmp(pb->ser_err, 1); } while(0)
/*
 * Recursively print an expression.
 * This assumes that the shared nodes has been marked as such.
 * The prefix flag is used to get a readable dump.
 */
void
printrec(BFILE *f, struct print_bits *pb, NODEPTR n, bool prefix)
{
  NODEPTR n1;
  int share = 0;
  enum node_tag tag;
  char prbuf[30];

  for (;;) {
    n1 = get_IND(n);
    if (is_NIL(n1))
      break;
    /*putb('*', f);*/
    n = n1;
  }

  if (test_bit(pb->shared_bits, n)) {
    /* The node is shared */
    if (test_bit(pb->marked_bits, n)) {
      /* Not yet printed, so emit a label */
      if (prefix) {
        putb(':', f);
        putdecb((value_t)LABEL(n), f);
        putb(' ', f);
      } else {
        share = 1;
      }
      clear_bit(pb->marked_bits, n);  /* mark as printed */
    } else {
      /* This node has already been printed, so just use a reference. */
      putb('_', f);
      putdecb((value_t)LABEL(n), f);
      if (!prefix)
        putb(' ', f);
      return;
    }
  }

  //if (n == atptr) putb('@', f);
  tag = get_XTAG(n);
  switch (tag) {
  case X_AP:
    if (prefix) {
      putb('(', f);
      printrec(f, pb, FUN(n), prefix);
      putb(' ', f);
      printrec(f, pb, ARG(n), prefix);
      putb(')', f);
    } else {
      printrec(f, pb, FUN(n), prefix);
      printrec(f, pb, ARG(n), prefix);
      putb('@', f);
    }
    break;
  case D_INT: putb('#', f); putdecb(GETVALUE(n), f); break;
#if WANT_INT64
  case D_INT64: putb('#', f); putb('#', f); putdecb64(GETINT64VALUE(n), f); break;
#endif  /* WANT_INT64 */
#if WANT_FLOAT64
  case D_DBL: putb('&', f); putdblb(GETDBLVALUE(n), f); break;
#endif
#if WANT_FLOAT32
  case D_FLT32: putb('&', f); putb('&', f); putdblb((double)GETFLTVALUE(n), f); break;
#endif
  case D_WEAK: SER_ERR("serialize WEAK unimplemented");
  case D_ARR:
    if (prefix) {
      /* Arrays serialize as '[sz] e_1 ... e_sz' */
      putb('[', f);
      putdecb((value_t)ARR(n)->size, f);
      putb(']', f);
      for(size_t i = 0; i < ARR(n)->size; i++) {
        putb(' ', f);
        printrec(f, pb, ARR(n)->array[i], prefix);
      }
    } else {
      /* Arrays serialize as 'e_1 ... e_sz [sz]' */
      for(size_t i = 0; i < ARR(n)->size; i++) {
        printrec(f, pb, ARR(n)->array[i], prefix);
      }
      putb('[', f);
      putdecb((value_t)ARR(n)->size, f);
      putb(']', f);
    }
    break;
  case D_MVAR:
    SER_ERR("cannot serialize MVAR");
    break;
  case D_PTR:
    if(PTR(n) == NULL) {
      if (prefix) {
        putsb("(toPtr #0)", f);
      } else {
        putsb("toPtr #0 @", f);
      }
    } else
#if WANT_STDIO
    /* The pointer can be a forptr comb_std* that has been dereferenced */
    if (PTR(n) == FORPTR(comb_stdin)->payload.bs_array) {
      mk_AP(spare_node, combFP2P, comb_stdin);
      printrec(f, pb, spare_node, prefix);
    } else if (PTR(n) == FORPTR(comb_stdout)->payload.bs_array) {
      mk_AP(spare_node, combFP2P, comb_stdout);
      printrec(f, pb, spare_node, prefix);
    } else if (PTR(n) == FORPTR(comb_stderr)->payload.bs_array) {
      mk_AP(spare_node, combFP2P, comb_stderr);
      printrec(f, pb, spare_node, prefix);
    } else
#endif  /* WANT_STDIO */
    if (prefix) {
      snprintf(prbuf, sizeof prbuf, "PTR<%p>",PTR(n));
      putsb(prbuf, f);
    } else {
      SER_ERR("Cannot serialize pointers");
    }
    break;
  case D_FUNPTR:
    /* There are a few function pointers that happen without user FFI.
     * We need to be able to serialize these.
     * XXX Make a table if we need more.
     */
    if (FUNPTR(n) == 0) {
      putsb(";0 ", f);
    } else if (FUNPTR(n) == (HsFunPtr)closeb) {
      putsb(";closeb ", f);
    } else if (prefix) {
      snprintf(prbuf, sizeof prbuf, "FUNPTR<%p>", FUNPTR(n));
      putsb(prbuf, f);
    } else {
      SER_ERR("Cannot serialize function pointers");
    }
    break;
  case D_THID:
    if (prefix) {
      snprintf(prbuf, sizeof prbuf, "THID<%d>", (int)THR(n)->mt_id);
      putsb(prbuf, f);
    } else {
      SER_ERR("cannot serialize ThreadId yet");
    }
    break;
  case D_FORPTR:
#if WANT_STDIO
    if (eq_NODEPTR(n, comb_stdin))
      putsb("IO.stdin", f);
    else if (eq_NODEPTR(n, comb_stdout))
      putsb("IO.stdout", f);
    else if (eq_NODEPTR(n, comb_stderr))
      putsb("IO.stderr", f);
    else
#endif  /* WANT_STDIO */
#if WANT_GMP || WANT_IMATH
    if (FORPTR(n)->finalizer->fptype == FP_MPZ) {
      /* Serialize as %99999" */
      mpz_ptr op = FORPTR(n)->payload.bs_array; /* get the mpz */
      int sz = mpz_sizeinbase(op, 10);        /* maximum length */
      char *s = mmalloc(sz + 2);
      (void)mpz_get_str(s, 10, op);           /* convert to a string */
      putsb("%", f);
      putsb(s, f);
      putsb("\"", f);                         /* so we can use parse_string */
      FREE(s);
    } else
#endif  /* WANT_GMP || WANT_IMATH */
    if (FORPTR(n)->finalizer->fptype == FP_BSTR) {
      struct bytestring bs = FORPTR(n)->payload;
      if (bs.bs_size > 100) {
        /* Encode large bytestrings with $len data */
        putb('$', f);
        putdecb(bs.bs_size, f);
        putb(' ', f);
        writeb(bs.bs_array, bs.bs_size, f);
      } else {
        print_string(f, bs);
      }
    } else if (prefix) {
      snprintf(prbuf, sizeof prbuf, "FORPTR<%p>",FORPTR(n));
      putsb(prbuf, f);
    } else {
      SER_ERR("Cannot serialize foreign pointers");
    }
    break;
  case D_IO_CCALL: putb('^', f); putsb(FFI_IX(GETVALUE(n)).ffi_name, f); break;
  case D_BADDYN: putb('^', f); putsb(CSTR(n), f); break;
#if WANT_TICK
  case D_TICK:
    putb('!', f);
    print_string(f, tick_table[GETVALUE(n)].tick_name);
    break;
#endif
  case V_CASED:
    {
      struct case_arity *c = get_case_arity(n);
      putb('D', f);
      for (uint32_t k = 0; k < c->m; k++) {
        putb('_', f);
        putdecb((value_t)c->arities[k], f);
      }
      break;
    }
  case V_CASES:
  case V_LOOKS:
    {
      struct case_arity *c = get_case_arity(n);
      putb(tag == V_CASES ? 'E' : 'M', f);
      for (uint32_t j = 0; j < c->m; j++) {
        putb('_', f);
        putdecb((value_t)c->tags[j], f);
        putb('_', f);
        putdecb((value_t)c->arities[j], f);
      }
      break;
    }
  case V_LOOKD:
    putb('L', f);
    putb('_', f);
    putdecb((value_t)get_CASE_size(n), f);
    break;
  case V_CONSTR:
    {
      putb('C', f);
      putb('_', f);
      int k, nn;
      get_CON_info(n, &k, &nn);
      putdecb((value_t)k, f);
      putb('_', f);
      putdecb((value_t)nn, f);
      break;
    }
  default:
    if (0 <= tag && tag <= T_LAST_TAG) {
      if (tag_names[tag]) {
        putsb(tag_names[tag], f);
      } else {
        SER_ERR("TAG1");
      }
    } else {
      SER_ERR("TAG2");
    }
    break;
  }
  if (!prefix) {
    if (tag != X_AP)
      putb(' ', f);
    if (share) {
      putb(':', f);
      putdecb((value_t)LABEL(n), f);
      putb(' ', f);
    }
  }
}

/* Serialize a graph to file. */
void
printb(BFILE *f, NODEPTR n, bool header)
{
  struct print_bits pb;
  counter_t num_shared = 0;

  pb.marked_bits = mcalloc(free_map_nwords, sizeof(bits_t));
  pb.shared_bits = mcalloc(free_map_nwords, sizeof(bits_t));
  find_sharing(&num_shared, &pb, n);
  if (header) {
    putsb(VERSION, f);
    putdecb(num_shared, f);
    putb('\n', f);
  }
  if (setjmp(pb.ser_err)) {
    /* exception */
    FREE(pb.marked_bits);
    FREE(pb.shared_bits);
    raise_rts(exn_serialize);
  } else {
    printrec(f, &pb, n, !header);
    if (header) {
      putb('}', f);
    }
    FREE(pb.marked_bits);
    FREE(pb.shared_bits);
  }
}

/* Show a graph. */
void
pps(NODEPTR n)
{
  pp(stdout, n);
}

void
pp(FILE *f, NODEPTR n)
{
  BFILE *bf = add_FILE(f);
  printb(bf, n, false);
  putb('\n', bf);
  freeb_file(bf);
}

#if 0
NODEPTR *topnode;

void
ppmsg(const char *msg, NODEPTR n)
{
  printf("%s", msg);
  pp(stdout, n);
  printf("\n");
}

void
dump(const char *msg, NODEPTR at)
{
  atptr = at;
  printf("dump: %s\n", msg);
  pp(stdout, *topnode);
}
#endif

#endif  /* WANT_STDIO */

NODEPTR
mkInt(value_t i)
{
#if INTTABLE
  if (LOW_INT <= i && i < HIGH_INT) {
    return intTable[i - LOW_INT];
  }
#endif

  NODEPTR n;
  n = alloc_node(D_INT);
  SETVALUE(n, i);
  return n;
}

#if WANT_INT64
NODEPTR
mkInt64(int64_t i)
{
  NODEPTR n;
  n = alloc_node(D_INT64);
  SETINT64VALUE(n, i);
  return n;
}
#endif  /* WANT_INT64 */

#if WANT_FLOAT32
NODEPTR
mkFlt32(flt32_t d)
{
  NODEPTR n;
  n = alloc_node(D_FLT32);
  SETFLTVALUE(n, d);
  return n;
}
#endif  /* WANT_FLOAT32 */

#if WANT_FLOAT64
NODEPTR
mkFlt64(flt64_t d)
{
  NODEPTR n;
  n = alloc_node(D_DBL);
  SETDBLVALUE(n, d);
  return n;
}
#endif  /* WANT_FLOAT64 */

NODEPTR
mkPtr(void* p)
{
  NODEPTR n;
  n = alloc_node(D_PTR);
  PTR(n) = p;
  return n;
}

NODEPTR
mkFunPtr(void (*p)(void))
{
  NODEPTR n;
  n = alloc_node(D_FUNPTR);
  FUNPTR(n) = p;
  return n;
}

struct forptr*
mkForPtr(struct bytestring bs)
{
  struct final *fin = mcalloc(1, sizeof(struct final));
  struct forptr *fp = mcalloc(1, sizeof(struct forptr));
  if (bs.bs_size == NOSIZE) {
    num_fin_alloc++;
  } else {
    num_bs_alloc++;
    num_bs_inuse += bs.bs_size;
    num_bs_bytes += bs.bs_size;
    if (num_bs_inuse > num_bs_inuse_max)
      num_bs_inuse_max = num_bs_inuse;
  }
  //printf("mkForPtr p=%p fin=%p fp=%p\n", p, fin, fp);
  fin->next = final_root;
  final_root = fin;
  fin->final = 0;
  fin->arg = bs.bs_array;
  fin->size = bs.bs_size;          /* The size is not really needed */
  fin->back = fp;
  fin->marked = 0;
  fp->next = 0;
  fp->payload = bs;
  fp->finalizer = fin;
  //  fp->desc = 0;
  return fp;
}

struct forptr*
mkForPtrP(void *p)
{
  struct bytestring bs;
  bs.bs_size = NOSIZE;
  bs.bs_capacity = 0;
  bs.bs_array = p;
  return mkForPtr(bs);
}

struct forptr*
addForPtr(struct forptr *ofp, value_t s)
{
  struct forptr *fp = mcalloc(1, sizeof(struct forptr));
  struct final *fin = ofp->finalizer;

  /* Only allowed for immutable bytestrings */
  if (ofp->payload.bs_capacity != 0) {
    ERR("addForPtr");
  }

  fp->next = fin->back;
  fin->back = fp;
  if (ofp->payload.bs_size != NOSIZE)
    fp->payload.bs_size = ofp->payload.bs_size - s;
  else
    fp->payload.bs_size = NOSIZE;
  fp->payload.bs_array = (uint8_t*)ofp->payload.bs_array + s;
  fp->finalizer = fin;
  return fp;
}

struct forptr*
bssubstr(struct forptr *fp, value_t offs, value_t len)
{
  struct forptr *res = addForPtr(fp, offs);
  res->payload.bs_size = len;
  return res;
}

/* The array might have moved, so update base pointer in the finalizer. */
void
adjforptr(struct forptr *fp)
{
  struct final *fin = fp->finalizer;
  if (fp->next || fin->back != fp) {
    /* We can only do this if fp is the only pointer to the allocated array.
       Sharing can only happen by adding to a forptr, and we don't do that
       to mutable BSTR pointers. */
    ERR("adjforptr");
  }
  fin->arg = fp->payload.bs_array;
}
    
void
bsappbyte(struct forptr *fp, uint8_t b)
{
  struct bytestring *bsp = &fp->payload;
  if (bsp->bs_size >= bsp->bs_capacity) {
    bsp->bs_capacity += bsp->bs_capacity / 2 + 2;
    bsp->bs_array = mrealloc(bsp->bs_array, bsp->bs_capacity);
    adjforptr(fp);
  }
  ((uint8_t*)bsp->bs_array)[bsp->bs_size++] = b;
}

static INLINE NODEPTR
mkNil(void)
{
  return combFalse;
}

static INLINE NODEPTR
mkCons(NODEPTR x, NODEPTR xs)
{
  return new_ap(new_ap(combCons, x), xs);
}

size_t
strNodes(size_t len)
{
  /* Each character will need a CHAR node and a CONS node, a CONS uses 2 T_AP nodes */
  len *= (1 + 2);
  /* And each string will need a NIL */
  len += 1;
  return len;
}

/* Turn a C string into a combinator string.
 * Does NOT do UTF decoding.
 */
NODEPTR
mkString(struct bytestring bs)
{
  NODEPTR n, nc;
  size_t i;
  const unsigned char *str = bs.bs_array; /* no sign bits, please */

  n = mkNil();
  for(i = bs.bs_size; i > 0; i--) {
    nc = mkInt(str[i-1]);
    n = mkCons(nc, n);
  }
  return n;
}

NODEPTR
mkStringC(char *str)
{
  return mkString(mk_ro_bytestring(strlen(str), str));
}

NODEPTR
mkStringU(struct bytestring bs)
{
  BFILE *ubuf = add_utf8(openb_rd_mem(bs.bs_array, bs.bs_size));
  NODEPTR n, *np, nc;

  //printf("mkStringU %d %s\n", (int)bs.size, (char*)bs.string);

  n = mkNil();
  np = &n;
  for(;;) {
    int c = getb(ubuf);
    if (c < 0)
      break;
    nc = mkInt(c);
    *np = mkCons(nc, *np);
    np = &ARG(*np);
  }
  closeb(ubuf);
  return n;
}

NODEPTR
bsunpack(struct bytestring bs)
{
  NODEPTR n, *np, nc;
  size_t i;

  n = mkNil();
  np = &n;
  for(i = 0; i < bs.bs_size; i++) {
    nc = mkInt(((uint8_t *)bs.bs_array)[i]);
    *np = mkCons(nc, *np);
    np = &ARG(*np);
  }
  return n;
}

/* XXX This should somehow be merged with other utf8 decoders */
/* Decode first character of a string and optionally return the rest of the string. */
/* Handles regular and modified UTF-8. */
value_t
headutf8(struct bytestring bs, void **ret)
{
  uint8_t *p = bs.bs_array;
  if (bs.bs_size == 0)
    ERR("headUTF8 0");
#if !WANT_UTF8
  if (ret)
    *ret = p + 1;
  return *p;
#else
  int c1 = *p++;
  if ((c1 & 0x80) == 0) {
    if (ret)
      *ret = p;
    return c1;
  }
  if ((c1 & 0xe0) == 0xc0 && bs.bs_size >= 2) {
    int c2 = p[0];
    if ((c2 & 0xc0) == 0x80) {
      int c = ((c1 & 0x1f) << 6) | (c2 & 0x3f);
      if (c == 0 || c >= 0x80) {
        if (ret)
          *ret = p + 1;
        return c;
      }
    }
  } else if ((c1 & 0xf0) == 0xe0 && bs.bs_size >= 3) {
    int c2 = p[0], c3 = p[1];
    if ((c2 & 0xc0) == 0x80 && (c3 & 0xc0) == 0x80) {
      int c = ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
      if (c >= 0x800 && (c < 0xd800 || c > 0xdfff)) {
        if (ret)
          *ret = p + 2;
        return c;
      }
    }
  } else if ((c1 & 0xf8) == 0xf0 && bs.bs_size >= 4) {
    int c2 = p[0], c3 = p[1], c4 = p[2];
    if ((c2 & 0xc0) == 0x80 && (c3 & 0xc0) == 0x80 && (c4 & 0xc0) == 0x80) {
      int c = ((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f);
      if (c >= 0x10000 && c < 0x110000) {
        if (ret)
          *ret = p + 3;
        return c;
      }
    }
  }
  if (ret)
    *ret = p;
  return 0xdc00 | c1;
#endif  /* WANT_UTF8 */
}

/*
 * Helpers for Data.Text, whose Text is a (modified) UTF-8 ByteString.
 * Positions are byte offsets; counts are in characters.  A character
 * starts at every byte that is not a continuation byte (10xxxxxx).
 * Without WANT_UTF8 every byte is a character.
 */
#if WANT_UTF8
#define UTF8CONT(b) (((b) & 0xc0) == 0x80)
#else
#define UTF8CONT(b) ((void)(b), 0)
#endif

/* Number of characters. */
static value_t
utf8length(struct bytestring bs)
{
  const uint8_t *p = bs.bs_array;
  value_t n = 0;
  for (size_t i = 0; i < bs.bs_size; i++)
    n += !UTF8CONT(p[i]);
  return n;
}

/* Byte offset after the first k characters from byte offset i (clamped
 * to the string). */
static value_t
utf8offsetfrom(struct bytestring bs, value_t i0, value_t k)
{
  const uint8_t *p = bs.bs_array;
  size_t n = bs.bs_size;
  size_t i = i0 < 0 ? 0 : (size_t)i0 > n ? n : (size_t)i0;
  for (; k > 0 && i < n; k--) {
    i++;
    while (i < n && UTF8CONT(p[i]))
      i++;
  }
  return i;
}

/* Byte offset where the last k characters before byte offset i start
 * (clamped to the string). */
static value_t
utf8offsetendbefore(struct bytestring bs, value_t i0, value_t k)
{
  const uint8_t *p = bs.bs_array;
  size_t n = bs.bs_size;
  size_t i = i0 < 0 ? 0 : (size_t)i0 > n ? n : (size_t)i0;
  for (; k > 0 && i > 0; k--) {
    i--;
    while (i > 0 && UTF8CONT(p[i]))
      i--;
  }
  return i;
}

/* Byte offset after the first k characters (clamped to the string). */
static value_t
utf8offset(struct bytestring bs, value_t k)
{
  return utf8offsetfrom(bs, 0, k);
}

/* Byte offset where the last k characters start (clamped to the string). */
static value_t
utf8offsetend(struct bytestring bs, value_t k)
{
  return utf8offsetendbefore(bs, bs.bs_size, k);
}

/* Decode the character starting at byte offset i (which must be < size).
 * Returns (code point << 3) | (bytes used). */
static value_t
utf8decode(struct bytestring bs, value_t i)
{
  struct bytestring rest;
  void *out;
  value_t c;
  if (i < 0 || (size_t)i >= bs.bs_size)
    ERR("utf8decode");
  rest.bs_array = (uint8_t *)bs.bs_array + i;
  rest.bs_size = bs.bs_size - i;
  rest.bs_capacity = 0;
  c = headutf8(rest, &out);
  return (c << 3) | ((uint8_t *)out - (uint8_t *)rest.bs_array);
}

/* Decode the character that ends at byte offset e (which must be > 0).
 * Returns (code point << 3) | (bytes used). */
static value_t
utf8decodeend(struct bytestring bs, value_t e)
{
  const uint8_t *p = bs.bs_array;
  value_t lo, s;
  if (e <= 0 || (size_t)e > bs.bs_size)
    ERR("utf8decodeend");
  lo = e > 4 ? e - 4 : 0;
  for (s = e - 1; s > lo && UTF8CONT(p[s]); s--)
    ;
  return (utf8decode(bs, s) & ~(value_t)7) | (e - s);
}

/* Data.Char.isSpace: the ASCII spaces and the Unicode Space_Separator (Zs)
 * characters. */
static int
utf8isspace(value_t c)
{
  return c == ' ' || (c >= '\t' && c <= '\r') ||
         c == 0xa0 || c == 0x1680 || (c >= 0x2000 && c <= 0x200a) ||
         c == 0x202f || c == 0x205f || c == 0x3000;
}

/* First byte offset >= i whose character is (if want is 0: is not) a space. */
static value_t
utf8skip(struct bytestring bs, value_t i, int want)
{
  const uint8_t *p = bs.bs_array;
  value_t n = bs.bs_size;
  if (i < 0)
    i = 0;
  while (i < n) {
    if (p[i] < 0x80) {          /* ASCII fast path */
      if (utf8isspace(p[i]) == want)
        return i;
      i++;
    } else {
      value_t r = utf8decode(bs, i);
      if (utf8isspace(r >> 3) == want)
        return i;
      i += r & 7;
    }
  }
  return n;
}

/* Byte offset after the string with trailing spaces removed. */
static value_t
utf8trimspaceend(struct bytestring bs)
{
  const uint8_t *p = bs.bs_array;
  value_t e = bs.bs_size;
  while (e > 0) {
    if (p[e-1] < 0x80) {
      if (!utf8isspace(p[e-1]))
        break;
      e--;
    } else {
      value_t r = utf8decodeend(bs, e);
      if (!utf8isspace(r >> 3))
        break;
      e -= r & 7;
    }
  }
  return e;
}

/* The characters in reverse order. */
static struct bytestring
utf8reverse(struct bytestring bs)
{
  struct bytestring r = mk_ro_bytestring(bs.bs_size, NULL);
  const uint8_t *p = bs.bs_array;
  uint8_t *q = (uint8_t *)r.bs_array + bs.bs_size;
  size_t i = 0, n = bs.bs_size;
  while (i < n) {
    size_t s = i++;
    while (i < n && UTF8CONT(p[i]))
      i++;
    q -= i - s;
    memcpy(q, p + s, i - s);
  }
  return r;
}

/* Byte offset of the first occurrence of pat in str at or after i, or -1.
 * Since UTF-8 is self-synchronizing, a match of a valid encoding always
 * starts at a character boundary. */
static value_t
bsfind(struct bytestring pat, struct bytestring str, value_t i)
{
  const uint8_t *p = pat.bs_array, *s = str.bs_array;
  size_t m = pat.bs_size, n = str.bs_size;
  if (i < 0)
    i = 0;
  if ((size_t)i > n || m > n - i)
    return -1;
  if (m == 0)
    return i;
  for (const uint8_t *q = s + i, *end = s + (n - m); q <= end; q++) {
    q = memchr(q, p[0], end - q + 1);
    if (!q)
      return -1;
    if (memcmp(q + 1, p + 1, m - 1) == 0)
      return q - s;
  }
  return -1;
}

/* Byte offset of the last occurrence of pat in str, or -1.
 * An empty pattern occurs at the end. */
static value_t
bsrfind(struct bytestring pat, struct bytestring str)
{
  const uint8_t *p = pat.bs_array, *s = str.bs_array;
  size_t m = pat.bs_size, n = str.bs_size;
  if (m > n)
    return -1;
  if (m == 0)
    return n;
  for (size_t i = n - m + 1; i-- > 0; )
    if (s[i] == p[0] && memcmp(s + i + 1, p + 1, m - 1) == 0)
      return i;
  return -1;
}

/* 1 if every byte is ASCII, else 0. */
static value_t
bsisascii(struct bytestring bs)
{
  const uint8_t *p = bs.bs_array;
  for (size_t i = 0; i < bs.bs_size; i++)
    if (p[i] >= 0x80)
      return 0;
  return 1;
}

/* ASCII lower case (upper == 0) or upper case (upper != 0). */
static struct bytestring
bsasciicase(struct bytestring bs, value_t upper)
{
  struct bytestring r = mk_ro_bytestring(bs.bs_size, NULL);
  const uint8_t *p = bs.bs_array;
  uint8_t *q = r.bs_array;
  for (size_t i = 0; i < bs.bs_size; i++) {
    uint8_t c = p[i];
    if (upper ? (c >= 'a' && c <= 'z') : (c >= 'A' && c <= 'Z'))
      c ^= 0x20;
    q[i] = c;
  }
  return r;
}

/* Evaluate to a Bool */
static INLINE value_t
evalbool(NODEPTR n)
{
  return eq_NODEPTR(evali(n), combTrue);
}

/* Evaluate to an INT */
static INLINE value_t
evalint(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_INT) {
    ERR1("evalint, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return GETVALUE(n);
}

#if WANT_INT64
/* Evaluate to an INT64 */
static INLINE int64_t
evalint64(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_INT64) {
    ERR1("evalint64, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return GETINT64VALUE(n);
}
#endif  /* WANT_INT64 */

#if WANT_FLOAT64
/* Evaluate to a flt64_t */
static INLINE flt64_t
evaldbl(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_DBL) {
    ERR1("evaldbl, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return GETDBLVALUE(n);
}
#endif  /* WANT_FLOAT64 */

#if WANT_FLOAT32
/* Evaluate to a flt32_t */
static INLINE flt32_t
evalflt(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_FLT32) {
    ERR1("evalflt, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return GETFLTVALUE(n);
}
#endif  /* WANT_FLOAT32 */

/* Evaluate to a T_PTR */
void *
evalptr(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_PTR) {
    ERR1("evalptr, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return PTR(n);
}

/* Evaluate to a T_FUNPTR */
HsFunPtr
evalfunptr(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_FUNPTR) {
    ERR1("evalfunptr, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return FUNPTR(n);
}

/* Evaluate to a T_FORPTR */
struct forptr *
evalforptr(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_FORPTR) {
    ERR1("evalforptr, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return FORPTR(n);
}

/* Evaluate to a bytestring */
struct forptr *
evalbstr(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_FORPTR || FORPTR(n)->finalizer->fptype != FP_BSTR) {
    ERR1("evalbstr, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return FORPTR(n);
}

/* Evaluate to a T_THID */
struct mthread *
evalthid(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_THID) {
    ERR1("evalthid, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return THR(n);
}

/* Evaluate to a T_MVAR */
struct mvar *
evalmvar(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_MVAR) {
    ERR1("evalmvar, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return MVAR(n);
}

/* Evaluate to a T_WEAK */
void *
evalweak(NODEPTR n)
{
  n = evali(n);
#if SANITY
  if (get_XTAG(n) != D_WEAK) {
    ERR1("evalweak, bad tag %s", TAGNAME(get_XTAG(n)));
  }
#endif
  return WEAK(n);
}

/* Evaluate a string, returns a newly allocated buffer.
 * XXX this is cheating, should use continuations.
 * XXX the malloc()ed string is leaked if we yield in here.
 * Caller is responsible to free().
 * Does modified UTF-8 encoding.
 * Only used to display an uncaught exception.
 * XXX Should remove from DYNSYM
 */
struct bytestring
evalstring(NODEPTR n)
{
  size_t sz = 100;
  char *buf = mmalloc(sz);
  size_t offs;
  uvalue_t c;
  NODEPTR x;

  for (offs = 0;;) {
    if (offs >= sz - 4) {
      sz *= 2;
      buf = mrealloc(buf, sz);
    }
    PUSH(n);                    /* protect the list from GC */
    n = evali(n);
    POP(1);
    if (eq_NODEPTR(n, combNil))
      break;
    else if (get_XTAG(n) == X_AP && get_XTAG(x = indir(&FUN(n))) == X_AP && eq_NODEPTR(indir(&FUN(x)), combCons)) { /* Cons */
      PUSH(n);                  /* protect from GC */
      c = evalint(ARG(x));
      n = POPTOP();
      if ((c & 0x1ff800) == 0xd800) {
        // c is a surrogate
        c = 0xfffd; // replacement character
      }
      if (0 < c && c < 0x80) {   /* exclude 0, since this is modified UTF-8 */
        buf[offs++] = (char)c;
      } else if (c < 0x800) {
        /* 0 encodes here, with an over-long representation */
        buf[offs++] = ((c >> 6 )       ) | 0xc0;
        buf[offs++] = ((c      ) & 0x3f) | 0x80;
      } else if (c < 0x10000) {
        buf[offs++] = ((c >> 12)       ) | 0xe0;
        buf[offs++] = ((c >> 6 ) & 0x3f) | 0x80;
        buf[offs++] = ((c      ) & 0x3f) | 0x80;
      } else if (c < 0x110000) {
        buf[offs++] = ((c >> 18)       ) | 0xf0;
        buf[offs++] = ((c >> 12) & 0x3f) | 0x80;
        buf[offs++] = ((c >> 6 ) & 0x3f) | 0x80;
        buf[offs++] = ((c      ) & 0x3f) | 0x80;
      } else {
	ERR("invalid char");
      }
      n = ARG(n);
    } else {
      ERR("evalstring not Nil/Cons");
    }
  }
  buf[offs] = 0;                /* in case we use it as a C string */
  return mk_ro_bytestring(offs, buf);
}

/*
 * Concatenate a list of ByteStrings with a single copy.
 * The spine and the elements must already be evaluated (concatBS in
 * Data.ByteString.Internal does that): evaluating here could yield to
 * another thread, which restarts the primitive from the beginning.
 * Without evaluation there is no allocation of nodes, so no GC either.
 */
static NODEPTR
bsconcat_follow(NODEPTR n)
{
  return indir(&n);
}

struct bytestring
bsconcat(NODEPTR list)
{
  struct bytestring r;
  size_t size = 0;
  uint8_t *q = 0;
  int pass;

  for (pass = 0; pass < 2; pass++) {
    NODEPTR n = list, x, e;
    if (pass == 1) {
      r = mk_ro_bytestring(size, NULL);
      q = r.bs_array;
    }
    for (;;) {
      n = bsconcat_follow(n);
      if (eq_NODEPTR(n, combNil))
        break;
      if (!(get_XTAG(n) == X_AP && get_XTAG(x = indir(&FUN(n))) == X_AP && eq_NODEPTR(indir(&FUN(x)), combCons)))
        ERR("bsconcat: list not evaluated");
      e = bsconcat_follow(ARG(x));
      if (get_XTAG(e) != D_FORPTR)
        ERR("bsconcat: element not evaluated");
      struct bytestring bs = FORPTR(e)->payload;
      if (pass == 0) {
        size += bs.bs_size;
      } else {
        memcpy(q, bs.bs_array, bs.bs_size);
        q += bs.bs_size;
      }
      n = ARG(n);
    }
  }
  return r;
}

struct bytestring
bsreplicate(size_t size, uint8_t value)
{
  struct bytestring bs = mk_ro_bytestring(size, NULL);
  memset(bs.bs_array, value, size);
  return bs;
}

struct bytestring
bsappend(struct bytestring p, struct bytestring q)
{
  struct bytestring r = mk_ro_bytestring(p.bs_size + q.bs_size, NULL);
  memcpy(r.bs_array, p.bs_array, p.bs_size);
  memcpy((uint8_t *)r.bs_array + p.bs_size, q.bs_array, q.bs_size);
  return r;
}

struct bytestring
bsappenddot(struct bytestring p, struct bytestring q)
{
  struct bytestring r = mk_ro_bytestring(p.bs_size + q.bs_size + 1, NULL);
  memcpy(r.bs_array, p.bs_array, p.bs_size);
  memcpy((uint8_t *)r.bs_array + p.bs_size, ".", 1);
  memcpy((uint8_t *)r.bs_array + p.bs_size + 1, q.bs_array, q.bs_size);
  return r;
}

/*
 * Compare bytestrings.
 */
int
bscompare(struct bytestring bsp, struct bytestring bsq)
{
  if (bsp.bs_array == bsq.bs_array && bsp.bs_size == bsq.bs_size)
    return 0;
  size_t len = bsp.bs_size < bsq.bs_size ? bsp.bs_size : bsq.bs_size;
  if (len) {
    int r = memcmp(bsp.bs_array, bsq.bs_array, len);
    if (r < 0)
      return -1;
    if (r > 0)
      return 1;
  }
  /* Got to the end of the shorter string. */
  /* The shorter string is considered smaller. */
  if (bsp.bs_size < bsq.bs_size)
    return -1;
  if (bsp.bs_size > bsq.bs_size)
    return 1;
  return 0;
}

static INLINE bool
bsequal(struct bytestring bsp, struct bytestring bsq)
{
  return bsp.bs_size == bsq.bs_size &&
    (bsp.bs_array == bsq.bs_array || bsp.bs_size == 0 ||
     memcmp(bsp.bs_array, bsq.bs_array, bsp.bs_size) == 0);
}

void
rnf_rec(bits_t *done, NODEPTR n)
{
 top:
  if (test_bit(done, n))
    return;
  set_bit(done, n);
  if (doing_rnf) {
    /* If evaluating n raises an exception or does IO, leave n unevaluated.
     * Returning a partly evaluated node instead (as before) would pass
     * a value of the wrong type to a primitive. */
    NODEPTR *stk = stack_ptr;
    struct handler *ohdl = cur_handler;
    if (setjmp(rnf_skip)) {
      stack_ptr = stk;
      while (cur_handler != ohdl) {
        struct handler *h = cur_handler;
        cur_handler = h->hdl_old;
        FREE(h);
      }
      return;
    }
  }
  n = evali(n);
  if (get_XTAG(n) == X_AP) {
    PUSH(ARG(n));               /* protect from GC */
    rnf_rec(done, FUN(n));
    n = POPTOP();
    goto top;
  }
}

void
rnf(value_t noerr, NODEPTR n)
{
  if (doing_rnf)
    ERR("recursive rnf()");
  /* Mark visited nodes to avoid getting stuck in loops. */
  struct rnf_state rs;
  rs.rs_done = mcalloc(free_map_nwords, sizeof(bits_t));
  rs.rs_hdl = cur_handler;
  rs.rs_old = cur_rnf;
  cur_rnf = &rs;
  doing_rnf = (bool)noerr;
  rnf_rec(rs.rs_done, n);
  doing_rnf = false;
  cur_rnf = rs.rs_old;
  FREE(rs.rs_done);
}

#if EXTRASANITY                 /* the only user so far; extracting code for a JIT will need them too */
/* The number of arguments the virtual node h (the head of a spine: a
 * combinator, primitive, case, or constructor) needs before it reduces, or -1
 * if it never reduces (a constructor).  For the combinators that also reduce
 * with fewer arguments (e.g., B' x y = B (x y)) it is the smaller number. */
static INLINE int
arity_virt(NODEPTR h)
{
  enum node_tag t = get_VIRT_TAG(h);
  switch (t) {
  case V_CONSTR:
  case V_LOOKD:
  case V_LOOKS:
    return -1;
  case V_CASED:
    return get_case_arity(h)->m + 1;
  case V_CASES:
    return get_case_arity(h)->m + 2;
  default:
    return tag_arity[t];
  }
}

/* Is n in weak head normal form, i.e., would evaluating it do no reduction?
 * It is if it is a value (an Int, a ByteString, ...), a constructor applied to
 * at most its fields, or a partial application.  So count the arguments down
 * the spine, and compare with the arity of the head. */
static INLINE bool
is_whnf(NODEPTR n)
{
  int d = 0;                    /* arguments so far */
  for (;;) {
    if (is_VIRT(n)) {
      int a = arity_virt(n);
      return a < 0 || d < a;
    }
    NODEPTR f = FUN(n);
    if (is_AP(f)) {             /* an application */
      d++;
      n = f;
      continue;
    }
    switch (get_DATA_TAG(f)) {
    case D_IND:      n = ARG(n); continue;
    case D_IO_CCALL: return d < FFI_IX(GETVALUE(n)).ffi_arity + 1;
    case D_TICK:     return d < 1;
    case D_BADDYN:   return false; /* evaluating it is an error */
    default:         return true;  /* a value */
    }
  }
}

/* A case checks for as many arguments as the arity in primops[]; the
 * combinators that also reduce with fewer arguments check for more in their
 * other branch.  The arity of the other tags is in the node, see arity_virt. */
static void
check_arity(enum node_tag tag, int k)
{
  if (tag <= T_FIRST_REAL_TAG || tag >= T_LAST_TAG || k == tag_arity[tag])
    return;
  switch (tag) {
  case T_BB: case T_Z: case T_R: case T_K2: case T_K3: case T_K4: case T_CCB:
    if (k > tag_arity[tag])
      return;
    break;
  default:
    break;
  }
#if WANT_STDIO
  fprintf(stderr, "arity of %s: CHECK(%d), but primops[] says %d\n", TAGNAME(tag), k, (int)tag_arity[tag]);
#endif
  ERR("check_arity");
}
#endif  /* EXTRASANITY */

/*
 * The interpreter is split in three functions:
 *  evali       the core: unwinding, combinators, case and constructors, the IO
 *              monad combinators, seq, and Int arithmetic.  It handles most
 *              reductions.
 *  evalprimop  every other primitive; evali calls it for a tag it does not handle.
 *  evalprimret the continuations of the strict primitives that are not on Int,
 *              i.e., what to do when an argument has been evaluated and there is
 *              a BININT64_1 (etc) marker on the stack.
 * evalprimop and evalprimret return the node to continue with, and whether
 * evali should evaluate it (PRIM_TOP, i.e., goto top) or return it as a value
 * (PRIM_RET, i.e., goto ret).
 */
enum prim_next { PRIM_TOP, PRIM_RET };
struct prim_res { NODEPTR n; enum prim_next next; };

/* Macros shared by evali, evalprimop, and evalprimret.  RET, GOTOP, CHECK,
 * and the GOxxx macros are defined separately for evali and for the other two. */
#define HASNARGS(k) (STACK_ABOVE(stk) >= (k))
/* Check that there are at least k arguments, return if not. */
#if EXTRASANITY
#define CHECK(k) do { check_arity(tag, (k)); if (!HASNARGS(k)) RET; } while(0)
#else
#define CHECK(k) do { if (!HASNARGS(k)) RET; } while(0)
#endif
#define SETIND(n, x) set_IND(n, x)
#define GOBOOL(b) do { GOIND(b ? combTrue : combFalse); } while(0)
/* CHKARGN checks that there are at least N arguments.
 * It also
 *  - sets n to the "top" node
 *  - set x, y, ... to the arguments
 *  - pops N stack elements
 * NOTE: No GC is allowed after these, since the stack has been popped.
 */
#define POPARG2 do { POP(2); n = TOP(-1); y = ARG(n); x = ARG(TOP(-2)); } while(0)
#define POPARG3 do { POP(3); n = TOP(-1); z = ARG(n); y = ARG(TOP(-2)); x = ARG(TOP(-3)); } while(0)
#define POPARG4 do { POP(4); n = TOP(-1); w = ARG(n); z = ARG(TOP(-2)); y = ARG(TOP(-3)); x = ARG(TOP(-4)); } while(0)
#define CHKARG0 do { } while(0)
#define CHKARG1 do { CHECK(1); POP(1); n = TOP(-1); x = ARG(n); } while(0)
#define CHKARG2 do { CHECK(2); POPARG2; } while(0)
#define CHKARG3 do { CHECK(3); POPARG3; } while(0)
#define CHKARG4 do { CHECK(4); POPARG4; } while(0)
#define CHKARG5 do { CHECK(5); POP(5); n = TOP(-1); /*v = ARG(n);*/ w = ARG(TOP(-2)); z = ARG(TOP(-3)); y = ARG(TOP(-4)); x = ARG(TOP(-5)); } while(0)
/* Read argument i (0-based) from the unpopped stack, and pop k entries setting n.
 * Use these instead of pre-loading arguments into locals across eval*() or GCCHECK(),
 * because GC may bypass and free a D_IND cell referenced by a stale local NODEPTR. */
#define STKARG(i) ARG(TOP(i))
#define POPN(k)   do { POP(k); n = TOP(-1); } while(0)

/* Alloc a possible GC action, e, between setting x and popping */
#define CHKARGEV1(e)   do { CHECK(1); x = ARG(TOP(0)); e; POP(1); n = TOP(-1); } while(0)

/* Overwrite the (real) node n with a data node */
#define SETINT(n,r)    do { set_DATA_TAG((n), D_INT); SETVALUE((n), (r)); } while(0)
#define SETINT64(n,r)  do { set_DATA_TAG((n), D_INT64); SETINT64VALUE((n), (r)); } while(0)
#define SETDBL(n,d)    do { set_DATA_TAG((n), D_DBL); SETDBLVALUE((n), (d)); } while(0)
#define SETFLT(n,d)    do { set_DATA_TAG((n), D_FLT32); SETFLTVALUE((n), (d)); } while(0)
#define SETPTR(n,r)    do { set_DATA_TAG((n), D_PTR); PTR(n) = (r); } while(0)
#define SETFUNPTR(n,r) do { set_DATA_TAG((n), D_FUNPTR); FUNPTR(n) = (r); } while(0)
#define SETFORPTR(n,r) do { set_DATA_TAG((n), D_FORPTR); FORPTR(n) = (r); } while(0)
#define SETBSTR(n,r)   do { set_DATA_TAG((n), D_FORPTR); FORPTR(n) = (r); FORPTR(n)->finalizer->fptype = FP_BSTR; } while(0)
#define OPINT1(e)      do { CHECK(1); xi = evalint(ARG(TOP(0)));                            e; POP(1); n = TOP(-1); } while(0);
#define OPPTR2(e)      do { CHECK(2); xp = evalptr(ARG(TOP(0))); yp = evalptr(ARG(TOP(1))); e; POP(2); n = TOP(-1); } while(0);
#define CMPP(op)       do { OPPTR2(r = xp op yp); GOIND(r ? combTrue : combFalse); } while(0)


/* Control macros for evalprimop and evalprimret */
#define RET do { return (struct prim_res){ n, PRIM_RET }; } while(0)
#define GOTOP(x) do { return (struct prim_res){ (x), PRIM_TOP }; } while(0)
#define GOIND(x) do { NODEPTR _x = (x); SETIND(n, _x); GOTOP(_x); } while(0)
#define GOAP(f,a) do { FUN(n) = (f); ARG(n) = (a); GOTOP(n); } while(0)
#define GOAP2(f,a,b) do { FUN(n) = new_ap((f), (a)); ARG(n) = (b); GOTOP(n); } while(0)
#define GOPAIR(a) do { FUN(n) = new_ap(combP, (a)); GOTOP(n); } while(0)
#define GOPAIRUNIT do { FUN(n) = combPUnit; GOTOP(n); } while(0)

/* Reduce the primitive tag, which is at the head of the spine (n).
 * stk is the stack base of the calling evali. */
static NOINLINE struct prim_res
evalprimop(NODEPTR n, enum node_tag tag, NODEPTR *stk)
{
  NODEPTR x, y, z, w;
  value_t xi, yi;
  struct forptr *xfp;
  struct ioarray *arr;
#if WANT_STDIO
  void *bfile;
  int hdr;
#endif  /* WANT_STDIO */

  switch (tag) {

#if WANT_FLOAT32
  case T_FADD:
  case T_FSUB:
  case T_FMUL:
  case T_FDIV:
  case T_FEQ:
  case T_FNE:
  case T_FLT:
  case T_FLE:
  case T_FGT:
  case T_FGE:
    CHECK(2);
    n = ARG(TOP(1));
    PUSH(combBINFLT2);
    GOTOP(n);
  case T_FNEG:
    CHECK(1);
    n = ARG(TOP(0));
    PUSH(combUNFLT1);
    GOTOP(n);

  case T_I64TOF:
    {
#if WANT_INT64
    CHECK(1);
    flt32_t rf = (flt32_t)evalint64(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETFLT(n, rf);
    RET;
#else
    ERR("No Int64");
#endif
    }

  case T_ITOF:
    {
    CHECK(1);
    flt32_t rf = (flt32_t)evalint(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETFLT(n, rf);
    RET;
    }

  case T_UTOF:
    {
    CHECK(1);
    flt32_t rf = (flt32_t)(uvalue_t)evalint(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETFLT(n, rf);
    RET;
    }

  case T_FTOI:
    {
    CHECK(1);
    value_t i = (value_t)evalflt(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, i);
    RET;
    }

#endif  /* WANT_FLOAT32 */

#if WANT_FLOAT64 && WANT_FLOAT32
  case T_DTOF:
    {
    CHECK(1);
    float xf = (float)evaldbl(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETFLT(n, xf);
    RET;
    }
  case T_FTOD:
    {
    CHECK(1);
    double xd = (double)evalflt(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETDBL(n, xd);
    RET;
    }
#endif  /* WANT_FLOAT64 && WANT_FLOAT32 */

#if WANT_FLOAT64
  case T_DADD:
  case T_DSUB:
  case T_DMUL:
  case T_DDIV:
  case T_DEQ:
  case T_DNE:
  case T_DLT:
  case T_DLE:
  case T_DGT:
  case T_DGE:
    CHECK(2);
    n = ARG(TOP(1));
    PUSH(combBINDBL2);
    GOTOP(n);
  case T_DNEG:
    CHECK(1);
    n = ARG(TOP(0));
    PUSH(combUNDBL1);
    GOTOP(n);

#if WANT_INT64
  case T_I64TOD:
    {
    CHECK(1);
    flt64_t rd = (flt64_t)evalint64(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETDBL(n, rd);
    RET;
    }
#endif

  case T_ITOD:
    {
    CHECK(1);
    flt64_t rd = (flt64_t)evalint(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETDBL(n, rd);
    RET;
    }

  case T_UTOD:
    {
    CHECK(1);
    flt64_t rd = (flt64_t)(uvalue_t)evalint(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETDBL(n, rd);
    RET;
    }

  case T_DTOI:
    {
    CHECK(1);
    value_t i = (value_t)evaldbl(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, i);
    RET;
    }

#endif  /* WANT_FLOAT64 */

  case T_ISINT:
    CHECK(1);
    x = evali(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, get_XTAG(x) == D_INT ? GETVALUE(x) : -1);
    RET;

  case T_BSAPPEND:
  case T_BSAPPENDDOT:
  case T_BSEQ:
  case T_BSNE:
  case T_BSLT:
  case T_BSLE:
  case T_BSGT:
  case T_BSGE:
  case T_BSCMP:
    CHECK(2);
    n = ARG(TOP(1));
    PUSH(combBINBS2);
    GOTOP(n);

#if WANT_INT64
  case T_ADD64:
  case T_SUB64:
  case T_MUL64:
  case T_QUOT64:
  case T_REM64:
  case T_SUBR64:
  case T_UADD64:
  case T_USUB64:
  case T_UMUL64:
  case T_UQUOT64:
  case T_UREM64:
  case T_USUBR64:
  case T_AND64:
  case T_OR64:
  case T_XOR64:
  case T_SHL64:
  case T_SHR64:
  case T_ASHR64:
  case T_EQ64:
  case T_NE64:
  case T_LT64:
  case T_LE64:
  case T_GT64:
  case T_GE64:
  case T_ICMP64:
  case T_ULT64:
  case T_ULE64:
  case T_UGT64:
  case T_UGE64:
  case T_UCMP64:
    CHECK(2);
    /*
    fprintf(stderr, "bin64 op=%s\n", TAGNAME(tag)); fflush(stderr);
    { NODEPTR x = evali(ARG(TOP(1)));
      fprintf(stderr, "x.tag=%s x.val=%lld\n", TAGNAME(get_XTAG(x)), GETINT64VALUE(x)); fflush(stderr);
      NODEPTR y = evali(ARG(TOP(0)));
      fprintf(stderr, "y.tag=%s y.val=%ld\n", TAGNAME(get_XTAG(y)), GETVALUE(y)); fflush(stderr);
    }
    */
    n = ARG(TOP(1));
    if (get_XTAG(n) == D_INT64) {
      //fprintf(stderr, "push combBININT64_1\n"); fflush(stderr);
      n = ARG(TOP(0));
      PUSH(combBININT64_1);
      if (get_XTAG(n) == D_INT64) {
        /* Both arguments are evaluated: the BININT64_1 continuation in
         * evalprimret does the operation. */
        RET;
      }
    } else {
      //fprintf(stderr, "push combBININT64_2\n"); fflush(stderr);
      PUSH(combBININT64_2);
    }
    GOTOP(n);
  case T_NEG64:
  case T_UNEG64:
  case T_INV64:
  case T_POPCOUNT64:
  case T_CLZ64:
  case T_CTZ64:
    CHECK(1);
    n = ARG(TOP(0));
    PUSH(combUNINT64_1);
    GOTOP(n);
#endif  /* WANT_INT64 */


    /* Convert between different types. */
#define CONV(t, set, get) do { CHECK(1); x = evali(ARG(TOP(0))); n = POPTOP(); set_DATA_TAG(n, t); set(n, get(x)); RET; } while(0)
#if WANT_INT64
  case T_TODBL:    CONV(D_DBL,    SETINT64VALUE, GETINT64VALUE); /* raw int64_t -> double */
  case T_FROMDBL:  CONV(D_INT64,  SETINT64VALUE, GETINT64VALUE); /* raw double -> int64_t */
  case T_ITOI64:   CONV(D_INT64,  SETINT64VALUE, GETVALUE);
  case T_UTOU64:   CONV(D_INT64,  SETINT64VALUE, (uint64_t)(uvalue_t)GETVALUE);
  case T_I64TOI:   CONV(D_INT,    SETVALUE,      GETINT64VALUE);
  case T_U64TOU:   CONV(D_INT,    SETVALUE,      GETINT64VALUE);
#endif  /* WANT_INT64 */
#if WANT_FLOAT32
  case T_TOFLT:    CONV(D_FLT32,  SETINT32VALUE, (uint32_t)GETVALUE);
  case T_FROMFLT:  CONV(D_INT,    SETVALUE,      GETINT32VALUE);
#endif  /* WANT_FLOAT32 */
  case T_TOINT:    CONV(D_INT,    SETVALUE,      GETVALUE);
  case T_TOPTR:    CONV(D_PTR,    SETVALUE,      GETVALUE);
  case T_TOFUNPTR: CONV(D_FUNPTR, SETVALUE,      GETVALUE);
#undef CONV

  case T_FPADD: CHECK(2); xfp = evalforptr(ARG(TOP(0))); yi = evalint(ARG(TOP(1))); POP(2); n = TOP(-1); SETFORPTR(n, addForPtr(xfp, yi)); RET;
  case T_FP2P:
    CHECK(1);
    xfp = evalforptr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETPTR(n, xfp->payload.bs_array);
    RET;

  case T_FP2BS:
    CHECK(2);
    xfp = evalforptr(ARG(TOP(0)));
    xi = evalint(ARG(TOP(1)));
    POP(2);
    n = TOP(-1);
    xfp->payload.bs_size = xi;
    SETBSTR(n, xfp);
    RET;

  case T_BS2FP:
    CHECK(1);
    xfp = evalbstr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETFORPTR(n, xfp);
    RET;

  case T_ARR_EQ:
    {
      CHECK(2);
      x = evali(ARG(TOP(0)));
      arr = ARR(x);
      y = evali(ARG(TOP(1)));
      POP(2);
      n = TOP(-1);
      GOBOOL(arr == ARR(y));
    }

  case T_BSHEADUTF8:
    CHECK(1);
    xfp = evalbstr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, headutf8(xfp->payload, (void**)0));
    RET;

  case T_BSTAILUTF8:
    CHECK(1);
    xfp = evalbstr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    { void *out;
      (void)headutf8(xfp->payload, &out);             /* skip one UTF8 character */
      xi = (char*)out - (char*)xfp->payload.bs_array; /* offset */
      yi = xfp->payload.bs_size - xi;                 /* remaining length */
      SETBSTR(n, bssubstr(xfp, xi, yi));              /* make a substring */
    }
    RET;

    /* Data.Text helpers on one ByteString */
  case T_UTF8LENGTH:
  case T_UTF8TRIMSPACEEND:
  case T_UTF8REVERSE:
  case T_BSISASCII:
    CHECK(1);
    xfp = evalbstr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    switch (tag) {
    case T_UTF8LENGTH:       SETINT(n, utf8length(xfp->payload)); break;
    case T_UTF8TRIMSPACEEND: SETINT(n, utf8trimspaceend(xfp->payload)); break;
    case T_BSISASCII:        SETINT(n, bsisascii(xfp->payload)); break;
    default:                 SETBSTR(n, mkForPtrFree(utf8reverse(xfp->payload))); break;
    }
    RET;

    /* Data.Text helpers on a ByteString and an Int */
  case T_UTF8OFFSET:
  case T_UTF8OFFSETEND:
  case T_UTF8DECODE:
  case T_UTF8DECODEEND:
  case T_UTF8SKIPSPACE:
  case T_UTF8SKIPNONSPACE:
  case T_BSASCIICASE:
    CHECK(2);
    xfp = evalbstr(ARG(TOP(0)));
    xi = evalint(ARG(TOP(1)));
    POP(2);
    n = TOP(-1);
    switch (tag) {
    case T_UTF8OFFSET:       SETINT(n, utf8offset(xfp->payload, xi)); break;
    case T_UTF8OFFSETEND:    SETINT(n, utf8offsetend(xfp->payload, xi)); break;
    case T_UTF8DECODE:       SETINT(n, utf8decode(xfp->payload, xi)); break;
    case T_UTF8DECODEEND:    SETINT(n, utf8decodeend(xfp->payload, xi)); break;
    case T_UTF8SKIPSPACE:    SETINT(n, utf8skip(xfp->payload, xi, 0)); break;
    case T_UTF8SKIPNONSPACE: SETINT(n, utf8skip(xfp->payload, xi, 1)); break;
    default:                 SETBSTR(n, mkForPtrFree(bsasciicase(xfp->payload, xi))); break;
    }
    RET;

  case T_UTF8OFFSETFROM:
  case T_UTF8OFFSETENDBEFORE:
    CHECK(3);
    {
      value_t xk;
      xfp = evalbstr(ARG(TOP(0)));
      xi = evalint(ARG(TOP(1)));
      xk = evalint(ARG(TOP(2)));
      POP(3);
      n = TOP(-1);
      if (tag == T_UTF8OFFSETFROM)
        SETINT(n, utf8offsetfrom(xfp->payload, xi, xk));
      else
        SETINT(n, utf8offsetendbefore(xfp->payload, xi, xk));
    }
    RET;

  case T_BSFIND:
    CHECK(3);
    {
      struct forptr *pfp = evalbstr(ARG(TOP(0)));
      struct forptr *sfp = evalbstr(ARG(TOP(1)));
      xi = evalint(ARG(TOP(2)));
      POP(3);
      n = TOP(-1);
      SETINT(n, bsfind(pfp->payload, sfp->payload, xi));
    }
    RET;

  case T_BSCONCAT:
    CHECK(1);
    {
      struct bytestring rb = bsconcat(ARG(TOP(0)));   /* evaluates nothing */
      POP(1);
      n = TOP(-1);
      SETBSTR(n, mkForPtrFree(rb));
    }
    RET;

  case T_BSRFIND:
    CHECK(2);
    {
      struct forptr *pfp = evalbstr(ARG(TOP(0)));
      struct forptr *sfp = evalbstr(ARG(TOP(1)));
      POP(2);
      n = TOP(-1);
      SETINT(n, bsrfind(pfp->payload, sfp->payload));
    }
    RET;

  case T_BSFROMUTF8:
    if (doing_rnf) longjmp(rnf_skip, 1);
    CHECK(1);

    xfp = evalbstr(ARG(TOP(0)));
    GCCHECK(strNodes(xfp->payload.bs_size));
    POP(1);
    n = TOP(-1);
    //printf("T_FROMUTF8 x = %p fp=%p payload.string=%p\n", x, x->uarg.uuforptr, x->uarg.uuforptr->payload.string);
    GOIND(mkStringU(xfp->payload));

  case T_BSUNPACK:
    if (doing_rnf) longjmp(rnf_skip, 1);
    CHECK(1);
    struct forptr *xfp = evalbstr(ARG(TOP(0)));
    GCCHECK(strNodes(xfp->payload.bs_size));
    POP(1);
    n = TOP(-1);
    GOIND(bsunpack(xfp->payload));

  case T_BSREPLICATE:
    CHECK(2);
    xi = evalint(ARG(TOP(0)));
    yi = evalint(ARG(TOP(1)));
    POP(2);
    n = TOP(-1);
    SETBSTR(n, mkForPtrFree(bsreplicate(xi, yi)));
    RET;

  case T_BSLENGTH:
    CHECK(1);
    xfp = evalbstr(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, xfp->payload.bs_size);
    RET;

  case T_BSSUBSTR:
    CHECK(3);
    xfp = evalbstr(ARG(TOP(0)));
    xi = evalint(ARG(TOP(1)));
    yi = evalint(ARG(TOP(2)));
    POP(3);
    n = TOP(-1);
    SETBSTR(n, bssubstr(xfp, xi, yi));
    RET;

  case T_BSINDEX:
    CHECK(2);
    xfp = evalbstr(ARG(TOP(0)));
    xi = evalint(ARG(TOP(1)));
    POP(2);
    n = TOP(-1);
    SETINT(n, ((uint8_t *)xfp->payload.bs_array)[xi]);
    RET;

  case T_BSNEW:
    {
    CHECK(3);
    struct bytestring bs;
    value_t sz = evalint(STKARG(0));
    value_t cap = evalint(STKARG(1));
    if (sz < 0 || cap < sz)
      ERR("BSNEW");
    bs.bs_size = (size_t)sz;
    bs.bs_capacity = (size_t)cap;
    GCCHECK(2);                 /* PAIR + StrNode */
    bs.bs_array = mmalloc(bs.bs_capacity);
    memset(bs.bs_array, 0, bs.bs_size);
    POPN(3);
    GOPAIR(mkStrNode(bs));
    }

  case T_BSFREEZE:
    {
    CHECK(2);
    xfp = evalbstr(STKARG(0));
    GCCHECK(1);                 /* PAIR */
    struct bytestring *bsp = &xfp->payload;
    if (bsp->bs_size != bsp->bs_capacity) {
      bsp->bs_array = mrealloc(bsp->bs_array, bsp->bs_size);
      adjforptr(xfp);           /* in case bs_array moved */
    }
    bsp->bs_capacity = 0;        /* mark as immutable */
    x = STKARG(0);
    POPN(2);
    GOPAIR(x);
    }

  case T_BSAPPBYTE:
    CHECK(3);
    xfp = evalbstr(STKARG(0));
    xi = evalint(STKARG(1));
    bsappbyte(xfp, xi);
    POPN(3);
    GOPAIRUNIT;
    
  case T_BSAPPCHAR:
    {
    CHECK(3);
    xfp = evalbstr(STKARG(0));
    uvalue_t c = (uvalue_t)evalint(STKARG(1));
    if (0xdc80 <= c && c <= 0xdcff) {
      /* Using dc80-dcff to roundtrip bad bytes */
      bsappbyte(xfp, c & 0xff);
    } else {
      if (0xd800 <= c && c <= 0xdfff) {
        /* c is a surrogate */
        c = 0xfffd; /* replacement character */
      }
      if (0 < c && c < 0x80) {   /* exclude 0, since this is modified UTF-8 */
        bsappbyte(xfp, c);
      } else if (c < 0x800) {
        /* 0 encodes here, with an over-long representation */
        bsappbyte(xfp, ((c >> 6 )       ) | 0xc0);
        bsappbyte(xfp, ((c      ) & 0x3f) | 0x80);
      } else if (c < 0x10000) {
        bsappbyte(xfp, ((c >> 12)       ) | 0xe0);
        bsappbyte(xfp, ((c >> 6 ) & 0x3f) | 0x80);
        bsappbyte(xfp, ((c      ) & 0x3f) | 0x80);
      } else if (c < 0x110000) {
        bsappbyte(xfp, ((c >> 18)       ) | 0xf0);
        bsappbyte(xfp, ((c >> 12) & 0x3f) | 0x80);
        bsappbyte(xfp, ((c >> 6 ) & 0x3f) | 0x80);
        bsappbyte(xfp, ((c      ) & 0x3f) | 0x80);
      } else {
        ERR("invalid char");
      }
    }
    POPN(3);
    GOPAIRUNIT;
    }

  case T_BSREAD:
    CHECK(3);
    xfp = evalbstr(STKARG(0));
    xi = evalint(STKARG(1));
    GCCHECK(2);                 /* PAIR + Int */
    POPN(3);
    yi = ((uint8_t *)xfp->payload.bs_array)[xi];
    GOPAIR(mkInt(yi));

  case T_BSWRITE:
    CHECK(4);
    xfp = evalbstr(STKARG(0));
    xi = evalint(STKARG(1));
    yi = evalint(STKARG(2));
    POPN(4);
    ((uint8_t *)xfp->payload.bs_array)[xi] = (uint8_t)yi;
    GOPAIRUNIT;

  case T_RAISE:
    CHKARG1;
    raise_exn(x);               /* never returns */

  case T_SPNEW:
    GCCHECK(2);                 /* PAIR + Int */
    CHKARG2;
    xi = new_stableptr(x);
    GOPAIR(mkInt(xi));
  case T_SPDEREF:
    CHECK(2);
    xi = evalint(STKARG(0));
    GCCHECK(1);                 /* PAIR */
    POPN(2);
    GOPAIR(deref_stableptr(xi));
  case T_SPFREE:
    CHECK(2);
    xi = evalint(STKARG(0));
    free_stableptr(xi);
    POPN(2);
    GOPAIRUNIT;

  case T_WKNEW:
    GCCHECK(2);                 /* PAIR + weak */
    CHKARG3;
    GOPAIR(new_weak_ptr(x, y, NIL_NODEPTR));
  case T_WKNEWFIN:
    GCCHECK(3);                 /* PAIR + weak + finalizer */
    CHKARG4;
    GOPAIR(new_weak_ptr(x, y, z));
  case T_WKDEREF:
    CHECK(2);
    {
      struct weak_ptr *wp = evalweak(STKARG(0));
      GCCHECK(2);               /* Just + PAIR */
      x = deref_weak_ptr(wp);
      POPN(2);
      GOPAIR(x);
    }
  case T_WKFINAL:
    CHECK(2);
    finalize_weak_ptr(evalweak(STKARG(0)));
    POPN(2);
    GOPAIRUNIT;


  case T_RNF:
    if (doing_rnf) longjmp(rnf_skip, 1);
    CHECK(2);
    xi = evalint(STKARG(0));
    rnf(xi, STKARG(1)); POPN(2); GOIND(combUnit);

  case T_IO_PERFORMIO:
    GCCHECK(2);
    if (doing_rnf) longjmp(rnf_skip, 1);
    CHKARG1;
    /* Conjure up a new world and evaluate the io with that world, finally selecting the result */
    /* PERFORMIO io  -->  io World K */
#if 1
    GOAP2(x, combWorld, combK);
#else
    {
      /* Don't count performio reductions. */
      /* Useful when Debug.Trace.trace should have zero cost */
      NODEPTR p1 = new_ap(x, combWorld);
      NODEPTR p2 = new_ap(p1, combK);
      counter_t s = glob_slice;
      glob_slice = 1000000000;
      NODEPTR p3 = evali(p2);
      glob_slice = s;
      GOIND(p3);
    }
#endif

  case T_IO_ATOMIC:
    /* Perform an IO action "atomically".
     * It's not really atomic, but 10 uninterrupted slices.
     * This is good enough for atomicModifyIORef. */
    {
      CHECK(2);                      /* arg0=action, arg1=world */
      GCCHECK(2);
      NODEPTR p1 = new_ap(STKARG(0), STKARG(1));
      NODEPTR p2 = new_ap(p1, combK);
      glob_slice += slice * 10;      /* give the current thread 10 extra slices
                                      * (Note: not undone if evali raises a caught exception) */
      NODEPTR p3 = evali(p2);        /* this evaluation is guaranteed at least 10 uninterrupted slices */
      glob_slice -= slice * 10;      /* and take them away again */
      GCCHECKSAVE(p3, 2);
      POPN(2);
      GOPAIR(p3);
    }
    break;
  case T_IO_LAZYBINDST:
    /* Lazy bind, used for the lazy ST monad.
     * DO NOT USE FOR IO, because effects are not guaranteed to happen.
     *   (x `lazyBind` y) z = let w = x z in y (fst w) (snd w)
     */
    GCCHECK(4);
    CHKARG3;
    w = new_ap(x, z);
    GOAP2(y, new_ap(combPFst, w), new_ap(combPSnd, w));
  case T_IO_LAZYBIND:
    /* Lazy bind, used for unsafeInterleaveIO.
     *   (x `lazyBind` y) z = let w = x z in y (fst w) (snd w)
     * since the actual state token doesn't matter we use the more efficient
     *   (x `lazyBind` y) z = let w = x z in y (fst w) z
     */
    GCCHECK(4);
    CHKARG3;
    w = new_ap(x, z);
    GOAP2(y, new_ap(combPFst, w), z);
  case T_IO_STRICT:
    /* Force the world argument before executing the IO.
     * IO.strict io World = seq World (io World)
     */
    CHECK(2);
    (void)evali(STKARG(1));     /* evaluate the world */
    x = STKARG(0);
    y = STKARG(1);
    POPN(2);
    GOAP(x, y);                 /* and run IO computation */
#if WANT_STDIO
  case T_IO_PP:
    CHKARG2;
    pp(stderr, x);
    GOPAIRUNIT;

  case T_NODEADDR:
    /* The NODEPTR of the argument, which is not evaluated, as an Int. */
    CHECK(1);
    x = ARG(TOP(0));
    n = POPTOP();
    SETINT(n, (value_t)x.node_bits);
    RET;

  case T_IO_RTSINFO:
    GCCHECK(2);
    CHKARG1;                    /* x = ST */
    GOPAIR(mkPtr(&rts_info));
  case T_IO_PRINT:
    hdr = false;
    goto ser;
  case T_IO_SERIALIZE:
    hdr = true;
  ser:
#if 0
    gc();                     /* DUBIOUS: do a GC to get possible GC reductions */
#endif
    CHECK(3);
    bfile = (struct BFILE*)evalptr(STKARG(0));
    printb(bfile, evali(STKARG(1)), hdr);
    putb('\n', bfile);
    POPN(3);
    GOPAIRUNIT;
  case T_IO_DESERIALIZE:
    CHECK(2);
    bfile = (struct BFILE*)evalptr(STKARG(0));
    x = parse_top(bfile, 0);
    if (is_NIL(x))
      raise_rts(exn_deserialize);
    GCCHECKSAVE(x, 1);        /* PAIR */
    POPN(2);
    GOPAIR(x);
#endif
#if WANT_ARGS
  case T_IO_GETARGREF:
    GCCHECK(2);
    CHKARG1;
    x = alloc_node(D_ARR);
    ARR(x) = argarray;
    GOPAIR(x);
#endif
  case D_IO_CCALL:
    {
      int a = (int)GETVALUE(n);   /* function number */
      //printf("  %s\n", FFI_IX(a).ffi_name);
      int arity = FFI_IX(a).ffi_arity;
      CHECK(arity + 1);           /* arity C arguments plus the World token */
      if (doing_rnf) longjmp(rnf_skip, 1);   /* do not call C from rnf() without errors */
      GCCHECK(1);                 /* room for placeholder */
      funptr_t f = FFI_IX(a).ffi_fun;
      PUSH(mkPtr(0));             /* placeholder for result, protected from GC */
      int k = f((int)STACK_INDEX(stk)); /* call FFI function, return number of arguments */
      if (k != arity) {
#if WANT_STDIO
        fprintf(stderr, "ccall arity %s %d!=%d\n", FFI_IX(a).ffi_name, arity, k);
#endif
        ERR("ccall arity");     /* temporary sanity check */
      }
      GCCHECK(1);                 /* room for pair */
      x = POPTOP();               /* pop actual result */
      POP(arity);                 /* pop the pushed arguments */
      if (stack_ptr >= stack_bot)
        ERR("CCALL POP");
      n = POPTOP();               /* node to update */
      GOPAIR(x);                  /* and this is the result */
    }

  case T_PACKCSTRING:
    {
      CHECK(2);
      char *cstr = evalptr(STKARG(0));
      GCCHECK(2);                 /* StrNode + PAIR */
      struct bytestring bs = mk_ro_bytestring(strlen(cstr), NULL);
      memcpy(bs.bs_array, cstr, bs.bs_size);
      NODEPTR res = mkStrNode(bs);
      POPN(2);
      GOPAIR(res);
    }
  case T_PACKCSTRINGLEN:
    {
      CHECK(3);
      char *cstr = evalptr(STKARG(0));
      size_t len = evalint(STKARG(1));
      GCCHECK(2);                 /* StrNode + PAIR */
      struct bytestring bs = mk_ro_bytestring(len, NULL);
      memcpy(bs.bs_array, cstr, bs.bs_size);
      NODEPTR res = mkStrNode(bs);
      POPN(3);
      GOPAIR(res);
    }
  case T_BSGRAB:
    {
      CHECK(2);
      char *p = evalptr(STKARG(0));
      GCCHECK(2);                 /* StrNode + PAIR */
      struct bytestring bs = mk_ro_bytestring(strlen(p), p);
      NODEPTR res = mkStrNode(bs);
      POPN(2);
      GOPAIR(res);
    }
  case T_BSGRABLEN:
    {
      CHECK(3);
      char *p = evalptr(STKARG(0));
      size_t len = evalint(STKARG(1));
      GCCHECK(2);                 /* StrNode + PAIR */
      struct bytestring bs = mk_ro_bytestring(len, p);
      NODEPTR res = mkStrNode(bs);
      POPN(3);
      GOPAIR(res);
    }

  case T_ARR_ALLOC:
    {
      CHECK(3);
      value_t sz = evalint(STKARG(0));
      if (sz < 0)
        ERR("ARR_ALLOC");
      GCCHECK(2);               /* before arr_alloc, a GC would free the unreferenced array */
      struct ioarray *arr = arr_alloc((size_t)sz, STKARG(1));
      NODEPTR res = alloc_node(D_ARR);
      ARR(res) = arr;
      POPN(3);
      GOPAIR(res);
    }
  case T_ARR_COPY:
    {
      CHECK(2);
      NODEPTR a = evali(STKARG(0));
      if (get_XTAG(a) != D_ARR)
        ERR("T_ARR_COPY tag");
      GCCHECK(2);               /* before arr_copy, a GC would free the unreferenced array */
      struct ioarray *arr = arr_copy(ARR(a));
      NODEPTR res = alloc_node(D_ARR);
      ARR(res) = arr;
      POPN(2);
      GOPAIR(res);
    }
  case T_ARR_SIZE:
    {
      CHECK(2);
      NODEPTR a = evali(STKARG(0));
      if (get_XTAG(a) != D_ARR)
        ERR("bad ARR tag");
      size_t sz = ARR(a)->size;
      GCCHECK(2);
      NODEPTR res = mkInt(sz);
      POPN(2);
      GOPAIR(res);
    }
  case T_ARR_READ:
    {
      CHECK(3);
      size_t i = evalint(STKARG(1));
      NODEPTR a = evali(STKARG(0));
      if (get_XTAG(a) != D_ARR)
        ERR("bad ARR tag");
      if (i >= ARR(a)->size)
        ERR("ARR_READ");
      GCCHECK(1);
      NODEPTR res = ARR(a)->array[i];
      POPN(3);
      GOPAIR(res);
    }
  case T_ARR_WRITE:
    {
      CHECK(4);
      size_t i = evalint(STKARG(1));
      NODEPTR a = evali(STKARG(0));
      if (get_XTAG(a) != D_ARR)
        ERR("bad ARR tag");
      if (i >= ARR(a)->size) {
        ERR("ARR_WRITE");
      }
      ARR(a)->array[i] = STKARG(2);
      POPN(4);
      GOPAIRUNIT;
    }

  case T_ARR_TRUNC:
    {
      CHECK(3);
      size_t i = evalint(STKARG(1));
      NODEPTR a = evali(STKARG(0));
      if (get_XTAG(a) != D_ARR)
        ERR("bad ARR tag");
      if (i > ARR(a)->size) {
        ERR("ARR_TRUNC");
      }
      ARR(a)->size = i;
      POPN(3);
      GOPAIRUNIT;
    }

  case T_FPNEW:
    {
      CHECK(2);
      //printf("T_FPNEW\n");
      void *xp = evalptr(STKARG(0));
      //printf("T_FPNEW xp=%p\n", xp);
      GCCHECK(2);
      NODEPTR res = alloc_node(D_FORPTR);
      SETFORPTR(res, mkForPtrP(xp));
      POPN(2);
      GOPAIR(res);
    }
  case T_FPFIN:
    {
      CHECK(3);
      //printf("T_FPFIN\n");
      struct forptr *xfp = evalforptr(STKARG(1));
      //printf("T_FPFIN xfp=%p\n", xfp);
      HsFunPtr xp = evalfunptr(STKARG(0));
      //printf("T_FPFIN yp=%p\n", yp);
      xfp->finalizer->final = xp;
      POPN(3);
      GOPAIRUNIT;
    }
  case T_IO_GC:
    //printf("gc()\n");
    CHECK(2);
    {
      int red = evalint(STKARG(0));
      int oldred = want_gc_red;
      want_gc_red = red;
      gc();
      want_gc_red = oldred;
    }
    POPN(2);
    GOPAIRUNIT;

  case T_IO_STATS:
    {
    GCCHECK(5);
    CHKARG1;
    NODEPTR res = new_ap(new_ap(combPair, mkInt((uvalue_t)NUM_ALLOC)), mkInt((uvalue_t)(num_reductions - glob_slice)));
    GOPAIR(res);
    }

  case T_IO_FORK:
    {
      GCCHECK(3);
      CHKARG2;                /* set x=io, y=ST, n */
      struct mthread *mt = new_thread(new_ap(x, y)); /* copy the world */
      mt->mt_mask = runq.mq_head->mt_mask; /* inherit masking state */
      NODEPTR res = alloc_node(D_THID);
      THR(res) = mt;
      GOPAIR(res);
    }

  case T_IO_THID:
    {
      GCCHECK(2);
      CHKARG1;
      NODEPTR res = alloc_node(D_THID);
      THR(res) = runq.mq_head;            /* head of the run queue is the current thread */
      GOPAIR(res);
    }
  case T_IO_THROWTO:
    {
      CHECK(3);                 /* arg0=thid, arg1=exn, arg2=ST */
      check_thrown(true);       /* check if we have a thrown exception */
      struct mthread *mt = evalthid(STKARG(0));
      throwto(mt, STKARG(1));
      POPN(3);
      GOPAIRUNIT;
    }
  case T_IO_YIELD:
    CHECK(1);
    FUN(TOP(0)) = combPUnit;    /* update before yield() in case it reschedules */
    yield();
    POPN(1);
    GOTOP(n);

  case T_IO_NEWMVAR:
    {
      GCCHECK(2);
      CHKARG1;
      struct mvar *mv = new_mvar();
      NODEPTR res = alloc_node(D_MVAR);
      MVAR(res) = mv;
      GOPAIR(res);
    }
  case T_IO_TAKEMVAR:
    {
      CHECK(2);              /* arg0=mvar, arg1=ST */
      check_thrown(true);    /* check if we have a thrown exception */
      NODEPTR res = take_mvar(false, evalmvar(STKARG(0))); /* never returns if it blocks */
      GCCHECKSAVE(res, 1);
      POPN(2);
      GOPAIR(res);
    }
  case T_IO_READMVAR:
    {
      CHECK(2);
      check_thrown(true);    /* check if we have a thrown exception */
      NODEPTR res = read_mvar(false, evalmvar(STKARG(0))); /* never returns if it blocks */
      GCCHECKSAVE(res, 1);
      POPN(2);
      GOPAIR(res);
    }
  case T_IO_PUTMVAR:
    {
      CHECK(3);              /* arg0=mvar, arg1=value, arg2=ST */
      check_thrown(true);    /* check if we have a thrown exception */
      struct mvar *mv = evalmvar(STKARG(0));
      (void)put_mvar(false, mv, STKARG(1)); /* never returns if it blocks */
      POPN(3);
      GOPAIRUNIT;
    }
  case T_IO_TRYTAKEMVAR:
    {
      CHECK(2);
      NODEPTR res = take_mvar(true, evalmvar(STKARG(0)));
      GCCHECKSAVE(res, 2);
      if (!is_NIL(res))
        res = new_ap(combJust, res);
      else
        res = combNothing;
      POPN(2);
      GOPAIR(res);
    }
  case T_IO_TRYREADMVAR:
    {
      CHECK(2);
      NODEPTR res = read_mvar(true, evalmvar(STKARG(0)));
      GCCHECKSAVE(res, 2);
      if (!is_NIL(res))
        res = new_ap(combJust, res);
      else
        res = combNothing;
      POPN(2);
      GOPAIR(res);
    }
  case T_IO_TRYPUTMVAR:
    {
      CHECK(3);
      struct mvar *mv = evalmvar(STKARG(0));
      NODEPTR res = put_mvar(true, mv, STKARG(1)) ? combTrue : combFalse;
      GCCHECK(1);
      POPN(3);
      GOPAIR(res);
    }
  case T_IO_THREADDELAY:
    {
      CHECK(2);
#if defined(CLOCK_INIT)
      check_thrown(true);      /* check if we have a thrown exception */
      if (runq.mq_head->mt_at == -1) {
        /* delay has already expired, so just return */
        runq.mq_head->mt_at = 0;
        POPN(2);
        GOPAIRUNIT;
      } else {
        value_t dly = evalint(STKARG(0));
        if (dly <= 0) {
          POPN(2);
          GOPAIRUNIT;
        }
        thread_delay((uvalue_t)dly); /* never returns */
      }
#else
      ERR("threadDelay: no clock");
#endif
    }
  case T_IO_THREADSTATUS:
    {
      CHECK(2);
      struct mthread *mt = evalthid(STKARG(0));
      GCCHECK(2);
      POPN(2);
      GOPAIR(mkInt(mt->mt_state));
    }
  case T_IO_WAITRDFD:
  case T_IO_WAITWRFD: {
#if WANT_IO_POLL
    CHECK(2); /* arg0 = filedescriptor, arg1 = RealWorld; no pop yet */
    GCCHECK(2);

    /*
     * When the thread wakes up again it will re-execute that last op.
     * check_pollq() sets mt_fd=IO_POLL_EVENT_HAS_HAPPENED when waking the thread.
     * If we did not do this check we would just sleep again.
     */
    if (runq.mq_head->mt_fd == IO_POLL_EVENT_HAS_HAPPENED) {
      runq.mq_head->mt_fd = IO_POLL_WAITING_FOR_NONE;
      POPN(2);
      GOPAIR(mkInt(0));
    }

    check_thrown(true);      /* check if we have a thrown exception */

    int fd = evalint(STKARG(0));
    int events = tag == T_IO_WAITRDFD ? POLLIN : POLLOUT;

    /* Set up the waiting thread's state, preparing it to leave the run queue
     * until an event is ready for it.
     */
    struct mthread *mt = remove_q_head(&runq);
    mt->mt_fd     = fd;
    mt->mt_events = events;
    add_q_tail(&pollq, mt);     /* put it on the q of I/O waiters */
#if THREAD_DEBUG
      if (thread_trace) {
        printf("T_IO_WAITxxFD: wait for FD=%d, events=%x, thread=%d\n", fd, events, (int)mt->mt_id);
      }
#endif  /* THREAD_DEBUG */

    POP(2);
    resched(mt, ts_wait_io);    /* set the thread state and reschedule */
#else /* WANT_IO_POLL */
    CHECK(2);
#if WANT_ERRNO
    errno = EINVAL;
#endif
    GCCHECK(2);
    POPN(2);
    GOPAIR(mkInt(-1));          /* cannot poll */
#endif /* WANT_IO_POLL */
  }
  case T_IO_GETMASKINGSTATE:
    GCCHECK(2);
    CHKARG1;                    /* x = ST */
    GOPAIR(mkInt(runq.mq_head->mt_mask));

  case T_IO_SETMASKINGSTATE:
    CHECK(2);                   /* arg0 = level, arg1 = ST */
    runq.mq_head->mt_mask = evalint(STKARG(0));
    POPN(2);
    GOPAIRUNIT;

  case T_CATCH:
    /* CATCH x y z --> CATCHR m (x z) y z
     * where m is the current masking state, which the handler restores.
     * It is kept in the graph rather than taken when CATCHR runs, because
     * a rescheduled thread starts over from its root and runs CATCHR again,
     * maybe with another masking state, e.g., inside a mask in the io. */
    GCCHECK(5);
    CHKARG3;                    /* x=io, y=hdl, z=ST */
    GOAP(new_ap(new_ap(new_ap(combCATCHR, mkInt(runq.mq_head->mt_mask)), new_ap(x, z)), y), z);
  case T_CATCHR:
    {
      CHECK(4);                 /* arg0 = mask, arg1 = (io st), arg2 = hdl, arg3 = st */
      struct handler *h = mmalloc(sizeof *h);
      h->hdl_old = cur_handler;
      cur_handler = h;
      NODEPTR *ostack = stack_ptr;      /* old stack pointer */
      if (setjmp(h->hdl_buf)) {
        /* An exception occurred: */
        stack_ptr = ostack;
        runq.mq_head->mt_mask = mask_interruptible; /* evaluate with mask */
        NODEPTR exn = h->hdl_exn;       /* exception value */
        cur_handler = h->hdl_old;       /* reset handler */
        FREE(h);
        GCCHECKSAVE(exn, 7);
        x = STKARG(0);
        z = STKARG(2);
        w = STKARG(3);
        POPN(4);
        /*
         * Run:
         *  hdl exn `primBind` \ r ->
         *  primSetMaskingState m `primThen`
         *  primReturn r
         * i.e.,
         *  primBind (hdl exn) (B' primThen (primSetMaskingState m) primReturn)
         */
        NODEPTR p = new_ap(combIOBIND, new_ap(z, exn));
        NODEPTR q = new_ap(new_ap(new_ap(combBB, combIOTHEN), new_ap(combSETMASKINGSTATE, x)), combIORETURN);
        GOAP2(p, q, w);
      } else {
        /* Normal execution: */
        y = evali(STKARG(1));     /* execute the io */
        /* No exception occurred */
        cur_handler = h->hdl_old; /* restore old handler */
        FREE(h);
        POPN(4);
        GOIND(y);
      }
    }

  case T_THNUM:
    {
    CHECK(1);
    struct mthread *mt = evalthid(ARG(TOP(0)));
    POP(1);
    n = TOP(-1);
    SETINT(n, (uvalue_t)mt->mt_id);
    RET;
    }

#if WANT_TICK
  case D_TICK:
    xi = GETVALUE(n);
    CHKARG1;
    // fprintf(stderr, "tick=%s\n", (char*)tick_table[xi].tick_name.string); fflush(stderr);
    dotick(xi);
    GOIND(x);
#endif

  default:
    ERR1("eval tag %s", TAGNAME(get_XTAG(n)));
  }
  RET;
}

/* n has been evaluated, and the marker tag is on top of the stack. */
static NOINLINE struct prim_res
evalprimret(NODEPTR n, enum node_tag tag)
{
  uvalue_t yu, ru;
#if WANT_INT64
  uint64_t x64u, y64u, r64u;
#endif  /* WANT_INT64 */
#if WANT_FLOAT32
  flt32_t xf, yf, rf;
#endif  /* WANT_FLOAT32 */
#if WANT_FLOAT64
  flt64_t xd, yd, rd;
#endif  /* WANT_FLOAT64 */
  struct bytestring xbs, ybs, rbs;
  value_t r;
  NODEPTR p, y;

  switch (tag) {

#if WANT_INT64
    case T_BININT64_2:
      n = ARG(TOP(1));
      TOP(0) = combBININT64_1;
      GOTOP(n);

    case T_BININT64_1:
      /* First argument */
#if SANITY
      if (get_XTAG(n) != D_INT64) {
        //fprintf(stderr, "tag=%s\n", TAGNAME(get_XTAG(n))); fflush(stderr);
        ERR("BININT64 0");
      }
#endif  /* SANITY */
      x64u = (uint64_t)GETINT64VALUE(n);
      /* Second argument */
      y = ARG(TOP(2));
      while (get_XTAG(y) == D_IND)
        y = get_IND(y);
      /* The second argument to the shift ops is an int, so use a hack for that */
      if (get_XTAG(y) == D_INT64)
        y64u = (uint64_t)GETINT64VALUE(y);
      else if (get_XTAG(y) == D_INT)
        yu = (uvalue_t)GETVALUE(y);
      else
        ERR("BININT64 1");
      p = FUN(TOP(1));
      POP(3);
      n = TOP(-1);
    binint64:
      switch (get_XTAG(p)) {
      case D_IND:   p = get_IND(p); goto binint64;
      case T_ADD64: ADD_OVERFLOW(int64_t, r64u, x64u, y64u, raise_rts); break;
      case T_SUB64: SUB_OVERFLOW(int64_t, r64u, x64u, y64u, raise_rts); break;
      case T_MUL64: MUL_OVERFLOW(int64_t, r64u, x64u, y64u, raise_rts); break;
      case T_SUBR64:SUB_OVERFLOW(int64_t, r64u, y64u, x64u, raise_rts); break;
      case T_QUOT64:if (y64u == 0)
                      raise_rts(exn_dividebyzero);
                    else if ((int64_t)x64u == INT64_MIN && (int64_t)y64u == -1)
                      raise_rts(exn_overflow);
                    else
                      r64u = (uint64_t)((int64_t)x64u / (int64_t)y64u);
                    break;
      case T_REM64: if (y64u == 0)
                      raise_rts(exn_dividebyzero);
                    else if ((int64_t)y64u == -1) /* MIN % -1 traps on x86 */
                      r64u = 0;
                    else
                      r64u = (uint64_t)((int64_t)x64u % (int64_t)y64u);
                    break;
      case T_UADD64:r64u = x64u + y64u; break;
      case T_USUB64:r64u = x64u - y64u; break;
      case T_UMUL64:r64u = x64u * y64u; break;
      case T_USUBR64:r64u = y64u - x64u; break;
      case T_UQUOT64:if (y64u == 0)
                      raise_rts(exn_dividebyzero);
                    else
                      r64u = x64u / y64u;
                    break;
      case T_UREM64:if (y64u == 0)
                      raise_rts(exn_dividebyzero);
                    else
                      r64u = x64u % y64u;
                    break;
      case T_AND64: r64u = x64u & y64u; break;
      case T_OR64:  r64u = x64u | y64u; break;
      case T_XOR64: r64u = x64u ^ y64u; break;
      case T_SHL64: r64u = x64u << yu; break;
      case T_SHR64: r64u = x64u >> yu; break;
      case T_ASHR64:r64u = (uint64_t)((int64_t)x64u >> yu); break;

      case T_EQ64:  GOBOOL(x64u == y64u);
      case T_NE64:  GOBOOL(x64u != y64u);
      case T_ULT64: GOBOOL(x64u <  y64u);
      case T_ULE64: GOBOOL(x64u <= y64u);
      case T_UGT64: GOBOOL(x64u >  y64u);
      case T_UGE64: GOBOOL(x64u >= y64u);
      case T_UCMP64:GOIND(x64u <  y64u ? combLT   : x64u > y64u ? combGT : combEQ);
      case T_LT64:  GOBOOL((int64_t)x64u <  (int64_t)y64u);
      case T_LE64:  GOBOOL((int64_t)x64u <= (int64_t)y64u);
      case T_GT64:  GOBOOL((int64_t)x64u >  (int64_t)y64u);
      case T_GE64:  GOBOOL((int64_t)x64u >= (int64_t)y64u);
      case T_ICMP64:GOIND((int64_t)x64u <  (int64_t)y64u ? combLT   : (int64_t)x64u > (int64_t)y64u ? combGT : combEQ);

      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("BININT64");
      }
      SETINT64(n, (int64_t)r64u);
      RET;

    case T_UNINT64_1:
      /* The argument */
#if SANITY
      if (get_XTAG(n) != D_INT64)
        ERR("UNINT64 0");
#endif
      x64u = (uint64_t)GETINT64VALUE(n);
      p = FUN(TOP(1));
      POP(2);
      n = TOP(-1);
    unint64:
      switch (get_XTAG(p)) {
      case D_IND:        p = get_IND(p); goto unint64;
      case T_NEG64:
#if WANT_OVERFLOW
                       if ((int64_t)x64u == INT64_MIN) raise_rts(exn_overflow);
#endif
                       r64u = -x64u; break;
      case T_UNEG64:     r64u = -x64u; break;
      case T_INV64:      r64u = ~x64u; break;
      case T_POPCOUNT64: ru = POPCOUNT64(x64u); SETINT(n, (value_t)ru); RET;
      case T_CLZ64:      ru = CLZ64(x64u); SETINT(n, (value_t)ru); RET;
      case T_CTZ64:      ru = CTZ64(x64u); SETINT(n, (value_t)ru); RET;
      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("UNINT64");
      }
      SETINT64(n, (int64_t)r64u);
      RET;
#endif  /* WANT_INT64 */

#if WANT_FLOAT32
    case T_BINFLT2:
      n = ARG(TOP(1));
      TOP(0) = combBINFLT1;
      GOTOP(n);

    case T_BINFLT1:
      /* First argument */
#if SANITY
      if (get_XTAG(n) != D_FLT32)
        ERR("BINFLT 0");
#endif  /* SANITY */
      xf = GETFLTVALUE(n);
      /* Second argument */
      y = ARG(TOP(2));
      while (get_XTAG(y) == D_IND)
        y = get_IND(y);
#if SANITY
      if (get_XTAG(y) != D_FLT32)
        ERR("BINFLT 1");
#endif  /* SANITY */
      yf = GETFLTVALUE(y);
      p = FUN(TOP(1));
      POP(3);
      n = TOP(-1);
    binflt:
      switch (get_XTAG(p)) {
      case D_IND:   p = get_IND(p); goto binflt;
      case T_FADD:  rf = xf + yf; break;
      case T_FSUB:  rf = xf - yf; break;
      case T_FMUL:  rf = xf * yf; break;
      case T_FDIV:  rf = xf / yf; break;

      case T_FEQ:   GOBOOL(xf == yf);
      case T_FNE:   GOBOOL(xf != yf);
      case T_FLT:   GOBOOL(xf <  yf);
      case T_FLE:   GOBOOL(xf <= yf);
      case T_FGT:   GOBOOL(xf >  yf);
      case T_FGE:   GOBOOL(xf >= yf);

      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("BINFLT");
      }
      SETFLT(n, rf);
      RET;

    case T_UNFLT1:
      /* The argument */
#if SANITY
      if (get_XTAG(n) != D_FLT32)
        ERR("UNFLT 0");
#endif
      xf = GETFLTVALUE(n);
      p = FUN(TOP(1));
      POP(2);
      n = TOP(-1);
    unflt:
      switch (get_XTAG(p)) {
      case D_IND:   p = get_IND(p); goto unflt;
      case T_FNEG:  rf = -xf; break;
      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("UNFLT");
      }
      SETFLT(n, rf);
      RET;
#endif  /* WANT_FLOAT32 */

#if WANT_FLOAT64
    case T_BINDBL2:
      n = ARG(TOP(1));
      TOP(0) = combBINDBL1;
      GOTOP(n);

    case T_BINDBL1:
      /* First argument */
#if SANITY
      if (get_XTAG(n) != D_DBL)
        ERR("BINDBL 0");
#endif  /* SANITY */
      xd = GETDBLVALUE(n);
      /* Second argument */
      y = ARG(TOP(2));
      while (get_XTAG(y) == D_IND)
        y = get_IND(y);
#if SANITY
      if (get_XTAG(y) != D_DBL)
        ERR("BINDBL 1");
#endif  /* SANITY */
      yd = GETDBLVALUE(y);
      p = FUN(TOP(1));
      POP(3);
      n = TOP(-1);
    bindbl:
      switch (get_XTAG(p)) {
      case D_IND:   p = get_IND(p); goto bindbl;
      case T_DADD:  rd = xd + yd; break;
      case T_DSUB:  rd = xd - yd; break;
      case T_DMUL:  rd = xd * yd; break;
      case T_DDIV:  rd = xd / yd; break;

      case T_DEQ:   GOBOOL(xd == yd);
      case T_DNE:   GOBOOL(xd != yd);
      case T_DLT:   GOBOOL(xd <  yd);
      case T_DLE:   GOBOOL(xd <= yd);
      case T_DGT:   GOBOOL(xd >  yd);
      case T_DGE:   GOBOOL(xd >= yd);

      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("BINDBL");
      }
      SETDBL(n, rd);
      RET;

    case T_UNDBL1:
      /* The argument */
#if SANITY
      if (get_XTAG(n) != D_DBL)
        ERR("UNDBL 0");
#endif
      xd = GETDBLVALUE(n);
      p = FUN(TOP(1));
      POP(2);
      n = TOP(-1);
    undbl:
      switch (get_XTAG(p)) {
      case D_IND:   p = get_IND(p); goto undbl;
      case T_DNEG:  rd = -xd; break;
      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("UNDBL");
      }
      SETDBL(n, rd);
      RET;
#endif  /* WANT_FLOAT64 */

    case T_BINBS2:
      n = ARG(TOP(1));
      TOP(0) = combBINBS1;
      GOTOP(n);

    case T_BINBS1:
      /* First argument */
#if SANITY
      if (get_XTAG(n) != D_FORPTR || FORPTR(n)->finalizer->fptype != FP_BSTR)
        ERR("BINBS 0");
#endif  /* SANITY */
      xbs = BSTR(n);
      /* Second argument */
      y = ARG(TOP(2));
      while (get_XTAG(y) == D_IND)
        y = get_IND(y);
#if SANITY
      if (get_XTAG(y) != D_FORPTR || FORPTR(y)->finalizer->fptype != FP_BSTR)
        ERR("BINBS 1");
#endif  /* SANITY */
      ybs = BSTR(y);
      p = FUN(TOP(1));
      POP(3);
      n = TOP(-1);
    binbs:
      switch (get_XTAG(p)) {
      case D_IND:    p = get_IND(p); goto binbs;

      case T_BSAPPEND: rbs = bsappend(xbs, ybs); break;
      case T_BSAPPENDDOT: rbs = bsappenddot(xbs, ybs); break;
      case T_BSEQ:   GOBOOL(bsequal(xbs, ybs));
      case T_BSNE:   GOBOOL(!bsequal(xbs, ybs));
      case T_BSLT:   GOBOOL(bscompare(xbs, ybs) <  0);
      case T_BSLE:   GOBOOL(bscompare(xbs, ybs) <= 0);
      case T_BSGT:   GOBOOL(bscompare(xbs, ybs) >  0);
      case T_BSGE:   GOBOOL(bscompare(xbs, ybs) >= 0);
      case T_BSCMP:  r = bscompare(xbs, ybs); GOIND(r < 0 ? combLT : r > 0 ? combGT : combEQ);

      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("BINBS");
      }
      SETBSTR(n, mkForPtrFree(rbs));
      RET;

  default:
    ERR1("evalprimret %s", TAGNAME(tag));
  }
}

#undef RET
#undef GOTOP
#undef GOIND
#undef GOAP
#undef GOAP2
#undef GOPAIR
#undef GOPAIRUNIT

/*
 * evali keeps the hot globals in locals of the same name, so the C compiler
 * can keep them in registers: stack_ptr, glob_slice, bump_ptr, and bump_end,
 * and stack_limit (read-only).  Inside evali the names refer to the locals,
 * so PUSH, TOP, CHKARGn, etc. use them; the functions below reach the globals.
 * EV_SAVE writes the locals back, and EV_LOAD reads them again.
 * The globals must be current whenever other code runs, so evali does EV_SAVE
 * before every call that can look at them, allocate, GC, or longjmp (an
 * exception or a thread switch), and EV_LOAD after the calls that return.
 * With BUMP_ALLOC, a single check on (bump_end - bump_ptr) guards both GC and
 * bump refill: whenever k cells remain in the current run they are already
 * claimed and free, so num_free is not needed in evali at all.
 */
static INLINE NODEPTR *ev_get_stack_ptr(void) { return stack_ptr; }
static INLINE void ev_set_stack_ptr(NODEPTR *s) { stack_ptr = s; }
#if STACKOVL
static INLINE NODEPTR *ev_get_stack_limit(void) { return stack_limit; }
#endif  /* STACKOVL */
static INLINE int ev_get_glob_slice(void) { return glob_slice; }
static INLINE void ev_set_glob_slice(int s) { glob_slice = s; }

static ALWAYS_INLINE NODEPTR
mk_ap_node(struct node *p, NODEPTR f, NODEPTR a)
{
  NODEPTR n;
  n.node_ptr = p;
  FUN(n) = f;
  ARG(n) = a;
  return n;
}

#if BUMP_ALLOC
static INLINE struct node *ev_get_bump_ptr(void) { return bump_ptr; }
static INLINE struct node *ev_get_bump_end(void) { return bump_end; }
static INLINE void ev_set_bump(struct node *p, struct node *e) { bump_ptr = p; bump_end = e; }
#define EV_SAVE_BUMP() ev_set_bump(bump_ptr, bump_end)
#define EV_LOAD_BUMP() do { bump_ptr = ev_get_bump_ptr(); bump_end = ev_get_bump_end(); } while(0)

struct node2 { struct node *p0, *p1; };
struct node3 { struct node *p0, *p1, *p2; };

/* Cold slow-path allocators for evali: called only when bump_end - bump_ptr < k.
 * They save stack_ptr and bump state, GC if needed, and allocate k cells
 * (handling fragmented runs cleanly without wasting the tail of the current run).
 * Neither modifies stack_ptr or glob_slice on return, so evali only reloads bump_ptr/bump_end. */
static NOINLINE struct node *
ev_alloc1(NODEPTR *sp)
{
  if (UNLIKELY(num_free <= 1)) {
    stack_ptr = sp;
    bump_ptr = bump_end = 0;
    gc_for(1);
  }
  bump_refill_body();
  return bump_ptr++;
}

static NOINLINE struct node2
ev_alloc2(NODEPTR *sp, struct node *bp, struct node *be)
{
  struct node2 r;
  if (UNLIKELY(2 >= num_free + (counter_t)(be - bp))) {
    stack_ptr = sp;
    bump_ptr = bp;
    bump_end = be;
    gc_for(2);
    bp = be = 0;
  }
  if (bp < be) {
    r.p0 = bp++;
  } else {
    bump_refill_body();
    bp = bump_ptr;
    be = bump_end;
    r.p0 = bp++;
  }
  if (bp < be) {
    r.p1 = bp++;
  } else {
    bump_refill_body();
    bp = bump_ptr;
    be = bump_end;
    r.p1 = bp++;
  }
  bump_ptr = bp;
  bump_end = be;
  return r;
}

static NOINLINE struct node3
ev_alloc3(NODEPTR *sp, struct node *bp, struct node *be)
{
  struct node3 r;
  if (UNLIKELY(3 >= num_free + (counter_t)(be - bp))) {
    stack_ptr = sp;
    bump_ptr = bp;
    bump_end = be;
    gc_for(3);
    bp = be = 0;
  }
  bump_ptr = bp;
  bump_end = be;
  r.p0 = alloc_node_ptr();
  r.p1 = alloc_node_ptr();
  r.p2 = alloc_node_ptr();
  return r;
}

static NOINLINE NODEPTR
ev_constr_alloc(NODEPTR *sp, struct node *bp, struct node *be, int fidx, int nn)
{
  if (UNLIKELY((size_t)(nn - 1) >= num_free + (counter_t)(be - bp))) {
    stack_ptr = sp;
    bump_ptr = bp;
    bump_end = be;
    gc_for(nn - 1);
    bp = be = 0;
  }
  bump_ptr = bp;
  bump_end = be;
  NODEPTR fk = ARG(sp[fidx]);
  for (int i = 0; i < nn - 1; i++)
    fk = mk_ap_node(alloc_node_ptr(), fk, ARG(sp[i]));
  return fk;
}
#else  /* BUMP_ALLOC */
#define EV_SAVE_BUMP() do { } while(0)
#define EV_LOAD_BUMP() do { } while(0)
#endif  /* BUMP_ALLOC */
#define EV_SAVE() do { ev_set_stack_ptr(stack_ptr); ev_set_glob_slice(glob_slice); EV_SAVE_BUMP(); } while(0)
#define EV_LOAD() do { stack_ptr = ev_get_stack_ptr(); glob_slice = ev_get_glob_slice(); EV_LOAD_BUMP(); } while(0)

#undef GCCHECK
#define GCCHECK(k) do { if (!((k) < num_free)) { EV_SAVE(); gc_for((k)); EV_LOAD(); } } while(0)
#define EV_RAISE(e) do { EV_SAVE(); raise_rts(e); } while(0)

#if defined(__EMSCRIPTEN__)
/* With -sWASM=0 (JavaScript only) binaryen inlines evalprimop and evalprimret,
 * which have a single caller each, into evali in spite of NOINLINE.  V8 does not
 * optimize the result ("Function is too big to be optimized"), so all of evali
 * runs slowly.  A call through a volatile pointer cannot be inlined. */
static struct prim_res (* volatile evalprimop_p)(NODEPTR, enum node_tag, NODEPTR *) = evalprimop;
static struct prim_res (* volatile evalprimret_p)(NODEPTR, enum node_tag) = evalprimret;
#define EVALPRIMOP evalprimop_p
#define EVALPRIMRET evalprimret_p
#else
#define EVALPRIMOP evalprimop
#define EVALPRIMRET evalprimret
#endif

/* Evaluate a node, returns when the node is in WHNF. */
NODEPTR
evali(NODEPTR an)
{
  /* The cached globals, see above. */
  NODEPTR *stack_ptr = ev_get_stack_ptr();
  int glob_slice = ev_get_glob_slice();
#if STACKOVL
  NODEPTR * const stack_limit = ev_get_stack_limit();
#endif  /* STACKOVL */
#if BUMP_ALLOC
  struct node *bump_ptr = ev_get_bump_ptr();
  struct node *bump_end = ev_get_bump_end();
#endif  /* BUMP_ALLOC */
  NODEPTR n = an;
  NODEPTR *stk = stack_ptr;
  NODEPTR x, y, z, w;
  enum node_tag tag;

  //fprintf(stderr, "evali enter stk=%d\n", (int)stk);

#if MAXSTACKDEPTH
  counter_t old_cur_c_stack = cur_c_stack;
  if (++cur_c_stack > max_c_stack)
    max_c_stack = cur_c_stack;
#endif

/* Control macros for evali.
 * GOAP and GOAP2_P run right after popping k >= 1 (or k >= 2) spine entries
 * with n = TOP(-1), so n is already in place on the stack and net stack growth
 * is <= 0 (no STACKOVL check, no rewriting n, no reloading FUN(n)/FUN(m)).
 */
#define RET do { goto ret; } while(0)
#define GOIND(x) do { NODEPTR _x = (x); SETIND(n, _x); n = _x; goto top; } while(0)
#define GOAP(f,a) do { \
    NODEPTR _f = (f), _a = (a); \
    FUN(n) = _f; ARG(n) = _a; \
    --stack_ptr; \
    XASSERT(STACK_ABOVE(stk) >= 1 && eq_NODEPTR(TOP(0), n)); \
    n = _f; \
    goto top; \
  } while(0)
#define GOAP2_P(p,f,a,b) do { \
    NODEPTR _f = (f), _a = (a), _b = (b); \
    NODEPTR _m = mk_ap_node((p), _f, _a); \
    FUN(n) = _m; ARG(n) = _b; \
    stack_ptr -= 2; \
    XASSERT(STACK_ABOVE(stk) >= 2 && eq_NODEPTR(TOP(1), n)); \
    TOP(0) = _m; \
    n = _f; \
    goto top; \
  } while(0)
#if BUMP_ALLOC
#define ALLOC1(p0) do { \
    if (UNLIKELY(bump_ptr == bump_end)) { (p0) = ev_alloc1(stack_ptr); EV_LOAD_BUMP(); } \
    else { (p0) = bump_ptr++; } \
  } while(0)
#define ALLOC2(p0, p1) do { \
    if (UNLIKELY(bump_end - bump_ptr < 2)) { struct node2 _r = ev_alloc2(stack_ptr, bump_ptr, bump_end); EV_LOAD_BUMP(); (p0) = _r.p0; (p1) = _r.p1; } \
    else { (p0) = bump_ptr; (p1) = bump_ptr + 1; bump_ptr += 2; } \
  } while(0)
#define ALLOC3(p0, p1, p2) do { \
    if (UNLIKELY(bump_end - bump_ptr < 3)) { struct node3 _r = ev_alloc3(stack_ptr, bump_ptr, bump_end); EV_LOAD_BUMP(); (p0) = _r.p0; (p1) = _r.p1; (p2) = _r.p2; } \
    else { (p0) = bump_ptr; (p1) = bump_ptr + 1; (p2) = bump_ptr + 2; bump_ptr += 3; } \
  } while(0)
#else
#define ALLOC1(p0)         do { GCCHECK(1); (p0) = alloc_node_ptr(); } while(0)
#define ALLOC2(p0, p1)     do { GCCHECK(2); (p0) = alloc_node_ptr(); (p1) = alloc_node_ptr(); } while(0)
#define ALLOC3(p0, p1, p2) do { GCCHECK(3); (p0) = alloc_node_ptr(); (p1) = alloc_node_ptr(); (p2) = alloc_node_ptr(); } while(0)
#endif

 top:
  /*pp(stdout, an);*/
  if (--glob_slice <= 0) {
    EV_SAVE();
    yield();
    EV_LOAD();
  }
  for(;;) {
    if (is_VIRT(n)) {
      /* A virtual pointer, i.e., a combinator, primop, constructor, etc. */
      tag = get_VIRT_TAG(n);
      break;
    }
    NODEPTR f = FUN(n);
    if (is_AP(f)) {
      /* follow AP nodes down the spine */
      PUSH(n);
      n = f;
      continue;
    }
    tag = get_DATA_TAG(f);
    if (tag == D_IND) {
      /* Follow and short-circuit the chain of indirections. */
      NODEPTR on = n;
      do {
        n = ARG(n);
      } while (is_DATA_NODE(n, D_IND));
      ARG(on) = n;              /* short-circuit; on is still an IND */
      continue;                 /* n can be an AP node */
    }
    break;
  }
  /* Invariant: at this point n=current node, tag=get_XTAG(n), and tag is not X_AP or D_IND */

  //printf("%s %d\n", tag_names[tag], (int)stack_ptr);
  //if (stack_ptr < -1)
  //  ERR("stack_ptr");
  switch (tag) {
  case X_AP:   PUSH(n);
    n = FUN(n); goto top;

  case D_INT:    RET;
  case D_DBL:    RET;
  case D_INT64:  RET;
  case D_FLT32:  RET;
  case D_PTR:    RET;
  case D_FUNPTR: RET;
  case D_FORPTR: RET;
  case D_ARR:    RET;
  case D_THID:   RET;
  case D_MVAR:   RET;
  case D_WEAK:   RET;
  case D_BADDYN: ERR1("FFI unknown %s", CSTR(n));

  /*
   * Some of these reductions, (e.g., Z x y = K (x y)) are there to avoid
   * that increase in arity that some "optimizations" in Abstract.hs
   * stop reductions from happening.  This can be important for "full laziness".
   * In practice, these reductions almost never happen, but there they are anyway. :)
   */
  case T_S:    { struct node *p0, *p1; CHECK(3); ALLOC2(p0, p1); POPARG3; GOAP2_P(p0, x, z, mk_ap_node(p1, y, z)); }             /* S x y z = x z (y z) */
  case T_SS:   { struct node *p0, *p1, *p2; CHECK(4); ALLOC3(p0, p1, p2); POPARG4; GOAP2_P(p0, x, mk_ap_node(p1, y, w), mk_ap_node(p2, z, w)); } /* S' x y z w = x (y w) (z w) */
  case T_K:                CHKARG2; GOIND(x);                                             /* K x y = *x */
  case T_A:                CHKARG2; GOIND(y);                                             /* A x y = *y */
  case T_U:                CHKARG2; GOAP(y, x);                                           /* U x y = y x */
  case T_I:                CHKARG1; GOIND(x);                                             /* I x = *x */
  case T_Y:                CHKARG1; GOAP(x, n);                                           /* n@(Y x) = x n */
  case T_B:    { struct node *p0; CHECK(3); ALLOC1(p0); POPARG3; GOAP(x, mk_ap_node(p0, y, z)); }                              /* B x y z = x (y z) */
  case T_BB:   if (!HASNARGS(4)) {
                 struct node *p0; CHECK(2); ALLOC1(p0); POPARG2; COUNT(red_bb); GOAP(combB, mk_ap_node(p0, x, y));             /* B' x y = B (x y) */
               } else {
                 struct node *p0, *p1; ALLOC2(p0, p1); POPARG4; GOAP2_P(p0, x, y, mk_ap_node(p1, z, w));                       /* B' x y z w = x y (z w) */
               }
  case T_Z:    if (!HASNARGS(3)) {
                 struct node *p0; CHECK(2); ALLOC1(p0); POPARG2; COUNT(red_z); GOAP(combK, mk_ap_node(p0, x, y));              /* Z x y = K (x y) */
               } else {
                 CHKARG3; GOAP(x, y);                                                                                           /* Z x y z = x y */
               }
  t_c:
  case T_C:    { struct node *p0; CHECK(3); ALLOC1(p0); POPARG3; GOAP2_P(p0, x, z, y); }                                       /* C x y z = x z y */
  case T_CC:   { struct node *p0, *p1; CHECK(4); ALLOC2(p0, p1); POPARG4; GOAP2_P(p0, x, mk_ap_node(p1, y, w), z); }           /* C' x y z w = x (y w) z */
  t_p:
  case T_P:    { struct node *p0; CHECK(3); ALLOC1(p0); POPARG3; GOAP2_P(p0, z, x, y); }                                       /* P x y z = z x y */
  case T_R:    if(!HASNARGS(3)) {
                 struct node *p0; CHECK(2); ALLOC1(p0); POPARG2; COUNT(red_r); GOAP2_P(p0, combC, y, x);                       /* R x y = C y x */
               } else {
                 struct node *p0; ALLOC1(p0); POPARG3; GOAP2_P(p0, y, z, x);                                                   /* R x y z = y z x */
               }
  case T_O:    { struct node *p0; CHECK(4); ALLOC1(p0); POPARG4; GOAP2_P(p0, w, x, y); }                                       /* O x y z w = w x y */
  case T_K2:   if (!HASNARGS(3)) {
                           CHKARG2; COUNT(red_k2); GOAP(combK, x); } else {               /* K2 x y = K x */
                           CHKARG3; GOIND(x); }                                           /* K2 x y z = *x */
  case T_K3:   if (!HASNARGS(4)) {
                           CHKARG2; COUNT(red_k3); GOAP(combK2, x); } else {              /* K3 x y = K2 x */
                           CHKARG4; GOIND(x); }                                           /* K3 x y z w = *x */
  case T_K4:   if (!HASNARGS(5)) {
                           CHKARG2; COUNT(red_k4); GOAP(combK3, x); } else {              /* K4 x y = K3 x */
                           CHKARG5; GOIND(x); }                                           /* K4 x y z w v = *x */
  case T_CCB:  if (!HASNARGS(4)) {
                 struct node *p0, *p1; CHECK(3); ALLOC2(p0, p1); POPARG3; COUNT(red_ccb); GOAP2_P(p0, combB, mk_ap_node(p1, x, z), y); /* C'B x y z = B (x z) y */
               } else {
                 struct node *p0, *p1; ALLOC2(p0, p1); POPARG4; GOAP2_P(p0, x, z, mk_ap_node(p1, y, w));                       /* C'B x y z w = x z (y w) */
               }

    /*
     * Strict primitives require evaluating the arguments before we can proceed.
     * The easiest way to do this is to just recursively call evali() for each argument.
     * The drawback of this is that it uses a lot of C stack.  (E.g., recompiling MicroHs
     * uses a stack depth of 1800).
     * Instead we use the following scheme:
     *  When we find a strict binary (int) primitive we push T_BININT2,
     *  set n=second argument.
     *  Continue evaluation of n.
     *  When n is finally evaluated and we are about to return we check if the stack top is T_BININT2.
     *  If so, change the stack top to T_BININT1,
     *  set n=first argument.
     *  Continue evaluation of n.
     *  When n is finally evaluated and we are about to return we check if the stack top is T_BININT1.
     *  If so, we know that both arguments are now evaluated, and we perform the strict operation.
     * If we switch threads at any time during this the BININT1/2 on top of the stack will be lost,
     * but this doesn't matter; the operation is there and we will find it and redo BININT2 again.
     *
     * On my desktop machine this is about 3% slower, on my laptop (Apple M1) it is about 3% faster.
     *
     * Pictorially for BININT
     *  Before the code below:
     *  ----
     *  | --------> @
     *  ----       / \
     *  | ------> @   y
     *  ----     / \
     *  n ----> ADD x
     *
     * After
     *  ----
     *  | --------> @
     *  ----       / \
     *  | ------> @   y
     *  ----     / \
     *  | ->BI2 ADD x
     *  ----        ^
     *  n ----------|
     *
     *  x becomes an INT, stack is not empty, BININT2 found on top
     *  ----
     *  | --------> @
     *  ----       / \
     *  | ------> @   y
     *  ----     / \
     *  | ->BI2 ADD INT
     *  ----        ^
     *  n ----------|
     *
     *  After
     *  ----
     *  | --------> @
     *  ----       / \
     *  | ------> @   y
     *  ----     / \    \
     *  | ->BI1 ADD INT  |
     *  ----             |
     *  n ---------------|
     *
     *  y becomes an INT, stack is not empty, BININT1 found on top
     *  do arithmetic
     *  ----
     *  | --------> @
     *  ----       / \
     *  | ------> @   INT
     *  ----     / \    \
     *  | ->BI1 ADD INT  |
     *  ----             |
     *  n ---------------|
     *
     *  ----
     *  n -------> INT(x+y)
     */
  case T_ADD:
  case T_SUB:
  case T_MUL:
  case T_QUOT:
  case T_REM:
  case T_SUBR:
  case T_UADD:
  case T_USUB:
  case T_UMUL:
  case T_UQUOT:
  case T_UREM:
  case T_USUBR:
  case T_AND:
  case T_OR:
  case T_XOR:
  case T_SHL:
  case T_SHR:
  case T_ASHR:
  case T_EQ:
  case T_NE:
  case T_LT:
  case T_LE:
  case T_GT:
  case T_GE:
  case T_ICMP:
  case T_ULT:
  case T_ULE:
  case T_UGT:
  case T_UGE:
  case T_UCMP:
    CHECK(2);
    n = ARG(TOP(1));
    if (is_DATA_NODE(n, D_INT)) {
      n = ARG(TOP(0));
      PUSH(combBININT1);
      if (is_DATA_NODE(n, D_INT))
        goto binint1;
    } else {
      PUSH(combBININT2);
    }
    goto top;

  case T_NEG:
  case T_UNEG:
  case T_INV:
  case T_POPCOUNT:
  case T_CLZ:
  case T_CTZ:
    CHECK(1);
    n = ARG(TOP(0));
    PUSH(combUNINT1);
    goto top;

  case T_SEQ:  CHECK(2); EV_SAVE(); evali(ARG(TOP(0))); EV_LOAD(); POP(2); n = TOP(-1); y = ARG(n); GOIND(y); /* seq x y = eval(x); y */

  case T_IO_BIND:
    goto t_c;
  case T_IO_RETURN:
    goto t_p;
  case T_IO_THEN:
    {
      struct node *p0, *p1;
      CHECK(2);
      ALLOC2(p0, p1);
      POPARG2;
      GOAP2_P(p0, combIOBIND, x, mk_ap_node(p1, combK, y));
    }

  /* The scrutinee is the last argument:
   *   CASED_a0_..._a{m-1} f0 ... f{m-1} e
   *   CASES_k1_n1_..._km_nm d f1 ... fm e
   * Push a virtual LOOKx as a continuation marker and evaluate e:
   * LOOKD_m, or LOOKS with the same table entry as the CASES.
   * The arms stay where they are on the stack.
   * Nothing is allocated, and no heap node is written.
   */
  case V_CASED:
    {
      int m = get_case_arity(n)->m;
      CHECK(m + 1);
      n = ARG(TOP(m));
      PUSH(mk_CASE(V_LOOKD, m));
      goto top;
    }
  case V_CASES:
    {
      uint32_t i = get_case_index(n);
      int m = case_arities[i].m;
      CHECK(m + 2);
      n = ARG(TOP(m + 1));
      PUSH(mk_CASE(V_LOOKS, i));
      goto top;
    }
  case V_LOOKD:
  case V_LOOKS:
    RET;
  case V_CONSTR:
    {
      int k, nn;
      get_CON_info(n, &k, &nn);
      /* Stack layout when there is a case waiting:
       *   TOP(0) .. TOP(nn-1)   constructor spine, ARG(TOP(i)) is field i
       *   TOP(nn)               LOOKx_m marker
       *   TOP(nn+1) ...         the case spine, the last one is the case redex
       */
      CHECK(nn+1);
      NODEPTR p = TOP(nn);
      if (!is_VIRT(p))
        ERR("CONSTR: no LOOK");
      enum node_tag ltag = get_VIRT_TAG(p);
      NODEPTR fk;
      int fidx;                 /* stack index of the selected arm */
      int npop;                 /* number of stack entries to pop, the last one is the case redex */
      if (ltag == V_LOOKD) {
        /* CONSTR_k_n e1 ... en LOOKD_m (CASED_a0_..._a{m-1} f0 ... f{m-1} _)  -->  fk e1 ... en */
        fidx = nn + 1 + k;
        npop = nn + 2 + get_CASE_size(p);
      } else if (ltag == V_LOOKS) {
        /* CONSTR_k_n e1 ... en LOOKS (CASES_k1_n1_..._km_nm d f1 ... fm _)  -->  fj e1 ... en, when kj is k
         *                                                                  -->  d, when no kj is k
         * The constructor numbers are increasing, so use binary search.
         */
        struct case_arity *c = get_case_arity(p);
        int lo = 0, hi = c->m;  /* search arms [lo, hi) */
        npop = nn + 3 + c->m;
        for (;;) {
          if (lo >= hi) {
            /* Not found, use the default, which takes no arguments. */
            fk = ARG(TOP(nn + 1));
            POP(npop);
            n = TOP(-1);
            GOIND(fk);
          }
          int mid = (lo + hi) / 2;
          int ki = c->tags[mid];
          if (ki == k) {
            fidx = nn + 2 + mid;
            break;
          } else if (ki < k) {
            lo = mid + 1;
          } else {
            hi = mid;
          }
        }
      } else {
        ERR1("CONSTR: no LOOK, found %s", TAGNAME(ltag));
      }
      if (nn == 0) {
        fk = ARG(TOP(fidx));
        POP(npop);
        n = TOP(-1);
        GOIND(fk);
      }
      /* Specialize the common 1- and 2-field constructors (e.g., Just, (:), (,)):
       * nn == 1 avoids the allocation check and loop, and nn == 2 uses GOAP2_P to
       * push the constructed AP node directly onto the stack instead of writing it
       * out and immediately unwinding it back from memory. */
      if (nn == 1) {
        fk = ARG(TOP(fidx));
        x = ARG(TOP(0));
        POP(npop);
        n = TOP(-1);
        GOAP(fk, x);
      }
      if (nn == 2) {
        struct node *p0;
        ALLOC1(p0);
        fk = ARG(TOP(fidx));
        x = ARG(TOP(0));
        y = ARG(TOP(1));
        POP(npop);
        n = TOP(-1);
        GOAP2_P(p0, fk, x, y);
      }
      /* Build fk e1 ... e{n-1}, and overwrite the case redex with (fk e1 ... e{n-1}) en.
       * This allocates n-1 nodes, and needs no indirection. */
#if BUMP_ALLOC
      if (UNLIKELY(bump_end - bump_ptr < nn - 1)) {
        fk = ev_constr_alloc(stack_ptr, bump_ptr, bump_end, fidx, nn);
        EV_LOAD_BUMP();
      } else {
        fk = ARG(TOP(fidx));
        for (int i = 0; i < nn - 1; i++)
          fk = mk_ap_node(bump_ptr++, fk, ARG(TOP(i)));
      }
#else
      GCCHECK(nn - 1);
      fk = ARG(TOP(fidx));
      for (int i = 0; i < nn - 1; i++)
        fk = new_ap(fk, ARG(TOP(i)));
#endif
      NODEPTR en = ARG(TOP(nn - 1));
      POP(npop);
      n = TOP(-1);
      GOAP(fk, en);
    }

  default:
    {
      /* Every other primitive */
      EV_SAVE();
      struct prim_res pr = EVALPRIMOP(n, tag, stk);
      EV_LOAD();
      n = pr.n;
      if (pr.next == PRIM_TOP)
        goto top;
      goto ret;
    }
  }


 ret:
  if (stack_ptr != stk) {
    // In this case, n was an AP that got pushed and potentially
    // updated.
    uvalue_t xu, yu, ru;
    NODEPTR p;
    NODEPTR top0 = TOP(0);

    if (!is_VIRT(top0))
      goto ret_done;
    tag = get_VIRT_TAG(top0);
    switch (tag) {
    case T_BININT2:
      n = ARG(TOP(1));
      TOP(0) = combBININT1;
      goto top;

    case T_BININT1:
      /* First argument */
#if SANITY
      if (!is_DATA_NODE(n, D_INT))
        ERR("BININT 0");
#endif  /* SANITY */
    binint1:
      xu = (uvalue_t)GETVALUE(n);
      /* Second argument */
      y = ARG(TOP(2));
      while (is_DATA_NODE(y, D_IND))
        y = ARG(y);
#if SANITY
      if (!is_DATA_NODE(y, D_INT))
        ERR("BININT 1");
#endif  /* SANITY */
      yu = (uvalue_t)GETVALUE(y);
      p = FUN(TOP(1));
      POP(3);
      n = TOP(-1);
      while (is_DATA_NODE(p, D_IND))
        p = ARG(p);
      if (!is_VIRT(p))
        ERR("BININT");
      switch (get_VIRT_TAG(p)) {
      case T_ADD:   ADD_OVERFLOW(value_t, ru, xu, yu, EV_RAISE); break;
      case T_SUB:   SUB_OVERFLOW(value_t, ru, xu, yu, EV_RAISE); break;
      case T_MUL:   MUL_OVERFLOW(value_t, ru, xu, yu, EV_RAISE); break;
      case T_SUBR:  SUB_OVERFLOW(value_t, ru, yu, xu, EV_RAISE); break;
      case T_QUOT:  if (yu == 0)
                      EV_RAISE(exn_dividebyzero);
                    else if ((value_t)xu == VALUE_MIN && (value_t)yu == -1)
                      EV_RAISE(exn_overflow);
                    else
                      ru = (uvalue_t)((value_t)xu / (value_t)yu);
                    break;
      case T_REM:   if (yu == 0)
                      EV_RAISE(exn_dividebyzero);
                    else if ((value_t)yu == -1) /* MIN % -1 traps on x86 */
                      ru = 0;
                    else
                      ru = (uvalue_t)((value_t)xu % (value_t)yu);
                    break;
      case T_UADD:  ru = xu + yu; break;
      case T_USUB:  ru = xu - yu; break;
      case T_UMUL:  ru = xu * yu; break;
      case T_USUBR: ru = yu - xu; break;
      case T_UQUOT: if (yu == 0)
                      EV_RAISE(exn_dividebyzero);
                    else
                      ru = xu / yu;
                    break;
      case T_UREM:  if (yu == 0)
                      EV_RAISE(exn_dividebyzero);
                    else
                      ru = xu % yu;
                    break;
      case T_AND:   ru = xu & yu; break;
      case T_OR:    ru = xu | yu; break;
      case T_XOR:   ru = xu ^ yu; break;
      case T_SHL:   ru = xu << yu; break;
      case T_SHR:   ru = xu >> yu; break;
      case T_ASHR:  ru = (uvalue_t)((value_t)xu >> yu); break;

      case T_EQ:    GOBOOL(xu == yu);
      case T_NE:    GOBOOL(xu != yu);
      case T_ULT:   GOBOOL(xu <  yu);
      case T_ULE:   GOBOOL(xu <= yu);
      case T_UGT:   GOBOOL(xu >  yu);
      case T_UGE:   GOBOOL(xu >= yu);
      case T_UCMP:  GOIND(xu <  yu ? combLT   : xu > yu ? combGT : combEQ);
      case T_LT:    GOBOOL((value_t)xu <  (value_t)yu);
      case T_LE:    GOBOOL((value_t)xu <= (value_t)yu);
      case T_GT:    GOBOOL((value_t)xu >  (value_t)yu);
      case T_GE:    GOBOOL((value_t)xu >= (value_t)yu);
      case T_ICMP:  GOIND((value_t)xu <  (value_t)yu ? combLT   : (value_t)xu > (value_t)yu ? combGT : combEQ);

      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("BININT");
      }
      SETINT(n, (value_t)ru);
      goto ret;

    case T_UNINT1:
      /* The argument */
#if SANITY
      if (!is_DATA_NODE(n, D_INT))
        ERR("UNINT 0");
#endif
      xu = (uvalue_t)GETVALUE(n);
      p = FUN(TOP(1));
      POP(2);
      n = TOP(-1);
      while (is_DATA_NODE(p, D_IND))
        p = ARG(p);
      if (!is_VIRT(p))
        ERR("UNINT");
      switch (get_VIRT_TAG(p)) {
      case T_NEG:
#if WANT_OVERFLOW
                       if ((value_t)xu == VALUE_MIN) EV_RAISE(exn_overflow);
#endif
                       ru = -xu; break;
      case T_UNEG:     ru = -xu; break;
      case T_INV:      ru = ~xu; break;
      case T_POPCOUNT: ru = POPCOUNT(xu); break;
      case T_CLZ:      ru = CLZ(xu); break;
      case T_CTZ:      ru = CTZ(xu); break;
      default:
        //fprintf(stderr, "tag=%d\n", get_XTAG(FUN(TOP(0))));
        ERR("UNINT");
      }
      SETINT(n, (value_t)ru);
      goto ret;

#if WANT_INT64
    case T_BININT64_2:
    case T_BININT64_1:
    case T_UNINT64_1:
#endif  /* WANT_INT64 */
#if WANT_FLOAT32
    case T_BINFLT2:
    case T_BINFLT1:
    case T_UNFLT1:
#endif  /* WANT_FLOAT32 */
#if WANT_FLOAT64
    case T_BINDBL2:
    case T_BINDBL1:
    case T_UNDBL1:
#endif  /* WANT_FLOAT64 */
    case T_BINBS2:
    case T_BINBS1:
      {
        EV_SAVE();
        struct prim_res pr = EVALPRIMRET(n, tag);
        EV_LOAD();
        n = pr.n;
        if (pr.next == PRIM_TOP)
          goto top;
        goto ret;
      }

    default:
    ret_done:
      stack_ptr = stk;
      n = TOP(-1);
    }
  }
#if MAXSTACKDEPTH
  cur_c_stack = old_cur_c_stack; /* reset rather than counting down, in case of longjump */
#endif
  //fprintf(stderr, "evali exit stack_ptr=%d\n", (int)stack_ptr);
#if EXTRASANITY
  if (!is_whnf(n))
    ERR("evali: the result is not in WHNF");
#endif
  EV_SAVE();
  return n;
}

/* Back to the global version */
#undef GCCHECK
#define GCCHECK(n) gc_check((n))

static char *progname = "?";

NORETURN void
die_exn(NODEPTR exn)
{
  /* No handler:
   * First convert the exception to a string by calling displaySomeException.
   * The display function compiles to combShowExn, so we need to build
   * (combShowExn exn) and evaluate it.
   */
  NODEPTR x;
  const char *msg;

  in_raise = true;

  if (get_XTAG(exn) == D_INT) {
    /* This is the special hack for RTS generated exception, represented by a D_INT */
    enum rts_exn eexn = GETVALUE(exn);
    static const char *msgs[] =
      { "stack overflow",
        "heap overflow",
        "thread killed",
        "user interrupt",
        "DivideByZero",
        "blocked MVar",
        "blocked STM",
        "arithmetic overflow",
        "Serialize",
        "Deserialize",
        "<<loop>>",
      };
    if (eexn < 0 || eexn >= exn_last) {
      msg = "unknown";
    } else {
      msg = msgs[eexn];
    }
  } else {
    /* just overwrite the top stack element, we don't need it */
    CLEARSTK();
    GCCHECKSAVE(exn, 1);
    PUSH(new_ap(combShowExn, exn));  /* TOP(0) = (combShowExn exn) */
    x = evali(TOP(0));               /* evaluate it */
    msg = evalstring(x).bs_array;    /* and convert to a C string */
    POP(1);
  }
#if WANT_STDIO
  /* A horrible hack until we get proper exceptions */
  if (strcmp(msg, "ExitSuccess") == 0) {
    EXIT(0);
  } else if (strncmp(msg, "ExitFailure ", 12) == 0) {
    EXIT(atoi(msg + 12));
  } else {
    fprintf(stderr, "\n%s: uncaught exception: %s\n", progname, msg);
    EXIT(1);
  }
#else  /* WANT_STDIO */
  ERR1("mhs error: %s", msg);
#endif  /* WANT_STDIO */
}

#if WANT_ARGS
heapoffs_t
memsize(const char *p)
{
  heapoffs_t n = 0;
  while (isdigit((unsigned char)*p)) {
    n = n * 10 + (heapoffs_t)(*p - '0');
    p++;
  }
  switch (*p) {
  case 'k': case 'K': n *= 1000; break;
  case 'm': case 'M': n *= 1000000; break;
  case 'g': case 'G': n *= 1000000000; break;
  default: break;
  }
  return n;
}
#endif

extern const uint8_t *combexpr;
extern const int combexprlen;

#if WANT_TICK
int dump_ticks = 0;
#endif

#if WANT_ARGS
  #if WANT_STDIO
  #define MHS_INIT_ARGS(a,b,c,d) mhs_init_args(a,b,c,d)
  #else
  #define MHS_INIT_ARGS(a,b,c,d) mhs_init_args(a,b)
  #endif
#else  /* WANT_ARGS */
  #if WANT_STDIO
  #define MHS_INIT_ARGS(a,b,c,d) mhs_init_args(c,d)
  #else
  #define MHS_INIT_ARGS(a,b,c,d) mhs_init_args()
  #endif
#endif  /* WANT_ARGS */

NODEPTR
MHS_INIT_ARGS(
              int argc, char **argv,
              char **outnamep,
              size_t *file_sizep
)
{
  NODEPTR prog;
#if WANT_ARGS
  char *inname = 0;
  char **av;
  char **gargv;
  int gargc;
  int inrts;
#endif

#if 0
  /* MINGW doesn't do buffering right */
  setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
  setvbuf(stderr, NULL, _IONBF, BUFSIZ);
#endif

#ifdef INITIALIZATION
  main_setup(); /* Do platform specific start-up. */
#endif

#ifdef CLOCK_INIT
  CLOCK_INIT();
#endif

#if WANT_SIGINT
  {
    (void)signal(SIGINT, handle_sigint);
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
  }
#endif

  heap_size = HEAP_CELLS;       /* number of heap cells */
  stack_size = STACK_SIZE;      /* number of stack slots */

#if WANT_ARGS
  progname = argv[0];
  argc--, argv++;
  gargv = argv;
  for (av = argv, inrts = 0; argc--; argv++) {
    char *p = *argv;
    if (inrts) {
      if (strcmp(p, "-RTS") == 0) {
        inrts = 0;
      } else {
        if (strcmp(p, "-v") == 0)
          verbose++;
#if WANT_TICK
        else if (strcmp(p, "-T") == 0)
          dump_ticks = 1;
#endif
        else if (strncmp(p, "-H", 2) == 0)
          heap_size = memsize(&p[2]);
        else if (strncmp(p, "-K", 2) == 0)
          stack_size = memsize(&p[2]);
        else if (strncmp(p, "-r", 2) == 0)
          inname = &p[2];
#if WANT_STDIO
        else if (strncmp(p, "-o", 2) == 0)
          *outnamep = &p[2];
        else if (strcmp(p, "-B") == 0)
          gcbell++;
#endif  /* WANT_STDIO */
        else
          ERR("Usage: eval [+RTS [-v] [-B] [-T] [-Hheap-size] [-Kstack-size] [-rFILE] [-oFILE] -RTS] arg ...");
      }
    } else {
      if (strcmp(p, "+RTS") == 0) {
        inrts = 1;
      } else {
        *av++ = p;
      }
    }
  }
  gargc = av - gargv;

  if (inname == 0)
    inname = "out.comb";
#endif

  init_nodes();
  NODEPTR *stack_area = mmalloc(sizeof(NODEPTR) * stack_size);
  stack_bot = stack_area + stack_size;   /* the stack grows down from here */
  stack_limit = stack_area + 1;
  CLEARSTK();
  init_stableptr();

  num_reductions = 0;

#if WANT_ARGS
  /* Initialize an IORef (i.e., single element IOArray
   * to contain the list of program arguments.
   * The 0th element is the program name, and the rest
   * are the non RTS arguments.
   */
  {
    NODEPTR n;
    /* No GC checks, the heap is empty. */
    n = mkNil();
    for(int i = gargc-1; i >= 0; i--) {
      n = mkCons(mkStringC(gargv[i]), n);
    }
    n = mkCons(mkStringC(progname), n);
    argarray = arr_alloc(1, n);      /* An IORef contains a single element array */
    argarray->permanent = true;         /* never GC the arguments, because a T_IO_GETARGREF can reach argarray */
  }
#endif  /* WANT_ARGS */

  {
    /* Read the combinator code. */
    BFILE *bf;
    if (combexpr) {
      /* The code is in memory, create a memore buffer BFILE */
      bf = openb_rd_mem(combexpr, combexprlen);
#if WANT_STDIO
      *file_sizep = combexprlen;
#endif
    } else {
#if WANT_STDIO & WANT_ARGS
      /* Open a regular file */
      FILE *f = fopen(inname, "rb");
      if (!f)
        ERR1("file not found %s", inname);
      fseek(f, 0, SEEK_END);
      *file_sizep = ftell(f);   /* find its size */
      rewind(f);

      bf = add_FILE(f);
#else
      ERR("no stdio");
#endif
    }
    int c = getb(bf);
#if WANT_BASE64
    /* Compressed combinators start with a 'z', otherwise 'v' (for version) */
    if (c != 'z' && c != 'v') {
      /* Neither z nor v, assume base64 encoded */
      ungetb(c, bf);
      bf = add_base64_decoder(bf);
      c = getb(bf);
    }
#endif
    if (c == 'z') {
#if WANT_LZMA
      /* add LZMA decompressor transducer */
      bf = add_lzma_decompressor(bf);
#else
      ERR("LZMA compression not supported");
#endif
    } else {
      /* put it back, we need it */
      ungetb(c, bf);
    }
    prog = parse_top(bf, xffe_table);
    if (is_NIL(prog))
      ERR("deser failed");
    closeb(bf);
  }

  /* GC unused stuff, nice for -o */
  PUSH(prog);
  want_gc_red = 1;
  gc();
  gc();                         /* this finds some more GC reductions */
  want_gc_red = 0;              /* can be enabled, but it is rarely a win */
  prog = POPTOP();
  return prog;
}

void
mhs_init(void)
{
  char *args[2] = { "<mhs_init>", 0 };
  char *outname;
  size_t file_size;
  (void)MHS_INIT_ARGS(1, args, &outname, &file_size);
}

int
mhs_main(int argc, char **argv)
{
  NODEPTR prog;
  char *outname = 0;
  size_t file_size = 0;
#if WANT_KPERF
  counter_t instrs;
#endif  /* WANT_KPERF */
  boot_time = GETTIMEMICRO();

  prog = MHS_INIT_ARGS(argc, argv, &outname, &file_size);

#if WANT_STDIO
  heapoffs_t start_size = num_marked;
  if (outname) {
    /* Save GCed file (smaller), and exit. */
    FILE *out = fopen(outname, "wb");
    if (!out)
      ERR1("cannot open output file %s", outname);
    struct BFILE *bf = add_FILE(out);
    printb(bf, prog, true);
    closeb(bf);
    EXIT(0);
  }
  if (verbose > 2) {
    pp(stdout, prog);
  }
#endif
  run_time -= GETTIMEMILLI();

#if 0
  topnode = &prog;
#endif
#if WANT_KPERF
  if (!start_kperf()) {
    // ERR("kperf init failed");
#if WANT_STDIO
    fprintf(stderr, "start_kperf() failed, ignored\n");
#endif
  }
#endif  /* WANT_KPERF */
  start_exec(prog);
#if WANT_STDIO
  /* Flush standard handles in case there is some BFILE buffering */
  flushb((BFILE*)FORPTR(comb_stdout)->payload.bs_array);
  flushb((BFILE*)FORPTR(comb_stderr)->payload.bs_array);
#endif  /* WANT_STDIO */
  gc();                      /* Run finalizers */
#if WANT_KPERF
  instrs = end_kperf();
#endif  /* WANT_KPERF */
  run_time += GETTIMEMILLI();

#if WANT_STDIO
  if (verbose) {
    if (verbose > 1) {
      PRINT("node size=%"PRIheap", heap size bytes=%"PRIheap"\n", (heapoffs_t)NODE_SIZE, heap_size * NODE_SIZE);
    }
    setlocale(LC_NUMERIC, "en_US");  /* Make %' work on platforms that support it */
    PRINT("%"PCOMMA"15"PRIheap" combinator file size\n", (heapoffs_t)file_size);
    PRINT("%"PCOMMA"15"PRIheap" cells at start\n", start_size);
    PRINT("%"PCOMMA"15"PRIheap" cells heap size (%"PCOMMA""PRIheap" bytes)\n", heap_size, heap_size * NODE_SIZE);
    PRINT("%"PCOMMA"15"PRIcounter" cells allocated (%"PCOMMA".1f Mbyte/s)\n", NUM_ALLOC, NUM_ALLOC * NODE_SIZE / ((double)run_time / 1000) / 1000000);
    PRINT("%"PCOMMA"15"PRIcounter" GCs\n", num_gc);
    PRINT("%"PCOMMA"15"PRIcounter" max cells used\n", max_num_marked);
    PRINT("%"PCOMMA"15"PRIcounter" reductions (%"PCOMMA".1f Mred/s)\n", num_reductions, num_reductions / ((double)run_time / 1000) / 1000000);
    PRINT("%"PCOMMA"15"PRIcounter" yields (%"PCOMMA""PRIcounter" resched)\n", num_yield, num_resched);
    PRINT("%"PCOMMA"15"PRIcounter" array alloc\n", num_arr_alloc);
    PRINT("%"PCOMMA"15"PRIcounter" array free\n", num_arr_free);
    PRINT("%"PCOMMA"15"PRIcounter" foreign alloc\n", num_fin_alloc);
    PRINT("%"PCOMMA"15"PRIcounter" foreign free\n", num_fin_free);
    PRINT("%"PCOMMA"15"PRIcounter" bytestring alloc (max %"PCOMMA""PRIcounter")\n", num_bs_alloc, num_bs_alloc_max);
    PRINT("%"PCOMMA"15"PRIcounter" bytestring alloc bytes (max %"PCOMMA""PRIcounter")\n", num_bs_bytes, num_bs_inuse_max);
    PRINT("%"PCOMMA"15"PRIcounter" bytestring free\n", num_bs_free);
    PRINT("%"PCOMMA"15"PRIcounter" thread create\n", num_thread_create-1);
    PRINT("%"PCOMMA"15"PRIcounter" thread reap\n", num_thread_reap);
    PRINT("%"PCOMMA"15"PRIcounter" stableptr alloc\n", num_stable_alloc);
    PRINT("%"PCOMMA"15"PRIcounter" stableptr free\n", num_stable_free);
    PRINT("%"PCOMMA"15"PRIcounter" weakptr alloc\n", num_new_weak);
    PRINT("%"PCOMMA"15"PRIcounter" weakptr free\n", num_gc_weak);
    {
      /* XXX temporary */
      size_t bytes = max_case_arities * sizeof(struct case_arity);
      for (uint32_t i = 0; i < num_case_arities; i++)
        bytes += case_arities[i].m * (case_arities[i].tags ? 1 + sizeof(uint16_t) : 1);
      PRINT("%"PCOMMA"15d case arity entries (%"PCOMMA"d bytes)\n", (int)num_case_arities, (int)bytes);
    }
#if MAXSTACKDEPTH
    PRINT("%"PCOMMA"15d max stack depth\n", (int)max_stack_depth);
    PRINT("%"PCOMMA"15d max C stack depth\n", (int)max_c_stack);
#endif
    // PRINT("%"PCOMMA"15d avg gc stack depth\n", (int)(gc_tot / num_gc));
    // PRINT("%"PCOMMA"15"PRIcounter" max mark depth\n", max_mark_depth);
    PRINT("%15.2fs total expired time\n", (double)run_time / 1000);
    PRINT("%15.2fs gc expired time = %3.1f%% (%.2fs mark + %.2fs scan)\n",
          (double)(gc_mark_time + gc_scan_time) / 1000,
          (double)(gc_mark_time + gc_scan_time) / (double)run_time * 100,
          (double)gc_mark_time / 1000,
          (double)gc_scan_time / 1000);
#if WANT_KPERF
    if (instrs > 0) {
      PRINT("%"PCOMMA"15"PRIcounter" instructions (%.1f instr/red)\n", instrs, (double)instrs / (double)num_reductions);
    }
#endif  /* WANT_KPERF */
#if GCRED
    PRINT(" GC reductions A=%"PRIcounter", K=%"PRIcounter", I=%"PRIcounter", int=%"PRIcounter", flip=%"PRIcounter","
          " BI=%"PRIcounter", BxI=%"PRIcounter", C'BxI=%"PRIcounter", CC=%"PRIcounter", C'I=%"PRIcounter", C'BBCP=%"PRIcounter"\n",
          red_a, red_k, red_i, red_int, red_flip, red_bi, red_bxi, red_ccbi, red_cc, red_cci, red_ccbbcp);
    PRINT(" special reductions B'=%"PRIcounter" K4=%"PRIcounter" K3=%"PRIcounter" K2=%"PRIcounter" C'B=%"PRIcounter", Z=%"PRIcounter", R=%"PRIcounter"\n",
          red_bb, red_k4, red_k3, red_k2, red_ccb, red_z, red_r);
#endif
  }
#endif  /* WANT_STDIO */

#if WANT_TICK
  if (dump_ticks) {
    dump_tick_table(stdout);
  }
#endif

#ifdef TEARDOWN
  main_teardown(); /* do some platform specific teardown */
#endif
  EXIT(0);
}

#if WANT_MD5
#include "md5.c"
#endif  /* WANT_MD5 */

#if WANT_LZ77
#include "lz77.c"
#endif

/***************************/
/* Foreign export helpers  */

/* The ffe_value in the C code is a struct node*, but it is really a NODEPTR,
 * so it can be a virtual pointer. */
void
ffe_push(struct node *p)
{
  NODEPTR n;
  n.node_ptr = p;
  PUSH(n);
}

void
ffe_pop(void)
{
  POP(1);
}

/* Allocate a new node (will be overwritten) */
stackptr_t
ffe_alloc(void)
{
  PUSH(alloc_node(D_DBL));
  return STACK_INDEX(stack_ptr);
}

void
ffe_apply(void)
{
  NODEPTR arg = POPTOP();
  NODEPTR fun = POPTOP();
  PUSH(new_ap(fun, arg));
}

/* For stand-alone exported functions this is called with the threading inactive.
 * On the other hand, if a 'foreign import' calls back to a 'foreign export' the
 * threading is alread running.
 */
/* XXX This is not quite right.  The surrounding mhs_to_xxx should be in the thread. */
stackptr_t
ffe_eval(void)
{
  if (main_thread) {
    /* threading active, run on current stack */
    (void)evali(TOP(0));
  } else {
    /* Start up the threading to evaluate the node.
     * start_exec() runs an IO action, i.e., it applies its argument to World.
     * The node is a value, not an IO action, so pass (K node), which
     * ignores World.  Applying the node itself to World would give it an
     * extra argument, which a constructor cannot take.
     */
    GCCHECK(2);                 /* K node, and the World application in start_exec */
    start_exec(new_ap(combK, TOP(0)));
  }
  /* The mhs_to_xxx functions bizarrely return the ARG(TOP(n+1)) value.
   * The wrapper will call with n=-1, so we need to put the result at ARG(TOP(0))
   */
  GCCHECK(1);
  TOP(0) = new_ap(combI, TOP(0));
  return STACK_INDEX(stack_ptr);
}

stackptr_t
ffe_exec(void)
{
  NODEPTR n = POPTOP();
  PUSH(new_ap(combPERFORMIO, n));
  return ffe_eval();
}

/* apply_sp :: StablePtr (Ptr a -> IO (Ptr b)) -> Ptr a -> IO (Ptr b) */
void *
apply_sp(uvalue_t sp, void *arg)
{
  GCCHECK(3);
  NODEPTR f = deref_stableptr(sp);
  NODEPTR a = alloc_node(D_PTR);
  PTR(a) = arg;
  PUSH(new_ap(combPERFORMIO, new_ap(f, a)));
  void *r = evalptr(TOP(0));
  POP(1);
  return r;
}

/*********************/
/* FFI adapters      */

#define MHS_FROM(name, set, type) \
from_t \
name(stackptr_t stk, int n, type x) \
{ \
  NODEPTR r = TOP(0);           /* The pre-allocated cell for the result, */ \
  set(r, x);                    /* Put result in pre-allocated cell. */ \
  return n;                     /* return arity */ \
}
#if WANT_FLOAT64
MHS_FROM(mhs_from_Double, SETDBL, flt64_t);
#endif
#if WANT_FLOAT32
MHS_FROM(mhs_from_Float, SETFLT, flt32_t);
#endif
MHS_FROM(mhs_from_Int, SETINT, value_t);
/* A Bool is the C_0_0 (False) or C_1_0 (True) constructor, not an int node. */
#define SETBOOL(n, x) SETIND((n), (x) ? combTrue : combFalse)
MHS_FROM(mhs_from_Bool, SETBOOL, value_t);
#if WANT_INT64
MHS_FROM(mhs_from_Int64, SETINT64, int64_t);
#endif
MHS_FROM(mhs_from_Word, SETINT, uvalue_t);
MHS_FROM(mhs_from_Word8, SETINT, uvalue_t);
#if WANT_INT64
MHS_FROM(mhs_from_Word64, SETINT64, uint64_t);
#endif
MHS_FROM(mhs_from_Ptr, SETPTR, void*);
MHS_FROM(mhs_from_ForPtr, SETFORPTR, struct forptr *);
MHS_FROM(mhs_from_FunPtr, SETFUNPTR, HsFunPtr);
MHS_FROM(mhs_from_CChar, SETINT, char);
MHS_FROM(mhs_from_CSChar, SETINT, signed char);
MHS_FROM(mhs_from_CUChar, SETINT, unsigned char);
MHS_FROM(mhs_from_CShort, SETINT, short);
MHS_FROM(mhs_from_CUShort, SETINT, unsigned short);
MHS_FROM(mhs_from_CInt, SETINT, int);
MHS_FROM(mhs_from_CUInt, SETINT, unsigned int);
MHS_FROM(mhs_from_CLong, SETINT, long);
MHS_FROM(mhs_from_CULong, SETINT, unsigned long);
#if WANT_INT64
/* CLLong and CULLong are newtypes of Int64 and Word64 */
MHS_FROM(mhs_from_CLLong, SETINT64, long long);
MHS_FROM(mhs_from_CULLong, SETINT64, unsigned long long);
#else
MHS_FROM(mhs_from_CLLong, SETINT, long long);
MHS_FROM(mhs_from_CULLong, SETINT, unsigned long long);
#endif
MHS_FROM(mhs_from_CSize, SETINT, size_t);
#if WANT_TIME
MHS_FROM(mhs_from_CTime, SETINT, time_t);
#endif
// MHS_FROM(mhs_from_CSSize, SETINT, ssize_t);
MHS_FROM(mhs_from_CIntPtr, SETINT, intptr_t);
MHS_FROM(mhs_from_CUIntPtr, SETINT, uintptr_t);
from_t
mhs_from_Unit(stackptr_t stk, int n)
{
  POP(1);                       /* return value cell */
  PUSH(combUnit);               /* push unit instead */
  return n;
}

#define MHS_TO(name, eval, type) \
type name(stackptr_t stk, int n) \
{ \
  return eval(ARG(TOP(n+1)));                /* The stack has a reserved cell on top of the arguments */ \
}
#if WANT_FLOAT32
MHS_TO(mhs_to_Float, evalflt, flt32_t);
#endif
#if WANT_FLOAT64
MHS_TO(mhs_to_Double, evaldbl, flt64_t);
#endif
MHS_TO(mhs_to_Int, evalint, value_t);
MHS_TO(mhs_to_Bool, evalbool, value_t);
#if WANT_INT64
MHS_TO(mhs_to_Int64, evalint64, int64_t);
#endif
MHS_TO(mhs_to_Word, evalint, uvalue_t);
MHS_TO(mhs_to_Word8, evalint, uint8_t);
#if WANT_INT64
MHS_TO(mhs_to_Word64, evalint64, uint64_t);
#endif
MHS_TO(mhs_to_Ptr, evalptr, void*);
MHS_TO(mhs_to_FunPtr, evalfunptr, HsFunPtr);
MHS_TO(mhs_to_CChar, evalint, char);
MHS_TO(mhs_to_CSChar, evalint, signed char);
MHS_TO(mhs_to_CUChar, evalint, unsigned char);
MHS_TO(mhs_to_CShort, evalint, short);
MHS_TO(mhs_to_CUShort, evalint, unsigned short);
MHS_TO(mhs_to_CInt, evalint, int);
MHS_TO(mhs_to_CUInt, evalint, unsigned int);
MHS_TO(mhs_to_CLong, evalint, long);
MHS_TO(mhs_to_CULong, evalint, unsigned long);
#if WANT_INT64
/* CLLong and CULLong are newtypes of Int64 and Word64 */
MHS_TO(mhs_to_CLLong, evalint64, long long);
MHS_TO(mhs_to_CULLong, evalint64, unsigned long long);
#else
MHS_TO(mhs_to_CLLong, evalint, long long);
MHS_TO(mhs_to_CULLong, evalint, unsigned long long);
#endif
MHS_TO(mhs_to_CSize, evalint, size_t);
#if WANT_TIME
MHS_TO(mhs_to_CTime, evalint, time_t);
#endif
// MHS_TO(mhs_to_CSSize, evalint, ssize_t);
MHS_TO(mhs_to_CIntPtr, evalint, intptr_t);
MHS_TO(mhs_to_CUIntPtr, evalint, uintptr_t);

/* The rest of this file was generated by the compiler, with some minor edits with #if. */
from_t mhs_GETRAW(int s) { return  mhs_from_Int(s, 0, GETRAW()); }
from_t mhs_GETTIMEMICRO(int s) { return  mhs_from_Int(s, 0, GETTIMEMICRO()); }
from_t mhs_GETBOOTTIMEMICRO(int s) { return  mhs_from_Int(s, 0, boot_time); }
#if WANT_MATH
#if WANT_FLOAT64
from_t mhs_acos(int s) { return mhs_from_Double(s, 1, acos(mhs_to_Double(s, 0))); }
from_t mhs_asin(int s) { return mhs_from_Double(s, 1, asin(mhs_to_Double(s, 0))); }
from_t mhs_atan(int s) { return mhs_from_Double(s, 1, atan(mhs_to_Double(s, 0))); }
from_t mhs_atan2(int s) { return mhs_from_Double(s, 2, atan2(mhs_to_Double(s, 0), mhs_to_Double(s, 1))); }
from_t mhs_cos(int s) { return mhs_from_Double(s, 1, cos(mhs_to_Double(s, 0))); }
from_t mhs_exp(int s) { return mhs_from_Double(s, 1, exp(mhs_to_Double(s, 0))); }
from_t mhs_log(int s) { return mhs_from_Double(s, 1, log(mhs_to_Double(s, 0))); }
from_t mhs_sin(int s) { return mhs_from_Double(s, 1, sin(mhs_to_Double(s, 0))); }
from_t mhs_sqrt(int s) { return mhs_from_Double(s, 1, sqrt(mhs_to_Double(s, 0))); }
from_t mhs_tan(int s) { return mhs_from_Double(s, 1, tan(mhs_to_Double(s, 0))); }
from_t mhs_scalbn(int s) { return mhs_from_Double(s, 2, scalbn(mhs_to_Double(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_pow(int s) { return mhs_from_Double(s, 2, pow(mhs_to_Double(s, 0), mhs_to_Double(s, 1))); }
#endif  /* WANT_FLOAT64 */
#if WANT_FLOAT32
from_t mhs_acosf(int s) { return mhs_from_Float(s, 1, acosf(mhs_to_Float(s, 0))); }
from_t mhs_asinf(int s) { return mhs_from_Float(s, 1, asinf(mhs_to_Float(s, 0))); }
from_t mhs_atanf(int s) { return mhs_from_Float(s, 1, atanf(mhs_to_Float(s, 0))); }
from_t mhs_atan2f(int s) { return mhs_from_Float(s, 2, atan2f(mhs_to_Float(s, 0), mhs_to_Float(s, 1))); }
from_t mhs_cosf(int s) { return mhs_from_Float(s, 1, cosf(mhs_to_Float(s, 0))); }
from_t mhs_expf(int s) { return mhs_from_Float(s, 1, expf(mhs_to_Float(s, 0))); }
from_t mhs_logf(int s) { return mhs_from_Float(s, 1, logf(mhs_to_Float(s, 0))); }
from_t mhs_sinf(int s) { return mhs_from_Float(s, 1, sinf(mhs_to_Float(s, 0))); }
from_t mhs_sqrtf(int s) { return mhs_from_Float(s, 1, sqrtf(mhs_to_Float(s, 0))); }
from_t mhs_tanf(int s) { return mhs_from_Float(s, 1, tanf(mhs_to_Float(s, 0))); }
from_t mhs_scalbnf(int s) { return mhs_from_Float(s, 2, scalbnf(mhs_to_Float(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_powf(int s) { return mhs_from_Float(s, 2, powf(mhs_to_Float(s, 0), mhs_to_Float(s, 1))); }
#endif  /* WANT_FLOAT32 */
#endif  /* WANT_MATH */

#if defined(__EMSCRIPTEN__)
from_t mhs_js_debug(int s) { EM_ASM({ console.log(UTF8ToString($0)) }, mhs_to_Ptr(s, 0)); return mhs_from_Unit(s, 1); }
from_t mhs_js_eval_run(int s) { EM_ASM({ eval(UTF8ToString($0)) }, mhs_to_Ptr(s, 0)); return mhs_from_Unit(s, 1); }
from_t mhs_js_eval_call(int s) { return mhs_from_Ptr(s, 1, EM_ASM_PTR({ return stringToNewUTF8(JSON.stringify(eval(UTF8ToString($0)))) }, mhs_to_Ptr(s, 0))); }
from_t mhs_js_set_haskellCallback(int s) { EM_ASM({ _haskellCallback = $0 }, mhs_to_Int(s, 0)); return mhs_from_Unit(s, 1); }
#endif

#if WANT_STDIO
from_t mhs_add_FILE(int s) { return mhs_from_Ptr(s, 1, add_FILE(mhs_to_Ptr(s, 0))); }
from_t mhs_putchar(int s) { putchar(mhs_to_Int(s, 0)); return mhs_from_Unit(s, 1); } /* for debugging */
from_t mhs_fopen(int s) { return mhs_from_Ptr(s, 2, fopen(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1))); }
from_t mhs_system(int s) { return mhs_from_Int(s, 1, system(mhs_to_Ptr(s, 0))); }
from_t mhs_tmpname(int s) { return mhs_from_Ptr(s, 2, TMPNAME(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1))); }
from_t mhs_remove(int s) { return mhs_from_Int(s, 1, remove(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_STDIO */
#if WANT_FD
from_t mhs_add_fd(int s) { return mhs_from_Ptr(s, 1, add_fd(mhs_to_Int(s, 0))); }
from_t mhs_open(int s) { return mhs_from_Int(s, 3, open(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1), mhs_to_Int(s, 2))); }
#endif  /* WANT_FD */
#if WANT_BUF
from_t mhs_add_buf(int s) { return mhs_from_Ptr(s, 2, add_buf(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
#endif  /* WANT_BUF */
#if WANT_CRLF
from_t mhs_add_crlf(int s) { return mhs_from_Ptr(s, 1, add_crlf(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_CRLF */
#if WANT_UTF8
from_t mhs_add_utf8(int s) { return mhs_from_Ptr(s, 1, add_utf8(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_UTF8 */
#if WANT_BASE64
from_t mhs_add_base64_encoder(int s) { return mhs_from_Ptr(s, 1, add_base64_encoder(mhs_to_Ptr(s, 0))); }
from_t mhs_add_base64_decoder(int s) { return mhs_from_Ptr(s, 1, add_base64_decoder(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_BASE64 */
from_t mhs_closeb(int s) { closeb(mhs_to_Ptr(s, 0)); return mhs_from_Unit(s, 1); }
from_t mhs_addr_closeb(int s) { return mhs_from_FunPtr(s, 0, (HsFunPtr)&closeb); }
from_t mhs_flushb(int s) { flushb(mhs_to_Ptr(s, 0)); return mhs_from_Unit(s, 1); }
from_t mhs_getb(int s) { return mhs_from_Int(s, 1, getb(mhs_to_Ptr(s, 0))); }
from_t mhs_putb(int s) { putb(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_ungetb(int s) { ungetb(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_openwrmem(int s) { return mhs_from_Ptr(s, 0, openb_wr_mem()); }
from_t mhs_openrdmem(int s) { return mhs_from_Ptr(s, 2, openb_rd_mem(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_getmem(int s) { get_mem(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2));  return mhs_from_Unit(s, 3); }
from_t mhs_readb(int s) { return mhs_from_Int(s, 3, readb(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1), mhs_to_Ptr(s, 2))); }
from_t mhs_writeb(int s) { return mhs_from_Int(s, 3, writeb(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1), mhs_to_Ptr(s, 2))); }
from_t mhs_isattyb(int s) { return mhs_from_Int(s, 1, isattyb(mhs_to_Ptr(s, 0))); }
#if WANT_INT64
from_t mhs_filesizeb(int s) { return mhs_from_Int64(s, 1, filesizeb(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_INT64 */

#if WANT_MD5
from_t mhs_md5Array(int s) { md5Array(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_md5BFILE(int s) { md5BFILE(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_md5String(int s) { md5String(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
#endif  /* WANT_MD5 */

#if WANT_LZ77
from_t mhs_add_lz77_compressor(int s) { return mhs_from_Ptr(s, 1, add_lz77_compressor(mhs_to_Ptr(s, 0))); }
from_t mhs_add_lz77_decompressor(int s) { return mhs_from_Ptr(s, 1, add_lz77_decompressor(mhs_to_Ptr(s, 0))); }
from_t mhs_lz77c(int s) { return mhs_from_CSize(s, 3, lz77c(mhs_to_Ptr(s, 0), mhs_to_CSize(s, 1), mhs_to_Ptr(s, 2))); }
#endif  /* WANT_LZ77 */

#if WANT_LZMA
from_t mhs_add_lzma_compressor(int s) { return mhs_from_Ptr(s, 1, add_lzma_compressor(mhs_to_Ptr(s, 0))); }
from_t mhs_add_lzma_decompressor(int s) { return mhs_from_Ptr(s, 1, add_lzma_decompressor(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_LZ77 */

#if WANT_RLE
from_t mhs_add_rle_compressor(int s) { return mhs_from_Ptr(s, 1, add_rle_compressor(mhs_to_Ptr(s, 0))); }
from_t mhs_add_rle_decompressor(int s) { return mhs_from_Ptr(s, 1, add_rle_decompressor(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_RLE */

#if WANT_BWT
from_t mhs_add_bwt_compressor(int s) { return mhs_from_Ptr(s, 1, add_bwt_compressor(mhs_to_Ptr(s, 0))); }
from_t mhs_add_bwt_decompressor(int s) { return mhs_from_Ptr(s, 1, add_bwt_decompressor(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_BWT */

from_t mhs_calloc(int s) { return mhs_from_Ptr(s, 2, CALLOC(mhs_to_CSize(s, 0), mhs_to_CSize(s, 1))); }
from_t mhs_realloc(int s) { return mhs_from_Ptr(s, 2, REALLOC(mhs_to_Ptr(s, 0), mhs_to_CSize(s, 1))); }
from_t mhs_free(int s) { FREE(mhs_to_Ptr(s, 0)); return mhs_from_Unit(s, 1); }
from_t mhs_addr_free(int s) { return mhs_from_FunPtr(s, 0, (HsFunPtr)&FREE); }
from_t mhs_iswindows(int s) { return mhs_from_Int(s, 0, iswindows()); }
from_t mhs_ismacos(int s) { return mhs_from_Int(s, 0, ismacos()); }
from_t mhs_islinux(int s) { return mhs_from_Int(s, 0, islinux()); }
from_t mhs_ismingw(int s) { return mhs_from_Int(s, 0, ismingw()); }
from_t mhs_malloc(int s) { return mhs_from_Ptr(s, 1, MALLOC(mhs_to_CSize(s, 0))); }
from_t mhs_memcpy(int s) { memcpy(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_CSize(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_strlen(int s) { return mhs_from_CSize(s, 1, strlen(mhs_to_Ptr(s, 0))); }
from_t mhs_strcpy(int s) { strcpy(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_memmove(int s) { memmove(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_CSize(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_peekPtr(int s) { return mhs_from_Ptr(s, 1, peekPtr(mhs_to_Ptr(s, 0))); }
from_t mhs_peekWord(int s) { return mhs_from_Word(s, 1, peekWord(mhs_to_Ptr(s, 0))); }
from_t mhs_pokePtr(int s) { pokePtr(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_pokeWord(int s) { pokeWord(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }

from_t mhs_peek_uint8(int s) { return mhs_from_Word(s, 1, peek_uint8(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uint8(int s) { poke_uint8(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_uint16(int s) { return mhs_from_Word(s, 1, peek_uint16(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uint16(int s) { poke_uint16(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_uint32(int s) { return mhs_from_Word(s, 1, peek_uint32(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uint32(int s) { poke_uint32(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }
#if WANT_INT64
from_t mhs_peek_uint64(int s) { return mhs_from_Word64(s, 1, peek_uint64(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uint64(int s) { poke_uint64(mhs_to_Ptr(s, 0), mhs_to_Word64(s, 1)); return mhs_from_Unit(s, 2); }
#endif  /* WANT_INT64 */

from_t mhs_peek_int8(int s) { return mhs_from_Int(s, 1, peek_int8(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_int8(int s) { poke_int8(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_int16(int s) { return mhs_from_Int(s, 1, peek_int16(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_int16(int s) { poke_int16(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_int32(int s) { return mhs_from_Int(s, 1, peek_int32(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_int32(int s) { poke_int32(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1)); return mhs_from_Unit(s, 2); }
#if WANT_INT64
from_t mhs_peek_int64(int s) { return mhs_from_Int64(s, 1, peek_int64(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_int64(int s) { poke_int64(mhs_to_Ptr(s, 0), mhs_to_Int64(s, 1)); return mhs_from_Unit(s, 2); }
#endif  /* WANT_INT64 */
from_t mhs_peek_char(int s) { return mhs_from_CChar(s, 1, peek_char(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_char(int s) { poke_char(mhs_to_Ptr(s, 0), mhs_to_CChar(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_schar(int s) { return mhs_from_CSChar(s, 1, peek_schar(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_schar(int s) { poke_schar(mhs_to_Ptr(s, 0), mhs_to_CSChar(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_uchar(int s) { return mhs_from_CUChar(s, 1, peek_uchar(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uchar(int s) { poke_uchar(mhs_to_Ptr(s, 0), mhs_to_CUChar(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_short(int s) { return mhs_from_CShort(s, 1, peek_short(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_short(int s) { poke_short(mhs_to_Ptr(s, 0), mhs_to_CShort(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_ushort(int s) { return mhs_from_CUShort(s, 1, peek_ushort(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_ushort(int s) { poke_ushort(mhs_to_Ptr(s, 0), mhs_to_CUShort(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_int(int s) { return mhs_from_CInt(s, 1, peek_int(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_int(int s) { poke_int(mhs_to_Ptr(s, 0), mhs_to_CInt(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_uint(int s) { return mhs_from_CUInt(s, 1, peek_uint(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_uint(int s) { poke_uint(mhs_to_Ptr(s, 0), mhs_to_CUInt(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_long(int s) { return mhs_from_CLong(s, 1, peek_long(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_long(int s) { poke_long(mhs_to_Ptr(s, 0), mhs_to_CLong(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_ulong(int s) { return mhs_from_CULong(s, 1, peek_ulong(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_ulong(int s) { poke_ulong(mhs_to_Ptr(s, 0), mhs_to_CULong(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_llong(int s) { return mhs_from_CLLong(s, 1, peek_llong(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_llong(int s) { poke_llong(mhs_to_Ptr(s, 0), mhs_to_CLLong(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_ullong(int s) { return mhs_from_CULLong(s, 1, peek_ullong(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_ullong(int s) { poke_ullong(mhs_to_Ptr(s, 0), mhs_to_CULLong(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_peek_size_t(int s) { return mhs_from_CSize(s, 1, peek_size_t(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_size_t(int s) { poke_size_t(mhs_to_Ptr(s, 0), mhs_to_CSize(s, 1)); return mhs_from_Unit(s, 2); }
#if WANT_FLOAT32
from_t mhs_peek_flt32(int s) { return mhs_from_Float(s, 1, peek_flt32(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_flt32(int s) { poke_flt32(mhs_to_Ptr(s, 0), mhs_to_Float(s, 1)); return mhs_from_Unit(s, 2); }
#endif  /* WANT_FLOAT32 */
#if WANT_FLOAT64
from_t mhs_peek_flt64(int s) { return mhs_from_Double(s, 1, peek_flt64(mhs_to_Ptr(s, 0))); }
from_t mhs_poke_flt64(int s) { poke_flt64(mhs_to_Ptr(s, 0), mhs_to_Double(s, 1)); return mhs_from_Unit(s, 2); }
#endif  /* WANT_FLOAT64 */
from_t mhs_sizeof_char(int s) { return mhs_from_Int(s, 0, sizeof(char)); }
from_t mhs_sizeof_short(int s) { return mhs_from_Int(s, 0, sizeof(short)); }
from_t mhs_sizeof_int(int s) { return mhs_from_Int(s, 0, sizeof(int)); }
from_t mhs_sizeof_llong(int s) { return mhs_from_Int(s, 0, sizeof(long long)); }
from_t mhs_sizeof_long(int s) { return mhs_from_Int(s, 0, sizeof(long)); }
from_t mhs_sizeof_size_t(int s) { return mhs_from_Int(s, 0, sizeof(size_t)); }
#if WANT_DIR
from_t mhs_closedir(int s) { return mhs_from_Int(s, 1, closedir(mhs_to_Ptr(s, 0))); }
from_t mhs_opendir(int s) { return mhs_from_Ptr(s, 1, opendir(mhs_to_Ptr(s, 0))); }
from_t mhs_readdir(int s) { return mhs_from_Ptr(s, 1, readdir(mhs_to_Ptr(s, 0))); }
from_t mhs_c_d_name(int s) { return mhs_from_Ptr(s, 1, ((struct dirent *)(mhs_to_Ptr(s, 0)))->d_name); }
from_t mhs_chdir(int s) { return mhs_from_Int(s, 1, chdir(mhs_to_Ptr(s, 0))); }
from_t mhs_mkdir(int s) { return mhs_from_Int(s, 2, MKDIR(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_getcwd(int s) { return mhs_from_Ptr(s, 2, getcwd(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_get_permissions(int s) { return mhs_from_Int(s, 1, get_permissions(mhs_to_Ptr(s, 0))); }
from_t mhs_set_permissions(int s) { return mhs_from_Int(s, 2, set_permissions(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_get_file_size(int s) { return mhs_from_Int64(s, 1, get_file_size(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_DIR */
from_t mhs_getcpu(int s) { GETCPUTIME(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
#if WANT_ENV
from_t mhs_getenv(int s) { return mhs_from_Ptr(s, 1, getenv(mhs_to_Ptr(s, 0))); }
from_t mhs_environ(int s) { return mhs_from_Ptr(s, 0, environ); }
from_t mhs_unsetenv(int s) { return mhs_from_Int(s, 1, unsetenv(mhs_to_Ptr(s, 0))); }
from_t mhs_setenv(int s) { return mhs_from_Int(s, 3, setenv(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2))); }
#endif  /* WANT_ENV */

/* Use this to detect if we have (and want) GMP/imath or not. */
from_t mhs_want_gmp(int s) { return mhs_from_Int(s, 0, WANT_GMP); }
from_t mhs_want_imath(int s) { return mhs_from_Int(s, 0, WANT_IMATH); }

#if WANT_GMP || WANT_IMATH
void
free_mpz(void *p)
{
  /*  printf("free_mpz %p\n", p);*/
  mpz_clear(p);                 /* free any extra storage */
  FREE(p);                      /* and free the mpz itself */
}

/* Allocate an initialize a GMP integer */
struct forptr *
new_mpz(void)
{
#if 0
  {
    static int done = 0;
    if (!done) {
      printf("GMP\n");
      done = 1;
    }
  }
#endif
  mpz_ptr p = mmalloc(sizeof(*p));
  mpz_init(p);
  struct forptr *fp = mkForPtrP(p);
  fp->finalizer->final = (HsFunPtr)free_mpz;
  fp->finalizer->fptype = FP_MPZ;
  /*  printf("new_mpz %p %p\n", p, fp); */
  return fp;
}

intptr_t
mpz_get_si_(mpz_t op)
{
  uintptr_t r = mpz_get_ui(op);
  if (mpz_sgn(op) < 0) {
    r = -r;
  }
  return (intptr_t)r;
}

#if 0
void
print_mpz(mpz_ptr p)
{
  mpz_out_str(stdout, 10, p);
}
#endif

#if WANT_INT64 && (WORD_SIZE < 64 || LONG_MAX < INT64_MAX)
/* GMP lacks 64 bit support on 32 bit platforms, and where long is 32 bits (LLP64) */
void
mpz_init_set_ui64(mpz_t rop, uint64_t op)
{
  mpz_init_set_ui(rop, op >> 32);
  mpz_mul_2exp(rop, rop, 32);
  mpz_add_ui(rop, rop, op & 0xffffffff);
}
void
mpz_init_set_si64(mpz_t rop, int64_t op)
{
  if (op >= 0) {
    mpz_init_set_ui64(rop, (uint64_t)op);
  } else {
    mpz_init_set_ui64(rop, -(uint64_t)op);
    mpz_neg(rop, rop);
  }
}
int64_t
mpz_get_si64(mpz_t op)
{
  mpz_t t;
  mpz_init_set(t, op);
  mpz_tdiv_q_2exp(t, t, 32);
  uint64_t hi = mpz_get_ui(t);
  uint64_t lo = mpz_get_ui(op);
  mpz_clear(t);
  uint64_t r = (hi << 32) | lo;
  if (mpz_sgn(op) < 0) {
    r = -r;
  }
  return r;
}
#endif  /* WANT_INT64 */
#if WORD_SIZE == 64 && LONG_MAX >= INT64_MAX
#define mpz_init_set_ui64 mpz_init_set_ui
#define mpz_init_set_si64 mpz_init_set_si
#define mpz_get_si64 mpz_get_si_
#endif

from_t mhs_new_mpz(int s) { return mhs_from_ForPtr(s, 0, new_mpz()); }

/* Stubs for GMP functions */
from_t mhs_mpz_abs(int s) { mpz_abs(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_add(int s) { mpz_add(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_and(int s) { mpz_and(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_cmp(int s) { return mhs_from_Int(s, 2, mpz_cmp(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1))); }
#if WANT_FLOAT64
from_t mhs_mpz_get_d(int s) { return mhs_from_Double(s, 1, mpz_get_d(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_FLOAT64 */
#if WANT_FLOAT32
from_t mhs_mpz_get_f(int s) { return mhs_from_Float(s, 1, (float)mpz_get_d(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_FLOAT32 */
#if WANT_INT64 && WORD_SIZE == 64 && LONG_MAX < INT64_MAX
/* LLP64: Int and Word are wider than the long the GMP interface takes. */
from_t mhs_mpz_get_si(int s) { return mhs_from_Int(s, 1, mpz_get_si64(mhs_to_Ptr(s, 0))); }
from_t mhs_mpz_init_set_si(int s) { mpz_init_set_si64(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_init_set_ui(int s) { mpz_init_set_ui64(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }
#else
from_t mhs_mpz_get_si(int s) { return mhs_from_Int(s, 1, mpz_get_si_(mhs_to_Ptr(s, 0))); }
from_t mhs_mpz_init_set_si(int s) { mpz_init_set_si(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_init_set_ui(int s) { mpz_init_set_ui(mhs_to_Ptr(s, 0), mhs_to_Word(s, 1)); return mhs_from_Unit(s, 2); }
#endif
from_t mhs_mpz_ior(int s) { mpz_ior(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_mul(int s) { mpz_mul(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_mul_2exp(int s) { mpz_mul_2exp(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_neg(int s) { mpz_neg(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_popcount(int s) {
  mpz_ptr a = mhs_to_Ptr(s, 0);
  from_t r;
  if (mpz_sgn(a) < 0) {
    mpz_t neg_a;
    mpz_init(neg_a);
    mpz_neg(neg_a, a);
    r = mhs_from_Int(s, 1, -mpz_popcount(neg_a));
    mpz_clear(neg_a);
  } else {
    r = mhs_from_Int(s, 1, mpz_popcount(a));
  }
  return r;
}
from_t mhs_mpz_sub(int s) { mpz_sub(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_fdiv_q_2exp(int s) { mpz_fdiv_q_2exp(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2)); return mhs_from_Unit(s, 3); }
from_t mhs_mpz_tdiv_qr(int s) { mpz_tdiv_qr(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2), mhs_to_Ptr(s, 3)); return mhs_from_Unit(s, 4); }
from_t mhs_mpz_tstbit(int s) { return mhs_from_Int(s, 2, mpz_tstbit(mhs_to_Ptr(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_mpz_xor(int s) { mpz_xor(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2)); return mhs_from_Unit(s, 3); }
#if WANT_INT64
from_t mhs_mpz_init_set_si64(int s) { mpz_init_set_si64(mhs_to_Ptr(s, 0), mhs_to_Int64(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_init_set_ui64(int s) { mpz_init_set_ui64(mhs_to_Ptr(s, 0), mhs_to_Word64(s, 1)); return mhs_from_Unit(s, 2); }
from_t mhs_mpz_get_si64(int s) { return mhs_from_Int64(s, 1, mpz_get_si64(mhs_to_Ptr(s, 0))); }
#endif  /* WANT_INT64 */
from_t mhs_mpz_log2(int s) {
  mpz_ptr a = mhs_to_Ptr(s, 0);
  return mhs_from_Int(s, 1, mpz_sizeinbase(a, 2) - 1);
}
#endif  /* WANT_GMP || WANT_IMATH */
#if WANT_TIME
from_t mhs_gettimeofday(int s) { return mhs_from_Int(s, 2, gettimeofday(mhs_to_Ptr(s, 0), mhs_to_Ptr(s, 1))); }
#endif
from_t mhs_get_executable_path(int s) { return mhs_from_Ptr(s, 0, get_executable_path()); }

#if WANT_SOCKET
#include <sys/socket.h>
from_t mhs_F_SETFL(int s) { return mhs_from_Int(s, 0, F_SETFL); }
from_t mhs_O_NONBLOCK(int s) { return mhs_from_Int(s, 0, O_NONBLOCK); }
from_t mhs_SOL_SOCKET(int s) { return mhs_from_Int(s, 0, SOL_SOCKET); }
from_t mhs_SO_DEBUG(int s) { return mhs_from_Int(s, 0, SO_DEBUG); }
from_t mhs_SO_ERROR(int s) { return mhs_from_Int(s, 0, SO_ERROR); }
from_t mhs_SO_REUSEADDR(int s) { return mhs_from_Int(s, 0, SO_REUSEADDR); }
from_t mhs_SO_TYPE(int s) { return mhs_from_Int(s, 0, SO_TYPE); }
from_t mhs_accept(int s) { return mhs_from_Int(s, 3, accept(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1), mhs_to_Ptr(s, 2))); }
from_t mhs_bind(int s) { return mhs_from_Int(s, 3, bind(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2))); }
from_t mhs_close(int s) { return mhs_from_Int(s, 1, close(mhs_to_Int(s, 0))); }
from_t mhs_connect(int s) { return mhs_from_Int(s, 3, connect(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1), mhs_to_Int(s, 2))); }
from_t mhs_fcntl(int s) { return mhs_from_Int(s, 3, fcntl(mhs_to_Int(s, 0), mhs_to_Int(s, 1), mhs_to_Int(s, 2))); }
from_t mhs_getsockopt(int s) { return mhs_from_Int(s, 5, getsockopt(mhs_to_Int(s, 0), mhs_to_Int(s, 1), mhs_to_Int(s, 2), mhs_to_Ptr(s, 3), mhs_to_Ptr(s, 4))); }
from_t mhs_listen(int s) { return mhs_from_Int(s, 2, listen(mhs_to_Int(s, 0), mhs_to_Int(s, 1))); }
from_t mhs_recv(int s) { return mhs_from_Int(s, 4, recv(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1), mhs_to_Word(s, 2), mhs_to_Int(s, 3))); }
from_t mhs_send(int s) { return mhs_from_Int(s, 4, send(mhs_to_Int(s, 0), mhs_to_Ptr(s, 1), mhs_to_Word(s, 2), mhs_to_Int(s, 3))); }
from_t mhs_setsockopt(int s) { return mhs_from_Int(s, 5, setsockopt(mhs_to_Int(s, 0), mhs_to_Int(s, 1), mhs_to_Int(s, 2), mhs_to_Ptr(s, 3), mhs_to_Int(s, 4))); }
from_t mhs_socket(int s) { return mhs_from_Int(s, 3, socket(mhs_to_Int(s, 0), mhs_to_Int(s, 1), mhs_to_Int(s, 2))); }
#endif  /* WANT_SOCKET */

const struct ffi_entry ffi_table[] = {
  { "GETRAW", 0, mhs_GETRAW},
  { "GETTIMEMICRO", 0, mhs_GETTIMEMICRO},
  { "GETBOOTTIMEMICRO", 0, mhs_GETBOOTTIMEMICRO},
#if WANT_MATH
#if WANT_FLOAT64
  { "acos", 1, mhs_acos},
  { "asin", 1, mhs_asin},
  { "atan", 1, mhs_atan},
  { "atan2", 2, mhs_atan2},
  { "cos", 1, mhs_cos},
  { "exp", 1, mhs_exp},
  { "log", 1, mhs_log},
  { "sin", 1, mhs_sin},
  { "sqrt", 1, mhs_sqrt},
  { "tan", 1, mhs_tan},
  { "scalbn", 2, mhs_scalbn},
  { "pow", 2, mhs_pow},
#endif  /* WANT_FLOAT64 */
#if WANT_FLOAT32
  { "acosf", 1, mhs_acosf},
  { "asinf", 1, mhs_asinf},
  { "atanf", 1, mhs_atanf},
  { "atan2f", 2, mhs_atan2f},
  { "cosf", 1, mhs_cosf},
  { "expf", 1, mhs_expf},
  { "logf", 1, mhs_logf},
  { "sinf", 1, mhs_sinf},
  { "sqrtf", 1, mhs_sqrtf},
  { "tanf", 1, mhs_tanf},
  { "scalbnf", 2, mhs_scalbnf},
  { "powf", 2, mhs_powf},
#endif  /* WANT_FLOAT32 */
#endif  /* WANT_MATH */
#if WANT_FLOAT64
  { "poke_flt64", 2, mhs_poke_flt64 },
  { "peek_flt64", 1, mhs_peek_flt64 },
#endif  /* WANT_FLOAT64 */
#if WANT_FLOAT32
  { "poke_flt32", 2, mhs_poke_flt32 },
  { "peek_flt32", 1, mhs_peek_flt32 },
#endif  /* WANT_FLOAT32 */

#if defined(__EMSCRIPTEN__)
  { "js_debug", 1, mhs_js_debug},
  { "js_eval_run", 1, mhs_js_eval_run},
  { "js_eval_call", 1, mhs_js_eval_call},
  { "js_set_haskellCallback", 1, mhs_js_set_haskellCallback},
#endif

#if WANT_STDIO
  { "add_FILE", 1, mhs_add_FILE},
  { "putchar", 1, mhs_putchar},
  { "fopen", 2, mhs_fopen},
  { "tmpname", 2, mhs_tmpname},
  { "remove", 1, mhs_remove},
  { "system", 1, mhs_system},
#endif  /* WANT_STDIO */
#if WANT_FD
  { "add_fd", 1, mhs_add_fd},
  { "open", 3, mhs_open},
#endif  /* WANT_FD */
#if WANT_BUF
  { "add_buf", 2, mhs_add_buf},
#endif  /* WANT_BUF */
#if WANT_CRLF
  { "add_crlf", 1, mhs_add_crlf},
#endif  /* WANT_CRLF */
#if WANT_UTF8
  { "add_utf8", 1, mhs_add_utf8},
#endif  /* WANT_UTF8 */
#if WANT_BASE64
  { "add_base64_encoder", 1, mhs_add_base64_encoder},
  { "add_base64_decoder", 1, mhs_add_base64_decoder},
#endif  /* WANT_BASE64 */
  { "closeb", 1, mhs_closeb},
  { "&closeb", 0, mhs_addr_closeb},
  { "flushb", 1, mhs_flushb},
  { "getb", 1, mhs_getb},
  { "putb", 2, mhs_putb},
  { "ungetb", 2, mhs_ungetb},
  { "openb_wr_mem", 0, mhs_openwrmem},
  { "openb_rd_mem", 2, mhs_openrdmem},
  { "get_mem", 3, mhs_getmem},
  { "readb", 3, mhs_readb},
  { "writeb", 3, mhs_writeb},
  { "isattyb", 1, mhs_isattyb},
#if WANT_INT64
  { "filesizeb", 1, mhs_filesizeb},
#endif  /* WANT_INT64 */

#if WANT_MD5
  { "md5Array", 3, mhs_md5Array},
  { "md5BFILE", 2, mhs_md5BFILE},
  { "md5String", 2, mhs_md5String},
#endif  /* WANT_MD5 */

#if WANT_LZ77
  { "add_lz77_compressor", 1, mhs_add_lz77_compressor},
  { "add_lz77_decompressor", 1, mhs_add_lz77_decompressor},
  { "lz77c", 3, mhs_lz77c},
#endif  /* WANT_LZ77 */

#if WANT_LZMA
  { "add_lzma_compressor", 1, mhs_add_lzma_compressor},
  { "add_lzma_decompressor", 1, mhs_add_lzma_decompressor},
#endif  /* WANT_LZ77 */

#if WANT_RLE
  { "add_rle_compressor", 1, mhs_add_rle_compressor},
  { "add_rle_decompressor", 1, mhs_add_rle_decompressor},
#endif  /* WANT_RLE */

#if WANT_BWT
  { "add_bwt_compressor", 1, mhs_add_bwt_compressor},
  { "add_bwt_decompressor", 1, mhs_add_bwt_decompressor},
#endif  /* WANT_RLE */

  { "calloc", 2, mhs_calloc},
  { "realloc", 2, mhs_realloc},
  { "free", 1, mhs_free},
  { "&free", 0, mhs_addr_free},
  { "iswindows", 0, mhs_iswindows},
  { "ismacos", 0, mhs_ismacos},
  { "islinux", 0, mhs_islinux},
  { "ismingw", 0, mhs_ismingw},
  { "malloc", 1, mhs_malloc},
  { "memcpy", 3, mhs_memcpy},
  { "memmove", 3, mhs_memmove},
  { "strlen", 1, mhs_strlen},
  { "strcpy", 2, mhs_strcpy},
  { "peekPtr", 1, mhs_peekPtr},
  { "peekWord", 1, mhs_peekWord},
  { "pokePtr", 2, mhs_pokePtr},
  { "pokeWord", 2, mhs_pokeWord},

  { "peek_uint8", 1, mhs_peek_uint8},
  { "poke_uint8", 2, mhs_poke_uint8},
  { "peek_uint16", 1, mhs_peek_uint16},
  { "poke_uint16", 2, mhs_poke_uint16},
  { "peek_uint32", 1, mhs_peek_uint32},
  { "poke_uint32", 2, mhs_poke_uint32},
#if WANT_INT64
  { "peek_uint64", 1, mhs_peek_uint64},
  { "poke_uint64", 2, mhs_poke_uint64},
#endif  /* WANT_INT64 */
  { "peek_uint", 1, mhs_peek_uint},
  { "poke_uint", 2, mhs_poke_uint},

  { "peek_int8", 1, mhs_peek_int8},
  { "poke_int8", 2, mhs_poke_int8},
  { "peek_int16", 1, mhs_peek_int16},
  { "poke_int16", 2, mhs_poke_int16},
  { "peek_int32", 1, mhs_peek_int32},
  { "poke_int32", 2, mhs_poke_int32},
#if WANT_INT64
  { "peek_int64", 1, mhs_peek_int64},
  { "poke_int64", 2, mhs_poke_int64},
#endif  /* WANT_INT64 */
  { "peek_int", 1, mhs_peek_int},
  { "poke_int", 2, mhs_poke_int},
  { "peek_char", 1, mhs_peek_char},
  { "poke_char", 2, mhs_poke_char},
  { "peek_schar", 1, mhs_peek_schar},
  { "poke_schar", 2, mhs_poke_schar},
  { "peek_uchar", 1, mhs_peek_uchar},
  { "poke_uchar", 2, mhs_poke_uchar},
  { "peek_short", 1, mhs_peek_short},
  { "poke_short", 2, mhs_poke_short},
  { "peek_ushort", 1, mhs_peek_ushort},
  { "poke_ushort", 2, mhs_poke_ushort},
  { "peek_llong", 1, mhs_peek_llong},
  { "peek_long", 1, mhs_peek_long},
  { "peek_ullong", 1, mhs_peek_ullong},
  { "peek_ulong", 1, mhs_peek_ulong},
  { "peek_size_t", 1, mhs_peek_size_t},
  { "poke_llong", 2, mhs_poke_llong},
  { "poke_long", 2, mhs_poke_long},
  { "poke_ullong", 2, mhs_poke_ullong},
  { "poke_ulong", 2, mhs_poke_ulong},
  { "poke_size_t", 2, mhs_poke_size_t},
  { "sizeof_char", 0, mhs_sizeof_char},
  { "sizeof_short", 0, mhs_sizeof_short},
  { "sizeof_int", 0, mhs_sizeof_int},
  { "sizeof_llong", 0, mhs_sizeof_llong},
  { "sizeof_long", 0, mhs_sizeof_long},
  { "sizeof_size_t", 0, mhs_sizeof_size_t},
#if WANT_DIR
  { "c_d_name", 1, mhs_c_d_name},
  { "closedir", 1, mhs_closedir},
  { "opendir", 1, mhs_opendir},
  { "readdir", 1, mhs_readdir},
  { "chdir", 1, mhs_chdir},
  { "mkdir", 2, mhs_mkdir},
  { "getcwd", 2, mhs_getcwd},
  { "set_permissions", 2, mhs_set_permissions},
  { "get_permissions", 1, mhs_get_permissions},
  { "get_file_size", 1, mhs_get_file_size},
#endif  /* WANT_DIR */
  { "getcpu", 2, mhs_getcpu},
  { "want_gmp", 0, mhs_want_gmp},
  { "want_imath", 0, mhs_want_imath},
#if WANT_GMP || WANT_IMATH
  { "new_mpz", 0, mhs_new_mpz},
  { "mpz_abs", 2, mhs_mpz_abs},
  { "mpz_add", 3, mhs_mpz_add},
  { "mpz_and", 3, mhs_mpz_and},
  { "mpz_cmp", 2, mhs_mpz_cmp},
#if WANT_FLOAT64
  { "mpz_get_d", 1, mhs_mpz_get_d},
#endif  /* WANT_FLOAT64 */
  { "mpz_get_si", 1, mhs_mpz_get_si},
  { "mpz_init_set_si", 2, mhs_mpz_init_set_si},
  { "mpz_init_set_ui", 2, mhs_mpz_init_set_ui},
  { "mpz_ior", 3, mhs_mpz_ior},
  { "mpz_mul", 3, mhs_mpz_mul},
  { "mpz_mul_2exp", 3, mhs_mpz_mul_2exp},
  { "mpz_neg", 2, mhs_mpz_neg},
  { "mpz_popcount", 1, mhs_mpz_popcount},
  { "mpz_sub", 3, mhs_mpz_sub},
  { "mpz_fdiv_q_2exp", 3, mhs_mpz_fdiv_q_2exp},
  { "mpz_tdiv_qr", 4, mhs_mpz_tdiv_qr},
  { "mpz_tstbit", 2, mhs_mpz_tstbit},
  { "mpz_xor", 3, mhs_mpz_xor},
#if WANT_FLOAT32
  { "mpz_get_f", 1, mhs_mpz_get_f},
#endif  /* WANT_FLOAT32 */
#if WANT_INT64
  { "mpz_init_set_si64", 2, mhs_mpz_init_set_si64},
  { "mpz_init_set_ui64", 2, mhs_mpz_init_set_ui64},
  { "mpz_get_si64", 1, mhs_mpz_get_si64},
#endif  /* WANT_INT64 */
  { "mpz_log2", 1, mhs_mpz_log2},
#endif  /* WANT_GMP || WANT_IMATH */
#if WANT_TIME
  { "gettimeofday", 2, mhs_gettimeofday},
#endif
#if WANT_ERRNO
  { "E2BIG", 0, mhs_E2BIG},
  { "EACCES", 0, mhs_EACCES},
  { "EADDRINUSE", 0, mhs_EADDRINUSE},
  { "EADDRNOTAVAIL", 0, mhs_EADDRNOTAVAIL},
  { "EADV", 0, mhs_EADV},
  { "EAFNOSUPPORT", 0, mhs_EAFNOSUPPORT},
  { "EAGAIN", 0, mhs_EAGAIN},
  { "EALREADY", 0, mhs_EALREADY},
  { "EBADF", 0, mhs_EBADF},
  { "EBADMSG", 0, mhs_EBADMSG},
  { "EBADRPC", 0, mhs_EBADRPC},
  { "EBUSY", 0, mhs_EBUSY},
  { "ECHILD", 0, mhs_ECHILD},
  { "ECOMM", 0, mhs_ECOMM},
  { "ECONNABORTED", 0, mhs_ECONNABORTED},
  { "ECONNREFUSED", 0, mhs_ECONNREFUSED},
  { "ECONNRESET", 0, mhs_ECONNRESET},
  { "EDEADLK", 0, mhs_EDEADLK},
  { "EDESTADDRREQ", 0, mhs_EDESTADDRREQ},
  { "EDIRTY", 0, mhs_EDIRTY},
  { "EDOM", 0, mhs_EDOM},
  { "EDQUOT", 0, mhs_EDQUOT},
  { "EEXIST", 0, mhs_EEXIST},
  { "EFAULT", 0, mhs_EFAULT},
  { "EFBIG", 0, mhs_EFBIG},
  { "EFTYPE", 0, mhs_EFTYPE},
  { "EHOSTDOWN", 0, mhs_EHOSTDOWN},
  { "EHOSTUNREACH", 0, mhs_EHOSTUNREACH},
  { "EIDRM", 0, mhs_EIDRM},
  { "EILSEQ", 0, mhs_EILSEQ},
  { "EINPROGRESS", 0, mhs_EINPROGRESS},
  { "EINTR", 0, mhs_EINTR},
  { "EINVAL", 0, mhs_EINVAL},
  { "EIO", 0, mhs_EIO},
  { "EISCONN", 0, mhs_EISCONN},
  { "EISDIR", 0, mhs_EISDIR},
  { "ELOOP", 0, mhs_ELOOP},
  { "EMFILE", 0, mhs_EMFILE},
  { "EMLINK", 0, mhs_EMLINK},
  { "EMSGSIZE", 0, mhs_EMSGSIZE},
  { "EMULTIHOP", 0, mhs_EMULTIHOP},
  { "ENAMETOOLONG", 0, mhs_ENAMETOOLONG},
  { "ENETDOWN", 0, mhs_ENETDOWN},
  { "ENETRESET", 0, mhs_ENETRESET},
  { "ENETUNREACH", 0, mhs_ENETUNREACH},
  { "ENFILE", 0, mhs_ENFILE},
  { "ENOBUFS", 0, mhs_ENOBUFS},
  { "ENODATA", 0, mhs_ENODATA},
  { "ENODEV", 0, mhs_ENODEV},
  { "ENOENT", 0, mhs_ENOENT},
  { "ENOEXEC", 0, mhs_ENOEXEC},
  { "ENOLCK", 0, mhs_ENOLCK},
  { "ENOLINK", 0, mhs_ENOLINK},
  { "ENOMEM", 0, mhs_ENOMEM},
  { "ENOMSG", 0, mhs_ENOMSG},
  { "ENONET", 0, mhs_ENONET},
  { "ENOPROTOOPT", 0, mhs_ENOPROTOOPT},
  { "ENOSPC", 0, mhs_ENOSPC},
  { "ENOSR", 0, mhs_ENOSR},
  { "ENOSTR", 0, mhs_ENOSTR},
  { "ENOSYS", 0, mhs_ENOSYS},
  { "ENOTBLK", 0, mhs_ENOTBLK},
  { "ENOTCONN", 0, mhs_ENOTCONN},
  { "ENOTDIR", 0, mhs_ENOTDIR},
  { "ENOTEMPTY", 0, mhs_ENOTEMPTY},
  { "ENOTSOCK", 0, mhs_ENOTSOCK},
  { "ENOTSUP", 0, mhs_ENOTSUP},
  { "ENOTTY", 0, mhs_ENOTTY},
  { "ENXIO", 0, mhs_ENXIO},
  { "EOPNOTSUPP", 0, mhs_EOPNOTSUPP},
  { "EPERM", 0, mhs_EPERM},
  { "EPFNOSUPPORT", 0, mhs_EPFNOSUPPORT},
  { "EPIPE", 0, mhs_EPIPE},
  { "EPROCLIM", 0, mhs_EPROCLIM},
  { "EPROCUNAVAIL", 0, mhs_EPROCUNAVAIL},
  { "EPROGMISMATCH", 0, mhs_EPROGMISMATCH},
  { "EPROGUNAVAIL", 0, mhs_EPROGUNAVAIL},
  { "EPROTO", 0, mhs_EPROTO},
  { "EPROTONOSUPPORT", 0, mhs_EPROTONOSUPPORT},
  { "EPROTOTYPE", 0, mhs_EPROTOTYPE},
  { "ERANGE", 0, mhs_ERANGE},
  { "EREMCHG", 0, mhs_EREMCHG},
  { "EREMOTE", 0, mhs_EREMOTE},
  { "EROFS", 0, mhs_EROFS},
  { "ERPCMISMATCH", 0, mhs_ERPCMISMATCH},
  { "ERREMOTE", 0, mhs_ERREMOTE},
  { "ESHUTDOWN", 0, mhs_ESHUTDOWN},
  { "ESOCKTNOSUPPORT", 0, mhs_ESOCKTNOSUPPORT},
  { "ESPIPE", 0, mhs_ESPIPE},
  { "ESRCH", 0, mhs_ESRCH},
  { "ESRMNT", 0, mhs_ESRMNT},
  { "ESTALE", 0, mhs_ESTALE},
  { "ETIME", 0, mhs_ETIME},
  { "ETIMEDOUT", 0, mhs_ETIMEDOUT},
  { "ETOOMANYREFS", 0, mhs_ETOOMANYREFS},
  { "ETXTBSY", 0, mhs_ETXTBSY},
  { "EUSERS", 0, mhs_EUSERS},
  { "EWOULDBLOCK", 0, mhs_EWOULDBLOCK},
  { "EXDEV", 0, mhs_EXDEV},
  { "&errno", 0, mhs_addr_errno},
  { "strerror_r", 3, mhs_strerror_r},
#endif
  { "get_executable_path", 0, mhs_get_executable_path},
#if WANT_ENV
  { "getenv", 1, mhs_getenv},
  { "environ", 0, mhs_environ},
  { "unsetenv", 1, mhs_unsetenv},
  { "setenv", 3, mhs_setenv},
#endif  /* WANT_ENV */
#if WANT_SOCKET
  { "F_SETFL", 0, mhs_F_SETFL},
  { "O_NONBLOCK", 0, mhs_O_NONBLOCK},
  { "SOL_SOCKET", 0, mhs_SOL_SOCKET},
  { "SO_DEBUG", 0, mhs_SO_DEBUG},
  { "SO_ERROR", 0, mhs_SO_ERROR},
  { "SO_REUSEADDR", 0, mhs_SO_REUSEADDR},
  { "SO_TYPE", 0, mhs_SO_TYPE},
  { "accept", 3, mhs_accept},
  { "bind", 3, mhs_bind},
  { "close", 1, mhs_close},
  { "connect", 3, mhs_connect},
  { "fcntl", 3, mhs_fcntl},
  { "getsockopt", 5, mhs_getsockopt},
  { "listen", 2, mhs_listen},
  { "recv", 4, mhs_recv},
  { "send", 4, mhs_send},
  { "setsockopt", 5, mhs_setsockopt},
  { "socket", 3, mhs_socket},
#endif  /* WANT_SOCKET */
  { 0,0 }
};

int num_ffi = sizeof(ffi_table) / sizeof(ffi_table[0]);



/*******************************/
/* HsFFI.h API */

void
hs_init(int *argc, char **argv[])
{
  (void)mhs_main(*argc, *argv);
}

void
hs_exit(void)
{
  extern void _exit(int);       /* XXX why do we need this */
  _exit(0);
}

void
hs_set_argv(int argc, char *argv[])
{
  ERR("hs_set_argv not implemented");
}

void
hs_perform_gc(void)
{
  gc();
}

void
hs_free_stable_ptr(void *sp)
{
  free_stableptr((uvalue_t)sp);
}

void
hs_free_fun_ptr(HsFunPtr fp)
{
  ERR("hs_free_fun_ptr not implemented");
}