Simplify `mrb_value` structure for `MRB_WORD_BOXING`.

parent 73e289c5
...@@ -47,13 +47,13 @@ enum mrb_special_consts { ...@@ -47,13 +47,13 @@ enum mrb_special_consts {
#define BOXWORD_IMMEDIATE_MASK 0x07 #define BOXWORD_IMMEDIATE_MASK 0x07
#define BOXWORD_SHIFT_VALUE(o,n,t) \ #define BOXWORD_SHIFT_VALUE(o,n,t) \
(t)(((long)(o).w) >> BOXWORD_##n##_SHIFT) (t)(((long)(o)) >> BOXWORD_##n##_SHIFT)
#define BOXWORD_SET_SHIFT_VALUE(o,n,v) \ #define BOXWORD_SET_SHIFT_VALUE(o,n,v) \
((o).w = (((unsigned long)(v)) << BOXWORD_##n##_SHIFT) | BOXWORD_##n##_FLAG) ((o) = (((unsigned long)(v)) << BOXWORD_##n##_SHIFT) | BOXWORD_##n##_FLAG)
#define BOXWORD_SHIFT_VALUE_P(o,n) \ #define BOXWORD_SHIFT_VALUE_P(o,n) \
(((o).w & BOXWORD_##n##_MASK) == BOXWORD_##n##_FLAG) (((o) & BOXWORD_##n##_MASK) == BOXWORD_##n##_FLAG)
#define BOXWORD_OBJ_TYPE_P(o,n) \ #define BOXWORD_OBJ_TYPE_P(o,n) \
(!mrb_immediate_p(o) && (o).value.bp->tt == MRB_TT_##n) (!mrb_immediate_p(o) && mrb_val_union(o).bp->tt == MRB_TT_##n)
/* /*
* mrb_value representation: * mrb_value representation:
...@@ -67,56 +67,63 @@ enum mrb_special_consts { ...@@ -67,56 +67,63 @@ enum mrb_special_consts {
* symbol: ...SSSS SS10 (use only upper 32-bit as symbol value on 64-bit CPU) * symbol: ...SSSS SS10 (use only upper 32-bit as symbol value on 64-bit CPU)
* object: ...PPPP P000 (any bits are 1) * object: ...PPPP P000 (any bits are 1)
*/ */
typedef union mrb_value { typedef uintptr_t mrb_value;
union { union mrb_value {
void *p; void *p;
#ifdef MRB_64BIT #ifdef MRB_64BIT
/* use struct to avoid bit shift. */ /* use struct to avoid bit shift. */
struct { struct {
MRB_ENDIAN_LOHI( MRB_ENDIAN_LOHI(
mrb_sym sym; mrb_sym sym;
,uint32_t sym_flag; ,uint32_t sym_flag;
) )
}; };
#endif #endif
struct RBasic *bp; struct RBasic *bp;
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
struct RFloat *fp; struct RFloat *fp;
#endif #endif
struct RCptr *vp; struct RCptr *vp;
} value; uintptr_t w;
unsigned long w; };
} mrb_value;
static inline union mrb_value
mrb_val_union(mrb_value v)
{
union mrb_value x;
x.w = v;
return x;
}
MRB_API mrb_value mrb_word_boxing_cptr_value(struct mrb_state*, void*); MRB_API mrb_value mrb_word_boxing_cptr_value(struct mrb_state*, void*);
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
MRB_API mrb_value mrb_word_boxing_float_value(struct mrb_state*, mrb_float); MRB_API mrb_value mrb_word_boxing_float_value(struct mrb_state*, mrb_float);
#endif #endif
#define mrb_ptr(o) (o).value.p #define mrb_ptr(o) mrb_val_union(o).p
#define mrb_cptr(o) (o).value.vp->p #define mrb_cptr(o) mrb_val_union(o).vp->p
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
#define mrb_float(o) (o).value.fp->f #define mrb_float(o) mrb_val_union(o).fp->f
#endif #endif
#define mrb_fixnum(o) BOXWORD_SHIFT_VALUE(o, FIXNUM, mrb_int) #define mrb_fixnum(o) BOXWORD_SHIFT_VALUE(o, FIXNUM, mrb_int)
#ifdef MRB_64BIT #ifdef MRB_64BIT
#define mrb_symbol(o) (o).value.sym #define mrb_symbol(o) mrb_val_union(o).sym
#else #else
#define mrb_symbol(o) BOXWORD_SHIFT_VALUE(o, SYMBOL, mrb_sym) #define mrb_symbol(o) BOXWORD_SHIFT_VALUE(o, SYMBOL, mrb_sym)
#endif #endif
#define mrb_bool(o) (((o).w & ~(unsigned long)MRB_Qfalse) != 0) #define mrb_bool(o) (((o) & ~(unsigned long)MRB_Qfalse) != 0)
#define mrb_immediate_p(o) ((o).w & BOXWORD_IMMEDIATE_MASK || (o).w == MRB_Qnil) #define mrb_immediate_p(o) ((o) & BOXWORD_IMMEDIATE_MASK || (o) == MRB_Qnil)
#define mrb_fixnum_p(o) BOXWORD_SHIFT_VALUE_P(o, FIXNUM) #define mrb_fixnum_p(o) BOXWORD_SHIFT_VALUE_P(o, FIXNUM)
#ifdef MRB_64BIT #ifdef MRB_64BIT
#define mrb_symbol_p(o) ((o).value.sym_flag == BOXWORD_SYMBOL_FLAG) #define mrb_symbol_p(o) (mrb_val_union(o).sym_flag == BOXWORD_SYMBOL_FLAG)
#else #else
#define mrb_symbol_p(o) BOXWORD_SHIFT_VALUE_P(o, SYMBOL) #define mrb_symbol_p(o) BOXWORD_SHIFT_VALUE_P(o, SYMBOL)
#endif #endif
#define mrb_undef_p(o) ((o).w == MRB_Qundef) #define mrb_undef_p(o) ((o) == MRB_Qundef)
#define mrb_nil_p(o) ((o).w == MRB_Qnil) #define mrb_nil_p(o) ((o) == MRB_Qnil)
#define mrb_false_p(o) ((o).w == MRB_Qfalse) #define mrb_false_p(o) ((o) == MRB_Qfalse)
#define mrb_true_p(o) ((o).w == MRB_Qtrue) #define mrb_true_p(o) ((o) == MRB_Qtrue)
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
#define mrb_float_p(o) BOXWORD_OBJ_TYPE_P(o, FLOAT) #define mrb_float_p(o) BOXWORD_OBJ_TYPE_P(o, FLOAT)
#endif #endif
...@@ -143,18 +150,23 @@ MRB_API mrb_value mrb_word_boxing_float_value(struct mrb_state*, mrb_float); ...@@ -143,18 +150,23 @@ MRB_API mrb_value mrb_word_boxing_float_value(struct mrb_state*, mrb_float);
#define SET_FLOAT_VALUE(mrb,r,v) ((r) = mrb_word_boxing_float_value(mrb, v)) #define SET_FLOAT_VALUE(mrb,r,v) ((r) = mrb_word_boxing_float_value(mrb, v))
#endif #endif
#define SET_CPTR_VALUE(mrb,r,v) ((r) = mrb_word_boxing_cptr_value(mrb, v)) #define SET_CPTR_VALUE(mrb,r,v) ((r) = mrb_word_boxing_cptr_value(mrb, v))
#define SET_UNDEF_VALUE(r) ((r).w = MRB_Qundef) #define SET_UNDEF_VALUE(r) ((r) = MRB_Qundef)
#define SET_NIL_VALUE(r) ((r).w = MRB_Qnil) #define SET_NIL_VALUE(r) ((r) = MRB_Qnil)
#define SET_FALSE_VALUE(r) ((r).w = MRB_Qfalse) #define SET_FALSE_VALUE(r) ((r) = MRB_Qfalse)
#define SET_TRUE_VALUE(r) ((r).w = MRB_Qtrue) #define SET_TRUE_VALUE(r) ((r) = MRB_Qtrue)
#define SET_BOOL_VALUE(r,b) ((b) ? SET_TRUE_VALUE(r) : SET_FALSE_VALUE(r)) #define SET_BOOL_VALUE(r,b) ((b) ? SET_TRUE_VALUE(r) : SET_FALSE_VALUE(r))
#define SET_INT_VALUE(r,n) BOXWORD_SET_SHIFT_VALUE(r, FIXNUM, n) #define SET_INT_VALUE(r,n) BOXWORD_SET_SHIFT_VALUE(r, FIXNUM, n)
#ifdef MRB_64BIT #ifdef MRB_64BIT
#define SET_SYM_VALUE(r,v) ((r).value.sym = v, (r).value.sym_flag = BOXWORD_SYMBOL_FLAG) #define SET_SYM_VALUE(r,v) do {\
union mrb_value mrb_value_union_variable;\
mrb_value_union_variable.sym = v;\
mrb_value_union_variable.sym_flag = BOXWORD_SYMBOL_FLAG;\
(r) = mrb_value_union_variable.w;\
} while (0)
#else #else
#define SET_SYM_VALUE(r,n) BOXWORD_SET_SHIFT_VALUE(r, SYMBOL, n) #define SET_SYM_VALUE(r,n) BOXWORD_SET_SHIFT_VALUE(r, SYMBOL, n)
#endif #endif
#define SET_OBJ_VALUE(r,v) ((r).value.p = v) #define SET_OBJ_VALUE(r,v) ((r) = (uintptr_t)v)
MRB_INLINE enum mrb_vtype MRB_INLINE enum mrb_vtype
mrb_type(mrb_value o) mrb_type(mrb_value o)
...@@ -164,7 +176,7 @@ mrb_type(mrb_value o) ...@@ -164,7 +176,7 @@ mrb_type(mrb_value o)
mrb_fixnum_p(o) ? MRB_TT_FIXNUM : mrb_fixnum_p(o) ? MRB_TT_FIXNUM :
mrb_symbol_p(o) ? MRB_TT_SYMBOL : mrb_symbol_p(o) ? MRB_TT_SYMBOL :
mrb_undef_p(o) ? MRB_TT_UNDEF : mrb_undef_p(o) ? MRB_TT_UNDEF :
o.value.bp->tt; mrb_val_union(o).bp->tt;
} }
#endif /* MRUBY_BOXING_WORD_H */ #endif /* MRUBY_BOXING_WORD_H */
...@@ -151,12 +151,12 @@ mrb_obj_id(mrb_value obj) ...@@ -151,12 +151,12 @@ mrb_obj_id(mrb_value obj)
MRB_API mrb_value MRB_API mrb_value
mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f) mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f)
{ {
mrb_value v; union mrb_value v;
v.value.p = mrb_obj_alloc(mrb, MRB_TT_FLOAT, mrb->float_class); v.p = mrb_obj_alloc(mrb, MRB_TT_FLOAT, mrb->float_class);
v.value.fp->f = f; v.fp->f = f;
MRB_SET_FROZEN_FLAG(v.value.bp); MRB_SET_FROZEN_FLAG(v.bp);
return v; return v.w;
} }
#endif /* MRB_WITHOUT_FLOAT */ #endif /* MRB_WITHOUT_FLOAT */
#endif /* MRB_WORD_BOXING */ #endif /* MRB_WORD_BOXING */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment