1 /* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
12 #include "common-main.h"
15 static GC_frameIndex
returnAddressToFrameIndex (GC_returnAddress ra
) {
16 return (GC_frameIndex
)ra
;
19 #define MLtonCallFromC \
21 PRIVATE uintptr_t nextFun; \
22 PRIVATE int returnToC; \
23 static void MLton_callFromC () { \
28 fprintf (stderr, "MLton_callFromC() starting\n"); \
30 GC_setSavedThread (s, GC_getCurrentThread (s)); \
31 s->atomicState += 3; \
32 if (s->signalsInfo.signalIsPending) \
33 s->limit = s->limitPlusSlop - GC_HEAP_LIMIT_SLOP; \
34 /* Switch to the C Handler thread. */ \
35 GC_switchToThread (s, GC_getCallFromCHandlerThread (s), 0); \
36 nextFun = *(uintptr_t*)(s->stackTop - GC_RETURNADDRESS_SIZE); \
37 cont.nextChunk = nextChunks[nextFun]; \
40 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
41 } while (not returnToC); \
43 s->atomicState += 1; \
44 GC_switchToThread (s, GC_getSavedThread (s), 0); \
45 s->atomicState -= 1; \
46 if (0 == s->atomicState \
47 && s->signalsInfo.signalIsPending) \
50 fprintf (stderr, "MLton_callFromC done\n"); \
53 #define MLtonMain(al, mg, mfs, mmc, pk, ps, mc, ml) \
55 PUBLIC int MLton_main (int argc, char* argv[]) { \
57 Initialize (al, mg, mfs, mmc, pk, ps); \
58 if (gcState.amOriginal) { \
60 PrepFarJump(mc, ml); \
62 /* Return to the saved world */ \
63 nextFun = *(uintptr_t*)(gcState.stackTop - GC_RETURNADDRESS_SIZE); \
64 cont.nextChunk = nextChunks[nextFun]; \
68 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
69 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
70 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
71 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
72 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
73 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
74 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
75 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
80 #define MLtonLibrary(al, mg, mfs, mmc, pk, ps, mc, ml) \
82 PUBLIC void LIB_OPEN(LIBNAME) (int argc, char* argv[]) { \
84 Initialize (al, mg, mfs, mmc, pk, ps); \
85 if (gcState.amOriginal) { \
87 PrepFarJump(mc, ml); \
89 /* Return to the saved world */ \
90 nextFun = *(uintptr_t*)(gcState.stackTop - GC_RETURNADDRESS_SIZE); \
91 cont.nextChunk = nextChunks[nextFun]; \
96 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
97 } while (not returnToC); \
99 PUBLIC void LIB_CLOSE(LIBNAME) () { \
101 nextFun = *(uintptr_t*)(gcState.stackTop - GC_RETURNADDRESS_SIZE); \
102 cont.nextChunk = nextChunks[nextFun]; \
105 cont=(*(struct cont(*)(void))cont.nextChunk)(); \
106 } while (not returnToC); \
110 #endif /* #ifndef _C_MAIN_H */