[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / includes / CCall.h
1 /* -----------------------------------------------------------------------------
2  * $Id: CCall.h,v 1.4 2000/01/13 14:34:00 hwloidl Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Macros for performing C calls from the STG world.
7  *
8  * -------------------------------------------------------------------------- */
9
10 #ifndef CCALL_H
11 #define CCALL_H
12
13 /* 
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.
17  *
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.
21  * 
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.
27  */
28
29 /* Unsafe C-Calls have no macros: we just use a straightforward call.
30  */
31
32 /*
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
37  * the result!)
38  */
39
40 #define STGCALL0(f) \
41   CALLER_SAVE_ALL (void) f(); CALLER_RESTORE_ALL
42
43 #define STGCALL1(f,a) \
44   CALLER_SAVE_ALL (void) f(a); CALLER_RESTORE_ALL
45
46 #define STGCALL2(f,a,b) \
47   CALLER_SAVE_ALL (void) f(a,b); CALLER_RESTORE_ALL
48
49 #define STGCALL3(f,a,b,c) \
50   CALLER_SAVE_ALL (void) f(a,b,c); CALLER_RESTORE_ALL
51
52 #define STGCALL4(f,a,b,c,d) \
53   CALLER_SAVE_ALL (void) f(a,b,c,d); CALLER_RESTORE_ALL
54
55 #define STGCALL5(f,a,b,c,d,e) \
56   CALLER_SAVE_ALL (void) f(a,b,c,d,e); CALLER_RESTORE_ALL
57
58 #define STGCALL6(f,a,b,c,d,e,z) \
59   CALLER_SAVE_ALL (void) f(a,b,c,d,e,z); CALLER_RESTORE_ALL
60
61
62 #define RET_STGCALL0(t,f) \
63   ({ t _r; CALLER_SAVE_ALL _r = f(); CALLER_RESTORE_ALL; _r; })
64
65 #define RET_STGCALL1(t,f,a) \
66   ({ t _r; CALLER_SAVE_ALL _r = f(a); CALLER_RESTORE_ALL; _r; })
67
68 #define RET_STGCALL2(t,f,a,b) \
69   ({ t _r; CALLER_SAVE_ALL _r = f(a,b); CALLER_RESTORE_ALL; _r; })
70
71 #define RET_STGCALL3(t,f,a,b,c) \
72   ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c); CALLER_RESTORE_ALL; _r; })
73
74 #define RET_STGCALL4(t,f,a,b,c,d) \
75   ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c,d); CALLER_RESTORE_ALL; _r; })
76
77 #define RET_STGCALL5(t,f,a,b,c,d,e) \
78   ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c,d,e); CALLER_RESTORE_ALL; _r; })
79
80 #define RET_STGCALL6(t,f,a,b,c,d,e,z) \
81   ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c,d,e,z); CALLER_RESTORE_ALL; _r; })
82
83
84 /*
85  * A PRIM_STGCALL is used when we have arranged to save the R<n>,
86  * F<n>, and D<n> registers already, we only need the "system"
87  * registers saved for us.  These are used in PrimOps, where the
88  * compiler has a good idea of what registers are live, and so doesn't
89  * need to save all of them.
90  */
91
92 #define PRIM_STGCALL0(f) \
93   CALLER_SAVE_SYSTEM (void) f(); CALLER_RESTORE_SYSTEM
94
95 #define PRIM_STGCALL1(f,a) \
96   CALLER_SAVE_SYSTEM (void) f(a); CALLER_RESTORE_SYSTEM
97
98 #define PRIM_STGCALL2(f,a,b) \
99   CALLER_SAVE_SYSTEM (void) f(a,b); CALLER_RESTORE_SYSTEM
100
101 #define PRIM_STGCALL3(f,a,b,c) \
102   CALLER_SAVE_SYSTEM (void) f(a,b,c); CALLER_RESTORE_SYSTEM
103
104 #define PRIM_STGCALL4(f,a,b,c,d) \
105   CALLER_SAVE_SYSTEM (void) f(a,b,c,d); CALLER_RESTORE_SYSTEM
106
107 #define PRIM_STGCALL5(f,a,b,c,d,e) \
108   CALLER_SAVE_SYSTEM (void) f(a,b,c,d,e); CALLER_RESTORE_SYSTEM
109
110 #define PRIM_STGCALL6(f,a,b,c,d,e,z) \
111   CALLER_SAVE_SYSTEM (void) f(a,b,c,d,e,z); CALLER_RESTORE_SYSTEM
112
113
114 #define RET_PRIM_STGCALL0(t,f) \
115   ({ t _r; CALLER_SAVE_SYSTEM _r = f(); CALLER_RESTORE_SYSTEM; _r; })
116
117 #define RET_PRIM_STGCALL1(t,f,a) \
118   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a); CALLER_RESTORE_SYSTEM; _r; })
119
120 #define RET_PRIM_STGCALL2(t,f,a,b) \
121   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b); CALLER_RESTORE_SYSTEM; _r; })
122
123 #define RET_PRIM_STGCALL3(t,f,a,b,c) \
124   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c); CALLER_RESTORE_SYSTEM; _r; })
125
126 #define RET_PRIM_STGCALL4(t,f,a,b,c,d) \
127   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c,d); CALLER_RESTORE_SYSTEM; _r; })
128
129 #define RET_PRIM_STGCALL5(t,f,a,b,c,d,e) \
130   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c,d,e); CALLER_RESTORE_SYSTEM; _r; })
131
132 #define RET_PRIM_STGCALL6(t,f,a,b,c,d,e,z) \
133   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b,c,d,e,z); CALLER_RESTORE_SYSTEM; _r; })
134
135 /* ToDo: ccalls that might garbage collect - do we need to return to
136  * the scheduler to perform these?  Similarly, ccalls that might want
137  * to call Haskell right back, or start a new thread or something.
138  */
139
140 #endif /* CCALL_H */
141