FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mruby/src/codegen.c at master · javier/mruby · GitHub
javier
/
mruby
Public
forked from
mruby/mruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
mruby
/
src
/
codegen.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2279 lines (2047 loc) · 47.7 KB
Breadcrumbs
mruby
/
src
/
codegen.c
Copy path
File metadata and controls
2279 lines (2047 loc) · 47.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** codegen.c - mruby code generator
**
** See Copyright Notice in mruby.h
*/
#undef
CODEGEN_TEST
#define
CODEGEN_DUMP
#include
"mruby.h"
#include
"mruby/irep.h"
#include
"compile.h"
#include
"mruby/proc.h"
#include
"opcode.h"
#include
"mruby/string.h"
#include
<string.h>
#include
<stdlib.h>
typedef
mrb_ast_node
node
;
typedef
struct
mrb_parser_state
parser_state
;
struct
loopinfo
{
enum
looptype
{
LOOP_NORMAL
,
LOOP_BLOCK
,
LOOP_FOR
,
LOOP_BEGIN
,
LOOP_RESCUE
,
}
type
;
int
pc1
,
pc2
,
pc3
,
acc
;
int
ensure_level
;
struct
loopinfo
*
prev
;
};
typedef
struct
scope
{
mrb_state
*
mrb
;
mrb_pool
*
mpool
;
jmp_buf
jmp
;
struct
scope
*
prev
;
node
*
lv
;
int
sp
;
int
pc
;
int
lastlabel
;
int
ainfo
;
struct
loopinfo
*
loop
;
int
ensure_level
;
mrb_code
*
iseq
;
int
icapa
;
mrb_value
*
pool
;
int
plen
;
int
pcapa
;
mrb_sym
*
syms
;
int
slen
;
int
nlocals
;
int
nregs
;
int
idx
;
}
codegen_scope
;
static
codegen_scope
*
scope_new
(
mrb_state
*
mrb
,
codegen_scope
*
prev
,
node
*
lv
);
static
void
scope_finish
(
codegen_scope
*
s
,
int
idx
);
static
struct
loopinfo
*
loop_push
(
codegen_scope
*
s
,
enum
looptype
t
);
static
void
loop_break
(
codegen_scope
*
s
,
node
*
tree
);
static
void
loop_pop
(
codegen_scope
*
s
,
int
val
);
static
void
gen_assignment
(
codegen_scope
*
s
,
node
*
node
,
int
sp
,
int
val
);
static
void
gen_vmassignment
(
codegen_scope
*
s
,
node
*
tree
,
int
rhs
,
int
val
);
static
void
codegen
(
codegen_scope
*
s
,
node
*
tree
,
int
val
);
static
void
codegen_error
(
codegen_scope
*
s
,
const
char
*
message
)
{
if
(!
s
)
return
;
while
(
s
->
prev
) {
mrb_pool_close
(
s
->
mpool
);
s
=
s
->
prev
;
}
mrb_pool_close
(
s
->
mpool
);
fprintf
(
stderr
,
"codegen error: %s\n"
,
message
);
longjmp
(
s
->
jmp
,
1
);
}
static
void
*
codegen_palloc
(
codegen_scope
*
s
,
size_t
len
)
{
void
*
p
=
mrb_pool_alloc
(
s
->
mpool
,
len
);
if
(!
p
)
codegen_error
(
s
,
"pool memory allocation"
);
return
p
;
}
void
*
codegen_malloc
(
codegen_scope
*
s
,
size_t
len
)
{
void
*
p
=
mrb_malloc
(
s
->
mrb
,
len
);
if
(!
p
)
codegen_error
(
s
,
"mrb_malloc"
);
return
p
;
}
void
*
codegen_realloc
(
codegen_scope
*
s
,
void
*
p
,
size_t
len
)
{
p
=
mrb_realloc
(
s
->
mrb
,
p
,
len
);
if
(!
p
&&
len
>
0
)
codegen_error
(
s
,
"mrb_realloc"
);
return
p
;
}
static
int
new_label
(
codegen_scope
*
s
)
{
s
->
lastlabel
=
s
->
pc
;
return
s
->
pc
;
}
static
inline
void
genop
(
codegen_scope
*
s
,
mrb_code
i
)
{
if
(
s
->
pc
==
s
->
icapa
) {
s
->
icapa
*=
2
;
s
->
iseq
=
codegen_realloc
(
s
,
s
->
iseq
,
sizeof
(
mrb_code
)
*
s
->
icapa
);
}
s
->
iseq
[
s
->
pc
]
=
i
;
s
->
pc
++
;
}
static
void
genop_peep
(
codegen_scope
*
s
,
mrb_code
i
,
int
val
)
{
// peephole optimization
if
(!
val
&&
s
->
lastlabel
!=
s
->
pc
&&
s
->
pc
>
0
) {
mrb_code
i0
=
s
->
iseq
[
s
->
pc
-
1
];
int
c1
=
GET_OPCODE
(
i
);
int
c0
=
GET_OPCODE
(
i0
);
switch
(
c1
) {
case
OP_MOVE
:
switch
(
c0
) {
case
OP_MOVE
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i
)
==
GETARG_B
(
i0
)
&&
GETARG_A
(
i
) >=
s
->
nlocals
) {
// skip swapping OP_MOVE
return
;
}
break
;
case
OP_LOADI
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i0
) >=
s
->
nlocals
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_AsBx
(
OP_LOADI
,
GETARG_A
(
i
),
GETARG_sBx
(
i0
));
return
;
}
break
;
case
OP_ARRAY
:
case
OP_HASH
:
case
OP_RANGE
:
case
OP_AREF
:
case
OP_GETUPVAR
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i0
) >=
s
->
nlocals
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_ABC
(
c0
,
GETARG_A
(
i
),
GETARG_B
(
i0
),
GETARG_C
(
i0
));
return
;
}
break
;
case
OP_LOADSYM
:
case
OP_GETGLOBAL
:
case
OP_GETIV
:
case
OP_GETCV
:
case
OP_GETCONST
:
case
OP_GETSPECIAL
:
case
OP_LOADL
:
case
OP_STRING
:
case
OP_GETMCNST
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i0
) >=
s
->
nlocals
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_ABx
(
c0
,
GETARG_A
(
i
),
GETARG_Bx
(
i0
));
return
;
}
break
;
case
OP_SCLASS
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i0
) >=
s
->
nlocals
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_AB
(
c0
,
GETARG_A
(
i
),
GETARG_B
(
i0
));
return
;
}
break
;
case
OP_LOADNIL
:
case
OP_LOADSELF
:
case
OP_LOADT
:
case
OP_LOADF
:
case
OP_OCLASS
:
if
(
GETARG_B
(
i
)
==
GETARG_A
(
i0
)
&&
GETARG_A
(
i0
) >=
s
->
nlocals
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_A
(
c0
,
GETARG_A
(
i
));
return
;
}
break
;
}
break
;
case
OP_SETIV
:
case
OP_SETCV
:
case
OP_SETCONST
:
case
OP_SETMCNST
:
switch
(
c0
) {
case
OP_MOVE
:
if
(
GETARG_A
(
i
)
==
GETARG_A
(
i0
)) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_ABx
(
c1
,
GETARG_B
(
i0
),
GETARG_Bx
(
i
));
return
;
}
break
;
}
break
;
case
OP_SETUPVAR
:
switch
(
c0
) {
case
OP_MOVE
:
if
(
GETARG_A
(
i
)
==
GETARG_A
(
i0
)) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_ABC
(
c1
,
GETARG_B
(
i0
),
GETARG_B
(
i
),
GETARG_C
(
i
));
return
;
}
break
;
}
break
;
case
OP_EPOP
:
if
(
c0
==
OP_EPOP
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_A
(
OP_EPOP
,
GETARG_A
(
i0
)
+
GETARG_A
(
i
));
return
;
}
break
;
case
OP_POPERR
:
if
(
c0
==
OP_POPERR
) {
s
->
iseq
[
s
->
pc
-
1
]
=
MKOP_A
(
OP_POPERR
,
GETARG_A
(
i0
)
+
GETARG_A
(
i
));
return
;
}
break
;
}
}
genop
(
s
,
i
);
}
static
void
scope_error
(
codegen_scope
*
s
)
{
exit
(
1
);
}
static
inline
void
dispatch
(
codegen_scope
*
s
,
int
pc
)
{
int
diff
=
s
->
pc
-
pc
;
mrb_code
i
=
s
->
iseq
[
pc
];
int
c
=
GET_OPCODE
(
i
);
s
->
lastlabel
=
s
->
pc
;
switch
(
c
) {
case
OP_JMP
:
case
OP_JMPIF
:
case
OP_JMPNOT
:
case
OP_ONERR
:
break
;
default
:
fprintf
(
stderr
,
"bug: dispatch on non JMP op\n"
);
scope_error
(
s
);
}
s
->
iseq
[
pc
]
=
MKOP_AsBx
(
c
,
GETARG_A
(
i
),
diff
);
}
static
void
dispatch_linked
(
codegen_scope
*
s
,
int
pc
)
{
mrb_code
i
;
int
pos
;
if
(!
pc
)
return
;
for
(;;) {
i
=
s
->
iseq
[
pc
];
pos
=
GETARG_sBx
(
i
);
dispatch
(
s
,
pc
);
if
(!
pos
)
break
;
pc
=
pos
;
}
}
#define
nregs_update
do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0)
static
void
push_
(
codegen_scope
*
s
)
{
if
(
s
->
sp
>
511
) {
codegen_error
(
s
,
"too complex expression"
);
}
s
->
sp
++
;
nregs_update
;
}
#if
0
static
void
push_n_
(
codegen_scope
*
s
,
int
n
)
{
if
(
s
->
sp
+
n
>
511
) {
codegen_error
(
s
,
"too complex expression"
);
}
s
->
sp
+=
n
;
nregs_update
;
}
#endif
#define
push
() push_(s)
#define
push_n
(
n
) push_n_(s, n)
#define
pop
() (s->sp--)
#define
pop_n
(
n
) (s->sp-=(n))
#define
cursp
() (s->sp)
static
inline
int
new_lit
(
codegen_scope
*
s
,
mrb_value
val
)
{
int
i
;
for
(
i
=
0
;
i
<
s
->
plen
;
i
++
) {
if
(
memcmp
(
&
s
->
pool
[
i
],
&
val
,
sizeof
(
mrb_value
))
==
0
)
return
i
;
}
if
(
s
->
plen
==
s
->
pcapa
) {
s
->
pcapa
*=
2
;
s
->
pool
=
codegen_realloc
(
s
,
s
->
pool
,
sizeof
(
mrb_value
)
*
s
->
pcapa
);
}
s
->
pool
[
s
->
plen
]
=
val
;
return
s
->
plen
++
;
}
static
inline
int
new_msym
(
codegen_scope
*
s
,
mrb_sym
sym
)
{
int
i
,
len
;
len
=
s
->
slen
;
if
(
len
>
255
)
len
=
255
;
for
(
i
=
0
;
i
<
len
;
i
++
) {
if
(
s
->
syms
[
i
]
==
sym
)
return
i
;
if
(
s
->
syms
[
i
]
==
0
)
break
;
}
if
(
i
>
255
) {
codegen_error
(
s
,
"too many symbols (max 256)"
);
}
s
->
syms
[
i
]
=
sym
;
if
(
i
==
s
->
slen
)
s
->
slen
++
;
return
i
;
}
static
inline
int
new_sym
(
codegen_scope
*
s
,
mrb_sym
sym
)
{
int
i
;
for
(
i
=
0
;
i
<
s
->
slen
;
i
++
) {
if
(
s
->
syms
[
i
]
==
sym
)
return
i
;
}
if
(
s
->
slen
>
125
&&
s
->
slen
<
256
) {
s
->
syms
=
codegen_realloc
(
s
,
s
->
syms
,
sizeof
(
mrb_sym
)
*
65536
);
memset
(
s
->
syms
+
s
->
slen
,
0
,
sizeof
(
mrb_sym
)
*
(
256
-
s
->
slen
));
s
->
slen
=
256
;
}
s
->
syms
[
s
->
slen
]
=
sym
;
return
s
->
slen
++
;
}
static
int
node_len
(
node
*
tree
)
{
int
n
=
0
;
while
(
tree
) {
n
++
;
tree
=
tree
->
cdr
;
}
return
n
;
}
#define
lv_name
(
lv
) ((mrb_sym)(lv)->car)
static
int
lv_idx
(
codegen_scope
*
s
,
mrb_sym
id
)
{
node
*
lv
=
s
->
lv
;
int
n
=
1
;
while
(
lv
) {
if
(
lv_name
(
lv
)
==
id
)
return
n
;
n
++
;
lv
=
lv
->
cdr
;
}
return
0
;
}
#define
NOVAL
0
#define
VAL
1
static
void
for_body
(
codegen_scope
*
s
,
node
*
tree
)
{
codegen_scope
*
prev
=
s
;
int
idx
,
base
=
s
->
idx
;
struct
loopinfo
*
lp
;
node
*
n2
;
mrb_code
c
;
// generate receiver
codegen
(
s
,
tree
->
cdr
->
car
,
VAL
);
// generate loop-block
s
=
scope_new
(
s
->
mrb
,
s
,
tree
->
car
);
idx
=
s
->
idx
;
lp
=
loop_push
(
s
,
LOOP_FOR
);
lp
->
pc1
=
new_label
(
s
);
// generate loop variable
n2
=
tree
->
car
;
if
(
n2
->
car
&&
!
n2
->
car
->
cdr
&&
!
n2
->
cdr
) {
genop
(
s
,
MKOP_Ax
(
OP_ENTER
,
1
<<
18
));
gen_assignment
(
s
,
n2
->
car
->
car
,
1
,
NOVAL
);
}
else
{
genop
(
s
,
MKOP_Ax
(
OP_ENTER
,
1
<<
18
));
gen_vmassignment
(
s
,
n2
,
1
,
VAL
);
}
codegen
(
s
,
tree
->
cdr
->
cdr
->
car
,
VAL
);
pop
();
c
=
s
->
iseq
[
s
->
pc
-
1
];
if
(
GET_OPCODE
(
c
)
!=
OP_RETURN
||
GETARG_B
(
c
)
!=
OP_R_NORMAL
||
s
->
pc
==
s
->
lastlabel
) {
genop
(
s
,
MKOP_AB
(
OP_RETURN
,
cursp
(),
OP_R_NORMAL
));
}
loop_pop
(
s
,
NOVAL
);
scope_finish
(
s
,
idx
);
s
=
prev
;
genop
(
s
,
MKOP_Abc
(
OP_LAMBDA
,
cursp
(),
idx
-
base
,
OP_L_BLOCK
));
pop
();
idx
=
new_msym
(
s
,
mrb_intern
(
s
->
mrb
,
"each"
));
genop
(
s
,
MKOP_ABC
(
OP_SEND
,
cursp
(),
idx
,
0
));
}
static
int
lambda_body
(
codegen_scope
*
s
,
node
*
tree
,
int
blk
)
{
int
idx
,
base
=
s
->
idx
;
mrb_code
c
;
s
=
scope_new
(
s
->
mrb
,
s
,
tree
->
car
);
idx
=
s
->
idx
;
if
(
blk
) {
struct
loopinfo
*
lp
=
loop_push
(
s
,
LOOP_BLOCK
);
lp
->
pc1
=
new_label
(
s
);
}
tree
=
tree
->
cdr
;
if
(
tree
->
car
) {
int
ma
,
oa
,
ra
,
pa
,
ka
,
kd
,
ba
,
a
;
int
pos
,
i
;
node
*
n
,
*
opt
;
ma
=
node_len
(
tree
->
car
->
car
);
n
=
tree
->
car
->
car
;
while
(
n
) {
n
=
n
->
cdr
;
}
oa
=
node_len
(
tree
->
car
->
cdr
->
car
);
ra
=
tree
->
car
->
cdr
->
cdr
->
car
?
1
:
0
;
pa
=
node_len
(
tree
->
car
->
cdr
->
cdr
->
cdr
->
car
);
ka
=
kd
=
0
;
ba
=
tree
->
car
->
cdr
->
cdr
->
cdr
->
cdr
?
1
:
0
;
a
=
((
ma
&
0x1f
) <<
18
)
| ((
oa
&
0x1f
) <<
13
)
| ((
ra
&
1
) <<
12
)
| ((
pa
&
0x1f
) <<
7
)
| ((
ka
&
0x1f
) <<
2
)
| ((
kd
&
1
)<<
1
)
| (
ba
&
1
);
s
->
ainfo
=
(((
ma
+
oa
)
&
0x3f
) <<
6
)
/* (12bits = 6:1:5) */
| ((
ra
&
1
) <<
5
)
| (
pa
&
0x1f
);
genop
(
s
,
MKOP_Ax
(
OP_ENTER
,
a
));
pos
=
new_label
(
s
);
for
(
i
=
0
;
i
<
oa
;
i
++
) {
new_label
(
s
);
genop
(
s
,
MKOP_Ax
(
OP_JMP
,
0
));
}
if
(
oa
>
0
) {
genop
(
s
,
MKOP_Ax
(
OP_JMP
,
0
));
}
opt
=
tree
->
car
->
cdr
->
car
;
i
=
0
;
while
(
opt
) {
int
idx
;
dispatch
(
s
,
pos
+
i
);
codegen
(
s
,
opt
->
car
->
cdr
,
VAL
);
idx
=
lv_idx
(
s
, (
mrb_sym
)
opt
->
car
->
car
);
pop
();
genop_peep
(
s
,
MKOP_AB
(
OP_MOVE
,
idx
,
cursp
()),
NOVAL
);
i
++
;
opt
=
opt
->
cdr
;
}
if
(
oa
>
0
) {
dispatch
(
s
,
pos
+
i
);
}
}
codegen
(
s
,
tree
->
cdr
->
car
,
VAL
);
pop
();
c
=
s
->
iseq
[
s
->
pc
-
1
];
if
(
GET_OPCODE
(
c
)
!=
OP_RETURN
||
GETARG_B
(
c
)
!=
OP_R_NORMAL
||
s
->
pc
==
s
->
lastlabel
) {
genop
(
s
,
MKOP_AB
(
OP_RETURN
,
cursp
(),
OP_R_NORMAL
));
}
if
(
blk
) {
loop_pop
(
s
,
NOVAL
);
}
scope_finish
(
s
,
idx
);
return
idx
-
base
;
}
static
int
scope_body
(
codegen_scope
*
s
,
node
*
tree
)
{
codegen_scope
*
scope
=
scope_new
(
s
->
mrb
,
s
,
tree
->
car
);
int
idx
=
scope
->
idx
;
if
(!
s
->
iseq
) {
codegen
(
scope
,
tree
->
cdr
,
NOVAL
);
genop
(
scope
,
MKOP_A
(
OP_STOP
,
0
));
}
else
{
codegen
(
scope
,
tree
->
cdr
,
VAL
);
genop
(
scope
,
MKOP_AB
(
OP_RETURN
,
cursp
(),
OP_R_NORMAL
));
}
scope_finish
(
scope
,
idx
);
return
idx
-
s
->
idx
;
}
static
int
nosplat
(
node
*
t
)
{
while
(
t
) {
if
((
intptr_t
)
t
->
car
->
car
==
NODE_SPLAT
)
return
0
;
t
=
t
->
cdr
;
}
return
1
;
}
static
mrb_sym
attrsym
(
codegen_scope
*
s
,
mrb_sym
a
)
{
const
char
*
name
=
mrb_sym2name
(
s
->
mrb
,
a
);
char
*
name2
;
size_t
len
=
strlen
(
name
);
name2
=
codegen_palloc
(
s
,
len
+
1
);
strcpy
(
name2
,
name
);
name2
[
len
]
=
'='
;
name2
[
len
+
1
]
=
'\0'
;
return
mrb_intern
(
s
->
mrb
,
name2
);
}
static
int
gen_values
(
codegen_scope
*
s
,
node
*
t
)
{
int
n
=
0
;
while
(
t
) {
if
((
intptr_t
)
t
->
car
->
car
==
NODE_SPLAT
) {
// splat mode
pop_n
(
n
);
genop
(
s
,
MKOP_ABC
(
OP_ARRAY
,
cursp
(),
cursp
(),
n
));
push
();
codegen
(
s
,
t
->
car
,
VAL
);
pop
();
pop
();
genop
(
s
,
MKOP_AB
(
OP_ARYCAT
,
cursp
(),
cursp
()
+
1
));
t
=
t
->
cdr
;
while
(
t
) {
push
();
codegen
(
s
,
t
->
car
,
VAL
);
pop
();
pop
();
if
((
intptr_t
)
t
->
car
->
car
==
NODE_SPLAT
) {
genop
(
s
,
MKOP_AB
(
OP_ARYCAT
,
cursp
(),
cursp
()
+
1
));
}
else
{
genop
(
s
,
MKOP_AB
(
OP_ARYPUSH
,
cursp
(),
cursp
()
+
1
));
}
t
=
t
->
cdr
;
}
return
-1
;
}
// normal (no splat) mode
codegen
(
s
,
t
->
car
,
VAL
);
n
++
;
t
=
t
->
cdr
;
}
return
n
;
}
#define
CALL_MAXARGS
127
static
void
gen_call
(
codegen_scope
*
s
,
node
*
tree
,
mrb_sym
name
,
int
sp
,
int
val
)
{
mrb_sym
sym
=
name
?
name
: (
mrb_sym
)
tree
->
cdr
->
car
;
int
idx
;
int
n
=
0
,
noop
=
0
,
sendv
=
0
;
codegen
(
s
,
tree
->
car
,
VAL
);
/* receiver */
idx
=
new_msym
(
s
,
sym
);
tree
=
tree
->
cdr
->
cdr
->
car
;
if
(
tree
) {
n
=
gen_values
(
s
,
tree
->
car
);
if
(
n
<
0
) {
n
=
noop
=
sendv
=
1
;
push
();
}
}
if
(
sp
) {
if
(
sendv
) {
pop
();
genop
(
s
,
MKOP_AB
(
OP_ARYPUSH
,
cursp
(),
sp
));
push
();
}
else
{
genop
(
s
,
MKOP_AB
(
OP_MOVE
,
cursp
(),
sp
));
push
();
n
++
;
}
}
if
(
tree
&&
tree
->
cdr
) {
noop
=
1
;
codegen
(
s
,
tree
->
cdr
,
VAL
);
pop
();
}
else
{
genop
(
s
,
MKOP_A
(
OP_LOADNIL
,
cursp
()));
}
pop_n
(
n
+
1
);
{
const
char
*
name
=
mrb_sym2name
(
s
->
mrb
,
sym
);
if
(!
noop
&&
name
[
0
]
==
'+'
&&
strlen
(
name
)
==
1
) {
genop
(
s
,
MKOP_ABC
(
OP_ADD
,
cursp
(),
idx
,
n
));
}
else
if
(!
noop
&&
name
[
0
]
==
'-'
&&
strlen
(
name
)
==
1
) {
genop
(
s
,
MKOP_ABC
(
OP_SUB
,
cursp
(),
idx
,
n
));
}
else
if
(!
noop
&&
name
[
0
]
==
'<'
&&
strlen
(
name
)
==
1
) {
genop
(
s
,
MKOP_ABC
(
OP_LT
,
cursp
(),
idx
,
n
));
}
else
if
(!
noop
&&
name
[
0
]
==
'<'
&&
strlen
(
name
)
==
2
&&
name
[
1
]
==
'='
) {
genop
(
s
,
MKOP_ABC
(
OP_LE
,
cursp
(),
idx
,
n
));
}
else
if
(!
noop
&&
name
[
0
]
==
'>'
&&
strlen
(
name
)
==
1
) {
genop
(
s
,
MKOP_ABC
(
OP_GT
,
cursp
(),
idx
,
n
));
}
else
if
(!
noop
&&
name
[
0
]
==
'>'
&&
strlen
(
name
)
==
2
&&
name
[
1
]
==
'='
) {
genop
(
s
,
MKOP_ABC
(
OP_GE
,
cursp
(),
idx
,
n
));
}
else
{
if
(
sendv
)
n
=
CALL_MAXARGS
;
genop
(
s
,
MKOP_ABC
(
OP_SEND
,
cursp
(),
idx
,
n
));
}
}
if
(
val
) {
push
();
}
}
static
void
gen_assignment
(
codegen_scope
*
s
,
node
*
node
,
int
sp
,
int
val
)
{
int
idx
;
int
type
=
(
intptr_t
)
node
->
car
;
node
=
node
->
cdr
;
switch
((
intptr_t
)
type
) {
case
NODE_GVAR
:
idx
=
new_sym
(
s
, (
mrb_sym
)
node
);
genop_peep
(
s
,
MKOP_ABx
(
OP_SETGLOBAL
,
sp
,
idx
),
val
);
break
;
case
NODE_LVAR
:
idx
=
lv_idx
(
s
, (
mrb_sym
)
node
);
if
(
idx
>
0
) {
if
(
idx
!=
sp
) {
genop_peep
(
s
,
MKOP_AB
(
OP_MOVE
,
idx
,
sp
),
val
);
}
break
;
}
else
{
/* upvar */
int
lv
=
0
;
codegen_scope
*
up
=
s
->
prev
;
while
(
up
) {
idx
=
lv_idx
(
up
, (
mrb_sym
)
node
);
if
(
idx
>
0
) {
genop_peep
(
s
,
MKOP_ABC
(
OP_SETUPVAR
,
sp
,
idx
,
lv
),
val
);
break
;
}
lv
++
;
up
=
up
->
prev
;
}
// assert(up!=0);
}
break
;
case
NODE_IVAR
:
idx
=
new_sym
(
s
, (
mrb_sym
)
node
);
genop_peep
(
s
,
MKOP_ABx
(
OP_SETIV
,
sp
,
idx
),
val
);
break
;
case
NODE_CVAR
:
idx
=
new_sym
(
s
, (
mrb_sym
)
node
);
genop_peep
(
s
,
MKOP_ABx
(
OP_SETCV
,
sp
,
idx
),
val
);
break
;
case
NODE_CONST
:
idx
=
new_sym
(
s
, (
mrb_sym
)
node
);
genop_peep
(
s
,
MKOP_ABx
(
OP_SETCONST
,
sp
,
idx
),
val
);
break
;
case
NODE_COLON2
:
idx
=
new_sym
(
s
, (
mrb_sym
)
node
->
cdr
);
genop_peep
(
s
,
MKOP_AB
(
OP_MOVE
,
cursp
(),
sp
),
NOVAL
);
push
();
codegen
(
s
,
node
->
car
,
VAL
);
pop_n
(
2
);
genop_peep
(
s
,
MKOP_ABx
(
OP_SETMCNST
,
cursp
(),
idx
),
val
);
break
;
case
NODE_CALL
:
push
();
gen_call
(
s
,
node
,
attrsym
(
s
, (
mrb_sym
)
node
->
cdr
->
car
),
sp
,
val
);
val
=
NOVAL
;
/* push should have done in gen_call() */
break
;
default
:
printf
(
"unknown lhs %d\n"
,
type
);
break
;
}
if
(
val
)
push
();
}
static
void
gen_vmassignment
(
codegen_scope
*
s
,
node
*
tree
,
int
rhs
,
int
val
)
{
int
n
=
0
,
post
=
0
;
node
*
t
,
*
p
;
if
(
tree
->
car
) {
/* pre */
t
=
tree
->
car
;
n
=
0
;
while
(
t
) {
genop
(
s
,
MKOP_ABC
(
OP_AREF
,
cursp
(),
rhs
,
n
));
gen_assignment
(
s
,
t
->
car
,
cursp
(),
NOVAL
);
n
++
;
t
=
t
->
cdr
;
}
}
t
=
tree
->
cdr
;
if
(
t
) {
if
(
t
->
cdr
) {
/* post count */
p
=
t
->
cdr
->
car
;
while
(
p
) {
post
++
;
p
=
p
->
cdr
;
}
}
if
(
val
) {
genop
(
s
,
MKOP_AB
(
OP_MOVE
,
cursp
(),
rhs
));
push
();
}
pop
();
genop
(
s
,
MKOP_ABC
(
OP_APOST
,
cursp
(),
n
,
post
));
n
=
1
;
if
(
t
->
car
) {
/* rest */
gen_assignment
(
s
,
t
->
car
,
cursp
(),
NOVAL
);
}
if
(
t
->
cdr
&&
t
->
cdr
->
car
) {
t
=
t
->
cdr
->
car
;
while
(
t
) {
gen_assignment
(
s
,
t
->
car
,
cursp
()
+
n
,
NOVAL
);
t
=
t
->
cdr
;
n
++
;
}
}
}
}
static
void
raise_error
(
codegen_scope
*
s
,
const
char
*
msg
)
{
int
idx
=
new_lit
(
s
,
mrb_str_new_cstr
(
s
->
mrb
,
msg
));
genop
(
s
,
MKOP_ABx
(
OP_ERR
,
0
,
idx
));
}
static
void
codegen
(
codegen_scope
*
s
,
node
*
tree
,
int
val
)
{
int
nt
;
if
(!
tree
)
return
;
nt
=
(
intptr_t
)
tree
->
car
;
tree
=
tree
->
cdr
;
switch
(
nt
) {
case
NODE_BEGIN
:
while
(
tree
) {
codegen
(
s
,
tree
->
car
,
tree
->
cdr
?
NOVAL
:
val
);
tree
=
tree
->
cdr
;
}
break
;
case
NODE_RESCUE
:
{
int
onerr
,
noexc
,
exend
,
pos1
,
pos2
,
tmp
;
struct
loopinfo
*
lp
;
onerr
=
new_label
(
s
);
genop
(
s
,
MKOP_Bx
(
OP_ONERR
,
0
));
lp
=
loop_push
(
s
,
LOOP_BEGIN
);
lp
->
pc1
=
onerr
;
if
(
tree
->
car
) {
codegen
(
s
,
tree
->
car
,
val
);
}
lp
->
type
=
LOOP_RESCUE
;
noexc
=
new_label
(
s
);
genop
(
s
,
MKOP_Bx
(
OP_JMP
,
0
));
dispatch
(
s
,
onerr
);
tree
=
tree
->
cdr
;
exend
=
0
;
pos1
=
0
;
if
(
tree
->
car
) {
node
*
n2
=
tree
->
car
;
int
exc
=
cursp
();
genop
(
s
,
MKOP_A
(
OP_RESCUE
,
exc
));
push
();
while
(
n2
) {
node
*
n3
=
n2
->
car
;
if
(
pos1
)
dispatch
(
s
,
pos1
);
if
(
n3
->
car
) {
node
*
n4
=
n3
->
car
;
pos2
=
0
;
while
(
n4
) {
codegen
(
s
,
n4
->
car
,
VAL
);
genop
(
s
,
MKOP_AB
(
OP_MOVE
,
cursp
(),
exc
));
push
();
genop
(
s
,
MKOP_A
(
OP_LOADNIL
,
cursp
()));
pop
();
pop
();
genop
(
s
,
MKOP_ABC
(
OP_SEND
,
cursp
(),
new_msym
(
s
,
mrb_intern
(
s
->
mrb
,
"==="
)),
1
));
tmp
=
new_label
(
s
);
genop
(
s
,
MKOP_AsBx
(
OP_JMPIF
,
cursp
(),
pos2
));
pos2
=
tmp
;
n4
=
n4
->
cdr
;
}
pos1
=
new_label
(
s
);
genop
(
s
,
MKOP_Bx
(
OP_JMP
,
0
));
dispatch_linked
(
s
,
pos2
);
}
pop
();
if
(
n3
->
cdr
->
car
) {
gen_assignment
(
s
,
n3
->
cdr
->
car
,
exc
,
NOVAL
);
}
if
(
n3
->
cdr
->
cdr
->
car
) {
codegen
(
s
,
n3
->
cdr
->
cdr
->
car
,
val
);
}
tmp
=
new_label
(
s
);
genop
(
s
,
MKOP_AsBx
(
OP_JMP
,
cursp
(),
exend
));
exend
=
tmp
;
n2
=
n2
->
cdr
;
push
();
}
if
(
pos1
) {
dispatch
(
s
,
pos1
);
genop
(
s
,
MKOP_A
(
OP_RAISE
,
exc
));
}
}
tree
=
tree
->
cdr
;
dispatch
(
s
,
noexc
);
genop
(
s
,
MKOP_A
(
OP_POPERR
,
1
));
if
(
tree
->
car
) {
codegen
(
s
,
tree
->
car
,
val
);
}
dispatch_linked
(
s
,
exend
);
loop_pop
(
s
,
NOVAL
);
}
break
;
case
NODE_ENSURE
:
{
int
idx
;
int
epush
=
s
->
pc
;
genop
(
s
,
MKOP_Bx
(
OP_EPUSH
,
0
));
s
->
ensure_level
++
;
codegen
(
s
,
tree
->
car
,
val
);
idx
=
scope_body
(
s
,
tree
->
cdr
);
s
->
iseq
[
epush
]
=
MKOP_Bx
(
OP_EPUSH
,
idx
);
s
->
ensure_level
--
;
genop_peep
(
s
,
MKOP_A
(
OP_EPOP
,
1
),
NOVAL
);
}
break
;
case
NODE_LAMBDA
:
{
int
idx
=
lambda_body
(
s
,
tree
,
1
);
genop
(
s
,
MKOP_Abc
(
OP_LAMBDA
,
cursp
(),
idx
,
OP_L_LAMBDA
));
push
();
}
break
;
case
NODE_BLOCK
:
{
int
idx
=
lambda_body
(
s
,
tree
,
1
);
genop
(
s
,
MKOP_Abc
(
OP_LAMBDA
,
cursp
(),
idx
,
OP_L_BLOCK
));
push
();
}
break
;
case
NODE_IF
:
{
int
pos1
,
pos2
;
node
*
e
=
tree
->
cdr
->
cdr
->
car
;
codegen
(
s
,
tree
->
car
,
VAL
);
pop
();
pos1
=
new_label
(
s
);
genop
(
s
,
MKOP_AsBx
(
OP_JMPNOT
,
cursp
(),
0
));
codegen
(
s
,
tree
->
cdr
->
car
,
val
);
if
(
e
) {
if
(
val
)
pop
();
pos2
=
new_label
(
s
);
genop
(
s
,
MKOP_sBx
(
OP_JMP
,
0
));
dispatch
(
s
,
pos1
);
codegen
(
s
,
e
,
val
);
dispatch
(
s
,
pos2
);
}
else
{
if
(
val
) {
pop
();
genop
(
s
,
MKOP_A
(
OP_LOADNIL
,
cursp
()));
push
();
}
dispatch
(
s
,
pos1
);
}
}
break
;
case
NODE_AND
:
{
int
pos
;
codegen
(
s
,
tree
->
car
,
VAL
);
pos
=
new_label
(
s
);
pop
();
genop
(
s
,
MKOP_AsBx
(
OP_JMPNOT
,
cursp
(),
0
));
codegen
(
s
,
tree
->
cdr
,
val
);
dispatch
(
s
,
pos
);
}
break
;
case
NODE_OR
:
{
int
pos
;
codegen
(
s
,
tree
->
car
,
VAL
);
pos
=
new_label
(
s
);
pop
();
genop
(
s
,
MKOP_AsBx
(
OP_JMPIF
,
cursp
(),
0
));
codegen
(
s
,
tree
->
cdr
,
val
);
dispatch
(
s
,
pos
);
}
break
;
case
NODE_WHILE
:
{
struct
loopinfo
*
lp
=
loop_push
(
s
,
LOOP_NORMAL
);
lp
->
pc1
=
new_label
(
s
);
codegen
(
s
,
tree
->
car
,
VAL
);
pop
();
lp
->
pc2
=
new_label
(
s
);
genop
(
s
,
MKOP_AsBx
(
OP_JMPNOT
,
cursp
(),
0
));
codegen
(
s
,
tree
->
cdr
,
NOVAL
);
genop
(
s
,
MKOP_sBx
(
OP_JMP
,
lp
->
pc1
-
s
->
pc
));
dispatch
(
s
,
lp
->
pc2
);
loop_pop
(
s
,
val
);
}
break
;
case
NODE_UNTIL
:
{
struct
loopinfo
*
lp
=
loop_push
(
s
,
LOOP_NORMAL
);
lp
->
pc1
=
new_label
(
s
);
codegen
(
s
,
tree
->
car
,
VAL
);
pop
();
lp
->
pc2
=
new_label
(
s
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL