/
imcc.y
2205 lines (1955 loc) · 62.4 KB
/
imcc.y
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
%{
/*
* imcc.y
*
* Intermediate Code Compiler for Parrot.
*
* Copyright (C) 2002 Melvin Smith <melvin.smith@mindspring.com>
* Copyright (C) 2002-2008, The Perl Foundation.
*
* Grammar of the PIR language parser.
*
* $Id$
*
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define _PARSER
#define PARSER_MAIN
#include "imc.h"
#include "parrot/dynext.h"
#include "pbc.h"
#include "parser.h"
#include "optimizer.h"
/* prevent declarations of malloc() and free() in the generated parser. */
#define YYMALLOC
#define YYFREE
#ifndef YYENABLE_NLS
# define YYENABLE_NLS 0
#endif
#ifndef YYLTYPE_IS_TRIVIAL
# define YYLTYPE_IS_TRIVIAL 0
#endif
/* HEADERIZER HFILE: compilers/imcc/imc.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
static void add_pcc_named_arg(PARROT_INTERP,
ARGMOD(SymReg *cur_call),
ARGIN(const char *name),
ARGIN(SymReg *value))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(*cur_call);
static void add_pcc_named_param(PARROT_INTERP,
ARGMOD(SymReg *cur_call),
ARGIN(const char *name),
ARGIN(SymReg *value))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(*cur_call);
static void add_pcc_named_result(PARROT_INTERP,
ARGMOD(SymReg *cur_call),
ARGIN(const char *name),
ARGIN(SymReg *value))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(*cur_call);
static void add_pcc_named_return(PARROT_INTERP,
ARGMOD(SymReg *cur_call),
ARGIN(const char *name),
ARGIN(SymReg *value))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(*cur_call);
static void adv_named_set(PARROT_INTERP, ARGIN(char *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void begin_return_or_yield(PARROT_INTERP, int yield)
__attribute__nonnull__(1);
static void clear_state(PARROT_INTERP)
__attribute__nonnull__(1);
static void do_loadlib(PARROT_INTERP, ARGIN(const char *lib))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction* func_ins(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(SymReg *lhs),
ARGIN(const char *op),
ARGMOD(SymReg **r),
int n,
int keyv,
int emit)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
__attribute__nonnull__(5)
FUNC_MODIFIES(*unit)
FUNC_MODIFIES(*r);
PARROT_CAN_RETURN_NULL
static Instruction * iINDEXFETCH(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(SymReg *r0),
ARGIN(SymReg *r1),
ARGIN(SymReg *r2))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
__attribute__nonnull__(5)
FUNC_MODIFIES(*unit);
PARROT_CAN_RETURN_NULL
static Instruction * iINDEXSET(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(SymReg *r0),
ARGIN(SymReg *r1),
ARGIN(SymReg *r2))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
__attribute__nonnull__(5)
FUNC_MODIFIES(*unit);
PARROT_CANNOT_RETURN_NULL
static Instruction * iLABEL(PARROT_INTERP,
ARGMOD_NULLOK(IMC_Unit *unit),
ARGMOD(SymReg *r0))
__attribute__nonnull__(1)
__attribute__nonnull__(3)
FUNC_MODIFIES(*unit)
FUNC_MODIFIES(*r0);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static const char * inv_op(ARGIN(const char *op))
__attribute__nonnull__(1);
PARROT_CANNOT_RETURN_NULL
static Instruction * iSUBROUTINE(PARROT_INTERP,
ARGMOD_NULLOK(IMC_Unit *unit),
ARGMOD(SymReg *r))
__attribute__nonnull__(1)
__attribute__nonnull__(3)
FUNC_MODIFIES(*unit)
FUNC_MODIFIES(*r);
PARROT_IGNORABLE_RESULT
PARROT_CAN_RETURN_NULL
static Instruction * MK_I(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(const char *fmt),
int n,
...)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*unit);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction* mk_pmc_const(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(const char *type),
ARGMOD(SymReg *left),
ARGIN(const char *constant))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
__attribute__nonnull__(5)
FUNC_MODIFIES(*unit)
FUNC_MODIFIES(*left);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction* mk_pmc_const_named(PARROT_INTERP,
ARGMOD(IMC_Unit *unit),
ARGIN(const char *name),
ARGMOD(SymReg *left),
ARGIN(const char *constant))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
__attribute__nonnull__(5)
FUNC_MODIFIES(*unit)
FUNC_MODIFIES(*left);
PARROT_CANNOT_RETURN_NULL
static SymReg * mk_sub_address_fromc(PARROT_INTERP, ARGIN(const char *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_CANNOT_RETURN_NULL
static SymReg * mk_sub_address_u(PARROT_INTERP, ARGIN(const char *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void set_lexical(PARROT_INTERP,
ARGMOD(SymReg *r),
ARGIN(const char *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*r);
#define ASSERT_ARGS_add_pcc_named_arg __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(cur_call) \
|| PARROT_ASSERT_ARG(name) \
|| PARROT_ASSERT_ARG(value)
#define ASSERT_ARGS_add_pcc_named_param __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(cur_call) \
|| PARROT_ASSERT_ARG(name) \
|| PARROT_ASSERT_ARG(value)
#define ASSERT_ARGS_add_pcc_named_result __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(cur_call) \
|| PARROT_ASSERT_ARG(name) \
|| PARROT_ASSERT_ARG(value)
#define ASSERT_ARGS_add_pcc_named_return __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(cur_call) \
|| PARROT_ASSERT_ARG(name) \
|| PARROT_ASSERT_ARG(value)
#define ASSERT_ARGS_adv_named_set __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(name)
#define ASSERT_ARGS_begin_return_or_yield __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp)
#define ASSERT_ARGS_clear_state __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp)
#define ASSERT_ARGS_do_loadlib __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(lib)
#define ASSERT_ARGS_func_ins __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(lhs) \
|| PARROT_ASSERT_ARG(op) \
|| PARROT_ASSERT_ARG(r)
#define ASSERT_ARGS_iINDEXFETCH __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(r0) \
|| PARROT_ASSERT_ARG(r1) \
|| PARROT_ASSERT_ARG(r2)
#define ASSERT_ARGS_iINDEXSET __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(r0) \
|| PARROT_ASSERT_ARG(r1) \
|| PARROT_ASSERT_ARG(r2)
#define ASSERT_ARGS_iLABEL __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(r0)
#define ASSERT_ARGS_inv_op __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(op)
#define ASSERT_ARGS_iSUBROUTINE __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(r)
#define ASSERT_ARGS_MK_I __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(fmt)
#define ASSERT_ARGS_mk_pmc_const __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(type) \
|| PARROT_ASSERT_ARG(left) \
|| PARROT_ASSERT_ARG(constant)
#define ASSERT_ARGS_mk_pmc_const_named __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(unit) \
|| PARROT_ASSERT_ARG(name) \
|| PARROT_ASSERT_ARG(left) \
|| PARROT_ASSERT_ARG(constant)
#define ASSERT_ARGS_mk_sub_address_fromc __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(name)
#define ASSERT_ARGS_mk_sub_address_u __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(name)
#define ASSERT_ARGS_set_lexical __attribute__unused__ int _ASSERT_ARGS_CHECK = \
PARROT_ASSERT_ARG(interp) \
|| PARROT_ASSERT_ARG(r) \
|| PARROT_ASSERT_ARG(name)
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
/*
* We use a pure parser with the interpreter as a parameter. However this still
* doesn't make the parser reentrant, as there are too many globals around.
*/
/*
* Choosing instructions for Parrot is pretty easy since many are
* polymorphic.
*/
/*
* MK_I: build and emitb instruction by INS
*
* fmt may contain:
* op %s, %s # comment
* or just
* op
*
*
* NOTE: Most usage of this function is with
* IMCC_INFO(interp)->cur_unit, but there are some
* exceptions. Thus, we can't easily factorize that piece of
* code.
*/
PARROT_IGNORABLE_RESULT
PARROT_CAN_RETURN_NULL
static Instruction *
MK_I(PARROT_INTERP, ARGMOD(IMC_Unit *unit), ARGIN(const char *fmt), int n, ...)
{
ASSERT_ARGS(MK_I)
char opname[64];
char *p;
const char *q;
va_list ap;
SymReg *r[IMCC_MAX_FIX_REGS];
int i;
for (p = opname, q = fmt; *q && *q != ' ';)
*p++ = *q++;
*p = 0;
if (!*q)
fmt = NULL;
else
fmt = ++q;
#ifdef OPDEBUG
fprintf(stderr, "op '%s' format '%s' (%d)\n", opname, fmt?:"", n);
#endif
va_start(ap, n);
i = 0;
for (i = 0; i < n; ++i) {
r[i] = va_arg(ap, SymReg *);
}
va_end(ap);
return INS(interp, unit, opname, fmt, r, n,
IMCC_INFO(interp)->keyvec, 1);
}
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction*
mk_pmc_const(PARROT_INTERP, ARGMOD(IMC_Unit *unit), ARGIN(const char *type),
ARGMOD(SymReg *left), ARGIN(const char *constant))
{
ASSERT_ARGS(mk_pmc_const)
const int type_enum = atoi(type);
const int ascii = (*constant == '\'' || *constant == '"');
SymReg *rhs;
SymReg *r[3];
char *name;
if (left->type == VTADDRESS) { /* IDENTIFIER */
if (IMCC_INFO(interp)->state->pasm_file) {
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"Ident as PMC constant",
" %s\n", left->name);
}
left->type = VTIDENTIFIER;
left->set = 'P';
}
r[0] = left;
if (ascii) {
/* strip delimiters */
name = str_dup(constant + 1);
name[strlen(name) - 1] = '\0';
}
else {
name = str_dup(constant);
}
switch (type_enum) {
case enum_class_Sub:
case enum_class_Coroutine:
rhs = mk_const(interp, name, 'p');
if (!ascii)
rhs->type |= VT_ENCODED;
rhs->usage = U_FIXUP | U_SUBID_LOOKUP;;
break;
default:
rhs = mk_const(interp, name, 'P');
break;
}
r[1] = rhs;
rhs->pmc_type = type_enum;
mem_sys_free(name);
return INS(interp, unit, "set_p_pc", "", r, 2, 0, 1);
}
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction*
mk_pmc_const_named(PARROT_INTERP, ARGMOD(IMC_Unit *unit),
ARGIN(const char *name), ARGMOD(SymReg *left), ARGIN(const char *constant))
{
ASSERT_ARGS(mk_pmc_const_named)
SymReg *rhs;
SymReg *r[3];
char *const_name;
const int ascii = (*constant == '\'' || *constant == '"');
char *unquoted_name = str_dup(name + 1);
size_t name_length = strlen(unquoted_name) - 1;
unquoted_name[name_length] = '\0';
if (left->type == VTADDRESS) { /* IDENTIFIER */
if (IMCC_INFO(interp)->state->pasm_file) {
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"Ident as PMC constant",
" %s\n", left->name);
}
left->type = VTIDENTIFIER;
left->set = 'P';
}
r[0] = left;
if (ascii) {
/* strip delimiters */
const_name = str_dup(constant + 1);
const_name[strlen(const_name) - 1] = '\0';
}
else {
const_name = str_dup(constant);
}
if ((strncmp(unquoted_name, "Sub", name_length) == 0)
|| (strncmp(unquoted_name, "Coroutine", name_length) == 0)) {
rhs = mk_const(interp, const_name, 'p');
if (!ascii)
rhs->type |= VT_ENCODED;
rhs->usage = U_FIXUP | U_SUBID_LOOKUP;
}
else {
rhs = mk_const(interp, const_name, 'P');
}
r[1] = rhs;
rhs->pmc_type = pmc_type(interp,
string_from_cstring(interp, unquoted_name, name_length));
mem_sys_free(unquoted_name);
mem_sys_free(const_name);
return INS(interp, unit, "set_p_pc", "", r, 2, 0, 1);
}
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static Instruction*
func_ins(PARROT_INTERP, ARGMOD(IMC_Unit *unit), ARGIN(SymReg *lhs), ARGIN(const char *op),
ARGMOD(SymReg **r), int n, int keyv, int emit)
{
ASSERT_ARGS(func_ins)
int i;
/* shift regs up by 1 */
for (i = n - 1; i >= 0; --i)
r[i+1] = r[i];
r[0] = lhs;
/* shift keyvec */
keyv <<= 1;
return INS(interp, unit, op, "", r, n+1, keyv, emit);
}
/*
* special instructions
*
* labels and such
*/
static void
clear_state(PARROT_INTERP)
{
ASSERT_ARGS(clear_state)
IMCC_INFO(interp) -> nargs = 0;
IMCC_INFO(interp) -> keyvec = 0;
}
PARROT_CANNOT_RETURN_NULL
Instruction *
INS_LABEL(PARROT_INTERP, ARGMOD_NULLOK(IMC_Unit *unit), ARGMOD(SymReg *r0), int emit)
{
ASSERT_ARGS(INS_LABEL)
Instruction * const ins = _mk_instruction("", "%s:", 1, &r0, 0);
ins->type = ITLABEL;
r0->first_ins = ins;
if (emit)
emitb(interp, unit, ins);
return ins;
}
PARROT_CANNOT_RETURN_NULL
static Instruction *
iLABEL(PARROT_INTERP, ARGMOD_NULLOK(IMC_Unit *unit), ARGMOD(SymReg *r0))
{
ASSERT_ARGS(iLABEL)
Instruction * const i = INS_LABEL(interp, unit, r0, 1);
i->line = IMCC_INFO(interp)->line;
clear_state(interp);
return i;
}
PARROT_CANNOT_RETURN_NULL
static Instruction *
iSUBROUTINE(PARROT_INTERP, ARGMOD_NULLOK(IMC_Unit *unit), ARGMOD(SymReg *r))
{
ASSERT_ARGS(iSUBROUTINE)
Instruction * const i = iLABEL(interp, unit, r);
r->type = (r->type & VT_ENCODED) ? VT_PCC_SUB|VT_ENCODED : VT_PCC_SUB;
r->pcc_sub = mem_allocate_zeroed_typed(pcc_sub_t);
IMCC_INFO(interp)->cur_call = r;
i->line = IMCC_INFO(interp)->line;
add_namespace(interp, unit);
return i;
}
/*
* substr or X = P[key]
*/
PARROT_CAN_RETURN_NULL
static Instruction *
iINDEXFETCH(PARROT_INTERP, ARGMOD(IMC_Unit *unit), ARGIN(SymReg *r0), ARGIN(SymReg *r1),
ARGIN(SymReg *r2))
{
ASSERT_ARGS(iINDEXFETCH)
IMCC_INFO(interp) -> keyvec |= KEY_BIT(2);
return MK_I(interp, unit, "set %s, %s[%s]", 3, r0, r1, r2);
}
/*
* substr or P[key] = X
*/
PARROT_CAN_RETURN_NULL
static Instruction *
iINDEXSET(PARROT_INTERP, ARGMOD(IMC_Unit *unit), ARGIN(SymReg *r0), ARGIN(SymReg *r1),
ARGIN(SymReg *r2))
{
ASSERT_ARGS(iINDEXSET)
if (r0->set == 'P') {
IMCC_INFO(interp)->keyvec |= KEY_BIT(1);
MK_I(interp, unit, "set %s[%s], %s", 3, r0, r1, r2);
}
else
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"unsupported indexed set op\n");
return NULL;
}
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static const char *
inv_op(ARGIN(const char *op))
{
ASSERT_ARGS(inv_op)
int n;
return get_neg_op(op, &n);
}
PARROT_CANNOT_RETURN_NULL
Instruction *
IMCC_create_itcall_label(PARROT_INTERP)
{
ASSERT_ARGS(IMCC_create_itcall_label)
char name[128];
SymReg *r;
Instruction *i;
snprintf(name, sizeof (name), "%cpcc_sub_call_%d", IMCC_INTERNAL_CHAR,
IMCC_INFO(interp)->cnr++);
r = mk_pcc_sub(interp, name, 0);
i = iLABEL(interp, IMCC_INFO(interp)->cur_unit, r);
i->type = ITCALL | ITPCCSUB;
IMCC_INFO(interp)->cur_call = r;
return i;
}
PARROT_CANNOT_RETURN_NULL
static SymReg *
mk_sub_address_fromc(PARROT_INTERP, ARGIN(const char *name))
{
ASSERT_ARGS(mk_sub_address_fromc)
/* name is a quoted sub name */
SymReg *r;
char *name_copy = str_dup(name + 1);
name_copy[strlen(name_copy) - 1] = '\0';
r = mk_sub_address(interp, name_copy);
mem_sys_free(name_copy);
return r;
}
PARROT_CANNOT_RETURN_NULL
static SymReg *
mk_sub_address_u(PARROT_INTERP, ARGIN(const char *name))
{
ASSERT_ARGS(mk_sub_address_u)
SymReg * const r = mk_sub_address(interp, name);
r->type |= VT_ENCODED;
return r;
}
void
IMCC_itcall_sub(PARROT_INTERP, ARGIN(SymReg *sub))
{
ASSERT_ARGS(IMCC_itcall_sub)
IMCC_INFO(interp)->cur_call->pcc_sub->sub = sub;
if (IMCC_INFO(interp)->cur_obj) {
if (IMCC_INFO(interp)->cur_obj->set != 'P')
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR, "object isn't a PMC");
IMCC_INFO(interp)->cur_call->pcc_sub->object = IMCC_INFO(interp)->cur_obj;
IMCC_INFO(interp)->cur_obj = NULL;
}
if (IMCC_INFO(interp)->cur_call->pcc_sub->sub->pmc_type == enum_class_NCI)
IMCC_INFO(interp)->cur_call->pcc_sub->flags |= isNCI;
if (IMCC_INFO(interp)->cur_unit->type == IMC_PCCSUB)
IMCC_INFO(interp)->cur_unit->instructions->symregs[0]->pcc_sub->calls_a_sub |= 1;
}
static void
begin_return_or_yield(PARROT_INTERP, int yield)
{
ASSERT_ARGS(begin_return_or_yield)
Instruction *i;
Instruction * const ins = IMCC_INFO(interp)->cur_unit->instructions;
char name[128];
if (!ins || !ins->symregs[0] || !(ins->symregs[0]->type & VT_PCC_SUB))
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"yield or return directive outside pcc subroutine\n");
if (yield)
ins->symregs[0]->pcc_sub->calls_a_sub = 1 | ITPCCYIELD;
snprintf(name, sizeof (name), yield ? "%cpcc_sub_yield_%d" : "%cpcc_sub_ret_%d",
IMCC_INTERNAL_CHAR, IMCC_INFO(interp)->cnr++);
interp->imc_info->sr_return = mk_pcc_sub(interp, name, 0);
i = iLABEL(interp, IMCC_INFO(interp)->cur_unit, interp->imc_info->sr_return);
i->type = yield ? ITPCCSUB | ITLABEL | ITPCCYIELD : ITPCCSUB | ITLABEL ;
interp->imc_info->asm_state = yield ? AsmInYield : AsmInReturn;
}
static void
set_lexical(PARROT_INTERP, ARGMOD(SymReg *r), ARGIN(const char *name))
{
ASSERT_ARGS(set_lexical)
SymReg * const n = mk_const(interp, name, 'S');
r->usage |= U_LEXICAL;
if (n == r->reg)
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"register %s already declared as lexical %s", r->name, n->name);
/* chain all names in r->reg */
n->reg = r->reg;
r->reg = n;
r->use_count++;
}
static void
add_pcc_named_arg(PARROT_INTERP, ARGMOD(SymReg *cur_call), ARGIN(const char *name),
ARGIN(SymReg *value))
{
ASSERT_ARGS(add_pcc_named_arg)
SymReg * const r = mk_const(interp, name, 'S');
r->type |= VT_NAMED;
add_pcc_arg(cur_call, r);
add_pcc_arg(cur_call, value);
}
static void
add_pcc_named_result(PARROT_INTERP, ARGMOD(SymReg *cur_call), ARGIN(const char *name),
ARGIN(SymReg *value))
{
ASSERT_ARGS(add_pcc_named_result)
SymReg * const r = mk_const(interp, name, 'S');
r->type |= VT_NAMED;
add_pcc_result(cur_call, r);
add_pcc_result(cur_call, value);
}
static void
add_pcc_named_param(PARROT_INTERP, ARGMOD(SymReg *cur_call), ARGIN(const char *name),
ARGIN(SymReg *value))
{
ASSERT_ARGS(add_pcc_named_param)
SymReg * const r = mk_const(interp, name, 'S');
r->type |= VT_NAMED;
add_pcc_arg(cur_call, r);
add_pcc_arg(cur_call, value);
}
static void
add_pcc_named_return(PARROT_INTERP, ARGMOD(SymReg *cur_call), ARGIN(const char *name),
ARGIN(SymReg *value))
{
ASSERT_ARGS(add_pcc_named_return)
SymReg * const r = mk_const(interp, name, 'S');
r->type |= VT_NAMED;
add_pcc_result(cur_call, r);
add_pcc_result(cur_call, value);
}
/* XXX Can name be consted? */
static void
adv_named_set(PARROT_INTERP, ARGIN(char *name))
{
ASSERT_ARGS(adv_named_set)
if (IMCC_INFO(interp)->adv_named_id) {
IMCC_fataly(interp, EXCEPTION_SYNTAX_ERROR,
"Named parameter with more than one name.\n");
}
IMCC_INFO(interp)->adv_named_id = name;
}
static void
do_loadlib(PARROT_INTERP, ARGIN(const char *lib))
{
ASSERT_ARGS(do_loadlib)
STRING * const s = string_unescape_cstring(interp, lib + 1, '"', NULL);
PMC *ignored = Parrot_load_lib(interp, s, NULL);
UNUSED(ignored);
Parrot_register_HLL_lib(interp, s);
}
/* HEADERIZER STOP */
%}
%union {
IdList * idlist;
int t;
char * s;
SymReg * sr;
Instruction *i;
}
/* We need precedence for a few tokens to resolve a couple of conflicts */
%nonassoc LOW_PREC
%nonassoc '\n'
%nonassoc <t> PARAM
%token <t> HLL HLL_MAP TK_LINE TK_FILE
%token <t> GOTO ARG IF UNLESS PNULL SET_RETURN SET_YIELD
%token <t> ADV_FLAT ADV_SLURPY ADV_OPTIONAL ADV_OPT_FLAG ADV_NAMED ADV_ARROW
%token <t> NEW ADV_INVOCANT
%token <t> NAMESPACE DOT_METHOD
%token <t> SUB SYM LOCAL LEXICAL CONST ANNOTATE
%token <t> INC DEC GLOBAL_CONST
%token <t> PLUS_ASSIGN MINUS_ASSIGN MUL_ASSIGN DIV_ASSIGN CONCAT_ASSIGN
%token <t> BAND_ASSIGN BOR_ASSIGN BXOR_ASSIGN FDIV FDIV_ASSIGN MOD_ASSIGN
%token <t> SHR_ASSIGN SHL_ASSIGN SHR_U_ASSIGN
%token <t> SHIFT_LEFT SHIFT_RIGHT INTV FLOATV STRINGV PMCV LOG_XOR
%token <t> RELOP_EQ RELOP_NE RELOP_GT RELOP_GTE RELOP_LT RELOP_LTE
%token <t> RESULT RETURN TAILCALL YIELDT GET_RESULTS
%token <t> POW SHIFT_RIGHT_U LOG_AND LOG_OR
%token <t> COMMA ESUB DOTDOT
%token <t> PCC_BEGIN PCC_END PCC_CALL PCC_SUB PCC_BEGIN_RETURN PCC_END_RETURN
%token <t> PCC_BEGIN_YIELD PCC_END_YIELD NCI_CALL METH_CALL INVOCANT
%token <t> MAIN LOAD INIT IMMEDIATE POSTCOMP METHOD ANON OUTER NEED_LEX
%token <t> MULTI VTABLE_METHOD LOADLIB SUB_INSTANCE_OF SUBID
%token <t> NS_ENTRY
%token <t> UNIQUE_REG
%token <s> LABEL
%token <t> EMIT EOM
%token <s> IREG NREG SREG PREG IDENTIFIER REG MACRO ENDM
%token <s> STRINGC INTC FLOATC USTRINGC
%token <s> PARROT_OP
%type <t> type hll_def return_or_yield comma_or_goto opt_unique_reg
%type <i> program
%type <i> class_namespace
%type <i> constdef sub emit pcc_ret pcc_yield
%type <i> compilation_units compilation_unit pmc_const pragma
%type <s> classname relop any_string assign_op bin_op un_op
%type <i> labels _labels label statement sub_call
%type <i> pcc_sub_call
%type <sr> sub_param sub_params pcc_arg pcc_result pcc_args pcc_results sub_param_type_def
%type <sr> pcc_returns pcc_yields pcc_return pcc_call arg arglist the_sub multi_type
%type <t> argtype_list argtype paramtype_list paramtype
%type <t> pcc_return_many
%type <t> proto sub_proto sub_proto_list multi multi_types outer
%type <t> vtable instanceof subid
%type <t> method ns_entry_name
%type <i> instruction assignment conditional_statement labeled_inst opt_label op_assign
%type <i> if_statement unless_statement
%type <i> func_assign get_results
%type <i> opt_invocant
%type <i> annotate_directive
%type <sr> target targetlist reg const var result pcc_set_yield
%type <sr> keylist keylist_force _keylist key maybe_ns
%type <sr> vars _vars var_or_i _var_or_i label_op sub_label_op sub_label_op_c
%type <i> pasmcode pasmline pasm_inst
%type <sr> pasm_args
%type <i> var_returns
%token <sr> VAR
%token <t> LINECOMMENT
%token <s> FILECOMMENT
%type <idlist> id_list id_list_id
%nonassoc CONCAT DOT
/* %locations */
%pure_parser
/* Note that we pass interp last, because Bison only passes
the last param to yyerror(). (Tested on bison <= 2.3)
*/
%parse-param {void *yyscanner}
%lex-param {void *yyscanner}
%parse-param {Parrot_Interp interp}
%lex-param {Parrot_Interp interp}
%start program
/* In effort to make the grammar readable but not militaristic, please space indent
code blocks on 10 col boundaries and keep indentation same for all code blocks
in a rule. Indent rule tokens | and ; to 4th col and sub rules 6th col
*/
%%
program:
compilation_units { if (yynerrs) YYABORT; $$ = 0; }
;
compilation_units:
compilation_unit
| compilation_units compilation_unit
;
compilation_unit:
class_namespace { $$ = $1; }
| constdef { $$ = $1; }
| sub
{
$$ = $1;
imc_close_unit(interp, IMCC_INFO(interp)->cur_unit);
IMCC_INFO(interp)->cur_unit = 0;
}
| emit
{
$$ = $1;
imc_close_unit(interp, IMCC_INFO(interp)->cur_unit);
IMCC_INFO(interp)->cur_unit = 0;
}
| MACRO '\n' { $$ = 0; }
| pragma { $$ = 0; }
| location_directive { $$ = 0; }
| '\n' { $$ = 0; }
;
pragma:
hll_def '\n' { $$ = 0; }
| LOADLIB STRINGC '\n'
{
$$ = 0;
do_loadlib(interp, $2);
mem_sys_free($2);
}
;
location_directive:
TK_LINE INTC COMMA STRINGC '\n'
{
IMCC_INFO(interp)->line = atoi($2);
set_filename(interp, $4);
}
| TK_FILE STRINGC '\n'
{
set_filename(interp, $2);
}
;
annotate_directive:
ANNOTATE STRINGC COMMA const
{
/* We'll want to store an entry while emitting instructions, so just
* store annotation like it's an instruction. */
SymReg * const key = mk_const(interp, $2, 'S');
$$ = MK_I(interp, IMCC_INFO(interp)->cur_unit, ".annotate", 2, key, $4);
mem_sys_free($2);
}
;
hll_def:
HLL STRINGC
{
STRING * const hll_name = string_unescape_cstring(interp, $2 + 1, '"', NULL);
CONTEXT(interp)->current_HLL =
Parrot_register_HLL(interp, hll_name);
IMCC_INFO(interp)->cur_namespace = NULL;
$$ = 0;
}
| HLL_MAP STRINGC '=' STRINGC
{
Parrot_Context *ctx = CONTEXT(interp);
STRING * const built_in_name =
string_unescape_cstring(interp, $2 + 1, '"', NULL);
STRING * const language_name =
string_unescape_cstring(interp, $4 + 1, '"', NULL);
int built_in_type = pmc_type(interp, built_in_name);
int language_type = pmc_type(interp, language_name);
Parrot_register_HLL_type(interp, ctx->current_HLL,
built_in_type, language_type);
$$ = 0;
}
;
constdef:
CONST { is_def = 1; } type IDENTIFIER '=' const
{
mk_const_ident(interp, $4, $3, $6, 1);
mem_sys_free($4);
is_def = 0;
}
;
pmc_const:
CONST { is_def=1; } INTC var_or_i '=' any_string
{
$$ = mk_pmc_const(interp, IMCC_INFO(interp)->cur_unit, $3, $4, $6);
is_def = 0;
}
| CONST { is_def=1; } STRINGC var_or_i '=' any_string
{
$$ = mk_pmc_const_named(interp, IMCC_INFO(interp)->cur_unit, $3, $4, $6);
is_def = 0;
}
;
any_string:
STRINGC
| USTRINGC
;
pasmcode:
pasmline
| pasmcode pasmline
;