mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/boxing_nan.h@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/boxing_nan.h - nan boxing mrb_value definition |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_BOXING_NAN_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_BOXING_NAN_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #ifdef MRB_USE_FLOAT |
mzta | 0:158c61bb030f | 11 | # error ---->> MRB_NAN_BOXING and MRB_USE_FLOAT conflict <<---- |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | #ifdef MRB_INT64 |
mzta | 0:158c61bb030f | 15 | # error ---->> MRB_NAN_BOXING and MRB_INT64 conflict <<---- |
mzta | 0:158c61bb030f | 16 | #endif |
mzta | 0:158c61bb030f | 17 | |
mzta | 0:158c61bb030f | 18 | #define MRB_FIXNUM_SHIFT 0 |
mzta | 0:158c61bb030f | 19 | #define MRB_TT_HAS_BASIC MRB_TT_OBJECT |
mzta | 0:158c61bb030f | 20 | |
mzta | 0:158c61bb030f | 21 | #ifdef MRB_ENDIAN_BIG |
mzta | 0:158c61bb030f | 22 | #define MRB_ENDIAN_LOHI(a,b) a b |
mzta | 0:158c61bb030f | 23 | #else |
mzta | 0:158c61bb030f | 24 | #define MRB_ENDIAN_LOHI(a,b) b a |
mzta | 0:158c61bb030f | 25 | #endif |
mzta | 0:158c61bb030f | 26 | |
mzta | 0:158c61bb030f | 27 | /* value representation by nan-boxing: |
mzta | 0:158c61bb030f | 28 | * float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF |
mzta | 0:158c61bb030f | 29 | * object: 111111111111TTTT TTPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP |
mzta | 0:158c61bb030f | 30 | * int : 1111111111110001 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII |
mzta | 0:158c61bb030f | 31 | * sym : 1111111111110001 0100000000000000 SSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSS |
mzta | 0:158c61bb030f | 32 | * In order to get enough bit size to save TT, all pointers are shifted 2 bits |
mzta | 0:158c61bb030f | 33 | * in the right direction. Also, TTTTTT is the mrb_vtype + 1; |
mzta | 0:158c61bb030f | 34 | */ |
mzta | 0:158c61bb030f | 35 | typedef struct mrb_value { |
mzta | 0:158c61bb030f | 36 | union { |
mzta | 0:158c61bb030f | 37 | mrb_float f; |
mzta | 0:158c61bb030f | 38 | union { |
mzta | 0:158c61bb030f | 39 | void *p; |
mzta | 0:158c61bb030f | 40 | struct { |
mzta | 0:158c61bb030f | 41 | MRB_ENDIAN_LOHI( |
mzta | 0:158c61bb030f | 42 | uint32_t ttt; |
mzta | 0:158c61bb030f | 43 | ,union { |
mzta | 0:158c61bb030f | 44 | mrb_int i; |
mzta | 0:158c61bb030f | 45 | mrb_sym sym; |
mzta | 0:158c61bb030f | 46 | }; |
mzta | 0:158c61bb030f | 47 | ) |
mzta | 0:158c61bb030f | 48 | }; |
mzta | 0:158c61bb030f | 49 | } value; |
mzta | 0:158c61bb030f | 50 | }; |
mzta | 0:158c61bb030f | 51 | } mrb_value; |
mzta | 0:158c61bb030f | 52 | |
mzta | 0:158c61bb030f | 53 | #define mrb_float_pool(mrb,f) mrb_float_value(mrb,f) |
mzta | 0:158c61bb030f | 54 | |
mzta | 0:158c61bb030f | 55 | #define mrb_tt(o) ((enum mrb_vtype)(((o).value.ttt & 0xfc000)>>14)-1) |
mzta | 0:158c61bb030f | 56 | #define mrb_type(o) ((uint32_t)0xfff00000 < (o).value.ttt ? mrb_tt(o) : MRB_TT_FLOAT) |
mzta | 0:158c61bb030f | 57 | #define mrb_ptr(o) ((void*)((((uintptr_t)0x3fffffffffff)&((uintptr_t)((o).value.p)))<<2)) |
mzta | 0:158c61bb030f | 58 | #define mrb_float(o) (o).f |
mzta | 0:158c61bb030f | 59 | #define mrb_cptr(o) mrb_ptr(o) |
mzta | 0:158c61bb030f | 60 | #define mrb_fixnum(o) (o).value.i |
mzta | 0:158c61bb030f | 61 | #define mrb_symbol(o) (o).value.sym |
mzta | 0:158c61bb030f | 62 | |
mzta | 0:158c61bb030f | 63 | #define BOXNAN_SET_VALUE(o, tt, attr, v) do {\ |
mzta | 0:158c61bb030f | 64 | (o).value.ttt = (0xfff00000|(((tt)+1)<<14));\ |
mzta | 0:158c61bb030f | 65 | switch (tt) {\ |
mzta | 0:158c61bb030f | 66 | case MRB_TT_FALSE:\ |
mzta | 0:158c61bb030f | 67 | case MRB_TT_TRUE:\ |
mzta | 0:158c61bb030f | 68 | case MRB_TT_UNDEF:\ |
mzta | 0:158c61bb030f | 69 | case MRB_TT_FIXNUM:\ |
mzta | 0:158c61bb030f | 70 | case MRB_TT_SYMBOL: (o).attr = (v); break;\ |
mzta | 0:158c61bb030f | 71 | default: (o).value.i = 0; (o).value.p = (void*)((uintptr_t)(o).value.p | (((uintptr_t)(v))>>2)); break;\ |
mzta | 0:158c61bb030f | 72 | }\ |
mzta | 0:158c61bb030f | 73 | } while (0) |
mzta | 0:158c61bb030f | 74 | |
mzta | 0:158c61bb030f | 75 | #define SET_FLOAT_VALUE(mrb,r,v) do { \ |
mzta | 0:158c61bb030f | 76 | if (v != v) { \ |
mzta | 0:158c61bb030f | 77 | (r).value.ttt = 0x7ff80000; \ |
mzta | 0:158c61bb030f | 78 | (r).value.i = 0; \ |
mzta | 0:158c61bb030f | 79 | } else { \ |
mzta | 0:158c61bb030f | 80 | (r).f = v; \ |
mzta | 0:158c61bb030f | 81 | }} while(0) |
mzta | 0:158c61bb030f | 82 | |
mzta | 0:158c61bb030f | 83 | #define SET_NIL_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_FALSE, value.i, 0) |
mzta | 0:158c61bb030f | 84 | #define SET_FALSE_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_FALSE, value.i, 1) |
mzta | 0:158c61bb030f | 85 | #define SET_TRUE_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_TRUE, value.i, 1) |
mzta | 0:158c61bb030f | 86 | #define SET_BOOL_VALUE(r,b) BOXNAN_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1) |
mzta | 0:158c61bb030f | 87 | #define SET_INT_VALUE(r,n) BOXNAN_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n)) |
mzta | 0:158c61bb030f | 88 | #define SET_SYM_VALUE(r,v) BOXNAN_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v)) |
mzta | 0:158c61bb030f | 89 | #define SET_OBJ_VALUE(r,v) BOXNAN_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v)) |
mzta | 0:158c61bb030f | 90 | #define SET_PROC_VALUE(r,v) BOXNAN_SET_VALUE(r, MRB_TT_PROC, value.p, v) |
mzta | 0:158c61bb030f | 91 | #define SET_CPTR_VALUE(mrb,r,v) BOXNAN_SET_VALUE(r, MRB_TT_CPTR, value.p, v) |
mzta | 0:158c61bb030f | 92 | #define SET_UNDEF_VALUE(r) BOXNAN_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0) |
mzta | 0:158c61bb030f | 93 | |
mzta | 0:158c61bb030f | 94 | #endif /* MRUBY_BOXING_NAN_H */ |
mzta | 0:158c61bb030f | 95 |