Граф коммитов

1031 Коммитов

Автор SHA1 Сообщение Дата
Koichi Sasada 46acd0075d support builtin features with Ruby and C.
Support loading builtin features written in Ruby, which implement
with C builtin functions.
[Feature #16254]

Several features:

(1) Load .rb file at boottime with native binary.

Now, prelude.rb is loaded at boottime. However, this file is contained
into the interpreter as a text format and we need to compile it.
This patch contains a feature to load from binary format.

(2) __builtin_func() in Ruby call func() written in C.

In Ruby file, we can write `__builtin_func()` like method call.
However this is not a method call, but special syntax to call
a function `func()` written in C. C functions should be defined
in a file (same compile unit) which load this .rb file.

Functions (`func` in above example) should be defined with
  (a) 1st parameter: rb_execution_context_t *ec
  (b) rest parameters (0 to 15).
  (c) VALUE return type.
This is very similar requirements for functions used by
rb_define_method(), however `rb_execution_context_t *ec`
is new requirement.

(3) automatic C code generation from .rb files.

tool/mk_builtin_loader.rb creates a C code to load .rb files
needed by miniruby and ruby command. This script is run by
BASERUBY, so *.rb should be written in BASERUBY compatbile
syntax. This script load a .rb file and find all of __builtin_
prefix method calls, and generate a part of C code to export
functions.

tool/mk_builtin_binary.rb creates a C code which contains
binary compiled Ruby files needed by ruby command.
2019-11-08 09:09:29 +09:00
Lourens Naudé 65744fb19e Right size the iseq coverage branches tmp array - initializes with 5 elements 2019-10-29 11:30:49 +09:00
Aaron Patterson 339a891c7d
Pin keys of this st_table 2019-10-28 11:47:09 -07:00
Koichi Sasada 58b363bf0d respect `param.flags.ruby2_keywords` at to_binary.
`param.flags.ruby2_keywords` is not store/load correctly at to_binary
so restore this flag correctly.
2019-10-25 04:39:22 +09:00
Nobuyoshi Nakada b609bdeb53
Define arguments forwarding as `ruby2_keywords` style
Get rid of these redundant and useless warnings.

```
$ ruby -e 'def bar(a) a; end; def foo(...) bar(...) end; foo({})'
-e:1: warning: The last argument is used as the keyword parameter
-e:1: warning: for `foo' defined here
-e:1: warning: The keyword argument is passed as the last hash parameter
-e:1: warning: for `bar' defined here
```
2019-10-25 01:16:05 +09:00
Alan Wu f1de438380 Use CPDEBUG for debug code 2019-10-24 18:03:42 +09:00
Alan Wu 89e7997622 Combine call info and cache to speed up method invocation
To perform a regular method call, the VM needs two structs,
`rb_call_info` and `rb_call_cache`. At the moment, we allocate these two
structures in separate buffers. In the worst case, the CPU needs to read
4 cache lines to complete a method call. Putting the two structures
together reduces the maximum number of cache line reads to 2.

Combining the structures also saves 8 bytes per call site as the current
layout uses separate two pointers for the call info and the call cache.
This saves about 2 MiB on Discourse.

This change improves the Optcarrot benchmark at least 3%. For more
details, see attached bugs.ruby-lang.org ticket.

Complications:
 - A new instruction attribute `comptime_sp_inc` is introduced to
 calculate SP increase at compile time without using call caches. At
 compile time, a `TS_CALLDATA` operand points to a call info struct, but
 at runtime, the same operand points to a call data struct. Instruction
 that explicitly define `sp_inc` also need to define `comptime_sp_inc`.
 - MJIT code for copying call cache becomes slightly more complicated.
 - This changes the bytecode format, which might break existing tools.

[Misc #16258]
2019-10-24 18:03:42 +09:00
Nobuyoshi Nakada afab8122c3
Fix the exception when CPDEBUG 2019-10-23 02:04:36 +09:00
Alan Wu 9c553139ae Fix build for CPDEBUG=1
The declarations went out-of-sync in dcfb7f6.
2019-10-22 12:21:02 +09:00
Lourens Naudé 0ca4f74967 Right size the numtable in insn_make_insn_table to VM_INSTRUCTION_SIZE 2019-10-11 11:15:43 +09:00
卜部昌平 7e0ae1698d avoid overflow in integer multiplication
This changeset basically replaces `ruby_xmalloc(x * y)` into
`ruby_xmalloc2(x, y)`.  Some convenient functions are also
provided for instance `rb_xmalloc_mul_add(x, y, z)` which allocates
x * y + z byes.
2019-10-09 12:12:28 +09:00
Yusuke Endoh b43afa0a8f Make parser_params have parent_iseq instead of base_block
The parser needs to determine whether a local varaiable is defined or
not in outer scope.  For the sake, "base_block" field has kept the outer
block.

However, the whole block was actually unneeded; the parser used only
base_block->iseq.

So, this change lets parser_params have the iseq directly, instead of
the whole block.
2019-10-04 02:30:36 +09:00
Alan Wu 99d3043bd8 Iseq#to_binary: dump flag for **nil (#2508)
RUBY_ISEQ_DUMP_DEBUG=to_binary and the attached test case was failing.
Dump the flag to make sure `**nil` can round-trip properly.
2019-10-02 16:05:40 +09:00
Nobuyoshi Nakada 79d5332a2d
Drop eliminated catch-entries
Drop catch table entries used in eliminated block, as well as
call_infos.  [Bug #16184]
2019-09-27 21:12:27 +09:00
Nobuyoshi Nakada 0c6f36668a
Adjusted spaces [ci skip] 2019-09-27 10:20:56 +09:00
Aaron Patterson 4808afb360
Replace `freeze_string` with `rb_fstring` 2019-09-26 13:56:42 -07:00
Aaron Patterson 0846d48853
Remove `iseq_add_mark_object_compile_time`
This function is just a synonym for RB_OBJ_WRITTEN, so we can just
directly call that.
2019-09-26 13:56:42 -07:00
Aaron Patterson e197d9ca71
Execute write barrier instead of adding to array
We can mark everything via the instruction objects, so just execute the
write barrier instead of appending to the array
2019-09-26 13:56:41 -07:00
Aaron Patterson 98d7583bfc
Pull `iseq_add_mark_object_compile_time` out of `freeze_string`
`freeze_string` essentially called iseq_add_mark_object_compile_time.  I
need to know where all writes occur on the `rb_iseq_t`, so this commit
separates the function calls so we can add write barriers in the right
place.
2019-09-26 13:56:41 -07:00
Aaron Patterson f639e04699
Pull "mark object" up
Move the "add mark object" function to the location where we should be
calling RB_OBJ_WRITTEN.  I'm going to add verification code next so we
can make sure the objects we're adding to the array are also reachable
from the mark function.
2019-09-26 13:56:41 -07:00
Aaron Patterson 50fadefb7e
Scan the ISEQ arena for markables and mark them
This commit scans the ISEQ arena for objects that can be marked and
marks them.  This should make the mark array unnecessary.
2019-09-26 13:56:41 -07:00
Aaron Patterson a618d64086
Allocate `INSN *` out of a separate arena 2019-09-26 13:56:41 -07:00
Aaron Patterson 3cd8f76f7f
Introduce a secondary arena
We'll scan the secondary arena during GC mark. So, we should only
allocate "markable" instruction linked list nodes out of the secondary
arena.
2019-09-26 13:56:41 -07:00
Aaron Patterson 451776f13d
Pass in arena to allocator
This is so we can configure a new arena later
2019-09-26 13:56:41 -07:00
Nobuyoshi Nakada e81a3e6df5
Allows calling a private method only with bare `self` 2019-09-20 22:05:54 +09:00
Nobuyoshi Nakada e6378cdcd8
Allow calling a private accessor with `self.`
[Feature #11297] [Feature #16123]
2019-09-20 02:21:37 +09:00
Dylan Thacker-Smith 7fbd2f7cc2
Allow calling a private method with `self.`
This makes it consistent with calling private attribute assignment
methods, which currently is allowed (e.g. `self.value =`).

Calling a private method in this way can be useful when trying to
assign the return value to a local variable with the same name.

[Feature #11297] [Feature #16123]
2019-09-20 02:20:59 +09:00
Nobuyoshi Nakada e13b09c450
Use EXPECT_NODE_NONULL 2019-09-19 23:45:09 +09:00
Nobuyoshi Nakada 82f25404ff
Check COMPILE_RECV result 2019-09-19 23:44:37 +09:00
NagayamaRyoga 20baa08d65 Improve the output of `RubyVM::InstructionSequence#to_binary` (#2450)
The output of RubyVM::InstructionSequence#to_binary is extremely large.
We have reduced the output of #to_binary by more than 70%.

The execution speed of RubyVM::InstructionSequence.load_from_binary is about 7% slower, but when reading a binary from a file, it may be faster than the master.

Since Bootsnap gem uses #to_binary, this proposal reduces the compilation cache size of Rails projects to about 1/4.

See details: [Feature #16163]
2019-09-19 17:35:32 +09:00
Koichi Sasada 2da6b328bb introduce IBF_(MAJOR|MINOR)_VERSION.
RubyVM::InstructionSequence.to_binary generates a bytecode binary
representation. To check compatibility with binary and loading
MRI we prepared major/minor version and compare them at loading
time. However, development version of MRI can change this format
but we can not increment minor version to make them consistent
with Ruby's major/minor versions.

To solve this issue, we introduce new minor version scheme
(binary's minor_version = ruby's minor * 10000 + dev ver)
and we can check incompatibility with older dev version.
2019-09-13 16:24:28 +09:00
Kazuhiro NISHIYAMA 0691a748b6
Fix a typo [ci skip] 2019-09-09 20:06:00 +09:00
Yusuke Endoh 46bfe907f1 compile.c (compile_hash): rewrite keyword splat handling
and add some comments.
(I confirm that `foo(**{})` allocates no hash object.)
2019-09-08 03:17:04 +09:00
Yusuke Endoh 95297a15f1 compile.c (compile_hash): rewrite the compilation algorithm
This is a similar refactoring to 8c908c9890,
but the target is compile_hash.
2019-09-08 03:17:04 +09:00
Yusuke Endoh 4f63634af1 compile.c (NODE_OP_ASGN1): Remove unneeded DECL_ANCHOR 2019-09-08 01:22:26 +09:00
Yusuke Endoh 95f9d7c76d compile.c (keyword_node_p): Refactor out keyword node checks 2019-09-08 00:28:03 +09:00
Yusuke Endoh 86b74d1a73 compile.c (compile_hash): Remove redundant check for NODE_ZLIST
NODE_ZLIST case is handled in compile_hash, so iseq_compile_each0
doesn't have to do the same check redundantly.
2019-09-08 00:28:03 +09:00
Yusuke Endoh 050f67c9c6 compile.c (compile_hash): Simplify the keyword handling
The length of NODE_LIST chain in NODE_HASH is always even because it
represents key-value pairs.  There is no need to check for the
odd-length case.
2019-09-08 00:28:03 +09:00
Yusuke Endoh bb78c83678 compile.c (compile_hash): don't add a temporal array to mark_ary
The array is just for a temporal buffer to create a hash, not stored in
the final iseq.
2019-09-08 00:28:03 +09:00
Yusuke Endoh 7cba9a8406 compile.c (compile_array): undef a temporal macro 2019-09-08 00:28:02 +09:00
git a9b63db3b6 * remove trailing spaces. [ci skip] 2019-09-07 22:22:30 +09:00
Yusuke Endoh 8c908c9890 compile.c (compile_array): rewrite the compilation algorithm
The original code looks unnecessarily complicated (to me).
Also, it creates a pre-allocated array only for the prefix of the array.

The new code optimizes not only the prefix but also the subsequence that
is longer than 0x40 elements.

    # not optimized
    10000000.times { [1+1, 1,2,3,4,...,63]    } # 2.12 sec.
    # (1+1; push 1; push 2; ...; puts 63; newarray 64; concatarray)

    # optimized
    10000000.times { [1+1, 1,2,3,4,...,63,64] } # 1.46 sec.
    # (1+1; newarray 1; putobject [1,2,3,...,64]; concatarray)
2019-09-07 22:08:39 +09:00
Yusuke Endoh 07876bf6db compile.c (compile_hash): refactoring
The same refactoring as to b601b13c7267889bf394146353c5f2b0eb488278.
2019-09-07 20:25:12 +09:00
Yusuke Endoh 187328b703 compile.c (compile_array): refactoring
"popped" case can be so simple, so this change moves the branch to the
first, instead of scattering `if (popped)` branches to the main part.
Also, the return value "len" is not used.  So it returns just 0 or 1.
2019-09-07 20:25:12 +09:00
Yusuke Endoh a2260bd636 compile.c: Separate compile_list to two functions for Array and Hash
compile_list was for the compilation of Array literal and Hash literal.
I guess it was originally reasonable to handle them in one function, but
now, compilation of Array is very different from Hash.  So the function
was complicated by many branches for Array and Hash.

This change separates the function to two ones for Array and Hash.
2019-09-07 16:45:49 +09:00
Yusuke Endoh 2f2f8107d0 compile.c (compile_list): allow an odd-length hidden array literal
An array literal [1,2,...,301] was compiled to the following iseq:

  duparray [1,2,...,300]
  putobject [301]
  concatarray

The Array literal optimization took every two elements maybe because it
must handle not only Array but also Hash.
Now the optimization takes each element if it is an Array literal.  So
the new iseq is: duparray [1,2,...,301].
2019-09-07 16:26:38 +09:00
Yusuke Endoh 1e008105bc compile.c (compile_list): emit newarraykwsplat only at the last chunk
`[{}, {}, {}, ..., {}, *{}]` is wrongly created.

A big array literal is created and concatenated for every 256 elements.
The newarraykwsplat must be emitted only at the last chunk.
2019-09-07 16:05:15 +09:00
Yusuke Endoh a7a2be7a31 Rename some function/definition names that handles NODE_LIST
from array to list.
Follow up to ac50ac03aeb210763730cdc45f230e236519223d
2019-09-07 13:56:41 +09:00
Yusuke Endoh 99c9431ea1 Rename NODE_ARRAY to NODE_LIST to reflect its actual use cases
and NODE_ZARRAY to NODE_ZLIST.

NODE_ARRAY is used not only by an Array literal, but also the contents
of Hash literals, method call arguments, dynamic string literals, etc.
In addition, the structure of NODE_ARRAY is a linked list, not an array.

This is very confusing, so I believe `NODE_LIST` is a better name.
2019-09-07 13:56:29 +09:00
Jeremy Evans 1d5066efb0 Make m(**{}) mean call without keywords
Previously, **{} was removed by the parser:

```
$ ruby --dump=parse -e '{**{}}'
 @ NODE_SCOPE (line: 1, location: (1,0)-(1,6))
 +- nd_tbl: (empty)
 +- nd_args:
 |   (null node)
 +- nd_body:
     @ NODE_HASH (line: 1, location: (1,0)-(1,6))*
     +- nd_brace: 1 (hash literal)
     +- nd_head:
         (null node)
```

Since it was removed by the parser, the compiler did not know
about it, and `m(**{})` was therefore treated as `m()`.

This modifies the parser to not remove the `**{}`.  A simple
approach for this is fairly simple by just removing a few
lines from the parser, but that would cause two hash
allocations every time it was used.  The approach taken here
modifies both the parser and the compiler, and results in `**{}`
not allocating any hashes in the usual case.

The basic idea is we use a literal node in the parser containing
a frozen empty hash literal.  In the compiler, we recognize when
that is used, and if it is the only keyword present, we just
push it onto the VM stack (no creation of a new hash or merging
of keywords).  If it is the first keyword present, we push a
new empty hash onto the VM stack, so that later keywords can
merge into it.  If it is not the first keyword present, we can
ignore it, since the there is no reason to merge an empty hash
into the existing hash.

Example instructions for `m(**{})`

Before (note ARGS_SIMPLE):

```
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,7)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 opt_send_without_block       <callinfo!mid:m, argc:0, FCALL|ARGS_SIMPLE>, <callcache>
0004 leave
```

After (note putobject and KW_SPLAT):

```
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,7)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 putobject                    {}
0003 opt_send_without_block       <callinfo!mid:m, argc:1, FCALL|KW_SPLAT>, <callcache>
0006 leave
```

Example instructions for `m(**h, **{})`

Before and After (no change):

```
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 putspecialobject             1
0003 newhash                      0
0005 putself
0006 opt_send_without_block       <callinfo!mid:h, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0009 opt_send_without_block       <callinfo!mid:core#hash_merge_kwd, argc:2, ARGS_SIMPLE>, <callcache>
0012 opt_send_without_block       <callinfo!mid:m, argc:1, FCALL|KW_SPLAT>, <callcache>
0015 leave
```

Example instructions for `m(**{}, **h)`

Before:

```
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 putspecialobject             1
0003 newhash                      0
0005 putself
0006 opt_send_without_block       <callinfo!mid:h, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0009 opt_send_without_block       <callinfo!mid:core#hash_merge_kwd, argc:2, ARGS_SIMPLE>, <callcache>
0012 opt_send_without_block       <callinfo!mid:m, argc:1, FCALL|KW_SPLAT>, <callcache>
0015 leave
```

After (basically the same except for the addition of swap):

```
== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 newhash                      0
0003 putspecialobject             1
0005 swap
0006 putself
0007 opt_send_without_block       <callinfo!mid:h, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0010 opt_send_without_block       <callinfo!mid:core#hash_merge_kwd, argc:2, ARGS_SIMPLE>, <callcache>
0013 opt_send_without_block       <callinfo!mid:m, argc:1, FCALL|KW_SPLAT>, <callcache>
0016 leave
```
2019-09-05 09:57:43 -07:00