45 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto d6556195ef boxing_nan.h, boxing_word.h: place newline before else
The mruby C style places `else` on its own line. Reformat the
remaining `} else {` / `} else if (...)` occurrences.

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-28 12:56:24 +09:00
dearblue 0d00743a5f Define the typedef for mrb_state earlier
This improves consistency with other definitions.
2026-04-19 21:17:18 +09:00
Aurora Nockert 646c37ecda Add a fast-path for mrb_type
By adding a fast-path where we ignore boxed types we can gain a pretty substantial speedup of mrb_iv_get, making it about 25% faster during a standard optcarrot benchmark run.

NOTE: It is just mrb_iv_get that is that much faster, the whole benchmark seems to be about 3-5% faster with word boxing.
2024-06-27 22:46:13 +02:00
dearblue 88b1654725 Allows handling of unaligned cptrs for MRB_NAN_BOXING
It seems to be preferable to be able to handle pointers of type `char` as well.
For this purpose, `mrb_nanbox_tt_inline` has been reorganized.

- `MRB_NANBOX_TT_POINTER` has been split into `MRB_NANBOX_TT_OBJECT` and `MRB_NANBOX_TT_CPTR`
- `MRB_NANBOX_TT_SYMBOL` has been merged into `MRB_NANBOX_TT_MISC`
2022-01-05 17:23:35 +09:00
Yukihiro "Matz" Matsumoto 0fbdd71dcf boxing_nan.h: remove false description in the comment [ci skip]
Ref #5597
2021-12-15 23:07:34 +09:00
Yukihiro "Matz" Matsumoto a5567fbad4 boxing_nan.h: update mrb_immediate_p and related macros. 2021-11-08 14:33:50 +09:00
Yukihiro "Matz" Matsumoto d6f6467ad7 boxing_nan.h (mrb_float_p): wrap macro argument by parenthesis. 2021-11-04 20:49:19 +09:00
dearblue 1de662cb8b Fix failed build with MRB_NAN_BOXING and enable_cxx_abi
```console
% c++ -xc++ -std=c++03 -S -Iinclude -DMRB_NAN_BOXING -DMRB_NO_PRESYM -o- src/array.c > /dev/null
In file included from src/array.c:7:
In file included from include/mruby.h:115:
In file included from include/mruby/value.h:201:
include/mruby/boxing_nan.h:95:12: error: cannot initialize return object of type 'enum mrb_vtype' with an rvalue of type 'int'
    return (enum mrb_vtype)(o.u >> 8) & 0x1f;
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```
2021-10-31 22:17:15 +09:00
Yukihiro "Matz" Matsumoto 9f0410789d boxing_nan.h: allow MRB_INT64 with MRB_NAN_BOXING.
Integers out of 32 bit range will be allocated in the heap.
2021-10-30 17:44:46 +09:00
Yukihiro "Matz" Matsumoto 85fcd2dc6f boxing_nan.h: implement Favor pointer NaN Boxing.
Favor pointer means encode NaN boxed values to keep pointer values
unmodified, to reduce the cost of far frequent pointer value retrievals.
2021-10-30 17:43:35 +09:00
Yukihiro "Matz" Matsumoto 0069e6738a boxing_nan.h: refactor NaN Boxing.
Preparation for:

* 64 bit integer with NaN Boxing
* Favor pointer NaN Boxing
2021-10-28 12:22:21 +09:00
Yukihiro "Matz" Matsumoto fca63c6060 boxing_nan.h: revert part of dccd66f
WIP code was mistakenly committed.
2021-10-26 17:33:49 +09:00
dearblue b774832ee1 Make mrb_static_assert() a variable argument
`mrb_static_assert()` extends the macro function to take one or two arguments.
If the argument is other than that, an error will occur.

References:
- static_assert のメッセージ省略を許可 - cpprefjp C++日本語リファレンス
  https://cpprefjp.github.io/lang/cpp17/extending_static_assert.html
- c - Overloading Macro on Number of Arguments - Stack Overflow
  https://stackoverflow.com/a/11763277
