summaryrefslogtreecommitdiff
path: root/compile.c
AgeCommit message (Collapse)Author
2020-04-15Create succ_index_table as a part of `iseq_setup`Nobuyoshi Nakada
With compiling `CPDEBUG >= 2`, `rb_iseq_disasm` segfaults if this table has not been created. Also `ibf_load_iseq_each` calls `rb_iseq_insns_info_encode_positions`. Notes: Merged: https://github.com/ruby/ruby/pull/3033
2020-04-15Disassemble nop-inserted listNobuyoshi Nakada
2020-04-15Show heading for update_catch_except_flagsNobuyoshi Nakada
2020-04-12Avoid UB with flexible array memberAlan Wu
Accessing past the end of an array is technically UB. Use C99 flexible array member instead to avoid the UB and simplify allocation size calculation. See also: DCL38-C in the SEI CERT C Coding Standard Notes: Merged: https://github.com/ruby/ruby/pull/3017 Merged-By: XrXr
2020-04-08Suppress -Wswitch warningsNobuyoshi Nakada
2020-04-08Merge pull request #2991 from shyouhei/ruby.h卜部昌平
Split ruby.h Notes: Merged-By: shyouhei <shyouhei@ruby-lang.org>
2020-03-17Reduce allocations for keyword argument hashesJeremy Evans
Previously, passing a keyword splat to a method always allocated a hash on the caller side, and accepting arbitrary keywords in a method allocated a separate hash on the callee side. Passing explicit keywords to a method that accepted a keyword splat did not allocate a hash on the caller side, but resulted in two hashes allocated on the callee side. This commit makes passing a single keyword splat to a method not allocate a hash on the caller side. Passing multiple keyword splats or a mix of explicit keywords and a keyword splat still generates a hash on the caller side. On the callee side, if arbitrary keywords are not accepted, it does not allocate a hash. If arbitrary keywords are accepted, it will allocate a hash, but this commit uses a callinfo flag to indicate whether the caller already allocated a hash, and if so, the callee can use the passed hash without duplicating it. So this commit should make it so that a maximum of a single hash is allocated during method calls. To set the callinfo flag appropriately, method call argument compilation checks if only a single keyword splat is given. If only one keyword splat is given, the VM_CALL_KW_SPLAT_MUT callinfo flag is not set, since in that case the keyword splat is passed directly and not mutable. If more than one splat is used, a new hash needs to be generated on the caller side, and in that case the callinfo flag is set, indicating the keyword splat is mutable by the callee. In compile_hash, used for both hash and keyword argument compilation, if compiling keyword arguments and only a single keyword splat is used, pass the argument directly. On the caller side, in vm_args.c, the callinfo flag needs to be recognized and handled. Because the keyword splat argument may not be a hash, it needs to be converted to a hash first if not. Then, unless the callinfo flag is set, the hash needs to be duplicated. The temporary copy of the callinfo flag, kw_flag, is updated if a hash was duplicated, to prevent the need to duplicate it again. If we are converting to a hash or duplicating a hash, we need to update the argument array, which can including duplicating the positional splat array if one was passed. CALLER_SETUP_ARG and a couple other places needs to be modified to handle similar issues for other types of calls. This includes fairly comprehensive tests for different ways keywords are handled internally, checking that you get equal results but that keyword splats on the caller side result in distinct objects for keyword rest parameters. Included are benchmarks for keyword argument calls. Brief results when compiled without optimization: def kw(a: 1) a end def kws(**kw) kw end h = {a: 1} kw(a: 1) # about same kw(**h) # 2.37x faster kws(a: 1) # 1.30x faster kws(**h) # 2.19x faster kw(a: 1, **h) # 1.03x slower kw(**h, **h) # about same kws(a: 1, **h) # 1.16x faster kws(**h, **h) # 1.14x faster Notes: Merged: https://github.com/ruby/ruby/pull/2945
2020-03-17Make {**{}} return unfrozen empty hashJeremy Evans
Previously, method call keyword splats and hash keyword splats were compiled exactly the same. This is because parse-wise, they operate on indentical nodes when it comes to compiling the **{}. Fix this by using an ugly hack of temporarily modifying the nd_brace flag in the method call keyword splat case. Inside compile_hash, only optimize the **{} case for hashes where the nd_brace flag has been modified to reflect we are in the method call keyword splat case and it is safe to do so. Since compile_keyword_args is only called in one place, move the keyword_node_p call out of that method to the single caller to avoid duplicating the code. Notes: Merged: https://github.com/ruby/ruby/pull/2945
2020-03-12Correctly detect whether strict alignment is needed on OpenBSDJeremy Evans
From Stefan Sperling <stsp@apache.org>
2020-02-22CI can be NULL.Koichi Sasada
Unused CI (introduced from peephole optimization, etc) can be NULL so introduce NULL check.
2020-02-22Introduce disposable call-cache.Koichi Sasada
This patch contains several ideas: (1) Disposable inline method cache (IMC) for race-free inline method cache * Making call-cache (CC) as a RVALUE (GC target object) and allocate new CC on cache miss. * This technique allows race-free access from parallel processing elements like RCU. (2) Introduce per-Class method cache (pCMC) * Instead of fixed-size global method cache (GMC), pCMC allows flexible cache size. * Caching CCs reduces CC allocation and allow sharing CC's fast-path between same call-info (CI) call-sites. (3) Invalidate an inline method cache by invalidating corresponding method entries (MEs) * Instead of using class serials, we set "invalidated" flag for method entry itself to represent cache invalidation. * Compare with using class serials, the impact of method modification (add/overwrite/delete) is small. * Updating class serials invalidate all method caches of the class and sub-classes. * Proposed approach only invalidate the method cache of only one ME. See [Feature #16614] for more details. Notes: Merged: https://github.com/ruby/ruby/pull/2888
2020-02-22VALUE size packed callinfo (ci).Koichi Sasada
Now, rb_call_info contains how to call the method with tuple of (mid, orig_argc, flags, kwarg). Most of cases, kwarg == NULL and mid+argc+flags only requires 64bits. So this patch packed rb_call_info to VALUE (1 word) on such cases. If we can not represent it in VALUE, then use imemo_callinfo which contains conventional callinfo (rb_callinfo, renamed from rb_call_info). iseq->body->ci_kw_size is removed because all of callinfo is VALUE size (packed ci or a pointer to imemo_callinfo). To access ci information, we need to use these functions: vm_ci_mid(ci), _flag(ci), _argc(ci), _kwarg(ci). struct rb_call_info_kw_arg is renamed to rb_callinfo_kwarg. rb_funcallv_with_cc() and rb_method_basic_definition_p_with_cc() is temporary removed because cd->ci should be marked. Notes: Merged: https://github.com/ruby/ruby/pull/2888
2020-02-20Fixed missing `return`Nobuyoshi Nakada
Get rid of double writing.
2020-02-20printf can be a macro卜部昌平
Namely glibc has this macro on -DFORTIFY_SOURCE. We have to prevent macro redefinition with different macro body.
2020-02-16Split the optimizable range item conditionsNobuyoshi Nakada
2020-02-16Reduce begin-less/end-less range allocationMasataka Pocke Kuwabara
``` $ cat test.yaml prelude: | def endless 1.. end def beginless ..1 end def endless_substr(str) str[1..] end benchmark: endless: endless beginless: beginless endless_substr: "endless_substr('foo')" $ RBENV_VERSION=trunk ruby -v ruby 2.8.0dev (2020-02-15T12:52:03Z master 961630126b) [x86_64-linux] $ RBENV_VERSION=patched ruby -v ruby 2.8.0dev (2020-02-15T12:52:03Z origin/master 961630126b) [x86_64-linux] $ benchmark-driver test.yaml --rbenv 'patched;trunk' Warming up -------------------------------------- endless 45.948M i/s - 46.076M times in 1.002782s (21.76ns/i, 26clocks/i) beginless 49.986M i/s - 50.237M times in 1.005037s (20.01ns/i, 24clocks/i) endless_substr 8.067M i/s - 8.187M times in 1.014936s (123.96ns/i, 148clocks/i) Calculating ------------------------------------- patched trunk endless 115.679M 21.500M i/s - 137.843M times in 1.191597s 6.411398s beginless 112.599M 22.060M i/s - 149.957M times in 1.331778s 6.797768s endless_substr 8.888M 6.760M i/s - 24.201M times in 2.722995s 3.580038s Comparison: endless patched: 115679391.9 i/s trunk: 21499711.2 i/s - 5.38x slower beginless patched: 112598731.5 i/s trunk: 22059673.0 i/s - 5.10x slower endless_substr patched: 8887513.1 i/s trunk: 6759886.2 i/s - 1.31x slower ``` Notes: Merged: https://github.com/ruby/ruby/pull/2910
2020-02-11Make yield in singleton class definitions in methods a SyntaxErrorJeremy Evans
This behavior was deprecated in 2.7 and scheduled to be removed in 3.0. Calling yield in a class definition outside a method is now a SyntaxError instead of a LocalJumpError, as well. Notes: Merged: https://github.com/ruby/ruby/pull/2901
2020-02-09compile.c: Drop obj_list from ibf_dumpNagayamaRyoga
[Feature #16505] Notes: Merged: https://github.com/ruby/ruby/pull/2835
2020-02-09compile.c: Drop iseq_list from ibf_dumpNagayamaRyoga
[Feature #16505] Notes: Merged: https://github.com/ruby/ruby/pull/2835
2020-02-09Deduplicate objects efficiently when dumping iseq to binaryNagayamaRyoga
We were inefficient in cases where there are a lot of duplicates due to the use of linear search. Use a hash table instead. These cases are not that rare in the wild. [Feature #16505] Notes: Merged: https://github.com/ruby/ruby/pull/2835
2020-02-03Check type of empty keyword [Bug #16603]Seiei Miyagi
Co-authored-by: Yusuke Endoh <mame@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/2874
2020-02-01compile.c: remove a unused variableYusuke Endoh
2020-01-30Optimized branches in pattern matchingNobuyoshi Nakada
2020-01-03move internal/debug.h definitions to internal.hKoichi Sasada
Debug utilities should be accessible from any internal code.
2019-12-26decouple internal.h headers卜部昌平
Saves comitters' daily life by avoid #include-ing everything from internal.h to make each file do so instead. This would significantly speed up incremental builds. We take the following inclusion order in this changeset: 1. "ruby/config.h", where _GNU_SOURCE is defined (must be the very first thing among everything). 2. RUBY_EXTCONF_H if any. 3. Standard C headers, sorted alphabetically. 4. Other system headers, maybe guarded by #ifdef 5. Everything else, sorted alphabetically. Exceptions are those win32-related headers, which tend not be self- containing (headers have inclusion order dependencies). Notes: Merged: https://github.com/ruby/ruby/pull/2711
2019-12-25export a function for MJIT.Koichi Sasada
rb_iseq_complete() can be used by MJIT.
2019-12-25take care of USE_LAZY_LOAD=1.Koichi Sasada
On USE_LAZY_LOAD=1, the iseq should be loaded. So rb_iseq_check() is needed. Furthermore, now lazy loading with builtin_function_table is not supported, so it should cancel lazy loading.
2019-12-22Manage deprecation warning by the flagNobuyoshi Nakada
2019-12-22compile.c: avoid newarraykwsplat for argumentsv2_7_0_rc2Yusuke Endoh
`foo(*rest, post, **empty_kw)` is compiled like `foo(*rest + [post, **empty_kw])`, and `**empty_kw` is removed by "newarraykwsplat" instruction. However, the method call still has a flag of KW_SPLAT, so "post" is considered as a keyword hash, which caused a segfault. Note that the flag cannot be removed if "empty_kw" is not always empty. This change fixes the issue by compiling arguments with "newarray" instead of "newarraykwsplat". [Bug #16442]
2019-12-20Fixed misspellingsNobuyoshi Nakada
Fixed misspellings reported at [Bug #16437], only in ruby and rubyspec.
2019-12-13readable function names for inline functions.Koichi Sasada
Now, C functions written by __builtin_cexpr!(code) and others are named as "__builtin_inline#{n}". However, it is difficult to know what the function is. This patch rename them into "__builtin_foo_#{lineno}" when cexpr! is in 'foo' method.
2019-12-12add casts卜部昌平
%p is for void *. Becuase fprintf is a function with variadic arguments automatic cast from any pointer to void * does not work. We have to be explicit.
2019-12-09vm_args.c (rb_warn_check): Use iseq_unique_id instead of its pointerYusuke Endoh
(This is the second try of 036bc1da6c6c9b0fa9b7f5968d897a9554dd770e.) If iseq is GC'ed, the pointer of iseq may be reused, which may hide a deprecation warning of keyword argument change. http://ci.rvm.jp/results/trunk-test1@phosphorus-docker/2474221 ``` 1) Failure: TestKeywordArguments#test_explicit_super_kwsplat [/tmp/ruby/v2/src/trunk-test1/test/ruby/test_keyword.rb:549]: --- expected +++ actual @@ -1 +1 @@ -/The keyword argument is passed as the last hash parameter.* for `m'/m +"" ``` This change ad-hocly adds iseq_unique_id for each iseq, and use it instead of iseq pointer. This covers the case where caller is GC'ed. Still, the case where callee is GC'ed, is not covered. But anyway, it is very rare that iseq is GC'ed. Even when it occurs, it just hides some warnings. It's no big deal.
2019-12-05Introduce an "Inline IVAR cache" structAaron Patterson
This commit introduces an "inline ivar cache" struct. The reason we need this is so compaction can differentiate from an ivar cache and a regular inline cache. Regular inline caches contain references to `VALUE` and ivar caches just contain references to the ivar index. With this new struct we can easily update references for inline caches (but not inline var caches as they just contain an int)
2019-12-04compile.c: stop wrong peephole optimization when covearge is enabledYusuke Endoh
jump-jump optimization ignores the event flags of the jump instruction being skipped, which leads to overlook of line events. This changeset stops the wrong optimization when coverage measurement is neabled and when the jump instruction has any event flag. Note that this issue is not only for coverage but also for TracePoint, and this change does not fix TracePoint. However, fixing it fundamentally is tough (which requires revamp of the compiler). This issue is critical in terms of coverage measurement, but minor for TracePoint (ko1 said), so we here choose a stopgap measurement. [Bug #15980] [Bug #16397] Note for backporters: this changeset can be viewed by `git diff -w`.
2019-12-04compile.c: trivial refactoringYusuke Endoh
Use `for` instead of `while` to make it explicit that it is a traverse of bytecode.
2019-11-27rename __builtin_inline!(code) and introduce others.Koichi Sasada
rename __builtin_inline!(code) to __builtin_cstmt(code). Also this commit introduce the following inlining C code features. * __builtin_cstmt!(STMT) (renamed from __builtin_inline!) Define a function which run STMT implicitly and call this function at evatuation time. Note that you need to return some value in STMT. If there is a local variables (includes method parameters), you can read these values. static VALUE func(ec, self) { VALUE x = ...; STMT } Usage: def double a # a is readable from C code. __builtin_cstmt! 'return INT2FIX(FIX2INT(a) * 2);' end * __builtin_cexpr!(EXPR) Define a function which invoke EXPR implicitly like `__builtin_cstmt!`. Different from cstmt!, which compiled with `return EXPR;`. (`return` and `;` are added implicitly) static VALUE func(ec, self) { VALUE x = ...; return EXPPR; } Usage: def double a __builtin_cexpr! 'INT2FIX(FIX2INT(a) * 2)' end * __builtin_cconst!(EXPR) Define a function which invoke EXPR implicitly like cexpr!. However, the function is called once at compile time, not evaluated time. Any local variables are not accessible (because there is no local variable at compile time). Usage: GCC = __builtin_cconst! '__GNUC__' * __builtin_cinit!(STMT) STMT are writtein in auto-generated code. This code does not return any value. Usage: __builtin_cinit! '#include <zlib.h>' def no_compression? __builtin_cconst! 'Z_NO_COMPRESSION ? Qtrue : Qfalse' end
2019-11-19make functions static卜部昌平
These functions are used from within a compilation unit so we can make them static, for better binary size. This changeset reduces the size of generated ruby binary from 26,590,128 bytes to 26,584,472 bytes on my macihne. Notes: Merged: https://github.com/ruby/ruby/pull/2682
2019-11-18vm_invoke_builtin_delegate with start index.Koichi Sasada
opt_invokebuiltin_delegate and opt_invokebuiltin_delegate_leave invokes builtin functions with same parameters of the method. This technique eliminate stack push operations. However, delegation parameters should be completely same as given parameters. (e.g. `def foo(a, b, c) __builtin_foo(a, b, c)` is okay, but __builtin_foo(b, c) is not allowed) This patch relaxes this restriction. ISeq has a local variables table which includes parameters. For example, the method defined as `def foo(a, b, c) x=y=nil`, then local variables table contains [a, b, c, x, y]. If calling builtin-function with arguments which are sub-array of the lvar table, use opt_invokebuiltin_delegate instruction with start index. For example, `__builtin_foo(b, c)`, `__builtin_bar(c, x, y)` is okay, and so on.
2019-11-18More fixes for $SAFE/taint post mergingJeremy Evans
Notes: Merged: https://github.com/ruby/ruby/pull/2476
2019-11-13Avoid top-level search for nested constant reference from nil in defined?Dylan Thacker-Smith
Fixes [Bug #16332] Constant access was changed to no longer allow top-level constant access through `nil`, but `defined?` wasn't changed at the same time to stay consistent. Use a separate defined type to distinguish between a constant referenced from the current lexical scope and one referenced from another namespace. Notes: Merged: https://github.com/ruby/ruby/pull/2657
2019-11-12Revert "Method reference operator"Nobuyoshi Nakada
This reverts commit 67c574736912003c377218153f9d3b9c0c96a17b. [Feature #16275]
2019-11-12Get rid of `__` prefix which is presereved by C standardNobuyoshi Nakada
2019-11-11__builtin_inline!Koichi Sasada
Add an experimental `__builtin_inline!(c_expression)` special intrinsic which run a C code snippet. In `c_expression`, you can access the following variables: * ec (rb_execution_context_t *) * self (const VALUE) * local variables (const VALUE) Not that you can read these variables, but you can not write them. You need to return from this expression and return value will be a result of __builtin_inline!(). Examples: `def foo(x) __builtin_inline!('return rb_p(x);'); end` calls `p(x)`. `def double(x) __builtin_inline!('return INT2NUM(NUM2INT(x) * 2);')` returns x*2.
2019-11-08Make prefix staticNobuyoshi Nakada
2019-11-08cstr -> bytesKoichi Sasada
rb_iseq_ibf_load_cstr() accepts bytes, but not NUL-terminate C string. To make it clear, rename it to _bytes.
2019-11-08revival of __func__卜部昌平
dad2abc69fdd1af52df353b8604017bd6a5c6a99 deleted __func__ but ruby already use this feature under RUBY_FUNCTION_NAME_STRING macro. Use it.
2019-11-08do not use __func__.Koichi Sasada
Microsoft Visual Studio 12.0 doesn't support it.
2019-11-08fix typeKoichi Sasada
2019-11-08* remove trailing spaces. [ci skip]git