2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
15 #include "private/gc_priv.h"
23 # include <machine/reg.h>
27 #if defined(MACOS) && defined(__MWERKS__)
31 # define NONVOLATILE_GPR_COUNT 19
32 struct ppc_registers
{
33 unsigned long gprs
[NONVOLATILE_GPR_COUNT
]; /* R13-R31 */
35 typedef struct ppc_registers ppc_registers
;
37 asm static void getRegisters(register ppc_registers
* regs
)
39 stmw r13
,regs
->gprs
/* save R13-R31 */
43 static void PushMacRegisters(void)
48 for (i
= 0; i
< NONVOLATILE_GPR_COUNT
; i
++)
49 GC_push_one(regs
.gprs
[i
]);
54 asm static void PushMacRegisters(void)
56 sub
.w
#4,sp /* reserve space for one parameter */
63 # if !__option(a6frames)
64 /* <pcb> perhaps a6 should be pushed if stack frames are not being used */
68 /* skip a5 (globals), a6 (frame pointer), and a7 (stack pointer) */
81 add
.w
#4,sp /* fix stack */
87 #endif /* MACOS && __MWERKS__ */
89 # if defined(SPARC) || defined(IA64)
90 /* Value returned from register flushing routine; either sp (SPARC) */
91 /* or ar.bsp (IA64). */
92 GC_INNER ptr_t GC_save_regs_ret_val
= NULL
;
95 /* Routine to mark from registers that are preserved by the C compiler. */
96 /* This must be ported to every new architecture. It is not optional, */
97 /* and should not be used on platforms that are either UNIX-like, or */
98 /* require thread support. */
100 #undef HAVE_PUSH_REGS
102 #if defined(USE_ASM_PUSH_REGS)
103 # define HAVE_PUSH_REGS
104 #else /* No asm implementation */
106 # if defined(M68K) && defined(AMIGA)
107 /* This function is not static because it could also be */
108 /* errorneously defined in .S file, so this error would be caught */
110 void GC_push_regs(void)
112 /* AMIGA - could be replaced by generic code */
113 /* a0, a1, d0 and d1 are caller save */
116 asm("subq.w &0x4,%sp"); /* allocate word on top of stack */
118 asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
119 asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
120 asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
121 asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
122 asm("mov.l %a6,(%sp)"); asm("jsr _GC_push_one");
123 /* Skip frame pointer and stack pointer */
124 asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
125 asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
126 asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
127 asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
128 asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
129 asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");
131 asm("addq.w &0x4,%sp"); /* put stack back where it was */
132 # else /* !__GNUC__ */
133 GC_push_one(getreg(REG_A2
));
134 GC_push_one(getreg(REG_A3
));
136 /* Can probably be changed to #if 0 -Kjetil M. (a4=globals) */
137 GC_push_one(getreg(REG_A4
));
139 GC_push_one(getreg(REG_A5
));
140 GC_push_one(getreg(REG_A6
));
141 /* Skip stack pointer */
142 GC_push_one(getreg(REG_D2
));
143 GC_push_one(getreg(REG_D3
));
144 GC_push_one(getreg(REG_D4
));
145 GC_push_one(getreg(REG_D5
));
146 GC_push_one(getreg(REG_D6
));
147 GC_push_one(getreg(REG_D7
));
148 # endif /* !__GNUC__ */
150 # define HAVE_PUSH_REGS
152 # elif defined(MACOS)
154 # if defined(M68K) && defined(THINK_C)
155 # define PushMacReg(reg) \
158 void GC_push_regs(void)
161 sub
.w
#4,sp ; reserve space for one parameter.
165 ; skip
a5 (globals
), a6 (frame pointer
), and a7 (stack pointer
)
172 add
.w
#4,sp ; fix stack.
175 # define HAVE_PUSH_REGS
177 # elif defined(__MWERKS__)
178 void GC_push_regs(void)
182 # define HAVE_PUSH_REGS
183 # endif /* __MWERKS__ */
186 #endif /* !USE_ASM_PUSH_REGS */
188 #if defined(HAVE_PUSH_REGS) && defined(THREADS)
189 # error GC_push_regs cannot be used with threads
190 /* Would fail for GC_do_blocking. There are probably other safety */
192 # undef HAVE_PUSH_REGS
195 #if !defined(HAVE_PUSH_REGS) && defined(UNIX_LIKE)
197 # ifndef NO_GETCONTEXT
198 # if defined(DARWIN) \
199 && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 /*MAC_OS_X_VERSION_10_6*/)
200 # include <sys/ucontext.h>
202 # include <ucontext.h>
203 # endif /* !DARWIN */
204 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
208 #endif /* !HAVE_PUSH_REGS */
210 /* Ensure that either registers are pushed, or callee-save registers */
211 /* are somewhere on the stack, and then call fn(arg, ctxt). */
212 /* ctxt is either a pointer to a ucontext_t we generated, or NULL. */
213 GC_INNER
void GC_with_callee_saves_pushed(void (*fn
)(ptr_t
, void *),
219 # if defined(HAVE_PUSH_REGS)
221 # elif defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
222 /* Older versions of Darwin seem to lack getcontext(). */
223 /* ARM and MIPS Linux often doesn't support a real */
226 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
227 /* Workaround a bug (clearing the FPU exception mask) in */
228 /* getcontext on Linux/x86_64. */
230 /* We manipulate FPU control word here just not to force the */
231 /* client application to use -lm linker option. */
232 unsigned short old_fcw
;
233 __asm__
__volatile__ ("fstcw %0" : "=m" (*&old_fcw
));
235 int except_mask
= fegetexcept();
238 if (getcontext(&ctxt
) < 0)
239 ABORT ("getcontext failed: Use another register retrieval method?");
240 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
242 __asm__
__volatile__ ("fldcw %0" : : "m" (*&old_fcw
));
245 /* And now correct the exception mask in SSE MXCSR. */
246 __asm__
__volatile__ ("stmxcsr %0" : "=m" (*&mxcsr
));
247 mxcsr
= (mxcsr
& ~(FE_ALL_EXCEPT
<< 7)) |
248 ((old_fcw
& FE_ALL_EXCEPT
) << 7);
249 __asm__
__volatile__ ("ldmxcsr %0" : : "m" (*&mxcsr
));
252 if (feenableexcept(except_mask
) < 0)
253 ABORT("feenableexcept failed");
257 # if defined(SPARC) || defined(IA64)
258 /* On a register window machine, we need to save register */
259 /* contents on the stack for this to work. This may already be */
260 /* subsumed by the getcontext() call. */
261 GC_save_regs_ret_val
= GC_save_regs_in_stack();
262 # endif /* register windows. */
263 # elif defined(HAVE_BUILTIN_UNWIND_INIT)
264 /* This was suggested by Richard Henderson as the way to */
265 /* force callee-save registers and register windows onto */
267 __builtin_unwind_init();
268 # else /* !HAVE_BUILTIN_UNWIND_INIT && !UNIX_LIKE */
269 /* && !HAVE_PUSH_REGS */
271 /* The idea is due to Parag Patel at HP. */
272 /* We're not sure whether he would like */
273 /* to be acknowledged for it or not. */
275 register word
* i
= (word
*) regs
;
276 register ptr_t lim
= (ptr_t
)(regs
) + (sizeof regs
);
278 /* Setjmp doesn't always clear all of the buffer. */
279 /* That tends to preserve garbage. Clear it. */
280 for (; (word
)i
< (word
)lim
; i
++) {
283 # if defined(MSWIN32) || defined(MSWINCE) || defined(UTS4) \
284 || defined(OS2) || defined(CX_UX) || defined(__CC_ARM) \
285 || defined(LINUX) || defined(EWS4800) || defined(RTEMS)
288 (void) _setjmp(regs
);
289 /* We don't want to mess with signals. According to */
290 /* SUSV3, setjmp() may or may not save signal mask. */
291 /* _setjmp won't, but is less portable. */
293 # endif /* !HAVE_PUSH_REGS ... */
294 /* FIXME: context here is sometimes just zero. At the moment the */
295 /* callees don't really need it. */
297 /* Strongly discourage the compiler from treating the above */
298 /* as a tail-call, since that would pop the register */
299 /* contents before we get a chance to look at them. */
300 GC_noop1((word
)(&dummy
));
303 #if defined(ASM_CLEAR_CODE)
305 ptr_t
GC_clear_stack_inner(ptr_t arg
, word limit
)
307 return limit
? arg
: 0; /* use both arguments */
309 /* The real version is in a .S file */
311 #endif /* ASM_CLEAR_CODE */