<commit_before>/*==================================================================*
 |			gc.c                 
 |		Copyright (c) 1987-1995 Applied Logic Systems, Inc.
 |
 |			-- garbage compactor for ALS Prolog
 |
 | Author:  <NAME>
 | Creation Date: 5/24/87
 | Revision History:
 | 08/02/88 - kev -- 88k port
 | 04/25/89 - kev -- port back to sun
 | 10/04/90 - kev -- removed machine-specific designations in port to SPARC
 | 05/14/91 - raman -- amiga port (heap/stack high mem)
 | 04/03/93 - raman -- made it generic
 | 10/27/93 - C. Houpt -- Use ASCII Beep (7) for gcbeep on Mac.
 *==================================================================*/
#include "defs.h"
#include "machinst.h"
#include "wintcode.h"
#ifdef FREEZE
#include "freeze.h"
#endif

/*
#define DEBUGFREEZE 1
#define GCDEBUG 1
*/

#ifdef GCDEBUG
#include <time.h>
#endif

#undef mask

static unsigned long *marks;

#define TOPTR(w)    ((long *) (((long) (w)) & ~MTP_TAGMASK))
#define SETBIT(a,b) *((a)+((b)>>5)) |= 1<<((b)&037)
#define CLRBIT(a,b) *((a)+((b)>>5)) &= ~(1<<((b)&037))
#define TSTBIT(a,b) (*((a)+((b)>>5)) & (1<<((b)&037)))
#define MARK(h)     SETBIT(marks,(h)-wm_heapbase),nmarked++
#define MARKED(h)   TSTBIT(marks,(h)-wm_heapbase)
#define REV(loc)    rev(heap_low,heap_high,(long *)(loc),(long)(loc))

#define FENCEVAL(v) MFENCE_VAL(v)

#define chpt_E(v) env_E(((long) chpt_SPB(v)) & ~1)

/*---------------------------------------------------------------------------------*
 | Whether MTP_CONST is defined or not indicates whether we are using the
 | 88k tagging scheme or not.  If MTP_CONST is defined, then UIAs are
 | represented as offsets instead of real pointers.  The 88k tagging scheme
 | has the advantage in that all pointer objects are more easily represented
 | and in a more uniform manner.
 |
 | In much of the code which follows, there are ifdefs or ifndefs on MTP_CONST.
 | These should be taken to mean that we are using the 88k tagging scheme
 | if MTP_CONST is NOT defined.
 *---------------------------------------------------------------------------------*/

#ifndef MTP_CONST
		/* 88K */
#define ISPTR(h)   (!(((long) (h)) & (MTP_INT | MTP_SYM) & ~MTP_BGND))
#define ISFENCE(v) (((v) & MTP_TAGMASK) == MTP_FENCE)
#define ISUIA(v)   ((v) & ((MTP_DOUBLE & MTP_UIA) & ~MTP_BGND))
#define ISCONST(h) (((long) (h)) & (MTP_INT | MTP_SYM) & ~MTP_BGND)
#define REVBIT     0x00000001
#define BIAS	   (EBIAS/4)

#else  /* MTP_CONST */
#define ISPTR(h)   ((((long) (h)) & MTP_TAGMASK) != MTP_CONST)
#define ISFENCE(v) (((v) & MTP_CONSTMASK) == MTP_FENCE)
#define ISUIA(v)   (((v) & MTP_CONSTMASK) == MTP_UIA)
#define UIAVAL(v)  ((MUIA(v)) >> 2)
#define ISCONST(h) ((((long) (h)) & MTP_TAGMASK) == MTP_INT)
#define REVBIT     0x80000000
#define BIAS       0

#endif /* MTP_CONST */


#ifdef PARAMREVBIT
#define ADDR_MASK	0x7FFFFFFF

#define REVERSEIT(targ, v)	((((targ) | ((v) & MTP_TAGMASK)) & ADDR_MASK) | ReversedHiBit);
#define ISNORMAL(v)		(((v) & ~ADDR_MASK) ^ ReversedHiBit)
#define UNREVERSEIT(v)		(((v) & ADDR_MASK) | AddressHiBit)

