# include "gc←private.h"
# include <stdio.h>
# include <setjmp.h>
# ifdef OS2
#   define ←setjmp(b) setjmp(b)
#   define ←longjmp(b,v) longjmp(b,v)
# endif

/* Routine to mark from registers that are preserved by the C compiler. */
/* This must be ported to every new architecture.  There is a generic   */
/* version at the end, that is likely, but not guaranteed to work       */
/* on your architecture.  Run the test←setjmp program to see whether    */
/* there is any chance it will work.                                    */
GC←mark←regs()
{
#       ifdef RT
	  register long TMP←SP; /* must be bound to r11 */
#       endif
#       ifdef VAX
	/* VAX - generic code below does not work under 4.2 */
	  /* r1 through r5 are caller save, and therefore     */
	  /* on the stack or dead.                            */
	  asm("pushl r11");     asm("calls $1,←GC←tl←mark");
	  asm("pushl r10"); 	asm("calls $1,←GC←tl←mark");
	  asm("pushl r9");	asm("calls $1,←GC←tl←mark");
	  asm("pushl r8");	asm("calls $1,←GC←tl←mark");
	  asm("pushl r7");	asm("calls $1,←GC←tl←mark");
	  asm("pushl r6");	asm("calls $1,←GC←tl←mark");
#       endif
#       if defined(M68K) && defined(SUNOS)
	/*  M68K SUNOS - could be replaced by generic code */
	  /* a0, a1 and d1 are caller save          */
	  /*  and therefore are on stack or dead.   */
	
	  asm("subqw #0x4,sp");		/* allocate word on top of stack */

	  asm("movl a2,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl a3,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl a4,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl a5,sp@");	asm("jbsr ←GC←tl←mark");
	  /* Skip frame pointer and stack pointer */
	  asm("movl d1,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d2,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d3,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d4,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d5,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d6,sp@");	asm("jbsr ←GC←tl←mark");
	  asm("movl d7,sp@");	asm("jbsr ←GC←tl←mark");

	  asm("addqw #0x4,sp");		/* put stack back where it was	*/
#       endif

#       if defined(M68K) && defined(HP)
	/*  M68K HP - could be replaced by generic code */
	  /* a0, a1 and d1 are caller save.  */
	
	  asm("subq.w &0x4,%sp");	/* allocate word on top of stack */

	  asm("mov.l %a2,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %a3,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %a4,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %a5,(%sp)"); asm("jsr ←GC←tl←mark");
	  /* Skip frame pointer and stack pointer */
	  asm("mov.l %d1,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d2,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d3,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d4,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d5,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d6,(%sp)"); asm("jsr ←GC←tl←mark");
	  asm("mov.l %d7,(%sp)"); asm("jsr ←GC←tl←mark");

	  asm("addq.w &0x4,%sp");	/* put stack back where it was	*/
#       endif /* M68K HP */

#       if defined(I386) && !defined(OS2) && !defined(SUNOS5)
	/* I386 code, generic code does not appear to work */
	/* It does appear to work under OS2, and asms dont */
	  asm("pushl %eax");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %ecx");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %edx");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %esi");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %edi");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %ebx");  asm("call ←GC←tl←mark"); asm("addl $4,%esp");
#       endif

#       if defined(I386) && defined(SUNOS5)
	/* I386 code, generic code does not appear to work */
	/* It does appear to work under OS2, and asms dont */
	  asm("pushl %eax");  asm("call GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %ecx");  asm("call GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %edx");  asm("call GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %esi");  asm("call GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %edi");  asm("call GC←tl←mark"); asm("addl $4,%esp");
	  asm("pushl %ebx");  asm("call GC←tl←mark"); asm("addl $4,%esp");
#       endif

#       ifdef NS32K
	  asm ("movd r3, tos"); asm ("bsr ?←GC←tl←mark"); asm ("adjspb $-4");
	  asm ("movd r4, tos"); asm ("bsr ?←GC←tl←mark"); asm ("adjspb $-4");
	  asm ("movd r5, tos"); asm ("bsr ?←GC←tl←mark"); asm ("adjspb $-4");
	  asm ("movd r6, tos"); asm ("bsr ?←GC←tl←mark"); asm ("adjspb $-4");
	  asm ("movd r7, tos"); asm ("bsr ?←GC←tl←mark"); asm ("adjspb $-4");
