1 /* -----------------------------------------------------------------------------
2 * $Id: CCall.h,v 1.2 1998/12/02 13:20:53 simonm Exp $
4 * Macros for performing C calls from the STG world.
5 * -------------------------------------------------------------------------- */
11 * Most C-Calls made from STG land are of the 'unsafe' variety.
12 * An unsafe C-Call is one where we trust the C function not to do
13 * anything nefarious while it has control.
15 * Nefarious actions include doing allocation on the Haskell heap,
16 * garbage collecting, creating/deleting threads, re-entering the
17 * scheduler, and messing with runtime system data structures.
19 * For these calls, the code generator will kindly provide CALLER_SAVE
20 * and CALLER_RESTORE macros for any registers that are live across the
21 * call. These macros may expand into saves of the relevant registers
22 * if those registers are designated caller-saves by the C calling
23 * convention, otherwise they will expand to nothing.
26 /* Unsafe C-Calls have no macros: we just use a straightforward call.
30 * An STGCALL<n> is used when we want the relevant registers to be
31 * saved automatically. An STGCALL doesn't return a result, there's
32 * an alternative set of RET_STGCALL<n> macros for that (and we hope
33 * that the restoring of the caller-saves registers doesn't clobber
38 CALLER_SAVE_ALL (void) f(); CALLER_RESTORE_ALL
40 #define STGCALL1(f,a) \
41 CALLER_SAVE_ALL (void) f(a); CALLER_RESTORE_ALL
43 #define STGCALL2(f,a,b) \
44 CALLER_SAVE_ALL (void) f(a,b); CALLER_RESTORE_ALL
46 #define STGCALL3(f,a,b,c) \
47 CALLER_SAVE_ALL (void) f(a,b,c); CALLER_RESTORE_ALL
49 #define STGCALL4(f,a,b,c,d) \
50 CALLER_SAVE_ALL (void) f(a,b,c,d); CALLER_RESTORE_ALL
52 #define STGCALL5(f,a,b,c,d,e) \
53 CALLER_SAVE_ALL (void) f(a,b,c,d,e); CALLER_RESTORE_ALL
56 #define RET_STGCALL0(t,f) \
57 ({ t _r; CALLER_SAVE_ALL _r = f(); CALLER_RESTORE_ALL; _r; })
59 #define RET_STGCALL1(t,f,a) \
60 ({ t _r; CALLER_SAVE_ALL _r = f(a); CALLER_RESTORE_ALL; _r; })
62 #define RET_STGCALL2(t,f,a,b) \
63 ({ t _r; CALLER_SAVE_ALL _r = f(a,b); CALLER_RESTORE_ALL; _r; })
65 #define RET_STGCALL3(t,f,a,b,c) \
66 ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c); CALLER_RESTORE_ALL; _r; })
68 #define RET_STGCALL4(t,f,a,b,c,d) \
69 ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c,d); CALLER_RESTORE_ALL; _r; })
71 #define RET_STGCALL5(t,f,a,b,c,d,e) \
72 ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c,d,e); CALLER_RESTORE_ALL; _r; })
76 * A PRIM_STGCALL is used when we have arranged to save the R<n>,
77 * F<n>, and D<n> registers already, we only need the "system"
78 * registers saved for us. These are used in PrimOps, where the
79 * compiler has a good idea of what registers are live, and so doesn't
80 * need to save all of them.
83 #define PRIM_STGCALL0(f) \
84 CALLER_SAVE_SYSTEM (void) f(); CALLER_RESTORE_SYSTEM
86 #define PRIM_STGCALL1(f,a) \
87 CALLER_SAVE_SYSTEM (void) f(a); CALLER_RESTORE_SYSTEM
89 #define PRIM_STGCALL2(f,a,b) \
90 CALLER_SAVE_SYSTEM (void) f(a,b); CALLER_RESTORE_SYSTEM
92 #define PRIM_STGCALL3(f,a,b,c) \
93 CALLER_SAVE_SYSTEM (void) f(a,b,c); CALLER_RESTORE_SYSTEM
95 #define PRIM_STGCALL4(f,a,b,c,d) \
96 CALLER_SAVE_SYSTEM (void) f(a,b,c,d); CALLER_RESTORE_SYSTEM
98 #define PRIM_STGCALL5(f,a,b,c,d,e) \
99 CALLER_SAVE_SYSTEM (void) f(a,b,c,d,e); CALLER_RESTORE_SYSTEM
102 #define RET_PRIM_STGCALL0(t,f) \
103 ({ t _r; CALLER_SAVE_SYSTEM _r = f(); CALLER_RESTORE_SYSTEM; _r; })
105 #define RET_PRIM_STGCALL1(t,f,a) \
106 ({ t _r; CALLER_SAVE_SYSTEM _r = f(a); CALLER_RESTORE_SYSTEM; _r; })
108 #define RET_PRIM_STGCALL2(t,f,a,b) \
109 ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b); CALLER_RESTORE_SYSTEM; _r; })
111 #define RET_PRIM_STGCALL3(t,f,a,b,c) \
112 ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c); CALLER_RESTORE_SYSTEM; _r; })
114 #define RET_PRIM_STGCALL4(t,f,a,b,c,d) \
115 ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c,d); CALLER_RESTORE_SYSTEM; _r; })
117 #define RET_PRIM_STGCALL5(t,f,a,b,c,d,e) \
118 ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c,d,e); CALLER_RESTORE_SYSTEM; _r; })
120 /* ToDo: ccalls that might garbage collect - do we need to return to
121 * the scheduler to perform these? Similarly, ccalls that might want
122 * to call Haskell right back, or start a new thread or something.