From 1a80d7bcdaa9a57f0feaf0a14ced8c9fb442d25d Mon Sep 17 00:00:00 2001 From: 卜部昌平 Date: Wed, 4 Dec 2019 14:01:56 +0900 Subject: internal/string.h rework Reduced the number of macros defined in the file. Also made it explicit for MJIT_FUNC_EXPORTTED functions to be so. --- internal/compilers.h | 5 +++ internal/string.h | 120 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 85 insertions(+), 40 deletions(-) (limited to 'internal') diff --git a/internal/compilers.h b/internal/compilers.h index 0f470937c5..2b1189a670 100644 --- a/internal/compilers.h +++ b/internal/compilers.h @@ -92,6 +92,7 @@ # define __has_builtin____builtin_clz GCC_VERSION_SINCE(3, 6, 0) # define __has_builtin____builtin_clzl GCC_VERSION_SINCE(3, 6, 0) # define __has_builtin____builtin_clzll GCC_VERSION_SINCE(3, 6, 0) +# define __has_builtin____builtin_constant_p GCC_VERSION_SINCE(2,95, 3) # define __has_builtin____builtin_ctz GCC_VERSION_SINCE(3, 6, 0) # define __has_builtin____builtin_ctzl GCC_VERSION_SINCE(3, 6, 0) # define __has_builtin____builtin_ctzll GCC_VERSION_SINCE(3, 6, 0) @@ -130,6 +131,10 @@ # undef __has_builtin____builtin_clzll # define __has_builtin____builtin_clzll HAVE_BUILTIN____BUILTIN_CLZLL # endif +# ifdef HAVE_BUILTIN____BUILTIN_CONSTANT_P +# undef __has_builtin____builtin_constant_p +# define __has_builtin____builtin_constant_p HAVE_BUILTIN____BUILTIN_CONSTANT_P +# endif # ifdef HAVE_BUILTIN____BUILTIN_CTZ # undef __has_builtin____builtin_ctz # define __has_builtin____builtin_ctz HAVE_BUILTIN____BUILTIN_CTZ diff --git a/internal/string.h b/internal/string.h index d020d06642..dae4f1bdc0 100644 --- a/internal/string.h +++ b/internal/string.h @@ -9,56 +9,102 @@ * modify this file, provided that the conditions mentioned in the * file COPYING are met. Consult the file for details. */ +#include "ruby/config.h" +#include /* for size_t */ +#include "internal/compilers.h" /* for __has_builtin */ +#include "internal/stdbool.h" /* for bool */ +#include "ruby/encoding.h" /* for rb_encoding */ +#include "ruby/ruby.h" /* for VALUE */ +#define STR_NOEMBED FL_USER1 +#define STR_SHARED FL_USER2 /* = ELTS_SHARED */ /* string.c */ VALUE rb_fstring(VALUE); -VALUE rb_fstring_new(const char *ptr, long len); -#define rb_fstring_lit(str) rb_fstring_new((str), rb_strlen_lit(str)) -#define rb_fstring_literal(str) rb_fstring_lit(str) VALUE rb_fstring_cstr(const char *str); -#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P -# define rb_fstring_cstr(str) RB_GNUC_EXTENSION_BLOCK( \ - (__builtin_constant_p(str)) ? \ - rb_fstring_new((str), (long)strlen(str)) : \ - rb_fstring_cstr(str) \ -) -#endif -#ifdef RUBY_ENCODING_H VALUE rb_fstring_enc_new(const char *ptr, long len, rb_encoding *enc); -#define rb_fstring_enc_lit(str, enc) rb_fstring_enc_new((str), rb_strlen_lit(str), (enc)) -#define rb_fstring_enc_literal(str, enc) rb_fstring_enc_lit(str, enc) -#endif int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p); int rb_str_symname_p(VALUE); VALUE rb_str_quote_unprintable(VALUE); -VALUE rb_id_quote_unprintable(ID); -#define QUOTE(str) rb_str_quote_unprintable(str) -#define QUOTE_ID(id) rb_id_quote_unprintable(id) char *rb_str_fill_terminator(VALUE str, const int termlen); void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen); VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg); VALUE rb_str_chomp_string(VALUE str, VALUE chomp); -#ifdef RUBY_ENCODING_H VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc); VALUE rb_str_cat_conv_enc_opts(VALUE newstr, long ofs, const char *ptr, long len, rb_encoding *from, int ecflags, VALUE ecopts); VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl); VALUE rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc); -#endif -#define STR_NOEMBED FL_USER1 -#define STR_SHARED FL_USER2 /* = ELTS_SHARED */ -#define STR_EMBED_P(str) (!FL_TEST_RAW((str), STR_NOEMBED)) -#define STR_SHARED_P(s) FL_ALL_RAW((s), STR_NOEMBED|ELTS_SHARED) -#define is_ascii_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) -#define is_broken_string(str) (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) size_t rb_str_memsize(VALUE); -VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc); char *rb_str_to_cstr(VALUE str); -VALUE rb_str_eql(VALUE str1, VALUE str2); -VALUE rb_obj_as_string_result(VALUE str, VALUE obj); const char *ruby_escaped_char(int c); -VALUE rb_str_opt_plus(VALUE, VALUE); +static inline bool STR_EMBED_P(VALUE str); +static inline bool STR_SHARED_P(VALUE str); +static inline VALUE QUOTE(VALUE v); +static inline VALUE QUOTE_ID(ID v); +static inline bool is_ascii_string(VALUE str); +static inline bool is_broken_string(VALUE str); +static inline VALUE rb_str_eql_internal(const VALUE str1, const VALUE str2); + +RUBY_SYMBOL_EXPORT_BEGIN +/* string.c (export) */ +VALUE rb_str_tmp_frozen_acquire(VALUE str); +void rb_str_tmp_frozen_release(VALUE str, VALUE tmp); +VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc); +VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE); +VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE); +RUBY_SYMBOL_EXPORT_END + +MJIT_SYMBOL_EXPORT_BEGIN +VALUE rb_fstring_new(const char *ptr, long len); +VALUE rb_obj_as_string_result(VALUE str, VALUE obj); +VALUE rb_str_opt_plus(VALUE x, VALUE y); +VALUE rb_str_concat_literals(size_t num, const VALUE *strary); +VALUE rb_str_eql(VALUE str1, VALUE str2); +VALUE rb_id_quote_unprintable(ID); +VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, int kw_splat, VALUE passed_proc); +MJIT_SYMBOL_EXPORT_END + +#define rb_fstring_lit(str) rb_fstring_new((str), rb_strlen_lit(str)) +#define rb_fstring_literal(str) rb_fstring_lit(str) +#define rb_fstring_enc_lit(str, enc) rb_fstring_enc_new((str), rb_strlen_lit(str), (enc)) +#define rb_fstring_enc_literal(str, enc) rb_fstring_enc_lit(str, enc) + +static inline VALUE +QUOTE(VALUE v) +{ + return rb_str_quote_unprintable(v); +} + +static inline VALUE +QUOTE_ID(ID i) +{ + return rb_id_quote_unprintable(i); +} + +static inline bool +STR_EMBED_P(VALUE str) +{ + return ! FL_TEST_RAW(str, STR_NOEMBED); +} + +static inline bool +STR_SHARED_P(VALUE str) +{ + return FL_ALL_RAW(str, STR_NOEMBED | ELTS_SHARED); +} + +static inline bool +is_ascii_string(VALUE str) +{ + return rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT; +} + +static inline bool +is_broken_string(VALUE str) +{ + return rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN; +} /* expect tail call optimization */ static inline VALUE @@ -76,16 +122,10 @@ rb_str_eql_internal(const VALUE str1, const VALUE str2) return Qfalse; } -RUBY_SYMBOL_EXPORT_BEGIN -/* string.c (export) */ -VALUE rb_str_tmp_frozen_acquire(VALUE str); -void rb_str_tmp_frozen_release(VALUE str, VALUE tmp); -#ifdef RUBY_ENCODING_H -/* internal use */ -VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc); +#if __has_builtin(__builtin_constant_p) +# define rb_fstring_cstr(str) \ + (__builtin_constant_p(str) ? \ + rb_fstring_new((str), (long)strlen(str)) : \ + (rb_fstring_cstr)(str)) #endif -VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE); -VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE); -RUBY_SYMBOL_EXPORT_END - #endif /* INTERNAL_STRING_H */ -- cgit v1.2.3