Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-compiler/core/codegen.c

raw
1 /*
2 ** codegen.c - mruby code generator
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/compile.h>
9 #include <mruby/proc.h>
10 #include <mruby/dump.h>
11 #include <mruby/numeric.h>
12 #include <mruby/string.h>
13 #include <mruby/debug.h>
14 #include <mruby/presym.h>
15 #include "node.h"
16 #include <mruby/opcode.h>
17 #include <mruby/re.h>
18 #include <mruby/throw.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <mruby/internal.h>
22
23 #ifndef MRB_CODEGEN_LEVEL_MAX
24 #define MRB_CODEGEN_LEVEL_MAX 256
25 #endif
26
27 #define MAXARG_S (1<<16)
28
29 typedef mrb_ast_node node;
30 typedef struct mrb_parser_state parser_state;
31
32 enum looptype {
33 LOOP_NORMAL,
34 LOOP_BLOCK,
35 LOOP_FOR,
36 LOOP_BEGIN,
37 LOOP_RESCUE,
38 };
39
40 struct loopinfo {
41 enum looptype type;
42 uint32_t pc0; /* `next` destination */
43 uint32_t pc1; /* `redo` destination */
44 uint32_t pc2; /* `break` destination */
45 int reg; /* destination register */
46 struct loopinfo *prev;
47 };
48
49 typedef struct scope {
50 mrb_state *mrb;
51 mrb_pool *mpool;
52
53 struct scope *prev;
54
55 node *lv;
56
57 uint16_t sp;
58 uint32_t pc;
59 uint32_t lastpc;
60 uint32_t lastlabel;
61 uint16_t ainfo:15;
62 mrb_bool mscope:1;
63
64 struct loopinfo *loop;
65 mrb_sym filename_sym;
66 uint16_t lineno;
67
68 mrb_code *iseq;
69 uint16_t *lines;
70 uint32_t icapa;
71
72 mrb_irep *irep;
73 mrb_pool_value *pool;
74 mrb_sym *syms;
75 mrb_irep **reps;
76 struct mrb_irep_catch_handler *catch_table;
77 uint32_t pcapa, scapa, rcapa;
78
79 uint16_t nlocals;
80 uint16_t nregs;
81 int ai;
82
83 int debug_start_pos;
84 uint16_t filename_index;
85 parser_state* parser;
86
87 int rlev; /* recursion levels */
88 } codegen_scope;
89
90 static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv);
91 static void scope_finish(codegen_scope *s);
92 static struct loopinfo *loop_push(codegen_scope *s, enum looptype t);
93 static void loop_break(codegen_scope *s, node *tree);
94 static void loop_pop(codegen_scope *s, int val);
95
96 /*
97 * The search for catch handlers starts at the end of the table in mrb_vm_run().
98 * Therefore, the next handler to be added must meet one of the following conditions.
99 * - Larger start position
100 * - Same start position but smaller end position
101 */
102 static int catch_handler_new(codegen_scope *s);
103 static void catch_handler_set(codegen_scope *s, int ent, enum mrb_catch_type type, uint32_t begin, uint32_t end, uint32_t target);
104
105 static void gen_assignment(codegen_scope *s, node *tree, node *rhs, int sp, int val);
106 static void gen_massignment(codegen_scope *s, node *tree, int sp, int val);
107
108 static void codegen(codegen_scope *s, node *tree, int val);
109 static void raise_error(codegen_scope *s, const char *msg);
110
111 static void
112 codegen_error(codegen_scope *s, const char *message)
113 {
114 if (!s) return;
115 #ifndef MRB_NO_STDIO
116 if (s->filename_sym && s->lineno) {
117 const char *filename = mrb_sym_name_len(s->mrb, s->filename_sym, NULL);
118 fprintf(stderr, "%s:%d: %s\n", filename, s->lineno, message);
119 }
120 else {
121 fprintf(stderr, "%s\n", message);
122 }
123 #endif
124 while (s->prev) {
125 codegen_scope *tmp = s->prev;
126 if (s->irep) {
127 mrb_free(s->mrb, s->iseq);
128 for (int i=0; i<s->irep->plen; i++) {
129 mrb_pool_value *pv = &s->pool[i];
130 if ((pv->tt & 0x3) == IREP_TT_STR || pv->tt == IREP_TT_BIGINT) {
131 mrb_free(s->mrb, (void*)pv->u.str);
132 }
133 }
134 mrb_free(s->mrb, s->pool);
135 mrb_free(s->mrb, s->syms);
136 mrb_free(s->mrb, s->catch_table);
137 if (s->reps) {
138 /* copied from mrb_irep_free() in state.c */
139 for (int i=0; i<s->irep->rlen; i++) {
140 if (s->reps[i])
141 mrb_irep_decref(s->mrb, (mrb_irep*)s->reps[i]);
142 }
143 mrb_free(s->mrb, s->reps);
144 }
145 mrb_free(s->mrb, s->lines);
146 }
147 mrb_pool_close(s->mpool);
148 s = tmp;
149 }
150 MRB_THROW(s->mrb->jmp);
151 }
152
153 static void*
154 codegen_palloc(codegen_scope *s, size_t len)
155 {
156 void *p = mrb_pool_alloc(s->mpool, len);
157
158 if (!p) codegen_error(s, "pool memory allocation");
159 return p;
160 }
161
162 static void*
163 codegen_realloc(codegen_scope *s, void *p, size_t len)
164 {
165 p = mrb_realloc_simple(s->mrb, p, len);
166
167 if (!p && len > 0) codegen_error(s, "mrb_realloc");
168 return p;
169 }
170
171 static void
172 check_no_ext_ops(codegen_scope *s, uint16_t a, uint16_t b)
173 {
174 if (s->parser->no_ext_ops && (a | b) > 0xff) {
175 codegen_error(s, "need OP_EXTs instruction (currently OP_EXTs are prohibited)");
176 }
177 }
178
179 static int
180 new_label(codegen_scope *s)
181 {
182 return s->lastlabel = s->pc;
183 }
184
185 static void
186 emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
187 {
188 if (pc >= s->icapa) {
189 if (pc == UINT32_MAX) {
190 codegen_error(s, "too big code block");
191 }
192 if (pc >= UINT32_MAX / 2) {
193 pc = UINT32_MAX;
194 }
195 else {
196 s->icapa *= 2;
197 }
198 s->iseq = (mrb_code*)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa);
199 if (s->lines) {
200 s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->icapa);
201 }
202 }
203 if (s->lines) {
204 if (s->lineno > 0 || pc == 0)
205 s->lines[pc] = s->lineno;
206 else
207 s->lines[pc] = s->lines[pc-1];
208 }
209 s->iseq[pc] = i;
210 }
211
212 static void
213 emit_S(codegen_scope *s, int pc, uint16_t i)
214 {
215 uint8_t hi = i>>8;
216 uint8_t lo = i&0xff;
217
218 emit_B(s, pc, hi);
219 emit_B(s, pc+1, lo);
220 }
221
222 static void
223 gen_B(codegen_scope *s, uint8_t i)
224 {
225 emit_B(s, s->pc, i);
226 s->pc++;
227 }
228
229 static void
230 gen_S(codegen_scope *s, uint16_t i)
231 {
232 emit_S(s, s->pc, i);
233 s->pc += 2;
234 }
235
236 static void
237 genop_0(codegen_scope *s, mrb_code i)
238 {
239 s->lastpc = s->pc;
240 gen_B(s, i);
241 }
242
243 static void
244 genop_1(codegen_scope *s, mrb_code i, uint16_t a)
245 {
246 s->lastpc = s->pc;
247 check_no_ext_ops(s, a, 0);
248 if (a > 0xff) {
249 gen_B(s, OP_EXT1);
250 gen_B(s, i);
251 gen_S(s, a);
252 }
253 else {
254 gen_B(s, i);
255 gen_B(s, (uint8_t)a);
256 }
257 }
258
259 static void
260 genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
261 {
262 s->lastpc = s->pc;
263 check_no_ext_ops(s, a, b);
264 if (a > 0xff && b > 0xff) {
265 gen_B(s, OP_EXT3);
266 gen_B(s, i);
267 gen_S(s, a);
268 gen_S(s, b);
269 }
270 else if (b > 0xff) {
271 gen_B(s, OP_EXT2);
272 gen_B(s, i);
273 gen_B(s, (uint8_t)a);
274 gen_S(s, b);
275 }
276 else if (a > 0xff) {
277 gen_B(s, OP_EXT1);
278 gen_B(s, i);
279 gen_S(s, a);
280 gen_B(s, (uint8_t)b);
281 }
282 else {
283 gen_B(s, i);
284 gen_B(s, (uint8_t)a);
285 gen_B(s, (uint8_t)b);
286 }
287 }
288
289 static void
290 genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint16_t c)
291 {
292 genop_2(s, i, a, b);
293 gen_B(s, (uint8_t)c);
294 }
295
296 static void
297 genop_2S(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
298 {
299 genop_1(s, i, a);
300 gen_S(s, b);
301 }
302
303 static void
304 genop_2SS(codegen_scope *s, mrb_code i, uint16_t a, uint32_t b)
305 {
306 genop_1(s, i, a);
307 gen_S(s, b>>16);
308 gen_S(s, b&0xffff);
309 }
310
311 static void
312 genop_W(codegen_scope *s, mrb_code i, uint32_t a)
313 {
314 uint8_t a1 = (a>>16) & 0xff;
315 uint8_t a2 = (a>>8) & 0xff;
316 uint8_t a3 = a & 0xff;
317
318 s->lastpc = s->pc;
319 gen_B(s, i);
320 gen_B(s, a1);
321 gen_B(s, a2);
322 gen_B(s, a3);
323 }
324
325 #define NOVAL 0
326 #define VAL 1
327
328 static mrb_bool
329 no_optimize(codegen_scope *s)
330 {
331 if (s && s->parser && s->parser->no_optimize)
332 return TRUE;
333 return FALSE;
334 }
335
336 struct mrb_insn_data
337 mrb_decode_insn(const mrb_code *pc)
338 {
339 struct mrb_insn_data data = { 0 };
340 if (pc == 0) return data;
341 data.addr = pc;
342 mrb_code insn = READ_B();
343 uint16_t a = 0;
344 uint16_t b = 0;
345 uint16_t c = 0;
346
347 switch (insn) {
348 #define FETCH_Z() /* empty */
349 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x (); break;
350 #include "mruby/ops.h"
351 #undef OPCODE
352 }
353 switch (insn) {
354 case OP_EXT1:
355 insn = READ_B();
356 switch (insn) {
357 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _1 (); break;
358 #include "mruby/ops.h"
359 #undef OPCODE
360 }
361 break;
362 case OP_EXT2:
363 insn = READ_B();
364 switch (insn) {
365 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _2 (); break;
366 #include "mruby/ops.h"
367 #undef OPCODE
368 }
369 break;
370 case OP_EXT3:
371 insn = READ_B();
372 switch (insn) {
373 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _3 (); break;
374 #include "mruby/ops.h"
375 #undef OPCODE
376 }
377 break;
378 default:
379 break;
380 }
381 data.insn = insn;
382 data.a = a;
383 data.b = b;
384 data.c = c;
385 return data;
386 }
387
388 #undef OPCODE
389 #define Z 1
390 #define S 3
391 #define W 4
392 #define OPCODE(_,x) x,
393 /* instruction sizes */
394 static uint8_t mrb_insn_size[] = {
395 #define B 2
396 #define BB 3
397 #define BBB 4
398 #define BS 4
399 #define BSS 6
400 #include "mruby/ops.h"
401 #undef B
402 #undef BB
403 #undef BBB
404 #undef BS
405 #undef BSS
406 };
407 /* EXT1 instruction sizes */
408 static uint8_t mrb_insn_size1[] = {
409 #define B 3
410 #define BB 4
411 #define BBB 5
412 #define BS 5
413 #define BSS 7
414 #include "mruby/ops.h"
415 #undef B
416 #undef BS
417 #undef BSS
418 };
419 /* EXT2 instruction sizes */
420 static uint8_t mrb_insn_size2[] = {
421 #define B 2
422 #define BS 4
423 #define BSS 6
424 #include "mruby/ops.h"
425 #undef B
426 #undef BB
427 #undef BBB
428 #undef BS
429 #undef BSS
430 };
431 /* EXT3 instruction sizes */
432 #define B 3
433 #define BB 5
434 #define BBB 6
435 #define BS 5
436 #define BSS 7
437 static uint8_t mrb_insn_size3[] = {
438 #include "mruby/ops.h"
439 };
440 #undef B
441 #undef BB
442 #undef BBB
443 #undef BS
444 #undef BSS
445 #undef OPCODE
446
447 static const mrb_code*
448 mrb_prev_pc(codegen_scope *s, const mrb_code *pc)
449 {
450 const mrb_code *prev_pc = NULL;
451 const mrb_code *i = s->iseq;
452
453 while (i<pc) {
454 uint8_t insn = i[0];
455 prev_pc = i;
456 switch (insn) {
457 case OP_EXT1:
458 i += mrb_insn_size1[i[1]] + 1;
459 break;
460 case OP_EXT2:
461 i += mrb_insn_size2[i[1]] + 1;
462 break;
463 case OP_EXT3:
464 i += mrb_insn_size3[i[1]] + 1;
465 break;
466 default:
467 i += mrb_insn_size[insn];
468 break;
469 }
470 }
471 return prev_pc;
472 }
473
474 #define pc_addr(s) &((s)->iseq[(s)->pc])
475 #define addr_pc(s, addr) (uint32_t)((addr) - s->iseq)
476 #define rewind_pc(s) s->pc = s->lastpc
477
478 static struct mrb_insn_data
479 mrb_last_insn(codegen_scope *s)
480 {
481 if (s->pc == 0) {
482 struct mrb_insn_data data = { OP_NOP, 0 };
483 return data;
484 }
485 return mrb_decode_insn(&s->iseq[s->lastpc]);
486 }
487
488 static mrb_bool
489 no_peephole(codegen_scope *s)
490 {
491 return no_optimize(s) || s->lastlabel == s->pc || s->pc == 0 || s->pc == s->lastpc;
492 }
493
494 #define JMPLINK_START UINT32_MAX
495
496 static void
497 gen_jmpdst(codegen_scope *s, uint32_t pc)
498 {
499
500 if (pc == JMPLINK_START) {
501 pc = 0;
502 }
503 uint32_t pos2 = s->pc+2;
504 int32_t off = pc - pos2;
505
506 if (off > INT16_MAX || INT16_MIN > off) {
507 codegen_error(s, "too big jump offset");
508 }
509 gen_S(s, (uint16_t)off);
510 }
511
512 static uint32_t
513 genjmp(codegen_scope *s, mrb_code i, uint32_t pc)
514 {
515 uint32_t pos;
516
517 genop_0(s, i);
518 pos = s->pc;
519 gen_jmpdst(s, pc);
520 return pos;
521 }
522
523 #define genjmp_0(s,i) genjmp(s,i,JMPLINK_START)
524
525 static uint32_t
526 genjmp2(codegen_scope *s, mrb_code i, uint16_t a, uint32_t pc, int val)
527 {
528 uint32_t pos;
529
530 if (!no_peephole(s) && !val) {
531 struct mrb_insn_data data = mrb_last_insn(s);
532
533 switch (data.insn) {
534 case OP_MOVE:
535 if (data.a == a && data.a > s->nlocals) {
536 rewind_pc(s);
537 a = data.b;
538 }
539 break;
540 case OP_LOADNIL:
541 case OP_LOADF:
542 if (data.a == a || data.a > s->nlocals) {
543 s->pc = addr_pc(s, data.addr);
544 if (i == OP_JMPNOT || (i == OP_JMPNIL && data.insn == OP_LOADNIL)) {
545 return genjmp(s, OP_JMP, pc);
546 }
547 else { /* OP_JMPIF */
548 return JMPLINK_START;
549 }
550 }
551 break;
552 case OP_LOADT: case OP_LOADI: case OP_LOADINEG: case OP_LOADI__1:
553 case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
554 case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
555 if (data.a == a || data.a > s->nlocals) {
556 s->pc = addr_pc(s, data.addr);
557 if (i == OP_JMPIF) {
558 return genjmp(s, OP_JMP, pc);
559 }
560 else { /* OP_JMPNOT and OP_JMPNIL */
561 return JMPLINK_START;
562 }
563 }
564 break;
565 }
566 }
567
568 if (a > 0xff) {
569 check_no_ext_ops(s, a, 0);
570 gen_B(s, OP_EXT1);
571 genop_0(s, i);
572 gen_S(s, a);
573 }
574 else {
575 genop_0(s, i);
576 gen_B(s, (uint8_t)a);
577 }
578 pos = s->pc;
579 gen_jmpdst(s, pc);
580 return pos;
581 }
582
583 #define genjmp2_0(s,i,a,val) genjmp2(s,i,a,JMPLINK_START,val)
584
585 static mrb_bool get_int_operand(codegen_scope *s, struct mrb_insn_data *data, mrb_int *ns);
586 static void gen_int(codegen_scope *s, uint16_t dst, mrb_int i);
587
588 static void
589 gen_move(codegen_scope *s, uint16_t dst, uint16_t src, int nopeep)
590 {
591 if (nopeep || no_peephole(s)) goto normal;
592 else if (dst == src) return;
593 else {
594 struct mrb_insn_data data = mrb_last_insn(s);
595
596 switch (data.insn) {
597 case OP_MOVE:
598 if (dst == src) return; /* remove useless MOVE */
599 if (data.a == src) {
600 if (data.b == dst) /* skip swapping MOVE */
601 return;
602 if (data.a < s->nlocals) goto normal;
603 rewind_pc(s);
604 s->lastpc = addr_pc(s, mrb_prev_pc(s, data.addr));
605 gen_move(s, dst, data.b, FALSE);
606 return;
607 }
608 if (dst == data.a) { /* skip overwritten move */
609 rewind_pc(s);
610 s->lastpc = addr_pc(s, mrb_prev_pc(s, data.addr));
611 gen_move(s, dst, src, FALSE);
612 return;
613 }
614 goto normal;
615 case OP_LOADNIL: case OP_LOADSELF: case OP_LOADT: case OP_LOADF:
616 case OP_LOADI__1:
617 case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
618 case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
619 if (data.a != src || data.a < s->nlocals) goto normal;
620 rewind_pc(s);
621 genop_1(s, data.insn, dst);
622 return;
623 case OP_HASH:
624 if (data.b != 0) goto normal;
625 /* fall through */
626 case OP_LOADI: case OP_LOADINEG:
627 case OP_LOADL: case OP_LOADSYM:
628 case OP_GETGV: case OP_GETSV: case OP_GETIV: case OP_GETCV:
629 case OP_GETCONST: case OP_STRING:
630 case OP_LAMBDA: case OP_BLOCK: case OP_METHOD: case OP_BLKPUSH:
631 if (data.a != src || data.a < s->nlocals) goto normal;
632 rewind_pc(s);
633 genop_2(s, data.insn, dst, data.b);
634 return;
635 case OP_LOADI16:
636 if (data.a != src || data.a < s->nlocals) goto normal;
637 rewind_pc(s);
638 genop_2S(s, data.insn, dst, data.b);
639 return;
640 case OP_LOADI32:
641 if (data.a != src || data.a < s->nlocals) goto normal;
642 else {
643 uint32_t i = (uint32_t)data.b<<16|data.c;
644 rewind_pc(s);
645 genop_2SS(s, data.insn, dst, i);
646 }
647 return;
648 case OP_ARRAY:
649 if (data.a != src || data.a < s->nlocals || data.a < dst) goto normal;
650 rewind_pc(s);
651 if (data.b == 0 || dst == data.a)
652 genop_2(s, OP_ARRAY, dst, 0);
653 else
654 genop_3(s, OP_ARRAY2, dst, data.a, data.b);
655 return;
656 case OP_ARRAY2:
657 if (data.a != src || data.a < s->nlocals || data.a < dst) goto normal;
658 rewind_pc(s);
659 genop_3(s, OP_ARRAY2, dst, data.b, data.c);
660 return;
661 case OP_AREF:
662 case OP_GETUPVAR:
663 if (data.a != src || data.a < s->nlocals) goto normal;
664 rewind_pc(s);
665 genop_3(s, data.insn, dst, data.b, data.c);
666 return;
667 case OP_ADDI: case OP_SUBI:
668 if (addr_pc(s, data.addr) == s->lastlabel || data.a != src || data.a < s->nlocals) goto normal;
669 else {
670 struct mrb_insn_data data0 = mrb_decode_insn(mrb_prev_pc(s, data.addr));
671 if (data0.insn != OP_MOVE || data0.a != data.a || data0.b != dst) goto normal;
672 s->pc = addr_pc(s, data0.addr);
673 if (addr_pc(s, data0.addr) != s->lastlabel) {
674 /* constant folding */
675 data0 = mrb_decode_insn(mrb_prev_pc(s, data0.addr));
676 mrb_int n;
677 if (data0.a == dst && get_int_operand(s, &data0, &n)) {
678 if ((data.insn == OP_ADDI && !mrb_int_add_overflow(n, data.b, &n)) ||
679 (data.insn == OP_SUBI && !mrb_int_sub_overflow(n, data.b, &n))) {
680 s->pc = addr_pc(s, data0.addr);
681 gen_int(s, dst, n);
682 return;
683 }
684 }
685 }
686 }
687 genop_2(s, data.insn, dst, data.b);
688 return;
689 default:
690 break;
691 }
692 }
693 normal:
694 genop_2(s, OP_MOVE, dst, src);
695 return;
696 }
697
698 static int search_upvar(codegen_scope *s, mrb_sym id, int *idx);
699
700 static void
701 gen_getupvar(codegen_scope *s, uint16_t dst, mrb_sym id)
702 {
703 int idx;
704 int lv = search_upvar(s, id, &idx);
705
706 if (!no_peephole(s)) {
707 struct mrb_insn_data data = mrb_last_insn(s);
708 if (data.insn == OP_SETUPVAR && data.a == dst && data.b == idx && data.c == lv) {
709 /* skip GETUPVAR right after SETUPVAR */
710 return;
711 }
712 }
713 genop_3(s, OP_GETUPVAR, dst, idx, lv);
714 }
715
716 static void
717 gen_setupvar(codegen_scope *s, uint16_t dst, mrb_sym id)
718 {
719 int idx;
720 int lv = search_upvar(s, id, &idx);
721
722 if (!no_peephole(s)) {
723 struct mrb_insn_data data = mrb_last_insn(s);
724 if (data.insn == OP_MOVE && data.a == dst) {
725 dst = data.b;
726 rewind_pc(s);
727 }
728 }
729 genop_3(s, OP_SETUPVAR, dst, idx, lv);
730 }
731
732 static void
733 gen_return(codegen_scope *s, uint8_t op, uint16_t src)
734 {
735 if (no_peephole(s)) {
736 genop_1(s, op, src);
737 }
738 else {
739 struct mrb_insn_data data = mrb_last_insn(s);
740
741 if (data.insn == OP_MOVE && src == data.a) {
742 rewind_pc(s);
743 genop_1(s, op, data.b);
744 }
745 else if (data.insn != OP_RETURN) {
746 genop_1(s, op, src);
747 }
748 }
749 }
750
751 static mrb_bool
752 get_int_operand(codegen_scope *s, struct mrb_insn_data *data, mrb_int *n)
753 {
754 switch (data->insn) {
755 case OP_LOADI__1:
756 *n = -1;
757 return TRUE;
758
759 case OP_LOADINEG:
760 *n = -data->b;
761 return TRUE;
762
763 case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
764 case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
765 *n = data->insn - OP_LOADI_0;
766 return TRUE;
767
768 case OP_LOADI:
769 case OP_LOADI16:
770 *n = (int16_t)data->b;
771 return TRUE;
772
773 case OP_LOADI32:
774 *n = (int32_t)((uint32_t)data->b<<16)+data->c;
775 return TRUE;
776
777 case OP_LOADL:
778 {
779 mrb_pool_value *pv = &s->pool[data->b];
780
781 if (pv->tt == IREP_TT_INT32) {
782 *n = (mrb_int)pv->u.i32;
783 }
784 #ifdef MRB_INT64
785 else if (pv->tt == IREP_TT_INT64) {
786 *n = (mrb_int)pv->u.i64;
787 }
788 #endif
789 else {
790 return FALSE;
791 }
792 }
793 return TRUE;
794
795 default:
796 return FALSE;
797 }
798 }
799
800 static void
801 gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst)
802 {
803 if (no_peephole(s)) {
804 normal:
805 genop_1(s, op, dst);
806 return;
807 }
808 else {
809 struct mrb_insn_data data = mrb_last_insn(s);
810 mrb_int n;
811
812 if (!get_int_operand(s, &data, &n)) {
813 /* not integer immediate */
814 goto normal;
815 }
816 struct mrb_insn_data data0 = mrb_decode_insn(mrb_prev_pc(s, data.addr));
817 mrb_int n0;
818 if (addr_pc(s, data.addr) == s->lastlabel || !get_int_operand(s, &data0, &n0)) {
819 /* OP_ADDI/OP_SUBI takes upto 8bits */
820 if (n > INT8_MAX || n < INT8_MIN) goto normal;
821 rewind_pc(s);
822 if (n == 0) return;
823 if (n > 0) {
824 if (op == OP_ADD) genop_2(s, OP_ADDI, dst, (uint16_t)n);
825 else genop_2(s, OP_SUBI, dst, (uint16_t)n);
826 }
827 else { /* n < 0 */
828 n = -n;
829 if (op == OP_ADD) genop_2(s, OP_SUBI, dst, (uint16_t)n);
830 else genop_2(s, OP_ADDI, dst, (uint16_t)n);
831 }
832 return;
833 }
834 if (op == OP_ADD) {
835 if (mrb_int_add_overflow(n0, n, &n)) goto normal;
836 }
837 else { /* OP_SUB */
838 if (mrb_int_sub_overflow(n0, n, &n)) goto normal;
839 }
840 s->pc = addr_pc(s, data0.addr);
841 gen_int(s, dst, n);
842 }
843 }
844
845 static void
846 gen_muldiv(codegen_scope *s, uint8_t op, uint16_t dst)
847 {
848 if (no_peephole(s)) {
849 normal:
850 genop_1(s, op, dst);
851 return;
852 }
853 else {
854 struct mrb_insn_data data = mrb_last_insn(s);
855 mrb_int n, n0;
856 if (addr_pc(s, data.addr) == s->lastlabel || !get_int_operand(s, &data, &n)) {
857 /* not integer immediate */
858 goto normal;
859 }
860 struct mrb_insn_data data0 = mrb_decode_insn(mrb_prev_pc(s, data.addr));
861 if (!get_int_operand(s, &data0, &n0)) {
862 goto normal;
863 }
864 if (op == OP_MUL) {
865 if (mrb_int_mul_overflow(n0, n, &n)) goto normal;
866 }
867 else { /* OP_DIV */
868 if (n == 0) goto normal;
869 if (n0 == MRB_INT_MIN && n == -1) goto normal;
870 n = mrb_div_int(n0, n);
871 }
872 s->pc = addr_pc(s, data0.addr);
873 gen_int(s, dst, n);
874 }
875 }
876
877 mrb_bool mrb_num_shift(mrb_state *mrb, mrb_int val, mrb_int width, mrb_int *num);
878
879 static mrb_bool
880 gen_binop(codegen_scope *s, mrb_sym op, uint16_t dst)
881 {
882 if (no_peephole(s)) return FALSE;
883 else if (op == MRB_OPSYM_2(s->mrb, aref)) {
884 genop_1(s, OP_GETIDX, dst);
885 return TRUE;
886 }
887 else {
888 struct mrb_insn_data data = mrb_last_insn(s);
889 mrb_int n, n0;
890 if (addr_pc(s, data.addr) == s->lastlabel || !get_int_operand(s, &data, &n)) {
891 /* not integer immediate */
892 return FALSE;
893 }
894 struct mrb_insn_data data0 = mrb_decode_insn(mrb_prev_pc(s, data.addr));
895 if (!get_int_operand(s, &data0, &n0)) {
896 return FALSE;
897 }
898 if (op == MRB_OPSYM_2(s->mrb, lshift)) {
899 if (!mrb_num_shift(s->mrb, n0, n, &n)) return FALSE;
900 }
901 else if (op == MRB_OPSYM_2(s->mrb, rshift)) {
902 if (n == MRB_INT_MIN) return FALSE;
903 if (!mrb_num_shift(s->mrb, n0, -n, &n)) return FALSE;
904 }
905 else if (op == MRB_OPSYM_2(s->mrb, mod) && n != 0) {
906 if (n0 == MRB_INT_MIN && n == -1) {
907 n = 0;
908 }
909 else {
910 mrb_int n1 = n0 % n;
911 if ((n0 < 0) != (n < 0) && n1 != 0) {
912 n1 += n;
913 }
914 n = n1;
915 }
916 }
917 else if (op == MRB_OPSYM_2(s->mrb, and)) {
918 n = n0 & n;
919 }
920 else if (op == MRB_OPSYM_2(s->mrb, or)) {
921 n = n0 | n;
922 }
923 else if (op == MRB_OPSYM_2(s->mrb, xor)) {
924 n = n0 ^ n;
925 }
926 else {
927 return FALSE;
928 }
929 s->pc = addr_pc(s, data0.addr);
930 gen_int(s, dst, n);
931 return TRUE;
932 }
933 }
934
935 static uint32_t
936 dispatch(codegen_scope *s, uint32_t pos0)
937 {
938 int32_t pos1;
939 int32_t offset;
940 int16_t newpos;
941
942 if (pos0 == JMPLINK_START) return 0;
943
944 pos1 = pos0 + 2;
945 offset = s->pc - pos1;
946 if (offset > INT16_MAX) {
947 codegen_error(s, "too big jmp offset");
948 }
949 s->lastlabel = s->pc;
950 newpos = (int16_t)PEEK_S(s->iseq+pos0);
951 emit_S(s, pos0, (uint16_t)offset);
952 if (newpos == 0) return 0;
953 return pos1+newpos;
954 }
955
956 static void
957 dispatch_linked(codegen_scope *s, uint32_t pos)
958 {
959 if (pos==JMPLINK_START) return;
960 for (;;) {
961 pos = dispatch(s, pos);
962 if (pos==0) break;
963 }
964 }
965
966 #define nregs_update do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0)
967 static void
968 push_n_(codegen_scope *s, int n)
969 {
970 if (s->sp+n >= 0xffff) {
971 codegen_error(s, "too complex expression");
972 }
973 s->sp+=n;
974 nregs_update;
975 }
976
977 static void
978 pop_n_(codegen_scope *s, int n)
979 {
980 if ((int)s->sp-n < 0) {
981 codegen_error(s, "stack pointer underflow");
982 }
983 s->sp-=n;
984 }
985
986 #define push() push_n_(s,1)
987 #define push_n(n) push_n_(s,n)
988 #define pop() pop_n_(s,1)
989 #define pop_n(n) pop_n_(s,n)
990 #define cursp() (s->sp)
991
992 static mrb_pool_value*
993 lit_pool_extend(codegen_scope *s)
994 {
995 if (s->irep->plen == s->pcapa) {
996 s->pcapa *= 2;
997 s->pool = (mrb_pool_value*)codegen_realloc(s, s->pool, sizeof(mrb_pool_value)*s->pcapa);
998 }
999
1000 return &s->pool[s->irep->plen++];
1001 }
1002
1003 static int
1004 new_litbint(codegen_scope *s, const char *p, int base, mrb_bool neg)
1005 {
1006 int i;
1007 size_t plen;
1008 mrb_pool_value *pv;
1009
1010 plen = strlen(p);
1011 if (plen > 255) {
1012 codegen_error(s, "integer too big");
1013 }
1014 for (i=0; i<s->irep->plen; i++) {
1015 size_t len;
1016 pv = &s->pool[i];
1017 if (pv->tt != IREP_TT_BIGINT) continue;
1018 len = pv->u.str[0];
1019 if (len == plen && pv->u.str[1] == base && memcmp(pv->u.str+2, p, len) == 0)
1020 return i;
1021 }
1022
1023 pv = lit_pool_extend(s);
1024
1025 char *buf;
1026 pv->tt = IREP_TT_BIGINT;
1027 buf = (char*)codegen_realloc(s, NULL, plen+3);
1028 buf[0] = (char)plen;
1029 if (neg) buf[1] = -base;
1030 else buf[1] = base;
1031 memcpy(buf+2, p, plen);
1032 buf[plen+2] = '\0';
1033 pv->u.str = buf;
1034
1035 return i;
1036 }
1037
1038 static int
1039 new_lit_str(codegen_scope *s, const char *str, mrb_int len)
1040 {
1041 int i;
1042 mrb_pool_value *pv;
1043
1044 for (i=0; i<s->irep->plen; i++) {
1045 pv = &s->pool[i];
1046 if (pv->tt & IREP_TT_NFLAG) continue;
1047 mrb_int plen = pv->tt>>2;
1048 if (len != plen) continue;
1049 if (memcmp(pv->u.str, str, plen) == 0)
1050 return i;
1051 }
1052
1053 pv = lit_pool_extend(s);
1054
1055 if (mrb_ro_data_p(str)) {
1056 pv->tt = (uint32_t)(len<<2) | IREP_TT_SSTR;
1057 pv->u.str = str;
1058 }
1059 else {
1060 char *p;
1061 pv->tt = (uint32_t)(len<<2) | IREP_TT_STR;
1062 p = (char*)codegen_realloc(s, NULL, len+1);
1063 memcpy(p, str, len);
1064 p[len] = '\0';
1065 pv->u.str = p;
1066 }
1067
1068 return i;
1069 }
1070
1071 static int
1072 new_lit_cstr(codegen_scope *s, const char *str)
1073 {
1074 return new_lit_str(s, str, (mrb_int)strlen(str));
1075 }
1076
1077 static int
1078 new_lit_int(codegen_scope *s, mrb_int num)
1079 {
1080 int i;
1081 mrb_pool_value *pv;
1082
1083 for (i=0; i<s->irep->plen; i++) {
1084 pv = &s->pool[i];
1085 if (pv->tt == IREP_TT_INT32) {
1086 if (num == pv->u.i32) return i;
1087 }
1088 #ifdef MRB_64BIT
1089 else if (pv->tt == IREP_TT_INT64) {
1090 if (num == pv->u.i64) return i;
1091 }
1092 continue;
1093 #endif
1094 }
1095
1096 pv = lit_pool_extend(s);
1097
1098 #ifdef MRB_INT64
1099 pv->tt = IREP_TT_INT64;
1100 pv->u.i64 = num;
1101 #else
1102 pv->tt = IREP_TT_INT32;
1103 pv->u.i32 = num;
1104 #endif
1105
1106 return i;
1107 }
1108
1109 #ifndef MRB_NO_FLOAT
1110 static int
1111 new_lit_float(codegen_scope *s, mrb_float num)
1112 {
1113 int i;
1114 mrb_pool_value *pv;
1115
1116 for (i=0; i<s->irep->plen; i++) {
1117 mrb_float f;
1118 pv = &s->pool[i];
1119 if (pv->tt != IREP_TT_FLOAT) continue;
1120 f = pv->u.f;
1121 if (f == num && !signbit(f) == !signbit(num)) return i;
1122 }
1123
1124 pv = lit_pool_extend(s);
1125
1126 pv->tt = IREP_TT_FLOAT;
1127 pv->u.f = num;
1128
1129 return i;
1130 }
1131 #endif
1132
1133 static int
1134 new_sym(codegen_scope *s, mrb_sym sym)
1135 {
1136 int i, len;
1137
1138 mrb_assert(s->irep);
1139
1140 len = s->irep->slen;
1141 for (i=0; i<len; i++) {
1142 if (s->syms[i] == sym) return i;
1143 }
1144 if (s->irep->slen >= s->scapa) {
1145 s->scapa *= 2;
1146 if (s->scapa > 0xffff) {
1147 codegen_error(s, "too many symbols");
1148 }
1149 s->syms = (mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*s->scapa);
1150 }
1151 s->syms[s->irep->slen] = sym;
1152 return s->irep->slen++;
1153 }
1154
1155 static void
1156 gen_setxv(codegen_scope *s, uint8_t op, uint16_t dst, mrb_sym sym, int val)
1157 {
1158 int idx = new_sym(s, sym);
1159 if (!val && !no_peephole(s)) {
1160 struct mrb_insn_data data = mrb_last_insn(s);
1161 if (data.insn == OP_MOVE && data.a == dst) {
1162 dst = data.b;
1163 rewind_pc(s);
1164 }
1165 }
1166 genop_2(s, op, dst, idx);
1167 }
1168
1169 static void
1170 gen_int(codegen_scope *s, uint16_t dst, mrb_int i)
1171 {
1172 if (i < 0) {
1173 if (i == -1) genop_1(s, OP_LOADI__1, dst);
1174 else if (i >= -0xff) genop_2(s, OP_LOADINEG, dst, (uint16_t)-i);
1175 else if (i >= INT16_MIN) genop_2S(s, OP_LOADI16, dst, (uint16_t)i);
1176 else if (i >= INT32_MIN) genop_2SS(s, OP_LOADI32, dst, (uint32_t)i);
1177 else goto int_lit;
1178 }
1179 else if (i < 8) genop_1(s, OP_LOADI_0 + (uint8_t)i, dst);
1180 else if (i <= 0xff) genop_2(s, OP_LOADI, dst, (uint16_t)i);
1181 else if (i <= INT16_MAX) genop_2S(s, OP_LOADI16, dst, (uint16_t)i);
1182 else if (i <= INT32_MAX) genop_2SS(s, OP_LOADI32, dst, (uint32_t)i);
1183 else {
1184 int_lit:
1185 genop_2(s, OP_LOADL, dst, new_lit_int(s, i));
1186 }
1187 }
1188
1189 static mrb_bool
1190 gen_uniop(codegen_scope *s, mrb_sym sym, uint16_t dst)
1191 {
1192 if (no_peephole(s)) return FALSE;
1193 struct mrb_insn_data data = mrb_last_insn(s);
1194 mrb_int n;
1195
1196 if (!get_int_operand(s, &data, &n)) return FALSE;
1197 if (sym == MRB_OPSYM_2(s->mrb, plus)) {
1198 /* unary plus does nothing */
1199 }
1200 else if (sym == MRB_OPSYM_2(s->mrb, minus)) {
1201 if (n == MRB_INT_MIN) return FALSE;
1202 n = -n;
1203 }
1204 else if (sym == MRB_OPSYM_2(s->mrb, neg)) {
1205 n = ~n;
1206 }
1207 else {
1208 return FALSE;
1209 }
1210 s->pc = addr_pc(s, data.addr);
1211 gen_int(s, dst, n);
1212 return TRUE;
1213 }
1214
1215 static int
1216 node_len(node *tree)
1217 {
1218 int n = 0;
1219
1220 while (tree) {
1221 n++;
1222 tree = tree->cdr;
1223 }
1224 return n;
1225 }
1226
1227 #define nint(x) ((int)(intptr_t)(x))
1228 #define nchar(x) ((char)(intptr_t)(x))
1229 #define nsym(x) ((mrb_sym)(intptr_t)(x))
1230
1231 #define lv_name(lv) nsym((lv)->car)
1232
1233 static int
1234 lv_idx(codegen_scope *s, mrb_sym id)
1235 {
1236 node *lv = s->lv;
1237 int n = 1;
1238
1239 while (lv) {
1240 if (lv_name(lv) == id) return n;
1241 n++;
1242 lv = lv->cdr;
1243 }
1244 return 0;
1245 }
1246
1247 static int
1248 search_upvar(codegen_scope *s, mrb_sym id, int *idx)
1249 {
1250 const struct RProc *u;
1251 int lv = 0;
1252 codegen_scope *up = s->prev;
1253
1254 while (up) {
1255 *idx = lv_idx(up, id);
1256 if (*idx > 0) {
1257 return lv;
1258 }
1259 lv++;
1260 up = up->prev;
1261 }
1262
1263 if (lv < 1) lv = 1;
1264 u = s->parser->upper;
1265 while (u && !MRB_PROC_CFUNC_P(u)) {
1266 const struct mrb_irep *ir = u->body.irep;
1267 uint_fast16_t n = ir->nlocals;
1268 int i;
1269
1270 const mrb_sym *v = ir->lv;
1271 if (v) {
1272 for (i=1; n > 1; n--, v++, i++) {
1273 if (*v == id) {
1274 *idx = i;
1275 return lv - 1;
1276 }
1277 }
1278 }
1279 if (MRB_PROC_SCOPE_P(u)) break;
1280 u = u->upper;
1281 lv++;
1282 }
1283
1284 if (id == MRB_OPSYM_2(s->mrb, and)) {
1285 codegen_error(s, "No anonymous block parameter");
1286 }
1287 else if (id == MRB_OPSYM_2(s->mrb, mul)) {
1288 codegen_error(s, "No anonymous rest parameter");
1289 }
1290 else if (id == MRB_OPSYM_2(s->mrb, pow)) {
1291 codegen_error(s, "No anonymous keyword rest parameter");
1292 }
1293 else {
1294 codegen_error(s, "Can't find local variables");
1295 }
1296 return -1; /* not reached */
1297 }
1298
1299 static void
1300 for_body(codegen_scope *s, node *tree)
1301 {
1302 codegen_scope *prev = s;
1303 int idx;
1304 struct loopinfo *lp;
1305 node *n2;
1306
1307 /* generate receiver */
1308 codegen(s, tree->cdr->car, VAL);
1309 /* generate loop-block */
1310 s = scope_new(s->mrb, s, NULL);
1311
1312 push(); /* push for a block parameter */
1313
1314 /* generate loop variable */
1315 n2 = tree->car;
1316 genop_W(s, OP_ENTER, 0x40000);
1317 if (n2->car && !n2->car->cdr && !n2->cdr) {
1318 gen_assignment(s, n2->car->car, NULL, 1, NOVAL);
1319 }
1320 else {
1321 gen_massignment(s, n2, 1, VAL);
1322 }
1323 /* construct loop */
1324 lp = loop_push(s, LOOP_FOR);
1325 lp->pc1 = new_label(s);
1326
1327 /* loop body */
1328 codegen(s, tree->cdr->cdr->car, VAL);
1329 pop();
1330 gen_return(s, OP_RETURN, cursp());
1331 loop_pop(s, NOVAL);
1332 scope_finish(s);
1333 s = prev;
1334 genop_2(s, OP_BLOCK, cursp(), s->irep->rlen-1);
1335 push();pop(); /* space for a block */
1336 pop();
1337 idx = new_sym(s, MRB_SYM_2(s->mrb, each));
1338 genop_3(s, OP_SENDB, cursp(), idx, 0);
1339 }
1340
1341 static int
1342 lambda_body(codegen_scope *s, node *tree, int blk)
1343 {
1344 codegen_scope *parent = s;
1345 s = scope_new(s->mrb, s, tree->car);
1346
1347 s->mscope = !blk;
1348
1349 if (blk) {
1350 struct loopinfo *lp = loop_push(s, LOOP_BLOCK);
1351 lp->pc0 = new_label(s);
1352 }
1353 tree = tree->cdr;
1354 if (tree->car == NULL) {
1355 genop_W(s, OP_ENTER, 0);
1356 s->ainfo = 0;
1357 }
1358 else {
1359 mrb_aspec a;
1360 int ma, oa, ra, pa, ka, kd, ba, i;
1361 uint32_t pos;
1362 node *opt;
1363 node *margs, *pargs;
1364 node *tail;
1365
1366 /* mandatory arguments */
1367 ma = node_len(tree->car->car);
1368 margs = tree->car->car;
1369 tail = tree->car->cdr->cdr->cdr->cdr;
1370
1371 /* optional arguments */
1372 oa = node_len(tree->car->cdr->car);
1373 /* rest argument? */
1374 ra = tree->car->cdr->cdr->car ? 1 : 0;
1375 /* mandatory arguments after rest argument */
1376 pa = node_len(tree->car->cdr->cdr->cdr->car);
1377 pargs = tree->car->cdr->cdr->cdr->car;
1378 /* keyword arguments */
1379 ka = tail? node_len(tail->cdr->car) : 0;
1380 /* keyword dictionary? */
1381 kd = tail && tail->cdr->cdr->car? 1 : 0;
1382 /* block argument? */
1383 ba = tail && tail->cdr->cdr->cdr->car ? 1 : 0;
1384
1385 if (ma > 0x1f || oa > 0x1f || pa > 0x1f || ka > 0x1f) {
1386 codegen_error(s, "too many formal arguments");
1387 }
1388 /* (23bits = 5:5:1:5:5:1:1) */
1389 a = MRB_ARGS_REQ(ma)
1390 | MRB_ARGS_OPT(oa)
1391 | (ra? MRB_ARGS_REST() : 0)
1392 | MRB_ARGS_POST(pa)
1393 | MRB_ARGS_KEY(ka, kd)
1394 | (ba? MRB_ARGS_BLOCK() : 0);
1395 genop_W(s, OP_ENTER, a);
1396 /* (12bits = 5:1:5:1) */
1397 s->ainfo = (((ma+oa) & 0x3f) << 7)
1398 | ((ra & 0x1) << 6)
1399 | ((pa & 0x1f) << 1)
1400 | ((ka | kd) ? 1 : 0);
1401 /* generate jump table for optional arguments initializer */
1402 pos = new_label(s);
1403 for (i=0; i<oa; i++) {
1404 new_label(s);
1405 genjmp_0(s, OP_JMP);
1406 }
1407 if (oa > 0) {
1408 genjmp_0(s, OP_JMP);
1409 }
1410 opt = tree->car->cdr->car;
1411 i = 0;
1412 while (opt) {
1413 int idx;
1414 mrb_sym id = nsym(opt->car->car);
1415
1416 dispatch(s, pos+i*3+1);
1417 codegen(s, opt->car->cdr, VAL);
1418 pop();
1419 idx = lv_idx(s, id);
1420 if (idx > 0) {
1421 gen_move(s, idx, cursp(), 0);
1422 }
1423 else {
1424 gen_getupvar(s, cursp(), id);
1425 }
1426 i++;
1427 opt = opt->cdr;
1428 }
1429 if (oa > 0) {
1430 dispatch(s, pos+i*3+1);
1431 }
1432
1433 /* keyword arguments */
1434 if (tail) {
1435 node *kwds = tail->cdr->car;
1436 int kwrest = 0;
1437
1438 if (tail->cdr->cdr->car) {
1439 kwrest = 1;
1440 }
1441 mrb_assert(nint(tail->car) == NODE_ARGS_TAIL);
1442 mrb_assert(node_len(tail) == 4);
1443
1444 while (kwds) {
1445 int jmpif_key_p, jmp_def_set = -1;
1446 node *kwd = kwds->car, *def_arg = kwd->cdr->cdr->car;
1447 mrb_sym kwd_sym = nsym(kwd->cdr->car);
1448
1449 mrb_assert(nint(kwd->car) == NODE_KW_ARG);
1450
1451 if (def_arg) {
1452 int idx;
1453 genop_2(s, OP_KEY_P, lv_idx(s, kwd_sym), new_sym(s, kwd_sym));
1454 jmpif_key_p = genjmp2_0(s, OP_JMPIF, lv_idx(s, kwd_sym), NOVAL);
1455 codegen(s, def_arg, VAL);
1456 pop();
1457 idx = lv_idx(s, kwd_sym);
1458 if (idx > 0) {
1459 gen_move(s, idx, cursp(), 0);
1460 }
1461 else {
1462 gen_getupvar(s, cursp(), kwd_sym);
1463 }
1464 jmp_def_set = genjmp_0(s, OP_JMP);
1465 dispatch(s, jmpif_key_p);
1466 }
1467 genop_2(s, OP_KARG, lv_idx(s, kwd_sym), new_sym(s, kwd_sym));
1468 if (jmp_def_set != -1) {
1469 dispatch(s, jmp_def_set);
1470 }
1471 i++;
1472
1473 kwds = kwds->cdr;
1474 }
1475 if (tail->cdr->car && !kwrest) {
1476 genop_0(s, OP_KEYEND);
1477 }
1478 }
1479
1480 /* argument destructuring */
1481 if (margs) {
1482 node *n = margs;
1483
1484 pos = 1;
1485 while (n) {
1486 if (nint(n->car->car) == NODE_MASGN) {
1487 gen_massignment(s, n->car->cdr->car, pos, NOVAL);
1488 }
1489 pos++;
1490 n = n->cdr;
1491 }
1492 }
1493 if (pargs) {
1494 node *n = pargs;
1495
1496 pos = ma+oa+ra+1;
1497 while (n) {
1498 if (nint(n->car->car) == NODE_MASGN) {
1499 gen_massignment(s, n->car->cdr->car, pos, NOVAL);
1500 }
1501 pos++;
1502 n = n->cdr;
1503 }
1504 }
1505 }
1506
1507 codegen(s, tree->cdr->car, VAL);
1508 pop();
1509 if (s->pc > 0) {
1510 gen_return(s, OP_RETURN, cursp());
1511 }
1512 if (blk) {
1513 loop_pop(s, NOVAL);
1514 }
1515 scope_finish(s);
1516 return parent->irep->rlen - 1;
1517 }
1518
1519 static int
1520 scope_body(codegen_scope *s, node *tree, int val)
1521 {
1522 codegen_scope *scope = scope_new(s->mrb, s, tree->car);
1523
1524 codegen(scope, tree->cdr, VAL);
1525 gen_return(scope, OP_RETURN, scope->sp-1);
1526 if (!s->iseq) {
1527 genop_0(scope, OP_STOP);
1528 }
1529 scope_finish(scope);
1530 if (!s->irep) {
1531 /* should not happen */
1532 return 0;
1533 }
1534 return s->irep->rlen - 1;
1535 }
1536
1537 static mrb_bool
1538 nosplat(node *t)
1539 {
1540 while (t) {
1541 if (nint(t->car->car) == NODE_SPLAT) return FALSE;
1542 t = t->cdr;
1543 }
1544 return TRUE;
1545 }
1546
1547 static mrb_sym
1548 attrsym(codegen_scope *s, mrb_sym a)
1549 {
1550 const char *name;
1551 mrb_int len;
1552 char *name2;
1553
1554 name = mrb_sym_name_len(s->mrb, a, &len);
1555 name2 = (char*)codegen_palloc(s,
1556 (size_t)len
1557 + 1 /* '=' */
1558 + 1 /* '\0' */
1559 );
1560 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
1561 memcpy(name2, name, (size_t)len);
1562 name2[len] = '=';
1563 name2[len+1] = '\0';
1564
1565 return mrb_intern(s->mrb, name2, len+1);
1566 }
1567
1568 #define CALL_MAXARGS 15
1569 #define GEN_LIT_ARY_MAX 64
1570 #define GEN_VAL_STACK_MAX 99
1571
1572 static int
1573 gen_values(codegen_scope *s, node *t, int val, int limit)
1574 {
1575 int n = 0;
1576 int first = 1;
1577 int slimit = GEN_VAL_STACK_MAX;
1578
1579 if (limit == 0) limit = GEN_LIT_ARY_MAX;
1580 if (cursp() >= slimit) slimit = INT16_MAX;
1581
1582 if (!val) {
1583 while (t) {
1584 codegen(s, t->car, NOVAL);
1585 n++;
1586 t = t->cdr;
1587 }
1588 return n;
1589 }
1590
1591 while (t) {
1592 int is_splat = nint(t->car->car) == NODE_SPLAT;
1593
1594 if (is_splat || cursp() >= slimit) { /* flush stack */
1595 pop_n(n);
1596 if (first) {
1597 if (n == 0) {
1598 genop_1(s, OP_LOADNIL, cursp());
1599 }
1600 else {
1601 genop_2(s, OP_ARRAY, cursp(), n);
1602 }
1603 push();
1604 first = 0;
1605 limit = GEN_LIT_ARY_MAX;
1606 }
1607 else if (n > 0) {
1608 pop();
1609 genop_2(s, OP_ARYPUSH, cursp(), n);
1610 push();
1611 }
1612 n = 0;
1613 }
1614 codegen(s, t->car, val);
1615 if (is_splat) {
1616 pop(); pop();
1617 genop_1(s, OP_ARYCAT, cursp());
1618 push();
1619 }
1620 else {
1621 n++;
1622 }
1623 t = t->cdr;
1624 }
1625 if (!first) {
1626 pop();
1627 if (n > 0) {
1628 pop_n(n);
1629 genop_2(s, OP_ARYPUSH, cursp(), n);
1630 }
1631 return -1; /* variable length */
1632 }
1633 else if (n > limit) {
1634 pop_n(n);
1635 genop_2(s, OP_ARRAY, cursp(), n);
1636 return -1;
1637 }
1638 return n;
1639 }
1640
1641 static int
1642 gen_hash(codegen_scope *s, node *tree, int val, int limit)
1643 {
1644 int slimit = GEN_VAL_STACK_MAX;
1645 if (cursp() >= GEN_LIT_ARY_MAX) slimit = INT16_MAX;
1646 int len = 0;
1647 mrb_bool update = FALSE;
1648 mrb_bool first = TRUE;
1649
1650 while (tree) {
1651 if (nint(tree->car->car->car) == NODE_KW_REST_ARGS) {
1652 if (val && first) {
1653 genop_2(s, OP_HASH, cursp(), 0);
1654 push();
1655 update = TRUE;
1656 }
1657 else if (val && len > 0) {
1658 pop_n(len*2);
1659 if (!update) {
1660 genop_2(s, OP_HASH, cursp(), len);
1661 }
1662 else {
1663 pop();
1664 genop_2(s, OP_HASHADD, cursp(), len);
1665 }
1666 push();
1667 }
1668 codegen(s, tree->car->cdr, val);
1669 if (val && (len > 0 || update)) {
1670 pop(); pop();
1671 genop_1(s, OP_HASHCAT, cursp());
1672 push();
1673 }
1674 update = TRUE;
1675 len = 0;
1676 }
1677 else {
1678 codegen(s, tree->car->car, val);
1679 codegen(s, tree->car->cdr, val);
1680 len++;
1681 }
1682 tree = tree->cdr;
1683 if (val && cursp() >= slimit) {
1684 pop_n(len*2);
1685 if (!update) {
1686 genop_2(s, OP_HASH, cursp(), len);
1687 }
1688 else {
1689 pop();
1690 genop_2(s, OP_HASHADD, cursp(), len);
1691 }
1692 push();
1693 update = TRUE;
1694 len = 0;
1695 }
1696 first = FALSE;
1697 }
1698 if (val && len > limit) {
1699 pop_n(len*2);
1700 genop_2(s, OP_HASH, cursp(), len);
1701 push();
1702 return -1;
1703 }
1704 if (update) {
1705 if (val && len > 0) {
1706 pop_n(len*2+1);
1707 genop_2(s, OP_HASHADD, cursp(), len);
1708 push();
1709 }
1710 return -1; /* variable length */
1711 }
1712 return len;
1713 }
1714
1715 static void
1716 gen_call(codegen_scope *s, node *tree, int val, int safe)
1717 {
1718 mrb_sym sym = nsym(tree->cdr->car);
1719 int skip = 0, n = 0, nk = 0, noop = no_optimize(s), noself = 0, blk = 0, sp_save = cursp();
1720
1721 if (!tree->car) {
1722 noself = noop = 1;
1723 push();
1724 }
1725 else {
1726 codegen(s, tree->car, VAL); /* receiver */
1727 }
1728 if (safe) {
1729 int recv = cursp()-1;
1730 gen_move(s, cursp(), recv, 1);
1731 skip = genjmp2_0(s, OP_JMPNIL, cursp(), val);
1732 }
1733 tree = tree->cdr->cdr->car;
1734 if (tree) {
1735 if (tree->car) { /* positional arguments */
1736 n = gen_values(s, tree->car, VAL, 14);
1737 if (n < 0) { /* variable length */
1738 noop = 1; /* not operator */
1739 n = 15;
1740 push();
1741 }
1742 }
1743 if (tree->cdr->car) { /* keyword arguments */
1744 noop = 1;
1745 nk = gen_hash(s, tree->cdr->car->cdr, VAL, 14);
1746 if (nk < 0) nk = 15;
1747 }
1748 }
1749 if (tree && tree->cdr && tree->cdr->cdr) {
1750 codegen(s, tree->cdr->cdr, VAL);
1751 pop();
1752 noop = 1;
1753 blk = 1;
1754 }
1755 push();pop();
1756 s->sp = sp_save;
1757 if (!noop && sym == MRB_OPSYM_2(s->mrb, add) && n == 1) {
1758 gen_addsub(s, OP_ADD, cursp());
1759 }
1760 else if (!noop && sym == MRB_OPSYM_2(s->mrb, sub) && n == 1) {
1761 gen_addsub(s, OP_SUB, cursp());
1762 }
1763 else if (!noop && sym == MRB_OPSYM_2(s->mrb, mul) && n == 1) {
1764 gen_muldiv(s, OP_MUL, cursp());
1765 }
1766 else if (!noop && sym == MRB_OPSYM_2(s->mrb, div) && n == 1) {
1767 gen_muldiv(s, OP_DIV, cursp());
1768 }
1769 else if (!noop && sym == MRB_OPSYM_2(s->mrb, lt) && n == 1) {
1770 genop_1(s, OP_LT, cursp());
1771 }
1772 else if (!noop && sym == MRB_OPSYM_2(s->mrb, le) && n == 1) {
1773 genop_1(s, OP_LE, cursp());
1774 }
1775 else if (!noop && sym == MRB_OPSYM_2(s->mrb, gt) && n == 1) {
1776 genop_1(s, OP_GT, cursp());
1777 }
1778 else if (!noop && sym == MRB_OPSYM_2(s->mrb, ge) && n == 1) {
1779 genop_1(s, OP_GE, cursp());
1780 }
1781 else if (!noop && sym == MRB_OPSYM_2(s->mrb, eq) && n == 1) {
1782 genop_1(s, OP_EQ, cursp());
1783 }
1784 else if (!noop && sym == MRB_OPSYM_2(s->mrb, aset) && n == 2) {
1785 genop_1(s, OP_SETIDX, cursp());
1786 }
1787 else if (!noop && n == 0 && gen_uniop(s, sym, cursp())) {
1788 /* constant folding succeeded */
1789 }
1790 else if (!noop && n == 1 && gen_binop(s, sym, cursp())) {
1791 /* constant folding succeeded */
1792 }
1793 else if (noself){
1794 genop_3(s, blk ? OP_SSENDB : OP_SSEND, cursp(), new_sym(s, sym), n|(nk<<4));
1795 }
1796 else {
1797 genop_3(s, blk ? OP_SENDB : OP_SEND, cursp(), new_sym(s, sym), n|(nk<<4));
1798 }
1799 if (safe) {
1800 dispatch(s, skip);
1801 }
1802 if (val) {
1803 push();
1804 }
1805 }
1806
1807 static void
1808 gen_assignment(codegen_scope *s, node *tree, node *rhs, int sp, int val)
1809 {
1810 int idx;
1811 int type = nint(tree->car);
1812
1813 switch (type) {
1814 case NODE_GVAR:
1815 case NODE_ARG:
1816 case NODE_LVAR:
1817 case NODE_IVAR:
1818 case NODE_CVAR:
1819 case NODE_CONST:
1820 case NODE_NIL:
1821 case NODE_MASGN:
1822 if (rhs) {
1823 codegen(s, rhs, VAL);
1824 pop();
1825 sp = cursp();
1826 }
1827 break;
1828
1829 case NODE_COLON2:
1830 case NODE_COLON3:
1831 case NODE_CALL:
1832 case NODE_SCALL:
1833 /* keep evaluation order */
1834 break;
1835
1836 case NODE_NVAR:
1837 /* never happens; should have already checked in the parser */
1838 codegen_error(s, "Can't assign to numbered parameter");
1839 break;
1840
1841 default:
1842 codegen_error(s, "unknown lhs");
1843 break;
1844 }
1845
1846 tree = tree->cdr;
1847 switch (type) {
1848 case NODE_GVAR:
1849 gen_setxv(s, OP_SETGV, sp, nsym(tree), val);
1850 break;
1851 case NODE_ARG:
1852 case NODE_LVAR:
1853 idx = lv_idx(s, nsym(tree));
1854 if (idx > 0) {
1855 if (idx != sp) {
1856 gen_move(s, idx, sp, val);
1857 }
1858 break;
1859 }
1860 else { /* upvar */
1861 gen_setupvar(s, sp, nsym(tree));
1862 }
1863 break;
1864 case NODE_IVAR:
1865 gen_setxv(s, OP_SETIV, sp, nsym(tree), val);
1866 break;
1867 case NODE_CVAR:
1868 gen_setxv(s, OP_SETCV, sp, nsym(tree), val);
1869 break;
1870 case NODE_CONST:
1871 gen_setxv(s, OP_SETCONST, sp, nsym(tree), val);
1872 break;
1873 case NODE_COLON2:
1874 case NODE_COLON3:
1875 if (sp) {
1876 gen_move(s, cursp(), sp, 0);
1877 }
1878 sp = cursp();
1879 push();
1880 if (type == NODE_COLON2) {
1881 codegen(s, tree->car, VAL);
1882 idx = new_sym(s, nsym(tree->cdr));
1883 }
1884 else { /* NODE_COLON3 */
1885 genop_1(s, OP_OCLASS, cursp());
1886 push();
1887 idx = new_sym(s, nsym(tree));
1888 }
1889 if (rhs) {
1890 codegen(s, rhs, VAL); pop();
1891 gen_move(s, sp, cursp(), 0);
1892 }
1893 pop_n(2);
1894 genop_2(s, OP_SETMCNST, sp, idx);
1895 break;
1896
1897 case NODE_CALL:
1898 case NODE_SCALL:
1899 {
1900 int noself = 0, safe = (type == NODE_SCALL), skip = 0, top, call, n = 0;
1901 mrb_sym mid = nsym(tree->cdr->car);
1902
1903 top = cursp();
1904 if (val || sp == cursp()) {
1905 push(); /* room for retval */
1906 }
1907 call = cursp();
1908 if (!tree->car) {
1909 noself = 1;
1910 push();
1911 }
1912 else {
1913 codegen(s, tree->car, VAL); /* receiver */
1914 }
1915 if (safe) {
1916 int recv = cursp()-1;
1917 gen_move(s, cursp(), recv, 1);
1918 skip = genjmp2_0(s, OP_JMPNIL, cursp(), val);
1919 }
1920 tree = tree->cdr->cdr->car;
1921 if (tree) {
1922 if (tree->car) { /* positional arguments */
1923 n = gen_values(s, tree->car, VAL, (tree->cdr->car)?13:14);
1924 if (n < 0) { /* variable length */
1925 n = 15;
1926 push();
1927 }
1928 }
1929 if (tree->cdr->car) { /* keyword arguments */
1930 if (n == 13 || n == 14) {
1931 pop_n(n);
1932 genop_2(s, OP_ARRAY, cursp(), n);
1933 push();
1934 n = 15;
1935 }
1936 gen_hash(s, tree->cdr->car->cdr, VAL, 0);
1937 if (n < 14) {
1938 n++;
1939 }
1940 else {
1941 pop_n(2);
1942 genop_2(s, OP_ARYPUSH, cursp(), 1);
1943 }
1944 push();
1945 }
1946 }
1947 if (rhs) {
1948 codegen(s, rhs, VAL);
1949 pop();
1950 }
1951 else {
1952 gen_move(s, cursp(), sp, 0);
1953 }
1954 if (val) {
1955 gen_move(s, top, cursp(), 1);
1956 }
1957 if (n < 15) {
1958 n++;
1959 if (n == 15) {
1960 pop_n(14);
1961 genop_2(s, OP_ARRAY, cursp(), 15);
1962 }
1963 }
1964 else {
1965 pop();
1966 genop_2(s, OP_ARYPUSH, cursp(), 1);
1967 }
1968 push(); pop();
1969 s->sp = call;
1970 if (mid == MRB_OPSYM_2(s->mrb, aref) && n == 2) {
1971 push_n(4); pop_n(4); /* self + idx + value + (invisible block for OP_SEND) */
1972 genop_1(s, OP_SETIDX, cursp());
1973 }
1974 else {
1975 int st = 2 /* self + block */ +
1976 (((n >> 0) & 0x0f) < 15 ? ((n >> 0) & 0x0f) : 1) +
1977 (((n >> 4) & 0x0f) < 15 ? ((n >> 4) & 0x0f) * 2 : 1);
1978 push_n(st); pop_n(st);
1979 genop_3(s, noself ? OP_SSEND : OP_SEND, cursp(), new_sym(s, attrsym(s, mid)), n);
1980 }
1981 if (safe) {
1982 dispatch(s, skip);
1983 }
1984 s->sp = top;
1985 }
1986 break;
1987
1988 case NODE_MASGN:
1989 gen_massignment(s, tree->car, sp, val);
1990 break;
1991
1992 /* splat without assignment */
1993 case NODE_NIL:
1994 break;
1995
1996 default:
1997 codegen_error(s, "unknown lhs");
1998 break;
1999 }
2000 if (val) push();
2001 }
2002
2003 static void
2004 gen_massignment(codegen_scope *s, node *tree, int rhs, int val)
2005 {
2006 int n = 0, post = 0;
2007 node *t, *p;
2008
2009 if (tree->car) { /* pre */
2010 t = tree->car;
2011 n = 0;
2012 while (t) {
2013 int sp = cursp();
2014
2015 genop_3(s, OP_AREF, sp, rhs, n);
2016 push();
2017 gen_assignment(s, t->car, NULL, sp, NOVAL);
2018 pop();
2019 n++;
2020 t = t->cdr;
2021 }
2022 }
2023 t = tree->cdr;
2024 if (t) {
2025 if (t->cdr) { /* post count */
2026 p = t->cdr->car;
2027 while (p) {
2028 post++;
2029 p = p->cdr;
2030 }
2031 }
2032 gen_move(s, cursp(), rhs, val);
2033 push_n(post+1);
2034 pop_n(post+1);
2035 genop_3(s, OP_APOST, cursp(), n, post);
2036 n = 1;
2037 if (t->car && t->car != (node*)-1) { /* rest */
2038 gen_assignment(s, t->car, NULL, cursp(), NOVAL);
2039 }
2040 if (t->cdr && t->cdr->car) {
2041 t = t->cdr->car;
2042 while (t) {
2043 gen_assignment(s, t->car, NULL, cursp()+n, NOVAL);
2044 t = t->cdr;
2045 n++;
2046 }
2047 }
2048 if (val) {
2049 gen_move(s, cursp(), rhs, 0);
2050 }
2051 }
2052 }
2053
2054 static void
2055 gen_intern(codegen_scope *s)
2056 {
2057 pop();
2058 if (!no_peephole(s)) {
2059 struct mrb_insn_data data = mrb_last_insn(s);
2060
2061 if (data.insn == OP_STRING && data.a == cursp()) {
2062 rewind_pc(s);
2063 genop_2(s, OP_SYMBOL, data.a, data.b);
2064 push();
2065 return;
2066 }
2067 }
2068 genop_1(s, OP_INTERN, cursp());
2069 push();
2070 }
2071
2072 static void
2073 gen_literal_array(codegen_scope *s, node *tree, mrb_bool sym, int val)
2074 {
2075 if (val) {
2076 int i = 0, j = 0, gen = 0;
2077
2078 while (tree) {
2079 switch (nint(tree->car->car)) {
2080 case NODE_STR:
2081 if ((tree->cdr == NULL) && (nint(tree->car->cdr->cdr) == 0))
2082 break;
2083 /* fall through */
2084 case NODE_BEGIN:
2085 codegen(s, tree->car, VAL);
2086 j++;
2087 break;
2088
2089 case NODE_LITERAL_DELIM:
2090 if (j > 0) {
2091 j = 0;
2092 i++;
2093 if (sym)
2094 gen_intern(s);
2095 }
2096 break;
2097 }
2098 while (j >= 2) {
2099 pop(); pop();
2100 genop_1(s, OP_STRCAT, cursp());
2101 push();
2102 j--;
2103 }
2104 if (i > GEN_LIT_ARY_MAX) {
2105 pop_n(i);
2106 if (gen) {
2107 pop();
2108 genop_2(s, OP_ARYPUSH, cursp(), i);
2109 }
2110 else {
2111 genop_2(s, OP_ARRAY, cursp(), i);
2112 gen = 1;
2113 }
2114 push();
2115 i = 0;
2116 }
2117 tree = tree->cdr;
2118 }
2119 if (j > 0) {
2120 i++;
2121 if (sym)
2122 gen_intern(s);
2123 }
2124 pop_n(i);
2125 if (gen) {
2126 pop();
2127 genop_2(s, OP_ARYPUSH, cursp(), i);
2128 }
2129 else {
2130 genop_2(s, OP_ARRAY, cursp(), i);
2131 }
2132 push();
2133 }
2134 else {
2135 while (tree) {
2136 switch (nint(tree->car->car)) {
2137 case NODE_BEGIN: case NODE_BLOCK:
2138 codegen(s, tree->car, NOVAL);
2139 }
2140 tree = tree->cdr;
2141 }
2142 }
2143 }
2144
2145 static void
2146 raise_error(codegen_scope *s, const char *msg)
2147 {
2148 int idx = new_lit_cstr(s, msg);
2149
2150 genop_1(s, OP_ERR, idx);
2151 }
2152
2153 static mrb_int
2154 readint(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_bool *overflow)
2155 {
2156 const char *e = p + strlen(p);
2157 mrb_int result = 0;
2158
2159 mrb_assert(base >= 2 && base <= 16);
2160 if (*p == '+') p++;
2161 while (p < e) {
2162 int n;
2163 char c = *p;
2164 switch (c) {
2165 case '0': case '1': case '2': case '3':
2166 case '4': case '5': case '6': case '7':
2167 n = c - '0'; break;
2168 case '8': case '9':
2169 n = c - '0'; break;
2170 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2171 n = c - 'a' + 10; break;
2172 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2173 n = c - 'A' + 10; break;
2174 default:
2175 codegen_error(s, "malformed readint input");
2176 *overflow = TRUE;
2177 /* not reached */
2178 return result;
2179 }
2180 if (mrb_int_mul_overflow(result, base, &result)) {
2181 overflow:
2182 *overflow = TRUE;
2183 return 0;
2184 }
2185 mrb_uint tmp = ((mrb_uint)result)+n;
2186 if (neg && tmp == (mrb_uint)MRB_INT_MAX+1) {
2187 *overflow = FALSE;
2188 return MRB_INT_MIN;
2189 }
2190 if (tmp > MRB_INT_MAX) goto overflow;
2191 result = (mrb_int)tmp;
2192 p++;
2193 }
2194 *overflow = FALSE;
2195 if (neg) return -result;
2196 return result;
2197 }
2198
2199 static void
2200 gen_retval(codegen_scope *s, node *tree)
2201 {
2202 if (nint(tree->car) == NODE_SPLAT) {
2203 codegen(s, tree, VAL);
2204 pop();
2205 genop_1(s, OP_ARYSPLAT, cursp());
2206 }
2207 else {
2208 codegen(s, tree, VAL);
2209 pop();
2210 }
2211 }
2212
2213 static mrb_bool
2214 true_always(node *tree)
2215 {
2216 switch (nint(tree->car)) {
2217 case NODE_TRUE:
2218 case NODE_INT:
2219 case NODE_STR:
2220 case NODE_SYM:
2221 return TRUE;
2222 default:
2223 return FALSE;
2224 }
2225 }
2226
2227 static mrb_bool
2228 false_always(node *tree)
2229 {
2230 switch (nint(tree->car)) {
2231 case NODE_FALSE:
2232 case NODE_NIL:
2233 return TRUE;
2234 default:
2235 return FALSE;
2236 }
2237 }
2238
2239 static void
2240 gen_blkmove(codegen_scope *s, uint16_t ainfo, int lv)
2241 {
2242 int m1 = (ainfo>>7)&0x3f;
2243 int r = (ainfo>>6)&0x1;
2244 int m2 = (ainfo>>1)&0x1f;
2245 int kd = (ainfo)&0x1;
2246 int off = m1+r+m2+kd+1;
2247 if (lv == 0) {
2248 gen_move(s, cursp(), off, 0);
2249 }
2250 else {
2251 genop_3(s, OP_GETUPVAR, cursp(), off, lv);
2252 }
2253 push();
2254 }
2255
2256 static void
2257 codegen(codegen_scope *s, node *tree, int val)
2258 {
2259 int nt;
2260 int rlev = s->rlev;
2261
2262 if (!tree) {
2263 if (val) {
2264 genop_1(s, OP_LOADNIL, cursp());
2265 push();
2266 }
2267 return;
2268 }
2269
2270 s->rlev++;
2271 if (s->rlev > MRB_CODEGEN_LEVEL_MAX) {
2272 codegen_error(s, "too complex expression");
2273 }
2274 if (s->irep && s->filename_index != tree->filename_index) {
2275 mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
2276 const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
2277
2278 mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
2279 filename, s->lines, s->debug_start_pos, s->pc);
2280 s->debug_start_pos = s->pc;
2281 s->filename_index = tree->filename_index;
2282 s->filename_sym = mrb_parser_get_filename(s->parser, tree->filename_index);
2283 }
2284
2285 nt = nint(tree->car);
2286 s->lineno = tree->lineno;
2287 tree = tree->cdr;
2288 switch (nt) {
2289 case NODE_BEGIN:
2290 if (val && !tree) {
2291 genop_1(s, OP_LOADNIL, cursp());
2292 push();
2293 }
2294 while (tree) {
2295 codegen(s, tree->car, tree->cdr ? NOVAL : val);
2296 tree = tree->cdr;
2297 }
2298 break;
2299
2300 case NODE_RESCUE:
2301 {
2302 int noexc;
2303 uint32_t exend, pos1, pos2, tmp;
2304 struct loopinfo *lp;
2305 int catch_entry, begin, end;
2306
2307 if (tree->car == NULL) goto exit;
2308 lp = loop_push(s, LOOP_BEGIN);
2309 lp->pc0 = new_label(s);
2310 catch_entry = catch_handler_new(s);
2311 begin = s->pc;
2312 codegen(s, tree->car, VAL);
2313 pop();
2314 lp->type = LOOP_RESCUE;
2315 end = s->pc;
2316 noexc = genjmp_0(s, OP_JMP);
2317 catch_handler_set(s, catch_entry, MRB_CATCH_RESCUE, begin, end, s->pc);
2318 tree = tree->cdr;
2319 exend = JMPLINK_START;
2320 pos1 = JMPLINK_START;
2321 if (tree->car) {
2322 node *n2 = tree->car;
2323 int exc = cursp();
2324
2325 genop_1(s, OP_EXCEPT, exc);
2326 push();
2327 while (n2) {
2328 node *n3 = n2->car;
2329 node *n4 = n3->car;
2330
2331 dispatch(s, pos1);
2332 pos2 = JMPLINK_START;
2333 do {
2334 if (n4 && n4->car && nint(n4->car->car) == NODE_SPLAT) {
2335 codegen(s, n4->car, VAL);
2336 gen_move(s, cursp(), exc, 0);
2337 push_n(2); pop_n(2); /* space for one arg and a block */
2338 pop();
2339 genop_3(s, OP_SEND, cursp(), new_sym(s, MRB_SYM_2(s->mrb, __case_eqq)), 1);
2340 }
2341 else {
2342 if (n4) {
2343 codegen(s, n4->car, VAL);
2344 }
2345 else {
2346 genop_2(s, OP_GETCONST, cursp(), new_sym(s, MRB_SYM_2(s->mrb, StandardError)));
2347 push();
2348 }
2349 pop();
2350 genop_2(s, OP_RESCUE, exc, cursp());
2351 }
2352 tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, val);
2353 pos2 = tmp;
2354 if (n4) {
2355 n4 = n4->cdr;
2356 }
2357 } while (n4);
2358 pos1 = genjmp_0(s, OP_JMP);
2359 dispatch_linked(s, pos2);
2360
2361 pop();
2362 if (n3->cdr->car) {
2363 gen_assignment(s, n3->cdr->car, NULL, exc, NOVAL);
2364 }
2365 if (n3->cdr->cdr->car) {
2366 codegen(s, n3->cdr->cdr->car, val);
2367 if (val) pop();
2368 }
2369 tmp = genjmp(s, OP_JMP, exend);
2370 exend = tmp;
2371 n2 = n2->cdr;
2372 push();
2373 }
2374 if (pos1 != JMPLINK_START) {
2375 dispatch(s, pos1);
2376 genop_1(s, OP_RAISEIF, exc);
2377 }
2378 }
2379 pop();
2380 tree = tree->cdr;
2381 dispatch(s, noexc);
2382 if (tree->car) {
2383 codegen(s, tree->car, val);
2384 }
2385 else if (val) {
2386 push();
2387 }
2388 dispatch_linked(s, exend);
2389 loop_pop(s, NOVAL);
2390 }
2391 break;
2392
2393 case NODE_ENSURE:
2394 if (!tree->cdr || !tree->cdr->cdr ||
2395 (nint(tree->cdr->cdr->car) == NODE_BEGIN &&
2396 tree->cdr->cdr->cdr)) {
2397 int catch_entry, begin, end, target;
2398 int idx;
2399
2400 catch_entry = catch_handler_new(s);
2401 begin = s->pc;
2402 codegen(s, tree->car, val);
2403 end = target = s->pc;
2404 push();
2405 idx = cursp();
2406 genop_1(s, OP_EXCEPT, idx);
2407 push();
2408 codegen(s, tree->cdr->cdr, NOVAL);
2409 pop();
2410 genop_1(s, OP_RAISEIF, idx);
2411 pop();
2412 catch_handler_set(s, catch_entry, MRB_CATCH_ENSURE, begin, end, target);
2413 }
2414 else { /* empty ensure ignored */
2415 codegen(s, tree->car, val);
2416 }
2417 break;
2418
2419 case NODE_LAMBDA:
2420 if (val) {
2421 int idx = lambda_body(s, tree, 1);
2422
2423 genop_2(s, OP_LAMBDA, cursp(), idx);
2424 push();
2425 }
2426 break;
2427
2428 case NODE_BLOCK:
2429 if (val) {
2430 int idx = lambda_body(s, tree, 1);
2431
2432 genop_2(s, OP_BLOCK, cursp(), idx);
2433 push();
2434 }
2435 break;
2436
2437 case NODE_IF:
2438 {
2439 uint32_t pos1, pos2;
2440 mrb_bool nil_p = FALSE;
2441 node *elsepart = tree->cdr->cdr->car;
2442
2443 if (!tree->car) {
2444 codegen(s, elsepart, val);
2445 goto exit;
2446 }
2447 if (true_always(tree->car)) {
2448 codegen(s, tree->cdr->car, val);
2449 goto exit;
2450 }
2451 if (false_always(tree->car)) {
2452 codegen(s, elsepart, val);
2453 goto exit;
2454 }
2455 if (nint(tree->car->car) == NODE_CALL) {
2456 node *n = tree->car->cdr;
2457 mrb_sym mid = nsym(n->cdr->car);
2458 mrb_sym sym_nil_p = MRB_SYM_Q_2(s->mrb, nil);
2459 if (mid == sym_nil_p && n->cdr->cdr->car == NULL) {
2460 nil_p = TRUE;
2461 codegen(s, n->car, VAL);
2462 }
2463 }
2464 if (!nil_p) {
2465 codegen(s, tree->car, VAL);
2466 }
2467 pop();
2468 if (val || tree->cdr->car) {
2469 if (nil_p) {
2470 pos2 = genjmp2_0(s, OP_JMPNIL, cursp(), val);
2471 pos1 = genjmp_0(s, OP_JMP);
2472 dispatch(s, pos2);
2473 }
2474 else {
2475 pos1 = genjmp2_0(s, OP_JMPNOT, cursp(), val);
2476 }
2477 codegen(s, tree->cdr->car, val);
2478 if (val) pop();
2479 if (elsepart || val) {
2480 pos2 = genjmp_0(s, OP_JMP);
2481 dispatch(s, pos1);
2482 codegen(s, elsepart, val);
2483 dispatch(s, pos2);
2484 }
2485 else {
2486 dispatch(s, pos1);
2487 }
2488 }
2489 else { /* empty then-part */
2490 if (elsepart) {
2491 if (nil_p) {
2492 pos1 = genjmp2_0(s, OP_JMPNIL, cursp(), val);
2493 }
2494 else {
2495 pos1 = genjmp2_0(s, OP_JMPIF, cursp(), val);
2496 }
2497 codegen(s, elsepart, val);
2498 dispatch(s, pos1);
2499 }
2500 else if (val && !nil_p) {
2501 genop_1(s, OP_LOADNIL, cursp());
2502 push();
2503 }
2504 }
2505 }
2506 break;
2507
2508 case NODE_AND:
2509 {
2510 uint32_t pos;
2511
2512 if (true_always(tree->car)) {
2513 codegen(s, tree->cdr, val);
2514 goto exit;
2515 }
2516 if (false_always(tree->car)) {
2517 codegen(s, tree->car, val);
2518 goto exit;
2519 }
2520 codegen(s, tree->car, VAL);
2521 pop();
2522 pos = genjmp2_0(s, OP_JMPNOT, cursp(), val);
2523 codegen(s, tree->cdr, val);
2524 dispatch(s, pos);
2525 }
2526 break;
2527
2528 case NODE_OR:
2529 {
2530 uint32_t pos;
2531
2532 if (true_always(tree->car)) {
2533 codegen(s, tree->car, val);
2534 goto exit;
2535 }
2536 if (false_always(tree->car)) {
2537 codegen(s, tree->cdr, val);
2538 goto exit;
2539 }
2540 codegen(s, tree->car, VAL);
2541 pop();
2542 pos = genjmp2_0(s, OP_JMPIF, cursp(), val);
2543 codegen(s, tree->cdr, val);
2544 dispatch(s, pos);
2545 }
2546 break;
2547
2548 case NODE_WHILE:
2549 case NODE_UNTIL:
2550 {
2551 if (true_always(tree->car)) {
2552 if (nt == NODE_UNTIL) {
2553 if (val) {
2554 genop_1(s, OP_LOADNIL, cursp());
2555 push();
2556 }
2557 goto exit;
2558 }
2559 }
2560 else if (false_always(tree->car)) {
2561 if (nt == NODE_WHILE) {
2562 if (val) {
2563 genop_1(s, OP_LOADNIL, cursp());
2564 push();
2565 }
2566 goto exit;
2567 }
2568 }
2569
2570 uint32_t pos = JMPLINK_START;
2571 struct loopinfo *lp = loop_push(s, LOOP_NORMAL);
2572
2573 if (!val) lp->reg = -1;
2574 lp->pc0 = new_label(s);
2575 codegen(s, tree->car, VAL);
2576 pop();
2577 if (nt == NODE_WHILE) {
2578 pos = genjmp2_0(s, OP_JMPNOT, cursp(), NOVAL);
2579 }
2580 else {
2581 pos = genjmp2_0(s, OP_JMPIF, cursp(), NOVAL);
2582 }
2583 lp->pc1 = new_label(s);
2584 codegen(s, tree->cdr, NOVAL);
2585 genjmp(s, OP_JMP, lp->pc0);
2586 dispatch(s, pos);
2587 loop_pop(s, val);
2588 }
2589 break;
2590
2591 case NODE_FOR:
2592 for_body(s, tree);
2593 if (val) push();
2594 break;
2595
2596 case NODE_CASE:
2597 {
2598 int head = 0;
2599 uint32_t pos1, pos2, pos3, tmp;
2600 node *n;
2601
2602 pos3 = JMPLINK_START;
2603 if (tree->car) {
2604 head = cursp();
2605 codegen(s, tree->car, VAL);
2606 }
2607 tree = tree->cdr;
2608 while (tree) {
2609 n = tree->car->car;
2610 pos1 = pos2 = JMPLINK_START;
2611 while (n) {
2612 codegen(s, n->car, VAL);
2613 if (head) {
2614 gen_move(s, cursp(), head, 0);
2615 push(); push(); pop(); pop(); pop();
2616 if (nint(n->car->car) == NODE_SPLAT) {
2617 genop_3(s, OP_SEND, cursp(), new_sym(s, MRB_SYM_2(s->mrb, __case_eqq)), 1);
2618 }
2619 else {
2620 genop_3(s, OP_SEND, cursp(), new_sym(s, MRB_OPSYM_2(s->mrb, eqq)), 1);
2621 }
2622 }
2623 else {
2624 pop();
2625 }
2626 tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, !head);
2627 pos2 = tmp;
2628 n = n->cdr;
2629 }
2630 if (tree->car->car) {
2631 pos1 = genjmp_0(s, OP_JMP);
2632 dispatch_linked(s, pos2);
2633 }
2634 codegen(s, tree->car->cdr, val);
2635 if (val) pop();
2636 tmp = genjmp(s, OP_JMP, pos3);
2637 pos3 = tmp;
2638 dispatch(s, pos1);
2639 tree = tree->cdr;
2640 }
2641 if (val) {
2642 uint32_t pos = cursp();
2643 genop_1(s, OP_LOADNIL, cursp());
2644 if (pos3 != JMPLINK_START) dispatch_linked(s, pos3);
2645 if (head) pop();
2646 if (cursp() != pos) {
2647 gen_move(s, cursp(), pos, 0);
2648 }
2649 push();
2650 }
2651 else {
2652 if (pos3 != JMPLINK_START) {
2653 dispatch_linked(s, pos3);
2654 }
2655 if (head) {
2656 pop();
2657 }
2658 }
2659 }
2660 break;
2661
2662 case NODE_SCOPE:
2663 scope_body(s, tree, NOVAL);
2664 break;
2665
2666 case NODE_FCALL:
2667 case NODE_CALL:
2668 gen_call(s, tree, val, 0);
2669 break;
2670 case NODE_SCALL:
2671 gen_call(s, tree, val, 1);
2672 break;
2673
2674 case NODE_DOT2:
2675 codegen(s, tree->car, val);
2676 codegen(s, tree->cdr, val);
2677 if (val) {
2678 pop(); pop();
2679 genop_1(s, OP_RANGE_INC, cursp());
2680 push();
2681 }
2682 break;
2683
2684 case NODE_DOT3:
2685 codegen(s, tree->car, val);
2686 codegen(s, tree->cdr, val);
2687 if (val) {
2688 pop(); pop();
2689 genop_1(s, OP_RANGE_EXC, cursp());
2690 push();
2691 }
2692 break;
2693
2694 case NODE_COLON2:
2695 {
2696 int sym = new_sym(s, nsym(tree->cdr));
2697
2698 codegen(s, tree->car, VAL);
2699 pop();
2700 genop_2(s, OP_GETMCNST, cursp(), sym);
2701 if (val) push();
2702 }
2703 break;
2704
2705 case NODE_COLON3:
2706 {
2707 int sym = new_sym(s, nsym(tree));
2708
2709 genop_1(s, OP_OCLASS, cursp());
2710 genop_2(s, OP_GETMCNST, cursp(), sym);
2711 if (val) push();
2712 }
2713 break;
2714
2715 case NODE_ARRAY:
2716 {
2717 int n;
2718
2719 n = gen_values(s, tree, val, 0);
2720 if (val) {
2721 if (n >= 0) {
2722 pop_n(n);
2723 genop_2(s, OP_ARRAY, cursp(), n);
2724 }
2725 push();
2726 }
2727 }
2728 break;
2729
2730 case NODE_HASH:
2731 case NODE_KW_HASH:
2732 {
2733 int nk = gen_hash(s, tree, val, GEN_LIT_ARY_MAX);
2734 if (val && nk >= 0) {
2735 pop_n(nk*2);
2736 genop_2(s, OP_HASH, cursp(), nk);
2737 push();
2738 }
2739 }
2740 break;
2741
2742 case NODE_SPLAT:
2743 codegen(s, tree, val);
2744 break;
2745
2746 case NODE_ASGN:
2747 gen_assignment(s, tree->car, tree->cdr, 0, val);
2748 break;
2749
2750 case NODE_MASGN:
2751 {
2752 int len = 0, n = 0, post = 0;
2753 node *t = tree->cdr, *p;
2754 int rhs = cursp();
2755
2756 if (!val && nint(t->car) == NODE_ARRAY && t->cdr && nosplat(t->cdr)) {
2757 /* fixed rhs */
2758 t = t->cdr;
2759 while (t) {
2760 codegen(s, t->car, VAL);
2761 len++;
2762 t = t->cdr;
2763 }
2764 tree = tree->car;
2765 if (tree->car) { /* pre */
2766 t = tree->car;
2767 n = 0;
2768 while (t) {
2769 if (n < len) {
2770 gen_assignment(s, t->car, NULL, rhs+n, NOVAL);
2771 n++;
2772 }
2773 else {
2774 genop_1(s, OP_LOADNIL, rhs+n);
2775 gen_assignment(s, t->car, NULL, rhs+n, NOVAL);
2776 }
2777 t = t->cdr;
2778 }
2779 }
2780 t = tree->cdr;
2781 if (t) {
2782 if (t->cdr) { /* post count */
2783 p = t->cdr->car;
2784 while (p) {
2785 post++;
2786 p = p->cdr;
2787 }
2788 }
2789 if (t->car) { /* rest (len - pre - post) */
2790 int rn;
2791
2792 if (len < post + n) {
2793 rn = 0;
2794 }
2795 else {
2796 rn = len - post - n;
2797 }
2798 if (cursp() == rhs+n) {
2799 genop_2(s, OP_ARRAY, cursp(), rn);
2800 }
2801 else {
2802 genop_3(s, OP_ARRAY2, cursp(), rhs+n, rn);
2803 }
2804 gen_assignment(s, t->car, NULL, cursp(), NOVAL);
2805 n += rn;
2806 }
2807 if (t->cdr && t->cdr->car) {
2808 t = t->cdr->car;
2809 while (t) {
2810 if (n<len) {
2811 gen_assignment(s, t->car, NULL, rhs+n, NOVAL);
2812 }
2813 else {
2814 genop_1(s, OP_LOADNIL, cursp());
2815 gen_assignment(s, t->car, NULL, cursp(), NOVAL);
2816 }
2817 t = t->cdr;
2818 n++;
2819 }
2820 }
2821 }
2822 pop_n(len);
2823 }
2824 else {
2825 /* variable rhs */
2826 codegen(s, t, VAL);
2827 gen_massignment(s, tree->car, rhs, val);
2828 if (!val) {
2829 pop();
2830 }
2831 }
2832 }
2833 break;
2834
2835 case NODE_OP_ASGN:
2836 {
2837 mrb_sym sym = nsym(tree->cdr->car);
2838 mrb_int len;
2839 const char *name = mrb_sym_name_len(s->mrb, sym, &len);
2840 int idx, callargs = -1, vsp = -1;
2841
2842 if ((len == 2 && name[0] == '|' && name[1] == '|') &&
2843 (nint(tree->car->car) == NODE_CONST ||
2844 nint(tree->car->car) == NODE_CVAR)) {
2845 int catch_entry, begin, end;
2846 int noexc, exc;
2847 struct loopinfo *lp;
2848
2849 lp = loop_push(s, LOOP_BEGIN);
2850 lp->pc0 = new_label(s);
2851 catch_entry = catch_handler_new(s);
2852 begin = s->pc;
2853 exc = cursp();
2854 codegen(s, tree->car, VAL);
2855 end = s->pc;
2856 noexc = genjmp_0(s, OP_JMP);
2857 lp->type = LOOP_RESCUE;
2858 catch_handler_set(s, catch_entry, MRB_CATCH_RESCUE, begin, end, s->pc);
2859 genop_1(s, OP_EXCEPT, exc);
2860 genop_1(s, OP_LOADF, exc);
2861 dispatch(s, noexc);
2862 loop_pop(s, NOVAL);
2863 }
2864 else if (nint(tree->car->car) == NODE_CALL) {
2865 node *n = tree->car->cdr;
2866 int base, i, nargs = 0;
2867 callargs = 0;
2868
2869 if (val) {
2870 vsp = cursp();
2871 push();
2872 }
2873 codegen(s, n->car, VAL); /* receiver */
2874 idx = new_sym(s, nsym(n->cdr->car));
2875 base = cursp()-1;
2876 if (n->cdr->cdr->car) {
2877 nargs = gen_values(s, n->cdr->cdr->car->car, VAL, 13);
2878 if (nargs >= 0) {
2879 callargs = nargs;
2880 }
2881 else { /* varargs */
2882 push();
2883 nargs = 1;
2884 callargs = CALL_MAXARGS;
2885 }
2886 }
2887 /* copy receiver and arguments */
2888 gen_move(s, cursp(), base, 1);
2889 for (i=0; i<nargs; i++) {
2890 gen_move(s, cursp()+i+1, base+i+1, 1);
2891 }
2892 push_n(nargs+2);pop_n(nargs+2); /* space for receiver, arguments and a block */
2893 genop_3(s, OP_SEND, cursp(), idx, callargs);
2894 push();
2895 }
2896 else {
2897 codegen(s, tree->car, VAL);
2898 }
2899 if (len == 2 &&
2900 ((name[0] == '|' && name[1] == '|') ||
2901 (name[0] == '&' && name[1] == '&'))) {
2902 uint32_t pos;
2903
2904 pop();
2905 if (val) {
2906 if (vsp >= 0) {
2907 gen_move(s, vsp, cursp(), 1);
2908 }
2909 pos = genjmp2_0(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), val);
2910 }
2911 else {
2912 pos = genjmp2_0(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), val);
2913 }
2914 codegen(s, tree->cdr->cdr->car, VAL);
2915 pop();
2916 if (val && vsp >= 0) {
2917 gen_move(s, vsp, cursp(), 1);
2918 }
2919 if (nint(tree->car->car) == NODE_CALL) {
2920 if (callargs == CALL_MAXARGS) {
2921 pop();
2922 genop_2(s, OP_ARYPUSH, cursp(), 1);
2923 }
2924 else {
2925 pop_n(callargs);
2926 callargs++;
2927 }
2928 pop();
2929 idx = new_sym(s, attrsym(s, nsym(tree->car->cdr->cdr->car)));
2930 genop_3(s, OP_SEND, cursp(), idx, callargs);
2931 }
2932 else {
2933 gen_assignment(s, tree->car, NULL, cursp(), val);
2934 }
2935 dispatch(s, pos);
2936 goto exit;
2937 }
2938 codegen(s, tree->cdr->cdr->car, VAL);
2939 push(); pop();
2940 pop(); pop();
2941
2942 if (len == 1 && name[0] == '+') {
2943 gen_addsub(s, OP_ADD, cursp());
2944 }
2945 else if (len == 1 && name[0] == '-') {
2946 gen_addsub(s, OP_SUB, cursp());
2947 }
2948 else if (len == 1 && name[0] == '*') {
2949 genop_1(s, OP_MUL, cursp());
2950 }
2951 else if (len == 1 && name[0] == '/') {
2952 genop_1(s, OP_DIV, cursp());
2953 }
2954 else if (len == 1 && name[0] == '<') {
2955 genop_1(s, OP_LT, cursp());
2956 }
2957 else if (len == 2 && name[0] == '<' && name[1] == '=') {
2958 genop_1(s, OP_LE, cursp());
2959 }
2960 else if (len == 1 && name[0] == '>') {
2961 genop_1(s, OP_GT, cursp());
2962 }
2963 else if (len == 2 && name[0] == '>' && name[1] == '=') {
2964 genop_1(s, OP_GE, cursp());
2965 }
2966 else {
2967 idx = new_sym(s, sym);
2968 genop_3(s, OP_SEND, cursp(), idx, 1);
2969 }
2970 if (callargs < 0) {
2971 gen_assignment(s, tree->car, NULL, cursp(), val);
2972 }
2973 else {
2974 if (val && vsp >= 0) {
2975 gen_move(s, vsp, cursp(), 0);
2976 }
2977 if (callargs == CALL_MAXARGS) {
2978 pop();
2979 genop_2(s, OP_ARYPUSH, cursp(), 1);
2980 }
2981 else {
2982 pop_n(callargs);
2983 callargs++;
2984 }
2985 pop();
2986 idx = new_sym(s, attrsym(s,nsym(tree->car->cdr->cdr->car)));
2987 genop_3(s, OP_SEND, cursp(), idx, callargs);
2988 }
2989 }
2990 break;
2991
2992 case NODE_SUPER:
2993 {
2994 codegen_scope *s2 = s;
2995 int lv = 0;
2996 int n = 0, nk = 0, st = 0;
2997
2998 push();
2999 while (!s2->mscope) {
3000 lv++;
3001 s2 = s2->prev;
3002 if (!s2) break;
3003 }
3004 if (tree) {
3005 node *args = tree->car;
3006 if (args) {
3007 st = n = gen_values(s, args, VAL, 14);
3008 if (n < 0) {
3009 st = 1; n = 15;
3010 push();
3011 }
3012 }
3013 /* keyword arguments */
3014 if (tree->cdr->car) {
3015 nk = gen_hash(s, tree->cdr->car->cdr, VAL, 14);
3016 if (nk < 0) {st++; nk = 15;}
3017 else st += nk*2;
3018 n |= nk<<4;
3019 }
3020 /* block arguments */
3021 if (tree->cdr->cdr) {
3022 codegen(s, tree->cdr->cdr, VAL);
3023 }
3024 else if (s2) gen_blkmove(s, s2->ainfo, lv);
3025 else {
3026 genop_1(s, OP_LOADNIL, cursp());
3027 push();
3028 }
3029 }
3030 else {
3031 if (s2) gen_blkmove(s, s2->ainfo, lv);
3032 else {
3033 genop_1(s, OP_LOADNIL, cursp());
3034 push();
3035 }
3036 }
3037 st++;
3038 pop_n(st+1);
3039 genop_2(s, OP_SUPER, cursp(), n);
3040 if (val) push();
3041 }
3042 break;
3043
3044 case NODE_ZSUPER:
3045 {
3046 codegen_scope *s2 = s;
3047 int lv = 0;
3048 uint16_t ainfo = 0;
3049 int n = CALL_MAXARGS;
3050 int sp = cursp();
3051
3052 push(); /* room for receiver */
3053 while (!s2->mscope) {
3054 lv++;
3055 s2 = s2->prev;
3056 if (!s2) break;
3057 }
3058 if (s2 && s2->ainfo > 0) {
3059 ainfo = s2->ainfo;
3060 }
3061 if (ainfo > 0) {
3062 genop_2S(s, OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf));
3063 push(); push(); push(); /* ARGARY pushes 3 values at most */
3064 pop(); pop(); pop();
3065 /* keyword arguments */
3066 if (ainfo & 0x1) {
3067 n |= CALL_MAXARGS<<4;
3068 push();
3069 }
3070 /* block argument */
3071 if (tree && tree->cdr && tree->cdr->cdr) {
3072 push();
3073 codegen(s, tree->cdr->cdr, VAL);
3074 }
3075 }
3076 else {
3077 /* block argument */
3078 if (tree && tree->cdr && tree->cdr->cdr) {
3079 codegen(s, tree->cdr->cdr, VAL);
3080 }
3081 else {
3082 gen_blkmove(s, 0, lv);
3083 }
3084 n = 0;
3085 }
3086 s->sp = sp;
3087 genop_2(s, OP_SUPER, cursp(), n);
3088 if (val) push();
3089 }
3090 break;
3091
3092 case NODE_RETURN:
3093 if (tree) {
3094 gen_retval(s, tree);
3095 }
3096 else {
3097 genop_1(s, OP_LOADNIL, cursp());
3098 }
3099 if (s->loop) {
3100 gen_return(s, OP_RETURN_BLK, cursp());
3101 }
3102 else {
3103 gen_return(s, OP_RETURN, cursp());
3104 }
3105 if (val) push();
3106 break;
3107
3108 case NODE_YIELD:
3109 {
3110 codegen_scope *s2 = s;
3111 int lv = 0, ainfo = -1;
3112 int n = 0, sendv = 0;
3113
3114 while (!s2->mscope) {
3115 lv++;
3116 s2 = s2->prev;
3117 if (!s2) break;
3118 }
3119 if (s2) {
3120 ainfo = (int)s2->ainfo;
3121 }
3122 if (ainfo < 0) codegen_error(s, "invalid yield (SyntaxError)");
3123 push();
3124 if (tree) {
3125 n = gen_values(s, tree, VAL, 14);
3126 if (n < 0) {
3127 n = sendv = 1;
3128 push();
3129 }
3130 }
3131 push();pop(); /* space for a block */
3132 pop_n(n+1);
3133 genop_2S(s, OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf));
3134 if (sendv) n = CALL_MAXARGS;
3135 genop_3(s, OP_SEND, cursp(), new_sym(s, MRB_SYM_2(s->mrb, call)), n);
3136 if (val) push();
3137 }
3138 break;
3139
3140 case NODE_BREAK:
3141 loop_break(s, tree);
3142 if (val) push();
3143 break;
3144
3145 case NODE_NEXT:
3146 if (!s->loop) {
3147 raise_error(s, "unexpected next");
3148 }
3149 else if (s->loop->type == LOOP_NORMAL) {
3150 codegen(s, tree, NOVAL);
3151 genjmp(s, OP_JMPUW, s->loop->pc0);
3152 }
3153 else {
3154 if (tree) {
3155 codegen(s, tree, VAL);
3156 pop();
3157 }
3158 else {
3159 genop_1(s, OP_LOADNIL, cursp());
3160 }
3161 gen_return(s, OP_RETURN, cursp());
3162 }
3163 if (val) push();
3164 break;
3165
3166 case NODE_REDO:
3167 if (!s->loop || s->loop->type == LOOP_BEGIN || s->loop->type == LOOP_RESCUE) {
3168 raise_error(s, "unexpected redo");
3169 }
3170 else {
3171 genjmp(s, OP_JMPUW, s->loop->pc1);
3172 }
3173 if (val) push();
3174 break;
3175
3176 case NODE_RETRY:
3177 {
3178 const char *msg = "unexpected retry";
3179 const struct loopinfo *lp = s->loop;
3180
3181 while (lp && lp->type != LOOP_RESCUE) {
3182 lp = lp->prev;
3183 }
3184 if (!lp) {
3185 raise_error(s, msg);
3186 }
3187 else {
3188 genjmp(s, OP_JMPUW, lp->pc0);
3189 }
3190 if (val) push();
3191 }
3192 break;
3193
3194 case NODE_LVAR:
3195 if (val) {
3196 int idx = lv_idx(s, nsym(tree));
3197
3198 if (idx > 0) {
3199 gen_move(s, cursp(), idx, val);
3200 }
3201 else {
3202 gen_getupvar(s, cursp(), nsym(tree));
3203 }
3204 push();
3205 }
3206 break;
3207
3208 case NODE_NVAR:
3209 if (val) {
3210 int idx = nint(tree);
3211
3212 gen_move(s, cursp(), idx, val);
3213
3214 push();
3215 }
3216 break;
3217
3218 case NODE_GVAR:
3219 {
3220 int sym = new_sym(s, nsym(tree));
3221
3222 genop_2(s, OP_GETGV, cursp(), sym);
3223 if (val) push();
3224 }
3225 break;
3226
3227 case NODE_IVAR:
3228 {
3229 int sym = new_sym(s, nsym(tree));
3230
3231 genop_2(s, OP_GETIV, cursp(), sym);
3232 if (val) push();
3233 }
3234 break;
3235
3236 case NODE_CVAR:
3237 {
3238 int sym = new_sym(s, nsym(tree));
3239
3240 genop_2(s, OP_GETCV, cursp(), sym);
3241 if (val) push();
3242 }
3243 break;
3244
3245 case NODE_CONST:
3246 {
3247 int sym = new_sym(s, nsym(tree));
3248
3249 genop_2(s, OP_GETCONST, cursp(), sym);
3250 if (val) push();
3251 }
3252 break;
3253
3254 case NODE_BACK_REF:
3255 if (val) {
3256 char buf[] = {'$', nchar(tree)};
3257 int sym = new_sym(s, mrb_intern(s->mrb, buf, sizeof(buf)));
3258
3259 genop_2(s, OP_GETGV, cursp(), sym);
3260 push();
3261 }
3262 break;
3263
3264 case NODE_NTH_REF:
3265 if (val) {
3266 mrb_state *mrb = s->mrb;
3267 mrb_value str;
3268 int sym;
3269
3270 str = mrb_format(mrb, "$%d", nint(tree));
3271 sym = new_sym(s, mrb_intern_str(mrb, str));
3272 genop_2(s, OP_GETGV, cursp(), sym);
3273 push();
3274 }
3275 break;
3276
3277 case NODE_ARG:
3278 /* should not happen */
3279 break;
3280
3281 case NODE_BLOCK_ARG:
3282 if (!tree) {
3283 int idx = lv_idx(s, MRB_OPSYM_2(s->mrb, and));
3284
3285 if (idx == 0) {
3286 gen_getupvar(s, cursp(), MRB_OPSYM_2(s->mrb, and));
3287 }
3288 else {
3289 gen_move(s, cursp(), idx, val);
3290 }
3291 if (val) push();
3292 }
3293 else {
3294 codegen(s, tree, val);
3295 }
3296 break;
3297
3298 case NODE_INT:
3299 if (val) {
3300 char *p = (char*)tree->car;
3301 int base = nint(tree->cdr->car);
3302 mrb_int i;
3303 mrb_bool overflow;
3304
3305 i = readint(s, p, base, FALSE, &overflow);
3306 if (overflow) {
3307 int off = new_litbint(s, p, base, FALSE);
3308 genop_2(s, OP_LOADL, cursp(), off);
3309 }
3310 else {
3311 gen_int(s, cursp(), i);
3312 }
3313 push();
3314 }
3315 break;
3316
3317 #ifndef MRB_NO_FLOAT
3318 case NODE_FLOAT:
3319 if (val) {
3320 char *p = (char*)tree;
3321 double f;
3322 mrb_read_float(p, NULL, &f);
3323 int off = new_lit_float(s, (mrb_float)f);
3324
3325 genop_2(s, OP_LOADL, cursp(), off);
3326 push();
3327 }
3328 break;
3329 #endif
3330
3331 case NODE_NEGATE:
3332 {
3333 nt = nint(tree->car);
3334 switch (nt) {
3335 #ifndef MRB_NO_FLOAT
3336 case NODE_FLOAT:
3337 if (val) {
3338 char *p = (char*)tree->cdr;
3339 double f;
3340 mrb_read_float(p, NULL, &f);
3341 int off = new_lit_float(s, (mrb_float)-f);
3342
3343 genop_2(s, OP_LOADL, cursp(), off);
3344 push();
3345 }
3346 break;
3347 #endif
3348
3349 case NODE_INT:
3350 if (val) {
3351 char *p = (char*)tree->cdr->car;
3352 int base = nint(tree->cdr->cdr->car);
3353 mrb_int i;
3354 mrb_bool overflow;
3355
3356 i = readint(s, p, base, TRUE, &overflow);
3357 if (overflow) {
3358 int off = new_litbint(s, p, base, TRUE);
3359 genop_2(s, OP_LOADL, cursp(), off);
3360 }
3361 else {
3362 gen_int(s, cursp(), i);
3363 }
3364 push();
3365 }
3366 break;
3367
3368 default:
3369 if (val) {
3370 codegen(s, tree, VAL);
3371 pop();
3372 push_n(2);pop_n(2); /* space for receiver&block */
3373 mrb_sym minus = MRB_OPSYM_2(s->mrb, minus);
3374 if (!gen_uniop(s, minus, cursp())) {
3375 genop_3(s, OP_SEND, cursp(), new_sym(s, minus), 0);
3376 }
3377 push();
3378 }
3379 else {
3380 codegen(s, tree, NOVAL);
3381 }
3382 break;
3383 }
3384 }
3385 break;
3386
3387 case NODE_STR:
3388 if (val) {
3389 char *p = (char*)tree->car;
3390 mrb_int len = nint(tree->cdr);
3391 int off = new_lit_str(s, p, len);
3392
3393 genop_2(s, OP_STRING, cursp(), off);
3394 push();
3395 }
3396 break;
3397
3398 case NODE_HEREDOC:
3399 tree = ((struct mrb_parser_heredoc_info*)tree)->doc;
3400 /* fall through */
3401 case NODE_DSTR:
3402 if (val) {
3403 node *n = tree;
3404
3405 if (!n) {
3406 genop_1(s, OP_LOADNIL, cursp());
3407 push();
3408 break;
3409 }
3410 codegen(s, n->car, VAL);
3411 n = n->cdr;
3412 while (n) {
3413 codegen(s, n->car, VAL);
3414 pop(); pop();
3415 genop_1(s, OP_STRCAT, cursp());
3416 push();
3417 n = n->cdr;
3418 }
3419 }
3420 else {
3421 node *n = tree;
3422
3423 while (n) {
3424 if (nint(n->car->car) != NODE_STR) {
3425 codegen(s, n->car, NOVAL);
3426 }
3427 n = n->cdr;
3428 }
3429 }
3430 break;
3431
3432 case NODE_WORDS:
3433 gen_literal_array(s, tree, FALSE, val);
3434 break;
3435
3436 case NODE_SYMBOLS:
3437 gen_literal_array(s, tree, TRUE, val);
3438 break;
3439
3440 case NODE_DXSTR:
3441 {
3442 node *n;
3443 int sym = new_sym(s, MRB_SYM_2(s->mrb, Kernel));
3444
3445 genop_1(s, OP_LOADSELF, cursp());
3446 push();
3447 codegen(s, tree->car, VAL);
3448 n = tree->cdr;
3449 while (n) {
3450 if (nint(n->car->car) == NODE_XSTR) {
3451 n->car->car = (struct mrb_ast_node*)(intptr_t)NODE_STR;
3452 mrb_assert(!n->cdr); /* must be the end */
3453 }
3454 codegen(s, n->car, VAL);
3455 pop(); pop();
3456 genop_1(s, OP_STRCAT, cursp());
3457 push();
3458 n = n->cdr;
3459 }
3460 push(); /* for block */
3461 pop_n(3);
3462 sym = new_sym(s, MRB_OPSYM_2(s->mrb, tick)); /* ` */
3463 genop_3(s, OP_SEND, cursp(), sym, 1);
3464 if (val) push();
3465 }
3466 break;
3467
3468 case NODE_XSTR:
3469 {
3470 char *p = (char*)tree->car;
3471 mrb_int len = nint(tree->cdr);
3472 int off = new_lit_str(s, p, len);
3473 int sym;
3474
3475 genop_1(s, OP_LOADSELF, cursp());
3476 push();
3477 genop_2(s, OP_STRING, cursp(), off);
3478 push(); push();
3479 pop_n(3);
3480 sym = new_sym(s, MRB_OPSYM_2(s->mrb, tick)); /* ` */
3481 genop_3(s, OP_SEND, cursp(), sym, 1);
3482 if (val) push();
3483 }
3484 break;
3485
3486 case NODE_REGX:
3487 if (val) {
3488 char *p1 = (char*)tree->car;
3489 char *p2 = (char*)tree->cdr->car;
3490 char *p3 = (char*)tree->cdr->cdr;
3491 int sym = new_sym(s, mrb_intern_lit(s->mrb, REGEXP_CLASS));
3492 int off = new_lit_cstr(s, p1);
3493 int argc = 1;
3494
3495 genop_1(s, OP_OCLASS, cursp());
3496 genop_2(s, OP_GETMCNST, cursp(), sym);
3497 push();
3498 genop_2(s, OP_STRING, cursp(), off);
3499 push();
3500 if (p2 || p3) {
3501 if (p2) { /* opt */
3502 off = new_lit_cstr(s, p2);
3503 genop_2(s, OP_STRING, cursp(), off);
3504 }
3505 else {
3506 genop_1(s, OP_LOADNIL, cursp());
3507 }
3508 push();
3509 argc++;
3510 if (p3) { /* enc */
3511 off = new_lit_str(s, p3, 1);
3512 genop_2(s, OP_STRING, cursp(), off);
3513 push();
3514 argc++;
3515 }
3516 }
3517 push(); /* space for a block */
3518 pop_n(argc+2);
3519 sym = new_sym(s, MRB_SYM_2(s->mrb, compile));
3520 genop_3(s, OP_SEND, cursp(), sym, argc);
3521 push();
3522 }
3523 break;
3524
3525 case NODE_DREGX:
3526 if (val) {
3527 node *n = tree->car;
3528 int sym = new_sym(s, mrb_intern_lit(s->mrb, REGEXP_CLASS));
3529 int argc = 1;
3530 int off;
3531 char *p;
3532
3533 genop_1(s, OP_OCLASS, cursp());
3534 genop_2(s, OP_GETMCNST, cursp(), sym);
3535 push();
3536 codegen(s, n->car, VAL);
3537 n = n->cdr;
3538 while (n) {
3539 codegen(s, n->car, VAL);
3540 pop(); pop();
3541 genop_1(s, OP_STRCAT, cursp());
3542 push();
3543 n = n->cdr;
3544 }
3545 n = tree->cdr->cdr;
3546 if (n->car) { /* tail */
3547 p = (char*)n->car;
3548 off = new_lit_cstr(s, p);
3549 codegen(s, tree->car, VAL);
3550 genop_2(s, OP_STRING, cursp(), off);
3551 pop();
3552 genop_1(s, OP_STRCAT, cursp());
3553 push();
3554 }
3555 if (n->cdr->car) { /* opt */
3556 char *p2 = (char*)n->cdr->car;
3557 off = new_lit_cstr(s, p2);
3558 genop_2(s, OP_STRING, cursp(), off);
3559 push();
3560 argc++;
3561 }
3562 if (n->cdr->cdr) { /* enc */
3563 char *p2 = (char*)n->cdr->cdr;
3564 off = new_lit_cstr(s, p2);
3565 genop_2(s, OP_STRING, cursp(), off);
3566 push();
3567 argc++;
3568 }
3569 push(); /* space for a block */
3570 pop_n(argc+2);
3571 sym = new_sym(s, MRB_SYM_2(s->mrb, compile));
3572 genop_3(s, OP_SEND, cursp(), sym, argc);
3573 push();
3574 }
3575 else {
3576 node *n = tree->car;
3577
3578 while (n) {
3579 if (nint(n->car->car) != NODE_STR) {
3580 codegen(s, n->car, NOVAL);
3581 }
3582 n = n->cdr;
3583 }
3584 }
3585 break;
3586
3587 case NODE_SYM:
3588 if (val) {
3589 int sym = new_sym(s, nsym(tree));
3590
3591 genop_2(s, OP_LOADSYM, cursp(), sym);
3592 push();
3593 }
3594 break;
3595
3596 case NODE_DSYM:
3597 codegen(s, tree, val);
3598 if (val) {
3599 gen_intern(s);
3600 }
3601 break;
3602
3603 case NODE_SELF:
3604 if (val) {
3605 genop_1(s, OP_LOADSELF, cursp());
3606 push();
3607 }
3608 break;
3609
3610 case NODE_NIL:
3611 if (val) {
3612 genop_1(s, OP_LOADNIL, cursp());
3613 push();
3614 }
3615 break;
3616
3617 case NODE_TRUE:
3618 if (val) {
3619 genop_1(s, OP_LOADT, cursp());
3620 push();
3621 }
3622 break;
3623
3624 case NODE_FALSE:
3625 if (val) {
3626 genop_1(s, OP_LOADF, cursp());
3627 push();
3628 }
3629 break;
3630
3631 case NODE_ALIAS:
3632 {
3633 int a = new_sym(s, nsym(tree->car));
3634 int b = new_sym(s, nsym(tree->cdr));
3635
3636 genop_2(s, OP_ALIAS, a, b);
3637 if (val) {
3638 genop_1(s, OP_LOADNIL, cursp());
3639 push();
3640 }
3641 }
3642 break;
3643
3644 case NODE_UNDEF:
3645 {
3646 node *t = tree;
3647
3648 while (t) {
3649 int symbol = new_sym(s, nsym(t->car));
3650 genop_1(s, OP_UNDEF, symbol);
3651 t = t->cdr;
3652 }
3653 if (val) {
3654 genop_1(s, OP_LOADNIL, cursp());
3655 push();
3656 }
3657 }
3658 break;
3659
3660 case NODE_CLASS:
3661 {
3662 int idx;
3663 node *body;
3664
3665 if (tree->car->car == (node*)0) {
3666 genop_1(s, OP_LOADNIL, cursp());
3667 push();
3668 }
3669 else if (tree->car->car == (node*)1) {
3670 genop_1(s, OP_OCLASS, cursp());
3671 push();
3672 }
3673 else {
3674 codegen(s, tree->car->car, VAL);
3675 }
3676 if (tree->cdr->car) {
3677 codegen(s, tree->cdr->car, VAL);
3678 }
3679 else {
3680 genop_1(s, OP_LOADNIL, cursp());
3681 push();
3682 }
3683 pop(); pop();
3684 idx = new_sym(s, nsym(tree->car->cdr));
3685 genop_2(s, OP_CLASS, cursp(), idx);
3686 body = tree->cdr->cdr->car;
3687 if (nint(body->cdr->car) == NODE_BEGIN && body->cdr->cdr == NULL) {
3688 genop_1(s, OP_LOADNIL, cursp());
3689 }
3690 else {
3691 idx = scope_body(s, body, val);
3692 genop_2(s, OP_EXEC, cursp(), idx);
3693 }
3694 if (val) {
3695 push();
3696 }
3697 }
3698 break;
3699
3700 case NODE_MODULE:
3701 {
3702 int idx;
3703
3704 if (tree->car->car == (node*)0) {
3705 genop_1(s, OP_LOADNIL, cursp());
3706 push();
3707 }
3708 else if (tree->car->car == (node*)1) {
3709 genop_1(s, OP_OCLASS, cursp());
3710 push();
3711 }
3712 else {
3713 codegen(s, tree->car->car, VAL);
3714 }
3715 pop();
3716 idx = new_sym(s, nsym(tree->car->cdr));
3717 genop_2(s, OP_MODULE, cursp(), idx);
3718 if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN &&
3719 tree->cdr->car->cdr->cdr == NULL) {
3720 genop_1(s, OP_LOADNIL, cursp());
3721 }
3722 else {
3723 idx = scope_body(s, tree->cdr->car, val);
3724 genop_2(s, OP_EXEC, cursp(), idx);
3725 }
3726 if (val) {
3727 push();
3728 }
3729 }
3730 break;
3731
3732 case NODE_SCLASS:
3733 {
3734 int idx;
3735
3736 codegen(s, tree->car, VAL);
3737 pop();
3738 genop_1(s, OP_SCLASS, cursp());
3739 if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN &&
3740 tree->cdr->car->cdr->cdr == NULL) {
3741 genop_1(s, OP_LOADNIL, cursp());
3742 }
3743 else {
3744 idx = scope_body(s, tree->cdr->car, val);
3745 genop_2(s, OP_EXEC, cursp(), idx);
3746 }
3747 if (val) {
3748 push();
3749 }
3750 }
3751 break;
3752
3753 case NODE_DEF:
3754 {
3755 int sym = new_sym(s, nsym(tree->car));
3756 int idx = lambda_body(s, tree->cdr, 0);
3757
3758 genop_1(s, OP_TCLASS, cursp());
3759 push();
3760 genop_2(s, OP_METHOD, cursp(), idx);
3761 push(); pop();
3762 pop();
3763 genop_2(s, OP_DEF, cursp(), sym);
3764 if (val) push();
3765 }
3766 break;
3767
3768 case NODE_SDEF:
3769 {
3770 node *recv = tree->car;
3771 int sym = new_sym(s, nsym(tree->cdr->car));
3772 int idx = lambda_body(s, tree->cdr->cdr, 0);
3773
3774 codegen(s, recv, VAL);
3775 pop();
3776 genop_1(s, OP_SCLASS, cursp());
3777 push();
3778 genop_2(s, OP_METHOD, cursp(), idx);
3779 push(); pop();
3780 pop();
3781 genop_2(s, OP_DEF, cursp(), sym);
3782 if (val) push();
3783 }
3784 break;
3785
3786 case NODE_POSTEXE:
3787 codegen(s, tree, NOVAL);
3788 break;
3789
3790 default:
3791 break;
3792 }
3793 exit:
3794 s->rlev = rlev;
3795 }
3796
3797 static void
3798 scope_add_irep(codegen_scope *s)
3799 {
3800 mrb_irep *irep;
3801 codegen_scope *prev = s->prev;
3802
3803 if (prev->irep == NULL) {
3804 irep = mrb_add_irep(s->mrb);
3805 prev->irep = s->irep = irep;
3806 return;
3807 }
3808 else {
3809 if (prev->irep->rlen == UINT16_MAX) {
3810 codegen_error(s, "too many nested blocks/methods");
3811 }
3812 s->irep = irep = mrb_add_irep(s->mrb);
3813 if (prev->irep->rlen == prev->rcapa) {
3814 prev->rcapa *= 2;
3815 prev->reps = (mrb_irep**)codegen_realloc(s, prev->reps, sizeof(mrb_irep*)*prev->rcapa);
3816 }
3817 prev->reps[prev->irep->rlen] = irep;
3818 prev->irep->rlen++;
3819 }
3820 }
3821
3822 static codegen_scope*
3823 scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
3824 {
3825 static const codegen_scope codegen_scope_zero = { 0 };
3826 mrb_pool *pool = mrb_pool_open(mrb);
3827 codegen_scope *s = (codegen_scope*)mrb_pool_alloc(pool, sizeof(codegen_scope));
3828
3829 if (!s) {
3830 if (prev)
3831 codegen_error(prev, "unexpected scope");
3832 return NULL;
3833 }
3834 *s = codegen_scope_zero;
3835 s->mrb = mrb;
3836 s->mpool = pool;
3837 if (!prev) return s;
3838 s->prev = prev;
3839 s->ainfo = 0;
3840 s->mscope = 0;
3841
3842 scope_add_irep(s);
3843
3844 s->rcapa = 8;
3845 s->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*s->rcapa);
3846
3847 s->icapa = 1024;
3848 s->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*s->icapa);
3849
3850 s->pcapa = 32;
3851 s->pool = (mrb_pool_value*)mrb_malloc(mrb, sizeof(mrb_pool_value)*s->pcapa);
3852
3853 s->scapa = 256;
3854 s->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*s->scapa);
3855
3856 s->lv = nlv;
3857 s->sp += node_len(nlv)+1; /* add self */
3858 s->nlocals = s->sp;
3859 if (nlv) {
3860 mrb_sym *lv;
3861 node *n = nlv;
3862 size_t i = 0;
3863
3864 s->irep->lv = lv = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*(s->nlocals-1));
3865 for (i=0, n=nlv; n; i++,n=n->cdr) {
3866 lv[i] = lv_name(n);
3867 }
3868 mrb_assert(i + 1 == s->nlocals);
3869 }
3870 s->ai = mrb_gc_arena_save(mrb);
3871
3872 s->filename_sym = prev->filename_sym;
3873 if (s->filename_sym) {
3874 s->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*s->icapa);
3875 }
3876 s->lineno = prev->lineno;
3877
3878 /* debug setting */
3879 s->debug_start_pos = 0;
3880 if (s->filename_sym) {
3881 mrb_debug_info_alloc(mrb, s->irep);
3882 }
3883 else {
3884 s->irep->debug_info = NULL;
3885 }
3886 s->parser = prev->parser;
3887 s->filename_index = prev->filename_index;
3888
3889 s->rlev = prev->rlev+1;
3890
3891 return s;
3892 }
3893
3894 static void
3895 scope_finish(codegen_scope *s)
3896 {
3897 mrb_state *mrb = s->mrb;
3898 mrb_irep *irep = s->irep;
3899
3900 if (s->nlocals > 0xff) {
3901 codegen_error(s, "too many local variables");
3902 }
3903 irep->flags = 0;
3904 if (s->iseq) {
3905 size_t catchsize = sizeof(struct mrb_irep_catch_handler) * irep->clen;
3906 irep->iseq = (const mrb_code*)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc + catchsize);
3907 irep->ilen = s->pc;
3908 if (irep->clen > 0) {
3909 memcpy((void*)(irep->iseq + irep->ilen), s->catch_table, catchsize);
3910 }
3911 }
3912 else {
3913 irep->clen = 0;
3914 }
3915 mrb_free(s->mrb, s->catch_table);
3916 s->catch_table = NULL;
3917 irep->pool = (const mrb_pool_value*)codegen_realloc(s, s->pool, sizeof(mrb_pool_value)*irep->plen);
3918 irep->syms = (const mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*irep->slen);
3919 irep->reps = (const mrb_irep**)codegen_realloc(s, s->reps, sizeof(mrb_irep*)*irep->rlen);
3920 if (s->filename_sym) {
3921 mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
3922 const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
3923
3924 mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
3925 filename, s->lines, s->debug_start_pos, s->pc);
3926 }
3927 mrb_free(s->mrb, s->lines);
3928
3929 irep->nlocals = s->nlocals;
3930 irep->nregs = s->nregs;
3931
3932 mrb_gc_arena_restore(mrb, s->ai);
3933 mrb_pool_close(s->mpool);
3934 }
3935
3936 static struct loopinfo*
3937 loop_push(codegen_scope *s, enum looptype t)
3938 {
3939 struct loopinfo *p = (struct loopinfo*)codegen_palloc(s, sizeof(struct loopinfo));
3940
3941 p->type = t;
3942 p->pc0 = p->pc1 = p->pc2 = JMPLINK_START;
3943 p->prev = s->loop;
3944 p->reg = cursp();
3945 s->loop = p;
3946
3947 return p;
3948 }
3949
3950 static void
3951 loop_break(codegen_scope *s, node *tree)
3952 {
3953 if (!s->loop) {
3954 codegen(s, tree, NOVAL);
3955 raise_error(s, "unexpected break");
3956 }
3957 else {
3958 struct loopinfo *loop;
3959
3960
3961 loop = s->loop;
3962 if (tree) {
3963 if (loop->reg < 0) {
3964 codegen(s, tree, NOVAL);
3965 }
3966 else {
3967 gen_retval(s, tree);
3968 }
3969 }
3970 while (loop) {
3971 if (loop->type == LOOP_BEGIN) {
3972 loop = loop->prev;
3973 }
3974 else if (loop->type == LOOP_RESCUE) {
3975 loop = loop->prev;
3976 }
3977 else{
3978 break;
3979 }
3980 }
3981 if (!loop) {
3982 raise_error(s, "unexpected break");
3983 return;
3984 }
3985
3986 if (loop->type == LOOP_NORMAL) {
3987 int tmp;
3988
3989 if (loop->reg >= 0) {
3990 if (tree) {
3991 gen_move(s, loop->reg, cursp(), 0);
3992 }
3993 else {
3994 genop_1(s, OP_LOADNIL, loop->reg);
3995 }
3996 }
3997 tmp = genjmp(s, OP_JMPUW, loop->pc2);
3998 loop->pc2 = tmp;
3999 }
4000 else {
4001 if (!tree) {
4002 genop_1(s, OP_LOADNIL, cursp());
4003 }
4004 gen_return(s, OP_BREAK, cursp());
4005 }
4006 }
4007 }
4008
4009 static void
4010 loop_pop(codegen_scope *s, int val)
4011 {
4012 if (val) {
4013 genop_1(s, OP_LOADNIL, cursp());
4014 }
4015 dispatch_linked(s, s->loop->pc2);
4016 s->loop = s->loop->prev;
4017 if (val) push();
4018 }
4019
4020 static int
4021 catch_handler_new(codegen_scope *s)
4022 {
4023 size_t newsize = sizeof(struct mrb_irep_catch_handler) * (s->irep->clen + 1);
4024 s->catch_table = (struct mrb_irep_catch_handler*)codegen_realloc(s, (void*)s->catch_table, newsize);
4025 return s->irep->clen++;
4026 }
4027
4028 static void
4029 catch_handler_set(codegen_scope *s, int ent, enum mrb_catch_type type, uint32_t begin, uint32_t end, uint32_t target)
4030 {
4031 struct mrb_irep_catch_handler *e;
4032
4033 mrb_assert(ent >= 0 && ent < s->irep->clen);
4034
4035 e = &s->catch_table[ent];
4036 uint8_to_bin(type, &e->type);
4037 mrb_irep_catch_handler_pack(begin, e->begin);
4038 mrb_irep_catch_handler_pack(end, e->end);
4039 mrb_irep_catch_handler_pack(target, e->target);
4040 }
4041
4042 static struct RProc*
4043 generate_code(mrb_state *mrb, parser_state *p, int val)
4044 {
4045 codegen_scope *scope = scope_new(mrb, 0, 0);
4046 struct mrb_jmpbuf *prev_jmp = mrb->jmp;
4047 struct mrb_jmpbuf jmpbuf;
4048 struct RProc *proc;
4049
4050 mrb->jmp = &jmpbuf;
4051
4052 scope->mrb = mrb;
4053 scope->parser = p;
4054 scope->filename_sym = p->filename_sym;
4055 scope->filename_index = p->current_filename_index;
4056
4057 MRB_TRY(mrb->jmp) {
4058 /* prepare irep */
4059 codegen(scope, p->tree, val);
4060 proc = mrb_proc_new(mrb, scope->irep);
4061 mrb_irep_decref(mrb, scope->irep);
4062 mrb_pool_close(scope->mpool);
4063 proc->c = NULL;
4064 if (mrb->c->cibase && mrb->c->cibase->proc == proc->upper) {
4065 proc->upper = NULL;
4066 }
4067 mrb->jmp = prev_jmp;
4068 return proc;
4069 }
4070 MRB_CATCH(mrb->jmp) {
4071 mrb_irep_decref(mrb, scope->irep);
4072 mrb_pool_close(scope->mpool);
4073 mrb->jmp = prev_jmp;
4074 return NULL;
4075 }
4076 MRB_END_EXC(mrb->jmp);
4077 }
4078
4079 MRB_API struct RProc*
4080 mrb_generate_code(mrb_state *mrb, parser_state *p)
4081 {
4082 return generate_code(mrb, p, VAL);
4083 }
4084
4085 void
4086 mrb_irep_remove_lv(mrb_state *mrb, mrb_irep *irep)
4087 {
4088 int i;
4089
4090 if (irep->flags & MRB_IREP_NO_FREE) return;
4091 if (irep->lv) {
4092 mrb_free(mrb, (void*)irep->lv);
4093 irep->lv = NULL;
4094 }
4095 if (!irep->reps) return;
4096 for (i = 0; i < irep->rlen; i++) {
4097 mrb_irep_remove_lv(mrb, (mrb_irep*)irep->reps[i]);
4098 }
4099 }