kanayago 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +15 -0
  3. data/.rubocop_todo.yml +23 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +79 -0
  6. data/Rakefile +182 -0
  7. data/ext/kanayago/ccan/check_type/check_type.h +63 -0
  8. data/ext/kanayago/ccan/container_of/container_of.h +142 -0
  9. data/ext/kanayago/ccan/list/list.h +791 -0
  10. data/ext/kanayago/ccan/str/str.h +17 -0
  11. data/ext/kanayago/constant.h +53 -0
  12. data/ext/kanayago/extconf.rb +21 -0
  13. data/ext/kanayago/id.h +347 -0
  14. data/ext/kanayago/id_table.h +39 -0
  15. data/ext/kanayago/internal/array.h +151 -0
  16. data/ext/kanayago/internal/basic_operators.h +64 -0
  17. data/ext/kanayago/internal/bignum.h +244 -0
  18. data/ext/kanayago/internal/bits.h +568 -0
  19. data/ext/kanayago/internal/compile.h +34 -0
  20. data/ext/kanayago/internal/compilers.h +107 -0
  21. data/ext/kanayago/internal/complex.h +29 -0
  22. data/ext/kanayago/internal/encoding.h +36 -0
  23. data/ext/kanayago/internal/error.h +218 -0
  24. data/ext/kanayago/internal/fixnum.h +184 -0
  25. data/ext/kanayago/internal/gc.h +322 -0
  26. data/ext/kanayago/internal/hash.h +191 -0
  27. data/ext/kanayago/internal/imemo.h +261 -0
  28. data/ext/kanayago/internal/io.h +140 -0
  29. data/ext/kanayago/internal/numeric.h +274 -0
  30. data/ext/kanayago/internal/parse.h +117 -0
  31. data/ext/kanayago/internal/rational.h +71 -0
  32. data/ext/kanayago/internal/re.h +28 -0
  33. data/ext/kanayago/internal/ruby_parser.h +125 -0
  34. data/ext/kanayago/internal/sanitizers.h +297 -0
  35. data/ext/kanayago/internal/serial.h +23 -0
  36. data/ext/kanayago/internal/static_assert.h +16 -0
  37. data/ext/kanayago/internal/string.h +186 -0
  38. data/ext/kanayago/internal/symbol.h +45 -0
  39. data/ext/kanayago/internal/thread.h +79 -0
  40. data/ext/kanayago/internal/variable.h +72 -0
  41. data/ext/kanayago/internal/vm.h +137 -0
  42. data/ext/kanayago/internal/warnings.h +16 -0
  43. data/ext/kanayago/internal.h +108 -0
  44. data/ext/kanayago/kanayago.c +420 -0
  45. data/ext/kanayago/kanayago.h +21 -0
  46. data/ext/kanayago/lex.c +302 -0
  47. data/ext/kanayago/method.h +255 -0
  48. data/ext/kanayago/node.c +440 -0
  49. data/ext/kanayago/node.h +111 -0
  50. data/ext/kanayago/node_name.inc +224 -0
  51. data/ext/kanayago/parse.c +26931 -0
  52. data/ext/kanayago/parse.h +244 -0
  53. data/ext/kanayago/parse.tmp.y +16145 -0
  54. data/ext/kanayago/parser_bits.h +564 -0
  55. data/ext/kanayago/parser_node.h +32 -0
  56. data/ext/kanayago/parser_st.c +164 -0
  57. data/ext/kanayago/parser_st.h +162 -0
  58. data/ext/kanayago/parser_value.h +106 -0
  59. data/ext/kanayago/probes.h +4 -0
  60. data/ext/kanayago/ruby_assert.h +14 -0
  61. data/ext/kanayago/ruby_atomic.h +23 -0
  62. data/ext/kanayago/ruby_parser.c +1165 -0
  63. data/ext/kanayago/rubyparser.h +1391 -0
  64. data/ext/kanayago/shape.h +234 -0
  65. data/ext/kanayago/st.c +2339 -0
  66. data/ext/kanayago/symbol.h +123 -0
  67. data/ext/kanayago/thread_pthread.h +168 -0
  68. data/ext/kanayago/universal_parser.c +230 -0
  69. data/ext/kanayago/vm_core.h +2215 -0
  70. data/ext/kanayago/vm_opts.h +67 -0
  71. data/lib/kanayago/version.rb +5 -0
  72. data/lib/kanayago.rb +11 -0
  73. data/sig/kanayago.rbs +4 -0
  74. metadata +116 -0
