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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
|
/*
File: pattern_match.c
Author: John van Groningen
*/
#define DEBUG_OUTPUT 0
#if defined (applec) || defined (__MWERKS__) || defined (__MRC__)
# define __ppc__
#endif
#include <stdio.h>
#include "compiledefines.h"
#include "types.t"
#include "syntaxtr.t"
#include "pattern_match.h"
#include "buildtree.h"
#include "comsupport.h"
#include "statesgen.h"
#include "settings.h"
#include "codegen_types.h"
#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)
static void error_in_function (char *m)
{
ErrorInCompiler ("",m,"");
}
#if DEBUG_OUTPUT
char *node_id_name (NodeId node_id)
{
static char node_id_name_s[65];
if (node_id->nid_ident!=NULL && node_id->nid_ident->ident_name!=NULL)
return node_id->nid_ident->ident_name;
else {
sprintf (node_id_name_s,"i_%lx",(long)node_id);
return node_id_name_s;
}
}
#endif
static NodeP new_switch_node (NodeIdP node_id,NodeP case_node,StateP state_p,NodeS ***root_l)
{
NodeP switch_node;
switch_node=CompAllocType (NodeS);
switch_node->node_kind=SwitchNode;
switch_node->node_node_id=node_id;
switch_node->node_arity=1;
switch_node->node_arguments=NewArgument (case_node);
switch_node->node_state=*state_p;
#if DEBUG_OUTPUT
printf ("dec %s %d\n",node_id_name (node_id),node_id->nid_refcount);
#endif
--node_id->nid_refcount;
**root_l=switch_node;
*root_l=&case_node->node_arguments->arg_node;
return switch_node;
}
static NodeP new_case_node (SymbolP symbol,int symbol_arity,NodeP node,NodeDefP **def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,StrictNodeIdP **strict_node_ids_l
#endif
)
{
NodeP case_node;
case_node=CompAllocType (NodeS);
case_node->node_kind=CaseNode;
case_node->node_symbol=symbol;
case_node->node_arity=symbol_arity;
case_node->node_arguments=NewArgument (node);
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
case_node->node_su.su_u.u_case=CompAllocType (CaseNodeContentsS);
case_node->node_strict_node_ids=NULL;
#endif
case_node->node_node_id_ref_counts=NULL;
case_node->node_node_defs=**def_l;
**def_l=NULL;
*def_l=&case_node->node_node_defs;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
case_node->node_strict_node_ids=**strict_node_ids_l;
**strict_node_ids_l=NULL;
*strict_node_ids_l=&case_node->node_strict_node_ids;
#endif
return case_node;
}
struct root_and_defs_l {
NodeP ** root_l;
NodeDefP ** def_l;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
StrictNodeIdP **strict_node_ids_l;
NodeDefP ** end_lhs_defs_l;
#endif
};
struct root_and_defs {
NodeP root;
NodeDefP defs;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
StrictNodeIdP strict_node_ids;
#endif
};
static NodeP new_push_node (Symbol symbol,int arity,ArgP arguments)
{
NodeP push_node;
push_node=CompAllocType (NodeS);
push_node->node_kind=PushNode;
push_node->node_arity=arity;
push_node->node_arguments=arguments;
push_node->node_record_symbol=symbol;
push_node->node_number=0; /* if !=0 then unique */
return push_node;
}
NodeIdRefCountListP new_node_id_ref_count (NodeIdRefCountListP node_id_ref_count_list,NodeIdP node_id,int ref_count)
{
NodeIdRefCountListP new_node_id_ref_count_elem;
new_node_id_ref_count_elem=CompAllocType (NodeIdRefCountListS);
new_node_id_ref_count_elem->nrcl_next=node_id_ref_count_list;
new_node_id_ref_count_elem->nrcl_node_id=node_id;
new_node_id_ref_count_elem->nrcl_ref_count=ref_count;
return new_node_id_ref_count_elem;
}
static NodeIdRefCountListP *insert_new_node_id_ref_count (NodeIdRefCountListP *node_id_ref_count_p,NodeIdP node_id,int ref_count)
{
NodeIdRefCountListP node_id_ref_count_elem;
node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id,ref_count);
*node_id_ref_count_p=node_id_ref_count_elem;
return &node_id_ref_count_elem->nrcl_next;
}
static void transform_normal_pattern_node (NodeP node,StateP state_p,NodeIdP node_id,struct root_and_defs_l *root_and_defs_lp);
static void transform_pattern_arguments (SymbolP symbol,ArgP arguments,int arity,NodeIdP node_id,struct root_and_defs_l *root_and_defs_lp)
{
NodeP push_node;
NodeIdListElementP *last_node_id_p;
ArgP arg,arg1,arg2;
arg2=NewArgument (**root_and_defs_lp->root_l);
arg1=NewArgument (NULL);
arg1->arg_next=arg2;
push_node=new_push_node (symbol,arity,arg1);
**root_and_defs_lp->root_l=push_node;
*root_and_defs_lp->root_l=&arg2->arg_node;
last_node_id_p=&push_node->node_node_ids;
for_l (arg,arguments,arg_next){
NodeIdP argument_node_id;
NodeP node;
node=arg->arg_node;
if (node->node_kind==NormalNode){
argument_node_id=NewNodeId (NULL);
argument_node_id->nid_refcount=-1;
argument_node_id->nid_lhs_state_p_=&arg->arg_state;
transform_normal_pattern_node (node,&arg->arg_state,argument_node_id,root_and_defs_lp);
} else {
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
NodeP argument_node_id_node;
argument_node_id=node->node_node_id;
argument_node_id->nid_lhs_state_p_=&arg->arg_state;
argument_node_id_node=argument_node_id->nid_node;
if (argument_node_id_node){
argument_node_id->nid_node=NULL;
transform_normal_pattern_node (argument_node_id_node,&arg->arg_state,argument_node_id,root_and_defs_lp);
}
#else
argument_node_id=node->node_node_id;
if (argument_node_id->nid_node)
transform_normal_pattern_node (argument_node_id->nid_node,&arg->arg_state,argument_node_id,root_and_defs_lp);
#endif
}
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
argument_node_id->nid_state_=arg->arg_state;
#endif
*last_node_id_p=CompAllocType (NodeIdListElementS);
(*last_node_id_p)->nidl_node_id=argument_node_id;
last_node_id_p=&(*last_node_id_p)->nidl_next;
}
*last_node_id_p=NULL;
arg1->arg_node=NewNodeIdNode (node_id);
}
static void transform_normal_pattern_node (NodeP node,StateP state_p,NodeIdP node_id,struct root_and_defs_l *root_and_defs_lp)
{
SymbolP symbol;
NodeP switch_node,case_node;
NodeP **root_l;
NodeDefP **def_l;
symbol=node->node_symbol;
root_l=root_and_defs_lp->root_l;
def_l=root_and_defs_lp->def_l;
switch (symbol->symb_kind){
case definition:
case_node=new_case_node (symbol,node->node_arity,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
if (node->node_arity>0)
transform_pattern_arguments (symbol,node->node_arguments,node->node_arity,node_id,root_and_defs_lp);
return;
case cons_symb:
case_node=new_case_node (symbol,2,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
transform_pattern_arguments (symbol,node->node_arguments,2,node_id,root_and_defs_lp);
return;
case nil_symb:
case_node=new_case_node (symbol,0,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
return;
case tuple_symb:
case_node=new_case_node (symbol,node->node_arity,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
transform_pattern_arguments (symbol,node->node_arguments,node->node_arity,node_id,root_and_defs_lp);
return;
case apply_symb:
case if_symb:
error_in_function ("transform_normal_pattern_node");
return;
case string_denot:
case_node=new_case_node (symbol,0,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
return;
default:
if (symbol->symb_kind < Nr_Of_Basic_Types)
error_in_function ("transform_normal_pattern_node");
else {
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
if (state_p->state_object==BasicSymbolStates [symbol->symb_kind].state_object){
#endif
case_node=new_case_node (symbol,0,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
return;
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
} else if (state_p->state_object==UnknownObj
# if ABSTRACT_OBJECT
|| state_p->state_object==AbstractObj
# endif
){
case_node=new_case_node (symbol,0,**root_l,def_l
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
,root_and_defs_lp->strict_node_ids_l
#endif
);
switch_node=new_switch_node (node_id,case_node,state_p,root_l);
return;
} else
error_in_function ("transform_normal_pattern_node");
#endif
}
}
}
static void transform_argument (ArgP arg_p,struct root_and_defs_l *root_and_defs_lp)
{
NodeP node;
node=arg_p->arg_node;
switch (node->node_kind){
case NormalNode:
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
if (arg_p->arg_state.state_type==TupleState || arg_p->arg_state.state_type==RecordState){
ArgP arg;
for_l (arg,node->node_arguments,arg_next)
transform_argument (arg,root_and_defs_lp);
} else
#endif
{
NodeIdP node_id;
node_id=NewNodeId (NULL);
node_id->nid_refcount=-1;
node_id->nid_lhs_state_p_=&arg_p->arg_state;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
if (node->node_symbol->symb_kind==tuple_symb ||
(node->node_symbol->symb_kind==definition && node->node_symbol->symb_def->sdef_kind==RECORDTYPE))
{
error_in_function ("transform_argument");
} else
#endif
transform_normal_pattern_node (node,&arg_p->arg_state,node_id,root_and_defs_lp);
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
node_id->nid_state_=arg_p->arg_state;
#endif
arg_p->arg_node=NewNodeIdNode (node_id);
}
break;
case NodeIdNode:
{
NodeIdP node_id;
node_id=node->node_node_id;
if (node_id->nid_node!=NULL){
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
SymbolP node_id_nid_node_symbol;
node_id_nid_node_symbol=node_id->nid_node->node_symbol;
if (node_id_nid_node_symbol->symb_kind==tuple_symb ||
(node_id_nid_node_symbol->symb_kind==definition && node_id_nid_node_symbol->symb_def->sdef_kind==RECORDTYPE))
{
error_in_function ("transform_argument 1");
}
#else
if (arg_p->arg_state.state_type==TupleState || arg_p->arg_state.state_type==RecordState){
error_in_function ("transform_argument 1");
} else
#endif
{
transform_normal_pattern_node (node_id->nid_node,&arg_p->arg_state,node_id,root_and_defs_lp);
node_id->nid_node=NULL;
}
}
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
node_id->nid_lhs_state_p_=&arg_p->arg_state;
#else
node_id->nid_state_=arg_p->arg_state;
#endif
break;
}
default:
error_in_function ("transform_argument");
}
}
#if 0
# include "dbprint.h"
#endif
static void replace_global_ref_count_by_local_ref_count (NodeIdRefCountListP node_id_ref_count_list)
{
NodeIdRefCountListP node_id_ref_count_elem;
for_l (node_id_ref_count_elem,node_id_ref_count_list,nrcl_next){
int local_ref_count;
NodeIdP node_id;
node_id=node_id_ref_count_elem->nrcl_node_id;
local_ref_count=node_id_ref_count_elem->nrcl_ref_count;
#if DEBUG_OUTPUT
printf ("global_to_local %s %d %d ",node_id_name (node_id),node_id->nid_refcount,node_id_ref_count_elem->nrcl_ref_count);
#endif
node_id_ref_count_elem->nrcl_ref_count=node_id->nid_refcount - local_ref_count;
node_id->nid_refcount = local_ref_count;
}
#if DEBUG_OUTPUT
printf ("\n");
#endif
}
void set_local_reference_counts (NodeP case_node)
{
replace_global_ref_count_by_local_ref_count (case_node->node_node_id_ref_counts);
}
static void replace_local_ref_count_by_global_ref_count (NodeIdRefCountListP node_id_ref_count_list)
{
NodeIdRefCountListP node_id_ref_count_elem;
for_l (node_id_ref_count_elem,node_id_ref_count_list,nrcl_next){
int local_ref_count;
NodeIdP node_id;
node_id=node_id_ref_count_elem->nrcl_node_id;
local_ref_count=node_id->nid_refcount;
#if DEBUG_OUTPUT
printf ("local_to_global %s %d %d ",node_id_name (node_id),node_id->nid_refcount,node_id_ref_count_elem->nrcl_ref_count);
#endif
node_id->nid_refcount = local_ref_count + node_id_ref_count_elem->nrcl_ref_count;
node_id_ref_count_elem->nrcl_ref_count=local_ref_count;
}
#if DEBUG_OUTPUT
printf ("\n");
#endif
}
void set_global_reference_counts (NodeP case_node)
{
replace_local_ref_count_by_global_ref_count (case_node->node_node_id_ref_counts);
}
#if BOXED_RECORDS
void set_global_reference_counts_and_exchange_record_update_marks (NodeP case_node)
{
NodeIdRefCountListP node_id_ref_count_elem;
for_l (node_id_ref_count_elem,case_node->node_node_id_ref_counts,nrcl_next){
int local_ref_count;
NodeIdP node_id;
unsigned int node_id_mark2;
node_id=node_id_ref_count_elem->nrcl_node_id;
node_id_mark2=node_id->nid_mark2;
node_id->nid_mark2=(node_id_mark2 & ~NID_RECORD_USED_BY_NON_SELECTOR_OR_UPDATES) | node_id_ref_count_elem->nrcl_mark2;
node_id_ref_count_elem->nrcl_mark2=node_id_mark2 & NID_RECORD_USED_BY_NON_SELECTOR_OR_UPDATES;
local_ref_count=node_id->nid_refcount;
node_id->nid_refcount = local_ref_count + node_id_ref_count_elem->nrcl_ref_count;
node_id_ref_count_elem->nrcl_ref_count=local_ref_count;
}
}
#endif
static void merge_node_id_ref_count_lists (NodeIdRefCountListP *list1_p,NodeIdRefCountListP list2)
{
while (list2!=NULL){
NodeIdP node_id;
NodeIdRefCountListP next_list2,list1;
node_id=list2->nrcl_node_id;
while (list1=*list1_p,list1!=NULL && list1->nrcl_node_id<=node_id)
list1_p=&list1->nrcl_next;
if (list1==NULL){
*list1_p=list2;
return;
}
next_list2=list2->nrcl_next;
*list1_p=list2;
list2->nrcl_next=list1;
list1_p=&list2->nrcl_next;
list2=next_list2;
}
}
static void sort_node_id_ref_count_lists (NodeIdRefCountListP *list_p)
{
NodeIdRefCountListP element1,element2,element3;
element1=*list_p;
if (element1==NULL)
return;
element2=element1->nrcl_next;
if (element2==NULL)
return;
element3=element2->nrcl_next;
if (element3==NULL){
if (element1->nrcl_node_id<=element2->nrcl_node_id)
return;
*list_p=element2;
element2->nrcl_next=element1;
element1->nrcl_next=NULL;
} else {
NodeIdRefCountListP list2,end_list1,end_list2;
list2=element2;
end_list1=element1;
end_list2=element2;
element1=element3;
do {
end_list1->nrcl_next=element1;
end_list1=element1;
element2=element1->nrcl_next;
if (element2==NULL)
break;
end_list2->nrcl_next=element2;
end_list2=element2;
element1=element2->nrcl_next;
} while (element1!=NULL);
end_list1->nrcl_next=NULL;
end_list2->nrcl_next=NULL;
sort_node_id_ref_count_lists (list_p);
sort_node_id_ref_count_lists (&list2);
merge_node_id_ref_count_lists (list_p,list2);
}
}
static void add_sorted_node_id_ref_count_list (NodeIdRefCountListP *node_id_ref_count_list1_p,NodeIdRefCountListP node_id_ref_count_list2)
{
NodeIdRefCountListP node_id_ref_count_list1;
while (node_id_ref_count_list2!=NULL){
NodeIdP node_id;
node_id=node_id_ref_count_list2->nrcl_node_id;
while (node_id_ref_count_list1=*node_id_ref_count_list1_p,node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id<node_id)
node_id_ref_count_list1_p=&node_id_ref_count_list1->nrcl_next;
if (node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id==node_id){
#if DEBUG_OUTPUT
printf ("add %s %d %d %d\n",node_id_name (node_id),node_id->nid_refcount,node_id_ref_count_list1->nrcl_ref_count,node_id_ref_count_list2->nrcl_ref_count);
#endif
node_id_ref_count_list1->nrcl_ref_count += node_id_ref_count_list2->nrcl_ref_count+1;
node_id_ref_count_list1_p=&node_id_ref_count_list1->nrcl_next;
} else {
NodeIdRefCountListP new_node_id_ref_count_elem;
#if DEBUG_OUTPUT
printf ("addnew %s %d %d\n",node_id_name (node_id),node_id->nid_refcount,node_id_ref_count_list2->nrcl_ref_count);
#endif
new_node_id_ref_count_elem=new_node_id_ref_count (node_id_ref_count_list1,node_id_ref_count_list2->nrcl_node_id,node_id_ref_count_list2->nrcl_ref_count);
*node_id_ref_count_list1_p=new_node_id_ref_count_elem;
node_id_ref_count_list1_p=&new_node_id_ref_count_elem->nrcl_next;
}
node_id_ref_count_list2=node_id_ref_count_list2->nrcl_next;
}
}
/* JVG added 16-2-2000 */
static void add_sorted_node_id_ref_count_list_for_case (NodeIdRefCountListP *node_id_ref_count_list1_p,NodeIdRefCountListP node_id_ref_count_list2)
{
NodeIdRefCountListP node_id_ref_count_list1;
while (node_id_ref_count_list2!=NULL){
NodeIdP node_id;
node_id=node_id_ref_count_list2->nrcl_node_id;
while (node_id_ref_count_list1=*node_id_ref_count_list1_p,node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id<node_id)
node_id_ref_count_list1_p=&node_id_ref_count_list1->nrcl_next;
if (node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id==node_id){
#if DEBUG_OUTPUT
printf ("add %s %d %d %d\n",node_id_name (node_id),node_id->nid_refcount,node_id_ref_count_list1->nrcl_ref_count,node_id_ref_count_list2->nrcl_ref_count);
#endif
node_id_ref_count_list1->nrcl_ref_count += node_id_ref_count_list2->nrcl_ref_count+1;
node_id_ref_count_list1_p=&node_id_ref_count_list1->nrcl_next;
} /* else do nothing*/
node_id_ref_count_list2=node_id_ref_count_list2->nrcl_next;
}
}
/**/
/*
static NodeIdRefCountListP merge_sorted_node_id_ref_count_lists
(NodeIdRefCountListP node_id_ref_count_list1,NodeIdRefCountListP node_id_ref_count_list2)
{
NodeIdRefCountListP node_id_ref_count_list,*node_id_ref_count_list_p;
node_id_ref_count_list_p=&node_id_ref_count_list;
while (node_id_ref_count_list2!=NULL){
NodeIdP node_id;
node_id=node_id_ref_count_list2->nrcl_node_id;
while (node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id<node_id){
NodeIdRefCountListP new_node_id_ref_count_elem;
#if DEBUG_OUTPUT
{
char *node_id_name;
node_id_name="";
if (node_id_ref_count_list1->nrcl_node_id->nid_ident!=NULL && node_id_ref_count_list1->nrcl_node_id->nid_ident->ident_name!=NULL)
node_id_name=node_id_ref_count_list1->nrcl_node_id->nid_ident->ident_name;
printf ("from1 %s %d %d\n",node_id_name,node_id_ref_count_list1->nrcl_node_id->nid_refcount,node_id_ref_count_list1->nrcl_ref_count);
}
#endif
new_node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id_ref_count_list1->nrcl_node_id,node_id_ref_count_list1->nrcl_ref_count);
*node_id_ref_count_list_p=new_node_id_ref_count_elem;
node_id_ref_count_list_p=&new_node_id_ref_count_elem->nrcl_next;
node_id_ref_count_list1=node_id_ref_count_list1->nrcl_next;
}
if (node_id_ref_count_list1!=NULL && node_id_ref_count_list1->nrcl_node_id==node_id){
NodeIdRefCountListP new_node_id_ref_count_elem;
#if DEBUG_OUTPUT
{
char *node_id_name;
node_id_name="";
if (node_id->nid_ident!=NULL && node_id->nid_ident->ident_name!=NULL)
node_id_name=node_id->nid_ident->ident_name;
printf ("combine %s %d %d\n",node_id_name,node_id_ref_count_list1->nrcl_node_id->nid_refcount,node_id_ref_count_list2->nrcl_ref_count);
}
#endif
new_node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id,
node_id_ref_count_list1->nrcl_ref_count+node_id_ref_count_list2->nrcl_ref_count+1);
*node_id_ref_count_list_p=new_node_id_ref_count_elem;
node_id_ref_count_list_p=&new_node_id_ref_count_elem->nrcl_next;
node_id_ref_count_list1=node_id_ref_count_list1->nrcl_next;
} else {
NodeIdRefCountListP new_node_id_ref_count_elem;
#if DEBUG_OUTPUT
{
char *node_id_name;
node_id_name="";
if (node_id_ref_count_list2->nrcl_node_id->nid_ident!=NULL && node_id_ref_count_list2->nrcl_node_id->nid_ident->ident_name!=NULL)
node_id_name=node_id_ref_count_list2->nrcl_node_id->nid_ident->ident_name;
printf ("from2 %s %d %d\n",node_id_name,node_id_ref_count_list2->nrcl_node_id->nid_refcount,node_id_ref_count_list2->nrcl_ref_count);
}
#endif
new_node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id_ref_count_list2->nrcl_node_id,node_id_ref_count_list2->nrcl_ref_count);
*node_id_ref_count_list_p=new_node_id_ref_count_elem;
node_id_ref_count_list_p=&new_node_id_ref_count_elem->nrcl_next;
}
node_id_ref_count_list2=node_id_ref_count_list2->nrcl_next;
}
while (node_id_ref_count_list1!=NULL){
NodeIdRefCountListP new_node_id_ref_count_elem;
#if DEBUG_OUTPUT
{
char *node_id_name;
node_id_name="";
if (node_id_ref_count_list1->nrcl_node_id->nid_ident!=NULL && node_id_ref_count_list1->nrcl_node_id->nid_ident->ident_name!=NULL)
node_id_name=node_id_ref_count_list1->nrcl_node_id->nid_ident->ident_name;
printf ("from1 %s %d %d\n",node_id_name,node_id_ref_count_list1->nrcl_node_id->nid_refcount,node_id_ref_count_list1->nrcl_ref_count);
}
#endif
new_node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id_ref_count_list1->nrcl_node_id,node_id_ref_count_list1->nrcl_ref_count);
*node_id_ref_count_list_p=new_node_id_ref_count_elem;
node_id_ref_count_list_p=&new_node_id_ref_count_elem->nrcl_next;
node_id_ref_count_list1=node_id_ref_count_list1->nrcl_next;
}
*node_id_ref_count_list_p=NULL;
return node_id_ref_count_list;
}
*/
static NodeIdRefCountListP duplicate_node_id_ref_count_list (NodeIdRefCountListP node_id_ref_count_list)
{
NodeIdRefCountListP node_id_ref_count_elem,new_node_id_ref_count_list,*new_node_id_ref_count_list_p;
new_node_id_ref_count_list_p=&new_node_id_ref_count_list;
for (node_id_ref_count_elem=node_id_ref_count_list; node_id_ref_count_elem!=NULL; node_id_ref_count_elem=node_id_ref_count_elem->nrcl_next){
NodeIdRefCountListP new_node_id_ref_count_elem;
new_node_id_ref_count_elem=new_node_id_ref_count (NULL,node_id_ref_count_elem->nrcl_node_id,node_id_ref_count_elem->nrcl_ref_count);
#if DEBUG_OUTPUT
printf ("duplicate %s %d %d\n",node_id_name (node_id_ref_count_elem->nrcl_node_id),node_id_ref_count_elem->nrcl_node_id->nid_refcount,node_id_ref_count_elem->nrcl_ref_count);
#endif
*new_node_id_ref_count_list_p=new_node_id_ref_count_elem;
new_node_id_ref_count_list_p=&new_node_id_ref_count_elem->nrcl_next;
}
*new_node_id_ref_count_list_p=NULL;
return new_node_id_ref_count_list;
}
#ifdef CLEAN2
extern int contains_fail (NodeP node_p);
#endif
static int determine_failing_cases_and_adjust_ref_counts (NodeP node,NodeIdRefCountListP *node_id_ref_count_list_p)
{
switch (node->node_kind){
case SwitchNode:
{
ArgP arg;
int switch_may_fail,default_may_fail;
int node_id_ref_count_list_sorted;
node_id_ref_count_list_sorted=0;
for (arg=node->node_arguments; arg!=NULL; arg=arg->arg_next)
if (arg->arg_node->node_kind!=CaseNode && arg->arg_node->node_kind!=OverloadedCaseNode)
break;
default_may_fail=1;
if (arg!=NULL){
NodeP arg_node;
arg_node=arg->arg_node;
if (arg_node->node_kind!=DefaultNode)
error_in_function ("determine_failing_cases_and_adjust_ref_counts");
default_may_fail=determine_failing_cases_and_adjust_ref_counts (arg_node->node_arguments->arg_node,node_id_ref_count_list_p);
arg_node->node_number=default_may_fail;
if (default_may_fail){
/* NodeP default_rhs_node; */
sort_node_id_ref_count_lists (&arg_node->node_node_id_ref_counts);
if (!node_id_ref_count_list_sorted){
sort_node_id_ref_count_lists (node_id_ref_count_list_p);
node_id_ref_count_list_sorted=1;
}
/* JVG: maybe incorrect, optimisation: find simple case which can not fail and set node_id_refcounts
default_rhs_node=arg_node->node_arguments->arg_node;
if (default_rhs_node->node_kind==PushNode)
default_rhs_node=default_rhs_node->node_arguments->arg_next->arg_node;
if (default_rhs_node->node_kind==SwitchNode && default_rhs_node->node_arguments->arg_next==NULL)
default_rhs_node->node_arguments->arg_node->node_node_id_ref_counts
= duplicate_node_id_ref_count_list (arg_node->node_node_id_ref_counts);
*/
add_sorted_node_id_ref_count_list (&arg_node->node_node_id_ref_counts,*node_id_ref_count_list_p);
node_id_ref_count_list_p=&arg_node->node_node_id_ref_counts;
/*
arg_node->node_node_id_ref_counts=merge_sorted_node_id_ref_count_lists (arg_node->node_node_id_ref_counts,*node_id_ref_count_list_p);
node_id_ref_count_list_p=&arg_node->node_node_id_ref_counts;
*/
} else
node_id_ref_count_list_p=&arg_node->node_node_id_ref_counts;
}
switch_may_fail=1;
/* to do: if non failing case for every constructor, default not reachable */
#if 1 /* added 8-4-1999 */
if (node->node_arguments->arg_next==NULL && node->node_arguments->arg_node->node_kind==CaseNode
&& (node->node_arguments->arg_node->node_symbol->symb_kind==tuple_symb
|| (node->node_arguments->arg_node->node_symbol->symb_kind==definition &&
node->node_arguments->arg_node->node_symbol->symb_def->sdef_kind==RECORDTYPE)))
{
int case_may_fail;
NodeP arg_node;
arg_node=node->node_arguments->arg_node;
case_may_fail=determine_failing_cases_and_adjust_ref_counts (arg_node->node_arguments->arg_node,node_id_ref_count_list_p);
arg_node->node_number=case_may_fail;
switch_may_fail=case_may_fail;
} else
#endif
for_l (arg,node->node_arguments,arg_next){
NodeP arg_node;
arg_node=arg->arg_node;
switch (arg_node->node_kind){
case OverloadedCaseNode:
arg_node = arg_node->node_node;
/* no break */
case CaseNode:
{
int case_may_fail;
case_may_fail=determine_failing_cases_and_adjust_ref_counts (arg_node->node_arguments->arg_node,node_id_ref_count_list_p);
if (case_may_fail && node->node_arguments->arg_next!=NULL){
/* NodeP case_rhs_node; */
sort_node_id_ref_count_lists (&arg_node->node_node_id_ref_counts);
if (!node_id_ref_count_list_sorted){
sort_node_id_ref_count_lists (node_id_ref_count_list_p);
node_id_ref_count_list_sorted=1;
}
/* JVG: maybe incorrect, optimisation: find simple case which can not fail and set node_id_refcounts
case_rhs_node=arg_node->node_arguments->arg_node;
if (case_rhs_node->node_kind==PushNode)
case_rhs_node=case_rhs_node->node_arguments->arg_next->arg_node;
if (case_rhs_node->node_kind==SwitchNode && case_rhs_node->node_arguments->arg_next==NULL)
case_rhs_node->node_arguments->arg_node->node_node_id_ref_counts
= duplicate_node_id_ref_count_list (arg_node->node_node_id_ref_counts);
*/
/* JVG changed 16-2-2000
add_sorted_node_id_ref_count_list (&arg_node->node_node_id_ref_counts,*node_id_ref_count_list_p);
*/
add_sorted_node_id_ref_count_list_for_case (&arg_node->node_node_id_ref_counts,*node_id_ref_count_list_p);
/**/
/*
arg_node->node_node_id_ref_counts=
merge_sorted_node_id_ref_count_lists (arg_node->node_node_id_ref_counts,*node_id_ref_count_list_p);
*/
}
arg_node->node_number=case_may_fail;
break;
}
case DefaultNode:
switch_may_fail=default_may_fail;
break;
default:
error_in_function ("determine_failing_cases_and_adjust_ref_counts");
}
}
return switch_may_fail;
}
case PushNode:
return determine_failing_cases_and_adjust_ref_counts (node->node_arguments->arg_next->arg_node,node_id_ref_count_list_p);
case GuardNode:
return determine_failing_cases_and_adjust_ref_counts (node->node_arguments->arg_next->arg_node,node_id_ref_count_list_p);
case IfNode:
#ifdef CLEAN2
return contains_fail (node);
#else
{
NodeP else_node;
else_node=node->node_arguments->arg_next->arg_next->arg_node;
while (else_node->node_kind==IfNode)
else_node=else_node->node_arguments->arg_next->arg_next->arg_node;
return else_node->node_kind==NormalNode && else_node->node_symbol->symb_kind==fail_symb;
}
#endif
default:
return False;
}
}
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
void determine_failing_cases_and_adjust_ref_counts_of_rule (RuleAltP first_alt)
{
NodeIdRefCountListP node_id_ref_count_list;
if (first_alt->alt_kind!=Contractum)
return;
node_id_ref_count_list=NULL;
determine_failing_cases_and_adjust_ref_counts (first_alt->alt_rhs_root,&node_id_ref_count_list);
# if 0
PrintRuleAlt (first_alt,4,StdOut);
# endif
}
#endif
#if 0
#include "dbprint.h"
#endif
void transform_patterns_to_case_and_guard_nodes (RuleAltP rule_alts)
{
RuleAltP first_alt;
ArgP arg;
struct root_and_defs_l root_and_defs_l;
NodeP *node_p;
NodeDefP *def_p;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
StrictNodeIdP *strict_node_ids_p;
NodeDefP *end_lhs_defs_p;
#endif
first_alt=rule_alts;
if (first_alt->alt_kind!=Contractum)
return;
node_p=&first_alt->alt_rhs_root;
def_p=&first_alt->alt_rhs_defs;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
strict_node_ids_p=&first_alt->alt_strict_node_ids;
end_lhs_defs_p=&first_alt->alt_lhs_defs;
#endif
root_and_defs_l.root_l=&node_p;
root_and_defs_l.def_l=&def_p;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
root_and_defs_l.strict_node_ids_l=&strict_node_ids_p;
root_and_defs_l.end_lhs_defs_l=&end_lhs_defs_p;
#endif
for_l (arg,first_alt->alt_lhs_root->node_arguments,arg_next)
transform_argument (arg,&root_and_defs_l);
if (first_alt->alt_next!=NULL)
error_in_function ("transform_patterns_to_case_and_guard_nodes");
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
*end_lhs_defs_p=NULL;
#endif
first_alt->alt_next=NULL;
#if 0
PrintRuleAlt (first_alt,4,StdOut);
#endif
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
{
NodeIdRefCountListP node_id_ref_count_list;
node_id_ref_count_list=NULL;
determine_failing_cases_and_adjust_ref_counts (first_alt->alt_rhs_root,&node_id_ref_count_list);
}
# if 0
PrintRuleAlt (first_alt,4,StdOut);
# endif
#endif
}
|