#else

#if !defined(AmigaUNIX)
#define LOWMEMORY 1
#endif


#ifdef LOWMEMORY
#define REVERSEIT(targ,v) (((targ) | ((v)&MTP_TAGMASK)) | REVBIT)
#define ISNORMAL(v)	   (!(v & REVBIT))
#define UNREVERSEIT(v)	((v) & ~REVBIT)


#else  /* not-LOWMEMORY */
    /*-------------------------------------------------------*
     | If the heap and the environment are in the high memory,
     | i.e the first bit of an address is always one,
     | Following macros should be used. -- Ilyas 10/25/89
     *-------------------------------------------------------*/
#define REVERSEIT(targ,v) ((targ | (v&MTP_TAGMASK)) & ~REVBIT)
#define ISNORMAL(v)	      (v <= 0)
#define UNREVERSEIT(v)	  (v | REVBIT)

#endif /* LOWMEMORY */
#endif /* PARAMREVBIT */

#define ISNORMALUIA(v)	(!(v & REVBIT))

#define env_E(v)   (* (((long **) (v)) + BIAS))
#define env_CP(v)  (* (((Code **) (v)) + BIAS + 1))
#define val_at(v)  (* (((long *) (v)) + BIAS))

/*---------------------------------------------------------------------------------*
 | Variables:
 |
 |      mrccp           -- pointer to the Most Recently Compacted Choice Point
 |                         This variable will have its value set after phase 1.
 |      heap_low        -- pointer to lowest cell in uncompacted region of
 |                         heap.  This cell will not necessarily be marked.
 |      heap_high       -- pointer to the highest cell in the uncompacted
 |                         region.  We force this cell to be marked so that
 |                         the HB values may always be updated.
 |      oldest_ap       -- this is chpt_SPB(mrccp)
 |      nmarked         -- counter indicating the number of cells marked
 |      e               -- environment pointer for phase 2
 |      b               -- pointer to choice points in phase 2
 |      ap              -- argument pointer in phase 2
 |      apb             -- argument pointer corresponding to b for phase 2
 *---------------------------------------------------------------------------------*/

static long *heap_low;
static long *heap_high;
static long nmarked;

static int gccallcount;		/* number of times gc has been called */
extern int gcbeep;

static	long *	mark_args	PARAMS(( long *, Code * ));
static	void	mark_from	PARAMS(( long * ));
static	void	rev		    PARAMS(( long *, long *, long *, long ));
static	void	rev_update	PARAMS(( long *, long ));
static	void	init_marks	PARAMS(( void ));
static	void	mark		PARAMS(( long ));

#include <stdio.h>

	/*-----------------------------------------------------
	 | gc()
	 *----------------------------------------------------*/

