/
cgen_c_oprule.rb
executable file
·799 lines (712 loc) · 28.9 KB
/
cgen_c_oprule.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
module CodeGenC
class CodeGen
@@ruletab ||= {}
def self.define_ccgen_rule_op(name, &block)
@@ruletab[:CCGEN] ||= {}
if @@ruletab[:CCGEN][name] then
raise "Already defined #{name}"
end
@@ruletab[:CCGEN][name] = block
end
define_ccgen_rule_op :NOP do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :MOVE do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
outr = inst.outreg[0]
ccgen.dcode << "#{gen_declare(ccgen, outr, tup, infer)};/*snd*/\n"
reg_real_value_noconv(ccgen, inst.inreg[0], node, tup, infer, history)
}
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADI do |ccgen, inst, node, infer, history, tup|
oreg = inst.outreg[0]
if node.root.export_regs.include?(oreg) and false then
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer)};\n"
src = inst.para[0]
srct = get_ctype_from_robj(src)
dstt = get_ctype(ccgen, oreg, tup, infer)
src = gen_type_conversion(ccgen, dstt, srct, src, tup, node, infer, history, oreg)
ccgen.pcode << "v#{oreg.id} = #{src};\n"
end
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADL do |ccgen, inst, node, infer, history, tup|
@@ruletab[:CCGEN][:LOADI].call(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADSYM do |ccgen, inst, node, infer, history, tup|
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADNIL do |ccgen, inst, node, infer, history, tup|
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADSELF do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :LOADT do |ccgen, inst, node, infer, history, tup|
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :LOADF do |ccgen, inst, node, infer, history, tup|
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :GETGLOBAL do |ccgen, inst, node, infer, history, tup|
oreg = inst.outreg[0]
case inst.para[0]
when :$/
ccgen.dcode << gen_declare(ccgen, oreg, tup, infer)
ccgen.dcode << ";\n"
ccgen.pcode << "v#{oreg.id} = mrb_gv_get(mrb, mrb_intern_lit(mrb, \"#{inst.para[0]}\"));\n"
else
end
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :SETGLOBAL do |ccgen, inst, node, infer, history, tup|
p "FOO"
p tup
p infer.typetupletab.rev_table[tup]
p inst.inreg[0].id
p inst.inreg[0].get_type(tup).map {|ty| ty} if inst.inreg[0].type[tup]
inst.inreg[0].type.each do |tp, tys|
p "#{tp} #{tys.map {|ty| ty.place}}"
end
nil
end
define_ccgen_rule_op :GETCONST do |ccgen, inst, node, infer, history, tup|
oreg = inst.outreg[0]
if oreg.type[tup] and
oreg.type[tup][0].is_a?(MTypeInf::LiteralType) then
val = oreg.type[tup][0].val
else
p "Unknown Constant value"
p inst.para[0]
p oreg.type
end
if !(val.is_a?(Fixnum) or val.is_a?(Float) or ccgen.clstab[val]) then
cno = ccgen.clstab.size
ccgen.hcode << "#{get_ctype(ccgen, oreg, tup, infer)} const#{cno};\n"
ccgen.clstab[val] = [inst.para[0], "const#{cno}"]
end
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :SETCONST do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :GETMCNST do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :GETIV do |ccgen, inst, node, infer, history, tup|
dst = inst.outreg[0]
ivreg = inst.inreg[0]
slf = inst.inreg[1]
gen_get_iv(ccgen, inst, node, infer, history, tup, slf, ivreg, dst)
nil
end
define_ccgen_rule_op :SETIV do |ccgen, inst, node, infer, history, tup|
dst = inst.outreg[0]
valr = inst.inreg[0]
slf = inst.inreg[1]
ivreg = inst.outreg[0]
gen_set_iv(ccgen, inst, node, infer, history, tup, slf, ivreg, valr, dst)
nil
end
define_ccgen_rule_op :GETUPVAR do |ccgen, inst, node, infer, history, tup|
up = inst.para[1]
ireg = inst.inreg[0]
oreg = inst.outreg[0]
proc = ccgen.callstack[-1][0]
pty = ccgen.callstack[-1][2]
up.times do
proc = proc.parent
end
dstt = get_ctype(ccgen, oreg, tup, infer)
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer)};\n"
if pty == :mrb_value then
pos = proc.env.index(ireg)
val = "(mrb_proc_ptr(mrbproc))->e.env->stack[#{pos + 1}]"
val = gen_type_conversion(ccgen, dstt, :mrb_value, val, tup, node, infer, history, oreg)
ccgen.pcode << "v#{oreg.id} = #{val};\n"
else
val = "proc->env#{"->prev" * up}->v#{ireg.id}"
#val = gen_type_conversion(ccgen, pty, dstt, val, tup, node, infer, history, oreg)
ccgen.pcode << "v#{oreg.id} = #{val};\n"
end
nil
end
define_ccgen_rule_op :SETUPVAR do |ccgen, inst, node, infer, history, tup|
up = inst.para[1]
oreg = inst.outreg[0]
ireg = inst.inreg[0]
pty = ccgen.callstack[-1][2]
proc = ccgen.callstack[-1][0]
frame = inst.para[0]
ptup = 0
proc.tups.each do |f, t|
if frame == f then
ptup = t
break
end
end
dstt = get_ctype(ccgen, oreg, ptup, infer)
if pty == :mrb_value then
pos = proc.env.index(oreg)
dst = "(mrb_proc_ptr(mrbproc))->e.env->stack[#{pos + 1}]"
val = reg_real_value2(ccgen, ireg, oreg, node, tup, ptup, infer, history)
val = gen_type_conversion(ccgen, :mrb_value, dstt, val, tup, node, infer, history, oreg)
ccgen.pcode << "#{dst} = #{val};\n"
else
val = reg_real_value2(ccgen, ireg, oreg, node, tup, ptup, infer, history)
ccgen.pcode << "proc->env#{"->prev" * up}->v#{oreg.id} = #{val};\n"
end
nil
end
define_ccgen_rule_op :ENTER do |ccgen, inst, node, infer, history, tup|
ax = inst.para[0]
m1 = (ax >> 18) & 0x1f
o = (ax >> 13) & 0x1f
r = (ax >> 12) & 0x1
m2 = (ax >> 7) & 0x1f
argc = infer.typetupletab.rev_table[tup].size - 4
if r == 1 then
if argc - m1 == 1 then
oreg = inst.outreg[m1]
# argument num is 1
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer)};\n"
if oreg.is_escape?(tup) then
ccgen.pcode << "v#{oreg.id} = mrb_ary_new_from_values(mrb, 1, &v#{inst.inreg[m1].id});\n"
else
ccgen.pcode << "v#{oreg.id} = &v#{inst.inreg[m1].id};\n"
end
inst.para[2][m1] = "v#{oreg.id}"
else
# TODO multiple variale argument
(argc - m1).times do |i|
if inst.outreg[m1].is_escape?(tup) then
inst.para[2][m1 + i] = "(ARY_PTR(mrb_ary_ptr(v#{inst.outreg[m1].id}))[#{i}])"
else
inst.para[2][m1 + i] = "v#{inst.outreg[m1].id}[#{i}]"
end
end
end
end
inst.inreg.each_with_index do |ireg, i|
oreg = inst.outreg[i]
if node.root.export_regs.include?(oreg) then
cirep = node.root.irep
reggen = oreg.genpoint
if (!oreg.is_a?(RiteSSA::ParmReg) or oreg.gen_declare > argc) and
(!oreg.is_a?(RiteSSA::ParmReg) and
(oreg.refpoint.any? {|ins| ins.node.root.irep == cirep} or
(reggen.op == :ENTER and
reggen.outreg.index(oreg) > argc))) then
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer, true)};\n"
end
if ireg.refpoint.size == 0 and
(!ireg.is_a?(RiteSSA::ParmReg) or ireg.genpoint > argc) then
ccgen.dcode << "#{gen_declare(ccgen, ireg, tup, infer, true)};\n"
end
if ireg.type[tup] == nil and
get_ctype(ccgen, oreg, tup, infer) != :mrb_value then
ccgen.pcode << "env.v#{oreg.id} = 0;/*enter */\n"
else
src = reg_real_value(ccgen, ireg, oreg,
node, tup, infer, history)
if src then
ccgen.pcode << "env.v#{oreg.id} = #{src};/*enter */\n"
end
end
end
end
if o != 0 and argc > m1 + m2 then
if r == 1 then
pos = argc - m1 - m2
if pos >= inst.para[3].size then
pos = inst.para[3].size - 1
end
else
pos = argc - m1 - m2
end
nnode = inst.para[3][pos].exit_link[0]
["", [nnode]]
else
nil
end
end
define_ccgen_rule_op :JMP do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :JMPIF do |ccgen, inst, node, infer, history, tup|
cond, srct = reg_real_value_noconv(ccgen, inst.inreg[0], node, tup, infer, history)
cond = gen_type_conversion(ccgen, :mrb_bool, srct, cond, node, tup, infer, history, nil)
r = 0
inst.inreg[0].get_type(tup).each do |ty|
if ty.class_object == NilClass or
ty.class_object == FalseClass then
r |= 1
else
r |= 2
end
end
if cond == true or r == 2 then
["", [node.exit_link[1]]]
elsif cond == false or r == 1 then
["", [node.exit_link[0]]]
else
src = "if (#{cond}) goto L#{node.exit_link[1].id}; else goto L#{node.exit_link[0].id};\n"
[src, [node.exit_link[1], node.exit_link[0]]]
end
end
define_ccgen_rule_op :JMPNOT do |ccgen, inst, node, infer, history, tup|
cond, srct = reg_real_value_noconv(ccgen, inst.inreg[0], node, tup, infer, history)
cond = gen_type_conversion(ccgen, :mrb_bool, srct, cond, node, tup, infer, history, nil)
r = 0
inst.inreg[0].get_type(tup).each do |ty|
if ty.class_object == NilClass or
ty.class_object == FalseClass then
r |= 1
else
r |= 2
end
end
if cond == true or r == 2 then
nd = MTypeInf::TypeInferencer.get_jmp_target(node, 0, inst)
["", [nd]]
elsif cond == false or r == 1 then
nd = MTypeInf::TypeInferencer.get_jmp_target(node, 1, inst)
["", [nd]]
else
src = "if (#{cond}) goto L#{node.exit_link[0].id}; else goto L#{node.exit_link[1].id};\n"
nd0 = MTypeInf::TypeInferencer.get_jmp_target(node, 0, inst)
nd1 = MTypeInf::TypeInferencer.get_jmp_target(node, 1, inst)
[src, [nd0, nd1]]
end
end
define_ccgen_rule_op :ONERR do |ccgen, inst, node, infer, history, tup|
ccgen.pcode << "{\n"
ccgen.pcode << "struct mrb_jmpbuf *oldjmp = mrb->jmp;\n"
ccgen.pcode << "struct mrb_jmpbuf newjmp;\n"
ccgen.pcode << "mrb->jmp = &newjmp;\n"
ccgen.pcode << "if (MRB_SETJMP(mrb->jmp->impl)) {\n"
ccgen.pcode << "mrb->jmp = oldjmp;\n"
ccgen.code_gen_node(node.exit_link[1], infer, :onerr, history, tup)
ccgen.pcode << "}\n"
nil
end
define_ccgen_rule_op :RESCUE do |ccgen, inst, node, infer, history, tup|
if inst.para[0] == 0 then
oreg = inst.outreg[0]
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer, true)};\n"
ccgen.pcode << "v#{oreg.id} = mrb_obj_value(mrb->exc);\n"
ccgen.pcode << "mrb->exc = 0;\n"
else
oreg = inst.outreg[0]
if oreg.type[tup].size > 1 then
ireg0 = inst.inreg[0]
ireg1 = inst.inreg[1]
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer, true)};\n"
ccgen.pcode << "v#{oreg.id} = mrb_bool_value(mrb_obj_is_kind_of(mrb, v#{ireg0.id}, mrb_class_ptr(v#{ireg1.id})));\n"
end
end
nil
end
define_ccgen_rule_op :POPERR do |ccgen, inst, node, infer, history, tup|
ccgen.pcode << "mrb->jmp = oldjmp;\n"
ccgen.pcode << "}\n"
nil
end
define_ccgen_rule_op :RAISE do |ccgen, inst, node, infer, history, tup|
arg, argt = reg_real_value_noconv(ccgen, inst.inreg[0], node, tup, infer, history)
arg = gen_type_conversion(ccgen, :mrb_value, argt, arg, tup, node, infer, history, nil)
ccgen.pcode << "mrb_exc_raise(mrb, mrb_make_exception(mrb, 1, &#{arg}));\n"
nil
end
define_ccgen_rule_op :SEND do |ccgen, inst, node, infer, history, tup|
op_send(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :SENDB do |ccgen, inst, node, infer, history, tup|
op_send(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :RETURN do |ccgen, inst, node, infer, history, tup|
# Skip unnessery boxing
otype = get_ctype(ccgen, inst.outreg[0], tup, infer)
itype = get_ctype(ccgen, inst.inreg[0], tup, infer)
if ccgen.callstack[-1][3] or
otype != :mrb_value or
itype == :mrb_value then
retval = reg_real_value(ccgen, inst.inreg[0], node.root.retreg, node, tup, infer, history)
else
retval = nil
end
aregs = node.root.allocate_reg[tup]
useheap = nil
if aregs then
useheap = aregs.any? {|reg|
!CodeGen::gen_typesize(ccgen, reg, tup, infer)
}
end
if useheap then
ccgen.pcode << "mrb_gc_arena_restore(mrb, ai);\n"
end
if inst.para[1] != node.root then # This is not method
if inst.para[0] == 1 then
ccgen.have_ret_handler = true
ccgen.pcode << "prevgctab->ret_status = 1;\n"
end
if inst.para[0] == 2 then
ccgen.have_ret_handler = true
ccgen.pcode << "prevgctab->ret_status = 2;\n"
end
end
if retval then
ccgen.pcode << "return #{retval};\n"
else
ccgen.pcode << "return mrb_nil_value();\n"
end
nil
end
define_ccgen_rule_op :EQ do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :==)
}
nil
end
define_ccgen_rule_op :LT do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :<)
}
nil
end
define_ccgen_rule_op :LE do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :<=)
}
nil
end
define_ccgen_rule_op :GT do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :>)
}
nil
end
define_ccgen_rule_op :GE do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :>=)
}
nil
end
define_ccgen_rule_op :ADD do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :+)
}
nil
end
define_ccgen_rule_op :SUB do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :-)
}
nil
end
define_ccgen_rule_op :MUL do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :*)
}
nil
end
define_ccgen_rule_op :DIV do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.inreg[1], :/)
}
nil
end
define_ccgen_rule_op :ADDI do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.para[1], :+)
}
nil
end
define_ccgen_rule_op :SUBI do |ccgen, inst, node, infer, history, tup|
do_if_multi_use(ccgen, inst, node, infer, history, tup) {
gen_term_top(ccgen, inst, node, tup, infer, history, inst.inreg[0], inst.para[1], :-)
}
nil
end
define_ccgen_rule_op :ARRAY do |ccgen, inst, node, infer, history, tup|
reg = inst.outreg[0]
uv = MTypeInf::ContainerType::UNDEF_VALUE
aryt = reg.get_type(tup)[0]
eareg = aryt.element
aescape = reg.is_escape?(tup) #or (!aryt.immidiate_only)
etype = get_ctype(ccgen, eareg[uv], tup, infer)
if etype.is_a?(Array) then
etype = etype[0..1].join(' ')
end
if aescape then
vals = inst.inreg.map {|ireg|
val, convp = reg_real_value_noconv(ccgen, ireg, node, tup, infer, history)
gen_type_conversion(ccgen, :mrb_value, convp, val, tup, node, infer, history, reg)
}
ccgen.dcode << "mrb_value v#{reg.id};\n"
ccgen.pcode << "{\n"
ccgen.pcode << "mrb_value tmpele[] = {\n"
ccgen.pcode << vals.join(', ')
ccgen.pcode << "\n};\n"
gen_gc_table2(ccgen, node, reg)
ccgen.pcode << "v#{reg.id} = mrb_ary_new_from_values(mrb, #{vals.size}, tmpele);\n"
ccgen.pcode << "ARY_SET_LEN(mrb_ary_ptr(v#{reg.id}), #{vals.size});\n"
ccgen.callstack[-1][1] = true
ccgen.pcode << "}\n"
else
type = get_ctype_aux(ccgen, reg, tup, infer)
if type == :array then
i = -1
ereg = eareg[uv]
vals = inst.inreg.map {|ireg|
i = i + 1
reg_real_value(ccgen, ireg, ereg, node, tup, infer, history)
}
valnum = vals.size
asize = eareg.size
asize = (asize < valnum) ? valnum : asize
rc = can_use_caller_area(reg.get_type(tup)[0])
if rc == 2 or rc == 3 then
ccgen.dcode << "#{etype} *v#{reg.id};\n"
if rc == 2 then
ccgen.pcode << "v#{reg.id} = prevgctab->caller_alloc;\n"
ccgen.pcode << "prevgctab->caller_alloc += (sizeof(#{etype}) * #{asize + 1});\n"
elsif rc == 3 then
ccgen.pcode << "v#{reg.id} = prevgctab->prev->caller_alloc;\n"
ccgen.pcode << "prevgctab->prev->caller_alloc += (sizeof(#{etype}) * #{asize + 1});\n"
end
vals.each_with_index do |src, i|
ccgen.pcode << "v#{reg.id}[#{i}] = #{src};\n"
end
else
ccgen.pcode << "#{etype} v#{reg.id}[#{asize + 1}] = {\n"
ccgen.pcode << vals.join(', ')
ccgen.pcode << "};\n"
end
if etype == :mrb_value then
ccgen.pcode << "v#{reg.id}[#{valnum}].value.ttt = MRB_TT_FREE;\n"
(asize - valnum).times do |i|
ccgen.pcode << "v#{reg.id}[#{valnum + i + 1}].value.ttt = MRB_TT_FREE;\n"
end
csize = ccgen.gccomplex_size
ccgen.gccomplex_size += 1
ccgen.pcode << "gctab->complex[#{csize}] = v#{reg.id};\n"
ccgen.pcode << "gctab->csize = #{ccgen.gccomplex_size};\n"
end
end
end
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :ARYCAT do |ccgen, inst, node, infer, history, tup|
pos = inst.para[0]
basereg = inst.inreg[0]
valreg = inst.inreg[1]
valereg = valreg.type[tup][0].element[pos]
oreg = inst.outreg[0]
elereg = oreg.type[tup][0].element[MTypeInf::ContainerType::UNDEF_VALUE]
if valereg then
valsrc = reg_real_value(ccgen, valereg, elereg, node, tup, infer, history)
else
valsrc = "mrb_nil_value()"
end
ccgen.dcode << gen_declare(ccgen, oreg, tup, infer)
ccgen.dcode << ";\n"
ccgen.pcode << "v#{basereg.id}[#{pos}] = #{valsrc};\n"
ccgen.pcode << "v#{oreg.id} = v#{basereg.id};\n"
nil
end
define_ccgen_rule_op :AREF do |ccgen, inst, node, infer, history, tup|
idx = inst.para[0]
gen_array_aref(ccgen, inst, node, infer, history, tup, idx)
nil
end
define_ccgen_rule_op :STRING do |ccgen, inst, node, infer, history, tup|
strlit = unescape_string(inst.para[0])
oreg = inst.outreg[0]
if oreg.is_escape?(tup) then
ccgen.dcode << "mrb_value v#{oreg.id};\n"
gen_gc_table_core(ccgen, node, infer, history, tup, inst.para[1], inst.para[2], 0)
ccgen.pcode << "mrb->ud = (void *)gctab;\n"
ccgen.pcode << "v#{oreg.id} = mrb_str_new(mrb, #{strlit}, #{inst.para[0].size});"
else
ccgen.dcode << "char *v#{oreg.id} = #{strlit};\n"
end
nil
end
define_ccgen_rule_op :STRCAT do |ccgen, inst, node, infer, history, tup|
ireg0 = inst.inreg[0]
ireg0.flush_type(tup)
ireg1 = inst.inreg[1]
ireg1.flush_type(tup)
oreg = inst.outreg[0]
ccgen.dcode << "mrb_value v#{oreg.id};\n"
val0, val0t = reg_real_value_noconv(ccgen, ireg0, node, tup, infer, history)
val1, val1t = reg_real_value_noconv(ccgen, ireg1, node, tup, infer, history)
if val0t == :mrb_value then
if val1t == :mrb_value then
ccgen.pcode << "mrb->ud = (void *)gctab;\n"
ccgen.pcode << "v#{oreg.id} = mrb_str_cat_str(mrb, #{val0}, #{val1});\n"
else
val1 = gen_type_conversion(ccgen, [:char, "*"], val1t, val1, node, tup, infer, history, nil)
ccgen.pcode << "mrb->ud = (void *)gctab;\n"
ccgen.pcode << "v#{oreg.id} = mrb_str_cat_cstr(mrb, #{val0}, #{val1});\n"
end
else
val0 = gen_type_conversion(ccgen, :mrb_value, val0t, val0, node, tup, infer, history, oreg)
p0var = "v#{oreg.id}"
ccgen.pcode << "#{p0var} = #{val0};\n"
if val1t == :mrb_value then
# val1, dmy = reg_real_value_noconv(ccgen, ireg1, node, tup, infer, history)
p1var = "v#{ireg1.id}"
if p1var != val1 then
ccgen.dcode << "mrb_value #{p1var};\n"
ccgen.pcode << "#{p1var} = #{val1};\n"
end
ccgen.pcode << "mrb->ud = (void *)gctab;\n"
ccgen.pcode << "v#{oreg.id} = mrb_str_cat_str(mrb, #{p0var}, #{p1var});\n"
else
val1 = gen_type_conversion(ccgen, [:char, "*"], val1t, val1, node, tup, infer, history, nil)
ccgen.pcode << "mrb->ud = (void *)gctab;\n"
ccgen.pcode << "v#{oreg.id} = mrb_str_cat_cstr(mrb, #{p0var}, #{val1});\n"
end
end
nil
end
define_ccgen_rule_op :LAMBDA do |ccgen, inst, node, infer, history, tup|
# make env struct
envreg = inst.para[1]
proc = inst.outreg[0].get_type(tup)[0]
cproc = ccgen.callstack[-1][0]
pproc = cproc.parent
if !ccgen.using_proc.include?(proc) then
tupnum = proc.using_tup.size
if envreg.size > 0 or pproc then
ccgen.hcode << "struct proc#{proc.id} {\n"
ccgen.hcode << "int id;\n"
ccgen.hcode << "void *code[#{tupnum}];\n"
ccgen.hcode << "struct env#{cproc.id} *env;\n"
else
ccgen.hcode << "struct proc#{proc.id} {\n"
ccgen.hcode << "int id;\n"
ccgen.hcode << "void *code[#{tupnum}];\n"
end
slfdecl = gen_declare(ccgen, proc.slfreg, tup, infer, false, true)
ccgen.hcode << "#{slfdecl};\n"
ccgen.hcode << "};\n"
ccgen.using_proc.push proc
end
regno = inst.outreg[0].id
ccgen.dcode << "struct proc#{proc.id} v#{regno};\n"
ccgen.pcode << "v#{regno}.id = #{proc.id};\n"
ccgen.pcode << "v#{regno}.self = self;\n"
if envreg.size > 0 or pproc then
ccgen.pcode << "v#{regno}.env = &env;\n"
end
dstt = get_ctype(ccgen, inst.outreg[0], tup, infer)
proc.using_tup.each do |tp, i|
bfunc = gen_block_func("p#{proc.id}", proc.slf[0].class_object, inst.para[3], tp)
# ccgen.pcode << "v#{regno}.code[#{i}] = (void *)#{bfunc};\n"
pproc = ccgen.callstack[-1][0]
minf = [bfunc, proc, tp, dstt, pproc]
ccgen.proctab[proc.irep] ||= []
ccgen.proctab[proc.irep][i] = minf
end
if node.root.is_export_env then
val = gen_type_conversion(ccgen, dstt, [:gproc, proc.id], "(gproc)&v#{regno}", tup, node, infer, history, inst.outreg[0])
ccgen.dcode << "mrb_value vv#{regno};\n"
ccgen.pcode << "vv#{regno} = #{val};\n"
end
set_closure_env(ccgen, inst, node, infer, history, tup)
nil
end
define_ccgen_rule_op :HASH do |ccgen, inst, node, infer, history, tup|
oreg = inst.outreg[0]
uv = MTypeInf::ContainerType::UNDEF_VALUE
hasht = oreg.get_type(tup)[0]
eareg = hasht.element
etype = get_ctype(ccgen, eareg[uv], tup, infer)
if oreg.is_escape?(tup) or true then
ccgen.dcode << "mrb_value v#{oreg.id};\n"
gen_gc_table2(ccgen, node, oreg)
ccgen.pcode << "v#{oreg.id} = mrb_hash_new(mrb);\n"
else
ccgen.dcode << "struct hash_#{etype} *v#{oreg.id};\n"
ccgen.pcode << "v#{oreg.id} = alloca(sizeof(struct hash_#{etype}));\n"
ccgen.pcode << "v#{oreg.id}->first = #{bval};\n"
ccgen.pcode << "v#{oreg.id}->last = #{eval};\n"
ccgen.pcode << "v#{oreg.id}->exclude_end = #{inst.para[0]};\n"
end
nil
end
define_ccgen_rule_op :CLASS do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :MODULE do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :EXEC do |ccgen, inst, node, infer, history, tup|
tclass = inst.inreg[0].flush_type(tup)[tup]
root = node.root
co = tclass[0].val
irepssa = inst.objcache[co]
intype = [tclass]
ntup = infer.typetupletab.get_tupple_id(intype, MTypeInf::PrimitiveType.new(NilClass), tup)
fname = "f#{inst.inreg[0].id}"
otype = get_ctype(ccgen, inst.outreg[0], tup, infer)
ccgen.pcode << "#{otype} #{fname}(){\n"
ccgen.pcode << gen_declare_core(ccgen, inst.inreg[0], tup, infer, false, "self")
ccgen.pcode << ";\n"
ccgen.code_gen_node(irepssa.nodes[0], infer, nil, history, ntup)
ccgen.pcode << "}\n"
ccgen.pcode << "#{fname}();\n"
nil
end
define_ccgen_rule_op :METHOD do |ccgen, inst, node, infer, history, tup|
if ccgen.tmp_attribute.size != 0 then
rt = inst.inreg[0].type(tup)[0][0].val
name = inst.para[0]
ccgen.method_attribute[[name, rt]] = ccgen.tmp_attribute.dup
ccgen.tmp_attribute.clear
end
nil
end
define_ccgen_rule_op :TCLASS do |ccgen, inst, node, infer, history, tup|
nil
end
define_ccgen_rule_op :RANGE do |ccgen, inst, node, infer, history, tup|
oreg = inst.outreg[0]
et = oreg.get_type(tup)[0]
ereg = et.element[0]
bval, bt = reg_real_value_noconv(ccgen, inst.inreg[0], node, tup, infer, history)
eval, et = reg_real_value_noconv(ccgen, inst.inreg[1], node, tup, infer, history)
etype = get_ctype(ccgen, ereg, tup, infer)
if oreg.is_escape?(tup) then
bval = gen_type_conversion(ccgen, :mrb_value, bt, bval, tup, node, infer, history, oreg)
eval = gen_type_conversion(ccgen, :mrb_value, et, eval, tup, node, infer, history, oreg)
# TODO BOXING Range
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer)};\n"
ccgen.pcode << "v#{oreg.id} = mrb_range_new(mrb, #{bval}, #{eval}, #{inst.para[0]});\n"
else
ccgen.dcode << "#{gen_declare(ccgen, oreg, tup, infer)};\n"
ccgen.pcode << "v#{oreg.id} = alloca(sizeof(struct range_#{etype}));\n"
ccgen.pcode << "v#{oreg.id}->first = #{bval};\n"
ccgen.pcode << "v#{oreg.id}->last = #{eval};\n"
ccgen.pcode << "v#{oreg.id}->exclude_end = #{inst.para[0]};\n"
end
nil
end
end
end