[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / iseq.h
bloba0b59c441f6a8c401faf1bd3b0220b9358dd6c4e
1 #ifndef RUBY_ISEQ_H
2 #define RUBY_ISEQ_H 1
3 /**********************************************************************
5 iseq.h -
7 $Author$
8 created at: 04/01/01 23:36:57 JST
10 Copyright (C) 2004-2008 Koichi Sasada
12 **********************************************************************/
13 #include "internal/gc.h"
14 #include "shape.h"
15 #include "vm_core.h"
16 #include "prism_compile.h"
18 RUBY_EXTERN const int ruby_api_version[];
19 #define ISEQ_MAJOR_VERSION ((unsigned int)ruby_api_version[0])
20 #define ISEQ_MINOR_VERSION ((unsigned int)ruby_api_version[1])
22 #define ISEQ_MBITS_SIZE sizeof(iseq_bits_t)
23 #define ISEQ_MBITS_BITLENGTH (ISEQ_MBITS_SIZE * CHAR_BIT)
24 #define ISEQ_MBITS_SET(buf, i) (buf[(i) / ISEQ_MBITS_BITLENGTH] |= ((iseq_bits_t)1 << ((i) % ISEQ_MBITS_BITLENGTH)))
25 #define ISEQ_MBITS_SET_P(buf, i) ((buf[(i) / ISEQ_MBITS_BITLENGTH] >> ((i) % ISEQ_MBITS_BITLENGTH)) & 0x1)
26 #define ISEQ_MBITS_BUFLEN(size) roomof(size, ISEQ_MBITS_BITLENGTH)
28 #ifndef USE_ISEQ_NODE_ID
29 #define USE_ISEQ_NODE_ID 1
30 #endif
32 #ifndef rb_iseq_t
33 typedef struct rb_iseq_struct rb_iseq_t;
34 #define rb_iseq_t rb_iseq_t
35 #endif
36 typedef void (*rb_iseq_callback)(const rb_iseq_t *, void *);
38 extern const ID rb_iseq_shared_exc_local_tbl[];
40 #define ISEQ_COVERAGE(iseq) ISEQ_BODY(iseq)->variable.coverage
41 #define ISEQ_COVERAGE_SET(iseq, cov) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.coverage, cov)
42 #define ISEQ_LINE_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_LINES)
43 #define ISEQ_BRANCH_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_BRANCHES)
45 #define ISEQ_PC2BRANCHINDEX(iseq) ISEQ_BODY(iseq)->variable.pc2branchindex
46 #define ISEQ_PC2BRANCHINDEX_SET(iseq, h) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.pc2branchindex, h)
48 #define ISEQ_FLIP_CNT(iseq) ISEQ_BODY(iseq)->variable.flip_count
50 #define ISEQ_FROZEN_STRING_LITERAL_ENABLED 1
51 #define ISEQ_FROZEN_STRING_LITERAL_DISABLED 0
52 #define ISEQ_FROZEN_STRING_LITERAL_UNSET -1
54 static inline rb_snum_t
55 ISEQ_FLIP_CNT_INCREMENT(const rb_iseq_t *iseq)
57 rb_snum_t cnt = ISEQ_BODY(iseq)->variable.flip_count;
58 ISEQ_BODY(iseq)->variable.flip_count += 1;
59 return cnt;
62 static inline VALUE *
63 ISEQ_ORIGINAL_ISEQ(const rb_iseq_t *iseq)
65 return ISEQ_BODY(iseq)->variable.original_iseq;
68 static inline void
69 ISEQ_ORIGINAL_ISEQ_CLEAR(const rb_iseq_t *iseq)
71 void *ptr = ISEQ_BODY(iseq)->variable.original_iseq;
72 ISEQ_BODY(iseq)->variable.original_iseq = NULL;
73 ruby_xfree(ptr);
76 static inline VALUE *
77 ISEQ_ORIGINAL_ISEQ_ALLOC(const rb_iseq_t *iseq, long size)
79 return ISEQ_BODY(iseq)->variable.original_iseq =
80 ALLOC_N(VALUE, size);
83 #define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
84 RUBY_EVENT_CLASS | \
85 RUBY_EVENT_END | \
86 RUBY_EVENT_CALL | \
87 RUBY_EVENT_RETURN| \
88 RUBY_EVENT_C_CALL| \
89 RUBY_EVENT_C_RETURN | \
90 RUBY_EVENT_B_CALL | \
91 RUBY_EVENT_B_RETURN | \
92 RUBY_EVENT_RESCUE | \
93 RUBY_EVENT_COVERAGE_LINE| \
94 RUBY_EVENT_COVERAGE_BRANCH)
96 #define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
97 #define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
98 #define ISEQ_TRANSLATED IMEMO_FL_USER3
100 #define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
102 struct iseq_compile_data {
103 /* GC is needed */
104 const VALUE err_info;
105 const VALUE catch_table_ary; /* Array */
107 /* GC is not needed */
108 struct iseq_label_data *start_label;
109 struct iseq_label_data *end_label;
110 struct iseq_label_data *redo_label;
111 const rb_iseq_t *current_block;
112 struct iseq_compile_data_ensure_node_stack *ensure_node_stack;
113 struct {
114 struct iseq_compile_data_storage *storage_head;
115 struct iseq_compile_data_storage *storage_current;
116 } node;
117 struct {
118 struct iseq_compile_data_storage *storage_head;
119 struct iseq_compile_data_storage *storage_current;
120 } insn;
121 bool in_rescue;
122 int loopval_popped; /* used by NODE_BREAK */
123 int last_line;
124 int label_no;
125 int node_level;
126 int isolated_depth;
127 unsigned int ci_index;
128 unsigned int ic_index;
129 const rb_compile_option_t *option;
130 struct rb_id_table *ivar_cache_table;
131 const struct rb_builtin_function *builtin_function_table;
132 const NODE *root_node;
133 bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
134 #if OPT_SUPPORT_JOKE
135 st_table *labels_table;
136 #endif
139 static inline struct iseq_compile_data *
140 ISEQ_COMPILE_DATA(const rb_iseq_t *iseq)
142 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
143 return iseq->aux.compile_data;
145 else {
146 return NULL;
150 static inline void
151 ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
153 iseq->aux.compile_data = ZALLOC(struct iseq_compile_data);
154 iseq->flags |= ISEQ_USE_COMPILE_DATA;
157 static inline void
158 ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
160 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
161 iseq->aux.compile_data = NULL;
164 static inline rb_iseq_t *
165 iseq_imemo_alloc(void)
167 return IMEMO_NEW(rb_iseq_t, imemo_iseq, 0);
170 VALUE rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt);
171 void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
172 const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
173 const rb_iseq_t *rb_iseq_ibf_load_bytes(const char *cstr, size_t);
174 VALUE rb_iseq_ibf_load_extra_data(VALUE str);
175 void rb_iseq_init_trace(rb_iseq_t *iseq);
176 int rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line, bool target_bmethod);
177 int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval);
178 const rb_iseq_t *rb_iseq_load_iseq(VALUE fname);
179 int rb_iseq_opt_frozen_string_literal(void);
181 #if VM_INSN_INFO_TABLE_IMPL == 2
182 unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
183 #endif
185 int rb_vm_insn_addr2opcode(const void *addr);
187 RUBY_SYMBOL_EXPORT_BEGIN
189 /* compile.c */
190 VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
191 VALUE rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc);
192 VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
193 void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,
194 VALUE locals, VALUE args,
195 VALUE exception, VALUE body);
196 void rb_iseq_mark_and_pin_insn_storage(struct iseq_compile_data_storage *arena);
198 VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
199 VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
200 unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos);
201 #ifdef USE_ISEQ_NODE_ID
202 int rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos);
203 #endif
204 void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events);
205 void rb_iseq_trace_set_all(rb_event_flag_t turnon_events);
206 void rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq);
208 struct rb_iseq_constant_body *rb_iseq_constant_body_alloc(void);
209 VALUE rb_iseqw_new(const rb_iseq_t *iseq);
210 const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw);
212 VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */
213 int rb_iseq_from_eval_p(const rb_iseq_t *iseq);
214 VALUE rb_iseq_type(const rb_iseq_t *iseq);
215 VALUE rb_iseq_label(const rb_iseq_t *iseq);
216 VALUE rb_iseq_base_label(const rb_iseq_t *iseq);
217 VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq);
218 VALUE rb_iseq_method_name(const rb_iseq_t *iseq);
219 void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column);
221 void rb_iseq_remove_coverage_all(void);
223 /* proc.c */
224 const rb_iseq_t *rb_method_iseq(VALUE body);
225 const rb_iseq_t *rb_proc_get_iseq(VALUE proc, int *is_proc);
227 struct rb_compile_option_struct {
228 unsigned int inline_const_cache: 1;
229 unsigned int peephole_optimization: 1;
230 unsigned int tailcall_optimization: 1;
231 unsigned int specialized_instruction: 1;
232 unsigned int operands_unification: 1;
233 unsigned int instructions_unification: 1;
234 signed int frozen_string_literal: 2; /* -1: not specified, 0: false, 1: true */
235 unsigned int debug_frozen_string_literal: 1;
236 unsigned int coverage_enabled: 1;
237 int debug_level;
240 struct iseq_insn_info_entry {
241 int line_no;
242 #ifdef USE_ISEQ_NODE_ID
243 int node_id;
244 #endif
245 rb_event_flag_t events;
249 * iseq type:
250 * CATCH_TYPE_RESCUE, CATCH_TYPE_ENSURE:
251 * use iseq as continuation.
253 * CATCH_TYPE_BREAK (iter):
254 * use iseq as key.
256 * CATCH_TYPE_BREAK (while), CATCH_TYPE_RETRY,
257 * CATCH_TYPE_REDO, CATCH_TYPE_NEXT:
258 * NULL.
260 enum rb_catch_type {
261 CATCH_TYPE_RESCUE = INT2FIX(1),
262 CATCH_TYPE_ENSURE = INT2FIX(2),
263 CATCH_TYPE_RETRY = INT2FIX(3),
264 CATCH_TYPE_BREAK = INT2FIX(4),
265 CATCH_TYPE_REDO = INT2FIX(5),
266 CATCH_TYPE_NEXT = INT2FIX(6)
269 struct iseq_catch_table_entry {
270 enum rb_catch_type type;
271 rb_iseq_t *iseq;
273 unsigned int start;
274 unsigned int end;
275 unsigned int cont;
276 unsigned int sp;
279 RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN()
280 struct iseq_catch_table {
281 unsigned int size;
282 struct iseq_catch_table_entry entries[FLEX_ARY_LEN];
283 } RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END();
285 static inline int
286 iseq_catch_table_bytes(int n)
288 enum {
289 catch_table_entry_size = sizeof(struct iseq_catch_table_entry),
290 catch_table_entries_max = (INT_MAX - offsetof(struct iseq_catch_table, entries)) / catch_table_entry_size
292 if (n > catch_table_entries_max) rb_fatal("too large iseq_catch_table - %d", n);
293 return (int)(offsetof(struct iseq_catch_table, entries) +
294 n * catch_table_entry_size);
297 #define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
299 struct iseq_compile_data_storage {
300 struct iseq_compile_data_storage *next;
301 unsigned int pos;
302 unsigned int size;
303 char buff[FLEX_ARY_LEN];
306 /* defined? */
308 enum defined_type {
309 DEFINED_NOT_DEFINED,
310 DEFINED_NIL = 1,
311 DEFINED_IVAR,
312 DEFINED_LVAR,
313 DEFINED_GVAR,
314 DEFINED_CVAR,
315 DEFINED_CONST,
316 DEFINED_METHOD,
317 DEFINED_YIELD,
318 DEFINED_ZSUPER,
319 DEFINED_SELF,
320 DEFINED_TRUE,
321 DEFINED_FALSE,
322 DEFINED_ASGN,
323 DEFINED_EXPR,
324 DEFINED_REF,
325 DEFINED_FUNC,
326 DEFINED_CONST_FROM
329 VALUE rb_iseq_defined_string(enum defined_type type);
331 /* vm.c */
332 VALUE rb_iseq_local_variables(const rb_iseq_t *iseq);
334 attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq);
336 void rb_free_encoded_insn_data(void);
338 RUBY_SYMBOL_EXPORT_END
340 #endif /* RUBY_ISEQ_H */