int
gc()
{
    long *mrccp;
    long *e;
    long *b;
    long *ap;
    long *apb;
    long *tr, *tr2;
    long *oldest_ap;
    Code *ra;			/* return address */
    register int i;
    register long *h;
    register unsigned long *mp;
    register unsigned long m;
    int   compactionbit;
#ifdef GCDEBUG
    unsigned long start_tick, finish_tick;
#endif

    /*---------------------------------------------------------------*
     | Force certain external variables to be biased for gc purposes.
     | These will be unbiased on exit from gc.
     *---------------------------------------------------------------*/

    wm_heapbase -= BIAS;
    wm_H -= BIAS;
    wm_E -= BIAS;

    if (gcbeep) {
	    putchar(007);
#ifdef MacOS
	    putchar(007);
#else
	    putchar(0200);
	    putchar(0200);
#endif
	    fflush(stdout);
    }

    gccallcount++;

#ifdef GCDEBUG<commit_msg>demo hardware key check<commit_after>/*==================================================================*
 |			gc.c                 
 |		Copyright (c) 1987-1995 Applied Logic Systems, Inc.
 |
 |			-- garbage compactor for ALS Prolog
 |
 | Author:  <NAME>
 | Creation Date: 5/24/87
 | Revision History:
 | 08/02/88 - kev -- 88k port
 | 04/25/89 - kev -- port back to sun
 | 10/04/90 - kev -- removed machine-specific designations in port to SPARC
 | 05/14/91 - raman -- amiga port (heap/stack high mem)
 | 04/03/93 - raman -- made it generic
 | 10/27/93 - C. Houpt -- Use ASCII Beep (7) for gcbeep on Mac.
 *==================================================================*/
#include "defs.h"
#include "machinst.h"
#include "wintcode.h"
#ifdef FREEZE
#include "freeze.h"
#endif

/*
#define DEBUGFREEZE 1
#define GCDEBUG 1
*/

#ifdef SSI_UNIKEY
#include <ssi_cw32.h>
#endif

#ifdef GCDEBUG
#include <time.h>
#endif

#undef mask

static unsigned long *marks;

#define TOPTR(w)    ((long *) (((long) (w)) & ~MTP_TAGMASK))
#define SETBIT(a,b) *((a)+((b)>>5)) |= 1<<((b)&037)
#define CLRBIT(a,b) *((a)+((b)>>5)) &= ~(1<<((b)&037))
#define TSTBIT(a,b) (*((a)+((b)>>5)) & (1<<((b)&037)))
#define MARK(h)     SETBIT(marks,(h)-wm_heapbase),nmarked++
#define MARKED(h)   TSTBIT(marks,(h)-wm_heapbase)
#define REV(loc)    rev(heap_low,heap_high,(long *)(loc),(long)(loc))

#define FENCEVAL(v) MFENCE_VAL(v)

#define chpt_E(v) env_E(((long) chpt_SPB(v)) & ~1)

/*---------------------------------------------------------------------------------*
 | Whether MTP_CONST is defined or not indicates whether we are using the
 | 88k tagging scheme or not.  If MTP_CONST is defined, then UIAs are
 | represented as offsets instead of real pointers.  The 88k tagging scheme
 | has the advantage in that all pointer objects are more easily represented
 | and in a more uniform manner.
 |
 | In much of the code which follows, there are ifdefs or ifndefs on MTP_CONST.
 | These should be taken to mean that we are using the 88k tagging scheme
 | if MTP_CONST is NOT defined.
 *---------------------------------------------------------------------------------*/

#ifndef MTP_CONST
		/* 88K */
#define ISPTR(h)   (!(((long) (h)) & (MTP_INT | MTP_SYM) & ~MTP_BGND))
#define ISFENCE(v) (((v) & MTP_TAGMASK) == MTP_FENCE)
#define ISUIA(v)   ((v) & ((MTP_DOUBLE & MTP_UIA) & ~MTP_BGND))
#define ISCONST(h) (((long) (h)) & (MTP_INT | MTP_SYM) & ~MTP_BGND)
#define REVBIT     0x00000001
#define BIAS	   (EBIAS/4)

#else  /* MTP_CONST */
#define ISPTR(h)   ((((long) (h)) & MTP_TAGMASK) != MTP_CONST)
#define ISFENCE(v) (((v) & MTP_CONSTMASK) == MTP_FENCE)
#define ISUIA(v)   (((v) & MTP_CONSTMASK) == MTP_UIA)
#define UIAVAL(v)  ((MUIA(v)) >> 2)
#define ISCONST(h) ((((long) (h)) & MTP_TAGMASK) == MTP_INT)
#define REVBIT     0x80000000
#define BIAS       0

#endif /* MTP_CONST */


#ifdef PARAMREVBIT
#define ADDR_MASK	0x7FFFFFFF

#define REVERSEIT(targ, v)	((((targ) | ((v) & MTP_TAGMASK)) & ADDR_MASK) | ReversedHiBit);
#define ISNORMAL(v)		(((v) & ~ADDR_MASK) ^ ReversedHiBit)
#define UNREVERSEIT(v)		(((v) & ADDR_MASK) | AddressHiBit)

#else

#if !defined(AmigaUNIX)
#define LOWMEMORY 1
#endif


#ifdef LOWMEMORY
#define REVERSEIT(targ,v) (((targ) | ((v)&MTP_TAGMASK)) | REVBIT)
#define ISNORMAL(v)	   (!(v & REVBIT))
#define UNREVERSEIT(v)	((v) & ~REVBIT)


#else  /* not-LOWMEMORY */
    /*-------------------------------------------------------*
     | If the heap and the environment are in the high memory,
     | i.e the first bit of an address is always one,
     | Following macros should be used. -- Ilyas 10/25/89
     *-------------------------------------------------------*/
#define REVERSEIT(targ,v) ((targ | (v&MTP_TAGMASK)) & ~REVBIT)
#define ISNORMAL(v)	      (v <= 0)
#define UNREVERSEIT(v)	  (v | REVBIT)

#endif /* LOWMEMORY */
#endif /* PARAMREVBIT */

#define ISNORMALUIA(v)	(!(v & REVBIT))

#define env_E(v)   (* (((long **) (v)) + BIAS))
#define env_CP(v)  (* (((Code **) (v)) + BIAS + 1))
#define val_at(v)  (* (((long *) (v)) + BIAS))

/*---------------------------------------------------------------------------------*
 | Variables:
 |
 |      mrccp           -- pointer to the Most Recently Compacted Choice Point
 |                         This variable will have its value set after phase 1.
 |      heap_low        -- pointer to lowest cell in uncompacted region of
 |                         heap.  This cell will not necessarily be marked.
 |      heap_high       -- pointer to the highest cell in the uncompacted
 |                         region.  We force this cell to be marked so that
 |                         the HB values may always be updated.
 |      oldest_ap       -- this is chpt_SPB(mrccp)
 |      nmarked         -- counter indicating the number of cells marked
 |      e               -- environment pointer for phase 2
 |      b               -- pointer to choice points in phase 2
 |      ap              -- argument pointer in phase 2
 |      apb             -- argument pointer corresponding to b for phase 2
 *---------------------------------------------------------------------------------*/

static long *heap_low;
static long *heap_high;
static long nmarked;

static int gccallcount;		/* number of times gc has been called */
extern int gcbeep;

static	long *	mark_args	PARAMS(( long *, Code * ));
static	void	mark_from	PARAMS(( long * ));
static	void	rev		    PARAMS(( long *, long *, long *, long ));
static	void	rev_update	PARAMS(( long *, long ));
static	void	init_marks	PARAMS(( void ));
static	void	mark		PARAMS(( long ));

#include <stdio.h>

	/*-----------------------------------------------------
	 | gc()
	 *----------------------------------------------------*/

int
gc()
{
    long *mrccp;
    long *e;
    long *b;
    long *ap;
    long *apb;
    long *tr, *tr2;
    long *oldest_ap;
    Code *ra;			/* return address */
    register int i;
    register long *h;
    register unsigned long *mp;
    register unsigned long m;
    int   compactionbit;
#ifdef GCDEBUG
    unsigned long start_tick, finish_tick;
#endif

    /*---------------------------------------------------------------*
     | Force certain external variables to be biased for gc purposes.
     | These will be unbiased on exit from gc.
     *---------------------------------------------------------------*/

#ifdef SSI_UNIKEY
    if (SSI_Check(0)) {
	PI_app_printf(PI_app_printf_error,
"\
Error: UniKey Hardware key required.\n\
This demo version of ALS Prolog requires a UniKey hardware key.\n\
Please check that the hardware key is correctly attached to the parallel port.\n\
Exiting ALS Prolog.\n\
"
	);
	exit(1);
    }
#endif

    wm_heapbase -= BIAS;
    wm_H -= BIAS;
    wm_E -= BIAS;

    if (gcbeep) {
	    putchar(007);
#ifdef MacOS
	    putchar(007);
#else
	    putchar(0200);
	    putchar(0200);
#endif
	    fflush(stdout);
    }

    gccallcount++;

#ifdef GCDEBUG
{'fraction_non_alphanumeric': 0.24707460684206603, 'fraction_numerical': 0.019676529775657748, 'mean_word_length': 2.062072113190324, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4008040', 'n_tokens_mistral': 4723, 'n_tokens_neox': 4373, 'n_words': 1328}