@@ -0,0 +1,234 @@
1
+ #ifndef RUBY_SHAPE_H
2
+ #define RUBY_SHAPE_H
3
+
4
+ #include "internal/gc.h"
5
+
6
+ #if (SIZEOF_UINT64_T <= SIZEOF_VALUE)
7
+
8
+ #define SIZEOF_SHAPE_T 4
9
+ #define SHAPE_IN_BASIC_FLAGS 1
10
+ typedef uint32_t attr_index_t;
11
+ typedef uint32_t shape_id_t;
12
+ # define SHAPE_ID_NUM_BITS 32
13
+
14
+ #else
15
+
16
+ #define SIZEOF_SHAPE_T 2
17
+ #define SHAPE_IN_BASIC_FLAGS 0
18
+ typedef uint16_t attr_index_t;
19
+ typedef uint16_t shape_id_t;
20
+ # define SHAPE_ID_NUM_BITS 16
21
+
22
+ #endif
23
+
24
+ typedef uint32_t redblack_id_t;
25
+
26
+ #define MAX_IVARS (attr_index_t)(-1)
27
+
28
+ # define SHAPE_MASK (((uintptr_t)1 << SHAPE_ID_NUM_BITS) - 1)
29
+ # define SHAPE_FLAG_MASK (((VALUE)-1) >> SHAPE_ID_NUM_BITS)
30
+
31
+ # define SHAPE_FLAG_SHIFT ((SIZEOF_VALUE * 8) - SHAPE_ID_NUM_BITS)
32
+
33
+ # define SHAPE_MAX_VARIATIONS 8
34
+
35
+ # define INVALID_SHAPE_ID SHAPE_MASK
36
+ # define ROOT_SHAPE_ID 0x0
37
+
38
+ # define SPECIAL_CONST_SHAPE_ID (ROOT_SHAPE_ID + 1)
39
+ # define OBJ_TOO_COMPLEX_SHAPE_ID (SPECIAL_CONST_SHAPE_ID + 1)
40
+ # define FIRST_T_OBJECT_SHAPE_ID (OBJ_TOO_COMPLEX_SHAPE_ID + 1)
41
+
42
+ typedef struct redblack_node redblack_node_t;
43
+
44
+ struct rb_shape {
45
+ struct rb_id_table * edges; // id_table from ID (ivar) to next shape
46
+ ID edge_name; // ID (ivar) for transition from parent to rb_shape
47
+ attr_index_t next_iv_index;
48
+ uint32_t capacity; // Total capacity of the object with this shape
49
+ uint8_t type;
50
+ uint8_t size_pool_index;
51
+ shape_id_t parent_id;
52
+ redblack_node_t * ancestor_index;
53
+ };
54
+
55
+ typedef struct rb_shape rb_shape_t;
56
+
57
+ struct redblack_node {
58
+ ID key;
59
+ rb_shape_t * value;
60
+ redblack_id_t l;
61
+ redblack_id_t r;
62
+ };
63
+
64
+ enum shape_type {
65
+ SHAPE_ROOT,
66
+ SHAPE_IVAR,
67
+ SHAPE_FROZEN,
68
+ SHAPE_T_OBJECT,
69
+ SHAPE_OBJ_TOO_COMPLEX,
70
+ };
71
+
72
+ typedef struct {
73
+ /* object shapes */
74
+ rb_shape_t *shape_list;
75
+ rb_shape_t *root_shape;
76
+ shape_id_t next_shape_id;
77
+
78
+ redblack_node_t *shape_cache;
79
+ unsigned int cache_size;
80
+ } rb_shape_tree_t;
81
+ RUBY_EXTERN rb_shape_tree_t *rb_shape_tree_ptr;
82
+
83
+ static inline rb_shape_tree_t *
84
+ rb_current_shape_tree(void)
85
+ {
86
+ return rb_shape_tree_ptr;
87
+ }
88
+ #define GET_SHAPE_TREE() rb_current_shape_tree()
89
+
90
+ static inline shape_id_t
91
+ get_shape_id_from_flags(VALUE obj)
92
+ {
93
+ RUBY_ASSERT(!RB_SPECIAL_CONST_P(obj));
94
+ return (shape_id_t)(SHAPE_MASK & ((RBASIC(obj)->flags) >> SHAPE_FLAG_SHIFT));
95
+ }
96
+
97
+ static inline void
98
+ set_shape_id_in_flags(VALUE obj, shape_id_t shape_id)
99
+ {
100
+ // Ractors are occupying the upper 32 bits of flags, but only in debug mode
101
+ // Object shapes are occupying top bits
102
+ RBASIC(obj)->flags &= SHAPE_FLAG_MASK;
103
+ RBASIC(obj)->flags |= ((VALUE)(shape_id) << SHAPE_FLAG_SHIFT);
104
+ }
105
+
106
+
107
+ #if SHAPE_IN_BASIC_FLAGS
108
+ static inline shape_id_t
109
+ RBASIC_SHAPE_ID(VALUE obj)
110
+ {
111
+ return get_shape_id_from_flags(obj);
112
+ }
113
+
114
+ static inline void
115
+ RBASIC_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
116
+ {
117
+ set_shape_id_in_flags(obj, shape_id);
118
+ }
119
+ #endif
120
+
121
+ static inline shape_id_t
122
+ ROBJECT_SHAPE_ID(VALUE obj)
123
+ {
124
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
125
+ return get_shape_id_from_flags(obj);
126
+ }
127
+
128
+ static inline void
129
+ ROBJECT_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
130
+ {
131
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
132
+ set_shape_id_in_flags(obj, shape_id);
133
+ }
134
+
135
+ static inline shape_id_t
136
+ RCLASS_SHAPE_ID(VALUE obj)
137
+ {
138
+ RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
139
+ return get_shape_id_from_flags(obj);
140
+ }
141
+
142
+ static inline void
143
+ RCLASS_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
144
+ {
145
+ RUBY_ASSERT(RB_TYPE_P(obj, T_CLASS) || RB_TYPE_P(obj, T_MODULE));
146
+ set_shape_id_in_flags(obj, shape_id);
147
+ }
148
+
149
+ rb_shape_t * rb_shape_get_root_shape(void);
150
+ int32_t rb_shape_id_offset(void);
151
+
152
+ rb_shape_t * rb_shape_get_parent(rb_shape_t * shape);
153
+
154
+ RUBY_FUNC_EXPORTED rb_shape_t *rb_shape_get_shape_by_id(shape_id_t shape_id);
155
+ RUBY_FUNC_EXPORTED shape_id_t rb_shape_get_shape_id(VALUE obj);
156
+ rb_shape_t * rb_shape_get_next_iv_shape(rb_shape_t * shape, ID id);
157
+ bool rb_shape_get_iv_index(rb_shape_t * shape, ID id, attr_index_t * value);
158
+ bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t * value, shape_id_t *shape_id_hint);
159
+ RUBY_FUNC_EXPORTED bool rb_shape_obj_too_complex(VALUE obj);
160
+
161
+ void rb_shape_set_shape(VALUE obj, rb_shape_t* shape);
162
+ rb_shape_t* rb_shape_get_shape(VALUE obj);
163
+ int rb_shape_frozen_shape_p(rb_shape_t* shape);
164
+ rb_shape_t* rb_shape_transition_shape_frozen(VALUE obj);
165
+ bool rb_shape_transition_shape_remove_ivar(VALUE obj, ID id, rb_shape_t *shape, VALUE * removed);
166
+ rb_shape_t* rb_shape_get_next(rb_shape_t* shape, VALUE obj, ID id);
167
+ rb_shape_t* rb_shape_get_next_no_warnings(rb_shape_t* shape, VALUE obj, ID id);
168
+
169
+ rb_shape_t * rb_shape_rebuild_shape(rb_shape_t * initial_shape, rb_shape_t * dest_shape);
170
+
171
+ static inline uint32_t
172
+ ROBJECT_IV_CAPACITY(VALUE obj)
173
+ {
174
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
175
+ // Asking for capacity doesn't make sense when the object is using
176
+ // a hash table for storing instance variables
177
+ RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
178
+ return rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj))->capacity;
179
+ }
180
+
181
+ static inline st_table *
182
+ ROBJECT_IV_HASH(VALUE obj)
183
+ {
184
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
185
+ RUBY_ASSERT(rb_shape_obj_too_complex(obj));
186
+ return (st_table *)ROBJECT(obj)->as.heap.ivptr;
187
+ }
188
+
189
+ static inline void
190
+ ROBJECT_SET_IV_HASH(VALUE obj, const st_table *tbl)
191
+ {
192
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
193
+ RUBY_ASSERT(rb_shape_obj_too_complex(obj));
194
+ ROBJECT(obj)->as.heap.ivptr = (VALUE *)tbl;
195
+ }
196
+
197
+ size_t rb_id_table_size(const struct rb_id_table *tbl);
198
+
199
+ static inline uint32_t
200
+ ROBJECT_IV_COUNT(VALUE obj)
201
+ {
202
+ if (rb_shape_obj_too_complex(obj)) {
203
+ return (uint32_t)rb_st_table_size(ROBJECT_IV_HASH(obj));
204
+ }
205
+ else {
206
+ RBIMPL_ASSERT_TYPE(obj, RUBY_T_OBJECT);
207
+ RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
208
+ return rb_shape_get_shape_by_id(ROBJECT_SHAPE_ID(obj))->next_iv_index;
209
+ }
210
+ }
211
+
212
+ static inline uint32_t
213
+ RBASIC_IV_COUNT(VALUE obj)
214
+ {
215
+ return rb_shape_get_shape_by_id(rb_shape_get_shape_id(obj))->next_iv_index;
216
+ }
217
+
218
+ rb_shape_t *rb_shape_traverse_from_new_root(rb_shape_t *initial_shape, rb_shape_t *orig_shape);
219
+
220
+ bool rb_shape_set_shape_id(VALUE obj, shape_id_t shape_id);
221
+
222
+ VALUE rb_obj_debug_shape(VALUE self, VALUE obj);
223
+
224
+ // For ext/objspace
225
+ RUBY_SYMBOL_EXPORT_BEGIN
226
+ typedef void each_shape_callback(rb_shape_t * shape, void *data);
227
+ void rb_shape_each_shape(each_shape_callback callback, void *data);
228
+ size_t rb_shape_memsize(rb_shape_t *shape);
229
+ size_t rb_shape_edges_count(rb_shape_t *shape);
230
+ size_t rb_shape_depth(rb_shape_t *shape);
231
+ shape_id_t rb_shape_id(rb_shape_t * shape);
232
+ RUBY_SYMBOL_EXPORT_END
233
+
234
+ #endif