factor/native/memory.h

153 lines
2.8 KiB
C
Raw Normal View History

2005-05-10 22:30:58 -04:00
/* macros for reading/writing memory, useful when working around
C's type system */
INLINE CELL get(CELL where)
{
return *((CELL*)where);
}
INLINE void put(CELL where, CELL what)
{
*((CELL*)where) = what;
}
INLINE u16 cget(CELL where)
{
return *((u16*)where);
}
INLINE void cput(CELL where, u16 what)
{
*((u16*)where) = what;
}
INLINE BYTE bget(CELL where)
{
return *((BYTE*)where);
}
INLINE void bput(CELL where, BYTE what)
{
*((BYTE*)where) = what;
}
2005-05-13 00:09:49 -04:00
INLINE CELL align8(CELL a)
2005-05-10 22:30:58 -04:00
{
2005-05-13 00:09:49 -04:00
return ((a & 7) == 0) ? a : ((a + 8) & ~7);
2005-05-10 22:30:58 -04:00
}
2005-05-13 00:09:49 -04:00
#define TAG_MASK 7
#define TAG_BITS 3
#define TAG(cell) ((CELL)(cell) & TAG_MASK)
#define RETAG(cell,tag) ((CELL)(cell) | (tag))
#define UNTAG(cell) ((CELL)(cell) & ~TAG_MASK)
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
/*** Tags ***/
#define FIXNUM_TYPE 0
#define BIGNUM_TYPE 1
#define CONS_TYPE 2
#define OBJECT_TYPE 3
#define RATIO_TYPE 4
#define FLOAT_TYPE 5
#define COMPLEX_TYPE 6
#define HEADER_TYPE 7 /* anything less than this is a tag */
#define GC_COLLECTED 7 /* See gc.c */
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
/*** Header types ***/
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
/* Canonical T object */
#define T_TYPE 7
CELL T;
2004-07-16 02:26:21 -04:00
2005-05-13 00:09:49 -04:00
#define ARRAY_TYPE 8
2004-08-29 03:20:19 -04:00
2005-05-13 00:09:49 -04:00
/* Canonical F object */
#define F_TYPE 9
#define F RETAG(0,OBJECT_TYPE)
2004-07-16 02:26:21 -04:00
2005-05-13 00:09:49 -04:00
#define HASHTABLE_TYPE 10
#define VECTOR_TYPE 11
#define STRING_TYPE 12
#define SBUF_TYPE 13
#define DLL_TYPE 15
#define WRAPPER_TYPE 14
2005-05-13 00:09:49 -04:00
#define ALIEN_TYPE 16
#define WORD_TYPE 17
#define TUPLE_TYPE 18
#define BYTE_ARRAY_TYPE 19
#define DISPLACED_ALIEN_TYPE 20
2004-07-16 02:26:21 -04:00
2005-05-13 00:09:49 -04:00
#define TYPE_COUNT 21
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
INLINE bool headerp(CELL cell)
2004-07-16 02:26:21 -04:00
{
2005-05-13 00:09:49 -04:00
return (cell != F
&& TAG(cell) == OBJECT_TYPE
&& cell < RETAG(TYPE_COUNT << TAG_BITS,OBJECT_TYPE));
2004-07-16 02:26:21 -04:00
}
2005-05-13 00:09:49 -04:00
INLINE CELL tag_header(CELL cell)
2004-08-16 20:42:30 -04:00
{
2005-05-13 00:09:49 -04:00
return RETAG(cell << TAG_BITS,OBJECT_TYPE);
2004-08-16 20:42:30 -04:00
}
2005-05-13 00:09:49 -04:00
INLINE CELL untag_header(CELL cell)
2004-07-16 02:26:21 -04:00
{
2005-05-13 00:09:49 -04:00
return cell >> TAG_BITS;
2004-07-16 02:26:21 -04:00
}
2005-05-13 00:09:49 -04:00
INLINE CELL tag_object(void* cell)
2004-07-16 02:26:21 -04:00
{
2005-05-13 00:09:49 -04:00
return RETAG(cell,OBJECT_TYPE);
2004-07-16 02:26:21 -04:00
}
2005-05-13 00:09:49 -04:00
INLINE CELL object_type(CELL tagged)
2004-07-16 02:26:21 -04:00
{
2005-05-13 00:09:49 -04:00
if(tagged == F)
return F_TYPE;
else
return untag_header(get(UNTAG(tagged)));
2004-07-16 02:26:21 -04:00
}
2005-05-13 00:09:49 -04:00
INLINE void type_check(CELL type, CELL tagged)
2004-07-16 02:26:21 -04:00
{
2005-05-13 00:09:49 -04:00
if(type < HEADER_TYPE)
{
if(TAG(tagged) == type)
return;
}
else if(TAG(tagged) == OBJECT_TYPE
&& object_type(tagged) == type)
{
return;
}
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
type_error(type,tagged);
}
2005-05-10 22:30:58 -04:00
2005-05-13 00:09:49 -04:00
INLINE CELL type_of(CELL tagged)
2005-05-12 03:52:56 -04:00
{
2005-05-13 00:09:49 -04:00
CELL tag = TAG(tagged);
if(tag == OBJECT_TYPE)
return object_type(tagged);
else
return tag;
}
2005-05-13 00:09:49 -04:00
CELL untagged_object_size(CELL pointer);
CELL object_size(CELL pointer);
void primitive_room(void);
void primitive_type(void);
void primitive_slot(void);
void primitive_set_slot(void);
void primitive_integer_slot(void);
void primitive_set_integer_slot(void);
2004-09-18 22:29:29 -04:00
void primitive_address(void);
2005-02-18 20:37:01 -05:00
void primitive_size(void);
void primitive_begin_scan(void);
void primitive_next_object(void);
void primitive_end_scan(void);
2005-05-13 00:09:49 -04:00
/* set up guard pages to check for under/overflow.
size must be a multiple of the page size */
void* alloc_guarded(CELL size);