/*
**
** The C code is generated by ATS/Anairiats
** The compilation time is: 2013-9-3:  2h:41m
**
*/

/* include some .h files */
#ifndef _ATS_HEADER_NONE
#include "ats_config.h"
#include "ats_basics.h"
#include "ats_types.h"
#include "ats_exception.h"
#include "ats_memory.h"
#endif /* _ATS_HEADER_NONE */

/* include some .cats files */
#ifndef _ATS_PRELUDE_NONE
#include "prelude/CATS/basics.cats"
#include "prelude/CATS/bool.cats"
#include "prelude/CATS/char.cats"
#include "prelude/CATS/byte.cats"
#include "prelude/CATS/float.cats"
#include "prelude/CATS/integer.cats"
#include "prelude/CATS/integer_ptr.cats"
#include "prelude/CATS/integer_fixed.cats"
#include "prelude/CATS/sizetype.cats"
#include "prelude/CATS/pointer.cats"
#include "prelude/CATS/reference.cats"
#include "prelude/CATS/string.cats"
#include "prelude/CATS/lazy.cats"
#include "prelude/CATS/lazy_vt.cats"
#include "prelude/CATS/printf.cats"
#include "prelude/CATS/list.cats"
#include "prelude/CATS/option.cats"
#include "prelude/CATS/array.cats"
#include "prelude/CATS/matrix.cats"
#endif /* _ATS_PRELUDE_NONE */
/* prologues from statically loaded files */

/* external codes at top */
#line 51 "gcats1_misc.dats"


static
int the_stack_direction = 0 ;
//
// dir=1/-1 : upward/downward
//
static
int gc_stack_dir_get_inner (int *some_ptr) {
  int some_int ;
  if (&some_int > some_ptr) return 1 ; else return -1 ;
  return 0 ; /* deadcode */
}

static
int gc_stack_dir_get_outer () {
//
static volatile void* get_inner = &gc_stack_dir_get_inner ;
//
  int some_int ;
  if (!the_stack_direction)
    the_stack_direction = ((int(*)(int*))get_inner)(&some_int) ;
/*
  fprintf (stderr, "the_stack_direction = %i\n", the_stack_direction) ;
*/
  return the_stack_direction ;
}

ats_int_type
gc_stack_dir_get () { return gc_stack_dir_get_outer () ; }

/* ****** ****** */

#ifdef _ATS_MULTITHREAD
static __thread
ats_ptr_type the_stack_beg = (ats_ptr_type)0 ;
#else /* single thread */
static
ats_ptr_type the_stack_beg = (ats_ptr_type)0 ;
#endif

ats_void_type
gc_stack_beg_set (ats_int_type dir) {
  long int pagesize, pagemask ; uintptr_t beg ;

  if (the_stack_beg) return ; // already set

  // pagesize must be a power of 2
  pagesize = sysconf(_SC_PAGESIZE) ; // system configuration
/*
  fprintf(stderr, "gc_stack_beg_set: dir = %i\n", dir) ;
  fprintf(stderr, "gc_stack_beg_set: pagesize = %li\n", pagesize) ;
  fprintf(stderr, "gc_stack_beg_set: &pagesize = %p\n", &pagesize) ;
*/
  pagemask = ~(pagesize - 1) ; // 1...10...0

  if (dir > 0) {
    beg = (uintptr_t)(&pagesize) ;
    beg &= pagemask ;
  } else {
    beg = (uintptr_t)(&pagesize) + pagesize ;
    beg &= pagemask ;
    beg -= sizeof(freeitmlst) ;
  }

  the_stack_beg = (ats_ptr_type)beg ;

  return ;
}

ats_ptr_type
gc_stack_beg_get (
  // there is no argument for this function
) {
  if (!the_stack_beg) {
    fprintf (stderr, "GC Fatal Error: [gc_stack_beg_get]") ;
    fprintf (stderr, ": [the_stack_beg] is not yet set.\n") ;
    exit (1) ;
  }
  return the_stack_beg ;
} // end of [gc_stack_beg_get]


/* type definitions */
/* external typedefs */
/* external dynamic constructor declarations */
/* external dynamic constant declarations */
/* external dynamic terminating constant declarations */
#ifdef _ATS_PROOFCHECK
/* empty */
#endif /* _ATS_PROOFCHECK */

/* assuming abstract types */
/* sum constructor declarations */
/* exn constructor declarations */
/* global dynamic (non-functional) constant declarations */
/* internal function declarations */
/* partial value template declarations */
/* static temporary variable declarations */
/* external value variable declarations */

/* function implementations */

/* static load function */

extern ats_void_type ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_2esats__staload (void) ;

ats_void_type
ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__staload () {
static int ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__staload_flag = 0 ;
if (ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__staload_flag) return ;
ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__staload_flag = 1 ;

ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_2esats__staload () ;

return ;
} /* staload function */

/* dynamic load function */

// dynload flag declaration
extern ats_int_type ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__dynload_flag ;

ats_void_type
ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__dynload () {
ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__dynload_flag = 1 ;
ATS_2d0_2e2_2e10_2ccomp_2runtime_2GCATS1_2gcats1_misc_2edats__staload () ;

#ifdef _ATS_PROOFCHECK
#endif /* _ATS_PROOFCHECK */

/* marking static variables for GC */

/* marking external values for GC */

/* code for dynamic loading */
return ;
} /* end of [dynload function] */

/* external codes at mid */
/* external codes at bot */

/* ****** ****** */

/* end of [gcats1_misc_dats.c] */
