Commit 249f05e7 authored by David Turnbull's avatar David Turnbull Committed by Yukihiro "Matz" Matsumoto

Clean up value.h and mrb_value boxing

parent 5c50bcd2
......@@ -325,11 +325,11 @@ int mrb_gc_arena_save(mrb_state*);
void mrb_gc_arena_restore(mrb_state*,int);
void mrb_gc_mark(mrb_state*,struct RBasic*);
#define mrb_gc_mark_value(mrb,val) do {\
if (MRB_TT_HAS_BASIC_P(mrb_type(val))) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
} while (0)
void mrb_field_write_barrier(mrb_state *, struct RBasic*, struct RBasic*);
#define mrb_field_write_barrier_value(mrb, obj, val) do{\
if (MRB_TT_HAS_BASIC_P(mrb_type(val))) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
} while (0)
void mrb_write_barrier(mrb_state *, struct RBasic*);
......
/*
** mruby/boxing_nan.h - nan boxing mrb_value definition
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_BOXING_NAN_H
#define MRUBY_BOXING_NAN_H
#ifdef MRB_USE_FLOAT
# error ---->> MRB_NAN_BOXING and MRB_USE_FLOAT conflict <<----
#endif
#ifdef MRB_INT64
# error ---->> MRB_NAN_BOXING and MRB_INT64 conflict <<----
#endif
#define MRB_FIXNUM_SHIFT 0
#define MRB_TT_HAS_BASIC MRB_TT_OBJECT
#ifdef MRB_ENDIAN_BIG
#define MRB_ENDIAN_LOHI(a,b) a b
#else
#define MRB_ENDIAN_LOHI(a,b) b a
#endif
/* value representation by nan-boxing:
* float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF
* object: 111111111111TTTT TTPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP
* int : 1111111111110001 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII
* sym : 1111111111110001 0100000000000000 SSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSS
* In order to get enough bit size to save TT, all pointers are shifted 2 bits
* in the right direction. Also, TTTTTT is the mrb_vtype + 1;
*/
typedef struct mrb_value {
union {
mrb_float f;
union {
void *p;
struct {
MRB_ENDIAN_LOHI(
uint32_t ttt;
,union {
mrb_int i;
mrb_sym sym;
};
)
};
} value;
};
} mrb_value;
#define mrb_float_pool(mrb,f) mrb_float_value(mrb,f)
#define mrb_tt(o) ((enum mrb_vtype)(((o).value.ttt & 0xfc000)>>14)-1)
#define mrb_type(o) ((uint32_t)0xfff00000 < (o).value.ttt ? mrb_tt(o) : MRB_TT_FLOAT)
#define mrb_ptr(o) ((void*)((((uintptr_t)0x3fffffffffff)&((uintptr_t)((o).value.p)))<<2))
#define mrb_float(o) (o).f
#define mrb_cptr(o) mrb_ptr(o)
#define mrb_fixnum(o) (o).value.i
#define mrb_symbol(o) (o).value.sym
#define BOXNAN_SET_VALUE(o, tt, attr, v) do {\
(o).value.ttt = (0xfff00000|(((tt)+1)<<14));\
switch (tt) {\
case MRB_TT_FALSE:\
case MRB_TT_TRUE:\
case MRB_TT_UNDEF:\
case MRB_TT_FIXNUM:\
case MRB_TT_SYMBOL: (o).attr = (v); break;\
default: (o).value.i = 0; (o).value.p = (void*)((uintptr_t)(o).value.p | (((uintptr_t)(v))>>2)); break;\
}\
} while (0)
#define SET_FLOAT_VALUE(mrb,r,v) do { \
if (v != v) { \
(r).value.ttt = 0x7ff80000; \
(r).value.i = 0; \
} else { \
(r).f = v; \
}} while(0)
#define SET_NIL_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_FALSE, value.i, 0)
#define SET_FALSE_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_FALSE, value.i, 1)
#define SET_TRUE_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_TRUE, value.i, 1)
#define SET_BOOL_VALUE(r,b) BOXNAN_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1)
#define SET_FIXNUM_VALUE(r,n) BOXNAN_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n))
#define SET_SYM_VALUE(r,v) BOXNAN_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v))
#define SET_OBJ_VALUE(r,v) BOXNAN_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v))
#define SET_PROC_VALUE(r,v) BOXNAN_SET_VALUE(r, MRB_TT_PROC, value.p, v)
#define SET_CPTR_VALUE(mrb,r,v) BOXNAN_SET_VALUE(r, MRB_TT_CPTR, value.p, v)
#define SET_UNDEF_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0)
#endif /* MRUBY_BOXING_NAN_H */
/*
** mruby/boxing_nix.h - unboxed mrb_value definition
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_BOXING_NIX_H
#define MRUBY_BOXING_NIX_H
#define MRB_FIXNUM_SHIFT 0
#define MRB_TT_HAS_BASIC MRB_TT_OBJECT
typedef struct mrb_value {
union {
mrb_float f;
void *p;
mrb_int i;
mrb_sym sym;
} value;
enum mrb_vtype tt;
} mrb_value;
#define mrb_float_pool(mrb,f) mrb_float_value(mrb,f)
#define mrb_ptr(o) (o).value.p
#define mrb_cptr(o) mrb_ptr(o)
#define mrb_float(o) (o).value.f
#define mrb_fixnum(o) (o).value.i
#define mrb_symbol(o) (o).value.sym
#define mrb_type(o) (o).tt
#define BOXNIX_SET_VALUE(o, ttt, attr, v) do {\
(o).tt = ttt;\
(o).attr = v;\
} while (0)
#define SET_NIL_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_FALSE, value.i, 0)
#define SET_FALSE_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_FALSE, value.i, 1)
#define SET_TRUE_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_TRUE, value.i, 1)
#define SET_BOOL_VALUE(r,b) BOXNIX_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1)
#define SET_FIXNUM_VALUE(r,n) BOXNIX_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n))
#define SET_FLOAT_VALUE(mrb,r,v) BOXNIX_SET_VALUE(r, MRB_TT_FLOAT, value.f, (v))
#define SET_SYM_VALUE(r,v) BOXNIX_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v))
#define SET_OBJ_VALUE(r,v) BOXNIX_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v))
#define SET_PROC_VALUE(r,v) BOXNIX_SET_VALUE(r, MRB_TT_PROC, value.p, v)
#define SET_CPTR_VALUE(mrb,r,v) BOXNIX_SET_VALUE(r, MRB_TT_CPTR, value.p, v)
#define SET_UNDEF_VALUE(r) BOXNIX_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0)
#endif /* MRUBY_BOXING_NIX_H */
/*
** mruby/boxing_word.h - word boxing mrb_value definition
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_BOXING_WORD_H
#define MRUBY_BOXING_WORD_H
#if defined(MRB_INT16)
# error MRB_INT16 is too small for MRB_WORD_BOXING.
#endif
#define MRB_FIXNUM_SHIFT 1
#define MRB_TT_HAS_BASIC MRB_TT_FLOAT
enum mrb_special_consts {
MRB_Qnil = 0,
MRB_Qfalse = 2,
MRB_Qtrue = 4,
MRB_Qundef = 6,
};
#define MRB_FIXNUM_FLAG 0x01
#define MRB_SYMBOL_FLAG 0x0e
#define MRB_SPECIAL_SHIFT 8
typedef union mrb_value {
union {
void *p;
struct {
unsigned int i_flag : MRB_FIXNUM_SHIFT;
mrb_int i : (MRB_INT_BIT - MRB_FIXNUM_SHIFT);
};
struct {
unsigned int sym_flag : MRB_SPECIAL_SHIFT;
int sym : (sizeof(mrb_sym) * CHAR_BIT);
};
struct RBasic *bp;
struct RFloat *fp;
struct RCptr *vp;
} value;
unsigned long w;
} mrb_value;
mrb_value word_boxing_mrb_cptr_value(struct mrb_state*, void*);
mrb_value word_boxing_mrb_float_value(struct mrb_state*, mrb_float);
mrb_value word_boxing_mrb_float_pool(struct mrb_state*, mrb_float);
#define mrb_float_pool(mrb,f) word_boxing_mrb_float_pool(mrb,f)
#define mrb_ptr(o) (o).value.p
#define mrb_cptr(o) (o).value.vp->p
#define mrb_float(o) (o).value.fp->f
#define mrb_fixnum(o) (o).value.i
#define mrb_symbol(o) (o).value.sym
static inline enum mrb_vtype
mrb_type(mrb_value o)
{
switch (o.w) {
case MRB_Qfalse:
case MRB_Qnil:
return MRB_TT_FALSE;
case MRB_Qtrue:
return MRB_TT_TRUE;
case MRB_Qundef:
return MRB_TT_UNDEF;
}
if (o.value.i_flag == MRB_FIXNUM_FLAG) {
return MRB_TT_FIXNUM;
}
if (o.value.sym_flag == MRB_SYMBOL_FLAG) {
return MRB_TT_SYMBOL;
}
return o.value.bp->tt;
}
#define mrb_bool(o) ((o).w != MRB_Qnil && (o).w != MRB_Qfalse)
#define mrb_fixnum_p(o) ((o).value.i_flag == MRB_FIXNUM_FLAG)
#define mrb_undef_p(o) ((o).w == MRB_Qundef)
#define mrb_nil_p(o) ((o).w == MRB_Qnil)
#define BOXWORD_SET_VALUE(o, ttt, attr, v) do {\
(o).w = 0;\
(o).attr = (v);\
switch (ttt) {\
case MRB_TT_FALSE: (o).w = (v) ? MRB_Qfalse : MRB_Qnil; break;\
case MRB_TT_TRUE: (o).w = MRB_Qtrue; break;\
case MRB_TT_UNDEF: (o).w = MRB_Qundef; break;\
case MRB_TT_FIXNUM: (o).value.i_flag = MRB_FIXNUM_FLAG; break;\
case MRB_TT_SYMBOL: (o).value.sym_flag = MRB_SYMBOL_FLAG; break;\
default: if ((o).value.bp) (o).value.bp->tt = ttt; break;\
}\
} while (0)
#define SET_FLOAT_VALUE(mrb,r,v) r = word_boxing_mrb_float_value(mrb, v)
#define SET_CPTR_VALUE(mrb,r,v) r = word_boxing_mrb_cptr_value(mrb, v)
#define SET_NIL_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_FALSE, value.i, 0)
#define SET_FALSE_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_FALSE, value.i, 1)
#define SET_TRUE_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_TRUE, value.i, 1)
#define SET_BOOL_VALUE(r,b) BOXWORD_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1)
#define SET_FIXNUM_VALUE(r,n) BOXWORD_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n))
#define SET_SYM_VALUE(r,v) BOXWORD_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v))
#define SET_OBJ_VALUE(r,v) BOXWORD_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v))
#define SET_PROC_VALUE(r,v) BOXWORD_SET_VALUE(r, MRB_TT_PROC, value.p, v)
#define SET_UNDEF_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0)
#endif /* MRUBY_BOXING_WORD_H */
......@@ -28,7 +28,7 @@ mrb_class(mrb_state *mrb, mrb_value v)
{
switch (mrb_type(v)) {
case MRB_TT_FALSE:
if (v.value.i)
if (mrb_fixnum(v))
return mrb->false_class;
return mrb->nil_class;
case MRB_TT_TRUE:
......
/*
** mruby/object.h - mruby object definition
**
** See Copyright Notice in mruby.h
*/
#ifndef MRUBY_OBJECT_H
#define MRUBY_OBJECT_H
#define MRB_OBJECT_HEADER \
enum mrb_vtype tt:8;\
uint32_t color:3;\
uint32_t flags:21;\
struct RClass *c;\
struct RBasic *gcnext
/* white: 011, black: 100, gray: 000 */
#define MRB_GC_GRAY 0
#define MRB_GC_WHITE_A 1
#define MRB_GC_WHITE_B (1 << 1)
#define MRB_GC_BLACK (1 << 2)
#define MRB_GC_WHITES (MRB_GC_WHITE_A | MRB_GC_WHITE_B)
#define MRB_GC_COLOR_MASK 7
#define paint_gray(o) ((o)->color = MRB_GC_GRAY)
#define paint_black(o) ((o)->color = MRB_GC_BLACK)
#define paint_white(o) ((o)->color = MRB_GC_WHITES)
#define paint_partial_white(s, o) ((o)->color = (s)->current_white_part)
#define is_gray(o) ((o)->color == MRB_GC_GRAY)
#define is_white(o) ((o)->color & MRB_GC_WHITES)
#define is_black(o) ((o)->color & MRB_GC_BLACK)
#define is_dead(s, o) (((o)->color & other_white_part(s) & MRB_GC_WHITES) || (o)->tt == MRB_TT_FREE)
#define flip_white_part(s) ((s)->current_white_part = other_white_part(s))
#define other_white_part(s) ((s)->current_white_part ^ MRB_GC_WHITES)
struct RBasic {
MRB_OBJECT_HEADER;
};
#define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v)))
/* obsolete macro mrb_basic; will be removed soon */
#define mrb_basic(v) mrb_basic_ptr(v)
struct RObject {
MRB_OBJECT_HEADER;
struct iv_tbl *iv;
};
#define mrb_obj_ptr(v) ((struct RObject*)(mrb_ptr(v)))
/* obsolete macro mrb_object; will be removed soon */
#define mrb_object(o) mrb_obj_ptr(o)
#define mrb_immediate_p(x) (mrb_type(x) < MRB_TT_HAS_BASIC)
#define mrb_special_const_p(x) mrb_immediate_p(x)
struct RFiber {
MRB_OBJECT_HEADER;
struct mrb_context *cxt;
};
struct RFloat {
MRB_OBJECT_HEADER;
mrb_float f;
};
struct RCptr {
MRB_OBJECT_HEADER;
void *p;
};
#endif /* MRUBY_OBJECT_H */
This diff is collapsed.
......@@ -8,8 +8,6 @@
#define FIBER_STACK_INIT_SIZE 64
#define FIBER_CI_INIT_SIZE 8
#define SET_NIL_VALUE(r) MRB_SET_VALUE(r, MRB_TT_FALSE, value.i, 0)
/*
* call-seq:
* Fiber.new{...} -> obj
......
......@@ -1500,7 +1500,7 @@ undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
mrb_name_error(mrb, a, "undefined method '%S' for class '%S'", mrb_sym2str(mrb, a), mrb_obj_value(c));
}
else {
MRB_SET_VALUE(m, MRB_TT_PROC, value.p, 0);
SET_PROC_VALUE(m, 0);
mrb_define_method_vm(mrb, c, a, m);
}
}
......
......@@ -148,7 +148,7 @@ mrb_obj_id(mrb_value obj)
#ifdef MRB_WORD_BOXING
mrb_value
mrb_float_value(mrb_state *mrb, mrb_float f)
word_boxing_mrb_float_value(mrb_state *mrb, mrb_float f)
{
mrb_value v;
......@@ -158,7 +158,7 @@ mrb_float_value(mrb_state *mrb, mrb_float f)
}
mrb_value
mrb_float_pool(mrb_state *mrb, mrb_float f)
word_boxing_mrb_float_pool(mrb_state *mrb, mrb_float f)
{
struct RFloat *nf = (struct RFloat *)mrb_malloc(mrb, sizeof(struct RFloat));
nf->tt = MRB_TT_FLOAT;
......@@ -168,7 +168,7 @@ mrb_float_pool(mrb_state *mrb, mrb_float f)
}
mrb_value
mrb_cptr_value(mrb_state *mrb, void *p)
word_boxing_mrb_cptr_value(mrb_state *mrb, void *p)
{
mrb_value v;
......
......@@ -687,12 +687,8 @@ int_to_i(mrb_state *mrb, mrb_value num)
return num;
}
#ifdef MRB_FIXNUM_SHIFT
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1-MRB_FIXNUM_SHIFT)/2))
#else
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1)/2))
#endif
/*tests if N*N would overflow*/
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1-MRB_FIXNUM_SHIFT)/2))
#define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
mrb_value
......
......@@ -19,9 +19,9 @@ mrb_obj_eq(mrb_state *mrb, mrb_value v1, mrb_value v2)
case MRB_TT_FALSE:
case MRB_TT_FIXNUM:
return (v1.value.i == v2.value.i);
return (mrb_fixnum(v1) == mrb_fixnum(v2));
case MRB_TT_SYMBOL:
return (v1.value.sym == v2.value.sym);
return (mrb_symbol(v1) == mrb_symbol(v2));
case MRB_TT_FLOAT:
return (mrb_float(v1) == mrb_float(v2));
......
This diff is collapsed.
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