Removed extra reference to packihx
[fw/sdcc] / support / gc / mach_dep.c
1 /* 
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  *
5  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
7  *
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.
13  */
14 /* Boehm, November 17, 1995 12:13 pm PST */
15 # include "gc_priv.h"
16 # include <stdio.h>
17 # include <setjmp.h>
18 # if defined(OS2) || defined(CX_UX)
19 #   define _setjmp(b) setjmp(b)
20 #   define _longjmp(b,v) longjmp(b,v)
21 # endif
22 # ifdef AMIGA
23 #   ifndef __GNUC__
24 #     include <dos/dos.h>
25 #   else
26 #     include <machine/reg.h>
27 #   endif
28 # endif
29
30 #if defined(__MWERKS__) && !defined(POWERPC)
31
32 asm static void PushMacRegisters()
33 {
34     sub.w   #4,sp                   // reserve space for one parameter.
35     move.l  a2,(sp)
36     jsr         GC_push_one
37     move.l  a3,(sp)
38     jsr         GC_push_one
39     move.l  a4,(sp)
40     jsr         GC_push_one
41 #   if !__option(a6frames)
42         // <pcb> perhaps a6 should be pushed if stack frames are not being used.    
43         move.l  a6,(sp)
44         jsr             GC_push_one
45 #   endif
46         // skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
47     move.l  d2,(sp)
48     jsr         GC_push_one
49     move.l  d3,(sp)
50     jsr         GC_push_one
51     move.l  d4,(sp)
52     jsr         GC_push_one
53     move.l  d5,(sp)
54     jsr         GC_push_one
55     move.l  d6,(sp)
56     jsr         GC_push_one
57     move.l  d7,(sp)
58     jsr         GC_push_one
59     add.w   #4,sp                   // fix stack.
60     rts
61 }
62
63 #endif /* __MWERKS__ */
64
65 /* Routine to mark from registers that are preserved by the C compiler. */
66 /* This must be ported to every new architecture.  There is a generic   */
67 /* version at the end, that is likely, but not guaranteed to work       */
68 /* on your architecture.  Run the test_setjmp program to see whether    */
69 /* there is any chance it will work.                                    */
70
71 #ifndef USE_GENERIC_PUSH_REGS
72 void GC_push_regs()
73 {
74 #       ifdef RT
75           register long TMP_SP; /* must be bound to r11 */
76 #       endif
77 #       ifdef VAX
78           /* VAX - generic code below does not work under 4.2 */
79           /* r1 through r5 are caller save, and therefore     */
80           /* on the stack or dead.                            */
81           asm("pushl r11");     asm("calls $1,_GC_push_one");
82           asm("pushl r10");     asm("calls $1,_GC_push_one");
83           asm("pushl r9");      asm("calls $1,_GC_push_one");
84           asm("pushl r8");      asm("calls $1,_GC_push_one");
85           asm("pushl r7");      asm("calls $1,_GC_push_one");
86           asm("pushl r6");      asm("calls $1,_GC_push_one");
87 #       endif
88 #       if defined(M68K) && (defined(SUNOS4) || defined(NEXT))
89         /*  M68K SUNOS - could be replaced by generic code */
90           /* a0, a1 and d1 are caller save          */
91           /*  and therefore are on stack or dead.   */
92         
93           asm("subqw #0x4,sp");         /* allocate word on top of stack */
94
95           asm("movl a2,sp@");   asm("jbsr _GC_push_one");
96           asm("movl a3,sp@");   asm("jbsr _GC_push_one");
97           asm("movl a4,sp@");   asm("jbsr _GC_push_one");
98           asm("movl a5,sp@");   asm("jbsr _GC_push_one");
99           /* Skip frame pointer and stack pointer */
100           asm("movl d1,sp@");   asm("jbsr _GC_push_one");
101           asm("movl d2,sp@");   asm("jbsr _GC_push_one");
102           asm("movl d3,sp@");   asm("jbsr _GC_push_one");
103           asm("movl d4,sp@");   asm("jbsr _GC_push_one");
104           asm("movl d5,sp@");   asm("jbsr _GC_push_one");
105           asm("movl d6,sp@");   asm("jbsr _GC_push_one");
106           asm("movl d7,sp@");   asm("jbsr _GC_push_one");
107
108           asm("addqw #0x4,sp");         /* put stack back where it was  */
109 #       endif
110
111 #       if defined(M68K) && defined(HP)
112         /*  M68K HP - could be replaced by generic code */
113           /* a0, a1 and d1 are caller save.  */
114         
115           asm("subq.w &0x4,%sp");       /* allocate word on top of stack */
116
117           asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
118           asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
119           asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
120           asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
121           /* Skip frame pointer and stack pointer */
122           asm("mov.l %d1,(%sp)"); asm("jsr _GC_push_one");
123           asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
124           asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
125           asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
126           asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
127           asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
128           asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");
129
130           asm("addq.w &0x4,%sp");       /* put stack back where it was  */
131 #       endif /* M68K HP */
132
133 #       if defined(M68K) && defined(AMIGA)
134          /*  AMIGA - could be replaced by generic code                  */
135          /* a0, a1, d0 and d1 are caller save */
136
137 #        ifdef __GNUC__
138           asm("subq.w &0x4,%sp");       /* allocate word on top of stack */
139
140           asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
141           asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
142           asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
143           asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
144           asm("mov.l %a6,(%sp)"); asm("jsr _GC_push_one");
145           /* Skip frame pointer and stack pointer */
146           asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
147           asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
148           asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
149           asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
150           asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
151           asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");
152
153           asm("addq.w &0x4,%sp");       /* put stack back where it was  */
154 #        else /* !__GNUC__ */
155           GC_push_one(getreg(REG_A2));
156           GC_push_one(getreg(REG_A3));
157           GC_push_one(getreg(REG_A4));
158           GC_push_one(getreg(REG_A5));
159           GC_push_one(getreg(REG_A6));
160           /* Skip stack pointer */
161           GC_push_one(getreg(REG_D2));
162           GC_push_one(getreg(REG_D3));
163           GC_push_one(getreg(REG_D4));
164           GC_push_one(getreg(REG_D5));
165           GC_push_one(getreg(REG_D6));
166           GC_push_one(getreg(REG_D7));
167 #        endif /* !__GNUC__ */
168 #       endif /* AMIGA */
169
170 #       if defined(M68K) && defined(MACOS)
171 #       if defined(THINK_C)
172 #         define PushMacReg(reg) \
173               move.l  reg,(sp) \
174               jsr             GC_push_one
175           asm {
176               sub.w   #4,sp                   ; reserve space for one parameter.
177               PushMacReg(a2);
178               PushMacReg(a3);
179               PushMacReg(a4);
180               ; skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
181               PushMacReg(d2);
182               PushMacReg(d3);
183               PushMacReg(d4);
184               PushMacReg(d5);
185               PushMacReg(d6);
186               PushMacReg(d7);
187               add.w   #4,sp                   ; fix stack.
188           }
189 #         undef PushMacReg
190 #       endif /* THINK_C */
191 #       if defined(__MWERKS__)
192           PushMacRegisters();
193 #       endif   /* __MWERKS__ */
194 #   endif       /* MACOS */
195
196 #       if defined(I386) &&!defined(OS2) &&!defined(SVR4) &&!defined(MSWIN32) \
197         && !defined(SCO) && !defined(SCO_ELF) \
198         && !(defined(LINUX)       && defined(__ELF__)) \
199         && !(defined(__FreeBSD__) && defined(__ELF__)) \
200         && !defined(DOS4GW)
201         /* I386 code, generic code does not appear to work */
202         /* It does appear to work under OS2, and asms dont */
203         /* This is used for some 38g UNIX variants and for CYGWIN32 */
204           asm("pushl %eax");  asm("call _GC_push_one"); asm("addl $4,%esp");
205           asm("pushl %ecx");  asm("call _GC_push_one"); asm("addl $4,%esp");
206           asm("pushl %edx");  asm("call _GC_push_one"); asm("addl $4,%esp");
207           asm("pushl %ebp");  asm("call _GC_push_one"); asm("addl $4,%esp");
208           asm("pushl %esi");  asm("call _GC_push_one"); asm("addl $4,%esp");
209           asm("pushl %edi");  asm("call _GC_push_one"); asm("addl $4,%esp");
210           asm("pushl %ebx");  asm("call _GC_push_one"); asm("addl $4,%esp");
211 #       endif
212
213 #       if ( defined(I386) && defined(LINUX) && defined(__ELF__) ) \
214         || ( defined(I386) && defined(__FreeBSD__) && defined(__ELF__) )
215
216         /* This is modified for Linux with ELF (Note: _ELF_ only) */
217         /* This section handles FreeBSD with ELF. */
218           asm("pushl %eax");  asm("call GC_push_one"); asm("addl $4,%esp");
219           asm("pushl %ecx");  asm("call GC_push_one"); asm("addl $4,%esp");
220           asm("pushl %edx");  asm("call GC_push_one"); asm("addl $4,%esp");
221           asm("pushl %ebp");  asm("call GC_push_one"); asm("addl $4,%esp");
222           asm("pushl %esi");  asm("call GC_push_one"); asm("addl $4,%esp");
223           asm("pushl %edi");  asm("call GC_push_one"); asm("addl $4,%esp");
224           asm("pushl %ebx");  asm("call GC_push_one"); asm("addl $4,%esp");
225 #       endif
226
227 #       if defined(I386) && defined(__MINGW32__)
228         /* I386 code, generic code does not appear to work */
229         /* It does appear to work under OS2, and asms dont */
230         /* This is used for some 38g UNIX variants and for CYGWIN32 */
231           asm("pushl %eax");  asm("call _GC_push_one"); asm("addl $4,%esp");
232           asm("pushl %ecx");  asm("call _GC_push_one"); asm("addl $4,%esp");
233           asm("pushl %edx");  asm("call _GC_push_one"); asm("addl $4,%esp");
234           asm("pushl %ebp");  asm("call _GC_push_one"); asm("addl $4,%esp");
235           asm("pushl %esi");  asm("call _GC_push_one"); asm("addl $4,%esp");
236           asm("pushl %edi");  asm("call _GC_push_one"); asm("addl $4,%esp");
237           asm("pushl %ebx");  asm("call _GC_push_one"); asm("addl $4,%esp");
238 #       endif
239
240 #       if defined(I386) && defined(MSWIN32) && !defined(USE_GENERIC) && !defined(__MINGW32__)
241         /* I386 code, Microsoft variant         */
242           __asm  push eax
243           __asm  call GC_push_one
244           __asm  add esp,4
245           __asm  push ebx
246           __asm  call GC_push_one
247           __asm  add esp,4
248           __asm  push ecx
249           __asm  call GC_push_one
250           __asm  add esp,4
251           __asm  push edx
252           __asm  call GC_push_one
253           __asm  add esp,4
254           __asm  push ebp
255           __asm  call GC_push_one
256           __asm  add esp,4
257           __asm  push esi
258           __asm  call GC_push_one
259           __asm  add esp,4
260           __asm  push edi
261           __asm  call GC_push_one
262           __asm  add esp,4
263 #       endif
264
265 #       if defined(I386) && (defined(SVR4) || defined(SCO) || defined(SCO_ELF))
266         /* I386 code, SVR4 variant, generic code does not appear to work */
267           asm("pushl %eax");  asm("call GC_push_one"); asm("addl $4,%esp");
268           asm("pushl %ebx");  asm("call GC_push_one"); asm("addl $4,%esp");
269           asm("pushl %ecx");  asm("call GC_push_one"); asm("addl $4,%esp");
270           asm("pushl %edx");  asm("call GC_push_one"); asm("addl $4,%esp");
271           asm("pushl %ebp");  asm("call GC_push_one"); asm("addl $4,%esp");
272           asm("pushl %esi");  asm("call GC_push_one"); asm("addl $4,%esp");
273           asm("pushl %edi");  asm("call GC_push_one"); asm("addl $4,%esp");
274 #       endif
275
276 #       ifdef NS32K
277           asm ("movd r3, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
278           asm ("movd r4, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
279           asm ("movd r5, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
280           asm ("movd r6, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
281           asm ("movd r7, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
282 #       endif
283
284 #       ifdef SPARC
285           {
286               word GC_save_regs_in_stack();
287               
288               /* generic code will not work */
289               (void)GC_save_regs_in_stack();
290           }
291 #       endif
292
293 #       ifdef RT
294             GC_push_one(TMP_SP);    /* GC_push_one from r11 */
295
296             asm("cas r11, r6, r0"); GC_push_one(TMP_SP);        /* r6 */
297             asm("cas r11, r7, r0"); GC_push_one(TMP_SP);        /* through */
298             asm("cas r11, r8, r0"); GC_push_one(TMP_SP);        /* r10 */
299             asm("cas r11, r9, r0"); GC_push_one(TMP_SP);
300             asm("cas r11, r10, r0"); GC_push_one(TMP_SP);
301
302             asm("cas r11, r12, r0"); GC_push_one(TMP_SP); /* r12 */
303             asm("cas r11, r13, r0"); GC_push_one(TMP_SP); /* through */
304             asm("cas r11, r14, r0"); GC_push_one(TMP_SP); /* r15 */
305             asm("cas r11, r15, r0"); GC_push_one(TMP_SP);
306 #       endif
307
308 #       if defined(M68K) && defined(SYSV)
309         /*  Once again similar to SUN and HP, though setjmp appears to work.
310                 --Parag
311          */
312 #        ifdef __GNUC__
313           asm("subqw #0x4,%sp");        /* allocate word on top of stack */
314   
315           asm("movl %a2,%sp@"); asm("jbsr GC_push_one");
316           asm("movl %a3,%sp@"); asm("jbsr GC_push_one");
317           asm("movl %a4,%sp@"); asm("jbsr GC_push_one");
318           asm("movl %a5,%sp@"); asm("jbsr GC_push_one");
319           /* Skip frame pointer and stack pointer */
320           asm("movl %d1,%sp@"); asm("jbsr GC_push_one");
321           asm("movl %d2,%sp@"); asm("jbsr GC_push_one");
322           asm("movl %d3,%sp@"); asm("jbsr GC_push_one");
323           asm("movl %d4,%sp@"); asm("jbsr GC_push_one");
324           asm("movl %d5,%sp@"); asm("jbsr GC_push_one");
325           asm("movl %d6,%sp@"); asm("jbsr GC_push_one");
326           asm("movl %d7,%sp@"); asm("jbsr GC_push_one");
327   
328           asm("addqw #0x4,%sp");        /* put stack back where it was  */
329 #        else /* !__GNUC__*/
330           asm("subq.w &0x4,%sp");       /* allocate word on top of stack */
331   
332           asm("mov.l %a2,(%sp)"); asm("jsr GC_push_one");
333           asm("mov.l %a3,(%sp)"); asm("jsr GC_push_one");
334           asm("mov.l %a4,(%sp)"); asm("jsr GC_push_one");
335           asm("mov.l %a5,(%sp)"); asm("jsr GC_push_one");
336           /* Skip frame pointer and stack pointer */
337           asm("mov.l %d1,(%sp)"); asm("jsr GC_push_one");
338           asm("mov.l %d2,(%sp)"); asm("jsr GC_push_one");
339           asm("mov.l %d3,(%sp)"); asm("jsr GC_push_one");
340           asm("mov.l %d4,(%sp)"); asm("jsr GC_push_one");
341           asm("mov.l %d5,(%sp)"); asm("jsr GC_push_one");
342           asm("mov.l %d6,(%sp)"); asm("jsr GC_push_one");
343           asm("mov.l %d7,(%sp)"); asm("jsr GC_push_one");
344   
345           asm("addq.w &0x4,%sp");       /* put stack back where it was  */
346 #        endif /* !__GNUC__ */
347 #       endif /* M68K/SYSV */
348
349
350       /* other machines... */
351 #       if !(defined M68K) && !(defined VAX) && !(defined RT) 
352 #       if !(defined SPARC) && !(defined I386) && !(defined NS32K)
353 #       if !defined(POWERPC) && !defined(UTS4)
354             --> bad news <--
355 #       endif
356 #       endif
357 #       endif
358 }
359 #endif /* !USE_GENERIC_PUSH_REGS */
360
361 #if defined(USE_GENERIC_PUSH_REGS)
362 void GC_generic_push_regs(cold_gc_frame)
363 ptr_t cold_gc_frame;
364 {
365         /* Generic code                          */
366         /* The idea is due to Parag Patel at HP. */
367         /* We're not sure whether he would like  */
368         /* to be he acknowledged for it or not.  */
369         {
370             static jmp_buf regs;
371             register word * i = (word *) regs;
372             register ptr_t lim = (ptr_t)(regs) + (sizeof regs);
373
374             /* Setjmp on Sun 3s doesn't clear all of the buffer.  */
375             /* That tends to preserve garbage.  Clear it.         */
376                 for (; (char *)i < lim; i++) {
377                     *i = 0;
378                 }
379 #           if defined(POWERPC) || defined(MSWIN32) || defined(UTS4)
380                 (void) setjmp(regs);
381 #           else
382                 (void) _setjmp(regs);
383 #           endif
384             GC_push_current_stack(cold_gc_frame);
385         }
386 }
387 #endif /* USE_GENERIC_PUSH_REGS */
388
389 /* On register window machines, we need a way to force registers into   */
390 /* the stack.   Return sp.                                              */
391 # ifdef SPARC
392     asm("       .seg    \"text\"");
393 #   ifdef SVR4
394       asm("     .globl  GC_save_regs_in_stack");
395       asm("GC_save_regs_in_stack:");
396       asm("     .type GC_save_regs_in_stack,#function");
397 #   else
398       asm("     .globl  _GC_save_regs_in_stack");
399       asm("_GC_save_regs_in_stack:");
400 #   endif
401     asm("       ta      0x3   ! ST_FLUSH_WINDOWS");
402     asm("       mov     %sp,%o0");
403     asm("       retl");
404     asm("       nop");
405 #   ifdef SVR4
406       asm("     .GC_save_regs_in_stack_end:");
407       asm("     .size GC_save_regs_in_stack,.GC_save_regs_in_stack_end-GC_save_regs_in_stack");
408 #   endif
409 #   ifdef LINT
410         word GC_save_regs_in_stack() { return(0 /* sp really */);}
411 #   endif
412 # endif
413
414
415 /* GC_clear_stack_inner(arg, limit) clears stack area up to limit and   */
416 /* returns arg.  Stack clearing is crucial on SPARC, so we supply       */
417 /* an assembly version that's more careful.  Assumes limit is hotter    */
418 /* than sp, and limit is 8 byte aligned.                                */
419 #if defined(ASM_CLEAR_CODE) && !defined(THREADS)
420 #ifndef SPARC
421         --> fix it
422 #endif
423 # ifdef SUNOS4
424     asm(".globl _GC_clear_stack_inner");
425     asm("_GC_clear_stack_inner:");
426 # else
427     asm(".globl GC_clear_stack_inner");
428     asm("GC_clear_stack_inner:");
429     asm(".type GC_save_regs_in_stack,#function");
430 # endif
431   asm("mov %sp,%o2");           /* Save sp      */
432   asm("add %sp,-8,%o3");        /* p = sp-8     */
433   asm("clr %g1");               /* [g0,g1] = 0  */
434   asm("add %o1,-0x60,%sp");     /* Move sp out of the way,      */
435                                 /* so that traps still work.    */
436                                 /* Includes some extra words    */
437                                 /* so we can be sloppy below.   */
438   asm("loop:");
439   asm("std %g0,[%o3]");         /* *(long long *)p = 0  */
440   asm("cmp %o3,%o1");
441   asm("bgu loop ");             /* if (p > limit) goto loop     */
442     asm("add %o3,-8,%o3");      /* p -= 8 (delay slot) */
443   asm("retl");
444     asm("mov %o2,%sp");         /* Restore sp., delay slot      */
445   /* First argument = %o0 = return value */
446 #   ifdef SVR4
447       asm("     .GC_clear_stack_inner_end:");
448       asm("     .size GC_clear_stack_inner,.GC_clear_stack_inner_end-GC_clear_stack_inner");
449 #   endif
450   
451 # ifdef LINT
452     /*ARGSUSED*/
453     ptr_t GC_clear_stack_inner(arg, limit)
454     ptr_t arg; word limit;
455     { return(arg); }
456 # endif
457 #endif