2 ** codegen.c - mruby code generator
4 ** See Copyright Notice in mruby.h
12 #include "mruby/compile.h"
13 #include "mruby/proc.h"
14 #include "mruby/numeric.h"
15 #include "mruby/string.h"
16 #include "mruby/debug.h"
20 #include "mrb_throw.h"
22 typedef mrb_ast_node node
;
23 typedef struct mrb_parser_state parser_state
;
35 int pc1
, pc2
, pc3
, acc
;
37 struct loopinfo
*prev
;
40 typedef struct scope
{
43 struct mrb_jmpbuf jmp
;
55 struct loopinfo
*loop
;
74 uint16_t filename_index
;
78 static codegen_scope
* scope_new(mrb_state
*mrb
, codegen_scope
*prev
, node
*lv
);
79 static void scope_finish(codegen_scope
*s
);
80 static struct loopinfo
*loop_push(codegen_scope
*s
, enum looptype t
);
81 static void loop_break(codegen_scope
*s
, node
*tree
);
82 static void loop_pop(codegen_scope
*s
, int val
);
84 static void gen_assignment(codegen_scope
*s
, node
*node
, int sp
, int val
);
85 static void gen_vmassignment(codegen_scope
*s
, node
*tree
, int rhs
, int val
);
87 static void codegen(codegen_scope
*s
, node
*tree
, int val
);
90 codegen_error(codegen_scope
*s
, const char *message
)
94 codegen_scope
*tmp
= s
->prev
;
95 mrb_pool_close(s
->mpool
);
99 if (s
->filename
&& s
->lineno
) {
100 fprintf(stderr
, "codegen error:%s:%d: %s\n", s
->filename
, s
->lineno
, message
);
103 fprintf(stderr
, "codegen error: %s\n", message
);
110 codegen_palloc(codegen_scope
*s
, size_t len
)
112 void *p
= mrb_pool_alloc(s
->mpool
, len
);
114 if (!p
) codegen_error(s
, "pool memory allocation");
119 codegen_malloc(codegen_scope
*s
, size_t len
)
121 void *p
= mrb_malloc_simple(s
->mrb
, len
);
123 if (!p
) codegen_error(s
, "mrb_malloc");
128 codegen_realloc(codegen_scope
*s
, void *p
, size_t len
)
130 p
= mrb_realloc_simple(s
->mrb
, p
, len
);
132 if (!p
&& len
> 0) codegen_error(s
, "mrb_realloc");
137 new_label(codegen_scope
*s
)
139 s
->lastlabel
= s
->pc
;
144 genop(codegen_scope
*s
, mrb_code i
)
146 if (s
->pc
== s
->icapa
) {
148 s
->iseq
= (mrb_code
*)codegen_realloc(s
, s
->iseq
, sizeof(mrb_code
)*s
->icapa
);
150 s
->lines
= (uint16_t*)codegen_realloc(s
, s
->lines
, sizeof(short)*s
->icapa
);
151 s
->irep
->lines
= s
->lines
;
156 s
->lines
[s
->pc
] = s
->lineno
;
165 genop_peep(codegen_scope
*s
, mrb_code i
, int val
)
167 /* peephole optimization */
168 if (s
->lastlabel
!= s
->pc
&& s
->pc
> 0) {
169 mrb_code i0
= s
->iseq
[s
->pc
-1];
170 int c1
= GET_OPCODE(i
);
171 int c0
= GET_OPCODE(i0
);
175 if (GETARG_A(i
) == GETARG_B(i
)) {
176 /* skip useless OP_MOVE */
182 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i
) == GETARG_B(i0
) && GETARG_A(i
) >= s
->nlocals
) {
183 /* skip swapping OP_MOVE */
186 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
187 s
->iseq
[s
->pc
-1] = MKOP_AB(OP_MOVE
, GETARG_A(i
), GETARG_B(i0
));
192 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
193 s
->iseq
[s
->pc
-1] = MKOP_AsBx(OP_LOADI
, GETARG_A(i
), GETARG_sBx(i0
));
202 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
203 s
->iseq
[s
->pc
-1] = MKOP_ABC(c0
, GETARG_A(i
), GETARG_B(i0
), GETARG_C(i0
));
215 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
216 s
->iseq
[s
->pc
-1] = MKOP_ABx(c0
, GETARG_A(i
), GETARG_Bx(i0
));
221 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
222 s
->iseq
[s
->pc
-1] = MKOP_AB(c0
, GETARG_A(i
), GETARG_B(i0
));
231 if (GETARG_B(i
) == GETARG_A(i0
) && GETARG_A(i0
) >= s
->nlocals
) {
232 s
->iseq
[s
->pc
-1] = MKOP_A(c0
, GETARG_A(i
));
247 if (GETARG_A(i
) == GETARG_A(i0
)) {
248 s
->iseq
[s
->pc
-1] = MKOP_ABx(c1
, GETARG_B(i0
), GETARG_Bx(i
));
256 if (GETARG_A(i
) == GETARG_A(i0
)) {
257 s
->iseq
[s
->pc
-1] = MKOP_ABC(c1
, GETARG_B(i0
), GETARG_B(i
), GETARG_C(i
));
264 s
->iseq
[s
->pc
-1] = MKOP_A(OP_EPOP
, GETARG_A(i0
)+GETARG_A(i
));
269 if (c0
== OP_POPERR
) {
270 s
->iseq
[s
->pc
-1] = MKOP_A(OP_POPERR
, GETARG_A(i0
)+GETARG_A(i
));
279 s
->iseq
[s
->pc
-1] = MKOP_AB(OP_RETURN
, GETARG_B(i0
), OP_R_NORMAL
);
288 genop_peep(s
, i0
, NOVAL
);
289 i0
= s
->iseq
[s
->pc
-1];
290 return genop(s
, MKOP_AB(OP_RETURN
, GETARG_A(i0
), OP_R_NORMAL
));
293 if (GETARG_B(i
) == OP_R_NORMAL
&& GETARG_A(i
) == GETARG_A(i0
)) {
294 s
->iseq
[s
->pc
-1] = MKOP_ABC(OP_TAILCALL
, GETARG_A(i0
), GETARG_B(i0
), GETARG_C(i0
));
305 if (c0
== OP_LOADI
) {
306 int c
= GETARG_sBx(i0
);
308 if (c1
== OP_SUB
) c
= -c
;
309 if (c
> 127 || c
< -127) break;
311 s
->iseq
[s
->pc
-1] = MKOP_ABC(OP_ADDI
, GETARG_A(i
), GETARG_B(i
), c
);
313 s
->iseq
[s
->pc
-1] = MKOP_ABC(OP_SUBI
, GETARG_A(i
), GETARG_B(i
), -c
);
317 if (c0
== OP_STRING
) {
318 int i
= GETARG_Bx(i0
);
320 if (mrb_type(s
->irep
->pool
[i
]) == MRB_TT_STRING
&&
321 RSTRING_LEN(s
->irep
->pool
[i
]) == 0) {
329 if (c0
== OP_MOVE
&& GETARG_A(i
) == GETARG_A(i0
)) {
330 s
->iseq
[s
->pc
-1] = MKOP_AsBx(c1
, GETARG_B(i0
), GETARG_sBx(i
));
342 scope_error(codegen_scope
*s
)
348 dispatch(codegen_scope
*s
, int pc
)
350 int diff
= s
->pc
- pc
;
351 mrb_code i
= s
->iseq
[pc
];
352 int c
= GET_OPCODE(i
);
354 s
->lastlabel
= s
->pc
;
363 fprintf(stderr
, "bug: dispatch on non JMP op\n");
368 s
->iseq
[pc
] = MKOP_AsBx(c
, GETARG_A(i
), diff
);
372 dispatch_linked(codegen_scope
*s
, int pc
)
387 #define nregs_update do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0)
389 push_(codegen_scope
*s
)
392 codegen_error(s
, "too complex expression");
398 #define push() push_(s)
399 #define pop_(s) ((s)->sp--)
400 #define pop() pop_(s)
401 #define pop_n(n) (s->sp-=(n))
402 #define cursp() (s->sp)
405 new_lit(codegen_scope
*s
, mrb_value val
)
410 switch (mrb_type(val
)) {
412 for (i
=0; i
<s
->irep
->plen
; i
++) {
414 pv
= &s
->irep
->pool
[i
];
416 if (mrb_type(*pv
) != MRB_TT_STRING
) continue;
417 if ((len
= RSTRING_LEN(*pv
)) != RSTRING_LEN(val
)) continue;
418 if (memcmp(RSTRING_PTR(*pv
), RSTRING_PTR(val
), len
) == 0)
423 for (i
=0; i
<s
->irep
->plen
; i
++) {
424 pv
= &s
->irep
->pool
[i
];
425 if (mrb_type(*pv
) != MRB_TT_FLOAT
) continue;
426 if (mrb_float(*pv
) == mrb_float(val
)) return i
;
430 for (i
=0; i
<s
->irep
->plen
; i
++) {
431 pv
= &s
->irep
->pool
[i
];
432 if (!mrb_fixnum_p(*pv
)) continue;
433 if (mrb_fixnum(*pv
) == mrb_fixnum(val
)) return i
;
437 /* should not happen */
441 if (s
->irep
->plen
== s
->pcapa
) {
443 s
->irep
->pool
= (mrb_value
*)codegen_realloc(s
, s
->irep
->pool
, sizeof(mrb_value
)*s
->pcapa
);
446 pv
= &s
->irep
->pool
[s
->irep
->plen
];
449 switch (mrb_type(val
)) {
451 *pv
= mrb_str_pool(s
->mrb
, val
);
455 #ifdef MRB_WORD_BOXING
456 *pv
= mrb_float_pool(s
->mrb
, mrb_float(val
));
464 /* should not happen */
471 new_msym(codegen_scope
*s
, mrb_sym sym
)
476 if (len
> 256) len
= 256;
477 for (i
=0; i
<len
; i
++) {
478 if (s
->irep
->syms
[i
] == sym
) return i
;
479 if (s
->irep
->syms
[i
] == 0) break;
482 codegen_error(s
, "too many symbols (max 256)");
484 s
->irep
->syms
[i
] = sym
;
485 if (i
== s
->irep
->slen
) s
->irep
->slen
++;
490 new_sym(codegen_scope
*s
, mrb_sym sym
)
494 for (i
=0; i
<s
->irep
->slen
; i
++) {
495 if (s
->irep
->syms
[i
] == sym
) return i
;
497 if (s
->irep
->slen
> 125 && s
->irep
->slen
< 256) {
498 s
->irep
->syms
= (mrb_sym
*)codegen_realloc(s
, s
->irep
->syms
, sizeof(mrb_sym
)*65536);
499 for (i
= 0; i
< 256 - s
->irep
->slen
; i
++) {
500 static const mrb_sym mrb_sym_zero
= { 0 };
501 s
->irep
->syms
[i
+ s
->irep
->slen
] = mrb_sym_zero
;
505 s
->irep
->syms
[s
->irep
->slen
] = sym
;
506 return s
->irep
->slen
++;
521 #define sym(x) ((mrb_sym)(intptr_t)(x))
522 #define lv_name(lv) sym((lv)->car)
524 lv_idx(codegen_scope
*s
, mrb_sym id
)
530 if (lv_name(lv
) == id
) return n
;
538 for_body(codegen_scope
*s
, node
*tree
)
540 codegen_scope
*prev
= s
;
546 /* generate receiver */
547 codegen(s
, tree
->cdr
->car
, VAL
);
548 /* generate loop-block */
549 s
= scope_new(s
->mrb
, s
, tree
->car
);
551 lp
= loop_push(s
, LOOP_FOR
);
552 lp
->pc1
= new_label(s
);
554 /* generate loop variable */
556 if (n2
->car
&& !n2
->car
->cdr
&& !n2
->cdr
) {
557 genop(s
, MKOP_Ax(OP_ENTER
, 0x40000));
558 gen_assignment(s
, n2
->car
->car
, 1, NOVAL
);
561 genop(s
, MKOP_Ax(OP_ENTER
, 0x40000));
562 gen_vmassignment(s
, n2
, 1, VAL
);
564 codegen(s
, tree
->cdr
->cdr
->car
, VAL
);
567 c
= s
->iseq
[s
->pc
-1];
568 if (GET_OPCODE(c
) != OP_RETURN
|| GETARG_B(c
) != OP_R_NORMAL
|| s
->pc
== s
->lastlabel
)
569 genop_peep(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_NORMAL
), NOVAL
);
574 genop(s
, MKOP_Abc(OP_LAMBDA
, cursp(), s
->irep
->rlen
-1, OP_L_BLOCK
));
576 idx
= new_msym(s
, mrb_intern_lit(s
->mrb
, "each"));
577 genop(s
, MKOP_ABC(OP_SENDB
, cursp(), idx
, 0));
581 lambda_body(codegen_scope
*s
, node
*tree
, int blk
)
584 codegen_scope
*parent
= s
;
585 s
= scope_new(s
->mrb
, s
, tree
->car
);
589 struct loopinfo
*lp
= loop_push(s
, LOOP_BLOCK
);
590 lp
->pc1
= new_label(s
);
595 int ma
, oa
, ra
, pa
, ka
, kd
, ba
;
599 ma
= node_len(tree
->car
->car
);
604 oa
= node_len(tree
->car
->cdr
->car
);
605 ra
= tree
->car
->cdr
->cdr
->car
? 1 : 0;
606 pa
= node_len(tree
->car
->cdr
->cdr
->cdr
->car
);
608 ba
= tree
->car
->cdr
->cdr
->cdr
->cdr
? 1 : 0;
610 a
= ((mrb_aspec
)(ma
& 0x1f) << 18)
611 | ((mrb_aspec
)(oa
& 0x1f) << 13)
617 s
->ainfo
= (((ma
+oa
) & 0x3f) << 6) /* (12bits = 6:1:5) */
620 genop(s
, MKOP_Ax(OP_ENTER
, a
));
622 for (i
=0; i
<oa
; i
++) {
624 genop(s
, MKOP_sBx(OP_JMP
, 0));
627 genop(s
, MKOP_sBx(OP_JMP
, 0));
629 opt
= tree
->car
->cdr
->car
;
635 codegen(s
, opt
->car
->cdr
, VAL
);
636 idx
= lv_idx(s
, (mrb_sym
)(intptr_t)opt
->car
->car
);
638 genop_peep(s
, MKOP_AB(OP_MOVE
, idx
, cursp()), NOVAL
);
646 codegen(s
, tree
->cdr
->car
, VAL
);
649 c
= s
->iseq
[s
->pc
-1];
650 if (GET_OPCODE(c
) != OP_RETURN
|| GETARG_B(c
) != OP_R_NORMAL
|| s
->pc
== s
->lastlabel
) {
652 genop(s
, MKOP_A(OP_LOADNIL
, 0));
653 genop(s
, MKOP_AB(OP_RETURN
, 0, OP_R_NORMAL
));
656 genop_peep(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_NORMAL
), NOVAL
);
664 return parent
->irep
->rlen
- 1;
668 scope_body(codegen_scope
*s
, node
*tree
, int val
)
670 codegen_scope
*scope
= scope_new(s
->mrb
, s
, tree
->car
);
672 codegen(scope
, tree
->cdr
, VAL
);
674 genop(scope
, MKOP_A(OP_STOP
, 0));
677 genop(scope
, MKOP_AB(OP_RETURN
, 0, OP_R_NORMAL
));
680 if (scope
->nregs
== 0) {
681 genop(scope
, MKOP_A(OP_LOADNIL
, 0));
682 genop(scope
, MKOP_AB(OP_RETURN
, 0, OP_R_NORMAL
));
685 genop_peep(scope
, MKOP_AB(OP_RETURN
, scope
->sp
-1, OP_R_NORMAL
), NOVAL
);
690 /* should not happen */
693 return s
->irep
->rlen
- 1;
700 if ((intptr_t)t
->car
->car
== NODE_SPLAT
) return FALSE
;
707 attrsym(codegen_scope
*s
, mrb_sym a
)
713 name
= mrb_sym2name_len(s
->mrb
, a
, &len
);
714 name2
= (char *)codegen_palloc(s
,
719 memcpy(name2
, name
, (size_t)len
);
723 return mrb_intern(s
->mrb
, name2
, len
+1);
727 gen_values(codegen_scope
*s
, node
*t
, int val
)
733 is_splat
= (intptr_t)t
->car
->car
== NODE_SPLAT
; /* splat mode */
734 if (n
>= 127 || is_splat
) {
737 genop(s
, MKOP_ABC(OP_ARRAY
, cursp(), cursp(), n
));
739 codegen(s
, t
->car
, VAL
);
742 genop(s
, MKOP_AB(OP_ARYCAT
, cursp(), cursp()+1));
745 genop(s
, MKOP_AB(OP_ARYPUSH
, cursp(), cursp()+1));
750 codegen(s
, t
->car
, VAL
);
752 if ((intptr_t)t
->car
->car
== NODE_SPLAT
) {
753 genop(s
, MKOP_AB(OP_ARYCAT
, cursp(), cursp()+1));
756 genop(s
, MKOP_AB(OP_ARYPUSH
, cursp(), cursp()+1));
762 codegen(s
, t
->car
->cdr
, NOVAL
);
765 codegen(s
, t
->car
, NOVAL
);
771 /* normal (no splat) mode */
772 codegen(s
, t
->car
, val
);
779 #define CALL_MAXARGS 127
782 gen_call(codegen_scope
*s
, node
*tree
, mrb_sym name
, int sp
, int val
)
784 mrb_sym sym
= name
? name
: sym(tree
->cdr
->car
);
786 int n
= 0, noop
= 0, sendv
= 0, blk
= 0;
788 codegen(s
, tree
->car
, VAL
); /* receiver */
789 idx
= new_msym(s
, sym
);
790 tree
= tree
->cdr
->cdr
->car
;
792 n
= gen_values(s
, tree
->car
, VAL
);
794 n
= noop
= sendv
= 1;
801 genop(s
, MKOP_AB(OP_ARYPUSH
, cursp(), sp
));
805 genop(s
, MKOP_AB(OP_MOVE
, cursp(), sp
));
810 if (tree
&& tree
->cdr
) {
812 codegen(s
, tree
->cdr
, VAL
);
821 const char *name
= mrb_sym2name_len(s
->mrb
, sym
, &len
);
823 if (!noop
&& len
== 1 && name
[0] == '+') {
824 genop_peep(s
, MKOP_ABC(OP_ADD
, cursp(), idx
, n
), val
);
826 else if (!noop
&& len
== 1 && name
[0] == '-') {
827 genop_peep(s
, MKOP_ABC(OP_SUB
, cursp(), idx
, n
), val
);
829 else if (!noop
&& len
== 1 && name
[0] == '*') {
830 genop(s
, MKOP_ABC(OP_MUL
, cursp(), idx
, n
));
832 else if (!noop
&& len
== 1 && name
[0] == '/') {
833 genop(s
, MKOP_ABC(OP_DIV
, cursp(), idx
, n
));
835 else if (!noop
&& len
== 1 && name
[0] == '<') {
836 genop(s
, MKOP_ABC(OP_LT
, cursp(), idx
, n
));
838 else if (!noop
&& len
== 2 && name
[0] == '<' && name
[1] == '=') {
839 genop(s
, MKOP_ABC(OP_LE
, cursp(), idx
, n
));
841 else if (!noop
&& len
== 1 && name
[0] == '>') {
842 genop(s
, MKOP_ABC(OP_GT
, cursp(), idx
, n
));
844 else if (!noop
&& len
== 2 && name
[0] == '>' && name
[1] == '=') {
845 genop(s
, MKOP_ABC(OP_GE
, cursp(), idx
, n
));
847 else if (!noop
&& len
== 2 && name
[0] == '=' && name
[1] == '=') {
848 genop(s
, MKOP_ABC(OP_EQ
, cursp(), idx
, n
));
851 if (sendv
) n
= CALL_MAXARGS
;
852 if (blk
> 0) { /* no block */
853 genop(s
, MKOP_ABC(OP_SEND
, cursp(), idx
, n
));
856 genop(s
, MKOP_ABC(OP_SENDB
, cursp(), idx
, n
));
866 gen_assignment(codegen_scope
*s
, node
*node
, int sp
, int val
)
869 int type
= (intptr_t)node
->car
;
872 switch ((intptr_t)type
) {
874 idx
= new_sym(s
, sym(node
));
875 genop_peep(s
, MKOP_ABx(OP_SETGLOBAL
, sp
, idx
), val
);
878 idx
= lv_idx(s
, sym(node
));
881 genop_peep(s
, MKOP_AB(OP_MOVE
, idx
, sp
), val
);
887 codegen_scope
*up
= s
->prev
;
890 idx
= lv_idx(up
, sym(node
));
892 genop_peep(s
, MKOP_ABC(OP_SETUPVAR
, sp
, idx
, lv
), val
);
901 idx
= new_sym(s
, sym(node
));
902 genop_peep(s
, MKOP_ABx(OP_SETIV
, sp
, idx
), val
);
905 idx
= new_sym(s
, sym(node
));
906 genop_peep(s
, MKOP_ABx(OP_SETCV
, sp
, idx
), val
);
909 idx
= new_sym(s
, sym(node
));
910 genop_peep(s
, MKOP_ABx(OP_SETCONST
, sp
, idx
), val
);
913 idx
= new_sym(s
, sym(node
->cdr
));
914 genop_peep(s
, MKOP_AB(OP_MOVE
, cursp(), sp
), NOVAL
);
916 codegen(s
, node
->car
, VAL
);
918 genop_peep(s
, MKOP_ABx(OP_SETMCNST
, cursp(), idx
), val
);
923 gen_call(s
, node
, attrsym(s
, sym(node
->cdr
->car
)), sp
, NOVAL
);
926 genop_peep(s
, MKOP_AB(OP_MOVE
, cursp(), sp
), val
);
932 printf("unknown lhs %d\n", type
);
940 gen_vmassignment(codegen_scope
*s
, node
*tree
, int rhs
, int val
)
945 if (tree
->car
) { /* pre */
949 genop(s
, MKOP_ABC(OP_AREF
, cursp(), rhs
, n
));
950 gen_assignment(s
, t
->car
, cursp(), NOVAL
);
957 if (t
->cdr
) { /* post count */
965 genop(s
, MKOP_AB(OP_MOVE
, cursp(), rhs
));
969 genop(s
, MKOP_ABC(OP_APOST
, cursp(), n
, post
));
971 if (t
->car
) { /* rest */
972 gen_assignment(s
, t
->car
, cursp(), NOVAL
);
974 if (t
->cdr
&& t
->cdr
->car
) {
977 gen_assignment(s
, t
->car
, cursp()+n
, NOVAL
);
989 gen_send_intern(codegen_scope
*s
)
992 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "intern")), 0));
996 gen_literal_array(codegen_scope
*s
, node
*tree
, mrb_bool sym
, int val
)
1002 switch ((intptr_t)tree
->car
->car
) {
1004 if ((tree
->cdr
== NULL
) && ((intptr_t)tree
->car
->cdr
->cdr
== 0))
1008 codegen(s
, tree
->car
, VAL
);
1012 case NODE_LITERAL_DELIM
:
1023 genop_peep(s
, MKOP_AB(OP_STRCAT
, cursp(), cursp()+1), VAL
);
1035 genop(s
, MKOP_ABC(OP_ARRAY
, cursp(), cursp(), i
));
1040 switch ((intptr_t)tree
->car
->car
) {
1041 case NODE_BEGIN
: case NODE_BLOCK
:
1042 codegen(s
, tree
->car
, NOVAL
);
1050 raise_error(codegen_scope
*s
, const char *msg
)
1052 int idx
= new_lit(s
, mrb_str_new_cstr(s
->mrb
, msg
));
1054 genop(s
, MKOP_ABx(OP_ERR
, 1, idx
));
1058 readint_float(codegen_scope
*s
, const char *p
, int base
)
1060 const char *e
= p
+ strlen(p
);
1067 c
= tolower((unsigned char)c
);
1068 for (n
=0; n
<base
; n
++) {
1069 if (mrb_digitmap
[n
] == c
) {
1076 codegen_error(s
, "malformed readint input");
1084 readint_mrb_int(codegen_scope
*s
, const char *p
, int base
, mrb_bool neg
, mrb_bool
*overflow
)
1086 const char *e
= p
+ strlen(p
);
1093 c
= tolower((unsigned char)c
);
1094 for (n
=0; n
<base
; n
++) {
1095 if (mrb_digitmap
[n
] == c
) {
1100 codegen_error(s
, "malformed readint input");
1104 if ((MRB_INT_MIN
+ n
)/base
> result
) {
1112 if ((MRB_INT_MAX
- n
)/base
< result
) {
1126 codegen(codegen_scope
*s
, node
*tree
, int val
)
1132 if (s
->irep
&& s
->pc
> 0 && s
->filename_index
!= tree
->filename_index
) {
1133 s
->irep
->filename
= mrb_parser_get_filename(s
->parser
, s
->filename_index
);
1134 mrb_debug_info_append_file(s
->mrb
, s
->irep
, s
->debug_start_pos
, s
->pc
);
1135 s
->debug_start_pos
= s
->pc
;
1136 s
->filename_index
= tree
->filename_index
;
1137 s
->filename
= mrb_parser_get_filename(s
->parser
, tree
->filename_index
);
1140 nt
= (intptr_t)tree
->car
;
1141 s
->lineno
= tree
->lineno
;
1146 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1150 codegen(s
, tree
->car
, tree
->cdr
? NOVAL
: val
);
1157 int onerr
, noexc
, exend
, pos1
, pos2
, tmp
;
1158 struct loopinfo
*lp
;
1160 onerr
= genop(s
, MKOP_Bx(OP_ONERR
, 0));
1161 lp
= loop_push(s
, LOOP_BEGIN
);
1164 codegen(s
, tree
->car
, val
);
1167 lp
->type
= LOOP_RESCUE
;
1168 noexc
= genop(s
, MKOP_Bx(OP_JMP
, 0));
1174 node
*n2
= tree
->car
;
1177 genop(s
, MKOP_A(OP_RESCUE
, exc
));
1183 if (pos1
) dispatch(s
, pos1
);
1187 codegen(s
, n4
->car
, VAL
);
1190 genop(s
, MKOP_ABx(OP_GETCONST
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "StandardError"))));
1193 genop(s
, MKOP_AB(OP_MOVE
, cursp(), exc
));
1195 if (n4
&& n4
->car
&& (intptr_t)n4
->car
->car
== NODE_SPLAT
) {
1196 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "__case_eqq")), 1));
1199 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "===")), 1));
1201 tmp
= genop(s
, MKOP_AsBx(OP_JMPIF
, cursp(), pos2
));
1207 pos1
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1208 dispatch_linked(s
, pos2
);
1212 gen_assignment(s
, n3
->cdr
->car
, exc
, NOVAL
);
1214 if (n3
->cdr
->cdr
->car
) {
1215 codegen(s
, n3
->cdr
->cdr
->car
, val
);
1218 tmp
= genop(s
, MKOP_sBx(OP_JMP
, exend
));
1225 genop(s
, MKOP_A(OP_RAISE
, exc
));
1231 genop(s
, MKOP_A(OP_POPERR
, 1));
1233 codegen(s
, tree
->car
, val
);
1238 dispatch_linked(s
, exend
);
1248 genop(s
, MKOP_Bx(OP_EPUSH
, 0));
1250 codegen(s
, tree
->car
, val
);
1251 idx
= scope_body(s
, tree
->cdr
, NOVAL
);
1252 s
->iseq
[epush
] = MKOP_Bx(OP_EPUSH
, idx
);
1254 genop_peep(s
, MKOP_A(OP_EPOP
, 1), NOVAL
);
1260 int idx
= lambda_body(s
, tree
, 1);
1262 genop(s
, MKOP_Abc(OP_LAMBDA
, cursp(), idx
, OP_L_LAMBDA
));
1269 int idx
= lambda_body(s
, tree
, 1);
1271 genop(s
, MKOP_Abc(OP_LAMBDA
, cursp(), idx
, OP_L_BLOCK
));
1279 node
*e
= tree
->cdr
->cdr
->car
;
1281 codegen(s
, tree
->car
, VAL
);
1283 pos1
= genop_peep(s
, MKOP_AsBx(OP_JMPNOT
, cursp(), 0), NOVAL
);
1285 codegen(s
, tree
->cdr
->car
, val
);
1286 if (val
&& !(tree
->cdr
->car
)) {
1287 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1292 pos2
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1300 pos2
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1302 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1317 codegen(s
, tree
->car
, VAL
);
1319 pos
= genop(s
, MKOP_AsBx(OP_JMPNOT
, cursp(), 0));
1320 codegen(s
, tree
->cdr
, val
);
1329 codegen(s
, tree
->car
, VAL
);
1331 pos
= genop(s
, MKOP_AsBx(OP_JMPIF
, cursp(), 0));
1332 codegen(s
, tree
->cdr
, val
);
1339 struct loopinfo
*lp
= loop_push(s
, LOOP_NORMAL
);
1341 lp
->pc1
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1342 lp
->pc2
= new_label(s
);
1343 codegen(s
, tree
->cdr
, NOVAL
);
1344 dispatch(s
, lp
->pc1
);
1345 codegen(s
, tree
->car
, VAL
);
1347 genop(s
, MKOP_AsBx(OP_JMPIF
, cursp(), lp
->pc2
- s
->pc
));
1355 struct loopinfo
*lp
= loop_push(s
, LOOP_NORMAL
);
1357 lp
->pc1
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1358 lp
->pc2
= new_label(s
);
1359 codegen(s
, tree
->cdr
, NOVAL
);
1360 dispatch(s
, lp
->pc1
);
1361 codegen(s
, tree
->car
, VAL
);
1363 genop(s
, MKOP_AsBx(OP_JMPNOT
, cursp(), lp
->pc2
- s
->pc
));
1377 int pos1
, pos2
, pos3
, tmp
;
1383 codegen(s
, tree
->car
, VAL
);
1390 codegen(s
, n
->car
, VAL
);
1392 genop(s
, MKOP_AB(OP_MOVE
, cursp(), head
));
1394 if ((intptr_t)n
->car
->car
== NODE_SPLAT
) {
1395 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "__case_eqq")), 1));
1398 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "===")), 1));
1404 tmp
= genop(s
, MKOP_AsBx(OP_JMPIF
, cursp(), pos2
));
1408 if (tree
->car
->car
) {
1409 pos1
= genop(s
, MKOP_sBx(OP_JMP
, 0));
1410 dispatch_linked(s
, pos2
);
1412 codegen(s
, tree
->car
->cdr
, val
);
1414 tmp
= genop(s
, MKOP_sBx(OP_JMP
, pos3
));
1416 if (pos1
) dispatch(s
, pos1
);
1421 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1422 if (pos3
) dispatch_linked(s
, pos3
);
1424 genop(s
, MKOP_AB(OP_MOVE
, cursp(), pos
));
1428 dispatch_linked(s
, pos3
);
1434 scope_body(s
, tree
, NOVAL
);
1439 gen_call(s
, tree
, 0, 0, val
);
1443 codegen(s
, tree
->car
, val
);
1444 codegen(s
, tree
->cdr
, val
);
1447 genop(s
, MKOP_ABC(OP_RANGE
, cursp(), cursp(), 0));
1453 codegen(s
, tree
->car
, val
);
1454 codegen(s
, tree
->cdr
, val
);
1457 genop(s
, MKOP_ABC(OP_RANGE
, cursp(), cursp(), 1));
1464 int sym
= new_sym(s
, sym(tree
->cdr
));
1466 codegen(s
, tree
->car
, VAL
);
1468 genop(s
, MKOP_ABx(OP_GETMCNST
, cursp(), sym
));
1475 int sym
= new_sym(s
, sym(tree
));
1477 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
1478 genop(s
, MKOP_ABx(OP_GETMCNST
, cursp(), sym
));
1487 n
= gen_values(s
, tree
, val
);
1491 genop(s
, MKOP_ABC(OP_ARRAY
, cursp(), cursp(), n
));
1506 codegen(s
, tree
->car
->car
, val
);
1507 codegen(s
, tree
->car
->cdr
, val
);
1513 genop(s
, MKOP_ABC(OP_HASH
, cursp(), cursp(), len
));
1520 codegen(s
, tree
, VAL
);
1524 codegen(s
, tree
->cdr
, VAL
);
1526 gen_assignment(s
, tree
->car
, cursp(), val
);
1531 int len
= 0, n
= 0, post
= 0;
1532 node
*t
= tree
->cdr
, *p
;
1535 if ((intptr_t)t
->car
== NODE_ARRAY
&& nosplat(t
->cdr
)) {
1539 codegen(s
, t
->car
, VAL
);
1544 if (tree
->car
) { /* pre */
1548 gen_assignment(s
, t
->car
, rhs
+n
, NOVAL
);
1555 if (t
->cdr
) { /* post count */
1562 if (t
->car
) { /* rest (len - pre - post) */
1563 int rn
= len
- post
- n
;
1565 genop(s
, MKOP_ABC(OP_ARRAY
, cursp(), rhs
+n
, rn
));
1566 gen_assignment(s
, t
->car
, cursp(), NOVAL
);
1569 if (t
->cdr
&& t
->cdr
->car
) {
1572 gen_assignment(s
, t
->car
, rhs
+n
, NOVAL
);
1580 genop(s
, MKOP_ABC(OP_ARRAY
, rhs
, rhs
, len
));
1587 gen_vmassignment(s
, tree
->car
, rhs
, val
);
1594 mrb_sym sym
= sym(tree
->cdr
->car
);
1596 const char *name
= mrb_sym2name_len(s
->mrb
, sym
, &len
);
1599 codegen(s
, tree
->car
, VAL
);
1601 ((name
[0] == '|' && name
[1] == '|') ||
1602 (name
[0] == '&' && name
[1] == '&'))) {
1606 pos
= genop_peep(s
, MKOP_AsBx(name
[0] == '|' ? OP_JMPIF
: OP_JMPNOT
, cursp(), 0), NOVAL
);
1607 codegen(s
, tree
->cdr
->cdr
->car
, VAL
);
1609 gen_assignment(s
, tree
->car
, cursp(), val
);
1613 codegen(s
, tree
->cdr
->cdr
->car
, VAL
);
1616 idx
= new_msym(s
, sym
);
1617 if (len
== 1 && name
[0] == '+') {
1618 genop_peep(s
, MKOP_ABC(OP_ADD
, cursp(), idx
, 1), val
);
1620 else if (len
== 1 && name
[0] == '-') {
1621 genop_peep(s
, MKOP_ABC(OP_SUB
, cursp(), idx
, 1), val
);
1623 else if (len
== 1 && name
[0] == '*') {
1624 genop(s
, MKOP_ABC(OP_MUL
, cursp(), idx
, 1));
1626 else if (len
== 1 && name
[0] == '/') {
1627 genop(s
, MKOP_ABC(OP_DIV
, cursp(), idx
, 1));
1629 else if (len
== 1 && name
[0] == '<') {
1630 genop(s
, MKOP_ABC(OP_LT
, cursp(), idx
, 1));
1632 else if (len
== 2 && name
[0] == '<' && name
[1] == '=') {
1633 genop(s
, MKOP_ABC(OP_LE
, cursp(), idx
, 1));
1635 else if (len
== 1 && name
[0] == '>') {
1636 genop(s
, MKOP_ABC(OP_GT
, cursp(), idx
, 1));
1638 else if (len
== 2 && name
[0] == '>' && name
[1] == '=') {
1639 genop(s
, MKOP_ABC(OP_GE
, cursp(), idx
, 1));
1642 genop(s
, MKOP_ABC(OP_SEND
, cursp(), idx
, 1));
1645 gen_assignment(s
, tree
->car
, cursp(), val
);
1650 int n
= 0, noop
= 0, sendv
= 0;
1652 push(); /* room for receiver */
1654 node
*args
= tree
->car
;
1656 n
= gen_values(s
, args
, VAL
);
1658 n
= noop
= sendv
= 1;
1663 if (tree
&& tree
->cdr
) {
1664 codegen(s
, tree
->cdr
, VAL
);
1668 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1671 if (sendv
) n
= CALL_MAXARGS
;
1672 genop(s
, MKOP_ABC(OP_SUPER
, cursp(), 0, n
));
1679 codegen_scope
*s2
= s
;
1680 int lv
= 0, ainfo
= 0;
1682 push(); /* room for receiver */
1683 while (!s2
->mscope
) {
1688 if (s2
) ainfo
= s2
->ainfo
;
1689 genop(s
, MKOP_ABx(OP_ARGARY
, cursp(), (ainfo
<<4)|(lv
& 0xf)));
1690 if (tree
&& tree
->cdr
) {
1692 codegen(s
, tree
->cdr
, VAL
);
1696 genop(s
, MKOP_ABC(OP_SUPER
, cursp(), 0, CALL_MAXARGS
));
1703 codegen(s
, tree
, VAL
);
1707 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1710 genop(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_RETURN
));
1713 genop_peep(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_NORMAL
), NOVAL
);
1720 codegen_scope
*s2
= s
;
1721 int lv
= 0, ainfo
= 0;
1722 int n
= 0, sendv
= 0;
1724 while (!s2
->mscope
) {
1729 if (s2
) ainfo
= s2
->ainfo
;
1730 genop(s
, MKOP_ABx(OP_BLKPUSH
, cursp(), (ainfo
<<4)|(lv
& 0xf)));
1733 n
= gen_values(s
, tree
, VAL
);
1740 if (sendv
) n
= CALL_MAXARGS
;
1741 genop(s
, MKOP_ABC(OP_SEND
, cursp(), new_msym(s
, mrb_intern_lit(s
->mrb
, "call")), n
));
1747 loop_break(s
, tree
);
1753 raise_error(s
, "unexpected next");
1755 else if (s
->loop
->type
== LOOP_NORMAL
) {
1756 if (s
->ensure_level
> s
->loop
->ensure_level
) {
1757 genop_peep(s
, MKOP_A(OP_EPOP
, s
->ensure_level
- s
->loop
->ensure_level
), NOVAL
);
1759 codegen(s
, tree
, NOVAL
);
1760 genop(s
, MKOP_sBx(OP_JMP
, s
->loop
->pc1
- s
->pc
));
1764 codegen(s
, tree
, VAL
);
1768 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
1770 genop_peep(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_NORMAL
), NOVAL
);
1777 raise_error(s
, "unexpected redo");
1780 if (s
->ensure_level
> s
->loop
->ensure_level
) {
1781 genop_peep(s
, MKOP_A(OP_EPOP
, s
->ensure_level
- s
->loop
->ensure_level
), NOVAL
);
1783 genop(s
, MKOP_sBx(OP_JMP
, s
->loop
->pc2
- s
->pc
));
1789 const char *msg
= "unexpected retry";
1792 raise_error(s
, msg
);
1795 struct loopinfo
*lp
= s
->loop
;
1798 while (lp
&& lp
->type
!= LOOP_RESCUE
) {
1799 if (lp
->type
== LOOP_BEGIN
) {
1805 raise_error(s
, msg
);
1810 genop_peep(s
, MKOP_A(OP_POPERR
, 1), NOVAL
);
1813 if (s
->ensure_level
> lp
->ensure_level
) {
1814 genop_peep(s
, MKOP_A(OP_EPOP
, s
->ensure_level
- lp
->ensure_level
), NOVAL
);
1816 genop(s
, MKOP_sBx(OP_JMP
, lp
->pc1
- s
->pc
));
1824 int idx
= lv_idx(s
, sym(tree
));
1827 genop(s
, MKOP_AB(OP_MOVE
, cursp(), idx
));
1831 codegen_scope
*up
= s
->prev
;
1834 idx
= lv_idx(up
, sym(tree
));
1836 genop(s
, MKOP_ABC(OP_GETUPVAR
, cursp(), idx
, lv
));
1849 int sym
= new_sym(s
, sym(tree
));
1851 genop(s
, MKOP_ABx(OP_GETGLOBAL
, cursp(), sym
));
1858 int sym
= new_sym(s
, sym(tree
));
1860 genop(s
, MKOP_ABx(OP_GETIV
, cursp(), sym
));
1867 int sym
= new_sym(s
, sym(tree
));
1869 genop(s
, MKOP_ABx(OP_GETCV
, cursp(), sym
));
1876 int sym
= new_sym(s
, sym(tree
));
1878 genop(s
, MKOP_ABx(OP_GETCONST
, cursp(), sym
));
1884 codegen(s
, tree
, VAL
);
1889 char buf
[2] = { '$' };
1893 buf
[1] = (char)(intptr_t)tree
;
1894 str
= mrb_str_new(s
->mrb
, buf
, 2);
1895 sym
= new_sym(s
, mrb_intern_str(s
->mrb
, str
));
1896 genop(s
, MKOP_ABx(OP_GETGLOBAL
, cursp(), sym
));
1904 mrb_state
*mrb
= s
->mrb
;
1905 mrb_value fix
= mrb_fixnum_value((intptr_t)tree
);
1906 mrb_value str
= mrb_str_buf_new(mrb
, 4);
1908 mrb_str_cat_lit(mrb
, str
, "$");
1909 mrb_str_buf_append(mrb
, str
, mrb_fixnum_to_str(mrb
, fix
, 10));
1910 sym
= new_sym(s
, mrb_intern_str(mrb
, str
));
1911 genop(s
, MKOP_ABx(OP_GETGLOBAL
, cursp(), sym
));
1917 /* should not happen */
1920 case NODE_BLOCK_ARG
:
1921 codegen(s
, tree
, VAL
);
1926 char *p
= (char*)tree
->car
;
1927 int base
= (intptr_t)tree
->cdr
->car
;
1932 i
= readint_mrb_int(s
, p
, base
, FALSE
, &overflow
);
1934 double f
= readint_float(s
, p
, base
);
1935 int off
= new_lit(s
, mrb_float_value(s
->mrb
, f
));
1937 genop(s
, MKOP_ABx(OP_LOADL
, cursp(), off
));
1940 if (i
< MAXARG_sBx
&& i
> -MAXARG_sBx
) {
1941 co
= MKOP_AsBx(OP_LOADI
, cursp(), i
);
1944 int off
= new_lit(s
, mrb_fixnum_value(i
));
1945 co
= MKOP_ABx(OP_LOADL
, cursp(), off
);
1955 char *p
= (char*)tree
;
1956 mrb_float f
= str_to_mrb_float(p
);
1957 int off
= new_lit(s
, mrb_float_value(s
->mrb
, f
));
1959 genop(s
, MKOP_ABx(OP_LOADL
, cursp(), off
));
1966 nt
= (intptr_t)tree
->car
;
1971 char *p
= (char*)tree
;
1972 mrb_float f
= str_to_mrb_float(p
);
1973 int off
= new_lit(s
, mrb_float_value(s
->mrb
, -f
));
1975 genop(s
, MKOP_ABx(OP_LOADL
, cursp(), off
));
1982 char *p
= (char*)tree
->car
;
1983 int base
= (intptr_t)tree
->cdr
->car
;
1988 i
= readint_mrb_int(s
, p
, base
, TRUE
, &overflow
);
1990 double f
= readint_float(s
, p
, base
);
1991 int off
= new_lit(s
, mrb_float_value(s
->mrb
, -f
));
1993 genop(s
, MKOP_ABx(OP_LOADL
, cursp(), off
));
1996 if (i
< MAXARG_sBx
&& i
> -MAXARG_sBx
) {
1997 co
= MKOP_AsBx(OP_LOADI
, cursp(), i
);
2000 int off
= new_lit(s
, mrb_fixnum_value(i
));
2001 co
= MKOP_ABx(OP_LOADL
, cursp(), off
);
2011 int sym
= new_msym(s
, mrb_intern_lit(s
->mrb
, "-"));
2013 genop(s
, MKOP_ABx(OP_LOADI
, cursp(), 0));
2015 codegen(s
, tree
, VAL
);
2017 genop(s
, MKOP_ABC(OP_SUB
, cursp(), sym
, 2));
2026 char *p
= (char*)tree
->car
;
2027 size_t len
= (intptr_t)tree
->cdr
;
2028 int ai
= mrb_gc_arena_save(s
->mrb
);
2029 int off
= new_lit(s
, mrb_str_new(s
->mrb
, p
, len
));
2031 mrb_gc_arena_restore(s
->mrb
, ai
);
2032 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2038 tree
= ((struct mrb_parser_heredoc_info
*)tree
)->doc
;
2044 codegen(s
, n
->car
, VAL
);
2047 codegen(s
, n
->car
, VAL
);
2049 genop_peep(s
, MKOP_AB(OP_STRCAT
, cursp(), cursp()+1), VAL
);
2058 if ((intptr_t)n
->car
->car
!= NODE_STR
) {
2059 codegen(s
, n
->car
, NOVAL
);
2067 gen_literal_array(s
, tree
, FALSE
, val
);
2071 gen_literal_array(s
, tree
, TRUE
, val
);
2076 char *p
= (char*)tree
->car
;
2077 size_t len
= (intptr_t)tree
->cdr
;
2078 int ai
= mrb_gc_arena_save(s
->mrb
);
2079 int sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, "Kernel"));
2080 int off
= new_lit(s
, mrb_str_new(s
->mrb
, p
, len
));
2082 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
2083 genop(s
, MKOP_ABx(OP_GETMCNST
, cursp(), sym
));
2085 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2087 sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, "`"));
2088 genop(s
, MKOP_ABC(OP_SEND
, cursp(), sym
, 1));
2089 mrb_gc_arena_restore(s
->mrb
, ai
);
2096 char *p1
= (char*)tree
->car
;
2097 char *p2
= (char*)tree
->cdr
;
2098 int ai
= mrb_gc_arena_save(s
->mrb
);
2099 int sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, REGEXP_CLASS
));
2100 int off
= new_lit(s
, mrb_str_new_cstr(s
->mrb
, p1
));
2103 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
2104 genop(s
, MKOP_ABx(OP_GETMCNST
, cursp(), sym
));
2106 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2109 off
= new_lit(s
, mrb_str_new_cstr(s
->mrb
, p2
));
2110 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2115 sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, "compile"));
2116 genop(s
, MKOP_ABC(OP_SEND
, cursp(), sym
, argc
));
2117 mrb_gc_arena_restore(s
->mrb
, ai
);
2124 node
*n
= tree
->car
;
2125 int ai
= mrb_gc_arena_save(s
->mrb
);
2126 int sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, REGEXP_CLASS
));
2131 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
2132 genop(s
, MKOP_ABx(OP_GETMCNST
, cursp(), sym
));
2134 codegen(s
, n
->car
, VAL
);
2137 codegen(s
, n
->car
, VAL
);
2139 genop_peep(s
, MKOP_AB(OP_STRCAT
, cursp(), cursp()+1), VAL
);
2146 off
= new_lit(s
, mrb_str_new_cstr(s
->mrb
, p
));
2147 codegen(s
, tree
->car
, VAL
);
2148 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2150 genop_peep(s
, MKOP_AB(OP_STRCAT
, cursp(), cursp()+1), VAL
);
2153 char *p2
= (char*)n
->cdr
;
2157 off
= new_lit(s
, mrb_str_new_cstr(s
->mrb
, p2
));
2158 genop(s
, MKOP_ABx(OP_STRING
, cursp(), off
));
2163 sym
= new_sym(s
, mrb_intern_lit(s
->mrb
, "compile"));
2164 genop(s
, MKOP_ABC(OP_SEND
, cursp(), sym
, argc
));
2165 mrb_gc_arena_restore(s
->mrb
, ai
);
2169 node
*n
= tree
->car
;
2172 if ((intptr_t)n
->car
->car
!= NODE_STR
) {
2173 codegen(s
, n
->car
, NOVAL
);
2182 int sym
= new_sym(s
, sym(tree
));
2184 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), sym
));
2190 codegen(s
, tree
, val
);
2198 genop(s
, MKOP_A(OP_LOADSELF
, cursp()));
2205 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2212 genop(s
, MKOP_A(OP_LOADT
, cursp()));
2219 genop(s
, MKOP_A(OP_LOADF
, cursp()));
2226 int a
= new_msym(s
, sym(tree
->car
));
2227 int b
= new_msym(s
, sym(tree
->cdr
));
2228 int c
= new_msym(s
, mrb_intern_lit(s
->mrb
,"alias_method"));
2230 genop(s
, MKOP_A(OP_TCLASS
, cursp()));
2232 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), a
));
2234 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), b
));
2236 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2238 genop(s
, MKOP_ABC(OP_SEND
, cursp(), c
, 2));
2247 int undef
= new_msym(s
, mrb_intern_lit(s
->mrb
, "undef_method"));
2251 genop(s
, MKOP_A(OP_TCLASS
, cursp()));
2254 int symbol
= new_msym(s
, sym(t
->car
));
2255 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), symbol
));
2261 genop(s
, MKOP_ABC(OP_SEND
, cursp(), undef
, num
));
2272 if (tree
->car
->car
== (node
*)0) {
2273 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2276 else if (tree
->car
->car
== (node
*)1) {
2277 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
2281 codegen(s
, tree
->car
->car
, VAL
);
2283 if (tree
->cdr
->car
) {
2284 codegen(s
, tree
->cdr
->car
, VAL
);
2287 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2291 idx
= new_msym(s
, sym(tree
->car
->cdr
));
2292 genop(s
, MKOP_AB(OP_CLASS
, cursp(), idx
));
2293 idx
= scope_body(s
, tree
->cdr
->cdr
->car
, val
);
2294 genop(s
, MKOP_ABx(OP_EXEC
, cursp(), idx
));
2305 if (tree
->car
->car
== (node
*)0) {
2306 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2309 else if (tree
->car
->car
== (node
*)1) {
2310 genop(s
, MKOP_A(OP_OCLASS
, cursp()));
2314 codegen(s
, tree
->car
->car
, VAL
);
2317 idx
= new_msym(s
, sym(tree
->car
->cdr
));
2318 genop(s
, MKOP_AB(OP_MODULE
, cursp(), idx
));
2319 idx
= scope_body(s
, tree
->cdr
->car
, val
);
2320 genop(s
, MKOP_ABx(OP_EXEC
, cursp(), idx
));
2331 codegen(s
, tree
->car
, VAL
);
2333 genop(s
, MKOP_AB(OP_SCLASS
, cursp(), cursp()));
2334 idx
= scope_body(s
, tree
->cdr
->car
, val
);
2335 genop(s
, MKOP_ABx(OP_EXEC
, cursp(), idx
));
2344 int sym
= new_msym(s
, sym(tree
->car
));
2345 int idx
= lambda_body(s
, tree
->cdr
, 0);
2347 genop(s
, MKOP_A(OP_TCLASS
, cursp()));
2349 genop(s
, MKOP_Abc(OP_LAMBDA
, cursp(), idx
, OP_L_METHOD
));
2351 genop(s
, MKOP_AB(OP_METHOD
, cursp(), sym
));
2353 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), sym
));
2361 node
*recv
= tree
->car
;
2362 int sym
= new_msym(s
, sym(tree
->cdr
->car
));
2363 int idx
= lambda_body(s
, tree
->cdr
->cdr
, 0);
2365 codegen(s
, recv
, VAL
);
2367 genop(s
, MKOP_AB(OP_SCLASS
, cursp(), cursp()));
2369 genop(s
, MKOP_Abc(OP_LAMBDA
, cursp(), idx
, OP_L_METHOD
));
2371 genop(s
, MKOP_AB(OP_METHOD
, cursp(), sym
));
2373 genop(s
, MKOP_ABx(OP_LOADSYM
, cursp(), sym
));
2380 codegen(s
, tree
, NOVAL
);
2389 scope_add_irep(codegen_scope
*s
, mrb_irep
*irep
)
2391 if (s
->irep
== NULL
) {
2395 if (s
->irep
->rlen
== s
->rcapa
) {
2397 s
->irep
->reps
= (mrb_irep
**)codegen_realloc(s
, s
->irep
->reps
, sizeof(mrb_irep
*)*s
->rcapa
);
2399 s
->irep
->reps
[s
->irep
->rlen
] = irep
;
2403 static codegen_scope
*
2404 scope_new(mrb_state
*mrb
, codegen_scope
*prev
, node
*lv
)
2406 static const codegen_scope codegen_scope_zero
= { 0 };
2407 mrb_pool
*pool
= mrb_pool_open(mrb
);
2408 codegen_scope
*p
= (codegen_scope
*)mrb_pool_alloc(pool
, sizeof(codegen_scope
));
2411 *p
= codegen_scope_zero
;
2414 if (!prev
) return p
;
2419 p
->irep
= mrb_add_irep(mrb
);
2420 scope_add_irep(prev
, p
->irep
);
2423 p
->irep
->reps
= (mrb_irep
**)mrb_malloc(mrb
, sizeof(mrb_irep
*)*p
->rcapa
);
2426 p
->iseq
= (mrb_code
*)mrb_malloc(mrb
, sizeof(mrb_code
)*p
->icapa
);
2427 p
->irep
->iseq
= p
->iseq
;
2430 p
->irep
->pool
= (mrb_value
*)mrb_malloc(mrb
, sizeof(mrb_value
)*p
->pcapa
);
2434 p
->irep
->syms
= (mrb_sym
*)mrb_malloc(mrb
, sizeof(mrb_sym
)*p
->scapa
);
2438 p
->sp
+= node_len(lv
)+1; /* add self */
2440 p
->ai
= mrb_gc_arena_save(mrb
);
2442 p
->filename
= prev
->filename
;
2444 p
->lines
= (uint16_t*)mrb_malloc(mrb
, sizeof(short)*p
->icapa
);
2446 p
->lineno
= prev
->lineno
;
2449 p
->debug_start_pos
= 0;
2451 mrb_debug_info_alloc(mrb
, p
->irep
);
2452 p
->irep
->filename
= p
->filename
;
2453 p
->irep
->lines
= p
->lines
;
2456 p
->irep
->debug_info
= NULL
;
2458 p
->parser
= prev
->parser
;
2459 p
->filename_index
= prev
->filename_index
;
2465 scope_finish(codegen_scope
*s
)
2467 mrb_state
*mrb
= s
->mrb
;
2468 mrb_irep
*irep
= s
->irep
;
2474 irep
->iseq
= (mrb_code
*)codegen_realloc(s
, s
->iseq
, sizeof(mrb_code
)*s
->pc
);
2477 irep
->lines
= (uint16_t *)codegen_realloc(s
, s
->lines
, sizeof(uint16_t)*s
->pc
);
2483 irep
->pool
= (mrb_value
*)codegen_realloc(s
, irep
->pool
, sizeof(mrb_value
)*irep
->plen
);
2484 irep
->syms
= (mrb_sym
*)codegen_realloc(s
, irep
->syms
, sizeof(mrb_sym
)*irep
->slen
);
2485 irep
->reps
= (mrb_irep
**)codegen_realloc(s
, irep
->reps
, sizeof(mrb_irep
*)*irep
->rlen
);
2487 s
->irep
->filename
= mrb_parser_get_filename(s
->parser
, s
->filename_index
);
2488 mrb_debug_info_append_file(mrb
, s
->irep
, s
->debug_start_pos
, s
->pc
);
2490 fname_len
= strlen(s
->filename
);
2491 fname
= (char*)codegen_malloc(s
, fname_len
+ 1);
2492 memcpy(fname
, s
->filename
, fname_len
);
2493 fname
[fname_len
] = '\0';
2494 irep
->filename
= fname
;
2497 irep
->nlocals
= s
->nlocals
;
2498 irep
->nregs
= s
->nregs
;
2500 mrb_gc_arena_restore(mrb
, s
->ai
);
2501 mrb_pool_close(s
->mpool
);
2504 static struct loopinfo
*
2505 loop_push(codegen_scope
*s
, enum looptype t
)
2507 struct loopinfo
*p
= (struct loopinfo
*)codegen_palloc(s
, sizeof(struct loopinfo
));
2510 p
->pc1
= p
->pc2
= p
->pc3
= 0;
2512 p
->ensure_level
= s
->ensure_level
;
2520 loop_break(codegen_scope
*s
, node
*tree
)
2523 codegen(s
, tree
, NOVAL
);
2524 raise_error(s
, "unexpected break");
2527 struct loopinfo
*loop
;
2530 codegen(s
, tree
, VAL
);
2535 while (loop
->type
== LOOP_BEGIN
) {
2536 genop_peep(s
, MKOP_A(OP_POPERR
, 1), NOVAL
);
2539 while (loop
->type
== LOOP_RESCUE
) {
2542 if (loop
->type
== LOOP_NORMAL
) {
2545 if (s
->ensure_level
> s
->loop
->ensure_level
) {
2546 genop_peep(s
, MKOP_A(OP_EPOP
, s
->ensure_level
- s
->loop
->ensure_level
), NOVAL
);
2549 genop_peep(s
, MKOP_AB(OP_MOVE
, loop
->acc
, cursp()), NOVAL
);
2551 tmp
= genop(s
, MKOP_sBx(OP_JMP
, loop
->pc3
));
2555 genop(s
, MKOP_AB(OP_RETURN
, cursp(), OP_R_BREAK
));
2561 loop_pop(codegen_scope
*s
, int val
)
2564 genop(s
, MKOP_A(OP_LOADNIL
, cursp()));
2566 dispatch_linked(s
, s
->loop
->pc3
);
2567 s
->loop
= s
->loop
->prev
;
2572 codedump(mrb_state
*mrb
, mrb_irep
*irep
)
2580 printf("irep %p nregs=%d nlocals=%d pools=%d syms=%d reps=%d\n", irep
,
2581 irep
->nregs
, irep
->nlocals
, (int)irep
->plen
, (int)irep
->slen
, (int)irep
->rlen
);
2583 mrb_assert(irep
->ilen
<= INT_MAX
);
2584 for (i
= 0; i
< (int)(irep
->ilen
); i
++) {
2585 ai
= mrb_gc_arena_save(mrb
);
2588 switch (GET_OPCODE(c
)) {
2593 printf("OP_MOVE\tR%d\tR%d\n", GETARG_A(c
), GETARG_B(c
));
2596 printf("OP_LOADL\tR%d\tL(%d)\n", GETARG_A(c
), GETARG_Bx(c
));
2599 printf("OP_LOADI\tR%d\t%d\n", GETARG_A(c
), GETARG_sBx(c
));
2602 printf("OP_LOADSYM\tR%d\t:%s\n", GETARG_A(c
),
2603 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2606 printf("OP_LOADNIL\tR%d\n", GETARG_A(c
));
2609 printf("OP_LOADSELF\tR%d\n", GETARG_A(c
));
2612 printf("OP_LOADT\tR%d\n", GETARG_A(c
));
2615 printf("OP_LOADF\tR%d\n", GETARG_A(c
));
2618 printf("OP_GETGLOBAL\tR%d\t:%s\n", GETARG_A(c
),
2619 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2622 printf("OP_SETGLOBAL\t:%s\tR%d\n",
2623 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]),
2627 printf("OP_GETCONST\tR%d\t:%s\n", GETARG_A(c
),
2628 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2631 printf("OP_SETCONST\t:%s\tR%d\n",
2632 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]),
2636 printf("OP_GETMCNST\tR%d\tR%d::%s\n", GETARG_A(c
), GETARG_A(c
),
2637 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2640 printf("OP_SETMCNST\tR%d::%s\tR%d\n", GETARG_A(c
)+1,
2641 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]),
2645 printf("OP_GETIV\tR%d\t%s\n", GETARG_A(c
),
2646 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2649 printf("OP_SETIV\t%s\tR%d\n",
2650 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]),
2654 printf("OP_GETUPVAR\tR%d\t%d\t%d\n",
2655 GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2658 printf("OP_SETUPVAR\tR%d\t%d\t%d\n",
2659 GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2662 printf("OP_GETCV\tR%d\t%s\n", GETARG_A(c
),
2663 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]));
2666 printf("OP_SETCV\t%s\tR%d\n",
2667 mrb_sym2name(mrb
, irep
->syms
[GETARG_Bx(c
)]),
2671 printf("OP_JMP\t\t%03d\n", i
+GETARG_sBx(c
));
2674 printf("OP_JMPIF\tR%d\t%03d\n", GETARG_A(c
), i
+GETARG_sBx(c
));
2677 printf("OP_JMPNOT\tR%d\t%03d\n", GETARG_A(c
), i
+GETARG_sBx(c
));
2680 printf("OP_SEND\tR%d\t:%s\t%d\n", GETARG_A(c
),
2681 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2685 printf("OP_SENDB\tR%d\t:%s\t%d\n", GETARG_A(c
),
2686 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2690 printf("OP_TAILCALL\tR%d\t:%s\t%d\n", GETARG_A(c
),
2691 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2695 printf("OP_SUPER\tR%d\t%d\n", GETARG_A(c
),
2699 printf("OP_ARGARY\tR%d\t%d:%d:%d:%d\n", GETARG_A(c
),
2700 (GETARG_Bx(c
)>>10)&0x3f,
2701 (GETARG_Bx(c
)>>9)&0x1,
2702 (GETARG_Bx(c
)>>4)&0x1f,
2703 (GETARG_Bx(c
)>>0)&0xf);
2707 printf("OP_ENTER\t%d:%d:%d:%d:%d:%d:%d\n",
2708 (GETARG_Ax(c
)>>18)&0x1f,
2709 (GETARG_Ax(c
)>>13)&0x1f,
2710 (GETARG_Ax(c
)>>12)&0x1,
2711 (GETARG_Ax(c
)>>7)&0x1f,
2712 (GETARG_Ax(c
)>>2)&0x1f,
2713 (GETARG_Ax(c
)>>1)&0x1,
2714 GETARG_Ax(c
) & 0x1);
2717 printf("OP_RETURN\tR%d", GETARG_A(c
));
2718 switch (GETARG_B(c
)) {
2720 printf("\n"); break;
2722 printf("\treturn\n"); break;
2724 printf("\tbreak\n"); break;
2726 printf("\tbroken\n"); break;
2731 printf("OP_BLKPUSH\tR%d\t%d:%d:%d:%d\n", GETARG_A(c
),
2732 (GETARG_Bx(c
)>>10)&0x3f,
2733 (GETARG_Bx(c
)>>9)&0x1,
2734 (GETARG_Bx(c
)>>4)&0x1f,
2735 (GETARG_Bx(c
)>>0)&0xf);
2739 printf("OP_LAMBDA\tR%d\tI(%+d)\t%d\n", GETARG_A(c
), GETARG_b(c
)+1, GETARG_c(c
));
2742 printf("OP_RANGE\tR%d\tR%d\t%d\n", GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2745 printf("OP_METHOD\tR%d\t:%s\n", GETARG_A(c
),
2746 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]));
2750 printf("OP_ADD\tR%d\t:%s\t%d\n", GETARG_A(c
),
2751 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2755 printf("OP_ADDI\tR%d\t:%s\t%d\n", GETARG_A(c
),
2756 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2760 printf("OP_SUB\tR%d\t:%s\t%d\n", GETARG_A(c
),
2761 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2765 printf("OP_SUBI\tR%d\t:%s\t%d\n", GETARG_A(c
),
2766 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2770 printf("OP_MUL\tR%d\t:%s\t%d\n", GETARG_A(c
),
2771 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2775 printf("OP_DIV\tR%d\t:%s\t%d\n", GETARG_A(c
),
2776 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2780 printf("OP_LT\tR%d\t:%s\t%d\n", GETARG_A(c
),
2781 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2785 printf("OP_LE\tR%d\t:%s\t%d\n", GETARG_A(c
),
2786 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2790 printf("OP_GT\tR%d\t:%s\t%d\n", GETARG_A(c
),
2791 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2795 printf("OP_GE\tR%d\t:%s\t%d\n", GETARG_A(c
),
2796 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2800 printf("OP_EQ\tR%d\t:%s\t%d\n", GETARG_A(c
),
2801 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]),
2806 printf("OP_STOP\n");
2810 printf("OP_ARRAY\tR%d\tR%d\t%d\n", GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2813 printf("OP_ARYCAT\tR%d\tR%d\n", GETARG_A(c
), GETARG_B(c
));
2816 printf("OP_ARYPUSH\tR%d\tR%d\n", GETARG_A(c
), GETARG_B(c
));
2819 printf("OP_AREF\tR%d\tR%d\t%d\n", GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2822 printf("OP_APOST\tR%d\t%d\t%d\n", GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2826 mrb_value v
= irep
->pool
[GETARG_Bx(c
)];
2827 mrb_value s
= mrb_str_dump(mrb
, mrb_str_new(mrb
, RSTRING_PTR(v
), RSTRING_LEN(v
)));
2828 printf("OP_STRING\tR%d\t%s\n", GETARG_A(c
), RSTRING_PTR(s
));
2832 printf("OP_STRCAT\tR%d\tR%d\n", GETARG_A(c
), GETARG_B(c
));
2835 printf("OP_HASH\tR%d\tR%d\t%d\n", GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2839 printf("OP_OCLASS\tR%d\n", GETARG_A(c
));
2842 printf("OP_CLASS\tR%d\t:%s\n", GETARG_A(c
),
2843 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]));
2846 printf("OP_MODULE\tR%d\t:%s\n", GETARG_A(c
),
2847 mrb_sym2name(mrb
, irep
->syms
[GETARG_B(c
)]));
2850 printf("OP_EXEC\tR%d\tI(%+d)\n", GETARG_A(c
), GETARG_Bx(c
)+1);
2853 printf("OP_SCLASS\tR%d\tR%d\n", GETARG_A(c
), GETARG_B(c
));
2856 printf("OP_TCLASS\tR%d\n", GETARG_A(c
));
2860 mrb_value v
= irep
->pool
[GETARG_Bx(c
)];
2861 mrb_value s
= mrb_str_dump(mrb
, mrb_str_new(mrb
, RSTRING_PTR(v
), RSTRING_LEN(v
)));
2862 printf("OP_ERR\t%s\n", RSTRING_PTR(s
));
2866 printf("OP_EPUSH\t:I(%+d)\n", GETARG_Bx(c
)+1);
2869 printf("OP_ONERR\t%03d\n", i
+GETARG_sBx(c
));
2872 printf("OP_RESCUE\tR%d\n", GETARG_A(c
));
2875 printf("OP_RAISE\tR%d\n", GETARG_A(c
));
2878 printf("OP_POPERR\t%d\n", GETARG_A(c
));
2881 printf("OP_EPOP\t%d\n", GETARG_A(c
));
2885 printf("OP_unknown %d\t%d\t%d\t%d\n", GET_OPCODE(c
),
2886 GETARG_A(c
), GETARG_B(c
), GETARG_C(c
));
2889 mrb_gc_arena_restore(mrb
, ai
);
2896 codedump_recur(mrb_state
*mrb
, mrb_irep
*irep
)
2900 codedump(mrb
, irep
);
2901 for (i
=0; i
<irep
->rlen
; i
++) {
2902 codedump_recur(mrb
, irep
->reps
[i
]);
2907 mrb_codedump_all(mrb_state
*mrb
, struct RProc
*proc
)
2909 codedump_recur(mrb
, proc
->body
.irep
);
2913 mrb_generate_code(mrb_state
*mrb
, parser_state
*p
)
2915 codegen_scope
*scope
= scope_new(mrb
, 0, 0);
2923 scope
->filename
= p
->filename
;
2924 scope
->filename_index
= p
->current_filename_index
;
2926 MRB_TRY(&scope
->jmp
) {
2928 codegen(scope
, p
->tree
, NOVAL
);
2929 proc
= mrb_proc_new(mrb
, scope
->irep
);
2930 mrb_irep_decref(mrb
, scope
->irep
);
2931 mrb_pool_close(scope
->mpool
);
2934 MRB_CATCH(&scope
->jmp
) {
2935 if (scope
->filename
== scope
->irep
->filename
) {
2936 scope
->irep
->filename
= NULL
;
2938 mrb_irep_decref(mrb
, scope
->irep
);
2939 mrb_pool_close(scope
->mpool
);
2942 MRB_END_EXC(&scope
->jmp
);