Commit Graph

925 Commits

Author SHA1 Message Date
Hiroshi Mimaki 0589749952 mruby 3.0.0-preview. 2020-10-16 10:43:03 +09:00
Yukihiro "Matz" Matsumoto 5069fb15e4 Cause error explicitly from MRB_TRY() with cxx_exception; ref #5088
`MRB_TRY()` does not work when compiled by C compiler with `cxx_exception`
configuration. We should explicitly warn.
2020-10-12 18:20:29 +09:00
Yukihiro "Matz" Matsumoto 896338e219 Unify mrb_str_to_str to mrb_obj_as_string.
Redirect `mrb_str_to_str` to `mrb_obj_as_string` via C macro.
Inspired by #5082
2020-10-12 18:20:26 +09:00
Yukihiro "Matz" Matsumoto f1d02bfc15 Clarify the meaning of MRB_IREP_STATIC; ref #5084 2020-10-12 18:20:26 +09:00
Yukihiro "Matz" Matsumoto c1f5fdafb6 Remove duplicated constant IREP_TT_SFLAG; #5084 2020-10-12 18:20:25 +09:00
Yukihiro "Matz" Matsumoto 3ff2757bf7 Fix mrb_int_mul_overflow() to check either operand being zero. 2020-10-12 18:20:22 +09:00
Yukihiro "Matz" Matsumoto 9b8e5658ce Fix integer casting on 64 bit platforms.
On platforms where `sizeof(long)` is 4, casting `(long)` can lose data
or sign information.
2020-10-12 18:20:18 +09:00
Yukihiro "Matz" Matsumoto 471479e723 Change float representation in mrb binary files.
From human readable (ASCII) string representation to binary dump of
IEEE754 in little endian.
2020-10-12 18:20:06 +09:00
Yukihiro "Matz" Matsumoto 397b005715 Replace the implementation of method tables in classes/modules.
They are basically the copy of instance variable tables. On my Linux
box, memory consumption of `mrbtest` measured by `valgrind` is:

- old: 17,683,830 bytes
- new: 14,283,749 bytes
2020-10-12 18:20:06 +09:00
Yukihiro "Matz" Matsumoto 7e3d22f095 Raname mrb_exc_new_str_lit() to mrb_exc_new_lit().
It uses `mrb_str_new_lit()` internally, but it doesn't need to express
it in the name of the function (macro).
2020-10-12 18:20:06 +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 5a7cbebc8a Remove MRB_NO_FLOAT_INLINE and MRB_WBOX_FLOAT_INLINE configuration.
They are not used from the beginning.
2020-10-12 16:21:49 +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
dearblue 80fe9838d2 Integrate Fixnum class into Integer class
* The `Fixnum` constant is now an alias for the `Integer` class.
* Remove `struct mrb_state::fixnum_class` member.
  If necessary, use `struct mrb_state::integer_class` instead.
2020-10-12 16:21:44 +09:00
Yukihiro "Matz" Matsumoto 424afa4446 Make Proc#parameters to support keyword arguments; fix #5066
TODO: Unlike CRuby, mruby's `Proc#parameters` does not distinguish
required keyword arguments and optional keyword arguments currently.
2020-10-12 16:21:42 +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 72d071540c Rename MRB_METHOD_T_STRUCT to MRB_USE_METHOD_T_STRUCT.
It's the first change of renaming configuration options to `MRB_XXX` to
`MRB_USE_XXX` or `MRB_NO_XXX`.
2020-10-12 16:21:38 +09:00
Yukihiro "Matz" Matsumoto 8813ebec07 Skip array embedding if MRB_NO_BOXING and MRB_32BIT; fix #4382
On some platforms, `sizeof(mrb_value) > sizeof(void*)*3`, which makes
`MRB_ARY_EMBED_LEN_MAX` zero. And zero sized array cause compile errors.
2020-10-12 16:21:36 +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 107c777341 Rename OP_JUW instruction to OP_JMPUW. 2020-10-12 16:21:35 +09:00
Yukihiro "Matz" Matsumoto 500f721f70 Fix typo _hander -> _handler. 2020-10-12 16:21:34 +09:00
dearblue c1f112c49a Replace global jump with catch handler implementation
When a global jump occurs, look at the catch handler table to determine where to jump.
In that case, `pc` already shows the following instruction, but since the table shows `begin_offset ... end_offset`, the comparison is done with `begin_offset < pc && pc <= end_offset`.
If there is a corresponding handler, move `pc` to `handler.target_offset` and continue running the VM.

