//override functions declared in include/cyclone_drivers.h

// require that the index be less than the numelts of x
// using valueof(`n) directly results in a compiler error for some reason...
u8@`H add_for_deptype(u8*{valueof(`n)}@nozeroterm x, int i) 
  @requires(i < numelts(x));

u16@`H read_u16_from_u8(u8*{valueof(`n)}@nozeroterm x, int offset, int bound) 
  @requires((bound == numelts(x)) && (offset < numelts(x)));

u32@`H read_u32_from_u8(u8*{valueof(`n)}@nozeroterm x, int offset, int bound) 
  @requires((bound == numelts(x)) && (offset < numelts(x)));

void write_u16_to_u8(u8*{valueof(`n)}@nozeroterm ioaddr, int offset, int bound, u16 val16) 
  @requires((bound == numelts(ioaddr)) && (offset < numelts(ioaddr)));

void*`H cycescape_u8_to_void(u8*{valueof(`n)}@nozeroterm a);

extern int cyc_runtime_register_thread(struct _tls_slot<`r> *`r slot) __attribute__((no_throw));
extern int cyc_runtime_unregister_thread() __attribute__((no_throw));

extern int cyc_runtime_register_thread_dbg(struct _tls_slot<`r> *`r slot, const char *fname) __attribute__((no_throw));
extern int cyc_runtime_unregister_thread_dbg(const char *fname) __attribute__((no_throw));

struct _tls_slot<`r::R> {
  unsigned int pid;
  unsigned int usage_count;
  tls_record_t *`r record;
};


static inline `a* malloc_init(sizeof_t<`a> size);

static inline `a*`U umalloc_init(sizeof_t<`a> size);
static inline `a*`RC rcmalloc_init(sizeof_t<`a> size);
static inline `a*`r rmalloc_init(region_t<`r> reg, sizeof_t<`a> size);


char *@nozeroterm cycescape_coerce_charstar_noz(char *{valueof(`n)} @nozeroterm a);


static inline `a CYC_DO_ARITHMETIC(`a p, u32 size, u32 num);
static inline `a CYC_DO_CAST(`b::TB p, sizeof_t<`a::B> size );

static inline `a *safe_arith(`a::A *{valueof(`n)} base, sizeof_t<`a> eltsize, unsigned int off) 
  @requires((off < numelts(base)));

static inline u8 _THIN_CYC_UTIL_IN8(u8 *{valueof(`n)} @nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));
static inline u16 _THIN_CYC_UTIL_IN16(u8 *{valueof(`n)} @nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));
static inline u32 _THIN_CYC_UTIL_IN32(u8 *{valueof(`n)} @nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));


static inline void _THIN_CYC_UTIL_OUT8(u8 val8, u8 *{valueof(`n)}@nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));
static inline void _THIN_CYC_UTIL_OUT16(u16 val16, u8 *{valueof(`n)}@nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));
static inline void _THIN_CYC_UTIL_OUT32(u32 val32, u8 *{valueof(`n)}@nozeroterm ioaddr, int reg)
  @requires((numelts(ioaddr) > reg));


