touched up hash macros and functions
This commit is contained in:
124
moo/lib/moo.h
124
moo/lib/moo.h
@ -131,8 +131,13 @@ enum moo_trait_t
|
||||
};
|
||||
typedef enum moo_trait_t moo_trait_t;
|
||||
|
||||
typedef struct moo_obj_t moo_obj_t;
|
||||
typedef struct moo_obj_t* moo_oop_t;
|
||||
|
||||
/* =========================================================================
|
||||
* SPECIALIZED OOP TYPES
|
||||
* ========================================================================= */
|
||||
|
||||
/* moo_oop_t defined in moo-cmn.h
|
||||
* moo_obj_t defined further down */
|
||||
|
||||
/* these are more specialized types for moo_obj_t */
|
||||
typedef struct moo_obj_oop_t moo_obj_oop_t;
|
||||
@ -154,9 +159,9 @@ typedef struct moo_obj_word_t* moo_oop_word_t;
|
||||
#define MOO_OOHW_BITS (MOO_SIZEOF_OOHW_T * 8)
|
||||
|
||||
|
||||
/* ========================================================================= */
|
||||
/* BIGINT TYPES AND MACROS */
|
||||
/* ========================================================================= */
|
||||
/* =========================================================================
|
||||
* BIGINT TYPES AND MACROS
|
||||
* ========================================================================= */
|
||||
#if (MOO_SIZEOF_UINTMAX_T > MOO_SIZEOF_OOW_T)
|
||||
# define MOO_USE_FULL_WORD
|
||||
#endif
|
||||
@ -197,102 +202,9 @@ enum moo_method_type_t
|
||||
};
|
||||
typedef enum moo_method_type_t moo_method_type_t;
|
||||
|
||||
/*
|
||||
* OOP encoding
|
||||
* An object pointer(OOP) is an ordinary pointer value to an object.
|
||||
* but some simple numeric values are also encoded into OOP using a simple
|
||||
* bit-shifting and masking.
|
||||
*
|
||||
* A real OOP is stored without any bit-shifting while a non-pointer value encoded
|
||||
* in an OOP is bit-shifted to the left by 2 and the 2 least-significant bits
|
||||
* are set to 1 or 2.
|
||||
*
|
||||
* This scheme works because the object allocators aligns the object size to
|
||||
* a multiple of sizeof(moo_oop_t). This way, the 2 least-significant bits
|
||||
* of a real OOP are always 0s.
|
||||
*
|
||||
* With 2 bits, i can encode only 3 special types except object pointers.
|
||||
* Since I need more than 3 special types, I extend the tag bits up to 4 bits
|
||||
* to represent a special data type that doesn't require a range as wide
|
||||
* as a small integer. A unicode character, for instance, only requires 21
|
||||
* bits at most. An error doesn't need to be as diverse as a small integer.
|
||||
*/
|
||||
|
||||
#define MOO_OOP_TAG_BITS_LO 2
|
||||
#define MOO_OOP_TAG_BITS_HI 2
|
||||
|
||||
#define MOO_OOP_TAG_SMOOI 1 /* 01 */
|
||||
#define MOO_OOP_TAG_SMPTR 2 /* 10 */
|
||||
#define MOO_OOP_TAG_EXTENDED 3 /* 11 - internal use only */
|
||||
#define MOO_OOP_TAG_CHAR 3 /* 0011 */
|
||||
#define MOO_OOP_TAG_ERROR 7 /* 0111 */
|
||||
#define MOO_OOP_TAG_RESERVED0 11 /* 1011 */
|
||||
#define MOO_OOP_TAG_RESERVED1 15 /* 1111 */
|
||||
|
||||
#define MOO_OOP_GET_TAG_LO(oop) (((moo_oow_t)oop) & MOO_LBMASK(moo_oow_t, MOO_OOP_TAG_BITS_LO))
|
||||
#define MOO_OOP_GET_TAG_LOHI(oop) (((moo_oow_t)oop) & MOO_LBMASK(moo_oow_t, MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_HI))
|
||||
#define MOO_OOP_GET_TAG(oop) (MOO_OOP_GET_TAG_LO(oop) == MOO_OOP_TAG_EXTENDED? MOO_OOP_GET_TAG_LOHI(oop): MOO_OOP_GET_TAG_LO(oop))
|
||||
|
||||
#define MOO_OOP_IS_NUMERIC(oop) (MOO_OOP_GET_TAG_LO(oop) != 0)
|
||||
#define MOO_OOP_IS_POINTER(oop) (MOO_OOP_GET_TAG_LO(oop) == 0)
|
||||
|
||||
#define MOO_OOP_IS_SMOOI(oop) (MOO_OOP_GET_TAG_LO(oop) == MOO_OOP_TAG_SMOOI)
|
||||
#define MOO_OOP_IS_SMPTR(oop) (MOO_OOP_GET_TAG_LO(oop) == MOO_OOP_TAG_SMPTR)
|
||||
|
||||
#define MOO_SMOOI_TO_OOP(num) ((moo_oop_t)((((moo_oow_t)(moo_ooi_t)(num)) << MOO_OOP_TAG_BITS_LO) | MOO_OOP_TAG_SMOOI))
|
||||
#define MOO_OOP_TO_SMOOI(oop) (((moo_ooi_t)oop) >> MOO_OOP_TAG_BITS_LO)
|
||||
/*
|
||||
#define MOO_SMPTR_TO_OOP(ptr) ((moo_oop_t)((((moo_oow_t)(ptr)) << MOO_OOP_TAG_BITS_LO) | MOO_OOP_TAG_SMPTR))
|
||||
#define MOO_OOP_TO_SMPTR(oop) (((moo_ooi_t)oop) >> MOO_OOP_TAG_BITS_LO)
|
||||
*/
|
||||
#define MOO_SMPTR_TO_OOP(ptr) ((moo_oop_t)(((moo_oow_t)ptr) | MOO_OOP_TAG_SMPTR))
|
||||
#define MOO_OOP_TO_SMPTR(oop) ((void*)(((moo_oow_t)oop) & ~MOO_LBMASK(moo_oow_t, MOO_OOP_TAG_BITS_LO)))
|
||||
|
||||
#define MOO_OOP_IS_CHAR(oop) (MOO_OOP_GET_TAG(oop) == MOO_OOP_TAG_CHAR)
|
||||
#define MOO_OOP_IS_ERROR(oop) (MOO_OOP_GET_TAG(oop) == MOO_OOP_TAG_ERROR)
|
||||
|
||||
#define MOO_OOP_TO_CHAR(oop) (((moo_oow_t)oop) >> (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_LO))
|
||||
#define MOO_CHAR_TO_OOP(num) ((moo_oop_t)((((moo_oow_t)(num)) << (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_LO)) | MOO_OOP_TAG_CHAR))
|
||||
#define MOO_OOP_TO_ERROR(oop) (((moo_oow_t)oop) >> (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_LO))
|
||||
#define MOO_ERROR_TO_OOP(num) ((moo_oop_t)((((moo_oow_t)(num)) << (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_LO)) | MOO_OOP_TAG_ERROR))
|
||||
|
||||
/* -------------------------------- */
|
||||
|
||||
/* SMOOI takes up 62 bits on a 64-bit architecture and 30 bits
|
||||
* on a 32-bit architecture. The absolute value takes up 61 bits and 29 bits
|
||||
* respectively for the sign bit. */
|
||||
#define MOO_SMOOI_BITS (MOO_OOI_BITS - MOO_OOP_TAG_BITS_LO)
|
||||
#define MOO_SMOOI_ABS_BITS (MOO_SMOOI_BITS - 1)
|
||||
#define MOO_SMOOI_MAX ((moo_ooi_t)(~((moo_oow_t)0) >> (MOO_OOP_TAG_BITS_LO + 1)))
|
||||
/* Sacrificing 1 bit pattern for a negative SMOOI makes
|
||||
* implementation a lot eaisier in many aspects. */
|
||||
/*#define MOO_SMOOI_MIN (-MOO_SMOOI_MAX - 1)*/
|
||||
#define MOO_SMOOI_MIN (-MOO_SMOOI_MAX)
|
||||
#define MOO_IN_SMOOI_RANGE(ooi) ((ooi) >= MOO_SMOOI_MIN && (ooi) <= MOO_SMOOI_MAX)
|
||||
|
||||
/* SMPTR is a special value which has been devised to encode an address value
|
||||
* whose low MOO_OOP_TAG_BITS_LO bits are 0. its class is SmallPointer. A pointer
|
||||
* returned by most of system functions would be aligned to the pointer size.
|
||||
* you can use the followings macros when converting such a pointer without loss. */
|
||||
#define MOO_IN_SMPTR_RANGE(ptr) ((((moo_oow_t)ptr) & MOO_LBMASK(moo_oow_t, MOO_OOP_TAG_BITS_LO)) == 0)
|
||||
|
||||
#define MOO_CHAR_BITS (MOO_OOI_BITS - MOO_OOP_TAG_BITS_LO - MOO_OOP_TAG_BITS_HI)
|
||||
#define MOO_CHAR_MIN 0
|
||||
#define MOO_CHAR_MAX (~((moo_oow_t)0) >> (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_HI))
|
||||
|
||||
#define MOO_ERROR_BITS (MOO_OOI_BITS - MOO_OOP_TAG_BITS_LO - MOO_OOP_TAG_BITS_HI)
|
||||
#define MOO_ERROR_MIN 0
|
||||
#define MOO_ERROR_MAX (~((moo_oow_t)0) >> (MOO_OOP_TAG_BITS_LO + MOO_OOP_TAG_BITS_HI))
|
||||
|
||||
/* TODO: There are untested code where a small integer is converted to moo_oow_t.
|
||||
* for example, the spec making macro treats the number as moo_oow_t instead of moo_ooi_t.
|
||||
* as of this writing, i skip testing that part with the spec value exceeding MOO_SMOOI_MAX.
|
||||
* later, please verify it works, probably by limiting the value ranges in such macros
|
||||
*/
|
||||
|
||||
/*
|
||||
* Object structure
|
||||
*/
|
||||
/* =========================================================================
|
||||
* OBJECT STRUCTURE
|
||||
* ========================================================================= */
|
||||
enum moo_obj_type_t
|
||||
{
|
||||
MOO_OBJ_TYPE_OOP,
|
||||
@ -382,7 +294,7 @@ typedef enum moo_gcfin_t moo_gcfin_t;
|
||||
#define MOO_OBJ_FLAGS_KERNEL_BITS 2
|
||||
#define MOO_OBJ_FLAGS_PERM_BITS 1
|
||||
#define MOO_OBJ_FLAGS_MOVED_BITS 1
|
||||
#define MOO_OBJ_FLAGS_PROC_BITS 1
|
||||
#define MOO_OBJ_FLAGS_PROC_BITS 1
|
||||
#define MOO_OBJ_FLAGS_RDONLY_BITS 1
|
||||
#define MOO_OBJ_FLAGS_GCFIN_BITS 4
|
||||
#define MOO_OBJ_FLAGS_TRAILER_BITS 1
|
||||
@ -1002,6 +914,11 @@ struct moo_fpdec_t
|
||||
#define MOO_BYTESOF(moo,oop) \
|
||||
(MOO_OOP_IS_NUMERIC(oop)? MOO_SIZEOF(moo_oow_t): MOO_OBJ_BYTESOF(oop))
|
||||
|
||||
|
||||
/* =========================================================================
|
||||
* HEAP
|
||||
* ========================================================================= */
|
||||
|
||||
typedef struct moo_space_t moo_space_t;
|
||||
|
||||
struct moo_space_t
|
||||
@ -1023,9 +940,8 @@ struct moo_heap_t
|
||||
moo_space_t newspace;
|
||||
};
|
||||
|
||||
|
||||
/* =========================================================================
|
||||
* MOO VM LOGGING
|
||||
* VM LOGGING
|
||||
* ========================================================================= */
|
||||
|
||||
enum moo_log_mask_t
|
||||
|
Reference in New Issue
Block a user