-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
4754 lines (4364 loc) · 165 KB
/
gen.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
/*
FILE NAME: gen.c
Copyright (C) 1997-2016 Vladimir Makarov.
Written by Vladimir Makarov <[email protected]>
This file is part of the tool OKA.
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.
TITLE: generator of OKA (pipeline hazards description translator)
DESCRIPTION: This file generates interface and implementation file of
the pipeline hazards description.
SPECIAL CONSIDERATION:
The generator is to be called after OKA semantic analyzer only
if any error was not fixed.
Defining macro `NDEBUG' (e.g. by option `-D' in C compiler
command line) during the file compilation disables to fix
some internal errors of the generator.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#else /* In this case we are oriented to ANSI C */
#ifndef HAVE_MEMSET
#define HAVE_MEMSET
#endif
#endif /* #ifdef HAVE_CONFIG_H */
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "position.h"
#include "errors.h"
#include "hashtab.h"
#include "bits.h"
#include "ticker.h"
#include "vlobject.h"
#include "common.h"
#include "tab.h"
#include "ird.h"
#include "gen.h"
#include <float.h>
#include <limits.h>
#include <assert.h>
#ifndef HAVE_MEMSET
static void *
memset (void *to, int value, size_t size)
{
char *cto = (char *) to;
while (size > 0)
{
*cto++ = value;
size--;
}
return to;
}
#endif /* #ifndef HAVE_MEMSET */
/* The following variable value is number of automaton which are
really being created. This value is defined on the base of
argument of option `-split'. If the variable has zero value the
number of automatons is defined by the constructions `%automaton'.
This case occures when option `-split' is absent or has zero
argument. If constructions `%automaton' is absent only one
automaton is being created. */
static int automatons_number;
/* The following variable values are times of
transformation of regular expressions
building NDFA
NDFA -> DFA
DFA minimization
building instruction equivalence classes
all previous ones
code output
*/
static ticker_t transformation_time;
static ticker_t NDFA_time;
static ticker_t NDFA_to_DFA_time;
static ticker_t minimize_time;
static ticker_t equivalence_time;
static ticker_t automaton_generation_time;
static ticker_t output_time;
static IR_node_t cycle_advancing_single_instruction_declaration;
static void
add_cycle_advancing_single_instruction_declaration (void)
{
IR_node_t current_single_declaration;
IR_node_t last_single_declaration;
IR_node_t cycle_advancing_identifier;
char *identifier_itself;
last_single_declaration = NULL;
for (current_single_declaration = IR_single_declaration_list (description);
current_single_declaration != NULL;
current_single_declaration
= IR_next_single_declaration (current_single_declaration))
last_single_declaration = current_single_declaration;
IR_TOP_ADD_STRING ("$cycle_advancing");
identifier_itself = insert_identifier (IR_TOP_BEGIN ());
if (identifier_itself == (char *) IR_TOP_BEGIN ())
IR_TOP_FINISH ();
else
IR_TOP_NULLIFY ();
cycle_advancing_identifier
= IR_new_identifier (no_position, identifier_itself, NULL);
cycle_advancing_single_instruction_declaration
= IR_new_single_instruction_declaration
(no_position, cycle_advancing_identifier, NULL);
if (last_single_declaration == NULL)
IR_set_single_declaration_list
(description, cycle_advancing_single_instruction_declaration);
else
IR_set_next_single_declaration
(last_single_declaration,
cycle_advancing_single_instruction_declaration);
IR_set_instruction_number (cycle_advancing_single_instruction_declaration,
IR_instructions_number (description));
IR_set_instructions_number (description,
IR_instructions_number (description) + 1);
}
/* This page contains functions which are used to output pipeline
hazards recognizer (PHR). These function are used to fix output
errors in time. All output of PHR is made only through these
function. */
/* The following variable value is number of current line in the
description file. */
static int current_description_file_line;
/* The following variable value is number of current line in the
interface file. */
static int current_interface_file_line;
/* The following variable value is number of current line in the
implementation file. */
static int current_implementation_file_line;
/* This function outputs string onto interface or implementation file
and fixes output errors. */
static void
output_string (FILE *f, const char *string)
{
for (; *string != '\0'; string++)
{
if (fputc (*string, f) == EOF)
{
if (f == output_description_file)
system_error (TRUE, no_position, "fatal error -- %s: ",
output_description_file_name);
else if (f == output_interface_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_interface_file_name);
else if (f == output_implementation_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_implementation_file_name);
else
assert (f == stderr);
}
if (*string == '\n')
{
if (f == output_description_file)
current_description_file_line++;
else if (f == output_interface_file)
current_interface_file_line++;
else if (f == output_implementation_file)
current_implementation_file_line++;
}
}
}
/* This function outputs character onto interface or implementation
file and fixes output errors. */
static void
output_char (int ch, FILE *f)
{
if (fputc (ch, f) == EOF)
{
if (f == output_description_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_description_file_name);
else if (f == output_interface_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_interface_file_name);
else if (f == output_implementation_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_implementation_file_name);
else
assert (f == stderr);
}
if (ch == '\n')
{
if (f == output_description_file)
current_description_file_line++;
else if (f == output_interface_file)
current_interface_file_line++;
else if (f == output_implementation_file)
current_implementation_file_line++;
}
}
static void
output_decimal_number (FILE *f, int number, int minimum_width)
{
char format [30];
assert (minimum_width >= 0);
sprintf (format, "%%%dd", minimum_width);
if (fprintf (f, format, number) == EOF)
{
if (f == output_description_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_description_file_name);
else if (f == output_interface_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_interface_file_name);
else if (f == output_implementation_file)
system_error (TRUE, no_position, "fatal_error -- %s: ",
output_implementation_file_name);
else
assert (f == stderr);
}
}
/* ??? */
static void
initiate_output (void)
{
current_interface_file_line = 1;
current_implementation_file_line = 1;
if (v_flag)
{
current_description_file_line = 1;
output_description_file = fopen (output_description_file_name, "w");
if (output_description_file == NULL)
system_error (TRUE, no_position,
"fatal error -- %s: ", output_description_file_name);
}
output_interface_file = fopen (output_interface_file_name, "w");
if (output_interface_file == NULL)
system_error (TRUE, no_position,
"fatal error -- %s: ", output_interface_file_name);
output_implementation_file = fopen (output_implementation_file_name, "w");
if (output_implementation_file == NULL)
system_error (TRUE, no_position,
"fatal error -- %s: ", output_implementation_file_name);
}
/* Abstract data `alternative states'. */
static IR_node_t first_free_alternative_state;
#ifndef NDEBUG
static int allocated_alternative_states_number = 0;
#endif
static IR_node_t
get_free_alternative_state (void)
{
IR_node_t result;
if (first_free_alternative_state != NULL)
{
result = first_free_alternative_state;
first_free_alternative_state
= IR_next_alternative_state (first_free_alternative_state);
}
else
{
#ifndef NDEBUG
allocated_alternative_states_number++;
#endif
result = IR_new_alternative_state (NULL, NULL);
}
return result;
}
static void
free_alternative_state (IR_node_t alternative_state)
{
if (alternative_state == NULL)
return;
IR_set_next_alternative_state (alternative_state,
first_free_alternative_state);
first_free_alternative_state = alternative_state;
}
static void
free_alternative_states_list (IR_node_t alternative_states_list)
{
IR_node_t current_alternative_state;
IR_node_t next_alternative_state;
for (current_alternative_state = alternative_states_list;
current_alternative_state != NULL;
current_alternative_state = next_alternative_state)
{
next_alternative_state
= IR_next_alternative_state (current_alternative_state);
free_alternative_state (current_alternative_state);
}
}
static int
alternative_state_comparison (const void *alternative_state_ptr_1,
const void *alternative_state_ptr_2)
{
if (IR_unique_number (IR_state (*(IR_node_t *) alternative_state_ptr_1))
== IR_unique_number (IR_state (*(IR_node_t *) alternative_state_ptr_2)))
return 0;
else if (IR_unique_number (IR_state (*(IR_node_t *) alternative_state_ptr_1))
< IR_unique_number (IR_state
(*(IR_node_t *) alternative_state_ptr_2)))
return -1;
else
return 1;
}
static IR_node_t
sort_and_make_unique_alternative_states_list
(IR_node_t alternative_states_list)
{
IR_node_t current_alternative_state;
vlo_t alternative_states;
int current_index;
int previous_unique_state_index;
IR_node_t result;
IR_node_t *result_ptr;
VLO_CREATE (alternative_states, 0);
for (current_alternative_state = alternative_states_list;
current_alternative_state != NULL;
current_alternative_state
= IR_next_alternative_state (current_alternative_state))
VLO_ADD_MEMORY (alternative_states, ¤t_alternative_state,
sizeof (current_alternative_state));
qsort (VLO_BEGIN (alternative_states),
VLO_LENGTH (alternative_states) / sizeof (IR_node_t),
sizeof (IR_node_t),
alternative_state_comparison);
if (VLO_LENGTH (alternative_states) == 0)
result = NULL;
else
{
result_ptr = (IR_node_t *) VLO_BEGIN (alternative_states);
for (previous_unique_state_index = 0, current_index = 1;
current_index
< VLO_LENGTH (alternative_states) / sizeof (IR_node_t);
current_index++)
if (IR_state (result_ptr [previous_unique_state_index])
!= IR_state (result_ptr [current_index]))
{
previous_unique_state_index++;
result_ptr [previous_unique_state_index]
= result_ptr [current_index];
}
for (current_index = previous_unique_state_index + 1;
current_index
< VLO_LENGTH (alternative_states) / sizeof (IR_node_t);
current_index++)
free_alternative_state (result_ptr [current_index]);
VLO_SHORTEN (alternative_states,
(current_index - previous_unique_state_index - 1)
* sizeof (IR_node_t *));
result_ptr = (IR_node_t *) VLO_BEGIN (alternative_states);
for (current_index = 1;
current_index
< VLO_LENGTH (alternative_states) / sizeof (IR_node_t);
current_index++)
IR_set_next_alternative_state (result_ptr [current_index - 1],
result_ptr [current_index]);
IR_set_next_alternative_state (result_ptr [current_index - 1], NULL);
result = *result_ptr;
}
VLO_DELETE (alternative_states);
return result;
}
/* Remember that the lists must be already sorted. */
static int
alternative_state_list_eq (IR_node_t alternative_state_list_1,
IR_node_t alternative_state_list_2)
{
while (alternative_state_list_1 != NULL && alternative_state_list_2 != NULL
&& alternative_state_comparison (&alternative_state_list_1,
&alternative_state_list_2) == 0)
{
alternative_state_list_1
= IR_next_alternative_state (alternative_state_list_1);
alternative_state_list_2
= IR_next_alternative_state (alternative_state_list_2);
}
return alternative_state_list_1 == alternative_state_list_2;
}
static void
initiate_alternative_states (void)
{
first_free_alternative_state = NULL;
}
static void
finish_alternative_states (void)
{
}
/* This page contains abstract data `state'. */
static int max_cycles_number;
static int elements_in_cycle_reservation;
static int elements_in_reservations;
static vlo_t units_container;
static IR_node_t *units_array;
/* The state table itself is represented by the following variable. */
static hash_table_t state_table;
static vlo_t free_states;
static int current_unique_state_number;
#ifndef NDEBUG
static int allocated_states_number = 0;
#endif
static reservation_sets_list_t
get_empty_reservation_sets_list (void)
{
reservation_sets_list_t result;
IR_TOP_EXPAND (elements_in_reservations
* sizeof (unit_reservation_set_element_t));
result = IR_TOP_BEGIN ();
IR_TOP_FINISH ();
memset (result, 0,
elements_in_reservations * sizeof (unit_reservation_set_element_t));
return result;
}
static unsigned
reservation_sets_list_hash_value (reservation_sets_list_t reservations)
{
unsigned int hash_value;
int unit_reservation_set_elements_number;
unit_reservation_set_element_t *unit_reservation_set_element_ptr;
hash_value = 0;
unit_reservation_set_elements_number = elements_in_reservations;
unit_reservation_set_element_ptr = reservations;
while (unit_reservation_set_elements_number != 0)
{
unit_reservation_set_elements_number--;
hash_value
= ((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
| (hash_value << CHAR_BIT))
+ *unit_reservation_set_element_ptr;
unit_reservation_set_element_ptr++;
}
return hash_value;
}
/* ??? */
static int
reservation_sets_lists_comparison (reservation_sets_list_t reservations_1,
reservation_sets_list_t reservations_2)
{
int unit_reservation_set_elements_number;
unit_reservation_set_element_t *unit_reservation_set_element_ptr_1;
unit_reservation_set_element_t *unit_reservation_set_element_ptr_2;
assert (reservations_1 != NULL && reservations_2 != NULL);
unit_reservation_set_elements_number = elements_in_reservations;
unit_reservation_set_element_ptr_1 = reservations_1;
unit_reservation_set_element_ptr_2 = reservations_2;
while (unit_reservation_set_elements_number != 0
&& (*unit_reservation_set_element_ptr_1
== *unit_reservation_set_element_ptr_2))
{
unit_reservation_set_elements_number--;
unit_reservation_set_element_ptr_1++;
unit_reservation_set_element_ptr_2++;
}
if (unit_reservation_set_elements_number == 0)
return 0;
else if (*unit_reservation_set_element_ptr_1
< *unit_reservation_set_element_ptr_2)
return -1;
else
return 1;
}
static int
reservation_sets_lists_eq (reservation_sets_list_t reservations_1,
reservation_sets_list_t reservations_2)
{
return
reservation_sets_lists_comparison (reservations_1, reservations_2) == 0;
}
static void
set_up_unit_reservation (reservation_sets_list_t reservations,
int cycle_number, int unit_number)
{
assert (cycle_number < max_cycles_number);
SET_BIT (reservations,
cycle_number * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t) * CHAR_BIT
+ unit_number, 1);
}
#if 0
static int
it_is_empty_reservation_sets_list (reservation_sets_list_t operand)
{
unit_reservation_set_element_t *unit_reservation_set_element_ptr;
int unit_reservation_set_elements_number;
assert (operand != NULL);
for (unit_reservation_set_elements_number = elements_in_reservations,
unit_reservation_set_element_ptr = operand;
unit_reservation_set_elements_number != 0;
unit_reservation_set_element_ptr++,
unit_reservation_set_elements_number--)
if (*unit_reservation_set_element_ptr != 0)
return FALSE;
return TRUE;
}
#endif
static reservation_sets_list_t
get_exclusion_set (reservation_sets_list_t in_set);
static int
reservation_sets_lists_are_intersected (reservation_sets_list_t operand_1,
reservation_sets_list_t operand_2)
{
unit_reservation_set_element_t *current_unit_set_element_ptr_1;
unit_reservation_set_element_t *current_unit_set_element_ptr_2;
unit_reservation_set_element_t *current_cycle_ptr_1;
unit_reservation_set_element_t *current_cycle_ptr_2;
assert (operand_1 != NULL && operand_2 != NULL);
for (current_unit_set_element_ptr_1 = operand_1,
current_unit_set_element_ptr_2 = operand_2;
current_unit_set_element_ptr_1 < operand_1 + elements_in_reservations;
current_unit_set_element_ptr_1++, current_unit_set_element_ptr_2++)
if (*current_unit_set_element_ptr_1 & *current_unit_set_element_ptr_2)
return TRUE;
for (current_cycle_ptr_1 = operand_1,
current_cycle_ptr_2 = operand_2;
current_cycle_ptr_1 < operand_1 + elements_in_reservations;
current_cycle_ptr_1 += elements_in_cycle_reservation,
current_cycle_ptr_2 += elements_in_cycle_reservation)
for (current_unit_set_element_ptr_1 = current_cycle_ptr_1,
current_unit_set_element_ptr_2
= get_exclusion_set (current_cycle_ptr_2);
current_unit_set_element_ptr_1
< current_cycle_ptr_1 + elements_in_cycle_reservation;
current_unit_set_element_ptr_1++, current_unit_set_element_ptr_2++)
if (*current_unit_set_element_ptr_1 & *current_unit_set_element_ptr_2)
return TRUE;
return FALSE;
}
static void
reservation_sets_list_shift (reservation_sets_list_t result,
reservation_sets_list_t operand)
{
int i;
assert (result != NULL && operand != NULL && result != operand);
for (i = elements_in_cycle_reservation; i < elements_in_reservations; i++)
result [i - elements_in_cycle_reservation] = operand [i];
}
static void
reservation_sets_list_or (reservation_sets_list_t result,
reservation_sets_list_t operand_1,
reservation_sets_list_t operand_2)
{
unit_reservation_set_element_t *current_unit_set_element_ptr_1;
unit_reservation_set_element_t *current_unit_set_element_ptr_2;
unit_reservation_set_element_t *current_result_unit_set_element_ptr;
assert (result != NULL && operand_1 != NULL && operand_2 != NULL);
for (current_unit_set_element_ptr_1 = operand_1,
current_unit_set_element_ptr_2 = operand_2,
current_result_unit_set_element_ptr = result;
current_unit_set_element_ptr_1 < operand_1 + elements_in_reservations;
current_unit_set_element_ptr_1++, current_unit_set_element_ptr_2++,
current_result_unit_set_element_ptr++)
*current_result_unit_set_element_ptr
= *current_unit_set_element_ptr_1 | *current_unit_set_element_ptr_2;
}
#if 0
static void
reservation_sets_list_and (reservation_sets_list_t result,
reservation_sets_list_t operand_1,
reservation_sets_list_t operand_2)
{
unit_reservation_set_element_t *current_unit_set_element_ptr_1;
unit_reservation_set_element_t *current_unit_set_element_ptr_2;
unit_reservation_set_element_t *current_result_unit_set_element_ptr;
assert (result != NULL && operand_1 != NULL && operand_2 != NULL);
for (current_unit_set_element_ptr_1 = operand_1,
current_unit_set_element_ptr_2 = operand_2,
current_result_unit_set_element_ptr = result;
current_unit_set_element_ptr_1 < operand_1 + elements_in_reservations;
current_unit_set_element_ptr_1++, current_unit_set_element_ptr_2++,
current_result_unit_set_element_ptr++)
*current_result_unit_set_element_ptr
= *current_unit_set_element_ptr_1 & *current_unit_set_element_ptr_2;
}
#endif
static void
output_cycle_reservations (FILE *f, reservation_sets_list_t reservations,
int start_cycle, int repetition_number)
{
int current_unit_number;
int reserved_units_number;
reserved_units_number = 0;
for (current_unit_number = 0;
current_unit_number < IR_units_number (description);
current_unit_number++)
if (BIT (reservations,
start_cycle * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t) * CHAR_BIT
+ current_unit_number))
reserved_units_number++;
assert (repetition_number > 0);
if (repetition_number != 1 && reserved_units_number > 1)
output_char ('(', f);
reserved_units_number = 0;
for (current_unit_number = 0;
current_unit_number < IR_units_number (description);
current_unit_number++)
if (BIT (reservations,
start_cycle * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t) * CHAR_BIT
+ current_unit_number))
{
if (reserved_units_number != 0)
output_char ('+', f);
reserved_units_number++;
output_string (f,
IR_identifier_itself
(IR_identifier (units_array [current_unit_number])));
}
if (reserved_units_number == 0)
output_string (f, "%nothing");
assert (repetition_number > 0);
if (reserved_units_number != 0 && repetition_number != 1)
{
if (reserved_units_number > 1)
output_char (')', f);
output_char ('*', f);
output_decimal_number (f, repetition_number, 0);
}
}
static void
output_reservation_sets_list (FILE *f, reservation_sets_list_t reservations)
{
int start_cycle;
int current_cycle_number;
int repetition_number;
int first_cycle_output_flag;
repetition_number = 0;
first_cycle_output_flag = FALSE;
for (current_cycle_number = 0;
current_cycle_number < max_cycles_number;
current_cycle_number++)
if (repetition_number == 0)
{
repetition_number++;
start_cycle = current_cycle_number;
}
else if (bit_string_comparison
(reservations,
start_cycle * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t) * CHAR_BIT,
reservations,
current_cycle_number * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t) * CHAR_BIT,
IR_units_number (description)) == 0)
repetition_number++;
else
{
if (first_cycle_output_flag)
output_string (f, " ");
output_cycle_reservations (f, reservations, start_cycle,
repetition_number);
first_cycle_output_flag = TRUE;
repetition_number = 1;
start_cycle = current_cycle_number;
}
if (!is_zero_bit_string (reservations,
start_cycle * elements_in_cycle_reservation
* sizeof (unit_reservation_set_element_t)
* CHAR_BIT,
IR_units_number (description))
|| !first_cycle_output_flag)
{
if (first_cycle_output_flag)
output_string (f, " ");
output_cycle_reservations (f, reservations, start_cycle,
repetition_number);
}
}
static IR_node_t
get_free_state (int with_reservations, IR_node_t automaton)
{
IR_node_t result;
assert (max_cycles_number > 0 && automaton != NULL);
if (VLO_LENGTH (free_states) != 0)
{
result
= ((IR_node_t *) VLO_BEGIN (free_states))
[VLO_LENGTH (free_states) / sizeof (IR_node_t) - 1];
VLO_SHORTEN (free_states, sizeof (IR_node_t));
IR_set_automaton (result, automaton);
IR_set_first_out_arc (result, NULL);
IR_set_it_was_placed_in_stack_for_NDFA_forming (result, FALSE);
IR_set_it_was_placed_in_stack_for_DFA_forming (result, FALSE);
IR_set_component_states (result, NULL);
}
else
{
#ifndef NDEBUG
allocated_states_number++;
#endif
result = IR_new_state (NULL, current_unique_state_number, automaton);
current_unique_state_number++;
}
if (with_reservations)
{
if (IR_reservations (result) == NULL)
IR_set_reservations (result, get_empty_reservation_sets_list ());
else
memset
(IR_reservations (result), 0,
elements_in_reservations * sizeof (unit_reservation_set_element_t));
}
return result;
}
static void
free_state (IR_node_t state)
{
free_alternative_states_list (IR_component_states (state));
VLO_ADD_MEMORY (free_states, &state, sizeof (state));
}
static unsigned
state_hash_function (hash_table_entry_t state)
{
unsigned int hash_value;
IR_node_t current_alternative_state;
if (IR_component_states ((IR_node_t) state) == NULL)
hash_value
= reservation_sets_list_hash_value (IR_reservations ((IR_node_t) state));
else
{
hash_value = 0;
for (current_alternative_state = IR_component_states ((IR_node_t) state);
current_alternative_state != NULL;
current_alternative_state
= IR_next_alternative_state (current_alternative_state))
hash_value
= ((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
| (hash_value << CHAR_BIT))
+ IR_unique_number (IR_state (current_alternative_state));
}
hash_value
= ((hash_value >> (sizeof (unsigned) - 1) * CHAR_BIT)
| (hash_value << CHAR_BIT))
+ IR_automaton_order_number (IR_automaton ((IR_node_t) state));
return hash_value;
}
/* ??? */
static int
state_eq_function (hash_table_entry_t state_1, hash_table_entry_t state_2)
{
IR_node_t current_alternative_state_1;
IR_node_t current_alternative_state_2;
if (IR_automaton ((IR_node_t) state_1) != IR_automaton ((IR_node_t) state_2))
return FALSE;
else if (IR_component_states ((IR_node_t) state_1) == NULL
&& IR_component_states ((IR_node_t) state_2) == NULL)
return reservation_sets_lists_eq (IR_reservations ((IR_node_t) state_1),
IR_reservations ((IR_node_t) state_2));
else if (IR_component_states ((IR_node_t) state_1) != NULL
&& IR_component_states ((IR_node_t) state_2) != NULL)
{
for (current_alternative_state_1
= IR_component_states ((IR_node_t) state_1),
current_alternative_state_2
= IR_component_states ((IR_node_t) state_2);
current_alternative_state_1 != NULL
&& current_alternative_state_2 != NULL;
current_alternative_state_1
= IR_next_alternative_state (current_alternative_state_1),
current_alternative_state_2
= IR_next_alternative_state (current_alternative_state_2))
/* All state in the list must be already in the hash table.
Also the lists must be sorted. */
if (IR_state (current_alternative_state_1)
!= IR_state (current_alternative_state_2))
return FALSE;
return current_alternative_state_1 == current_alternative_state_2;
}
else
return FALSE;
}
/* ??? */
static IR_node_t
insert_state (IR_node_t state)
{
hash_table_entry_t *entry_ptr;
entry_ptr
= find_hash_table_entry (state_table, (hash_table_entry_t) state, TRUE);
if (*entry_ptr == NULL)
*entry_ptr = (hash_table_entry_t) state;
return (IR_node_t) *entry_ptr;
}
#ifndef NDEBUG
static IR_node_t
find_state (IR_node_t state)
{
hash_table_entry_t *entry_ptr;
entry_ptr
= find_hash_table_entry (state_table, (hash_table_entry_t) state, FALSE);
return (IR_node_t) *entry_ptr;
}
#endif
static void
set_up_state_reservation (IR_node_t state, int cycle_number, int unit_number)
{
set_up_unit_reservation (IR_reservations (state), cycle_number, unit_number);
}
#if 0
static int
it_is_state_without_reservations (IR_node_t state)
{
return it_is_empty_reservation_sets_list (IR_reservations (state));
}
#endif
static int
state_reservations_are_intersected (IR_node_t state_1, IR_node_t state_2)
{
assert (IR_automaton (state_1) == IR_automaton (state_2));
return reservation_sets_lists_are_intersected (IR_reservations (state_1),
IR_reservations (state_2));
}
static IR_node_t
states_union (IR_node_t state_1, IR_node_t state_2)
{
IR_node_t result;
IR_node_t state_in_table;
assert (IR_automaton (state_1) == IR_automaton (state_2));
result = get_free_state (TRUE, IR_automaton (state_1));
reservation_sets_list_or
(IR_reservations (result),
IR_reservations (state_1), IR_reservations (state_2));
state_in_table = insert_state (result);
if (result != state_in_table)
{
free_state (result);
result = state_in_table;
}
return result;
}
static IR_node_t
state_shift (IR_node_t state)
{
IR_node_t result;
IR_node_t state_in_table;
result = get_free_state (TRUE, IR_automaton (state));
reservation_sets_list_shift (IR_reservations (result),
IR_reservations (state));
state_in_table = insert_state (result);
if (result != state_in_table)
{
free_state (result);
result = state_in_table;
}
return result;
}
/* ??? */
static void
initiate_states (void)
{
IR_node_t current_single_declaration;
VLO_CREATE (units_container, 0);
VLO_EXPAND (units_container,
IR_units_number (description) * sizeof (IR_node_t));
units_array = VLO_BEGIN (units_container);
for (current_single_declaration = IR_single_declaration_list (description);
current_single_declaration != NULL;
current_single_declaration
= IR_next_single_declaration (current_single_declaration))
if (IR_IS_OF_TYPE (current_single_declaration,
IR_NM_single_unit_declaration))
{
units_array [IR_unit_number (current_single_declaration)]
= current_single_declaration;
}
max_cycles_number = IR_max_instruction_reservation_cycles (description);
if (max_cycles_number == 0)
max_cycles_number++;
elements_in_cycle_reservation
= ((IR_units_number (description)
+ sizeof (unit_reservation_set_element_t) * CHAR_BIT - 1)
/ (sizeof (unit_reservation_set_element_t) * CHAR_BIT));
elements_in_reservations = elements_in_cycle_reservation * max_cycles_number;
current_unique_state_number = 0;
initiate_alternative_states ();
VLO_CREATE (free_states, 5000);
state_table = create_hash_table (5000, state_hash_function,
state_eq_function);