When a global jump across `ensure` is made by `return`, `break`, `next`, `redo` and `retry`, the extended `RBreak` object saves and restores the C-level execution position.
This extended `RBreak` can have tag information, which makes it a pseudo coroutine (the "tag" mimics CRuby).

The implementation of pseudo coroutines by `RBreak` is summarized by `CHECKPOINT_RESTORE ... CHECKPOINT_MAIN ... CHECKPOINT_END` and `throw_tagged_break` / `unwind_ensure` macros.
The restart of processing is branched by `RBREAK_TAG_FOREACH(DISPATCH_CHECKPOINTS)`.

- Not only `rescue` blocks but also `ensure` blocks are now sandwiched between `OP_EXCEPT` and `OP_RAISEIF`.

- Remove the function `ecall()`.
  It is no longer necessary to re-enter the VM to perform an "ensure block".

  This will resolves #1888.

- Added instruction `OP_JUW` (Jump while UnWind).

  It jumps unconditionally like `OP_JMP`, but searches the catch handler table and executes the ensure block.
  Since it searches the catch handler table, it is much heavier than `OP_JMP`.
2020-10-12 16:21:33 +09:00
dearblue 0d7b4deccf Removed push/pop instructions for rescue/ensure
`OP_PUSHERR`, `OP_POPERR`, `OP_EPUSH` and `OP_EPOP` are removed.
2020-10-12 16:21:32 +09:00
dearblue f467b02d45 Extended OP_EXCEPT and OP_RAISE (OP_RAISEIF) instructions
- `OP_EXCEPT` checks if `mrb->exc` is `NULL`, `MRB_TT_EXCEPTION` or
  `MRB_TT_BREAK`.
  If `mrb->exc` is `NULL`, it will be replaced with `nil`.

- If `OP_RAISE` is `nil`, it does nothing and the immediately
  following instruction is executed (like `OP_NOP`).
  Also, in case of `RBreak` object, it moves to the processing for
  `break`.
  With this change, the instruction name is changed from
  `OP_RAISE` to `OP_RAISEIF`.
2020-10-12 16:21:32 +09:00
dearblue a54a3df32c Extended mruby binary format
The catch handler table is combined with iseq block.
This is to prevent the structure from growing by adding a field for the
catch handler table to the `mrb_irep` structure.

"iseq block" and "catch handler table":
  [number of catch handler table (2 bytes)]
  [number of byte code (4 bytes)]
  [iseq (any bytes)]
  [catch handlers (multiple of 7 bytes)]

catch handler:
  [catch type (1 byte)]
  [begin offset (2 bytes)]
  [end offset (2 bytes)]
  [target offset (2 bytes)]

catch type: enum mrb_catch_type (0 = rescue, 1 = ensure)
begin offset: Includes the specified instruction address
end offset: Does not include the specified instruction address
target offset: replaces pc with the specified instruction address