#       endif

#       ifdef SPARC
	  /* generic code will not work */
	  GC←save←regs←in←stack();
#       endif

#	ifdef RT
	    GC←tl←mark(TMP←SP);    /* GC←tl←mark from r11 */

	    asm("cas r11, r6, r0"); GC←tl←mark(TMP←SP);	/* r6 */
	    asm("cas r11, r7, r0"); GC←tl←mark(TMP←SP);	/* through */
	    asm("cas r11, r8, r0"); GC←tl←mark(TMP←SP);	/* r10 */
	    asm("cas r11, r9, r0"); GC←tl←mark(TMP←SP);
	    asm("cas r11, r10, r0"); GC←tl←mark(TMP←SP);

	    asm("cas r11, r12, r0"); GC←tl←mark(TMP←SP); /* r12 */
	    asm("cas r11, r13, r0"); GC←tl←mark(TMP←SP); /* through */
	    asm("cas r11, r14, r0"); GC←tl←mark(TMP←SP); /* r15 */
	    asm("cas r11, r15, r0"); GC←tl←mark(TMP←SP);
#       endif

#       if defined(M68K) && defined(SYSV)
  	/*  Once again similar to SUN and HP, though setjmp appears to work.
  		--Parag
  	 */
#        ifdef ←←GNUC←←
  	  asm("subqw #0x4,%sp");	/* allocate word on top of stack */
  
  	  asm("movl %a2,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %a3,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %a4,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %a5,%sp@");	asm("jbsr GC←tl←mark");
  	  /* Skip frame pointer and stack pointer */
  	  asm("movl %d1,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d2,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d3,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d4,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d5,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d6,%sp@");	asm("jbsr GC←tl←mark");
  	  asm("movl %d7,%sp@");	asm("jbsr GC←tl←mark");
  
  	  asm("addqw #0x4,%sp");	/* put stack back where it was	*/
#        else /* !←←GNUC←←*/
  	  asm("subq.w &0x4,%sp");	/* allocate word on top of stack */
  
  	  asm("mov.l %a2,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %a3,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %a4,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %a5,(%sp)"); asm("jsr GC←tl←mark");
  	  /* Skip frame pointer and stack pointer */
  	  asm("mov.l %d1,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %d2,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %d3,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %d4,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %d5,(%sp)"); asm("jsr GC←tl←mark");
   	  asm("mov.l %d6,(%sp)"); asm("jsr GC←tl←mark");
  	  asm("mov.l %d7,(%sp)"); asm("jsr GC←tl←mark");
  
  	  asm("addq.w &0x4,%sp");	/* put stack back where it was	*/
#        endif /* !←←GNUC←← */
#       endif /* M68K/SYSV */


#     if defined(HP←PA) || (defined(I386) && defined(OS2))
	/* Generic code                          */
	/* The idea is due to Parag Patel at HP. */
	/* We're not sure whether he would like  */
	/* to be he acknowledged for it or not.  */
	{
	    jmp←buf regs;
	    register word * i = (word *) regs;
	    register ptr←t lim = (ptr←t)(regs) + (sizeof regs);

	    /* Setjmp on Sun 3s doesn't clear all of the buffer.  */
	    /* That tends to preserve garbage.  Clear it.         */
		for (; (char *)i < lim; i++) {
		    *i = 0;
		}
	    (void) ←setjmp(regs);
	    GC←mark←all←stack((ptr←t)regs, lim);
	}
#     endif

      /* other machines... */
#       if !(defined M68K) && !(defined VAX) && !(defined RT) 
#	if !(defined SPARC) && !(defined I386) &&!(defined NS32K)
#	if !defined(HP←PA)
	    --> bad news <--
#       endif
#       endif
#       endif
}

/* On register window machines, we need a way to force registers into 	*/
/* the stack.								*/
# ifdef SPARC
    asm("	.seg 	\"text\"");
#   ifdef SUNOS5
      asm("	.globl	GC←save←regs←in←stack");
      asm("GC←save←regs←in←stack:");
#   else
      asm("	.globl	←GC←save←regs←in←stack");
      asm("←GC←save←regs←in←stack:");
#   endif
    asm("	ta	0x3   ! ST←FLUSH←WINDOWS");
    asm("	mov	%sp,%o0");
    asm("	retl");
    asm("	nop");
# endif