[HACK] Disable compaction for now.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Place static closures in their own section.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
BC-breaking changes to C-- CLOSURE syntax.

Previously, there were two variants of CLOSURE in C--:

    - Top-level CLOSURE(foo_closure, foo, lits...), which defines a new
      static closure and gives it a name, and

    - Array CLOSURE(foo, lits...), which was used for the static char
      and integer arrays.

They used the same name, were confusing, and didn't even generate
the correct internal label representation!  So now, we have two
new forms:

    - Top-level CLOSURE(foo, lits...) which automatically generates
      foo_closure (along with foo_info, which we were doing already)

    - Array ANONYMOUS_CLOSURE(foo, lits...) which doesn't generate
      a foo_closure identifier.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Properly generate info tables for static closures in C--.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Rename _closure to _static_closure, apply naming consistently.

In preparation for indirecting all references to closures,
we rename _closure to _static_closure to ensure any old code
will get an undefined symbol error.  In order to reference
a closure foobar_closure (which is now undefined), you should instead
use STATIC_CLOSURE(foobar).  For convenience, a number of these
old identifiers are macro'd.

Across C-- and C (Windows and otherwise), there were differing
conventions on whether or not foobar_closure or &foobar_closure
was the address of the closure.  Now, all foobar_closure references
are addresses, and no & is necessary.

CHARLIKE/INTLIKE were not changed, simply alpha-renamed.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement closure indirections.

Closure indirections live in the staticclosureinds section
and initially point to their corresponding closure in
the staticclosures.  The intent is that all code goes through
a closure indirection, and at runtime the indirections can
change.

These pieces of code have to go through closure indirections:

    - Code references to a static closure
    - SRTs
    - Heap check (but we want to fix this)
    - References to closures in RTS (see STATIC_CLOSURE) macro
    - Runtime symbol lookup (e.g. GHCi, dynwrapper)
    - The main closure passed to hs_main

Closure indirections should simplify these components (but that
is not implemented yet):

    - Windows DLL handling (which already needed an indirection)

In order to keep the code running, the fields of static closures
are not indirected (although they should be); this will be switched
over in a later commit.

Important invariant: an indirection may only be dereferenced
after the runtime is setup.  In particular, references to
closures in __attribute__(constructor) need to be kept indirections;
this is what the stable pointer deferral is for.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement runtime setup code for static closures.

Fields of static closures are now indirected, and we have a simple
runtime initialization code initStaticClosures() which merely rewrites
all of the indirections into untagged pointers and untangles references
between the static closures.  This also copies static closures into
blocks, although this data is unused.

The runtime system finds out about indirections by way of
__attribute__(constructor) methods; thus C-- compilation may now
generate foreign stubs (if it defines closures).

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Indirect SRTs and heap checks.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
CHARLIKE/INTLIKE must use indirections.

While these are "closures", they aren't allocated the usual
way, so we just special case them in closure initialization.

It is very important that these arrays fit in a single block,
since a pointer to a closure across the boundary needs to have
the appropriate BF_STATIC flag set.

Nota bene: C will implicitly dereference the _ind reference,
but C-- will not; thus the extra load in C-- land.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Closure tables must use indirections.

ToDo: This could be improved by short-circuiting the indirections in the
closure table.  However, we have to arrange for the RTS to know about
all of our closure tables, which is a bit annoying to do.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Add some thread-safety.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Replace HEAP_ALLOCED with block descriptor check.

Blocks have a new flag BF_STATIC, which indicates if the data
contained therein is static.  This means we can get rid of
all of the complicated caching logic in MBlock.h.  For now,
we still maintain the memory mapping, so we can deallocate
all our memory at the end and do some sanity checks.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Move tag from outside to inside indirection.

In the old world, code referring to a closure had code like:

    foobar_closure + tag

Previously, when there was no dereference, this was essentially
free; but in the new world, we have:

    *foobar_static_closure_ind + tag

and that means a few extra instructions at runtime.  But
we can store the tag in the indirection itself, to simplify
the code back to:

    *foobar_static_closure_ind

A bit of stuff needs to be rewired. One important thing:
CHARLIKE/INTLIKE do *not* do this scheme, since there isn't
a separate tagged pointer for each closure, and it's free
anyway, since we already need to add an offset to index
into the appropriate little object.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Reduce number of exported symbols (e.g. _static_closure)

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement neededBlocks

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
[START] Implement core infrastructure for resource limits.

This is a basic implementation of the core infrastructure for resource
limits, although it doesn't do anything, as there are no primops for
actually changing your resource container.

TOFIX: GHCi doesn't work.

TOFIX: Compiled-code changes should be optional (either a new way or
configure time option), as they increase code size a bit.

TOFIX: Compaction is not supported.

TOFIX: Some of the RTS function names are not very good.

WONTFIX: 32-bit does not have enough space to support the resource
container pointer in the block descriptor.  So this compilation mode
is not compatible with 32-bit.

Patch notes:

    * Two new registers: RC and OldRC.

    * The key invariants is that the resource container of
      CurrentNursery must be the same as the current resource
      container as recorded in the register RC.

    * Compiled-code changes in two places.  First,
      during thunk entry, we need to do an extra conditional on the RC
      to determine if we need to change containers (doing this fast
      relies on all closures, in particular static closures, having a
      valid block descriptor).  Second, the current RC must be saved and
      restored along with the rest of thread state: thus update frames
      are augmented to record the RC.  We also introduce a new update
      frame for just restoring an RC.  (Exception handling code applies
      these frames).

    * The modified entry code now looks like this:

        W_ rc = Bdescr(node)->rc;
        if (rc != RC) {
            RC = rc; // CurrentNursery/RC invariant violated
            goto gc;
        }

      The slowpath in rts/HeapStackCheck.cmm is responsible for noticing
      that the Current/RC invariant is incorrect, and adjusting
      CurrentNursery so that the invariant is restored, as well as
      pushing a frame stg_restore_container to restore the previous RC.

    * New C-- macros: RC_NURSERY to calculate the resource container of
      the nursery, and RC_CURRENT_NURSERY for the currentNursery

    * GC thread workspaces and nurseries are now reorganized to live in
      the resource container (NB: we change this later in the patchset).
      We do this by appending a variable size array at the end of
      resource containers, which contains a rcthread struct per thread.
      The garbage collector indirects through this, which results in a
      few more dereferences in the evacuate() fast-path.  Work stealing
      is turned off (NB: this is fixed later)

    * New RTS functions alloc*For which allocate into a resource
      container, and allocateFor and cmmMaybeAllocate for respecting
      resource limits when allocating into the current nursery.

    * The most important situation, the heap overflow handler, is
      modified to respect resource limits.

    * New threads inherit the RC of their parent.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement dead container garbage collection [INCOMPLETE]

LEAKS MEMORY! need to stgFree the ResourceContainer struct,
but then we need to invalidate existing RC# pointers.  A little
tricky, so don't bother for now.

Tightens up some invariants, yay!

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement centralized work queue for GC, with a shitty heuristic for finding new work

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
Implement scan stack to find new work in RCs.

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>
Implement support for PINNED objects.

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>