2021-10-24 23:11:52 +09:00
Yukihiro "Matz" Matsumoto dccd66f9ef Support Ruby3.0 keyword arguments.
The Difference

Since Ruby1.9, the keyword arguments were emulated by Ruby using the hash
object at the bottom of the arguments. But we have gradually moved toward
keyword arguments separated from normal (positinal) arguments.

At the same time, we value compatibility, so that Ruby3.0 keyword
arguments are somewhat compromise. Basically, keyword arguments are
separated from positional arguments, except when the method does not
take any formal keyword arguments, given keyword arguments (packed
in the hash object) are considered as the last argument.

And we also allow non symbol keys in the keyword arguments. In that
case, those keys are just passed in the `**` hash (or raise
`ArgumentError` for unknown keys).

The Instruction Changes

We have changed `OP_SEND` instruction. `OP_SEND` instruction used to
take 3 operands, the register, the symbol, the number of (positional)
arguments. The meaning of the third operand has been changed. It is now
considered as `n|(nk<<4)`, where `n` is the number of positional
arguments, and `nk` is the number of keyword arguments, both occupies
4 bits in the operand.

The number `15` in both `n` and `nk` means variable sized arguments are
packed in the object. Positional arguments will be packed in the array,
and keyword arguments will be packed in the hash object. That means
arguments more than 14 values are always packed in the object.

Arguments information for other instructions (`OP_SENDB` and `OP_SUPER`)
are also changed. It works as the third operand of `OP_SEND`. the
difference between `OP_SEND` and `OP_SENDB` is just trivial. It assigns
`nil` to the block hidden arguments (right after arguments).

The instruction `OP_SENDV` and `OP_SENDVB` are removed. Those
instructions are replaced by `OP_SEND` and `OP_SENDB` respectively with
the `15` (variable sized) argument information.

Calling Convention

When calling a method, the stack elements shall be in the order of the
receiver of the method, positional arguments, keyword arguments and the
block argument. If the number of positional or keyword arugument (`n` or
`nk`) is zero, corresponding arguments will be empty. So when `n=0` and
`nk=0` the stack layout (from bottom to top) will be:

+-----------------------+
| recv | block (or nil) |
+-----------------------+

The last elements `block` should be explicitly filled before `OP_SEND`
or assigned to `nil` by `OP_SENDB` internally. In other words, the
following have exactly same behavior:

OP_SENDB clears `block` implicitly:

```
OP_SENDB reg sym 0
```

OP_SEND clears `block` implicitly:

```
OP_LOADNIL  R2
OP_SEND     R2 sym 0
```

When calling a method with only positional arguments (n=0..14) without
keyword arguments, the stack layout will be like following:

+--------------------------------------------+
| recv | arg1 | ... | arg_n | block (or nil) |
+--------------------------------------------+

When calling a method with arguments packed in the array (n=15) which
means argument splat (*) is used in the actual arguments, or more than
14 arguments are passed the stack layout will be like following:

+-------------------------------+
| recv | array | block (or nil) |
+-------------------------------+

The number of the actual arguments is determined by the length of the
argument array.

When keyword arguments are given (nk>0), keyword arguments are passed
between positional arguments and the block argument. For example, when
we pass one positional argument `1` and one keyword argument `a: 2`,
the stack layout will be like:

+------------------------------------+
| recv | 1 | :a | 2 | block (or nil) |
+------------------------------------+

Note that keyword arguments consume `2*nk` elements in the stack when
`nk=0..14` (unpacked).

When calling a method with keyword arguments packed in the hash object
(nk=15) which means keyword argument splat (**) is used or more than
14 keyword arguments in the actual arguments, the stack layout will
be like:

+------------------------------+
| recv | hash | block (or nil) |
+------------------------------+

Note for mruby/c

When mruby/c authors try to support new keyword arguments, they need
to handle the new meaning of the argument information operand. If they
choose not to support keyword arguments in mruby/c, it just raise
error when `nk` (taken by `(c>>4)&0xf`) is not zero. And combine
`OP_SENDV` behavior with `OP_SEND` when `n` is `15`.

