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

42 Коммитов

Автор SHA1 Сообщение Дата
Nobuyoshi Nakada 54199a3f5f
Use `VALUE` instead of `intptr_t`
On emscripten `intptr_t`, `uintptr_t`, `ptrdiff_t` and so on are
defined as `long`, but `PRIdPTR` and so on defined as `int`.
2021-08-16 15:36:37 +09:00
Benoit Daloze 68d6bd0873 Fix trivial -Wundef warnings
* See [Feature #17752]

Co-authored-by: xtkoba (Tee KOBAYASHI) <xtkoba+ruby@gmail.com>
2021-05-04 14:56:55 +02:00
Nobuyoshi Nakada 37e2a67a74
Method ID of call and fcall can be const not only ident 2021-01-01 00:54:38 +09:00
Nobuyoshi Nakada 0fbf4d0374
Access to reserved word parameter like as `__builtin.arg!(:if)` 2020-12-31 15:11:38 +09:00
Yusuke Endoh 248f1ef282 tool/mk_builtin_loader.rb: prevent "assigned but unused variable" 2020-12-12 23:09:11 +09:00
卜部昌平 1fb4e28002 skip inlining cexpr! that are not attr! inline
Requested by ko1.
2020-07-16 11:49:09 +09:00
卜部昌平 927fe2422f mk_builtin_loader.rb: STACK_ADDR_FROM_TOP unusable
Stacks are emulated in MJIT, must not touch the original VM stack.

See also http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/3061353
2020-07-13 12:30:43 +09:00
卜部昌平 4506f6119a %p is not portable accross platforms
This commit fixes compiler error on MSVC.  %p on that platform is not
suitable to represent a compile-time constant.

https://ci.appveyor.com/project/ruby/ruby/builds/34017163/job/vj2a8uk3gwv9yxak#L24381
2020-07-13 08:56:18 +09:00
卜部昌平 2363a16e9a add comments 2020-07-13 08:56:18 +09:00
卜部昌平 5783d84a17 fix typo 2020-07-13 08:56:18 +09:00
卜部昌平 9721f477c7 inline Primitive.cexpr!
We can obtain the verbatim source code of Primitive.cexpr!.  Why not
paste that content into the JITed program.
2020-07-13 08:56:18 +09:00
卜部昌平 f66e0212ef precalc invokebuiltin destinations
Noticed that struct rb_builtin_function is a purely compile-time
constant.  MJIT can eliminate some runtime calculations by statically
generate dedicated C code generator for each builtin functions.
2020-07-13 08:56:18 +09:00
Koichi Sasada 95f5fd9a55 fix up Primitive.cinit! code
Recent changes break Primitive.cinit!(c_code) so fix it.
2020-07-05 10:44:36 +09:00
Koichi Sasada 74e1bca79d support all locals for cexpr!, cstmt!
Primitve.cexpr! and .cstmt! can access Ruby's parameter and
*local variables* (note that local parameters are also local
variables). However recent changes only allow to access
parameters. This patch fix it.

For example, the following code can work:

def foo a, b, k: :kw, **kwrest
  c = a + b
  d = k
  e = kwrest
  p Primitive.cstmt!(%q(rb_p(rb_ary_new_from_args(5, a, b, c, d, e));
                        return Qnil;))
end
2020-07-04 17:28:23 +09:00
Nobuyoshi Nakada 09b936d89c
Calculate header line count instead of hardcoding 2020-06-28 14:14:29 +09:00
Nobuyoshi Nakada 1020f7e3aa
Replace separators in input file name in header too 2020-06-28 14:12:26 +09:00
Nobuyoshi Nakada 44a659ad97
Replace ALT_SEPARATOR with SEPARATOR also in output file name
To suppress warnings by Visual C.
```
./integer.rb(5) : warning C4129: 'i' : unrecognized character escape sequence
./kernel.rb(21) : warning C4129: 'k' : unrecognized character escape sequence
```
2020-06-28 13:20:40 +09:00
Takashi Kokubun 7561db8c00
Introduce Primitive.attr! to annotate 'inline' (#3242)
[Feature #15589]
2020-06-20 17:13:03 -07:00
Nobuyoshi Nakada 49f0fd21e4 [Feature #16254] Allow `Primitive.func` style 2020-06-19 18:46:55 +09:00
Nobuyoshi Nakada c8703a17ce [Feature #16254] Allow `__builtin.func` style 2020-06-19 18:46:55 +09:00
Nobuyoshi Nakada d23917dd85
Support arguments of singleton method 2020-06-14 16:09:00 +09:00
Nobuyoshi Nakada b22bfdaa9a
Fixed up rest, keywords, keyword rest and block arguments 2020-06-14 16:08:33 +09:00
Takashi Kokubun 997133d595
Make __builtin_cexpr! and __builtin_cstmt! work again
with Ripper.

a3e6f52c17 introduced __builtin_cexpr! and
__builtin_cstmt!, but nobody has used them and then they broke on
79292b3088 by undefined `params`.

This patch fixes the undefined `params`, but still we're not using them
yet.
2020-06-13 23:48:34 -07:00
Nobuyoshi Nakada 79292b3088
Make builtin loader sources by Ripper 2020-05-19 12:54:51 +09:00
Nobuyoshi Nakada 11fa1dcc23
Also scan `rescue` clauses 2020-04-04 12:38:00 +09:00
Nobuyoshi Nakada d7bef803ac Separate builtin initialization calls 2019-12-29 12:34:55 +09:00
卜部昌平 5e22f873ed decouple 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).
2019-12-26 20:45:12 +09:00
Nobuyoshi Nakada 5de284ec78
Fixed a typo in an exception class name 2019-12-23 10:37:30 +09:00
Koichi Sasada 97a17a51b8 readable function names for inline functions.
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-13 17:55:45 +09:00
Koichi Sasada a3e6f52c17 rename __builtin_inline!(code) and introduce others.
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-27 03:55:28 +09:00
Nobuyoshi Nakada 7db719c516
Write rbinc files to the source directory
Update the target file itself of the dependency on this script.
Fall back to the current working directory if unwritable.
2019-11-26 21:49:42 +09:00
Nobuyoshi Nakada b0fb9bda9a
Strip the last line which become trailing spaces 2019-11-12 16:16:46 +09:00
Nobuyoshi Nakada 2de3feab44
Get rid of `__` prefix which is presereved by C standard 2019-11-12 16:09:36 +09:00
Koichi Sasada 3141642380 __builtin_inline!
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-11 16:47:50 +09:00
Nobuyoshi Nakada e3c8524411
Full-path of builtin scripts no longer needed 2019-11-09 19:43:14 +09:00
Koichi Sasada b5d8849220 Revert "don't embed full-path."
This reverts commit dfac2e9eb3.

It does not work if cwd is different from builddir...
2019-11-09 07:09:01 +09:00
Koichi Sasada dfac2e9eb3 don't embed full-path.
miniruby load *.rb from srcdir. To specify file path,
tool/mk_builtin_loader.rb embed full path of each *.rb file.
However it prevent to pre-generation of required files for tarball.
This patch generate srcdir/*.rb from __FILE__ information.
2019-11-09 06:57:58 +09:00
Yusuke Endoh 882179a0ec tool/mk_builtin_loader.rb: check if op is an array or not
The insn array includes not only an array but also some literal objects.
2019-11-08 23:29:50 +09:00
Nobuyoshi Nakada 2e29b65109
Add file mode to generated files [ci skip] 2019-11-08 16:37:42 +09:00
Nobuyoshi Nakada 20971799f2
Renamed `load_*.inc` as `*.rbinc` to utilize a suffix rule 2019-11-08 16:30:28 +09:00
Koichi Sasada 2eb02dfd3b Stop compiling if type mismatch was found.
If there is a type mismatch between expected builtin function type
and actual function type, C compiler shows warning.

For example, `__builtin_func(1, 2)` expects
`func(rb_ec_t*, VALUE self, VALUE p1, VALUE p2)` function definition.

However, it is easy to overlook "warning" messages. So this patch
changes to stop compiling as an error if there is a mismatch.
2019-11-08 15:29:02 +09:00
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