forked from rubyomr-preview/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_insnhelper.c
2151 lines (1850 loc) · 52.6 KB
/
vm_insnhelper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
vm_insnhelper.c - instruction helper functions.
$Author$
Copyright (C) 2007 Koichi Sasada
**********************************************************************/
/* finish iseq array */
#include "insns.inc"
#include <math.h>
#include "constant.h"
#include "internal.h"
#include "probes.h"
#include "probes_helper.h"
/* control stack frame */
#ifndef INLINE
#define INLINE inline
#endif
static rb_control_frame_t *
vm_get_ruby_level_caller_cfp(const rb_thread_t *th, const rb_control_frame_t *cfp);
VALUE
ruby_vm_sysstack_error_copy(void)
{
VALUE e = rb_obj_alloc(rb_eSysStackError);
rb_obj_copy_ivar(e, sysstack_error);
return e;
}
static void
vm_stackoverflow(void)
{
rb_exc_raise(ruby_vm_sysstack_error_copy());
}
static inline 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,
int stack_max)
{
rb_control_frame_t *const cfp = th->cfp - 1;
int i;
/* check stack overflow */
CHECK_VM_STACK_OVERFLOW0(cfp, sp, local_size + stack_max);
th->cfp = cfp;
/* setup vm value stack */
/* initialize local variables */
for (i=0; i < local_size; i++) {
*sp++ = Qnil;
}
/* set special val */
*sp = specval;
/* setup vm control frame stack */
cfp->pc = (VALUE *)pc;
cfp->sp = sp + 1;
#if VM_DEBUG_BP_CHECK
cfp->bp_check = sp + 1;
#endif
cfp->ep = sp;
cfp->iseq = (rb_iseq_t *) iseq;
cfp->flag = type;
cfp->self = self;
cfp->block_iseq = 0;
cfp->proc = 0;
cfp->me = me;
if (klass) {
cfp->klass = klass;
}
else {
rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, prev_cfp)) {
cfp->klass = Qnil;
}
else {
cfp->klass = prev_cfp->klass;
}
}
if (VMDEBUG == 2) {
SDR();
}
return cfp;
}
static inline void
vm_pop_frame(rb_thread_t *th)
{
th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
if (VMDEBUG == 2) {
SDR();
}
}
/* method dispatch */
static inline VALUE
rb_arity_error_new(int argc, int min, int max)
{
VALUE err_mess = 0;
if (min == max) {
err_mess = rb_sprintf("wrong number of arguments (%d for %d)", argc, min);
}
else if (max == UNLIMITED_ARGUMENTS) {
err_mess = rb_sprintf("wrong number of arguments (%d for %d+)", argc, min);
}
else {
err_mess = rb_sprintf("wrong number of arguments (%d for %d..%d)", argc, min, max);
}
return rb_exc_new3(rb_eArgError, err_mess);
}
void
rb_error_arity(int argc, int min, int max)
{
rb_exc_raise(rb_arity_error_new(argc, min, max));
}
/* svar */
static inline NODE *
lep_svar_place(rb_thread_t *th, VALUE *lep)
{
VALUE *svar;
if (lep && th->root_lep != lep) {
svar = &lep[-1];
}
else {
svar = &th->root_svar;
}
if (NIL_P(*svar)) {
*svar = (VALUE)NEW_IF(Qnil, Qnil, Qnil);
}
return (NODE *)*svar;
}
static VALUE
lep_svar_get(rb_thread_t *th, VALUE *lep, rb_num_t key)
{
NODE *svar = lep_svar_place(th, lep);
switch (key) {
case 0:
return svar->u1.value;
case 1:
return svar->u2.value;
default: {
const VALUE ary = svar->u3.value;
if (NIL_P(ary)) {
return Qnil;
}
else {
return rb_ary_entry(ary, key - DEFAULT_SPECIAL_VAR_COUNT);
}
}
}
}
static void
lep_svar_set(rb_thread_t *th, VALUE *lep, rb_num_t key, VALUE val)
{
NODE *svar = lep_svar_place(th, lep);
switch (key) {
case 0:
RB_OBJ_WRITE(svar, &svar->u1.value, val);
return;
case 1:
RB_OBJ_WRITE(svar, &svar->u2.value, val);
return;
default: {
VALUE ary = svar->u3.value;
if (NIL_P(ary)) {
RB_OBJ_WRITE(svar, &svar->u3.value, ary = rb_ary_new());
}
rb_ary_store(ary, key - DEFAULT_SPECIAL_VAR_COUNT, val);
}
}
}
static inline VALUE
vm_getspecial(rb_thread_t *th, VALUE *lep, rb_num_t key, rb_num_t type)
{
VALUE val;
if (type == 0) {
val = lep_svar_get(th, lep, key);
}
else {
VALUE backref = lep_svar_get(th, lep, 1);
if (type & 0x01) {
switch (type >> 1) {
case '&':
val = rb_reg_last_match(backref);
break;
case '`':
val = rb_reg_match_pre(backref);
break;
case '\'':
val = rb_reg_match_post(backref);
break;
case '+':
val = rb_reg_match_last(backref);
break;
default:
rb_bug("unexpected back-ref");
}
}
else {
val = rb_reg_nth_match((int)(type >> 1), backref);
}
}
return val;
}
static NODE *
vm_get_cref0(const rb_iseq_t *iseq, const VALUE *ep)
{
while (1) {
if (VM_EP_LEP_P(ep)) {
if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) return NULL;
return iseq->cref_stack;
}
else if (ep[-1] != Qnil) {
return (NODE *)ep[-1];
}
ep = VM_EP_PREV_EP(ep);
}
}
NODE *
rb_vm_get_cref(const rb_iseq_t *iseq, const VALUE *ep)
{
NODE *cref = vm_get_cref0(iseq, ep);
if (cref == 0) {
rb_bug("rb_vm_get_cref: unreachable");
}
return cref;
}
void
rb_vm_rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass, NODE **new_cref_ptr)
{
NODE *new_node;
while (node) {
if (node->nd_clss == old_klass) {
new_node = NEW_CREF(new_klass);
COPY_CREF_OMOD(new_node, node);
RB_OBJ_WRITE(new_node, &new_node->nd_next, node->nd_next);
*new_cref_ptr = new_node;
return;
}
new_node = NEW_CREF(node->nd_clss);
COPY_CREF_OMOD(new_node, node);
node = node->nd_next;
*new_cref_ptr = new_node;
new_cref_ptr = &new_node->nd_next;
}
*new_cref_ptr = NULL;
}
static NODE *
vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr)
{
rb_control_frame_t *cfp = vm_get_ruby_level_caller_cfp(th, th->cfp);
NODE *cref = NEW_CREF(klass);
cref->nd_refinements = Qnil;
cref->nd_visi = noex;
if (blockptr) {
RB_OBJ_WRITE(cref, &cref->nd_next, vm_get_cref0(blockptr->iseq, blockptr->ep));
}
else if (cfp) {
RB_OBJ_WRITE(cref, &cref->nd_next, vm_get_cref0(cfp->iseq, cfp->ep));
}
/* TODO: why cref->nd_next is 1? */
if (cref->nd_next && cref->nd_next != (void *) 1 &&
!NIL_P(cref->nd_next->nd_refinements)) {
COPY_CREF_OMOD(cref, cref->nd_next);
}
return cref;
}
static inline VALUE
vm_get_cbase(const rb_iseq_t *iseq, const VALUE *ep)
{
NODE *cref = rb_vm_get_cref(iseq, ep);
VALUE klass = Qundef;
while (cref) {
if ((klass = cref->nd_clss) != 0) {
break;
}
cref = cref->nd_next;
}
return klass;
}
static inline VALUE
vm_get_const_base(const rb_iseq_t *iseq, const VALUE *ep)
{
NODE *cref = rb_vm_get_cref(iseq, ep);
VALUE klass = Qundef;
while (cref) {
if (!(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) &&
(klass = cref->nd_clss) != 0) {
break;
}
cref = cref->nd_next;
}
return klass;
}
static inline void
vm_check_if_namespace(VALUE klass)
{
VALUE str;
if (!RB_TYPE_P(klass, T_CLASS) && !RB_TYPE_P(klass, T_MODULE)) {
str = rb_inspect(klass);
rb_raise(rb_eTypeError, "%s is not a class/module",
StringValuePtr(str));
}
}
static inline VALUE
vm_get_iclass(rb_control_frame_t *cfp, VALUE klass)
{
if (RB_TYPE_P(klass, T_MODULE) &&
FL_TEST(klass, RMODULE_IS_OVERLAID) &&
RB_TYPE_P(cfp->klass, T_ICLASS) &&
RBASIC(cfp->klass)->klass == klass) {
return cfp->klass;
}
else {
return klass;
}
}
static inline VALUE
vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
VALUE orig_klass, ID id, int is_defined)
{
VALUE val;
if (orig_klass == Qnil) {
/* in current lexical scope */
const NODE *root_cref = rb_vm_get_cref(iseq, th->cfp->ep);
const NODE *cref;
VALUE klass = orig_klass;
while (root_cref && root_cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) {
root_cref = root_cref->nd_next;
}
cref = root_cref;
while (cref && cref->nd_next) {
if (cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL) {
klass = Qnil;
}
else {
klass = cref->nd_clss;
}
cref = cref->nd_next;
if (!NIL_P(klass)) {
VALUE av, am = 0;
rb_const_entry_t *ce;
search_continue:
if ((ce = rb_const_lookup(klass, id))) {
val = ce->value;
if (val == Qundef) {
if (am == klass) break;
am = klass;
if (is_defined) return 1;
if (rb_autoloading_value(klass, id, &av)) return av;
rb_autoload_load(klass, id);
goto search_continue;
}
else {
if (is_defined) {
return 1;
}
else {
return val;
}
}
}
}
}
/* search self */
if (root_cref && !NIL_P(root_cref->nd_clss)) {
klass = vm_get_iclass(th->cfp, root_cref->nd_clss);
}
else {
klass = CLASS_OF(th->cfp->self);
}
if (is_defined) {
return rb_const_defined(klass, id);
}
else {
return rb_const_get(klass, id);
}
}
else {
vm_check_if_namespace(orig_klass);
if (is_defined) {
return rb_public_const_defined_from(orig_klass, id);
}
else {
return rb_public_const_get_from(orig_klass, id);
}
}
}
static inline VALUE
vm_get_cvar_base(NODE *cref, rb_control_frame_t *cfp)
{
VALUE klass;
if (!cref) {
rb_bug("vm_get_cvar_base: no cref");
}
while (cref->nd_next &&
(NIL_P(cref->nd_clss) || FL_TEST(cref->nd_clss, FL_SINGLETON) ||
(cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL))) {
cref = cref->nd_next;
}
if (!cref->nd_next) {
rb_warn("class variable access from toplevel");
}
klass = vm_get_iclass(cfp, cref->nd_clss);
if (NIL_P(klass)) {
rb_raise(rb_eTypeError, "no class variables available");
}
return klass;
}
static VALUE
vm_search_const_defined_class(const VALUE cbase, ID id)
{
if (rb_const_defined_at(cbase, id)) return cbase;
if (cbase == rb_cObject) {
VALUE tmp = RCLASS_SUPER(cbase);
while (tmp) {
if (rb_const_defined_at(tmp, id)) return tmp;
tmp = RCLASS_SUPER(tmp);
}
}
return 0;
}
#ifndef USE_IC_FOR_IVAR
#define USE_IC_FOR_IVAR 1
#endif
static inline VALUE
vm_getivar(VALUE obj, ID id, IC ic, rb_call_info_t *ci, int is_attr)
{
#if USE_IC_FOR_IVAR
if (RB_TYPE_P(obj, T_OBJECT)) {
VALUE val = Qundef;
VALUE klass = RBASIC(obj)->klass;
const long len = ROBJECT_NUMIV(obj);
const VALUE *const ptr = ROBJECT_IVPTR(obj);
if (LIKELY(is_attr ? ci->aux.index > 0 : ic->ic_serial == RCLASS_SERIAL(klass))) {
int index = !is_attr ? (int)ic->ic_value.index : ci->aux.index - 1;
if (index < len) {
val = ptr[index];
}
}
else {
st_data_t index;
struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
if (iv_index_tbl) {
if (st_lookup(iv_index_tbl, id, &index)) {
if ((long)index < len) {
val = ptr[index];
}
if (!is_attr) {
ic->ic_value.index = index;
ic->ic_serial = RCLASS_SERIAL(klass);
}
else { /* call_info */
ci->aux.index = (int)index + 1;
}
}
}
}
if (UNLIKELY(val == Qundef)) {
if (!is_attr && RTEST(ruby_verbose))
rb_warning("instance variable %"PRIsVALUE" not initialized", QUOTE_ID(id));
val = Qnil;
}
return val;
}
#endif /* USE_IC_FOR_IVAR */
if (is_attr)
return rb_attr_get(obj, id);
return rb_ivar_get(obj, id);
}
static inline VALUE
vm_setivar(VALUE obj, ID id, VALUE val, IC ic, rb_call_info_t *ci, int is_attr)
{
#if USE_IC_FOR_IVAR
rb_check_frozen(obj);
if (RB_TYPE_P(obj, T_OBJECT)) {
VALUE klass = RBASIC(obj)->klass;
st_data_t index;
if (LIKELY(
(!is_attr && ic->ic_serial == RCLASS_SERIAL(klass)) ||
(is_attr && ci->aux.index > 0))) {
long index = !is_attr ? (long)ic->ic_value.index : ci->aux.index-1;
long len = ROBJECT_NUMIV(obj);
VALUE *ptr = ROBJECT_IVPTR(obj);
if (index < len) {
RB_OBJ_WRITE(obj, &ptr[index], val);
return val; /* inline cache hit */
}
}
else {
struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
if (!is_attr) {
ic->ic_value.index = index;
ic->ic_serial = RCLASS_SERIAL(klass);
}
else if (index >= INT_MAX) {
rb_raise(rb_eArgError, "too many instance variables");
}
else {
ci->aux.index = (int)(index + 1);
}
}
/* fall through */
}
}
#endif /* USE_IC_FOR_IVAR */
return rb_ivar_set(obj, id, val);
}
static VALUE
vm_getinstancevariable(VALUE obj, ID id, IC ic)
{
return vm_getivar(obj, id, ic, 0, 0);
}
static void
vm_setinstancevariable(VALUE obj, ID id, VALUE val, IC ic)
{
vm_setivar(obj, id, val, ic, 0, 0);
}
static VALUE
vm_throw_continue(rb_thread_t *th, VALUE throwobj)
{
/* continue throw */
VALUE err = throwobj;
if (FIXNUM_P(err)) {
th->state = FIX2INT(err);
}
else if (SYMBOL_P(err)) {
th->state = TAG_THROW;
}
else if (BUILTIN_TYPE(err) == T_NODE) {
th->state = GET_THROWOBJ_STATE(err);
}
else {
th->state = TAG_RAISE;
/*th->state = FIX2INT(rb_ivar_get(err, idThrowState));*/
}
return err;
}
static VALUE
vm_throw_start(rb_thread_t * const th, rb_control_frame_t * const reg_cfp, int state, const int flag, const rb_num_t level, const VALUE throwobj)
{
rb_control_frame_t *escape_cfp = NULL;
const rb_control_frame_t * const eocfp = RUBY_VM_END_CONTROL_FRAME(th); /* end of control frame pointer */
if (flag != 0) {
/* do nothing */
}
else if (state == TAG_BREAK) {
int is_orphan = 1;
VALUE *ep = GET_EP();
rb_iseq_t *base_iseq = GET_ISEQ();
escape_cfp = reg_cfp;
while (base_iseq->type != ISEQ_TYPE_BLOCK) {
if (escape_cfp->iseq->type == ISEQ_TYPE_CLASS) {
escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
ep = escape_cfp->ep;
base_iseq = escape_cfp->iseq;
}
else {
ep = VM_EP_PREV_EP(ep);
base_iseq = base_iseq->parent_iseq;
escape_cfp = rb_vm_search_cf_from_ep(th, escape_cfp, ep);
assert(escape_cfp->iseq == base_iseq);
}
}
if (VM_FRAME_TYPE(escape_cfp) == VM_FRAME_MAGIC_LAMBDA) {
/* lambda{... break ...} */
is_orphan = 0;
state = TAG_RETURN;
}
else {
ep = VM_EP_PREV_EP(ep);
while (escape_cfp < eocfp) {
if (escape_cfp->ep == ep) {
const VALUE epc = escape_cfp->pc - escape_cfp->iseq->iseq_encoded;
const rb_iseq_t * const iseq = escape_cfp->iseq;
const struct iseq_catch_table * const ct = iseq->catch_table;
const int ct_size = ct->size;
int i;
for (i=0; i<ct_size; i++) {
const struct iseq_catch_table_entry * const entry = &ct->entries[i];;
if (entry->type == CATCH_TYPE_BREAK && entry->start < epc && entry->end >= epc) {
if (entry->cont == epc) { /* found! */
is_orphan = 0;
}
break;
}
}
break;
}
escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
}
}
if (is_orphan) {
rb_vm_localjump_error("break from proc-closure", throwobj, TAG_BREAK);
}
}
else if (state == TAG_RETRY) {
rb_num_t i;
VALUE *ep = VM_EP_PREV_EP(GET_EP());
for (i = 0; i < level; i++) {
ep = VM_EP_PREV_EP(ep);
}
escape_cfp = rb_vm_search_cf_from_ep(th, reg_cfp, ep);
}
else if (state == TAG_RETURN) {
VALUE *current_ep = GET_EP();
VALUE *target_lep = VM_EP_LEP(current_ep);
int in_class_frame = 0;
escape_cfp = reg_cfp;
while (escape_cfp < eocfp) {
VALUE *lep = VM_CF_LEP(escape_cfp);
if (!target_lep) {
target_lep = lep;
}
if (lep == target_lep && escape_cfp->iseq->type == ISEQ_TYPE_CLASS) {
in_class_frame = 1;
target_lep = 0;
}
if (lep == target_lep) {
if (VM_FRAME_TYPE(escape_cfp) == VM_FRAME_MAGIC_LAMBDA) {
if (in_class_frame) {
/* lambda {class A; ... return ...; end} */
goto valid_return;
}
else {
VALUE *tep = current_ep;
while (target_lep != tep) {
if (escape_cfp->ep == tep) {
/* in lambda */
goto valid_return;
}
tep = VM_EP_PREV_EP(tep);
}
}
}
}
if (escape_cfp->ep == target_lep && escape_cfp->iseq->type == ISEQ_TYPE_METHOD) {
goto valid_return;
}
escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
}
rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN);
valid_return:;
/* do nothing */
}
else {
rb_bug("isns(throw): unsupport throw type");
}
th->state = state;
return (VALUE)NEW_THROW_OBJECT(throwobj, (VALUE)escape_cfp, state);
}
static VALUE
vm_throw(rb_thread_t *th, rb_control_frame_t *reg_cfp,
rb_num_t throw_state, VALUE throwobj)
{
const int state = (int)(throw_state & 0xff);
const int flag = (int)(throw_state & 0x8000);
const rb_num_t level = throw_state >> 16;
if (state != 0) {
return vm_throw_start(th, reg_cfp, state, flag, level, throwobj);
}
else {
return vm_throw_continue(th, throwobj);
}
}
static inline void
vm_expandarray(rb_control_frame_t *cfp, VALUE ary, rb_num_t num, int flag)
{
int is_splat = flag & 0x01;
rb_num_t space_size = num + is_splat;
VALUE *base = cfp->sp;
const VALUE *ptr;
rb_num_t len;
if (!RB_TYPE_P(ary, T_ARRAY)) {
ary = rb_ary_to_ary(ary);
}
cfp->sp += space_size;
ptr = RARRAY_CONST_PTR(ary);
len = (rb_num_t)RARRAY_LEN(ary);
if (flag & 0x02) {
/* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */
rb_num_t i = 0, j;
if (len < num) {
for (i=0; i<num-len; i++) {
*base++ = Qnil;
}
}
for (j=0; i<num; i++, j++) {
VALUE v = ptr[len - j - 1];
*base++ = v;
}
if (is_splat) {
*base = rb_ary_new4(len - j, ptr);
}
}
else {
/* normal: ary[num..-1], ary[num-2], ary[num-3], ..., ary[0] # top */
rb_num_t i;
VALUE *bptr = &base[space_size - 1];
for (i=0; i<num; i++) {
if (len <= i) {
for (; i<num; i++) {
*bptr-- = Qnil;
}
break;
}
*bptr-- = ptr[i];
}
if (is_splat) {
if (num > len) {
*bptr = rb_ary_new();
}
else {
*bptr = rb_ary_new4(len - num, ptr + num);
}
}
}
RB_GC_GUARD(ary);
}
static VALUE vm_call_general(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *ci);
static void
vm_search_method(rb_call_info_t *ci, VALUE recv)
{
VALUE klass = CLASS_OF(recv);
#if OPT_INLINE_METHOD_CACHE
if (LIKELY(GET_GLOBAL_METHOD_STATE() == ci->method_state && RCLASS_SERIAL(klass) == ci->class_serial)) {
/* cache hit! */
return;
}
#endif
ci->me = rb_method_entry(klass, ci->mid, &ci->defined_class);
ci->klass = klass;
ci->call = vm_call_general;
#if OPT_INLINE_METHOD_CACHE
ci->method_state = GET_GLOBAL_METHOD_STATE();
ci->class_serial = RCLASS_SERIAL(klass);
#endif
}
static inline int
check_cfunc(const rb_method_entry_t *me, VALUE (*func)())
{
if (me && me->def->type == VM_METHOD_TYPE_CFUNC &&
me->def->body.cfunc.func == func) {
return 1;
}
else {
return 0;
}
}
static
#ifndef NO_BIG_INLINE
inline
#endif
VALUE
opt_eq_func(VALUE recv, VALUE obj, CALL_INFO ci)
{
if (FIXNUM_2_P(recv, obj) &&
BASIC_OP_UNREDEFINED_P(BOP_EQ, FIXNUM_REDEFINED_OP_FLAG)) {
return (recv == obj) ? Qtrue : Qfalse;
}
else if (FLONUM_2_P(recv, obj) &&
BASIC_OP_UNREDEFINED_P(BOP_EQ, FLOAT_REDEFINED_OP_FLAG)) {
return (recv == obj) ? Qtrue : Qfalse;
}
else if (!SPECIAL_CONST_P(recv) && !SPECIAL_CONST_P(obj)) {
if (RBASIC_CLASS(recv) == rb_cFloat &&
RBASIC_CLASS(obj) == rb_cFloat &&
BASIC_OP_UNREDEFINED_P(BOP_EQ, FLOAT_REDEFINED_OP_FLAG)) {
double a = RFLOAT_VALUE(recv);
double b = RFLOAT_VALUE(obj);
if (isnan(a) || isnan(b)) {
return Qfalse;
}
return (a == b) ? Qtrue : Qfalse;
}
else if (RBASIC_CLASS(recv) == rb_cString &&
RBASIC_CLASS(obj) == rb_cString &&
BASIC_OP_UNREDEFINED_P(BOP_EQ, STRING_REDEFINED_OP_FLAG)) {
return rb_str_equal(recv, obj);
}
}
{
vm_search_method(ci, recv);
if (check_cfunc(ci->me, rb_obj_equal)) {
return recv == obj ? Qtrue : Qfalse;
}
}
return Qundef;
}
VALUE
rb_equal_opt(VALUE obj1, VALUE obj2)
{
rb_call_info_t ci;
ci.mid = idEq;
ci.klass = 0;
ci.method_state = 0;
ci.me = NULL;
ci.defined_class = 0;
return opt_eq_func(obj1, obj2, &ci);
}
static VALUE
vm_call0(rb_thread_t*, VALUE, ID, int, const VALUE*, const rb_method_entry_t*, VALUE);
static VALUE
check_match(VALUE pattern, VALUE target, enum vm_check_match_type type)
{
switch (type) {
case VM_CHECKMATCH_TYPE_WHEN:
return pattern;
case VM_CHECKMATCH_TYPE_RESCUE:
if (!rb_obj_is_kind_of(pattern, rb_cModule)) {
rb_raise(rb_eTypeError, "class or module required for rescue clause");
}
/* fall through */
case VM_CHECKMATCH_TYPE_CASE: {
VALUE defined_class;
rb_method_entry_t *me = rb_method_entry_with_refinements(CLASS_OF(pattern), idEqq, &defined_class);
if (me) {
return vm_call0(GET_THREAD(), pattern, idEqq, 1, &target, me, defined_class);
}
else {
/* fallback to funcall (e.g. method_missing) */
return rb_funcall2(pattern, idEqq, 1, &target);
}
}
default:
rb_bug("check_match: unreachable");
}
}
#if defined(_MSC_VER) && _MSC_VER < 1300
#define CHECK_CMP_NAN(a, b) if (isnan(a) || isnan(b)) return Qfalse;
#else
#define CHECK_CMP_NAN(a, b) /* do nothing */
#endif
static inline VALUE
double_cmp_lt(double a, double b)
{
CHECK_CMP_NAN(a, b);
return a < b ? Qtrue : Qfalse;
}
static inline VALUE
double_cmp_le(double a, double b)
{
CHECK_CMP_NAN(a, b);
return a <= b ? Qtrue : Qfalse;
}
static inline VALUE
double_cmp_gt(double a, double b)
{
CHECK_CMP_NAN(a, b);
return a > b ? Qtrue : Qfalse;
}
static inline VALUE
double_cmp_ge(double a, double b)
{
CHECK_CMP_NAN(a, b);
return a >= b ? Qtrue : Qfalse;
}
static VALUE *
vm_base_ptr(rb_control_frame_t *cfp)
{
rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
VALUE *bp = prev_cfp->sp + cfp->iseq->local_size + 1;
if (cfp->iseq->type == ISEQ_TYPE_METHOD) {
/* adjust `self' */
bp += 1;
}
#if VM_DEBUG_BP_CHECK
if (bp != cfp->bp_check) {
fprintf(stderr, "bp_check: %ld, bp: %ld\n",
(long)(cfp->bp_check - GET_THREAD()->stack),
(long)(bp - GET_THREAD()->stack));