If they want to support keyword arguments seriously, contact me at
<matz@ruby.or.jp> or `@yukihiro_matz`. I can help you.
2021-10-12 20:16:36 +09:00
Yukihiro "Matz" Matsumoto ba6e121537 boxing_nan.h: rename prefix BOXNAN to NANBOX. 2021-08-26 16:06:02 +09:00
dearblue 3a92a4e3d8 Avoid warnings from clang's "-Wembedded-directive"
If it gives clang-11.0 `-Wembedded-directive`, a warning will be reported in `include/mruby/boxing_nan.h`.

```
include/mruby/boxing_nan.h:48:2: warning: embedding a directive within macro arguments has undefined behavior [-Wembedded-directive]
include/mruby/boxing_nan.h:52:2: warning: embedding a directive within macro arguments has undefined behavior [-Wembedded-directive]
```

The cause of this is #5117.
ref. e993b83c50
2020-11-23 21:22:50 +09:00
Yukihiro "Matz" Matsumoto 9fbf0ef886 Refactoring integer ranges.
- Remove `mrb_ssize`
- Fix `MRB_FIXNUM_{MIN,MAX}` to 32 bits on `MRB_NAN_BOXING`
2020-11-17 12:41:10 +09:00
Yukihiro "Matz" Matsumoto 352c4755b2 Merge pull request #5118 from dearblue/mrb_value
Define `mrb_value` in struct for Word/NaN boxing
2020-11-10 11:50:59 +09:00
dearblue e993b83c50 Fixed build NaN boxing with 32-bit CPU mode
`SET_CPTR_VALUE()` requires the `p` field on 32-bit CPU mode.
2020-11-08 14:15:19 +09:00
dearblue 926ec1dc7e Define mrb_value in struct for Word/NaN boxing
This is to make it possible to distinguish between `mrb_value` and `mrb_sym` in C++ "Function overloading" and C11 "Generic selection".
2020-11-08 14:14:00 +09:00
Yukihiro "Matz" Matsumoto 9d613159b5 Add mrb_integer() definition for MRB_NAN_BOXING. 2020-10-12 18:20:04 +09:00
Yukihiro "Matz" Matsumoto 2b188ed8a1 Reorganize Integer system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
2020-10-12 18:19:54 +09:00
Yukihiro "Matz" Matsumoto 55163a8a0a Rename MRB_TT_FIXNUM to MRB_TT_INTEGER.
We still have `#define MRB_TT_FIXNUM MRB_TT_INTEGER` for compatibility.
2020-10-12 16:21:47 +09:00
Yukihiro "Matz" Matsumoto 8a87549315 Rename float configuration option names.
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT`
- `MRB_USE_FLOAT` => `MRB_USE_FLOAT32`

The former is to use `USE_XXX` naming convention. The latter is to make
sure `float` is 32bit float and not floating point number in general.
2020-10-12 16:21:40 +09:00
Yukihiro "Matz" Matsumoto 763b39d00b Simplify NaN boxing definitions.
Remove `#ifdef` from `union mrb_value_`.
2020-10-12 16:21:35 +09:00
Yukihiro "Matz" Matsumoto 4e56e0defc Rename union mrb_value to union mrb_value_.
Since some compiler complains when we `typedef` `mrb_value`.
2020-10-12 16:21:30 +09:00
Yukihiro "Matz" Matsumoto 73e289c54e Simplify mrb_value structure for MRB_NAN_BOXING. 2020-10-12 16:21:29 +09:00
Yukihiro "Matz" Matsumoto 425b142168 Refine MRB_NAN_BOXING on 32bit architecture.
You don't need pointer tweaking on 32bit architecture, where pointers
fit in 32bit (lower half of mrb_value).
2020-10-12 16:21:27 +09:00
Yukihiro "Matz" Matsumoto 5e9dc72bc5 Pack mrb_value into uint64_t when MRB_NAN_BOXING. 2020-10-12 16:21:25 +09:00
Yukihiro "Matz" Matsumoto d428fa0c4a Replace entire irep->pool.
Changes:
- `pool format is completely replaced
  - supported types: `STR`, `INT32`, `INT64`, `FLOAT`
  - `FLOAT` may be replaced by binary representation in the future
- insert `NUL` after string literals in `mrb` files
- `irep->pool` no longer store values in `mrb_value`
- instead it stores in `mrb_pool_value`
- less allocation
- `mrb_irep` can be stored in ROM
2020-10-12 16:21:08 +09:00
dearblue d0501c69b0 Fix MRB_TT_CPTR object with MRB_NAN_BOXING
Previously, if `MRB_NAN_BOXING` is defined, for example,
`mrb_cptr_value()` could not keep an odd address.

If it is `MRB_32BIT`, it can be embedded in `NaN` as it is.

If it is `MRB_64BIT`, some operations are shared with `MRB_WORD_BOXING`.
In this case, the MRB_API function `mrb_nan_boxing_cptr_value()` is
defined.
2020-04-13 23:12:09 +09:00
KOBAYASHI Shuji a53f28275e Fix MRB_FIXNUM_SHIFT with MRB_WORD_BOXING, MRB_INT32 and MRB_64BIT
### Example

  ```ruby
  # example.rb
  max32 = 2**30 - 1 + 2**30
  min32 = -max32-1
  [max32, max32+1, min32, min32-1].each{|n| p [n, n.class]}
  ```

#### Before this patch:

  ```
  $ bin/mruby example.rb
  [2147483647, Float]
  [2147483648, Float]
  [-2147483648, Float]
  [-2147483649, Float]
  ```

#### After this patch:

  ```
  $ bin/mruby example.rb
  [2147483647, Fixnum]
  [2147483648, Float]
  [-2147483648, Fixnum]
  [-2147483649, Float]
  ```
2019-12-01 22:09:38 +09:00
KOBAYASHI Shuji 19e2cc187b Avoid bit fields in mrb_value; ref b2c3d88f
The changes at b2c3d88f were inappropriate because the memory layout of bit
fields are implementation defined. Therefor, I fixed it not to use bit
fields.
2019-09-06 13:24:35 +09:00
Yukihiro "Matz" Matsumoto 1a4ea94bc7 Fix mrb_immediate_p(MRB_TT_FREE) to return false; ref #4679 2019-08-31 23:19:08 +09:00
David Siaw b5299b1c58 fix up documentation for values 2019-08-18 20:12:44 +09:00
dearblue 14133f4057 Fix macro arguments with paren 2018-12-18 23:05:36 +09:00
YAMAMOTO Masaya acdc2d1f24 Add MRB_WITHOUT_FLOAT 2017-10-11 17:58:11 +09:00
Yukihiro "Matz" Matsumoto 77331d127b Unify else clause style 2017-04-03 16:56:27 +09:00
Tomasz Dąbrowski 626648dc41 Fixed NaN boxing when MRB_INT16 is set 2016-11-26 13:05:02 +01:00
Tomasz Dąbrowski 1af9e363f2 Fixes for compiling mruby as C++ 2016-11-24 09:28:00 +09:00
Tomasz Dąbrowski 8cdaf1ed35 Fixed NaN boxing for 64-bit configrations on platforms that use full 48-bit usermode pointers
Definition of boxed pointer is following:
`111111111111TTTT TTPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP`

Previously, only the last 32-bit were set, and remaining 14 bits were zeroed when setting `ttt`.
2016-11-21 12:37:45 +01:00
cremno 4c868a9562 remove SET_PROC_VALUE() macro
It isn't used anymore since the #2791 merge. It's also unlikely to be
needed again as it wasn't before (its usage was unnecessary).
2015-05-17 22:25:55 +02:00
furunkel 99a00cf417 Avoid unnecessary stores and reloads 2015-05-12 13:46:56 +02:00
Yukihiro "Matz" Matsumoto 0cf86dea0e rename SET_FIXNUM_VALUE to SET_INT_VALUE since it sets mrb_int value 2014-07-09 06:40:19 +09:00
David Turnbull 249f05e7d7 Clean up value.h and mrb_value boxing 2014-07-09 06:32:11 +09:00