This table is not expanded by `read_irep_record_1()`.
The necessary elements are expanded one by one when used.
2020-10-12 16:21:32 +09:00
Yukihiro "Matz" Matsumoto 6fbc03b3a8 Revert 4c001673b
Probably I misunderstand strict aliasing rule of C++. The fix in
4c001673b was other way around.
2020-10-12 16:21:30 +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 53e46be7e2 Simplify mrb_value structure for MRB_WORD_BOXING. 2020-10-12 16:21:29 +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 3d8a38bea4 You don't need to keep index in local variables info in irep. 2020-10-12 16:21:22 +09:00
Yukihiro "Matz" Matsumoto 639946a006 Enable method cache by default.
Introduced `MRB_NO_METHOD_CACHE` which is inverse of `MRB_METHOD_CACHE`
that should be enabled intestinally. In addition, the default cache is
made bigger (128 -> 256).
2020-10-12 16:21:22 +09:00
Yukihiro "Matz" Matsumoto 6db0162ec7 Upgrade RITE_VM_VERSION to 0300 (means mruby 3.0). 2020-10-12 16:21:20 +09:00
Yukihiro "Matz" Matsumoto dec34d6c7b Split MRB_BINARY_FORMAT to major and minor.
The minor versions should be upper compatible. So mere opcode, section
addition can be done without breaking compiled binary.
2020-10-12 16:21:20 +09:00
Yukihiro "Matz" Matsumoto 8f0ac27196 Update opcode reference and comment.
- no OP_EXT_ anymore
- OP_LOADI16 in right position
2020-10-12 16:21:19 +09:00
Yukihiro "Matz" Matsumoto fd10c72319 Remove OP_EXT[123] from operands. 2020-10-12 16:21:19 +09:00
Yukihiro "Matz" Matsumoto ce30a4e04b Add const to irep structure to place data on ROM. 2020-10-12 16:21:16 +09:00
Yukihiro "Matz" Matsumoto 9debf63340 Specify the size of struct RStringEmbed array part. 2020-10-12 16:21:13 +09:00
Yukihiro "Matz" Matsumoto ee111dd175 Clarify the use of MRB_64BIT and MRB_INT64 in dump.c and load.c.
- `MRB_64BIT`: the size of a pointer is 64 bits
- `MRB_INT64`: the size of `mrb_int` is 64 bits
2020-10-12 16:21:11 +09:00
Yukihiro "Matz" Matsumoto 52507b1083 Generate C struct from irep instead of binary dump. 2020-10-12 16:21:10 +09:00
Yukihiro "Matz" Matsumoto 7deb41b0e8 Generate C source file to represent mrb_irep structures.
Type `mrbc -S -B<init> -o<outfile> <rbfiles...>` to generate the C
source code that holds compiled `mrb_irep`.

Appending the following code to the bottom of the generated code,
`mruby` executes the compiled code:

```C
int
main()
{
  mrb_state *mrb = mrb_open();
  struct RProc *p = mrb_proc_new(mrb, &init_irep);
  mrb_vm_run(mrb, p, mrb_top_self(mrb), 0);
  mrb_close(mrb);
  return 0;
}
```

Eventually static compile should use this representation, instead
of `uint8_t` array that holds `mrb` data, so that we can skip
interpreting `mrb` data.
2020-10-12 16:21:09 +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
Yukihiro "Matz" Matsumoto daa37be549 Rename struct mrb_locals to struct mrb_lvinfo.
That stands for "local variable information".
2020-10-12 16:21:08 +09:00
Yukihiro "Matz" Matsumoto ad15e59cd9 Add irep C struct dump from mrbc with -S option.
But we need more work:
- recursive `irep` dump (`irep->reps`)
- pool values dump (`irep->pool`)
2020-10-12 16:21:07 +09:00
Yukihiro "Matz" Matsumoto 7ab7cadbbb Stringify non C identifier symbols to stop macro errors by old gcc. 2020-10-12 16:21:04 +09:00
Yukihiro "Matz" Matsumoto 5a3e014e49 Constify irep members.
- `pool`
- `syms`
- `reps`
2020-10-12 16:21:03 +09:00
Yukihiro "Matz" Matsumoto fa6a9f6f13 Add functions that take symbols as arguments.
- :
2020-10-12 16:20:58 +09:00
Yukihiro "Matz" Matsumoto 246c76e261 Rename MRB_OPSYM() to MRB_QSYM().
Where `QSYM` means quoted symbols, which cannot be represented C
symbols, so specify aliases instead.

- operators: name of the operation, e.g. add for `+`
- predicates: add `_p` suffix instead of `?`
- bang methods: add `_b` suffix instead of `!`
- instance variables: add `a_` prefix instead of `@`
- global variables: add `d_` prefix instead of `@`
- class variables: unsupported; don't use them
2020-10-12 16:20:53 +09:00