From f5537912abed308ad6967ae52bfa8699f14dae93 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 21 Dec 2018 08:49:23 +0900 Subject: Define `MRB_ENDIAN_BIG` automatically; ref #4190 You had to define this macro on big endian platforms, but it is very error-prone. So define the macro automatically if possible. --- include/mrbconf.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index cc28acfaa..553ed62cc 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -58,12 +58,16 @@ # endif #endif +/* define on big endian machines; used by MRB_NAN_BOXING, etc. */ +#ifndef MRB_ENDIAN_BIG +# if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN +# define MRB_ENDIAN_BIG +# endif +#endif + /* represent mrb_value in boxed double; conflict with MRB_USE_FLOAT and MRB_WITHOUT_FLOAT */ //#define MRB_NAN_BOXING -/* define on big endian machines; used by MRB_NAN_BOXING */ -//#define MRB_ENDIAN_BIG - /* represent mrb_value as a word (natural unit of data for the processor) */ //#define MRB_WORD_BOXING -- cgit v1.2.3 From ed2bc5d4ed363f8d8d1f4bee69555972cfbed262 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 21 Dec 2018 09:28:19 +0900 Subject: Simplify `MRB_ENDIAN_BIG` macro definition; ref #4190 `cpp` does not raise error on undefined macro access in condition. --- include/mrbconf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index 553ed62cc..9bdd11af6 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -60,7 +60,7 @@ /* define on big endian machines; used by MRB_NAN_BOXING, etc. */ #ifndef MRB_ENDIAN_BIG -# if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN +# if BYTE_ORDER == BIG_ENDIAN # define MRB_ENDIAN_BIG # endif #endif -- cgit v1.2.3 From e9ceb865b6621739c217f68bebea023069ef63ca Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 22 Dec 2018 20:37:51 +0900 Subject: Fix MRB_ENDIAN_BIG is always defined, if byte order macro is not defined --- include/mrbconf.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index 9bdd11af6..b1aec8334 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -60,7 +60,8 @@ /* define on big endian machines; used by MRB_NAN_BOXING, etc. */ #ifndef MRB_ENDIAN_BIG -# if BYTE_ORDER == BIG_ENDIAN +# if (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \ + (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) # define MRB_ENDIAN_BIG # endif #endif -- cgit v1.2.3 From 37bc343e0ad6b837de672c8c0cf6bf00876f746b Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 6 Feb 2019 23:35:26 +0900 Subject: Disable `Symbol.all_symbols`. --- include/mrbconf.h | 1 + mrbgems/mruby-symbol-ext/src/symbol.c | 9 ++++----- mrbgems/mruby-symbol-ext/test/symbol.rb | 11 +++++++---- src/symbol.c | 6 ++++++ 4 files changed, 18 insertions(+), 9 deletions(-) (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index b1aec8334..08e69d3aa 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -120,6 +120,7 @@ /* -DMRB_ENABLE_XXXX to enable following features */ //#define MRB_ENABLE_DEBUG_HOOK /* hooks for debugger */ +//#define MRB_ENABLE_ALL_SYMBOLS /* Symbols.all_symbols */ /* end of configuration */ diff --git a/mrbgems/mruby-symbol-ext/src/symbol.c b/mrbgems/mruby-symbol-ext/src/symbol.c index 05e7d5b91..ccb2971dc 100644 --- a/mrbgems/mruby-symbol-ext/src/symbol.c +++ b/mrbgems/mruby-symbol-ext/src/symbol.c @@ -3,11 +3,6 @@ #include #include -typedef struct symbol_name { - size_t len; - const char *name; -} symbol_name; - /* * call-seq: * Symbol.all_symbols => array @@ -23,6 +18,7 @@ typedef struct symbol_name { * :Tms, :getwd, :$=, :ThreadGroup, * :wait2, :$>] */ +#ifdef MRB_ENABLE_ALL_SYMBOLS static mrb_value mrb_sym_all_symbols(mrb_state *mrb, mrb_value self) { @@ -36,6 +32,7 @@ mrb_sym_all_symbols(mrb_state *mrb, mrb_value self) return ary; } +#endif /* * call-seq: @@ -61,7 +58,9 @@ void mrb_mruby_symbol_ext_gem_init(mrb_state* mrb) { struct RClass *s = mrb->symbol_class; +#ifdef MRB_ENABLE_ALL_SYMBOLS mrb_define_class_method(mrb, s, "all_symbols", mrb_sym_all_symbols, MRB_ARGS_NONE()); +#endif mrb_define_method(mrb, s, "length", mrb_sym_length, MRB_ARGS_NONE()); mrb_define_method(mrb, s, "size", mrb_sym_length, MRB_ARGS_NONE()); } diff --git a/mrbgems/mruby-symbol-ext/test/symbol.rb b/mrbgems/mruby-symbol-ext/test/symbol.rb index 2c7a62b0c..61ecad247 100644 --- a/mrbgems/mruby-symbol-ext/test/symbol.rb +++ b/mrbgems/mruby-symbol-ext/test/symbol.rb @@ -1,10 +1,13 @@ +# coding: utf-8 ## # Symbol(Ext) Test -assert('Symbol.all_symbols') do - foo = [:__symbol_test_1, :__symbol_test_2, :__symbol_test_3].sort - symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort - assert_equal foo, symbols +if Symbol.respond_to?(:all_symbols) + assert('Symbol.all_symbols') do + foo = [:__symbol_test_1, :__symbol_test_2, :__symbol_test_3].sort + symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort + assert_equal foo, symbols + end end %w[size length].each do |n| diff --git a/src/symbol.c b/src/symbol.c index ab7504772..9cc566245 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -28,6 +28,7 @@ sym_validate_len(mrb_state *mrb, size_t len) } } +#ifndef MRB_ENABLE_ALL_SYMBOLS static char pack_table[] = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static mrb_sym @@ -100,6 +101,7 @@ sym_inline_unpack(mrb_sym sym, char *buf) buf[i] = '\0'; return buf; } +#endif uint8_t symhash(const char *key, size_t len) @@ -123,9 +125,11 @@ find_symbol(mrb_state *mrb, const char *name, uint16_t len, uint8_t hash) mrb_sym i; symbol_name *sname; +#ifndef MRB_ENABLE_ALL_SYMBOLS /* inline symbol */ i = sym_inline_pack(name, len); if (i > 0) return i; +#endif i = mrb->symhash[hash]; if (i == 0) return 0; @@ -247,11 +251,13 @@ mrb_check_intern_str(mrb_state *mrb, mrb_value str) MRB_API const char* mrb_sym2name_len(mrb_state *mrb, mrb_sym sym, mrb_int *lenp) { +#ifndef MRB_ENABLE_ALL_SYMBOLS if (sym & 1) { /* inline packed symbol */ sym_inline_unpack(sym, mrb->symbuf); if (lenp) *lenp = strlen(mrb->symbuf); return mrb->symbuf; } +#endif sym >>= 1; if (sym == 0 || mrb->symidx < sym) { -- cgit v1.2.3 From b57f61ac95254cdd9becf8aab1e4e15bae404564 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 27 Apr 2019 22:28:54 +0900 Subject: Update document for any configurations - (Modify) `MRB_INT16` - (Add) `MRB_INT32` - (Modify) `MRB_INT64` - (Add) `MRB_USE_ETEXT_EDATA` - (Add) `MRB_NO_INIT_ARRAY_START - (Add) `MRB_WITHOUT_FLOAT` - (Add) `MRB_METHOD_CACHE` - (Add) `MRB_METHOD_CACHE_SIZE` - (Add) `MRB_METHOD_TABLE_INLINE - (Add) `MRB_ENABLE_ALL_SYMBOLS` --- doc/guides/mrbconf.md | 49 +++++++++++++++++++++++++++++++++++++++++++------ include/mrbconf.h | 9 +++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) (limited to 'include/mrbconf.h') diff --git a/doc/guides/mrbconf.md b/doc/guides/mrbconf.md index f957f8ce2..4e3a81aa3 100644 --- a/doc/guides/mrbconf.md +++ b/doc/guides/mrbconf.md @@ -50,15 +50,21 @@ You can use mrbconfs with following ways: * When defined single precision floating point type(C type `float`) is used as `mrb_float`. * Else double precision floating point type(C type `double`) is used as `mrb_float`. +`MRB_WITHOUT_FLOAT` +* When defined removes floating point numbers from mruby. +* It makes mruby easier to handle in "Microcontroller without FPU" and "Kernel Space". + `MRB_INT16` * When defined `int16_t` will be defined as `mrb_int`. -* Conflicts with `MRB_INT64`. +* Conflicts with `MRB_INT32` and `MRB_INT64`. + +`MRB_INT32` +* When defined, or both `MRB_INT16` and `MRB_INT64` are not defined on 32-bit CPU mode, `int32_t` will be defined as `mrb_int`. +* Conflicts with `MRB_INT16` and `MRB_INT64`. `MRB_INT64` -* When defined `int64_t` will be defined as `mrb_int`. -* Conflicts with `MRB_INT16`. -* When `MRB_INT16` or `MRB_INT64` isn't defined `int`(most of the times 32-bit integer) -will be defined as `mrb_int`. +* When defined, or both `MRB_INT16` and `MRB_INT32` are not defined on 64-bit CPU mode, `int64_t` will be defined as `mrb_int`. +* Conflicts with `MRB_INT16` and `MRB_INT32`. ## Garbage collector configuration. @@ -115,7 +121,7 @@ largest value of required alignment. `MRB_NAN_BOXING` * If defined represent `mrb_value` in boxed `double`. -* Conflicts with `MRB_USE_FLOAT`. +* Conflicts with `MRB_USE_FLOAT` and `MRB_WITHOUT_FLOAT`. `MRB_WORD_BOXING` * If defined represent `mrb_value` as a word. @@ -126,6 +132,20 @@ largest value of required alignment. * Default value is `4`. * Specifies size of each segment in segment list. +## Reduce heap memory configuration. + +`MRB_USE_ETEXT_EDATA` +* If you specify the address of a read-only section when creating a symbol or string, that string will be used as it is. +* Heap memory can be saved. +* Uses `_etext` and `__init_array_start`. +* It must be `_etext < data_addr < &__init_array_start`. + +`MRB_NO_INIT_ARRAY_START` +* Ignored if `MRB_USE_ETEXT_EDATA` is not defined. +* Please try if `__init_array_start` is not available. +* Uses `_etext` and `_edata`. +* It must be `_etext < data_addr < _edata`. + ## Other configuration. `MRB_UTF8_STRING` * Adds UTF-8 encoding support to character-oriented String instance methods. @@ -144,3 +164,20 @@ largest value of required alignment. `MRB_STR_BUF_MIN_SIZE` * Default value is `128`. * Specifies initial capacity of `RString` created by `mrb_str_buf_new` function.. + +`MRB_METHOD_CACHE` +* Improve performance for method dispatch. + +`MRB_METHOD_CACHE_SIZE` +* Default value is `128`. +* Ignored if `MRB_METHOD_CACHE` is not defined. +* Need to be the power of 2. + +`MRB_METHOD_TABLE_INLINE` +* Reduce the size of method table. +* Requires LSB of function pointers to be zero. +* For example, you might need to specify `--falign-functions=n` (where `n > 1`) for GCC. + +`MRB_ENABLE_ALL_SYMBOLS` +* Make it available `Symbols.all_symbols` in `mrbgems/mruby-symbol-ext` +* Increase heap memory usage. diff --git a/include/mrbconf.h b/include/mrbconf.h index 08e69d3aa..caae08d7c 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -41,10 +41,15 @@ /* you might need to specify --falign-functions=n (where n>1) */ //#define MRB_METHOD_TABLE_INLINE -/* add -DMRB_INT16 to use 16bit integer for mrb_int; conflict with MRB_INT64 */ +/* add -DMRB_INT16 to use 16bit integer for mrb_int; conflict with MRB_INT32 and MRB_INT64 */ //#define MRB_INT16 -/* add -DMRB_INT64 to use 64bit integer for mrb_int; conflict with MRB_INT16 */ +/* add -DMRB_INT32 to use 32bit integer for mrb_int; conflict with MRB_INT16 and MRB_INT64; + Default for 32-bit CPU mode. */ +//#define MRB_INT32 + +/* add -DMRB_INT64 to use 64bit integer for mrb_int; conflict with MRB_INT16 and MRB_INT32; + Default for 64-bit CPU mode. */ //#define MRB_INT64 /* if no specific integer type is chosen */ -- cgit v1.2.3 From 828a99894179e858b4ddda4ba686413b56f986de Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 27 Apr 2019 22:29:20 +0900 Subject: Update document for `MRB_USE_CUSTOM_RO_DATA_P` --- doc/guides/mrbconf.md | 7 +++++++ include/mrbconf.h | 5 +++++ 2 files changed, 12 insertions(+) (limited to 'include/mrbconf.h') diff --git a/doc/guides/mrbconf.md b/doc/guides/mrbconf.md index 4e3a81aa3..3c20b3388 100644 --- a/doc/guides/mrbconf.md +++ b/doc/guides/mrbconf.md @@ -146,6 +146,13 @@ largest value of required alignment. * Uses `_etext` and `_edata`. * It must be `_etext < data_addr < _edata`. +`MRB_USE_CUSTOM_RO_DATA_P` +* Takes precedence over `MRB_USE_ETEXT_EDATA`. +* Please try if both `MRB_USE_ETEXT_EDATA` and `MRB_NO_INIT_ARRAY_START` are not available. +* The `mrb_ro_data_p()` function is implemented by the user in an arbitrary file. +* The prototype declaration is `mrb_bool mrb_ro_data_p(const char *ptr)`. +* Return `TRUE` if `ptr` is in read-only section, otherwise return `FALSE`. + ## Other configuration. `MRB_UTF8_STRING` * Adds UTF-8 encoding support to character-oriented String instance methods. diff --git a/include/mrbconf.h b/include/mrbconf.h index caae08d7c..6b0b9e37e 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -93,6 +93,11 @@ effective only when MRB_USE_ETEXT_EDATA is defined */ //#define MRB_NO_INIT_ARRAY_START +/* if do not works both MRB_USE_ETEXT_EDATA and MRB_NO_INIT_ARRAY_START, + you can try mrb_ro_data_p() that you have implemented yourself in any file; + prototype is `mrb_bool mrb_ro_data_p(const char *ptr)` */ +//#define MRB_USE_CUSTOM_RO_DATA_P + /* turn off generational GC by default */ //#define MRB_GC_TURN_OFF_GENERATIONAL -- cgit v1.2.3 From 8808219e6d51673e6fa582819703e6e5912439b0 Mon Sep 17 00:00:00 2001 From: Ukrainskiy Sergey Date: Thu, 9 Aug 2018 17:09:02 +0900 Subject: Initial suffix support --- include/mrbconf.h | 3 + mrbgems/default.gembox | 6 ++ mrbgems/mruby-compiler/core/parse.y | 148 +++++++++++++++++++++++++++--- mrbgems/mruby-complex/mrbgem.rake | 5 + mrbgems/mruby-complex/mrblib/complex.rb | 95 +++++++++++++++++++ mrbgems/mruby-complex/test/complex.rb | 3 + mrbgems/mruby-rational/mrbgem.rake | 5 + mrbgems/mruby-rational/mrblib/rational.rb | 46 ++++++++++ mrbgems/mruby-rational/test/rational.rb | 3 + 9 files changed, 301 insertions(+), 13 deletions(-) create mode 100644 mrbgems/mruby-complex/mrbgem.rake create mode 100644 mrbgems/mruby-complex/mrblib/complex.rb create mode 100644 mrbgems/mruby-complex/test/complex.rb create mode 100644 mrbgems/mruby-rational/mrbgem.rake create mode 100644 mrbgems/mruby-rational/mrblib/rational.rb create mode 100644 mrbgems/mruby-rational/test/rational.rb (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index 6b0b9e37e..ee816e322 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -63,6 +63,9 @@ # endif #endif +#define MRB_COMPLEX_NUMBERS +#define MRB_RATIONAL_NUMBERS + /* define on big endian machines; used by MRB_NAN_BOXING, etc. */ #ifndef MRB_ENDIAN_BIG # if (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \ diff --git a/mrbgems/default.gembox b/mrbgems/default.gembox index 23e65fcee..501aee146 100644 --- a/mrbgems/default.gembox +++ b/mrbgems/default.gembox @@ -68,6 +68,12 @@ MRuby::GemBox.new do |conf| # Use Enumerator::Lazy class (require mruby-enumerator) conf.gem :core => "mruby-enum-lazy" + # Use Complex class + #conf.gem :core => "mruby-complex" + + # Use Rational class + #conf.gem :core => "mruby-rational" + # Use toplevel object (main) methods extension conf.gem :core => "mruby-toplevel-ext" diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index b7d47118f..b18906ed3 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -76,6 +76,24 @@ typedef unsigned int stack_type; #define nint(x) ((node*)(intptr_t)(x)) #define intn(x) ((int)(intptr_t)(x)) +#if defined(MRB_COMPLEX_NUMBERS) || defined(MRB_RATIONAL_NUMBERS) + #define MRB_SUFFIX_SUPPORT + + #ifdef MRB_RATIONAL_NUMBERS + #define NUM_SUFFIX_R (1<<0) + #else + #define NUM_SUFFIX_R 0 + #endif + + #ifdef MRB_COMPLEX_NUMBERS + #define NUM_SUFFIX_I (1<<1) + #else + #define NUM_SUFFIX_I 0 + #endif + + #define NUM_SUFFIX_ALL (NUM_SUFFIX_R | NUM_SUFFIX_I) +#endif + static inline mrb_sym intern_cstr_gen(parser_state *p, const char *s) { @@ -842,19 +860,62 @@ new_op_asgn(parser_state *p, node *a, mrb_sym op, node *b) return list4((node*)NODE_OP_ASGN, a, nsym(op), b); } +#ifdef MRB_COMPLEX_NUMBERS +static node* +new_imaginary(parser_state *p, node *imaginary); +#endif + +#ifdef MRB_RATIONAL_NUMBERS +static node* +new_rational(parser_state *p, node *rational) +{ + return new_call(p, new_const(p, intern_cstr("Rational")), intern_cstr("new"), list1(list1(rational)), 1); +} +#endif + /* (:int . i) */ static node* -new_int(parser_state *p, const char *s, int base) +new_int(parser_state *p, const char *s, int base, int suffix) { - return list3((node*)NODE_INT, (node*)strdup(s), nint(base)); + node* result = list3((node*)NODE_INT, (node*)strdup(s), nint(base)); +#ifdef MRB_RATIONAL_NUMBERS + if (suffix & NUM_SUFFIX_R) { + result = new_rational(p, result); + } +#endif +#ifdef MRB_COMPLEX_NUMBERS + if (suffix & NUM_SUFFIX_I) { + result = new_imaginary(p, result); + } +#endif + return result; } #ifndef MRB_WITHOUT_FLOAT /* (:float . i) */ static node* -new_float(parser_state *p, const char *s) +new_float(parser_state *p, const char *s, int suffix) +{ + node* result = cons((node*)NODE_FLOAT, (node*)strdup(s)); +#ifdef MRB_RATIONAL_NUMBERS + if (suffix & NUM_SUFFIX_R) { + result = new_rational(p, result); + } +#endif +#ifdef MRB_COMPLEX_NUMBERS + if (suffix & NUM_SUFFIX_I) { + result = new_imaginary(p, result); + } +#endif + return result; +} +#endif + +#ifdef MRB_COMPLEX_NUMBERS +static node* +new_imaginary(parser_state *p, node *imaginary) { - return cons((node*)NODE_FLOAT, (node*)strdup(s)); + return new_call(p, new_const(p, intern_cstr("Complex")), intern_cstr("new"), list1(list2(new_int(p, "0", 10, 0), imaginary)), 1); } #endif @@ -3192,7 +3253,7 @@ var_ref : variable char buf[16]; dump_int(p->lineno, buf); - $$ = new_int(p, buf, 10); + $$ = new_int(p, buf, 10, 0); } | keyword__ENCODING__ { @@ -4520,6 +4581,45 @@ parse_string(parser_state *p) return tSTRING; } +#ifdef MRB_SUFFIX_SUPPORT +static int +number_literal_suffix(parser_state *p, int mask) +{ + int c, result = 0; + node *list = 0; + int column = p->column; + + while ((c = nextc(p)) != -1) { + list = push(list, (node*)(intptr_t)c); + + if ((mask & NUM_SUFFIX_I) && c == 'i') { + result |= (mask & NUM_SUFFIX_I); + mask &= ~NUM_SUFFIX_I; + /* r after i, rational of complex is disallowed */ + mask &= ~NUM_SUFFIX_R; + continue; + } + if ((mask & NUM_SUFFIX_R) && c == 'r') { + result |= (mask & NUM_SUFFIX_R); + mask &= ~NUM_SUFFIX_R; + continue; + } + if (!ISASCII(c) || ISALPHA(c) || c == '_') { + p->column = column; + if (p->pb) { + p->pb = append((node*)list, p->pb); + } + else { + p->pb = list; + } + return 0; + } + pushback(p, c); + break; + } + return result; +} +#endif static int heredoc_identifier(parser_state *p) @@ -5094,6 +5194,7 @@ parser_yylex(parser_state *p) case '5': case '6': case '7': case '8': case '9': { int is_float, seen_point, seen_e, nondigit; + int suffix; is_float = seen_point = seen_e = nondigit = 0; p->lstate = EXPR_ENDARG; @@ -5127,7 +5228,10 @@ parser_yylex(parser_state *p) no_digits(); } else if (nondigit) goto trailing_uc; - pylval.nd = new_int(p, tok(p), 16); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, tok(p), 16, suffix); return tINTEGER; } if (c == 'b' || c == 'B') { @@ -5151,7 +5255,10 @@ parser_yylex(parser_state *p) no_digits(); } else if (nondigit) goto trailing_uc; - pylval.nd = new_int(p, tok(p), 2); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, tok(p), 2, suffix); return tINTEGER; } if (c == 'd' || c == 'D') { @@ -5175,7 +5282,10 @@ parser_yylex(parser_state *p) no_digits(); } else if (nondigit) goto trailing_uc; - pylval.nd = new_int(p, tok(p), 10); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, tok(p), 10, suffix); return tINTEGER; } if (c == '_') { @@ -5208,7 +5318,10 @@ parser_yylex(parser_state *p) pushback(p, c); tokfix(p); if (nondigit) goto trailing_uc; - pylval.nd = new_int(p, tok(p), 8); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, tok(p), 8, suffix); return tINTEGER; } if (nondigit) { @@ -5225,7 +5338,10 @@ parser_yylex(parser_state *p) } else { pushback(p, c); - pylval.nd = new_int(p, "0", 10); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, "0", 10, suffix); return tINTEGER; } } @@ -5299,7 +5415,7 @@ parser_yylex(parser_state *p) if (is_float) { #ifdef MRB_WITHOUT_FLOAT yywarning(p, "floating point numbers are not supported"); - pylval.nd = new_int(p, "0", 10); + pylval.nd = new_int(p, "0", 10, 0); return tINTEGER; #else double d; @@ -5314,11 +5430,17 @@ parser_yylex(parser_state *p) yywarning_s(p, "float out of range", tok(p)); errno = 0; } - pylval.nd = new_float(p, tok(p)); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_float(p, tok(p), suffix); return tFLOAT; #endif } - pylval.nd = new_int(p, tok(p), 10); + #ifdef MRB_SUFFIX_SUPPORT + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + #endif + pylval.nd = new_int(p, tok(p), 10, suffix); return tINTEGER; } diff --git a/mrbgems/mruby-complex/mrbgem.rake b/mrbgems/mruby-complex/mrbgem.rake new file mode 100644 index 000000000..f8f04d0b8 --- /dev/null +++ b/mrbgems/mruby-complex/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-complex') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' + spec.summary = 'Complex class' +end diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb new file mode 100644 index 000000000..0815c9a71 --- /dev/null +++ b/mrbgems/mruby-complex/mrblib/complex.rb @@ -0,0 +1,95 @@ +class Complex < Numeric + def initialize(real = 0, imaginary = 0) + @real = real + @imaginary = imaginary + end + + def inspect + "(#{to_s})" + end + + def to_s + "#{real}#{'+'}#{imaginary}i" + end + + def +@ + Complex.new(real, imaginary) + end + + def -@ + Complex.new(-real, -imaginary) + end + + def +(rhs) + if rhs.is_a? Complex + Complex.new(real + rhs.real, imaginary + rhs.imaginary) + elsif rhs.is_a? Numeric + Complex.new(real + rhs, imaginary) + end + end + + def -(rhs) + if rhs.is_a? Complex + Complex.new(real - rhs.real, imaginary - rhs.imaginary) + elsif rhs.is_a? Numeric + Complex.new(real - rhs, imaginary) + end + end + + def *(rhs) + if rhs.is_a? Complex + Complex.new(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary) + elsif rhs.is_a? Numeric + Complex.new(real * rhs, imaginary * rhs) + end + end + + def /(rhs) + if rhs.is_a? Complex + div = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary + Complex.new((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div) + elsif rhs.is_a? Numeric + Complex.new(real / rhs, imaginary / rhs) + end + end + + attr_reader :real, :imaginary +end + +def Complex(real = 0, imaginary = 0) + Complex.new(real, imaginary) +end + +module ForwardOperatorToComplex + def __forward_operator_to_complex(op, &b) + original_operator_name = "__original_operator_#{op}_complex" + alias_method original_operator_name, op + define_method op do |rhs| + if rhs.is_a? Complex + Complex.new(self).send(op, rhs) + else + send(original_operator_name, rhs) + end + end + end + + def __forward_operators_to_complex + __forward_operator_to_complex :+ + __forward_operator_to_complex :- + __forward_operator_to_complex :* + __forward_operator_to_complex :/ + + singleton_class.undef_method :__forward_operator_to_complex + singleton_class.undef_method :__forward_operators_to_complex + end +end + +class Fixnum + extend ForwardOperatorToComplex + __forward_operators_to_complex +end + +class Float + extend ForwardOperatorToComplex + __forward_operators_to_complex +end \ No newline at end of file diff --git a/mrbgems/mruby-complex/test/complex.rb b/mrbgems/mruby-complex/test/complex.rb new file mode 100644 index 000000000..890dd4ff1 --- /dev/null +++ b/mrbgems/mruby-complex/test/complex.rb @@ -0,0 +1,3 @@ +assert 'Complex' do + assert_equal Complex, 0i.class +end \ No newline at end of file diff --git a/mrbgems/mruby-rational/mrbgem.rake b/mrbgems/mruby-rational/mrbgem.rake new file mode 100644 index 000000000..4b540dec4 --- /dev/null +++ b/mrbgems/mruby-rational/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-rational') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' + spec.summary = 'Rational class' +end diff --git a/mrbgems/mruby-rational/mrblib/rational.rb b/mrbgems/mruby-rational/mrblib/rational.rb new file mode 100644 index 000000000..457c0488a --- /dev/null +++ b/mrbgems/mruby-rational/mrblib/rational.rb @@ -0,0 +1,46 @@ +class Rational < Numeric + def initialize(numerator = 0, denominator = 1) + @numerator = numerator + @denominator = denominator + end + + attr_reader :numerator, :denominator +end + +def Rational(numerator = 0, denominator = 1) + Rational.new(numerator, denominator) +end + +module ForwardOperatorToRational + def __forward_operator_to_rational(op, &b) + original_operator_name = "__original_operator_#{op}_rational" + alias_method original_operator_name, op + define_method op do |rhs| + if rhs.is_a? Rational + Rational.new(self).send(op, rhs) + else + send(original_operator_name, rhs) + end + end + end + + def __forward_operators_to_rational + __forward_operator_to_rational :+ + __forward_operator_to_rational :- + __forward_operator_to_rational :* + __forward_operator_to_rational :/ + + singleton_class.undef_method :__forward_operator_to_rational + singleton_class.undef_method :__forward_operators_to_rational + end +end + +class Fixnum + extend ForwardOperatorToRational + __forward_operators_to_rational +end + +class Float + extend ForwardOperatorToRational + __forward_operators_to_rational +end \ No newline at end of file diff --git a/mrbgems/mruby-rational/test/rational.rb b/mrbgems/mruby-rational/test/rational.rb new file mode 100644 index 000000000..6f20a6cd4 --- /dev/null +++ b/mrbgems/mruby-rational/test/rational.rb @@ -0,0 +1,3 @@ +assert 'Rational' do + assert_equal Rational, 0r.class +end \ No newline at end of file -- cgit v1.2.3 From a14ae801022291bfa7ffcc15d1acbc0fea3407a6 Mon Sep 17 00:00:00 2001 From: dearblue Date: Fri, 17 May 2019 22:04:25 +0900 Subject: (Proof of Concept) mruby tuning profiles [ci skip] Not only mruby, it is one of the difficult things to adjust the performance vs. memory efficiency of the software. If the approximate target device is divided and the adjustment value for it is prepared by default, it is a good indicator to do fine adjustment. This PR divides into four profiles. ***Caution: There is no basis for the definitions in the patch.*** - `MRB_CONSTRAINED_BASELINE_PROFILE` - for microprocessors. Reduce memory consumption. - `MRB_BASELINE_PROFILE` - Default value of mruby. - `MRB_MAIN_PROFILE` - For desktop computers. Assume that a huge amount of RAM, 10 MiB or more, is on board. - `MRB_HIGH_PROFILE` - for servers. Assume that mruby VM has a long life. As you can see the profile name has been ~~stolen~~ imitated from H.264. --- include/mrbconf.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'include/mrbconf.h') diff --git a/include/mrbconf.h b/include/mrbconf.h index ee816e322..f5e8858ce 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -159,4 +159,70 @@ # define TRUE 1 #endif +/* +** mruby tuning profiles +**/ + +/* A profile for micro controllers */ +#if defined(MRB_CONSTRAINED_BASELINE_PROFILE) +# ifndef KHASH_DEFAULT_SIZE +# define KHASH_DEFAULT_SIZE 16 +# endif + +# ifndef MRB_STR_BUF_MIN_SIZE +# define MRB_STR_BUF_MIN_SIZE 32 +# endif + +# ifndef MRB_HEAP_PAGE_SIZE +# define MRB_HEAP_PAGE_SIZE 256 +# endif + +/* A profile for default mruby */ +#elif defined(MRB_BASELINE_PROFILE) + +/* A profile for desktop computers or workstations; rich memory! */ +#elif defined(MRB_MAIN_PROFILE) +# ifndef MRB_METHOD_CACHE +# define MRB_METHOD_CACHE +# endif + +# ifndef MRB_METHOD_CACHE_SIZE +# define MRB_METHOD_CACHE_SIZE (1<<10) +# endif + +# ifndef MRB_METHOD_TABLE_INLINE +# define MRB_METHOD_TABLE_INLINE +# endif + +# ifndef MRB_IV_SEGMENT_SIZE +# define MRB_IV_SEGMENT_SIZE 32 +# endif + +# ifndef MRB_HEAP_PAGE_SIZE +# define MRB_HEAP_PAGE_SIZE 4096 +# endif + +/* A profile for server; mruby vm is long life */ +#elif defined(MRB_HIGH_PROFILE) +# ifndef MRB_METHOD_CACHE +# define MRB_METHOD_CACHE +# endif + +# ifndef MRB_METHOD_CACHE_SIZE +# define MRB_METHOD_CACHE_SIZE (1<<12) +# endif + +# ifndef MRB_METHOD_TABLE_INLINE +# define MRB_METHOD_TABLE_INLINE +# endif + +# ifndef MRB_IV_SEGMENT_SIZE +# define MRB_IV_SEGMENT_SIZE 64 +# endif + +# ifndef MRB_HEAP_PAGE_SIZE +# define MRB_HEAP_PAGE_SIZE 4096 +# endif +#endif + #endif /* MRUBYCONF_H */ -- cgit v1.2.3