| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef _C_MAIN_H_ |
| 11 |
#define _C_MAIN_H_ |
| 12 |
|
| 13 |
#include "common-main.h" |
| 14 |
#include "c-common.h" |
| 15 |
|
| 16 |
PRIVATE GC_state MLton_gcState() { |
| 17 |
static struct GC_state gcState; |
| 18 |
return &gcState; |
| 19 |
} |
| 20 |
|
| 21 |
static GC_frameIndex returnAddressToFrameIndex (GC_returnAddress ra) { |
| 22 |
return (GC_frameIndex)ra; |
| 23 |
} |
| 24 |
|
| 25 |
static inline uintptr_t getNextBlockFromStackTop (GC_state s) { |
| 26 |
return *(uintptr_t*)(s->stackTop - GC_RETURNADDRESS_SIZE); |
| 27 |
} |
| 28 |
|
| 29 |
PRIVATE uintptr_t MLton_unreachable() { return -2; } |
| 30 |
|
| 31 |
PRIVATE extern ChunkFnPtr_t const nextChunks[]; |
| 32 |
|
| 33 |
static inline void MLton_trampoline (GC_state s, uintptr_t nextBlock, bool mayReturnToC) { |
| 34 |
do { |
| 35 |
nextBlock = (*(nextChunks[nextBlock]))(s, s->stackTop, s->frontier, nextBlock); |
| 36 |
} while (!mayReturnToC || nextBlock != (uintptr_t)-1); |
| 37 |
} |
| 38 |
|
| 39 |
#define MLtonCallFromC() \ |
| 40 |
PRIVATE uintptr_t Thread_returnToC() { return -1; } \ |
| 41 |
static void MLton_callFromC (CPointer localOpArgsResPtr) { \ |
| 42 |
uintptr_t nextBlock; \ |
| 43 |
GC_state s = MLton_gcState(); \ |
| 44 |
if (DEBUG_CCODEGEN) \ |
| 45 |
fprintf (stderr, "MLton_callFromC() starting\n"); \ |
| 46 |
s->callFromCOpArgsResPtr = localOpArgsResPtr; \ |
| 47 |
GC_setSavedThread (s, GC_getCurrentThread (s)); \ |
| 48 |
s->atomicState += 3; \ |
| 49 |
if (s->signalsInfo.signalIsPending) \ |
| 50 |
s->limit = s->limitPlusSlop - GC_HEAP_LIMIT_SLOP; \ |
| 51 |
\ |
| 52 |
GC_switchToThread (s, GC_getCallFromCHandlerThread (s), 0); \ |
| 53 |
nextBlock = getNextBlockFromStackTop (s); \ |
| 54 |
MLton_trampoline (s, nextBlock, TRUE); \ |
| 55 |
s->atomicState += 1; \ |
| 56 |
GC_switchToThread (s, GC_getSavedThread (s), 0); \ |
| 57 |
s->atomicState -= 1; \ |
| 58 |
if (0 == s->atomicState \ |
| 59 |
&& s->signalsInfo.signalIsPending) \ |
| 60 |
s->limit = 0; \ |
| 61 |
if (DEBUG_CCODEGEN) \ |
| 62 |
fprintf (stderr, "MLton_callFromC done\n"); \ |
| 63 |
} |
| 64 |
|
| 65 |
#define MLtonMain(al, mg, mfs, mmc, pk, ps, ml) \ |
| 66 |
PUBLIC int MLton_main (int argc, char* argv[]) { \ |
| 67 |
uintptr_t nextBlock; \ |
| 68 |
GC_state s = MLton_gcState(); \ |
| 69 |
Initialize (s, al, mg, mfs, mmc, pk, ps); \ |
| 70 |
if (s->amOriginal) { \ |
| 71 |
nextBlock = ml; \ |
| 72 |
} else { \ |
| 73 |
\ |
| 74 |
nextBlock = getNextBlockFromStackTop (s); \ |
| 75 |
} \ |
| 76 |
MLton_trampoline (s, nextBlock, FALSE); \ |
| 77 |
return 1; \ |
| 78 |
} |
| 79 |
|
| 80 |
#define MLtonLibrary(al, mg, mfs, mmc, pk, ps, ml) \ |
| 81 |
PUBLIC void LIB_OPEN(LIBNAME) (int argc, char* argv[]) { \ |
| 82 |
uintptr_t nextBlock; \ |
| 83 |
GC_state s = MLton_gcState(); \ |
| 84 |
Initialize (s, al, mg, mfs, mmc, pk, ps); \ |
| 85 |
if (s->amOriginal) { \ |
| 86 |
nextBlock = ml; \ |
| 87 |
} else { \ |
| 88 |
\ |
| 89 |
nextBlock = getNextBlockFromStackTop (s); \ |
| 90 |
} \ |
| 91 |
MLton_trampoline (s, nextBlock, TRUE); \ |
| 92 |
} \ |
| 93 |
PUBLIC void LIB_CLOSE(LIBNAME) () { \ |
| 94 |
uintptr_t nextBlock; \ |
| 95 |
GC_state s = MLton_gcState(); \ |
| 96 |
nextBlock = getNextBlockFromStackTop (s); \ |
| 97 |
MLton_trampoline (s, nextBlock, TRUE); \ |
| 98 |
GC_done(s); \ |
| 99 |
} |
| 100 |
|
| 101 |
#endif |