Ruby  2.0.0p648(2015-12-16revision53162)
vm.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  vm.c -
4 
5  $Author: usa $
6 
7  Copyright (C) 2004-2007 Koichi Sasada
8 
9 **********************************************************************/
10 
11 #include "ruby/ruby.h"
12 #include "ruby/vm.h"
13 #include "ruby/st.h"
14 #include "ruby/encoding.h"
15 #include "internal.h"
16 
17 #include "gc.h"
18 #include "vm_core.h"
19 #include "iseq.h"
20 #include "eval_intern.h"
21 #include "probes.h"
22 #include "probes_helper.h"
23 
24 static inline VALUE *
26 {
27  while (1) {
28  if (VM_EP_LEP_P(ep)) {
29  return ep;
30  }
31  ep = VM_EP_PREV_EP(ep);
32  }
33 }
34 
35 VALUE *
37 {
38  return VM_EP_LEP(ep);
39 }
40 
41 static inline VALUE *
43 {
44  return VM_EP_LEP(cfp->ep);
45 }
46 
47 static inline VALUE *
49 {
50  return VM_EP_PREV_EP((cfp)->ep);
51 }
52 
53 static inline rb_block_t *
55 {
56  VALUE *ep = VM_CF_LEP(cfp);
57  return VM_EP_BLOCK_PTR(ep);
58 }
59 
60 rb_block_t *
62 {
63  return VM_CF_BLOCK_PTR(cfp);
64 }
65 
66 #if VM_COLLECT_USAGE_DETAILS
67 static void vm_collect_usage_operand(int insn, int n, VALUE op);
68 static void vm_collect_usage_insn(int insn);
69 static void vm_collect_usage_register(int reg, int isset);
70 #endif
71 
72 static VALUE
73 vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self, VALUE defined_class,
74  int argc, const VALUE *argv, const rb_block_t *blockptr);
75 
76 #include "vm_insnhelper.h"
77 #include "vm_insnhelper.c"
78 #include "vm_exec.h"
79 #include "vm_exec.c"
80 
81 #include "vm_method.c"
82 #include "vm_eval.c"
83 
84 #include <assert.h>
85 
86 #define BUFSIZE 0x100
87 #define PROCDEBUG 0
88 
93 
99 
100 static void thread_free(void *ptr);
101 
102 void
104 {
106 }
107 
108 static void vm_clear_global_method_cache(void);
109 
110 static void
112 {
113  /* TODO: Clear all inline cache entries in all iseqs.
114  How to iterate all iseqs in sweep phase?
115  rb_objspace_each_objects() doesn't work at sweep phase.
116  */
117 }
118 
119 static void
121 {
125 }
126 
127 void
129 {
131 }
132 
133 /* control stack frame */
134 
135 static void
137 {
138  rb_iseq_t *iseq;
139  GetISeqPtr(iseqval, iseq);
140 
141  if (iseq->type != ISEQ_TYPE_TOP) {
142  rb_raise(rb_eTypeError, "Not a toplevel InstructionSequence");
143  }
144 
145  /* for return */
146  CHECK_VM_STACK_OVERFLOW(th->cfp, iseq->local_size + iseq->stack_max);
149  iseq->iseq_encoded, th->cfp->sp, iseq->local_size, 0);
150 }
151 
152 static void
153 vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const NODE *cref, rb_block_t *base_block)
154 {
155  rb_iseq_t *iseq;
156  GetISeqPtr(iseqval, iseq);
157 
158  CHECK_VM_STACK_OVERFLOW(th->cfp, iseq->local_size + iseq->stack_max);
160  base_block->self, base_block->klass,
161  VM_ENVVAL_PREV_EP_PTR(base_block->ep), iseq->iseq_encoded,
162  th->cfp->sp, iseq->local_size, 0);
163 
164  if (cref) {
165  th->cfp->ep[-1] = (VALUE)cref;
166  }
167 }
168 
169 static void
171 {
172  VALUE toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
173  rb_binding_t *bind;
174  rb_iseq_t *iseq;
175  rb_env_t *env;
176 
177  GetBindingPtr(toplevel_binding, bind);
178  GetEnvPtr(bind->env, env);
179  vm_set_eval_stack(th, iseqval, 0, &env->block);
180 
181  /* save binding */
182  GetISeqPtr(iseqval, iseq);
183  if (bind && iseq->local_size > 0) {
184  bind->env = rb_vm_make_env_object(th, th->cfp);
185  }
186 }
187 
190 {
191  while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
192  if (cfp->iseq) {
193  return (rb_control_frame_t *)cfp;
194  }
196  }
197  return 0;
198 }
199 
202 {
203  while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
204  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
205  return (rb_control_frame_t *)cfp;
206  }
208  }
209  return 0;
210 }
211 
212 static rb_control_frame_t *
214 {
215  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
216  return cfp;
217  }
218 
220 
221  while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
222  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
223  return cfp;
224  }
225 
226  if ((cfp->flag & VM_FRAME_FLAG_PASSED) == 0) {
227  break;
228  }
230  }
231  return 0;
232 }
233 
234 void
236 {
237  rb_thread_t *th = GET_THREAD();
238  const rb_method_entry_t *me = th->cfp->me;
241  vm_pop_frame(th);
242 }
243 
244 void
246 {
247  /* check skipped frame */
248  while (th->cfp != cfp) {
249 #if VMDEBUG
250  printf("skipped frame: %s\n", vm_frametype_name(th->cfp));
251 #endif
252  if (VM_FRAME_TYPE(th->cfp) != VM_FRAME_MAGIC_CFUNC) {
253  vm_pop_frame(th);
254  }
255  else { /* unlikely path */
257  }
258  }
259 }
260 
261 /* obsolete */
262 void
264 {
265  rb_thread_t *th = GET_THREAD();
266  vm_pop_frame(th);
267 }
268 
269 /* at exit */
270 
271 void
273 {
275 }
276 
277 static void
279 {
280  VALUE hook = (VALUE)&vm->at_exit;
281 
282  while (RARRAY_LEN(hook) > 0) {
283  typedef void rb_vm_at_exit_func(rb_vm_t*);
284  rb_vm_at_exit_func *func = (rb_vm_at_exit_func*)rb_ary_pop(hook);
285  (*func)(vm);
286  }
287  rb_ary_free(hook);
288 }
289 
290 /* Env */
291 
292 /*
293  env{
294  env[0] // special (block or prev env)
295  env[1] // env object
296  };
297  */
298 
299 #define ENV_IN_HEAP_P(th, env) \
300  (!((th)->stack <= (env) && (env) < ((th)->stack + (th)->stack_size)))
301 #define ENV_VAL(env) ((env)[1])
302 
303 static void
304 env_mark(void * const ptr)
305 {
306  RUBY_MARK_ENTER("env");
307  if (ptr) {
308  const rb_env_t * const env = ptr;
309 
310  if (env->env) {
311  /* TODO: should mark more restricted range */
312  RUBY_GC_INFO("env->env\n");
313  rb_gc_mark_locations(env->env, env->env + env->env_size);
314  }
315 
316  RUBY_GC_INFO("env->prev_envval\n");
320 
321  if (env->block.iseq) {
322  if (BUILTIN_TYPE(env->block.iseq) == T_NODE) {
324  }
325  else {
327  }
328  }
329  }
330  RUBY_MARK_LEAVE("env");
331 }
332 
333 static void
334 env_free(void * const ptr)
335 {
336  RUBY_FREE_ENTER("env");
337  if (ptr) {
338  rb_env_t *const env = ptr;
340  ruby_xfree(ptr);
341  }
342  RUBY_FREE_LEAVE("env");
343 }
344 
345 static size_t
346 env_memsize(const void *ptr)
347 {
348  if (ptr) {
349  const rb_env_t * const env = ptr;
350  size_t size = sizeof(rb_env_t);
351  if (env->env) {
352  size += env->env_size * sizeof(VALUE);
353  }
354  return size;
355  }
356  return 0;
357 }
358 
360  "VM/env",
362 };
363 
364 static VALUE
366 {
367  VALUE obj;
368  rb_env_t *env;
369  obj = TypedData_Make_Struct(rb_cEnv, rb_env_t, &env_data_type, env);
370  env->env = 0;
371  env->prev_envval = 0;
372  env->block.iseq = 0;
373  return obj;
374 }
375 
376 static VALUE check_env_value(VALUE envval);
377 
378 static int
380 {
381  fprintf(stderr, "---\n");
382  fprintf(stderr, "envptr: %p\n", (void *)&env->block.ep[0]);
383  fprintf(stderr, "envval: %10p ", (void *)env->block.ep[1]);
384  dp(env->block.ep[1]);
385  fprintf(stderr, "ep: %10p\n", (void *)env->block.ep);
386  if (env->prev_envval) {
387  fprintf(stderr, ">>\n");
389  fprintf(stderr, "<<\n");
390  }
391  return 1;
392 }
393 
394 static VALUE
396 {
397  rb_env_t *env;
398  GetEnvPtr(envval, env);
399 
400  if (check_env(env)) {
401  return envval;
402  }
403  rb_bug("invalid env");
404  return Qnil; /* unreachable */
405 }
406 
407 static VALUE
409  VALUE *envptr, VALUE * const endptr)
410 {
411  VALUE envval, penvval = 0;
412  rb_env_t *env;
413  VALUE *nenvptr;
414  int i, local_size;
415 
416  if (ENV_IN_HEAP_P(th, envptr)) {
417  return ENV_VAL(envptr);
418  }
419 
420  if (envptr != endptr) {
421  VALUE *penvptr = GC_GUARDED_PTR_REF(*envptr);
422  rb_control_frame_t *pcfp = cfp;
423 
424  if (ENV_IN_HEAP_P(th, penvptr)) {
425  penvval = ENV_VAL(penvptr);
426  }
427  else {
428  while (pcfp->ep != penvptr) {
429  pcfp++;
430  if (pcfp->ep == 0) {
431  SDR();
432  rb_bug("invalid ep");
433  }
434  }
435  penvval = vm_make_env_each(th, pcfp, penvptr, endptr);
436  *envptr = VM_ENVVAL_PREV_EP_PTR(pcfp->ep);
437  }
438  }
439 
440  /* allocate env */
441  envval = env_alloc();
442  GetEnvPtr(envval, env);
443 
444  if (!RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
445  local_size = 2;
446  }
447  else {
448  local_size = cfp->iseq->local_size;
449  }
450 
451  env->env_size = local_size + 1 + 1;
452  env->local_size = local_size;
453  env->env = ALLOC_N(VALUE, env->env_size);
454  env->prev_envval = penvval;
455 
456  for (i = 0; i <= local_size; i++) {
457  env->env[i] = envptr[-local_size + i];
458 #if 0
459  fprintf(stderr, "%2d ", &envptr[-local_size + i] - th->stack); dp(env->env[i]);
460  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
461  /* clear value stack for GC */
462  envptr[-local_size + i] = 0;
463  }
464 #endif
465  }
466 
467  *envptr = envval; /* GC mark */
468  nenvptr = &env->env[i - 1];
469  nenvptr[1] = envval; /* frame self */
470 
471  /* reset ep in cfp */
472  cfp->ep = nenvptr;
473 
474  /* as Binding */
475  env->block.self = cfp->self;
476  env->block.ep = cfp->ep;
477  env->block.iseq = cfp->iseq;
478 
479  if (!RUBY_VM_NORMAL_ISEQ_P(cfp->iseq)) {
480  /* TODO */
481  env->block.iseq = 0;
482  }
483  return envval;
484 }
485 
486 static int
488 {
489  int i;
490  if (!iseq) return 0;
491  for (i = 0; i < iseq->local_table_size; i++) {
492  ID lid = iseq->local_table[i];
493  if (rb_is_local_id(lid)) {
494  rb_ary_push(ary, ID2SYM(lid));
495  }
496  }
497  return 1;
498 }
499 
500 static int
502 {
503 
504  while (collect_local_variables_in_iseq(env->block.iseq, ary),
505  env->prev_envval) {
506  GetEnvPtr(env->prev_envval, env);
507  }
508  return 0;
509 }
510 
511 static int
513 {
514  if (ENV_IN_HEAP_P(th, ep)) {
515  rb_env_t *env;
516  GetEnvPtr(ENV_VAL(ep), env);
518  return 1;
519  }
520  else {
521  return 0;
522  }
523 }
524 
525 static void vm_rewrite_ep_in_errinfo(rb_thread_t *th);
527 static VALUE vm_make_env_object(rb_thread_t * th, rb_control_frame_t *cfp, VALUE *blockprocptr);
528 
529 VALUE
531 {
532  VALUE blockprocval;
533  return vm_make_env_object(th, cfp, &blockprocval);
534 }
535 
536 static VALUE
538 {
539  VALUE envval;
540  VALUE *lep = VM_CF_LEP(cfp);
541  rb_block_t *blockptr = VM_EP_BLOCK_PTR(lep);
542 
543  if (blockptr) {
544  VALUE blockprocval = vm_make_proc_from_block(th, blockptr);
545  rb_proc_t *p;
546  GetProcPtr(blockprocval, p);
547  lep[0] = VM_ENVVAL_BLOCK_PTR(&p->block);
548  *blockprocptr = blockprocval;
549  }
550 
551  envval = vm_make_env_each(th, cfp, cfp->ep, lep);
553 
554  if (PROCDEBUG) {
555  check_env_value(envval);
556  }
557 
558  return envval;
559 }
560 
561 static void
563 {
564  rb_control_frame_t *cfp = th->cfp;
565  while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)) {
566  /* rewrite ep in errinfo to point to heap */
567  if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) &&
568  (cfp->iseq->type == ISEQ_TYPE_RESCUE ||
569  cfp->iseq->type == ISEQ_TYPE_ENSURE)) {
570  VALUE errinfo = cfp->ep[-2]; /* #$! */
571  if (RB_TYPE_P(errinfo, T_NODE)) {
572  VALUE *escape_ep = GET_THROWOBJ_CATCH_POINT(errinfo);
573  if (! ENV_IN_HEAP_P(th, escape_ep)) {
574  VALUE epval = *escape_ep;
575  if (!SPECIAL_CONST_P(epval) && RBASIC(epval)->klass == rb_cEnv) {
576  rb_env_t *epenv;
577  GetEnvPtr(epval, epenv);
578  SET_THROWOBJ_CATCH_POINT(errinfo, (VALUE)(epenv->env + epenv->local_size));
579  }
580  }
581  }
582  }
584  }
585 }
586 
587 void
589 {
590  rb_control_frame_t *cfp = th->cfp;
591  while ((cfp = rb_vm_get_binding_creatable_next_cfp(th, cfp)) != 0) {
592  rb_vm_make_env_object(th, cfp);
594  }
595 }
596 
597 /* Proc */
598 
599 static VALUE
601 {
602  if (!block->proc) {
603  block->proc = rb_vm_make_proc(th, block, rb_cProc);
604  }
605  return block->proc;
606 }
607 
608 VALUE
609 rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass)
610 {
611  VALUE procval, envval, blockprocval = 0;
612  rb_proc_t *proc;
614 
615  if (block->proc) {
616  rb_bug("rb_vm_make_proc: Proc value is already created.");
617  }
618 
619  envval = vm_make_env_object(th, cfp, &blockprocval);
620 
621  if (PROCDEBUG) {
622  check_env_value(envval);
623  }
624  procval = rb_proc_alloc(klass);
625  GetProcPtr(procval, proc);
626  proc->blockprocval = blockprocval;
627  proc->block.self = block->self;
628  proc->block.klass = block->klass;
629  proc->block.ep = block->ep;
630  proc->block.iseq = block->iseq;
631  proc->block.proc = procval;
632  proc->envval = envval;
633  proc->safe_level = th->safe_level;
634 
635  if (VMDEBUG) {
636  if (th->stack < block->ep && block->ep < th->stack + th->stack_size) {
637  rb_bug("invalid ptr: block->ep");
638  }
639  }
640 
641  return procval;
642 }
643 
644 /* Binding */
645 
646 VALUE
648 {
650  rb_control_frame_t *ruby_level_cfp = rb_vm_get_ruby_level_next_cfp(th, src_cfp);
651  VALUE bindval, envval;
652  rb_binding_t *bind;
653  VALUE blockprocval = 0;
654 
655  if (cfp == 0 || ruby_level_cfp == 0) {
656  rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
657  }
658 
659  while (1) {
660  envval = vm_make_env_object(th, cfp, &blockprocval);
661  if (cfp == ruby_level_cfp) {
662  break;
663  }
665  }
666 
667  bindval = rb_binding_alloc(rb_cBinding);
668  GetBindingPtr(bindval, bind);
669  bind->env = envval;
670  bind->path = ruby_level_cfp->iseq->location.path;
671  bind->blockprocval = blockprocval;
672  bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
673 
674  return bindval;
675 }
676 
677 /* C -> Ruby: block */
678 
679 static inline VALUE
681  VALUE self, int argc, const VALUE *argv,
682  const rb_block_t *blockptr, const NODE *cref,
683  VALUE defined_class)
684 {
685  if (SPECIAL_CONST_P(block->iseq))
686  return Qnil;
687  else if (BUILTIN_TYPE(block->iseq) != T_NODE) {
688  const rb_iseq_t *iseq = block->iseq;
689  const rb_control_frame_t *cfp;
690  int i, opt_pc, arg_size = iseq->arg_size;
691  int type = block_proc_is_lambda(block->proc) ?
693 
694  cfp = th->cfp;
695  CHECK_VM_STACK_OVERFLOW(cfp, argc + iseq->stack_max);
696 
697  for (i=0; i<argc; i++) {
698  cfp->sp[i] = argv[i];
699  }
700 
701  opt_pc = vm_yield_setup_args(th, iseq, argc, cfp->sp, blockptr,
702  type == VM_FRAME_MAGIC_LAMBDA);
703 
704  vm_push_frame(th, iseq, type | VM_FRAME_FLAG_FINISH,
705  self, defined_class,
706  VM_ENVVAL_PREV_EP_PTR(block->ep),
707  iseq->iseq_encoded + opt_pc,
708  cfp->sp + arg_size, iseq->local_size - arg_size,
709  th->passed_me);
710  th->passed_me = 0;
711 
712  if (cref) {
713  th->cfp->ep[-1] = (VALUE)cref;
714  }
715 
716  return vm_exec(th);
717  }
718  else {
719  return vm_yield_with_cfunc(th, block, self, argc, argv, blockptr);
720  }
721 }
722 
723 static inline const rb_block_t *
725 {
726  const rb_block_t *blockptr = VM_CF_BLOCK_PTR(th->cfp);
727 
728  if (blockptr == 0) {
729  rb_vm_localjump_error("no block given", Qnil, 0);
730  }
731 
732  return blockptr;
733 }
734 
735 static inline VALUE
736 vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const NODE *cref)
737 {
738  const rb_block_t *blockptr = check_block(th);
739  return invoke_block_from_c(th, blockptr, blockptr->self, argc, argv, 0, cref,
740  blockptr->klass);
741 }
742 
743 static inline VALUE
744 vm_yield(rb_thread_t *th, int argc, const VALUE *argv)
745 {
746  const rb_block_t *blockptr = check_block(th);
747  return invoke_block_from_c(th, blockptr, blockptr->self, argc, argv, 0, 0,
748  blockptr->klass);
749 }
750 
751 static VALUE
752 vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self, VALUE defined_class,
753  int argc, const VALUE *argv, const rb_block_t *blockptr)
754 {
755  VALUE val = Qundef;
756  int state;
757  volatile int stored_safe = th->safe_level;
758 
759  TH_PUSH_TAG(th);
760  if ((state = EXEC_TAG()) == 0) {
761  if (!proc->is_from_method) {
762  th->safe_level = proc->safe_level;
763  }
764  val = invoke_block_from_c(th, &proc->block, self, argc, argv, blockptr, 0,
765  defined_class);
766  }
767  TH_POP_TAG();
768 
769  if (!proc->is_from_method) {
770  th->safe_level = stored_safe;
771  }
772 
773  if (state) {
774  JUMP_TAG(state);
775  }
776  return val;
777 }
778 
779 VALUE
781  int argc, const VALUE *argv, const rb_block_t *blockptr)
782 {
783  return vm_invoke_proc(th, proc, proc->block.self, proc->block.klass,
784  argc, argv, blockptr);
785 }
786 
787 /* special variable */
788 
789 static rb_control_frame_t *
791 {
792  while (cfp->pc == 0) {
795  return 0;
796  }
797  }
798  return cfp;
799 }
800 
801 static VALUE
803 {
804  cfp = vm_normal_frame(th, cfp);
805  return lep_svar_get(th, cfp ? VM_CF_LEP(cfp) : 0, key);
806 }
807 
808 static void
810 {
811  cfp = vm_normal_frame(th, cfp);
812  lep_svar_set(th, cfp ? VM_CF_LEP(cfp) : 0, key, val);
813 }
814 
815 static VALUE
817 {
818  rb_thread_t *th = GET_THREAD();
819  return vm_cfp_svar_get(th, th->cfp, key);
820 }
821 
822 static void
824 {
825  rb_thread_t *th = GET_THREAD();
826  vm_cfp_svar_set(th, th->cfp, key, val);
827 }
828 
829 VALUE
831 {
832  return vm_svar_get(1);
833 }
834 
835 void
837 {
838  vm_svar_set(1, val);
839 }
840 
841 VALUE
843 {
844  return vm_svar_get(0);
845 }
846 
847 void
849 {
850  vm_svar_set(0, val);
851 }
852 
853 /* misc */
854 
855 VALUE
857 {
858  rb_thread_t *th = GET_THREAD();
860 
861  if (cfp) {
862  return cfp->iseq->location.path;
863  }
864  else {
865  return Qnil;
866  }
867 }
868 
869 const char *
871 {
872  rb_thread_t *th = GET_THREAD();
874 
875  if (cfp) {
876  return RSTRING_PTR(cfp->iseq->location.path);
877  }
878  else {
879  return 0;
880  }
881 }
882 
883 int
885 {
886  rb_thread_t *th = GET_THREAD();
888 
889  if (cfp) {
890  return rb_vm_get_sourceline(cfp);
891  }
892  else {
893  return 0;
894  }
895 }
896 
897 NODE *
899 {
900  rb_thread_t *th = GET_THREAD();
902 
903  if (cfp == 0) {
904  return NULL;
905  }
906  return rb_vm_get_cref(cfp->iseq, cfp->ep);
907 }
908 
909 #if 0
910 void
911 debug_cref(NODE *cref)
912 {
913  while (cref) {
914  dp(cref->nd_clss);
915  printf("%ld\n", cref->nd_visi);
916  cref = cref->nd_next;
917  }
918 }
919 #endif
920 
921 VALUE
923 {
924  rb_thread_t *th = GET_THREAD();
926 
927  if (cfp == 0) {
928  rb_raise(rb_eRuntimeError, "Can't call on top of Fiber or Thread");
929  }
930  return vm_get_cbase(cfp->iseq, cfp->ep);
931 }
932 
933 /* jump */
934 
935 static VALUE
936 make_localjump_error(const char *mesg, VALUE value, int reason)
937 {
938  extern VALUE rb_eLocalJumpError;
939  VALUE exc = rb_exc_new2(rb_eLocalJumpError, mesg);
940  ID id;
941 
942  switch (reason) {
943  case TAG_BREAK:
944  CONST_ID(id, "break");
945  break;
946  case TAG_REDO:
947  CONST_ID(id, "redo");
948  break;
949  case TAG_RETRY:
950  CONST_ID(id, "retry");
951  break;
952  case TAG_NEXT:
953  CONST_ID(id, "next");
954  break;
955  case TAG_RETURN:
956  CONST_ID(id, "return");
957  break;
958  default:
959  CONST_ID(id, "noreason");
960  break;
961  }
962  rb_iv_set(exc, "@exit_value", value);
963  rb_iv_set(exc, "@reason", ID2SYM(id));
964  return exc;
965 }
966 
967 void
968 rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
969 {
970  VALUE exc = make_localjump_error(mesg, value, reason);
971  rb_exc_raise(exc);
972 }
973 
974 VALUE
976 {
977  VALUE result = Qnil;
978 
979  if (val == Qundef) {
980  val = GET_THREAD()->tag->retval;
981  }
982  switch (state) {
983  case 0:
984  break;
985  case TAG_RETURN:
986  result = make_localjump_error("unexpected return", val, state);
987  break;
988  case TAG_BREAK:
989  result = make_localjump_error("unexpected break", val, state);
990  break;
991  case TAG_NEXT:
992  result = make_localjump_error("unexpected next", val, state);
993  break;
994  case TAG_REDO:
995  result = make_localjump_error("unexpected redo", Qnil, state);
996  break;
997  case TAG_RETRY:
998  result = make_localjump_error("retry outside of rescue clause", Qnil, state);
999  break;
1000  default:
1001  break;
1002  }
1003  return result;
1004 }
1005 
1006 void
1008 {
1010  if (!NIL_P(exc)) rb_exc_raise(exc);
1011  JUMP_TAG(state);
1012 }
1013 
1014 NORETURN(static void vm_iter_break(rb_thread_t *th, VALUE val));
1015 
1016 static void
1018 {
1019  rb_control_frame_t *cfp = th->cfp;
1020  VALUE *ep = VM_CF_PREV_EP(cfp);
1021 
1022  th->state = TAG_BREAK;
1023  th->errinfo = (VALUE)NEW_THROW_OBJECT(val, (VALUE)ep, TAG_BREAK);
1024  TH_JUMP_TAG(th, TAG_BREAK);
1025 }
1026 
1027 void
1029 {
1031 }
1032 
1033 void
1035 {
1036  vm_iter_break(GET_THREAD(), val);
1037 }
1038 
1039 /* optimization: redefine management */
1040 
1042 
1043 static int
1045 {
1046  if (klass == rb_cFixnum) return FIXNUM_REDEFINED_OP_FLAG;
1047  if (klass == rb_cFloat) return FLOAT_REDEFINED_OP_FLAG;
1048  if (klass == rb_cString) return STRING_REDEFINED_OP_FLAG;
1049  if (klass == rb_cArray) return ARRAY_REDEFINED_OP_FLAG;
1050  if (klass == rb_cHash) return HASH_REDEFINED_OP_FLAG;
1051  if (klass == rb_cBignum) return BIGNUM_REDEFINED_OP_FLAG;
1052  if (klass == rb_cSymbol) return SYMBOL_REDEFINED_OP_FLAG;
1053  if (klass == rb_cTime) return TIME_REDEFINED_OP_FLAG;
1054  return 0;
1055 }
1056 
1057 static void
1059 {
1060  st_data_t bop;
1061  if (!me->def || me->def->type == VM_METHOD_TYPE_CFUNC) {
1062  if (st_lookup(vm_opt_method_table, (st_data_t)me, &bop)) {
1063  int flag = vm_redefinition_check_flag(klass);
1064 
1065  ruby_vm_redefined_flag[bop] |= flag;
1066  }
1067  }
1068 }
1069 
1070 static int
1072 {
1073  ID mid = (ID)key;
1074  rb_method_entry_t *me = (rb_method_entry_t *)value;
1075  VALUE klass = (VALUE)data;
1076  rb_method_entry_t *newme = rb_method_entry(klass, mid, NULL);
1077 
1078  if (newme != me)
1080  return ST_CONTINUE;
1081 }
1082 
1083 void
1085 {
1086  if (!vm_redefinition_check_flag(klass)) return;
1088  (st_data_t)klass);
1089 }
1090 
1091 static void
1092 add_opt_method(VALUE klass, ID mid, VALUE bop)
1093 {
1094  rb_method_entry_t *me;
1095  if (st_lookup(RCLASS_M_TBL(klass), mid, (void *)&me) && me->def &&
1096  me->def->type == VM_METHOD_TYPE_CFUNC) {
1097  st_insert(vm_opt_method_table, (st_data_t)me, (st_data_t)bop);
1098  }
1099  else {
1100  rb_bug("undefined optimized method: %s", rb_id2name(mid));
1101  }
1102 }
1103 
1104 static void
1106 {
1107  ID mid;
1108  VALUE bop;
1109 
1110  vm_opt_method_table = st_init_numtable();
1111 
1112 #define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
1113 #define C(k) add_opt_method(rb_c##k, mid, bop)
1114  OP(PLUS, PLUS), (C(Fixnum), C(Float), C(String), C(Array));
1115  OP(MINUS, MINUS), (C(Fixnum), C(Float));
1116  OP(MULT, MULT), (C(Fixnum), C(Float));
1117  OP(DIV, DIV), (C(Fixnum), C(Float));
1118  OP(MOD, MOD), (C(Fixnum), C(Float));
1119  OP(Eq, EQ), (C(Fixnum), C(Float), C(String));
1120  OP(Eqq, EQQ), (C(Fixnum), C(Bignum), C(Float), C(Symbol), C(String));
1121  OP(LT, LT), (C(Fixnum), C(Float));
1122  OP(LE, LE), (C(Fixnum), C(Float));
1123  OP(GT, GT), (C(Fixnum), C(Float));
1124  OP(GE, GE), (C(Fixnum), C(Float));
1125  OP(LTLT, LTLT), (C(String), C(Array));
1126  OP(AREF, AREF), (C(Array), C(Hash));
1127  OP(ASET, ASET), (C(Array), C(Hash));
1128  OP(Length, LENGTH), (C(Array), C(String), C(Hash));
1129  OP(Size, SIZE), (C(Array), C(String), C(Hash));
1130  OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
1131  OP(Succ, SUCC), (C(Fixnum), C(String), C(Time));
1132 #undef C
1133 #undef OP
1134 }
1135 
1136 /* for vm development */
1137 
1138 #if VMDEBUG
1139 static const char *
1140 vm_frametype_name(const rb_control_frame_t *cfp)
1141 {
1142  switch (VM_FRAME_TYPE(cfp)) {
1143  case VM_FRAME_MAGIC_METHOD: return "method";
1144  case VM_FRAME_MAGIC_BLOCK: return "block";
1145  case VM_FRAME_MAGIC_CLASS: return "class";
1146  case VM_FRAME_MAGIC_TOP: return "top";
1147  case VM_FRAME_MAGIC_CFUNC: return "cfunc";
1148  case VM_FRAME_MAGIC_PROC: return "proc";
1149  case VM_FRAME_MAGIC_IFUNC: return "ifunc";
1150  case VM_FRAME_MAGIC_EVAL: return "eval";
1151  case VM_FRAME_MAGIC_LAMBDA: return "lambda";
1152  case VM_FRAME_MAGIC_RESCUE: return "rescue";
1153  default:
1154  rb_bug("unknown frame");
1155  }
1156 }
1157 #endif
1158 
1159 /* evaluator body */
1160 
1161 /* finish
1162  VMe (h1) finish
1163  VM finish F1 F2
1164  cfunc finish F1 F2 C1
1165  rb_funcall finish F1 F2 C1
1166  VMe finish F1 F2 C1
1167  VM finish F1 F2 C1 F3
1168 
1169  F1 - F3 : pushed by VM
1170  C1 : pushed by send insn (CFUNC)
1171 
1172  struct CONTROL_FRAME {
1173  VALUE *pc; // cfp[0], program counter
1174  VALUE *sp; // cfp[1], stack pointer
1175  VALUE *bp; // cfp[2], base pointer
1176  rb_iseq_t *iseq; // cfp[3], iseq
1177  VALUE flag; // cfp[4], magic
1178  VALUE self; // cfp[5], self
1179  VALUE *ep; // cfp[6], env pointer
1180  rb_iseq_t * block_iseq; // cfp[7], block iseq
1181  VALUE proc; // cfp[8], always 0
1182  };
1183 
1184  struct BLOCK {
1185  VALUE self;
1186  VALUE *ep;
1187  rb_iseq_t *block_iseq;
1188  VALUE proc;
1189  };
1190 
1191  struct METHOD_CONTROL_FRAME {
1192  rb_control_frame_t frame;
1193  };
1194 
1195  struct METHOD_FRAME {
1196  VALUE arg0;
1197  ...
1198  VALUE argM;
1199  VALUE param0;
1200  ...
1201  VALUE paramN;
1202  VALUE cref;
1203  VALUE special; // lep [1]
1204  struct block_object *block_ptr | 0x01; // lep [0]
1205  };
1206 
1207  struct BLOCK_CONTROL_FRAME {
1208  rb_control_frame_t frame;
1209  };
1210 
1211  struct BLOCK_FRAME {
1212  VALUE arg0;
1213  ...
1214  VALUE argM;
1215  VALUE param0;
1216  ...
1217  VALUE paramN;
1218  VALUE cref;
1219  VALUE *(prev_ptr | 0x01); // ep[0]
1220  };
1221 
1222  struct CLASS_CONTROL_FRAME {
1223  rb_control_frame_t frame;
1224  };
1225 
1226  struct CLASS_FRAME {
1227  VALUE param0;
1228  ...
1229  VALUE paramN;
1230  VALUE cref;
1231  VALUE prev_ep; // for frame jump
1232  };
1233 
1234  struct C_METHOD_CONTROL_FRAME {
1235  VALUE *pc; // 0
1236  VALUE *sp; // stack pointer
1237  VALUE *bp; // base pointer (used in exception)
1238  rb_iseq_t *iseq; // cmi
1239  VALUE magic; // C_METHOD_FRAME
1240  VALUE self; // ?
1241  VALUE *ep; // ep == lep
1242  rb_iseq_t * block_iseq; //
1243  VALUE proc; // always 0
1244  };
1245 
1246  struct C_BLOCK_CONTROL_FRAME {
1247  VALUE *pc; // point only "finish" insn
1248  VALUE *sp; // sp
1249  rb_iseq_t *iseq; // ?
1250  VALUE magic; // C_METHOD_FRAME
1251  VALUE self; // needed?
1252  VALUE *ep; // ep
1253  rb_iseq_t * block_iseq; // 0
1254  };
1255  */
1256 
1257 
1258 static VALUE
1260 {
1261  int state;
1262  VALUE result, err;
1263  VALUE initial = 0;
1264  VALUE *escape_ep = NULL;
1265 
1266  TH_PUSH_TAG(th);
1267  _tag.retval = Qnil;
1268  if ((state = EXEC_TAG()) == 0) {
1269  vm_loop_start:
1270  result = vm_exec_core(th, initial);
1271  if ((state = th->state) != 0) {
1272  err = result;
1273  th->state = 0;
1274  goto exception_handler;
1275  }
1276  }
1277  else {
1278  int i;
1279  struct iseq_catch_table_entry *entry;
1280  unsigned long epc, cont_pc, cont_sp;
1281  VALUE catch_iseqval;
1282  rb_control_frame_t *cfp;
1283  VALUE type;
1284 
1285  err = th->errinfo;
1286 
1287  exception_handler:
1288  cont_pc = cont_sp = catch_iseqval = 0;
1289 
1290  while (th->cfp->pc == 0 || th->cfp->iseq == 0) {
1292  const rb_method_entry_t *me = th->cfp->me;
1295  }
1297  }
1298 
1299  cfp = th->cfp;
1300  epc = cfp->pc - cfp->iseq->iseq_encoded;
1301 
1302  if (state == TAG_BREAK || state == TAG_RETURN) {
1303  escape_ep = GET_THROWOBJ_CATCH_POINT(err);
1304 
1305  if (cfp->ep == escape_ep) {
1306  if (state == TAG_RETURN) {
1307  if (!VM_FRAME_TYPE_FINISH_P(cfp)) {
1308  SET_THROWOBJ_CATCH_POINT(err, (VALUE)(cfp + 1)->ep);
1309  SET_THROWOBJ_STATE(err, state = TAG_BREAK);
1310  }
1311  else {
1312  for (i = 0; i < cfp->iseq->catch_table_size; i++) {
1313  entry = &cfp->iseq->catch_table[i];
1314  if (entry->start < epc && entry->end >= epc) {
1315  if (entry->type == CATCH_TYPE_ENSURE) {
1316  catch_iseqval = entry->iseq;
1317  cont_pc = entry->cont;
1318  cont_sp = entry->sp;
1319  break;
1320  }
1321  }
1322  }
1323  if (!catch_iseqval) {
1324  result = GET_THROWOBJ_VAL(err);
1325  th->errinfo = Qnil;
1326  vm_pop_frame(th);
1327  goto finish_vme;
1328  }
1329  }
1330  /* through */
1331  }
1332  else {
1333  /* TAG_BREAK */
1334 #if OPT_STACK_CACHING
1335  initial = (GET_THROWOBJ_VAL(err));
1336 #else
1337  *th->cfp->sp++ = (GET_THROWOBJ_VAL(err));
1338 #endif
1339  th->errinfo = Qnil;
1340  goto vm_loop_start;
1341  }
1342  }
1343  }
1344 
1345  if (state == TAG_RAISE) {
1346  for (i = 0; i < cfp->iseq->catch_table_size; i++) {
1347  entry = &cfp->iseq->catch_table[i];
1348  if (entry->start < epc && entry->end >= epc) {
1349 
1350  if (entry->type == CATCH_TYPE_RESCUE ||
1351  entry->type == CATCH_TYPE_ENSURE) {
1352  catch_iseqval = entry->iseq;
1353  cont_pc = entry->cont;
1354  cont_sp = entry->sp;
1355  break;
1356  }
1357  }
1358  }
1359  }
1360  else if (state == TAG_RETRY) {
1361  for (i = 0; i < cfp->iseq->catch_table_size; i++) {
1362  entry = &cfp->iseq->catch_table[i];
1363  if (entry->start < epc && entry->end >= epc) {
1364 
1365  if (entry->type == CATCH_TYPE_ENSURE) {
1366  catch_iseqval = entry->iseq;
1367  cont_pc = entry->cont;
1368  cont_sp = entry->sp;
1369  break;
1370  }
1371  else if (entry->type == CATCH_TYPE_RETRY) {
1372  VALUE *escape_ep;
1373  escape_ep = GET_THROWOBJ_CATCH_POINT(err);
1374  if (cfp->ep == escape_ep) {
1375  cfp->pc = cfp->iseq->iseq_encoded + entry->cont;
1376  th->errinfo = Qnil;
1377  goto vm_loop_start;
1378  }
1379  }
1380  }
1381  }
1382  }
1383  else if (state == TAG_BREAK && ((VALUE)escape_ep & ~0x03) == 0) {
1384  type = CATCH_TYPE_BREAK;
1385 
1386  search_restart_point:
1387  for (i = 0; i < cfp->iseq->catch_table_size; i++) {
1388  entry = &cfp->iseq->catch_table[i];
1389 
1390  if (entry->start < epc && entry->end >= epc) {
1391  if (entry->type == CATCH_TYPE_ENSURE) {
1392  catch_iseqval = entry->iseq;
1393  cont_pc = entry->cont;
1394  cont_sp = entry->sp;
1395  break;
1396  }
1397  else if (entry->type == type) {
1398  cfp->pc = cfp->iseq->iseq_encoded + entry->cont;
1399  cfp->sp = vm_base_ptr(cfp) + entry->sp;
1400 
1401  if (state != TAG_REDO) {
1402 #if OPT_STACK_CACHING
1403  initial = (GET_THROWOBJ_VAL(err));
1404 #else
1405  *th->cfp->sp++ = (GET_THROWOBJ_VAL(err));
1406 #endif
1407  }
1408  th->errinfo = Qnil;
1409  th->state = 0;
1410  goto vm_loop_start;
1411  }
1412  }
1413  }
1414  }
1415  else if (state == TAG_REDO) {
1416  type = CATCH_TYPE_REDO;
1417  goto search_restart_point;
1418  }
1419  else if (state == TAG_NEXT) {
1420  type = CATCH_TYPE_NEXT;
1421  goto search_restart_point;
1422  }
1423  else {
1424  for (i = 0; i < cfp->iseq->catch_table_size; i++) {
1425  entry = &cfp->iseq->catch_table[i];
1426  if (entry->start < epc && entry->end >= epc) {
1427 
1428  if (entry->type == CATCH_TYPE_ENSURE) {
1429  catch_iseqval = entry->iseq;
1430  cont_pc = entry->cont;
1431  cont_sp = entry->sp;
1432  break;
1433  }
1434  }
1435  }
1436  }
1437 
1438  if (catch_iseqval != 0) {
1439  /* found catch table */
1440  rb_iseq_t *catch_iseq;
1441 
1442  /* enter catch scope */
1443  GetISeqPtr(catch_iseqval, catch_iseq);
1444  cfp->sp = vm_base_ptr(cfp) + cont_sp;
1445  cfp->pc = cfp->iseq->iseq_encoded + cont_pc;
1446 
1447  /* push block frame */
1448  cfp->sp[0] = err;
1449  vm_push_frame(th, catch_iseq, VM_FRAME_MAGIC_RESCUE,
1450  cfp->self, cfp->klass,
1451  VM_ENVVAL_PREV_EP_PTR(cfp->ep),
1452  catch_iseq->iseq_encoded,
1453  cfp->sp + 1 /* push value */,
1454  catch_iseq->local_size - 1,
1455  cfp->me);
1456 
1457  state = 0;
1458  th->state = 0;
1459  th->errinfo = Qnil;
1460  goto vm_loop_start;
1461  }
1462  else {
1463  /* skip frame */
1464 
1465  switch (VM_FRAME_TYPE(th->cfp)) {
1466  case VM_FRAME_MAGIC_METHOD:
1469  break;
1470  case VM_FRAME_MAGIC_BLOCK:
1472  break;
1473  case VM_FRAME_MAGIC_CLASS:
1475  break;
1476  }
1477 
1478  if (VM_FRAME_TYPE_FINISH_P(th->cfp)) {
1479  vm_pop_frame(th);
1480  th->errinfo = err;
1481  TH_POP_TAG2();
1482  JUMP_TAG(state);
1483  }
1484  else {
1486  goto exception_handler;
1487  }
1488  }
1489  }
1490  finish_vme:
1491  TH_POP_TAG();
1492  return result;
1493 }
1494 
1495 /* misc */
1496 
1497 VALUE
1499 {
1500  rb_thread_t *th = GET_THREAD();
1501  VALUE val;
1502 
1503  vm_set_top_stack(th, iseqval);
1504 
1505  val = vm_exec(th);
1506  RB_GC_GUARD(iseqval); /* prohibit tail call optimization */
1507  return val;
1508 }
1509 
1510 VALUE
1512 {
1513  rb_thread_t *th = GET_THREAD();
1514  VALUE val;
1515 
1516  vm_set_main_stack(th, iseqval);
1517 
1518  val = vm_exec(th);
1519  RB_GC_GUARD(iseqval); /* prohibit tail call optimization */
1520  return val;
1521 }
1522 
1523 int
1525 {
1526  rb_iseq_t *iseq = cfp->iseq;
1527  if (!iseq && cfp->me) {
1528  if (idp) *idp = cfp->me->def->original_id;
1529  if (klassp) *klassp = cfp->me->klass;
1530  return 1;
1531  }
1532  while (iseq) {
1533  if (RUBY_VM_IFUNC_P(iseq)) {
1534  if (idp) CONST_ID(*idp, "<ifunc>");
1535  if (klassp) *klassp = 0;
1536  return 1;
1537  }
1538  if (iseq->defined_method_id) {
1539  if (idp) *idp = iseq->defined_method_id;
1540  if (klassp) *klassp = iseq->klass;
1541  return 1;
1542  }
1543  if (iseq->local_iseq == iseq) {
1544  break;
1545  }
1546  iseq = iseq->parent_iseq;
1547  }
1548  return 0;
1549 }
1550 
1551 int
1553 {
1554  return rb_vm_control_frame_id_and_class(th->cfp, idp, klassp);
1555 }
1556 
1557 int
1559 {
1560  return rb_thread_method_id_and_class(GET_THREAD(), idp, klassp);
1561 }
1562 
1563 VALUE
1565 {
1566  const rb_control_frame_t *cfp = th->cfp;
1567  VALUE str = Qnil;
1568 
1569  if (cfp->iseq != 0) {
1570  if (cfp->pc != 0) {
1571  rb_iseq_t *iseq = cfp->iseq;
1572  int line_no = rb_vm_get_sourceline(cfp);
1573  char *file = RSTRING_PTR(iseq->location.path);
1574  str = rb_sprintf("%s:%d:in `%s'",
1575  file, line_no, RSTRING_PTR(iseq->location.label));
1576  }
1577  }
1578  else if (cfp->me->def->original_id) {
1579  str = rb_sprintf("`%s#%s' (cfunc)",
1580  rb_class2name(cfp->me->klass),
1581  rb_id2name(cfp->me->def->original_id));
1582  }
1583 
1584  return str;
1585 }
1586 
1587 VALUE
1589  const rb_block_t *blockptr, VALUE filename)
1590 {
1591  rb_thread_t *th = GET_THREAD();
1592  const rb_control_frame_t *reg_cfp = th->cfp;
1593  volatile VALUE iseqval = rb_iseq_new(0, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
1594  VALUE val;
1595 
1597  recv, CLASS_OF(recv), VM_ENVVAL_BLOCK_PTR(blockptr), 0, reg_cfp->sp, 1, 0);
1598 
1599  val = (*func)(arg);
1600 
1601  vm_pop_frame(th);
1602  return val;
1603 }
1604 
1605 /* vm */
1606 
1607 static int
1609 {
1610  VALUE thval = (VALUE)key;
1611  rb_gc_mark(thval);
1612  return ST_CONTINUE;
1613 }
1614 
1616 
1617 void
1618 rb_vm_mark(void *ptr)
1619 {
1620  int i;
1621 
1622  RUBY_MARK_ENTER("vm");
1623  RUBY_GC_INFO("-------------------------------------------------\n");
1624  if (ptr) {
1625  rb_vm_t *vm = ptr;
1626  if (vm->living_threads) {
1628  }
1640 
1641  if (vm->loading_table) {
1643  }
1644  if (vm->loaded_features_index) {
1646  }
1647 
1649 
1650  for (i = 0; i < RUBY_NSIG; i++) {
1651  if (vm->trap_list[i].cmd)
1652  rb_gc_mark(vm->trap_list[i].cmd);
1653  }
1654  if (vm->defined_strings) {
1656  }
1657  }
1658 
1659  RUBY_MARK_LEAVE("vm");
1660 }
1661 
1662 #define vm_free 0
1663 
1664 int
1666 {
1667  RUBY_FREE_ENTER("vm");
1668  if (vm) {
1669  rb_thread_t *th = vm->main_thread;
1670 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
1671  struct rb_objspace *objspace = vm->objspace;
1672 #endif
1674  vm->main_thread = 0;
1675  if (th) {
1677  thread_free(th);
1678  }
1679  if (vm->living_threads) {
1681  vm->living_threads = 0;
1682  }
1683 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
1684  if (objspace) {
1685  rb_objspace_free(objspace);
1686  }
1687 #endif
1689  rb_vm_gvl_destroy(vm);
1690  ruby_xfree(vm);
1691  ruby_current_vm = 0;
1692  }
1693  RUBY_FREE_LEAVE("vm");
1694  return 0;
1695 }
1696 
1697 static size_t
1698 vm_memsize(const void *ptr)
1699 {
1700  if (ptr) {
1701  const rb_vm_t *vmobj = ptr;
1702  size_t size = sizeof(rb_vm_t);
1703  size += st_memsize(vmobj->living_threads);
1704  if (vmobj->defined_strings) {
1705  size += DEFINED_EXPR * sizeof(VALUE);
1706  }
1707  return size;
1708  }
1709  else {
1710  return 0;
1711  }
1712 }
1713 
1715  "VM",
1717 };
1718 
1719 
1720 static VALUE
1722 {
1723  rb_vm_t *vm = GET_VM();
1724  VALUE result = rb_hash_new();
1725 #define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
1726  SET(thread_vm_stack_size);
1727  SET(thread_machine_stack_size);
1728  SET(fiber_vm_stack_size);
1729  SET(fiber_machine_stack_size);
1730 #undef SET
1731  rb_obj_freeze(result);
1732  return result;
1733 }
1734 
1735 static size_t
1736 get_param(const char *name, size_t default_value, size_t min_value)
1737 {
1738  const char *envval;
1739  size_t result = default_value;
1740  if ((envval = getenv(name)) != 0) {
1741  long val = atol(envval);
1742  if (val < (long)min_value) {
1743  val = (long)min_value;
1744  }
1745  result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
1746  }
1747  if (0) fprintf(stderr, "%s: %"PRIdSIZE"\n", name, result); /* debug print */
1748 
1749  return result;
1750 }
1751 
1752 static void
1754 {
1755 #ifdef PTHREAD_STACK_MIN
1756  size_t size = *sizep;
1757 #endif
1758 
1759 #ifdef __SYMBIAN32__
1760  *sizep = 64 * 1024; /* 64KB: Let's be slightly more frugal on mobile platform */
1761 #endif
1762 
1763 #ifdef PTHREAD_STACK_MIN
1764  if (size < PTHREAD_STACK_MIN) {
1765  *sizep = PTHREAD_STACK_MIN * 2;
1766  }
1767 #endif
1768 }
1769 
1770 static void
1772 {
1774  get_param("RUBY_THREAD_VM_STACK_SIZE",
1777 
1779  get_param("RUBY_THREAD_MACHINE_STACK_SIZE",
1782 
1784  get_param("RUBY_FIBER_VM_STACK_SIZE",
1787 
1789  get_param("RUBY_FIBER_MACHINE_STACK_SIZE",
1792 
1793  /* environment dependent check */
1796 }
1797 
1798 static void
1800 {
1801  MEMZERO(vm, rb_vm_t, 1);
1802  vm->src_encoding_index = -1;
1803  vm->at_exit.basic.flags = (T_ARRAY | RARRAY_EMBED_FLAG) & ~RARRAY_EMBED_LEN_MASK; /* len set 0 */
1804  vm->at_exit.basic.klass = 0;
1805 
1807 }
1808 
1809 /* Thread */
1810 
1811 #define USE_THREAD_DATA_RECYCLE 1
1812 
1813 #if USE_THREAD_DATA_RECYCLE
1814 #define RECYCLE_MAX 64
1817 
1818 static VALUE *
1820 {
1821  if (thread_recycle_stack_count) {
1822  /* TODO: check stack size if stack sizes are variable */
1823  return thread_recycle_stack_slot[--thread_recycle_stack_count];
1824  }
1825  else {
1826  return ALLOC_N(VALUE, size);
1827  }
1828 }
1829 
1830 #else
1831 #define thread_recycle_stack(size) ALLOC_N(VALUE, (size))
1832 #endif
1833 
1834 void
1836 {
1837 #if USE_THREAD_DATA_RECYCLE
1838  if (thread_recycle_stack_count < RECYCLE_MAX) {
1839  thread_recycle_stack_slot[thread_recycle_stack_count++] = stack;
1840  return;
1841  }
1842 #endif
1843  ruby_xfree(stack);
1844 }
1845 
1846 #ifdef USE_THREAD_RECYCLE
1847 static rb_thread_t *
1848 thread_recycle_struct(void)
1849 {
1850  void *p = ALLOC_N(rb_thread_t, 1);
1851  memset(p, 0, sizeof(rb_thread_t));
1852  return p;
1853 }
1854 #endif
1855 
1856 void
1858 {
1859  rb_thread_t *th = NULL;
1860  RUBY_MARK_ENTER("thread");
1861  if (ptr) {
1862  th = ptr;
1863  if (th->stack) {
1864  VALUE *p = th->stack;
1865  VALUE *sp = th->cfp->sp;
1866  rb_control_frame_t *cfp = th->cfp;
1867  rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
1868 
1869  while (p < sp) {
1870  rb_gc_mark(*p++);
1871  }
1873 
1874  while (cfp != limit_cfp) {
1875  rb_iseq_t *iseq = cfp->iseq;
1876  rb_gc_mark(cfp->proc);
1877  rb_gc_mark(cfp->self);
1878  rb_gc_mark(cfp->klass);
1879  if (iseq) {
1880  rb_gc_mark(RUBY_VM_NORMAL_ISEQ_P(iseq) ? iseq->self : (VALUE)iseq);
1881  }
1882  if (cfp->me) {
1883  /* TODO: marking `me' can be more sophisticated way */
1884  ((rb_method_entry_t *)cfp->me)->mark = 1;
1885  rb_mark_method_entry(cfp->me);
1886  }
1887  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1888  }
1889  }
1890 
1891  /* mark ruby objects */
1894 
1907 
1909 
1911 
1912  if (GET_THREAD() != th && th->machine_stack_start && th->machine_stack_end) {
1915  (VALUE *)(&th->machine_regs) +
1916  sizeof(th->machine_regs) / sizeof(VALUE));
1917  }
1918 
1920  }
1921 
1922  RUBY_MARK_LEAVE("thread");
1923 }
1924 
1925 static void
1927 {
1928  rb_thread_t *th;
1929  RUBY_FREE_ENTER("thread");
1930 
1931  if (ptr) {
1932  th = ptr;
1933 
1934  if (!th->root_fiber) {
1936  }
1937 
1938  if (th->locking_mutex != Qfalse) {
1939  rb_bug("thread_free: locking_mutex must be NULL (%p:%p)", (void *)th, (void *)th->locking_mutex);
1940  }
1941  if (th->keeping_mutexes != NULL) {
1942  rb_bug("thread_free: keeping_mutexes must be NULL (%p:%p)", (void *)th, (void *)th->keeping_mutexes);
1943  }
1944 
1945  if (th->local_storage) {
1947  }
1948 
1949  if (th->vm && th->vm->main_thread == th) {
1950  RUBY_GC_INFO("main thread\n");
1951  }
1952  else {
1953 #ifdef USE_SIGALTSTACK
1954  if (th->altstack) {
1955  free(th->altstack);
1956  }
1957 #endif
1958  ruby_xfree(ptr);
1959  }
1960  if (ruby_current_thread == th)
1961  ruby_current_thread = NULL;
1962  }
1963  RUBY_FREE_LEAVE("thread");
1964 }
1965 
1966 static size_t
1967 thread_memsize(const void *ptr)
1968 {
1969  if (ptr) {
1970  const rb_thread_t *th = ptr;
1971  size_t size = sizeof(rb_thread_t);
1972 
1973  if (!th->root_fiber) {
1974  size += th->stack_size * sizeof(VALUE);
1975  }
1976  if (th->local_storage) {
1977  size += st_memsize(th->local_storage);
1978  }
1979  return size;
1980  }
1981  else {
1982  return 0;
1983  }
1984 }
1985 
1986 #define thread_data_type ruby_threadptr_data_type
1988  "VM/thread",
1989  {
1991  thread_free,
1993  },
1994 };
1995 
1996 VALUE
1998 {
2000  return Qtrue;
2001  }
2002  else {
2003  return Qfalse;
2004  }
2005 }
2006 
2007 static VALUE
2009 {
2010  VALUE volatile obj;
2011 #ifdef USE_THREAD_RECYCLE
2012  rb_thread_t *th = thread_recycle_struct();
2013  obj = TypedData_Wrap_Struct(klass, &thread_data_type, th);
2014 #else
2015  rb_thread_t *th;
2017 #endif
2018  return obj;
2019 }
2020 
2021 static void
2023 {
2024  th->self = self;
2025 
2026  /* allocate thread stack */
2027 #ifdef USE_SIGALTSTACK
2028  /* altstack of main thread is reallocated in another place */
2029  th->altstack = malloc(rb_sigaltstack_size());
2030 #endif
2031  /* th->stack_size is word number.
2032  * th->vm->default_params.thread_vm_stack_size is byte size.
2033  */
2034  th->stack_size = th->vm->default_params.thread_vm_stack_size / sizeof(VALUE);
2036 
2037  th->cfp = (void *)(th->stack + th->stack_size);
2038 
2039  vm_push_frame(th, 0 /* dummy iseq */, VM_FRAME_MAGIC_TOP | VM_FRAME_FLAG_FINISH,
2040  Qnil /* dummy self */, Qnil /* dummy klass */, VM_ENVVAL_BLOCK_PTR(0), 0 /* dummy pc */, th->stack, 1, 0);
2041 
2042  th->status = THREAD_RUNNABLE;
2043  th->errinfo = Qnil;
2044  th->last_status = Qnil;
2045  th->waiting_fd = -1;
2046  th->root_svar = Qnil;
2047 
2048 #if OPT_CALL_THREADED_CODE
2049  th->retval = Qundef;
2050 #endif
2051 }
2052 
2053 static VALUE
2055 {
2056  rb_thread_t *th;
2057  rb_vm_t *vm = GET_THREAD()->vm;
2058  GetThreadPtr(self, th);
2059 
2060  th->vm = vm;
2061  th_init(th, self);
2062  rb_iv_set(self, "locals", rb_hash_new());
2063 
2064  th->top_wrapper = 0;
2065  th->top_self = rb_vm_top_self();
2066  th->root_svar = Qnil;
2067  return self;
2068 }
2069 
2070 VALUE
2072 {
2073  VALUE self = thread_alloc(klass);
2074  ruby_thread_init(self);
2075  return self;
2076 }
2077 
2078 static void
2080  rb_num_t is_singleton, NODE *cref)
2081 {
2082  VALUE klass = cref->nd_clss;
2083  int noex = (int)cref->nd_visi;
2084  rb_iseq_t *miseq;
2085  GetISeqPtr(iseqval, miseq);
2086 
2087  if (miseq->klass) {
2088  RB_GC_GUARD(iseqval) = rb_iseq_clone(iseqval, 0);
2089  GetISeqPtr(iseqval, miseq);
2090  }
2091 
2092  if (NIL_P(klass)) {
2093  rb_raise(rb_eTypeError, "no class/module to add method");
2094  }
2095 
2096  if (is_singleton) {
2097  klass = rb_singleton_class(obj); /* class and frozen checked in this API */
2098  noex = NOEX_PUBLIC;
2099  }
2100 
2101  /* dup */
2102  COPY_CREF(miseq->cref_stack, cref);
2103  miseq->cref_stack->nd_visi = NOEX_PUBLIC;
2104  miseq->klass = klass;
2105  miseq->defined_method_id = id;
2106  rb_add_method(klass, id, VM_METHOD_TYPE_ISEQ, miseq, noex);
2107 
2108  if (!is_singleton && noex == NOEX_MODFUNC) {
2110  }
2112 }
2113 
2114 #define REWIND_CFP(expr) do { \
2115  rb_thread_t *th__ = GET_THREAD(); \
2116  th__->cfp++; expr; th__->cfp--; \
2117 } while (0)
2118 
2119 static VALUE
2121 {
2122  REWIND_CFP({
2123  vm_define_method(GET_THREAD(), cbase, SYM2ID(sym), iseqval, 0, rb_vm_cref());
2124  });
2125  return Qnil;
2126 }
2127 
2128 static VALUE
2130 {
2131  REWIND_CFP({
2132  vm_define_method(GET_THREAD(), cbase, SYM2ID(sym), iseqval, 1, rb_vm_cref());
2133  });
2134  return Qnil;
2135 }
2136 
2137 static VALUE
2139 {
2140  REWIND_CFP({
2141  rb_alias(cbase, SYM2ID(sym1), SYM2ID(sym2));
2142  });
2143  return Qnil;
2144 }
2145 
2146 static VALUE
2148 {
2149  REWIND_CFP({
2150  rb_alias_variable(SYM2ID(sym1), SYM2ID(sym2));
2151  });
2152  return Qnil;
2153 }
2154 
2155 static VALUE
2157 {
2158  REWIND_CFP({
2159  rb_undef(cbase, SYM2ID(sym));
2161  });
2162  return Qnil;
2163 }
2164 
2165 static VALUE
2167 {
2168  REWIND_CFP({
2169  rb_iseq_t *blockiseq;
2170  rb_block_t *blockptr;
2171  rb_thread_t *th = GET_THREAD();
2173  VALUE proc;
2174 
2175  if (cfp == 0) {
2176  rb_bug("m_core_set_postexe: unreachable");
2177  }
2178 
2179  GetISeqPtr(iseqval, blockiseq);
2180 
2181  blockptr = RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp);
2182  blockptr->iseq = blockiseq;
2183  blockptr->proc = 0;
2184 
2185  proc = rb_vm_make_proc(th, blockptr, rb_cProc);
2187  });
2188  return Qnil;
2189 }
2190 
2191 static VALUE m_core_hash_merge_ary(VALUE self, VALUE hash, VALUE ary);
2192 
2193 static VALUE
2195 {
2196  long i;
2197  assert(argc % 2 == 0);
2198  for (i=0; i<argc; i+=2) {
2199  rb_hash_aset(hash, argv[i], argv[i+1]);
2200  }
2201  return hash;
2202 }
2203 
2204 static VALUE
2206 {
2207  VALUE hash = rb_hash_new();
2208 
2211  }
2212 
2213  return m_core_hash_merge_ary(self, hash, ary);
2214 }
2215 
2216 static VALUE
2218 {
2219  core_hash_merge(hash, RARRAY_LEN(ary), RARRAY_PTR(ary));
2220  return hash;
2221 }
2222 
2223 static VALUE
2225 {
2226  VALUE hash = argv[0];
2227 
2228  core_hash_merge(hash, argc-1, argv+1);
2229 
2230  return hash;
2231 }
2232 
2233 static int
2234 kwmerge_ii(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
2235 {
2236  if (existing) return ST_STOP;
2237  *value = arg;
2238  return ST_CONTINUE;
2239 }
2240 
2241 static int
2243 {
2244  if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
2245  st_update(RHASH_TBL(hash), key, kwmerge_ii, (st_data_t)value);
2246  return ST_CONTINUE;
2247 }
2248 
2249 static VALUE
2251 {
2252  kw = rb_convert_type(kw, T_HASH, "Hash", "to_hash");
2253  rb_hash_foreach(kw, kwmerge_i, hash);
2254  return hash;
2255 }
2256 
2257 extern VALUE *rb_gc_stack_start;
2258 extern size_t rb_gc_stack_maxsize;
2259 #ifdef __ia64
2260 extern VALUE *rb_gc_register_stack_start;
2261 #endif
2262 
2263 /* debug functions */
2264 
2265 /* :nodoc: */
2266 static VALUE
2267 sdr(void)
2268 {
2269  rb_vm_bugreport();
2270  return Qnil;
2271 }
2272 
2273 /* :nodoc: */
2274 static VALUE
2275 nsdr(void)
2276 {
2277  VALUE ary = rb_ary_new();
2278 #if HAVE_BACKTRACE
2279 #include <execinfo.h>
2280 #define MAX_NATIVE_TRACE 1024
2281  static void *trace[MAX_NATIVE_TRACE];
2282  int n = backtrace(trace, MAX_NATIVE_TRACE);
2283  char **syms = backtrace_symbols(trace, n);
2284  int i;
2285 
2286  if (syms == 0) {
2287  rb_memerror();
2288  }
2289 
2290  for (i=0; i<n; i++) {
2291  rb_ary_push(ary, rb_str_new2(syms[i]));
2292  }
2293  free(syms); /* OK */
2294 #endif
2295  return ary;
2296 }
2297 
2298 #if VM_COLLECT_USAGE_DETAILS
2299 static VALUE usage_analysis_insn_stop(VALUE self);
2300 static VALUE usage_analysis_operand_stop(VALUE self);
2301 static VALUE usage_analysis_register_stop(VALUE self);
2302 #endif
2303 
2304 void
2305 Init_VM(void)
2306 {
2307  VALUE opts;
2308  VALUE klass;
2309  VALUE fcore;
2310 
2311  /* ::RubyVM */
2312  rb_cRubyVM = rb_define_class("RubyVM", rb_cObject);
2315 
2316  /* FrozenCore (hidden) */
2317  fcore = rb_class_new(rb_cBasicObject);
2318  RBASIC(fcore)->flags = T_ICLASS;
2319  klass = rb_singleton_class(fcore);
2330  rb_define_method_id(klass, idProc, rb_block_proc, 0);
2331  rb_define_method_id(klass, idLambda, rb_block_lambda, 0);
2332  rb_obj_freeze(fcore);
2334  rb_mRubyVMFrozenCore = fcore;
2335 
2336  /* ::RubyVM::Env */
2339  rb_undef_method(CLASS_OF(rb_cEnv), "new");
2340 
2341  /* ::Thread */
2342  rb_cThread = rb_define_class("Thread", rb_cObject);
2344 
2345 #if VM_COLLECT_USAGE_DETAILS
2346  /* ::RubyVM::USAGE_ANALYSIS_* */
2347  rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_INSN", rb_hash_new());
2348  rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_REGS", rb_hash_new());
2349  rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_INSN_BIGRAM", rb_hash_new());
2350 
2351  rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_STOP", usage_analysis_insn_stop, 0);
2352  rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_STOP", usage_analysis_operand_stop, 0);
2353  rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_STOP", usage_analysis_register_stop, 0);
2354 #endif
2355 
2356  /* ::RubyVM::OPTS, which shows vm build options */
2357  rb_define_const(rb_cRubyVM, "OPTS", opts = rb_ary_new());
2358 
2359 #if OPT_DIRECT_THREADED_CODE
2360  rb_ary_push(opts, rb_str_new2("direct threaded code"));
2361 #elif OPT_TOKEN_THREADED_CODE
2362  rb_ary_push(opts, rb_str_new2("token threaded code"));
2363 #elif OPT_CALL_THREADED_CODE
2364  rb_ary_push(opts, rb_str_new2("call threaded code"));
2365 #endif
2366 
2367 #if OPT_STACK_CACHING
2368  rb_ary_push(opts, rb_str_new2("stack caching"));
2369 #endif
2370 #if OPT_OPERANDS_UNIFICATION
2371  rb_ary_push(opts, rb_str_new2("operands unification]"));
2372 #endif
2373 #if OPT_INSTRUCTIONS_UNIFICATION
2374  rb_ary_push(opts, rb_str_new2("instructions unification"));
2375 #endif
2376 #if OPT_INLINE_METHOD_CACHE
2377  rb_ary_push(opts, rb_str_new2("inline method cache"));
2378 #endif
2379 #if OPT_BLOCKINLINING
2380  rb_ary_push(opts, rb_str_new2("block inlining"));
2381 #endif
2382 
2383  /* ::RubyVM::INSTRUCTION_NAMES */
2384  rb_define_const(rb_cRubyVM, "INSTRUCTION_NAMES", rb_insns_name_array());
2385 
2386  /* ::RubyVM::DEFAULT_PARAMS
2387  * This constant variable shows VM's default parameters.
2388  * Note that changing these values does not affect VM exection.
2389  * Specification is not stable and you should not depend on this value.
2390  * Of course, this constant is MRI specific.
2391  */
2392  rb_define_const(rb_cRubyVM, "DEFAULT_PARAMS", vm_default_params());
2393 
2394  /* debug functions ::RubyVM::SDR(), ::RubyVM::NSDR() */
2395 #if VMDEBUG
2398 #else
2399  (void)sdr;
2400  (void)nsdr;
2401 #endif
2402 
2403  /* VM bootstrap: phase 2 */
2404  {
2405  rb_vm_t *vm = ruby_current_vm;
2406  rb_thread_t *th = GET_THREAD();
2407  VALUE filename = rb_str_new2("<main>");
2408  volatile VALUE iseqval = rb_iseq_new(0, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
2409  volatile VALUE th_self;
2410  rb_iseq_t *iseq;
2411 
2412  /* create vm object */
2413  vm->self = TypedData_Wrap_Struct(rb_cRubyVM, &vm_data_type, vm);
2414 
2415  /* create main thread */
2416  th_self = th->self = TypedData_Wrap_Struct(rb_cThread, &thread_data_type, th);
2417  rb_iv_set(th_self, "locals", rb_hash_new());
2418  vm->main_thread = th;
2419  vm->running_thread = th;
2420  th->vm = vm;
2421  th->top_wrapper = 0;
2422  th->top_self = rb_vm_top_self();
2424 
2426  st_insert(vm->living_threads, th_self, (st_data_t) th->thread_id);
2427 
2428  rb_gc_register_mark_object(iseqval);
2429  GetISeqPtr(iseqval, iseq);
2430  th->cfp->iseq = iseq;
2431  th->cfp->pc = iseq->iseq_encoded;
2432  th->cfp->self = th->top_self;
2433  th->cfp->klass = Qnil;
2434 
2435  /*
2436  * The Binding of the top level scope
2437  */
2438  rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new());
2439  }
2441 
2442  /* vm_backtrace.c */
2445 }
2446 
2447 void
2449 {
2450  rb_thread_t *th = GET_VM()->main_thread;
2451  rb_control_frame_t *cfp = (void *)(th->stack + th->stack_size);
2452  --cfp;
2453  cfp->iseq->location.path = filename;
2454 }
2455 
2456 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
2457 struct rb_objspace *rb_objspace_alloc(void);
2458 #endif
2459 
2460 void
2462 {
2463  /* VM bootstrap: phase 1 */
2464  rb_vm_t * vm = ruby_mimmalloc(sizeof(*vm));
2465  rb_thread_t * th = ruby_mimmalloc(sizeof(*th));
2466  if (!vm || !th) {
2467  fprintf(stderr, "[FATAL] failed to allocate memory\n");
2468  exit(EXIT_FAILURE);
2469  }
2470  MEMZERO(th, rb_thread_t, 1);
2472 
2473  vm_init2(vm);
2474 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
2475  vm->objspace = rb_objspace_alloc();
2476 #endif
2477  ruby_current_vm = vm;
2478 
2480  th->vm = vm;
2481  th_init(th, 0);
2483 }
2484 
2485 /* top self */
2486 
2487 static VALUE
2489 {
2490  return rb_str_new2("main");
2491 }
2492 
2493 VALUE
2495 {
2496  return GET_VM()->top_self;
2497 }
2498 
2499 void
2501 {
2502  rb_vm_t *vm = GET_VM();
2503 
2506  rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
2507 
2508  /* initialize mark object array */
2510 }
2511 
2512 VALUE *
2514 {
2515  return &vm->verbose;
2516 }
2517 
2518 VALUE *
2520 {
2521  return &vm->debug;
2522 }
2523 
2524 VALUE *
2526 {
2527  return ruby_vm_verbose_ptr(GET_VM());
2528 }
2529 
2530 VALUE *
2532 {
2533  return ruby_vm_debug_ptr(GET_VM());
2534 }
2535 
2536 /* iseq.c */
2538  VALUE insn, int op_no, VALUE op,
2539  int len, size_t pos, VALUE *pnop, VALUE child);
2540 
2541 #if VM_COLLECT_USAGE_DETAILS
2542 
2543 #define HASH_ASET(h, k, v) st_insert(RHASH_TBL(h), (st_data_t)(k), (st_data_t)(v))
2544 
2545 /* uh = {
2546  * insn(Fixnum) => ihash(Hash)
2547  * }
2548  * ihash = {
2549  * -1(Fixnum) => count, # insn usage
2550  * 0(Fixnum) => ophash, # operand usage
2551  * }
2552  * ophash = {
2553  * val(interned string) => count(Fixnum)
2554  * }
2555  */
2556 static void
2557 vm_analysis_insn(int insn)
2558 {
2559  ID usage_hash;
2560  ID bigram_hash;
2561  static int prev_insn = -1;
2562 
2563  VALUE uh;
2564  VALUE ihash;
2565  VALUE cv;
2566 
2567  CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
2568  CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
2569  uh = rb_const_get(rb_cRubyVM, usage_hash);
2570  if ((ihash = rb_hash_aref(uh, INT2FIX(insn))) == Qnil) {
2571  ihash = rb_hash_new();
2572  HASH_ASET(uh, INT2FIX(insn), ihash);
2573  }
2574  if ((cv = rb_hash_aref(ihash, INT2FIX(-1))) == Qnil) {
2575  cv = INT2FIX(0);
2576  }
2577  HASH_ASET(ihash, INT2FIX(-1), INT2FIX(FIX2INT(cv) + 1));
2578 
2579  /* calc bigram */
2580  if (prev_insn != -1) {
2581  VALUE bi;
2582  VALUE ary[2];
2583  VALUE cv;
2584 
2585  ary[0] = INT2FIX(prev_insn);
2586  ary[1] = INT2FIX(insn);
2587  bi = rb_ary_new4(2, &ary[0]);
2588 
2589  uh = rb_const_get(rb_cRubyVM, bigram_hash);
2590  if ((cv = rb_hash_aref(uh, bi)) == Qnil) {
2591  cv = INT2FIX(0);
2592  }
2593  HASH_ASET(uh, bi, INT2FIX(FIX2INT(cv) + 1));
2594  }
2595  prev_insn = insn;
2596 }
2597 
2598 static void
2599 vm_analysis_operand(int insn, int n, VALUE op)
2600 {
2601  ID usage_hash;
2602 
2603  VALUE uh;
2604  VALUE ihash;
2605  VALUE ophash;
2606  VALUE valstr;
2607  VALUE cv;
2608 
2609  CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
2610 
2611  uh = rb_const_get(rb_cRubyVM, usage_hash);
2612  if ((ihash = rb_hash_aref(uh, INT2FIX(insn))) == Qnil) {
2613  ihash = rb_hash_new();
2614  HASH_ASET(uh, INT2FIX(insn), ihash);
2615  }
2616  if ((ophash = rb_hash_aref(ihash, INT2FIX(n))) == Qnil) {
2617  ophash = rb_hash_new();
2618  HASH_ASET(ihash, INT2FIX(n), ophash);
2619  }
2620  /* intern */
2621  valstr = insn_operand_intern(GET_THREAD()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
2622 
2623  /* set count */
2624  if ((cv = rb_hash_aref(ophash, valstr)) == Qnil) {
2625  cv = INT2FIX(0);
2626  }
2627  HASH_ASET(ophash, valstr, INT2FIX(FIX2INT(cv) + 1));
2628 }
2629 
2630 static void
2631 vm_analysis_register(int reg, int isset)
2632 {
2633  ID usage_hash;
2634  VALUE uh;
2635  VALUE valstr;
2636  static const char regstrs[][5] = {
2637  "pc", /* 0 */
2638  "sp", /* 1 */
2639  "ep", /* 2 */
2640  "cfp", /* 3 */
2641  "self", /* 4 */
2642  "iseq", /* 5 */
2643  };
2644  static const char getsetstr[][4] = {
2645  "get",
2646  "set",
2647  };
2648  static VALUE syms[sizeof(regstrs) / sizeof(regstrs[0])][2];
2649 
2650  VALUE cv;
2651 
2652  CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
2653  if (syms[0] == 0) {
2654  char buff[0x10];
2655  int i;
2656 
2657  for (i = 0; i < (int)(sizeof(regstrs) / sizeof(regstrs[0])); i++) {
2658  int j;
2659  for (j = 0; j < 2; j++) {
2660  snprintf(buff, 0x10, "%d %s %-4s", i, getsetstr[j], regstrs[i]);
2661  syms[i][j] = ID2SYM(rb_intern(buff));
2662  }
2663  }
2664  }
2665  valstr = syms[reg][isset];
2666 
2667  uh = rb_const_get(rb_cRubyVM, usage_hash);
2668  if ((cv = rb_hash_aref(uh, valstr)) == Qnil) {
2669  cv = INT2FIX(0);
2670  }
2671  HASH_ASET(uh, valstr, INT2FIX(FIX2INT(cv) + 1));
2672 }
2673 
2674 #undef HASH_ASET
2675 
2676 void (*ruby_vm_collect_usage_func_insn)(int insn) = vm_analysis_insn;
2677 void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op) = vm_analysis_operand;
2678 void (*ruby_vm_collect_usage_func_register)(int reg, int isset) = vm_analysis_register;
2679 
2680 /* :nodoc: */
2681 static VALUE
2682 usage_analysis_insn_stop(VALUE self)
2683 {
2685  return Qnil;
2686 }
2687 
2688 /* :nodoc: */
2689 static VALUE
2690 usage_analysis_operand_stop(VALUE self)
2691 {
2693  return Qnil;
2694 }
2695 
2696 /* :nodoc: */
2697 static VALUE
2698 usage_analysis_register_stop(VALUE self)
2699 {
2701  return Qnil;
2702 }
2703 
2704 #else
2705 
2707 void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op) = NULL;
2708 void (*ruby_vm_collect_usage_func_register)(int reg, int isset) = NULL;
2709 
2710 #endif
2711 
2712 #if VM_COLLECT_USAGE_DETAILS
2713 /* @param insn instruction number */
2714 static void
2715 vm_collect_usage_insn(int insn)
2716 {
2717  if (RUBY_DTRACE_INSN_ENABLED()) {
2718  RUBY_DTRACE_INSN(rb_insns_name(insn));
2719  }
2721  (*ruby_vm_collect_usage_func_insn)(insn);
2722 }
2723 
2724 /* @param insn instruction number
2725  * @param n n-th operand
2726  * @param op operand value
2727  */
2728 static void
2729 vm_collect_usage_operand(int insn, int n, VALUE op)
2730 {
2731  if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
2732  VALUE valstr;
2733 
2734  valstr = insn_operand_intern(GET_THREAD()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
2735 
2736  RUBY_DTRACE_INSN_OPERAND(RSTRING_PTR(valstr), rb_insns_name(insn));
2737  RB_GC_GUARD(valstr);
2738  }
2740  (*ruby_vm_collect_usage_func_operand)(insn, n, op);
2741 }
2742 
2743 /* @param reg register id. see code of vm_analysis_register() */
2744 /* @param iseset 0: read, 1: write */
2745 static void
2746 vm_collect_usage_register(int reg, int isset)
2747 {
2749  (*ruby_vm_collect_usage_func_register)(reg, isset);
2750 }
2751 #endif
2752 
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1456
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2218
int is_from_method
Definition: vm_core.h:674
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE
Definition: vm_core.h:413
#define VM_FRAME_MAGIC_BLOCK
Definition: vm_core.h:726
rb_control_frame_t * cfp
Definition: vm_core.h:500
#define RUBY_EVENT_B_RETURN
Definition: ruby.h:1593
#define T_SYMBOL
Definition: ruby.h:502
VALUE rb_eLocalJumpError
Definition: eval.c:29
VALUE * env
Definition: vm_core.h:682
static VALUE core_hash_merge(VALUE hash, long argc, const VALUE *argv)
Definition: vm.c:2194
VALUE insn_operand_intern(rb_iseq_t *iseq, VALUE insn, int op_no, VALUE op, int len, size_t pos, VALUE *pnop, VALUE child)
Definition: iseq.c:1138
rb_vm_t * vm
Definition: vm_core.h:495
static void vm_clear_all_inline_method_cache(void)
Definition: vm.c:111
static void thread_free(void *ptr)
Definition: vm.c:1926
VALUE expanded_load_path
Definition: vm_core.h:364
NODE * rb_vm_cref(void)
Definition: vm.c:898
VALUE prev_envval
Definition: vm_core.h:685
VALUE rb_proc_alloc(VALUE klass)
Definition: proc.c:84
void rb_vm_bugreport(void)
Definition: vm_dump.c:614
#define MOD(n, d)
Definition: date_core.c:147
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1084
VALUE rb_ary_pop(VALUE ary)
Definition: array.c:866
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
static VALUE make_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:936
RUBY_EXTERN VALUE rb_cFloat
Definition: ruby.h:1439
#define RARRAY_LEN(a)
Definition: ruby.h:899
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:1587
void rb_bug(const char *fmt,...)
Definition: error.c:295
rb_method_type_t type
Definition: method.h:77
static size_t vm_memsize(const void *ptr)
Definition: vm.c:1698
struct RArray at_exit
Definition: vm_core.h:394
rb_control_frame_t * rb_vm_get_binding_creatable_next_cfp(rb_thread_t *th, const rb_control_frame_t *cfp)
Definition: vm.c:189
VALUE ruby_vm_const_missing_count
Definition: vm.c:94
#define TAG_NEXT
Definition: eval_intern.h:165
static void vm_init2(rb_vm_t *vm)
Definition: vm.c:1799
#define rb_gc_mark_locations(start, end)
Definition: gc.c:2348
#define RUBY_VM_IFUNC_P(ptr)
Definition: vm_core.h:798
struct rb_thread_struct * running_thread
Definition: vm_core.h:344
static rb_control_frame_t * vm_get_ruby_level_caller_cfp(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:213
#define RUBY_DTRACE_METHOD_RETURN_HOOK(th, klass, id)
Definition: probes_helper.h:58
VALUE rb_iseq_new(NODE *node, VALUE name, VALUE path, VALUE absolute_path, VALUE parent, enum iseq_type type)
Definition: iseq.c:410
void rb_objspace_free(rb_objspace_t *objspace)
Definition: gc.c:389
int i
Definition: win32ole.c:784
int rb_vm_get_sourceline(const rb_control_frame_t *cfp)
Definition: vm_backtrace.c:33
#define VM_ENVVAL_PREV_EP_PTR(v)
Definition: vm_core.h:779
#define RUBY_EVENT_RETURN
Definition: ruby.h:1585
Definition: st.h:77
#define VM_FRAME_FLAG_FINISH
Definition: vm_core.h:742
VALUE cmd
Definition: vm_core.h:372
st_table * local_storage
Definition: vm_core.h:579
Definition: st.h:108
static VALUE m_core_hash_merge_ary(VALUE self, VALUE hash, VALUE ary)
Definition: vm.c:2217
#define VM_FRAME_TYPE_FINISH_P(cfp)
Definition: vm_core.h:743
static VALUE * VM_CF_PREV_EP(rb_control_frame_t *cfp)
Definition: vm.c:48
unsigned long end
Definition: iseq.h:68
static void vm_rewrite_ep_in_errinfo(rb_thread_t *th)
Definition: vm.c:562
#define VM_FRAME_MAGIC_CFUNC
Definition: vm_core.h:729
char ruby_vm_redefined_flag[BOP_LAST_]
Definition: vm.c:95
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:493
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:789
static VALUE ruby_vm_global_state_version
Definition: iseq.h:61
#define GetProcPtr(obj, ptr)
Definition: vm_core.h:665
static VALUE vm_yield(rb_thread_t *th, int argc, const VALUE *argv)
Definition: vm.c:744
const rb_method_entry_t * passed_me
Definition: vm_core.h:514
struct heaps_slot * ptr
Definition: gc.c:219
VALUE top_self
Definition: vm_core.h:360
#define RUBY_VM_NORMAL_ISEQ_P(ptr)
Definition: vm_core.h:799
#define SET_THROWOBJ_STATE(obj, val)
Definition: eval_intern.h:177
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
Definition: vm_core.h:419
void rb_vm_mark(void *ptr)
Definition: vm.c:1618
#define FLOAT_REDEFINED_OP_FLAG
#define CLASS_OF(v)
Definition: ruby.h:448
rb_block_t block
Definition: vm_core.h:686
VALUE rb_vm_call_cfunc(VALUE recv, VALUE(*func)(VALUE), VALUE arg, const rb_block_t *blockptr, VALUE filename)
Definition: vm.c:1588
void rb_vm_pop_cfunc_frame(void)
Definition: vm.c:235
#define Qtrue
Definition: ruby.h:434
int st_insert(st_table *, st_data_t, st_data_t)
static size_t env_memsize(const void *ptr)
Definition: vm.c:346
static int vm_mark_each_thread_func(st_data_t key, st_data_t value, st_data_t dummy)
Definition: vm.c:1608
struct st_table * loaded_features_index
Definition: vm_core.h:367
VALUE rb_cHash
Definition: hash.c:41
rb_iseq_t * iseq
Definition: vm_core.h:446
#define TAG_BREAK
Definition: eval_intern.h:164
static int check_env(rb_env_t *const env)
Definition: vm.c:379
struct rb_vm_struct::@131 default_params
static int block_proc_is_lambda(const VALUE procval)
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1016
static VALUE m_core_undef_method(VALUE self, VALUE cbase, VALUE sym)
Definition: vm.c:2156
void rb_fiber_reset_root_local_storage(VALUE thval)
Definition: cont.c:1400
VALUE pending_interrupt_mask_stack
Definition: vm_core.h:552
#define GET_THROWOBJ_CATCH_POINT(obj)
Definition: eval_intern.h:181
VALUE rb_insns_name_array(void)
Definition: compile.c:5542
const int id
Definition: nkf.c:209
VALUE * defined_strings
Definition: vm_core.h:396
#define C(k)
VALUE rb_vm_cbase(void)
Definition: vm.c:922
#define DIV(n, d)
Definition: date_core.c:146
int env_size
Definition: vm_core.h:683
VALUE mark_object_ary
Definition: vm_core.h:355
VALUE rb_eTypeError
Definition: error.c:516
VALUE * rb_ruby_debug_ptr(void)
Definition: vm.c:2531
#define RUBY_NSIG
Definition: vm_core.h:57
#define TH_JUMP_TAG(th, st)
Definition: eval_intern.h:144
#define ENV_IN_HEAP_P(th, env)
Definition: vm.c:299
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
void st_free_table(st_table *)
Definition: st.c:334
#define SYM2ID(x)
Definition: ruby.h:364
#define RUBY_VM_SIZE_ALIGN
Definition: vm_core.h:409
st_table * living_threads
Definition: vm_core.h:346
size_t fiber_machine_stack_size
Definition: vm_core.h:403
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:465
VALUE rb_backref_get(void)
Definition: vm.c:830
VALUE verbose
Definition: vm_core.h:381
#define RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp)
Definition: vm_core.h:802
#define VM_ENVVAL_BLOCK_PTR(v)
Definition: vm_core.h:777
VALUE rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass)
Definition: vm.c:609
int local_table_size
Definition: vm_core.h:226
#define VM_FRAME_MAGIC_METHOD
Definition: vm_core.h:725
struct rb_iseq_struct * local_iseq
Definition: vm_core.h:286
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:534
#define Check_Type(v, t)
Definition: ruby.h:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
VALUE rb_thread_alloc(VALUE klass)
Definition: vm.c:2071
static rb_control_frame_t * vm_normal_frame(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:790
#define VM_FRAME_TYPE(cfp)
Definition: vm_core.h:738
static void lep_svar_set(rb_thread_t *th, VALUE *lep, rb_num_t key, VALUE val)
static void vm_clear_all_cache()
Definition: vm.c:120
#define RUBY_MARK_LEAVE(msg)
Definition: gc.h:54
ID called_id
Definition: method.h:99
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2425
static void env_mark(void *const ptr)
Definition: vm.c:304
#define VM_FRAME_MAGIC_IFUNC
Definition: vm_core.h:731
#define RB_GC_GUARD(v)
Definition: ruby.h:530
#define T_HASH
Definition: ruby.h:493
void Init_VM(void)
Definition: vm.c:2305
static int thread_recycle_stack_count
Definition: vm.c:1816
void Init_vm_backtrace(void)
Definition: vm_backtrace.c:946
void rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
Definition: vm.c:968
size_t stack_max
Definition: vm_core.h:278
#define DATA_PTR(dta)
Definition: ruby.h:985
#define SDR()
Definition: vm_core.h:817
VALUE last_status
Definition: vm_core.h:503
void rb_gc_mark(VALUE ptr)
Definition: gc.c:2600
ID defined_method_id
Definition: vm_core.h:308
#define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp)
Definition: vm_core.h:795
#define RUBY_GC_INFO
Definition: gc.h:57
#define T_ARRAY
Definition: ruby.h:492
static VALUE * thread_recycle_stack_slot[RECYCLE_MAX]
Definition: vm.c:1815
#define VM_FRAME_MAGIC_RESCUE
Definition: vm_core.h:734
static VALUE vm_cfp_svar_get(rb_thread_t *th, rb_control_frame_t *cfp, VALUE key)
Definition: vm.c:802
#define TAG_RAISE
Definition: eval_intern.h:168
#define GetEnvPtr(obj, ptr)
Definition: vm_core.h:678
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: st.c:839
VALUE rb_sourcefilename(void)
Definition: vm.c:856
static VALUE lep_svar_get(rb_thread_t *th, VALUE *lep, rb_num_t key)
VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, int argc, const VALUE *argv, const rb_block_t *blockptr)
Definition: vm.c:780
static VALUE * VM_CF_LEP(rb_control_frame_t *cfp)
Definition: vm.c:42
struct rb_objspace * rb_objspace_alloc(void)
Definition: gc.c:374
VALUE env
Definition: vm_core.h:693
RUBY_EXTERN VALUE rb_cProc
Definition: ruby.h:1449
static int vm_yield_setup_args(rb_thread_t *const th, const rb_iseq_t *iseq, int argc, VALUE *argv, const rb_block_t *blockptr, int lambda)
void rb_undef(VALUE, ID)
Definition: vm_method.c:868
static const rb_data_type_t vm_data_type
Definition: vm.c:1714
#define RUBY_VM_FIBER_VM_STACK_SIZE
Definition: vm_core.h:416
static VALUE env_alloc(void)
Definition: vm.c:365
static VALUE vm_default_params(void)
Definition: vm.c:1721
static VALUE sdr(void)
Definition: vm.c:2267
VALUE klass
Definition: ruby.h:701
static VALUE main_to_s(VALUE obj)
Definition: vm.c:2488
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1362
VALUE rb_iseq_eval_main(VALUE iseqval)
Definition: vm.c:1511
void rb_mark_method_entry(const rb_method_entry_t *me)
Definition: gc.c:2449
void rb_gc_force_recycle(VALUE p)
Definition: gc.c:2963
#define RHASH_TBL(h)
Definition: ruby.h:928
#define SET(name)
VALUE envval
Definition: vm_core.h:671
VALUE thgroup_default
Definition: vm_core.h:347
static st_table * vm_opt_method_table
Definition: vm.c:1041
#define VM_FRAME_FLAG_PASSED
Definition: vm_core.h:741
#define sym(x)
Definition: date_core.c:3715
Definition: node.h:239
enum iseq_catch_table_entry::catch_type type
Win32OLEIDispatch * p
Definition: win32ole.c:786
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:200
void rb_exc_raise(VALUE mesg)
Definition: eval.c:527
void(* ruby_vm_collect_usage_func_register)(int reg, int isset)
Definition: vm.c:2708
VALUE * stack
Definition: vm_core.h:498
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1470
void Init_top_self(void)
Definition: vm.c:2500
int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, VALUE *klassp)
Definition: vm.c:1524
static VALUE vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const NODE *cref)
Definition: vm.c:736
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
static VALUE check_env_value(VALUE envval)
Definition: vm.c:395
enum rb_iseq_struct::iseq_type type
VALUE rb_binding_new(void)
Definition: proc.c:322
#define TH_POP_TAG()
Definition: eval_intern.h:129
int st_lookup(st_table *, st_data_t, st_data_t *)
static VALUE m_core_define_method(VALUE self, VALUE cbase, VALUE sym, VALUE iseqval)
Definition: vm.c:2120
#define MEMZERO(p, type, n)
Definition: ruby.h:1241
void rb_iter_break(void)
Definition: vm.c:1028
RUBY_EXTERN VALUE rb_cBinding
Definition: ruby.h:1429
unsigned short first_lineno
Definition: vm_core.h:696
static VALUE vm_make_env_each(rb_thread_t *const th, rb_control_frame_t *const cfp, VALUE *envptr, VALUE *const endptr)
Definition: vm.c:408
static void vm_set_main_stack(rb_thread_t *th, VALUE iseqval)
Definition: vm.c:170
void rb_ary_free(VALUE ary)
Definition: array.c:471
Definition: iseq.h:59
#define RUBY_VM_THREAD_VM_STACK_SIZE
Definition: vm_core.h:411
int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp)
Definition: vm.c:1552
static void th_init(rb_thread_t *th, VALUE self)
Definition: vm.c:2022
static void add_opt_method(VALUE klass, ID mid, VALUE bop)
Definition: vm.c:1092
const char * rb_insns_name(int i)
Definition: compile.c:5536
#define ALLOC_N(type, n)
Definition: ruby.h:1223
int src_encoding_index
Definition: vm_core.h:379
#define EXEC_TAG()
Definition: eval_intern.h:141
VALUE locking_mutex
Definition: vm_core.h:558
#define BIGNUM_REDEFINED_OP_FLAG
rb_control_frame_t * rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, const rb_control_frame_t *cfp)
Definition: vm.c:201
VALUE * rb_vm_ep_local_ep(VALUE *ep)
Definition: vm.c:36
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_eRuntimeError
Definition: error.c:515
VALUE rb_block_lambda(void)
Definition: proc.c:472
static void vm_iter_break(rb_thread_t *th, VALUE val)
Definition: vm.c:1017
static VALUE invoke_block_from_c(rb_thread_t *th, const rb_block_t *block, VALUE self, int argc, const VALUE *argv, const rb_block_t *blockptr, const NODE *cref, VALUE defined_class)
Definition: vm.c:680
const rb_data_type_t ruby_threadptr_data_type
Definition: vm.c:1987
size_t fiber_vm_stack_size
Definition: vm_core.h:402
#define GetBindingPtr(obj, ptr)
Definition: vm_core.h:689
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:478
struct rb_vm_struct rb_vm_t
void rb_vm_inc_const_missing_count(void)
Definition: vm.c:128
void rb_vm_set_progname(VALUE filename)
Definition: vm.c:2448
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1425
VALUE rb_ary_new(void)
Definition: array.c:424
#define dp(v)
Definition: vm_debug.h:23
VALUE load_path_check_cache
Definition: vm_core.h:363
#define snprintf
Definition: subst.h:6
#define RCLASS_ORIGIN(c)
Definition: internal.h:51
#define JUMP_TAG(st)
Definition: eval_intern.h:148
rb_iseq_t * iseq
Definition: vm_core.h:428
#define NIL_P(v)
Definition: ruby.h:446
#define VMDEBUG
Definition: vm_dump.c:19
#define COPY_CREF(c1, c2)
#define UNLIKELY(x)
Definition: vm_core.h:115
void rb_backref_set(VALUE val)
Definition: vm.c:836
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:488
jmp_buf machine_regs
Definition: vm_core.h:596
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2204
#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
Definition: vm_core.h:414
static int collect_local_variables_in_iseq(rb_iseq_t *iseq, const VALUE ary)
Definition: vm.c:487
VALUE rb_cRubyVM
Definition: vm.c:89
static const rb_block_t * check_block(rb_thread_t *th)
Definition: vm.c:724
static void check_machine_stack_size(size_t *sizep)
Definition: vm.c:1753
#define RUBY_MARK_ENTER(msg)
Definition: gc.h:53
static void vm_define_method(rb_thread_t *th, VALUE obj, ID id, VALUE iseqval, rb_num_t is_singleton, NODE *cref)
Definition: vm.c:2079
VALUE top_self
Definition: vm_core.h:520
int argc
Definition: ruby.c:130
struct st_table * loading_table
Definition: vm_core.h:368
#define VM_FRAME_MAGIC_CLASS
Definition: vm_core.h:727
#define Qfalse
Definition: ruby.h:433
static VALUE ruby_thread_init(VALUE self)
Definition: vm.c:2054
static size_t thread_memsize(const void *ptr)
Definition: vm.c:1967
void ruby_thread_init_stack(rb_thread_t *th)
Definition: thread.c:476
static int collect_local_variables_in_env(rb_env_t *env, const VALUE ary)
Definition: vm.c:501
#define RUBY_DTRACE_CMETHOD_RETURN_HOOK(th, klass, id)
Definition: probes_helper.h:64
Definition: method.h:95
static int kwmerge_ii(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: vm.c:2234
void rb_frame_pop(void)
Definition: vm.c:263
#define TAG_REDO
Definition: eval_intern.h:167
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:2982
#define T_NODE
Definition: ruby.h:506
VALUE * rb_gc_stack_start
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1740
int err
Definition: win32.c:87
VALUE rb_vm_make_env_object(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:530
#define EXIT_FAILURE
Definition: eval_intern.h:24
VALUE * machine_stack_start
Definition: vm_core.h:588
#define SET_THROWOBJ_CATCH_POINT(obj, val)
Definition: eval_intern.h:175
static VALUE vm_make_env_object(rb_thread_t *th, rb_control_frame_t *cfp, VALUE *blockprocptr)
Definition: vm.c:537
struct RBasic basic
Definition: ruby.h:882
ID * local_table
Definition: vm_core.h:225
static VALUE vm_exec(rb_thread_t *th)
Definition: vm.c:1259
#define RUBY_VM_GET_CFP_FROM_BLOCK_PTR(b)
Definition: vm_core.h:803
VALUE * ruby_vm_debug_ptr(rb_vm_t *vm)
Definition: vm.c:2519
static rb_control_frame_t * vm_push_frame(rb_thread_t *th, const rb_iseq_t *iseq, VALUE type, VALUE self, VALUE klass, VALUE specval, const VALUE *pc, VALUE *sp, int local_size, const rb_method_entry_t *me)
Definition: vm_insnhelper.c:34
rb_method_entry_t * rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr)
Definition: vm_method.c:572
void rb_thread_mark(void *ptr)
Definition: vm.c:1857
#define ARRAY_REDEFINED_OP_FLAG
VALUE klass
Definition: method.h:100
unsigned long start
Definition: iseq.h:67
#define RUBY_VM_FIBER_MACHINE_STACK_SIZE
Definition: vm_core.h:418
unsigned long rb_num_t
Definition: vm_core.h:124
void rb_gc_mark_machine_stack(rb_thread_t *th)
Definition: gc.c:2530
void * ruby_mimmalloc(size_t size)
Definition: gc.c:3664
void rb_call_end_proc(VALUE data)
Definition: eval_jump.c:11
#define TAG_RETURN
Definition: eval_intern.h:163
#define TH_POP_TAG2()
Definition: eval_intern.h:133
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1876
rb_hook_list_t event_hooks
Definition: vm_core.h:603
Definition: iseq.h:60
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase)
Definition: iseq.c:1900
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
void rb_alias_variable(ID, ID)
Definition: variable.c:863
#define RCLASS_M_TBL(c)
Definition: internal.h:49
Definition: iseq.h:64
static void vm_svar_set(VALUE key, VALUE val)
Definition: vm.c:823
static rb_block_t * VM_CF_BLOCK_PTR(rb_control_frame_t *cfp)
Definition: vm.c:54
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
#define rb_thread_set_current(th)
Definition: vm_core.h:903
#define VM_EP_PREV_EP(ep)
Definition: vm_core.h:782
VALUE special_exceptions[ruby_special_error_count]
Definition: vm_core.h:357
void rb_vm_stack_to_heap(rb_thread_t *th)
Definition: vm.c:588
struct rb_mutex_struct * keeping_mutexes
Definition: vm_core.h:559
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1272
VALUE loaded_features
Definition: vm_core.h:365
VALUE blockprocval
Definition: vm_core.h:695
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex)
Definition: vm_method.c:405
static void vm_set_top_stack(rb_thread_t *th, VALUE iseqval)
Definition: vm.c:136
#define EXEC_EVENT_HOOK_AND_POP_FRAME(th_, flag_, self_, id_, klass_, data_)
Definition: vm_core.h:1003
#define malloc
Definition: ripper.c:98
VALUE rb_hash_new(void)
Definition: hash.c:234
VALUE rb_iv_set(VALUE, const char *, VALUE)
Definition: variable.c:2594
void ruby_xfree(void *x)
Definition: gc.c:3653
static VALUE m_core_define_singleton_method(VALUE self, VALUE cbase, VALUE sym, VALUE iseqval)
Definition: vm.c:2129
struct rb_vm_struct::@130 trap_list[RUBY_NSIG]
int rb_is_local_id(ID id)
Definition: ripper.c:17083
static void vm_set_eval_stack(rb_thread_t *th, VALUE iseqval, const NODE *cref, rb_block_t *base_block)
Definition: vm.c:153
int rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
Definition: vm.c:1558
struct rb_iseq_struct * parent_iseq
Definition: vm_core.h:285
#define FIXNUM_REDEFINED_OP_FLAG
unsigned long ID
Definition: ruby.h:105
size_t thread_vm_stack_size
Definition: vm_core.h:400
VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val)
Definition: vm.c:975
VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp)
Definition: vm.c:647
void ruby_vm_at_exit(void(*func)(rb_vm_t *))
ruby_vm_at_exit registers a function func to be invoked when a VM passed away.
Definition: vm.c:272
int local_size
Definition: vm_core.h:684
static VALUE * VM_EP_LEP(VALUE *ep)
Definition: vm.c:25
VALUE rb_cEnv
Definition: vm.c:91
#define Qnil
Definition: ruby.h:435
VALUE rb_exc_new2(VALUE etype, const char *s)
Definition: error.c:547
int type
Definition: tcltklib.c:111
#define REWIND_CFP(expr)
Definition: vm.c:2114
#define VM_FRAME_MAGIC_EVAL
Definition: vm_core.h:732
#define BUILTIN_TYPE(x)
Definition: ruby.h:510
VALUE * iseq_encoded
Definition: vm_core.h:216
unsigned long VALUE
Definition: ruby.h:104
VALUE rb_vm_top_self(void)
Definition: vm.c:2494
VALUE rb_iseq_eval(VALUE iseqval)
Definition: vm.c:1498
static VALUE * vm_base_ptr(rb_control_frame_t *cfp)
static VALUE result
Definition: nkf.c:40
static void vm_pop_frame(rb_thread_t *th)
Definition: vm_insnhelper.c:99
static VALUE m_core_hash_merge_ptr(int argc, VALUE *argv, VALUE recv)
Definition: vm.c:2224
int catch_table_size
Definition: vm_core.h:282
#define RBASIC(obj)
Definition: ruby.h:1094
Definition: iseq.h:57
#define RARRAY_EMBED_FLAG
Definition: ruby.h:895
struct rb_thread_struct * main_thread
Definition: vm_core.h:343
const char * rb_class2name(VALUE)
Definition: variable.c:389
rb_thread_t * ruby_current_thread
Definition: vm.c:96
VALUE first_proc
Definition: vm_core.h:583
#define RARRAY_EMBED_LEN_MASK
Definition: ruby.h:897
#define FIX2INT(x)
Definition: ruby.h:624
void rb_mark_tbl(st_table *tbl)
Definition: gc.c:2543
void Init_native_thread(void)
#define SYMBOL_REDEFINED_OP_FLAG
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1209
rb_iseq_location_t location
Definition: vm_core.h:213
char * getenv()
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:122
VALUE flags
Definition: ruby.h:700
int rb_sigaltstack_size(void)
int ruby_vm_destruct(rb_vm_t *vm)
Definition: vm.c:1665
Definition: iseq.h:62
#define AREF(s, idx)
Definition: cparse.c:93
st_table * st_init_numtable(void)
Definition: st.c:272
#define RUBY_DTRACE_HASH_CREATE_ENABLED()
Definition: probes.h:59
#define NEW_THROW_OBJECT(val, pt, st)
Definition: eval_intern.h:173
Definition: iseq.h:63
void rb_set_end_proc(void(*func)(VALUE), VALUE data)
Definition: eval_jump.c:60
void rb_memerror(void)
Definition: gc.c:3408
VALUE blockprocval
Definition: vm_core.h:672
static VALUE * thread_recycle_stack(size_t size)
Definition: vm.c:1819
#define OP(mid_, bop_)
enum rb_thread_status status
Definition: vm_core.h:531
#define VM_FRAME_MAGIC_TOP
Definition: vm_core.h:728
void rb_iter_break_value(VALUE val)
Definition: vm.c:1034
static VALUE vm_svar_get(VALUE key)
Definition: vm.c:816
void vm_trace_mark_event_hooks(rb_hook_list_t *hooks)
Definition: vm_trace.c:51
#define RUBY_FREE_UNLESS_NULL(ptr)
Definition: gc.h:61
#define VM_FRAME_MAGIC_PROC
Definition: vm_core.h:730
static int kwmerge_i(VALUE key, VALUE value, VALUE hash)
Definition: vm.c:2242
#define RSTRING_PTR(str)
Definition: ruby.h:866
VALUE rb_mRubyVMFrozenCore
Definition: vm.c:92
VALUE * machine_stack_end
Definition: vm_core.h:589
static VALUE vm_exec_core(rb_thread_t *th, VALUE initial)
Definition: vm_exec.c:34
VALUE first_args
Definition: vm_core.h:584
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1338
static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *ep, VALUE ary)
Definition: vm.c:512
static size_t get_param(const char *name, size_t default_value, size_t min_value)
Definition: vm.c:1736
unsigned long sp
Definition: iseq.h:70
int size
Definition: encoding.c:52
#define VM_FRAME_MAGIC_LAMBDA
Definition: vm_core.h:733
struct rb_objspace * objspace
Definition: vm_core.h:387
#define INT2FIX(i)
Definition: ruby.h:241
VALUE top_wrapper
Definition: vm_core.h:521
static void vm_clear_global_method_cache(void)
int mark_stack_len
Definition: vm_core.h:597
rb_vm_t * ruby_current_vm
Definition: vm.c:97
static VALUE nsdr(void)
Definition: vm.c:2275
int safe_level
Definition: vm_core.h:673
VALUE rb_cBignum
Definition: bignum.c:28
static int at_exit
Definition: tcltklib.c:185
VALUE rb_lastline_get(void)
Definition: vm.c:842
static VALUE vm_get_cbase(const rb_iseq_t *iseq, const VALUE *ep)
VALUE root_svar
Definition: vm_core.h:527
rb_block_t block
Definition: vm_core.h:669
VALUE klass
Definition: vm_core.h:305
static int vm_redefinition_check_flag(VALUE klass)
Definition: vm.c:1044
void rb_thread_recycle_stack_release(VALUE *stack)
Definition: vm.c:1835
void Init_BareVM(void)
Definition: vm.c:2461
#define thread_data_type
Definition: vm.c:1986
VALUE rb_block_proc(void)
Definition: proc.c:458
static const unsigned char cv[]
Definition: nkf.c:564
rb_method_definition_t * def
Definition: method.h:98
size_t st_memsize(const st_table *)
Definition: st.c:342
NORETURN(static void vm_iter_break(rb_thread_t *th, VALUE val))
#define PROCDEBUG
Definition: vm.c:87
VALUE * rb_ruby_verbose_ptr(void)
Definition: vm.c:2525
const rb_method_entry_t * me
Definition: vm_core.h:435
#define RUBY_FREE_LEAVE(msg)
Definition: gc.h:56
unsigned long rb_event_flag_t
Definition: ruby.h:1603
rb_hook_list_t event_hooks
Definition: vm_core.h:377
#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN
Definition: vm_core.h:412
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:570
#define RARRAY_PTR(a)
Definition: ruby.h:904
#define RUBY_FREE_ENTER(msg)
Definition: gc.h:55
#define STRING_REDEFINED_OP_FLAG
static int check_redefined_method(st_data_t key, st_data_t value, st_data_t data)
Definition: vm.c:1071
static void vm_cfp_svar_set(rb_thread_t *th, rb_control_frame_t *cfp, VALUE key, const VALUE val)
Definition: vm.c:809
struct iseq_catch_table_entry * catch_table
Definition: vm_core.h:281
#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN
Definition: vm_core.h:417
uint8_t key[16]
Definition: random.c:1370
VALUE rb_cThread
Definition: vm.c:90
static void ruby_vm_run_at_exit_hooks(rb_vm_t *vm)
Definition: vm.c:278
#define VM_PROFILE_ATEXIT()
VALUE root_fiber
Definition: vm_core.h:608
int local_size
Definition: vm_core.h:229
#define VM_EP_BLOCK_PTR(ep)
Definition: vm_core.h:783
rb_event_flag_t ruby_vm_event_flags
Definition: vm.c:98
static const rb_data_type_t env_data_type
Definition: vm.c:359
#define RUBY_MARK_UNLESS_NULL(ptr)
Definition: gc.h:60
size_t thread_machine_stack_size
Definition: vm_core.h:401
VALUE load_path_snapshot
Definition: vm_core.h:362
rb_block_t * rb_vm_control_frame_block_ptr(rb_control_frame_t *cfp)
Definition: vm.c:61
#define RUBY_EVENT_END
Definition: ruby.h:1583
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1019
VALUE rb_cArray
Definition: array.c:29
#define GetThreadPtr(obj, ptr)
Definition: vm_core.h:452
VALUE loaded_features_snapshot
Definition: vm_core.h:366
size_t rb_gc_stack_maxsize
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
VALUE debug
Definition: vm_core.h:381
#define vm_free
Definition: vm.c:1662
void rb_vm_change_state(void)
Definition: vm.c:103
static VALUE thread_alloc(VALUE klass)
Definition: vm.c:2008
static void env_free(void *const ptr)
Definition: vm.c:334
#define TIME_REDEFINED_OP_FLAG
#define PRIdSIZE
Definition: ruby.h:186
VALUE self
Definition: vm_core.h:338
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_)
Definition: vm_core.h:1000
#define assert(condition)
Definition: ossl.h:45
NODE * rb_vm_get_cref(const rb_iseq_t *, const VALUE *)
const char * name
Definition: nkf.c:208
VALUE self
Definition: vm_core.h:292
#define ID2SYM(x)
Definition: ruby.h:363
#define GetISeqPtr(obj, ptr)
Definition: vm_core.h:183
unsigned long cont
Definition: iseq.h:69
const char * rb_id2name(ID id)
Definition: ripper.c:17012
static VALUE m_core_set_variable_alias(VALUE self, VALUE sym1, VALUE sym2)
Definition: vm.c:2147
unsigned long st_data_t
Definition: st.h:35
size_t stack_size
Definition: vm_core.h:499
struct rb_thread_struct rb_thread_t
static VALUE m_core_set_method_alias(VALUE self, VALUE cbase, VALUE sym1, VALUE sym2)
Definition: vm.c:2138
struct rb_vm_tag * tag
Definition: vm_core.h:561
static VALUE m_core_set_postexe(VALUE self, VALUE iseqval)
Definition: vm.c:2166
void(* ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op)
Definition: vm.c:2707
VALUE iseq
Definition: iseq.h:66
void rb_vm_rewind_cfp(rb_thread_t *th, rb_control_frame_t *cfp)
Definition: vm.c:245
void rb_vm_gvl_destroy(rb_vm_t *vm)
Definition: thread.c:273
static VALUE m_core_hash_merge_kwd(VALUE recv, VALUE hash, VALUE kw)
Definition: vm.c:2250
void rb_lastline_set(VALUE val)
Definition: vm.c:848
VALUE retval
Definition: vm_core.h:470
#define GC_GUARDED_PTR_REF(p)
Definition: vm_core.h:764
#define CONST_ID(var, str)
Definition: ruby.h:1318
static VALUE vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self, VALUE defined_class, int argc, const VALUE *argv, const rb_block_t *blockptr)
Definition: vm.c:752
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1012
#define ENV_VAL(env)
Definition: vm.c:301
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1143
VALUE load_path
Definition: vm_core.h:361
#define RECYCLE_MAX
Definition: vm.c:1814
#define CHECK_VM_STACK_OVERFLOW(cfp, margin)
Definition: vm_core.h:870
VALUE pending_interrupt_queue
Definition: vm_core.h:550
void(* ruby_vm_collect_usage_func_insn)(int insn)
Definition: vm.c:2706
RUBY_EXTERN VALUE rb_cSymbol
Definition: ruby.h:1458
static VALUE vm_make_proc_from_block(rb_thread_t *th, rb_block_t *block)
Definition: vm.c:600
#define rb_intern(str)
const char * rb_sourcefile(void)
Definition: vm.c:870
static VALUE m_core_hash_from_ary(VALUE self, VALUE ary)
Definition: vm.c:2205
#define SYMBOL_P(x)
Definition: ruby.h:362
static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass)
Definition: vm.c:1058
VALUE path
Definition: vm_core.h:694
#define env
#define NULL
Definition: _sdbm.c:102
#define Qundef
Definition: ruby.h:436
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
#define T_ICLASS
Definition: ruby.h:487
VALUE stat_insn_usage
Definition: vm_core.h:600
RUBY_EXTERN VALUE rb_cFixnum
Definition: ruby.h:1438
RUBY_EXTERN VALUE rb_cTime
Definition: ruby.h:1460
void rb_vm_jump_tag_but_local_jump(int state)
Definition: vm.c:1007
#define INC_VM_STATE_VERSION()
#define VM_EP_LEP_P(ep)
Definition: vm_core.h:784
#define HASH_REDEFINED_OP_FLAG
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:890
VALUE rb_class_new(VALUE super)
Creates a new class.
Definition: class.c:117
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
Definition: st.c:1006
VALUE rb_str_new2(const char *)
#define GET_THROWOBJ_VAL(obj)
Definition: eval_intern.h:180
free(psz)
VALUE rb_obj_is_thread(VALUE obj)
Definition: vm.c:1997
VALUE * ruby_vm_verbose_ptr(rb_vm_t *vm)
Definition: vm.c:2513
VALUE rb_binding_alloc(VALUE klass)
Definition: proc.c:283
static void vm_init_redefined_flag(void)
Definition: vm.c:1105
#define rb_thread_set_current_raw(th)
Definition: vm_core.h:902
VALUE coverages
Definition: vm_core.h:382
rb_thread_id_t thread_id
Definition: vm_core.h:530
int rb_sourceline(void)
Definition: vm.c:884
char ** argv
Definition: ruby.c:131
#define TAG_RETRY
Definition: eval_intern.h:166
VALUE * ep
Definition: vm_core.h:445
#define RUBY_DTRACE_HASH_CREATE(arg0, arg1, arg2)
Definition: probes.h:60
static VALUE vm_yield_with_cfunc(rb_thread_t *th, const rb_block_t *block, VALUE self, int argc, const VALUE *argv, const rb_block_t *blockargptr)
static void vm_default_params_setup(rb_vm_t *vm)
Definition: vm.c:1771
VALUE rb_thread_current_status(const rb_thread_t *th)
Definition: vm.c:1564
#define GET_VM()
Definition: vm_core.h:883