From 1d106f43fbc6d459712d8a02400690e980bb70e0 Mon Sep 17 00:00:00 2001 From: Vincent Geddes Date: Sun, 17 Aug 2008 17:43:44 +0200 Subject: [PATCH] Cleaned up bitfield manipulation code in st-method.h and st-object.h --- Makefile-panda.am | 1 + src/st-array.h | 33 +++++++++++++------ src/st-behavior.c | 1 + src/st-memory.c | 12 ++++--- src/st-method.h | 92 +++++++++++++++++++++++------------------------------ src/st-object.c | 1 + src/st-object.h | 85 +++++++++++++++++++------------------------------ src/st-primitives.c | 1 + src/st-types.h | 4 +-- 9 files changed, 108 insertions(+), 122 deletions(-) diff --git a/Makefile-panda.am b/Makefile-panda.am index 9c6ccf6..3cc96b8 100644 --- a/Makefile-panda.am +++ b/Makefile-panda.am @@ -46,6 +46,7 @@ libpanda_la_SOURCES = \ src/st-memory.c \ src/st-system.h \ src/st-system.c \ + src/st-handle.h \ src/st-identity-hashtable.h \ src/st-identity-hashtable.c \ src/st-heap.h \ diff --git a/src/st-array.h b/src/st-array.h index b76bf5c..4fb31ee 100644 --- a/src/st-array.h +++ b/src/st-array.h @@ -28,10 +28,17 @@ #include #include -#define ST_ARRAY(oop) ((struct st_array *) ST_POINTER (oop)) -#define ST_FLOAT_ARRAY(oop) ((struct st_float_array *) ST_POINTER (oop)) -#define ST_WORD_ARRAY(oop) ((struct st_word_array *) ST_POINTER (oop)) -#define ST_BYTE_ARRAY(oop) ((struct st_byte_array *) ST_POINTER (oop)) +#define ST_ARRAYED_OBJECT(oop) ((struct st_arrayed_object *) ST_POINTER (oop)) +#define ST_ARRAY(oop) ((struct st_array *) ST_POINTER (oop)) +#define ST_FLOAT_ARRAY(oop) ((struct st_float_array *) ST_POINTER (oop)) +#define ST_WORD_ARRAY(oop) ((struct st_word_array *) ST_POINTER (oop)) +#define ST_BYTE_ARRAY(oop) ((struct st_byte_array *) ST_POINTER (oop)) + +struct st_arrayed_object +{ + struct st_header header; + st_oop size; +}; struct st_array { @@ -61,13 +68,19 @@ struct st_byte_array st_uchar bytes[]; }; -bool st_byte_array_equal (st_oop object, st_oop other); -st_uint st_byte_array_hash (st_oop object); - -st_oop st_array_allocate (st_oop class, st_uint size); +bool st_byte_array_equal (st_oop object, st_oop other); +st_uint st_byte_array_hash (st_oop object); +st_oop st_array_allocate (st_oop class, st_uint size); st_oop st_float_array_allocate (st_oop class, int size); -st_oop st_word_array_allocate (st_oop class, int size); -st_oop st_byte_array_allocate (st_oop class, int size); +st_oop st_word_array_allocate (st_oop class, int size); +st_oop st_byte_array_allocate (st_oop class, int size); + +static inline st_oop +st_arrayed_object_size (st_oop object) +{ + return ST_ARRAYED_OBJECT (object)->size; +} + static inline st_oop * st_array_elements (st_oop object) diff --git a/src/st-behavior.c b/src/st-behavior.c index ba2753e..82a8bfa 100644 --- a/src/st-behavior.c +++ b/src/st-behavior.c @@ -30,6 +30,7 @@ #include "st-object.h" #include "st-array.h" #include "st-array.h" +#include "st-handle.h" st_list * diff --git a/src/st-memory.c b/src/st-memory.c index 80ec93f..4d576bc 100644 --- a/src/st-memory.c +++ b/src/st-memory.c @@ -35,6 +35,7 @@ #include "st-association.h" #include "st-array.h" #include "st-system.h" +#include "st-handle.h" #include #include @@ -664,11 +665,12 @@ garbage_collect (void) st_cpu_clear_caches (); memory->counter = 0; - st_log ("gc", "collected: %uK\n" - "heapSize: %uK\n" - "marking time: %.6fs\n" - "compaction time: %.6fs\n" - "remapping time: %.6fs\n", + st_log ("gc", "\n" + "collected: %uK\n" + "heapSize: %uK\n" + "marking time: %.6fs\n" + "compaction time: %.6fs\n" + "remapping time: %.6fs\n", memory->bytes_collected / 1024, (memory->bytes_collected + memory->bytes_allocated) / 1024, times[0], times[1], times[2]); diff --git a/src/st-method.h b/src/st-method.h index ec6d423..e9d5d36 100644 --- a/src/st-method.h +++ b/src/st-method.h @@ -117,125 +117,113 @@ typedef enum * 2: 6 */ -#define SET_BITFIELD(bits, field, value) \ - (((bits) & ~st_##field##_mask_aligned) | ((value & st_##field##_mask) << st_##field##_shift)) +#define _ST_METHOD_SET_BITFIELD(bitfield, field, value) \ + (((bitfield) & ~(_ST_METHOD_##field##_MASK << _ST_METHOD_##field##_SHIFT)) \ + | (((value) & _ST_METHOD_##field##_MASK) << _ST_METHOD_##field##_SHIFT)) -#define GET_BITFIELD(bits, field) \ - (((bits) >> st_##field##_shift) & st_##field##_mask) +#define _ST_METHOD_GET_BITFIELD(bitfield, field) \ + (((bitfield) >> _ST_METHOD_##field##_SHIFT) & _ST_METHOD_##field##_MASK) enum { - st_flag_bits = 3, - st_arg_bits = 5, - st_temp_bits = 6, - st_method_unused_bits = 7, - st_large_bits = 1, - st_instvar_bits = 16, - st_literal_bits = 4, - st_primitive_bits = 8, + _ST_METHOD_FLAG_BITS = 3, + _ST_METHOD_ARG_BITS = 5, + _ST_METHOD_TEMP_BITS = 6, + _ST_METHOD_UNUSED_BITS = 7, + _ST_METHOD_LARGE_BITS = 1, + _ST_METHOD_INSTVAR_BITS = 16, + _ST_METHOD_LITERAL_BITS = 4, + _ST_METHOD_PRIMITIVE_BITS = 8, - st_primitive_shift = ST_TAG_SIZE, - st_instvar_shift = ST_TAG_SIZE, - st_literal_shift = ST_TAG_SIZE, - st_large_shift = st_primitive_bits + st_primitive_shift, - st_method_unused_shift = st_large_bits + st_large_shift, - st_temp_shift = st_method_unused_bits + st_method_unused_shift, - st_arg_shift = st_temp_bits + st_temp_shift, - st_flag_shift = st_arg_bits + st_arg_shift, + _ST_METHOD_PRIMITIVE_SHIFT = ST_TAG_SIZE, + _ST_METHOD_INSTVAR_SHIFT = ST_TAG_SIZE, + _ST_METHOD_LITERAL_SHIFT = ST_TAG_SIZE, + _ST_METHOD_LARGE_SHIFT = _ST_METHOD_PRIMITIVE_BITS + _ST_METHOD_PRIMITIVE_SHIFT, + _ST_METHOD_UNUSED_SHIFT = _ST_METHOD_LARGE_BITS + _ST_METHOD_LARGE_SHIFT, + _ST_METHOD_TEMP_SHIFT = _ST_METHOD_UNUSED_BITS + _ST_METHOD_UNUSED_SHIFT, + _ST_METHOD_ARG_SHIFT = _ST_METHOD_TEMP_BITS + _ST_METHOD_TEMP_SHIFT, + _ST_METHOD_FLAG_SHIFT = _ST_METHOD_ARG_BITS + _ST_METHOD_ARG_SHIFT, - st_primitive_mask = ST_NTH_MASK (st_primitive_bits), - st_primitive_mask_aligned = st_primitive_mask << st_primitive_shift, - - st_large_mask = ST_NTH_MASK (st_large_bits), - st_large_mask_aligned = st_large_mask << st_large_shift, - - st_instvar_mask = ST_NTH_MASK (st_instvar_bits), - st_instvar_mask_aligned = st_instvar_mask << st_instvar_shift, - - st_literal_mask = ST_NTH_MASK (st_literal_bits), - st_literal_mask_aligned = st_literal_mask << st_literal_shift, - - st_temp_mask = ST_NTH_MASK (st_temp_bits), - st_temp_mask_aligned = st_temp_mask << st_temp_shift, - - st_arg_mask = ST_NTH_MASK (st_arg_bits), - st_arg_mask_aligned = st_arg_mask << st_arg_shift, - - st_flag_mask = ST_NTH_MASK (st_flag_bits), - st_flag_mask_aligned = st_flag_mask << st_flag_shift, + _ST_METHOD_PRIMITIVE_MASK = ST_NTH_MASK (_ST_METHOD_PRIMITIVE_BITS), + _ST_METHOD_LARGE_MASK = ST_NTH_MASK (_ST_METHOD_LARGE_BITS), + _ST_METHOD_INSTVAR_MASK = ST_NTH_MASK (_ST_METHOD_INSTVAR_BITS), + _ST_METHOD_LITERAL_MASK = ST_NTH_MASK (_ST_METHOD_LITERAL_BITS), + _ST_METHOD_TEMP_MASK = ST_NTH_MASK (_ST_METHOD_TEMP_BITS), + _ST_METHOD_ARG_MASK = ST_NTH_MASK (_ST_METHOD_ARG_BITS), + _ST_METHOD_FLAG_MASK = ST_NTH_MASK (_ST_METHOD_FLAG_BITS), }; static inline int st_method_get_temp_count (st_oop method) { - return GET_BITFIELD (ST_METHOD_HEADER (method), temp); + return _ST_METHOD_GET_BITFIELD (ST_METHOD_HEADER (method), TEMP); } static inline int st_method_get_arg_count (st_oop method) { - return GET_BITFIELD (ST_METHOD_HEADER (method), arg); + return _ST_METHOD_GET_BITFIELD (ST_METHOD_HEADER (method), ARG); } static inline int st_method_get_large_context (st_oop method) { - return GET_BITFIELD (ST_METHOD_HEADER (method), large); + return _ST_METHOD_GET_BITFIELD (ST_METHOD_HEADER (method), LARGE); } static inline int st_method_get_primitive_index (st_oop method) { - return GET_BITFIELD (ST_METHOD_HEADER (method), primitive); + return _ST_METHOD_GET_BITFIELD (ST_METHOD_HEADER (method), PRIMITIVE); } static inline st_method_flags st_method_get_flags (st_oop method) { - return GET_BITFIELD (ST_METHOD_HEADER (method), flag); + return _ST_METHOD_GET_BITFIELD (ST_METHOD_HEADER (method), FLAG); } static inline void st_method_set_flags (st_oop method, st_method_flags flags) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), flag, flags); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), FLAG, flags); } static inline void st_method_set_arg_count (st_oop method, int count) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), arg, count); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), ARG, count); } static inline void st_method_set_temp_count (st_oop method, int count) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), temp, count); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), TEMP, count); } static inline void st_method_set_large_context (st_oop method, bool is_large) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), large, is_large); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), LARGE, is_large); } static inline void st_method_set_primitive_index (st_oop method, int index) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), primitive, index); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), PRIMITIVE, index); } static inline void st_method_set_instvar_index (st_oop method, int index) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), instvar, index); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), INSTVAR, index); } static inline void st_method_set_literal_type (st_oop method, st_method_literal_type literal_type) { - ST_METHOD_HEADER (method) = SET_BITFIELD (ST_METHOD_HEADER (method), literal, literal_type); + ST_METHOD_HEADER (method) = _ST_METHOD_SET_BITFIELD (ST_METHOD_HEADER (method), LITERAL, literal_type); } diff --git a/src/st-object.c b/src/st-object.c index 7bbb87a..d5ba5fd 100644 --- a/src/st-object.c +++ b/src/st-object.c @@ -33,6 +33,7 @@ #include "st-symbol.h" #include "st-object.h" #include "st-character.h" +#include "st-handle.h" #include "st-unicode.h" void diff --git a/src/st-object.h b/src/st-object.h index e6ffe7e..043fa5c 100644 --- a/src/st-object.h +++ b/src/st-object.h @@ -31,8 +31,6 @@ #include #define ST_HEADER(oop) ((struct st_header *) ST_POINTER (oop)) -#define ST_ARRAYED_OBJECT(oop) ((struct st_arrayed_object *) ST_POINTER (oop)) -#define ST_HANDLE(oop) ((struct st_handle *) ST_POINTER (oop)) /* Every heap-allocated object starts with this header word */ /* format of mark oop @@ -51,45 +49,32 @@ struct st_header st_oop fields[]; }; -struct st_arrayed_object -{ - struct st_header header; - st_oop size; -}; +#define _ST_OBJECT_SET_BITFIELD(bitfield, field, value) \ + (((bitfield) & ~(_ST_OBJECT_##field##_MASK << _ST_OBJECT_##field##_SHIFT)) \ + | (((value) & _ST_OBJECT_##field##_MASK) << _ST_OBJECT_##field##_SHIFT)) -struct st_handle -{ - struct st_header header; - - uintptr_t value; -}; +#define _ST_OBJECT_GET_BITFIELD(bitfield, field) \ + (((bitfield) >> _ST_OBJECT_##field##_SHIFT) & _ST_OBJECT_##field##_MASK) enum { - st_unused_bits = 15, - st_hash_bits = 1, - st_object_large_bits = 1, - st_size_bits = 8, - st_format_bits = 6, - st_tag_bits = 2, - - st_tag_shift = 0, - st_format_shift = st_tag_bits + st_tag_shift, - st_size_shift = st_format_bits + st_format_shift, - st_object_large_shift = st_size_bits + st_size_shift, - st_hash_shift = st_object_large_bits + st_object_large_shift, - st_unused_shift = st_hash_bits + st_hash_shift, - - st_format_mask = ST_NTH_MASK (st_format_bits), - st_format_mask_in_place = st_format_mask << st_format_shift, - st_size_mask = ST_NTH_MASK (st_size_bits), - st_size_mask_in_place = st_size_mask << st_size_shift, - st_object_large_mask = ST_NTH_MASK (st_object_large_bits), - st_object_large_mask_in_place = st_object_large_mask << st_object_large_shift, - st_hash_mask = ST_NTH_MASK (st_hash_bits), - st_hash_mask_in_place = st_hash_mask << st_hash_shift, - st_unused_mask = ST_NTH_MASK (st_unused_bits), - st_unused_mask_in_place = st_unused_mask << st_unused_shift, + _ST_OBJECT_UNUSED_BITS = 15, + _ST_OBJECT_HASH_BITS = 1, + _ST_OBJECT_LARGE_BITS = 1, + _ST_OBJECT_SIZE_BITS = 8, + _ST_OBJECT_FORMAT_BITS = 6, + + _ST_OBJECT_FORMAT_SHIFT = ST_TAG_SIZE, + _ST_OBJECT_SIZE_SHIFT = _ST_OBJECT_FORMAT_BITS + _ST_OBJECT_FORMAT_SHIFT, + _ST_OBJECT_LARGE_SHIFT = _ST_OBJECT_SIZE_BITS + _ST_OBJECT_SIZE_SHIFT, + _ST_OBJECT_HASH_SHIFT = _ST_OBJECT_LARGE_BITS + _ST_OBJECT_LARGE_SHIFT, + _ST_OBJECT_UNUSED_SHIFT = _ST_OBJECT_HASH_BITS + _ST_OBJECT_HASH_SHIFT, + + _ST_OBJECT_FORMAT_MASK = ST_NTH_MASK (_ST_OBJECT_FORMAT_BITS), + _ST_OBJECT_SIZE_MASK = ST_NTH_MASK (_ST_OBJECT_SIZE_BITS), + _ST_OBJECT_LARGE_MASK = ST_NTH_MASK (_ST_OBJECT_LARGE_BITS), + _ST_OBJECT_HASH_MASK = ST_NTH_MASK (_ST_OBJECT_HASH_BITS), + _ST_OBJECT_UNUSED_MASK = ST_NTH_MASK (_ST_OBJECT_UNUSED_BITS), }; /* Make sure to update all cased code in VM when adding a new format */ @@ -110,8 +95,8 @@ typedef enum st_format } st_format; -bool st_object_equal (st_oop object, st_oop other); -st_uint st_object_hash (st_oop object); +bool st_object_equal (st_oop object, st_oop other); +st_uint st_object_hash (st_oop object); #define ST_OBJECT_MARK(oop) (ST_HEADER (oop)->mark) #define ST_OBJECT_CLASS(oop) (ST_HEADER (oop)->class) @@ -125,60 +110,54 @@ st_oop st_handle_allocate (st_oop class); static inline void st_object_set_format (st_oop object, st_format format) { - ST_OBJECT_MARK (object) = (ST_OBJECT_MARK (object) & ~st_format_mask_in_place) | (format << st_format_shift); + ST_OBJECT_MARK (object) = _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object), FORMAT, format); } static inline st_format st_object_format (st_oop object) { - return (ST_HEADER (object)->mark >> st_format_shift) & st_format_mask; + return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object), FORMAT); } static inline void st_object_set_large_context (st_oop object, bool is_large) { - ST_OBJECT_MARK (object) = (ST_OBJECT_MARK (object) & ~st_object_large_mask_in_place) | (is_large << st_object_large_shift); + ST_OBJECT_MARK (object) = _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object), LARGE, is_large); } static inline bool st_object_large_context (st_oop object) { - return (ST_OBJECT_MARK (object) >> st_object_large_shift) & st_object_large_mask; + return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object), LARGE); } static inline void st_object_set_hashed (st_oop object, bool hashed) { - ST_OBJECT_MARK (object) = (ST_OBJECT_MARK (object) & ~st_hash_mask_in_place) | (hashed << st_hash_shift); + ST_OBJECT_MARK (object) = _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object), HASH, hashed); } static inline bool st_object_is_hashed (st_oop object) { - return (ST_OBJECT_MARK (object) >> st_hash_shift) & st_hash_mask; + return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object), HASH); } static inline st_uint st_object_instance_size (st_oop object) { - return (ST_OBJECT_MARK (object) >> st_size_shift) & st_size_mask; + return _ST_OBJECT_GET_BITFIELD (ST_OBJECT_MARK (object), SIZE); } static inline st_uint st_object_set_instance_size (st_oop object, st_uint size) { - ST_OBJECT_MARK (object) = (ST_OBJECT_MARK (object) & ~st_size_mask_in_place) | (size << st_size_shift); + ST_OBJECT_MARK (object) = _ST_OBJECT_SET_BITFIELD (ST_OBJECT_MARK (object), SIZE, size); } void st_object_initialize_header (st_oop object, st_oop class); -static inline st_oop -st_arrayed_object_size (st_oop object) -{ - return ST_ARRAYED_OBJECT (object)->size; -} - static inline int st_object_tag (st_oop object) { diff --git a/src/st-primitives.c b/src/st-primitives.c index 78721a0..76db303 100644 --- a/src/st-primitives.c +++ b/src/st-primitives.c @@ -37,6 +37,7 @@ #include "st-dictionary.h" #include "st-unicode.h" #include "st-compiler.h" +#include "st-handle.h" #include #include diff --git a/src/st-types.h b/src/st-types.h index 0a5762b..dfd19b9 100644 --- a/src/st-types.h +++ b/src/st-types.h @@ -30,8 +30,6 @@ #include #include -#define ST_TAG_SIZE 2 - /* Check host data model. We only support LP64 at the moment. */ #if (SIZEOF_VOID_P == 4 && SIZEOF_INT == 4) @@ -58,6 +56,8 @@ enum { ST_MARK_TAG, }; +#define ST_TAG_SIZE 2 + /* basic oop pointer: * integral type wide enough to hold a C pointer. * Can either point to a heap object or contain a smi or Character immediate. -- 2.11.4.GIT