[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / includes / CCall.h
1 /* -----------------------------------------------------------------------------
2  * $Id: CCall.h,v 1.2 1998/12/02 13:20:53 simonm Exp $
3  *
4  * Macros for performing C calls from the STG world.
5  * -------------------------------------------------------------------------- */
6
7 #ifndef CCALL_H
8 #define CCALL_H
9
10 /* 
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.
14  *
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.
18  * 
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.
24  */
25
26 /* Unsafe C-Calls have no macros: we just use a straightforward call.
27  */
28
29 /*
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
34  * the result!)
35  */
36
37 #define STGCALL0(f) \
38   CALLER_SAVE_ALL (void) f(); CALLER_RESTORE_ALL
39
40 #define STGCALL1(f,a) \
41   CALLER_SAVE_ALL (void) f(a); CALLER_RESTORE_ALL
42
43 #define STGCALL2(f,a,b) \
44   CALLER_SAVE_ALL (void) f(a,b); CALLER_RESTORE_ALL
45
46 #define STGCALL3(f,a,b,c) \
47   CALLER_SAVE_ALL (void) f(a,b,c); CALLER_RESTORE_ALL
48
49 #define STGCALL4(f,a,b,c,d) \
50   CALLER_SAVE_ALL (void) f(a,b,c,d); CALLER_RESTORE_ALL
51
52 #define STGCALL5(f,a,b,c,d,e) \
53   CALLER_SAVE_ALL (void) f(a,b,c,d,e); CALLER_RESTORE_ALL
54
55
56 #define RET_STGCALL0(t,f) \
57   ({ t _r; CALLER_SAVE_ALL _r = f(); CALLER_RESTORE_ALL; _r; })
58
59 #define RET_STGCALL1(t,f,a) \
60   ({ t _r; CALLER_SAVE_ALL _r = f(a); CALLER_RESTORE_ALL; _r; })
61
62 #define RET_STGCALL2(t,f,a,b) \
63   ({ t _r; CALLER_SAVE_ALL _r = f(a,b); CALLER_RESTORE_ALL; _r; })
64
65 #define RET_STGCALL3(t,f,a,b,c) \
66   ({ t _r; CALLER_SAVE_ALL _r = f(a,b,c); CALLER_RESTORE_ALL; _r; })
67
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; })
70
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; })
73
74
75 /*
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.
81  */
82
83 #define PRIM_STGCALL0(f) \
84   CALLER_SAVE_SYSTEM (void) f(); CALLER_RESTORE_SYSTEM
85
86 #define PRIM_STGCALL1(f,a) \
87   CALLER_SAVE_SYSTEM (void) f(a); CALLER_RESTORE_SYSTEM
88
89 #define PRIM_STGCALL2(f,a,b) \
90   CALLER_SAVE_SYSTEM (void) f(a,b); CALLER_RESTORE_SYSTEM
91
92 #define PRIM_STGCALL3(f,a,b,c) \
93   CALLER_SAVE_SYSTEM (void) f(a,b,c); CALLER_RESTORE_SYSTEM
94
95 #define PRIM_STGCALL4(f,a,b,c,d) \
96   CALLER_SAVE_SYSTEM (void) f(a,b,c,d); CALLER_RESTORE_SYSTEM
97
98 #define PRIM_STGCALL5(f,a,b,c,d,e) \
99   CALLER_SAVE_SYSTEM (void) f(a,b,c,d,e); CALLER_RESTORE_SYSTEM
100
101
102 #define RET_PRIM_STGCALL0(t,f) \
103   ({ t _r; CALLER_SAVE_SYSTEM _r = f(); CALLER_RESTORE_SYSTEM; _r; })
104
105 #define RET_PRIM_STGCALL1(t,f,a) \
106   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a); CALLER_RESTORE_SYSTEM; _r; })
107
108 #define RET_PRIM_STGCALL2(t,f,a,b) \
109   ({ t _r; CALLER_SAVE_SYSTEM _r = f(a,b); CALLER_RESTORE_SYSTEM; _r; })
110
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; })
113
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; })
116
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; })
119
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.
123  */
124
125 #endif /* CCALL_H */
126