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