-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr.c
2417 lines (2290 loc) · 95.6 KB
/
lr.c
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
/*
Copyright (C) 1997-2016 Vladimir Makarov.
Written by Vladimir Makarov <[email protected]>
This file is part of the tool MSTA.
This is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* #ifdef HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include "vlobject.h"
#include "hashtab.h"
#include "ticker.h"
#include "common.h"
#include "ird.h"
#include "gen-comm.h"
#include "contexts.h"
#include "relation.h"
#include "lr-sets.h"
#include "lr.h"
#include <assert.h>
static void
insert_LR_situation_context (IR_node_t LR_situation)
{
if (IR_context (LR_situation) == NULL)
return;
IR_set_context (LR_situation,
insert_or_free_context (IR_context (LR_situation)));
}
static void
insert_LR_set_contexts (IR_node_t LR_set)
{
IR_node_t current_LR_situation;
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation = IR_next_LR_situation (current_LR_situation))
insert_LR_situation_context (current_LR_situation);
}
static void
free_unimportant_LR_situations (IR_node_t LR_set)
{
IR_node_t current_LR_situation;
IR_node_t last_important_LR_situation;
current_LR_situation = IR_LR_situation_list (LR_set);
do
{
if (IR_next_symbol_LR_situation (current_LR_situation) != NULL
&& !IR_important_LR_situation_flag (IR_next_symbol_LR_situation
(current_LR_situation)))
IR_set_next_symbol_LR_situation (current_LR_situation, NULL);
last_important_LR_situation = current_LR_situation;
current_LR_situation = IR_next_LR_situation (current_LR_situation);
}
while (current_LR_situation != NULL
&& IR_important_LR_situation_flag (current_LR_situation));
free_LR_situations_list (IR_next_LR_situation (last_important_LR_situation));
IR_set_next_LR_situation (last_important_LR_situation, NULL);
}
static void
add_LR_core_to_cyclic_list (IR_node_t LR_core)
{
IR_set_next_LR_core (IR_LR_core_list (description), LR_core);
IR_set_LR_core_list (description, LR_core);
}
static void
add_LR_set_to_cyclic_list (IR_node_t LR_set, IR_node_t LR_core)
{
if (IR_LR_set_list (LR_core) == NULL)
{
/* New LR-core. */
IR_set_next_LR_set (LR_set, LR_set);
IR_set_LR_set_list (LR_core, LR_set);
}
else
{
IR_set_next_LR_set (LR_set, IR_next_LR_set (IR_LR_set_list (LR_core)));
IR_set_next_LR_set (IR_LR_set_list (LR_core), LR_set);
IR_set_LR_set_list (LR_core, LR_set);
}
}
static int
compare_LR_situations_order (const void *LR_situation_1_ptr,
const void *LR_situation_2_ptr)
{
IR_node_t canonical_rule_1;
IR_node_t canonical_rule_2;
IR_node_t element_after_dot_1;
IR_node_t element_after_dot_2;
int prefix_length_1;
int prefix_length_2;
element_after_dot_1
= IR_element_after_dot (* (IR_node_t *) LR_situation_1_ptr);
element_after_dot_2
= IR_element_after_dot (*(IR_node_t *) LR_situation_2_ptr);
assert (element_after_dot_1 != element_after_dot_2);
canonical_rule_1 = IR_canonical_rule (element_after_dot_1);
canonical_rule_2 = IR_canonical_rule (element_after_dot_2);
prefix_length_1
= canonical_rule_right_hand_side_prefix_length (canonical_rule_1,
element_after_dot_1);
prefix_length_2
= canonical_rule_right_hand_side_prefix_length (canonical_rule_2,
element_after_dot_2);
if (prefix_length_1 < prefix_length_2)
return 1;
else if (prefix_length_1 > prefix_length_2)
return (-1);
else if (IR_canonical_rule_order_number (canonical_rule_1)
< IR_canonical_rule_order_number (canonical_rule_2))
return -1;
else
{
assert (IR_canonical_rule_order_number (canonical_rule_1)
!= IR_canonical_rule_order_number (canonical_rule_2));
return 1;
}
}
static IR_node_t
get_shifted_LR_situation_list (IR_node_t first_symbol_LR_situation)
{
IR_node_t current_new_LR_situation;
IR_node_t first_new_LR_situation;
IR_node_t last_new_LR_situation;
IR_node_t current_symbol_LR_situation;
vlo_t situations;
IR_node_t *LR_situation_ptr;
assert (IR_first_symbol_LR_situation (first_symbol_LR_situation)
&& !IR_IS_OF_TYPE (IR_element_after_dot (first_symbol_LR_situation),
IR_NM_canonical_rule_end));
VLO_CREATE (situations, 100);
for (current_symbol_LR_situation = first_symbol_LR_situation;
current_symbol_LR_situation != NULL;
current_symbol_LR_situation
= IR_next_symbol_LR_situation (current_symbol_LR_situation))
{
current_new_LR_situation
= get_new_LR_situation
(IR_next_right_hand_side_element
(IR_element_after_dot (current_symbol_LR_situation)),
IR_context (current_symbol_LR_situation), NULL, NULL, TRUE);
#if 0
if (IR_cp_flag (IR_element_after_dot (current_symbol_LR_situation)))
IR_set_under_control_point_flag (current_symbol_LR_situation, TRUE);
#endif
VLO_ADD_MEMORY (situations, ¤t_new_LR_situation,
sizeof (IR_node_t));
}
qsort (VLO_BEGIN (situations), VLO_LENGTH (situations) / sizeof (IR_node_t),
sizeof (IR_node_t), compare_LR_situations_order);
last_new_LR_situation = first_new_LR_situation = NULL;
for (LR_situation_ptr = VLO_BEGIN (situations);
LR_situation_ptr <= (IR_node_t *) VLO_END (situations);
LR_situation_ptr++)
{
if (last_new_LR_situation != NULL)
IR_set_next_LR_situation
(last_new_LR_situation, *LR_situation_ptr);
last_new_LR_situation = *LR_situation_ptr;
if (first_new_LR_situation == NULL)
first_new_LR_situation = last_new_LR_situation;
}
VLO_DELETE (situations);
return first_new_LR_situation;
}
/* Given LR-set must be full and contexts of given LR-set must be in
the table. */
static void
set_up_goto_LR_set_fields (IR_node_t LR_set, int any_LR_core_set_flag)
{
IR_node_t current_LR_situation;
IR_node_t first_new_LR_situation;
IR_node_t goto_LR_set;
IR_node_t LR_core;
/* Set up fields `goto_LR_set'. */
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation = IR_next_LR_situation (current_LR_situation))
if (IR_first_symbol_LR_situation (current_LR_situation)
&& !IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
IR_NM_canonical_rule_end))
{
first_new_LR_situation
= get_shifted_LR_situation_list (current_LR_situation);
LR_core = find_LR_core (first_new_LR_situation);
assert (LR_core != NULL);
/* This is new LR-core. */
if (any_LR_core_set_flag)
/* Any LR-set of the LR-core. */
goto_LR_set = IR_LR_set_list (LR_core);
else
goto_LR_set = find_LR_set (LR_core, first_new_LR_situation);
/* The LR-set can be found by `find_LR_set' after merging
LR-sets of a LR-core on LALR-optimization. */
if (goto_LR_set == NULL
&& IR_next_LR_set (IR_LR_set_list (LR_core)) == NULL)
goto_LR_set = IR_LR_set_list (LR_core);
assert (goto_LR_set != NULL);
free_LR_situations_list (first_new_LR_situation);
/* Set up field `goto_LR_set'. */
IR_set_goto_LR_set (current_LR_situation, goto_LR_set);
}
}
static void
make_full_LR_set (IR_node_t LR_set, int with_context_flag)
{
IR_node_t current_LR_situation;
IR_node_t last_LR_situation;
vlo_t LR_situations_stack;
IR_node_t single_definition;
IR_node_t current_canonical_rule;
context_t context;
IR_node_t last_reduce_LR_situation_processed;
int process_flag;
int under_control_point_flag;
context_t new_context;
VLO_CREATE (LR_situations_stack, 1000);
assert (IR_LR_situation_list (LR_set) != NULL);
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation = IR_next_LR_situation (current_LR_situation))
{
assert (IR_important_LR_situation_flag (current_LR_situation));
VLO_ADD_MEMORY (LR_situations_stack, ¤t_LR_situation,
sizeof (IR_node_t));
last_LR_situation = current_LR_situation;
}
while (VLO_LENGTH (LR_situations_stack) != 0)
{
current_LR_situation
= ((IR_node_t *) VLO_BEGIN (LR_situations_stack))
[VLO_LENGTH (LR_situations_stack) / sizeof (IR_node_t) - 1];
VLO_SHORTEN (LR_situations_stack, sizeof (IR_node_t));
if (IR_cp_flag (IR_element_after_dot (current_LR_situation)))
IR_set_under_control_point_flag (current_LR_situation, TRUE);
under_control_point_flag
= IR_under_control_point_flag (current_LR_situation);
if (!IR_important_LR_situation_flag (current_LR_situation))
IR_set_situation_in_stack_flag (current_LR_situation, FALSE);
/* Process the situation */
if (IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
IR_NM_canonical_rule_end))
continue;
single_definition = IR_element_itself (IR_element_after_dot
(current_LR_situation));
if (!IR_IS_OF_TYPE (single_definition, IR_NM_single_nonterm_definition))
continue;
if (with_context_flag)
context = FIRST_of_tail (IR_next_right_hand_side_element
(IR_element_after_dot (current_LR_situation)),
IR_context (current_LR_situation),
max_look_ahead_number);
for (current_canonical_rule
= IR_nonterm_canonical_rule_list (single_definition);
current_canonical_rule != NULL;
current_canonical_rule
= IR_next_nonterm_canonical_rule (current_canonical_rule))
{
process_flag = FALSE;
if (IR_canonical_rule_LR_situation (current_canonical_rule) != NULL)
{
if (with_context_flag)
{
current_LR_situation
= IR_canonical_rule_LR_situation (current_canonical_rule);
process_flag
= !context_in (context, IR_context (current_LR_situation));
if (process_flag)
context_or (IR_context (current_LR_situation), context);
}
if (!under_control_point_flag)
{
process_flag
= (process_flag
|| IR_under_control_point_flag (current_LR_situation));
IR_set_under_control_point_flag (current_LR_situation,
FALSE);
}
}
else
{
if (with_context_flag)
{
new_context = get_null_context ();
context_copy (new_context, context);
}
current_LR_situation
= get_new_LR_situation
(IR_right_hand_side (current_canonical_rule),
(with_context_flag ? new_context : NULL),
LR_set, NULL, FALSE);
IR_set_under_control_point_flag (current_LR_situation,
under_control_point_flag);
IR_set_next_LR_situation (last_LR_situation,
current_LR_situation);
last_LR_situation = current_LR_situation;
process_flag = TRUE;
IR_set_canonical_rule_LR_situation (current_canonical_rule,
current_LR_situation);
}
if (process_flag
&& !IR_situation_in_stack_flag (current_LR_situation))
{
IR_set_situation_in_stack_flag (current_LR_situation, TRUE);
VLO_ADD_MEMORY (LR_situations_stack, ¤t_LR_situation,
sizeof (IR_node_t));
}
}
if (with_context_flag)
free_context (context);
}
VLO_DELETE (LR_situations_stack);
/* Set up values of fields `first_symbol_LR_situation'. */
last_reduce_LR_situation_processed = NULL;
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation = IR_next_LR_situation (current_LR_situation))
{
IR_set_canonical_rule_LR_situation
(IR_canonical_rule (IR_element_after_dot (current_LR_situation)),
NULL);
if (IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
IR_NM_canonical_rule_end))
{
if (last_reduce_LR_situation_processed == NULL)
IR_set_first_symbol_LR_situation (current_LR_situation, TRUE);
else
{
IR_set_first_symbol_LR_situation (current_LR_situation, FALSE);
IR_set_next_symbol_LR_situation
(last_reduce_LR_situation_processed, current_LR_situation);
}
last_reduce_LR_situation_processed = current_LR_situation;
}
else
{
single_definition = IR_element_itself (IR_element_after_dot
(current_LR_situation));
if (IR_last_symbol_LR_situation_processed (single_definition)
== NULL)
IR_set_first_symbol_LR_situation (current_LR_situation, TRUE);
else
{
IR_set_first_symbol_LR_situation (current_LR_situation, FALSE);
IR_set_next_symbol_LR_situation
(IR_last_symbol_LR_situation_processed (single_definition),
current_LR_situation);
}
IR_set_last_symbol_LR_situation_processed
(single_definition, current_LR_situation);
}
}
/* Restore fields `last_symbol_LR_situation_processed'. */
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation = IR_next_LR_situation (current_LR_situation))
if (!IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
IR_NM_canonical_rule_end))
IR_set_last_symbol_LR_situation_processed
(IR_element_itself (IR_element_after_dot (current_LR_situation)),
FALSE);
#ifndef NDEBUG
for (single_definition = IR_single_definition_list (description);
single_definition != NULL;
single_definition = IR_next_single_definition (single_definition))
if (IR_last_symbol_LR_situation_processed (single_definition) != NULL)
abort ();
for (current_canonical_rule = IR_canonical_rule_list (description);
current_canonical_rule != NULL;
current_canonical_rule
= IR_next_canonical_rule (current_canonical_rule))
if (IR_canonical_rule_LR_situation (current_canonical_rule) != NULL)
abort ();
#endif
if (with_context_flag)
/* Insert new LR-situation contexts into the context table. */
insert_LR_set_contexts (LR_set);
}
static IR_node_t
create_start_LR_set (int with_context_flag)
{
IR_node_t start_LR_situation;
context_t start_context;
IR_node_t start_LR_set;
IR_node_t start_LR_core;
/* Create start LR-set `$accept : . axiom $end'. */
if (with_context_flag)
start_context = null_context_in_table;
else
start_context = NULL;
start_LR_situation
= get_new_LR_situation
(IR_right_hand_side (IR_canonical_rule_list (description)),
start_context, NULL, NULL, TRUE);
if (IR_cp_flag (IR_element_after_dot (start_LR_situation)))
IR_set_under_control_point_flag (start_LR_situation, TRUE);
start_LR_set = IR_new_LR_set (start_LR_situation, NULL, NULL);
IR_set_LR_set (start_LR_situation, start_LR_set);
/* Make cycle LR-set list. */
IR_set_next_LR_set (start_LR_set, start_LR_set);
start_LR_core = IR_new_LR_core (start_LR_set, NULL);
(void) insert_LR_core (start_LR_core);
IR_set_LR_core_list (description, start_LR_core);
/* Make cycle LR-core list. */
IR_set_next_LR_core (start_LR_core, start_LR_core);
IR_set_LR_core (start_LR_set, start_LR_core);
return start_LR_set;
}
static void
make_uncyclic_LR_set_and_core_lists (void)
{
IR_node_t LR_set;
IR_node_t LR_core;
/* Make uncyclic core list. */
LR_core = IR_LR_core_list (description);
IR_set_LR_core_list (description, IR_next_LR_core (LR_core));
IR_set_next_LR_core (LR_core, NULL);
/* Make uncyclic LR set lists. */
for (LR_core = IR_LR_core_list (description); LR_core != NULL;
LR_core = IR_next_LR_core (LR_core))
{
LR_set = IR_LR_set_list (LR_core);
IR_set_LR_set_list (LR_core, IR_next_LR_set (LR_set));
IR_set_next_LR_set (LR_set, NULL);
}
}
static void
count_LR_set_situations (IR_node_t LR_set, int *LR_sets_number,
int *important_LR_situations_number,
int *LR_situations_number)
{
IR_node_t current_LR_situation;
(*LR_sets_number)++;
current_LR_situation = IR_LR_situation_list (LR_set);
do
{
(*LR_situations_number)++;
(*important_LR_situations_number)++;
current_LR_situation = IR_next_LR_situation (current_LR_situation);
}
while (current_LR_situation != NULL
&& IR_important_LR_situation_flag (current_LR_situation));
while (current_LR_situation != NULL)
{
(*LR_situations_number)++;
current_LR_situation = IR_next_LR_situation (current_LR_situation);
}
}
static void
add_conflict (IR_node_t LR_set, IR_node_t used_LR_situation,
IR_node_t unused_LR_situation, token_string_t token_string)
{
IR_node_t conflict;
IR_node_t last_conflict;
conflict = IR_new_conflict (used_LR_situation, unused_LR_situation,
token_string, NULL);
last_conflict = IR_conflicts_list (LR_set);
if (last_conflict == NULL)
IR_set_conflicts_list (LR_set, conflict);
else
{
while (IR_next_conflict (last_conflict) != NULL)
last_conflict = IR_next_conflict (last_conflict);
IR_set_next_conflict (last_conflict, conflict);
}
}
static int
contains_error (token_string_t token_string)
{
IR_node_t single_term_definition;
int n;
for (n = 0;; n++)
{
single_term_definition = get_n_th_token (token_string, n);
if (single_term_definition == NULL)
break;
if (single_term_definition == error_single_definition)
return TRUE;
}
return FALSE;
}
/* ??? */
static int fold_is_used_in_conflict_resolution;
static IR_node_t conflict_resolution_fold_LR_situation;
static IR_node_t another_conflict_resolution_LR_situation;
static void
add_context_conflicts (token_string_t token_string)
{
if ((!IR_IS_OF_TYPE (IR_element_after_dot
(another_conflict_resolution_LR_situation),
IR_NM_canonical_rule_end)
&& !(IR_first_symbol_LR_situation
(another_conflict_resolution_LR_situation)))
|| (!error_conflict_flag && contains_error (token_string)
&& (characteristic_symbol_of_LR_set
(IR_LR_set (another_conflict_resolution_LR_situation))
== error_single_definition)))
return;
if (fold_is_used_in_conflict_resolution)
add_conflict (IR_LR_set (another_conflict_resolution_LR_situation),
conflict_resolution_fold_LR_situation,
another_conflict_resolution_LR_situation, token_string);
else
add_conflict (IR_LR_set (another_conflict_resolution_LR_situation),
another_conflict_resolution_LR_situation,
conflict_resolution_fold_LR_situation, token_string);
}
static void
add_terminal_and_context_concatenation (context_t *target_context,
IR_node_t terminal,
context_t added_context)
{
context_t context;
context = get_null_context ();
set_context_element_value (context, get_new_token_string (&terminal, 1), 1);
context_concat (context, added_context, max_look_ahead_number);
context_or (*target_context, context);
free_context (context);
}
static hash_table_t dependence_nodes_table;
static IR_node_t work_LR_set_dependence;
static IR_node_t work_shift_LR_situation_dependence;
static unsigned
dependence_nodes_table_hash_function (hash_table_entry_t dependence_node)
{
if (IR_IS_OF_TYPE ((IR_node_t) dependence_node, IR_NM_LR_set_dependence))
return
(unsigned long) IR_LR_set ((IR_node_t) dependence_node)
+ IR_back_distance ((IR_node_t) dependence_node)
+ IR_look_ahead ((IR_node_t) dependence_node)
+ (unsigned long) IR_nonterm ((IR_node_t) dependence_node);
#ifndef NDEBUG
else if (!IR_IS_OF_TYPE ((IR_node_t) dependence_node,
IR_NM_shift_LR_situation_dependence))
assert (FALSE);
#endif
else
return
(unsigned long) IR_shift_LR_situation ((IR_node_t) dependence_node)
+ IR_look_ahead ((IR_node_t) dependence_node);
}
static int
dependence_nodes_table_eq_function (hash_table_entry_t dependence_node_1,
hash_table_entry_t dependence_node_2)
{
if (IR_NODE_MODE ((IR_node_t) dependence_node_1)
!= IR_NODE_MODE ((IR_node_t) dependence_node_2))
return FALSE;
if (IR_IS_OF_TYPE ((IR_node_t) dependence_node_1, IR_NM_LR_set_dependence))
return ((IR_LR_set ((IR_node_t) dependence_node_1)
== IR_LR_set ((IR_node_t) dependence_node_2))
&& (IR_back_distance ((IR_node_t) dependence_node_1)
== IR_back_distance ((IR_node_t) dependence_node_2))
&& (IR_look_ahead ((IR_node_t) dependence_node_1)
== IR_look_ahead ((IR_node_t) dependence_node_2))
&& (IR_nonterm ((IR_node_t) dependence_node_1)
== IR_nonterm ((IR_node_t) dependence_node_2)));
#ifndef NDEBUG
else if (!IR_IS_OF_TYPE ((IR_node_t) dependence_node_1,
IR_NM_shift_LR_situation_dependence))
assert (FALSE);
#endif
else
return ((IR_shift_LR_situation ((IR_node_t) dependence_node_1)
== IR_shift_LR_situation ((IR_node_t) dependence_node_2))
&& (IR_look_ahead ((IR_node_t) dependence_node_1)
== IR_look_ahead ((IR_node_t) dependence_node_2)));
}
static int unique_dependence_node_number;
static void
set_unique_dependence_node_number (IR_node_t dependence_node)
{
IR_set_unique_number (dependence_node, unique_dependence_node_number);
unique_dependence_node_number++;
}
static IR_node_t
get_LR_set_dependence_node (IR_node_t LR_set, int back_distance,
int look_ahead, IR_node_t nonterm, int *new_flag)
{
hash_table_entry_t *entry_ptr;
IR_set_LR_set (work_LR_set_dependence, LR_set);
IR_set_back_distance (work_LR_set_dependence, back_distance);
IR_set_look_ahead (work_LR_set_dependence, look_ahead);
IR_set_nonterm (work_LR_set_dependence, nonterm);
entry_ptr = find_hash_table_entry (dependence_nodes_table,
work_LR_set_dependence, TRUE);
*new_flag = *entry_ptr == NULL;
if (*new_flag)
{
*entry_ptr
= (hash_table_entry_t) IR_new_LR_set_dependence (look_ahead, LR_set,
back_distance,
nonterm);
set_unique_dependence_node_number ((IR_node_t) *entry_ptr);
}
return (IR_node_t) *entry_ptr;
}
static IR_node_t
get_shift_LR_situation_dependence_node (IR_node_t shift_LR_situation,
int look_ahead, int *new_flag)
{
hash_table_entry_t *entry_ptr;
assert (IR_IS_OF_TYPE (IR_element_itself (IR_element_after_dot
(shift_LR_situation)),
IR_NM_single_term_definition)
&& IR_first_symbol_LR_situation (shift_LR_situation));
IR_set_shift_LR_situation (work_shift_LR_situation_dependence,
shift_LR_situation);
IR_set_look_ahead (work_shift_LR_situation_dependence, look_ahead);
entry_ptr = find_hash_table_entry (dependence_nodes_table,
work_shift_LR_situation_dependence, TRUE);
*new_flag = *entry_ptr == NULL;
if (*new_flag)
{
*entry_ptr
= ((hash_table_entry_t)
IR_new_shift_LR_situation_dependence
(look_ahead,
IR_element_itself (IR_element_after_dot (shift_LR_situation)),
shift_LR_situation));
set_unique_dependence_node_number ((IR_node_t) *entry_ptr);
}
return (IR_node_t) *entry_ptr;
}
static void
initiate_dependence_nodes_table (void)
{
work_LR_set_dependence = IR_create_node (IR_NM_LR_set_dependence);
work_shift_LR_situation_dependence
= IR_create_node (IR_NM_shift_LR_situation_dependence);
unique_dependence_node_number = 0;
dependence_nodes_table
= create_hash_table (15000, dependence_nodes_table_hash_function,
dependence_nodes_table_eq_function);
}
static void
finish_dependence_nodes_table (void)
{
#ifndef NDEBUG
if (debug_level >= 1)
fprintf
(stderr,
"Dependence nodes table: entries - %lu, elements - %lu, collisions - %d%%\n",
hash_table_size (dependence_nodes_table),
hash_table_elements_number (dependence_nodes_table),
get_collisions (dependence_nodes_table));
#endif
delete_hash_table (dependence_nodes_table);
}
#ifndef NDEBUG
#include "output.h"
static void
print_dependence_node (IR_node_t dependence_node, int context_flag)
{
IR_node_t current_dependence_list_element;
if (context_flag)
{
fprintf (stderr, "\n%d dependence context: ",
IR_unique_number (dependence_node));
output_context (stderr, IR_context (dependence_node));
}
else
{
if (IR_IS_OF_TYPE (dependence_node, IR_NM_shift_dependence))
{
fprintf (stderr, "\n%d shift dependence (la %d): ",
IR_unique_number (dependence_node),
IR_look_ahead (dependence_node));
output_single_definition (stderr, IR_token (dependence_node));
}
#ifndef NDEBUG
else if (!IR_IS_OF_TYPE (dependence_node, IR_NM_LR_set_dependence))
assert (FALSE);
#endif
else
{
fprintf (stderr, "\n%d LR-set dependence: back %d, la %d, ",
IR_unique_number (dependence_node),
IR_back_distance (dependence_node),
IR_look_ahead (dependence_node));
output_single_definition (stderr, IR_nonterm (dependence_node));
}
fprintf (stderr, "\n");
fprintf (stderr, "dependencies:");
for (current_dependence_list_element = IR_dependencies (dependence_node);
current_dependence_list_element != NULL;
current_dependence_list_element
= IR_next_dependence_list_element
(current_dependence_list_element))
fprintf (stderr, " %d",
IR_unique_number (IR_dependence
(current_dependence_list_element)));
}
fprintf (stderr, "\n");
}
#endif
/* The following structure describes history stack elements. */
struct stack_element
{
/* LR-set which is know will be on the stack during processing the
current LR-set. */
IR_node_t LR_set;
/* look ahead when we serached for the LR-set. */
int look_ahead;
};
static IR_node_t
process_reduces (IR_node_t LR_set, int look_ahead, vlo_t *history_stack,
vlo_t *dependence_nodes_stack, vlo_t *auxiliary_stack)
{
IR_node_t current_LR_situation;
IR_node_t first_element;
IR_node_t last_element;
IR_node_t dependencies;
IR_node_t canonical_rule;
IR_node_t nonterm_definition;
IR_node_t goto_LR_set;
IR_node_t dependence_node;
struct stack_element elem, *elem_ptr;
int rule_length;
int back_distance;
int new_flag;
int n;
if (look_ahead < 1)
return NULL;
/* We don't want to drop in cycle here therfore we searching for
LR-set which processed with given look ahead. */
for (n = 0, elem_ptr = VLO_BEGIN (*history_stack);
(char *) elem_ptr < (char *) VLO_BOUND (*history_stack);
elem_ptr++)
if (elem_ptr->LR_set == LR_set && elem_ptr->look_ahead == look_ahead)
{
/* We need at least processing twice for correct conflict
searching. */
if (n == 1)
return NULL;
n++;
}
elem.LR_set = LR_set;
elem.look_ahead = look_ahead;
VLO_ADD_MEMORY (*history_stack, &elem, sizeof (elem));
first_element = NULL;
for (current_LR_situation = IR_LR_situation_list (LR_set);
current_LR_situation != NULL;
current_LR_situation
= IR_next_LR_situation (current_LR_situation))
if (IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
IR_NM_canonical_rule_end))
{
canonical_rule
= IR_canonical_rule (IR_element_after_dot (current_LR_situation));
nonterm_definition = IR_left_hand_side (canonical_rule);
rule_length
= canonical_rule_right_hand_side_prefix_length (canonical_rule,
NULL);
back_distance
= (rule_length + 1
- VLO_LENGTH (*history_stack) / sizeof (struct stack_element));
if (back_distance >= 0)
{
dependence_node
= get_LR_set_dependence_node
(((struct stack_element *) VLO_BEGIN (*history_stack))->LR_set,
back_distance, look_ahead, nonterm_definition, &new_flag);
if (new_flag)
VLO_ADD_MEMORY (*dependence_nodes_stack,
&dependence_node, sizeof (IR_node_t));
first_element = IR_new_dependence_list_element (dependence_node,
first_element);
}
else
{
if (rule_length != 0)
VLO_ADD_MEMORY (*auxiliary_stack,
(char *) VLO_BOUND (*history_stack)
- rule_length * sizeof (struct stack_element),
rule_length * sizeof (struct stack_element));
VLO_SHORTEN (*history_stack,
rule_length * sizeof (struct stack_element));
goto_LR_set
= goto_by_nonterminal
(((struct stack_element *) VLO_BEGIN (*history_stack))
[VLO_LENGTH (*history_stack) / sizeof (struct stack_element)
- 1].LR_set,
nonterm_definition);
dependencies = process_reduces (goto_LR_set, look_ahead,
history_stack,
dependence_nodes_stack,
auxiliary_stack);
if (rule_length != 0)
{
VLO_ADD_MEMORY (*history_stack,
(char *) VLO_BOUND (*auxiliary_stack)
- rule_length * sizeof (struct stack_element),
rule_length * sizeof (struct stack_element));
VLO_SHORTEN (*auxiliary_stack,
rule_length * sizeof (struct stack_element));
}
if (dependencies != NULL)
{
for (last_element = dependencies;
IR_next_dependence_list_element (last_element) != NULL;
last_element
= IR_next_dependence_list_element (last_element))
;
IR_set_next_dependence_list_element (last_element,
first_element);
first_element = dependencies;
}
}
}
else if (IR_first_symbol_LR_situation (current_LR_situation)
&& IR_IS_OF_TYPE (IR_element_itself
(IR_element_after_dot
(current_LR_situation)),
IR_NM_single_term_definition))
{
dependence_node = IR_new_shift_dependence (look_ahead,
IR_element_itself
(IR_element_after_dot
(current_LR_situation)));
set_unique_dependence_node_number (dependence_node);
IR_set_dependencies (dependence_node,
process_reduces (IR_goto_LR_set
(current_LR_situation),
look_ahead - 1, history_stack,
dependence_nodes_stack,
auxiliary_stack));
assert (look_ahead == 1 || IR_dependencies (dependence_node) != NULL);
#ifndef NDEBUG
if (debug_level >= 2)
print_dependence_node (dependence_node, FALSE);
#endif
first_element = IR_new_dependence_list_element (dependence_node,
first_element);
}
VLO_SHORTEN (*history_stack, sizeof (struct stack_element));
return first_element;
}
static void
process_dependencies (vlo_t *dependence_nodes_stack,
vlo_t *auxiliary_stack, vlo_t *history_stack)
{
IR_node_t first_element;
IR_node_t dependence_node;
IR_node_t new_dependence_node;
IR_node_t temp_LR_set;
IR_node_t goto_LR_set;
IR_node_t LR_situation_of_immediate_LR_set_predecessor;
IR_double_link_t LR_situation_reference;
struct stack_element elem;
int new_flag;
while (VLO_LENGTH (*dependence_nodes_stack) != 0)
{
dependence_node
= ((IR_node_t *) VLO_BEGIN (*dependence_nodes_stack))
[VLO_LENGTH (*dependence_nodes_stack) / sizeof (IR_node_t) - 1];
VLO_SHORTEN (*dependence_nodes_stack, sizeof (IR_node_t));
if (IR_back_distance (dependence_node) != 0)
{
first_element = NULL;
for (LR_situation_reference
= IR__first_double_link (IR_LR_set (dependence_node));
LR_situation_reference != NULL;
LR_situation_reference
= IR__next_double_link (LR_situation_reference))
{
LR_situation_of_immediate_LR_set_predecessor
= IR__owner (LR_situation_reference);
if (IR_IS_OF_TYPE (LR_situation_of_immediate_LR_set_predecessor,
IR_NM_LR_situation))
{
/* See comments in file `ird.sprut'. */
assert (IR_first_symbol_LR_situation
(LR_situation_of_immediate_LR_set_predecessor));
new_dependence_node
= get_LR_set_dependence_node
(IR_LR_set
(LR_situation_of_immediate_LR_set_predecessor),
IR_back_distance (dependence_node) - 1,
IR_look_ahead (dependence_node),
IR_nonterm (dependence_node),
&new_flag);
if (new_flag)
VLO_ADD_MEMORY (*dependence_nodes_stack,
&new_dependence_node, sizeof (IR_node_t));
first_element
= IR_new_dependence_list_element (new_dependence_node,
first_element);
}
}
IR_set_dependencies (dependence_node, first_element);
}
else
{
VLO_NULLIFY (*history_stack);
temp_LR_set = IR_LR_set (dependence_node);
elem.LR_set = temp_LR_set;
elem.look_ahead = IR_look_ahead (dependence_node);
VLO_ADD_MEMORY (*history_stack, &elem,
sizeof (struct stack_element));
goto_LR_set = goto_by_nonterminal (temp_LR_set,
IR_nonterm (dependence_node));
IR_set_dependencies (dependence_node,
process_reduces
(goto_LR_set, IR_look_ahead (dependence_node),
history_stack, dependence_nodes_stack,
auxiliary_stack));
}
#ifndef NDEBUG
if (debug_level >= 2)
print_dependence_node (dependence_node, FALSE);
#endif
}
}
static IR_node_t
build_graph_from_reduce (IR_node_t reduce_LR_situation, int look_ahead)
{
IR_node_t result;
IR_node_t LR_set;
IR_node_t nonterm;
IR_node_t canonical_rule;
int back_distance;
int new_flag;
vlo_t dependence_nodes_stack;